OSDN Git Service

* trans-stmt.c (struct temporary_list): Delete.
[pf3gnuchains/gcc-fork.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2    Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
28
29
30 /* This flag is set if an old-style length selector is matched
31    during a type-declaration statement.  */
32
33 static int old_char_selector;
34
35 /* When variables acquire types and attributes from a declaration
36    statement, they get them from the following static variables.  The
37    first part of a declaration sets these variables and the second
38    part copies these into symbol structures.  */
39
40 static gfc_typespec current_ts;
41
42 static symbol_attribute current_attr;
43 static gfc_array_spec *current_as;
44 static int colon_seen;
45
46 /* Initializer of the previous enumerator.  */
47
48 static gfc_expr *last_initializer;
49
50 /* History of all the enumerators is maintained, so that
51    kind values of all the enumerators could be updated depending
52    upon the maximum initialized value.  */
53
54 typedef struct enumerator_history
55 {
56   gfc_symbol *sym;
57   gfc_expr *initializer;
58   struct enumerator_history *next;
59 }
60 enumerator_history;
61
62 /* Header of enum history chain.  */
63
64 static enumerator_history *enum_history = NULL;
65
66 /* Pointer of enum history node containing largest initializer.  */
67
68 static enumerator_history *max_enum = NULL;
69
70 /* gfc_new_block points to the symbol of a newly matched block.  */
71
72 gfc_symbol *gfc_new_block;
73
74
75 /********************* DATA statement subroutines *********************/
76
77 /* Free a gfc_data_variable structure and everything beneath it.  */
78
79 static void
80 free_variable (gfc_data_variable * p)
81 {
82   gfc_data_variable *q;
83
84   for (; p; p = q)
85     {
86       q = p->next;
87       gfc_free_expr (p->expr);
88       gfc_free_iterator (&p->iter, 0);
89       free_variable (p->list);
90
91       gfc_free (p);
92     }
93 }
94
95
96 /* Free a gfc_data_value structure and everything beneath it.  */
97
98 static void
99 free_value (gfc_data_value * p)
100 {
101   gfc_data_value *q;
102
103   for (; p; p = q)
104     {
105       q = p->next;
106       gfc_free_expr (p->expr);
107       gfc_free (p);
108     }
109 }
110
111
112 /* Free a list of gfc_data structures.  */
113
114 void
115 gfc_free_data (gfc_data * p)
116 {
117   gfc_data *q;
118
119   for (; p; p = q)
120     {
121       q = p->next;
122
123       free_variable (p->var);
124       free_value (p->value);
125
126       gfc_free (p);
127     }
128 }
129
130
131 static match var_element (gfc_data_variable *);
132
133 /* Match a list of variables terminated by an iterator and a right
134    parenthesis.  */
135
136 static match
137 var_list (gfc_data_variable * parent)
138 {
139   gfc_data_variable *tail, var;
140   match m;
141
142   m = var_element (&var);
143   if (m == MATCH_ERROR)
144     return MATCH_ERROR;
145   if (m == MATCH_NO)
146     goto syntax;
147
148   tail = gfc_get_data_variable ();
149   *tail = var;
150
151   parent->list = tail;
152
153   for (;;)
154     {
155       if (gfc_match_char (',') != MATCH_YES)
156         goto syntax;
157
158       m = gfc_match_iterator (&parent->iter, 1);
159       if (m == MATCH_YES)
160         break;
161       if (m == MATCH_ERROR)
162         return MATCH_ERROR;
163
164       m = var_element (&var);
165       if (m == MATCH_ERROR)
166         return MATCH_ERROR;
167       if (m == MATCH_NO)
168         goto syntax;
169
170       tail->next = gfc_get_data_variable ();
171       tail = tail->next;
172
173       *tail = var;
174     }
175
176   if (gfc_match_char (')') != MATCH_YES)
177     goto syntax;
178   return MATCH_YES;
179
180 syntax:
181   gfc_syntax_error (ST_DATA);
182   return MATCH_ERROR;
183 }
184
185
186 /* Match a single element in a data variable list, which can be a
187    variable-iterator list.  */
188
189 static match
190 var_element (gfc_data_variable * new)
191 {
192   match m;
193   gfc_symbol *sym;
194
195   memset (new, 0, sizeof (gfc_data_variable));
196
197   if (gfc_match_char ('(') == MATCH_YES)
198     return var_list (new);
199
200   m = gfc_match_variable (&new->expr, 0);
201   if (m != MATCH_YES)
202     return m;
203
204   sym = new->expr->symtree->n.sym;
205
206   if (!sym->attr.function && gfc_current_ns->parent && gfc_current_ns->parent == sym->ns)
207     {
208       gfc_error ("Host associated variable '%s' may not be in the DATA "
209                  "statement at %C.", sym->name);
210       return MATCH_ERROR;
211     }
212
213   if (gfc_current_state () != COMP_BLOCK_DATA
214         && sym->attr.in_common
215         && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
216                            "common block variable '%s' in DATA statement at %C",
217                            sym->name) == FAILURE)
218     return MATCH_ERROR;
219
220   if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
221     return MATCH_ERROR;
222
223   return MATCH_YES;
224 }
225
226
227 /* Match the top-level list of data variables.  */
228
229 static match
230 top_var_list (gfc_data * d)
231 {
232   gfc_data_variable var, *tail, *new;
233   match m;
234
235   tail = NULL;
236
237   for (;;)
238     {
239       m = var_element (&var);
240       if (m == MATCH_NO)
241         goto syntax;
242       if (m == MATCH_ERROR)
243         return MATCH_ERROR;
244
245       new = gfc_get_data_variable ();
246       *new = var;
247
248       if (tail == NULL)
249         d->var = new;
250       else
251         tail->next = new;
252
253       tail = new;
254
255       if (gfc_match_char ('/') == MATCH_YES)
256         break;
257       if (gfc_match_char (',') != MATCH_YES)
258         goto syntax;
259     }
260
261   return MATCH_YES;
262
263 syntax:
264   gfc_syntax_error (ST_DATA);
265   return MATCH_ERROR;
266 }
267
268
269 static match
270 match_data_constant (gfc_expr ** result)
271 {
272   char name[GFC_MAX_SYMBOL_LEN + 1];
273   gfc_symbol *sym;
274   gfc_expr *expr;
275   match m;
276
277   m = gfc_match_literal_constant (&expr, 1);
278   if (m == MATCH_YES)
279     {
280       *result = expr;
281       return MATCH_YES;
282     }
283
284   if (m == MATCH_ERROR)
285     return MATCH_ERROR;
286
287   m = gfc_match_null (result);
288   if (m != MATCH_NO)
289     return m;
290
291   m = gfc_match_name (name);
292   if (m != MATCH_YES)
293     return m;
294
295   if (gfc_find_symbol (name, NULL, 1, &sym))
296     return MATCH_ERROR;
297
298   if (sym == NULL
299       || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
300     {
301       gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
302                  name);
303       return MATCH_ERROR;
304     }
305   else if (sym->attr.flavor == FL_DERIVED)
306     return gfc_match_structure_constructor (sym, result);
307
308   *result = gfc_copy_expr (sym->value);
309   return MATCH_YES;
310 }
311
312
313 /* Match a list of values in a DATA statement.  The leading '/' has
314    already been seen at this point.  */
315
316 static match
317 top_val_list (gfc_data * data)
318 {
319   gfc_data_value *new, *tail;
320   gfc_expr *expr;
321   const char *msg;
322   match m;
323
324   tail = NULL;
325
326   for (;;)
327     {
328       m = match_data_constant (&expr);
329       if (m == MATCH_NO)
330         goto syntax;
331       if (m == MATCH_ERROR)
332         return MATCH_ERROR;
333
334       new = gfc_get_data_value ();
335
336       if (tail == NULL)
337         data->value = new;
338       else
339         tail->next = new;
340
341       tail = new;
342
343       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
344         {
345           tail->expr = expr;
346           tail->repeat = 1;
347         }
348       else
349         {
350           signed int tmp;
351           msg = gfc_extract_int (expr, &tmp);
352           gfc_free_expr (expr);
353           if (msg != NULL)
354             {
355               gfc_error (msg);
356               return MATCH_ERROR;
357             }
358           tail->repeat = tmp;
359
360           m = match_data_constant (&tail->expr);
361           if (m == MATCH_NO)
362             goto syntax;
363           if (m == MATCH_ERROR)
364             return MATCH_ERROR;
365         }
366
367       if (gfc_match_char ('/') == MATCH_YES)
368         break;
369       if (gfc_match_char (',') == MATCH_NO)
370         goto syntax;
371     }
372
373   return MATCH_YES;
374
375 syntax:
376   gfc_syntax_error (ST_DATA);
377   return MATCH_ERROR;
378 }
379
380
381 /* Matches an old style initialization.  */
382
383 static match
384 match_old_style_init (const char *name)
385 {
386   match m;
387   gfc_symtree *st;
388   gfc_data *newdata;
389
390   /* Set up data structure to hold initializers.  */
391   gfc_find_sym_tree (name, NULL, 0, &st);
392           
393   newdata = gfc_get_data ();
394   newdata->var = gfc_get_data_variable ();
395   newdata->var->expr = gfc_get_variable_expr (st);
396
397   /* Match initial value list. This also eats the terminal
398      '/'.  */
399   m = top_val_list (newdata);
400   if (m != MATCH_YES)
401     {
402       gfc_free (newdata);
403       return m;
404     }
405
406   if (gfc_pure (NULL))
407     {
408       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
409       gfc_free (newdata);
410       return MATCH_ERROR;
411     }
412
413   /* Chain in namespace list of DATA initializers.  */
414   newdata->next = gfc_current_ns->data;
415   gfc_current_ns->data = newdata;
416
417   return m;
418 }
419
420 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
421    we are matching a DATA statement and are therefore issuing an error
422    if we encounter something unexpected, if not, we're trying to match 
423    an old-style initialization expression of the form INTEGER I /2/.  */
424
425 match
426 gfc_match_data (void)
427 {
428   gfc_data *new;
429   match m;
430
431   for (;;)
432     {
433       new = gfc_get_data ();
434       new->where = gfc_current_locus;
435
436       m = top_var_list (new);
437       if (m != MATCH_YES)
438         goto cleanup;
439
440       m = top_val_list (new);
441       if (m != MATCH_YES)
442         goto cleanup;
443
444       new->next = gfc_current_ns->data;
445       gfc_current_ns->data = new;
446
447       if (gfc_match_eos () == MATCH_YES)
448         break;
449
450       gfc_match_char (',');     /* Optional comma */
451     }
452
453   if (gfc_pure (NULL))
454     {
455       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
456       return MATCH_ERROR;
457     }
458
459   return MATCH_YES;
460
461 cleanup:
462   gfc_free_data (new);
463   return MATCH_ERROR;
464 }
465
466
467 /************************ Declaration statements *********************/
468
469 /* Match an intent specification.  Since this can only happen after an
470    INTENT word, a legal intent-spec must follow.  */
471
472 static sym_intent
473 match_intent_spec (void)
474 {
475
476   if (gfc_match (" ( in out )") == MATCH_YES)
477     return INTENT_INOUT;
478   if (gfc_match (" ( in )") == MATCH_YES)
479     return INTENT_IN;
480   if (gfc_match (" ( out )") == MATCH_YES)
481     return INTENT_OUT;
482
483   gfc_error ("Bad INTENT specification at %C");
484   return INTENT_UNKNOWN;
485 }
486
487
488 /* Matches a character length specification, which is either a
489    specification expression or a '*'.  */
490
491 static match
492 char_len_param_value (gfc_expr ** expr)
493 {
494
495   if (gfc_match_char ('*') == MATCH_YES)
496     {
497       *expr = NULL;
498       return MATCH_YES;
499     }
500
501   return gfc_match_expr (expr);
502 }
503
504
505 /* A character length is a '*' followed by a literal integer or a
506    char_len_param_value in parenthesis.  */
507
508 static match
509 match_char_length (gfc_expr ** expr)
510 {
511   int length;
512   match m;
513
514   m = gfc_match_char ('*');
515   if (m != MATCH_YES)
516     return m;
517
518   m = gfc_match_small_literal_int (&length, NULL);
519   if (m == MATCH_ERROR)
520     return m;
521
522   if (m == MATCH_YES)
523     {
524       *expr = gfc_int_expr (length);
525       return m;
526     }
527
528   if (gfc_match_char ('(') == MATCH_NO)
529     goto syntax;
530
531   m = char_len_param_value (expr);
532   if (m == MATCH_ERROR)
533     return m;
534   if (m == MATCH_NO)
535     goto syntax;
536
537   if (gfc_match_char (')') == MATCH_NO)
538     {
539       gfc_free_expr (*expr);
540       *expr = NULL;
541       goto syntax;
542     }
543
544   return MATCH_YES;
545
546 syntax:
547   gfc_error ("Syntax error in character length specification at %C");
548   return MATCH_ERROR;
549 }
550
551
552 /* Special subroutine for finding a symbol.  Check if the name is found
553    in the current name space.  If not, and we're compiling a function or
554    subroutine and the parent compilation unit is an interface, then check
555    to see if the name we've been given is the name of the interface
556    (located in another namespace).  */
557
558 static int
559 find_special (const char *name, gfc_symbol ** result)
560 {
561   gfc_state_data *s;
562   int i;
563
564   i = gfc_get_symbol (name, NULL, result);
565   if (i==0) 
566     goto end;
567   
568   if (gfc_current_state () != COMP_SUBROUTINE
569       && gfc_current_state () != COMP_FUNCTION)
570     goto end;
571
572   s = gfc_state_stack->previous;
573   if (s == NULL)
574     goto end;
575
576   if (s->state != COMP_INTERFACE)
577     goto end;
578   if (s->sym == NULL)
579     goto end;                  /* Nameless interface */
580
581   if (strcmp (name, s->sym->name) == 0)
582     {
583       *result = s->sym;
584       return 0;
585     }
586
587 end:
588   return i;
589 }
590
591
592 /* Special subroutine for getting a symbol node associated with a
593    procedure name, used in SUBROUTINE and FUNCTION statements.  The
594    symbol is created in the parent using with symtree node in the
595    child unit pointing to the symbol.  If the current namespace has no
596    parent, then the symbol is just created in the current unit.  */
597
598 static int
599 get_proc_name (const char *name, gfc_symbol ** result)
600 {
601   gfc_symtree *st;
602   gfc_symbol *sym;
603   int rc;
604
605   if (gfc_current_ns->parent == NULL)
606     rc = gfc_get_symbol (name, NULL, result);
607   else
608     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
609
610   sym = *result;
611
612   if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
613     {
614       /* Trap another encompassed procedure with the same name.  All
615          these conditions are necessary to avoid picking up an entry
616          whose name clashes with that of the encompassing procedure;
617          this is handled using gsymbols to register unique,globally
618          accessible names.  */
619       if (sym->attr.flavor != 0
620             && sym->attr.proc != 0
621             && sym->formal)
622         gfc_error_now ("Procedure '%s' at %C is already defined at %L",
623                        name, &sym->declared_at);
624
625       /* Trap declarations of attributes in encompassing scope.  The
626          signature for this is that ts.kind is set.  Legitimate
627          references only set ts.type.  */
628       if (sym->ts.kind != 0
629             && sym->attr.proc == 0
630             && gfc_current_ns->parent != NULL
631             && sym->attr.access == 0)
632         gfc_error_now ("Procedure '%s' at %C has an explicit interface"
633                        " and must not have attributes declared at %L",
634                        name, &sym->declared_at);
635     }
636
637   if (gfc_current_ns->parent == NULL || *result == NULL)
638     return rc;
639
640   st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
641
642   st->n.sym = sym;
643   sym->refs++;
644
645   /* See if the procedure should be a module procedure */
646
647   if (sym->ns->proc_name != NULL
648       && sym->ns->proc_name->attr.flavor == FL_MODULE
649       && sym->attr.proc != PROC_MODULE
650       && gfc_add_procedure (&sym->attr, PROC_MODULE,
651                             sym->name, NULL) == FAILURE)
652     rc = 2;
653
654   return rc;
655 }
656
657
658 /* Function called by variable_decl() that adds a name to the symbol
659    table.  */
660
661 static try
662 build_sym (const char *name, gfc_charlen * cl,
663            gfc_array_spec ** as, locus * var_locus)
664 {
665   symbol_attribute attr;
666   gfc_symbol *sym;
667
668   /* if (find_special (name, &sym)) */
669   if (gfc_get_symbol (name, NULL, &sym))
670     return FAILURE;
671
672   /* Start updating the symbol table.  Add basic type attribute
673      if present.  */
674   if (current_ts.type != BT_UNKNOWN
675       &&(sym->attr.implicit_type == 0
676          || !gfc_compare_types (&sym->ts, &current_ts))
677       && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
678     return FAILURE;
679
680   if (sym->ts.type == BT_CHARACTER)
681     sym->ts.cl = cl;
682
683   /* Add dimension attribute if present.  */
684   if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
685     return FAILURE;
686   *as = NULL;
687
688   /* Add attribute to symbol.  The copy is so that we can reset the
689      dimension attribute.  */
690   attr = current_attr;
691   attr.dimension = 0;
692
693   if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
694     return FAILURE;
695
696   return SUCCESS;
697 }
698
699 /* Set character constant to the given length. The constant will be padded or
700    truncated.  */
701
702 void
703 gfc_set_constant_character_len (int len, gfc_expr * expr)
704 {
705   char * s;
706   int slen;
707
708   gcc_assert (expr->expr_type == EXPR_CONSTANT);
709   gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
710
711   slen = expr->value.character.length;
712   if (len != slen)
713     {
714       s = gfc_getmem (len);
715       memcpy (s, expr->value.character.string, MIN (len, slen));
716       if (len > slen)
717         memset (&s[slen], ' ', len - slen);
718       gfc_free (expr->value.character.string);
719       expr->value.character.string = s;
720       expr->value.character.length = len;
721     }
722 }
723
724
725 /* Function to create and update the enumerator history 
726    using the information passed as arguments.
727    Pointer "max_enum" is also updated, to point to 
728    enum history node containing largest initializer.  
729
730    SYM points to the symbol node of enumerator.
731    INIT points to its enumerator value.   */
732
733 static void 
734 create_enum_history(gfc_symbol *sym, gfc_expr *init)
735 {
736   enumerator_history *new_enum_history;
737   gcc_assert (sym != NULL && init != NULL);
738
739   new_enum_history = gfc_getmem (sizeof (enumerator_history));
740
741   new_enum_history->sym = sym;
742   new_enum_history->initializer = init;
743   new_enum_history->next = NULL;
744
745   if (enum_history == NULL)
746     {
747       enum_history = new_enum_history;
748       max_enum = enum_history;
749     }
750   else
751     {
752       new_enum_history->next = enum_history;
753       enum_history = new_enum_history;
754
755       if (mpz_cmp (max_enum->initializer->value.integer, 
756                    new_enum_history->initializer->value.integer) < 0)
757         max_enum = new_enum_history;
758     }
759 }
760
761
762 /* Function to free enum kind history.  */ 
763
764 void 
765 gfc_free_enum_history(void)
766 {
767   enumerator_history *current = enum_history;  
768   enumerator_history *next;  
769
770   while (current != NULL)
771     {
772       next = current->next;
773       gfc_free (current);
774       current = next;
775     }
776   max_enum = NULL;
777   enum_history = NULL;
778 }
779
780
781 /* Function called by variable_decl() that adds an initialization
782    expression to a symbol.  */
783
784 static try
785 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
786                       locus * var_locus)
787 {
788   symbol_attribute attr;
789   gfc_symbol *sym;
790   gfc_expr *init;
791
792   init = *initp;
793   if (find_special (name, &sym))
794     return FAILURE;
795
796   attr = sym->attr;
797
798   /* If this symbol is confirming an implicit parameter type,
799      then an initialization expression is not allowed.  */
800   if (attr.flavor == FL_PARAMETER
801       && sym->value != NULL
802       && *initp != NULL)
803     {
804       gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
805                  sym->name);
806       return FAILURE;
807     }
808
809   if (attr.in_common
810       && !attr.data
811       && *initp != NULL)
812     {
813       gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
814                  sym->name);
815       return FAILURE;
816     }
817
818   if (init == NULL)
819     {
820       /* An initializer is required for PARAMETER declarations.  */
821       if (attr.flavor == FL_PARAMETER)
822         {
823           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
824           return FAILURE;
825         }
826     }
827   else
828     {
829       /* If a variable appears in a DATA block, it cannot have an
830          initializer.  */
831       if (sym->attr.data)
832         {
833           gfc_error
834             ("Variable '%s' at %C with an initializer already appears "
835              "in a DATA statement", sym->name);
836           return FAILURE;
837         }
838
839       /* Check if the assignment can happen. This has to be put off
840          until later for a derived type variable.  */
841       if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
842           && gfc_check_assign_symbol (sym, init) == FAILURE)
843         return FAILURE;
844
845       if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
846         {
847           /* Update symbol character length according initializer.  */
848           if (sym->ts.cl->length == NULL)
849             {
850               /* If there are multiple CHARACTER variables declared on
851                  the same line, we don't want them to share the same
852                 length.  */
853               sym->ts.cl = gfc_get_charlen ();
854               sym->ts.cl->next = gfc_current_ns->cl_list;
855               gfc_current_ns->cl_list = sym->ts.cl;
856
857               if (init->expr_type == EXPR_CONSTANT)
858                 sym->ts.cl->length =
859                         gfc_int_expr (init->value.character.length);
860               else if (init->expr_type == EXPR_ARRAY)
861                 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
862             }
863           /* Update initializer character length according symbol.  */
864           else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
865             {
866               int len = mpz_get_si (sym->ts.cl->length->value.integer);
867               gfc_constructor * p;
868
869               if (init->expr_type == EXPR_CONSTANT)
870                 gfc_set_constant_character_len (len, init);
871               else if (init->expr_type == EXPR_ARRAY)
872                 {
873                   gfc_free_expr (init->ts.cl->length);
874                   init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
875                   for (p = init->value.constructor; p; p = p->next)
876                     gfc_set_constant_character_len (len, p->expr);
877                 }
878             }
879         }
880
881       /* Add initializer.  Make sure we keep the ranks sane.  */
882       if (sym->attr.dimension && init->rank == 0)
883         init->rank = sym->as->rank;
884
885       sym->value = init;
886       *initp = NULL;
887     }
888
889   /* Maintain enumerator history.  */
890   if (gfc_current_state () == COMP_ENUM)
891     create_enum_history (sym, init);
892
893   return SUCCESS;
894 }
895
896
897 /* Function called by variable_decl() that adds a name to a structure
898    being built.  */
899
900 static try
901 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
902               gfc_array_spec ** as)
903 {
904   gfc_component *c;
905
906   /* If the current symbol is of the same derived type that we're
907      constructing, it must have the pointer attribute.  */
908   if (current_ts.type == BT_DERIVED
909       && current_ts.derived == gfc_current_block ()
910       && current_attr.pointer == 0)
911     {
912       gfc_error ("Component at %C must have the POINTER attribute");
913       return FAILURE;
914     }
915
916   if (gfc_current_block ()->attr.pointer
917       && (*as)->rank != 0)
918     {
919       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
920         {
921           gfc_error ("Array component of structure at %C must have explicit "
922                      "or deferred shape");
923           return FAILURE;
924         }
925     }
926
927   if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
928     return FAILURE;
929
930   c->ts = current_ts;
931   c->ts.cl = cl;
932   gfc_set_component_attr (c, &current_attr);
933
934   c->initializer = *init;
935   *init = NULL;
936
937   c->as = *as;
938   if (c->as != NULL)
939     c->dimension = 1;
940   *as = NULL;
941
942   /* Check array components.  */
943   if (!c->dimension)
944     return SUCCESS;
945
946   if (c->pointer)
947     {
948       if (c->as->type != AS_DEFERRED)
949         {
950           gfc_error ("Pointer array component of structure at %C "
951                      "must have a deferred shape");
952           return FAILURE;
953         }
954     }
955   else
956     {
957       if (c->as->type != AS_EXPLICIT)
958         {
959           gfc_error
960             ("Array component of structure at %C must have an explicit "
961              "shape");
962           return FAILURE;
963         }
964     }
965
966   return SUCCESS;
967 }
968
969
970 /* Match a 'NULL()', and possibly take care of some side effects.  */
971
972 match
973 gfc_match_null (gfc_expr ** result)
974 {
975   gfc_symbol *sym;
976   gfc_expr *e;
977   match m;
978
979   m = gfc_match (" null ( )");
980   if (m != MATCH_YES)
981     return m;
982
983   /* The NULL symbol now has to be/become an intrinsic function.  */
984   if (gfc_get_symbol ("null", NULL, &sym))
985     {
986       gfc_error ("NULL() initialization at %C is ambiguous");
987       return MATCH_ERROR;
988     }
989
990   gfc_intrinsic_symbol (sym);
991
992   if (sym->attr.proc != PROC_INTRINSIC
993       && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
994                              sym->name, NULL) == FAILURE
995           || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
996     return MATCH_ERROR;
997
998   e = gfc_get_expr ();
999   e->where = gfc_current_locus;
1000   e->expr_type = EXPR_NULL;
1001   e->ts.type = BT_UNKNOWN;
1002
1003   *result = e;
1004
1005   return MATCH_YES;
1006 }
1007
1008
1009 /* Match a variable name with an optional initializer.  When this
1010    subroutine is called, a variable is expected to be parsed next.
1011    Depending on what is happening at the moment, updates either the
1012    symbol table or the current interface.  */
1013
1014 static match
1015 variable_decl (int elem)
1016 {
1017   char name[GFC_MAX_SYMBOL_LEN + 1];
1018   gfc_expr *initializer, *char_len;
1019   gfc_array_spec *as;
1020   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1021   gfc_charlen *cl;
1022   locus var_locus;
1023   match m;
1024   try t;
1025   gfc_symbol *sym;
1026   locus old_locus;
1027
1028   initializer = NULL;
1029   as = NULL;
1030   cp_as = NULL;
1031   old_locus = gfc_current_locus;
1032
1033   /* When we get here, we've just matched a list of attributes and
1034      maybe a type and a double colon.  The next thing we expect to see
1035      is the name of the symbol.  */
1036   m = gfc_match_name (name);
1037   if (m != MATCH_YES)
1038     goto cleanup;
1039
1040   var_locus = gfc_current_locus;
1041
1042   /* Now we could see the optional array spec. or character length.  */
1043   m = gfc_match_array_spec (&as);
1044   if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1045     cp_as = gfc_copy_array_spec (as);
1046   else if (m == MATCH_ERROR)
1047     goto cleanup;
1048
1049   if (m == MATCH_NO)
1050     as = gfc_copy_array_spec (current_as);
1051   else if (gfc_current_state () == COMP_ENUM)
1052     {
1053       gfc_error ("Enumerator cannot be array at %C");
1054       gfc_free_enum_history ();
1055       m = MATCH_ERROR;
1056       goto cleanup;
1057     }
1058
1059
1060   char_len = NULL;
1061   cl = NULL;
1062
1063   if (current_ts.type == BT_CHARACTER)
1064     {
1065       switch (match_char_length (&char_len))
1066         {
1067         case MATCH_YES:
1068           cl = gfc_get_charlen ();
1069           cl->next = gfc_current_ns->cl_list;
1070           gfc_current_ns->cl_list = cl;
1071
1072           cl->length = char_len;
1073           break;
1074
1075         /* Non-constant lengths need to be copied after the first
1076            element.  */
1077         case MATCH_NO:
1078           if (elem > 1 && current_ts.cl->length
1079                 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1080             {
1081               cl = gfc_get_charlen ();
1082               cl->next = gfc_current_ns->cl_list;
1083               gfc_current_ns->cl_list = cl;
1084               cl->length = gfc_copy_expr (current_ts.cl->length);
1085             }
1086           else
1087             cl = current_ts.cl;
1088
1089           break;
1090
1091         case MATCH_ERROR:
1092           goto cleanup;
1093         }
1094     }
1095
1096   /*  If this symbol has already shown up in a Cray Pointer declaration,
1097       then we want to set the type & bail out. */
1098   if (gfc_option.flag_cray_pointer)
1099     {
1100       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1101       if (sym != NULL && sym->attr.cray_pointee)
1102         {
1103           sym->ts.type = current_ts.type;
1104           sym->ts.kind = current_ts.kind;
1105           sym->ts.cl = cl;
1106           sym->ts.derived = current_ts.derived;
1107           m = MATCH_YES;
1108         
1109           /* Check to see if we have an array specification.  */
1110           if (cp_as != NULL)
1111             {
1112               if (sym->as != NULL)
1113                 {
1114                   gfc_error ("Duplicate array spec for Cray pointee at %C.");
1115                   gfc_free_array_spec (cp_as);
1116                   m = MATCH_ERROR;
1117                   goto cleanup;
1118                 }
1119               else
1120                 {
1121                   if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1122                     gfc_internal_error ("Couldn't set pointee array spec.");
1123               
1124                   /* Fix the array spec.  */
1125                   m = gfc_mod_pointee_as (sym->as);  
1126                   if (m == MATCH_ERROR)
1127                     goto cleanup;
1128                 }
1129             }     
1130           goto cleanup;
1131         }
1132       else
1133         {
1134           gfc_free_array_spec (cp_as);
1135         }
1136     }
1137   
1138     
1139   /* OK, we've successfully matched the declaration.  Now put the
1140      symbol in the current namespace, because it might be used in the
1141      optional initialization expression for this symbol, e.g. this is
1142      perfectly legal:
1143
1144      integer, parameter :: i = huge(i)
1145
1146      This is only true for parameters or variables of a basic type.
1147      For components of derived types, it is not true, so we don't
1148      create a symbol for those yet.  If we fail to create the symbol,
1149      bail out.  */
1150   if (gfc_current_state () != COMP_DERIVED
1151       && build_sym (name, cl, &as, &var_locus) == FAILURE)
1152     {
1153       m = MATCH_ERROR;
1154       goto cleanup;
1155     }
1156
1157   /* In functions that have a RESULT variable defined, the function
1158      name always refers to function calls.  Therefore, the name is
1159      not allowed to appear in specification statements.  */
1160   if (gfc_current_state () == COMP_FUNCTION
1161       && gfc_current_block () != NULL
1162       && gfc_current_block ()->result != NULL
1163       && gfc_current_block ()->result != gfc_current_block ()
1164       && strcmp (gfc_current_block ()->name, name) == 0)
1165     {
1166       gfc_error ("Function name '%s' not allowed at %C", name);
1167       m = MATCH_ERROR;
1168       goto cleanup;
1169     }
1170
1171   /* We allow old-style initializations of the form
1172        integer i /2/, j(4) /3*3, 1/
1173      (if no colon has been seen). These are different from data
1174      statements in that initializers are only allowed to apply to the
1175      variable immediately preceding, i.e.
1176        integer i, j /1, 2/
1177      is not allowed. Therefore we have to do some work manually, that
1178      could otherwise be left to the matchers for DATA statements.  */
1179
1180   if (!colon_seen && gfc_match (" /") == MATCH_YES)
1181     {
1182       if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1183                           "initialization at %C") == FAILURE)
1184         return MATCH_ERROR;
1185      
1186       return match_old_style_init (name);
1187     }
1188
1189   /* The double colon must be present in order to have initializers.
1190      Otherwise the statement is ambiguous with an assignment statement.  */
1191   if (colon_seen)
1192     {
1193       if (gfc_match (" =>") == MATCH_YES)
1194         {
1195
1196           if (!current_attr.pointer)
1197             {
1198               gfc_error ("Initialization at %C isn't for a pointer variable");
1199               m = MATCH_ERROR;
1200               goto cleanup;
1201             }
1202
1203           m = gfc_match_null (&initializer);
1204           if (m == MATCH_NO)
1205             {
1206               gfc_error ("Pointer initialization requires a NULL() at %C");
1207               m = MATCH_ERROR;
1208             }
1209
1210           if (gfc_pure (NULL))
1211             {
1212               gfc_error
1213                 ("Initialization of pointer at %C is not allowed in a "
1214                  "PURE procedure");
1215               m = MATCH_ERROR;
1216             }
1217
1218           if (m != MATCH_YES)
1219             goto cleanup;
1220
1221         }
1222       else if (gfc_match_char ('=') == MATCH_YES)
1223         {
1224           if (current_attr.pointer)
1225             {
1226               gfc_error
1227                 ("Pointer initialization at %C requires '=>', not '='");
1228               m = MATCH_ERROR;
1229               goto cleanup;
1230             }
1231
1232           m = gfc_match_init_expr (&initializer);
1233           if (m == MATCH_NO)
1234             {
1235               gfc_error ("Expected an initialization expression at %C");
1236               m = MATCH_ERROR;
1237             }
1238
1239           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1240             {
1241               gfc_error
1242                 ("Initialization of variable at %C is not allowed in a "
1243                  "PURE procedure");
1244               m = MATCH_ERROR;
1245             }
1246
1247           if (m != MATCH_YES)
1248             goto cleanup;
1249         }
1250     }
1251
1252   /* Check if we are parsing an enumeration and if the current enumerator
1253      variable has an initializer or not. If it does not have an
1254      initializer, the initialization value of the previous enumerator 
1255      (stored in last_initializer) is incremented by 1 and is used to
1256      initialize the current enumerator.  */
1257   if (gfc_current_state () == COMP_ENUM)
1258     {
1259       if (initializer == NULL)
1260         initializer = gfc_enum_initializer (last_initializer, old_locus);
1261  
1262       if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1263         {
1264           gfc_error("ENUMERATOR %L not initialized with integer expression",
1265                     &var_locus);
1266           m = MATCH_ERROR; 
1267           gfc_free_enum_history ();
1268           goto cleanup;
1269         }
1270
1271       /* Store this current initializer, for the next enumerator
1272          variable to be parsed.  */
1273       last_initializer = initializer;
1274     }
1275
1276   /* Add the initializer.  Note that it is fine if initializer is
1277      NULL here, because we sometimes also need to check if a
1278      declaration *must* have an initialization expression.  */
1279   if (gfc_current_state () != COMP_DERIVED)
1280     t = add_init_expr_to_sym (name, &initializer, &var_locus);
1281   else
1282     {
1283       if (current_ts.type == BT_DERIVED && !current_attr.pointer
1284           && !initializer)
1285         initializer = gfc_default_initializer (&current_ts);
1286       t = build_struct (name, cl, &initializer, &as);
1287     }
1288
1289   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1290
1291 cleanup:
1292   /* Free stuff up and return.  */
1293   gfc_free_expr (initializer);
1294   gfc_free_array_spec (as);
1295
1296   return m;
1297 }
1298
1299
1300 /* Match an extended-f77 kind specification.  */
1301
1302 match
1303 gfc_match_old_kind_spec (gfc_typespec * ts)
1304 {
1305   match m;
1306   int original_kind;
1307
1308   if (gfc_match_char ('*') != MATCH_YES)
1309     return MATCH_NO;
1310
1311   m = gfc_match_small_literal_int (&ts->kind, NULL);
1312   if (m != MATCH_YES)
1313     return MATCH_ERROR;
1314
1315   original_kind = ts->kind;
1316
1317   /* Massage the kind numbers for complex types.  */
1318   if (ts->type == BT_COMPLEX)
1319     {
1320       if (ts->kind % 2)
1321         {
1322           gfc_error ("Old-style type declaration %s*%d not supported at %C",
1323                      gfc_basic_typename (ts->type), original_kind);
1324           return MATCH_ERROR;
1325         }
1326       ts->kind /= 2;
1327     }
1328
1329   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1330     {
1331       gfc_error ("Old-style type declaration %s*%d not supported at %C",
1332                  gfc_basic_typename (ts->type), original_kind);
1333       return MATCH_ERROR;
1334     }
1335
1336   if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1337                       gfc_basic_typename (ts->type), original_kind) == FAILURE)
1338     return MATCH_ERROR;
1339
1340   return MATCH_YES;
1341 }
1342
1343
1344 /* Match a kind specification.  Since kinds are generally optional, we
1345    usually return MATCH_NO if something goes wrong.  If a "kind="
1346    string is found, then we know we have an error.  */
1347
1348 match
1349 gfc_match_kind_spec (gfc_typespec * ts)
1350 {
1351   locus where;
1352   gfc_expr *e;
1353   match m, n;
1354   const char *msg;
1355
1356   m = MATCH_NO;
1357   e = NULL;
1358
1359   where = gfc_current_locus;
1360
1361   if (gfc_match_char ('(') == MATCH_NO)
1362     return MATCH_NO;
1363
1364   /* Also gobbles optional text.  */
1365   if (gfc_match (" kind = ") == MATCH_YES)
1366     m = MATCH_ERROR;
1367
1368   n = gfc_match_init_expr (&e);
1369   if (n == MATCH_NO)
1370     gfc_error ("Expected initialization expression at %C");
1371   if (n != MATCH_YES)
1372     return MATCH_ERROR;
1373
1374   if (e->rank != 0)
1375     {
1376       gfc_error ("Expected scalar initialization expression at %C");
1377       m = MATCH_ERROR;
1378       goto no_match;
1379     }
1380
1381   msg = gfc_extract_int (e, &ts->kind);
1382   if (msg != NULL)
1383     {
1384       gfc_error (msg);
1385       m = MATCH_ERROR;
1386       goto no_match;
1387     }
1388
1389   gfc_free_expr (e);
1390   e = NULL;
1391
1392   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1393     {
1394       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1395                  gfc_basic_typename (ts->type));
1396
1397       m = MATCH_ERROR;
1398       goto no_match;
1399     }
1400
1401   if (gfc_match_char (')') != MATCH_YES)
1402     {
1403       gfc_error ("Missing right paren at %C");
1404       goto no_match;
1405     }
1406
1407   return MATCH_YES;
1408
1409 no_match:
1410   gfc_free_expr (e);
1411   gfc_current_locus = where;
1412   return m;
1413 }
1414
1415
1416 /* Match the various kind/length specifications in a CHARACTER
1417    declaration.  We don't return MATCH_NO.  */
1418
1419 static match
1420 match_char_spec (gfc_typespec * ts)
1421 {
1422   int i, kind, seen_length;
1423   gfc_charlen *cl;
1424   gfc_expr *len;
1425   match m;
1426
1427   kind = gfc_default_character_kind;
1428   len = NULL;
1429   seen_length = 0;
1430
1431   /* Try the old-style specification first.  */
1432   old_char_selector = 0;
1433
1434   m = match_char_length (&len);
1435   if (m != MATCH_NO)
1436     {
1437       if (m == MATCH_YES)
1438         old_char_selector = 1;
1439       seen_length = 1;
1440       goto done;
1441     }
1442
1443   m = gfc_match_char ('(');
1444   if (m != MATCH_YES)
1445     {
1446       m = MATCH_YES;    /* character without length is a single char */
1447       goto done;
1448     }
1449
1450   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] )   */
1451   if (gfc_match (" kind =") == MATCH_YES)
1452     {
1453       m = gfc_match_small_int (&kind);
1454       if (m == MATCH_ERROR)
1455         goto done;
1456       if (m == MATCH_NO)
1457         goto syntax;
1458
1459       if (gfc_match (" , len =") == MATCH_NO)
1460         goto rparen;
1461
1462       m = char_len_param_value (&len);
1463       if (m == MATCH_NO)
1464         goto syntax;
1465       if (m == MATCH_ERROR)
1466         goto done;
1467       seen_length = 1;
1468
1469       goto rparen;
1470     }
1471
1472   /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> )  */
1473   if (gfc_match (" len =") == MATCH_YES)
1474     {
1475       m = char_len_param_value (&len);
1476       if (m == MATCH_NO)
1477         goto syntax;
1478       if (m == MATCH_ERROR)
1479         goto done;
1480       seen_length = 1;
1481
1482       if (gfc_match_char (')') == MATCH_YES)
1483         goto done;
1484
1485       if (gfc_match (" , kind =") != MATCH_YES)
1486         goto syntax;
1487
1488       gfc_match_small_int (&kind);
1489
1490       if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1491         {
1492           gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1493           return MATCH_YES;
1494         }
1495
1496       goto rparen;
1497     }
1498
1499   /* Try to match   ( <len-param> ) or ( <len-param> , [ KIND = ] <int> )  */
1500   m = char_len_param_value (&len);
1501   if (m == MATCH_NO)
1502     goto syntax;
1503   if (m == MATCH_ERROR)
1504     goto done;
1505   seen_length = 1;
1506
1507   m = gfc_match_char (')');
1508   if (m == MATCH_YES)
1509     goto done;
1510
1511   if (gfc_match_char (',') != MATCH_YES)
1512     goto syntax;
1513
1514   gfc_match (" kind =");        /* Gobble optional text */
1515
1516   m = gfc_match_small_int (&kind);
1517   if (m == MATCH_ERROR)
1518     goto done;
1519   if (m == MATCH_NO)
1520     goto syntax;
1521
1522 rparen:
1523   /* Require a right-paren at this point.  */
1524   m = gfc_match_char (')');
1525   if (m == MATCH_YES)
1526     goto done;
1527
1528 syntax:
1529   gfc_error ("Syntax error in CHARACTER declaration at %C");
1530   m = MATCH_ERROR;
1531
1532 done:
1533   if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1534     {
1535       gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1536       m = MATCH_ERROR;
1537     }
1538
1539   if (m != MATCH_YES)
1540     {
1541       gfc_free_expr (len);
1542       return m;
1543     }
1544
1545   /* Do some final massaging of the length values.  */
1546   cl = gfc_get_charlen ();
1547   cl->next = gfc_current_ns->cl_list;
1548   gfc_current_ns->cl_list = cl;
1549
1550   if (seen_length == 0)
1551     cl->length = gfc_int_expr (1);
1552   else
1553     {
1554       if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1555         cl->length = len;
1556       else
1557         {
1558           gfc_free_expr (len);
1559           cl->length = gfc_int_expr (0);
1560         }
1561     }
1562
1563   ts->cl = cl;
1564   ts->kind = kind;
1565
1566   return MATCH_YES;
1567 }
1568
1569
1570 /* Matches a type specification.  If successful, sets the ts structure
1571    to the matched specification.  This is necessary for FUNCTION and
1572    IMPLICIT statements.
1573
1574    If implicit_flag is nonzero, then we don't check for the optional 
1575    kind specification.  Not doing so is needed for matching an IMPLICIT
1576    statement correctly.  */
1577
1578 static match
1579 match_type_spec (gfc_typespec * ts, int implicit_flag)
1580 {
1581   char name[GFC_MAX_SYMBOL_LEN + 1];
1582   gfc_symbol *sym;
1583   match m;
1584   int c;
1585
1586   gfc_clear_ts (ts);
1587
1588   if (gfc_match (" byte") == MATCH_YES)
1589     {
1590       if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C") 
1591           == FAILURE)
1592         return MATCH_ERROR;
1593
1594       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1595         {
1596           gfc_error ("BYTE type used at %C "
1597                      "is not available on the target machine");
1598           return MATCH_ERROR;
1599         }
1600       
1601       ts->type = BT_INTEGER;
1602       ts->kind = 1;
1603       return MATCH_YES;
1604     }
1605
1606   if (gfc_match (" integer") == MATCH_YES)
1607     {
1608       ts->type = BT_INTEGER;
1609       ts->kind = gfc_default_integer_kind;
1610       goto get_kind;
1611     }
1612
1613   if (gfc_match (" character") == MATCH_YES)
1614     {
1615       ts->type = BT_CHARACTER;
1616       if (implicit_flag == 0)
1617         return match_char_spec (ts);
1618       else
1619         return MATCH_YES;
1620     }
1621
1622   if (gfc_match (" real") == MATCH_YES)
1623     {
1624       ts->type = BT_REAL;
1625       ts->kind = gfc_default_real_kind;
1626       goto get_kind;
1627     }
1628
1629   if (gfc_match (" double precision") == MATCH_YES)
1630     {
1631       ts->type = BT_REAL;
1632       ts->kind = gfc_default_double_kind;
1633       return MATCH_YES;
1634     }
1635
1636   if (gfc_match (" complex") == MATCH_YES)
1637     {
1638       ts->type = BT_COMPLEX;
1639       ts->kind = gfc_default_complex_kind;
1640       goto get_kind;
1641     }
1642
1643   if (gfc_match (" double complex") == MATCH_YES)
1644     {
1645       if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1646                           "conform to the Fortran 95 standard") == FAILURE)
1647         return MATCH_ERROR;
1648
1649       ts->type = BT_COMPLEX;
1650       ts->kind = gfc_default_double_kind;
1651       return MATCH_YES;
1652     }
1653
1654   if (gfc_match (" logical") == MATCH_YES)
1655     {
1656       ts->type = BT_LOGICAL;
1657       ts->kind = gfc_default_logical_kind;
1658       goto get_kind;
1659     }
1660
1661   m = gfc_match (" type ( %n )", name);
1662   if (m != MATCH_YES)
1663     return m;
1664
1665   /* Search for the name but allow the components to be defined later.  */
1666   if (gfc_get_ha_symbol (name, &sym))
1667     {
1668       gfc_error ("Type name '%s' at %C is ambiguous", name);
1669       return MATCH_ERROR;
1670     }
1671
1672   if (sym->attr.flavor != FL_DERIVED
1673       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1674     return MATCH_ERROR;
1675
1676   ts->type = BT_DERIVED;
1677   ts->kind = 0;
1678   ts->derived = sym;
1679
1680   return MATCH_YES;
1681
1682 get_kind:
1683   /* For all types except double, derived and character, look for an
1684      optional kind specifier.  MATCH_NO is actually OK at this point.  */
1685   if (implicit_flag == 1)
1686     return MATCH_YES;
1687
1688   if (gfc_current_form == FORM_FREE)
1689     {
1690       c = gfc_peek_char();
1691       if (!gfc_is_whitespace(c) && c != '*' && c != '('
1692          && c != ':' && c != ',')
1693        return MATCH_NO;
1694     }
1695
1696   m = gfc_match_kind_spec (ts);
1697   if (m == MATCH_NO && ts->type != BT_CHARACTER)
1698     m = gfc_match_old_kind_spec (ts);
1699
1700   if (m == MATCH_NO)
1701     m = MATCH_YES;              /* No kind specifier found.  */
1702
1703   return m;
1704 }
1705
1706
1707 /* Match an IMPLICIT NONE statement.  Actually, this statement is
1708    already matched in parse.c, or we would not end up here in the
1709    first place.  So the only thing we need to check, is if there is
1710    trailing garbage.  If not, the match is successful.  */
1711
1712 match
1713 gfc_match_implicit_none (void)
1714 {
1715
1716   return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1717 }
1718
1719
1720 /* Match the letter range(s) of an IMPLICIT statement.  */
1721
1722 static match
1723 match_implicit_range (void)
1724 {
1725   int c, c1, c2, inner;
1726   locus cur_loc;
1727
1728   cur_loc = gfc_current_locus;
1729
1730   gfc_gobble_whitespace ();
1731   c = gfc_next_char ();
1732   if (c != '(')
1733     {
1734       gfc_error ("Missing character range in IMPLICIT at %C");
1735       goto bad;
1736     }
1737
1738   inner = 1;
1739   while (inner)
1740     {
1741       gfc_gobble_whitespace ();
1742       c1 = gfc_next_char ();
1743       if (!ISALPHA (c1))
1744         goto bad;
1745
1746       gfc_gobble_whitespace ();
1747       c = gfc_next_char ();
1748
1749       switch (c)
1750         {
1751         case ')':
1752           inner = 0;            /* Fall through */
1753
1754         case ',':
1755           c2 = c1;
1756           break;
1757
1758         case '-':
1759           gfc_gobble_whitespace ();
1760           c2 = gfc_next_char ();
1761           if (!ISALPHA (c2))
1762             goto bad;
1763
1764           gfc_gobble_whitespace ();
1765           c = gfc_next_char ();
1766
1767           if ((c != ',') && (c != ')'))
1768             goto bad;
1769           if (c == ')')
1770             inner = 0;
1771
1772           break;
1773
1774         default:
1775           goto bad;
1776         }
1777
1778       if (c1 > c2)
1779         {
1780           gfc_error ("Letters must be in alphabetic order in "
1781                      "IMPLICIT statement at %C");
1782           goto bad;
1783         }
1784
1785       /* See if we can add the newly matched range to the pending
1786          implicits from this IMPLICIT statement.  We do not check for
1787          conflicts with whatever earlier IMPLICIT statements may have
1788          set.  This is done when we've successfully finished matching
1789          the current one.  */
1790       if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1791         goto bad;
1792     }
1793
1794   return MATCH_YES;
1795
1796 bad:
1797   gfc_syntax_error (ST_IMPLICIT);
1798
1799   gfc_current_locus = cur_loc;
1800   return MATCH_ERROR;
1801 }
1802
1803
1804 /* Match an IMPLICIT statement, storing the types for
1805    gfc_set_implicit() if the statement is accepted by the parser.
1806    There is a strange looking, but legal syntactic construction
1807    possible.  It looks like:
1808
1809      IMPLICIT INTEGER (a-b) (c-d)
1810
1811    This is legal if "a-b" is a constant expression that happens to
1812    equal one of the legal kinds for integers.  The real problem
1813    happens with an implicit specification that looks like:
1814
1815      IMPLICIT INTEGER (a-b)
1816
1817    In this case, a typespec matcher that is "greedy" (as most of the
1818    matchers are) gobbles the character range as a kindspec, leaving
1819    nothing left.  We therefore have to go a bit more slowly in the
1820    matching process by inhibiting the kindspec checking during
1821    typespec matching and checking for a kind later.  */
1822
1823 match
1824 gfc_match_implicit (void)
1825 {
1826   gfc_typespec ts;
1827   locus cur_loc;
1828   int c;
1829   match m;
1830
1831   /* We don't allow empty implicit statements.  */
1832   if (gfc_match_eos () == MATCH_YES)
1833     {
1834       gfc_error ("Empty IMPLICIT statement at %C");
1835       return MATCH_ERROR;
1836     }
1837
1838   do
1839     {
1840       /* First cleanup.  */
1841       gfc_clear_new_implicit ();
1842
1843       /* A basic type is mandatory here.  */
1844       m = match_type_spec (&ts, 1);
1845       if (m == MATCH_ERROR)
1846         goto error;
1847       if (m == MATCH_NO)
1848         goto syntax;
1849
1850       cur_loc = gfc_current_locus;
1851       m = match_implicit_range ();
1852
1853       if (m == MATCH_YES)
1854         {
1855           /* We may have <TYPE> (<RANGE>).  */
1856           gfc_gobble_whitespace ();
1857           c = gfc_next_char ();
1858           if ((c == '\n') || (c == ','))
1859             {
1860               /* Check for CHARACTER with no length parameter.  */
1861               if (ts.type == BT_CHARACTER && !ts.cl)
1862                 {
1863                   ts.kind = gfc_default_character_kind;
1864                   ts.cl = gfc_get_charlen ();
1865                   ts.cl->next = gfc_current_ns->cl_list;
1866                   gfc_current_ns->cl_list = ts.cl;
1867                   ts.cl->length = gfc_int_expr (1);
1868                 }
1869
1870               /* Record the Successful match.  */
1871               if (gfc_merge_new_implicit (&ts) != SUCCESS)
1872                 return MATCH_ERROR;
1873               continue;
1874             }
1875
1876           gfc_current_locus = cur_loc;
1877         }
1878
1879       /* Discard the (incorrectly) matched range.  */
1880       gfc_clear_new_implicit ();
1881
1882       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
1883       if (ts.type == BT_CHARACTER)
1884         m = match_char_spec (&ts);
1885       else
1886         {
1887           m = gfc_match_kind_spec (&ts);
1888           if (m == MATCH_NO)
1889             {
1890               m = gfc_match_old_kind_spec (&ts);
1891               if (m == MATCH_ERROR)
1892                 goto error;
1893               if (m == MATCH_NO)
1894                 goto syntax;
1895             }
1896         }
1897       if (m == MATCH_ERROR)
1898         goto error;
1899
1900       m = match_implicit_range ();
1901       if (m == MATCH_ERROR)
1902         goto error;
1903       if (m == MATCH_NO)
1904         goto syntax;
1905
1906       gfc_gobble_whitespace ();
1907       c = gfc_next_char ();
1908       if ((c != '\n') && (c != ','))
1909         goto syntax;
1910
1911       if (gfc_merge_new_implicit (&ts) != SUCCESS)
1912         return MATCH_ERROR;
1913     }
1914   while (c == ',');
1915
1916   return MATCH_YES;
1917
1918 syntax:
1919   gfc_syntax_error (ST_IMPLICIT);
1920
1921 error:
1922   return MATCH_ERROR;
1923 }
1924
1925
1926 /* Matches an attribute specification including array specs.  If
1927    successful, leaves the variables current_attr and current_as
1928    holding the specification.  Also sets the colon_seen variable for
1929    later use by matchers associated with initializations.
1930
1931    This subroutine is a little tricky in the sense that we don't know
1932    if we really have an attr-spec until we hit the double colon.
1933    Until that time, we can only return MATCH_NO.  This forces us to
1934    check for duplicate specification at this level.  */
1935
1936 static match
1937 match_attr_spec (void)
1938 {
1939
1940   /* Modifiers that can exist in a type statement.  */
1941   typedef enum
1942   { GFC_DECL_BEGIN = 0,
1943     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1944     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1945     DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1946     DECL_TARGET, DECL_COLON, DECL_NONE,
1947     GFC_DECL_END /* Sentinel */
1948   }
1949   decl_types;
1950
1951 /* GFC_DECL_END is the sentinel, index starts at 0.  */
1952 #define NUM_DECL GFC_DECL_END
1953
1954   static mstring decls[] = {
1955     minit (", allocatable", DECL_ALLOCATABLE),
1956     minit (", dimension", DECL_DIMENSION),
1957     minit (", external", DECL_EXTERNAL),
1958     minit (", intent ( in )", DECL_IN),
1959     minit (", intent ( out )", DECL_OUT),
1960     minit (", intent ( in out )", DECL_INOUT),
1961     minit (", intrinsic", DECL_INTRINSIC),
1962     minit (", optional", DECL_OPTIONAL),
1963     minit (", parameter", DECL_PARAMETER),
1964     minit (", pointer", DECL_POINTER),
1965     minit (", private", DECL_PRIVATE),
1966     minit (", public", DECL_PUBLIC),
1967     minit (", save", DECL_SAVE),
1968     minit (", target", DECL_TARGET),
1969     minit ("::", DECL_COLON),
1970     minit (NULL, DECL_NONE)
1971   };
1972
1973   locus start, seen_at[NUM_DECL];
1974   int seen[NUM_DECL];
1975   decl_types d;
1976   const char *attr;
1977   match m;
1978   try t;
1979
1980   gfc_clear_attr (&current_attr);
1981   start = gfc_current_locus;
1982
1983   current_as = NULL;
1984   colon_seen = 0;
1985
1986   /* See if we get all of the keywords up to the final double colon.  */
1987   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1988     seen[d] = 0;
1989
1990   for (;;)
1991     {
1992       d = (decl_types) gfc_match_strings (decls);
1993       if (d == DECL_NONE || d == DECL_COLON)
1994         break;
1995        
1996       if (gfc_current_state () == COMP_ENUM)
1997         {
1998           gfc_error ("Enumerator cannot have attributes %C");
1999           return MATCH_ERROR;
2000         }
2001
2002       seen[d]++;
2003       seen_at[d] = gfc_current_locus;
2004
2005       if (d == DECL_DIMENSION)
2006         {
2007           m = gfc_match_array_spec (&current_as);
2008
2009           if (m == MATCH_NO)
2010             {
2011               gfc_error ("Missing dimension specification at %C");
2012               m = MATCH_ERROR;
2013             }
2014
2015           if (m == MATCH_ERROR)
2016             goto cleanup;
2017         }
2018     }
2019
2020   /* If we are parsing an enumeration and have ensured that no other
2021      attributes are present we can now set the parameter attribute.  */
2022   if (gfc_current_state () == COMP_ENUM)
2023     {
2024       t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
2025       if (t == FAILURE)
2026         {
2027           m = MATCH_ERROR;
2028           goto cleanup;
2029         }
2030     }
2031
2032   /* No double colon, so assume that we've been looking at something
2033      else the whole time.  */
2034   if (d == DECL_NONE)
2035     {
2036       m = MATCH_NO;
2037       goto cleanup;
2038     }
2039
2040   /* Since we've seen a double colon, we have to be looking at an
2041      attr-spec.  This means that we can now issue errors.  */
2042   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2043     if (seen[d] > 1)
2044       {
2045         switch (d)
2046           {
2047           case DECL_ALLOCATABLE:
2048             attr = "ALLOCATABLE";
2049             break;
2050           case DECL_DIMENSION:
2051             attr = "DIMENSION";
2052             break;
2053           case DECL_EXTERNAL:
2054             attr = "EXTERNAL";
2055             break;
2056           case DECL_IN:
2057             attr = "INTENT (IN)";
2058             break;
2059           case DECL_OUT:
2060             attr = "INTENT (OUT)";
2061             break;
2062           case DECL_INOUT:
2063             attr = "INTENT (IN OUT)";
2064             break;
2065           case DECL_INTRINSIC:
2066             attr = "INTRINSIC";
2067             break;
2068           case DECL_OPTIONAL:
2069             attr = "OPTIONAL";
2070             break;
2071           case DECL_PARAMETER:
2072             attr = "PARAMETER";
2073             break;
2074           case DECL_POINTER:
2075             attr = "POINTER";
2076             break;
2077           case DECL_PRIVATE:
2078             attr = "PRIVATE";
2079             break;
2080           case DECL_PUBLIC:
2081             attr = "PUBLIC";
2082             break;
2083           case DECL_SAVE:
2084             attr = "SAVE";
2085             break;
2086           case DECL_TARGET:
2087             attr = "TARGET";
2088             break;
2089           default:
2090             attr = NULL;        /* This shouldn't happen */
2091           }
2092
2093         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2094         m = MATCH_ERROR;
2095         goto cleanup;
2096       }
2097
2098   /* Now that we've dealt with duplicate attributes, add the attributes
2099      to the current attribute.  */
2100   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2101     {
2102       if (seen[d] == 0)
2103         continue;
2104
2105       if (gfc_current_state () == COMP_DERIVED
2106           && d != DECL_DIMENSION && d != DECL_POINTER
2107           && d != DECL_COLON && d != DECL_NONE)
2108         {
2109
2110           gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2111                      &seen_at[d]);
2112           m = MATCH_ERROR;
2113           goto cleanup;
2114         }
2115
2116       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2117              && gfc_current_state () != COMP_MODULE)
2118         {
2119           if (d == DECL_PRIVATE)
2120             attr = "PRIVATE";
2121           else
2122             attr = "PUBLIC";
2123
2124           gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2125                      attr, &seen_at[d]);
2126           m = MATCH_ERROR;
2127           goto cleanup;
2128         }
2129
2130       switch (d)
2131         {
2132         case DECL_ALLOCATABLE:
2133           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2134           break;
2135
2136         case DECL_DIMENSION:
2137           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2138           break;
2139
2140         case DECL_EXTERNAL:
2141           t = gfc_add_external (&current_attr, &seen_at[d]);
2142           break;
2143
2144         case DECL_IN:
2145           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2146           break;
2147
2148         case DECL_OUT:
2149           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2150           break;
2151
2152         case DECL_INOUT:
2153           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2154           break;
2155
2156         case DECL_INTRINSIC:
2157           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2158           break;
2159
2160         case DECL_OPTIONAL:
2161           t = gfc_add_optional (&current_attr, &seen_at[d]);
2162           break;
2163
2164         case DECL_PARAMETER:
2165           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2166           break;
2167
2168         case DECL_POINTER:
2169           t = gfc_add_pointer (&current_attr, &seen_at[d]);
2170           break;
2171
2172         case DECL_PRIVATE:
2173           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2174                               &seen_at[d]);
2175           break;
2176
2177         case DECL_PUBLIC:
2178           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2179                               &seen_at[d]);
2180           break;
2181
2182         case DECL_SAVE:
2183           t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2184           break;
2185
2186         case DECL_TARGET:
2187           t = gfc_add_target (&current_attr, &seen_at[d]);
2188           break;
2189
2190         default:
2191           gfc_internal_error ("match_attr_spec(): Bad attribute");
2192         }
2193
2194       if (t == FAILURE)
2195         {
2196           m = MATCH_ERROR;
2197           goto cleanup;
2198         }
2199     }
2200
2201   colon_seen = 1;
2202   return MATCH_YES;
2203
2204 cleanup:
2205   gfc_current_locus = start;
2206   gfc_free_array_spec (current_as);
2207   current_as = NULL;
2208   return m;
2209 }
2210
2211
2212 /* Match a data declaration statement.  */
2213
2214 match
2215 gfc_match_data_decl (void)
2216 {
2217   gfc_symbol *sym;
2218   match m;
2219   int elem;
2220
2221   m = match_type_spec (&current_ts, 0);
2222   if (m != MATCH_YES)
2223     return m;
2224
2225   if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2226     {
2227       sym = gfc_use_derived (current_ts.derived);
2228
2229       if (sym == NULL)
2230         {
2231           m = MATCH_ERROR;
2232           goto cleanup;
2233         }
2234
2235       current_ts.derived = sym;
2236     }
2237
2238   m = match_attr_spec ();
2239   if (m == MATCH_ERROR)
2240     {
2241       m = MATCH_NO;
2242       goto cleanup;
2243     }
2244
2245   if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2246     {
2247
2248       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2249         goto ok;
2250
2251       gfc_find_symbol (current_ts.derived->name,
2252                          current_ts.derived->ns->parent, 1, &sym);
2253
2254       /* Any symbol that we find had better be a type definition
2255          which has its components defined.  */
2256       if (sym != NULL && sym->attr.flavor == FL_DERIVED
2257             && current_ts.derived->components != NULL)
2258         goto ok;
2259
2260       /* Now we have an error, which we signal, and then fix up
2261          because the knock-on is plain and simple confusing.  */
2262       gfc_error_now ("Derived type at %C has not been previously defined "
2263                  "and so cannot appear in a derived type definition.");
2264       current_attr.pointer = 1;
2265       goto ok;
2266     }
2267
2268 ok:
2269   /* If we have an old-style character declaration, and no new-style
2270      attribute specifications, then there a comma is optional between
2271      the type specification and the variable list.  */
2272   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2273     gfc_match_char (',');
2274
2275   /* Give the types/attributes to symbols that follow. Give the element
2276      a number so that repeat character length expressions can be copied.  */
2277   elem = 1;
2278   for (;;)
2279     {
2280       m = variable_decl (elem++);
2281       if (m == MATCH_ERROR)
2282         goto cleanup;
2283       if (m == MATCH_NO)
2284         break;
2285
2286       if (gfc_match_eos () == MATCH_YES)
2287         goto cleanup;
2288       if (gfc_match_char (',') != MATCH_YES)
2289         break;
2290     }
2291
2292   gfc_error ("Syntax error in data declaration at %C");
2293   m = MATCH_ERROR;
2294
2295 cleanup:
2296   gfc_free_array_spec (current_as);
2297   current_as = NULL;
2298   return m;
2299 }
2300
2301
2302 /* Match a prefix associated with a function or subroutine
2303    declaration.  If the typespec pointer is nonnull, then a typespec
2304    can be matched.  Note that if nothing matches, MATCH_YES is
2305    returned (the null string was matched).  */
2306
2307 static match
2308 match_prefix (gfc_typespec * ts)
2309 {
2310   int seen_type;
2311
2312   gfc_clear_attr (&current_attr);
2313   seen_type = 0;
2314
2315 loop:
2316   if (!seen_type && ts != NULL
2317       && match_type_spec (ts, 0) == MATCH_YES
2318       && gfc_match_space () == MATCH_YES)
2319     {
2320
2321       seen_type = 1;
2322       goto loop;
2323     }
2324
2325   if (gfc_match ("elemental% ") == MATCH_YES)
2326     {
2327       if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2328         return MATCH_ERROR;
2329
2330       goto loop;
2331     }
2332
2333   if (gfc_match ("pure% ") == MATCH_YES)
2334     {
2335       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2336         return MATCH_ERROR;
2337
2338       goto loop;
2339     }
2340
2341   if (gfc_match ("recursive% ") == MATCH_YES)
2342     {
2343       if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2344         return MATCH_ERROR;
2345
2346       goto loop;
2347     }
2348
2349   /* At this point, the next item is not a prefix.  */
2350   return MATCH_YES;
2351 }
2352
2353
2354 /* Copy attributes matched by match_prefix() to attributes on a symbol.  */
2355
2356 static try
2357 copy_prefix (symbol_attribute * dest, locus * where)
2358 {
2359
2360   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2361     return FAILURE;
2362
2363   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2364     return FAILURE;
2365
2366   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2367     return FAILURE;
2368
2369   return SUCCESS;
2370 }
2371
2372
2373 /* Match a formal argument list.  */
2374
2375 match
2376 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2377 {
2378   gfc_formal_arglist *head, *tail, *p, *q;
2379   char name[GFC_MAX_SYMBOL_LEN + 1];
2380   gfc_symbol *sym;
2381   match m;
2382
2383   head = tail = NULL;
2384
2385   if (gfc_match_char ('(') != MATCH_YES)
2386     {
2387       if (null_flag)
2388         goto ok;
2389       return MATCH_NO;
2390     }
2391
2392   if (gfc_match_char (')') == MATCH_YES)
2393     goto ok;
2394
2395   for (;;)
2396     {
2397       if (gfc_match_char ('*') == MATCH_YES)
2398         sym = NULL;
2399       else
2400         {
2401           m = gfc_match_name (name);
2402           if (m != MATCH_YES)
2403             goto cleanup;
2404
2405           if (gfc_get_symbol (name, NULL, &sym))
2406             goto cleanup;
2407         }
2408
2409       p = gfc_get_formal_arglist ();
2410
2411       if (head == NULL)
2412         head = tail = p;
2413       else
2414         {
2415           tail->next = p;
2416           tail = p;
2417         }
2418
2419       tail->sym = sym;
2420
2421       /* We don't add the VARIABLE flavor because the name could be a
2422          dummy procedure.  We don't apply these attributes to formal
2423          arguments of statement functions.  */
2424       if (sym != NULL && !st_flag
2425           && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2426               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2427         {
2428           m = MATCH_ERROR;
2429           goto cleanup;
2430         }
2431
2432       /* The name of a program unit can be in a different namespace,
2433          so check for it explicitly.  After the statement is accepted,
2434          the name is checked for especially in gfc_get_symbol().  */
2435       if (gfc_new_block != NULL && sym != NULL
2436           && strcmp (sym->name, gfc_new_block->name) == 0)
2437         {
2438           gfc_error ("Name '%s' at %C is the name of the procedure",
2439                      sym->name);
2440           m = MATCH_ERROR;
2441           goto cleanup;
2442         }
2443
2444       if (gfc_match_char (')') == MATCH_YES)
2445         goto ok;
2446
2447       m = gfc_match_char (',');
2448       if (m != MATCH_YES)
2449         {
2450           gfc_error ("Unexpected junk in formal argument list at %C");
2451           goto cleanup;
2452         }
2453     }
2454
2455 ok:
2456   /* Check for duplicate symbols in the formal argument list.  */
2457   if (head != NULL)
2458     {
2459       for (p = head; p->next; p = p->next)
2460         {
2461           if (p->sym == NULL)
2462             continue;
2463
2464           for (q = p->next; q; q = q->next)
2465             if (p->sym == q->sym)
2466               {
2467                 gfc_error
2468                   ("Duplicate symbol '%s' in formal argument list at %C",
2469                    p->sym->name);
2470
2471                 m = MATCH_ERROR;
2472                 goto cleanup;
2473               }
2474         }
2475     }
2476
2477   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2478       FAILURE)
2479     {
2480       m = MATCH_ERROR;
2481       goto cleanup;
2482     }
2483
2484   return MATCH_YES;
2485
2486 cleanup:
2487   gfc_free_formal_arglist (head);
2488   return m;
2489 }
2490
2491
2492 /* Match a RESULT specification following a function declaration or
2493    ENTRY statement.  Also matches the end-of-statement.  */
2494
2495 static match
2496 match_result (gfc_symbol * function, gfc_symbol ** result)
2497 {
2498   char name[GFC_MAX_SYMBOL_LEN + 1];
2499   gfc_symbol *r;
2500   match m;
2501
2502   if (gfc_match (" result (") != MATCH_YES)
2503     return MATCH_NO;
2504
2505   m = gfc_match_name (name);
2506   if (m != MATCH_YES)
2507     return m;
2508
2509   if (gfc_match (" )%t") != MATCH_YES)
2510     {
2511       gfc_error ("Unexpected junk following RESULT variable at %C");
2512       return MATCH_ERROR;
2513     }
2514
2515   if (strcmp (function->name, name) == 0)
2516     {
2517       gfc_error
2518         ("RESULT variable at %C must be different than function name");
2519       return MATCH_ERROR;
2520     }
2521
2522   if (gfc_get_symbol (name, NULL, &r))
2523     return MATCH_ERROR;
2524
2525   if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2526       || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2527     return MATCH_ERROR;
2528
2529   *result = r;
2530
2531   return MATCH_YES;
2532 }
2533
2534
2535 /* Match a function declaration.  */
2536
2537 match
2538 gfc_match_function_decl (void)
2539 {
2540   char name[GFC_MAX_SYMBOL_LEN + 1];
2541   gfc_symbol *sym, *result;
2542   locus old_loc;
2543   match m;
2544
2545   if (gfc_current_state () != COMP_NONE
2546       && gfc_current_state () != COMP_INTERFACE
2547       && gfc_current_state () != COMP_CONTAINS)
2548     return MATCH_NO;
2549
2550   gfc_clear_ts (&current_ts);
2551
2552   old_loc = gfc_current_locus;
2553
2554   m = match_prefix (&current_ts);
2555   if (m != MATCH_YES)
2556     {
2557       gfc_current_locus = old_loc;
2558       return m;
2559     }
2560
2561   if (gfc_match ("function% %n", name) != MATCH_YES)
2562     {
2563       gfc_current_locus = old_loc;
2564       return MATCH_NO;
2565     }
2566
2567   if (get_proc_name (name, &sym))
2568     return MATCH_ERROR;
2569   gfc_new_block = sym;
2570
2571   m = gfc_match_formal_arglist (sym, 0, 0);
2572   if (m == MATCH_NO)
2573     {
2574       gfc_error ("Expected formal argument list in function "
2575                 "definition at %C");
2576       m = MATCH_ERROR;
2577       goto cleanup;
2578     }
2579   else if (m == MATCH_ERROR)
2580     goto cleanup;
2581
2582   result = NULL;
2583
2584   if (gfc_match_eos () != MATCH_YES)
2585     {
2586       /* See if a result variable is present.  */
2587       m = match_result (sym, &result);
2588       if (m == MATCH_NO)
2589         gfc_error ("Unexpected junk after function declaration at %C");
2590
2591       if (m != MATCH_YES)
2592         {
2593           m = MATCH_ERROR;
2594           goto cleanup;
2595         }
2596     }
2597
2598   /* Make changes to the symbol.  */
2599   m = MATCH_ERROR;
2600
2601   if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2602     goto cleanup;
2603
2604   if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2605       || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2606     goto cleanup;
2607
2608   if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2609     {
2610       gfc_error ("Function '%s' at %C already has a type of %s", name,
2611                  gfc_basic_typename (sym->ts.type));
2612       goto cleanup;
2613     }
2614
2615   if (result == NULL)
2616     {
2617       sym->ts = current_ts;
2618       sym->result = sym;
2619     }
2620   else
2621     {
2622       result->ts = current_ts;
2623       sym->result = result;
2624     }
2625
2626   return MATCH_YES;
2627
2628 cleanup:
2629   gfc_current_locus = old_loc;
2630   return m;
2631 }
2632
2633 /* This is mostly a copy of parse.c(add_global_procedure) but modified to pass the
2634    name of the entry, rather than the gfc_current_block name, and to return false
2635    upon finding an existing global entry.  */
2636
2637 static bool
2638 add_global_entry (const char * name, int sub)
2639 {
2640   gfc_gsymbol *s;
2641
2642   s = gfc_get_gsymbol(name);
2643
2644   if (s->defined
2645         || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2646     global_used(s, NULL);
2647   else
2648     {
2649       s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2650       s->where = gfc_current_locus;
2651       s->defined = 1;
2652       return true;
2653     }
2654   return false;
2655 }
2656
2657 /* Match an ENTRY statement.  */
2658
2659 match
2660 gfc_match_entry (void)
2661 {
2662   gfc_symbol *proc;
2663   gfc_symbol *result;
2664   gfc_symbol *entry;
2665   char name[GFC_MAX_SYMBOL_LEN + 1];
2666   gfc_compile_state state;
2667   match m;
2668   gfc_entry_list *el;
2669   locus old_loc;
2670
2671   m = gfc_match_name (name);
2672   if (m != MATCH_YES)
2673     return m;
2674
2675   state = gfc_current_state ();
2676   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2677     {
2678       switch (state)
2679         {
2680           case COMP_PROGRAM:
2681             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2682             break;
2683           case COMP_MODULE:
2684             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2685             break;
2686           case COMP_BLOCK_DATA:
2687             gfc_error
2688               ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2689             break;
2690           case COMP_INTERFACE:
2691             gfc_error
2692               ("ENTRY statement at %C cannot appear within an INTERFACE");
2693             break;
2694           case COMP_DERIVED:
2695             gfc_error
2696               ("ENTRY statement at %C cannot appear "
2697                "within a DERIVED TYPE block");
2698             break;
2699           case COMP_IF:
2700             gfc_error
2701               ("ENTRY statement at %C cannot appear within an IF-THEN block");
2702             break;
2703           case COMP_DO:
2704             gfc_error
2705               ("ENTRY statement at %C cannot appear within a DO block");
2706             break;
2707           case COMP_SELECT:
2708             gfc_error
2709               ("ENTRY statement at %C cannot appear within a SELECT block");
2710             break;
2711           case COMP_FORALL:
2712             gfc_error
2713               ("ENTRY statement at %C cannot appear within a FORALL block");
2714             break;
2715           case COMP_WHERE:
2716             gfc_error
2717               ("ENTRY statement at %C cannot appear within a WHERE block");
2718             break;
2719           case COMP_CONTAINS:
2720             gfc_error
2721               ("ENTRY statement at %C cannot appear "
2722                "within a contained subprogram");
2723             break;
2724           default:
2725             gfc_internal_error ("gfc_match_entry(): Bad state");
2726         }
2727       return MATCH_ERROR;
2728     }
2729
2730   if (gfc_current_ns->parent != NULL
2731       && gfc_current_ns->parent->proc_name
2732       && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2733     {
2734       gfc_error("ENTRY statement at %C cannot appear in a "
2735                 "contained procedure");
2736       return MATCH_ERROR;
2737     }
2738
2739   if (get_proc_name (name, &entry))
2740     return MATCH_ERROR;
2741
2742   proc = gfc_current_block ();
2743
2744   if (state == COMP_SUBROUTINE)
2745     {
2746       /* An entry in a subroutine.  */
2747       if (!add_global_entry (name, 1))
2748         return MATCH_ERROR;
2749
2750       m = gfc_match_formal_arglist (entry, 0, 1);
2751       if (m != MATCH_YES)
2752         return MATCH_ERROR;
2753
2754       if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2755           || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2756         return MATCH_ERROR;
2757     }
2758   else
2759     {
2760       /* An entry in a function.
2761          We need to take special care because writing
2762             ENTRY f()
2763          as
2764             ENTRY f
2765          is allowed, whereas
2766             ENTRY f() RESULT (r)
2767          can't be written as
2768             ENTRY f RESULT (r).  */
2769       if (!add_global_entry (name, 0))
2770         return MATCH_ERROR;
2771
2772       old_loc = gfc_current_locus;
2773       if (gfc_match_eos () == MATCH_YES)
2774         {
2775           gfc_current_locus = old_loc;
2776           /* Match the empty argument list, and add the interface to
2777              the symbol.  */
2778           m = gfc_match_formal_arglist (entry, 0, 1);
2779         }
2780       else
2781         m = gfc_match_formal_arglist (entry, 0, 0);
2782
2783       if (m != MATCH_YES)
2784         return MATCH_ERROR;
2785
2786       result = NULL;
2787
2788       if (gfc_match_eos () == MATCH_YES)
2789         {
2790           if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2791               || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2792             return MATCH_ERROR;
2793
2794           entry->result = entry;
2795         }
2796       else
2797         {
2798           m = match_result (proc, &result);
2799           if (m == MATCH_NO)
2800             gfc_syntax_error (ST_ENTRY);
2801           if (m != MATCH_YES)
2802             return MATCH_ERROR;
2803
2804           if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2805               || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2806               || gfc_add_function (&entry->attr, result->name,
2807                                    NULL) == FAILURE)
2808             return MATCH_ERROR;
2809
2810           entry->result = result;
2811         }
2812
2813       if (proc->attr.recursive && result == NULL)
2814         {
2815           gfc_error ("RESULT attribute required in ENTRY statement at %C");
2816           return MATCH_ERROR;
2817         }
2818     }
2819
2820   if (gfc_match_eos () != MATCH_YES)
2821     {
2822       gfc_syntax_error (ST_ENTRY);
2823       return MATCH_ERROR;
2824     }
2825
2826   entry->attr.recursive = proc->attr.recursive;
2827   entry->attr.elemental = proc->attr.elemental;
2828   entry->attr.pure = proc->attr.pure;
2829
2830   el = gfc_get_entry_list ();
2831   el->sym = entry;
2832   el->next = gfc_current_ns->entries;
2833   gfc_current_ns->entries = el;
2834   if (el->next)
2835     el->id = el->next->id + 1;
2836   else
2837     el->id = 1;
2838
2839   new_st.op = EXEC_ENTRY;
2840   new_st.ext.entry = el;
2841
2842   return MATCH_YES;
2843 }
2844
2845
2846 /* Match a subroutine statement, including optional prefixes.  */
2847
2848 match
2849 gfc_match_subroutine (void)
2850 {
2851   char name[GFC_MAX_SYMBOL_LEN + 1];
2852   gfc_symbol *sym;
2853   match m;
2854
2855   if (gfc_current_state () != COMP_NONE
2856       && gfc_current_state () != COMP_INTERFACE
2857       && gfc_current_state () != COMP_CONTAINS)
2858     return MATCH_NO;
2859
2860   m = match_prefix (NULL);
2861   if (m != MATCH_YES)
2862     return m;
2863
2864   m = gfc_match ("subroutine% %n", name);
2865   if (m != MATCH_YES)
2866     return m;
2867
2868   if (get_proc_name (name, &sym))
2869     return MATCH_ERROR;
2870   gfc_new_block = sym;
2871
2872   if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2873     return MATCH_ERROR;
2874
2875   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2876     return MATCH_ERROR;
2877
2878   if (gfc_match_eos () != MATCH_YES)
2879     {
2880       gfc_syntax_error (ST_SUBROUTINE);
2881       return MATCH_ERROR;
2882     }
2883
2884   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2885     return MATCH_ERROR;
2886
2887   return MATCH_YES;
2888 }
2889
2890
2891 /* Return nonzero if we're currently compiling a contained procedure.  */
2892
2893 static int
2894 contained_procedure (void)
2895 {
2896   gfc_state_data *s;
2897
2898   for (s=gfc_state_stack; s; s=s->previous)
2899     if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2900        && s->previous != NULL
2901        && s->previous->state == COMP_CONTAINS)
2902       return 1;
2903
2904   return 0;
2905 }
2906
2907 /* Set the kind of each enumerator.  The kind is selected such that it is 
2908    interoperable with the corresponding C enumeration type, making
2909    sure that -fshort-enums is honored.  */
2910
2911 static void
2912 set_enum_kind(void)
2913 {
2914   enumerator_history *current_history = NULL;
2915   int kind;
2916   int i;
2917
2918   if (max_enum == NULL || enum_history == NULL)
2919     return;
2920
2921   if (!gfc_option.fshort_enums)
2922     return; 
2923   
2924   i = 0;
2925   do
2926     {
2927       kind = gfc_integer_kinds[i++].kind;
2928     }
2929   while (kind < gfc_c_int_kind 
2930          && gfc_check_integer_range (max_enum->initializer->value.integer,
2931                                      kind) != ARITH_OK);
2932
2933   current_history = enum_history;
2934   while (current_history != NULL)
2935     {
2936       current_history->sym->ts.kind = kind;
2937       current_history = current_history->next;
2938     }
2939 }
2940
2941 /* Match any of the various end-block statements.  Returns the type of
2942    END to the caller.  The END INTERFACE, END IF, END DO and END
2943    SELECT statements cannot be replaced by a single END statement.  */
2944
2945 match
2946 gfc_match_end (gfc_statement * st)
2947 {
2948   char name[GFC_MAX_SYMBOL_LEN + 1];
2949   gfc_compile_state state;
2950   locus old_loc;
2951   const char *block_name;
2952   const char *target;
2953   int eos_ok;
2954   match m;
2955
2956   old_loc = gfc_current_locus;
2957   if (gfc_match ("end") != MATCH_YES)
2958     return MATCH_NO;
2959
2960   state = gfc_current_state ();
2961   block_name =
2962     gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2963
2964   if (state == COMP_CONTAINS)
2965     {
2966       state = gfc_state_stack->previous->state;
2967       block_name = gfc_state_stack->previous->sym == NULL ? NULL
2968         : gfc_state_stack->previous->sym->name;
2969     }
2970
2971   switch (state)
2972     {
2973     case COMP_NONE:
2974     case COMP_PROGRAM:
2975       *st = ST_END_PROGRAM;
2976       target = " program";
2977       eos_ok = 1;
2978       break;
2979
2980     case COMP_SUBROUTINE:
2981       *st = ST_END_SUBROUTINE;
2982       target = " subroutine";
2983       eos_ok = !contained_procedure ();
2984       break;
2985
2986     case COMP_FUNCTION:
2987       *st = ST_END_FUNCTION;
2988       target = " function";
2989       eos_ok = !contained_procedure ();
2990       break;
2991
2992     case COMP_BLOCK_DATA:
2993       *st = ST_END_BLOCK_DATA;
2994       target = " block data";
2995       eos_ok = 1;
2996       break;
2997
2998     case COMP_MODULE:
2999       *st = ST_END_MODULE;
3000       target = " module";
3001       eos_ok = 1;
3002       break;
3003
3004     case COMP_INTERFACE:
3005       *st = ST_END_INTERFACE;
3006       target = " interface";
3007       eos_ok = 0;
3008       break;
3009
3010     case COMP_DERIVED:
3011       *st = ST_END_TYPE;
3012       target = " type";
3013       eos_ok = 0;
3014       break;
3015
3016     case COMP_IF:
3017       *st = ST_ENDIF;
3018       target = " if";
3019       eos_ok = 0;
3020       break;
3021
3022     case COMP_DO:
3023       *st = ST_ENDDO;
3024       target = " do";
3025       eos_ok = 0;
3026       break;
3027
3028     case COMP_SELECT:
3029       *st = ST_END_SELECT;
3030       target = " select";
3031       eos_ok = 0;
3032       break;
3033
3034     case COMP_FORALL:
3035       *st = ST_END_FORALL;
3036       target = " forall";
3037       eos_ok = 0;
3038       break;
3039
3040     case COMP_WHERE:
3041       *st = ST_END_WHERE;
3042       target = " where";
3043       eos_ok = 0;
3044       break;
3045
3046     case COMP_ENUM:
3047       *st = ST_END_ENUM;
3048       target = " enum";
3049       eos_ok = 0;
3050       last_initializer = NULL;
3051       set_enum_kind ();
3052       gfc_free_enum_history ();
3053       break;
3054
3055     default:
3056       gfc_error ("Unexpected END statement at %C");
3057       goto cleanup;
3058     }
3059
3060   if (gfc_match_eos () == MATCH_YES)
3061     {
3062       if (!eos_ok)
3063         {
3064           /* We would have required END [something]  */
3065           gfc_error ("%s statement expected at %L",
3066                      gfc_ascii_statement (*st), &old_loc);
3067           goto cleanup;
3068         }
3069
3070       return MATCH_YES;
3071     }
3072
3073   /* Verify that we've got the sort of end-block that we're expecting.  */
3074   if (gfc_match (target) != MATCH_YES)
3075     {
3076       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3077       goto cleanup;
3078     }
3079
3080   /* If we're at the end, make sure a block name wasn't required.  */
3081   if (gfc_match_eos () == MATCH_YES)
3082     {
3083
3084       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3085         return MATCH_YES;
3086
3087       if (gfc_current_block () == NULL)
3088         return MATCH_YES;
3089
3090       gfc_error ("Expected block name of '%s' in %s statement at %C",
3091                  block_name, gfc_ascii_statement (*st));
3092
3093       return MATCH_ERROR;
3094     }
3095
3096   /* END INTERFACE has a special handler for its several possible endings.  */
3097   if (*st == ST_END_INTERFACE)
3098     return gfc_match_end_interface ();
3099
3100   /* We haven't hit the end of statement, so what is left must be an end-name.  */
3101   m = gfc_match_space ();
3102   if (m == MATCH_YES)
3103     m = gfc_match_name (name);
3104
3105   if (m == MATCH_NO)
3106     gfc_error ("Expected terminating name at %C");
3107   if (m != MATCH_YES)
3108     goto cleanup;
3109
3110   if (block_name == NULL)
3111     goto syntax;
3112
3113   if (strcmp (name, block_name) != 0)
3114     {
3115       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3116                  gfc_ascii_statement (*st));
3117       goto cleanup;
3118     }
3119
3120   if (gfc_match_eos () == MATCH_YES)
3121     return MATCH_YES;
3122
3123 syntax:
3124   gfc_syntax_error (*st);
3125
3126 cleanup:
3127   gfc_current_locus = old_loc;
3128   return MATCH_ERROR;
3129 }
3130
3131
3132
3133 /***************** Attribute declaration statements ****************/
3134
3135 /* Set the attribute of a single variable.  */
3136
3137 static match
3138 attr_decl1 (void)
3139 {
3140   char name[GFC_MAX_SYMBOL_LEN + 1];
3141   gfc_array_spec *as;
3142   gfc_symbol *sym;
3143   locus var_locus;
3144   match m;
3145
3146   as = NULL;
3147
3148   m = gfc_match_name (name);
3149   if (m != MATCH_YES)
3150     goto cleanup;
3151
3152   if (find_special (name, &sym))
3153     return MATCH_ERROR;
3154
3155   var_locus = gfc_current_locus;
3156
3157   /* Deal with possible array specification for certain attributes.  */
3158   if (current_attr.dimension
3159       || current_attr.allocatable
3160       || current_attr.pointer
3161       || current_attr.target)
3162     {
3163       m = gfc_match_array_spec (&as);
3164       if (m == MATCH_ERROR)
3165         goto cleanup;
3166
3167       if (current_attr.dimension && m == MATCH_NO)
3168         {
3169           gfc_error
3170             ("Missing array specification at %L in DIMENSION statement",
3171              &var_locus);
3172           m = MATCH_ERROR;
3173           goto cleanup;
3174         }
3175
3176       if ((current_attr.allocatable || current_attr.pointer)
3177           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3178         {
3179           gfc_error ("Array specification must be deferred at %L",
3180                      &var_locus);
3181           m = MATCH_ERROR;
3182           goto cleanup;
3183         }
3184     }
3185
3186   /* Update symbol table.  DIMENSION attribute is set in gfc_set_array_spec().  */
3187   if (current_attr.dimension == 0
3188       && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3189     {
3190       m = MATCH_ERROR;
3191       goto cleanup;
3192     }
3193
3194   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3195     {
3196       m = MATCH_ERROR;
3197       goto cleanup;
3198     }
3199     
3200   if (sym->attr.cray_pointee && sym->as != NULL)
3201     {
3202       /* Fix the array spec.  */
3203       m = gfc_mod_pointee_as (sym->as);         
3204       if (m == MATCH_ERROR)
3205         goto cleanup;
3206     }
3207
3208   if (gfc_add_attribute (&sym->attr, &var_locus, current_attr.intent) == FAILURE)
3209     {
3210       m = MATCH_ERROR;
3211       goto cleanup;
3212     }
3213
3214   if ((current_attr.external || current_attr.intrinsic)
3215       && sym->attr.flavor != FL_PROCEDURE
3216       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3217     {
3218       m = MATCH_ERROR;
3219       goto cleanup;
3220     }
3221
3222   return MATCH_YES;
3223
3224 cleanup:
3225   gfc_free_array_spec (as);
3226   return m;
3227 }
3228
3229
3230 /* Generic attribute declaration subroutine.  Used for attributes that
3231    just have a list of names.  */
3232
3233 static match
3234 attr_decl (void)
3235 {
3236   match m;
3237
3238   /* Gobble the optional double colon, by simply ignoring the result
3239      of gfc_match().  */
3240   gfc_match (" ::");
3241
3242   for (;;)
3243     {
3244       m = attr_decl1 ();
3245       if (m != MATCH_YES)
3246         break;
3247
3248       if (gfc_match_eos () == MATCH_YES)
3249         {
3250           m = MATCH_YES;
3251           break;
3252         }
3253
3254       if (gfc_match_char (',') != MATCH_YES)
3255         {
3256           gfc_error ("Unexpected character in variable list at %C");
3257           m = MATCH_ERROR;
3258           break;
3259         }
3260     }
3261
3262   return m;
3263 }
3264
3265
3266 /* This routine matches Cray Pointer declarations of the form:
3267    pointer ( <pointer>, <pointee> )
3268    or
3269    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...   
3270    The pointer, if already declared, should be an integer.  Otherwise, we 
3271    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
3272    be either a scalar, or an array declaration.  No space is allocated for
3273    the pointee.  For the statement 
3274    pointer (ipt, ar(10))
3275    any subsequent uses of ar will be translated (in C-notation) as
3276    ar(i) => ((<type> *) ipt)(i)   
3277    After gimplification, pointee variable will disappear in the code.  */
3278
3279 static match
3280 cray_pointer_decl (void)
3281 {
3282   match m;
3283   gfc_array_spec *as;
3284   gfc_symbol *cptr; /* Pointer symbol.  */
3285   gfc_symbol *cpte; /* Pointee symbol.  */
3286   locus var_locus;
3287   bool done = false;
3288
3289   while (!done)
3290     {
3291       if (gfc_match_char ('(') != MATCH_YES)
3292         {
3293           gfc_error ("Expected '(' at %C");
3294           return MATCH_ERROR;   
3295         }
3296  
3297       /* Match pointer.  */
3298       var_locus = gfc_current_locus;
3299       gfc_clear_attr (&current_attr);
3300       gfc_add_cray_pointer (&current_attr, &var_locus);
3301       current_ts.type = BT_INTEGER;
3302       current_ts.kind = gfc_index_integer_kind;
3303
3304       m = gfc_match_symbol (&cptr, 0);  
3305       if (m != MATCH_YES)
3306         {
3307           gfc_error ("Expected variable name at %C");
3308           return m;
3309         }
3310   
3311       if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3312         return MATCH_ERROR;
3313
3314       gfc_set_sym_referenced (cptr);      
3315
3316       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
3317         {
3318           cptr->ts.type = BT_INTEGER;
3319           cptr->ts.kind = gfc_index_integer_kind; 
3320         }
3321       else if (cptr->ts.type != BT_INTEGER)
3322         {
3323           gfc_error ("Cray pointer at %C must be an integer.");
3324           return MATCH_ERROR;
3325         }
3326       else if (cptr->ts.kind < gfc_index_integer_kind)
3327         gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3328                      " memory addresses require %d bytes.",
3329                      cptr->ts.kind,
3330                      gfc_index_integer_kind);
3331
3332       if (gfc_match_char (',') != MATCH_YES)
3333         {
3334           gfc_error ("Expected \",\" at %C");
3335           return MATCH_ERROR;    
3336         }
3337
3338       /* Match Pointee.  */  
3339       var_locus = gfc_current_locus;
3340       gfc_clear_attr (&current_attr);
3341       gfc_add_cray_pointee (&current_attr, &var_locus);
3342       current_ts.type = BT_UNKNOWN;
3343       current_ts.kind = 0;
3344
3345       m = gfc_match_symbol (&cpte, 0);
3346       if (m != MATCH_YES)
3347         {
3348           gfc_error ("Expected variable name at %C");
3349           return m;
3350         }
3351        
3352       /* Check for an optional array spec.  */
3353       m = gfc_match_array_spec (&as);
3354       if (m == MATCH_ERROR)
3355         {
3356           gfc_free_array_spec (as);
3357           return m;
3358         }
3359       else if (m == MATCH_NO)
3360         {
3361           gfc_free_array_spec (as);
3362           as = NULL;
3363         }   
3364
3365       if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3366         return MATCH_ERROR;
3367
3368       gfc_set_sym_referenced (cpte);
3369
3370       if (cpte->as == NULL)
3371         {
3372           if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3373             gfc_internal_error ("Couldn't set Cray pointee array spec.");
3374         }
3375       else if (as != NULL)
3376         {
3377           gfc_error ("Duplicate array spec for Cray pointee at %C.");
3378           gfc_free_array_spec (as);
3379           return MATCH_ERROR;
3380         }
3381       
3382       as = NULL;
3383     
3384       if (cpte->as != NULL)
3385         {
3386           /* Fix array spec.  */
3387           m = gfc_mod_pointee_as (cpte->as);
3388           if (m == MATCH_ERROR)
3389             return m;
3390         } 
3391    
3392       /* Point the Pointee at the Pointer.  */
3393       cpte->cp_pointer = cptr;
3394
3395       if (gfc_match_char (')') != MATCH_YES)
3396         {
3397           gfc_error ("Expected \")\" at %C");
3398           return MATCH_ERROR;    
3399         }
3400       m = gfc_match_char (',');
3401       if (m != MATCH_YES)
3402         done = true; /* Stop searching for more declarations.  */
3403
3404     }
3405   
3406   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
3407       || gfc_match_eos () != MATCH_YES)
3408     {
3409       gfc_error ("Expected \",\" or end of statement at %C");
3410       return MATCH_ERROR;
3411     }
3412   return MATCH_YES;
3413 }
3414
3415
3416 match
3417 gfc_match_external (void)
3418 {
3419
3420   gfc_clear_attr (&current_attr);
3421   current_attr.external = 1;
3422
3423   return attr_decl ();
3424 }
3425
3426
3427
3428 match
3429 gfc_match_intent (void)
3430 {
3431   sym_intent intent;
3432
3433   intent = match_intent_spec ();
3434   if (intent == INTENT_UNKNOWN)
3435     return MATCH_ERROR;
3436
3437   gfc_clear_attr (&current_attr);
3438   current_attr.intent = intent;
3439
3440   return attr_decl ();
3441 }
3442
3443
3444 match
3445 gfc_match_intrinsic (void)
3446 {
3447
3448   gfc_clear_attr (&current_attr);
3449   current_attr.intrinsic = 1;
3450
3451   return attr_decl ();
3452 }
3453
3454
3455 match
3456 gfc_match_optional (void)
3457 {
3458
3459   gfc_clear_attr (&current_attr);
3460   current_attr.optional = 1;
3461
3462   return attr_decl ();
3463 }
3464
3465
3466 match
3467 gfc_match_pointer (void)
3468 {
3469   gfc_gobble_whitespace ();
3470   if (gfc_peek_char () == '(')
3471     {
3472       if (!gfc_option.flag_cray_pointer)
3473         {
3474           gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3475                      " flag.");
3476           return MATCH_ERROR;
3477         }
3478       return cray_pointer_decl ();
3479     }
3480   else
3481     {
3482       gfc_clear_attr (&current_attr);
3483       current_attr.pointer = 1;
3484     
3485       return attr_decl ();
3486     }
3487 }
3488
3489
3490 match
3491 gfc_match_allocatable (void)
3492 {
3493
3494   gfc_clear_attr (&current_attr);
3495   current_attr.allocatable = 1;
3496
3497   return attr_decl ();
3498 }
3499
3500
3501 match
3502 gfc_match_dimension (void)
3503 {
3504
3505   gfc_clear_attr (&current_attr);
3506   current_attr.dimension = 1;
3507
3508   return attr_decl ();
3509 }
3510
3511
3512 match
3513 gfc_match_target (void)
3514 {
3515
3516   gfc_clear_attr (&current_attr);
3517   current_attr.target = 1;
3518
3519   return attr_decl ();
3520 }
3521
3522
3523 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3524    statement.  */
3525
3526 static match
3527 access_attr_decl (gfc_statement st)
3528 {
3529   char name[GFC_MAX_SYMBOL_LEN + 1];
3530   interface_type type;
3531   gfc_user_op *uop;
3532   gfc_symbol *sym;
3533   gfc_intrinsic_op operator;
3534   match m;
3535
3536   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3537     goto done;
3538
3539   for (;;)
3540     {
3541       m = gfc_match_generic_spec (&type, name, &operator);
3542       if (m == MATCH_NO)
3543         goto syntax;
3544       if (m == MATCH_ERROR)
3545         return MATCH_ERROR;
3546
3547       switch (type)
3548         {
3549         case INTERFACE_NAMELESS:
3550           goto syntax;
3551
3552         case INTERFACE_GENERIC:
3553           if (gfc_get_symbol (name, NULL, &sym))
3554             goto done;
3555
3556           if (gfc_add_access (&sym->attr,
3557                               (st ==
3558                                ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3559                               sym->name, NULL) == FAILURE)
3560             return MATCH_ERROR;
3561
3562           break;
3563
3564         case INTERFACE_INTRINSIC_OP:
3565           if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3566             {
3567               gfc_current_ns->operator_access[operator] =
3568                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3569             }
3570           else
3571             {
3572               gfc_error ("Access specification of the %s operator at %C has "
3573                          "already been specified", gfc_op2string (operator));
3574               goto done;
3575             }
3576
3577           break;
3578
3579         case INTERFACE_USER_OP:
3580           uop = gfc_get_uop (name);
3581
3582           if (uop->access == ACCESS_UNKNOWN)
3583             {
3584               uop->access =
3585                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3586             }
3587           else
3588             {
3589               gfc_error
3590                 ("Access specification of the .%s. operator at %C has "
3591                  "already been specified", sym->name);
3592               goto done;
3593             }
3594
3595           break;
3596         }
3597
3598       if (gfc_match_char (',') == MATCH_NO)
3599         break;
3600     }
3601
3602   if (gfc_match_eos () != MATCH_YES)
3603     goto syntax;
3604   return MATCH_YES;
3605
3606 syntax:
3607   gfc_syntax_error (st);
3608
3609 done:
3610   return MATCH_ERROR;
3611 }
3612
3613
3614 /* The PRIVATE statement is a bit weird in that it can be a attribute
3615    declaration, but also works as a standlone statement inside of a
3616    type declaration or a module.  */
3617
3618 match
3619 gfc_match_private (gfc_statement * st)
3620 {
3621
3622   if (gfc_match ("private") != MATCH_YES)
3623     return MATCH_NO;
3624
3625   if (gfc_current_state () == COMP_DERIVED)
3626     {
3627       if (gfc_match_eos () == MATCH_YES)
3628         {
3629           *st = ST_PRIVATE;
3630           return MATCH_YES;
3631         }
3632
3633       gfc_syntax_error (ST_PRIVATE);
3634       return MATCH_ERROR;
3635     }
3636
3637   if (gfc_match_eos () == MATCH_YES)
3638     {
3639       *st = ST_PRIVATE;
3640       return MATCH_YES;
3641     }
3642
3643   *st = ST_ATTR_DECL;
3644   return access_attr_decl (ST_PRIVATE);
3645 }
3646
3647
3648 match
3649 gfc_match_public (gfc_statement * st)
3650 {
3651
3652   if (gfc_match ("public") != MATCH_YES)
3653     return MATCH_NO;
3654
3655   if (gfc_match_eos () == MATCH_YES)
3656     {
3657       *st = ST_PUBLIC;
3658       return MATCH_YES;
3659     }
3660
3661   *st = ST_ATTR_DECL;
3662   return access_attr_decl (ST_PUBLIC);
3663 }
3664
3665
3666 /* Workhorse for gfc_match_parameter.  */
3667
3668 static match
3669 do_parm (void)
3670 {
3671   gfc_symbol *sym;
3672   gfc_expr *init;
3673   match m;
3674
3675   m = gfc_match_symbol (&sym, 0);
3676   if (m == MATCH_NO)
3677     gfc_error ("Expected variable name at %C in PARAMETER statement");
3678
3679   if (m != MATCH_YES)
3680     return m;
3681
3682   if (gfc_match_char ('=') == MATCH_NO)
3683     {
3684       gfc_error ("Expected = sign in PARAMETER statement at %C");
3685       return MATCH_ERROR;
3686     }
3687
3688   m = gfc_match_init_expr (&init);
3689   if (m == MATCH_NO)
3690     gfc_error ("Expected expression at %C in PARAMETER statement");
3691   if (m != MATCH_YES)
3692     return m;
3693
3694   if (sym->ts.type == BT_UNKNOWN
3695       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3696     {
3697       m = MATCH_ERROR;
3698       goto cleanup;
3699     }
3700
3701   if (gfc_check_assign_symbol (sym, init) == FAILURE
3702       || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3703     {
3704       m = MATCH_ERROR;
3705       goto cleanup;
3706     }
3707
3708   if (sym->ts.type == BT_CHARACTER
3709       && sym->ts.cl != NULL
3710       && sym->ts.cl->length != NULL
3711       && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3712       && init->expr_type == EXPR_CONSTANT
3713       && init->ts.type == BT_CHARACTER
3714       && init->ts.kind == 1)
3715     gfc_set_constant_character_len (
3716       mpz_get_si (sym->ts.cl->length->value.integer), init);
3717
3718   sym->value = init;
3719   return MATCH_YES;
3720
3721 cleanup:
3722   gfc_free_expr (init);
3723   return m;
3724 }
3725
3726
3727 /* Match a parameter statement, with the weird syntax that these have.  */
3728
3729 match
3730 gfc_match_parameter (void)
3731 {
3732   match m;
3733
3734   if (gfc_match_char ('(') == MATCH_NO)
3735     return MATCH_NO;
3736
3737   for (;;)
3738     {
3739       m = do_parm ();
3740       if (m != MATCH_YES)
3741         break;
3742
3743       if (gfc_match (" )%t") == MATCH_YES)
3744         break;
3745
3746       if (gfc_match_char (',') != MATCH_YES)
3747         {
3748           gfc_error ("Unexpected characters in PARAMETER statement at %C");
3749           m = MATCH_ERROR;
3750           break;
3751         }
3752     }
3753
3754   return m;
3755 }
3756
3757
3758 /* Save statements have a special syntax.  */
3759
3760 match
3761 gfc_match_save (void)
3762 {
3763   char n[GFC_MAX_SYMBOL_LEN+1];
3764   gfc_common_head *c;
3765   gfc_symbol *sym;
3766   match m;
3767
3768   if (gfc_match_eos () == MATCH_YES)
3769     {
3770       if (gfc_current_ns->seen_save)
3771         {
3772           if (gfc_notify_std (GFC_STD_LEGACY, 
3773                               "Blanket SAVE statement at %C follows previous "
3774                               "SAVE statement")
3775               == FAILURE)
3776             return MATCH_ERROR;
3777         }
3778
3779       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3780       return MATCH_YES;
3781     }
3782
3783   if (gfc_current_ns->save_all)
3784     {
3785       if (gfc_notify_std (GFC_STD_LEGACY, 
3786                           "SAVE statement at %C follows blanket SAVE statement")
3787           == FAILURE)
3788         return MATCH_ERROR;
3789     }
3790
3791   gfc_match (" ::");
3792
3793   for (;;)
3794     {
3795       m = gfc_match_symbol (&sym, 0);
3796       switch (m)
3797         {
3798         case MATCH_YES:
3799           if (gfc_add_save (&sym->attr, sym->name,
3800                             &gfc_current_locus) == FAILURE)
3801             return MATCH_ERROR;
3802           goto next_item;
3803
3804         case MATCH_NO:
3805           break;
3806
3807         case MATCH_ERROR:
3808           return MATCH_ERROR;
3809         }
3810
3811       m = gfc_match (" / %n /", &n);
3812       if (m == MATCH_ERROR)
3813         return MATCH_ERROR;
3814       if (m == MATCH_NO)
3815         goto syntax;
3816
3817       c = gfc_get_common (n, 0);
3818       c->saved = 1;
3819
3820       gfc_current_ns->seen_save = 1;
3821
3822     next_item:
3823       if (gfc_match_eos () == MATCH_YES)
3824         break;
3825       if (gfc_match_char (',') != MATCH_YES)
3826         goto syntax;
3827     }
3828
3829   return MATCH_YES;
3830
3831 syntax:
3832   gfc_error ("Syntax error in SAVE statement at %C");
3833   return MATCH_ERROR;
3834 }
3835
3836
3837 /* Match a module procedure statement.  Note that we have to modify
3838    symbols in the parent's namespace because the current one was there
3839    to receive symbols that are in an interface's formal argument list.  */
3840
3841 match
3842 gfc_match_modproc (void)
3843 {
3844   char name[GFC_MAX_SYMBOL_LEN + 1];
3845   gfc_symbol *sym;
3846   match m;
3847
3848   if (gfc_state_stack->state != COMP_INTERFACE
3849       || gfc_state_stack->previous == NULL
3850       || current_interface.type == INTERFACE_NAMELESS)
3851     {
3852       gfc_error
3853         ("MODULE PROCEDURE at %C must be in a generic module interface");
3854       return MATCH_ERROR;
3855     }
3856
3857   for (;;)
3858     {
3859       m = gfc_match_name (name);
3860       if (m == MATCH_NO)
3861         goto syntax;
3862       if (m != MATCH_YES)
3863         return MATCH_ERROR;
3864
3865       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3866         return MATCH_ERROR;
3867
3868       if (sym->attr.proc != PROC_MODULE
3869           && gfc_add_procedure (&sym->attr, PROC_MODULE,
3870                                 sym->name, NULL) == FAILURE)
3871         return MATCH_ERROR;
3872
3873       if (gfc_add_interface (sym) == FAILURE)
3874         return MATCH_ERROR;
3875
3876       if (gfc_match_eos () == MATCH_YES)
3877         break;
3878       if (gfc_match_char (',') != MATCH_YES)
3879         goto syntax;
3880     }
3881
3882   return MATCH_YES;
3883
3884 syntax:
3885   gfc_syntax_error (ST_MODULE_PROC);
3886   return MATCH_ERROR;
3887 }
3888
3889
3890 /* Match the beginning of a derived type declaration.  If a type name
3891    was the result of a function, then it is possible to have a symbol
3892    already to be known as a derived type yet have no components.  */
3893
3894 match
3895 gfc_match_derived_decl (void)
3896 {
3897   char name[GFC_MAX_SYMBOL_LEN + 1];
3898   symbol_attribute attr;
3899   gfc_symbol *sym;
3900   match m;
3901
3902   if (gfc_current_state () == COMP_DERIVED)
3903     return MATCH_NO;
3904
3905   gfc_clear_attr (&attr);
3906
3907 loop:
3908   if (gfc_match (" , private") == MATCH_YES)
3909     {
3910       if (gfc_find_state (COMP_MODULE) == FAILURE)
3911         {
3912           gfc_error
3913             ("Derived type at %C can only be PRIVATE within a MODULE");
3914           return MATCH_ERROR;
3915         }
3916
3917       if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3918         return MATCH_ERROR;
3919       goto loop;
3920     }
3921
3922   if (gfc_match (" , public") == MATCH_YES)
3923     {
3924       if (gfc_find_state (COMP_MODULE) == FAILURE)
3925         {
3926           gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3927           return MATCH_ERROR;
3928         }
3929
3930       if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3931         return MATCH_ERROR;
3932       goto loop;
3933     }
3934
3935   if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3936     {
3937       gfc_error ("Expected :: in TYPE definition at %C");
3938       return MATCH_ERROR;
3939     }
3940
3941   m = gfc_match (" %n%t", name);
3942   if (m != MATCH_YES)
3943     return m;
3944
3945   /* Make sure the name isn't the name of an intrinsic type.  The
3946      'double precision' type doesn't get past the name matcher.  */
3947   if (strcmp (name, "integer") == 0
3948       || strcmp (name, "real") == 0
3949       || strcmp (name, "character") == 0
3950       || strcmp (name, "logical") == 0
3951       || strcmp (name, "complex") == 0)
3952     {
3953       gfc_error
3954         ("Type name '%s' at %C cannot be the same as an intrinsic type",
3955          name);
3956       return MATCH_ERROR;
3957     }
3958
3959   if (gfc_get_symbol (name, NULL, &sym))
3960     return MATCH_ERROR;
3961
3962   if (sym->ts.type != BT_UNKNOWN)
3963     {
3964       gfc_error ("Derived type name '%s' at %C already has a basic type "
3965                  "of %s", sym->name, gfc_typename (&sym->ts));
3966       return MATCH_ERROR;
3967     }
3968
3969   /* The symbol may already have the derived attribute without the
3970      components.  The ways this can happen is via a function
3971      definition, an INTRINSIC statement or a subtype in another
3972      derived type that is a pointer.  The first part of the AND clause
3973      is true if a the symbol is not the return value of a function.  */
3974   if (sym->attr.flavor != FL_DERIVED
3975       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
3976     return MATCH_ERROR;
3977
3978   if (sym->components != NULL)
3979     {
3980       gfc_error
3981         ("Derived type definition of '%s' at %C has already been defined",
3982          sym->name);
3983       return MATCH_ERROR;
3984     }
3985
3986   if (attr.access != ACCESS_UNKNOWN
3987       && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
3988     return MATCH_ERROR;
3989
3990   gfc_new_block = sym;
3991
3992   return MATCH_YES;
3993 }
3994
3995
3996 /* Cray Pointees can be declared as: 
3997       pointer (ipt, a (n,m,...,*)) 
3998    By default, this is treated as an AS_ASSUMED_SIZE array.  We'll
3999    cheat and set a constant bound of 1 for the last dimension, if this
4000    is the case. Since there is no bounds-checking for Cray Pointees,
4001    this will be okay.  */
4002
4003 try
4004 gfc_mod_pointee_as (gfc_array_spec *as)
4005 {
4006   as->cray_pointee = true; /* This will be useful to know later.  */
4007   if (as->type == AS_ASSUMED_SIZE)
4008     {
4009       as->type = AS_EXPLICIT;
4010       as->upper[as->rank - 1] = gfc_int_expr (1);
4011       as->cp_was_assumed = true;
4012     }
4013   else if (as->type == AS_ASSUMED_SHAPE)
4014     {
4015       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4016       return MATCH_ERROR;
4017     }
4018   return MATCH_YES;
4019 }
4020
4021
4022 /* Match the enum definition statement, here we are trying to match 
4023    the first line of enum definition statement.  
4024    Returns MATCH_YES if match is found.  */
4025
4026 match
4027 gfc_match_enum (void)
4028 {
4029   match m;
4030   
4031   m = gfc_match_eos ();
4032   if (m != MATCH_YES)
4033     return m;
4034
4035   if (gfc_notify_std (GFC_STD_F2003, 
4036                       "New in Fortran 2003: ENUM AND ENUMERATOR at %C")
4037       == FAILURE)
4038     return MATCH_ERROR;
4039
4040   return MATCH_YES;
4041 }
4042
4043
4044 /* Match the enumerator definition statement. */
4045
4046 match
4047 gfc_match_enumerator_def (void)
4048 {
4049   match m;
4050   int elem; 
4051   
4052   gfc_clear_ts (&current_ts);
4053   
4054   m = gfc_match (" enumerator");
4055   if (m != MATCH_YES)
4056     return m;
4057   
4058   if (gfc_current_state () != COMP_ENUM)
4059     {
4060       gfc_error ("ENUM definition statement expected before %C");
4061       gfc_free_enum_history ();
4062       return MATCH_ERROR;
4063     }
4064
4065   (&current_ts)->type = BT_INTEGER;
4066   (&current_ts)->kind = gfc_c_int_kind;
4067   
4068   m = match_attr_spec ();
4069   if (m == MATCH_ERROR)
4070     {
4071       m = MATCH_NO;
4072       goto cleanup;
4073     }
4074
4075   elem = 1;
4076   for (;;)
4077     {
4078       m = variable_decl (elem++);
4079       if (m == MATCH_ERROR)
4080         goto cleanup;
4081       if (m == MATCH_NO)
4082         break;
4083
4084       if (gfc_match_eos () == MATCH_YES)
4085         goto cleanup;
4086       if (gfc_match_char (',') != MATCH_YES)
4087         break;
4088     }
4089
4090   if (gfc_current_state () == COMP_ENUM)
4091     {
4092       gfc_free_enum_history ();
4093       gfc_error ("Syntax error in ENUMERATOR definition at %C");
4094       m = MATCH_ERROR;
4095     }
4096
4097 cleanup:
4098   gfc_free_array_spec (current_as);
4099   current_as = NULL;
4100   return m;
4101
4102 }
4103