OSDN Git Service

2004-05-17 Steve Kargl <kargls@comcast.net>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2    Copyright (C) 2002 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include <string.h>
28
29
30 /* This flag is set if a an old-style length selector is matched
31    during a type-declaration statement.  */
32
33 static int old_char_selector;
34
35 /* When variables aquire 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 /* gfc_new_block points to the symbol of a newly matched block.  */
47
48 gfc_symbol *gfc_new_block;
49
50
51 /* Match an intent specification.  Since this can only happen after an
52    INTENT word, a legal intent-spec must follow.  */
53
54 static sym_intent
55 match_intent_spec (void)
56 {
57
58   if (gfc_match (" ( in out )") == MATCH_YES)
59     return INTENT_INOUT;
60   if (gfc_match (" ( in )") == MATCH_YES)
61     return INTENT_IN;
62   if (gfc_match (" ( out )") == MATCH_YES)
63     return INTENT_OUT;
64
65   gfc_error ("Bad INTENT specification at %C");
66   return INTENT_UNKNOWN;
67 }
68
69
70 /* Matches a character length specification, which is either a
71    specification expression or a '*'.  */
72
73 static match
74 char_len_param_value (gfc_expr ** expr)
75 {
76
77   if (gfc_match_char ('*') == MATCH_YES)
78     {
79       *expr = NULL;
80       return MATCH_YES;
81     }
82
83   return gfc_match_expr (expr);
84 }
85
86
87 /* A character length is a '*' followed by a literal integer or a
88    char_len_param_value in parenthesis.  */
89
90 static match
91 match_char_length (gfc_expr ** expr)
92 {
93   int length;
94   match m;
95
96   m = gfc_match_char ('*');
97   if (m != MATCH_YES)
98     return m;
99
100   m = gfc_match_small_literal_int (&length);
101   if (m == MATCH_ERROR)
102     return m;
103
104   if (m == MATCH_YES)
105     {
106       *expr = gfc_int_expr (length);
107       return m;
108     }
109
110   if (gfc_match_char ('(') == MATCH_NO)
111     goto syntax;
112
113   m = char_len_param_value (expr);
114   if (m == MATCH_ERROR)
115     return m;
116   if (m == MATCH_NO)
117     goto syntax;
118
119   if (gfc_match_char (')') == MATCH_NO)
120     {
121       gfc_free_expr (*expr);
122       *expr = NULL;
123       goto syntax;
124     }
125
126   return MATCH_YES;
127
128 syntax:
129   gfc_error ("Syntax error in character length specification at %C");
130   return MATCH_ERROR;
131 }
132
133
134 /* Special subroutine for finding a symbol.  If we're compiling a
135    function or subroutine and the parent compilation unit is an
136    interface, then check to see if the name we've been given is the
137    name of the interface (located in another namespace).  If so,
138    return that symbol.  If not, use gfc_get_symbol().  */
139
140 static int
141 find_special (const char *name, gfc_symbol ** result)
142 {
143   gfc_state_data *s;
144
145   if (gfc_current_state () != COMP_SUBROUTINE
146       && gfc_current_state () != COMP_FUNCTION)
147     goto normal;
148
149   s = gfc_state_stack->previous;
150   if (s == NULL)
151     goto normal;
152
153   if (s->state != COMP_INTERFACE)
154     goto normal;
155   if (s->sym == NULL)
156     goto normal;                /* Nameless interface */
157
158   if (strcmp (name, s->sym->name) == 0)
159     {
160       *result = s->sym;
161       return 0;
162     }
163
164 normal:
165   return gfc_get_symbol (name, NULL, result);
166 }
167
168
169 /* Special subroutine for getting a symbol node associated with a
170    procedure name, used in SUBROUTINE and FUNCTION statements.  The
171    symbol is created in the parent using with symtree node in the
172    child unit pointing to the symbol.  If the current namespace has no
173    parent, then the symbol is just created in the current unit.  */
174
175 static int
176 get_proc_name (const char *name, gfc_symbol ** result)
177 {
178   gfc_symtree *st;
179   gfc_symbol *sym;
180   int rc;
181
182   if (gfc_current_ns->parent == NULL)
183     return gfc_get_symbol (name, NULL, result);
184
185   rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
186   if (*result == NULL)
187     return rc;
188
189   /* Deal with ENTRY problem */
190
191   st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
192
193   sym = *result;
194   st->n.sym = sym;
195   sym->refs++;
196
197   /* See if the procedure should be a module procedure */
198
199   if (sym->ns->proc_name != NULL
200       && sym->ns->proc_name->attr.flavor == FL_MODULE
201       && sym->attr.proc != PROC_MODULE
202       && gfc_add_procedure (&sym->attr, PROC_MODULE, NULL) == FAILURE)
203     rc = 2;
204
205   return rc;
206 }
207
208
209 /* Function called by variable_decl() that adds a name to the symbol
210    table.  */
211
212 static try
213 build_sym (const char *name, gfc_charlen * cl,
214            gfc_array_spec ** as, locus * var_locus)
215 {
216   symbol_attribute attr;
217   gfc_symbol *sym;
218
219   if (find_special (name, &sym))
220     return FAILURE;
221
222   /* Start updating the symbol table.  Add basic type attribute
223      if present.  */
224   if (current_ts.type != BT_UNKNOWN
225       &&(sym->attr.implicit_type == 0
226          || !gfc_compare_types (&sym->ts, &current_ts))
227       && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
228     return FAILURE;
229
230   if (sym->ts.type == BT_CHARACTER)
231     sym->ts.cl = cl;
232
233   /* Add dimension attribute if present.  */
234   if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
235     return FAILURE;
236   *as = NULL;
237
238   /* Add attribute to symbol.  The copy is so that we can reset the
239      dimension attribute.  */
240   attr = current_attr;
241   attr.dimension = 0;
242
243   if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
244     return FAILURE;
245
246   return SUCCESS;
247 }
248
249
250 /* Function called by variable_decl() that adds an initialization
251    expression to a symbol.  */
252
253 static try
254 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
255                       locus * var_locus)
256 {
257   int i;
258   symbol_attribute attr;
259   gfc_symbol *sym;
260   gfc_expr *init;
261
262   init = *initp;
263   if (find_special (name, &sym))
264     return FAILURE;
265
266   attr = sym->attr;
267
268   /* If this symbol is confirming an implicit parameter type,
269      then an initialization expression is not allowed.  */
270   if (attr.flavor == FL_PARAMETER
271       && sym->value != NULL
272       && *initp != NULL)
273     {
274       gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
275                  sym->name);
276       return FAILURE;
277     }
278
279   if (attr.in_common
280       && !attr.data
281       && *initp != NULL)
282     {
283       gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
284                  sym->name);
285       return FAILURE;
286     }
287
288   if (init == NULL)
289     {
290       /* An initializer is required for PARAMETER declarations.  */
291       if (attr.flavor == FL_PARAMETER)
292         {
293           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
294           return FAILURE;
295         }
296     }
297   else
298     {
299       /* If a variable appears in a DATA block, it cannot have an
300          initializer.  */
301       if (sym->attr.data)
302         {
303           gfc_error
304             ("Variable '%s' at %C with an initializer already appears "
305              "in a DATA statement", sym->name);
306           return FAILURE;
307         }
308
309       /* Checking a derived type parameter has to be put off until later.  */
310       if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
311           && gfc_check_assign_symbol (sym, init) == FAILURE)
312         return FAILURE;
313
314       for (i = 0; i < sym->attr.dimension; i++)
315         {
316           if (sym->as->lower[i] == NULL
317               || sym->as->lower[i]->expr_type != EXPR_CONSTANT
318               || sym->as->upper[i] == NULL
319               || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
320             {
321               gfc_error ("Array '%s' at %C cannot have initializer",
322                          sym->name);
323               return FAILURE;
324             }
325         }
326
327       /* Add initializer.  Make sure we keep the ranks sane.  */
328       if (sym->attr.dimension && init->rank == 0)
329         init->rank = sym->as->rank;
330
331       sym->value = init;
332       *initp = NULL;
333     }
334
335   return SUCCESS;
336 }
337
338
339 /* Function called by variable_decl() that adds a name to a structure
340    being built.  */
341
342 static try
343 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
344               gfc_array_spec ** as)
345 {
346   gfc_component *c;
347
348   /* If the current symbol is of the same derived type that we're
349      constructing, it must have the pointer attribute.  */
350   if (current_ts.type == BT_DERIVED
351       && current_ts.derived == gfc_current_block ()
352       && current_attr.pointer == 0)
353     {
354       gfc_error ("Component at %C must have the POINTER attribute");
355       return FAILURE;
356     }
357
358   if (gfc_current_block ()->attr.pointer
359       && (*as)->rank != 0)
360     {
361       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
362         {
363           gfc_error ("Array component of structure at %C must have explicit "
364                      "or deferred shape");
365           return FAILURE;
366         }
367     }
368
369   if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
370     return FAILURE;
371
372   c->ts = current_ts;
373   c->ts.cl = cl;
374   gfc_set_component_attr (c, &current_attr);
375
376   c->initializer = *init;
377   *init = NULL;
378
379   c->as = *as;
380   if (c->as != NULL)
381     c->dimension = 1;
382   *as = NULL;
383
384   /* Check array components.  */
385   if (!c->dimension)
386     return SUCCESS;
387
388   if (c->pointer)
389     {
390       if (c->as->type != AS_DEFERRED)
391         {
392           gfc_error ("Pointer array component of structure at %C "
393                      "must have a deferred shape");
394           return FAILURE;
395         }
396     }
397   else
398     {
399       if (c->as->type != AS_EXPLICIT)
400         {
401           gfc_error
402             ("Array component of structure at %C must have an explicit "
403              "shape");
404           return FAILURE;
405         }
406     }
407
408   return SUCCESS;
409 }
410
411
412 /* Match a 'NULL()', and possibly take care of some side effects.  */
413
414 match
415 gfc_match_null (gfc_expr ** result)
416 {
417   gfc_symbol *sym;
418   gfc_expr *e;
419   match m;
420
421   m = gfc_match (" null ( )");
422   if (m != MATCH_YES)
423     return m;
424
425   /* The NULL symbol now has to be/become an intrinsic function.  */
426   if (gfc_get_symbol ("null", NULL, &sym))
427     {
428       gfc_error ("NULL() initialization at %C is ambiguous");
429       return MATCH_ERROR;
430     }
431
432   gfc_intrinsic_symbol (sym);
433
434   if (sym->attr.proc != PROC_INTRINSIC
435       && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC, NULL) == FAILURE
436           || gfc_add_function (&sym->attr, NULL) == FAILURE))
437     return MATCH_ERROR;
438
439   e = gfc_get_expr ();
440   e->where = *gfc_current_locus ();
441   e->expr_type = EXPR_NULL;
442   e->ts.type = BT_UNKNOWN;
443
444   *result = e;
445
446   return MATCH_YES;
447 }
448
449
450 /* Get an expression for a default initializer.  */
451 static gfc_expr *
452 default_initializer (void)
453 {
454   gfc_constructor *tail;
455   gfc_expr *init;
456   gfc_component *c;
457
458   init = NULL;
459
460   /* First see if we have a default initializer.  */
461   for (c = current_ts.derived->components; c; c = c->next)
462     {
463       if (c->initializer && init == NULL)
464         init = gfc_get_expr ();
465     }
466
467   if (init == NULL)
468     return NULL;
469
470   init->expr_type = EXPR_STRUCTURE;
471   init->ts = current_ts;
472   init->where = current_ts.derived->declared_at;
473   tail = NULL;
474   for (c = current_ts.derived->components; c; c = c->next)
475     {
476       if (tail == NULL)
477         init->value.constructor = tail = gfc_get_constructor ();
478       else
479         {
480           tail->next = gfc_get_constructor ();
481           tail = tail->next;
482         }
483
484       if (c->initializer)
485         tail->expr = gfc_copy_expr (c->initializer);
486     }
487   return init;
488 }
489
490
491 /* Match a variable name with an optional initializer.  When this
492    subroutine is called, a variable is expected to be parsed next.
493    Depending on what is happening at the moment, updates either the
494    symbol table or the current interface.  */
495
496 static match
497 variable_decl (void)
498 {
499   char name[GFC_MAX_SYMBOL_LEN + 1];
500   gfc_expr *initializer, *char_len;
501   gfc_array_spec *as;
502   gfc_charlen *cl;
503   locus var_locus;
504   match m;
505   try t;
506
507   initializer = NULL;
508   as = NULL;
509
510   /* When we get here, we've just matched a list of attributes and
511      maybe a type and a double colon.  The next thing we expect to see
512      is the name of the symbol.  */
513   m = gfc_match_name (name);
514   if (m != MATCH_YES)
515     goto cleanup;
516
517   var_locus = *gfc_current_locus ();
518
519   /* Now we could see the optional array spec. or character length.  */
520   m = gfc_match_array_spec (&as);
521   if (m == MATCH_ERROR)
522     goto cleanup;
523   if (m == MATCH_NO)
524     as = gfc_copy_array_spec (current_as);
525
526   char_len = NULL;
527   cl = NULL;
528
529   if (current_ts.type == BT_CHARACTER)
530     {
531       switch (match_char_length (&char_len))
532         {
533         case MATCH_YES:
534           cl = gfc_get_charlen ();
535           cl->next = gfc_current_ns->cl_list;
536           gfc_current_ns->cl_list = cl;
537
538           cl->length = char_len;
539           break;
540
541         case MATCH_NO:
542           cl = current_ts.cl;
543           break;
544
545         case MATCH_ERROR:
546           goto cleanup;
547         }
548     }
549
550   /* OK, we've successfully matched the declaration.  Now put the
551      symbol in the current namespace, because it might be used in the
552      optional intialization expression for this symbol, e.g. this is
553      perfectly legal:
554
555      integer, parameter :: i = huge(i)
556
557      This is only true for parameters or variables of a basic type.
558      For components of derived types, it is not true, so we don't
559      create a symbol for those yet.  If we fail to create the symbol,
560      bail out.  */
561   if (gfc_current_state () != COMP_DERIVED
562       && build_sym (name, cl, &as, &var_locus) == FAILURE)
563     {
564       m = MATCH_ERROR;
565       goto cleanup;
566     }
567
568   /* In functions that have a RESULT variable defined, the function
569      name always refers to function calls.  Therefore, the name is
570      not allowed to appear in specification statements.  */
571   if (gfc_current_state () == COMP_FUNCTION
572       && gfc_current_block () != NULL
573       && gfc_current_block ()->result != NULL
574       && gfc_current_block ()->result != gfc_current_block ()
575       && strcmp (gfc_current_block ()->name, name) == 0)
576     {
577       gfc_error ("Function name '%s' not allowed at %C", name);
578       m = MATCH_ERROR;
579       goto cleanup;
580     }
581
582   /* The double colon must be present in order to have initializers.
583      Otherwise the statement is ambiguous with an assignment statement.  */
584   if (colon_seen)
585     {
586       if (gfc_match (" =>") == MATCH_YES)
587         {
588
589           if (!current_attr.pointer)
590             {
591               gfc_error ("Initialization at %C isn't for a pointer variable");
592               m = MATCH_ERROR;
593               goto cleanup;
594             }
595
596           m = gfc_match_null (&initializer);
597           if (m == MATCH_NO)
598             {
599               gfc_error ("Pointer initialization requires a NULL at %C");
600               m = MATCH_ERROR;
601             }
602
603           if (gfc_pure (NULL))
604             {
605               gfc_error
606                 ("Initialization of pointer at %C is not allowed in a "
607                  "PURE procedure");
608               m = MATCH_ERROR;
609             }
610
611           if (m != MATCH_YES)
612             goto cleanup;
613
614           initializer->ts = current_ts;
615
616         }
617       else if (gfc_match_char ('=') == MATCH_YES)
618         {
619           if (current_attr.pointer)
620             {
621               gfc_error
622                 ("Pointer initialization at %C requires '=>', not '='");
623               m = MATCH_ERROR;
624               goto cleanup;
625             }
626
627           m = gfc_match_init_expr (&initializer);
628           if (m == MATCH_NO)
629             {
630               gfc_error ("Expected an initialization expression at %C");
631               m = MATCH_ERROR;
632             }
633
634           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
635             {
636               gfc_error
637                 ("Initialization of variable at %C is not allowed in a "
638                  "PURE procedure");
639               m = MATCH_ERROR;
640             }
641
642           if (m != MATCH_YES)
643             goto cleanup;
644         }
645     }
646
647   if (current_ts.type == BT_DERIVED && !initializer)
648     {
649       initializer = default_initializer ();
650     }
651
652   /* Add the initializer.  Note that it is fine if &initializer is
653      NULL here, because we sometimes also need to check if a
654      declaration *must* have an initialization expression.  */
655   if (gfc_current_state () != COMP_DERIVED)
656     t = add_init_expr_to_sym (name, &initializer, &var_locus);
657   else
658     t = build_struct (name, cl, &initializer, &as);
659
660   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
661
662 cleanup:
663   /* Free stuff up and return.  */
664   gfc_free_expr (initializer);
665   gfc_free_array_spec (as);
666
667   return m;
668 }
669
670
671 /* Match an extended-f77 kind specification.  */
672
673 match
674 gfc_match_old_kind_spec (gfc_typespec * ts)
675 {
676   match m;
677
678   if (gfc_match_char ('*') != MATCH_YES)
679     return MATCH_NO;
680
681   m = gfc_match_small_literal_int (&ts->kind);
682   if (m != MATCH_YES)
683     return MATCH_ERROR;
684
685   /* Massage the kind numbers for complex types.  */
686   if (ts->type == BT_COMPLEX && ts->kind == 8)
687     ts->kind = 4;
688   if (ts->type == BT_COMPLEX && ts->kind == 16)
689     ts->kind = 8;
690
691   if (gfc_validate_kind (ts->type, ts->kind) == -1)
692     {
693       gfc_error ("Old-style kind %d not supported for type %s at %C",
694                  ts->kind, gfc_basic_typename (ts->type));
695
696       return MATCH_ERROR;
697     }
698
699   return MATCH_YES;
700 }
701
702
703 /* Match a kind specification.  Since kinds are generally optional, we
704    usually return MATCH_NO if something goes wrong.  If a "kind="
705    string is found, then we know we have an error.  */
706
707 match
708 gfc_match_kind_spec (gfc_typespec * ts)
709 {
710   locus where;
711   gfc_expr *e;
712   match m, n;
713   const char *msg;
714
715   m = MATCH_NO;
716   e = NULL;
717
718   where = *gfc_current_locus ();
719
720   if (gfc_match_char ('(') == MATCH_NO)
721     return MATCH_NO;
722
723   /* Also gobbles optional text.  */
724   if (gfc_match (" kind = ") == MATCH_YES)
725     m = MATCH_ERROR;
726
727   n = gfc_match_init_expr (&e);
728   if (n == MATCH_NO)
729     gfc_error ("Expected initialization expression at %C");
730   if (n != MATCH_YES)
731     return MATCH_ERROR;
732
733   if (e->rank != 0)
734     {
735       gfc_error ("Expected scalar initialization expression at %C");
736       m = MATCH_ERROR;
737       goto no_match;
738     }
739
740   msg = gfc_extract_int (e, &ts->kind);
741   if (msg != NULL)
742     {
743       gfc_error (msg);
744       m = MATCH_ERROR;
745       goto no_match;
746     }
747
748   gfc_free_expr (e);
749   e = NULL;
750
751   if (gfc_validate_kind (ts->type, ts->kind) == -1)
752     {
753       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
754                  gfc_basic_typename (ts->type));
755
756       m = MATCH_ERROR;
757       goto no_match;
758     }
759
760   if (gfc_match_char (')') != MATCH_YES)
761     {
762       gfc_error ("Missing right paren at %C");
763       goto no_match;
764     }
765
766   return MATCH_YES;
767
768 no_match:
769   gfc_free_expr (e);
770   gfc_set_locus (&where);
771   return m;
772 }
773
774
775 /* Match the various kind/length specifications in a CHARACTER
776    declaration.  We don't return MATCH_NO.  */
777
778 static match
779 match_char_spec (gfc_typespec * ts)
780 {
781   int i, kind, seen_length;
782   gfc_charlen *cl;
783   gfc_expr *len;
784   match m;
785
786   kind = gfc_default_character_kind ();
787   len = NULL;
788   seen_length = 0;
789
790   /* Try the old-style specification first.  */
791   old_char_selector = 0;
792
793   m = match_char_length (&len);
794   if (m != MATCH_NO)
795     {
796       if (m == MATCH_YES)
797         old_char_selector = 1;
798       seen_length = 1;
799       goto done;
800     }
801
802   m = gfc_match_char ('(');
803   if (m != MATCH_YES)
804     {
805       m = MATCH_YES;    /* character without length is a single char */
806       goto done;
807     }
808
809   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] )   */
810   if (gfc_match (" kind =") == MATCH_YES)
811     {
812       m = gfc_match_small_int (&kind);
813       if (m == MATCH_ERROR)
814         goto done;
815       if (m == MATCH_NO)
816         goto syntax;
817
818       if (gfc_match (" , len =") == MATCH_NO)
819         goto rparen;
820
821       m = char_len_param_value (&len);
822       if (m == MATCH_NO)
823         goto syntax;
824       if (m == MATCH_ERROR)
825         goto done;
826       seen_length = 1;
827
828       goto rparen;
829     }
830
831   /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> )  */
832   if (gfc_match (" len =") == MATCH_YES)
833     {
834       m = char_len_param_value (&len);
835       if (m == MATCH_NO)
836         goto syntax;
837       if (m == MATCH_ERROR)
838         goto done;
839       seen_length = 1;
840
841       if (gfc_match_char (')') == MATCH_YES)
842         goto done;
843
844       if (gfc_match (" , kind =") != MATCH_YES)
845         goto syntax;
846
847       gfc_match_small_int (&kind);
848
849       if (gfc_validate_kind (BT_CHARACTER, kind) == -1)
850         {
851           gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
852           return MATCH_YES;
853         }
854
855       goto rparen;
856     }
857
858   /* Try to match   ( <len-param> ) or ( <len-param> , [ KIND = ] <int> )  */
859   m = char_len_param_value (&len);
860   if (m == MATCH_NO)
861     goto syntax;
862   if (m == MATCH_ERROR)
863     goto done;
864   seen_length = 1;
865
866   m = gfc_match_char (')');
867   if (m == MATCH_YES)
868     goto done;
869
870   if (gfc_match_char (',') != MATCH_YES)
871     goto syntax;
872
873   gfc_match (" kind =");        /* Gobble optional text */
874
875   m = gfc_match_small_int (&kind);
876   if (m == MATCH_ERROR)
877     goto done;
878   if (m == MATCH_NO)
879     goto syntax;
880
881 rparen:
882   /* Require a right-paren at this point.  */
883   m = gfc_match_char (')');
884   if (m == MATCH_YES)
885     goto done;
886
887 syntax:
888   gfc_error ("Syntax error in CHARACTER declaration at %C");
889   m = MATCH_ERROR;
890
891 done:
892   if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind) == -1)
893     {
894       gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
895       m = MATCH_ERROR;
896     }
897
898   if (m != MATCH_YES)
899     {
900       gfc_free_expr (len);
901       return m;
902     }
903
904   /* Do some final massaging of the length values.  */
905   cl = gfc_get_charlen ();
906   cl->next = gfc_current_ns->cl_list;
907   gfc_current_ns->cl_list = cl;
908
909   if (seen_length == 0)
910     cl->length = gfc_int_expr (1);
911   else
912     {
913       if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
914         cl->length = len;
915       else
916         {
917           gfc_free_expr (len);
918           cl->length = gfc_int_expr (0);
919         }
920     }
921
922   ts->cl = cl;
923   ts->kind = kind;
924
925   return MATCH_YES;
926 }
927
928
929 /* Matches a type specification.  If successful, sets the ts structure
930    to the matched specification.  This is necessary for FUNCTION and
931    IMPLICIT statements.
932
933    If kind_flag is nonzero, then we check for the optional kind
934    specification.  Not doing so is needed for matching an IMPLICIT
935    statement correctly.  */
936
937 match
938 gfc_match_type_spec (gfc_typespec * ts, int kind_flag)
939 {
940   char name[GFC_MAX_SYMBOL_LEN + 1];
941   gfc_symbol *sym;
942   match m;
943
944   gfc_clear_ts (ts);
945
946   if (gfc_match (" integer") == MATCH_YES)
947     {
948       ts->type = BT_INTEGER;
949       ts->kind = gfc_default_integer_kind ();
950       goto get_kind;
951     }
952
953   if (gfc_match (" character") == MATCH_YES)
954     {
955       ts->type = BT_CHARACTER;
956       return match_char_spec (ts);
957     }
958
959   if (gfc_match (" real") == MATCH_YES)
960     {
961       ts->type = BT_REAL;
962       ts->kind = gfc_default_real_kind ();
963       goto get_kind;
964     }
965
966   if (gfc_match (" double precision") == MATCH_YES)
967     {
968       ts->type = BT_REAL;
969       ts->kind = gfc_default_double_kind ();
970       return MATCH_YES;
971     }
972
973   if (gfc_match (" complex") == MATCH_YES)
974     {
975       ts->type = BT_COMPLEX;
976       ts->kind = gfc_default_complex_kind ();
977       goto get_kind;
978     }
979
980   if (gfc_match (" double complex") == MATCH_YES)
981     {
982       ts->type = BT_COMPLEX;
983       ts->kind = gfc_default_double_kind ();
984       return MATCH_YES;
985     }
986
987   if (gfc_match (" logical") == MATCH_YES)
988     {
989       ts->type = BT_LOGICAL;
990       ts->kind = gfc_default_logical_kind ();
991       goto get_kind;
992     }
993
994   m = gfc_match (" type ( %n )", name);
995   if (m != MATCH_YES)
996     return m;
997
998   /* Search for the name but allow the components to be defined later.  */
999   if (gfc_get_ha_symbol (name, &sym))
1000     {
1001       gfc_error ("Type name '%s' at %C is ambiguous", name);
1002       return MATCH_ERROR;
1003     }
1004
1005   if (sym->attr.flavor != FL_DERIVED
1006       && gfc_add_flavor (&sym->attr, FL_DERIVED, NULL) == FAILURE)
1007     return MATCH_ERROR;
1008
1009   ts->type = BT_DERIVED;
1010   ts->kind = 0;
1011   ts->derived = sym;
1012
1013   return MATCH_YES;
1014
1015 get_kind:
1016   /* For all types except double, derived and character, look for an
1017      optional kind specifier.  MATCH_NO is actually OK at this point.  */
1018   if (kind_flag == 0)
1019     return MATCH_YES;
1020
1021   m = gfc_match_kind_spec (ts);
1022   if (m == MATCH_NO && ts->type != BT_CHARACTER)
1023     m = gfc_match_old_kind_spec (ts);
1024
1025   if (m == MATCH_NO)
1026     m = MATCH_YES;              /* No kind specifier found.  */
1027
1028   return m;
1029 }
1030
1031
1032 /* Matches an attribute specification including array specs.  If
1033    successful, leaves the variables current_attr and current_as
1034    holding the specification.  Also sets the colon_seen variable for
1035    later use by matchers associated with initializations.
1036
1037    This subroutine is a little tricky in the sense that we don't know
1038    if we really have an attr-spec until we hit the double colon.
1039    Until that time, we can only return MATCH_NO.  This forces us to
1040    check for duplicate specification at this level.  */
1041
1042 static match
1043 match_attr_spec (void)
1044 {
1045
1046   /* Modifiers that can exist in a type statement.  */
1047   typedef enum
1048   { GFC_DECL_BEGIN = 0,
1049     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1050     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1051     DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1052     DECL_TARGET, DECL_COLON, DECL_NONE,
1053     GFC_DECL_END /* Sentinel */
1054   }
1055   decl_types;
1056
1057 /* GFC_DECL_END is the sentinel, index starts at 0.  */
1058 #define NUM_DECL GFC_DECL_END
1059
1060   static mstring decls[] = {
1061     minit (", allocatable", DECL_ALLOCATABLE),
1062     minit (", dimension", DECL_DIMENSION),
1063     minit (", external", DECL_EXTERNAL),
1064     minit (", intent ( in )", DECL_IN),
1065     minit (", intent ( out )", DECL_OUT),
1066     minit (", intent ( in out )", DECL_INOUT),
1067     minit (", intrinsic", DECL_INTRINSIC),
1068     minit (", optional", DECL_OPTIONAL),
1069     minit (", parameter", DECL_PARAMETER),
1070     minit (", pointer", DECL_POINTER),
1071     minit (", private", DECL_PRIVATE),
1072     minit (", public", DECL_PUBLIC),
1073     minit (", save", DECL_SAVE),
1074     minit (", target", DECL_TARGET),
1075     minit ("::", DECL_COLON),
1076     minit (NULL, DECL_NONE)
1077   };
1078
1079   locus start, seen_at[NUM_DECL];
1080   int seen[NUM_DECL];
1081   decl_types d;
1082   const char *attr;
1083   match m;
1084   try t;
1085
1086   gfc_clear_attr (&current_attr);
1087   start = *gfc_current_locus ();
1088
1089   current_as = NULL;
1090   colon_seen = 0;
1091
1092   /* See if we get all of the keywords up to the final double colon.  */
1093   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1094     seen[d] = 0;
1095
1096   for (;;)
1097     {
1098       d = (decl_types) gfc_match_strings (decls);
1099       if (d == DECL_NONE || d == DECL_COLON)
1100         break;
1101
1102       seen[d]++;
1103       seen_at[d] = *gfc_current_locus ();
1104
1105       if (d == DECL_DIMENSION)
1106         {
1107           m = gfc_match_array_spec (&current_as);
1108
1109           if (m == MATCH_NO)
1110             {
1111               gfc_error ("Missing dimension specification at %C");
1112               m = MATCH_ERROR;
1113             }
1114
1115           if (m == MATCH_ERROR)
1116             goto cleanup;
1117         }
1118     }
1119
1120   /* No double colon, so assume that we've been looking at something
1121      else the whole time.  */
1122   if (d == DECL_NONE)
1123     {
1124       m = MATCH_NO;
1125       goto cleanup;
1126     }
1127
1128   /* Since we've seen a double colon, we have to be looking at an
1129      attr-spec.  This means that we can now issue errors.  */
1130   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1131     if (seen[d] > 1)
1132       {
1133         switch (d)
1134           {
1135           case DECL_ALLOCATABLE:
1136             attr = "ALLOCATABLE";
1137             break;
1138           case DECL_DIMENSION:
1139             attr = "DIMENSION";
1140             break;
1141           case DECL_EXTERNAL:
1142             attr = "EXTERNAL";
1143             break;
1144           case DECL_IN:
1145             attr = "INTENT (IN)";
1146             break;
1147           case DECL_OUT:
1148             attr = "INTENT (OUT)";
1149             break;
1150           case DECL_INOUT:
1151             attr = "INTENT (IN OUT)";
1152             break;
1153           case DECL_INTRINSIC:
1154             attr = "INTRINSIC";
1155             break;
1156           case DECL_OPTIONAL:
1157             attr = "OPTIONAL";
1158             break;
1159           case DECL_PARAMETER:
1160             attr = "PARAMETER";
1161             break;
1162           case DECL_POINTER:
1163             attr = "POINTER";
1164             break;
1165           case DECL_PRIVATE:
1166             attr = "PRIVATE";
1167             break;
1168           case DECL_PUBLIC:
1169             attr = "PUBLIC";
1170             break;
1171           case DECL_SAVE:
1172             attr = "SAVE";
1173             break;
1174           case DECL_TARGET:
1175             attr = "TARGET";
1176             break;
1177           default:
1178             attr = NULL;        /* This shouldn't happen */
1179           }
1180
1181         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
1182         m = MATCH_ERROR;
1183         goto cleanup;
1184       }
1185
1186   /* Now that we've dealt with duplicate attributes, add the attributes
1187      to the current attribute.  */
1188   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1189     {
1190       if (seen[d] == 0)
1191         continue;
1192
1193       if (gfc_current_state () == COMP_DERIVED
1194           && d != DECL_DIMENSION && d != DECL_POINTER
1195           && d != DECL_COLON && d != DECL_NONE)
1196         {
1197
1198           gfc_error ("Attribute at %L is not allowed in a TYPE definition",
1199                      &seen_at[d]);
1200           m = MATCH_ERROR;
1201           goto cleanup;
1202         }
1203
1204       switch (d)
1205         {
1206         case DECL_ALLOCATABLE:
1207           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
1208           break;
1209
1210         case DECL_DIMENSION:
1211           t = gfc_add_dimension (&current_attr, &seen_at[d]);
1212           break;
1213
1214         case DECL_EXTERNAL:
1215           t = gfc_add_external (&current_attr, &seen_at[d]);
1216           break;
1217
1218         case DECL_IN:
1219           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
1220           break;
1221
1222         case DECL_OUT:
1223           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
1224           break;
1225
1226         case DECL_INOUT:
1227           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
1228           break;
1229
1230         case DECL_INTRINSIC:
1231           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
1232           break;
1233
1234         case DECL_OPTIONAL:
1235           t = gfc_add_optional (&current_attr, &seen_at[d]);
1236           break;
1237
1238         case DECL_PARAMETER:
1239           t = gfc_add_flavor (&current_attr, FL_PARAMETER, &seen_at[d]);
1240           break;
1241
1242         case DECL_POINTER:
1243           t = gfc_add_pointer (&current_attr, &seen_at[d]);
1244           break;
1245
1246         case DECL_PRIVATE:
1247           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, &seen_at[d]);
1248           break;
1249
1250         case DECL_PUBLIC:
1251           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, &seen_at[d]);
1252           break;
1253
1254         case DECL_SAVE:
1255           t = gfc_add_save (&current_attr, &seen_at[d]);
1256           break;
1257
1258         case DECL_TARGET:
1259           t = gfc_add_target (&current_attr, &seen_at[d]);
1260           break;
1261
1262         default:
1263           gfc_internal_error ("match_attr_spec(): Bad attribute");
1264         }
1265
1266       if (t == FAILURE)
1267         {
1268           m = MATCH_ERROR;
1269           goto cleanup;
1270         }
1271     }
1272
1273   colon_seen = 1;
1274   return MATCH_YES;
1275
1276 cleanup:
1277   gfc_set_locus (&start);
1278   gfc_free_array_spec (current_as);
1279   current_as = NULL;
1280   return m;
1281 }
1282
1283
1284 /* Match a data declaration statement.  */
1285
1286 match
1287 gfc_match_data_decl (void)
1288 {
1289   gfc_symbol *sym;
1290   match m;
1291
1292   m = gfc_match_type_spec (&current_ts, 1);
1293   if (m != MATCH_YES)
1294     return m;
1295
1296   if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
1297     {
1298       sym = gfc_use_derived (current_ts.derived);
1299
1300       if (sym == NULL)
1301         {
1302           m = MATCH_ERROR;
1303           goto cleanup;
1304         }
1305
1306       current_ts.derived = sym;
1307     }
1308
1309   m = match_attr_spec ();
1310   if (m == MATCH_ERROR)
1311     {
1312       m = MATCH_NO;
1313       goto cleanup;
1314     }
1315
1316   if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
1317     {
1318
1319       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
1320         goto ok;
1321
1322       if (gfc_find_symbol (current_ts.derived->name,
1323                            current_ts.derived->ns->parent, 1, &sym) == 0)
1324         goto ok;
1325
1326       /* Hope that an ambiguous symbol is itself masked by a type definition.  */
1327       if (sym != NULL && sym->attr.flavor == FL_DERIVED)
1328         goto ok;
1329
1330       gfc_error ("Derived type at %C has not been previously defined");
1331       m = MATCH_ERROR;
1332       goto cleanup;
1333     }
1334
1335 ok:
1336   /* If we have an old-style character declaration, and no new-style
1337      attribute specifications, then there a comma is optional between
1338      the type specification and the variable list.  */
1339   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
1340     gfc_match_char (',');
1341
1342   /* Give the types/attributes to symbols that follow.  */
1343   for (;;)
1344     {
1345       m = variable_decl ();
1346       if (m == MATCH_ERROR)
1347         goto cleanup;
1348       if (m == MATCH_NO)
1349         break;
1350
1351       if (gfc_match_eos () == MATCH_YES)
1352         goto cleanup;
1353       if (gfc_match_char (',') != MATCH_YES)
1354         break;
1355     }
1356
1357   gfc_error ("Syntax error in data declaration at %C");
1358   m = MATCH_ERROR;
1359
1360 cleanup:
1361   gfc_free_array_spec (current_as);
1362   current_as = NULL;
1363   return m;
1364 }
1365
1366
1367 /* Match a prefix associated with a function or subroutine
1368    declaration.  If the typespec pointer is nonnull, then a typespec
1369    can be matched.  Note that if nothing matches, MATCH_YES is
1370    returned (the null string was matched).  */
1371
1372 static match
1373 match_prefix (gfc_typespec * ts)
1374 {
1375   int seen_type;
1376
1377   gfc_clear_attr (&current_attr);
1378   seen_type = 0;
1379
1380 loop:
1381   if (!seen_type && ts != NULL
1382       && gfc_match_type_spec (ts, 1) == MATCH_YES
1383       && gfc_match_space () == MATCH_YES)
1384     {
1385
1386       seen_type = 1;
1387       goto loop;
1388     }
1389
1390   if (gfc_match ("elemental% ") == MATCH_YES)
1391     {
1392       if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
1393         return MATCH_ERROR;
1394
1395       goto loop;
1396     }
1397
1398   if (gfc_match ("pure% ") == MATCH_YES)
1399     {
1400       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
1401         return MATCH_ERROR;
1402
1403       goto loop;
1404     }
1405
1406   if (gfc_match ("recursive% ") == MATCH_YES)
1407     {
1408       if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
1409         return MATCH_ERROR;
1410
1411       goto loop;
1412     }
1413
1414   /* At this point, the next item is not a prefix.  */
1415   return MATCH_YES;
1416 }
1417
1418
1419 /* Copy attributes matched by match_prefix() to attributes on a symbol.  */
1420
1421 static try
1422 copy_prefix (symbol_attribute * dest, locus * where)
1423 {
1424
1425   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
1426     return FAILURE;
1427
1428   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
1429     return FAILURE;
1430
1431   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
1432     return FAILURE;
1433
1434   return SUCCESS;
1435 }
1436
1437
1438 /* Match a formal argument list.  */
1439
1440 match
1441 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
1442 {
1443   gfc_formal_arglist *head, *tail, *p, *q;
1444   char name[GFC_MAX_SYMBOL_LEN + 1];
1445   gfc_symbol *sym;
1446   match m;
1447
1448   head = tail = NULL;
1449
1450   if (gfc_match_char ('(') != MATCH_YES)
1451     {
1452       if (null_flag)
1453         goto ok;
1454       return MATCH_NO;
1455     }
1456
1457   if (gfc_match_char (')') == MATCH_YES)
1458     goto ok;
1459
1460   for (;;)
1461     {
1462       if (gfc_match_char ('*') == MATCH_YES)
1463         sym = NULL;
1464       else
1465         {
1466           m = gfc_match_name (name);
1467           if (m != MATCH_YES)
1468             goto cleanup;
1469
1470           if (gfc_get_symbol (name, NULL, &sym))
1471             goto cleanup;
1472         }
1473
1474       p = gfc_get_formal_arglist ();
1475
1476       if (head == NULL)
1477         head = tail = p;
1478       else
1479         {
1480           tail->next = p;
1481           tail = p;
1482         }
1483
1484       tail->sym = sym;
1485
1486       /* We don't add the VARIABLE flavor because the name could be a
1487          dummy procedure.  We don't apply these attributes to formal
1488          arguments of statement functions.  */
1489       if (sym != NULL && !st_flag
1490           && (gfc_add_dummy (&sym->attr, NULL) == FAILURE
1491               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
1492         {
1493           m = MATCH_ERROR;
1494           goto cleanup;
1495         }
1496
1497       /* The name of a program unit can be in a different namespace,
1498          so check for it explicitly.  After the statement is accepted,
1499          the name is checked for especially in gfc_get_symbol().  */
1500       if (gfc_new_block != NULL && sym != NULL
1501           && strcmp (sym->name, gfc_new_block->name) == 0)
1502         {
1503           gfc_error ("Name '%s' at %C is the name of the procedure",
1504                      sym->name);
1505           m = MATCH_ERROR;
1506           goto cleanup;
1507         }
1508
1509       if (gfc_match_char (')') == MATCH_YES)
1510         goto ok;
1511
1512       m = gfc_match_char (',');
1513       if (m != MATCH_YES)
1514         {
1515           gfc_error ("Unexpected junk in formal argument list at %C");
1516           goto cleanup;
1517         }
1518     }
1519
1520 ok:
1521   /* Check for duplicate symbols in the formal argument list.  */
1522   if (head != NULL)
1523     {
1524       for (p = head; p->next; p = p->next)
1525         {
1526           if (p->sym == NULL)
1527             continue;
1528
1529           for (q = p->next; q; q = q->next)
1530             if (p->sym == q->sym)
1531               {
1532                 gfc_error
1533                   ("Duplicate symbol '%s' in formal argument list at %C",
1534                    p->sym->name);
1535
1536                 m = MATCH_ERROR;
1537                 goto cleanup;
1538               }
1539         }
1540     }
1541
1542   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
1543       FAILURE)
1544     {
1545       m = MATCH_ERROR;
1546       goto cleanup;
1547     }
1548
1549   return MATCH_YES;
1550
1551 cleanup:
1552   gfc_free_formal_arglist (head);
1553   return m;
1554 }
1555
1556
1557 /* Match a RESULT specification following a function declaration or
1558    ENTRY statement.  Also matches the end-of-statement.  */
1559
1560 static match
1561 match_result (gfc_symbol * function, gfc_symbol ** result)
1562 {
1563   char name[GFC_MAX_SYMBOL_LEN + 1];
1564   gfc_symbol *r;
1565   match m;
1566
1567   if (gfc_match (" result (") != MATCH_YES)
1568     return MATCH_NO;
1569
1570   m = gfc_match_name (name);
1571   if (m != MATCH_YES)
1572     return m;
1573
1574   if (gfc_match (" )%t") != MATCH_YES)
1575     {
1576       gfc_error ("Unexpected junk following RESULT variable at %C");
1577       return MATCH_ERROR;
1578     }
1579
1580   if (strcmp (function->name, name) == 0)
1581     {
1582       gfc_error
1583         ("RESULT variable at %C must be different than function name");
1584       return MATCH_ERROR;
1585     }
1586
1587   if (gfc_get_symbol (name, NULL, &r))
1588     return MATCH_ERROR;
1589
1590   if (gfc_add_flavor (&r->attr, FL_VARIABLE, NULL) == FAILURE
1591       || gfc_add_result (&r->attr, NULL) == FAILURE)
1592     return MATCH_ERROR;
1593
1594   *result = r;
1595
1596   return MATCH_YES;
1597 }
1598
1599
1600 /* Match a function declaration.  */
1601
1602 match
1603 gfc_match_function_decl (void)
1604 {
1605   char name[GFC_MAX_SYMBOL_LEN + 1];
1606   gfc_symbol *sym, *result;
1607   locus old_loc;
1608   match m;
1609
1610   if (gfc_current_state () != COMP_NONE
1611       && gfc_current_state () != COMP_INTERFACE
1612       && gfc_current_state () != COMP_CONTAINS)
1613     return MATCH_NO;
1614
1615   gfc_clear_ts (&current_ts);
1616
1617   old_loc = *gfc_current_locus ();
1618
1619   m = match_prefix (&current_ts);
1620   if (m != MATCH_YES)
1621     {
1622       gfc_set_locus (&old_loc);
1623       return m;
1624     }
1625
1626   if (gfc_match ("function% %n", name) != MATCH_YES)
1627     {
1628       gfc_set_locus (&old_loc);
1629       return MATCH_NO;
1630     }
1631
1632   if (get_proc_name (name, &sym))
1633     return MATCH_ERROR;
1634   gfc_new_block = sym;
1635
1636   m = gfc_match_formal_arglist (sym, 0, 0);
1637   if (m == MATCH_NO)
1638     gfc_error ("Expected formal argument list in function definition at %C");
1639   else if (m == MATCH_ERROR)
1640     goto cleanup;
1641
1642   result = NULL;
1643
1644   if (gfc_match_eos () != MATCH_YES)
1645     {
1646       /* See if a result variable is present.  */
1647       m = match_result (sym, &result);
1648       if (m == MATCH_NO)
1649         gfc_error ("Unexpected junk after function declaration at %C");
1650
1651       if (m != MATCH_YES)
1652         {
1653           m = MATCH_ERROR;
1654           goto cleanup;
1655         }
1656     }
1657
1658   /* Make changes to the symbol.  */
1659   m = MATCH_ERROR;
1660
1661   if (gfc_add_function (&sym->attr, NULL) == FAILURE)
1662     goto cleanup;
1663
1664   if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
1665       || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
1666     goto cleanup;
1667
1668   if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
1669     {
1670       gfc_error ("Function '%s' at %C already has a type of %s", name,
1671                  gfc_basic_typename (sym->ts.type));
1672       goto cleanup;
1673     }
1674
1675   if (result == NULL)
1676     {
1677       sym->ts = current_ts;
1678       sym->result = sym;
1679     }
1680   else
1681     {
1682       result->ts = current_ts;
1683       sym->result = result;
1684     }
1685
1686   return MATCH_YES;
1687
1688 cleanup:
1689   gfc_set_locus (&old_loc);
1690   return m;
1691 }
1692
1693
1694 /* Match an ENTRY statement.  */
1695
1696 match
1697 gfc_match_entry (void)
1698 {
1699   gfc_symbol *function, *result, *entry;
1700   char name[GFC_MAX_SYMBOL_LEN + 1];
1701   gfc_compile_state state;
1702   match m;
1703
1704   m = gfc_match_name (name);
1705   if (m != MATCH_YES)
1706     return m;
1707
1708   if (get_proc_name (name, &entry))
1709     return MATCH_ERROR;
1710
1711   gfc_enclosing_unit (&state);
1712   switch (state)
1713     {
1714     case COMP_SUBROUTINE:
1715       m = gfc_match_formal_arglist (entry, 0, 1);
1716       if (m != MATCH_YES)
1717         return MATCH_ERROR;
1718
1719       if (gfc_current_state () != COMP_SUBROUTINE)
1720         goto exec_construct;
1721
1722       if (gfc_add_entry (&entry->attr, NULL) == FAILURE
1723           || gfc_add_subroutine (&entry->attr, NULL) == FAILURE)
1724         return MATCH_ERROR;
1725
1726       break;
1727
1728     case COMP_FUNCTION:
1729       m = gfc_match_formal_arglist (entry, 0, 0);
1730       if (m != MATCH_YES)
1731         return MATCH_ERROR;
1732
1733       if (gfc_current_state () != COMP_FUNCTION)
1734         goto exec_construct;
1735       function = gfc_state_stack->sym;
1736
1737       result = NULL;
1738
1739       if (gfc_match_eos () == MATCH_YES)
1740         {
1741           if (gfc_add_entry (&entry->attr, NULL) == FAILURE
1742               || gfc_add_function (&entry->attr, NULL) == FAILURE)
1743             return MATCH_ERROR;
1744
1745           entry->result = function->result;
1746
1747         }
1748       else
1749         {
1750           m = match_result (function, &result);
1751           if (m == MATCH_NO)
1752             gfc_syntax_error (ST_ENTRY);
1753           if (m != MATCH_YES)
1754             return MATCH_ERROR;
1755
1756           if (gfc_add_result (&result->attr, NULL) == FAILURE
1757               || gfc_add_entry (&entry->attr, NULL) == FAILURE
1758               || gfc_add_function (&entry->attr, NULL) == FAILURE)
1759             return MATCH_ERROR;
1760         }
1761
1762       if (function->attr.recursive && result == NULL)
1763         {
1764           gfc_error ("RESULT attribute required in ENTRY statement at %C");
1765           return MATCH_ERROR;
1766         }
1767
1768       break;
1769
1770     default:
1771       goto exec_construct;
1772     }
1773
1774   if (gfc_match_eos () != MATCH_YES)
1775     {
1776       gfc_syntax_error (ST_ENTRY);
1777       return MATCH_ERROR;
1778     }
1779
1780   return MATCH_YES;
1781
1782 exec_construct:
1783   gfc_error ("ENTRY statement at %C cannot appear within %s",
1784              gfc_state_name (gfc_current_state ()));
1785
1786   return MATCH_ERROR;
1787 }
1788
1789
1790 /* Match a subroutine statement, including optional prefixes.  */
1791
1792 match
1793 gfc_match_subroutine (void)
1794 {
1795   char name[GFC_MAX_SYMBOL_LEN + 1];
1796   gfc_symbol *sym;
1797   match m;
1798
1799   if (gfc_current_state () != COMP_NONE
1800       && gfc_current_state () != COMP_INTERFACE
1801       && gfc_current_state () != COMP_CONTAINS)
1802     return MATCH_NO;
1803
1804   m = match_prefix (NULL);
1805   if (m != MATCH_YES)
1806     return m;
1807
1808   m = gfc_match ("subroutine% %n", name);
1809   if (m != MATCH_YES)
1810     return m;
1811
1812   if (get_proc_name (name, &sym))
1813     return MATCH_ERROR;
1814   gfc_new_block = sym;
1815
1816   if (gfc_add_subroutine (&sym->attr, NULL) == FAILURE)
1817     return MATCH_ERROR;
1818
1819   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
1820     return MATCH_ERROR;
1821
1822   if (gfc_match_eos () != MATCH_YES)
1823     {
1824       gfc_syntax_error (ST_SUBROUTINE);
1825       return MATCH_ERROR;
1826     }
1827
1828   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
1829     return MATCH_ERROR;
1830
1831   return MATCH_YES;
1832 }
1833
1834
1835 /* Match any of the various end-block statements.  Returns the type of
1836    END to the caller.  The END INTERFACE, END IF, END DO and END
1837    SELECT statements cannot be replaced by a single END statement.  */
1838
1839 match
1840 gfc_match_end (gfc_statement * st)
1841 {
1842   char name[GFC_MAX_SYMBOL_LEN + 1];
1843   gfc_compile_state state;
1844   locus old_loc;
1845   const char *block_name;
1846   const char *target;
1847   match m;
1848
1849   old_loc = *gfc_current_locus ();
1850   if (gfc_match ("end") != MATCH_YES)
1851     return MATCH_NO;
1852
1853   state = gfc_current_state ();
1854   block_name =
1855     gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
1856
1857   if (state == COMP_CONTAINS)
1858     {
1859       state = gfc_state_stack->previous->state;
1860       block_name = gfc_state_stack->previous->sym == NULL ? NULL
1861         : gfc_state_stack->previous->sym->name;
1862     }
1863
1864   switch (state)
1865     {
1866     case COMP_NONE:
1867     case COMP_PROGRAM:
1868       *st = ST_END_PROGRAM;
1869       target = " program";
1870       break;
1871
1872     case COMP_SUBROUTINE:
1873       *st = ST_END_SUBROUTINE;
1874       target = " subroutine";
1875       break;
1876
1877     case COMP_FUNCTION:
1878       *st = ST_END_FUNCTION;
1879       target = " function";
1880       break;
1881
1882     case COMP_BLOCK_DATA:
1883       *st = ST_END_BLOCK_DATA;
1884       target = " block data";
1885       break;
1886
1887     case COMP_MODULE:
1888       *st = ST_END_MODULE;
1889       target = " module";
1890       break;
1891
1892     case COMP_INTERFACE:
1893       *st = ST_END_INTERFACE;
1894       target = " interface";
1895       break;
1896
1897     case COMP_DERIVED:
1898       *st = ST_END_TYPE;
1899       target = " type";
1900       break;
1901
1902     case COMP_IF:
1903       *st = ST_ENDIF;
1904       target = " if";
1905       break;
1906
1907     case COMP_DO:
1908       *st = ST_ENDDO;
1909       target = " do";
1910       break;
1911
1912     case COMP_SELECT:
1913       *st = ST_END_SELECT;
1914       target = " select";
1915       break;
1916
1917     case COMP_FORALL:
1918       *st = ST_END_FORALL;
1919       target = " forall";
1920       break;
1921
1922     case COMP_WHERE:
1923       *st = ST_END_WHERE;
1924       target = " where";
1925       break;
1926
1927     default:
1928       gfc_error ("Unexpected END statement at %C");
1929       goto cleanup;
1930     }
1931
1932   if (gfc_match_eos () == MATCH_YES)
1933     {
1934
1935       if (*st == ST_ENDIF || *st == ST_ENDDO || *st == ST_END_SELECT
1936           || *st == ST_END_INTERFACE || *st == ST_END_FORALL
1937           || *st == ST_END_WHERE)
1938         {
1939
1940           gfc_error ("%s statement expected at %C",
1941                      gfc_ascii_statement (*st));
1942           goto cleanup;
1943         }
1944
1945       return MATCH_YES;
1946     }
1947
1948   /* Verify that we've got the sort of end-block that we're expecting.  */
1949   if (gfc_match (target) != MATCH_YES)
1950     {
1951       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
1952       goto cleanup;
1953     }
1954
1955   /* If we're at the end, make sure a block name wasn't required.  */
1956   if (gfc_match_eos () == MATCH_YES)
1957     {
1958
1959       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
1960         return MATCH_YES;
1961
1962       if (gfc_current_block () == NULL)
1963         return MATCH_YES;
1964
1965       gfc_error ("Expected block name of '%s' in %s statement at %C",
1966                  block_name, gfc_ascii_statement (*st));
1967
1968       return MATCH_ERROR;
1969     }
1970
1971   /* END INTERFACE has a special handler for its several possible endings.  */
1972   if (*st == ST_END_INTERFACE)
1973     return gfc_match_end_interface ();
1974
1975   /* We haven't hit the end of statement, so what is left must be an end-name.  */
1976   m = gfc_match_space ();
1977   if (m == MATCH_YES)
1978     m = gfc_match_name (name);
1979
1980   if (m == MATCH_NO)
1981     gfc_error ("Expected terminating name at %C");
1982   if (m != MATCH_YES)
1983     goto cleanup;
1984
1985   if (block_name == NULL)
1986     goto syntax;
1987
1988   if (strcmp (name, block_name) != 0)
1989     {
1990       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
1991                  gfc_ascii_statement (*st));
1992       goto cleanup;
1993     }
1994
1995   if (gfc_match_eos () == MATCH_YES)
1996     return MATCH_YES;
1997
1998 syntax:
1999   gfc_syntax_error (*st);
2000
2001 cleanup:
2002   gfc_set_locus (&old_loc);
2003   return MATCH_ERROR;
2004 }
2005
2006
2007
2008 /***************** Attribute declaration statements ****************/
2009
2010 /* Set the attribute of a single variable.  */
2011
2012 static match
2013 attr_decl1 (void)
2014 {
2015   char name[GFC_MAX_SYMBOL_LEN + 1];
2016   gfc_array_spec *as;
2017   gfc_symbol *sym;
2018   locus var_locus;
2019   match m;
2020
2021   as = NULL;
2022
2023   m = gfc_match_name (name);
2024   if (m != MATCH_YES)
2025     goto cleanup;
2026
2027   if (find_special (name, &sym))
2028     return MATCH_ERROR;
2029
2030   var_locus = *gfc_current_locus ();
2031
2032   /* Deal with possible array specification for certain attributes.  */
2033   if (current_attr.dimension
2034       || current_attr.allocatable
2035       || current_attr.pointer
2036       || current_attr.target)
2037     {
2038       m = gfc_match_array_spec (&as);
2039       if (m == MATCH_ERROR)
2040         goto cleanup;
2041
2042       if (current_attr.dimension && m == MATCH_NO)
2043         {
2044           gfc_error
2045             ("Missing array specification at %L in DIMENSION statement",
2046              &var_locus);
2047           m = MATCH_ERROR;
2048           goto cleanup;
2049         }
2050
2051       if ((current_attr.allocatable || current_attr.pointer)
2052           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
2053         {
2054           gfc_error ("Array specification must be deferred at %L",
2055                      &var_locus);
2056           m = MATCH_ERROR;
2057           goto cleanup;
2058         }
2059     }
2060
2061   /* Update symbol table.  DIMENSION attribute is set in gfc_set_array_spec().  */
2062   if (current_attr.dimension == 0
2063       && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
2064     {
2065       m = MATCH_ERROR;
2066       goto cleanup;
2067     }
2068
2069   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
2070     {
2071       m = MATCH_ERROR;
2072       goto cleanup;
2073     }
2074
2075   if ((current_attr.external || current_attr.intrinsic)
2076       && sym->attr.flavor != FL_PROCEDURE
2077       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, NULL) == FAILURE)
2078     {
2079       m = MATCH_ERROR;
2080       goto cleanup;
2081     }
2082
2083   return MATCH_YES;
2084
2085 cleanup:
2086   gfc_free_array_spec (as);
2087   return m;
2088 }
2089
2090
2091 /* Generic attribute declaration subroutine.  Used for attributes that
2092    just have a list of names.  */
2093
2094 static match
2095 attr_decl (void)
2096 {
2097   match m;
2098
2099   /* Gobble the optional double colon, by simply ignoring the result
2100      of gfc_match().  */
2101   gfc_match (" ::");
2102
2103   for (;;)
2104     {
2105       m = attr_decl1 ();
2106       if (m != MATCH_YES)
2107         break;
2108
2109       if (gfc_match_eos () == MATCH_YES)
2110         {
2111           m = MATCH_YES;
2112           break;
2113         }
2114
2115       if (gfc_match_char (',') != MATCH_YES)
2116         {
2117           gfc_error ("Unexpected character in variable list at %C");
2118           m = MATCH_ERROR;
2119           break;
2120         }
2121     }
2122
2123   return m;
2124 }
2125
2126
2127 match
2128 gfc_match_external (void)
2129 {
2130
2131   gfc_clear_attr (&current_attr);
2132   gfc_add_external (&current_attr, NULL);
2133
2134   return attr_decl ();
2135 }
2136
2137
2138
2139 match
2140 gfc_match_intent (void)
2141 {
2142   sym_intent intent;
2143
2144   intent = match_intent_spec ();
2145   if (intent == INTENT_UNKNOWN)
2146     return MATCH_ERROR;
2147
2148   gfc_clear_attr (&current_attr);
2149   gfc_add_intent (&current_attr, intent, NULL); /* Can't fail */
2150
2151   return attr_decl ();
2152 }
2153
2154
2155 match
2156 gfc_match_intrinsic (void)
2157 {
2158
2159   gfc_clear_attr (&current_attr);
2160   gfc_add_intrinsic (&current_attr, NULL);
2161
2162   return attr_decl ();
2163 }
2164
2165
2166 match
2167 gfc_match_optional (void)
2168 {
2169
2170   gfc_clear_attr (&current_attr);
2171   gfc_add_optional (&current_attr, NULL);
2172
2173   return attr_decl ();
2174 }
2175
2176
2177 match
2178 gfc_match_pointer (void)
2179 {
2180
2181   gfc_clear_attr (&current_attr);
2182   gfc_add_pointer (&current_attr, NULL);
2183
2184   return attr_decl ();
2185 }
2186
2187
2188 match
2189 gfc_match_allocatable (void)
2190 {
2191
2192   gfc_clear_attr (&current_attr);
2193   gfc_add_allocatable (&current_attr, NULL);
2194
2195   return attr_decl ();
2196 }
2197
2198
2199 match
2200 gfc_match_dimension (void)
2201 {
2202
2203   gfc_clear_attr (&current_attr);
2204   gfc_add_dimension (&current_attr, NULL);
2205
2206   return attr_decl ();
2207 }
2208
2209
2210 match
2211 gfc_match_target (void)
2212 {
2213
2214   gfc_clear_attr (&current_attr);
2215   gfc_add_target (&current_attr, NULL);
2216
2217   return attr_decl ();
2218 }
2219
2220
2221 /* Match the list of entities being specified in a PUBLIC or PRIVATE
2222    statement.  */
2223
2224 static match
2225 access_attr_decl (gfc_statement st)
2226 {
2227   char name[GFC_MAX_SYMBOL_LEN + 1];
2228   interface_type type;
2229   gfc_user_op *uop;
2230   gfc_symbol *sym;
2231   gfc_intrinsic_op operator;
2232   match m;
2233
2234   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
2235     goto done;
2236
2237   for (;;)
2238     {
2239       m = gfc_match_generic_spec (&type, name, &operator);
2240       if (m == MATCH_NO)
2241         goto syntax;
2242       if (m == MATCH_ERROR)
2243         return MATCH_ERROR;
2244
2245       switch (type)
2246         {
2247         case INTERFACE_NAMELESS:
2248           goto syntax;
2249
2250         case INTERFACE_GENERIC:
2251           if (gfc_get_symbol (name, NULL, &sym))
2252             goto done;
2253
2254           if (gfc_add_access (&sym->attr,
2255                               (st ==
2256                                ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
2257                               NULL) == FAILURE)
2258             return MATCH_ERROR;
2259
2260           break;
2261
2262         case INTERFACE_INTRINSIC_OP:
2263           if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
2264             {
2265               gfc_current_ns->operator_access[operator] =
2266                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2267             }
2268           else
2269             {
2270               gfc_error ("Access specification of the %s operator at %C has "
2271                          "already been specified", gfc_op2string (operator));
2272               goto done;
2273             }
2274
2275           break;
2276
2277         case INTERFACE_USER_OP:
2278           uop = gfc_get_uop (name);
2279
2280           if (uop->access == ACCESS_UNKNOWN)
2281             {
2282               uop->access =
2283                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2284             }
2285           else
2286             {
2287               gfc_error
2288                 ("Access specification of the .%s. operator at %C has "
2289                  "already been specified", sym->name);
2290               goto done;
2291             }
2292
2293           break;
2294         }
2295
2296       if (gfc_match_char (',') == MATCH_NO)
2297         break;
2298     }
2299
2300   if (gfc_match_eos () != MATCH_YES)
2301     goto syntax;
2302   return MATCH_YES;
2303
2304 syntax:
2305   gfc_syntax_error (st);
2306
2307 done:
2308   return MATCH_ERROR;
2309 }
2310
2311
2312 /* The PRIVATE statement is a bit weird in that it can be a attribute
2313    declaration, but also works as a standlone statement inside of a
2314    type declaration or a module.  */
2315
2316 match
2317 gfc_match_private (gfc_statement * st)
2318 {
2319
2320   if (gfc_match ("private") != MATCH_YES)
2321     return MATCH_NO;
2322
2323   if (gfc_current_state () == COMP_DERIVED)
2324     {
2325       if (gfc_match_eos () == MATCH_YES)
2326         {
2327           *st = ST_PRIVATE;
2328           return MATCH_YES;
2329         }
2330
2331       gfc_syntax_error (ST_PRIVATE);
2332       return MATCH_ERROR;
2333     }
2334
2335   if (gfc_match_eos () == MATCH_YES)
2336     {
2337       *st = ST_PRIVATE;
2338       return MATCH_YES;
2339     }
2340
2341   *st = ST_ATTR_DECL;
2342   return access_attr_decl (ST_PRIVATE);
2343 }
2344
2345
2346 match
2347 gfc_match_public (gfc_statement * st)
2348 {
2349
2350   if (gfc_match ("public") != MATCH_YES)
2351     return MATCH_NO;
2352
2353   if (gfc_match_eos () == MATCH_YES)
2354     {
2355       *st = ST_PUBLIC;
2356       return MATCH_YES;
2357     }
2358
2359   *st = ST_ATTR_DECL;
2360   return access_attr_decl (ST_PUBLIC);
2361 }
2362
2363
2364 /* Workhorse for gfc_match_parameter.  */
2365
2366 static match
2367 do_parm (void)
2368 {
2369   gfc_symbol *sym;
2370   gfc_expr *init;
2371   match m;
2372
2373   m = gfc_match_symbol (&sym, 0);
2374   if (m == MATCH_NO)
2375     gfc_error ("Expected variable name at %C in PARAMETER statement");
2376
2377   if (m != MATCH_YES)
2378     return m;
2379
2380   if (gfc_match_char ('=') == MATCH_NO)
2381     {
2382       gfc_error ("Expected = sign in PARAMETER statement at %C");
2383       return MATCH_ERROR;
2384     }
2385
2386   m = gfc_match_init_expr (&init);
2387   if (m == MATCH_NO)
2388     gfc_error ("Expected expression at %C in PARAMETER statement");
2389   if (m != MATCH_YES)
2390     return m;
2391
2392   if (sym->ts.type == BT_UNKNOWN
2393       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2394     {
2395       m = MATCH_ERROR;
2396       goto cleanup;
2397     }
2398
2399   if (gfc_check_assign_symbol (sym, init) == FAILURE
2400       || gfc_add_flavor (&sym->attr, FL_PARAMETER, NULL) == FAILURE)
2401     {
2402       m = MATCH_ERROR;
2403       goto cleanup;
2404     }
2405
2406   sym->value = init;
2407   return MATCH_YES;
2408
2409 cleanup:
2410   gfc_free_expr (init);
2411   return m;
2412 }
2413
2414
2415 /* Match a parameter statement, with the weird syntax that these have.  */
2416
2417 match
2418 gfc_match_parameter (void)
2419 {
2420   match m;
2421
2422   if (gfc_match_char ('(') == MATCH_NO)
2423     return MATCH_NO;
2424
2425   for (;;)
2426     {
2427       m = do_parm ();
2428       if (m != MATCH_YES)
2429         break;
2430
2431       if (gfc_match (" )%t") == MATCH_YES)
2432         break;
2433
2434       if (gfc_match_char (',') != MATCH_YES)
2435         {
2436           gfc_error ("Unexpected characters in PARAMETER statement at %C");
2437           m = MATCH_ERROR;
2438           break;
2439         }
2440     }
2441
2442   return m;
2443 }
2444
2445
2446 /* Save statements have a special syntax.  */
2447
2448 match
2449 gfc_match_save (void)
2450 {
2451   gfc_symbol *sym;
2452   match m;
2453
2454   if (gfc_match_eos () == MATCH_YES)
2455     {
2456       if (gfc_current_ns->seen_save)
2457         {
2458           gfc_error ("Blanket SAVE statement at %C follows previous "
2459                      "SAVE statement");
2460
2461           return MATCH_ERROR;
2462         }
2463
2464       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
2465       return MATCH_YES;
2466     }
2467
2468   if (gfc_current_ns->save_all)
2469     {
2470       gfc_error ("SAVE statement at %C follows blanket SAVE statement");
2471       return MATCH_ERROR;
2472     }
2473
2474   gfc_match (" ::");
2475
2476   for (;;)
2477     {
2478       m = gfc_match_symbol (&sym, 0);
2479       switch (m)
2480         {
2481         case MATCH_YES:
2482           if (gfc_add_save (&sym->attr, gfc_current_locus ()) == FAILURE)
2483             return MATCH_ERROR;
2484           goto next_item;
2485
2486         case MATCH_NO:
2487           break;
2488
2489         case MATCH_ERROR:
2490           return MATCH_ERROR;
2491         }
2492
2493       m = gfc_match (" / %s /", &sym);
2494       if (m == MATCH_ERROR)
2495         return MATCH_ERROR;
2496       if (m == MATCH_NO)
2497         goto syntax;
2498
2499       if (gfc_add_saved_common (&sym->attr, NULL) == FAILURE)
2500         return MATCH_ERROR;
2501       gfc_current_ns->seen_save = 1;
2502
2503     next_item:
2504       if (gfc_match_eos () == MATCH_YES)
2505         break;
2506       if (gfc_match_char (',') != MATCH_YES)
2507         goto syntax;
2508     }
2509
2510   return MATCH_YES;
2511
2512 syntax:
2513   gfc_error ("Syntax error in SAVE statement at %C");
2514   return MATCH_ERROR;
2515 }
2516
2517
2518 /* Match a module procedure statement.  Note that we have to modify
2519    symbols in the parent's namespace because the current one was there
2520    to receive symbols that are in a interface's formal argument list.  */
2521
2522 match
2523 gfc_match_modproc (void)
2524 {
2525   char name[GFC_MAX_SYMBOL_LEN + 1];
2526   gfc_symbol *sym;
2527   match m;
2528
2529   if (gfc_state_stack->state != COMP_INTERFACE
2530       || gfc_state_stack->previous == NULL
2531       || current_interface.type == INTERFACE_NAMELESS)
2532     {
2533       gfc_error
2534         ("MODULE PROCEDURE at %C must be in a generic module interface");
2535       return MATCH_ERROR;
2536     }
2537
2538   for (;;)
2539     {
2540       m = gfc_match_name (name);
2541       if (m == MATCH_NO)
2542         goto syntax;
2543       if (m != MATCH_YES)
2544         return MATCH_ERROR;
2545
2546       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
2547         return MATCH_ERROR;
2548
2549       if (sym->attr.proc != PROC_MODULE
2550           && gfc_add_procedure (&sym->attr, PROC_MODULE, NULL) == FAILURE)
2551         return MATCH_ERROR;
2552
2553       if (gfc_add_interface (sym) == FAILURE)
2554         return MATCH_ERROR;
2555
2556       if (gfc_match_eos () == MATCH_YES)
2557         break;
2558       if (gfc_match_char (',') != MATCH_YES)
2559         goto syntax;
2560     }
2561
2562   return MATCH_YES;
2563
2564 syntax:
2565   gfc_syntax_error (ST_MODULE_PROC);
2566   return MATCH_ERROR;
2567 }
2568
2569
2570 /* Match the beginning of a derived type declaration.  If a type name
2571    was the result of a function, then it is possible to have a symbol
2572    already to be known as a derived type yet have no components.  */
2573
2574 match
2575 gfc_match_derived_decl (void)
2576 {
2577   char name[GFC_MAX_SYMBOL_LEN + 1];
2578   symbol_attribute attr;
2579   gfc_symbol *sym;
2580   match m;
2581
2582   if (gfc_current_state () == COMP_DERIVED)
2583     return MATCH_NO;
2584
2585   gfc_clear_attr (&attr);
2586
2587 loop:
2588   if (gfc_match (" , private") == MATCH_YES)
2589     {
2590       if (gfc_find_state (COMP_MODULE) == FAILURE)
2591         {
2592           gfc_error
2593             ("Derived type at %C can only be PRIVATE within a MODULE");
2594           return MATCH_ERROR;
2595         }
2596
2597       if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL) == FAILURE)
2598         return MATCH_ERROR;
2599       goto loop;
2600     }
2601
2602   if (gfc_match (" , public") == MATCH_YES)
2603     {
2604       if (gfc_find_state (COMP_MODULE) == FAILURE)
2605         {
2606           gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
2607           return MATCH_ERROR;
2608         }
2609
2610       if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL) == FAILURE)
2611         return MATCH_ERROR;
2612       goto loop;
2613     }
2614
2615   if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
2616     {
2617       gfc_error ("Expected :: in TYPE definition at %C");
2618       return MATCH_ERROR;
2619     }
2620
2621   m = gfc_match (" %n%t", name);
2622   if (m != MATCH_YES)
2623     return m;
2624
2625   /* Make sure the name isn't the name of an intrinsic type.  The
2626      'double precision' type doesn't get past the name matcher.  */
2627   if (strcmp (name, "integer") == 0
2628       || strcmp (name, "real") == 0
2629       || strcmp (name, "character") == 0
2630       || strcmp (name, "logical") == 0
2631       || strcmp (name, "complex") == 0)
2632     {
2633       gfc_error
2634         ("Type name '%s' at %C cannot be the same as an intrinsic type",
2635          name);
2636       return MATCH_ERROR;
2637     }
2638
2639   if (gfc_get_symbol (name, NULL, &sym))
2640     return MATCH_ERROR;
2641
2642   if (sym->ts.type != BT_UNKNOWN)
2643     {
2644       gfc_error ("Derived type name '%s' at %C already has a basic type "
2645                  "of %s", sym->name, gfc_typename (&sym->ts));
2646       return MATCH_ERROR;
2647     }
2648
2649   /* The symbol may already have the derived attribute without the
2650      components.  The ways this can happen is via a function
2651      definition, an INTRINSIC statement or a subtype in another
2652      derived type that is a pointer.  The first part of the AND clause
2653      is true if a the symbol is not the return value of a function. */
2654   if (sym->attr.flavor != FL_DERIVED
2655       && gfc_add_flavor (&sym->attr, FL_DERIVED, NULL) == FAILURE)
2656     return MATCH_ERROR;
2657
2658   if (sym->components != NULL)
2659     {
2660       gfc_error
2661         ("Derived type definition of '%s' at %C has already been defined",
2662          sym->name);
2663       return MATCH_ERROR;
2664     }
2665
2666   if (attr.access != ACCESS_UNKNOWN
2667       && gfc_add_access (&sym->attr, attr.access, NULL) == FAILURE)
2668     return MATCH_ERROR;
2669
2670   gfc_new_block = sym;
2671
2672   return MATCH_YES;
2673 }