OSDN Git Service

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