OSDN Git Service

2006-02-09 Paul Thomas <pault@gcc.gnu.org>
[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           initializer->ts = current_ts;
1222
1223         }
1224       else if (gfc_match_char ('=') == MATCH_YES)
1225         {
1226           if (current_attr.pointer)
1227             {
1228               gfc_error
1229                 ("Pointer initialization at %C requires '=>', not '='");
1230               m = MATCH_ERROR;
1231               goto cleanup;
1232             }
1233
1234           m = gfc_match_init_expr (&initializer);
1235           if (m == MATCH_NO)
1236             {
1237               gfc_error ("Expected an initialization expression at %C");
1238               m = MATCH_ERROR;
1239             }
1240
1241           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1242             {
1243               gfc_error
1244                 ("Initialization of variable at %C is not allowed in a "
1245                  "PURE procedure");
1246               m = MATCH_ERROR;
1247             }
1248
1249           if (m != MATCH_YES)
1250             goto cleanup;
1251         }
1252     }
1253
1254   /* Check if we are parsing an enumeration and if the current enumerator
1255      variable has an initializer or not. If it does not have an
1256      initializer, the initialization value of the previous enumerator 
1257      (stored in last_initializer) is incremented by 1 and is used to
1258      initialize the current enumerator.  */
1259   if (gfc_current_state () == COMP_ENUM)
1260     {
1261       if (initializer == NULL)
1262         initializer = gfc_enum_initializer (last_initializer, old_locus);
1263  
1264       if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1265         {
1266           gfc_error("ENUMERATOR %L not initialized with integer expression",
1267                     &var_locus);
1268           m = MATCH_ERROR; 
1269           gfc_free_enum_history ();
1270           goto cleanup;
1271         }
1272
1273       /* Store this current initializer, for the next enumerator
1274          variable to be parsed.  */
1275       last_initializer = initializer;
1276     }
1277
1278   /* Add the initializer.  Note that it is fine if initializer is
1279      NULL here, because we sometimes also need to check if a
1280      declaration *must* have an initialization expression.  */
1281   if (gfc_current_state () != COMP_DERIVED)
1282     t = add_init_expr_to_sym (name, &initializer, &var_locus);
1283   else
1284     {
1285       if (current_ts.type == BT_DERIVED && !current_attr.pointer && !initializer)
1286         initializer = gfc_default_initializer (&current_ts);
1287       t = build_struct (name, cl, &initializer, &as);
1288     }
1289
1290   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1291
1292 cleanup:
1293   /* Free stuff up and return.  */
1294   gfc_free_expr (initializer);
1295   gfc_free_array_spec (as);
1296
1297   return m;
1298 }
1299
1300
1301 /* Match an extended-f77 kind specification.  */
1302
1303 match
1304 gfc_match_old_kind_spec (gfc_typespec * ts)
1305 {
1306   match m;
1307   int original_kind;
1308
1309   if (gfc_match_char ('*') != MATCH_YES)
1310     return MATCH_NO;
1311
1312   m = gfc_match_small_literal_int (&ts->kind, NULL);
1313   if (m != MATCH_YES)
1314     return MATCH_ERROR;
1315
1316   original_kind = ts->kind;
1317
1318   /* Massage the kind numbers for complex types.  */
1319   if (ts->type == BT_COMPLEX)
1320     {
1321       if (ts->kind % 2)
1322         {
1323           gfc_error ("Old-style type declaration %s*%d not supported at %C",
1324                      gfc_basic_typename (ts->type), original_kind);
1325           return MATCH_ERROR;
1326         }
1327       ts->kind /= 2;
1328     }
1329
1330   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1331     {
1332       gfc_error ("Old-style type declaration %s*%d not supported at %C",
1333                  gfc_basic_typename (ts->type), original_kind);
1334       return MATCH_ERROR;
1335     }
1336
1337   if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1338                       gfc_basic_typename (ts->type), original_kind) == FAILURE)
1339     return MATCH_ERROR;
1340
1341   return MATCH_YES;
1342 }
1343
1344
1345 /* Match a kind specification.  Since kinds are generally optional, we
1346    usually return MATCH_NO if something goes wrong.  If a "kind="
1347    string is found, then we know we have an error.  */
1348
1349 match
1350 gfc_match_kind_spec (gfc_typespec * ts)
1351 {
1352   locus where;
1353   gfc_expr *e;
1354   match m, n;
1355   const char *msg;
1356
1357   m = MATCH_NO;
1358   e = NULL;
1359
1360   where = gfc_current_locus;
1361
1362   if (gfc_match_char ('(') == MATCH_NO)
1363     return MATCH_NO;
1364
1365   /* Also gobbles optional text.  */
1366   if (gfc_match (" kind = ") == MATCH_YES)
1367     m = MATCH_ERROR;
1368
1369   n = gfc_match_init_expr (&e);
1370   if (n == MATCH_NO)
1371     gfc_error ("Expected initialization expression at %C");
1372   if (n != MATCH_YES)
1373     return MATCH_ERROR;
1374
1375   if (e->rank != 0)
1376     {
1377       gfc_error ("Expected scalar initialization expression at %C");
1378       m = MATCH_ERROR;
1379       goto no_match;
1380     }
1381
1382   msg = gfc_extract_int (e, &ts->kind);
1383   if (msg != NULL)
1384     {
1385       gfc_error (msg);
1386       m = MATCH_ERROR;
1387       goto no_match;
1388     }
1389
1390   gfc_free_expr (e);
1391   e = NULL;
1392
1393   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1394     {
1395       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1396                  gfc_basic_typename (ts->type));
1397
1398       m = MATCH_ERROR;
1399       goto no_match;
1400     }
1401
1402   if (gfc_match_char (')') != MATCH_YES)
1403     {
1404       gfc_error ("Missing right paren at %C");
1405       goto no_match;
1406     }
1407
1408   return MATCH_YES;
1409
1410 no_match:
1411   gfc_free_expr (e);
1412   gfc_current_locus = where;
1413   return m;
1414 }
1415
1416
1417 /* Match the various kind/length specifications in a CHARACTER
1418    declaration.  We don't return MATCH_NO.  */
1419
1420 static match
1421 match_char_spec (gfc_typespec * ts)
1422 {
1423   int i, kind, seen_length;
1424   gfc_charlen *cl;
1425   gfc_expr *len;
1426   match m;
1427
1428   kind = gfc_default_character_kind;
1429   len = NULL;
1430   seen_length = 0;
1431
1432   /* Try the old-style specification first.  */
1433   old_char_selector = 0;
1434
1435   m = match_char_length (&len);
1436   if (m != MATCH_NO)
1437     {
1438       if (m == MATCH_YES)
1439         old_char_selector = 1;
1440       seen_length = 1;
1441       goto done;
1442     }
1443
1444   m = gfc_match_char ('(');
1445   if (m != MATCH_YES)
1446     {
1447       m = MATCH_YES;    /* character without length is a single char */
1448       goto done;
1449     }
1450
1451   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] )   */
1452   if (gfc_match (" kind =") == MATCH_YES)
1453     {
1454       m = gfc_match_small_int (&kind);
1455       if (m == MATCH_ERROR)
1456         goto done;
1457       if (m == MATCH_NO)
1458         goto syntax;
1459
1460       if (gfc_match (" , len =") == MATCH_NO)
1461         goto rparen;
1462
1463       m = char_len_param_value (&len);
1464       if (m == MATCH_NO)
1465         goto syntax;
1466       if (m == MATCH_ERROR)
1467         goto done;
1468       seen_length = 1;
1469
1470       goto rparen;
1471     }
1472
1473   /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> )  */
1474   if (gfc_match (" len =") == MATCH_YES)
1475     {
1476       m = char_len_param_value (&len);
1477       if (m == MATCH_NO)
1478         goto syntax;
1479       if (m == MATCH_ERROR)
1480         goto done;
1481       seen_length = 1;
1482
1483       if (gfc_match_char (')') == MATCH_YES)
1484         goto done;
1485
1486       if (gfc_match (" , kind =") != MATCH_YES)
1487         goto syntax;
1488
1489       gfc_match_small_int (&kind);
1490
1491       if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1492         {
1493           gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1494           return MATCH_YES;
1495         }
1496
1497       goto rparen;
1498     }
1499
1500   /* Try to match   ( <len-param> ) or ( <len-param> , [ KIND = ] <int> )  */
1501   m = char_len_param_value (&len);
1502   if (m == MATCH_NO)
1503     goto syntax;
1504   if (m == MATCH_ERROR)
1505     goto done;
1506   seen_length = 1;
1507
1508   m = gfc_match_char (')');
1509   if (m == MATCH_YES)
1510     goto done;
1511
1512   if (gfc_match_char (',') != MATCH_YES)
1513     goto syntax;
1514
1515   gfc_match (" kind =");        /* Gobble optional text */
1516
1517   m = gfc_match_small_int (&kind);
1518   if (m == MATCH_ERROR)
1519     goto done;
1520   if (m == MATCH_NO)
1521     goto syntax;
1522
1523 rparen:
1524   /* Require a right-paren at this point.  */
1525   m = gfc_match_char (')');
1526   if (m == MATCH_YES)
1527     goto done;
1528
1529 syntax:
1530   gfc_error ("Syntax error in CHARACTER declaration at %C");
1531   m = MATCH_ERROR;
1532
1533 done:
1534   if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1535     {
1536       gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1537       m = MATCH_ERROR;
1538     }
1539
1540   if (m != MATCH_YES)
1541     {
1542       gfc_free_expr (len);
1543       return m;
1544     }
1545
1546   /* Do some final massaging of the length values.  */
1547   cl = gfc_get_charlen ();
1548   cl->next = gfc_current_ns->cl_list;
1549   gfc_current_ns->cl_list = cl;
1550
1551   if (seen_length == 0)
1552     cl->length = gfc_int_expr (1);
1553   else
1554     {
1555       if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1556         cl->length = len;
1557       else
1558         {
1559           gfc_free_expr (len);
1560           cl->length = gfc_int_expr (0);
1561         }
1562     }
1563
1564   ts->cl = cl;
1565   ts->kind = kind;
1566
1567   return MATCH_YES;
1568 }
1569
1570
1571 /* Matches a type specification.  If successful, sets the ts structure
1572    to the matched specification.  This is necessary for FUNCTION and
1573    IMPLICIT statements.
1574
1575    If implicit_flag is nonzero, then we don't check for the optional 
1576    kind specification.  Not doing so is needed for matching an IMPLICIT
1577    statement correctly.  */
1578
1579 static match
1580 match_type_spec (gfc_typespec * ts, int implicit_flag)
1581 {
1582   char name[GFC_MAX_SYMBOL_LEN + 1];
1583   gfc_symbol *sym;
1584   match m;
1585   int c;
1586
1587   gfc_clear_ts (ts);
1588
1589   if (gfc_match (" byte") == MATCH_YES)
1590     {
1591       if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C") 
1592           == FAILURE)
1593         return MATCH_ERROR;
1594
1595       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1596         {
1597           gfc_error ("BYTE type used at %C "
1598                      "is not available on the target machine");
1599           return MATCH_ERROR;
1600         }
1601       
1602       ts->type = BT_INTEGER;
1603       ts->kind = 1;
1604       return MATCH_YES;
1605     }
1606
1607   if (gfc_match (" integer") == MATCH_YES)
1608     {
1609       ts->type = BT_INTEGER;
1610       ts->kind = gfc_default_integer_kind;
1611       goto get_kind;
1612     }
1613
1614   if (gfc_match (" character") == MATCH_YES)
1615     {
1616       ts->type = BT_CHARACTER;
1617       if (implicit_flag == 0)
1618         return match_char_spec (ts);
1619       else
1620         return MATCH_YES;
1621     }
1622
1623   if (gfc_match (" real") == MATCH_YES)
1624     {
1625       ts->type = BT_REAL;
1626       ts->kind = gfc_default_real_kind;
1627       goto get_kind;
1628     }
1629
1630   if (gfc_match (" double precision") == MATCH_YES)
1631     {
1632       ts->type = BT_REAL;
1633       ts->kind = gfc_default_double_kind;
1634       return MATCH_YES;
1635     }
1636
1637   if (gfc_match (" complex") == MATCH_YES)
1638     {
1639       ts->type = BT_COMPLEX;
1640       ts->kind = gfc_default_complex_kind;
1641       goto get_kind;
1642     }
1643
1644   if (gfc_match (" double complex") == MATCH_YES)
1645     {
1646       if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1647                           "conform to the Fortran 95 standard") == FAILURE)
1648         return MATCH_ERROR;
1649
1650       ts->type = BT_COMPLEX;
1651       ts->kind = gfc_default_double_kind;
1652       return MATCH_YES;
1653     }
1654
1655   if (gfc_match (" logical") == MATCH_YES)
1656     {
1657       ts->type = BT_LOGICAL;
1658       ts->kind = gfc_default_logical_kind;
1659       goto get_kind;
1660     }
1661
1662   m = gfc_match (" type ( %n )", name);
1663   if (m != MATCH_YES)
1664     return m;
1665
1666   /* Search for the name but allow the components to be defined later.  */
1667   if (gfc_get_ha_symbol (name, &sym))
1668     {
1669       gfc_error ("Type name '%s' at %C is ambiguous", name);
1670       return MATCH_ERROR;
1671     }
1672
1673   if (sym->attr.flavor != FL_DERIVED
1674       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1675     return MATCH_ERROR;
1676
1677   ts->type = BT_DERIVED;
1678   ts->kind = 0;
1679   ts->derived = sym;
1680
1681   return MATCH_YES;
1682
1683 get_kind:
1684   /* For all types except double, derived and character, look for an
1685      optional kind specifier.  MATCH_NO is actually OK at this point.  */
1686   if (implicit_flag == 1)
1687     return MATCH_YES;
1688
1689   if (gfc_current_form == FORM_FREE)
1690     {
1691       c = gfc_peek_char();
1692       if (!gfc_is_whitespace(c) && c != '*' && c != '('
1693          && c != ':' && c != ',')
1694        return MATCH_NO;
1695     }
1696
1697   m = gfc_match_kind_spec (ts);
1698   if (m == MATCH_NO && ts->type != BT_CHARACTER)
1699     m = gfc_match_old_kind_spec (ts);
1700
1701   if (m == MATCH_NO)
1702     m = MATCH_YES;              /* No kind specifier found.  */
1703
1704   return m;
1705 }
1706
1707
1708 /* Match an IMPLICIT NONE statement.  Actually, this statement is
1709    already matched in parse.c, or we would not end up here in the
1710    first place.  So the only thing we need to check, is if there is
1711    trailing garbage.  If not, the match is successful.  */
1712
1713 match
1714 gfc_match_implicit_none (void)
1715 {
1716
1717   return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1718 }
1719
1720
1721 /* Match the letter range(s) of an IMPLICIT statement.  */
1722
1723 static match
1724 match_implicit_range (void)
1725 {
1726   int c, c1, c2, inner;
1727   locus cur_loc;
1728
1729   cur_loc = gfc_current_locus;
1730
1731   gfc_gobble_whitespace ();
1732   c = gfc_next_char ();
1733   if (c != '(')
1734     {
1735       gfc_error ("Missing character range in IMPLICIT at %C");
1736       goto bad;
1737     }
1738
1739   inner = 1;
1740   while (inner)
1741     {
1742       gfc_gobble_whitespace ();
1743       c1 = gfc_next_char ();
1744       if (!ISALPHA (c1))
1745         goto bad;
1746
1747       gfc_gobble_whitespace ();
1748       c = gfc_next_char ();
1749
1750       switch (c)
1751         {
1752         case ')':
1753           inner = 0;            /* Fall through */
1754
1755         case ',':
1756           c2 = c1;
1757           break;
1758
1759         case '-':
1760           gfc_gobble_whitespace ();
1761           c2 = gfc_next_char ();
1762           if (!ISALPHA (c2))
1763             goto bad;
1764
1765           gfc_gobble_whitespace ();
1766           c = gfc_next_char ();
1767
1768           if ((c != ',') && (c != ')'))
1769             goto bad;
1770           if (c == ')')
1771             inner = 0;
1772
1773           break;
1774
1775         default:
1776           goto bad;
1777         }
1778
1779       if (c1 > c2)
1780         {
1781           gfc_error ("Letters must be in alphabetic order in "
1782                      "IMPLICIT statement at %C");
1783           goto bad;
1784         }
1785
1786       /* See if we can add the newly matched range to the pending
1787          implicits from this IMPLICIT statement.  We do not check for
1788          conflicts with whatever earlier IMPLICIT statements may have
1789          set.  This is done when we've successfully finished matching
1790          the current one.  */
1791       if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1792         goto bad;
1793     }
1794
1795   return MATCH_YES;
1796
1797 bad:
1798   gfc_syntax_error (ST_IMPLICIT);
1799
1800   gfc_current_locus = cur_loc;
1801   return MATCH_ERROR;
1802 }
1803
1804
1805 /* Match an IMPLICIT statement, storing the types for
1806    gfc_set_implicit() if the statement is accepted by the parser.
1807    There is a strange looking, but legal syntactic construction
1808    possible.  It looks like:
1809
1810      IMPLICIT INTEGER (a-b) (c-d)
1811
1812    This is legal if "a-b" is a constant expression that happens to
1813    equal one of the legal kinds for integers.  The real problem
1814    happens with an implicit specification that looks like:
1815
1816      IMPLICIT INTEGER (a-b)
1817
1818    In this case, a typespec matcher that is "greedy" (as most of the
1819    matchers are) gobbles the character range as a kindspec, leaving
1820    nothing left.  We therefore have to go a bit more slowly in the
1821    matching process by inhibiting the kindspec checking during
1822    typespec matching and checking for a kind later.  */
1823
1824 match
1825 gfc_match_implicit (void)
1826 {
1827   gfc_typespec ts;
1828   locus cur_loc;
1829   int c;
1830   match m;
1831
1832   /* We don't allow empty implicit statements.  */
1833   if (gfc_match_eos () == MATCH_YES)
1834     {
1835       gfc_error ("Empty IMPLICIT statement at %C");
1836       return MATCH_ERROR;
1837     }
1838
1839   do
1840     {
1841       /* First cleanup.  */
1842       gfc_clear_new_implicit ();
1843
1844       /* A basic type is mandatory here.  */
1845       m = match_type_spec (&ts, 1);
1846       if (m == MATCH_ERROR)
1847         goto error;
1848       if (m == MATCH_NO)
1849         goto syntax;
1850
1851       cur_loc = gfc_current_locus;
1852       m = match_implicit_range ();
1853
1854       if (m == MATCH_YES)
1855         {
1856           /* We may have <TYPE> (<RANGE>).  */
1857           gfc_gobble_whitespace ();
1858           c = gfc_next_char ();
1859           if ((c == '\n') || (c == ','))
1860             {
1861               /* Check for CHARACTER with no length parameter.  */
1862               if (ts.type == BT_CHARACTER && !ts.cl)
1863                 {
1864                   ts.kind = gfc_default_character_kind;
1865                   ts.cl = gfc_get_charlen ();
1866                   ts.cl->next = gfc_current_ns->cl_list;
1867                   gfc_current_ns->cl_list = ts.cl;
1868                   ts.cl->length = gfc_int_expr (1);
1869                 }
1870
1871               /* Record the Successful match.  */
1872               if (gfc_merge_new_implicit (&ts) != SUCCESS)
1873                 return MATCH_ERROR;
1874               continue;
1875             }
1876
1877           gfc_current_locus = cur_loc;
1878         }
1879
1880       /* Discard the (incorrectly) matched range.  */
1881       gfc_clear_new_implicit ();
1882
1883       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
1884       if (ts.type == BT_CHARACTER)
1885         m = match_char_spec (&ts);
1886       else
1887         {
1888           m = gfc_match_kind_spec (&ts);
1889           if (m == MATCH_NO)
1890             {
1891               m = gfc_match_old_kind_spec (&ts);
1892               if (m == MATCH_ERROR)
1893                 goto error;
1894               if (m == MATCH_NO)
1895                 goto syntax;
1896             }
1897         }
1898       if (m == MATCH_ERROR)
1899         goto error;
1900
1901       m = match_implicit_range ();
1902       if (m == MATCH_ERROR)
1903         goto error;
1904       if (m == MATCH_NO)
1905         goto syntax;
1906
1907       gfc_gobble_whitespace ();
1908       c = gfc_next_char ();
1909       if ((c != '\n') && (c != ','))
1910         goto syntax;
1911
1912       if (gfc_merge_new_implicit (&ts) != SUCCESS)
1913         return MATCH_ERROR;
1914     }
1915   while (c == ',');
1916
1917   return MATCH_YES;
1918
1919 syntax:
1920   gfc_syntax_error (ST_IMPLICIT);
1921
1922 error:
1923   return MATCH_ERROR;
1924 }
1925
1926
1927 /* Matches an attribute specification including array specs.  If
1928    successful, leaves the variables current_attr and current_as
1929    holding the specification.  Also sets the colon_seen variable for
1930    later use by matchers associated with initializations.
1931
1932    This subroutine is a little tricky in the sense that we don't know
1933    if we really have an attr-spec until we hit the double colon.
1934    Until that time, we can only return MATCH_NO.  This forces us to
1935    check for duplicate specification at this level.  */
1936
1937 static match
1938 match_attr_spec (void)
1939 {
1940
1941   /* Modifiers that can exist in a type statement.  */
1942   typedef enum
1943   { GFC_DECL_BEGIN = 0,
1944     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1945     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1946     DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1947     DECL_TARGET, DECL_COLON, DECL_NONE,
1948     GFC_DECL_END /* Sentinel */
1949   }
1950   decl_types;
1951
1952 /* GFC_DECL_END is the sentinel, index starts at 0.  */
1953 #define NUM_DECL GFC_DECL_END
1954
1955   static mstring decls[] = {
1956     minit (", allocatable", DECL_ALLOCATABLE),
1957     minit (", dimension", DECL_DIMENSION),
1958     minit (", external", DECL_EXTERNAL),
1959     minit (", intent ( in )", DECL_IN),
1960     minit (", intent ( out )", DECL_OUT),
1961     minit (", intent ( in out )", DECL_INOUT),
1962     minit (", intrinsic", DECL_INTRINSIC),
1963     minit (", optional", DECL_OPTIONAL),
1964     minit (", parameter", DECL_PARAMETER),
1965     minit (", pointer", DECL_POINTER),
1966     minit (", private", DECL_PRIVATE),
1967     minit (", public", DECL_PUBLIC),
1968     minit (", save", DECL_SAVE),
1969     minit (", target", DECL_TARGET),
1970     minit ("::", DECL_COLON),
1971     minit (NULL, DECL_NONE)
1972   };
1973
1974   locus start, seen_at[NUM_DECL];
1975   int seen[NUM_DECL];
1976   decl_types d;
1977   const char *attr;
1978   match m;
1979   try t;
1980
1981   gfc_clear_attr (&current_attr);
1982   start = gfc_current_locus;
1983
1984   current_as = NULL;
1985   colon_seen = 0;
1986
1987   /* See if we get all of the keywords up to the final double colon.  */
1988   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1989     seen[d] = 0;
1990
1991   for (;;)
1992     {
1993       d = (decl_types) gfc_match_strings (decls);
1994       if (d == DECL_NONE || d == DECL_COLON)
1995         break;
1996        
1997       if (gfc_current_state () == COMP_ENUM)
1998         {
1999           gfc_error ("Enumerator cannot have attributes %C");
2000           return MATCH_ERROR;
2001         }
2002
2003       seen[d]++;
2004       seen_at[d] = gfc_current_locus;
2005
2006       if (d == DECL_DIMENSION)
2007         {
2008           m = gfc_match_array_spec (&current_as);
2009
2010           if (m == MATCH_NO)
2011             {
2012               gfc_error ("Missing dimension specification at %C");
2013               m = MATCH_ERROR;
2014             }
2015
2016           if (m == MATCH_ERROR)
2017             goto cleanup;
2018         }
2019     }
2020
2021   /* If we are parsing an enumeration and have ensured that no other
2022      attributes are present we can now set the parameter attribute.  */
2023   if (gfc_current_state () == COMP_ENUM)
2024     {
2025       t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
2026       if (t == FAILURE)
2027         {
2028           m = MATCH_ERROR;
2029           goto cleanup;
2030         }
2031     }
2032
2033   /* No double colon, so assume that we've been looking at something
2034      else the whole time.  */
2035   if (d == DECL_NONE)
2036     {
2037       m = MATCH_NO;
2038       goto cleanup;
2039     }
2040
2041   /* Since we've seen a double colon, we have to be looking at an
2042      attr-spec.  This means that we can now issue errors.  */
2043   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2044     if (seen[d] > 1)
2045       {
2046         switch (d)
2047           {
2048           case DECL_ALLOCATABLE:
2049             attr = "ALLOCATABLE";
2050             break;
2051           case DECL_DIMENSION:
2052             attr = "DIMENSION";
2053             break;
2054           case DECL_EXTERNAL:
2055             attr = "EXTERNAL";
2056             break;
2057           case DECL_IN:
2058             attr = "INTENT (IN)";
2059             break;
2060           case DECL_OUT:
2061             attr = "INTENT (OUT)";
2062             break;
2063           case DECL_INOUT:
2064             attr = "INTENT (IN OUT)";
2065             break;
2066           case DECL_INTRINSIC:
2067             attr = "INTRINSIC";
2068             break;
2069           case DECL_OPTIONAL:
2070             attr = "OPTIONAL";
2071             break;
2072           case DECL_PARAMETER:
2073             attr = "PARAMETER";
2074             break;
2075           case DECL_POINTER:
2076             attr = "POINTER";
2077             break;
2078           case DECL_PRIVATE:
2079             attr = "PRIVATE";
2080             break;
2081           case DECL_PUBLIC:
2082             attr = "PUBLIC";
2083             break;
2084           case DECL_SAVE:
2085             attr = "SAVE";
2086             break;
2087           case DECL_TARGET:
2088             attr = "TARGET";
2089             break;
2090           default:
2091             attr = NULL;        /* This shouldn't happen */
2092           }
2093
2094         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2095         m = MATCH_ERROR;
2096         goto cleanup;
2097       }
2098
2099   /* Now that we've dealt with duplicate attributes, add the attributes
2100      to the current attribute.  */
2101   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2102     {
2103       if (seen[d] == 0)
2104         continue;
2105
2106       if (gfc_current_state () == COMP_DERIVED
2107           && d != DECL_DIMENSION && d != DECL_POINTER
2108           && d != DECL_COLON && d != DECL_NONE)
2109         {
2110
2111           gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2112                      &seen_at[d]);
2113           m = MATCH_ERROR;
2114           goto cleanup;
2115         }
2116
2117       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2118              && gfc_current_state () != COMP_MODULE)
2119         {
2120           if (d == DECL_PRIVATE)
2121             attr = "PRIVATE";
2122           else
2123             attr = "PUBLIC";
2124
2125           gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2126                      attr, &seen_at[d]);
2127           m = MATCH_ERROR;
2128           goto cleanup;
2129         }
2130
2131       switch (d)
2132         {
2133         case DECL_ALLOCATABLE:
2134           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2135           break;
2136
2137         case DECL_DIMENSION:
2138           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2139           break;
2140
2141         case DECL_EXTERNAL:
2142           t = gfc_add_external (&current_attr, &seen_at[d]);
2143           break;
2144
2145         case DECL_IN:
2146           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2147           break;
2148
2149         case DECL_OUT:
2150           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2151           break;
2152
2153         case DECL_INOUT:
2154           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2155           break;
2156
2157         case DECL_INTRINSIC:
2158           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2159           break;
2160
2161         case DECL_OPTIONAL:
2162           t = gfc_add_optional (&current_attr, &seen_at[d]);
2163           break;
2164
2165         case DECL_PARAMETER:
2166           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2167           break;
2168
2169         case DECL_POINTER:
2170           t = gfc_add_pointer (&current_attr, &seen_at[d]);
2171           break;
2172
2173         case DECL_PRIVATE:
2174           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2175                               &seen_at[d]);
2176           break;
2177
2178         case DECL_PUBLIC:
2179           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2180                               &seen_at[d]);
2181           break;
2182
2183         case DECL_SAVE:
2184           t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2185           break;
2186
2187         case DECL_TARGET:
2188           t = gfc_add_target (&current_attr, &seen_at[d]);
2189           break;
2190
2191         default:
2192           gfc_internal_error ("match_attr_spec(): Bad attribute");
2193         }
2194
2195       if (t == FAILURE)
2196         {
2197           m = MATCH_ERROR;
2198           goto cleanup;
2199         }
2200     }
2201
2202   colon_seen = 1;
2203   return MATCH_YES;
2204
2205 cleanup:
2206   gfc_current_locus = start;
2207   gfc_free_array_spec (current_as);
2208   current_as = NULL;
2209   return m;
2210 }
2211
2212
2213 /* Match a data declaration statement.  */
2214
2215 match
2216 gfc_match_data_decl (void)
2217 {
2218   gfc_symbol *sym;
2219   match m;
2220   int elem;
2221
2222   m = match_type_spec (&current_ts, 0);
2223   if (m != MATCH_YES)
2224     return m;
2225
2226   if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2227     {
2228       sym = gfc_use_derived (current_ts.derived);
2229
2230       if (sym == NULL)
2231         {
2232           m = MATCH_ERROR;
2233           goto cleanup;
2234         }
2235
2236       current_ts.derived = sym;
2237     }
2238
2239   m = match_attr_spec ();
2240   if (m == MATCH_ERROR)
2241     {
2242       m = MATCH_NO;
2243       goto cleanup;
2244     }
2245
2246   if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2247     {
2248
2249       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2250         goto ok;
2251
2252       gfc_find_symbol (current_ts.derived->name,
2253                          current_ts.derived->ns->parent, 1, &sym);
2254
2255       /* Any symbol that we find had better be a type definition
2256          which has its components defined.  */
2257       if (sym != NULL && sym->attr.flavor == FL_DERIVED
2258             && current_ts.derived->components != NULL)
2259         goto ok;
2260
2261       /* Now we have an error, which we signal, and then fix up
2262          because the knock-on is plain and simple confusing.  */
2263       gfc_error_now ("Derived type at %C has not been previously defined "
2264                  "and so cannot appear in a derived type definition.");
2265       current_attr.pointer = 1;
2266       goto ok;
2267     }
2268
2269 ok:
2270   /* If we have an old-style character declaration, and no new-style
2271      attribute specifications, then there a comma is optional between
2272      the type specification and the variable list.  */
2273   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2274     gfc_match_char (',');
2275
2276   /* Give the types/attributes to symbols that follow. Give the element
2277      a number so that repeat character length expressions can be copied.  */
2278   elem = 1;
2279   for (;;)
2280     {
2281       m = variable_decl (elem++);
2282       if (m == MATCH_ERROR)
2283         goto cleanup;
2284       if (m == MATCH_NO)
2285         break;
2286
2287       if (gfc_match_eos () == MATCH_YES)
2288         goto cleanup;
2289       if (gfc_match_char (',') != MATCH_YES)
2290         break;
2291     }
2292
2293   gfc_error ("Syntax error in data declaration at %C");
2294   m = MATCH_ERROR;
2295
2296 cleanup:
2297   gfc_free_array_spec (current_as);
2298   current_as = NULL;
2299   return m;
2300 }
2301
2302
2303 /* Match a prefix associated with a function or subroutine
2304    declaration.  If the typespec pointer is nonnull, then a typespec
2305    can be matched.  Note that if nothing matches, MATCH_YES is
2306    returned (the null string was matched).  */
2307
2308 static match
2309 match_prefix (gfc_typespec * ts)
2310 {
2311   int seen_type;
2312
2313   gfc_clear_attr (&current_attr);
2314   seen_type = 0;
2315
2316 loop:
2317   if (!seen_type && ts != NULL
2318       && match_type_spec (ts, 0) == MATCH_YES
2319       && gfc_match_space () == MATCH_YES)
2320     {
2321
2322       seen_type = 1;
2323       goto loop;
2324     }
2325
2326   if (gfc_match ("elemental% ") == MATCH_YES)
2327     {
2328       if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2329         return MATCH_ERROR;
2330
2331       goto loop;
2332     }
2333
2334   if (gfc_match ("pure% ") == MATCH_YES)
2335     {
2336       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2337         return MATCH_ERROR;
2338
2339       goto loop;
2340     }
2341
2342   if (gfc_match ("recursive% ") == MATCH_YES)
2343     {
2344       if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2345         return MATCH_ERROR;
2346
2347       goto loop;
2348     }
2349
2350   /* At this point, the next item is not a prefix.  */
2351   return MATCH_YES;
2352 }
2353
2354
2355 /* Copy attributes matched by match_prefix() to attributes on a symbol.  */
2356
2357 static try
2358 copy_prefix (symbol_attribute * dest, locus * where)
2359 {
2360
2361   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2362     return FAILURE;
2363
2364   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2365     return FAILURE;
2366
2367   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2368     return FAILURE;
2369
2370   return SUCCESS;
2371 }
2372
2373
2374 /* Match a formal argument list.  */
2375
2376 match
2377 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2378 {
2379   gfc_formal_arglist *head, *tail, *p, *q;
2380   char name[GFC_MAX_SYMBOL_LEN + 1];
2381   gfc_symbol *sym;
2382   match m;
2383
2384   head = tail = NULL;
2385
2386   if (gfc_match_char ('(') != MATCH_YES)
2387     {
2388       if (null_flag)
2389         goto ok;
2390       return MATCH_NO;
2391     }
2392
2393   if (gfc_match_char (')') == MATCH_YES)
2394     goto ok;
2395
2396   for (;;)
2397     {
2398       if (gfc_match_char ('*') == MATCH_YES)
2399         sym = NULL;
2400       else
2401         {
2402           m = gfc_match_name (name);
2403           if (m != MATCH_YES)
2404             goto cleanup;
2405
2406           if (gfc_get_symbol (name, NULL, &sym))
2407             goto cleanup;
2408         }
2409
2410       p = gfc_get_formal_arglist ();
2411
2412       if (head == NULL)
2413         head = tail = p;
2414       else
2415         {
2416           tail->next = p;
2417           tail = p;
2418         }
2419
2420       tail->sym = sym;
2421
2422       /* We don't add the VARIABLE flavor because the name could be a
2423          dummy procedure.  We don't apply these attributes to formal
2424          arguments of statement functions.  */
2425       if (sym != NULL && !st_flag
2426           && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2427               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2428         {
2429           m = MATCH_ERROR;
2430           goto cleanup;
2431         }
2432
2433       /* The name of a program unit can be in a different namespace,
2434          so check for it explicitly.  After the statement is accepted,
2435          the name is checked for especially in gfc_get_symbol().  */
2436       if (gfc_new_block != NULL && sym != NULL
2437           && strcmp (sym->name, gfc_new_block->name) == 0)
2438         {
2439           gfc_error ("Name '%s' at %C is the name of the procedure",
2440                      sym->name);
2441           m = MATCH_ERROR;
2442           goto cleanup;
2443         }
2444
2445       if (gfc_match_char (')') == MATCH_YES)
2446         goto ok;
2447
2448       m = gfc_match_char (',');
2449       if (m != MATCH_YES)
2450         {
2451           gfc_error ("Unexpected junk in formal argument list at %C");
2452           goto cleanup;
2453         }
2454     }
2455
2456 ok:
2457   /* Check for duplicate symbols in the formal argument list.  */
2458   if (head != NULL)
2459     {
2460       for (p = head; p->next; p = p->next)
2461         {
2462           if (p->sym == NULL)
2463             continue;
2464
2465           for (q = p->next; q; q = q->next)
2466             if (p->sym == q->sym)
2467               {
2468                 gfc_error
2469                   ("Duplicate symbol '%s' in formal argument list at %C",
2470                    p->sym->name);
2471
2472                 m = MATCH_ERROR;
2473                 goto cleanup;
2474               }
2475         }
2476     }
2477
2478   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2479       FAILURE)
2480     {
2481       m = MATCH_ERROR;
2482       goto cleanup;
2483     }
2484
2485   return MATCH_YES;
2486
2487 cleanup:
2488   gfc_free_formal_arglist (head);
2489   return m;
2490 }
2491
2492
2493 /* Match a RESULT specification following a function declaration or
2494    ENTRY statement.  Also matches the end-of-statement.  */
2495
2496 static match
2497 match_result (gfc_symbol * function, gfc_symbol ** result)
2498 {
2499   char name[GFC_MAX_SYMBOL_LEN + 1];
2500   gfc_symbol *r;
2501   match m;
2502
2503   if (gfc_match (" result (") != MATCH_YES)
2504     return MATCH_NO;
2505
2506   m = gfc_match_name (name);
2507   if (m != MATCH_YES)
2508     return m;
2509
2510   if (gfc_match (" )%t") != MATCH_YES)
2511     {
2512       gfc_error ("Unexpected junk following RESULT variable at %C");
2513       return MATCH_ERROR;
2514     }
2515
2516   if (strcmp (function->name, name) == 0)
2517     {
2518       gfc_error
2519         ("RESULT variable at %C must be different than function name");
2520       return MATCH_ERROR;
2521     }
2522
2523   if (gfc_get_symbol (name, NULL, &r))
2524     return MATCH_ERROR;
2525
2526   if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2527       || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2528     return MATCH_ERROR;
2529
2530   *result = r;
2531
2532   return MATCH_YES;
2533 }
2534
2535
2536 /* Match a function declaration.  */
2537
2538 match
2539 gfc_match_function_decl (void)
2540 {
2541   char name[GFC_MAX_SYMBOL_LEN + 1];
2542   gfc_symbol *sym, *result;
2543   locus old_loc;
2544   match m;
2545
2546   if (gfc_current_state () != COMP_NONE
2547       && gfc_current_state () != COMP_INTERFACE
2548       && gfc_current_state () != COMP_CONTAINS)
2549     return MATCH_NO;
2550
2551   gfc_clear_ts (&current_ts);
2552
2553   old_loc = gfc_current_locus;
2554
2555   m = match_prefix (&current_ts);
2556   if (m != MATCH_YES)
2557     {
2558       gfc_current_locus = old_loc;
2559       return m;
2560     }
2561
2562   if (gfc_match ("function% %n", name) != MATCH_YES)
2563     {
2564       gfc_current_locus = old_loc;
2565       return MATCH_NO;
2566     }
2567
2568   if (get_proc_name (name, &sym))
2569     return MATCH_ERROR;
2570   gfc_new_block = sym;
2571
2572   m = gfc_match_formal_arglist (sym, 0, 0);
2573   if (m == MATCH_NO)
2574     {
2575       gfc_error ("Expected formal argument list in function "
2576                 "definition at %C");
2577       m = MATCH_ERROR;
2578       goto cleanup;
2579     }
2580   else if (m == MATCH_ERROR)
2581     goto cleanup;
2582
2583   result = NULL;
2584
2585   if (gfc_match_eos () != MATCH_YES)
2586     {
2587       /* See if a result variable is present.  */
2588       m = match_result (sym, &result);
2589       if (m == MATCH_NO)
2590         gfc_error ("Unexpected junk after function declaration at %C");
2591
2592       if (m != MATCH_YES)
2593         {
2594           m = MATCH_ERROR;
2595           goto cleanup;
2596         }
2597     }
2598
2599   /* Make changes to the symbol.  */
2600   m = MATCH_ERROR;
2601
2602   if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2603     goto cleanup;
2604
2605   if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2606       || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2607     goto cleanup;
2608
2609   if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2610     {
2611       gfc_error ("Function '%s' at %C already has a type of %s", name,
2612                  gfc_basic_typename (sym->ts.type));
2613       goto cleanup;
2614     }
2615
2616   if (result == NULL)
2617     {
2618       sym->ts = current_ts;
2619       sym->result = sym;
2620     }
2621   else
2622     {
2623       result->ts = current_ts;
2624       sym->result = result;
2625     }
2626
2627   return MATCH_YES;
2628
2629 cleanup:
2630   gfc_current_locus = old_loc;
2631   return m;
2632 }
2633
2634 /* This is mostly a copy of parse.c(add_global_procedure) but modified to pass the
2635    name of the entry, rather than the gfc_current_block name, and to return false
2636    upon finding an existing global entry.  */
2637
2638 static bool
2639 add_global_entry (const char * name, int sub)
2640 {
2641   gfc_gsymbol *s;
2642
2643   s = gfc_get_gsymbol(name);
2644
2645   if (s->defined
2646         || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2647     global_used(s, NULL);
2648   else
2649     {
2650       s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2651       s->where = gfc_current_locus;
2652       s->defined = 1;
2653       return true;
2654     }
2655   return false;
2656 }
2657
2658 /* Match an ENTRY statement.  */
2659
2660 match
2661 gfc_match_entry (void)
2662 {
2663   gfc_symbol *proc;
2664   gfc_symbol *result;
2665   gfc_symbol *entry;
2666   char name[GFC_MAX_SYMBOL_LEN + 1];
2667   gfc_compile_state state;
2668   match m;
2669   gfc_entry_list *el;
2670   locus old_loc;
2671
2672   m = gfc_match_name (name);
2673   if (m != MATCH_YES)
2674     return m;
2675
2676   state = gfc_current_state ();
2677   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2678     {
2679       switch (state)
2680         {
2681           case COMP_PROGRAM:
2682             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2683             break;
2684           case COMP_MODULE:
2685             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2686             break;
2687           case COMP_BLOCK_DATA:
2688             gfc_error
2689               ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2690             break;
2691           case COMP_INTERFACE:
2692             gfc_error
2693               ("ENTRY statement at %C cannot appear within an INTERFACE");
2694             break;
2695           case COMP_DERIVED:
2696             gfc_error
2697               ("ENTRY statement at %C cannot appear "
2698                "within a DERIVED TYPE block");
2699             break;
2700           case COMP_IF:
2701             gfc_error
2702               ("ENTRY statement at %C cannot appear within an IF-THEN block");
2703             break;
2704           case COMP_DO:
2705             gfc_error
2706               ("ENTRY statement at %C cannot appear within a DO block");
2707             break;
2708           case COMP_SELECT:
2709             gfc_error
2710               ("ENTRY statement at %C cannot appear within a SELECT block");
2711             break;
2712           case COMP_FORALL:
2713             gfc_error
2714               ("ENTRY statement at %C cannot appear within a FORALL block");
2715             break;
2716           case COMP_WHERE:
2717             gfc_error
2718               ("ENTRY statement at %C cannot appear within a WHERE block");
2719             break;
2720           case COMP_CONTAINS:
2721             gfc_error
2722               ("ENTRY statement at %C cannot appear "
2723                "within a contained subprogram");
2724             break;
2725           default:
2726             gfc_internal_error ("gfc_match_entry(): Bad state");
2727         }
2728       return MATCH_ERROR;
2729     }
2730
2731   if (gfc_current_ns->parent != NULL
2732       && gfc_current_ns->parent->proc_name
2733       && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2734     {
2735       gfc_error("ENTRY statement at %C cannot appear in a "
2736                 "contained procedure");
2737       return MATCH_ERROR;
2738     }
2739
2740   if (get_proc_name (name, &entry))
2741     return MATCH_ERROR;
2742
2743   proc = gfc_current_block ();
2744
2745   if (state == COMP_SUBROUTINE)
2746     {
2747       /* An entry in a subroutine.  */
2748       if (!add_global_entry (name, 1))
2749         return MATCH_ERROR;
2750
2751       m = gfc_match_formal_arglist (entry, 0, 1);
2752       if (m != MATCH_YES)
2753         return MATCH_ERROR;
2754
2755       if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2756           || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2757         return MATCH_ERROR;
2758     }
2759   else
2760     {
2761       /* An entry in a function.
2762          We need to take special care because writing
2763             ENTRY f()
2764          as
2765             ENTRY f
2766          is allowed, whereas
2767             ENTRY f() RESULT (r)
2768          can't be written as
2769             ENTRY f RESULT (r).  */
2770       if (!add_global_entry (name, 0))
2771         return MATCH_ERROR;
2772
2773       old_loc = gfc_current_locus;
2774       if (gfc_match_eos () == MATCH_YES)
2775         {
2776           gfc_current_locus = old_loc;
2777           /* Match the empty argument list, and add the interface to
2778              the symbol.  */
2779           m = gfc_match_formal_arglist (entry, 0, 1);
2780         }
2781       else
2782         m = gfc_match_formal_arglist (entry, 0, 0);
2783
2784       if (m != MATCH_YES)
2785         return MATCH_ERROR;
2786
2787       result = NULL;
2788
2789       if (gfc_match_eos () == MATCH_YES)
2790         {
2791           if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2792               || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2793             return MATCH_ERROR;
2794
2795           entry->result = entry;
2796         }
2797       else
2798         {
2799           m = match_result (proc, &result);
2800           if (m == MATCH_NO)
2801             gfc_syntax_error (ST_ENTRY);
2802           if (m != MATCH_YES)
2803             return MATCH_ERROR;
2804
2805           if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2806               || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2807               || gfc_add_function (&entry->attr, result->name,
2808                                    NULL) == FAILURE)
2809             return MATCH_ERROR;
2810
2811           entry->result = result;
2812         }
2813
2814       if (proc->attr.recursive && result == NULL)
2815         {
2816           gfc_error ("RESULT attribute required in ENTRY statement at %C");
2817           return MATCH_ERROR;
2818         }
2819     }
2820
2821   if (gfc_match_eos () != MATCH_YES)
2822     {
2823       gfc_syntax_error (ST_ENTRY);
2824       return MATCH_ERROR;
2825     }
2826
2827   entry->attr.recursive = proc->attr.recursive;
2828   entry->attr.elemental = proc->attr.elemental;
2829   entry->attr.pure = proc->attr.pure;
2830
2831   el = gfc_get_entry_list ();
2832   el->sym = entry;
2833   el->next = gfc_current_ns->entries;
2834   gfc_current_ns->entries = el;
2835   if (el->next)
2836     el->id = el->next->id + 1;
2837   else
2838     el->id = 1;
2839
2840   new_st.op = EXEC_ENTRY;
2841   new_st.ext.entry = el;
2842
2843   return MATCH_YES;
2844 }
2845
2846
2847 /* Match a subroutine statement, including optional prefixes.  */
2848
2849 match
2850 gfc_match_subroutine (void)
2851 {
2852   char name[GFC_MAX_SYMBOL_LEN + 1];
2853   gfc_symbol *sym;
2854   match m;
2855
2856   if (gfc_current_state () != COMP_NONE
2857       && gfc_current_state () != COMP_INTERFACE
2858       && gfc_current_state () != COMP_CONTAINS)
2859     return MATCH_NO;
2860
2861   m = match_prefix (NULL);
2862   if (m != MATCH_YES)
2863     return m;
2864
2865   m = gfc_match ("subroutine% %n", name);
2866   if (m != MATCH_YES)
2867     return m;
2868
2869   if (get_proc_name (name, &sym))
2870     return MATCH_ERROR;
2871   gfc_new_block = sym;
2872
2873   if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2874     return MATCH_ERROR;
2875
2876   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2877     return MATCH_ERROR;
2878
2879   if (gfc_match_eos () != MATCH_YES)
2880     {
2881       gfc_syntax_error (ST_SUBROUTINE);
2882       return MATCH_ERROR;
2883     }
2884
2885   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2886     return MATCH_ERROR;
2887
2888   return MATCH_YES;
2889 }
2890
2891
2892 /* Return nonzero if we're currently compiling a contained procedure.  */
2893
2894 static int
2895 contained_procedure (void)
2896 {
2897   gfc_state_data *s;
2898
2899   for (s=gfc_state_stack; s; s=s->previous)
2900     if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2901        && s->previous != NULL
2902        && s->previous->state == COMP_CONTAINS)
2903       return 1;
2904
2905   return 0;
2906 }
2907
2908 /* Set the kind of each enumerator.  The kind is selected such that it is 
2909    interoperable with the corresponding C enumeration type, making
2910    sure that -fshort-enums is honored.  */
2911
2912 static void
2913 set_enum_kind(void)
2914 {
2915   enumerator_history *current_history = NULL;
2916   int kind;
2917   int i;
2918
2919   if (max_enum == NULL || enum_history == NULL)
2920     return;
2921
2922   if (!gfc_option.fshort_enums)
2923     return; 
2924   
2925   i = 0;
2926   do
2927     {
2928       kind = gfc_integer_kinds[i++].kind;
2929     }
2930   while (kind < gfc_c_int_kind 
2931          && gfc_check_integer_range (max_enum->initializer->value.integer,
2932                                      kind) != ARITH_OK);
2933
2934   current_history = enum_history;
2935   while (current_history != NULL)
2936     {
2937       current_history->sym->ts.kind = kind;
2938       current_history = current_history->next;
2939     }
2940 }
2941
2942 /* Match any of the various end-block statements.  Returns the type of
2943    END to the caller.  The END INTERFACE, END IF, END DO and END
2944    SELECT statements cannot be replaced by a single END statement.  */
2945
2946 match
2947 gfc_match_end (gfc_statement * st)
2948 {
2949   char name[GFC_MAX_SYMBOL_LEN + 1];
2950   gfc_compile_state state;
2951   locus old_loc;
2952   const char *block_name;
2953   const char *target;
2954   int eos_ok;
2955   match m;
2956
2957   old_loc = gfc_current_locus;
2958   if (gfc_match ("end") != MATCH_YES)
2959     return MATCH_NO;
2960
2961   state = gfc_current_state ();
2962   block_name =
2963     gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2964
2965   if (state == COMP_CONTAINS)
2966     {
2967       state = gfc_state_stack->previous->state;
2968       block_name = gfc_state_stack->previous->sym == NULL ? NULL
2969         : gfc_state_stack->previous->sym->name;
2970     }
2971
2972   switch (state)
2973     {
2974     case COMP_NONE:
2975     case COMP_PROGRAM:
2976       *st = ST_END_PROGRAM;
2977       target = " program";
2978       eos_ok = 1;
2979       break;
2980
2981     case COMP_SUBROUTINE:
2982       *st = ST_END_SUBROUTINE;
2983       target = " subroutine";
2984       eos_ok = !contained_procedure ();
2985       break;
2986
2987     case COMP_FUNCTION:
2988       *st = ST_END_FUNCTION;
2989       target = " function";
2990       eos_ok = !contained_procedure ();
2991       break;
2992
2993     case COMP_BLOCK_DATA:
2994       *st = ST_END_BLOCK_DATA;
2995       target = " block data";
2996       eos_ok = 1;
2997       break;
2998
2999     case COMP_MODULE:
3000       *st = ST_END_MODULE;
3001       target = " module";
3002       eos_ok = 1;
3003       break;
3004
3005     case COMP_INTERFACE:
3006       *st = ST_END_INTERFACE;
3007       target = " interface";
3008       eos_ok = 0;
3009       break;
3010
3011     case COMP_DERIVED:
3012       *st = ST_END_TYPE;
3013       target = " type";
3014       eos_ok = 0;
3015       break;
3016
3017     case COMP_IF:
3018       *st = ST_ENDIF;
3019       target = " if";
3020       eos_ok = 0;
3021       break;
3022
3023     case COMP_DO:
3024       *st = ST_ENDDO;
3025       target = " do";
3026       eos_ok = 0;
3027       break;
3028
3029     case COMP_SELECT:
3030       *st = ST_END_SELECT;
3031       target = " select";
3032       eos_ok = 0;
3033       break;
3034
3035     case COMP_FORALL:
3036       *st = ST_END_FORALL;
3037       target = " forall";
3038       eos_ok = 0;
3039       break;
3040
3041     case COMP_WHERE:
3042       *st = ST_END_WHERE;
3043       target = " where";
3044       eos_ok = 0;
3045       break;
3046
3047     case COMP_ENUM:
3048       *st = ST_END_ENUM;
3049       target = " enum";
3050       eos_ok = 0;
3051       last_initializer = NULL;
3052       set_enum_kind ();
3053       gfc_free_enum_history ();
3054       break;
3055
3056     default:
3057       gfc_error ("Unexpected END statement at %C");
3058       goto cleanup;
3059     }
3060
3061   if (gfc_match_eos () == MATCH_YES)
3062     {
3063       if (!eos_ok)
3064         {
3065           /* We would have required END [something]  */
3066           gfc_error ("%s statement expected at %L",
3067                      gfc_ascii_statement (*st), &old_loc);
3068           goto cleanup;
3069         }
3070
3071       return MATCH_YES;
3072     }
3073
3074   /* Verify that we've got the sort of end-block that we're expecting.  */
3075   if (gfc_match (target) != MATCH_YES)
3076     {
3077       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3078       goto cleanup;
3079     }
3080
3081   /* If we're at the end, make sure a block name wasn't required.  */
3082   if (gfc_match_eos () == MATCH_YES)
3083     {
3084
3085       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3086         return MATCH_YES;
3087
3088       if (gfc_current_block () == NULL)
3089         return MATCH_YES;
3090
3091       gfc_error ("Expected block name of '%s' in %s statement at %C",
3092                  block_name, gfc_ascii_statement (*st));
3093
3094       return MATCH_ERROR;
3095     }
3096
3097   /* END INTERFACE has a special handler for its several possible endings.  */
3098   if (*st == ST_END_INTERFACE)
3099     return gfc_match_end_interface ();
3100
3101   /* We haven't hit the end of statement, so what is left must be an end-name.  */
3102   m = gfc_match_space ();
3103   if (m == MATCH_YES)
3104     m = gfc_match_name (name);
3105
3106   if (m == MATCH_NO)
3107     gfc_error ("Expected terminating name at %C");
3108   if (m != MATCH_YES)
3109     goto cleanup;
3110
3111   if (block_name == NULL)
3112     goto syntax;
3113
3114   if (strcmp (name, block_name) != 0)
3115     {
3116       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3117                  gfc_ascii_statement (*st));
3118       goto cleanup;
3119     }
3120
3121   if (gfc_match_eos () == MATCH_YES)
3122     return MATCH_YES;
3123
3124 syntax:
3125   gfc_syntax_error (*st);
3126
3127 cleanup:
3128   gfc_current_locus = old_loc;
3129   return MATCH_ERROR;
3130 }
3131
3132
3133
3134 /***************** Attribute declaration statements ****************/
3135
3136 /* Set the attribute of a single variable.  */
3137
3138 static match
3139 attr_decl1 (void)
3140 {
3141   char name[GFC_MAX_SYMBOL_LEN + 1];
3142   gfc_array_spec *as;
3143   gfc_symbol *sym;
3144   locus var_locus;
3145   match m;
3146
3147   as = NULL;
3148
3149   m = gfc_match_name (name);
3150   if (m != MATCH_YES)
3151     goto cleanup;
3152
3153   if (find_special (name, &sym))
3154     return MATCH_ERROR;
3155
3156   var_locus = gfc_current_locus;
3157
3158   /* Deal with possible array specification for certain attributes.  */
3159   if (current_attr.dimension
3160       || current_attr.allocatable
3161       || current_attr.pointer
3162       || current_attr.target)
3163     {
3164       m = gfc_match_array_spec (&as);
3165       if (m == MATCH_ERROR)
3166         goto cleanup;
3167
3168       if (current_attr.dimension && m == MATCH_NO)
3169         {
3170           gfc_error
3171             ("Missing array specification at %L in DIMENSION statement",
3172              &var_locus);
3173           m = MATCH_ERROR;
3174           goto cleanup;
3175         }
3176
3177       if ((current_attr.allocatable || current_attr.pointer)
3178           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3179         {
3180           gfc_error ("Array specification must be deferred at %L",
3181                      &var_locus);
3182           m = MATCH_ERROR;
3183           goto cleanup;
3184         }
3185     }
3186
3187   /* Update symbol table.  DIMENSION attribute is set in gfc_set_array_spec().  */
3188   if (current_attr.dimension == 0
3189       && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3190     {
3191       m = MATCH_ERROR;
3192       goto cleanup;
3193     }
3194
3195   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3196     {
3197       m = MATCH_ERROR;
3198       goto cleanup;
3199     }
3200     
3201   if (sym->attr.cray_pointee && sym->as != NULL)
3202     {
3203       /* Fix the array spec.  */
3204       m = gfc_mod_pointee_as (sym->as);         
3205       if (m == MATCH_ERROR)
3206         goto cleanup;
3207     }
3208
3209   if (gfc_add_attribute (&sym->attr, &var_locus, current_attr.intent) == FAILURE)
3210     {
3211       m = MATCH_ERROR;
3212       goto cleanup;
3213     }
3214
3215   if ((current_attr.external || current_attr.intrinsic)
3216       && sym->attr.flavor != FL_PROCEDURE
3217       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3218     {
3219       m = MATCH_ERROR;
3220       goto cleanup;
3221     }
3222
3223   return MATCH_YES;
3224
3225 cleanup:
3226   gfc_free_array_spec (as);
3227   return m;
3228 }
3229
3230
3231 /* Generic attribute declaration subroutine.  Used for attributes that
3232    just have a list of names.  */
3233
3234 static match
3235 attr_decl (void)
3236 {
3237   match m;
3238
3239   /* Gobble the optional double colon, by simply ignoring the result
3240      of gfc_match().  */
3241   gfc_match (" ::");
3242
3243   for (;;)
3244     {
3245       m = attr_decl1 ();
3246       if (m != MATCH_YES)
3247         break;
3248
3249       if (gfc_match_eos () == MATCH_YES)
3250         {
3251           m = MATCH_YES;
3252           break;
3253         }
3254
3255       if (gfc_match_char (',') != MATCH_YES)
3256         {
3257           gfc_error ("Unexpected character in variable list at %C");
3258           m = MATCH_ERROR;
3259           break;
3260         }
3261     }
3262
3263   return m;
3264 }
3265
3266
3267 /* This routine matches Cray Pointer declarations of the form:
3268    pointer ( <pointer>, <pointee> )
3269    or
3270    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...   
3271    The pointer, if already declared, should be an integer.  Otherwise, we 
3272    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
3273    be either a scalar, or an array declaration.  No space is allocated for
3274    the pointee.  For the statement 
3275    pointer (ipt, ar(10))
3276    any subsequent uses of ar will be translated (in C-notation) as
3277    ar(i) => ((<type> *) ipt)(i)   
3278    After gimplification, pointee variable will disappear in the code.  */
3279
3280 static match
3281 cray_pointer_decl (void)
3282 {
3283   match m;
3284   gfc_array_spec *as;
3285   gfc_symbol *cptr; /* Pointer symbol.  */
3286   gfc_symbol *cpte; /* Pointee symbol.  */
3287   locus var_locus;
3288   bool done = false;
3289
3290   while (!done)
3291     {
3292       if (gfc_match_char ('(') != MATCH_YES)
3293         {
3294           gfc_error ("Expected '(' at %C");
3295           return MATCH_ERROR;   
3296         }
3297  
3298       /* Match pointer.  */
3299       var_locus = gfc_current_locus;
3300       gfc_clear_attr (&current_attr);
3301       gfc_add_cray_pointer (&current_attr, &var_locus);
3302       current_ts.type = BT_INTEGER;
3303       current_ts.kind = gfc_index_integer_kind;
3304
3305       m = gfc_match_symbol (&cptr, 0);  
3306       if (m != MATCH_YES)
3307         {
3308           gfc_error ("Expected variable name at %C");
3309           return m;
3310         }
3311   
3312       if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3313         return MATCH_ERROR;
3314
3315       gfc_set_sym_referenced (cptr);      
3316
3317       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
3318         {
3319           cptr->ts.type = BT_INTEGER;
3320           cptr->ts.kind = gfc_index_integer_kind; 
3321         }
3322       else if (cptr->ts.type != BT_INTEGER)
3323         {
3324           gfc_error ("Cray pointer at %C must be an integer.");
3325           return MATCH_ERROR;
3326         }
3327       else if (cptr->ts.kind < gfc_index_integer_kind)
3328         gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3329                      " memory addresses require %d bytes.",
3330                      cptr->ts.kind,
3331                      gfc_index_integer_kind);
3332
3333       if (gfc_match_char (',') != MATCH_YES)
3334         {
3335           gfc_error ("Expected \",\" at %C");
3336           return MATCH_ERROR;    
3337         }
3338
3339       /* Match Pointee.  */  
3340       var_locus = gfc_current_locus;
3341       gfc_clear_attr (&current_attr);
3342       gfc_add_cray_pointee (&current_attr, &var_locus);
3343       current_ts.type = BT_UNKNOWN;
3344       current_ts.kind = 0;
3345
3346       m = gfc_match_symbol (&cpte, 0);
3347       if (m != MATCH_YES)
3348         {
3349           gfc_error ("Expected variable name at %C");
3350           return m;
3351         }
3352        
3353       /* Check for an optional array spec.  */
3354       m = gfc_match_array_spec (&as);
3355       if (m == MATCH_ERROR)
3356         {
3357           gfc_free_array_spec (as);
3358           return m;
3359         }
3360       else if (m == MATCH_NO)
3361         {
3362           gfc_free_array_spec (as);
3363           as = NULL;
3364         }   
3365
3366       if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3367         return MATCH_ERROR;
3368
3369       gfc_set_sym_referenced (cpte);
3370
3371       if (cpte->as == NULL)
3372         {
3373           if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3374             gfc_internal_error ("Couldn't set Cray pointee array spec.");
3375         }
3376       else if (as != NULL)
3377         {
3378           gfc_error ("Duplicate array spec for Cray pointee at %C.");
3379           gfc_free_array_spec (as);
3380           return MATCH_ERROR;
3381         }
3382       
3383       as = NULL;
3384     
3385       if (cpte->as != NULL)
3386         {
3387           /* Fix array spec.  */
3388           m = gfc_mod_pointee_as (cpte->as);
3389           if (m == MATCH_ERROR)
3390             return m;
3391         } 
3392    
3393       /* Point the Pointee at the Pointer.  */
3394       cpte->cp_pointer = cptr;
3395
3396       if (gfc_match_char (')') != MATCH_YES)
3397         {
3398           gfc_error ("Expected \")\" at %C");
3399           return MATCH_ERROR;    
3400         }
3401       m = gfc_match_char (',');
3402       if (m != MATCH_YES)
3403         done = true; /* Stop searching for more declarations.  */
3404
3405     }
3406   
3407   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
3408       || gfc_match_eos () != MATCH_YES)
3409     {
3410       gfc_error ("Expected \",\" or end of statement at %C");
3411       return MATCH_ERROR;
3412     }
3413   return MATCH_YES;
3414 }
3415
3416
3417 match
3418 gfc_match_external (void)
3419 {
3420
3421   gfc_clear_attr (&current_attr);
3422   current_attr.external = 1;
3423
3424   return attr_decl ();
3425 }
3426
3427
3428
3429 match
3430 gfc_match_intent (void)
3431 {
3432   sym_intent intent;
3433
3434   intent = match_intent_spec ();
3435   if (intent == INTENT_UNKNOWN)
3436     return MATCH_ERROR;
3437
3438   gfc_clear_attr (&current_attr);
3439   current_attr.intent = intent;
3440
3441   return attr_decl ();
3442 }
3443
3444
3445 match
3446 gfc_match_intrinsic (void)
3447 {
3448
3449   gfc_clear_attr (&current_attr);
3450   current_attr.intrinsic = 1;
3451
3452   return attr_decl ();
3453 }
3454
3455
3456 match
3457 gfc_match_optional (void)
3458 {
3459
3460   gfc_clear_attr (&current_attr);
3461   current_attr.optional = 1;
3462
3463   return attr_decl ();
3464 }
3465
3466
3467 match
3468 gfc_match_pointer (void)
3469 {
3470   gfc_gobble_whitespace ();
3471   if (gfc_peek_char () == '(')
3472     {
3473       if (!gfc_option.flag_cray_pointer)
3474         {
3475           gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3476                      " flag.");
3477           return MATCH_ERROR;
3478         }
3479       return cray_pointer_decl ();
3480     }
3481   else
3482     {
3483       gfc_clear_attr (&current_attr);
3484       current_attr.pointer = 1;
3485     
3486       return attr_decl ();
3487     }
3488 }
3489
3490
3491 match
3492 gfc_match_allocatable (void)
3493 {
3494
3495   gfc_clear_attr (&current_attr);
3496   current_attr.allocatable = 1;
3497
3498   return attr_decl ();
3499 }
3500
3501
3502 match
3503 gfc_match_dimension (void)
3504 {
3505
3506   gfc_clear_attr (&current_attr);
3507   current_attr.dimension = 1;
3508
3509   return attr_decl ();
3510 }
3511
3512
3513 match
3514 gfc_match_target (void)
3515 {
3516
3517   gfc_clear_attr (&current_attr);
3518   current_attr.target = 1;
3519
3520   return attr_decl ();
3521 }
3522
3523
3524 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3525    statement.  */
3526
3527 static match
3528 access_attr_decl (gfc_statement st)
3529 {
3530   char name[GFC_MAX_SYMBOL_LEN + 1];
3531   interface_type type;
3532   gfc_user_op *uop;
3533   gfc_symbol *sym;
3534   gfc_intrinsic_op operator;
3535   match m;
3536
3537   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3538     goto done;
3539
3540   for (;;)
3541     {
3542       m = gfc_match_generic_spec (&type, name, &operator);
3543       if (m == MATCH_NO)
3544         goto syntax;
3545       if (m == MATCH_ERROR)
3546         return MATCH_ERROR;
3547
3548       switch (type)
3549         {
3550         case INTERFACE_NAMELESS:
3551           goto syntax;
3552
3553         case INTERFACE_GENERIC:
3554           if (gfc_get_symbol (name, NULL, &sym))
3555             goto done;
3556
3557           if (gfc_add_access (&sym->attr,
3558                               (st ==
3559                                ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3560                               sym->name, NULL) == FAILURE)
3561             return MATCH_ERROR;
3562
3563           break;
3564
3565         case INTERFACE_INTRINSIC_OP:
3566           if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3567             {
3568               gfc_current_ns->operator_access[operator] =
3569                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3570             }
3571           else
3572             {
3573               gfc_error ("Access specification of the %s operator at %C has "
3574                          "already been specified", gfc_op2string (operator));
3575               goto done;
3576             }
3577
3578           break;
3579
3580         case INTERFACE_USER_OP:
3581           uop = gfc_get_uop (name);
3582
3583           if (uop->access == ACCESS_UNKNOWN)
3584             {
3585               uop->access =
3586                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3587             }
3588           else
3589             {
3590               gfc_error
3591                 ("Access specification of the .%s. operator at %C has "
3592                  "already been specified", sym->name);
3593               goto done;
3594             }
3595
3596           break;
3597         }
3598
3599       if (gfc_match_char (',') == MATCH_NO)
3600         break;
3601     }
3602
3603   if (gfc_match_eos () != MATCH_YES)
3604     goto syntax;
3605   return MATCH_YES;
3606
3607 syntax:
3608   gfc_syntax_error (st);
3609
3610 done:
3611   return MATCH_ERROR;
3612 }
3613
3614
3615 /* The PRIVATE statement is a bit weird in that it can be a attribute
3616    declaration, but also works as a standlone statement inside of a
3617    type declaration or a module.  */
3618
3619 match
3620 gfc_match_private (gfc_statement * st)
3621 {
3622
3623   if (gfc_match ("private") != MATCH_YES)
3624     return MATCH_NO;
3625
3626   if (gfc_current_state () == COMP_DERIVED)
3627     {
3628       if (gfc_match_eos () == MATCH_YES)
3629         {
3630           *st = ST_PRIVATE;
3631           return MATCH_YES;
3632         }
3633
3634       gfc_syntax_error (ST_PRIVATE);
3635       return MATCH_ERROR;
3636     }
3637
3638   if (gfc_match_eos () == MATCH_YES)
3639     {
3640       *st = ST_PRIVATE;
3641       return MATCH_YES;
3642     }
3643
3644   *st = ST_ATTR_DECL;
3645   return access_attr_decl (ST_PRIVATE);
3646 }
3647
3648
3649 match
3650 gfc_match_public (gfc_statement * st)
3651 {
3652
3653   if (gfc_match ("public") != MATCH_YES)
3654     return MATCH_NO;
3655
3656   if (gfc_match_eos () == MATCH_YES)
3657     {
3658       *st = ST_PUBLIC;
3659       return MATCH_YES;
3660     }
3661
3662   *st = ST_ATTR_DECL;
3663   return access_attr_decl (ST_PUBLIC);
3664 }
3665
3666
3667 /* Workhorse for gfc_match_parameter.  */
3668
3669 static match
3670 do_parm (void)
3671 {
3672   gfc_symbol *sym;
3673   gfc_expr *init;
3674   match m;
3675
3676   m = gfc_match_symbol (&sym, 0);
3677   if (m == MATCH_NO)
3678     gfc_error ("Expected variable name at %C in PARAMETER statement");
3679
3680   if (m != MATCH_YES)
3681     return m;
3682
3683   if (gfc_match_char ('=') == MATCH_NO)
3684     {
3685       gfc_error ("Expected = sign in PARAMETER statement at %C");
3686       return MATCH_ERROR;
3687     }
3688
3689   m = gfc_match_init_expr (&init);
3690   if (m == MATCH_NO)
3691     gfc_error ("Expected expression at %C in PARAMETER statement");
3692   if (m != MATCH_YES)
3693     return m;
3694
3695   if (sym->ts.type == BT_UNKNOWN
3696       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3697     {
3698       m = MATCH_ERROR;
3699       goto cleanup;
3700     }
3701
3702   if (gfc_check_assign_symbol (sym, init) == FAILURE
3703       || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3704     {
3705       m = MATCH_ERROR;
3706       goto cleanup;
3707     }
3708
3709   if (sym->ts.type == BT_CHARACTER
3710       && sym->ts.cl != NULL
3711       && sym->ts.cl->length != NULL
3712       && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3713       && init->expr_type == EXPR_CONSTANT
3714       && init->ts.type == BT_CHARACTER
3715       && init->ts.kind == 1)
3716     gfc_set_constant_character_len (
3717       mpz_get_si (sym->ts.cl->length->value.integer), init);
3718
3719   sym->value = init;
3720   return MATCH_YES;
3721
3722 cleanup:
3723   gfc_free_expr (init);
3724   return m;
3725 }
3726
3727
3728 /* Match a parameter statement, with the weird syntax that these have.  */
3729
3730 match
3731 gfc_match_parameter (void)
3732 {
3733   match m;
3734
3735   if (gfc_match_char ('(') == MATCH_NO)
3736     return MATCH_NO;
3737
3738   for (;;)
3739     {
3740       m = do_parm ();
3741       if (m != MATCH_YES)
3742         break;
3743
3744       if (gfc_match (" )%t") == MATCH_YES)
3745         break;
3746
3747       if (gfc_match_char (',') != MATCH_YES)
3748         {
3749           gfc_error ("Unexpected characters in PARAMETER statement at %C");
3750           m = MATCH_ERROR;
3751           break;
3752         }
3753     }
3754
3755   return m;
3756 }
3757
3758
3759 /* Save statements have a special syntax.  */
3760
3761 match
3762 gfc_match_save (void)
3763 {
3764   char n[GFC_MAX_SYMBOL_LEN+1];
3765   gfc_common_head *c;
3766   gfc_symbol *sym;
3767   match m;
3768
3769   if (gfc_match_eos () == MATCH_YES)
3770     {
3771       if (gfc_current_ns->seen_save)
3772         {
3773           if (gfc_notify_std (GFC_STD_LEGACY, 
3774                               "Blanket SAVE statement at %C follows previous "
3775                               "SAVE statement")
3776               == FAILURE)
3777             return MATCH_ERROR;
3778         }
3779
3780       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3781       return MATCH_YES;
3782     }
3783
3784   if (gfc_current_ns->save_all)
3785     {
3786       if (gfc_notify_std (GFC_STD_LEGACY, 
3787                           "SAVE statement at %C follows blanket SAVE statement")
3788           == FAILURE)
3789         return MATCH_ERROR;
3790     }
3791
3792   gfc_match (" ::");
3793
3794   for (;;)
3795     {
3796       m = gfc_match_symbol (&sym, 0);
3797       switch (m)
3798         {
3799         case MATCH_YES:
3800           if (gfc_add_save (&sym->attr, sym->name,
3801                             &gfc_current_locus) == FAILURE)
3802             return MATCH_ERROR;
3803           goto next_item;
3804
3805         case MATCH_NO:
3806           break;
3807
3808         case MATCH_ERROR:
3809           return MATCH_ERROR;
3810         }
3811
3812       m = gfc_match (" / %n /", &n);
3813       if (m == MATCH_ERROR)
3814         return MATCH_ERROR;
3815       if (m == MATCH_NO)
3816         goto syntax;
3817
3818       c = gfc_get_common (n, 0);
3819       c->saved = 1;
3820
3821       gfc_current_ns->seen_save = 1;
3822
3823     next_item:
3824       if (gfc_match_eos () == MATCH_YES)
3825         break;
3826       if (gfc_match_char (',') != MATCH_YES)
3827         goto syntax;
3828     }
3829
3830   return MATCH_YES;
3831
3832 syntax:
3833   gfc_error ("Syntax error in SAVE statement at %C");
3834   return MATCH_ERROR;
3835 }
3836
3837
3838 /* Match a module procedure statement.  Note that we have to modify
3839    symbols in the parent's namespace because the current one was there
3840    to receive symbols that are in an interface's formal argument list.  */
3841
3842 match
3843 gfc_match_modproc (void)
3844 {
3845   char name[GFC_MAX_SYMBOL_LEN + 1];
3846   gfc_symbol *sym;
3847   match m;
3848
3849   if (gfc_state_stack->state != COMP_INTERFACE
3850       || gfc_state_stack->previous == NULL
3851       || current_interface.type == INTERFACE_NAMELESS)
3852     {
3853       gfc_error
3854         ("MODULE PROCEDURE at %C must be in a generic module interface");
3855       return MATCH_ERROR;
3856     }
3857
3858   for (;;)
3859     {
3860       m = gfc_match_name (name);
3861       if (m == MATCH_NO)
3862         goto syntax;
3863       if (m != MATCH_YES)
3864         return MATCH_ERROR;
3865
3866       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3867         return MATCH_ERROR;
3868
3869       if (sym->attr.proc != PROC_MODULE
3870           && gfc_add_procedure (&sym->attr, PROC_MODULE,
3871                                 sym->name, NULL) == FAILURE)
3872         return MATCH_ERROR;
3873
3874       if (gfc_add_interface (sym) == FAILURE)
3875         return MATCH_ERROR;
3876
3877       if (gfc_match_eos () == MATCH_YES)
3878         break;
3879       if (gfc_match_char (',') != MATCH_YES)
3880         goto syntax;
3881     }
3882
3883   return MATCH_YES;
3884
3885 syntax:
3886   gfc_syntax_error (ST_MODULE_PROC);
3887   return MATCH_ERROR;
3888 }
3889
3890
3891 /* Match the beginning of a derived type declaration.  If a type name
3892    was the result of a function, then it is possible to have a symbol
3893    already to be known as a derived type yet have no components.  */
3894
3895 match
3896 gfc_match_derived_decl (void)
3897 {
3898   char name[GFC_MAX_SYMBOL_LEN + 1];
3899   symbol_attribute attr;
3900   gfc_symbol *sym;
3901   match m;
3902
3903   if (gfc_current_state () == COMP_DERIVED)
3904     return MATCH_NO;
3905
3906   gfc_clear_attr (&attr);
3907
3908 loop:
3909   if (gfc_match (" , private") == MATCH_YES)
3910     {
3911       if (gfc_find_state (COMP_MODULE) == FAILURE)
3912         {
3913           gfc_error
3914             ("Derived type at %C can only be PRIVATE within a MODULE");
3915           return MATCH_ERROR;
3916         }
3917
3918       if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3919         return MATCH_ERROR;
3920       goto loop;
3921     }
3922
3923   if (gfc_match (" , public") == MATCH_YES)
3924     {
3925       if (gfc_find_state (COMP_MODULE) == FAILURE)
3926         {
3927           gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3928           return MATCH_ERROR;
3929         }
3930
3931       if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3932         return MATCH_ERROR;
3933       goto loop;
3934     }
3935
3936   if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3937     {
3938       gfc_error ("Expected :: in TYPE definition at %C");
3939       return MATCH_ERROR;
3940     }
3941
3942   m = gfc_match (" %n%t", name);
3943   if (m != MATCH_YES)
3944     return m;
3945
3946   /* Make sure the name isn't the name of an intrinsic type.  The
3947      'double precision' type doesn't get past the name matcher.  */
3948   if (strcmp (name, "integer") == 0
3949       || strcmp (name, "real") == 0
3950       || strcmp (name, "character") == 0
3951       || strcmp (name, "logical") == 0
3952       || strcmp (name, "complex") == 0)
3953     {
3954       gfc_error
3955         ("Type name '%s' at %C cannot be the same as an intrinsic type",
3956          name);
3957       return MATCH_ERROR;
3958     }
3959
3960   if (gfc_get_symbol (name, NULL, &sym))
3961     return MATCH_ERROR;
3962
3963   if (sym->ts.type != BT_UNKNOWN)
3964     {
3965       gfc_error ("Derived type name '%s' at %C already has a basic type "
3966                  "of %s", sym->name, gfc_typename (&sym->ts));
3967       return MATCH_ERROR;
3968     }
3969
3970   /* The symbol may already have the derived attribute without the
3971      components.  The ways this can happen is via a function
3972      definition, an INTRINSIC statement or a subtype in another
3973      derived type that is a pointer.  The first part of the AND clause
3974      is true if a the symbol is not the return value of a function.  */
3975   if (sym->attr.flavor != FL_DERIVED
3976       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
3977     return MATCH_ERROR;
3978
3979   if (sym->components != NULL)
3980     {
3981       gfc_error
3982         ("Derived type definition of '%s' at %C has already been defined",
3983          sym->name);
3984       return MATCH_ERROR;
3985     }
3986
3987   if (attr.access != ACCESS_UNKNOWN
3988       && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
3989     return MATCH_ERROR;
3990
3991   gfc_new_block = sym;
3992
3993   return MATCH_YES;
3994 }
3995
3996
3997 /* Cray Pointees can be declared as: 
3998       pointer (ipt, a (n,m,...,*)) 
3999    By default, this is treated as an AS_ASSUMED_SIZE array.  We'll
4000    cheat and set a constant bound of 1 for the last dimension, if this
4001    is the case. Since there is no bounds-checking for Cray Pointees,
4002    this will be okay.  */
4003
4004 try
4005 gfc_mod_pointee_as (gfc_array_spec *as)
4006 {
4007   as->cray_pointee = true; /* This will be useful to know later.  */
4008   if (as->type == AS_ASSUMED_SIZE)
4009     {
4010       as->type = AS_EXPLICIT;
4011       as->upper[as->rank - 1] = gfc_int_expr (1);
4012       as->cp_was_assumed = true;
4013     }
4014   else if (as->type == AS_ASSUMED_SHAPE)
4015     {
4016       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4017       return MATCH_ERROR;
4018     }
4019   return MATCH_YES;
4020 }
4021
4022
4023 /* Match the enum definition statement, here we are trying to match 
4024    the first line of enum definition statement.  
4025    Returns MATCH_YES if match is found.  */
4026
4027 match
4028 gfc_match_enum (void)
4029 {
4030   match m;
4031   
4032   m = gfc_match_eos ();
4033   if (m != MATCH_YES)
4034     return m;
4035
4036   if (gfc_notify_std (GFC_STD_F2003, 
4037                       "New in Fortran 2003: ENUM AND ENUMERATOR at %C")
4038       == FAILURE)
4039     return MATCH_ERROR;
4040
4041   return MATCH_YES;
4042 }
4043
4044
4045 /* Match the enumerator definition statement. */
4046
4047 match
4048 gfc_match_enumerator_def (void)
4049 {
4050   match m;
4051   int elem; 
4052   
4053   gfc_clear_ts (&current_ts);
4054   
4055   m = gfc_match (" enumerator");
4056   if (m != MATCH_YES)
4057     return m;
4058   
4059   if (gfc_current_state () != COMP_ENUM)
4060     {
4061       gfc_error ("ENUM definition statement expected before %C");
4062       gfc_free_enum_history ();
4063       return MATCH_ERROR;
4064     }
4065
4066   (&current_ts)->type = BT_INTEGER;
4067   (&current_ts)->kind = gfc_c_int_kind;
4068   
4069   m = match_attr_spec ();
4070   if (m == MATCH_ERROR)
4071     {
4072       m = MATCH_NO;
4073       goto cleanup;
4074     }
4075
4076   elem = 1;
4077   for (;;)
4078     {
4079       m = variable_decl (elem++);
4080       if (m == MATCH_ERROR)
4081         goto cleanup;
4082       if (m == MATCH_NO)
4083         break;
4084
4085       if (gfc_match_eos () == MATCH_YES)
4086         goto cleanup;
4087       if (gfc_match_char (',') != MATCH_YES)
4088         break;
4089     }
4090
4091   if (gfc_current_state () == COMP_ENUM)
4092     {
4093       gfc_free_enum_history ();
4094       gfc_error ("Syntax error in ENUMERATOR definition at %C");
4095       m = MATCH_ERROR;
4096     }
4097
4098 cleanup:
4099   gfc_free_array_spec (current_as);
4100   current_as = NULL;
4101   return m;
4102
4103 }
4104