OSDN Git Service

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