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