OSDN Git Service

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