]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
* gfortran.h: Add bitmasks for different FPE traps. Add fpe
[thirdparty/gcc.git] / gcc / fortran / decl.c
CommitLineData
4ee9c684 1/* Declaration statement matcher
0305ad9b 2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Andy Vaught
4
c84b470d 5This file is part of GCC.
4ee9c684 6
c84b470d 7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
4ee9c684 11
c84b470d 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
4ee9c684 16
17You should have received a copy of the GNU General Public License
c84b470d 18along with GCC; see the file COPYING. If not, write to the Free
30d4ffea 19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA. */
4ee9c684 21
22
23#include "config.h"
7436502b 24#include "system.h"
4ee9c684 25#include "gfortran.h"
26#include "match.h"
27#include "parse.h"
4ee9c684 28
29
36ae04f2 30/* This flag is set if an old-style length selector is matched
4ee9c684 31 during a type-declaration statement. */
32
33static int old_char_selector;
34
5739e54e 35/* When variables acquire types and attributes from a declaration
4ee9c684 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
40static gfc_typespec current_ts;
41
42static symbol_attribute current_attr;
43static gfc_array_spec *current_as;
44static int colon_seen;
45
46/* gfc_new_block points to the symbol of a newly matched block. */
47
48gfc_symbol *gfc_new_block;
49
50
b4f45d02 51/********************* DATA statement subroutines *********************/
52
53/* Free a gfc_data_variable structure and everything beneath it. */
54
55static void
56free_variable (gfc_data_variable * p)
57{
58 gfc_data_variable *q;
59
60 for (; p; p = q)
61 {
62 q = p->next;
63 gfc_free_expr (p->expr);
64 gfc_free_iterator (&p->iter, 0);
65 free_variable (p->list);
66
67 gfc_free (p);
68 }
69}
70
71
72/* Free a gfc_data_value structure and everything beneath it. */
73
74static void
75free_value (gfc_data_value * p)
76{
77 gfc_data_value *q;
78
79 for (; p; p = q)
80 {
81 q = p->next;
82 gfc_free_expr (p->expr);
83 gfc_free (p);
84 }
85}
86
87
88/* Free a list of gfc_data structures. */
89
90void
91gfc_free_data (gfc_data * p)
92{
93 gfc_data *q;
94
95 for (; p; p = q)
96 {
97 q = p->next;
98
99 free_variable (p->var);
100 free_value (p->value);
101
102 gfc_free (p);
103 }
104}
105
106
107static match var_element (gfc_data_variable *);
108
109/* Match a list of variables terminated by an iterator and a right
110 parenthesis. */
111
112static match
113var_list (gfc_data_variable * parent)
114{
115 gfc_data_variable *tail, var;
116 match m;
117
118 m = var_element (&var);
119 if (m == MATCH_ERROR)
120 return MATCH_ERROR;
121 if (m == MATCH_NO)
122 goto syntax;
123
124 tail = gfc_get_data_variable ();
125 *tail = var;
126
127 parent->list = tail;
128
129 for (;;)
130 {
131 if (gfc_match_char (',') != MATCH_YES)
132 goto syntax;
133
134 m = gfc_match_iterator (&parent->iter, 1);
135 if (m == MATCH_YES)
136 break;
137 if (m == MATCH_ERROR)
138 return MATCH_ERROR;
139
140 m = var_element (&var);
141 if (m == MATCH_ERROR)
142 return MATCH_ERROR;
143 if (m == MATCH_NO)
144 goto syntax;
145
146 tail->next = gfc_get_data_variable ();
147 tail = tail->next;
148
149 *tail = var;
150 }
151
152 if (gfc_match_char (')') != MATCH_YES)
153 goto syntax;
154 return MATCH_YES;
155
156syntax:
157 gfc_syntax_error (ST_DATA);
158 return MATCH_ERROR;
159}
160
161
162/* Match a single element in a data variable list, which can be a
163 variable-iterator list. */
164
165static match
166var_element (gfc_data_variable * new)
167{
168 match m;
169 gfc_symbol *sym;
170
171 memset (new, 0, sizeof (gfc_data_variable));
172
173 if (gfc_match_char ('(') == MATCH_YES)
174 return var_list (new);
175
176 m = gfc_match_variable (&new->expr, 0);
177 if (m != MATCH_YES)
178 return m;
179
180 sym = new->expr->symtree->n.sym;
181
182 if(sym->value != NULL)
183 {
184 gfc_error ("Variable '%s' at %C already has an initialization",
185 sym->name);
186 return MATCH_ERROR;
187 }
188
be63aff0 189#if 0 /* TODO: Find out where to move this message */
b4f45d02 190 if (sym->attr.in_common)
191 /* See if sym is in the blank common block. */
192 for (t = &sym->ns->blank_common; t; t = t->common_next)
193 if (sym == t->head)
194 {
195 gfc_error ("DATA statement at %C may not initialize variable "
196 "'%s' from blank COMMON", sym->name);
197 return MATCH_ERROR;
198 }
199#endif
200
950683ed 201 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
b4f45d02 202 return MATCH_ERROR;
203
204 return MATCH_YES;
205}
206
207
208/* Match the top-level list of data variables. */
209
210static match
211top_var_list (gfc_data * d)
212{
213 gfc_data_variable var, *tail, *new;
214 match m;
215
216 tail = NULL;
217
218 for (;;)
219 {
220 m = var_element (&var);
221 if (m == MATCH_NO)
222 goto syntax;
223 if (m == MATCH_ERROR)
224 return MATCH_ERROR;
225
226 new = gfc_get_data_variable ();
227 *new = var;
228
229 if (tail == NULL)
230 d->var = new;
231 else
232 tail->next = new;
233
234 tail = new;
235
236 if (gfc_match_char ('/') == MATCH_YES)
237 break;
238 if (gfc_match_char (',') != MATCH_YES)
239 goto syntax;
240 }
241
242 return MATCH_YES;
243
244syntax:
245 gfc_syntax_error (ST_DATA);
246 return MATCH_ERROR;
247}
248
249
250static match
251match_data_constant (gfc_expr ** result)
252{
253 char name[GFC_MAX_SYMBOL_LEN + 1];
254 gfc_symbol *sym;
255 gfc_expr *expr;
256 match m;
257
258 m = gfc_match_literal_constant (&expr, 1);
259 if (m == MATCH_YES)
260 {
261 *result = expr;
262 return MATCH_YES;
263 }
264
265 if (m == MATCH_ERROR)
266 return MATCH_ERROR;
267
268 m = gfc_match_null (result);
269 if (m != MATCH_NO)
270 return m;
271
272 m = gfc_match_name (name);
273 if (m != MATCH_YES)
274 return m;
275
276 if (gfc_find_symbol (name, NULL, 1, &sym))
277 return MATCH_ERROR;
278
279 if (sym == NULL
280 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
281 {
282 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
283 name);
284 return MATCH_ERROR;
285 }
286 else if (sym->attr.flavor == FL_DERIVED)
287 return gfc_match_structure_constructor (sym, result);
288
289 *result = gfc_copy_expr (sym->value);
290 return MATCH_YES;
291}
292
293
294/* Match a list of values in a DATA statement. The leading '/' has
295 already been seen at this point. */
296
297static match
298top_val_list (gfc_data * data)
299{
300 gfc_data_value *new, *tail;
301 gfc_expr *expr;
302 const char *msg;
303 match m;
304
305 tail = NULL;
306
307 for (;;)
308 {
309 m = match_data_constant (&expr);
310 if (m == MATCH_NO)
311 goto syntax;
312 if (m == MATCH_ERROR)
313 return MATCH_ERROR;
314
315 new = gfc_get_data_value ();
316
317 if (tail == NULL)
318 data->value = new;
319 else
320 tail->next = new;
321
322 tail = new;
323
324 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
325 {
326 tail->expr = expr;
327 tail->repeat = 1;
328 }
329 else
330 {
331 signed int tmp;
332 msg = gfc_extract_int (expr, &tmp);
333 gfc_free_expr (expr);
334 if (msg != NULL)
335 {
336 gfc_error (msg);
337 return MATCH_ERROR;
338 }
339 tail->repeat = tmp;
340
341 m = match_data_constant (&tail->expr);
342 if (m == MATCH_NO)
343 goto syntax;
344 if (m == MATCH_ERROR)
345 return MATCH_ERROR;
346 }
347
348 if (gfc_match_char ('/') == MATCH_YES)
349 break;
350 if (gfc_match_char (',') == MATCH_NO)
351 goto syntax;
352 }
353
354 return MATCH_YES;
355
356syntax:
357 gfc_syntax_error (ST_DATA);
358 return MATCH_ERROR;
359}
360
361
362/* Matches an old style initialization. */
363
364static match
365match_old_style_init (const char *name)
366{
367 match m;
368 gfc_symtree *st;
369 gfc_data *newdata;
370
371 /* Set up data structure to hold initializers. */
372 gfc_find_sym_tree (name, NULL, 0, &st);
373
374 newdata = gfc_get_data ();
375 newdata->var = gfc_get_data_variable ();
376 newdata->var->expr = gfc_get_variable_expr (st);
377
378 /* Match initial value list. This also eats the terminal
379 '/'. */
380 m = top_val_list (newdata);
381 if (m != MATCH_YES)
382 {
383 gfc_free (newdata);
384 return m;
385 }
386
387 if (gfc_pure (NULL))
388 {
389 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
390 gfc_free (newdata);
391 return MATCH_ERROR;
392 }
393
394 /* Chain in namespace list of DATA initializers. */
395 newdata->next = gfc_current_ns->data;
396 gfc_current_ns->data = newdata;
397
398 return m;
399}
400
401/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
39fca56b 402 we are matching a DATA statement and are therefore issuing an error
b4f45d02 403 if we encounter something unexpected, if not, we're trying to match
fe06c0d5 404 an old-style initialization expression of the form INTEGER I /2/. */
b4f45d02 405
406match
407gfc_match_data (void)
408{
409 gfc_data *new;
410 match m;
411
412 for (;;)
413 {
414 new = gfc_get_data ();
415 new->where = gfc_current_locus;
416
417 m = top_var_list (new);
418 if (m != MATCH_YES)
419 goto cleanup;
420
421 m = top_val_list (new);
422 if (m != MATCH_YES)
423 goto cleanup;
424
425 new->next = gfc_current_ns->data;
426 gfc_current_ns->data = new;
427
428 if (gfc_match_eos () == MATCH_YES)
429 break;
430
431 gfc_match_char (','); /* Optional comma */
432 }
433
434 if (gfc_pure (NULL))
435 {
436 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
437 return MATCH_ERROR;
438 }
439
440 return MATCH_YES;
441
442cleanup:
443 gfc_free_data (new);
444 return MATCH_ERROR;
445}
446
447
448/************************ Declaration statements *********************/
449
4ee9c684 450/* Match an intent specification. Since this can only happen after an
451 INTENT word, a legal intent-spec must follow. */
452
453static sym_intent
454match_intent_spec (void)
455{
456
457 if (gfc_match (" ( in out )") == MATCH_YES)
458 return INTENT_INOUT;
459 if (gfc_match (" ( in )") == MATCH_YES)
460 return INTENT_IN;
461 if (gfc_match (" ( out )") == MATCH_YES)
462 return INTENT_OUT;
463
464 gfc_error ("Bad INTENT specification at %C");
465 return INTENT_UNKNOWN;
466}
467
468
469/* Matches a character length specification, which is either a
470 specification expression or a '*'. */
471
472static match
473char_len_param_value (gfc_expr ** expr)
474{
475
476 if (gfc_match_char ('*') == MATCH_YES)
477 {
478 *expr = NULL;
479 return MATCH_YES;
480 }
481
482 return gfc_match_expr (expr);
483}
484
485
486/* A character length is a '*' followed by a literal integer or a
487 char_len_param_value in parenthesis. */
488
489static match
490match_char_length (gfc_expr ** expr)
491{
492 int length;
493 match m;
494
495 m = gfc_match_char ('*');
496 if (m != MATCH_YES)
497 return m;
498
499 m = gfc_match_small_literal_int (&length);
500 if (m == MATCH_ERROR)
501 return m;
502
503 if (m == MATCH_YES)
504 {
505 *expr = gfc_int_expr (length);
506 return m;
507 }
508
509 if (gfc_match_char ('(') == MATCH_NO)
510 goto syntax;
511
512 m = char_len_param_value (expr);
513 if (m == MATCH_ERROR)
514 return m;
515 if (m == MATCH_NO)
516 goto syntax;
517
518 if (gfc_match_char (')') == MATCH_NO)
519 {
520 gfc_free_expr (*expr);
521 *expr = NULL;
522 goto syntax;
523 }
524
525 return MATCH_YES;
526
527syntax:
528 gfc_error ("Syntax error in character length specification at %C");
529 return MATCH_ERROR;
530}
531
532
ee893be6 533/* Special subroutine for finding a symbol. Check if the name is found
534 in the current name space. If not, and we're compiling a function or
535 subroutine and the parent compilation unit is an interface, then check
536 to see if the name we've been given is the name of the interface
537 (located in another namespace). */
4ee9c684 538
539static int
540find_special (const char *name, gfc_symbol ** result)
541{
542 gfc_state_data *s;
ee893be6 543 int i;
4ee9c684 544
ee893be6 545 i = gfc_get_symbol (name, NULL, result);
546 if (i==0)
547 goto end;
548
4ee9c684 549 if (gfc_current_state () != COMP_SUBROUTINE
550 && gfc_current_state () != COMP_FUNCTION)
ee893be6 551 goto end;
4ee9c684 552
553 s = gfc_state_stack->previous;
554 if (s == NULL)
ee893be6 555 goto end;
4ee9c684 556
557 if (s->state != COMP_INTERFACE)
ee893be6 558 goto end;
4ee9c684 559 if (s->sym == NULL)
ee893be6 560 goto end; /* Nameless interface */
4ee9c684 561
562 if (strcmp (name, s->sym->name) == 0)
563 {
564 *result = s->sym;
565 return 0;
566 }
567
ee893be6 568end:
569 return i;
4ee9c684 570}
571
572
573/* Special subroutine for getting a symbol node associated with a
574 procedure name, used in SUBROUTINE and FUNCTION statements. The
575 symbol is created in the parent using with symtree node in the
576 child unit pointing to the symbol. If the current namespace has no
577 parent, then the symbol is just created in the current unit. */
578
579static int
580get_proc_name (const char *name, gfc_symbol ** result)
581{
582 gfc_symtree *st;
583 gfc_symbol *sym;
584 int rc;
585
586 if (gfc_current_ns->parent == NULL)
587 return gfc_get_symbol (name, NULL, result);
588
589 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
590 if (*result == NULL)
591 return rc;
592
1b716045 593 /* ??? Deal with ENTRY problem */
4ee9c684 594
595 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
596
597 sym = *result;
598 st->n.sym = sym;
599 sym->refs++;
600
601 /* See if the procedure should be a module procedure */
602
603 if (sym->ns->proc_name != NULL
604 && sym->ns->proc_name->attr.flavor == FL_MODULE
605 && sym->attr.proc != PROC_MODULE
950683ed 606 && gfc_add_procedure (&sym->attr, PROC_MODULE,
607 sym->name, NULL) == FAILURE)
4ee9c684 608 rc = 2;
609
610 return rc;
611}
612
613
614/* Function called by variable_decl() that adds a name to the symbol
615 table. */
616
617static try
618build_sym (const char *name, gfc_charlen * cl,
619 gfc_array_spec ** as, locus * var_locus)
620{
621 symbol_attribute attr;
622 gfc_symbol *sym;
623
ee893be6 624 /* if (find_special (name, &sym)) */
625 if (gfc_get_symbol (name, NULL, &sym))
4ee9c684 626 return FAILURE;
627
628 /* Start updating the symbol table. Add basic type attribute
629 if present. */
630 if (current_ts.type != BT_UNKNOWN
631 &&(sym->attr.implicit_type == 0
632 || !gfc_compare_types (&sym->ts, &current_ts))
633 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
634 return FAILURE;
635
636 if (sym->ts.type == BT_CHARACTER)
637 sym->ts.cl = cl;
638
639 /* Add dimension attribute if present. */
640 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
641 return FAILURE;
642 *as = NULL;
643
644 /* Add attribute to symbol. The copy is so that we can reset the
645 dimension attribute. */
646 attr = current_attr;
647 attr.dimension = 0;
648
649 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
650 return FAILURE;
651
652 return SUCCESS;
653}
654
a270dc8e 655/* Set character constant to the given length. The constant will be padded or
656 truncated. */
657
658void
659gfc_set_constant_character_len (int len, gfc_expr * expr)
660{
661 char * s;
662 int slen;
663
664 gcc_assert (expr->expr_type == EXPR_CONSTANT);
665 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
666
667 slen = expr->value.character.length;
668 if (len != slen)
669 {
670 s = gfc_getmem (len);
671 memcpy (s, expr->value.character.string, MIN (len, slen));
672 if (len > slen)
673 memset (&s[slen], ' ', len - slen);
674 gfc_free (expr->value.character.string);
675 expr->value.character.string = s;
676 expr->value.character.length = len;
677 }
678}
4ee9c684 679
680/* Function called by variable_decl() that adds an initialization
681 expression to a symbol. */
682
683static try
684add_init_expr_to_sym (const char *name, gfc_expr ** initp,
685 locus * var_locus)
686{
687 symbol_attribute attr;
688 gfc_symbol *sym;
689 gfc_expr *init;
690
691 init = *initp;
692 if (find_special (name, &sym))
693 return FAILURE;
694
695 attr = sym->attr;
696
697 /* If this symbol is confirming an implicit parameter type,
698 then an initialization expression is not allowed. */
699 if (attr.flavor == FL_PARAMETER
700 && sym->value != NULL
701 && *initp != NULL)
702 {
703 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
704 sym->name);
705 return FAILURE;
706 }
707
6cbc841e 708 if (attr.in_common
709 && !attr.data
710 && *initp != NULL)
711 {
712 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
713 sym->name);
714 return FAILURE;
715 }
716
4ee9c684 717 if (init == NULL)
718 {
719 /* An initializer is required for PARAMETER declarations. */
720 if (attr.flavor == FL_PARAMETER)
721 {
722 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
723 return FAILURE;
724 }
725 }
726 else
727 {
728 /* If a variable appears in a DATA block, it cannot have an
b97f1a18 729 initializer. */
4ee9c684 730 if (sym->attr.data)
731 {
732 gfc_error
733 ("Variable '%s' at %C with an initializer already appears "
734 "in a DATA statement", sym->name);
735 return FAILURE;
736 }
737
cca3db55 738 /* Check if the assignment can happen. This has to be put off
739 until later for a derived type variable. */
4ee9c684 740 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
741 && gfc_check_assign_symbol (sym, init) == FAILURE)
742 return FAILURE;
743
a270dc8e 744 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
745 {
746 /* Update symbol character length according initializer. */
747 if (sym->ts.cl->length == NULL)
748 {
749 if (init->expr_type == EXPR_CONSTANT)
750 sym->ts.cl->length =
751 gfc_int_expr (init->value.character.length);
752 else if (init->expr_type == EXPR_ARRAY)
753 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
754 }
755 /* Update initializer character length according symbol. */
756 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
757 {
758 int len = mpz_get_si (sym->ts.cl->length->value.integer);
759 gfc_constructor * p;
760
761 if (init->expr_type == EXPR_CONSTANT)
762 gfc_set_constant_character_len (len, init);
763 else if (init->expr_type == EXPR_ARRAY)
764 {
765 gfc_free_expr (init->ts.cl->length);
766 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
767 for (p = init->value.constructor; p; p = p->next)
768 gfc_set_constant_character_len (len, p->expr);
769 }
770 }
771 }
772
4ee9c684 773 /* Add initializer. Make sure we keep the ranks sane. */
774 if (sym->attr.dimension && init->rank == 0)
775 init->rank = sym->as->rank;
776
777 sym->value = init;
778 *initp = NULL;
779 }
780
781 return SUCCESS;
782}
783
784
785/* Function called by variable_decl() that adds a name to a structure
786 being built. */
787
788static try
789build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
790 gfc_array_spec ** as)
791{
792 gfc_component *c;
793
794 /* If the current symbol is of the same derived type that we're
795 constructing, it must have the pointer attribute. */
796 if (current_ts.type == BT_DERIVED
797 && current_ts.derived == gfc_current_block ()
798 && current_attr.pointer == 0)
799 {
800 gfc_error ("Component at %C must have the POINTER attribute");
801 return FAILURE;
802 }
803
804 if (gfc_current_block ()->attr.pointer
805 && (*as)->rank != 0)
806 {
807 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
808 {
809 gfc_error ("Array component of structure at %C must have explicit "
810 "or deferred shape");
811 return FAILURE;
812 }
813 }
814
815 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
816 return FAILURE;
817
818 c->ts = current_ts;
819 c->ts.cl = cl;
820 gfc_set_component_attr (c, &current_attr);
821
822 c->initializer = *init;
823 *init = NULL;
824
825 c->as = *as;
826 if (c->as != NULL)
827 c->dimension = 1;
828 *as = NULL;
829
830 /* Check array components. */
831 if (!c->dimension)
832 return SUCCESS;
833
834 if (c->pointer)
835 {
836 if (c->as->type != AS_DEFERRED)
837 {
838 gfc_error ("Pointer array component of structure at %C "
839 "must have a deferred shape");
840 return FAILURE;
841 }
842 }
843 else
844 {
845 if (c->as->type != AS_EXPLICIT)
846 {
847 gfc_error
848 ("Array component of structure at %C must have an explicit "
849 "shape");
850 return FAILURE;
851 }
852 }
853
854 return SUCCESS;
855}
856
857
858/* Match a 'NULL()', and possibly take care of some side effects. */
859
860match
861gfc_match_null (gfc_expr ** result)
862{
863 gfc_symbol *sym;
864 gfc_expr *e;
865 match m;
866
867 m = gfc_match (" null ( )");
868 if (m != MATCH_YES)
869 return m;
870
871 /* The NULL symbol now has to be/become an intrinsic function. */
872 if (gfc_get_symbol ("null", NULL, &sym))
873 {
874 gfc_error ("NULL() initialization at %C is ambiguous");
875 return MATCH_ERROR;
876 }
877
878 gfc_intrinsic_symbol (sym);
879
880 if (sym->attr.proc != PROC_INTRINSIC
950683ed 881 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
882 sym->name, NULL) == FAILURE
883 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
4ee9c684 884 return MATCH_ERROR;
885
886 e = gfc_get_expr ();
cbb9e6aa 887 e->where = gfc_current_locus;
4ee9c684 888 e->expr_type = EXPR_NULL;
889 e->ts.type = BT_UNKNOWN;
890
891 *result = e;
892
893 return MATCH_YES;
894}
895
896
4ee9c684 897/* Match a variable name with an optional initializer. When this
898 subroutine is called, a variable is expected to be parsed next.
899 Depending on what is happening at the moment, updates either the
900 symbol table or the current interface. */
901
902static match
903variable_decl (void)
904{
905 char name[GFC_MAX_SYMBOL_LEN + 1];
906 gfc_expr *initializer, *char_len;
907 gfc_array_spec *as;
908 gfc_charlen *cl;
909 locus var_locus;
910 match m;
911 try t;
912
913 initializer = NULL;
914 as = NULL;
915
916 /* When we get here, we've just matched a list of attributes and
917 maybe a type and a double colon. The next thing we expect to see
918 is the name of the symbol. */
919 m = gfc_match_name (name);
920 if (m != MATCH_YES)
921 goto cleanup;
922
cbb9e6aa 923 var_locus = gfc_current_locus;
4ee9c684 924
925 /* Now we could see the optional array spec. or character length. */
926 m = gfc_match_array_spec (&as);
927 if (m == MATCH_ERROR)
928 goto cleanup;
929 if (m == MATCH_NO)
930 as = gfc_copy_array_spec (current_as);
931
932 char_len = NULL;
933 cl = NULL;
934
935 if (current_ts.type == BT_CHARACTER)
936 {
937 switch (match_char_length (&char_len))
938 {
939 case MATCH_YES:
940 cl = gfc_get_charlen ();
941 cl->next = gfc_current_ns->cl_list;
942 gfc_current_ns->cl_list = cl;
943
944 cl->length = char_len;
945 break;
946
947 case MATCH_NO:
948 cl = current_ts.cl;
949 break;
950
951 case MATCH_ERROR:
952 goto cleanup;
953 }
954 }
955
956 /* OK, we've successfully matched the declaration. Now put the
957 symbol in the current namespace, because it might be used in the
fe06c0d5 958 optional initialization expression for this symbol, e.g. this is
4ee9c684 959 perfectly legal:
960
961 integer, parameter :: i = huge(i)
962
963 This is only true for parameters or variables of a basic type.
964 For components of derived types, it is not true, so we don't
965 create a symbol for those yet. If we fail to create the symbol,
966 bail out. */
967 if (gfc_current_state () != COMP_DERIVED
968 && build_sym (name, cl, &as, &var_locus) == FAILURE)
969 {
970 m = MATCH_ERROR;
971 goto cleanup;
972 }
973
974 /* In functions that have a RESULT variable defined, the function
975 name always refers to function calls. Therefore, the name is
976 not allowed to appear in specification statements. */
977 if (gfc_current_state () == COMP_FUNCTION
978 && gfc_current_block () != NULL
979 && gfc_current_block ()->result != NULL
980 && gfc_current_block ()->result != gfc_current_block ()
981 && strcmp (gfc_current_block ()->name, name) == 0)
982 {
983 gfc_error ("Function name '%s' not allowed at %C", name);
984 m = MATCH_ERROR;
985 goto cleanup;
986 }
987
b4f45d02 988 /* We allow old-style initializations of the form
989 integer i /2/, j(4) /3*3, 1/
990 (if no colon has been seen). These are different from data
991 statements in that initializers are only allowed to apply to the
992 variable immediately preceding, i.e.
993 integer i, j /1, 2/
994 is not allowed. Therefore we have to do some work manually, that
cca3db55 995 could otherwise be left to the matchers for DATA statements. */
b4f45d02 996
997 if (!colon_seen && gfc_match (" /") == MATCH_YES)
998 {
999 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1000 "initialization at %C") == FAILURE)
1001 return MATCH_ERROR;
1002
1003 return match_old_style_init (name);
1004 }
1005
4ee9c684 1006 /* The double colon must be present in order to have initializers.
1007 Otherwise the statement is ambiguous with an assignment statement. */
1008 if (colon_seen)
1009 {
1010 if (gfc_match (" =>") == MATCH_YES)
1011 {
1012
1013 if (!current_attr.pointer)
1014 {
1015 gfc_error ("Initialization at %C isn't for a pointer variable");
1016 m = MATCH_ERROR;
1017 goto cleanup;
1018 }
1019
1020 m = gfc_match_null (&initializer);
1021 if (m == MATCH_NO)
1022 {
1023 gfc_error ("Pointer initialization requires a NULL at %C");
1024 m = MATCH_ERROR;
1025 }
1026
1027 if (gfc_pure (NULL))
1028 {
1029 gfc_error
1030 ("Initialization of pointer at %C is not allowed in a "
1031 "PURE procedure");
1032 m = MATCH_ERROR;
1033 }
1034
1035 if (m != MATCH_YES)
1036 goto cleanup;
1037
1038 initializer->ts = current_ts;
1039
1040 }
1041 else if (gfc_match_char ('=') == MATCH_YES)
1042 {
1043 if (current_attr.pointer)
1044 {
1045 gfc_error
1046 ("Pointer initialization at %C requires '=>', not '='");
1047 m = MATCH_ERROR;
1048 goto cleanup;
1049 }
1050
1051 m = gfc_match_init_expr (&initializer);
1052 if (m == MATCH_NO)
1053 {
1054 gfc_error ("Expected an initialization expression at %C");
1055 m = MATCH_ERROR;
1056 }
1057
1058 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1059 {
1060 gfc_error
1061 ("Initialization of variable at %C is not allowed in a "
1062 "PURE procedure");
1063 m = MATCH_ERROR;
1064 }
1065
1066 if (m != MATCH_YES)
1067 goto cleanup;
1068 }
8ffad0f9 1069 }
1070
d9b3f26b 1071 /* Add the initializer. Note that it is fine if initializer is
4ee9c684 1072 NULL here, because we sometimes also need to check if a
1073 declaration *must* have an initialization expression. */
1074 if (gfc_current_state () != COMP_DERIVED)
1075 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1076 else
d9b3f26b 1077 {
1078 if (current_ts.type == BT_DERIVED && !initializer)
1079 initializer = gfc_default_initializer (&current_ts);
1080 t = build_struct (name, cl, &initializer, &as);
1081 }
4ee9c684 1082
1083 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1084
1085cleanup:
1086 /* Free stuff up and return. */
1087 gfc_free_expr (initializer);
1088 gfc_free_array_spec (as);
1089
1090 return m;
1091}
1092
1093
1094/* Match an extended-f77 kind specification. */
1095
1096match
1097gfc_match_old_kind_spec (gfc_typespec * ts)
1098{
1099 match m;
1100
1101 if (gfc_match_char ('*') != MATCH_YES)
1102 return MATCH_NO;
1103
1104 m = gfc_match_small_literal_int (&ts->kind);
1105 if (m != MATCH_YES)
1106 return MATCH_ERROR;
1107
1108 /* Massage the kind numbers for complex types. */
1109 if (ts->type == BT_COMPLEX && ts->kind == 8)
1110 ts->kind = 4;
1111 if (ts->type == BT_COMPLEX && ts->kind == 16)
1112 ts->kind = 8;
1113
f2d4ef3b 1114 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 1115 {
1116 gfc_error ("Old-style kind %d not supported for type %s at %C",
1117 ts->kind, gfc_basic_typename (ts->type));
1118
1119 return MATCH_ERROR;
1120 }
1121
1122 return MATCH_YES;
1123}
1124
1125
1126/* Match a kind specification. Since kinds are generally optional, we
1127 usually return MATCH_NO if something goes wrong. If a "kind="
1128 string is found, then we know we have an error. */
1129
1130match
1131gfc_match_kind_spec (gfc_typespec * ts)
1132{
1133 locus where;
1134 gfc_expr *e;
1135 match m, n;
1136 const char *msg;
1137
1138 m = MATCH_NO;
1139 e = NULL;
1140
cbb9e6aa 1141 where = gfc_current_locus;
4ee9c684 1142
1143 if (gfc_match_char ('(') == MATCH_NO)
1144 return MATCH_NO;
1145
1146 /* Also gobbles optional text. */
1147 if (gfc_match (" kind = ") == MATCH_YES)
1148 m = MATCH_ERROR;
1149
1150 n = gfc_match_init_expr (&e);
1151 if (n == MATCH_NO)
1152 gfc_error ("Expected initialization expression at %C");
1153 if (n != MATCH_YES)
1154 return MATCH_ERROR;
1155
1156 if (e->rank != 0)
1157 {
1158 gfc_error ("Expected scalar initialization expression at %C");
1159 m = MATCH_ERROR;
1160 goto no_match;
1161 }
1162
1163 msg = gfc_extract_int (e, &ts->kind);
1164 if (msg != NULL)
1165 {
1166 gfc_error (msg);
1167 m = MATCH_ERROR;
1168 goto no_match;
1169 }
1170
1171 gfc_free_expr (e);
1172 e = NULL;
1173
f2d4ef3b 1174 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 1175 {
1176 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1177 gfc_basic_typename (ts->type));
1178
1179 m = MATCH_ERROR;
1180 goto no_match;
1181 }
1182
1183 if (gfc_match_char (')') != MATCH_YES)
1184 {
1185 gfc_error ("Missing right paren at %C");
1186 goto no_match;
1187 }
1188
1189 return MATCH_YES;
1190
1191no_match:
1192 gfc_free_expr (e);
cbb9e6aa 1193 gfc_current_locus = where;
4ee9c684 1194 return m;
1195}
1196
1197
1198/* Match the various kind/length specifications in a CHARACTER
1199 declaration. We don't return MATCH_NO. */
1200
1201static match
1202match_char_spec (gfc_typespec * ts)
1203{
1204 int i, kind, seen_length;
1205 gfc_charlen *cl;
1206 gfc_expr *len;
1207 match m;
1208
b8a891cb 1209 kind = gfc_default_character_kind;
4ee9c684 1210 len = NULL;
1211 seen_length = 0;
1212
1213 /* Try the old-style specification first. */
1214 old_char_selector = 0;
1215
1216 m = match_char_length (&len);
1217 if (m != MATCH_NO)
1218 {
1219 if (m == MATCH_YES)
1220 old_char_selector = 1;
1221 seen_length = 1;
1222 goto done;
1223 }
1224
1225 m = gfc_match_char ('(');
1226 if (m != MATCH_YES)
1227 {
1228 m = MATCH_YES; /* character without length is a single char */
1229 goto done;
1230 }
1231
1232 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1233 if (gfc_match (" kind =") == MATCH_YES)
1234 {
1235 m = gfc_match_small_int (&kind);
1236 if (m == MATCH_ERROR)
1237 goto done;
1238 if (m == MATCH_NO)
1239 goto syntax;
1240
1241 if (gfc_match (" , len =") == MATCH_NO)
1242 goto rparen;
1243
1244 m = char_len_param_value (&len);
1245 if (m == MATCH_NO)
1246 goto syntax;
1247 if (m == MATCH_ERROR)
1248 goto done;
1249 seen_length = 1;
1250
1251 goto rparen;
1252 }
1253
1254 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
1255 if (gfc_match (" len =") == MATCH_YES)
1256 {
1257 m = char_len_param_value (&len);
1258 if (m == MATCH_NO)
1259 goto syntax;
1260 if (m == MATCH_ERROR)
1261 goto done;
1262 seen_length = 1;
1263
1264 if (gfc_match_char (')') == MATCH_YES)
1265 goto done;
1266
1267 if (gfc_match (" , kind =") != MATCH_YES)
1268 goto syntax;
1269
1270 gfc_match_small_int (&kind);
1271
f2d4ef3b 1272 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
4ee9c684 1273 {
1274 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1275 return MATCH_YES;
1276 }
1277
1278 goto rparen;
1279 }
1280
1281 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1282 m = char_len_param_value (&len);
1283 if (m == MATCH_NO)
1284 goto syntax;
1285 if (m == MATCH_ERROR)
1286 goto done;
1287 seen_length = 1;
1288
1289 m = gfc_match_char (')');
1290 if (m == MATCH_YES)
1291 goto done;
1292
1293 if (gfc_match_char (',') != MATCH_YES)
1294 goto syntax;
1295
1296 gfc_match (" kind ="); /* Gobble optional text */
1297
1298 m = gfc_match_small_int (&kind);
1299 if (m == MATCH_ERROR)
1300 goto done;
1301 if (m == MATCH_NO)
1302 goto syntax;
1303
1304rparen:
1305 /* Require a right-paren at this point. */
1306 m = gfc_match_char (')');
1307 if (m == MATCH_YES)
1308 goto done;
1309
1310syntax:
1311 gfc_error ("Syntax error in CHARACTER declaration at %C");
1312 m = MATCH_ERROR;
1313
1314done:
f2d4ef3b 1315 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
4ee9c684 1316 {
1317 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1318 m = MATCH_ERROR;
1319 }
1320
1321 if (m != MATCH_YES)
1322 {
1323 gfc_free_expr (len);
1324 return m;
1325 }
1326
1327 /* Do some final massaging of the length values. */
1328 cl = gfc_get_charlen ();
1329 cl->next = gfc_current_ns->cl_list;
1330 gfc_current_ns->cl_list = cl;
1331
1332 if (seen_length == 0)
1333 cl->length = gfc_int_expr (1);
1334 else
1335 {
1336 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1337 cl->length = len;
1338 else
1339 {
1340 gfc_free_expr (len);
1341 cl->length = gfc_int_expr (0);
1342 }
1343 }
1344
1345 ts->cl = cl;
1346 ts->kind = kind;
1347
1348 return MATCH_YES;
1349}
1350
1351
1352/* Matches a type specification. If successful, sets the ts structure
1353 to the matched specification. This is necessary for FUNCTION and
1354 IMPLICIT statements.
1355
39351103 1356 If implicit_flag is nonzero, then we don't check for the optional
1357 kind specification. Not doing so is needed for matching an IMPLICIT
4ee9c684 1358 statement correctly. */
1359
39351103 1360static match
1361match_type_spec (gfc_typespec * ts, int implicit_flag)
4ee9c684 1362{
1363 char name[GFC_MAX_SYMBOL_LEN + 1];
1364 gfc_symbol *sym;
1365 match m;
18f3698a 1366 int c;
4ee9c684 1367
1368 gfc_clear_ts (ts);
1369
1370 if (gfc_match (" integer") == MATCH_YES)
1371 {
1372 ts->type = BT_INTEGER;
b8a891cb 1373 ts->kind = gfc_default_integer_kind;
4ee9c684 1374 goto get_kind;
1375 }
1376
1377 if (gfc_match (" character") == MATCH_YES)
1378 {
1379 ts->type = BT_CHARACTER;
39351103 1380 if (implicit_flag == 0)
1381 return match_char_spec (ts);
1382 else
1383 return MATCH_YES;
4ee9c684 1384 }
1385
1386 if (gfc_match (" real") == MATCH_YES)
1387 {
1388 ts->type = BT_REAL;
b8a891cb 1389 ts->kind = gfc_default_real_kind;
4ee9c684 1390 goto get_kind;
1391 }
1392
1393 if (gfc_match (" double precision") == MATCH_YES)
1394 {
1395 ts->type = BT_REAL;
b8a891cb 1396 ts->kind = gfc_default_double_kind;
4ee9c684 1397 return MATCH_YES;
1398 }
1399
1400 if (gfc_match (" complex") == MATCH_YES)
1401 {
1402 ts->type = BT_COMPLEX;
b8a891cb 1403 ts->kind = gfc_default_complex_kind;
4ee9c684 1404 goto get_kind;
1405 }
1406
1407 if (gfc_match (" double complex") == MATCH_YES)
1408 {
1409 ts->type = BT_COMPLEX;
b8a891cb 1410 ts->kind = gfc_default_double_kind;
4ee9c684 1411 return MATCH_YES;
1412 }
1413
1414 if (gfc_match (" logical") == MATCH_YES)
1415 {
1416 ts->type = BT_LOGICAL;
b8a891cb 1417 ts->kind = gfc_default_logical_kind;
4ee9c684 1418 goto get_kind;
1419 }
1420
1421 m = gfc_match (" type ( %n )", name);
1422 if (m != MATCH_YES)
1423 return m;
1424
1425 /* Search for the name but allow the components to be defined later. */
1426 if (gfc_get_ha_symbol (name, &sym))
1427 {
1428 gfc_error ("Type name '%s' at %C is ambiguous", name);
1429 return MATCH_ERROR;
1430 }
1431
1432 if (sym->attr.flavor != FL_DERIVED
950683ed 1433 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4ee9c684 1434 return MATCH_ERROR;
1435
1436 ts->type = BT_DERIVED;
1437 ts->kind = 0;
1438 ts->derived = sym;
1439
1440 return MATCH_YES;
1441
1442get_kind:
1443 /* For all types except double, derived and character, look for an
1444 optional kind specifier. MATCH_NO is actually OK at this point. */
39351103 1445 if (implicit_flag == 1)
4ee9c684 1446 return MATCH_YES;
1447
18f3698a 1448 if (gfc_current_form == FORM_FREE)
1449 {
1450 c = gfc_peek_char();
1451 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1452 && c != ':' && c != ',')
1453 return MATCH_NO;
1454 }
1455
4ee9c684 1456 m = gfc_match_kind_spec (ts);
1457 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1458 m = gfc_match_old_kind_spec (ts);
1459
1460 if (m == MATCH_NO)
1461 m = MATCH_YES; /* No kind specifier found. */
1462
1463 return m;
1464}
1465
1466
39351103 1467/* Match an IMPLICIT NONE statement. Actually, this statement is
1468 already matched in parse.c, or we would not end up here in the
1469 first place. So the only thing we need to check, is if there is
1470 trailing garbage. If not, the match is successful. */
1471
1472match
1473gfc_match_implicit_none (void)
1474{
1475
1476 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1477}
1478
1479
1480/* Match the letter range(s) of an IMPLICIT statement. */
1481
1482static match
b70528c7 1483match_implicit_range (void)
39351103 1484{
1485 int c, c1, c2, inner;
1486 locus cur_loc;
1487
1488 cur_loc = gfc_current_locus;
1489
1490 gfc_gobble_whitespace ();
1491 c = gfc_next_char ();
1492 if (c != '(')
1493 {
1494 gfc_error ("Missing character range in IMPLICIT at %C");
1495 goto bad;
1496 }
1497
1498 inner = 1;
1499 while (inner)
1500 {
1501 gfc_gobble_whitespace ();
1502 c1 = gfc_next_char ();
1503 if (!ISALPHA (c1))
1504 goto bad;
1505
1506 gfc_gobble_whitespace ();
1507 c = gfc_next_char ();
1508
1509 switch (c)
1510 {
1511 case ')':
1512 inner = 0; /* Fall through */
1513
1514 case ',':
1515 c2 = c1;
1516 break;
1517
1518 case '-':
1519 gfc_gobble_whitespace ();
1520 c2 = gfc_next_char ();
1521 if (!ISALPHA (c2))
1522 goto bad;
1523
1524 gfc_gobble_whitespace ();
1525 c = gfc_next_char ();
1526
1527 if ((c != ',') && (c != ')'))
1528 goto bad;
1529 if (c == ')')
1530 inner = 0;
1531
1532 break;
1533
1534 default:
1535 goto bad;
1536 }
1537
1538 if (c1 > c2)
1539 {
1540 gfc_error ("Letters must be in alphabetic order in "
1541 "IMPLICIT statement at %C");
1542 goto bad;
1543 }
1544
1545 /* See if we can add the newly matched range to the pending
1546 implicits from this IMPLICIT statement. We do not check for
1547 conflicts with whatever earlier IMPLICIT statements may have
1548 set. This is done when we've successfully finished matching
1549 the current one. */
b70528c7 1550 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
39351103 1551 goto bad;
1552 }
1553
1554 return MATCH_YES;
1555
1556bad:
1557 gfc_syntax_error (ST_IMPLICIT);
1558
1559 gfc_current_locus = cur_loc;
1560 return MATCH_ERROR;
1561}
1562
1563
1564/* Match an IMPLICIT statement, storing the types for
1565 gfc_set_implicit() if the statement is accepted by the parser.
1566 There is a strange looking, but legal syntactic construction
1567 possible. It looks like:
1568
1569 IMPLICIT INTEGER (a-b) (c-d)
1570
1571 This is legal if "a-b" is a constant expression that happens to
1572 equal one of the legal kinds for integers. The real problem
1573 happens with an implicit specification that looks like:
1574
1575 IMPLICIT INTEGER (a-b)
1576
1577 In this case, a typespec matcher that is "greedy" (as most of the
1578 matchers are) gobbles the character range as a kindspec, leaving
1579 nothing left. We therefore have to go a bit more slowly in the
1580 matching process by inhibiting the kindspec checking during
1581 typespec matching and checking for a kind later. */
1582
1583match
1584gfc_match_implicit (void)
1585{
1586 gfc_typespec ts;
1587 locus cur_loc;
1588 int c;
1589 match m;
1590
1591 /* We don't allow empty implicit statements. */
1592 if (gfc_match_eos () == MATCH_YES)
1593 {
1594 gfc_error ("Empty IMPLICIT statement at %C");
1595 return MATCH_ERROR;
1596 }
1597
39351103 1598 do
1599 {
b70528c7 1600 /* First cleanup. */
1601 gfc_clear_new_implicit ();
1602
39351103 1603 /* A basic type is mandatory here. */
1604 m = match_type_spec (&ts, 1);
1605 if (m == MATCH_ERROR)
1606 goto error;
1607 if (m == MATCH_NO)
1608 goto syntax;
1609
1610 cur_loc = gfc_current_locus;
b70528c7 1611 m = match_implicit_range ();
39351103 1612
1613 if (m == MATCH_YES)
1614 {
b70528c7 1615 /* We may have <TYPE> (<RANGE>). */
39351103 1616 gfc_gobble_whitespace ();
1617 c = gfc_next_char ();
1618 if ((c == '\n') || (c == ','))
b70528c7 1619 {
1620 /* Check for CHARACTER with no length parameter. */
1621 if (ts.type == BT_CHARACTER && !ts.cl)
1622 {
b8a891cb 1623 ts.kind = gfc_default_character_kind;
b70528c7 1624 ts.cl = gfc_get_charlen ();
1625 ts.cl->next = gfc_current_ns->cl_list;
1626 gfc_current_ns->cl_list = ts.cl;
1627 ts.cl->length = gfc_int_expr (1);
1628 }
1629
1630 /* Record the Successful match. */
1631 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1632 return MATCH_ERROR;
1633 continue;
1634 }
39351103 1635
1636 gfc_current_locus = cur_loc;
1637 }
1638
b70528c7 1639 /* Discard the (incorrectly) matched range. */
1640 gfc_clear_new_implicit ();
1641
1642 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1643 if (ts.type == BT_CHARACTER)
1644 m = match_char_spec (&ts);
1645 else
39351103 1646 {
b70528c7 1647 m = gfc_match_kind_spec (&ts);
39351103 1648 if (m == MATCH_NO)
b70528c7 1649 {
1650 m = gfc_match_old_kind_spec (&ts);
1651 if (m == MATCH_ERROR)
1652 goto error;
1653 if (m == MATCH_NO)
1654 goto syntax;
1655 }
39351103 1656 }
b70528c7 1657 if (m == MATCH_ERROR)
1658 goto error;
39351103 1659
b70528c7 1660 m = match_implicit_range ();
39351103 1661 if (m == MATCH_ERROR)
1662 goto error;
1663 if (m == MATCH_NO)
1664 goto syntax;
1665
1666 gfc_gobble_whitespace ();
1667 c = gfc_next_char ();
1668 if ((c != '\n') && (c != ','))
1669 goto syntax;
1670
b70528c7 1671 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1672 return MATCH_ERROR;
39351103 1673 }
1674 while (c == ',');
1675
b70528c7 1676 return MATCH_YES;
39351103 1677
1678syntax:
1679 gfc_syntax_error (ST_IMPLICIT);
1680
1681error:
1682 return MATCH_ERROR;
1683}
1684
1685
4ee9c684 1686/* Matches an attribute specification including array specs. If
1687 successful, leaves the variables current_attr and current_as
1688 holding the specification. Also sets the colon_seen variable for
1689 later use by matchers associated with initializations.
1690
1691 This subroutine is a little tricky in the sense that we don't know
1692 if we really have an attr-spec until we hit the double colon.
1693 Until that time, we can only return MATCH_NO. This forces us to
1694 check for duplicate specification at this level. */
1695
1696static match
1697match_attr_spec (void)
1698{
1699
1700 /* Modifiers that can exist in a type statement. */
1701 typedef enum
1702 { GFC_DECL_BEGIN = 0,
1703 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1704 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1705 DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1706 DECL_TARGET, DECL_COLON, DECL_NONE,
1707 GFC_DECL_END /* Sentinel */
1708 }
1709 decl_types;
1710
1711/* GFC_DECL_END is the sentinel, index starts at 0. */
1712#define NUM_DECL GFC_DECL_END
1713
1714 static mstring decls[] = {
1715 minit (", allocatable", DECL_ALLOCATABLE),
1716 minit (", dimension", DECL_DIMENSION),
1717 minit (", external", DECL_EXTERNAL),
1718 minit (", intent ( in )", DECL_IN),
1719 minit (", intent ( out )", DECL_OUT),
1720 minit (", intent ( in out )", DECL_INOUT),
1721 minit (", intrinsic", DECL_INTRINSIC),
1722 minit (", optional", DECL_OPTIONAL),
1723 minit (", parameter", DECL_PARAMETER),
1724 minit (", pointer", DECL_POINTER),
1725 minit (", private", DECL_PRIVATE),
1726 minit (", public", DECL_PUBLIC),
1727 minit (", save", DECL_SAVE),
1728 minit (", target", DECL_TARGET),
1729 minit ("::", DECL_COLON),
1730 minit (NULL, DECL_NONE)
1731 };
1732
1733 locus start, seen_at[NUM_DECL];
1734 int seen[NUM_DECL];
1735 decl_types d;
1736 const char *attr;
1737 match m;
1738 try t;
1739
1740 gfc_clear_attr (&current_attr);
cbb9e6aa 1741 start = gfc_current_locus;
4ee9c684 1742
1743 current_as = NULL;
1744 colon_seen = 0;
1745
1746 /* See if we get all of the keywords up to the final double colon. */
1747 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1748 seen[d] = 0;
1749
1750 for (;;)
1751 {
1752 d = (decl_types) gfc_match_strings (decls);
1753 if (d == DECL_NONE || d == DECL_COLON)
1754 break;
1755
1756 seen[d]++;
cbb9e6aa 1757 seen_at[d] = gfc_current_locus;
4ee9c684 1758
1759 if (d == DECL_DIMENSION)
1760 {
1761 m = gfc_match_array_spec (&current_as);
1762
1763 if (m == MATCH_NO)
1764 {
1765 gfc_error ("Missing dimension specification at %C");
1766 m = MATCH_ERROR;
1767 }
1768
1769 if (m == MATCH_ERROR)
1770 goto cleanup;
1771 }
1772 }
1773
1774 /* No double colon, so assume that we've been looking at something
1775 else the whole time. */
1776 if (d == DECL_NONE)
1777 {
1778 m = MATCH_NO;
1779 goto cleanup;
1780 }
1781
1782 /* Since we've seen a double colon, we have to be looking at an
1783 attr-spec. This means that we can now issue errors. */
1784 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1785 if (seen[d] > 1)
1786 {
1787 switch (d)
1788 {
1789 case DECL_ALLOCATABLE:
1790 attr = "ALLOCATABLE";
1791 break;
1792 case DECL_DIMENSION:
1793 attr = "DIMENSION";
1794 break;
1795 case DECL_EXTERNAL:
1796 attr = "EXTERNAL";
1797 break;
1798 case DECL_IN:
1799 attr = "INTENT (IN)";
1800 break;
1801 case DECL_OUT:
1802 attr = "INTENT (OUT)";
1803 break;
1804 case DECL_INOUT:
1805 attr = "INTENT (IN OUT)";
1806 break;
1807 case DECL_INTRINSIC:
1808 attr = "INTRINSIC";
1809 break;
1810 case DECL_OPTIONAL:
1811 attr = "OPTIONAL";
1812 break;
1813 case DECL_PARAMETER:
1814 attr = "PARAMETER";
1815 break;
1816 case DECL_POINTER:
1817 attr = "POINTER";
1818 break;
1819 case DECL_PRIVATE:
1820 attr = "PRIVATE";
1821 break;
1822 case DECL_PUBLIC:
1823 attr = "PUBLIC";
1824 break;
1825 case DECL_SAVE:
1826 attr = "SAVE";
1827 break;
1828 case DECL_TARGET:
1829 attr = "TARGET";
1830 break;
1831 default:
1832 attr = NULL; /* This shouldn't happen */
1833 }
1834
1835 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
1836 m = MATCH_ERROR;
1837 goto cleanup;
1838 }
1839
1840 /* Now that we've dealt with duplicate attributes, add the attributes
1841 to the current attribute. */
1842 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1843 {
1844 if (seen[d] == 0)
1845 continue;
1846
1847 if (gfc_current_state () == COMP_DERIVED
1848 && d != DECL_DIMENSION && d != DECL_POINTER
1849 && d != DECL_COLON && d != DECL_NONE)
1850 {
1851
1852 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
1853 &seen_at[d]);
1854 m = MATCH_ERROR;
1855 goto cleanup;
1856 }
1857
1858 switch (d)
1859 {
1860 case DECL_ALLOCATABLE:
1861 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
1862 break;
1863
1864 case DECL_DIMENSION:
950683ed 1865 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
4ee9c684 1866 break;
1867
1868 case DECL_EXTERNAL:
1869 t = gfc_add_external (&current_attr, &seen_at[d]);
1870 break;
1871
1872 case DECL_IN:
1873 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
1874 break;
1875
1876 case DECL_OUT:
1877 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
1878 break;
1879
1880 case DECL_INOUT:
1881 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
1882 break;
1883
1884 case DECL_INTRINSIC:
1885 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
1886 break;
1887
1888 case DECL_OPTIONAL:
1889 t = gfc_add_optional (&current_attr, &seen_at[d]);
1890 break;
1891
1892 case DECL_PARAMETER:
950683ed 1893 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
4ee9c684 1894 break;
1895
1896 case DECL_POINTER:
1897 t = gfc_add_pointer (&current_attr, &seen_at[d]);
1898 break;
1899
1900 case DECL_PRIVATE:
950683ed 1901 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
1902 &seen_at[d]);
4ee9c684 1903 break;
1904
1905 case DECL_PUBLIC:
950683ed 1906 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
1907 &seen_at[d]);
4ee9c684 1908 break;
1909
1910 case DECL_SAVE:
950683ed 1911 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
4ee9c684 1912 break;
1913
1914 case DECL_TARGET:
1915 t = gfc_add_target (&current_attr, &seen_at[d]);
1916 break;
1917
1918 default:
1919 gfc_internal_error ("match_attr_spec(): Bad attribute");
1920 }
1921
1922 if (t == FAILURE)
1923 {
1924 m = MATCH_ERROR;
1925 goto cleanup;
1926 }
1927 }
1928
1929 colon_seen = 1;
1930 return MATCH_YES;
1931
1932cleanup:
cbb9e6aa 1933 gfc_current_locus = start;
4ee9c684 1934 gfc_free_array_spec (current_as);
1935 current_as = NULL;
1936 return m;
1937}
1938
1939
1940/* Match a data declaration statement. */
1941
1942match
1943gfc_match_data_decl (void)
1944{
1945 gfc_symbol *sym;
1946 match m;
1947
39351103 1948 m = match_type_spec (&current_ts, 0);
4ee9c684 1949 if (m != MATCH_YES)
1950 return m;
1951
1952 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
1953 {
1954 sym = gfc_use_derived (current_ts.derived);
1955
1956 if (sym == NULL)
1957 {
1958 m = MATCH_ERROR;
1959 goto cleanup;
1960 }
1961
1962 current_ts.derived = sym;
1963 }
1964
1965 m = match_attr_spec ();
1966 if (m == MATCH_ERROR)
1967 {
1968 m = MATCH_NO;
1969 goto cleanup;
1970 }
1971
1972 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
1973 {
1974
1975 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
1976 goto ok;
1977
1978 if (gfc_find_symbol (current_ts.derived->name,
1979 current_ts.derived->ns->parent, 1, &sym) == 0)
1980 goto ok;
1981
1982 /* Hope that an ambiguous symbol is itself masked by a type definition. */
1983 if (sym != NULL && sym->attr.flavor == FL_DERIVED)
1984 goto ok;
1985
1986 gfc_error ("Derived type at %C has not been previously defined");
1987 m = MATCH_ERROR;
1988 goto cleanup;
1989 }
1990
1991ok:
1992 /* If we have an old-style character declaration, and no new-style
1993 attribute specifications, then there a comma is optional between
1994 the type specification and the variable list. */
1995 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
1996 gfc_match_char (',');
1997
1998 /* Give the types/attributes to symbols that follow. */
1999 for (;;)
2000 {
2001 m = variable_decl ();
2002 if (m == MATCH_ERROR)
2003 goto cleanup;
2004 if (m == MATCH_NO)
2005 break;
2006
2007 if (gfc_match_eos () == MATCH_YES)
2008 goto cleanup;
2009 if (gfc_match_char (',') != MATCH_YES)
2010 break;
2011 }
2012
2013 gfc_error ("Syntax error in data declaration at %C");
2014 m = MATCH_ERROR;
2015
2016cleanup:
2017 gfc_free_array_spec (current_as);
2018 current_as = NULL;
2019 return m;
2020}
2021
2022
2023/* Match a prefix associated with a function or subroutine
2024 declaration. If the typespec pointer is nonnull, then a typespec
2025 can be matched. Note that if nothing matches, MATCH_YES is
2026 returned (the null string was matched). */
2027
2028static match
2029match_prefix (gfc_typespec * ts)
2030{
2031 int seen_type;
2032
2033 gfc_clear_attr (&current_attr);
2034 seen_type = 0;
2035
2036loop:
2037 if (!seen_type && ts != NULL
39351103 2038 && match_type_spec (ts, 0) == MATCH_YES
4ee9c684 2039 && gfc_match_space () == MATCH_YES)
2040 {
2041
2042 seen_type = 1;
2043 goto loop;
2044 }
2045
2046 if (gfc_match ("elemental% ") == MATCH_YES)
2047 {
2048 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2049 return MATCH_ERROR;
2050
2051 goto loop;
2052 }
2053
2054 if (gfc_match ("pure% ") == MATCH_YES)
2055 {
2056 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2057 return MATCH_ERROR;
2058
2059 goto loop;
2060 }
2061
2062 if (gfc_match ("recursive% ") == MATCH_YES)
2063 {
2064 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2065 return MATCH_ERROR;
2066
2067 goto loop;
2068 }
2069
2070 /* At this point, the next item is not a prefix. */
2071 return MATCH_YES;
2072}
2073
2074
2075/* Copy attributes matched by match_prefix() to attributes on a symbol. */
2076
2077static try
2078copy_prefix (symbol_attribute * dest, locus * where)
2079{
2080
2081 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2082 return FAILURE;
2083
2084 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2085 return FAILURE;
2086
2087 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2088 return FAILURE;
2089
2090 return SUCCESS;
2091}
2092
2093
2094/* Match a formal argument list. */
2095
2096match
2097gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2098{
2099 gfc_formal_arglist *head, *tail, *p, *q;
2100 char name[GFC_MAX_SYMBOL_LEN + 1];
2101 gfc_symbol *sym;
2102 match m;
2103
2104 head = tail = NULL;
2105
2106 if (gfc_match_char ('(') != MATCH_YES)
2107 {
2108 if (null_flag)
2109 goto ok;
2110 return MATCH_NO;
2111 }
2112
2113 if (gfc_match_char (')') == MATCH_YES)
2114 goto ok;
2115
2116 for (;;)
2117 {
2118 if (gfc_match_char ('*') == MATCH_YES)
2119 sym = NULL;
2120 else
2121 {
2122 m = gfc_match_name (name);
2123 if (m != MATCH_YES)
2124 goto cleanup;
2125
2126 if (gfc_get_symbol (name, NULL, &sym))
2127 goto cleanup;
2128 }
2129
2130 p = gfc_get_formal_arglist ();
2131
2132 if (head == NULL)
2133 head = tail = p;
2134 else
2135 {
2136 tail->next = p;
2137 tail = p;
2138 }
2139
2140 tail->sym = sym;
2141
2142 /* We don't add the VARIABLE flavor because the name could be a
2143 dummy procedure. We don't apply these attributes to formal
2144 arguments of statement functions. */
2145 if (sym != NULL && !st_flag
950683ed 2146 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4ee9c684 2147 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2148 {
2149 m = MATCH_ERROR;
2150 goto cleanup;
2151 }
2152
2153 /* The name of a program unit can be in a different namespace,
2154 so check for it explicitly. After the statement is accepted,
2155 the name is checked for especially in gfc_get_symbol(). */
2156 if (gfc_new_block != NULL && sym != NULL
2157 && strcmp (sym->name, gfc_new_block->name) == 0)
2158 {
2159 gfc_error ("Name '%s' at %C is the name of the procedure",
2160 sym->name);
2161 m = MATCH_ERROR;
2162 goto cleanup;
2163 }
2164
2165 if (gfc_match_char (')') == MATCH_YES)
2166 goto ok;
2167
2168 m = gfc_match_char (',');
2169 if (m != MATCH_YES)
2170 {
2171 gfc_error ("Unexpected junk in formal argument list at %C");
2172 goto cleanup;
2173 }
2174 }
2175
2176ok:
2177 /* Check for duplicate symbols in the formal argument list. */
2178 if (head != NULL)
2179 {
2180 for (p = head; p->next; p = p->next)
2181 {
2182 if (p->sym == NULL)
2183 continue;
2184
2185 for (q = p->next; q; q = q->next)
2186 if (p->sym == q->sym)
2187 {
2188 gfc_error
2189 ("Duplicate symbol '%s' in formal argument list at %C",
2190 p->sym->name);
2191
2192 m = MATCH_ERROR;
2193 goto cleanup;
2194 }
2195 }
2196 }
2197
2198 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2199 FAILURE)
2200 {
2201 m = MATCH_ERROR;
2202 goto cleanup;
2203 }
2204
2205 return MATCH_YES;
2206
2207cleanup:
2208 gfc_free_formal_arglist (head);
2209 return m;
2210}
2211
2212
2213/* Match a RESULT specification following a function declaration or
2214 ENTRY statement. Also matches the end-of-statement. */
2215
2216static match
2217match_result (gfc_symbol * function, gfc_symbol ** result)
2218{
2219 char name[GFC_MAX_SYMBOL_LEN + 1];
2220 gfc_symbol *r;
2221 match m;
2222
2223 if (gfc_match (" result (") != MATCH_YES)
2224 return MATCH_NO;
2225
2226 m = gfc_match_name (name);
2227 if (m != MATCH_YES)
2228 return m;
2229
2230 if (gfc_match (" )%t") != MATCH_YES)
2231 {
2232 gfc_error ("Unexpected junk following RESULT variable at %C");
2233 return MATCH_ERROR;
2234 }
2235
2236 if (strcmp (function->name, name) == 0)
2237 {
2238 gfc_error
2239 ("RESULT variable at %C must be different than function name");
2240 return MATCH_ERROR;
2241 }
2242
2243 if (gfc_get_symbol (name, NULL, &r))
2244 return MATCH_ERROR;
2245
950683ed 2246 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2247 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4ee9c684 2248 return MATCH_ERROR;
2249
2250 *result = r;
2251
2252 return MATCH_YES;
2253}
2254
2255
2256/* Match a function declaration. */
2257
2258match
2259gfc_match_function_decl (void)
2260{
2261 char name[GFC_MAX_SYMBOL_LEN + 1];
2262 gfc_symbol *sym, *result;
2263 locus old_loc;
2264 match m;
2265
2266 if (gfc_current_state () != COMP_NONE
2267 && gfc_current_state () != COMP_INTERFACE
2268 && gfc_current_state () != COMP_CONTAINS)
2269 return MATCH_NO;
2270
2271 gfc_clear_ts (&current_ts);
2272
cbb9e6aa 2273 old_loc = gfc_current_locus;
4ee9c684 2274
2275 m = match_prefix (&current_ts);
2276 if (m != MATCH_YES)
2277 {
cbb9e6aa 2278 gfc_current_locus = old_loc;
4ee9c684 2279 return m;
2280 }
2281
2282 if (gfc_match ("function% %n", name) != MATCH_YES)
2283 {
cbb9e6aa 2284 gfc_current_locus = old_loc;
4ee9c684 2285 return MATCH_NO;
2286 }
2287
2288 if (get_proc_name (name, &sym))
2289 return MATCH_ERROR;
2290 gfc_new_block = sym;
2291
2292 m = gfc_match_formal_arglist (sym, 0, 0);
2293 if (m == MATCH_NO)
2294 gfc_error ("Expected formal argument list in function definition at %C");
2295 else if (m == MATCH_ERROR)
2296 goto cleanup;
2297
2298 result = NULL;
2299
2300 if (gfc_match_eos () != MATCH_YES)
2301 {
2302 /* See if a result variable is present. */
2303 m = match_result (sym, &result);
2304 if (m == MATCH_NO)
2305 gfc_error ("Unexpected junk after function declaration at %C");
2306
2307 if (m != MATCH_YES)
2308 {
2309 m = MATCH_ERROR;
2310 goto cleanup;
2311 }
2312 }
2313
2314 /* Make changes to the symbol. */
2315 m = MATCH_ERROR;
2316
950683ed 2317 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4ee9c684 2318 goto cleanup;
2319
2320 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2321 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2322 goto cleanup;
2323
2324 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2325 {
2326 gfc_error ("Function '%s' at %C already has a type of %s", name,
2327 gfc_basic_typename (sym->ts.type));
2328 goto cleanup;
2329 }
2330
2331 if (result == NULL)
2332 {
2333 sym->ts = current_ts;
2334 sym->result = sym;
2335 }
2336 else
2337 {
2338 result->ts = current_ts;
2339 sym->result = result;
2340 }
2341
2342 return MATCH_YES;
2343
2344cleanup:
cbb9e6aa 2345 gfc_current_locus = old_loc;
4ee9c684 2346 return m;
2347}
2348
2349
2350/* Match an ENTRY statement. */
2351
2352match
2353gfc_match_entry (void)
2354{
1b716045 2355 gfc_symbol *proc;
2356 gfc_symbol *result;
2357 gfc_symbol *entry;
4ee9c684 2358 char name[GFC_MAX_SYMBOL_LEN + 1];
2359 gfc_compile_state state;
2360 match m;
1b716045 2361 gfc_entry_list *el;
4ee9c684 2362
2363 m = gfc_match_name (name);
2364 if (m != MATCH_YES)
2365 return m;
2366
1b716045 2367 state = gfc_current_state ();
2368 if (state != COMP_SUBROUTINE
2369 && state != COMP_FUNCTION)
2370 {
2371 gfc_error ("ENTRY statement at %C cannot appear within %s",
2372 gfc_state_name (gfc_current_state ()));
2373 return MATCH_ERROR;
2374 }
2375
2376 if (gfc_current_ns->parent != NULL
2377 && gfc_current_ns->parent->proc_name
2378 && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2379 {
2380 gfc_error("ENTRY statement at %C cannot appear in a "
2381 "contained procedure");
2382 return MATCH_ERROR;
2383 }
2384
4ee9c684 2385 if (get_proc_name (name, &entry))
2386 return MATCH_ERROR;
2387
1b716045 2388 proc = gfc_current_block ();
2389
2390 if (state == COMP_SUBROUTINE)
4ee9c684 2391 {
950683ed 2392 /* An entry in a subroutine. */
4ee9c684 2393 m = gfc_match_formal_arglist (entry, 0, 1);
2394 if (m != MATCH_YES)
2395 return MATCH_ERROR;
2396
950683ed 2397 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2398 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 2399 return MATCH_ERROR;
1b716045 2400 }
2401 else
2402 {
2403 /* An entry in a function. */
69643e1d 2404 m = gfc_match_formal_arglist (entry, 0, 1);
4ee9c684 2405 if (m != MATCH_YES)
2406 return MATCH_ERROR;
2407
4ee9c684 2408 result = NULL;
2409
2410 if (gfc_match_eos () == MATCH_YES)
2411 {
950683ed 2412 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2413 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 2414 return MATCH_ERROR;
2415
c6871095 2416 entry->result = entry;
4ee9c684 2417 }
2418 else
2419 {
1b716045 2420 m = match_result (proc, &result);
4ee9c684 2421 if (m == MATCH_NO)
2422 gfc_syntax_error (ST_ENTRY);
2423 if (m != MATCH_YES)
2424 return MATCH_ERROR;
2425
950683ed 2426 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2427 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2428 || gfc_add_function (&entry->attr, result->name,
2429 NULL) == FAILURE)
4ee9c684 2430 return MATCH_ERROR;
c6871095 2431
2432 entry->result = result;
4ee9c684 2433 }
2434
1b716045 2435 if (proc->attr.recursive && result == NULL)
4ee9c684 2436 {
2437 gfc_error ("RESULT attribute required in ENTRY statement at %C");
2438 return MATCH_ERROR;
2439 }
4ee9c684 2440 }
2441
2442 if (gfc_match_eos () != MATCH_YES)
2443 {
2444 gfc_syntax_error (ST_ENTRY);
2445 return MATCH_ERROR;
2446 }
2447
1b716045 2448 entry->attr.recursive = proc->attr.recursive;
2449 entry->attr.elemental = proc->attr.elemental;
2450 entry->attr.pure = proc->attr.pure;
4ee9c684 2451
1b716045 2452 el = gfc_get_entry_list ();
2453 el->sym = entry;
2454 el->next = gfc_current_ns->entries;
2455 gfc_current_ns->entries = el;
2456 if (el->next)
2457 el->id = el->next->id + 1;
2458 else
2459 el->id = 1;
4ee9c684 2460
1b716045 2461 new_st.op = EXEC_ENTRY;
2462 new_st.ext.entry = el;
2463
2464 return MATCH_YES;
4ee9c684 2465}
2466
2467
2468/* Match a subroutine statement, including optional prefixes. */
2469
2470match
2471gfc_match_subroutine (void)
2472{
2473 char name[GFC_MAX_SYMBOL_LEN + 1];
2474 gfc_symbol *sym;
2475 match m;
2476
2477 if (gfc_current_state () != COMP_NONE
2478 && gfc_current_state () != COMP_INTERFACE
2479 && gfc_current_state () != COMP_CONTAINS)
2480 return MATCH_NO;
2481
2482 m = match_prefix (NULL);
2483 if (m != MATCH_YES)
2484 return m;
2485
2486 m = gfc_match ("subroutine% %n", name);
2487 if (m != MATCH_YES)
2488 return m;
2489
2490 if (get_proc_name (name, &sym))
2491 return MATCH_ERROR;
2492 gfc_new_block = sym;
2493
950683ed 2494 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4ee9c684 2495 return MATCH_ERROR;
2496
2497 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2498 return MATCH_ERROR;
2499
2500 if (gfc_match_eos () != MATCH_YES)
2501 {
2502 gfc_syntax_error (ST_SUBROUTINE);
2503 return MATCH_ERROR;
2504 }
2505
2506 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2507 return MATCH_ERROR;
2508
2509 return MATCH_YES;
2510}
2511
2512
231e961a 2513/* Return nonzero if we're currently compiling a contained procedure. */
c0985832 2514
2515static int
2516contained_procedure (void)
2517{
2518 gfc_state_data *s;
2519
2520 for (s=gfc_state_stack; s; s=s->previous)
2521 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2522 && s->previous != NULL
2523 && s->previous->state == COMP_CONTAINS)
2524 return 1;
2525
2526 return 0;
2527}
2528
4ee9c684 2529/* Match any of the various end-block statements. Returns the type of
2530 END to the caller. The END INTERFACE, END IF, END DO and END
2531 SELECT statements cannot be replaced by a single END statement. */
2532
2533match
2534gfc_match_end (gfc_statement * st)
2535{
2536 char name[GFC_MAX_SYMBOL_LEN + 1];
2537 gfc_compile_state state;
2538 locus old_loc;
2539 const char *block_name;
2540 const char *target;
c0985832 2541 int eos_ok;
4ee9c684 2542 match m;
2543
cbb9e6aa 2544 old_loc = gfc_current_locus;
4ee9c684 2545 if (gfc_match ("end") != MATCH_YES)
2546 return MATCH_NO;
2547
2548 state = gfc_current_state ();
2549 block_name =
2550 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2551
2552 if (state == COMP_CONTAINS)
2553 {
2554 state = gfc_state_stack->previous->state;
2555 block_name = gfc_state_stack->previous->sym == NULL ? NULL
2556 : gfc_state_stack->previous->sym->name;
2557 }
2558
2559 switch (state)
2560 {
2561 case COMP_NONE:
2562 case COMP_PROGRAM:
2563 *st = ST_END_PROGRAM;
2564 target = " program";
c0985832 2565 eos_ok = 1;
4ee9c684 2566 break;
2567
2568 case COMP_SUBROUTINE:
2569 *st = ST_END_SUBROUTINE;
2570 target = " subroutine";
c0985832 2571 eos_ok = !contained_procedure ();
4ee9c684 2572 break;
2573
2574 case COMP_FUNCTION:
2575 *st = ST_END_FUNCTION;
2576 target = " function";
c0985832 2577 eos_ok = !contained_procedure ();
4ee9c684 2578 break;
2579
2580 case COMP_BLOCK_DATA:
2581 *st = ST_END_BLOCK_DATA;
2582 target = " block data";
c0985832 2583 eos_ok = 1;
4ee9c684 2584 break;
2585
2586 case COMP_MODULE:
2587 *st = ST_END_MODULE;
2588 target = " module";
c0985832 2589 eos_ok = 1;
4ee9c684 2590 break;
2591
2592 case COMP_INTERFACE:
2593 *st = ST_END_INTERFACE;
2594 target = " interface";
c0985832 2595 eos_ok = 0;
4ee9c684 2596 break;
2597
2598 case COMP_DERIVED:
2599 *st = ST_END_TYPE;
2600 target = " type";
c0985832 2601 eos_ok = 0;
4ee9c684 2602 break;
2603
2604 case COMP_IF:
2605 *st = ST_ENDIF;
2606 target = " if";
c0985832 2607 eos_ok = 0;
4ee9c684 2608 break;
2609
2610 case COMP_DO:
2611 *st = ST_ENDDO;
2612 target = " do";
c0985832 2613 eos_ok = 0;
4ee9c684 2614 break;
2615
2616 case COMP_SELECT:
2617 *st = ST_END_SELECT;
2618 target = " select";
c0985832 2619 eos_ok = 0;
4ee9c684 2620 break;
2621
2622 case COMP_FORALL:
2623 *st = ST_END_FORALL;
2624 target = " forall";
c0985832 2625 eos_ok = 0;
4ee9c684 2626 break;
2627
2628 case COMP_WHERE:
2629 *st = ST_END_WHERE;
2630 target = " where";
c0985832 2631 eos_ok = 0;
4ee9c684 2632 break;
2633
2634 default:
2635 gfc_error ("Unexpected END statement at %C");
2636 goto cleanup;
2637 }
2638
2639 if (gfc_match_eos () == MATCH_YES)
2640 {
c0985832 2641 if (!eos_ok)
4ee9c684 2642 {
c0985832 2643 /* We would have required END [something] */
d197c9ee 2644 gfc_error ("%s statement expected at %L",
2645 gfc_ascii_statement (*st), &old_loc);
4ee9c684 2646 goto cleanup;
2647 }
2648
2649 return MATCH_YES;
2650 }
2651
2652 /* Verify that we've got the sort of end-block that we're expecting. */
2653 if (gfc_match (target) != MATCH_YES)
2654 {
2655 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
2656 goto cleanup;
2657 }
2658
2659 /* If we're at the end, make sure a block name wasn't required. */
2660 if (gfc_match_eos () == MATCH_YES)
2661 {
2662
2663 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
2664 return MATCH_YES;
2665
2666 if (gfc_current_block () == NULL)
2667 return MATCH_YES;
2668
2669 gfc_error ("Expected block name of '%s' in %s statement at %C",
2670 block_name, gfc_ascii_statement (*st));
2671
2672 return MATCH_ERROR;
2673 }
2674
2675 /* END INTERFACE has a special handler for its several possible endings. */
2676 if (*st == ST_END_INTERFACE)
2677 return gfc_match_end_interface ();
2678
2679 /* We haven't hit the end of statement, so what is left must be an end-name. */
2680 m = gfc_match_space ();
2681 if (m == MATCH_YES)
2682 m = gfc_match_name (name);
2683
2684 if (m == MATCH_NO)
2685 gfc_error ("Expected terminating name at %C");
2686 if (m != MATCH_YES)
2687 goto cleanup;
2688
2689 if (block_name == NULL)
2690 goto syntax;
2691
2692 if (strcmp (name, block_name) != 0)
2693 {
2694 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
2695 gfc_ascii_statement (*st));
2696 goto cleanup;
2697 }
2698
2699 if (gfc_match_eos () == MATCH_YES)
2700 return MATCH_YES;
2701
2702syntax:
2703 gfc_syntax_error (*st);
2704
2705cleanup:
cbb9e6aa 2706 gfc_current_locus = old_loc;
4ee9c684 2707 return MATCH_ERROR;
2708}
2709
2710
2711
2712/***************** Attribute declaration statements ****************/
2713
2714/* Set the attribute of a single variable. */
2715
2716static match
2717attr_decl1 (void)
2718{
2719 char name[GFC_MAX_SYMBOL_LEN + 1];
2720 gfc_array_spec *as;
2721 gfc_symbol *sym;
2722 locus var_locus;
2723 match m;
2724
2725 as = NULL;
2726
2727 m = gfc_match_name (name);
2728 if (m != MATCH_YES)
2729 goto cleanup;
2730
2731 if (find_special (name, &sym))
2732 return MATCH_ERROR;
2733
cbb9e6aa 2734 var_locus = gfc_current_locus;
4ee9c684 2735
2736 /* Deal with possible array specification for certain attributes. */
2737 if (current_attr.dimension
2738 || current_attr.allocatable
2739 || current_attr.pointer
2740 || current_attr.target)
2741 {
2742 m = gfc_match_array_spec (&as);
2743 if (m == MATCH_ERROR)
2744 goto cleanup;
2745
2746 if (current_attr.dimension && m == MATCH_NO)
2747 {
2748 gfc_error
2749 ("Missing array specification at %L in DIMENSION statement",
2750 &var_locus);
2751 m = MATCH_ERROR;
2752 goto cleanup;
2753 }
2754
2755 if ((current_attr.allocatable || current_attr.pointer)
2756 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
2757 {
2758 gfc_error ("Array specification must be deferred at %L",
2759 &var_locus);
2760 m = MATCH_ERROR;
2761 goto cleanup;
2762 }
2763 }
2764
2765 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
2766 if (current_attr.dimension == 0
2767 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
2768 {
2769 m = MATCH_ERROR;
2770 goto cleanup;
2771 }
2772
2773 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
2774 {
2775 m = MATCH_ERROR;
2776 goto cleanup;
2777 }
2778
2779 if ((current_attr.external || current_attr.intrinsic)
2780 && sym->attr.flavor != FL_PROCEDURE
950683ed 2781 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4ee9c684 2782 {
2783 m = MATCH_ERROR;
2784 goto cleanup;
2785 }
2786
2787 return MATCH_YES;
2788
2789cleanup:
2790 gfc_free_array_spec (as);
2791 return m;
2792}
2793
2794
2795/* Generic attribute declaration subroutine. Used for attributes that
2796 just have a list of names. */
2797
2798static match
2799attr_decl (void)
2800{
2801 match m;
2802
2803 /* Gobble the optional double colon, by simply ignoring the result
2804 of gfc_match(). */
2805 gfc_match (" ::");
2806
2807 for (;;)
2808 {
2809 m = attr_decl1 ();
2810 if (m != MATCH_YES)
2811 break;
2812
2813 if (gfc_match_eos () == MATCH_YES)
2814 {
2815 m = MATCH_YES;
2816 break;
2817 }
2818
2819 if (gfc_match_char (',') != MATCH_YES)
2820 {
2821 gfc_error ("Unexpected character in variable list at %C");
2822 m = MATCH_ERROR;
2823 break;
2824 }
2825 }
2826
2827 return m;
2828}
2829
2830
2831match
2832gfc_match_external (void)
2833{
2834
2835 gfc_clear_attr (&current_attr);
2836 gfc_add_external (&current_attr, NULL);
2837
2838 return attr_decl ();
2839}
2840
2841
2842
2843match
2844gfc_match_intent (void)
2845{
2846 sym_intent intent;
2847
2848 intent = match_intent_spec ();
2849 if (intent == INTENT_UNKNOWN)
2850 return MATCH_ERROR;
2851
2852 gfc_clear_attr (&current_attr);
2853 gfc_add_intent (&current_attr, intent, NULL); /* Can't fail */
2854
2855 return attr_decl ();
2856}
2857
2858
2859match
2860gfc_match_intrinsic (void)
2861{
2862
2863 gfc_clear_attr (&current_attr);
2864 gfc_add_intrinsic (&current_attr, NULL);
2865
2866 return attr_decl ();
2867}
2868
2869
2870match
2871gfc_match_optional (void)
2872{
2873
2874 gfc_clear_attr (&current_attr);
2875 gfc_add_optional (&current_attr, NULL);
2876
2877 return attr_decl ();
2878}
2879
2880
2881match
2882gfc_match_pointer (void)
2883{
2884
2885 gfc_clear_attr (&current_attr);
2886 gfc_add_pointer (&current_attr, NULL);
2887
2888 return attr_decl ();
2889}
2890
2891
2892match
2893gfc_match_allocatable (void)
2894{
2895
2896 gfc_clear_attr (&current_attr);
2897 gfc_add_allocatable (&current_attr, NULL);
2898
2899 return attr_decl ();
2900}
2901
2902
2903match
2904gfc_match_dimension (void)
2905{
2906
2907 gfc_clear_attr (&current_attr);
950683ed 2908 gfc_add_dimension (&current_attr, NULL, NULL);
4ee9c684 2909
2910 return attr_decl ();
2911}
2912
2913
2914match
2915gfc_match_target (void)
2916{
2917
2918 gfc_clear_attr (&current_attr);
2919 gfc_add_target (&current_attr, NULL);
2920
2921 return attr_decl ();
2922}
2923
2924
2925/* Match the list of entities being specified in a PUBLIC or PRIVATE
2926 statement. */
2927
2928static match
2929access_attr_decl (gfc_statement st)
2930{
2931 char name[GFC_MAX_SYMBOL_LEN + 1];
2932 interface_type type;
2933 gfc_user_op *uop;
2934 gfc_symbol *sym;
2935 gfc_intrinsic_op operator;
2936 match m;
2937
2938 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
2939 goto done;
2940
2941 for (;;)
2942 {
2943 m = gfc_match_generic_spec (&type, name, &operator);
2944 if (m == MATCH_NO)
2945 goto syntax;
2946 if (m == MATCH_ERROR)
2947 return MATCH_ERROR;
2948
2949 switch (type)
2950 {
2951 case INTERFACE_NAMELESS:
2952 goto syntax;
2953
2954 case INTERFACE_GENERIC:
2955 if (gfc_get_symbol (name, NULL, &sym))
2956 goto done;
2957
2958 if (gfc_add_access (&sym->attr,
2959 (st ==
2960 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
950683ed 2961 sym->name, NULL) == FAILURE)
4ee9c684 2962 return MATCH_ERROR;
2963
2964 break;
2965
2966 case INTERFACE_INTRINSIC_OP:
2967 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
2968 {
2969 gfc_current_ns->operator_access[operator] =
2970 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2971 }
2972 else
2973 {
2974 gfc_error ("Access specification of the %s operator at %C has "
2975 "already been specified", gfc_op2string (operator));
2976 goto done;
2977 }
2978
2979 break;
2980
2981 case INTERFACE_USER_OP:
2982 uop = gfc_get_uop (name);
2983
2984 if (uop->access == ACCESS_UNKNOWN)
2985 {
2986 uop->access =
2987 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2988 }
2989 else
2990 {
2991 gfc_error
2992 ("Access specification of the .%s. operator at %C has "
2993 "already been specified", sym->name);
2994 goto done;
2995 }
2996
2997 break;
2998 }
2999
3000 if (gfc_match_char (',') == MATCH_NO)
3001 break;
3002 }
3003
3004 if (gfc_match_eos () != MATCH_YES)
3005 goto syntax;
3006 return MATCH_YES;
3007
3008syntax:
3009 gfc_syntax_error (st);
3010
3011done:
3012 return MATCH_ERROR;
3013}
3014
3015
3016/* The PRIVATE statement is a bit weird in that it can be a attribute
3017 declaration, but also works as a standlone statement inside of a
3018 type declaration or a module. */
3019
3020match
3021gfc_match_private (gfc_statement * st)
3022{
3023
3024 if (gfc_match ("private") != MATCH_YES)
3025 return MATCH_NO;
3026
3027 if (gfc_current_state () == COMP_DERIVED)
3028 {
3029 if (gfc_match_eos () == MATCH_YES)
3030 {
3031 *st = ST_PRIVATE;
3032 return MATCH_YES;
3033 }
3034
3035 gfc_syntax_error (ST_PRIVATE);
3036 return MATCH_ERROR;
3037 }
3038
3039 if (gfc_match_eos () == MATCH_YES)
3040 {
3041 *st = ST_PRIVATE;
3042 return MATCH_YES;
3043 }
3044
3045 *st = ST_ATTR_DECL;
3046 return access_attr_decl (ST_PRIVATE);
3047}
3048
3049
3050match
3051gfc_match_public (gfc_statement * st)
3052{
3053
3054 if (gfc_match ("public") != MATCH_YES)
3055 return MATCH_NO;
3056
3057 if (gfc_match_eos () == MATCH_YES)
3058 {
3059 *st = ST_PUBLIC;
3060 return MATCH_YES;
3061 }
3062
3063 *st = ST_ATTR_DECL;
3064 return access_attr_decl (ST_PUBLIC);
3065}
3066
3067
3068/* Workhorse for gfc_match_parameter. */
3069
3070static match
3071do_parm (void)
3072{
3073 gfc_symbol *sym;
3074 gfc_expr *init;
3075 match m;
3076
3077 m = gfc_match_symbol (&sym, 0);
3078 if (m == MATCH_NO)
3079 gfc_error ("Expected variable name at %C in PARAMETER statement");
3080
3081 if (m != MATCH_YES)
3082 return m;
3083
3084 if (gfc_match_char ('=') == MATCH_NO)
3085 {
3086 gfc_error ("Expected = sign in PARAMETER statement at %C");
3087 return MATCH_ERROR;
3088 }
3089
3090 m = gfc_match_init_expr (&init);
3091 if (m == MATCH_NO)
3092 gfc_error ("Expected expression at %C in PARAMETER statement");
3093 if (m != MATCH_YES)
3094 return m;
3095
3096 if (sym->ts.type == BT_UNKNOWN
3097 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3098 {
3099 m = MATCH_ERROR;
3100 goto cleanup;
3101 }
3102
3103 if (gfc_check_assign_symbol (sym, init) == FAILURE
950683ed 3104 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4ee9c684 3105 {
3106 m = MATCH_ERROR;
3107 goto cleanup;
3108 }
3109
c1b6da4a 3110 if (sym->ts.type == BT_CHARACTER
3111 && sym->ts.cl != NULL
3112 && sym->ts.cl->length != NULL
3113 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3114 && init->expr_type == EXPR_CONSTANT
3115 && init->ts.type == BT_CHARACTER
3116 && init->ts.kind == 1)
3117 gfc_set_constant_character_len (
3118 mpz_get_si (sym->ts.cl->length->value.integer), init);
3119
4ee9c684 3120 sym->value = init;
3121 return MATCH_YES;
3122
3123cleanup:
3124 gfc_free_expr (init);
3125 return m;
3126}
3127
3128
3129/* Match a parameter statement, with the weird syntax that these have. */
3130
3131match
3132gfc_match_parameter (void)
3133{
3134 match m;
3135
3136 if (gfc_match_char ('(') == MATCH_NO)
3137 return MATCH_NO;
3138
3139 for (;;)
3140 {
3141 m = do_parm ();
3142 if (m != MATCH_YES)
3143 break;
3144
3145 if (gfc_match (" )%t") == MATCH_YES)
3146 break;
3147
3148 if (gfc_match_char (',') != MATCH_YES)
3149 {
3150 gfc_error ("Unexpected characters in PARAMETER statement at %C");
3151 m = MATCH_ERROR;
3152 break;
3153 }
3154 }
3155
3156 return m;
3157}
3158
3159
3160/* Save statements have a special syntax. */
3161
3162match
3163gfc_match_save (void)
3164{
82f5ee13 3165 char n[GFC_MAX_SYMBOL_LEN+1];
3166 gfc_common_head *c;
4ee9c684 3167 gfc_symbol *sym;
3168 match m;
3169
3170 if (gfc_match_eos () == MATCH_YES)
3171 {
3172 if (gfc_current_ns->seen_save)
3173 {
3174 gfc_error ("Blanket SAVE statement at %C follows previous "
3175 "SAVE statement");
3176
3177 return MATCH_ERROR;
3178 }
3179
3180 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3181 return MATCH_YES;
3182 }
3183
3184 if (gfc_current_ns->save_all)
3185 {
3186 gfc_error ("SAVE statement at %C follows blanket SAVE statement");
3187 return MATCH_ERROR;
3188 }
3189
3190 gfc_match (" ::");
3191
3192 for (;;)
3193 {
3194 m = gfc_match_symbol (&sym, 0);
3195 switch (m)
3196 {
3197 case MATCH_YES:
950683ed 3198 if (gfc_add_save (&sym->attr, sym->name,
3199 &gfc_current_locus) == FAILURE)
4ee9c684 3200 return MATCH_ERROR;
3201 goto next_item;
3202
3203 case MATCH_NO:
3204 break;
3205
3206 case MATCH_ERROR:
3207 return MATCH_ERROR;
3208 }
3209
82f5ee13 3210 m = gfc_match (" / %n /", &n);
4ee9c684 3211 if (m == MATCH_ERROR)
3212 return MATCH_ERROR;
3213 if (m == MATCH_NO)
3214 goto syntax;
3215
403ddc45 3216 c = gfc_get_common (n, 0);
82f5ee13 3217 c->saved = 1;
3218
4ee9c684 3219 gfc_current_ns->seen_save = 1;
3220
3221 next_item:
3222 if (gfc_match_eos () == MATCH_YES)
3223 break;
3224 if (gfc_match_char (',') != MATCH_YES)
3225 goto syntax;
3226 }
3227
3228 return MATCH_YES;
3229
3230syntax:
3231 gfc_error ("Syntax error in SAVE statement at %C");
3232 return MATCH_ERROR;
3233}
3234
3235
3236/* Match a module procedure statement. Note that we have to modify
3237 symbols in the parent's namespace because the current one was there
89d91d02 3238 to receive symbols that are in an interface's formal argument list. */
4ee9c684 3239
3240match
3241gfc_match_modproc (void)
3242{
3243 char name[GFC_MAX_SYMBOL_LEN + 1];
3244 gfc_symbol *sym;
3245 match m;
3246
3247 if (gfc_state_stack->state != COMP_INTERFACE
3248 || gfc_state_stack->previous == NULL
3249 || current_interface.type == INTERFACE_NAMELESS)
3250 {
3251 gfc_error
3252 ("MODULE PROCEDURE at %C must be in a generic module interface");
3253 return MATCH_ERROR;
3254 }
3255
3256 for (;;)
3257 {
3258 m = gfc_match_name (name);
3259 if (m == MATCH_NO)
3260 goto syntax;
3261 if (m != MATCH_YES)
3262 return MATCH_ERROR;
3263
3264 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3265 return MATCH_ERROR;
3266
3267 if (sym->attr.proc != PROC_MODULE
950683ed 3268 && gfc_add_procedure (&sym->attr, PROC_MODULE,
3269 sym->name, NULL) == FAILURE)
4ee9c684 3270 return MATCH_ERROR;
3271
3272 if (gfc_add_interface (sym) == FAILURE)
3273 return MATCH_ERROR;
3274
3275 if (gfc_match_eos () == MATCH_YES)
3276 break;
3277 if (gfc_match_char (',') != MATCH_YES)
3278 goto syntax;
3279 }
3280
3281 return MATCH_YES;
3282
3283syntax:
3284 gfc_syntax_error (ST_MODULE_PROC);
3285 return MATCH_ERROR;
3286}
3287
3288
3289/* Match the beginning of a derived type declaration. If a type name
3290 was the result of a function, then it is possible to have a symbol
3291 already to be known as a derived type yet have no components. */
3292
3293match
3294gfc_match_derived_decl (void)
3295{
3296 char name[GFC_MAX_SYMBOL_LEN + 1];
3297 symbol_attribute attr;
3298 gfc_symbol *sym;
3299 match m;
3300
3301 if (gfc_current_state () == COMP_DERIVED)
3302 return MATCH_NO;
3303
3304 gfc_clear_attr (&attr);
3305
3306loop:
3307 if (gfc_match (" , private") == MATCH_YES)
3308 {
3309 if (gfc_find_state (COMP_MODULE) == FAILURE)
3310 {
3311 gfc_error
3312 ("Derived type at %C can only be PRIVATE within a MODULE");
3313 return MATCH_ERROR;
3314 }
3315
950683ed 3316 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4ee9c684 3317 return MATCH_ERROR;
3318 goto loop;
3319 }
3320
3321 if (gfc_match (" , public") == MATCH_YES)
3322 {
3323 if (gfc_find_state (COMP_MODULE) == FAILURE)
3324 {
3325 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3326 return MATCH_ERROR;
3327 }
3328
950683ed 3329 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4ee9c684 3330 return MATCH_ERROR;
3331 goto loop;
3332 }
3333
3334 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3335 {
3336 gfc_error ("Expected :: in TYPE definition at %C");
3337 return MATCH_ERROR;
3338 }
3339
3340 m = gfc_match (" %n%t", name);
3341 if (m != MATCH_YES)
3342 return m;
3343
3344 /* Make sure the name isn't the name of an intrinsic type. The
3345 'double precision' type doesn't get past the name matcher. */
3346 if (strcmp (name, "integer") == 0
3347 || strcmp (name, "real") == 0
3348 || strcmp (name, "character") == 0
3349 || strcmp (name, "logical") == 0
3350 || strcmp (name, "complex") == 0)
3351 {
3352 gfc_error
3353 ("Type name '%s' at %C cannot be the same as an intrinsic type",
3354 name);
3355 return MATCH_ERROR;
3356 }
3357
3358 if (gfc_get_symbol (name, NULL, &sym))
3359 return MATCH_ERROR;
3360
3361 if (sym->ts.type != BT_UNKNOWN)
3362 {
3363 gfc_error ("Derived type name '%s' at %C already has a basic type "
3364 "of %s", sym->name, gfc_typename (&sym->ts));
3365 return MATCH_ERROR;
3366 }
3367
3368 /* The symbol may already have the derived attribute without the
3369 components. The ways this can happen is via a function
3370 definition, an INTRINSIC statement or a subtype in another
3371 derived type that is a pointer. The first part of the AND clause
b14e2757 3372 is true if a the symbol is not the return value of a function. */
4ee9c684 3373 if (sym->attr.flavor != FL_DERIVED
950683ed 3374 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4ee9c684 3375 return MATCH_ERROR;
3376
3377 if (sym->components != NULL)
3378 {
3379 gfc_error
3380 ("Derived type definition of '%s' at %C has already been defined",
3381 sym->name);
3382 return MATCH_ERROR;
3383 }
3384
3385 if (attr.access != ACCESS_UNKNOWN
950683ed 3386 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4ee9c684 3387 return MATCH_ERROR;
3388
3389 gfc_new_block = sym;
3390
3391 return MATCH_YES;
3392}