OSDN Git Service

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