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