]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
d: Fix internal compiler error: in add_expr, at tree.c:7794
[thirdparty/gcc.git] / gcc / fortran / decl.c
CommitLineData
6de9cd9a 1/* Declaration statement matcher
a5544970 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Andy Vaught
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
9fc4d79b 10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a 20
6de9cd9a 21#include "config.h"
d22e4895 22#include "system.h"
953bee7c 23#include "coretypes.h"
2adfab87
AM
24#include "options.h"
25#include "tree.h"
6de9cd9a 26#include "gfortran.h"
2adfab87 27#include "stringpool.h"
6de9cd9a
DN
28#include "match.h"
29#include "parse.h"
b7e75771 30#include "constructor.h"
e8cecccc 31#include "target.h"
ca39e6f2
FXC
32
33/* Macros to access allocate memory for gfc_data_variable,
34 gfc_data_value and gfc_data. */
ece3f663
KG
35#define gfc_get_data_variable() XCNEW (gfc_data_variable)
36#define gfc_get_data_value() XCNEW (gfc_data_value)
37#define gfc_get_data() XCNEW (gfc_data)
ca39e6f2
FXC
38
39
524af0d6 40static bool set_binding_label (const char **, const char *, int);
62603fae
JB
41
42
2054fc29 43/* This flag is set if an old-style length selector is matched
6de9cd9a
DN
44 during a type-declaration statement. */
45
46static int old_char_selector;
47
46fa431d 48/* When variables acquire types and attributes from a declaration
6de9cd9a
DN
49 statement, they get them from the following static variables. The
50 first part of a declaration sets these variables and the second
51 part copies these into symbol structures. */
52
53static gfc_typespec current_ts;
54
55static symbol_attribute current_attr;
56static gfc_array_spec *current_as;
57static int colon_seen;
6f855a26 58static int attr_seen;
6de9cd9a 59
a8b3b0b6 60/* The current binding label (if any). */
9975a30b 61static const char* curr_binding_label;
a8b3b0b6
CR
62/* Need to know how many identifiers are on the current data declaration
63 line in case we're given the BIND(C) attribute with a NAME= specifier. */
64static int num_idents_on_line;
65/* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
66 can supply a name if the curr_binding_label is nil and NAME= was not. */
67static int has_name_equals = 0;
68
25d8f0a2
TS
69/* Initializer of the previous enumerator. */
70
71static gfc_expr *last_initializer;
72
73/* History of all the enumerators is maintained, so that
74 kind values of all the enumerators could be updated depending
75 upon the maximum initialized value. */
76
77typedef struct enumerator_history
78{
79 gfc_symbol *sym;
80 gfc_expr *initializer;
81 struct enumerator_history *next;
82}
83enumerator_history;
84
85/* Header of enum history chain. */
86
87static enumerator_history *enum_history = NULL;
88
89/* Pointer of enum history node containing largest initializer. */
90
91static enumerator_history *max_enum = NULL;
92
6de9cd9a
DN
93/* gfc_new_block points to the symbol of a newly matched block. */
94
95gfc_symbol *gfc_new_block;
96
1c8bcdf7 97bool gfc_matching_function;
e2d29968 98
170a8bd6
EB
99/* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
100int directive_unroll = -1;
101
facf0354
ML
102/* Map of middle-end built-ins that should be vectorized. */
103hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
104
5bab4c96
PT
105/* If a kind expression of a component of a parameterized derived type is
106 parameterized, temporarily store the expression here. */
107static gfc_expr *saved_kind_expr = NULL;
108
109/* Used to store the parameter list arising in a PDT declaration and
110 in the typespec of a PDT variable or component. */
111static gfc_actual_arglist *decl_type_param_list;
112static gfc_actual_arglist *type_param_spec_list;
113
294fbfc8
TS
114/********************* DATA statement subroutines *********************/
115
2220652d
PT
116static bool in_match_data = false;
117
118bool
119gfc_in_match_data (void)
120{
121 return in_match_data;
122}
123
ca39e6f2
FXC
124static void
125set_in_match_data (bool set_value)
2220652d
PT
126{
127 in_match_data = set_value;
128}
129
294fbfc8
TS
130/* Free a gfc_data_variable structure and everything beneath it. */
131
132static void
636dff67 133free_variable (gfc_data_variable *p)
294fbfc8
TS
134{
135 gfc_data_variable *q;
136
137 for (; p; p = q)
138 {
139 q = p->next;
140 gfc_free_expr (p->expr);
141 gfc_free_iterator (&p->iter, 0);
142 free_variable (p->list);
cede9502 143 free (p);
294fbfc8
TS
144 }
145}
146
147
148/* Free a gfc_data_value structure and everything beneath it. */
149
150static void
636dff67 151free_value (gfc_data_value *p)
294fbfc8
TS
152{
153 gfc_data_value *q;
154
155 for (; p; p = q)
156 {
157 q = p->next;
c9d75a48 158 mpz_clear (p->repeat);
294fbfc8 159 gfc_free_expr (p->expr);
cede9502 160 free (p);
294fbfc8
TS
161 }
162}
163
164
165/* Free a list of gfc_data structures. */
166
167void
636dff67 168gfc_free_data (gfc_data *p)
294fbfc8
TS
169{
170 gfc_data *q;
171
172 for (; p; p = q)
173 {
174 q = p->next;
294fbfc8
TS
175 free_variable (p->var);
176 free_value (p->value);
cede9502 177 free (p);
294fbfc8
TS
178 }
179}
180
181
a9f6f1f2 182/* Free all data in a namespace. */
636dff67 183
a9f6f1f2 184static void
66e4ab31 185gfc_free_data_all (gfc_namespace *ns)
a9f6f1f2
JD
186{
187 gfc_data *d;
188
189 for (;ns->data;)
190 {
191 d = ns->data->next;
cede9502 192 free (ns->data);
a9f6f1f2
JD
193 ns->data = d;
194 }
195}
196
d5e2274d
SB
197/* Reject data parsed since the last restore point was marked. */
198
199void
200gfc_reject_data (gfc_namespace *ns)
201{
202 gfc_data *d;
203
204 while (ns->data && ns->data != ns->old_data)
205 {
206 d = ns->data->next;
207 free (ns->data);
208 ns->data = d;
209 }
210}
a9f6f1f2 211
294fbfc8
TS
212static match var_element (gfc_data_variable *);
213
214/* Match a list of variables terminated by an iterator and a right
215 parenthesis. */
216
217static match
636dff67 218var_list (gfc_data_variable *parent)
294fbfc8
TS
219{
220 gfc_data_variable *tail, var;
221 match m;
222
223 m = var_element (&var);
224 if (m == MATCH_ERROR)
225 return MATCH_ERROR;
226 if (m == MATCH_NO)
227 goto syntax;
228
229 tail = gfc_get_data_variable ();
230 *tail = var;
231
232 parent->list = tail;
233
234 for (;;)
235 {
236 if (gfc_match_char (',') != MATCH_YES)
237 goto syntax;
238
239 m = gfc_match_iterator (&parent->iter, 1);
240 if (m == MATCH_YES)
241 break;
242 if (m == MATCH_ERROR)
243 return MATCH_ERROR;
244
245 m = var_element (&var);
246 if (m == MATCH_ERROR)
247 return MATCH_ERROR;
248 if (m == MATCH_NO)
249 goto syntax;
250
251 tail->next = gfc_get_data_variable ();
252 tail = tail->next;
253
254 *tail = var;
255 }
256
257 if (gfc_match_char (')') != MATCH_YES)
258 goto syntax;
259 return MATCH_YES;
260
261syntax:
262 gfc_syntax_error (ST_DATA);
263 return MATCH_ERROR;
264}
265
266
267/* Match a single element in a data variable list, which can be a
268 variable-iterator list. */
269
270static match
7b901ac4 271var_element (gfc_data_variable *new_var)
294fbfc8
TS
272{
273 match m;
274 gfc_symbol *sym;
275
7b901ac4 276 memset (new_var, 0, sizeof (gfc_data_variable));
294fbfc8
TS
277
278 if (gfc_match_char ('(') == MATCH_YES)
7b901ac4 279 return var_list (new_var);
294fbfc8 280
7b901ac4 281 m = gfc_match_variable (&new_var->expr, 0);
294fbfc8
TS
282 if (m != MATCH_YES)
283 return m;
284
094a0ecc
SK
285 if (new_var->expr->expr_type == EXPR_CONSTANT
286 && new_var->expr->symtree == NULL)
287 {
288 gfc_error ("Inquiry parameter cannot appear in a "
289 "data-stmt-object-list at %C");
290 return MATCH_ERROR;
291 }
292
7b901ac4 293 sym = new_var->expr->symtree->n.sym;
294fbfc8 294
f37e928c 295 /* Symbol should already have an associated type. */
524af0d6 296 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
f37e928c
DK
297 return MATCH_ERROR;
298
636dff67
SK
299 if (!sym->attr.function && gfc_current_ns->parent
300 && gfc_current_ns->parent == sym->ns)
294fbfc8 301 {
c4100eae 302 gfc_error ("Host associated variable %qs may not be in the DATA "
e25a0da3 303 "statement at %C", sym->name);
294fbfc8
TS
304 return MATCH_ERROR;
305 }
306
4075a94e 307 if (gfc_current_state () != COMP_BLOCK_DATA
636dff67 308 && sym->attr.in_common
524af0d6 309 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
a4d9b221 310 "common block variable %qs in DATA statement at %C",
524af0d6 311 sym->name))
4075a94e 312 return MATCH_ERROR;
294fbfc8 313
524af0d6 314 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
294fbfc8
TS
315 return MATCH_ERROR;
316
317 return MATCH_YES;
318}
319
320
321/* Match the top-level list of data variables. */
322
323static match
636dff67 324top_var_list (gfc_data *d)
294fbfc8 325{
7b901ac4 326 gfc_data_variable var, *tail, *new_var;
294fbfc8
TS
327 match m;
328
329 tail = NULL;
330
331 for (;;)
332 {
333 m = var_element (&var);
334 if (m == MATCH_NO)
335 goto syntax;
336 if (m == MATCH_ERROR)
337 return MATCH_ERROR;
338
7b901ac4
KG
339 new_var = gfc_get_data_variable ();
340 *new_var = var;
bebf94af
SK
341 if (new_var->expr)
342 new_var->expr->where = gfc_current_locus;
294fbfc8
TS
343
344 if (tail == NULL)
7b901ac4 345 d->var = new_var;
294fbfc8 346 else
7b901ac4 347 tail->next = new_var;
294fbfc8 348
7b901ac4 349 tail = new_var;
294fbfc8
TS
350
351 if (gfc_match_char ('/') == MATCH_YES)
352 break;
353 if (gfc_match_char (',') != MATCH_YES)
354 goto syntax;
355 }
356
357 return MATCH_YES;
358
359syntax:
360 gfc_syntax_error (ST_DATA);
a9f6f1f2 361 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
362 return MATCH_ERROR;
363}
364
365
366static match
636dff67 367match_data_constant (gfc_expr **result)
294fbfc8
TS
368{
369 char name[GFC_MAX_SYMBOL_LEN + 1];
c3f34952 370 gfc_symbol *sym, *dt_sym = NULL;
294fbfc8
TS
371 gfc_expr *expr;
372 match m;
36d3fb4c 373 locus old_loc;
294fbfc8
TS
374
375 m = gfc_match_literal_constant (&expr, 1);
376 if (m == MATCH_YES)
377 {
378 *result = expr;
379 return MATCH_YES;
380 }
381
382 if (m == MATCH_ERROR)
383 return MATCH_ERROR;
384
385 m = gfc_match_null (result);
386 if (m != MATCH_NO)
387 return m;
388
36d3fb4c
PT
389 old_loc = gfc_current_locus;
390
391 /* Should this be a structure component, try to match it
392 before matching a name. */
393 m = gfc_match_rvalue (result);
394 if (m == MATCH_ERROR)
395 return m;
396
397 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
398 {
524af0d6 399 if (!gfc_simplify_expr (*result, 0))
36d3fb4c
PT
400 m = MATCH_ERROR;
401 return m;
402 }
46f4f794 403 else if (m == MATCH_YES)
b6e841a6 404 {
19adb97a
SK
405 /* If a parameter inquiry ends up here, symtree is NULL but **result
406 contains the right constant expression. Check here. */
407 if ((*result)->symtree == NULL
408 && (*result)->expr_type == EXPR_CONSTANT
0a524296 409 && ((*result)->ts.type == BT_INTEGER
19adb97a
SK
410 || (*result)->ts.type == BT_REAL))
411 return m;
412
b6e841a6
SK
413 /* F2018:R845 data-stmt-constant is initial-data-target.
414 A data-stmt-constant shall be ... initial-data-target if and
415 only if the corresponding data-stmt-object has the POINTER
416 attribute. ... If data-stmt-constant is initial-data-target
417 the corresponding data statement object shall be
418 data-pointer-initialization compatible (7.5.4.6) with the initial
419 data target; the data statement object is initially associated
420 with the target. */
421 if ((*result)->symtree->n.sym->attr.save
422 && (*result)->symtree->n.sym->attr.target)
423 return m;
424 gfc_free_expr (*result);
425 }
36d3fb4c
PT
426
427 gfc_current_locus = old_loc;
428
294fbfc8
TS
429 m = gfc_match_name (name);
430 if (m != MATCH_YES)
431 return m;
432
433 if (gfc_find_symbol (name, NULL, 1, &sym))
434 return MATCH_ERROR;
435
c3f34952
TB
436 if (sym && sym->attr.generic)
437 dt_sym = gfc_find_dt_in_generic (sym);
438
294fbfc8 439 if (sym == NULL
c3f34952 440 || (sym->attr.flavor != FL_PARAMETER
f6288c24 441 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
294fbfc8 442 {
c4100eae 443 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
294fbfc8 444 name);
89f1f37e 445 *result = NULL;
294fbfc8
TS
446 return MATCH_ERROR;
447 }
f6288c24 448 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
c3f34952 449 return gfc_match_structure_constructor (dt_sym, result);
294fbfc8 450
d46e0870
JD
451 /* Check to see if the value is an initialization array expression. */
452 if (sym->value->expr_type == EXPR_ARRAY)
453 {
454 gfc_current_locus = old_loc;
455
456 m = gfc_match_init_expr (result);
457 if (m == MATCH_ERROR)
458 return m;
459
460 if (m == MATCH_YES)
461 {
524af0d6 462 if (!gfc_simplify_expr (*result, 0))
d46e0870
JD
463 m = MATCH_ERROR;
464
465 if ((*result)->expr_type == EXPR_CONSTANT)
466 return m;
467 else
468 {
469 gfc_error ("Invalid initializer %s in Data statement at %C", name);
470 return MATCH_ERROR;
471 }
472 }
473 }
474
294fbfc8
TS
475 *result = gfc_copy_expr (sym->value);
476 return MATCH_YES;
477}
478
479
480/* Match a list of values in a DATA statement. The leading '/' has
481 already been seen at this point. */
482
483static match
636dff67 484top_val_list (gfc_data *data)
294fbfc8 485{
7b901ac4 486 gfc_data_value *new_val, *tail;
294fbfc8 487 gfc_expr *expr;
294fbfc8
TS
488 match m;
489
490 tail = NULL;
491
492 for (;;)
493 {
494 m = match_data_constant (&expr);
495 if (m == MATCH_NO)
496 goto syntax;
497 if (m == MATCH_ERROR)
498 return MATCH_ERROR;
499
7b901ac4
KG
500 new_val = gfc_get_data_value ();
501 mpz_init (new_val->repeat);
294fbfc8
TS
502
503 if (tail == NULL)
7b901ac4 504 data->value = new_val;
294fbfc8 505 else
7b901ac4 506 tail->next = new_val;
294fbfc8 507
7b901ac4 508 tail = new_val;
294fbfc8
TS
509
510 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
511 {
512 tail->expr = expr;
f2112868 513 mpz_set_ui (tail->repeat, 1);
294fbfc8
TS
514 }
515 else
516 {
46f4f794 517 mpz_set (tail->repeat, expr->value.integer);
294fbfc8 518 gfc_free_expr (expr);
294fbfc8
TS
519
520 m = match_data_constant (&tail->expr);
521 if (m == MATCH_NO)
522 goto syntax;
523 if (m == MATCH_ERROR)
524 return MATCH_ERROR;
525 }
526
527 if (gfc_match_char ('/') == MATCH_YES)
528 break;
529 if (gfc_match_char (',') == MATCH_NO)
530 goto syntax;
531 }
532
533 return MATCH_YES;
534
535syntax:
536 gfc_syntax_error (ST_DATA);
a9f6f1f2 537 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
538 return MATCH_ERROR;
539}
540
541
542/* Matches an old style initialization. */
543
544static match
545match_old_style_init (const char *name)
546{
547 match m;
548 gfc_symtree *st;
ed0e3607 549 gfc_symbol *sym;
8dc63166 550 gfc_data *newdata, *nd;
294fbfc8
TS
551
552 /* Set up data structure to hold initializers. */
553 gfc_find_sym_tree (name, NULL, 0, &st);
ed0e3607
AL
554 sym = st->n.sym;
555
294fbfc8
TS
556 newdata = gfc_get_data ();
557 newdata->var = gfc_get_data_variable ();
558 newdata->var->expr = gfc_get_variable_expr (st);
e11449d1 559 newdata->var->expr->where = sym->declared_at;
8c5c0b80 560 newdata->where = gfc_current_locus;
294fbfc8 561
66e4ab31 562 /* Match initial value list. This also eats the terminal '/'. */
294fbfc8
TS
563 m = top_val_list (newdata);
564 if (m != MATCH_YES)
565 {
cede9502 566 free (newdata);
294fbfc8
TS
567 return m;
568 }
569
8dc63166
SK
570 /* Check that a BOZ did not creep into an old-style initialization. */
571 for (nd = newdata; nd; nd = nd->next)
572 {
573 if (nd->value->expr->ts.type == BT_BOZ
574 && gfc_invalid_boz ("BOZ at %L cannot appear in an old-style "
575 "initialization", &nd->value->expr->where))
576 return MATCH_ERROR;
577
578 if (nd->var->expr->ts.type != BT_INTEGER
579 && nd->var->expr->ts.type != BT_REAL
580 && nd->value->expr->ts.type == BT_BOZ)
581 {
582 gfc_error ("Mismatch in variable type and BOZ literal constant "
583 "at %L in an old-style initialization",
584 &nd->value->expr->where);
585 return MATCH_ERROR;
586 }
587 }
588
294fbfc8
TS
589 if (gfc_pure (NULL))
590 {
591 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
cede9502 592 free (newdata);
294fbfc8
TS
593 return MATCH_ERROR;
594 }
ccd7751b 595 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
f1f39033 596
ed0e3607 597 /* Mark the variable as having appeared in a data statement. */
524af0d6 598 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
ed0e3607 599 {
cede9502 600 free (newdata);
ed0e3607
AL
601 return MATCH_ERROR;
602 }
603
294fbfc8
TS
604 /* Chain in namespace list of DATA initializers. */
605 newdata->next = gfc_current_ns->data;
606 gfc_current_ns->data = newdata;
607
608 return m;
609}
610
636dff67 611
294fbfc8 612/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
13795658 613 we are matching a DATA statement and are therefore issuing an error
d51347f9 614 if we encounter something unexpected, if not, we're trying to match
69de3b83 615 an old-style initialization expression of the form INTEGER I /2/. */
294fbfc8
TS
616
617match
618gfc_match_data (void)
619{
7b901ac4 620 gfc_data *new_data;
02543f02 621 gfc_expr *e;
bebf94af 622 gfc_ref *ref;
294fbfc8 623 match m;
623c32bc
SK
624 char c;
625
626 /* DATA has been matched. In free form source code, the next character
719f5a10
SK
627 needs to be whitespace or '(' from an implied do-loop. Check that
628 here. */
623c32bc 629 c = gfc_peek_ascii_char ();
719f5a10 630 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
623c32bc 631 return MATCH_NO;
294fbfc8 632
5f0ba745
SK
633 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
634 if ((gfc_current_state () == COMP_FUNCTION
635 || gfc_current_state () == COMP_SUBROUTINE)
636 && gfc_state_stack->previous->state == COMP_INTERFACE)
637 {
638 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
639 return MATCH_ERROR;
640 }
641
ca39e6f2 642 set_in_match_data (true);
2220652d 643
294fbfc8
TS
644 for (;;)
645 {
7b901ac4
KG
646 new_data = gfc_get_data ();
647 new_data->where = gfc_current_locus;
294fbfc8 648
7b901ac4 649 m = top_var_list (new_data);
294fbfc8
TS
650 if (m != MATCH_YES)
651 goto cleanup;
652
c034c38f
SK
653 if (new_data->var->iter.var
654 && new_data->var->iter.var->ts.type == BT_INTEGER
655 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
656 && new_data->var->list
657 && new_data->var->list->expr
658 && new_data->var->list->expr->ts.type == BT_CHARACTER
659 && new_data->var->list->expr->ref
660 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
661 {
662 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
663 "statement", &new_data->var->list->expr->where);
664 goto cleanup;
665 }
666
02543f02
SK
667 /* Check for an entity with an allocatable component, which is not
668 allowed. */
669 e = new_data->var->expr;
670 if (e)
671 {
672 bool invalid;
673
674 invalid = false;
bebf94af 675 for (ref = e->ref; ref; ref = ref->next)
02543f02
SK
676 if ((ref->type == REF_COMPONENT
677 && ref->u.c.component->attr.allocatable)
678 || (ref->type == REF_ARRAY
679 && e->symtree->n.sym->attr.pointer != 1
680 && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
681 invalid = true;
682
683 if (invalid)
684 {
685 gfc_error ("Allocatable component or deferred-shaped array "
686 "near %C in DATA statement");
687 goto cleanup;
688 }
bebf94af
SK
689
690 /* F2008:C567 (R536) A data-i-do-object or a variable that appears
691 as a data-stmt-object shall not be an object designator in which
692 a pointer appears other than as the entire rightmost part-ref. */
693 ref = e->ref;
694 if (e->symtree->n.sym->ts.type == BT_DERIVED
695 && e->symtree->n.sym->attr.pointer
696 && ref->type == REF_COMPONENT)
697 goto partref;
698
699 for (; ref; ref = ref->next)
700 if (ref->type == REF_COMPONENT
701 && ref->u.c.component->attr.pointer
702 && ref->next)
703 goto partref;
02543f02
SK
704 }
705
7b901ac4 706 m = top_val_list (new_data);
294fbfc8
TS
707 if (m != MATCH_YES)
708 goto cleanup;
709
7b901ac4
KG
710 new_data->next = gfc_current_ns->data;
711 gfc_current_ns->data = new_data;
294fbfc8
TS
712
713 if (gfc_match_eos () == MATCH_YES)
714 break;
715
716 gfc_match_char (','); /* Optional comma */
717 }
718
ca39e6f2 719 set_in_match_data (false);
2220652d 720
294fbfc8
TS
721 if (gfc_pure (NULL))
722 {
723 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
724 return MATCH_ERROR;
725 }
ccd7751b 726 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
f1f39033 727
294fbfc8
TS
728 return MATCH_YES;
729
bebf94af
SK
730partref:
731
732 gfc_error ("part-ref with pointer attribute near %L is not "
733 "rightmost part-ref of data-stmt-object",
734 &e->where);
735
294fbfc8 736cleanup:
ca39e6f2 737 set_in_match_data (false);
7b901ac4 738 gfc_free_data (new_data);
294fbfc8
TS
739 return MATCH_ERROR;
740}
741
742
743/************************ Declaration statements *********************/
744
d3a9eea2 745
f6288c24
FR
746/* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
747 list). The difference here is the expression is a list of constants
6442a6f4 748 and is surrounded by '/'.
f6288c24
FR
749 The typespec ts must match the typespec of the variable which the
750 clist is initializing.
6442a6f4 751 The arrayspec tells whether this should match a list of constants
f6288c24
FR
752 corresponding to array elements or a scalar (as == NULL). */
753
754static match
755match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
756{
757 gfc_constructor_base array_head = NULL;
758 gfc_expr *expr = NULL;
e11449d1 759 match m = MATCH_ERROR;
f6288c24 760 locus where;
9b24c104 761 mpz_t repeat, cons_size, as_size;
f6288c24
FR
762 bool scalar;
763 int cmp;
764
765 gcc_assert (ts);
766
f6288c24
FR
767 /* We have already matched '/' - now look for a constant list, as with
768 top_val_list from decl.c, but append the result to an array. */
769 if (gfc_match ("/") == MATCH_YES)
770 {
771 gfc_error ("Empty old style initializer list at %C");
e11449d1 772 return MATCH_ERROR;
f6288c24
FR
773 }
774
775 where = gfc_current_locus;
e11449d1
FR
776 scalar = !as || !as->rank;
777
778 if (!scalar && !spec_size (as, &as_size))
779 {
780 gfc_error ("Array in initializer list at %L must have an explicit shape",
781 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
782 /* Nothing to cleanup yet. */
783 return MATCH_ERROR;
784 }
785
786 mpz_init_set_ui (repeat, 0);
787
f6288c24
FR
788 for (;;)
789 {
790 m = match_data_constant (&expr);
791 if (m != MATCH_YES)
792 expr = NULL; /* match_data_constant may set expr to garbage */
793 if (m == MATCH_NO)
794 goto syntax;
795 if (m == MATCH_ERROR)
796 goto cleanup;
797
798 /* Found r in repeat spec r*c; look for the constant to repeat. */
799 if ( gfc_match_char ('*') == MATCH_YES)
800 {
801 if (scalar)
802 {
803 gfc_error ("Repeat spec invalid in scalar initializer at %C");
804 goto cleanup;
805 }
806 if (expr->ts.type != BT_INTEGER)
807 {
808 gfc_error ("Repeat spec must be an integer at %C");
809 goto cleanup;
810 }
811 mpz_set (repeat, expr->value.integer);
812 gfc_free_expr (expr);
813 expr = NULL;
814
815 m = match_data_constant (&expr);
816 if (m == MATCH_NO)
e11449d1
FR
817 {
818 m = MATCH_ERROR;
819 gfc_error ("Expected data constant after repeat spec at %C");
820 }
f6288c24
FR
821 if (m != MATCH_YES)
822 goto cleanup;
823 }
824 /* No repeat spec, we matched the data constant itself. */
825 else
826 mpz_set_ui (repeat, 1);
827
828 if (!scalar)
829 {
830 /* Add the constant initializer as many times as repeated. */
831 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
832 {
833 /* Make sure types of elements match */
834 if(ts && !gfc_compare_types (&expr->ts, ts)
835 && !gfc_convert_type (expr, ts, 1))
836 goto cleanup;
837
838 gfc_constructor_append_expr (&array_head,
839 gfc_copy_expr (expr), &gfc_current_locus);
840 }
841
842 gfc_free_expr (expr);
843 expr = NULL;
844 }
845
846 /* For scalar initializers quit after one element. */
847 else
848 {
849 if(gfc_match_char ('/') != MATCH_YES)
850 {
851 gfc_error ("End of scalar initializer expected at %C");
852 goto cleanup;
853 }
854 break;
855 }
856
857 if (gfc_match_char ('/') == MATCH_YES)
858 break;
859 if (gfc_match_char (',') == MATCH_NO)
860 goto syntax;
861 }
862
e11449d1
FR
863 /* If we break early from here out, we encountered an error. */
864 m = MATCH_ERROR;
865
f6288c24
FR
866 /* Set up expr as an array constructor. */
867 if (!scalar)
868 {
869 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
870 expr->ts = *ts;
871 expr->value.constructor = array_head;
872
873 expr->rank = as->rank;
874 expr->shape = gfc_get_shape (expr->rank);
875
9b24c104
FR
876 /* Validate sizes. We built expr ourselves, so cons_size will be
877 constant (we fail above for non-constant expressions).
e11449d1 878 We still need to verify that the sizes match. */
9b24c104 879 gcc_assert (gfc_array_size (expr, &cons_size));
e11449d1
FR
880 cmp = mpz_cmp (cons_size, as_size);
881 if (cmp < 0)
882 gfc_error ("Not enough elements in array initializer at %C");
883 else if (cmp > 0)
884 gfc_error ("Too many elements in array initializer at %C");
9b24c104 885 mpz_clear (cons_size);
f6288c24 886 if (cmp)
9b24c104 887 goto cleanup;
f6288c24
FR
888 }
889
890 /* Make sure scalar types match. */
891 else if (!gfc_compare_types (&expr->ts, ts)
892 && !gfc_convert_type (expr, ts, 1))
893 goto cleanup;
894
895 if (expr->ts.u.cl)
896 expr->ts.u.cl->length_from_typespec = 1;
897
898 *result = expr;
e11449d1
FR
899 m = MATCH_YES;
900 goto done;
f6288c24
FR
901
902syntax:
e11449d1 903 m = MATCH_ERROR;
f6288c24
FR
904 gfc_error ("Syntax error in old style initializer list at %C");
905
906cleanup:
907 if (expr)
908 expr->value.constructor = NULL;
909 gfc_free_expr (expr);
910 gfc_constructor_free (array_head);
e11449d1
FR
911
912done:
f6288c24 913 mpz_clear (repeat);
e11449d1
FR
914 if (!scalar)
915 mpz_clear (as_size);
916 return m;
f6288c24
FR
917}
918
919
eea58adb 920/* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
d3a9eea2 921
524af0d6 922static bool
d3a9eea2
TB
923merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
924{
93d1ab50 925 int i, j;
d3a9eea2 926
63fbf586
TB
927 if ((from->type == AS_ASSUMED_RANK && to->corank)
928 || (to->type == AS_ASSUMED_RANK && from->corank))
929 {
930 gfc_error ("The assumed-rank array at %C shall not have a codimension");
524af0d6 931 return false;
63fbf586 932 }
c62c6622 933
d3a9eea2
TB
934 if (to->rank == 0 && from->rank > 0)
935 {
936 to->rank = from->rank;
937 to->type = from->type;
938 to->cray_pointee = from->cray_pointee;
939 to->cp_was_assumed = from->cp_was_assumed;
940
941 for (i = 0; i < to->corank; i++)
942 {
93d1ab50
SK
943 /* Do not exceed the limits on lower[] and upper[]. gfortran
944 cleans up elsewhere. */
945 j = from->rank + i;
946 if (j >= GFC_MAX_DIMENSIONS)
947 break;
948
949 to->lower[j] = to->lower[i];
950 to->upper[j] = to->upper[i];
d3a9eea2
TB
951 }
952 for (i = 0; i < from->rank; i++)
953 {
954 if (copy)
955 {
956 to->lower[i] = gfc_copy_expr (from->lower[i]);
957 to->upper[i] = gfc_copy_expr (from->upper[i]);
958 }
959 else
960 {
961 to->lower[i] = from->lower[i];
962 to->upper[i] = from->upper[i];
963 }
964 }
965 }
966 else if (to->corank == 0 && from->corank > 0)
967 {
968 to->corank = from->corank;
969 to->cotype = from->cotype;
970
971 for (i = 0; i < from->corank; i++)
972 {
93d1ab50
SK
973 /* Do not exceed the limits on lower[] and upper[]. gfortran
974 cleans up elsewhere. */
975 j = to->rank + i;
976 if (j >= GFC_MAX_DIMENSIONS)
977 break;
978
d3a9eea2
TB
979 if (copy)
980 {
93d1ab50
SK
981 to->lower[j] = gfc_copy_expr (from->lower[i]);
982 to->upper[j] = gfc_copy_expr (from->upper[i]);
d3a9eea2
TB
983 }
984 else
985 {
93d1ab50
SK
986 to->lower[j] = from->lower[i];
987 to->upper[j] = from->upper[i];
d3a9eea2
TB
988 }
989 }
990 }
63fbf586 991
299ab1b2 992 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
93d1ab50
SK
993 {
994 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
995 "allowed dimensions of %d",
996 to->rank, to->corank, GFC_MAX_DIMENSIONS);
997 to->corank = GFC_MAX_DIMENSIONS - to->rank;
998 return false;
999 }
524af0d6 1000 return true;
d3a9eea2
TB
1001}
1002
1003
6de9cd9a
DN
1004/* Match an intent specification. Since this can only happen after an
1005 INTENT word, a legal intent-spec must follow. */
1006
1007static sym_intent
1008match_intent_spec (void)
1009{
1010
1011 if (gfc_match (" ( in out )") == MATCH_YES)
1012 return INTENT_INOUT;
1013 if (gfc_match (" ( in )") == MATCH_YES)
1014 return INTENT_IN;
1015 if (gfc_match (" ( out )") == MATCH_YES)
1016 return INTENT_OUT;
1017
1018 gfc_error ("Bad INTENT specification at %C");
1019 return INTENT_UNKNOWN;
1020}
1021
1022
1023/* Matches a character length specification, which is either a
e69afb29 1024 specification expression, '*', or ':'. */
6de9cd9a
DN
1025
1026static match
e69afb29 1027char_len_param_value (gfc_expr **expr, bool *deferred)
6de9cd9a 1028{
cba28dad
JD
1029 match m;
1030
e69afb29
SK
1031 *expr = NULL;
1032 *deferred = false;
1033
6de9cd9a 1034 if (gfc_match_char ('*') == MATCH_YES)
e69afb29
SK
1035 return MATCH_YES;
1036
1037 if (gfc_match_char (':') == MATCH_YES)
6de9cd9a 1038 {
98a819ea 1039 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
e69afb29
SK
1040 return MATCH_ERROR;
1041
1042 *deferred = true;
1043
6de9cd9a
DN
1044 return MATCH_YES;
1045 }
1046
cba28dad 1047 m = gfc_match_expr (expr);
f37e928c 1048
98a819ea
SK
1049 if (m == MATCH_NO || m == MATCH_ERROR)
1050 return m;
1051
1052 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
f37e928c
DK
1053 return MATCH_ERROR;
1054
98a819ea 1055 if ((*expr)->expr_type == EXPR_FUNCTION)
cba28dad 1056 {
8d48826b
SK
1057 if ((*expr)->ts.type == BT_INTEGER
1058 || ((*expr)->ts.type == BT_UNKNOWN
1059 && strcmp((*expr)->symtree->name, "null") != 0))
1060 return MATCH_YES;
1061
1062 goto syntax;
1063 }
1064 else if ((*expr)->expr_type == EXPR_CONSTANT)
1065 {
1066 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1067 processor dependent and its value is greater than or equal to zero.
1068 F2008, 4.4.3.2: If the character length parameter value evaluates
1069 to a negative value, the length of character entities declared
1070 is zero. */
1071
1072 if ((*expr)->ts.type == BT_INTEGER)
cba28dad 1073 {
8d48826b
SK
1074 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1075 mpz_set_si ((*expr)->value.integer, 0);
cba28dad 1076 }
8d48826b
SK
1077 else
1078 goto syntax;
cba28dad 1079 }
8d48826b
SK
1080 else if ((*expr)->expr_type == EXPR_ARRAY)
1081 goto syntax;
1082 else if ((*expr)->expr_type == EXPR_VARIABLE)
1083 {
fb42421e 1084 bool t;
8d48826b
SK
1085 gfc_expr *e;
1086
1087 e = gfc_copy_expr (*expr);
1088
1089 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1090 which causes an ICE if gfc_reduce_init_expr() is called. */
54b96a2d
SK
1091 if (e->ref && e->ref->type == REF_ARRAY
1092 && e->ref->u.ar.type == AR_UNKNOWN
8d48826b
SK
1093 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1094 goto syntax;
1095
fb42421e
SK
1096 t = gfc_reduce_init_expr (e);
1097
8d987deb
SK
1098 if (!t && e->ts.type == BT_UNKNOWN
1099 && e->symtree->n.sym->attr.untyped == 1
63ac6251
TK
1100 && (flag_implicit_none
1101 || e->symtree->n.sym->ns->seen_implicit_none == 1
8d987deb 1102 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
fb42421e
SK
1103 {
1104 gfc_free_expr (e);
1105 goto syntax;
1106 }
98a819ea 1107
54b96a2d 1108 if ((e->ref && e->ref->type == REF_ARRAY
70112e2a 1109 && e->ref->u.ar.type != AR_ELEMENT)
8d48826b
SK
1110 || (!e->ref && e->expr_type == EXPR_ARRAY))
1111 {
1112 gfc_free_expr (e);
1113 goto syntax;
1114 }
1115
1116 gfc_free_expr (e);
1117 }
98a819ea 1118
cba28dad
JD
1119 return m;
1120
1121syntax:
8d48826b 1122 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
cba28dad 1123 return MATCH_ERROR;
6de9cd9a
DN
1124}
1125
1126
1127/* A character length is a '*' followed by a literal integer or a
1128 char_len_param_value in parenthesis. */
1129
1130static match
62732c30 1131match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
6de9cd9a 1132{
5cf54585 1133 int length;
6de9cd9a
DN
1134 match m;
1135
f5acf0f2 1136 *deferred = false;
6de9cd9a
DN
1137 m = gfc_match_char ('*');
1138 if (m != MATCH_YES)
1139 return m;
1140
5cf54585 1141 m = gfc_match_small_literal_int (&length, NULL);
6de9cd9a
DN
1142 if (m == MATCH_ERROR)
1143 return m;
1144
1145 if (m == MATCH_YES)
1146 {
62732c30 1147 if (obsolescent_check
524af0d6 1148 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
e2ab8b09 1149 return MATCH_ERROR;
f622221a 1150 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
6de9cd9a
DN
1151 return m;
1152 }
1153
1154 if (gfc_match_char ('(') == MATCH_NO)
1155 goto syntax;
1156
e69afb29 1157 m = char_len_param_value (expr, deferred);
1c8bcdf7
PT
1158 if (m != MATCH_YES && gfc_matching_function)
1159 {
1160 gfc_undo_symbols ();
1161 m = MATCH_YES;
1162 }
1163
6de9cd9a
DN
1164 if (m == MATCH_ERROR)
1165 return m;
1166 if (m == MATCH_NO)
1167 goto syntax;
1168
1169 if (gfc_match_char (')') == MATCH_NO)
1170 {
1171 gfc_free_expr (*expr);
1172 *expr = NULL;
1173 goto syntax;
1174 }
1175
1176 return MATCH_YES;
1177
1178syntax:
1179 gfc_error ("Syntax error in character length specification at %C");
1180 return MATCH_ERROR;
1181}
1182
1183
9e35b386
EE
1184/* Special subroutine for finding a symbol. Check if the name is found
1185 in the current name space. If not, and we're compiling a function or
1186 subroutine and the parent compilation unit is an interface, then check
1187 to see if the name we've been given is the name of the interface
1188 (located in another namespace). */
6de9cd9a
DN
1189
1190static int
08a6b8e0 1191find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
6de9cd9a
DN
1192{
1193 gfc_state_data *s;
08a6b8e0 1194 gfc_symtree *st;
9e35b386 1195 int i;
6de9cd9a 1196
08a6b8e0 1197 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
d51347f9 1198 if (i == 0)
08a6b8e0
TB
1199 {
1200 *result = st ? st->n.sym : NULL;
1201 goto end;
1202 }
d51347f9 1203
6de9cd9a
DN
1204 if (gfc_current_state () != COMP_SUBROUTINE
1205 && gfc_current_state () != COMP_FUNCTION)
9e35b386 1206 goto end;
6de9cd9a
DN
1207
1208 s = gfc_state_stack->previous;
1209 if (s == NULL)
9e35b386 1210 goto end;
6de9cd9a
DN
1211
1212 if (s->state != COMP_INTERFACE)
9e35b386 1213 goto end;
6de9cd9a 1214 if (s->sym == NULL)
66e4ab31 1215 goto end; /* Nameless interface. */
6de9cd9a
DN
1216
1217 if (strcmp (name, s->sym->name) == 0)
1218 {
1219 *result = s->sym;
1220 return 0;
1221 }
1222
9e35b386
EE
1223end:
1224 return i;
6de9cd9a
DN
1225}
1226
1227
1228/* Special subroutine for getting a symbol node associated with a
1229 procedure name, used in SUBROUTINE and FUNCTION statements. The
1230 symbol is created in the parent using with symtree node in the
1231 child unit pointing to the symbol. If the current namespace has no
1232 parent, then the symbol is just created in the current unit. */
1233
1234static int
636dff67 1235get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
6de9cd9a
DN
1236{
1237 gfc_symtree *st;
1238 gfc_symbol *sym;
a7ca4d8d 1239 int rc = 0;
6de9cd9a 1240
1a492601
PT
1241 /* Module functions have to be left in their own namespace because
1242 they have potentially (almost certainly!) already been referenced.
1243 In this sense, they are rather like external functions. This is
1244 fixed up in resolve.c(resolve_entries), where the symbol name-
1245 space is set to point to the master function, so that the fake
1246 result mechanism can work. */
1247 if (module_fcn_entry)
6c12686b
PT
1248 {
1249 /* Present if entry is declared to be a module procedure. */
1250 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
aa84a9a5 1251
6c12686b
PT
1252 if (*result == NULL)
1253 rc = gfc_get_symbol (name, NULL, result);
2e32a71e 1254 else if (!gfc_get_symbol (name, NULL, &sym) && sym
aa84a9a5
PT
1255 && (*result)->ts.type == BT_UNKNOWN
1256 && sym->attr.flavor == FL_UNKNOWN)
1257 /* Pick up the typespec for the entry, if declared in the function
1258 body. Note that this symbol is FL_UNKNOWN because it will
1259 only have appeared in a type declaration. The local symtree
1260 is set to point to the module symbol and a unique symtree
1261 to the local version. This latter ensures a correct clearing
1262 of the symbols. */
2e32a71e
PT
1263 {
1264 /* If the ENTRY proceeds its specification, we need to ensure
1265 that this does not raise a "has no IMPLICIT type" error. */
1266 if (sym->ts.type == BT_UNKNOWN)
0e5a218b 1267 sym->attr.untyped = 1;
2e32a71e 1268
0e5a218b 1269 (*result)->ts = sym->ts;
2e32a71e
PT
1270
1271 /* Put the symbol in the procedure namespace so that, should
df2fba9e 1272 the ENTRY precede its specification, the specification
2e32a71e
PT
1273 can be applied. */
1274 (*result)->ns = gfc_current_ns;
1275
1276 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1277 st->n.sym = *result;
1278 st = gfc_get_unique_symtree (gfc_current_ns);
2050626a 1279 sym->refs++;
2e32a71e
PT
1280 st->n.sym = sym;
1281 }
6c12686b 1282 }
68ea355b
PT
1283 else
1284 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
6de9cd9a 1285
a7ca4d8d
PT
1286 if (rc)
1287 return rc;
1288
68ea355b 1289 sym = *result;
79124116
PT
1290 if (sym->attr.proc == PROC_ST_FUNCTION)
1291 return rc;
6de9cd9a 1292
96c8b253 1293 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
4668d6f9
PT
1294 {
1295 /* Create a partially populated interface symbol to carry the
1296 characteristics of the procedure and the result. */
c064374d 1297 sym->tlink = gfc_new_symbol (name, sym->ns);
96c8b253 1298 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
c064374d 1299 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
4668d6f9 1300 if (sym->attr.dimension)
c064374d 1301 sym->tlink->as = gfc_copy_array_spec (sym->as);
4668d6f9
PT
1302
1303 /* Ideally, at this point, a copy would be made of the formal
1304 arguments and their namespace. However, this does not appear
1305 to be necessary, albeit at the expense of not being able to
1306 use gfc_compare_interfaces directly. */
1307
1308 if (sym->result && sym->result != sym)
1309 {
c064374d 1310 sym->tlink->result = sym->result;
4668d6f9
PT
1311 sym->result = NULL;
1312 }
1313 else if (sym->result)
1314 {
c064374d 1315 sym->tlink->result = sym->tlink;
4668d6f9
PT
1316 }
1317 }
1318 else if (sym && !sym->gfc_new
1319 && gfc_current_state () != COMP_INTERFACE)
68ea355b 1320 {
cda7004b
PT
1321 /* Trap another encompassed procedure with the same name. All
1322 these conditions are necessary to avoid picking up an entry
1323 whose name clashes with that of the encompassing procedure;
2050626a 1324 this is handled using gsymbols to register unique, globally
cda7004b 1325 accessible names. */
68ea355b 1326 if (sym->attr.flavor != 0
636dff67 1327 && sym->attr.proc != 0
64300da7 1328 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
636dff67 1329 && sym->attr.if_source != IFSRC_UNKNOWN)
b4439561
TB
1330 {
1331 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1332 name, &sym->declared_at);
1333 return true;
1334 }
64300da7
SK
1335 if (sym->attr.flavor != 0
1336 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
b4439561
TB
1337 {
1338 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1339 name, &sym->declared_at);
1340 return true;
1341 }
64300da7 1342
81ea7c11
SK
1343 if (sym->attr.external && sym->attr.procedure
1344 && gfc_current_state () == COMP_CONTAINS)
b4439561
TB
1345 {
1346 gfc_error_now ("Contained procedure %qs at %C clashes with "
1347 "procedure defined at %L",
1348 name, &sym->declared_at);
1349 return true;
1350 }
81ea7c11 1351
fd3e70af
JD
1352 /* Trap a procedure with a name the same as interface in the
1353 encompassing scope. */
1354 if (sym->attr.generic != 0
2305fa31
JD
1355 && (sym->attr.subroutine || sym->attr.function)
1356 && !sym->attr.mod_proc)
b4439561
TB
1357 {
1358 gfc_error_now ("Name %qs at %C is already defined"
1359 " as a generic interface at %L",
1360 name, &sym->declared_at);
1361 return true;
1362 }
fd3e70af 1363
68ea355b
PT
1364 /* Trap declarations of attributes in encompassing scope. The
1365 signature for this is that ts.kind is set. Legitimate
1366 references only set ts.type. */
1367 if (sym->ts.kind != 0
636dff67
SK
1368 && !sym->attr.implicit_type
1369 && sym->attr.proc == 0
1370 && gfc_current_ns->parent != NULL
1371 && sym->attr.access == 0
1372 && !module_fcn_entry)
b4439561
TB
1373 {
1374 gfc_error_now ("Procedure %qs at %C has an explicit interface "
96c8b253 1375 "from a previous declaration", name);
b4439561
TB
1376 return true;
1377 }
96c8b253
SK
1378 }
1379
b74fa126
SK
1380 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1381 subroutine-stmt of a module subprogram or of a nonabstract interface
1382 body that is declared in the scoping unit of a module or submodule. */
1383 if (sym->attr.external
1384 && (sym->attr.subroutine || sym->attr.function)
1385 && sym->attr.if_source == IFSRC_IFBODY
1386 && !current_attr.module_procedure
1387 && sym->attr.proc == PROC_MODULE
1388 && gfc_state_stack->state == COMP_CONTAINS)
b4439561
TB
1389 {
1390 gfc_error_now ("Procedure %qs defined in interface body at %L "
1391 "clashes with internal procedure defined at %C",
1392 name, &sym->declared_at);
1393 return true;
1394 }
b74fa126
SK
1395
1396 if (sym && !sym->gfc_new
1397 && sym->attr.flavor != FL_UNKNOWN
1398 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1399 && gfc_state_stack->state == COMP_CONTAINS
1400 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
b4439561
TB
1401 {
1402 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1403 name, &sym->declared_at);
1404 return true;
1405 }
68ea355b
PT
1406
1407 if (gfc_current_ns->parent == NULL || *result == NULL)
1408 return rc;
6de9cd9a 1409
1a492601
PT
1410 /* Module function entries will already have a symtree in
1411 the current namespace but will need one at module level. */
1412 if (module_fcn_entry)
6c12686b
PT
1413 {
1414 /* Present if entry is declared to be a module procedure. */
1415 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1416 if (st == NULL)
1417 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1418 }
1a492601
PT
1419 else
1420 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
6de9cd9a 1421
6de9cd9a
DN
1422 st->n.sym = sym;
1423 sym->refs++;
1424
66e4ab31 1425 /* See if the procedure should be a module procedure. */
6de9cd9a 1426
1a492601 1427 if (((sym->ns->proc_name != NULL
96c8b253
SK
1428 && sym->ns->proc_name->attr.flavor == FL_MODULE
1429 && sym->attr.proc != PROC_MODULE)
1430 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1431 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
6de9cd9a
DN
1432 rc = 2;
1433
1434 return rc;
1435}
1436
1437
a8b3b0b6
CR
1438/* Verify that the given symbol representing a parameter is C
1439 interoperable, by checking to see if it was marked as such after
1440 its declaration. If the given symbol is not interoperable, a
1441 warning is reported, thus removing the need to return the status to
1442 the calling function. The standard does not require the user use
1443 one of the iso_c_binding named constants to declare an
1444 interoperable parameter, but we can't be sure if the param is C
1445 interop or not if the user doesn't. For example, integer(4) may be
1446 legal Fortran, but doesn't have meaning in C. It may interop with
1447 a number of the C types, which causes a problem because the
1448 compiler can't know which one. This code is almost certainly not
1449 portable, and the user will get what they deserve if the C type
1450 across platforms isn't always interoperable with integer(4). If
1451 the user had used something like integer(c_int) or integer(c_long),
1452 the compiler could have automatically handled the varying sizes
1453 across platforms. */
1454
524af0d6 1455bool
00820a2a 1456gfc_verify_c_interop_param (gfc_symbol *sym)
a8b3b0b6
CR
1457{
1458 int is_c_interop = 0;
524af0d6 1459 bool retval = true;
a8b3b0b6
CR
1460
1461 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1462 Don't repeat the checks here. */
1463 if (sym->attr.implicit_type)
524af0d6 1464 return true;
f5acf0f2 1465
a8b3b0b6
CR
1466 /* For subroutines or functions that are passed to a BIND(C) procedure,
1467 they're interoperable if they're BIND(C) and their params are all
1468 interoperable. */
1469 if (sym->attr.flavor == FL_PROCEDURE)
1470 {
1471 if (sym->attr.is_bind_c == 0)
1472 {
4daa149b
TB
1473 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1474 "attribute to be C interoperable", sym->name,
1475 &(sym->declared_at));
524af0d6 1476 return false;
a8b3b0b6
CR
1477 }
1478 else
1479 {
1480 if (sym->attr.is_c_interop == 1)
1481 /* We've already checked this procedure; don't check it again. */
524af0d6 1482 return true;
a8b3b0b6
CR
1483 else
1484 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1485 sym->common_block);
1486 }
1487 }
f5acf0f2 1488
a8b3b0b6
CR
1489 /* See if we've stored a reference to a procedure that owns sym. */
1490 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1491 {
1492 if (sym->ns->proc_name->attr.is_bind_c == 1)
1493 {
524af0d6 1494 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
a8b3b0b6
CR
1495
1496 if (is_c_interop != 1)
1497 {
1498 /* Make personalized messages to give better feedback. */
1499 if (sym->ts.type == BT_DERIVED)
c4100eae
MLI
1500 gfc_error ("Variable %qs at %L is a dummy argument to the "
1501 "BIND(C) procedure %qs but is not C interoperable "
1502 "because derived type %qs is not C interoperable",
a8b3b0b6 1503 sym->name, &(sym->declared_at),
f5acf0f2 1504 sym->ns->proc_name->name,
bc21d315 1505 sym->ts.u.derived->name);
00820a2a 1506 else if (sym->ts.type == BT_CLASS)
c4100eae
MLI
1507 gfc_error ("Variable %qs at %L is a dummy argument to the "
1508 "BIND(C) procedure %qs but is not C interoperable "
00820a2a
JW
1509 "because it is polymorphic",
1510 sym->name, &(sym->declared_at),
1511 sym->ns->proc_name->name);
4daa149b 1512 else if (warn_c_binding_type)
48749dbc
MLI
1513 gfc_warning (OPT_Wc_binding_type,
1514 "Variable %qs at %L is a dummy argument of the "
1515 "BIND(C) procedure %qs but may not be C "
a8b3b0b6
CR
1516 "interoperable",
1517 sym->name, &(sym->declared_at),
1518 sym->ns->proc_name->name);
1519 }
aa5e22f0
CR
1520
1521 /* Character strings are only C interoperable if they have a
1522 length of 1. */
0a524296 1523 if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension)
aa5e22f0 1524 {
bc21d315 1525 gfc_charlen *cl = sym->ts.u.cl;
aa5e22f0
CR
1526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1527 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1528 {
0a524296
PT
1529 gfc_error ("Character argument %qs at %L "
1530 "must be length 1 because "
1531 "procedure %qs is BIND(C)",
1532 sym->name, &sym->declared_at,
1533 sym->ns->proc_name->name);
1534 retval = false;
aa5e22f0
CR
1535 }
1536 }
1537
a8b3b0b6
CR
1538 /* We have to make sure that any param to a bind(c) routine does
1539 not have the allocatable, pointer, or optional attributes,
1540 according to J3/04-007, section 5.1. */
60f6ca95 1541 if (sym->attr.allocatable == 1
286f737c 1542 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
a4d9b221 1543 "ALLOCATABLE attribute in procedure %qs "
60f6ca95
TB
1544 "with BIND(C)", sym->name,
1545 &(sym->declared_at),
1546 sym->ns->proc_name->name))
1547 retval = false;
1548
1549 if (sym->attr.pointer == 1
286f737c 1550 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
a4d9b221 1551 "POINTER attribute in procedure %qs "
60f6ca95
TB
1552 "with BIND(C)", sym->name,
1553 &(sym->declared_at),
1554 sym->ns->proc_name->name))
1555 retval = false;
1556
1557 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
a8b3b0b6 1558 {
c4100eae
MLI
1559 gfc_error ("Scalar variable %qs at %L with POINTER or "
1560 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
60f6ca95 1561 " supported", sym->name, &(sym->declared_at),
a8b3b0b6 1562 sym->ns->proc_name->name);
524af0d6 1563 retval = false;
a8b3b0b6
CR
1564 }
1565
2e8d9212 1566 if (sym->attr.optional == 1 && sym->attr.value)
a8b3b0b6 1567 {
c4100eae
MLI
1568 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1569 "and the VALUE attribute because procedure %qs "
2e8d9212 1570 "is BIND(C)", sym->name, &(sym->declared_at),
a8b3b0b6 1571 sym->ns->proc_name->name);
524af0d6 1572 retval = false;
a8b3b0b6 1573 }
2e8d9212 1574 else if (sym->attr.optional == 1
286f737c 1575 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
524af0d6 1576 "at %L with OPTIONAL attribute in "
70112e2a
PT
1577 "procedure %qs which is BIND(C)",
1578 sym->name, &(sym->declared_at),
524af0d6
JB
1579 sym->ns->proc_name->name))
1580 retval = false;
a8b3b0b6
CR
1581
1582 /* Make sure that if it has the dimension attribute, that it is
95d47b8d
TB
1583 either assumed size or explicit shape. Deferred shape is already
1584 covered by the pointer/allocatable attribute. */
1585 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
286f737c 1586 && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
524af0d6 1587 "at %L as dummy argument to the BIND(C) "
811582ec 1588 "procedure %qs at %L", sym->name,
70112e2a
PT
1589 &(sym->declared_at),
1590 sym->ns->proc_name->name,
524af0d6
JB
1591 &(sym->ns->proc_name->declared_at)))
1592 retval = false;
a8b3b0b6
CR
1593 }
1594 }
1595
1596 return retval;
1597}
1598
1599
cf2b3c22 1600
a8b3b0b6 1601/* Function called by variable_decl() that adds a name to the symbol table. */
6de9cd9a 1602
524af0d6 1603static bool
e69afb29 1604build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
636dff67 1605 gfc_array_spec **as, locus *var_locus)
6de9cd9a
DN
1606{
1607 symbol_attribute attr;
1608 gfc_symbol *sym;
1e6025b6 1609 int upper;
bedee914
PT
1610 gfc_symtree *st;
1611
1612 /* Symbols in a submodule are host associated from the parent module or
1613 submodules. Therefore, they can be overridden by declarations in the
1614 submodule scope. Deal with this by attaching the existing symbol to
1615 a new symtree and recycling the old symtree with a new symbol... */
1616 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1617 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1618 && st->n.sym != NULL
1619 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1620 {
1621 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1622 s->n.sym = st->n.sym;
1623 sym = gfc_new_symbol (name, gfc_current_ns);
6de9cd9a 1624
bedee914
PT
1625
1626 st->n.sym = sym;
1627 sym->refs++;
1628 gfc_set_sym_referenced (sym);
1629 }
1630 /* ...Otherwise generate a new symtree and new symbol. */
1631 else if (gfc_get_symbol (name, NULL, &sym))
524af0d6 1632 return false;
6de9cd9a 1633
1e6025b6
TK
1634 /* Check if the name has already been defined as a type. The
1635 first letter of the symtree will be in upper case then. Of
1636 course, this is only necessary if the upper case letter is
1637 actually different. */
1638
1639 upper = TOUPPER(name[0]);
1640 if (upper != name[0])
1641 {
1642 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1643 gfc_symtree *st;
1e6025b6 1644
025d57f0
MS
1645 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1646 strcpy (u_name, name);
1e6025b6
TK
1647 u_name[0] = upper;
1648
1649 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1650
f6288c24
FR
1651 /* STRUCTURE types can alias symbol names */
1652 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1e6025b6
TK
1653 {
1654 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1655 &st->n.sym->declared_at);
1656 return false;
1657 }
1658 }
1659
66e4ab31 1660 /* Start updating the symbol table. Add basic type attribute if present. */
6de9cd9a 1661 if (current_ts.type != BT_UNKNOWN
636dff67
SK
1662 && (sym->attr.implicit_type == 0
1663 || !gfc_compare_types (&sym->ts, &current_ts))
524af0d6
JB
1664 && !gfc_add_type (sym, &current_ts, var_locus))
1665 return false;
6de9cd9a
DN
1666
1667 if (sym->ts.type == BT_CHARACTER)
e69afb29
SK
1668 {
1669 sym->ts.u.cl = cl;
1670 sym->ts.deferred = cl_deferred;
1671 }
6de9cd9a
DN
1672
1673 /* Add dimension attribute if present. */
524af0d6
JB
1674 if (!gfc_set_array_spec (sym, *as, var_locus))
1675 return false;
6de9cd9a
DN
1676 *as = NULL;
1677
1678 /* Add attribute to symbol. The copy is so that we can reset the
1679 dimension attribute. */
1680 attr = current_attr;
1681 attr.dimension = 0;
be59db2d 1682 attr.codimension = 0;
6de9cd9a 1683
524af0d6
JB
1684 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1685 return false;
6de9cd9a 1686
a8b3b0b6
CR
1687 /* Finish any work that may need to be done for the binding label,
1688 if it's a bind(c). The bind(c) attr is found before the symbol
1689 is made, and before the symbol name (for data decls), so the
1690 current_ts is holding the binding label, or nothing if the
1691 name= attr wasn't given. Therefore, test here if we're dealing
1692 with a bind(c) and make sure the binding label is set correctly. */
1693 if (sym->attr.is_bind_c == 1)
1694 {
62603fae 1695 if (!sym->binding_label)
a8b3b0b6 1696 {
ad4a2f64
TB
1697 /* Set the binding label and verify that if a NAME= was specified
1698 then only one identifier was in the entity-decl-list. */
70112e2a 1699 if (!set_binding_label (&sym->binding_label, sym->name,
524af0d6
JB
1700 num_idents_on_line))
1701 return false;
a8b3b0b6
CR
1702 }
1703 }
1704
1705 /* See if we know we're in a common block, and if it's a bind(c)
1706 common then we need to make sure we're an interoperable type. */
1707 if (sym->attr.in_common == 1)
1708 {
1709 /* Test the common block object. */
1710 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1711 && sym->ts.is_c_interop != 1)
1712 {
4daa149b 1713 gfc_error_now ("Variable %qs in common block %qs at %C "
a8b3b0b6 1714 "must be declared with a C interoperable "
4daa149b 1715 "kind since common block %qs is BIND(C)",
a8b3b0b6
CR
1716 sym->name, sym->common_block->name,
1717 sym->common_block->name);
1718 gfc_clear_error ();
1719 }
1720 }
1721
9a3db5a3
PT
1722 sym->attr.implied_index = 0;
1723
5bab4c96
PT
1724 /* Use the parameter expressions for a parameterized derived type. */
1725 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1726 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1727 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1728
528622fd 1729 if (sym->ts.type == BT_CLASS)
9b6da3c7 1730 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
cf2b3c22 1731
524af0d6 1732 return true;
6de9cd9a
DN
1733}
1734
636dff67 1735
df7cc9b5 1736/* Set character constant to the given length. The constant will be padded or
d2848082
DK
1737 truncated. If we're inside an array constructor without a typespec, we
1738 additionally check that all elements have the same length; check_len -1
1739 means no checking. */
df7cc9b5
FW
1740
1741void
6b271a2e
JB
1742gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1743 gfc_charlen_t check_len)
df7cc9b5 1744{
00660189 1745 gfc_char_t *s;
6b271a2e 1746 gfc_charlen_t slen;
df7cc9b5 1747
834e9dbb
SK
1748 if (expr->ts.type != BT_CHARACTER)
1749 return;
63af1586 1750
b441ae1d
SK
1751 if (expr->expr_type != EXPR_CONSTANT)
1752 {
1753 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1754 return;
1755 }
df7cc9b5
FW
1756
1757 slen = expr->value.character.length;
1758 if (len != slen)
1759 {
00660189
FXC
1760 s = gfc_get_wide_string (len + 1);
1761 memcpy (s, expr->value.character.string,
1762 MIN (len, slen) * sizeof (gfc_char_t));
df7cc9b5 1763 if (len > slen)
00660189 1764 gfc_wide_memset (&s[slen], ' ', len - slen);
2220652d 1765
a96c39ea 1766 if (warn_character_truncation && slen > len)
4daa149b
TB
1767 gfc_warning_now (OPT_Wcharacter_truncation,
1768 "CHARACTER expression at %L is being truncated "
6b271a2e
JB
1769 "(%ld/%ld)", &expr->where,
1770 (long) slen, (long) len);
2220652d
PT
1771
1772 /* Apply the standard by 'hand' otherwise it gets cleared for
1773 initializers. */
d2848082
DK
1774 if (check_len != -1 && slen != check_len
1775 && !(gfc_option.allow_std & GFC_STD_GNU))
2220652d 1776 gfc_error_now ("The CHARACTER elements of the array constructor "
6b271a2e
JB
1777 "at %L must have the same length (%ld/%ld)",
1778 &expr->where, (long) slen,
1779 (long) check_len);
2220652d 1780
150675a8 1781 s[len] = '\0';
cede9502 1782 free (expr->value.character.string);
df7cc9b5
FW
1783 expr->value.character.string = s;
1784 expr->value.character.length = len;
e6ca33ba
HA
1785 /* If explicit representation was given, clear it
1786 as it is no longer needed after padding. */
1787 if (expr->representation.length)
1788 {
1789 expr->representation.length = 0;
1790 free (expr->representation.string);
1791 expr->representation.string = NULL;
1792 }
df7cc9b5
FW
1793 }
1794}
6de9cd9a 1795
25d8f0a2 1796
d51347f9 1797/* Function to create and update the enumerator history
25d8f0a2 1798 using the information passed as arguments.
d51347f9
TB
1799 Pointer "max_enum" is also updated, to point to
1800 enum history node containing largest initializer.
25d8f0a2
TS
1801
1802 SYM points to the symbol node of enumerator.
66e4ab31 1803 INIT points to its enumerator value. */
25d8f0a2 1804
d51347f9 1805static void
636dff67 1806create_enum_history (gfc_symbol *sym, gfc_expr *init)
25d8f0a2
TS
1807{
1808 enumerator_history *new_enum_history;
1809 gcc_assert (sym != NULL && init != NULL);
1810
ece3f663 1811 new_enum_history = XCNEW (enumerator_history);
25d8f0a2
TS
1812
1813 new_enum_history->sym = sym;
1814 new_enum_history->initializer = init;
1815 new_enum_history->next = NULL;
1816
1817 if (enum_history == NULL)
1818 {
1819 enum_history = new_enum_history;
1820 max_enum = enum_history;
1821 }
1822 else
1823 {
1824 new_enum_history->next = enum_history;
1825 enum_history = new_enum_history;
1826
d51347f9 1827 if (mpz_cmp (max_enum->initializer->value.integer,
25d8f0a2 1828 new_enum_history->initializer->value.integer) < 0)
636dff67 1829 max_enum = new_enum_history;
25d8f0a2
TS
1830 }
1831}
1832
1833
d51347f9 1834/* Function to free enum kind history. */
25d8f0a2 1835
d51347f9 1836void
636dff67 1837gfc_free_enum_history (void)
25d8f0a2 1838{
d51347f9
TB
1839 enumerator_history *current = enum_history;
1840 enumerator_history *next;
25d8f0a2
TS
1841
1842 while (current != NULL)
1843 {
1844 next = current->next;
cede9502 1845 free (current);
25d8f0a2
TS
1846 current = next;
1847 }
1848 max_enum = NULL;
1849 enum_history = NULL;
1850}
1851
1852
6de9cd9a
DN
1853/* Function called by variable_decl() that adds an initialization
1854 expression to a symbol. */
1855
524af0d6 1856static bool
66e4ab31 1857add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
6de9cd9a
DN
1858{
1859 symbol_attribute attr;
1860 gfc_symbol *sym;
1861 gfc_expr *init;
1862
1863 init = *initp;
08a6b8e0 1864 if (find_special (name, &sym, false))
524af0d6 1865 return false;
6de9cd9a
DN
1866
1867 attr = sym->attr;
1868
1869 /* If this symbol is confirming an implicit parameter type,
1870 then an initialization expression is not allowed. */
1871 if (attr.flavor == FL_PARAMETER
1872 && sym->value != NULL
1873 && *initp != NULL)
1874 {
c4100eae 1875 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
6de9cd9a 1876 sym->name);
524af0d6 1877 return false;
6de9cd9a
DN
1878 }
1879
1880 if (init == NULL)
1881 {
1882 /* An initializer is required for PARAMETER declarations. */
1883 if (attr.flavor == FL_PARAMETER)
1884 {
1885 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
524af0d6 1886 return false;
6de9cd9a
DN
1887 }
1888 }
1889 else
1890 {
1891 /* If a variable appears in a DATA block, it cannot have an
1de8a836 1892 initializer. */
6de9cd9a
DN
1893 if (sym->attr.data)
1894 {
c4100eae 1895 gfc_error ("Variable %qs at %C with an initializer already "
636dff67 1896 "appears in a DATA statement", sym->name);
524af0d6 1897 return false;
6de9cd9a
DN
1898 }
1899
75d17889 1900 /* Check if the assignment can happen. This has to be put off
80f95228 1901 until later for derived type variables and procedure pointers. */
f6288c24 1902 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
cf2b3c22 1903 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
f5acf0f2 1904 && !sym->attr.proc_pointer
524af0d6
JB
1905 && !gfc_check_assign_symbol (sym, NULL, init))
1906 return false;
6de9cd9a 1907
bc21d315 1908 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
51b128a0 1909 && init->ts.type == BT_CHARACTER)
df7cc9b5
FW
1910 {
1911 /* Update symbol character length according initializer. */
524af0d6
JB
1912 if (!gfc_check_assign_symbol (sym, NULL, init))
1913 return false;
51b128a0 1914
bc21d315 1915 if (sym->ts.u.cl->length == NULL)
df7cc9b5 1916 {
f622221a 1917 gfc_charlen_t clen;
66e4ab31
SK
1918 /* If there are multiple CHARACTER variables declared on the
1919 same line, we don't want them to share the same length. */
b76e28c6 1920 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
96f4873b 1921
a99288e5
PT
1922 if (sym->attr.flavor == FL_PARAMETER)
1923 {
1924 if (init->expr_type == EXPR_CONSTANT)
1925 {
1926 clen = init->value.character.length;
b7e75771 1927 sym->ts.u.cl->length
f622221a 1928 = gfc_get_int_expr (gfc_charlen_int_kind,
b7e75771 1929 NULL, clen);
a99288e5
PT
1930 }
1931 else if (init->expr_type == EXPR_ARRAY)
1932 {
c004a341 1933 if (init->ts.u.cl && init->ts.u.cl->length)
39abef62
LK
1934 {
1935 const gfc_expr *length = init->ts.u.cl->length;
1936 if (length->expr_type != EXPR_CONSTANT)
1937 {
1938 gfc_error ("Cannot initialize parameter array "
1939 "at %L "
1940 "with variable length elements",
1941 &sym->declared_at);
1942 return false;
1943 }
1944 clen = mpz_get_si (length->value.integer);
1945 }
dc0f176a
SK
1946 else if (init->value.constructor)
1947 {
1948 gfc_constructor *c;
70112e2a 1949 c = gfc_constructor_first (init->value.constructor);
dc0f176a
SK
1950 clen = c->expr->value.character.length;
1951 }
1952 else
1953 gcc_unreachable ();
b7e75771 1954 sym->ts.u.cl->length
f622221a 1955 = gfc_get_int_expr (gfc_charlen_int_kind,
b7e75771 1956 NULL, clen);
a99288e5 1957 }
bc21d315
JW
1958 else if (init->ts.u.cl && init->ts.u.cl->length)
1959 sym->ts.u.cl->length =
bc1efcb7 1960 gfc_copy_expr (init->ts.u.cl->length);
a99288e5 1961 }
df7cc9b5
FW
1962 }
1963 /* Update initializer character length according symbol. */
bc21d315 1964 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
df7cc9b5 1965 {
d30ecc9c
SK
1966 if (!gfc_specification_expr (sym->ts.u.cl->length))
1967 return false;
1968
aeb8c028
JJ
1969 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
1970 false);
1971 /* resolve_charlen will complain later on if the length
1972 is too large. Just skeep the initialization in that case. */
1973 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
1974 gfc_integer_kinds[k].huge) <= 0)
df7cc9b5 1975 {
aeb8c028
JJ
1976 HOST_WIDE_INT len
1977 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
1978
1979 if (init->expr_type == EXPR_CONSTANT)
1980 gfc_set_constant_character_len (len, init, -1);
1981 else if (init->expr_type == EXPR_ARRAY)
1982 {
1983 gfc_constructor *c;
b7e75771 1984
aeb8c028
JJ
1985 /* Build a new charlen to prevent simplification from
1986 deleting the length before it is resolved. */
1987 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1988 init->ts.u.cl->length
1989 = gfc_copy_expr (sym->ts.u.cl->length);
dcdc7b6c 1990
aeb8c028
JJ
1991 for (c = gfc_constructor_first (init->value.constructor);
1992 c; c = gfc_constructor_next (c))
1993 gfc_set_constant_character_len (len, c->expr, -1);
1994 }
df7cc9b5
FW
1995 }
1996 }
1997 }
1998
f5ca06e6
DK
1999 /* If sym is implied-shape, set its upper bounds from init. */
2000 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2001 && sym->as->type == AS_IMPLIED_SHAPE)
2002 {
2003 int dim;
2004
2005 if (init->rank == 0)
2006 {
1fe61adf 2007 gfc_error ("Cannot initialize implied-shape array at %L"
f5ca06e6 2008 " with scalar", &sym->declared_at);
524af0d6 2009 return false;
f5ca06e6 2010 }
f5ca06e6 2011
8ed5ae52
TK
2012 /* The shape may be NULL for EXPR_ARRAY, set it. */
2013 if (init->shape == NULL)
2014 {
2015 gcc_assert (init->expr_type == EXPR_ARRAY);
2016 init->shape = gfc_get_shape (1);
2017 if (!gfc_array_size (init, &init->shape[0]))
2018 gfc_internal_error ("gfc_array_size failed");
2019 }
f5ca06e6
DK
2020
2021 for (dim = 0; dim < sym->as->rank; ++dim)
2022 {
2023 int k;
cdffe788 2024 gfc_expr *e, *lower;
f5acf0f2 2025
f5ca06e6 2026 lower = sym->as->lower[dim];
cdffe788 2027
70112e2a 2028 /* If the lower bound is an array element from another
cdffe788
SK
2029 parameterized array, then it is marked with EXPR_VARIABLE and
2030 is an initialization expression. Try to reduce it. */
2031 if (lower->expr_type == EXPR_VARIABLE)
2032 gfc_reduce_init_expr (lower);
2033
2034 if (lower->expr_type == EXPR_CONSTANT)
2035 {
2036 /* All dimensions must be without upper bound. */
2037 gcc_assert (!sym->as->upper[dim]);
2038
2039 k = lower->ts.kind;
2040 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2041 mpz_add (e->value.integer, lower->value.integer,
2042 init->shape[dim]);
2043 mpz_sub_ui (e->value.integer, e->value.integer, 1);
2044 sym->as->upper[dim] = e;
2045 }
2046 else
f5ca06e6
DK
2047 {
2048 gfc_error ("Non-constant lower bound in implied-shape"
2049 " declaration at %L", &lower->where);
524af0d6 2050 return false;
f5ca06e6 2051 }
f5ca06e6
DK
2052 }
2053
2054 sym->as->type = AS_EXPLICIT;
2055 }
2056
a8b3b0b6
CR
2057 /* Need to check if the expression we initialized this
2058 to was one of the iso_c_binding named constants. If so,
2059 and we're a parameter (constant), let it be iso_c.
2060 For example:
2061 integer(c_int), parameter :: my_int = c_int
2062 integer(my_int) :: my_int_2
2063 If we mark my_int as iso_c (since we can see it's value
2064 is equal to one of the named constants), then my_int_2
2065 will be considered C interoperable. */
f6288c24 2066 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
a8b3b0b6
CR
2067 {
2068 sym->ts.is_iso_c |= init->ts.is_iso_c;
2069 sym->ts.is_c_interop |= init->ts.is_c_interop;
2070 /* attr bits needed for module files. */
2071 sym->attr.is_iso_c |= init->ts.is_iso_c;
2072 sym->attr.is_c_interop |= init->ts.is_c_interop;
2073 if (init->ts.is_iso_c)
2074 sym->ts.f90_type = init->ts.f90_type;
2075 }
b7e75771 2076
6de9cd9a
DN
2077 /* Add initializer. Make sure we keep the ranks sane. */
2078 if (sym->attr.dimension && init->rank == 0)
a9b43781
PT
2079 {
2080 mpz_t size;
2081 gfc_expr *array;
a9b43781
PT
2082 int n;
2083 if (sym->attr.flavor == FL_PARAMETER
2084 && init->expr_type == EXPR_CONSTANT
524af0d6 2085 && spec_size (sym->as, &size)
a9b43781
PT
2086 && mpz_cmp_si (size, 0) > 0)
2087 {
b7e75771
JD
2088 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2089 &init->where);
a9b43781 2090 for (n = 0; n < (int)mpz_get_si (size); n++)
b7e75771
JD
2091 gfc_constructor_append_expr (&array->value.constructor,
2092 n == 0
2093 ? init
2094 : gfc_copy_expr (init),
2095 &init->where);
f5acf0f2 2096
a9b43781
PT
2097 array->shape = gfc_get_shape (sym->as->rank);
2098 for (n = 0; n < sym->as->rank; n++)
2099 spec_dimen_size (sym->as, n, &array->shape[n]);
2100
2101 init = array;
2102 mpz_clear (size);
2103 }
2104 init->rank = sym->as->rank;
2105 }
6de9cd9a
DN
2106
2107 sym->value = init;
ef7236d2
DF
2108 if (sym->attr.save == SAVE_NONE)
2109 sym->attr.save = SAVE_IMPLICIT;
6de9cd9a
DN
2110 *initp = NULL;
2111 }
2112
524af0d6 2113 return true;
6de9cd9a
DN
2114}
2115
2116
2117/* Function called by variable_decl() that adds a name to a structure
2118 being built. */
2119
524af0d6 2120static bool
636dff67
SK
2121build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2122 gfc_array_spec **as)
6de9cd9a 2123{
f6288c24 2124 gfc_state_data *s;
6de9cd9a
DN
2125 gfc_component *c;
2126
619dd721 2127 /* F03:C438/C439. If the current symbol is of the same derived type that we're
6de9cd9a 2128 constructing, it must have the pointer attribute. */
619dd721 2129 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
bc21d315 2130 && current_ts.u.derived == gfc_current_block ()
6de9cd9a
DN
2131 && current_attr.pointer == 0)
2132 {
bf9f15ee
PT
2133 if (current_attr.allocatable
2134 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2135 "must have the POINTER attribute"))
2136 {
2137 return false;
2138 }
2139 else if (current_attr.allocatable == 0)
2140 {
9cbf8673
JW
2141 gfc_error ("Component at %C must have the POINTER attribute");
2142 return false;
2143 }
6de9cd9a 2144 }
9cbf8673
JW
2145
2146 /* F03:C437. */
2147 if (current_ts.type == BT_CLASS
2148 && !(current_attr.pointer || current_attr.allocatable))
2149 {
2150 gfc_error ("Component %qs with CLASS at %C must be allocatable "
2151 "or pointer", name);
2152 return false;
bf9f15ee 2153 }
6de9cd9a 2154
636dff67 2155 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
6de9cd9a
DN
2156 {
2157 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2158 {
2159 gfc_error ("Array component of structure at %C must have explicit "
2160 "or deferred shape");
524af0d6 2161 return false;
6de9cd9a
DN
2162 }
2163 }
2164
f6288c24
FR
2165 /* If we are in a nested union/map definition, gfc_add_component will not
2166 properly find repeated components because:
6442a6f4 2167 (i) gfc_add_component does a flat search, where components of unions
f6288c24
FR
2168 and maps are implicity chained so nested components may conflict.
2169 (ii) Unions and maps are not linked as components of their parent
2170 structures until after they are parsed.
2171 For (i) we use gfc_find_component which searches recursively, and for (ii)
2172 we search each block directly from the parse stack until we find the top
2173 level structure. */
2174
2175 s = gfc_state_stack;
2176 if (s->state == COMP_UNION || s->state == COMP_MAP)
2177 {
2178 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2179 {
2180 c = gfc_find_component (s->sym, name, true, true, NULL);
2181 if (c != NULL)
2182 {
2f029c08 2183 gfc_error_now ("Component %qs at %C already declared at %L",
f6288c24
FR
2184 name, &c->loc);
2185 return false;
2186 }
2187 /* Break after we've searched the entire chain. */
2188 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2189 break;
2190 s = s->previous;
2191 }
2192 }
2193
524af0d6
JB
2194 if (!gfc_add_component (gfc_current_block(), name, &c))
2195 return false;
6de9cd9a
DN
2196
2197 c->ts = current_ts;
bc21d315
JW
2198 if (c->ts.type == BT_CHARACTER)
2199 c->ts.u.cl = cl;
5bab4c96
PT
2200
2201 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
276515e6
PT
2202 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2203 && saved_kind_expr != NULL)
5bab4c96
PT
2204 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2205
d4b7d0f0 2206 c->attr = current_attr;
6de9cd9a
DN
2207
2208 c->initializer = *init;
2209 *init = NULL;
2210
2211 c->as = *as;
2212 if (c->as != NULL)
be59db2d
TB
2213 {
2214 if (c->as->corank)
2215 c->attr.codimension = 1;
2216 if (c->as->rank)
2217 c->attr.dimension = 1;
2218 }
6de9cd9a
DN
2219 *as = NULL;
2220
7fc61626 2221 gfc_apply_init (&c->ts, &c->attr, c->initializer);
28d08315 2222
6de9cd9a 2223 /* Check array components. */
d4b7d0f0 2224 if (!c->attr.dimension)
2e23972e 2225 goto scalar;
6de9cd9a 2226
d4b7d0f0 2227 if (c->attr.pointer)
6de9cd9a
DN
2228 {
2229 if (c->as->type != AS_DEFERRED)
2230 {
5046aff5
PT
2231 gfc_error ("Pointer array component of structure at %C must have a "
2232 "deferred shape");
a4f15a7d 2233 return false;
5046aff5
PT
2234 }
2235 }
d4b7d0f0 2236 else if (c->attr.allocatable)
5046aff5
PT
2237 {
2238 if (c->as->type != AS_DEFERRED)
2239 {
2240 gfc_error ("Allocatable component of structure at %C must have a "
2241 "deferred shape");
a4f15a7d 2242 return false;
6de9cd9a
DN
2243 }
2244 }
2245 else
2246 {
2247 if (c->as->type != AS_EXPLICIT)
2248 {
636dff67
SK
2249 gfc_error ("Array component of structure at %C must have an "
2250 "explicit shape");
a4f15a7d 2251 return false;
6de9cd9a
DN
2252 }
2253 }
2254
2e23972e
JW
2255scalar:
2256 if (c->ts.type == BT_CLASS)
a4f15a7d 2257 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
ea59b186 2258
5bab4c96
PT
2259 if (c->attr.pdt_kind || c->attr.pdt_len)
2260 {
2261 gfc_symbol *sym;
2262 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2263 0, &sym);
2264 if (sym == NULL)
2265 {
2266 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2267 "in the type parameter name list at %L",
2268 c->name, &gfc_current_block ()->declared_at);
2269 return false;
2270 }
2271 sym->ts = c->ts;
2272 sym->attr.pdt_kind = c->attr.pdt_kind;
2273 sym->attr.pdt_len = c->attr.pdt_len;
2274 if (c->initializer)
2275 sym->value = gfc_copy_expr (c->initializer);
2276 sym->attr.flavor = FL_VARIABLE;
2277 }
2278
2279 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2280 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2281 && decl_type_param_list)
2282 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2283
a4f15a7d 2284 return true;
6de9cd9a
DN
2285}
2286
2287
2288/* Match a 'NULL()', and possibly take care of some side effects. */
2289
2290match
636dff67 2291gfc_match_null (gfc_expr **result)
6de9cd9a
DN
2292{
2293 gfc_symbol *sym;
576f6da6 2294 match m, m2 = MATCH_NO;
6de9cd9a 2295
576f6da6
TB
2296 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2297 return MATCH_ERROR;
2298
2299 if (m == MATCH_NO)
2300 {
2301 locus old_loc;
2302 char name[GFC_MAX_SYMBOL_LEN + 1];
2303
94241120 2304 if ((m2 = gfc_match (" null (")) != MATCH_YES)
576f6da6
TB
2305 return m2;
2306
2307 old_loc = gfc_current_locus;
2308 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2309 return MATCH_ERROR;
2310 if (m2 != MATCH_YES
2311 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2312 return MATCH_ERROR;
2313 if (m2 == MATCH_NO)
2314 {
2315 gfc_current_locus = old_loc;
2316 return MATCH_NO;
2317 }
2318 }
6de9cd9a
DN
2319
2320 /* The NULL symbol now has to be/become an intrinsic function. */
2321 if (gfc_get_symbol ("null", NULL, &sym))
2322 {
2323 gfc_error ("NULL() initialization at %C is ambiguous");
2324 return MATCH_ERROR;
2325 }
2326
2327 gfc_intrinsic_symbol (sym);
2328
2329 if (sym->attr.proc != PROC_INTRINSIC
07416986 2330 && !(sym->attr.use_assoc && sym->attr.intrinsic)
524af0d6
JB
2331 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2332 || !gfc_add_function (&sym->attr, sym->name, NULL)))
6de9cd9a
DN
2333 return MATCH_ERROR;
2334
b7e75771 2335 *result = gfc_get_null_expr (&gfc_current_locus);
6de9cd9a 2336
576f6da6
TB
2337 /* Invalid per F2008, C512. */
2338 if (m2 == MATCH_YES)
2339 {
2340 gfc_error ("NULL() initialization at %C may not have MOLD");
2341 return MATCH_ERROR;
2342 }
2343
6de9cd9a
DN
2344 return MATCH_YES;
2345}
2346
2347
80f95228
JW
2348/* Match the initialization expr for a data pointer or procedure pointer. */
2349
2350static match
2351match_pointer_init (gfc_expr **init, int procptr)
2352{
2353 match m;
2354
f6288c24 2355 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
80f95228
JW
2356 {
2357 gfc_error ("Initialization of pointer at %C is not allowed in "
2358 "a PURE procedure");
2359 return MATCH_ERROR;
2360 }
ccd7751b 2361 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
80f95228 2362
eea58adb 2363 /* Match NULL() initialization. */
80f95228
JW
2364 m = gfc_match_null (init);
2365 if (m != MATCH_NO)
2366 return m;
2367
2368 /* Match non-NULL initialization. */
837c4b78 2369 gfc_matching_ptr_assignment = !procptr;
80f95228
JW
2370 gfc_matching_procptr_assignment = procptr;
2371 m = gfc_match_rvalue (init);
837c4b78 2372 gfc_matching_ptr_assignment = 0;
80f95228
JW
2373 gfc_matching_procptr_assignment = 0;
2374 if (m == MATCH_ERROR)
2375 return MATCH_ERROR;
2376 else if (m == MATCH_NO)
2377 {
2378 gfc_error ("Error in pointer initialization at %C");
2379 return MATCH_ERROR;
2380 }
2381
dc9a54fa
JW
2382 if (!procptr && !gfc_resolve_expr (*init))
2383 return MATCH_ERROR;
f5acf0f2 2384
524af0d6
JB
2385 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2386 "initialization at %C"))
80f95228
JW
2387 return MATCH_ERROR;
2388
2389 return MATCH_YES;
2390}
2391
2392
524af0d6 2393static bool
bb9de0c4
JW
2394check_function_name (char *name)
2395{
2396 /* In functions that have a RESULT variable defined, the function name always
2397 refers to function calls. Therefore, the name is not allowed to appear in
2398 specification statements. When checking this, be careful about
2399 'hidden' procedure pointer results ('ppr@'). */
2400
2401 if (gfc_current_state () == COMP_FUNCTION)
2402 {
2403 gfc_symbol *block = gfc_current_block ();
2404 if (block && block->result && block->result != block
2405 && strcmp (block->result->name, "ppr@") != 0
2406 && strcmp (block->name, name) == 0)
2407 {
ba77f7ba
SK
2408 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2409 "from appearing in a specification statement",
2410 block->result->name, &block->result->declared_at, name);
524af0d6 2411 return false;
bb9de0c4
JW
2412 }
2413 }
2414
524af0d6 2415 return true;
bb9de0c4
JW
2416}
2417
2418
6de9cd9a
DN
2419/* Match a variable name with an optional initializer. When this
2420 subroutine is called, a variable is expected to be parsed next.
2421 Depending on what is happening at the moment, updates either the
2422 symbol table or the current interface. */
2423
2424static match
949d5b72 2425variable_decl (int elem)
6de9cd9a
DN
2426{
2427 char name[GFC_MAX_SYMBOL_LEN + 1];
6f855a26 2428 static unsigned int fill_id = 0;
6de9cd9a
DN
2429 gfc_expr *initializer, *char_len;
2430 gfc_array_spec *as;
83d890b9 2431 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
6de9cd9a 2432 gfc_charlen *cl;
e69afb29 2433 bool cl_deferred;
6de9cd9a
DN
2434 locus var_locus;
2435 match m;
524af0d6 2436 bool t;
83d890b9 2437 gfc_symbol *sym;
6de9cd9a
DN
2438
2439 initializer = NULL;
2440 as = NULL;
83d890b9 2441 cp_as = NULL;
6de9cd9a
DN
2442
2443 /* When we get here, we've just matched a list of attributes and
2444 maybe a type and a double colon. The next thing we expect to see
2445 is the name of the symbol. */
6f855a26
FR
2446
2447 /* If we are parsing a structure with legacy support, we allow the symbol
2448 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2449 m = MATCH_NO;
2450 gfc_gobble_whitespace ();
2451 if (gfc_peek_ascii_char () == '%')
2452 {
2453 gfc_next_ascii_char ();
2454 m = gfc_match ("fill");
2455 }
2456
6de9cd9a 2457 if (m != MATCH_YES)
6f855a26
FR
2458 {
2459 m = gfc_match_name (name);
2460 if (m != MATCH_YES)
2461 goto cleanup;
2462 }
2463
2464 else
2465 {
2466 m = MATCH_ERROR;
2467 if (gfc_current_state () != COMP_STRUCTURE)
2468 {
2469 if (flag_dec_structure)
2470 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2471 else
2472 gfc_error ("%qs at %C is a DEC extension, enable with "
2473 "%<-fdec-structure%>", "%FILL");
2474 goto cleanup;
2475 }
2476
2477 if (attr_seen)
2478 {
2479 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2480 goto cleanup;
2481 }
2482
2483 /* %FILL components are given invalid fortran names. */
2484 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
6f855a26 2485 }
6de9cd9a 2486
63645982 2487 var_locus = gfc_current_locus;
6de9cd9a
DN
2488
2489 /* Now we could see the optional array spec. or character length. */
be59db2d 2490 m = gfc_match_array_spec (&as, true, true);
11126dc0 2491 if (m == MATCH_ERROR)
6de9cd9a 2492 goto cleanup;
25d8f0a2 2493
6de9cd9a
DN
2494 if (m == MATCH_NO)
2495 as = gfc_copy_array_spec (current_as);
63fbf586 2496 else if (current_as
524af0d6 2497 && !merge_array_spec (current_as, as, true))
63fbf586
TB
2498 {
2499 m = MATCH_ERROR;
2500 goto cleanup;
2501 }
6de9cd9a 2502
c61819ff 2503 if (flag_cray_pointer)
11126dc0
AL
2504 cp_as = gfc_copy_array_spec (as);
2505
f5ca06e6
DK
2506 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2507 determine (and check) whether it can be implied-shape. If it
67914693 2508 was parsed as assumed-size, change it because PARAMETERs cannot
09ef33c1
SK
2509 be assumed-size.
2510
2511 An explicit-shape-array cannot appear under several conditions.
2512 That check is done here as well. */
f5ca06e6
DK
2513 if (as)
2514 {
2515 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2516 {
2517 m = MATCH_ERROR;
1fe61adf 2518 gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
f5ca06e6
DK
2519 name, &var_locus);
2520 goto cleanup;
2521 }
2522
2523 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2524 && current_attr.flavor == FL_PARAMETER)
2525 as->type = AS_IMPLIED_SHAPE;
2526
2527 if (as->type == AS_IMPLIED_SHAPE
70112e2a 2528 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
524af0d6 2529 &var_locus))
f5ca06e6
DK
2530 {
2531 m = MATCH_ERROR;
2532 goto cleanup;
2533 }
09ef33c1
SK
2534
2535 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2536 constant expressions shall appear only in a subprogram, derived
2537 type definition, BLOCK construct, or interface body. */
2538 if (as->type == AS_EXPLICIT
2539 && gfc_current_state () != COMP_BLOCK
2540 && gfc_current_state () != COMP_DERIVED
2541 && gfc_current_state () != COMP_FUNCTION
2542 && gfc_current_state () != COMP_INTERFACE
2543 && gfc_current_state () != COMP_SUBROUTINE)
2544 {
2545 gfc_expr *e;
2546 bool not_constant = false;
2547
2548 for (int i = 0; i < as->rank; i++)
2549 {
2550 e = gfc_copy_expr (as->lower[i]);
2551 gfc_resolve_expr (e);
2552 gfc_simplify_expr (e, 0);
2553 if (e && (e->expr_type != EXPR_CONSTANT))
2554 {
2555 not_constant = true;
2556 break;
2557 }
2558 gfc_free_expr (e);
2559
2560 e = gfc_copy_expr (as->upper[i]);
2561 gfc_resolve_expr (e);
2562 gfc_simplify_expr (e, 0);
2563 if (e && (e->expr_type != EXPR_CONSTANT))
2564 {
2565 not_constant = true;
2566 break;
2567 }
2568 gfc_free_expr (e);
2569 }
2570
2571 if (not_constant)
5b9a3332 2572 {
09ef33c1
SK
2573 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2574 m = MATCH_ERROR;
2575 goto cleanup;
2576 }
2577 }
078c5aff
TK
2578 if (as->type == AS_EXPLICIT)
2579 {
2580 for (int i = 0; i < as->rank; i++)
2581 {
2582 gfc_expr *e, *n;
2583 e = as->lower[i];
2584 if (e->expr_type != EXPR_CONSTANT)
2585 {
2586 n = gfc_copy_expr (e);
2587 gfc_simplify_expr (n, 1);
2588 if (n->expr_type == EXPR_CONSTANT)
2589 gfc_replace_expr (e, n);
2590 else
2591 gfc_free_expr (n);
2592 }
2593 e = as->upper[i];
2594 if (e->expr_type != EXPR_CONSTANT)
2595 {
2596 n = gfc_copy_expr (e);
2597 gfc_simplify_expr (n, 1);
2598 if (n->expr_type == EXPR_CONSTANT)
2599 gfc_replace_expr (e, n);
2600 else
2601 gfc_free_expr (n);
2602 }
2603 }
2604 }
f5ca06e6
DK
2605 }
2606
6de9cd9a
DN
2607 char_len = NULL;
2608 cl = NULL;
e69afb29 2609 cl_deferred = false;
6de9cd9a
DN
2610
2611 if (current_ts.type == BT_CHARACTER)
2612 {
2767f2cc 2613 switch (match_char_length (&char_len, &cl_deferred, false))
6de9cd9a
DN
2614 {
2615 case MATCH_YES:
b76e28c6 2616 cl = gfc_new_charlen (gfc_current_ns, NULL);
6de9cd9a
DN
2617
2618 cl->length = char_len;
2619 break;
2620
949d5b72 2621 /* Non-constant lengths need to be copied after the first
9b21a380 2622 element. Also copy assumed lengths. */
6de9cd9a 2623 case MATCH_NO:
9b21a380 2624 if (elem > 1
bc21d315
JW
2625 && (current_ts.u.cl->length == NULL
2626 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
949d5b72 2627 {
b76e28c6 2628 cl = gfc_new_charlen (gfc_current_ns, NULL);
bc21d315 2629 cl->length = gfc_copy_expr (current_ts.u.cl->length);
949d5b72
PT
2630 }
2631 else
bc21d315 2632 cl = current_ts.u.cl;
949d5b72 2633
e69afb29
SK
2634 cl_deferred = current_ts.deferred;
2635
6de9cd9a
DN
2636 break;
2637
2638 case MATCH_ERROR:
2639 goto cleanup;
2640 }
2641 }
2642
4668d6f9
PT
2643 /* The dummy arguments and result of the abreviated form of MODULE
2644 PROCEDUREs, used in SUBMODULES should not be redefined. */
2645 if (gfc_current_ns->proc_name
2646 && gfc_current_ns->proc_name->abr_modproc_decl)
2647 {
2648 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2649 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2650 {
2651 m = MATCH_ERROR;
811582ec 2652 gfc_error ("%qs at %C is a redefinition of the declaration "
4668d6f9 2653 "in the corresponding interface for MODULE "
811582ec 2654 "PROCEDURE %qs", sym->name,
4668d6f9
PT
2655 gfc_current_ns->proc_name->name);
2656 goto cleanup;
2657 }
2658 }
2659
6f855a26 2660 /* %FILL components may not have initializers. */
2eb3745a 2661 if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
6f855a26
FR
2662 {
2663 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2664 m = MATCH_ERROR;
2665 goto cleanup;
2666 }
2667
83d890b9 2668 /* If this symbol has already shown up in a Cray Pointer declaration,
88f7d6fb 2669 and this is not a component declaration,
66e4ab31 2670 then we want to set the type & bail out. */
f6288c24 2671 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
83d890b9
AL
2672 {
2673 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2674 if (sym != NULL && sym->attr.cray_pointee)
2675 {
83d890b9 2676 m = MATCH_YES;
5b9a3332
PT
2677 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2678 {
2679 m = MATCH_ERROR;
2680 goto cleanup;
2681 }
f5acf0f2 2682
83d890b9
AL
2683 /* Check to see if we have an array specification. */
2684 if (cp_as != NULL)
2685 {
2686 if (sym->as != NULL)
2687 {
e25a0da3 2688 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
2689 gfc_free_array_spec (cp_as);
2690 m = MATCH_ERROR;
2691 goto cleanup;
2692 }
2693 else
2694 {
524af0d6 2695 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1fe61adf 2696 gfc_internal_error ("Cannot set pointee array spec.");
d51347f9 2697
83d890b9 2698 /* Fix the array spec. */
d51347f9 2699 m = gfc_mod_pointee_as (sym->as);
83d890b9
AL
2700 if (m == MATCH_ERROR)
2701 goto cleanup;
2702 }
d51347f9 2703 }
83d890b9
AL
2704 goto cleanup;
2705 }
2706 else
2707 {
2708 gfc_free_array_spec (cp_as);
2709 }
2710 }
d51347f9 2711
3070bab4
JW
2712 /* Procedure pointer as function result. */
2713 if (gfc_current_state () == COMP_FUNCTION
2714 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2715 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2716 strcpy (name, "ppr@");
2717
2718 if (gfc_current_state () == COMP_FUNCTION
2719 && strcmp (name, gfc_current_block ()->name) == 0
2720 && gfc_current_block ()->result
2721 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2722 strcpy (name, "ppr@");
d51347f9 2723
6de9cd9a
DN
2724 /* OK, we've successfully matched the declaration. Now put the
2725 symbol in the current namespace, because it might be used in the
69de3b83 2726 optional initialization expression for this symbol, e.g. this is
6de9cd9a
DN
2727 perfectly legal:
2728
2729 integer, parameter :: i = huge(i)
2730
2731 This is only true for parameters or variables of a basic type.
2732 For components of derived types, it is not true, so we don't
2733 create a symbol for those yet. If we fail to create the symbol,
2734 bail out. */
f6288c24 2735 if (!gfc_comp_struct (gfc_current_state ())
524af0d6 2736 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
6de9cd9a 2737 {
72af9f0b
PT
2738 m = MATCH_ERROR;
2739 goto cleanup;
2740 }
2741
524af0d6 2742 if (!check_function_name (name))
6de9cd9a 2743 {
6de9cd9a
DN
2744 m = MATCH_ERROR;
2745 goto cleanup;
2746 }
2747
294fbfc8
TS
2748 /* We allow old-style initializations of the form
2749 integer i /2/, j(4) /3*3, 1/
2750 (if no colon has been seen). These are different from data
2751 statements in that initializers are only allowed to apply to the
2752 variable immediately preceding, i.e.
2753 integer i, j /1, 2/
2754 is not allowed. Therefore we have to do some work manually, that
75d17889 2755 could otherwise be left to the matchers for DATA statements. */
294fbfc8
TS
2756
2757 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2758 {
524af0d6
JB
2759 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2760 "initialization at %C"))
294fbfc8 2761 return MATCH_ERROR;
f6288c24
FR
2762
2763 /* Allow old style initializations for components of STRUCTUREs and MAPs
2764 but not components of derived types. */
b18f1efc
JJ
2765 else if (gfc_current_state () == COMP_DERIVED)
2766 {
2767 gfc_error ("Invalid old style initialization for derived type "
2768 "component at %C");
2769 m = MATCH_ERROR;
2770 goto cleanup;
2771 }
f5acf0f2 2772
f6288c24
FR
2773 /* For structure components, read the initializer as a special
2774 expression and let the rest of this function apply the initializer
2775 as usual. */
2776 else if (gfc_comp_struct (gfc_current_state ()))
2777 {
2778 m = match_clist_expr (&initializer, &current_ts, as);
2779 if (m == MATCH_NO)
2780 gfc_error ("Syntax error in old style initialization of %s at %C",
2781 name);
2782 if (m != MATCH_YES)
2783 goto cleanup;
2784 }
2785
2786 /* Otherwise we treat the old style initialization just like a
2787 DATA declaration for the current variable. */
2788 else
2789 return match_old_style_init (name);
294fbfc8
TS
2790 }
2791
6de9cd9a
DN
2792 /* The double colon must be present in order to have initializers.
2793 Otherwise the statement is ambiguous with an assignment statement. */
2794 if (colon_seen)
2795 {
2796 if (gfc_match (" =>") == MATCH_YES)
2797 {
6de9cd9a
DN
2798 if (!current_attr.pointer)
2799 {
2800 gfc_error ("Initialization at %C isn't for a pointer variable");
2801 m = MATCH_ERROR;
2802 goto cleanup;
2803 }
2804
80f95228 2805 m = match_pointer_init (&initializer, 0);
6de9cd9a
DN
2806 if (m != MATCH_YES)
2807 goto cleanup;
2cf4aa79
SK
2808
2809 /* The target of a pointer initialization must have the SAVE
2810 attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
2811 is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
2812 if (initializer->expr_type == EXPR_VARIABLE
2813 && initializer->symtree->n.sym->attr.save == SAVE_NONE
2814 && (gfc_current_state () == COMP_PROGRAM
2815 || gfc_current_state () == COMP_MODULE
2816 || gfc_current_state () == COMP_SUBMODULE))
2817 initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
6de9cd9a
DN
2818 }
2819 else if (gfc_match_char ('=') == MATCH_YES)
2820 {
2821 if (current_attr.pointer)
2822 {
a4d9b221
TB
2823 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2824 "not %<=%>");
6de9cd9a
DN
2825 m = MATCH_ERROR;
2826 goto cleanup;
2827 }
2828
2829 m = gfc_match_init_expr (&initializer);
2830 if (m == MATCH_NO)
2831 {
2832 gfc_error ("Expected an initialization expression at %C");
2833 m = MATCH_ERROR;
2834 }
2835
ade20620 2836 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
f6288c24 2837 && !gfc_comp_struct (gfc_state_stack->state))
6de9cd9a 2838 {
636dff67
SK
2839 gfc_error ("Initialization of variable at %C is not allowed in "
2840 "a PURE procedure");
6de9cd9a
DN
2841 m = MATCH_ERROR;
2842 }
2843
ccd7751b 2844 if (current_attr.flavor != FL_PARAMETER
f6288c24 2845 && !gfc_comp_struct (gfc_state_stack->state))
ccd7751b
TB
2846 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2847
6de9cd9a
DN
2848 if (m != MATCH_YES)
2849 goto cleanup;
2850 }
cb44ab82
VL
2851 }
2852
5046aff5 2853 if (initializer != NULL && current_attr.allocatable
f6288c24 2854 && gfc_comp_struct (gfc_current_state ()))
5046aff5 2855 {
636dff67
SK
2856 gfc_error ("Initialization of allocatable component at %C is not "
2857 "allowed");
5046aff5
PT
2858 m = MATCH_ERROR;
2859 goto cleanup;
2860 }
2861
18a4e7e3
PT
2862 if (gfc_current_state () == COMP_DERIVED
2863 && gfc_current_block ()->attr.pdt_template)
2864 {
2865 gfc_symbol *param;
2866 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2867 0, &param);
2868 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2869 {
2870 gfc_error ("The component with KIND or LEN attribute at %C does not "
2871 "not appear in the type parameter list at %L",
2872 &gfc_current_block ()->declared_at);
2873 m = MATCH_ERROR;
2874 goto cleanup;
2875 }
2876 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2877 {
2878 gfc_error ("The component at %C that appears in the type parameter "
2879 "list at %L has neither the KIND nor LEN attribute",
2880 &gfc_current_block ()->declared_at);
2881 m = MATCH_ERROR;
2882 goto cleanup;
2883 }
2884 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2885 {
2886 gfc_error ("The component at %C which is a type parameter must be "
2887 "a scalar");
2888 m = MATCH_ERROR;
2889 goto cleanup;
2890 }
2891 else if (param && initializer)
2892 param->value = gfc_copy_expr (initializer);
2893 }
2894
e310b381 2895 /* Before adding a possible initilizer, do a simple check for compatibility
26ca4e05 2896 of lhs and rhs types. Assigning a REAL value to a derived type is not a
e310b381
SK
2897 good thing. */
2898 if (current_ts.type == BT_DERIVED && initializer
2899 && (gfc_numeric_ts (&initializer->ts)
2900 || initializer->ts.type == BT_LOGICAL
2901 || initializer->ts.type == BT_CHARACTER))
2902 {
26ca4e05 2903 gfc_error ("Incompatible initialization between a derived type "
e310b381
SK
2904 "entity and an entity with %qs type at %C",
2905 gfc_typename (&initializer->ts));
2906 m = MATCH_ERROR;
2907 goto cleanup;
2908 }
2909
2910
54b4ba60 2911 /* Add the initializer. Note that it is fine if initializer is
6de9cd9a
DN
2912 NULL here, because we sometimes also need to check if a
2913 declaration *must* have an initialization expression. */
f6288c24 2914 if (!gfc_comp_struct (gfc_current_state ()))
6de9cd9a
DN
2915 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2916 else
54b4ba60 2917 {
5046aff5 2918 if (current_ts.type == BT_DERIVED
636dff67 2919 && !current_attr.pointer && !initializer)
54b4ba60
PB
2920 initializer = gfc_default_initializer (&current_ts);
2921 t = build_struct (name, cl, &initializer, &as);
f6288c24
FR
2922
2923 /* If we match a nested structure definition we expect to see the
2924 * body even if the variable declarations blow up, so we need to keep
2925 * the structure declaration around. */
2926 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
2927 gfc_commit_symbol (gfc_new_block);
54b4ba60 2928 }
6de9cd9a 2929
524af0d6 2930 m = (t) ? MATCH_YES : MATCH_ERROR;
6de9cd9a
DN
2931
2932cleanup:
2933 /* Free stuff up and return. */
2934 gfc_free_expr (initializer);
2935 gfc_free_array_spec (as);
2936
2937 return m;
2938}
2939
2940
b2b81a3f
BM
2941/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2942 This assumes that the byte size is equal to the kind number for
2943 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
6de9cd9a
DN
2944
2945match
636dff67 2946gfc_match_old_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
2947{
2948 match m;
5cf54585 2949 int original_kind;
6de9cd9a
DN
2950
2951 if (gfc_match_char ('*') != MATCH_YES)
2952 return MATCH_NO;
2953
5cf54585 2954 m = gfc_match_small_literal_int (&ts->kind, NULL);
6de9cd9a
DN
2955 if (m != MATCH_YES)
2956 return MATCH_ERROR;
2957
e45b3c75
ES
2958 original_kind = ts->kind;
2959
6de9cd9a 2960 /* Massage the kind numbers for complex types. */
e45b3c75
ES
2961 if (ts->type == BT_COMPLEX)
2962 {
2963 if (ts->kind % 2)
636dff67
SK
2964 {
2965 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2966 gfc_basic_typename (ts->type), original_kind);
2967 return MATCH_ERROR;
2968 }
e45b3c75 2969 ts->kind /= 2;
f4347334
ZG
2970
2971 }
2972
203c7ebf 2973 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
f4347334
ZG
2974 ts->kind = 8;
2975
2976 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2977 {
2978 if (ts->kind == 4)
2979 {
203c7ebf 2980 if (flag_real4_kind == 8)
f4347334 2981 ts->kind = 8;
203c7ebf 2982 if (flag_real4_kind == 10)
f4347334 2983 ts->kind = 10;
203c7ebf 2984 if (flag_real4_kind == 16)
f4347334
ZG
2985 ts->kind = 16;
2986 }
2987
2988 if (ts->kind == 8)
2989 {
203c7ebf 2990 if (flag_real8_kind == 4)
f4347334 2991 ts->kind = 4;
203c7ebf 2992 if (flag_real8_kind == 10)
f4347334 2993 ts->kind = 10;
203c7ebf 2994 if (flag_real8_kind == 16)
f4347334
ZG
2995 ts->kind = 16;
2996 }
e45b3c75 2997 }
6de9cd9a 2998
e7a2d5fb 2999 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a 3000 {
e45b3c75 3001 gfc_error ("Old-style type declaration %s*%d not supported at %C",
636dff67 3002 gfc_basic_typename (ts->type), original_kind);
6de9cd9a
DN
3003 return MATCH_ERROR;
3004 }
3005
70112e2a
PT
3006 if (!gfc_notify_std (GFC_STD_GNU,
3007 "Nonstandard type declaration %s*%d at %C",
524af0d6 3008 gfc_basic_typename(ts->type), original_kind))
df8652dc
SK
3009 return MATCH_ERROR;
3010
6de9cd9a
DN
3011 return MATCH_YES;
3012}
3013
3014
3015/* Match a kind specification. Since kinds are generally optional, we
3016 usually return MATCH_NO if something goes wrong. If a "kind="
3017 string is found, then we know we have an error. */
3018
3019match
e2d29968 3020gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
6de9cd9a 3021{
e2d29968 3022 locus where, loc;
6de9cd9a
DN
3023 gfc_expr *e;
3024 match m, n;
96ee3a4a 3025 char c;
6de9cd9a
DN
3026
3027 m = MATCH_NO;
e2d29968 3028 n = MATCH_YES;
6de9cd9a 3029 e = NULL;
5bab4c96 3030 saved_kind_expr = NULL;
6de9cd9a 3031
e2d29968
PT
3032 where = loc = gfc_current_locus;
3033
3034 if (kind_expr_only)
3035 goto kind_expr;
6de9cd9a
DN
3036
3037 if (gfc_match_char ('(') == MATCH_NO)
3038 return MATCH_NO;
3039
3040 /* Also gobbles optional text. */
3041 if (gfc_match (" kind = ") == MATCH_YES)
3042 m = MATCH_ERROR;
3043
e2d29968
PT
3044 loc = gfc_current_locus;
3045
3046kind_expr:
5bab4c96 3047
6de9cd9a 3048 n = gfc_match_init_expr (&e);
e2d29968 3049
5bab4c96
PT
3050 if (gfc_derived_parameter_expr (e))
3051 {
3052 ts->kind = 0;
3053 saved_kind_expr = gfc_copy_expr (e);
3054 goto close_brackets;
3055 }
3056
6de9cd9a 3057 if (n != MATCH_YES)
e2d29968 3058 {
1c8bcdf7 3059 if (gfc_matching_function)
e2d29968 3060 {
f5acf0f2 3061 /* The function kind expression might include use associated or
1c8bcdf7
PT
3062 imported parameters and try again after the specification
3063 expressions..... */
e2d29968
PT
3064 if (gfc_match_char (')') != MATCH_YES)
3065 {
3066 gfc_error ("Missing right parenthesis at %C");
3067 m = MATCH_ERROR;
3068 goto no_match;
3069 }
3070
3071 gfc_free_expr (e);
e2d29968
PT
3072 gfc_undo_symbols ();
3073 return MATCH_YES;
3074 }
3075 else
3076 {
3077 /* ....or else, the match is real. */
3078 if (n == MATCH_NO)
3079 gfc_error ("Expected initialization expression at %C");
3080 if (n != MATCH_YES)
3081 return MATCH_ERROR;
3082 }
3083 }
6de9cd9a
DN
3084
3085 if (e->rank != 0)
3086 {
3087 gfc_error ("Expected scalar initialization expression at %C");
3088 m = MATCH_ERROR;
3089 goto no_match;
3090 }
3091
51f03c6b 3092 if (gfc_extract_int (e, &ts->kind, 1))
6de9cd9a 3093 {
6de9cd9a
DN
3094 m = MATCH_ERROR;
3095 goto no_match;
3096 }
3097
a8b3b0b6
CR
3098 /* Before throwing away the expression, let's see if we had a
3099 C interoperable kind (and store the fact). */
3100 if (e->ts.is_c_interop == 1)
3101 {
eea58adb 3102 /* Mark this as C interoperable if being declared with one
a8b3b0b6
CR
3103 of the named constants from iso_c_binding. */
3104 ts->is_c_interop = e->ts.is_iso_c;
3105 ts->f90_type = e->ts.f90_type;
e655a6cc
TK
3106 if (e->symtree)
3107 ts->interop_kind = e->symtree->n.sym;
a8b3b0b6 3108 }
f5acf0f2 3109
6de9cd9a
DN
3110 gfc_free_expr (e);
3111 e = NULL;
3112
a8b3b0b6
CR
3113 /* Ignore errors to this point, if we've gotten here. This means
3114 we ignore the m=MATCH_ERROR from above. */
e7a2d5fb 3115 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a
DN
3116 {
3117 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3118 gfc_basic_typename (ts->type));
96ee3a4a
TB
3119 gfc_current_locus = where;
3120 return MATCH_ERROR;
6de9cd9a 3121 }
96ee3a4a 3122
2ec855f1
TB
3123 /* Warn if, e.g., c_int is used for a REAL variable, but not
3124 if, e.g., c_double is used for COMPLEX as the standard
3125 explicitly says that the kind type parameter for complex and real
3126 variable is the same, i.e. c_float == c_float_complex. */
3127 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3128 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3129 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
db30e21c 3130 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2be51762
TB
3131 "is %s", gfc_basic_typename (ts->f90_type), &where,
3132 gfc_basic_typename (ts->type));
2ec855f1 3133
5bab4c96
PT
3134close_brackets:
3135
96ee3a4a 3136 gfc_gobble_whitespace ();
8fc541d3
FXC
3137 if ((c = gfc_next_ascii_char ()) != ')'
3138 && (ts->type != BT_CHARACTER || c != ','))
6de9cd9a 3139 {
96ee3a4a
TB
3140 if (ts->type == BT_CHARACTER)
3141 gfc_error ("Missing right parenthesis or comma at %C");
3142 else
3143 gfc_error ("Missing right parenthesis at %C");
e2d29968 3144 m = MATCH_ERROR;
6de9cd9a 3145 }
a8b3b0b6
CR
3146 else
3147 /* All tests passed. */
3148 m = MATCH_YES;
6de9cd9a 3149
a8b3b0b6
CR
3150 if(m == MATCH_ERROR)
3151 gfc_current_locus = where;
f4347334 3152
203c7ebf 3153 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
f4347334
ZG
3154 ts->kind = 8;
3155
3156 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3157 {
3158 if (ts->kind == 4)
3159 {
203c7ebf 3160 if (flag_real4_kind == 8)
f4347334 3161 ts->kind = 8;
203c7ebf 3162 if (flag_real4_kind == 10)
f4347334 3163 ts->kind = 10;
203c7ebf 3164 if (flag_real4_kind == 16)
f4347334
ZG
3165 ts->kind = 16;
3166 }
3167
3168 if (ts->kind == 8)
3169 {
203c7ebf 3170 if (flag_real8_kind == 4)
f4347334 3171 ts->kind = 4;
203c7ebf 3172 if (flag_real8_kind == 10)
f4347334 3173 ts->kind = 10;
203c7ebf 3174 if (flag_real8_kind == 16)
f4347334
ZG
3175 ts->kind = 16;
3176 }
3177 }
3178
a8b3b0b6
CR
3179 /* Return what we know from the test(s). */
3180 return m;
6de9cd9a
DN
3181
3182no_match:
3183 gfc_free_expr (e);
63645982 3184 gfc_current_locus = where;
6de9cd9a
DN
3185 return m;
3186}
3187
3188
187de1ed
FXC
3189static match
3190match_char_kind (int * kind, int * is_iso_c)
3191{
3192 locus where;
3193 gfc_expr *e;
3194 match m, n;
51f03c6b 3195 bool fail;
187de1ed
FXC
3196
3197 m = MATCH_NO;
3198 e = NULL;
3199 where = gfc_current_locus;
3200
3201 n = gfc_match_init_expr (&e);
96ee3a4a 3202
1c8bcdf7 3203 if (n != MATCH_YES && gfc_matching_function)
96ee3a4a 3204 {
1c8bcdf7 3205 /* The expression might include use-associated or imported
f5acf0f2 3206 parameters and try again after the specification
1c8bcdf7 3207 expressions. */
96ee3a4a 3208 gfc_free_expr (e);
96ee3a4a
TB
3209 gfc_undo_symbols ();
3210 return MATCH_YES;
3211 }
3212
187de1ed
FXC
3213 if (n == MATCH_NO)
3214 gfc_error ("Expected initialization expression at %C");
3215 if (n != MATCH_YES)
3216 return MATCH_ERROR;
3217
3218 if (e->rank != 0)
3219 {
3220 gfc_error ("Expected scalar initialization expression at %C");
3221 m = MATCH_ERROR;
3222 goto no_match;
3223 }
3224
87f3a5cf
PT
3225 if (gfc_derived_parameter_expr (e))
3226 {
3227 saved_kind_expr = e;
3228 *kind = 0;
3229 return MATCH_YES;
3230 }
3231
51f03c6b 3232 fail = gfc_extract_int (e, kind, 1);
187de1ed 3233 *is_iso_c = e->ts.is_iso_c;
51f03c6b 3234 if (fail)
187de1ed 3235 {
187de1ed
FXC
3236 m = MATCH_ERROR;
3237 goto no_match;
3238 }
3239
3240 gfc_free_expr (e);
3241
3242 /* Ignore errors to this point, if we've gotten here. This means
3243 we ignore the m=MATCH_ERROR from above. */
3244 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3245 {
3246 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3247 m = MATCH_ERROR;
3248 }
3249 else
3250 /* All tests passed. */
3251 m = MATCH_YES;
3252
3253 if (m == MATCH_ERROR)
3254 gfc_current_locus = where;
f5acf0f2 3255
187de1ed
FXC
3256 /* Return what we know from the test(s). */
3257 return m;
3258
3259no_match:
3260 gfc_free_expr (e);
3261 gfc_current_locus = where;
3262 return m;
3263}
3264
8234e5e0 3265
6de9cd9a
DN
3266/* Match the various kind/length specifications in a CHARACTER
3267 declaration. We don't return MATCH_NO. */
3268
8234e5e0
SK
3269match
3270gfc_match_char_spec (gfc_typespec *ts)
6de9cd9a 3271{
187de1ed 3272 int kind, seen_length, is_iso_c;
6de9cd9a
DN
3273 gfc_charlen *cl;
3274 gfc_expr *len;
3275 match m;
e69afb29 3276 bool deferred;
187de1ed 3277
6de9cd9a
DN
3278 len = NULL;
3279 seen_length = 0;
187de1ed
FXC
3280 kind = 0;
3281 is_iso_c = 0;
e69afb29 3282 deferred = false;
6de9cd9a
DN
3283
3284 /* Try the old-style specification first. */
3285 old_char_selector = 0;
3286
2767f2cc 3287 m = match_char_length (&len, &deferred, true);
6de9cd9a
DN
3288 if (m != MATCH_NO)
3289 {
3290 if (m == MATCH_YES)
3291 old_char_selector = 1;
3292 seen_length = 1;
3293 goto done;
3294 }
3295
3296 m = gfc_match_char ('(');
3297 if (m != MATCH_YES)
3298 {
a8b3b0b6 3299 m = MATCH_YES; /* Character without length is a single char. */
6de9cd9a
DN
3300 goto done;
3301 }
3302
a8b3b0b6 3303 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
6de9cd9a
DN
3304 if (gfc_match (" kind =") == MATCH_YES)
3305 {
187de1ed 3306 m = match_char_kind (&kind, &is_iso_c);
f5acf0f2 3307
6de9cd9a
DN
3308 if (m == MATCH_ERROR)
3309 goto done;
3310 if (m == MATCH_NO)
3311 goto syntax;
3312
3313 if (gfc_match (" , len =") == MATCH_NO)
3314 goto rparen;
3315
e69afb29 3316 m = char_len_param_value (&len, &deferred);
6de9cd9a
DN
3317 if (m == MATCH_NO)
3318 goto syntax;
3319 if (m == MATCH_ERROR)
3320 goto done;
3321 seen_length = 1;
3322
3323 goto rparen;
3324 }
3325
66e4ab31 3326 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
6de9cd9a
DN
3327 if (gfc_match (" len =") == MATCH_YES)
3328 {
e69afb29 3329 m = char_len_param_value (&len, &deferred);
6de9cd9a
DN
3330 if (m == MATCH_NO)
3331 goto syntax;
3332 if (m == MATCH_ERROR)
3333 goto done;
3334 seen_length = 1;
3335
3336 if (gfc_match_char (')') == MATCH_YES)
3337 goto done;
3338
3339 if (gfc_match (" , kind =") != MATCH_YES)
3340 goto syntax;
3341
187de1ed
FXC
3342 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3343 goto done;
6de9cd9a
DN
3344
3345 goto rparen;
3346 }
3347
66e4ab31 3348 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
e69afb29 3349 m = char_len_param_value (&len, &deferred);
6de9cd9a
DN
3350 if (m == MATCH_NO)
3351 goto syntax;
3352 if (m == MATCH_ERROR)
3353 goto done;
3354 seen_length = 1;
3355
3356 m = gfc_match_char (')');
3357 if (m == MATCH_YES)
3358 goto done;
3359
3360 if (gfc_match_char (',') != MATCH_YES)
3361 goto syntax;
3362
a8b3b0b6 3363 gfc_match (" kind ="); /* Gobble optional text. */
6de9cd9a 3364
187de1ed 3365 m = match_char_kind (&kind, &is_iso_c);
6de9cd9a
DN
3366 if (m == MATCH_ERROR)
3367 goto done;
3368 if (m == MATCH_NO)
3369 goto syntax;
3370
3371rparen:
3372 /* Require a right-paren at this point. */
3373 m = gfc_match_char (')');
3374 if (m == MATCH_YES)
3375 goto done;
3376
3377syntax:
3378 gfc_error ("Syntax error in CHARACTER declaration at %C");
3379 m = MATCH_ERROR;
16f8ffc8
JD
3380 gfc_free_expr (len);
3381 return m;
6de9cd9a
DN
3382
3383done:
a99d95a2
PT
3384 /* Deal with character functions after USE and IMPORT statements. */
3385 if (gfc_matching_function)
1c8bcdf7 3386 {
a99d95a2 3387 gfc_free_expr (len);
1c8bcdf7
PT
3388 gfc_undo_symbols ();
3389 return MATCH_YES;
3390 }
3391
6de9cd9a
DN
3392 if (m != MATCH_YES)
3393 {
3394 gfc_free_expr (len);
3395 return m;
3396 }
3397
3398 /* Do some final massaging of the length values. */
b76e28c6 3399 cl = gfc_new_charlen (gfc_current_ns, NULL);
6de9cd9a
DN
3400
3401 if (seen_length == 0)
f622221a 3402 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
6de9cd9a 3403 else
00df7c36 3404 {
3cf89a7b
SK
3405 /* If gfortran ends up here, then len may be reducible to a constant.
3406 Try to do that here. If it does not reduce, simply assign len to
3407 charlen. A complication occurs with user-defined generic functions,
3408 which are not resolved. Use a private namespace to deal with
3409 generic functions. */
3410
00df7c36
SK
3411 if (len && len->expr_type != EXPR_CONSTANT)
3412 {
3cf89a7b 3413 gfc_namespace *old_ns;
00df7c36 3414 gfc_expr *e;
3cf89a7b
SK
3415
3416 old_ns = gfc_current_ns;
3417 gfc_current_ns = gfc_get_namespace (NULL, 0);
3418
00df7c36
SK
3419 e = gfc_copy_expr (len);
3420 gfc_reduce_init_expr (e);
3421 if (e->expr_type == EXPR_CONSTANT)
58da192e
SK
3422 {
3423 gfc_replace_expr (len, e);
3424 if (mpz_cmp_si (len->value.integer, 0) < 0)
3425 mpz_set_ui (len->value.integer, 0);
3426 }
00df7c36
SK
3427 else
3428 gfc_free_expr (e);
3cf89a7b
SK
3429
3430 gfc_free_namespace (gfc_current_ns);
3431 gfc_current_ns = old_ns;
00df7c36 3432 }
3cf89a7b
SK
3433
3434 cl->length = len;
00df7c36 3435 }
6de9cd9a 3436
bc21d315 3437 ts->u.cl = cl;
187de1ed 3438 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
e69afb29 3439 ts->deferred = deferred;
6de9cd9a 3440
eea58adb 3441 /* We have to know if it was a C interoperable kind so we can
a8b3b0b6 3442 do accurate type checking of bind(c) procs, etc. */
187de1ed 3443 if (kind != 0)
eea58adb 3444 /* Mark this as C interoperable if being declared with one
187de1ed
FXC
3445 of the named constants from iso_c_binding. */
3446 ts->is_c_interop = is_iso_c;
a8b3b0b6 3447 else if (len != NULL)
187de1ed
FXC
3448 /* Here, we might have parsed something such as: character(c_char)
3449 In this case, the parsing code above grabs the c_char when
3450 looking for the length (line 1690, roughly). it's the last
3451 testcase for parsing the kind params of a character variable.
3452 However, it's not actually the length. this seems like it
f5acf0f2 3453 could be an error.
187de1ed
FXC
3454 To see if the user used a C interop kind, test the expr
3455 of the so called length, and see if it's C interoperable. */
3456 ts->is_c_interop = len->ts.is_iso_c;
f5acf0f2 3457
6de9cd9a
DN
3458 return MATCH_YES;
3459}
3460
3461
f6288c24
FR
3462/* Matches a RECORD declaration. */
3463
3464static match
e79e6763 3465match_record_decl (char *name)
f6288c24
FR
3466{
3467 locus old_loc;
3468 old_loc = gfc_current_locus;
e79e6763 3469 match m;
f6288c24 3470
e79e6763
FR
3471 m = gfc_match (" record /");
3472 if (m == MATCH_YES)
f6288c24 3473 {
f6d17ecd 3474 if (!flag_dec_structure)
f6288c24
FR
3475 {
3476 gfc_current_locus = old_loc;
3477 gfc_error ("RECORD at %C is an extension, enable it with "
a3f9f006 3478 "%<-fdec-structure%>");
f6288c24
FR
3479 return MATCH_ERROR;
3480 }
e79e6763
FR
3481 m = gfc_match (" %n/", name);
3482 if (m == MATCH_YES)
3483 return MATCH_YES;
f6288c24
FR
3484 }
3485
e79e6763 3486 gfc_current_locus = old_loc;
f6d17ecd 3487 if (flag_dec_structure
e79e6763
FR
3488 && (gfc_match (" record% ") == MATCH_YES
3489 || gfc_match (" record%t") == MATCH_YES))
3490 gfc_error ("Structure name expected after RECORD at %C");
3491 if (m == MATCH_NO)
f6288c24 3492 return MATCH_NO;
e79e6763
FR
3493
3494 return MATCH_ERROR;
f6288c24
FR
3495}
3496
5bab4c96
PT
3497
3498/* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3499 of expressions to substitute into the possibly parameterized expression
3500 'e'. Using a list is inefficient but should not be too bad since the
3501 number of type parameters is not likely to be large. */
3502static bool
3503insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3504 int* f)
3505{
3506 gfc_actual_arglist *param;
3507 gfc_expr *copy;
3508
3509 if (e->expr_type != EXPR_VARIABLE)
3510 return false;
3511
3512 gcc_assert (e->symtree);
3513 if (e->symtree->n.sym->attr.pdt_kind
3514 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3515 {
3516 for (param = type_param_spec_list; param; param = param->next)
3517 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3518 break;
3519
3520 if (param)
3521 {
3522 copy = gfc_copy_expr (param->expr);
3523 *e = *copy;
3524 free (copy);
3525 }
3526 }
3527
3528 return false;
3529}
3530
3531
3532bool
3533gfc_insert_kind_parameter_exprs (gfc_expr *e)
3534{
3535 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3536}
3537
3538
3539bool
3540gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3541{
3542 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3543 type_param_spec_list = param_list;
3544 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3545 type_param_spec_list = NULL;
3546 type_param_spec_list = old_param_spec_list;
3547}
3548
3549/* Determines the instance of a parameterized derived type to be used by
3550 matching determining the values of the kind parameters and using them
3551 in the name of the instance. If the instance exists, it is used, otherwise
3552 a new derived type is created. */
3553match
3554gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3555 gfc_actual_arglist **ext_param_list)
3556{
3557 /* The PDT template symbol. */
3558 gfc_symbol *pdt = *sym;
3559 /* The symbol for the parameter in the template f2k_namespace. */
3560 gfc_symbol *param;
3561 /* The hoped for instance of the PDT. */
3562 gfc_symbol *instance;
3563 /* The list of parameters appearing in the PDT declaration. */
3564 gfc_formal_arglist *type_param_name_list;
3565 /* Used to store the parameter specification list during recursive calls. */
3566 gfc_actual_arglist *old_param_spec_list;
3567 /* Pointers to the parameter specification being used. */
3568 gfc_actual_arglist *actual_param;
3569 gfc_actual_arglist *tail = NULL;
3570 /* Used to build up the name of the PDT instance. The prefix uses 4
3571 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3572 char name[GFC_MAX_SYMBOL_LEN + 21];
3573
3574 bool name_seen = (param_list == NULL);
3575 bool assumed_seen = false;
3576 bool deferred_seen = false;
3577 bool spec_error = false;
3578 int kind_value, i;
3579 gfc_expr *kind_expr;
3580 gfc_component *c1, *c2;
3581 match m;
3582
3583 type_param_spec_list = NULL;
3584
3585 type_param_name_list = pdt->formal;
3586 actual_param = param_list;
3587 sprintf (name, "Pdt%s", pdt->name);
3588
3589 /* Run through the parameter name list and pick up the actual
3590 parameter values or use the default values in the PDT declaration. */
3591 for (; type_param_name_list;
3592 type_param_name_list = type_param_name_list->next)
3593 {
3594 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3595 {
3596 if (actual_param->spec_type == SPEC_ASSUMED)
3597 spec_error = deferred_seen;
3598 else
3599 spec_error = assumed_seen;
3600
3601 if (spec_error)
3602 {
3603 gfc_error ("The type parameter spec list at %C cannot contain "
3604 "both ASSUMED and DEFERRED parameters");
18a4e7e3 3605 goto error_return;
5bab4c96
PT
3606 }
3607 }
3608
3609 if (actual_param && actual_param->name)
3610 name_seen = true;
3611 param = type_param_name_list->sym;
3612
276515e6
PT
3613 if (!param || !param->name)
3614 continue;
3615
18a4e7e3 3616 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
de624bee
PT
3617 /* An error should already have been thrown in resolve.c
3618 (resolve_fl_derived0). */
18a4e7e3 3619 if (!pdt->attr.use_assoc && !c1)
de624bee 3620 goto error_return;
18a4e7e3 3621
5bab4c96
PT
3622 kind_expr = NULL;
3623 if (!name_seen)
3624 {
18a4e7e3
PT
3625 if (!actual_param && !(c1 && c1->initializer))
3626 {
3627 gfc_error ("The type parameter spec list at %C does not contain "
3628 "enough parameter expressions");
3629 goto error_return;
3630 }
3631 else if (!actual_param && c1 && c1->initializer)
3632 kind_expr = gfc_copy_expr (c1->initializer);
3633 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
5bab4c96
PT
3634 kind_expr = gfc_copy_expr (actual_param->expr);
3635 }
3636 else
3637 {
3638 actual_param = param_list;
3639 for (;actual_param; actual_param = actual_param->next)
3640 if (actual_param->name
3641 && strcmp (actual_param->name, param->name) == 0)
3642 break;
3643 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3644 kind_expr = gfc_copy_expr (actual_param->expr);
3645 else
3646 {
62d3c075
PT
3647 if (c1->initializer)
3648 kind_expr = gfc_copy_expr (c1->initializer);
5bab4c96
PT
3649 else if (!(actual_param && param->attr.pdt_len))
3650 {
71a21b9e 3651 gfc_error ("The derived parameter %qs at %C does not "
5bab4c96 3652 "have a default value", param->name);
18a4e7e3 3653 goto error_return;
5bab4c96
PT
3654 }
3655 }
3656 }
3657
3658 /* Store the current parameter expressions in a temporary actual
3659 arglist 'list' so that they can be substituted in the corresponding
3660 expressions in the PDT instance. */
3661 if (type_param_spec_list == NULL)
3662 {
3663 type_param_spec_list = gfc_get_actual_arglist ();
3664 tail = type_param_spec_list;
3665 }
3666 else
3667 {
3668 tail->next = gfc_get_actual_arglist ();
3669 tail = tail->next;
3670 }
3671 tail->name = param->name;
3672
3673 if (kind_expr)
3674 {
87f3a5cf
PT
3675 /* Try simplification even for LEN expressions. */
3676 gfc_resolve_expr (kind_expr);
3677 gfc_simplify_expr (kind_expr, 1);
18a4e7e3
PT
3678 /* Variable expressions seem to default to BT_PROCEDURE.
3679 TODO find out why this is and fix it. */
3680 if (kind_expr->ts.type != BT_INTEGER
3681 && kind_expr->ts.type != BT_PROCEDURE)
3682 {
3683 gfc_error ("The parameter expression at %C must be of "
3684 "INTEGER type and not %s type",
3685 gfc_basic_typename (kind_expr->ts.type));
3686 goto error_return;
3687 }
3688
5bab4c96 3689 tail->expr = gfc_copy_expr (kind_expr);
5bab4c96
PT
3690 }
3691
3692 if (actual_param)
3693 tail->spec_type = actual_param->spec_type;
3694
3695 if (!param->attr.pdt_kind)
3696 {
18a4e7e3 3697 if (!name_seen && actual_param)
5bab4c96
PT
3698 actual_param = actual_param->next;
3699 if (kind_expr)
3700 {
3701 gfc_free_expr (kind_expr);
3702 kind_expr = NULL;
3703 }
3704 continue;
3705 }
3706
3707 if (actual_param
3708 && (actual_param->spec_type == SPEC_ASSUMED
3709 || actual_param->spec_type == SPEC_DEFERRED))
3710 {
71a21b9e 3711 gfc_error ("The KIND parameter %qs at %C cannot either be "
5bab4c96 3712 "ASSUMED or DEFERRED", param->name);
18a4e7e3 3713 goto error_return;
5bab4c96
PT
3714 }
3715
3716 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3717 {
71a21b9e 3718 gfc_error ("The value for the KIND parameter %qs at %C does not "
5bab4c96 3719 "reduce to a constant expression", param->name);
18a4e7e3 3720 goto error_return;
5bab4c96
PT
3721 }
3722
3723 gfc_extract_int (kind_expr, &kind_value);
8a302cb2 3724 sprintf (name + strlen (name), "_%d", kind_value);
5bab4c96
PT
3725
3726 if (!name_seen && actual_param)
3727 actual_param = actual_param->next;
3728 gfc_free_expr (kind_expr);
3729 }
3730
18a4e7e3
PT
3731 if (!name_seen && actual_param)
3732 {
3733 gfc_error ("The type parameter spec list at %C contains too many "
3734 "parameter expressions");
3735 goto error_return;
3736 }
3737
5bab4c96
PT
3738 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3739 build it, using 'pdt' as a template. */
3740 if (gfc_get_symbol (name, pdt->ns, &instance))
3741 {
3742 gfc_error ("Parameterized derived type at %C is ambiguous");
18a4e7e3 3743 goto error_return;
5bab4c96
PT
3744 }
3745
3746 m = MATCH_YES;
3747
3748 if (instance->attr.flavor == FL_DERIVED
3749 && instance->attr.pdt_type)
3750 {
3751 instance->refs++;
3752 if (ext_param_list)
3753 *ext_param_list = type_param_spec_list;
3754 *sym = instance;
3755 gfc_commit_symbols ();
3756 return m;
3757 }
3758
3759 /* Start building the new instance of the parameterized type. */
3760 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3761 instance->attr.pdt_template = 0;
3762 instance->attr.pdt_type = 1;
3763 instance->declared_at = gfc_current_locus;
3764
3765 /* Add the components, replacing the parameters in all expressions
3766 with the expressions for their values in 'type_param_spec_list'. */
3767 c1 = pdt->components;
3768 tail = type_param_spec_list;
3769 for (; c1; c1 = c1->next)
3770 {
3771 gfc_add_component (instance, c1->name, &c2);
276515e6 3772
5bab4c96
PT
3773 c2->ts = c1->ts;
3774 c2->attr = c1->attr;
3775
276515e6
PT
3776 /* The order of declaration of the type_specs might not be the
3777 same as that of the components. */
3778 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3779 {
3780 for (tail = type_param_spec_list; tail; tail = tail->next)
3781 if (strcmp (c1->name, tail->name) == 0)
3782 break;
3783 }
3784
5bab4c96
PT
3785 /* Deal with type extension by recursively calling this function
3786 to obtain the instance of the extended type. */
3787 if (gfc_current_state () != COMP_DERIVED
3788 && c1 == pdt->components
3789 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3790 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3791 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3792 {
3793 gfc_formal_arglist *f;
3794
3795 old_param_spec_list = type_param_spec_list;
3796
3797 /* Obtain a spec list appropriate to the extended type..*/
3798 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3799 type_param_spec_list = actual_param;
3800 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3801 actual_param = actual_param->next;
3802 if (actual_param)
3803 {
3804 gfc_free_actual_arglist (actual_param->next);
3805 actual_param->next = NULL;
3806 }
3807
3808 /* Now obtain the PDT instance for the extended type. */
3809 c2->param_list = type_param_spec_list;
3810 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3811 NULL);
3812 type_param_spec_list = old_param_spec_list;
3813
3814 c2->ts.u.derived->refs++;
3815 gfc_set_sym_referenced (c2->ts.u.derived);
3816
3817 /* Set extension level. */
3818 if (c2->ts.u.derived->attr.extension == 255)
3819 {
3820 /* Since the extension field is 8 bit wide, we can only have
3821 up to 255 extension levels. */
3822 gfc_error ("Maximum extension level reached with type %qs at %L",
3823 c2->ts.u.derived->name,
3824 &c2->ts.u.derived->declared_at);
18a4e7e3 3825 goto error_return;
5bab4c96
PT
3826 }
3827 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3828
5bab4c96
PT
3829 continue;
3830 }
3831
3832 /* Set the component kind using the parameterized expression. */
276515e6
PT
3833 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3834 && c1->kind_expr != NULL)
5bab4c96
PT
3835 {
3836 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3837 gfc_insert_kind_parameter_exprs (e);
87f3a5cf 3838 gfc_simplify_expr (e, 1);
5bab4c96
PT
3839 gfc_extract_int (e, &c2->ts.kind);
3840 gfc_free_expr (e);
18a4e7e3
PT
3841 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3842 {
3843 gfc_error ("Kind %d not supported for type %s at %C",
3844 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3845 goto error_return;
3846 }
5bab4c96
PT
3847 }
3848
3849 /* Similarly, set the string length if parameterized. */
3850 if (c1->ts.type == BT_CHARACTER
3851 && c1->ts.u.cl->length
3852 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3853 {
3854 gfc_expr *e;
3855 e = gfc_copy_expr (c1->ts.u.cl->length);
3856 gfc_insert_kind_parameter_exprs (e);
3857 gfc_simplify_expr (e, 1);
3858 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3859 c2->ts.u.cl->length = e;
3860 c2->attr.pdt_string = 1;
3861 }
3862
3863 /* Set up either the KIND/LEN initializer, if constant,
3864 or the parameterized expression. Use the template
3865 initializer if one is not already set in this instance. */
3866 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3867 {
3868 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3869 c2->initializer = gfc_copy_expr (tail->expr);
3870 else if (tail && tail->expr)
3871 {
3872 c2->param_list = gfc_get_actual_arglist ();
3873 c2->param_list->name = tail->name;
3874 c2->param_list->expr = gfc_copy_expr (tail->expr);
3875 c2->param_list->next = NULL;
3876 }
3877
3878 if (!c2->initializer && c1->initializer)
3879 c2->initializer = gfc_copy_expr (c1->initializer);
5bab4c96
PT
3880 }
3881
3882 /* Copy the array spec. */
3883 c2->as = gfc_copy_array_spec (c1->as);
3884 if (c1->ts.type == BT_CLASS)
3885 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
3886
3887 /* Determine if an array spec is parameterized. If so, substitute
3888 in the parameter expressions for the bounds and set the pdt_array
3889 attribute. Notice that this attribute must be unconditionally set
3890 if this is an array of parameterized character length. */
3891 if (c1->as && c1->as->type == AS_EXPLICIT)
3892 {
3893 bool pdt_array = false;
3894
3895 /* Are the bounds of the array parameterized? */
3896 for (i = 0; i < c1->as->rank; i++)
3897 {
3898 if (gfc_derived_parameter_expr (c1->as->lower[i]))
3899 pdt_array = true;
3900 if (gfc_derived_parameter_expr (c1->as->upper[i]))
3901 pdt_array = true;
3902 }
3903
3904 /* If they are, free the expressions for the bounds and
3905 replace them with the template expressions with substitute
3906 values. */
3907 for (i = 0; pdt_array && i < c1->as->rank; i++)
3908 {
3909 gfc_expr *e;
3910 e = gfc_copy_expr (c1->as->lower[i]);
3911 gfc_insert_kind_parameter_exprs (e);
3912 gfc_simplify_expr (e, 1);
3913 gfc_free_expr (c2->as->lower[i]);
3914 c2->as->lower[i] = e;
3915 e = gfc_copy_expr (c1->as->upper[i]);
3916 gfc_insert_kind_parameter_exprs (e);
3917 gfc_simplify_expr (e, 1);
3918 gfc_free_expr (c2->as->upper[i]);
3919 c2->as->upper[i] = e;
3920 }
3921 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
0b627b58
PT
3922 if (c1->initializer)
3923 {
3924 c2->initializer = gfc_copy_expr (c1->initializer);
3925 gfc_insert_kind_parameter_exprs (c2->initializer);
3926 gfc_simplify_expr (c2->initializer, 1);
3927 }
5bab4c96
PT
3928 }
3929
3930 /* Recurse into this function for PDT components. */
3931 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3932 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
3933 {
3934 gfc_actual_arglist *params;
3935 /* The component in the template has a list of specification
3936 expressions derived from its declaration. */
3937 params = gfc_copy_actual_arglist (c1->param_list);
3938 actual_param = params;
3939 /* Substitute the template parameters with the expressions
3940 from the specification list. */
3941 for (;actual_param; actual_param = actual_param->next)
3942 gfc_insert_parameter_exprs (actual_param->expr,
3943 type_param_spec_list);
3944
3945 /* Now obtain the PDT instance for the component. */
3946 old_param_spec_list = type_param_spec_list;
3947 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
3948 type_param_spec_list = old_param_spec_list;
3949
3950 c2->param_list = params;
2fcd5884
PT
3951 if (!(c2->attr.pointer || c2->attr.allocatable))
3952 c2->initializer = gfc_default_initializer (&c2->ts);
3953
3954 if (c2->attr.allocatable)
3955 instance->attr.alloc_comp = 1;
5bab4c96
PT
3956 }
3957 }
3958
3959 gfc_commit_symbol (instance);
3960 if (ext_param_list)
3961 *ext_param_list = type_param_spec_list;
3962 *sym = instance;
3963 return m;
18a4e7e3
PT
3964
3965error_return:
3966 gfc_free_actual_arglist (type_param_spec_list);
3967 return MATCH_ERROR;
5bab4c96
PT
3968}
3969
3970
e74f1cc8
JW
3971/* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
3972 structure to the matched specification. This is necessary for FUNCTION and
6de9cd9a
DN
3973 IMPLICIT statements.
3974
d51347f9 3975 If implicit_flag is nonzero, then we don't check for the optional
e5ddaa24 3976 kind specification. Not doing so is needed for matching an IMPLICIT
6de9cd9a
DN
3977 statement correctly. */
3978
e2d29968 3979match
e74f1cc8 3980gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
6de9cd9a
DN
3981{
3982 char name[GFC_MAX_SYMBOL_LEN + 1];
c3f34952 3983 gfc_symbol *sym, *dt_sym;
6de9cd9a 3984 match m;
8fc541d3 3985 char c;
0fb56814 3986 bool seen_deferred_kind, matched_type;
c3f34952 3987 const char *dt_name;
6de9cd9a 3988
5bab4c96
PT
3989 decl_type_param_list = NULL;
3990
1c8bcdf7
PT
3991 /* A belt and braces check that the typespec is correctly being treated
3992 as a deferred characteristic association. */
3993 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
a99d95a2
PT
3994 && (gfc_current_block ()->result->ts.kind == -1)
3995 && (ts->kind == -1);
6de9cd9a 3996 gfc_clear_ts (ts);
1c8bcdf7
PT
3997 if (seen_deferred_kind)
3998 ts->kind = -1;
6de9cd9a 3999
a8b3b0b6 4000 /* Clear the current binding label, in case one is given. */
62603fae 4001 curr_binding_label = NULL;
a8b3b0b6 4002
5f700e6d
AL
4003 if (gfc_match (" byte") == MATCH_YES)
4004 {
524af0d6 4005 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
5f700e6d
AL
4006 return MATCH_ERROR;
4007
4008 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4009 {
4010 gfc_error ("BYTE type used at %C "
4011 "is not available on the target machine");
4012 return MATCH_ERROR;
4013 }
d51347f9 4014
5f700e6d
AL
4015 ts->type = BT_INTEGER;
4016 ts->kind = 1;
4017 return MATCH_YES;
4018 }
4019
0fb56814 4020
45a69325 4021 m = gfc_match (" type (");
0fb56814 4022 matched_type = (m == MATCH_YES);
45a69325
TB
4023 if (matched_type)
4024 {
4025 gfc_gobble_whitespace ();
4026 if (gfc_peek_ascii_char () == '*')
4027 {
4028 if ((m = gfc_match ("*)")) != MATCH_YES)
4029 return m;
f6288c24 4030 if (gfc_comp_struct (gfc_current_state ()))
45a69325
TB
4031 {
4032 gfc_error ("Assumed type at %C is not allowed for components");
4033 return MATCH_ERROR;
4034 }
286f737c 4035 if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
45a69325
TB
4036 return MATCH_ERROR;
4037 ts->type = BT_ASSUMED;
4038 return MATCH_YES;
4039 }
4040
4041 m = gfc_match ("%n", name);
4042 matched_type = (m == MATCH_YES);
4043 }
4044
0fb56814
TB
4045 if ((matched_type && strcmp ("integer", name) == 0)
4046 || (!matched_type && gfc_match (" integer") == MATCH_YES))
6de9cd9a
DN
4047 {
4048 ts->type = BT_INTEGER;
9d64df18 4049 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
4050 goto get_kind;
4051 }
4052
0fb56814
TB
4053 if ((matched_type && strcmp ("character", name) == 0)
4054 || (!matched_type && gfc_match (" character") == MATCH_YES))
6de9cd9a 4055 {
0fb56814 4056 if (matched_type
524af0d6
JB
4057 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4058 "intrinsic-type-spec at %C"))
0fb56814
TB
4059 return MATCH_ERROR;
4060
6de9cd9a 4061 ts->type = BT_CHARACTER;
e5ddaa24 4062 if (implicit_flag == 0)
0fb56814 4063 m = gfc_match_char_spec (ts);
e5ddaa24 4064 else
0fb56814
TB
4065 m = MATCH_YES;
4066
4067 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4068 m = MATCH_ERROR;
4069
4070 return m;
6de9cd9a
DN
4071 }
4072
0fb56814
TB
4073 if ((matched_type && strcmp ("real", name) == 0)
4074 || (!matched_type && gfc_match (" real") == MATCH_YES))
6de9cd9a
DN
4075 {
4076 ts->type = BT_REAL;
9d64df18 4077 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
4078 goto get_kind;
4079 }
4080
0fb56814
TB
4081 if ((matched_type
4082 && (strcmp ("doubleprecision", name) == 0
4083 || (strcmp ("double", name) == 0
4084 && gfc_match (" precision") == MATCH_YES)))
4085 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
6de9cd9a 4086 {
0fb56814 4087 if (matched_type
524af0d6
JB
4088 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4089 "intrinsic-type-spec at %C"))
0fb56814
TB
4090 return MATCH_ERROR;
4091 if (matched_type && gfc_match_char (')') != MATCH_YES)
4092 return MATCH_ERROR;
4093
6de9cd9a 4094 ts->type = BT_REAL;
9d64df18 4095 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
4096 return MATCH_YES;
4097 }
4098
0fb56814
TB
4099 if ((matched_type && strcmp ("complex", name) == 0)
4100 || (!matched_type && gfc_match (" complex") == MATCH_YES))
6de9cd9a
DN
4101 {
4102 ts->type = BT_COMPLEX;
9d64df18 4103 ts->kind = gfc_default_complex_kind;
6de9cd9a
DN
4104 goto get_kind;
4105 }
4106
0fb56814
TB
4107 if ((matched_type
4108 && (strcmp ("doublecomplex", name) == 0
4109 || (strcmp ("double", name) == 0
4110 && gfc_match (" complex") == MATCH_YES)))
4111 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
6de9cd9a 4112 {
524af0d6 4113 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
0fb56814
TB
4114 return MATCH_ERROR;
4115
4116 if (matched_type
524af0d6
JB
4117 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4118 "intrinsic-type-spec at %C"))
0fb56814
TB
4119 return MATCH_ERROR;
4120
4121 if (matched_type && gfc_match_char (')') != MATCH_YES)
df8652dc
SK
4122 return MATCH_ERROR;
4123
6de9cd9a 4124 ts->type = BT_COMPLEX;
9d64df18 4125 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
4126 return MATCH_YES;
4127 }
4128
0fb56814
TB
4129 if ((matched_type && strcmp ("logical", name) == 0)
4130 || (!matched_type && gfc_match (" logical") == MATCH_YES))
6de9cd9a
DN
4131 {
4132 ts->type = BT_LOGICAL;
9d64df18 4133 ts->kind = gfc_default_logical_kind;
6de9cd9a
DN
4134 goto get_kind;
4135 }
4136
0fb56814 4137 if (matched_type)
5bab4c96
PT
4138 {
4139 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4140 if (m == MATCH_ERROR)
4141 return m;
4142
0fb56814 4143 m = gfc_match_char (')');
5bab4c96 4144 }
0fb56814 4145
f6288c24
FR
4146 if (m != MATCH_YES)
4147 m = match_record_decl (name);
4148
4149 if (matched_type || m == MATCH_YES)
4150 {
4151 ts->type = BT_DERIVED;
4152 /* We accept record/s/ or type(s) where s is a structure, but we
4153 * don't need all the extra derived-type stuff for structures. */
4154 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4155 {
2f029c08 4156 gfc_error ("Type name %qs at %C is ambiguous", name);
f6288c24
FR
4157 return MATCH_ERROR;
4158 }
5bab4c96
PT
4159
4160 if (sym && sym->attr.flavor == FL_DERIVED
4161 && sym->attr.pdt_template
4162 && gfc_current_state () != COMP_DERIVED)
4163 {
4164 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4165 if (m != MATCH_YES)
4166 return m;
4167 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4168 ts->u.derived = sym;
4169 strcpy (name, gfc_dt_lower_string (sym->name));
4170 }
4171
f6288c24
FR
4172 if (sym && sym->attr.flavor == FL_STRUCT)
4173 {
4174 ts->u.derived = sym;
4175 return MATCH_YES;
4176 }
4177 /* Actually a derived type. */
4178 }
4179
cf2b3c22 4180 else
727e8544 4181 {
f6288c24 4182 /* Match nested STRUCTURE declarations; only valid within another
e79e6763 4183 structure declaration. */
f6d17ecd 4184 if (flag_dec_structure
e79e6763
FR
4185 && (gfc_current_state () == COMP_STRUCTURE
4186 || gfc_current_state () == COMP_MAP))
4187 {
4188 m = gfc_match (" structure");
4189 if (m == MATCH_YES)
4190 {
4191 m = gfc_match_structure_decl ();
4192 if (m == MATCH_YES)
4193 {
4194 /* gfc_new_block is updated by match_structure_decl. */
4195 ts->type = BT_DERIVED;
4196 ts->u.derived = gfc_new_block;
4197 return MATCH_YES;
4198 }
4199 }
4200 if (m == MATCH_ERROR)
4201 return MATCH_ERROR;
4202 }
f6288c24 4203
528622fd
JW
4204 /* Match CLASS declarations. */
4205 m = gfc_match (" class ( * )");
4206 if (m == MATCH_ERROR)
4207 return MATCH_ERROR;
4208 else if (m == MATCH_YES)
4209 {
8b704316
PT
4210 gfc_symbol *upe;
4211 gfc_symtree *st;
4212 ts->type = BT_CLASS;
f5acf0f2 4213 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
8b704316
PT
4214 if (upe == NULL)
4215 {
f5acf0f2
PT
4216 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4217 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
8b704316
PT
4218 st->n.sym = upe;
4219 gfc_set_sym_referenced (upe);
4220 upe->refs++;
4221 upe->ts.type = BT_VOID;
4222 upe->attr.unlimited_polymorphic = 1;
4223 /* This is essential to force the construction of
4224 unlimited polymorphic component class containers. */
4225 upe->attr.zero_comp = 1;
70112e2a 4226 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
524af0d6 4227 &gfc_current_locus))
b93d8a3f
JW
4228 return MATCH_ERROR;
4229 }
8b704316
PT
4230 else
4231 {
b93d8a3f 4232 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
8b704316
PT
4233 st->n.sym = upe;
4234 upe->refs++;
4235 }
4236 ts->u.derived = upe;
4237 return m;
4238 }
528622fd 4239
5bab4c96
PT
4240 m = gfc_match (" class (");
4241
4242 if (m == MATCH_YES)
4243 m = gfc_match ("%n", name);
4244 else
4245 return m;
4246
727e8544
JW
4247 if (m != MATCH_YES)
4248 return m;
cf2b3c22 4249 ts->type = BT_CLASS;
727e8544 4250
524af0d6 4251 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
e74f1cc8 4252 return MATCH_ERROR;
5bab4c96
PT
4253
4254 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4255 if (m == MATCH_ERROR)
4256 return m;
4257
4258 m = gfc_match_char (')');
4259 if (m != MATCH_YES)
4260 return m;
727e8544 4261 }
6de9cd9a 4262
1c8bcdf7
PT
4263 /* Defer association of the derived type until the end of the
4264 specification block. However, if the derived type can be
f5acf0f2 4265 found, add it to the typespec. */
1c8bcdf7 4266 if (gfc_matching_function)
e2d29968 4267 {
bc21d315 4268 ts->u.derived = NULL;
1c8bcdf7
PT
4269 if (gfc_current_state () != COMP_INTERFACE
4270 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
c3f34952
TB
4271 {
4272 sym = gfc_find_dt_in_generic (sym);
4273 ts->u.derived = sym;
4274 }
e2d29968
PT
4275 return MATCH_YES;
4276 }
4277
4278 /* Search for the name but allow the components to be defined later. If
4279 type = -1, this typespec has been seen in a function declaration but
c3f34952 4280 the type could not be accessed at that point. The actual derived type is
eea58adb 4281 stored in a symtree with the first letter of the name capitalized; the
c3f34952
TB
4282 symtree with the all lower-case name contains the associated
4283 generic function. */
f6288c24 4284 dt_name = gfc_dt_upper_string (name);
1c8bcdf7 4285 sym = NULL;
c3f34952
TB
4286 dt_sym = NULL;
4287 if (ts->kind != -1)
6de9cd9a 4288 {
c3f34952
TB
4289 gfc_get_ha_symbol (name, &sym);
4290 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4291 {
c4100eae 4292 gfc_error ("Type name %qs at %C is ambiguous", name);
c3f34952
TB
4293 return MATCH_ERROR;
4294 }
4295 if (sym->generic && !dt_sym)
4296 dt_sym = gfc_find_dt_in_generic (sym);
18a4e7e3
PT
4297
4298 /* Host associated PDTs can get confused with their constructors
4299 because they ar instantiated in the template's namespace. */
4300 if (!dt_sym)
4301 {
4302 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4303 {
4304 gfc_error ("Type name %qs at %C is ambiguous", name);
4305 return MATCH_ERROR;
4306 }
4307 if (dt_sym && !dt_sym->attr.pdt_type)
4308 dt_sym = NULL;
4309 }
6de9cd9a 4310 }
e2d29968
PT
4311 else if (ts->kind == -1)
4312 {
1c8bcdf7
PT
4313 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4314 || gfc_current_ns->has_import_set;
c3f34952
TB
4315 gfc_find_symbol (name, NULL, iface, &sym);
4316 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
f5acf0f2 4317 {
c4100eae 4318 gfc_error ("Type name %qs at %C is ambiguous", name);
e2d29968
PT
4319 return MATCH_ERROR;
4320 }
c3f34952
TB
4321 if (sym && sym->generic && !dt_sym)
4322 dt_sym = gfc_find_dt_in_generic (sym);
e2d29968 4323
1c8bcdf7 4324 ts->kind = 0;
e2d29968
PT
4325 if (sym == NULL)
4326 return MATCH_NO;
4327 }
6de9cd9a 4328
f6288c24 4329 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
c3f34952
TB
4330 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4331 || sym->attr.subroutine)
4332 {
fea70c99
MLI
4333 gfc_error ("Type name %qs at %C conflicts with previously declared "
4334 "entity at %L, which has the same name", name,
4335 &sym->declared_at);
c3f34952
TB
4336 return MATCH_ERROR;
4337 }
6de9cd9a 4338
5bab4c96
PT
4339 if (sym && sym->attr.flavor == FL_DERIVED
4340 && sym->attr.pdt_template
4341 && gfc_current_state () != COMP_DERIVED)
18a4e7e3
PT
4342 {
4343 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4344 if (m != MATCH_YES)
4345 return m;
4346 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4347 ts->u.derived = sym;
4348 strcpy (name, gfc_dt_lower_string (sym->name));
4349 }
5bab4c96 4350
44c57c2f 4351 gfc_save_symbol_data (sym);
1c8bcdf7 4352 gfc_set_sym_referenced (sym);
c3f34952 4353 if (!sym->attr.generic
524af0d6 4354 && !gfc_add_generic (&sym->attr, sym->name, NULL))
c3f34952
TB
4355 return MATCH_ERROR;
4356
4357 if (!sym->attr.function
524af0d6 4358 && !gfc_add_function (&sym->attr, sym->name, NULL))
c3f34952
TB
4359 return MATCH_ERROR;
4360
5bab4c96
PT
4361 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4362 && dt_sym->attr.pdt_template
4363 && gfc_current_state () != COMP_DERIVED)
4364 {
4365 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4366 if (m != MATCH_YES)
4367 return m;
4368 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4369 }
4370
c3f34952
TB
4371 if (!dt_sym)
4372 {
4373 gfc_interface *intr, *head;
4374
4375 /* Use upper case to save the actual derived-type symbol. */
4376 gfc_get_symbol (dt_name, NULL, &dt_sym);
51f03c6b 4377 dt_sym->name = gfc_get_string ("%s", sym->name);
c3f34952
TB
4378 head = sym->generic;
4379 intr = gfc_get_interface ();
4380 intr->sym = dt_sym;
4381 intr->where = gfc_current_locus;
4382 intr->next = head;
4383 sym->generic = intr;
4384 sym->attr.if_source = IFSRC_DECL;
4385 }
44c57c2f
MM
4386 else
4387 gfc_save_symbol_data (dt_sym);
c3f34952
TB
4388
4389 gfc_set_sym_referenced (dt_sym);
4390
f6288c24 4391 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
524af0d6 4392 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
c3f34952
TB
4393 return MATCH_ERROR;
4394
4395 ts->u.derived = dt_sym;
6de9cd9a
DN
4396
4397 return MATCH_YES;
4398
4399get_kind:
0fb56814 4400 if (matched_type
524af0d6
JB
4401 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4402 "intrinsic-type-spec at %C"))
0fb56814
TB
4403 return MATCH_ERROR;
4404
6de9cd9a
DN
4405 /* For all types except double, derived and character, look for an
4406 optional kind specifier. MATCH_NO is actually OK at this point. */
e5ddaa24 4407 if (implicit_flag == 1)
0fb56814
TB
4408 {
4409 if (matched_type && gfc_match_char (')') != MATCH_YES)
4410 return MATCH_ERROR;
4411
4412 return MATCH_YES;
4413 }
6de9cd9a 4414
0ff0dfbf
TS
4415 if (gfc_current_form == FORM_FREE)
4416 {
0b3624f6
SK
4417 c = gfc_peek_ascii_char ();
4418 if (!gfc_is_whitespace (c) && c != '*' && c != '('
636dff67 4419 && c != ':' && c != ',')
0fb56814
TB
4420 {
4421 if (matched_type && c == ')')
4422 {
4423 gfc_next_ascii_char ();
4424 return MATCH_YES;
4425 }
4426 return MATCH_NO;
4427 }
0ff0dfbf
TS
4428 }
4429
e2d29968 4430 m = gfc_match_kind_spec (ts, false);
6de9cd9a 4431 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4381322d
SK
4432 {
4433 m = gfc_match_old_kind_spec (ts);
4434 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4435 return MATCH_ERROR;
4436 }
6de9cd9a 4437
0fb56814
TB
4438 if (matched_type && gfc_match_char (')') != MATCH_YES)
4439 return MATCH_ERROR;
4440
1c8bcdf7
PT
4441 /* Defer association of the KIND expression of function results
4442 until after USE and IMPORT statements. */
4443 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4444 || gfc_matching_function)
4445 return MATCH_YES;
4446
6de9cd9a
DN
4447 if (m == MATCH_NO)
4448 m = MATCH_YES; /* No kind specifier found. */
4449
4450 return m;
4451}
4452
4453
e5ddaa24
TS
4454/* Match an IMPLICIT NONE statement. Actually, this statement is
4455 already matched in parse.c, or we would not end up here in the
4456 first place. So the only thing we need to check, is if there is
4457 trailing garbage. If not, the match is successful. */
4458
4459match
4460gfc_match_implicit_none (void)
4461{
8b7a967e
TB
4462 char c;
4463 match m;
4464 char name[GFC_MAX_SYMBOL_LEN + 1];
4465 bool type = false;
4466 bool external = false;
a6c63173
TB
4467 locus cur_loc = gfc_current_locus;
4468
4469 if (gfc_current_ns->seen_implicit_none
4470 || gfc_current_ns->has_implicit_none_export)
4471 {
4472 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4473 return MATCH_ERROR;
4474 }
8b7a967e
TB
4475
4476 gfc_gobble_whitespace ();
4477 c = gfc_peek_ascii_char ();
4478 if (c == '(')
4479 {
4480 (void) gfc_next_ascii_char ();
8179b067 4481 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
8b7a967e 4482 return MATCH_ERROR;
a6c63173
TB
4483
4484 gfc_gobble_whitespace ();
4485 if (gfc_peek_ascii_char () == ')')
8b7a967e 4486 {
a6c63173
TB
4487 (void) gfc_next_ascii_char ();
4488 type = true;
4489 }
4490 else
4491 for(;;)
4492 {
4493 m = gfc_match (" %n", name);
4494 if (m != MATCH_YES)
4495 return MATCH_ERROR;
8b7a967e 4496
a6c63173
TB
4497 if (strcmp (name, "type") == 0)
4498 type = true;
4499 else if (strcmp (name, "external") == 0)
4500 external = true;
4501 else
4502 return MATCH_ERROR;
8b7a967e 4503
a6c63173
TB
4504 gfc_gobble_whitespace ();
4505 c = gfc_next_ascii_char ();
4506 if (c == ',')
4507 continue;
4508 if (c == ')')
4509 break;
4510 return MATCH_ERROR;
4511 }
8b7a967e
TB
4512 }
4513 else
4514 type = true;
4515
4516 if (gfc_match_eos () != MATCH_YES)
4517 return MATCH_ERROR;
4518
a6c63173 4519 gfc_set_implicit_none (type, external, &cur_loc);
8b7a967e
TB
4520
4521 return MATCH_YES;
e5ddaa24
TS
4522}
4523
4524
4525/* Match the letter range(s) of an IMPLICIT statement. */
4526
4527static match
1107b970 4528match_implicit_range (void)
e5ddaa24 4529{
8fc541d3
FXC
4530 char c, c1, c2;
4531 int inner;
e5ddaa24
TS
4532 locus cur_loc;
4533
4534 cur_loc = gfc_current_locus;
4535
4536 gfc_gobble_whitespace ();
8fc541d3 4537 c = gfc_next_ascii_char ();
e5ddaa24
TS
4538 if (c != '(')
4539 {
4540 gfc_error ("Missing character range in IMPLICIT at %C");
4541 goto bad;
4542 }
4543
4544 inner = 1;
4545 while (inner)
4546 {
4547 gfc_gobble_whitespace ();
8fc541d3 4548 c1 = gfc_next_ascii_char ();
e5ddaa24
TS
4549 if (!ISALPHA (c1))
4550 goto bad;
4551
4552 gfc_gobble_whitespace ();
8fc541d3 4553 c = gfc_next_ascii_char ();
e5ddaa24
TS
4554
4555 switch (c)
4556 {
4557 case ')':
66e4ab31 4558 inner = 0; /* Fall through. */
e5ddaa24
TS
4559
4560 case ',':
4561 c2 = c1;
4562 break;
4563
4564 case '-':
4565 gfc_gobble_whitespace ();
8fc541d3 4566 c2 = gfc_next_ascii_char ();
e5ddaa24
TS
4567 if (!ISALPHA (c2))
4568 goto bad;
4569
4570 gfc_gobble_whitespace ();
8fc541d3 4571 c = gfc_next_ascii_char ();
e5ddaa24
TS
4572
4573 if ((c != ',') && (c != ')'))
4574 goto bad;
4575 if (c == ')')
4576 inner = 0;
4577
4578 break;
4579
4580 default:
4581 goto bad;
4582 }
4583
4584 if (c1 > c2)
4585 {
4586 gfc_error ("Letters must be in alphabetic order in "
4587 "IMPLICIT statement at %C");
4588 goto bad;
4589 }
4590
4591 /* See if we can add the newly matched range to the pending
636dff67
SK
4592 implicits from this IMPLICIT statement. We do not check for
4593 conflicts with whatever earlier IMPLICIT statements may have
4594 set. This is done when we've successfully finished matching
4595 the current one. */
524af0d6 4596 if (!gfc_add_new_implicit_range (c1, c2))
e5ddaa24
TS
4597 goto bad;
4598 }
4599
4600 return MATCH_YES;
4601
4602bad:
4603 gfc_syntax_error (ST_IMPLICIT);
4604
4605 gfc_current_locus = cur_loc;
4606 return MATCH_ERROR;
4607}
4608
4609
4610/* Match an IMPLICIT statement, storing the types for
4611 gfc_set_implicit() if the statement is accepted by the parser.
4612 There is a strange looking, but legal syntactic construction
4613 possible. It looks like:
4614
4615 IMPLICIT INTEGER (a-b) (c-d)
4616
4617 This is legal if "a-b" is a constant expression that happens to
4618 equal one of the legal kinds for integers. The real problem
4619 happens with an implicit specification that looks like:
4620
4621 IMPLICIT INTEGER (a-b)
4622
4623 In this case, a typespec matcher that is "greedy" (as most of the
4624 matchers are) gobbles the character range as a kindspec, leaving
4625 nothing left. We therefore have to go a bit more slowly in the
4626 matching process by inhibiting the kindspec checking during
4627 typespec matching and checking for a kind later. */
4628
4629match
4630gfc_match_implicit (void)
4631{
4632 gfc_typespec ts;
4633 locus cur_loc;
8fc541d3 4634 char c;
e5ddaa24
TS
4635 match m;
4636
8b7a967e
TB
4637 if (gfc_current_ns->seen_implicit_none)
4638 {
4639 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4640 "statement");
4641 return MATCH_ERROR;
4642 }
4643
44000dbb
JD
4644 gfc_clear_ts (&ts);
4645
e5ddaa24
TS
4646 /* We don't allow empty implicit statements. */
4647 if (gfc_match_eos () == MATCH_YES)
4648 {
4649 gfc_error ("Empty IMPLICIT statement at %C");
4650 return MATCH_ERROR;
4651 }
4652
e5ddaa24
TS
4653 do
4654 {
1107b970
PB
4655 /* First cleanup. */
4656 gfc_clear_new_implicit ();
4657
e5ddaa24 4658 /* A basic type is mandatory here. */
e74f1cc8 4659 m = gfc_match_decl_type_spec (&ts, 1);
e5ddaa24
TS
4660 if (m == MATCH_ERROR)
4661 goto error;
4662 if (m == MATCH_NO)
4663 goto syntax;
4664
4665 cur_loc = gfc_current_locus;
1107b970 4666 m = match_implicit_range ();
e5ddaa24
TS
4667
4668 if (m == MATCH_YES)
4669 {
1107b970 4670 /* We may have <TYPE> (<RANGE>). */
e5ddaa24 4671 gfc_gobble_whitespace ();
a6c63173
TB
4672 c = gfc_peek_ascii_char ();
4673 if (c == ',' || c == '\n' || c == ';' || c == '!')
1107b970
PB
4674 {
4675 /* Check for CHARACTER with no length parameter. */
bc21d315 4676 if (ts.type == BT_CHARACTER && !ts.u.cl)
1107b970 4677 {
9d64df18 4678 ts.kind = gfc_default_character_kind;
b76e28c6 4679 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
f622221a 4680 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
b7e75771 4681 NULL, 1);
1107b970
PB
4682 }
4683
4684 /* Record the Successful match. */
524af0d6 4685 if (!gfc_merge_new_implicit (&ts))
1107b970 4686 return MATCH_ERROR;
a6c63173
TB
4687 if (c == ',')
4688 c = gfc_next_ascii_char ();
4689 else if (gfc_match_eos () == MATCH_ERROR)
4690 goto error;
1107b970
PB
4691 continue;
4692 }
e5ddaa24
TS
4693
4694 gfc_current_locus = cur_loc;
4695 }
4696
1107b970
PB
4697 /* Discard the (incorrectly) matched range. */
4698 gfc_clear_new_implicit ();
4699
4700 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4701 if (ts.type == BT_CHARACTER)
8234e5e0 4702 m = gfc_match_char_spec (&ts);
1107b970 4703 else
e5ddaa24 4704 {
e2d29968 4705 m = gfc_match_kind_spec (&ts, false);
e5ddaa24 4706 if (m == MATCH_NO)
1107b970
PB
4707 {
4708 m = gfc_match_old_kind_spec (&ts);
4709 if (m == MATCH_ERROR)
4710 goto error;
4711 if (m == MATCH_NO)
4712 goto syntax;
4713 }
e5ddaa24 4714 }
1107b970
PB
4715 if (m == MATCH_ERROR)
4716 goto error;
e5ddaa24 4717
1107b970 4718 m = match_implicit_range ();
e5ddaa24
TS
4719 if (m == MATCH_ERROR)
4720 goto error;
4721 if (m == MATCH_NO)
4722 goto syntax;
4723
4724 gfc_gobble_whitespace ();
8fc541d3 4725 c = gfc_next_ascii_char ();
a6c63173 4726 if (c != ',' && gfc_match_eos () != MATCH_YES)
e5ddaa24
TS
4727 goto syntax;
4728
524af0d6 4729 if (!gfc_merge_new_implicit (&ts))
1107b970 4730 return MATCH_ERROR;
e5ddaa24
TS
4731 }
4732 while (c == ',');
4733
1107b970 4734 return MATCH_YES;
e5ddaa24
TS
4735
4736syntax:
4737 gfc_syntax_error (ST_IMPLICIT);
4738
4739error:
4740 return MATCH_ERROR;
4741}
4742
66e4ab31 4743
8998be20
TB
4744match
4745gfc_match_import (void)
4746{
4747 char name[GFC_MAX_SYMBOL_LEN + 1];
4748 match m;
4749 gfc_symbol *sym;
4750 gfc_symtree *st;
4751
66e4ab31
SK
4752 if (gfc_current_ns->proc_name == NULL
4753 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
8998be20
TB
4754 {
4755 gfc_error ("IMPORT statement at %C only permitted in "
4756 "an INTERFACE body");
4757 return MATCH_ERROR;
4758 }
4759
4668d6f9
PT
4760 if (gfc_current_ns->proc_name->attr.module_procedure)
4761 {
4762 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4763 "in a module procedure interface body");
4764 return MATCH_ERROR;
4765 }
4766
524af0d6 4767 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
8998be20
TB
4768 return MATCH_ERROR;
4769
4770 if (gfc_match_eos () == MATCH_YES)
4771 {
4772 /* All host variables should be imported. */
4773 gfc_current_ns->has_import_set = 1;
4774 return MATCH_YES;
4775 }
4776
4777 if (gfc_match (" ::") == MATCH_YES)
4778 {
4779 if (gfc_match_eos () == MATCH_YES)
636dff67
SK
4780 {
4781 gfc_error ("Expecting list of named entities at %C");
4782 return MATCH_ERROR;
4783 }
8998be20
TB
4784 }
4785
4786 for(;;)
4787 {
2e8d9212 4788 sym = NULL;
8998be20
TB
4789 m = gfc_match (" %n", name);
4790 switch (m)
4791 {
4792 case MATCH_YES:
36d3fb4c 4793 if (gfc_current_ns->parent != NULL
66e4ab31 4794 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
36d3fb4c 4795 {
c4100eae 4796 gfc_error ("Type name %qs at %C is ambiguous", name);
36d3fb4c
PT
4797 return MATCH_ERROR;
4798 }
4e2cf5f5 4799 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
66e4ab31
SK
4800 && gfc_find_symbol (name,
4801 gfc_current_ns->proc_name->ns->parent,
4802 1, &sym))
636dff67 4803 {
c4100eae 4804 gfc_error ("Type name %qs at %C is ambiguous", name);
636dff67
SK
4805 return MATCH_ERROR;
4806 }
4807
4808 if (sym == NULL)
4809 {
c4100eae 4810 gfc_error ("Cannot IMPORT %qs from host scoping unit "
636dff67
SK
4811 "at %C - does not exist.", name);
4812 return MATCH_ERROR;
4813 }
4814
dd8b9dde 4815 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
636dff67 4816 {
db30e21c 4817 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
48749dbc 4818 "at %C", name);
636dff67
SK
4819 goto next_item;
4820 }
4821
dd8b9dde 4822 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
636dff67
SK
4823 st->n.sym = sym;
4824 sym->refs++;
5a8af0b4 4825 sym->attr.imported = 1;
8998be20 4826
c3f34952
TB
4827 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4828 {
4829 /* The actual derived type is stored in a symtree with the first
eea58adb 4830 letter of the name capitalized; the symtree with the all
1cc0e193 4831 lower-case name contains the associated generic function. */
c3f34952 4832 st = gfc_new_symtree (&gfc_current_ns->sym_root,
f6288c24 4833 gfc_dt_upper_string (name));
c3f34952
TB
4834 st->n.sym = sym;
4835 sym->refs++;
4836 sym->attr.imported = 1;
4837 }
4838
8998be20
TB
4839 goto next_item;
4840
4841 case MATCH_NO:
4842 break;
4843
4844 case MATCH_ERROR:
4845 return MATCH_ERROR;
4846 }
4847
4848 next_item:
4849 if (gfc_match_eos () == MATCH_YES)
4850 break;
4851 if (gfc_match_char (',') != MATCH_YES)
4852 goto syntax;
4853 }
4854
4855 return MATCH_YES;
4856
4857syntax:
4858 gfc_error ("Syntax error in IMPORT statement at %C");
4859 return MATCH_ERROR;
4860}
e5ddaa24 4861
66e4ab31 4862
f2449db4
RS
4863/* A minimal implementation of gfc_match without whitespace, escape
4864 characters or variable arguments. Returns true if the next
4865 characters match the TARGET template exactly. */
4866
4867static bool
4868match_string_p (const char *target)
4869{
4870 const char *p;
4871
4872 for (p = target; *p; p++)
8fc541d3 4873 if ((char) gfc_next_ascii_char () != *p)
f2449db4
RS
4874 return false;
4875 return true;
4876}
4877
6de9cd9a
DN
4878/* Matches an attribute specification including array specs. If
4879 successful, leaves the variables current_attr and current_as
4880 holding the specification. Also sets the colon_seen variable for
4881 later use by matchers associated with initializations.
4882
4883 This subroutine is a little tricky in the sense that we don't know
4884 if we really have an attr-spec until we hit the double colon.
4885 Until that time, we can only return MATCH_NO. This forces us to
4886 check for duplicate specification at this level. */
4887
4888static match
4889match_attr_spec (void)
4890{
6de9cd9a 4891 /* Modifiers that can exist in a type statement. */
d75d9546 4892 enum
ea20e8be
JW
4893 { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
4894 DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
4895 DECL_DIMENSION, DECL_EXTERNAL,
4896 DECL_INTRINSIC, DECL_OPTIONAL,
ee7e677f 4897 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
34d567d1 4898 DECL_STATIC, DECL_AUTOMATIC,
ee7e677f 4899 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
fe4e525c 4900 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5bab4c96 4901 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
d75d9546 4902 };
6de9cd9a
DN
4903
4904/* GFC_DECL_END is the sentinel, index starts at 0. */
4905#define NUM_DECL GFC_DECL_END
4906
ea20e8be
JW
4907 /* Make sure that values from sym_intent are safe to be used here. */
4908 gcc_assert (INTENT_IN > 0);
4909
6de9cd9a
DN
4910 locus start, seen_at[NUM_DECL];
4911 int seen[NUM_DECL];
09639a83 4912 unsigned int d;
6de9cd9a
DN
4913 const char *attr;
4914 match m;
524af0d6 4915 bool t;
6de9cd9a
DN
4916
4917 gfc_clear_attr (&current_attr);
63645982 4918 start = gfc_current_locus;
6de9cd9a
DN
4919
4920 current_as = NULL;
4921 colon_seen = 0;
6f855a26 4922 attr_seen = 0;
6de9cd9a
DN
4923
4924 /* See if we get all of the keywords up to the final double colon. */
4925 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
4926 seen[d] = 0;
4927
4928 for (;;)
4929 {
8fc541d3 4930 char ch;
a8b3b0b6 4931
f2449db4
RS
4932 d = DECL_NONE;
4933 gfc_gobble_whitespace ();
4934
8fc541d3 4935 ch = gfc_next_ascii_char ();
f2449db4
RS
4936 if (ch == ':')
4937 {
4938 /* This is the successful exit condition for the loop. */
8fc541d3 4939 if (gfc_next_ascii_char () == ':')
f2449db4
RS
4940 break;
4941 }
4942 else if (ch == ',')
a8b3b0b6 4943 {
a8b3b0b6 4944 gfc_gobble_whitespace ();
8fc541d3 4945 switch (gfc_peek_ascii_char ())
a8b3b0b6 4946 {
f2449db4 4947 case 'a':
1eee5628
TB
4948 gfc_next_ascii_char ();
4949 switch (gfc_next_ascii_char ())
4950 {
4951 case 'l':
4952 if (match_string_p ("locatable"))
4953 {
4954 /* Matched "allocatable". */
4955 d = DECL_ALLOCATABLE;
4956 }
4957 break;
4958
4959 case 's':
4960 if (match_string_p ("ynchronous"))
4961 {
4962 /* Matched "asynchronous". */
4963 d = DECL_ASYNCHRONOUS;
4964 }
4965 break;
34d567d1
FR
4966
4967 case 'u':
4968 if (match_string_p ("tomatic"))
4969 {
4970 /* Matched "automatic". */
4971 d = DECL_AUTOMATIC;
4972 }
4973 break;
1eee5628 4974 }
fe4e525c 4975 break;
f2449db4
RS
4976
4977 case 'b':
a8b3b0b6 4978 /* Try and match the bind(c). */
1eabf70a 4979 m = gfc_match_bind_c (NULL, true);
129d15a3 4980 if (m == MATCH_YES)
a8b3b0b6 4981 d = DECL_IS_BIND_C;
129d15a3
JW
4982 else if (m == MATCH_ERROR)
4983 goto cleanup;
f2449db4
RS
4984 break;
4985
be59db2d 4986 case 'c':
fe4e525c
TB
4987 gfc_next_ascii_char ();
4988 if ('o' != gfc_next_ascii_char ())
4989 break;
4990 switch (gfc_next_ascii_char ())
4991 {
4992 case 'd':
4993 if (match_string_p ("imension"))
4994 {
4995 d = DECL_CODIMENSION;
4996 break;
4997 }
191816a3 4998 /* FALLTHRU */
fe4e525c
TB
4999 case 'n':
5000 if (match_string_p ("tiguous"))
5001 {
5002 d = DECL_CONTIGUOUS;
5003 break;
5004 }
5005 }
be59db2d
TB
5006 break;
5007
f2449db4
RS
5008 case 'd':
5009 if (match_string_p ("dimension"))
5010 d = DECL_DIMENSION;
5011 break;
5012
5013 case 'e':
5014 if (match_string_p ("external"))
5015 d = DECL_EXTERNAL;
5016 break;
5017
5018 case 'i':
5019 if (match_string_p ("int"))
5020 {
8fc541d3 5021 ch = gfc_next_ascii_char ();
f2449db4
RS
5022 if (ch == 'e')
5023 {
5024 if (match_string_p ("nt"))
5025 {
5026 /* Matched "intent". */
ea20e8be
JW
5027 d = match_intent_spec ();
5028 if (d == INTENT_UNKNOWN)
5029 {
5030 m = MATCH_ERROR;
5031 goto cleanup;
5032 }
f2449db4
RS
5033 }
5034 }
5035 else if (ch == 'r')
5036 {
5037 if (match_string_p ("insic"))
5038 {
5039 /* Matched "intrinsic". */
5040 d = DECL_INTRINSIC;
5041 }
5042 }
5043 }
5044 break;
5045
5bab4c96
PT
5046 case 'k':
5047 if (match_string_p ("kind"))
5048 d = DECL_KIND;
5049 break;
5050
5051 case 'l':
5052 if (match_string_p ("len"))
5053 d = DECL_LEN;
5054 break;
5055
f2449db4
RS
5056 case 'o':
5057 if (match_string_p ("optional"))
5058 d = DECL_OPTIONAL;
5059 break;
5060
5061 case 'p':
8fc541d3
FXC
5062 gfc_next_ascii_char ();
5063 switch (gfc_next_ascii_char ())
f2449db4
RS
5064 {
5065 case 'a':
5066 if (match_string_p ("rameter"))
5067 {
5068 /* Matched "parameter". */
5069 d = DECL_PARAMETER;
5070 }
5071 break;
5072
5073 case 'o':
5074 if (match_string_p ("inter"))
5075 {
5076 /* Matched "pointer". */
5077 d = DECL_POINTER;
5078 }
5079 break;
5080
5081 case 'r':
8fc541d3 5082 ch = gfc_next_ascii_char ();
f2449db4
RS
5083 if (ch == 'i')
5084 {
5085 if (match_string_p ("vate"))
5086 {
5087 /* Matched "private". */
5088 d = DECL_PRIVATE;
5089 }
5090 }
5091 else if (ch == 'o')
5092 {
5093 if (match_string_p ("tected"))
5094 {
5095 /* Matched "protected". */
5096 d = DECL_PROTECTED;
5097 }
5098 }
5099 break;
5100
5101 case 'u':
5102 if (match_string_p ("blic"))
5103 {
5104 /* Matched "public". */
5105 d = DECL_PUBLIC;
5106 }
5107 break;
5108 }
5109 break;
5110
5111 case 's':
34d567d1
FR
5112 gfc_next_ascii_char ();
5113 switch (gfc_next_ascii_char ())
5114 {
5115 case 'a':
5116 if (match_string_p ("ve"))
5117 {
5118 /* Matched "save". */
5119 d = DECL_SAVE;
5120 }
5121 break;
5122
5123 case 't':
5124 if (match_string_p ("atic"))
5125 {
5126 /* Matched "static". */
5127 d = DECL_STATIC;
5128 }
5129 break;
5130 }
f2449db4
RS
5131 break;
5132
5133 case 't':
5134 if (match_string_p ("target"))
5135 d = DECL_TARGET;
5136 break;
5137
5138 case 'v':
8fc541d3
FXC
5139 gfc_next_ascii_char ();
5140 ch = gfc_next_ascii_char ();
f2449db4
RS
5141 if (ch == 'a')
5142 {
5143 if (match_string_p ("lue"))
5144 {
5145 /* Matched "value". */
5146 d = DECL_VALUE;
5147 }
5148 }
5149 else if (ch == 'o')
5150 {
5151 if (match_string_p ("latile"))
5152 {
5153 /* Matched "volatile". */
5154 d = DECL_VOLATILE;
5155 }
5156 }
5157 break;
a8b3b0b6
CR
5158 }
5159 }
d468bcdb 5160
f2449db4
RS
5161 /* No double colon and no recognizable decl_type, so assume that
5162 we've been looking at something else the whole time. */
5163 if (d == DECL_NONE)
5164 {
5165 m = MATCH_NO;
5166 goto cleanup;
5167 }
d51347f9 5168
acb388a0
JD
5169 /* Check to make sure any parens are paired up correctly. */
5170 if (gfc_match_parens () == MATCH_ERROR)
5171 {
5172 m = MATCH_ERROR;
5173 goto cleanup;
5174 }
5175
6de9cd9a 5176 seen[d]++;
63645982 5177 seen_at[d] = gfc_current_locus;
6de9cd9a 5178
d3a9eea2 5179 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
6de9cd9a 5180 {
d3a9eea2 5181 gfc_array_spec *as = NULL;
6de9cd9a 5182
d3a9eea2
TB
5183 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5184 d == DECL_CODIMENSION);
5185
5186 if (current_as == NULL)
5187 current_as = as;
5188 else if (m == MATCH_YES)
6de9cd9a 5189 {
524af0d6 5190 if (!merge_array_spec (as, current_as, false))
63fbf586 5191 m = MATCH_ERROR;
cede9502 5192 free (as);
6de9cd9a
DN
5193 }
5194
be59db2d
TB
5195 if (m == MATCH_NO)
5196 {
d3a9eea2
TB
5197 if (d == DECL_CODIMENSION)
5198 gfc_error ("Missing codimension specification at %C");
5199 else
5200 gfc_error ("Missing dimension specification at %C");
be59db2d
TB
5201 m = MATCH_ERROR;
5202 }
5203
5204 if (m == MATCH_ERROR)
5205 goto cleanup;
5206 }
6de9cd9a
DN
5207 }
5208
6de9cd9a
DN
5209 /* Since we've seen a double colon, we have to be looking at an
5210 attr-spec. This means that we can now issue errors. */
5211 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5212 if (seen[d] > 1)
5213 {
5214 switch (d)
5215 {
5216 case DECL_ALLOCATABLE:
5217 attr = "ALLOCATABLE";
5218 break;
1eee5628
TB
5219 case DECL_ASYNCHRONOUS:
5220 attr = "ASYNCHRONOUS";
5221 break;
be59db2d
TB
5222 case DECL_CODIMENSION:
5223 attr = "CODIMENSION";
5224 break;
fe4e525c
TB
5225 case DECL_CONTIGUOUS:
5226 attr = "CONTIGUOUS";
5227 break;
6de9cd9a
DN
5228 case DECL_DIMENSION:
5229 attr = "DIMENSION";
5230 break;
5231 case DECL_EXTERNAL:
5232 attr = "EXTERNAL";
5233 break;
5234 case DECL_IN:
5235 attr = "INTENT (IN)";
5236 break;
5237 case DECL_OUT:
5238 attr = "INTENT (OUT)";
5239 break;
5240 case DECL_INOUT:
5241 attr = "INTENT (IN OUT)";
5242 break;
5243 case DECL_INTRINSIC:
5244 attr = "INTRINSIC";
5245 break;
5246 case DECL_OPTIONAL:
5247 attr = "OPTIONAL";
5248 break;
5bab4c96
PT
5249 case DECL_KIND:
5250 attr = "KIND";
5251 break;
5252 case DECL_LEN:
5253 attr = "LEN";
5254 break;
6de9cd9a
DN
5255 case DECL_PARAMETER:
5256 attr = "PARAMETER";
5257 break;
5258 case DECL_POINTER:
5259 attr = "POINTER";
5260 break;
ee7e677f
TB
5261 case DECL_PROTECTED:
5262 attr = "PROTECTED";
5263 break;
6de9cd9a
DN
5264 case DECL_PRIVATE:
5265 attr = "PRIVATE";
5266 break;
5267 case DECL_PUBLIC:
5268 attr = "PUBLIC";
5269 break;
5270 case DECL_SAVE:
5271 attr = "SAVE";
5272 break;
34d567d1
FR
5273 case DECL_STATIC:
5274 attr = "STATIC";
5275 break;
5276 case DECL_AUTOMATIC:
5277 attr = "AUTOMATIC";
5278 break;
6de9cd9a
DN
5279 case DECL_TARGET:
5280 attr = "TARGET";
5281 break;
a8b3b0b6
CR
5282 case DECL_IS_BIND_C:
5283 attr = "IS_BIND_C";
5284 break;
5285 case DECL_VALUE:
5286 attr = "VALUE";
5287 break;
775e6c3a
TB
5288 case DECL_VOLATILE:
5289 attr = "VOLATILE";
5290 break;
6de9cd9a 5291 default:
66e4ab31 5292 attr = NULL; /* This shouldn't happen. */
6de9cd9a
DN
5293 }
5294
5295 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5296 m = MATCH_ERROR;
5297 goto cleanup;
5298 }
5299
5300 /* Now that we've dealt with duplicate attributes, add the attributes
5301 to the current attribute. */
5302 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5303 {
5304 if (seen[d] == 0)
5305 continue;
6f855a26
FR
5306 else
5307 attr_seen = 1;
6de9cd9a 5308
34d567d1
FR
5309 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5310 && !flag_dec_static)
5311 {
cf004230
FR
5312 gfc_error ("%s at %L is a DEC extension, enable with "
5313 "%<-fdec-static%>",
34d567d1
FR
5314 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5315 m = MATCH_ERROR;
5316 goto cleanup;
5317 }
5318 /* Allow SAVE with STATIC, but don't complain. */
5319 if (d == DECL_STATIC && seen[DECL_SAVE])
5320 continue;
5321
6de9cd9a 5322 if (gfc_current_state () == COMP_DERIVED
be59db2d
TB
5323 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5324 && d != DECL_POINTER && d != DECL_PRIVATE
fe4e525c 5325 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
6de9cd9a 5326 {
5046aff5
PT
5327 if (d == DECL_ALLOCATABLE)
5328 {
524af0d6
JB
5329 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
5330 "attribute at %C in a TYPE definition"))
5046aff5
PT
5331 {
5332 m = MATCH_ERROR;
5333 goto cleanup;
5334 }
636dff67 5335 }
5bab4c96
PT
5336 else if (d == DECL_KIND)
5337 {
5338 if (!gfc_notify_std (GFC_STD_F2003, "KIND "
5339 "attribute at %C in a TYPE definition"))
5340 {
5341 m = MATCH_ERROR;
5342 goto cleanup;
5343 }
5344 if (current_ts.type != BT_INTEGER)
5345 {
5346 gfc_error ("Component with KIND attribute at %C must be "
5347 "INTEGER");
5348 m = MATCH_ERROR;
5349 goto cleanup;
5350 }
5351 if (current_ts.kind != gfc_default_integer_kind)
5352 {
5353 gfc_error ("Component with KIND attribute at %C must be "
5354 "default integer kind (%d)",
5355 gfc_default_integer_kind);
5356 m = MATCH_ERROR;
5357 goto cleanup;
5358 }
5359 }
5360 else if (d == DECL_LEN)
5361 {
5362 if (!gfc_notify_std (GFC_STD_F2003, "LEN "
5363 "attribute at %C in a TYPE definition"))
5364 {
5365 m = MATCH_ERROR;
5366 goto cleanup;
5367 }
5368 if (current_ts.type != BT_INTEGER)
5369 {
5370 gfc_error ("Component with LEN attribute at %C must be "
5371 "INTEGER");
5372 m = MATCH_ERROR;
5373 goto cleanup;
5374 }
5375 if (current_ts.kind != gfc_default_integer_kind)
5376 {
5377 gfc_error ("Component with LEN attribute at %C must be "
5378 "default integer kind (%d)",
5379 gfc_default_integer_kind);
5380 m = MATCH_ERROR;
5381 goto cleanup;
5382 }
5383 }
636dff67 5384 else
5046aff5
PT
5385 {
5386 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
d51347f9 5387 &seen_at[d]);
5046aff5
PT
5388 m = MATCH_ERROR;
5389 goto cleanup;
5390 }
6de9cd9a
DN
5391 }
5392
4213f93b 5393 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
636dff67 5394 && gfc_current_state () != COMP_MODULE)
4213f93b
PT
5395 {
5396 if (d == DECL_PRIVATE)
5397 attr = "PRIVATE";
5398 else
5399 attr = "PUBLIC";
d51347f9
TB
5400 if (gfc_current_state () == COMP_DERIVED
5401 && gfc_state_stack->previous
5402 && gfc_state_stack->previous->state == COMP_MODULE)
5403 {
524af0d6 5404 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
70112e2a 5405 "at %L in a TYPE definition", attr,
524af0d6 5406 &seen_at[d]))
d51347f9
TB
5407 {
5408 m = MATCH_ERROR;
5409 goto cleanup;
5410 }
5411 }
5412 else
5413 {
5414 gfc_error ("%s attribute at %L is not allowed outside of the "
5415 "specification part of a module", attr, &seen_at[d]);
5416 m = MATCH_ERROR;
5417 goto cleanup;
5418 }
4213f93b
PT
5419 }
5420
5bab4c96
PT
5421 if (gfc_current_state () != COMP_DERIVED
5422 && (d == DECL_KIND || d == DECL_LEN))
5423 {
5424 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5425 "definition", &seen_at[d]);
5426 m = MATCH_ERROR;
5427 goto cleanup;
5428 }
5429
6de9cd9a
DN
5430 switch (d)
5431 {
5432 case DECL_ALLOCATABLE:
5433 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5434 break;
5435
1eee5628 5436 case DECL_ASYNCHRONOUS:
524af0d6
JB
5437 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5438 t = false;
1eee5628
TB
5439 else
5440 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5441 break;
5442
be59db2d
TB
5443 case DECL_CODIMENSION:
5444 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5445 break;
5446
fe4e525c 5447 case DECL_CONTIGUOUS:
524af0d6
JB
5448 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5449 t = false;
fe4e525c
TB
5450 else
5451 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5452 break;
5453
6de9cd9a 5454 case DECL_DIMENSION:
231b2fcc 5455 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
5456 break;
5457
5458 case DECL_EXTERNAL:
5459 t = gfc_add_external (&current_attr, &seen_at[d]);
5460 break;
5461
5462 case DECL_IN:
5463 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5464 break;
5465
5466 case DECL_OUT:
5467 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5468 break;
5469
5470 case DECL_INOUT:
5471 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5472 break;
5473
5474 case DECL_INTRINSIC:
5475 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5476 break;
5477
5478 case DECL_OPTIONAL:
5479 t = gfc_add_optional (&current_attr, &seen_at[d]);
5480 break;
5481
5bab4c96
PT
5482 case DECL_KIND:
5483 t = gfc_add_kind (&current_attr, &seen_at[d]);
5484 break;
5485
5486 case DECL_LEN:
5487 t = gfc_add_len (&current_attr, &seen_at[d]);
5488 break;
5489
6de9cd9a 5490 case DECL_PARAMETER:
231b2fcc 5491 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
6de9cd9a
DN
5492 break;
5493
5494 case DECL_POINTER:
5495 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5496 break;
5497
ee7e677f 5498 case DECL_PROTECTED:
721be0f4
SK
5499 if (gfc_current_state () != COMP_MODULE
5500 || (gfc_current_ns->proc_name
5501 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
ee7e677f
TB
5502 {
5503 gfc_error ("PROTECTED at %C only allowed in specification "
5504 "part of a module");
524af0d6 5505 t = false;
ee7e677f
TB
5506 break;
5507 }
5508
524af0d6
JB
5509 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5510 t = false;
ee7e677f
TB
5511 else
5512 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5513 break;
5514
6de9cd9a 5515 case DECL_PRIVATE:
231b2fcc
TS
5516 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5517 &seen_at[d]);
6de9cd9a
DN
5518 break;
5519
5520 case DECL_PUBLIC:
231b2fcc
TS
5521 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5522 &seen_at[d]);
6de9cd9a
DN
5523 break;
5524
34d567d1 5525 case DECL_STATIC:
6de9cd9a 5526 case DECL_SAVE:
80f95228 5527 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
6de9cd9a
DN
5528 break;
5529
34d567d1
FR
5530 case DECL_AUTOMATIC:
5531 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5532 break;
5533
6de9cd9a
DN
5534 case DECL_TARGET:
5535 t = gfc_add_target (&current_attr, &seen_at[d]);
5536 break;
5537
a8b3b0b6
CR
5538 case DECL_IS_BIND_C:
5539 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5540 break;
f5acf0f2 5541
06469efd 5542 case DECL_VALUE:
524af0d6
JB
5543 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5544 t = false;
06469efd
PT
5545 else
5546 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5547 break;
5548
775e6c3a 5549 case DECL_VOLATILE:
524af0d6
JB
5550 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5551 t = false;
775e6c3a
TB
5552 else
5553 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5554 break;
5555
6de9cd9a
DN
5556 default:
5557 gfc_internal_error ("match_attr_spec(): Bad attribute");
5558 }
5559
524af0d6 5560 if (!t)
6de9cd9a
DN
5561 {
5562 m = MATCH_ERROR;
5563 goto cleanup;
5564 }
5565 }
5566
dab2cbf8 5567 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4668d6f9
PT
5568 if ((gfc_current_state () == COMP_MODULE
5569 || gfc_current_state () == COMP_SUBMODULE)
5570 && !current_attr.save
dab2cbf8 5571 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
80f95228
JW
5572 current_attr.save = SAVE_IMPLICIT;
5573
6de9cd9a
DN
5574 colon_seen = 1;
5575 return MATCH_YES;
5576
5577cleanup:
63645982 5578 gfc_current_locus = start;
6de9cd9a
DN
5579 gfc_free_array_spec (current_as);
5580 current_as = NULL;
6f855a26 5581 attr_seen = 0;
6de9cd9a
DN
5582 return m;
5583}
5584
5585
a8b3b0b6
CR
5586/* Set the binding label, dest_label, either with the binding label
5587 stored in the given gfc_typespec, ts, or if none was provided, it
5588 will be the symbol name in all lower case, as required by the draft
5589 (J3/04-007, section 15.4.1). If a binding label was given and
5590 there is more than one argument (num_idents), it is an error. */
5591
524af0d6 5592static bool
f5acf0f2 5593set_binding_label (const char **dest_label, const char *sym_name,
9975a30b 5594 int num_idents)
a8b3b0b6 5595{
ad4a2f64 5596 if (num_idents > 1 && has_name_equals)
a8b3b0b6 5597 {
ad4a2f64
TB
5598 gfc_error ("Multiple identifiers provided with "
5599 "single NAME= specifier at %C");
524af0d6 5600 return false;
ad4a2f64 5601 }
a8b3b0b6 5602
62603fae 5603 if (curr_binding_label)
eea58adb 5604 /* Binding label given; store in temp holder till have sym. */
62603fae 5605 *dest_label = curr_binding_label;
a8b3b0b6
CR
5606 else
5607 {
5608 /* No binding label given, and the NAME= specifier did not exist,
5609 which means there was no NAME="". */
5610 if (sym_name != NULL && has_name_equals == 0)
62603fae 5611 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
a8b3b0b6 5612 }
f5acf0f2 5613
524af0d6 5614 return true;
a8b3b0b6
CR
5615}
5616
5617
5618/* Set the status of the given common block as being BIND(C) or not,
5619 depending on the given parameter, is_bind_c. */
5620
5621void
5622set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5623{
5624 com_block->is_bind_c = is_bind_c;
5625 return;
5626}
5627
5628
5629/* Verify that the given gfc_typespec is for a C interoperable type. */
5630
524af0d6 5631bool
00820a2a 5632gfc_verify_c_interop (gfc_typespec *ts)
a8b3b0b6 5633{
bc21d315 5634 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
ba3721c1 5635 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
524af0d6 5636 ? true : false;
00820a2a 5637 else if (ts->type == BT_CLASS)
524af0d6 5638 return false;
45a69325 5639 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
524af0d6 5640 return false;
45a69325 5641
524af0d6 5642 return true;
a8b3b0b6
CR
5643}
5644
5645
5646/* Verify that the variables of a given common block, which has been
5647 defined with the attribute specifier bind(c), to be of a C
5648 interoperable type. Errors will be reported here, if
5649 encountered. */
5650
524af0d6 5651bool
a8b3b0b6
CR
5652verify_com_block_vars_c_interop (gfc_common_head *com_block)
5653{
5654 gfc_symbol *curr_sym = NULL;
524af0d6 5655 bool retval = true;
a8b3b0b6
CR
5656
5657 curr_sym = com_block->head;
f5acf0f2 5658
a8b3b0b6
CR
5659 /* Make sure we have at least one symbol. */
5660 if (curr_sym == NULL)
5661 return retval;
5662
5663 /* Here we know we have a symbol, so we'll execute this loop
5664 at least once. */
5665 do
5666 {
5667 /* The second to last param, 1, says this is in a common block. */
5668 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5669 curr_sym = curr_sym->common_next;
f5acf0f2 5670 } while (curr_sym != NULL);
a8b3b0b6
CR
5671
5672 return retval;
5673}
5674
5675
5676/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5677 an appropriate error message is reported. */
5678
524af0d6 5679bool
a8b3b0b6
CR
5680verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5681 int is_in_common, gfc_common_head *com_block)
5682{
8327f9c2 5683 bool bind_c_function = false;
524af0d6 5684 bool retval = true;
d8fa96e0 5685
8327f9c2
TB
5686 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5687 bind_c_function = true;
5688
d8fa96e0
CR
5689 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5690 {
5691 tmp_sym = tmp_sym->result;
5692 /* Make sure it wasn't an implicitly typed result. */
4daa149b 5693 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
d8fa96e0 5694 {
48749dbc
MLI
5695 gfc_warning (OPT_Wc_binding_type,
5696 "Implicitly declared BIND(C) function %qs at "
d8fa96e0
CR
5697 "%L may not be C interoperable", tmp_sym->name,
5698 &tmp_sym->declared_at);
5699 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5700 /* Mark it as C interoperable to prevent duplicate warnings. */
5701 tmp_sym->ts.is_c_interop = 1;
5702 tmp_sym->attr.is_c_interop = 1;
5703 }
5704 }
8327f9c2 5705
a8b3b0b6
CR
5706 /* Here, we know we have the bind(c) attribute, so if we have
5707 enough type info, then verify that it's a C interop kind.
5708 The info could be in the symbol already, or possibly still in
5709 the given ts (current_ts), so look in both. */
f5acf0f2 5710 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
a8b3b0b6 5711 {
524af0d6 5712 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
a8b3b0b6
CR
5713 {
5714 /* See if we're dealing with a sym in a common block or not. */
4daa149b 5715 if (is_in_common == 1 && warn_c_binding_type)
a8b3b0b6 5716 {
48749dbc
MLI
5717 gfc_warning (OPT_Wc_binding_type,
5718 "Variable %qs in common block %qs at %L "
a8b3b0b6 5719 "may not be a C interoperable "
48749dbc 5720 "kind though common block %qs is BIND(C)",
a8b3b0b6
CR
5721 tmp_sym->name, com_block->name,
5722 &(tmp_sym->declared_at), com_block->name);
5723 }
5724 else
5725 {
5726 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
c4100eae 5727 gfc_error ("Type declaration %qs at %L is not C "
a8b3b0b6
CR
5728 "interoperable but it is BIND(C)",
5729 tmp_sym->name, &(tmp_sym->declared_at));
4daa149b 5730 else if (warn_c_binding_type)
48749dbc 5731 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
a8b3b0b6 5732 "may not be a C interoperable "
c4100eae 5733 "kind but it is BIND(C)",
a8b3b0b6
CR
5734 tmp_sym->name, &(tmp_sym->declared_at));
5735 }
5736 }
f5acf0f2 5737
a8b3b0b6
CR
5738 /* Variables declared w/in a common block can't be bind(c)
5739 since there's no way for C to see these variables, so there's
5740 semantically no reason for the attribute. */
5741 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5742 {
c4100eae 5743 gfc_error ("Variable %qs in common block %qs at "
a8b3b0b6
CR
5744 "%L cannot be declared with BIND(C) "
5745 "since it is not a global",
5746 tmp_sym->name, com_block->name,
5747 &(tmp_sym->declared_at));
524af0d6 5748 retval = false;
a8b3b0b6 5749 }
f5acf0f2 5750
67914693 5751 /* Scalar variables that are bind(c) cannot have the pointer
a8b3b0b6
CR
5752 or allocatable attributes. */
5753 if (tmp_sym->attr.is_bind_c == 1)
5754 {
5755 if (tmp_sym->attr.pointer == 1)
5756 {
c4100eae 5757 gfc_error ("Variable %qs at %L cannot have both the "
a8b3b0b6
CR
5758 "POINTER and BIND(C) attributes",
5759 tmp_sym->name, &(tmp_sym->declared_at));
524af0d6 5760 retval = false;
a8b3b0b6
CR
5761 }
5762
5763 if (tmp_sym->attr.allocatable == 1)
5764 {
c4100eae 5765 gfc_error ("Variable %qs at %L cannot have both the "
a8b3b0b6
CR
5766 "ALLOCATABLE and BIND(C) attributes",
5767 tmp_sym->name, &(tmp_sym->declared_at));
524af0d6 5768 retval = false;
a8b3b0b6
CR
5769 }
5770
8327f9c2
TB
5771 }
5772
5773 /* If it is a BIND(C) function, make sure the return value is a
5774 scalar value. The previous tests in this function made sure
5775 the type is interoperable. */
5776 if (bind_c_function && tmp_sym->as != NULL)
c4100eae 5777 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
8327f9c2
TB
5778 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5779
67914693 5780 /* BIND(C) functions cannot return a character string. */
8327f9c2 5781 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
bc21d315
JW
5782 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5783 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5784 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
86ba9ce6 5785 gfc_error ("Return type of BIND(C) function %qs of character "
0f4f8561 5786 "type at %L must have length 1", tmp_sym->name,
a8b3b0b6 5787 &(tmp_sym->declared_at));
a8b3b0b6
CR
5788 }
5789
5790 /* See if the symbol has been marked as private. If it has, make sure
5791 there is no binding label and warn the user if there is one. */
5792 if (tmp_sym->attr.access == ACCESS_PRIVATE
62603fae 5793 && tmp_sym->binding_label)
a8b3b0b6
CR
5794 /* Use gfc_warning_now because we won't say that the symbol fails
5795 just because of this. */
db30e21c 5796 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4daa149b 5797 "given the binding label %qs", tmp_sym->name,
a8b3b0b6
CR
5798 &(tmp_sym->declared_at), tmp_sym->binding_label);
5799
5800 return retval;
5801}
5802
5803
5804/* Set the appropriate fields for a symbol that's been declared as
5805 BIND(C) (the is_bind_c flag and the binding label), and verify that
5806 the type is C interoperable. Errors are reported by the functions
5807 used to set/test these fields. */
5808
524af0d6 5809bool
a8b3b0b6
CR
5810set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5811{
524af0d6 5812 bool retval = true;
f5acf0f2 5813
a8b3b0b6
CR
5814 /* TODO: Do we need to make sure the vars aren't marked private? */
5815
5816 /* Set the is_bind_c bit in symbol_attribute. */
5817 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5818
524af0d6
JB
5819 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5820 return false;
a8b3b0b6
CR
5821
5822 return retval;
5823}
5824
5825
5826/* Set the fields marking the given common block as BIND(C), including
5827 a binding label, and report any errors encountered. */
5828
524af0d6 5829bool
a8b3b0b6
CR
5830set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5831{
524af0d6 5832 bool retval = true;
f5acf0f2 5833
a8b3b0b6 5834 /* destLabel, common name, typespec (which may have binding label). */
70112e2a 5835 if (!set_binding_label (&com_block->binding_label, com_block->name,
524af0d6
JB
5836 num_idents))
5837 return false;
a8b3b0b6
CR
5838
5839 /* Set the given common block (com_block) to being bind(c) (1). */
5840 set_com_block_bind_c (com_block, 1);
5841
5842 return retval;
5843}
5844
5845
5846/* Retrieve the list of one or more identifiers that the given bind(c)
5847 attribute applies to. */
5848
524af0d6 5849bool
a8b3b0b6
CR
5850get_bind_c_idents (void)
5851{
5852 char name[GFC_MAX_SYMBOL_LEN + 1];
5853 int num_idents = 0;
5854 gfc_symbol *tmp_sym = NULL;
5855 match found_id;
5856 gfc_common_head *com_block = NULL;
f5acf0f2 5857
a8b3b0b6
CR
5858 if (gfc_match_name (name) == MATCH_YES)
5859 {
5860 found_id = MATCH_YES;
5861 gfc_get_ha_symbol (name, &tmp_sym);
5862 }
5863 else if (match_common_name (name) == MATCH_YES)
5864 {
5865 found_id = MATCH_YES;
5866 com_block = gfc_get_common (name, 0);
5867 }
5868 else
5869 {
5870 gfc_error ("Need either entity or common block name for "
5871 "attribute specification statement at %C");
524af0d6 5872 return false;
a8b3b0b6 5873 }
f5acf0f2 5874
a8b3b0b6
CR
5875 /* Save the current identifier and look for more. */
5876 do
5877 {
5878 /* Increment the number of identifiers found for this spec stmt. */
5879 num_idents++;
5880
5881 /* Make sure we have a sym or com block, and verify that it can
5882 be bind(c). Set the appropriate field(s) and look for more
5883 identifiers. */
f5acf0f2 5884 if (tmp_sym != NULL || com_block != NULL)
a8b3b0b6
CR
5885 {
5886 if (tmp_sym != NULL)
5887 {
524af0d6
JB
5888 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
5889 return false;
a8b3b0b6
CR
5890 }
5891 else
5892 {
524af0d6
JB
5893 if (!set_verify_bind_c_com_block (com_block, num_idents))
5894 return false;
a8b3b0b6 5895 }
f5acf0f2 5896
a8b3b0b6
CR
5897 /* Look to see if we have another identifier. */
5898 tmp_sym = NULL;
5899 if (gfc_match_eos () == MATCH_YES)
5900 found_id = MATCH_NO;
5901 else if (gfc_match_char (',') != MATCH_YES)
5902 found_id = MATCH_NO;
5903 else if (gfc_match_name (name) == MATCH_YES)
5904 {
5905 found_id = MATCH_YES;
5906 gfc_get_ha_symbol (name, &tmp_sym);
5907 }
5908 else if (match_common_name (name) == MATCH_YES)
5909 {
5910 found_id = MATCH_YES;
5911 com_block = gfc_get_common (name, 0);
5912 }
5913 else
5914 {
5915 gfc_error ("Missing entity or common block name for "
5916 "attribute specification statement at %C");
524af0d6 5917 return false;
a8b3b0b6
CR
5918 }
5919 }
5920 else
5921 {
5922 gfc_internal_error ("Missing symbol");
5923 }
5924 } while (found_id == MATCH_YES);
5925
5926 /* if we get here we were successful */
524af0d6 5927 return true;
a8b3b0b6
CR
5928}
5929
5930
5931/* Try and match a BIND(C) attribute specification statement. */
f5acf0f2 5932
a8b3b0b6
CR
5933match
5934gfc_match_bind_c_stmt (void)
5935{
5936 match found_match = MATCH_NO;
5937 gfc_typespec *ts;
5938
5939 ts = &current_ts;
f5acf0f2 5940
a8b3b0b6
CR
5941 /* This may not be necessary. */
5942 gfc_clear_ts (ts);
5943 /* Clear the temporary binding label holder. */
62603fae 5944 curr_binding_label = NULL;
a8b3b0b6
CR
5945
5946 /* Look for the bind(c). */
1eabf70a 5947 found_match = gfc_match_bind_c (NULL, true);
a8b3b0b6
CR
5948
5949 if (found_match == MATCH_YES)
5950 {
878cdb7b
TB
5951 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
5952 return MATCH_ERROR;
5953
a8b3b0b6
CR
5954 /* Look for the :: now, but it is not required. */
5955 gfc_match (" :: ");
5956
5957 /* Get the identifier(s) that needs to be updated. This may need to
5958 change to hand the flag(s) for the attr specified so all identifiers
5959 found can have all appropriate parts updated (assuming that the same
5960 spec stmt can have multiple attrs, such as both bind(c) and
5961 allocatable...). */
524af0d6 5962 if (!get_bind_c_idents ())
a8b3b0b6
CR
5963 /* Error message should have printed already. */
5964 return MATCH_ERROR;
5965 }
5966
5967 return found_match;
5968}
5969
5970
6de9cd9a
DN
5971/* Match a data declaration statement. */
5972
5973match
5974gfc_match_data_decl (void)
5975{
5976 gfc_symbol *sym;
5977 match m;
949d5b72 5978 int elem;
6de9cd9a 5979
5bab4c96
PT
5980 type_param_spec_list = NULL;
5981 decl_type_param_list = NULL;
5982
a8b3b0b6 5983 num_idents_on_line = 0;
f5acf0f2 5984
e74f1cc8 5985 m = gfc_match_decl_type_spec (&current_ts, 0);
6de9cd9a
DN
5986 if (m != MATCH_YES)
5987 return m;
5988
2e23972e 5989 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
f6288c24 5990 && !gfc_comp_struct (gfc_current_state ()))
6de9cd9a 5991 {
bc21d315 5992 sym = gfc_use_derived (current_ts.u.derived);
6de9cd9a
DN
5993
5994 if (sym == NULL)
5995 {
5996 m = MATCH_ERROR;
5997 goto cleanup;
5998 }
5999
bc21d315 6000 current_ts.u.derived = sym;
6de9cd9a
DN
6001 }
6002
6003 m = match_attr_spec ();
6004 if (m == MATCH_ERROR)
6005 {
6006 m = MATCH_NO;
6007 goto cleanup;
6008 }
6009
8b704316
PT
6010 if (current_ts.type == BT_CLASS
6011 && current_ts.u.derived->attr.unlimited_polymorphic)
6012 goto ok;
6013
2e23972e
JW
6014 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6015 && current_ts.u.derived->components == NULL
bc21d315 6016 && !current_ts.u.derived->attr.zero_comp)
6de9cd9a
DN
6017 {
6018
f6288c24 6019 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
6de9cd9a
DN
6020 goto ok;
6021
00cad178 6022 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
bf9f15ee
PT
6023 goto ok;
6024
bc21d315 6025 gfc_find_symbol (current_ts.u.derived->name,
dd8b9dde 6026 current_ts.u.derived->ns, 1, &sym);
6de9cd9a 6027
976e21f6 6028 /* Any symbol that we find had better be a type definition
f6288c24
FR
6029 which has its components defined, or be a structure definition
6030 actively being parsed. */
6031 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
bc21d315 6032 && (current_ts.u.derived->components != NULL
f6288c24
FR
6033 || current_ts.u.derived->attr.zero_comp
6034 || current_ts.u.derived == gfc_new_block))
6de9cd9a
DN
6035 goto ok;
6036
a1b80ec7
JW
6037 gfc_error ("Derived type at %C has not been previously defined "
6038 "and so cannot appear in a derived type definition");
6039 m = MATCH_ERROR;
6040 goto cleanup;
6de9cd9a
DN
6041 }
6042
6043ok:
6044 /* If we have an old-style character declaration, and no new-style
6045 attribute specifications, then there a comma is optional between
6046 the type specification and the variable list. */
6047 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6048 gfc_match_char (',');
6049
949d5b72
PT
6050 /* Give the types/attributes to symbols that follow. Give the element
6051 a number so that repeat character length expressions can be copied. */
6052 elem = 1;
6de9cd9a
DN
6053 for (;;)
6054 {
a8b3b0b6 6055 num_idents_on_line++;
949d5b72 6056 m = variable_decl (elem++);
6de9cd9a
DN
6057 if (m == MATCH_ERROR)
6058 goto cleanup;
6059 if (m == MATCH_NO)
6060 break;
6061
6062 if (gfc_match_eos () == MATCH_YES)
6063 goto cleanup;
6064 if (gfc_match_char (',') != MATCH_YES)
6065 break;
6066 }
6067
0f447a6e 6068 if (!gfc_error_flag_test ())
94903212
FR
6069 {
6070 /* An anonymous structure declaration is unambiguous; if we matched one
6071 according to gfc_match_structure_decl, we need to return MATCH_YES
6072 here to avoid confusing the remaining matchers, even if there was an
6073 error during variable_decl. We must flush any such errors. Note this
6074 causes the parser to gracefully continue parsing the remaining input
6075 as a structure body, which likely follows. */
6076 if (current_ts.type == BT_DERIVED && current_ts.u.derived
6077 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6078 {
6079 gfc_error_now ("Syntax error in anonymous structure declaration"
6080 " at %C");
6081 /* Skip the bad variable_decl and line up for the start of the
6082 structure body. */
6083 gfc_error_recovery ();
6084 m = MATCH_YES;
6085 goto cleanup;
6086 }
6087
6088 gfc_error ("Syntax error in data declaration at %C");
6089 }
6090
6de9cd9a
DN
6091 m = MATCH_ERROR;
6092
a9f6f1f2
JD
6093 gfc_free_data_all (gfc_current_ns);
6094
6de9cd9a 6095cleanup:
5bab4c96
PT
6096 if (saved_kind_expr)
6097 gfc_free_expr (saved_kind_expr);
6098 if (type_param_spec_list)
6099 gfc_free_actual_arglist (type_param_spec_list);
6100 if (decl_type_param_list)
6101 gfc_free_actual_arglist (decl_type_param_list);
6102 saved_kind_expr = NULL;
6de9cd9a
DN
6103 gfc_free_array_spec (current_as);
6104 current_as = NULL;
6105 return m;
6106}
6107
2810dfab
SK
6108static bool
6109in_module_or_interface(void)
6110{
6111 if (gfc_current_state () == COMP_MODULE
0a524296 6112 || gfc_current_state () == COMP_SUBMODULE
2810dfab
SK
6113 || gfc_current_state () == COMP_INTERFACE)
6114 return true;
6115
6116 if (gfc_state_stack->state == COMP_CONTAINS
6117 || gfc_state_stack->state == COMP_FUNCTION
6118 || gfc_state_stack->state == COMP_SUBROUTINE)
6119 {
6120 gfc_state_data *p;
6121 for (p = gfc_state_stack->previous; p ; p = p->previous)
6122 {
0a524296 6123 if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
2810dfab
SK
6124 || p->state == COMP_INTERFACE)
6125 return true;
6126 }
6127 }
6128 return false;
6129}
6de9cd9a
DN
6130
6131/* Match a prefix associated with a function or subroutine
6132 declaration. If the typespec pointer is nonnull, then a typespec
6133 can be matched. Note that if nothing matches, MATCH_YES is
6134 returned (the null string was matched). */
6135
1c8bcdf7
PT
6136match
6137gfc_match_prefix (gfc_typespec *ts)
6de9cd9a 6138{
7389bce6 6139 bool seen_type;
e6c14898
DK
6140 bool seen_impure;
6141 bool found_prefix;
6de9cd9a
DN
6142
6143 gfc_clear_attr (&current_attr);
e6c14898
DK
6144 seen_type = false;
6145 seen_impure = false;
6de9cd9a 6146
3df684e2
DK
6147 gcc_assert (!gfc_matching_prefix);
6148 gfc_matching_prefix = true;
f37e928c 6149
e6c14898 6150 do
6de9cd9a 6151 {
e6c14898 6152 found_prefix = false;
6de9cd9a 6153
70112e2a
PT
6154 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6155 corresponding attribute seems natural and distinguishes these
6156 procedures from procedure types of PROC_MODULE, which these are
6157 as well. */
6158 if (gfc_match ("module% ") == MATCH_YES)
6159 {
6160 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6161 goto error;
6162
2810dfab
SK
6163 if (!in_module_or_interface ())
6164 {
6165 gfc_error ("MODULE prefix at %C found outside of a module, "
6166 "submodule, or interface");
6167 goto error;
6168 }
6169
70112e2a
PT
6170 current_attr.module_procedure = 1;
6171 found_prefix = true;
6172 }
6173
e6c14898
DK
6174 if (!seen_type && ts != NULL
6175 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
6176 && gfc_match_space () == MATCH_YES)
6177 {
6de9cd9a 6178
e6c14898
DK
6179 seen_type = true;
6180 found_prefix = true;
6181 }
6182
6183 if (gfc_match ("elemental% ") == MATCH_YES)
6184 {
524af0d6 6185 if (!gfc_add_elemental (&current_attr, NULL))
e6c14898
DK
6186 goto error;
6187
6188 found_prefix = true;
6189 }
6190
6191 if (gfc_match ("pure% ") == MATCH_YES)
6192 {
524af0d6 6193 if (!gfc_add_pure (&current_attr, NULL))
e6c14898
DK
6194 goto error;
6195
6196 found_prefix = true;
6197 }
6de9cd9a 6198
e6c14898
DK
6199 if (gfc_match ("recursive% ") == MATCH_YES)
6200 {
524af0d6 6201 if (!gfc_add_recursive (&current_attr, NULL))
e6c14898
DK
6202 goto error;
6203
6204 found_prefix = true;
6205 }
6206
6207 /* IMPURE is a somewhat special case, as it needs not set an actual
6208 attribute but rather only prevents ELEMENTAL routines from being
6209 automatically PURE. */
6210 if (gfc_match ("impure% ") == MATCH_YES)
6211 {
524af0d6 6212 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
e6c14898
DK
6213 goto error;
6214
6215 seen_impure = true;
6216 found_prefix = true;
6217 }
6de9cd9a 6218 }
e6c14898 6219 while (found_prefix);
6de9cd9a 6220
e6c14898
DK
6221 /* IMPURE and PURE must not both appear, of course. */
6222 if (seen_impure && current_attr.pure)
6de9cd9a 6223 {
e6c14898
DK
6224 gfc_error ("PURE and IMPURE must not appear both at %C");
6225 goto error;
6de9cd9a
DN
6226 }
6227
e6c14898
DK
6228 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6229 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6de9cd9a 6230 {
524af0d6 6231 if (!gfc_add_pure (&current_attr, NULL))
f37e928c 6232 goto error;
6de9cd9a
DN
6233 }
6234
6235 /* At this point, the next item is not a prefix. */
3df684e2 6236 gcc_assert (gfc_matching_prefix);
4668d6f9 6237
3df684e2 6238 gfc_matching_prefix = false;
6de9cd9a 6239 return MATCH_YES;
f37e928c
DK
6240
6241error:
3df684e2
DK
6242 gcc_assert (gfc_matching_prefix);
6243 gfc_matching_prefix = false;
f37e928c 6244 return MATCH_ERROR;
6de9cd9a
DN
6245}
6246
6247
1c8bcdf7 6248/* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6de9cd9a 6249
524af0d6 6250static bool
636dff67 6251copy_prefix (symbol_attribute *dest, locus *where)
6de9cd9a 6252{
6442a6f4
PT
6253 if (dest->module_procedure)
6254 {
6255 if (current_attr.elemental)
6256 dest->elemental = 1;
6257
6258 if (current_attr.pure)
6259 dest->pure = 1;
6260
6261 if (current_attr.recursive)
6262 dest->recursive = 1;
6263
6264 /* Module procedures are unusual in that the 'dest' is copied from
6265 the interface declaration. However, this is an oportunity to
6266 check that the submodule declaration is compliant with the
6267 interface. */
6268 if (dest->elemental && !current_attr.elemental)
6269 {
6270 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6271 "missing at %L", where);
6272 return false;
6273 }
6274
6275 if (dest->pure && !current_attr.pure)
6276 {
6277 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6278 "missing at %L", where);
6279 return false;
6280 }
6281
6282 if (dest->recursive && !current_attr.recursive)
6283 {
6284 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6285 "missing at %L", where);
6286 return false;
6287 }
6288
6289 return true;
6290 }
6de9cd9a 6291
524af0d6
JB
6292 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6293 return false;
6de9cd9a 6294
6442a6f4
PT
6295 if (current_attr.pure && !gfc_add_pure (dest, where))
6296 return false;
6297
524af0d6
JB
6298 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6299 return false;
6de9cd9a 6300
524af0d6 6301 return true;
6de9cd9a
DN
6302}
6303
6304
5bab4c96
PT
6305/* Match a formal argument list or, if typeparam is true, a
6306 type_param_name_list. */
6de9cd9a
DN
6307
6308match
5bab4c96
PT
6309gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6310 int null_flag, bool typeparam)
6de9cd9a
DN
6311{
6312 gfc_formal_arglist *head, *tail, *p, *q;
6313 char name[GFC_MAX_SYMBOL_LEN + 1];
6314 gfc_symbol *sym;
6315 match m;
4668d6f9 6316 gfc_formal_arglist *formal = NULL;
6de9cd9a
DN
6317
6318 head = tail = NULL;
6319
4668d6f9
PT
6320 /* Keep the interface formal argument list and null it so that the
6321 matching for the new declaration can be done. The numbers and
6322 names of the arguments are checked here. The interface formal
6323 arguments are retained in formal_arglist and the characteristics
6324 are compared in resolve.c(resolve_fl_procedure). See the remark
6325 in get_proc_name about the eventual need to copy the formal_arglist
6326 and populate the formal namespace of the interface symbol. */
6327 if (progname->attr.module_procedure
6328 && progname->attr.host_assoc)
6329 {
6330 formal = progname->formal;
6331 progname->formal = NULL;
6332 }
6333
6de9cd9a
DN
6334 if (gfc_match_char ('(') != MATCH_YES)
6335 {
6336 if (null_flag)
6337 goto ok;
6338 return MATCH_NO;
6339 }
6340
6341 if (gfc_match_char (')') == MATCH_YES)
0a524296 6342 {
84083a71
JW
6343 if (typeparam)
6344 {
6345 gfc_error_now ("A type parameter list is required at %C");
6346 m = MATCH_ERROR;
6347 goto cleanup;
6348 }
6349 else
6350 goto ok;
6351 }
6de9cd9a
DN
6352
6353 for (;;)
6354 {
6355 if (gfc_match_char ('*') == MATCH_YES)
9362a03b
JW
6356 {
6357 sym = NULL;
276515e6
PT
6358 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6359 "Alternate-return argument at %C"))
9362a03b
JW
6360 {
6361 m = MATCH_ERROR;
6362 goto cleanup;
6363 }
276515e6
PT
6364 else if (typeparam)
6365 gfc_error_now ("A parameter name is required at %C");
9362a03b 6366 }
6de9cd9a
DN
6367 else
6368 {
6369 m = gfc_match_name (name);
6370 if (m != MATCH_YES)
276515e6
PT
6371 {
6372 if(typeparam)
6373 gfc_error_now ("A parameter name is required at %C");
6374 goto cleanup;
6375 }
6de9cd9a 6376
5bab4c96
PT
6377 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6378 goto cleanup;
6379 else if (typeparam
6380 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6de9cd9a
DN
6381 goto cleanup;
6382 }
6383
6384 p = gfc_get_formal_arglist ();
6385
6386 if (head == NULL)
6387 head = tail = p;
6388 else
6389 {
6390 tail->next = p;
6391 tail = p;
6392 }
6393
6394 tail->sym = sym;
6395
6396 /* We don't add the VARIABLE flavor because the name could be a
636dff67
SK
6397 dummy procedure. We don't apply these attributes to formal
6398 arguments of statement functions. */
6de9cd9a 6399 if (sym != NULL && !st_flag
524af0d6
JB
6400 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6401 || !gfc_missing_attr (&sym->attr, NULL)))
6de9cd9a
DN
6402 {
6403 m = MATCH_ERROR;
6404 goto cleanup;
6405 }
6406
6407 /* The name of a program unit can be in a different namespace,
636dff67
SK
6408 so check for it explicitly. After the statement is accepted,
6409 the name is checked for especially in gfc_get_symbol(). */
de624bee 6410 if (gfc_new_block != NULL && sym != NULL && !typeparam
6de9cd9a
DN
6411 && strcmp (sym->name, gfc_new_block->name) == 0)
6412 {
c4100eae 6413 gfc_error ("Name %qs at %C is the name of the procedure",
6de9cd9a
DN
6414 sym->name);
6415 m = MATCH_ERROR;
6416 goto cleanup;
6417 }
6418
6419 if (gfc_match_char (')') == MATCH_YES)
6420 goto ok;
6421
6422 m = gfc_match_char (',');
6423 if (m != MATCH_YES)
6424 {
de624bee
PT
6425 if (typeparam)
6426 gfc_error_now ("Expected parameter list in type declaration "
6427 "at %C");
6428 else
6429 gfc_error ("Unexpected junk in formal argument list at %C");
6de9cd9a
DN
6430 goto cleanup;
6431 }
6432 }
6433
6434ok:
6435 /* Check for duplicate symbols in the formal argument list. */
6436 if (head != NULL)
6437 {
6438 for (p = head; p->next; p = p->next)
6439 {
6440 if (p->sym == NULL)
6441 continue;
6442
6443 for (q = p->next; q; q = q->next)
6444 if (p->sym == q->sym)
6445 {
de624bee
PT
6446 if (typeparam)
6447 gfc_error_now ("Duplicate name %qs in parameter "
6448 "list at %C", p->sym->name);
6449 else
6450 gfc_error ("Duplicate symbol %qs in formal argument "
6451 "list at %C", p->sym->name);
6de9cd9a
DN
6452
6453 m = MATCH_ERROR;
6454 goto cleanup;
6455 }
6456 }
6457 }
6458
524af0d6 6459 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6de9cd9a
DN
6460 {
6461 m = MATCH_ERROR;
6462 goto cleanup;
6463 }
6464
e9d9b48d
PT
6465 /* gfc_error_now used in following and return with MATCH_YES because
6466 doing otherwise results in a cascade of extraneous errors and in
6467 some cases an ICE in symbol.c(gfc_release_symbol). */
0ef5fbc1 6468 if (progname->attr.module_procedure && progname->attr.host_assoc)
4668d6f9 6469 {
0ef5fbc1
PT
6470 bool arg_count_mismatch = false;
6471
6472 if (!formal && head)
6473 arg_count_mismatch = true;
6474
6475 /* Abbreviated module procedure declaration is not meant to have any
6476 formal arguments! */
e9d9b48d 6477 if (!progname->abr_modproc_decl && formal && !head)
0ef5fbc1
PT
6478 arg_count_mismatch = true;
6479
4668d6f9
PT
6480 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6481 {
6482 if ((p->next != NULL && q->next == NULL)
6483 || (p->next == NULL && q->next != NULL))
0ef5fbc1 6484 arg_count_mismatch = true;
4668d6f9
PT
6485 else if ((p->sym == NULL && q->sym == NULL)
6486 || strcmp (p->sym->name, q->sym->name) == 0)
6487 continue;
6488 else
6489 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6490 "argument names (%s/%s) at %C",
6491 p->sym->name, q->sym->name);
6492 }
0ef5fbc1
PT
6493
6494 if (arg_count_mismatch)
6495 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6496 "formal arguments at %C");
4668d6f9
PT
6497 }
6498
6de9cd9a
DN
6499 return MATCH_YES;
6500
6501cleanup:
6502 gfc_free_formal_arglist (head);
6503 return m;
6504}
6505
6506
6507/* Match a RESULT specification following a function declaration or
6508 ENTRY statement. Also matches the end-of-statement. */
6509
6510static match
66e4ab31 6511match_result (gfc_symbol *function, gfc_symbol **result)
6de9cd9a
DN
6512{
6513 char name[GFC_MAX_SYMBOL_LEN + 1];
6514 gfc_symbol *r;
6515 match m;
6516
6517 if (gfc_match (" result (") != MATCH_YES)
6518 return MATCH_NO;
6519
6520 m = gfc_match_name (name);
6521 if (m != MATCH_YES)
6522 return m;
6523
a8b3b0b6
CR
6524 /* Get the right paren, and that's it because there could be the
6525 bind(c) attribute after the result clause. */
524af0d6 6526 if (gfc_match_char (')') != MATCH_YES)
6de9cd9a 6527 {
a8b3b0b6 6528 /* TODO: should report the missing right paren here. */
6de9cd9a
DN
6529 return MATCH_ERROR;
6530 }
6531
6532 if (strcmp (function->name, name) == 0)
6533 {
636dff67 6534 gfc_error ("RESULT variable at %C must be different than function name");
6de9cd9a
DN
6535 return MATCH_ERROR;
6536 }
6537
6538 if (gfc_get_symbol (name, NULL, &r))
6539 return MATCH_ERROR;
6540
524af0d6 6541 if (!gfc_add_result (&r->attr, r->name, NULL))
6de9cd9a
DN
6542 return MATCH_ERROR;
6543
6544 *result = r;
6545
6546 return MATCH_YES;
6547}
6548
6549
a8b3b0b6
CR
6550/* Match a function suffix, which could be a combination of a result
6551 clause and BIND(C), either one, or neither. The draft does not
6552 require them to come in a specific order. */
6553
6554match
6555gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6556{
6557 match is_bind_c; /* Found bind(c). */
6558 match is_result; /* Found result clause. */
6559 match found_match; /* Status of whether we've found a good match. */
8fc541d3 6560 char peek_char; /* Character we're going to peek at. */
1eabf70a 6561 bool allow_binding_name;
a8b3b0b6
CR
6562
6563 /* Initialize to having found nothing. */
6564 found_match = MATCH_NO;
f5acf0f2 6565 is_bind_c = MATCH_NO;
a8b3b0b6
CR
6566 is_result = MATCH_NO;
6567
6568 /* Get the next char to narrow between result and bind(c). */
6569 gfc_gobble_whitespace ();
8fc541d3 6570 peek_char = gfc_peek_ascii_char ();
a8b3b0b6 6571
1eabf70a
TB
6572 /* C binding names are not allowed for internal procedures. */
6573 if (gfc_current_state () == COMP_CONTAINS
6574 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6575 allow_binding_name = false;
6576 else
6577 allow_binding_name = true;
6578
a8b3b0b6
CR
6579 switch (peek_char)
6580 {
6581 case 'r':
6582 /* Look for result clause. */
6583 is_result = match_result (sym, result);
6584 if (is_result == MATCH_YES)
6585 {
6586 /* Now see if there is a bind(c) after it. */
1eabf70a 6587 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
6588 /* We've found the result clause and possibly bind(c). */
6589 found_match = MATCH_YES;
6590 }
6591 else
6592 /* This should only be MATCH_ERROR. */
f5acf0f2 6593 found_match = is_result;
a8b3b0b6
CR
6594 break;
6595 case 'b':
6596 /* Look for bind(c) first. */
1eabf70a 6597 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
6598 if (is_bind_c == MATCH_YES)
6599 {
6600 /* Now see if a result clause followed it. */
6601 is_result = match_result (sym, result);
6602 found_match = MATCH_YES;
6603 }
6604 else
6605 {
6606 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6607 found_match = MATCH_ERROR;
6608 }
6609 break;
6610 default:
6611 gfc_error ("Unexpected junk after function declaration at %C");
6612 found_match = MATCH_ERROR;
6613 break;
6614 }
6615
a8b3b0b6 6616 if (is_bind_c == MATCH_YES)
01f4fff1 6617 {
1eabf70a 6618 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
01f4fff1 6619 if (gfc_current_state () == COMP_CONTAINS
1eabf70a 6620 && sym->ns->proc_name->attr.flavor != FL_MODULE
524af0d6
JB
6621 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6622 "at %L may not be specified for an internal "
6623 "procedure", &gfc_current_locus))
1eabf70a
TB
6624 return MATCH_ERROR;
6625
524af0d6 6626 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
01f4fff1
TB
6627 return MATCH_ERROR;
6628 }
f5acf0f2 6629
a8b3b0b6
CR
6630 return found_match;
6631}
6632
6633
3070bab4
JW
6634/* Procedure pointer return value without RESULT statement:
6635 Add "hidden" result variable named "ppr@". */
6636
524af0d6 6637static bool
3070bab4
JW
6638add_hidden_procptr_result (gfc_symbol *sym)
6639{
6640 bool case1,case2;
6641
6642 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
524af0d6 6643 return false;
3070bab4
JW
6644
6645 /* First usage case: PROCEDURE and EXTERNAL statements. */
6646 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6647 && strcmp (gfc_current_block ()->name, sym->name) == 0
6648 && sym->attr.external;
6649 /* Second usage case: INTERFACE statements. */
6650 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6651 && gfc_state_stack->previous->state == COMP_FUNCTION
6652 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6653
6654 if (case1 || case2)
6655 {
6656 gfc_symtree *stree;
6657 if (case1)
08a6b8e0 6658 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
5433e401 6659 else
c73b6478
JW
6660 {
6661 gfc_symtree *st2;
08a6b8e0 6662 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
c73b6478
JW
6663 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6664 st2->n.sym = stree->n.sym;
8c0f9dab 6665 stree->n.sym->refs++;
c73b6478 6666 }
3070bab4
JW
6667 sym->result = stree->n.sym;
6668
6669 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6670 sym->result->attr.pointer = sym->attr.pointer;
6671 sym->result->attr.external = sym->attr.external;
6672 sym->result->attr.referenced = sym->attr.referenced;
fc9c6e5d 6673 sym->result->ts = sym->ts;
3070bab4
JW
6674 sym->attr.proc_pointer = 0;
6675 sym->attr.pointer = 0;
6676 sym->attr.external = 0;
6677 if (sym->result->attr.external && sym->result->attr.pointer)
6678 {
6679 sym->result->attr.pointer = 0;
6680 sym->result->attr.proc_pointer = 1;
6681 }
6682
6683 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6684 }
6685 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6686 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6687 && sym->result && sym->result != sym && sym->result->attr.external
6688 && sym == gfc_current_ns->proc_name
6689 && sym == sym->result->ns->proc_name
6690 && strcmp ("ppr@", sym->result->name) == 0)
6691 {
6692 sym->result->attr.proc_pointer = 1;
6693 sym->attr.pointer = 0;
524af0d6 6694 return true;
3070bab4
JW
6695 }
6696 else
524af0d6 6697 return false;
3070bab4
JW
6698}
6699
6700
713485cc
JW
6701/* Match the interface for a PROCEDURE declaration,
6702 including brackets (R1212). */
69773742
JW
6703
6704static match
713485cc 6705match_procedure_interface (gfc_symbol **proc_if)
69773742
JW
6706{
6707 match m;
3276e0b3 6708 gfc_symtree *st;
69773742 6709 locus old_loc, entry_loc;
3276e0b3
PT
6710 gfc_namespace *old_ns = gfc_current_ns;
6711 char name[GFC_MAX_SYMBOL_LEN + 1];
69773742 6712
3276e0b3 6713 old_loc = entry_loc = gfc_current_locus;
69773742
JW
6714 gfc_clear_ts (&current_ts);
6715
6716 if (gfc_match (" (") != MATCH_YES)
6717 {
6718 gfc_current_locus = entry_loc;
6719 return MATCH_NO;
6720 }
6721
6722 /* Get the type spec. for the procedure interface. */
6723 old_loc = gfc_current_locus;
e74f1cc8 6724 m = gfc_match_decl_type_spec (&current_ts, 0);
f4256439 6725 gfc_gobble_whitespace ();
8fc541d3 6726 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
69773742
JW
6727 goto got_ts;
6728
6729 if (m == MATCH_ERROR)
6730 return m;
6731
3276e0b3 6732 /* Procedure interface is itself a procedure. */
69773742 6733 gfc_current_locus = old_loc;
3276e0b3 6734 m = gfc_match_name (name);
69773742 6735
3276e0b3
PT
6736 /* First look to see if it is already accessible in the current
6737 namespace because it is use associated or contained. */
6738 st = NULL;
6739 if (gfc_find_sym_tree (name, NULL, 0, &st))
6740 return MATCH_ERROR;
6741
6742 /* If it is still not found, then try the parent namespace, if it
6743 exists and create the symbol there if it is still not found. */
6744 if (gfc_current_ns->parent)
6745 gfc_current_ns = gfc_current_ns->parent;
6746 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6747 return MATCH_ERROR;
6748
6749 gfc_current_ns = old_ns;
6750 *proc_if = st->n.sym;
69773742 6751
713485cc 6752 if (*proc_if)
69773742 6753 {
713485cc 6754 (*proc_if)->refs++;
bb343a6c
TB
6755 /* Resolve interface if possible. That way, attr.procedure is only set
6756 if it is declared by a later procedure-declaration-stmt, which is
0e8d854e 6757 invalid per F08:C1216 (cf. resolve_procedure_interface). */
d73e0ccf
JD
6758 while ((*proc_if)->ts.interface
6759 && *proc_if != (*proc_if)->ts.interface)
713485cc 6760 *proc_if = (*proc_if)->ts.interface;
bb343a6c 6761
0e8d854e
JW
6762 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6763 && (*proc_if)->ts.type == BT_UNKNOWN
70112e2a 6764 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
524af0d6 6765 (*proc_if)->name, NULL))
0e8d854e 6766 return MATCH_ERROR;
69773742
JW
6767 }
6768
6769got_ts:
69773742
JW
6770 if (gfc_match (" )") != MATCH_YES)
6771 {
6772 gfc_current_locus = entry_loc;
6773 return MATCH_NO;
6774 }
6775
713485cc
JW
6776 return MATCH_YES;
6777}
6778
6779
6780/* Match a PROCEDURE declaration (R1211). */
6781
6782static match
6783match_procedure_decl (void)
6784{
6785 match m;
6786 gfc_symbol *sym, *proc_if = NULL;
6787 int num;
6788 gfc_expr *initializer = NULL;
6789
1cc0e193 6790 /* Parse interface (with brackets). */
713485cc
JW
6791 m = match_procedure_interface (&proc_if);
6792 if (m != MATCH_YES)
6793 return m;
6794
6795 /* Parse attributes (with colons). */
69773742
JW
6796 m = match_attr_spec();
6797 if (m == MATCH_ERROR)
6798 return MATCH_ERROR;
6799
0859be17
TB
6800 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6801 {
6802 current_attr.is_bind_c = 1;
6803 has_name_equals = 0;
6804 curr_binding_label = NULL;
6805 }
6806
69773742
JW
6807 /* Get procedure symbols. */
6808 for(num=1;;num++)
6809 {
69773742
JW
6810 m = gfc_match_symbol (&sym, 0);
6811 if (m == MATCH_NO)
6812 goto syntax;
6813 else if (m == MATCH_ERROR)
6814 return m;
6815
6816 /* Add current_attr to the symbol attributes. */
524af0d6 6817 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
69773742
JW
6818 return MATCH_ERROR;
6819
6820 if (sym->attr.is_bind_c)
6821 {
6822 /* Check for C1218. */
6823 if (!proc_if || !proc_if->attr.is_bind_c)
6824 {
6825 gfc_error ("BIND(C) attribute at %C requires "
6826 "an interface with BIND(C)");
6827 return MATCH_ERROR;
6828 }
6829 /* Check for C1217. */
6830 if (has_name_equals && sym->attr.pointer)
6831 {
6832 gfc_error ("BIND(C) procedure with NAME may not have "
6833 "POINTER attribute at %C");
6834 return MATCH_ERROR;
6835 }
6836 if (has_name_equals && sym->attr.dummy)
6837 {
6838 gfc_error ("Dummy procedure at %C may not have "
6839 "BIND(C) attribute with NAME");
6840 return MATCH_ERROR;
6841 }
6842 /* Set binding label for BIND(C). */
524af0d6 6843 if (!set_binding_label (&sym->binding_label, sym->name, num))
69773742
JW
6844 return MATCH_ERROR;
6845 }
6846
524af0d6 6847 if (!gfc_add_external (&sym->attr, NULL))
69773742 6848 return MATCH_ERROR;
3070bab4 6849
524af0d6 6850 if (add_hidden_procptr_result (sym))
3070bab4
JW
6851 sym = sym->result;
6852
524af0d6 6853 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
69773742
JW
6854 return MATCH_ERROR;
6855
6856 /* Set interface. */
6857 if (proc_if != NULL)
6cc309c9 6858 {
1d146030
JW
6859 if (sym->ts.type != BT_UNKNOWN)
6860 {
c4100eae 6861 gfc_error ("Procedure %qs at %L already has basic type of %s",
1d146030
JW
6862 sym->name, &gfc_current_locus,
6863 gfc_basic_typename (sym->ts.type));
6864 return MATCH_ERROR;
6865 }
32d99e68 6866 sym->ts.interface = proc_if;
6cc309c9 6867 sym->attr.untyped = 1;
c73b6478 6868 sym->attr.if_source = IFSRC_IFBODY;
6cc309c9 6869 }
69773742
JW
6870 else if (current_ts.type != BT_UNKNOWN)
6871 {
524af0d6 6872 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
1d146030 6873 return MATCH_ERROR;
32d99e68
JW
6874 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
6875 sym->ts.interface->ts = current_ts;
d91909c0 6876 sym->ts.interface->attr.flavor = FL_PROCEDURE;
32d99e68 6877 sym->ts.interface->attr.function = 1;
d91909c0 6878 sym->attr.function = 1;
c73b6478 6879 sym->attr.if_source = IFSRC_UNKNOWN;
69773742
JW
6880 }
6881
8fb74da4
JW
6882 if (gfc_match (" =>") == MATCH_YES)
6883 {
6884 if (!current_attr.pointer)
6885 {
6886 gfc_error ("Initialization at %C isn't for a pointer variable");
6887 m = MATCH_ERROR;
6888 goto cleanup;
6889 }
6890
80f95228 6891 m = match_pointer_init (&initializer, 1);
8fb74da4
JW
6892 if (m != MATCH_YES)
6893 goto cleanup;
6894
524af0d6 6895 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
8fb74da4
JW
6896 goto cleanup;
6897
6898 }
6899
69773742
JW
6900 if (gfc_match_eos () == MATCH_YES)
6901 return MATCH_YES;
6902 if (gfc_match_char (',') != MATCH_YES)
6903 goto syntax;
6904 }
6905
6906syntax:
6907 gfc_error ("Syntax error in PROCEDURE statement at %C");
6908 return MATCH_ERROR;
8fb74da4
JW
6909
6910cleanup:
6911 /* Free stuff up and return. */
6912 gfc_free_expr (initializer);
6913 return m;
69773742
JW
6914}
6915
6916
713485cc
JW
6917static match
6918match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
6919
6920
6921/* Match a procedure pointer component declaration (R445). */
6922
6923static match
6924match_ppc_decl (void)
6925{
6926 match m;
6927 gfc_symbol *proc_if = NULL;
6928 gfc_typespec ts;
6929 int num;
6930 gfc_component *c;
6931 gfc_expr *initializer = NULL;
6932 gfc_typebound_proc* tb;
6933 char name[GFC_MAX_SYMBOL_LEN + 1];
6934
6935 /* Parse interface (with brackets). */
6936 m = match_procedure_interface (&proc_if);
6937 if (m != MATCH_YES)
6938 goto syntax;
6939
6940 /* Parse attributes. */
6941 tb = XCNEW (gfc_typebound_proc);
6942 tb->where = gfc_current_locus;
6943 m = match_binding_attributes (tb, false, true);
6944 if (m == MATCH_ERROR)
6945 return m;
6946
713485cc
JW
6947 gfc_clear_attr (&current_attr);
6948 current_attr.procedure = 1;
6949 current_attr.proc_pointer = 1;
6950 current_attr.access = tb->access;
6951 current_attr.flavor = FL_PROCEDURE;
6952
6953 /* Match the colons (required). */
6954 if (gfc_match (" ::") != MATCH_YES)
6955 {
a4d9b221 6956 gfc_error ("Expected %<::%> after binding-attributes at %C");
713485cc
JW
6957 return MATCH_ERROR;
6958 }
6959
6960 /* Check for C450. */
6961 if (!tb->nopass && proc_if == NULL)
6962 {
6963 gfc_error("NOPASS or explicit interface required at %C");
6964 return MATCH_ERROR;
6965 }
6966
524af0d6 6967 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
3212c187
SK
6968 return MATCH_ERROR;
6969
713485cc
JW
6970 /* Match PPC names. */
6971 ts = current_ts;
6972 for(num=1;;num++)
6973 {
6974 m = gfc_match_name (name);
6975 if (m == MATCH_NO)
6976 goto syntax;
6977 else if (m == MATCH_ERROR)
6978 return m;
6979
524af0d6 6980 if (!gfc_add_component (gfc_current_block(), name, &c))
713485cc
JW
6981 return MATCH_ERROR;
6982
6983 /* Add current_attr to the symbol attributes. */
524af0d6 6984 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
713485cc
JW
6985 return MATCH_ERROR;
6986
524af0d6 6987 if (!gfc_add_external (&c->attr, NULL))
713485cc
JW
6988 return MATCH_ERROR;
6989
524af0d6 6990 if (!gfc_add_proc (&c->attr, name, NULL))
713485cc
JW
6991 return MATCH_ERROR;
6992
2be03814
TB
6993 if (num == 1)
6994 c->tb = tb;
6995 else
6996 {
6997 c->tb = XCNEW (gfc_typebound_proc);
6998 c->tb->where = gfc_current_locus;
6999 *c->tb = *tb;
7000 }
90661f26 7001
713485cc
JW
7002 /* Set interface. */
7003 if (proc_if != NULL)
7004 {
7005 c->ts.interface = proc_if;
7006 c->attr.untyped = 1;
7007 c->attr.if_source = IFSRC_IFBODY;
7008 }
7009 else if (ts.type != BT_UNKNOWN)
7010 {
7011 c->ts = ts;
7012 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
d7fee03d 7013 c->ts.interface->result = c->ts.interface;
713485cc 7014 c->ts.interface->ts = ts;
d91909c0 7015 c->ts.interface->attr.flavor = FL_PROCEDURE;
713485cc 7016 c->ts.interface->attr.function = 1;
d91909c0 7017 c->attr.function = 1;
713485cc
JW
7018 c->attr.if_source = IFSRC_UNKNOWN;
7019 }
7020
7021 if (gfc_match (" =>") == MATCH_YES)
7022 {
80f95228 7023 m = match_pointer_init (&initializer, 1);
713485cc
JW
7024 if (m != MATCH_YES)
7025 {
7026 gfc_free_expr (initializer);
7027 return m;
7028 }
7029 c->initializer = initializer;
7030 }
7031
7032 if (gfc_match_eos () == MATCH_YES)
7033 return MATCH_YES;
7034 if (gfc_match_char (',') != MATCH_YES)
7035 goto syntax;
7036 }
7037
7038syntax:
7039 gfc_error ("Syntax error in procedure pointer component at %C");
7040 return MATCH_ERROR;
7041}
7042
7043
69773742
JW
7044/* Match a PROCEDURE declaration inside an interface (R1206). */
7045
7046static match
7047match_procedure_in_interface (void)
7048{
7049 match m;
7050 gfc_symbol *sym;
7051 char name[GFC_MAX_SYMBOL_LEN + 1];
a6fcd41a 7052 locus old_locus;
69773742
JW
7053
7054 if (current_interface.type == INTERFACE_NAMELESS
7055 || current_interface.type == INTERFACE_ABSTRACT)
7056 {
7057 gfc_error ("PROCEDURE at %C must be in a generic interface");
7058 return MATCH_ERROR;
7059 }
7060
a6fcd41a
TB
7061 /* Check if the F2008 optional double colon appears. */
7062 gfc_gobble_whitespace ();
7063 old_locus = gfc_current_locus;
7064 if (gfc_match ("::") == MATCH_YES)
7065 {
524af0d6
JB
7066 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7067 "MODULE PROCEDURE statement at %L", &old_locus))
a6fcd41a
TB
7068 return MATCH_ERROR;
7069 }
7070 else
7071 gfc_current_locus = old_locus;
7072
69773742
JW
7073 for(;;)
7074 {
7075 m = gfc_match_name (name);
7076 if (m == MATCH_NO)
7077 goto syntax;
7078 else if (m == MATCH_ERROR)
7079 return m;
7080 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7081 return MATCH_ERROR;
7082
524af0d6 7083 if (!gfc_add_interface (sym))
69773742
JW
7084 return MATCH_ERROR;
7085
69773742
JW
7086 if (gfc_match_eos () == MATCH_YES)
7087 break;
7088 if (gfc_match_char (',') != MATCH_YES)
7089 goto syntax;
7090 }
7091
7092 return MATCH_YES;
7093
7094syntax:
7095 gfc_error ("Syntax error in PROCEDURE statement at %C");
7096 return MATCH_ERROR;
7097}
7098
7099
7100/* General matcher for PROCEDURE declarations. */
7101
30b608eb
DK
7102static match match_procedure_in_type (void);
7103
69773742
JW
7104match
7105gfc_match_procedure (void)
7106{
7107 match m;
7108
7109 switch (gfc_current_state ())
7110 {
7111 case COMP_NONE:
7112 case COMP_PROGRAM:
7113 case COMP_MODULE:
4668d6f9 7114 case COMP_SUBMODULE:
69773742
JW
7115 case COMP_SUBROUTINE:
7116 case COMP_FUNCTION:
3547d57e 7117 case COMP_BLOCK:
69773742
JW
7118 m = match_procedure_decl ();
7119 break;
7120 case COMP_INTERFACE:
7121 m = match_procedure_in_interface ();
7122 break;
7123 case COMP_DERIVED:
713485cc
JW
7124 m = match_ppc_decl ();
7125 break;
30b608eb
DK
7126 case COMP_DERIVED_CONTAINS:
7127 m = match_procedure_in_type ();
7128 break;
69773742
JW
7129 default:
7130 return MATCH_NO;
7131 }
7132
7133 if (m != MATCH_YES)
7134 return m;
7135
524af0d6 7136 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
69773742
JW
7137 return MATCH_ERROR;
7138
7139 return m;
7140}
7141
7142
c3005b0f
DK
7143/* Warn if a matched procedure has the same name as an intrinsic; this is
7144 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7145 parser-state-stack to find out whether we're in a module. */
7146
7147static void
73e42eef 7148do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
c3005b0f
DK
7149{
7150 bool in_module;
7151
7152 in_module = (gfc_state_stack->previous
4668d6f9
PT
7153 && (gfc_state_stack->previous->state == COMP_MODULE
7154 || gfc_state_stack->previous->state == COMP_SUBMODULE));
c3005b0f
DK
7155
7156 gfc_warn_intrinsic_shadow (sym, in_module, func);
7157}
7158
7159
6de9cd9a
DN
7160/* Match a function declaration. */
7161
7162match
7163gfc_match_function_decl (void)
7164{
7165 char name[GFC_MAX_SYMBOL_LEN + 1];
7166 gfc_symbol *sym, *result;
7167 locus old_loc;
7168 match m;
a8b3b0b6 7169 match suffix_match;
f5acf0f2 7170 match found_match; /* Status returned by match func. */
6de9cd9a
DN
7171
7172 if (gfc_current_state () != COMP_NONE
7173 && gfc_current_state () != COMP_INTERFACE
7174 && gfc_current_state () != COMP_CONTAINS)
7175 return MATCH_NO;
7176
7177 gfc_clear_ts (&current_ts);
7178
63645982 7179 old_loc = gfc_current_locus;
6de9cd9a 7180
1c8bcdf7 7181 m = gfc_match_prefix (&current_ts);
6de9cd9a
DN
7182 if (m != MATCH_YES)
7183 {
63645982 7184 gfc_current_locus = old_loc;
6de9cd9a
DN
7185 return m;
7186 }
7187
7188 if (gfc_match ("function% %n", name) != MATCH_YES)
7189 {
63645982 7190 gfc_current_locus = old_loc;
6de9cd9a
DN
7191 return MATCH_NO;
7192 }
4668d6f9 7193
1a492601 7194 if (get_proc_name (name, &sym, false))
6de9cd9a 7195 return MATCH_ERROR;
3070bab4 7196
524af0d6 7197 if (add_hidden_procptr_result (sym))
3070bab4
JW
7198 sym = sym->result;
7199
4668d6f9
PT
7200 if (current_attr.module_procedure)
7201 sym->attr.module_procedure = 1;
7202
6de9cd9a
DN
7203 gfc_new_block = sym;
7204
7205 m = gfc_match_formal_arglist (sym, 0, 0);
7206 if (m == MATCH_NO)
2b9a33ae
TS
7207 {
7208 gfc_error ("Expected formal argument list in function "
636dff67 7209 "definition at %C");
2b9a33ae
TS
7210 m = MATCH_ERROR;
7211 goto cleanup;
7212 }
6de9cd9a
DN
7213 else if (m == MATCH_ERROR)
7214 goto cleanup;
7215
7216 result = NULL;
7217
a8b3b0b6
CR
7218 /* According to the draft, the bind(c) and result clause can
7219 come in either order after the formal_arg_list (i.e., either
7220 can be first, both can exist together or by themselves or neither
7221 one). Therefore, the match_result can't match the end of the
7222 string, and check for the bind(c) or result clause in either order. */
7223 found_match = gfc_match_eos ();
7224
7225 /* Make sure that it isn't already declared as BIND(C). If it is, it
7226 must have been marked BIND(C) with a BIND(C) attribute and that is
7227 not allowed for procedures. */
7228 if (sym->attr.is_bind_c == 1)
7229 {
7230 sym->attr.is_bind_c = 0;
7231 if (sym->old_symbol != NULL)
7232 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7233 "variables or common blocks",
7234 &(sym->old_symbol->declared_at));
7235 else
7236 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7237 "variables or common blocks", &gfc_current_locus);
6de9cd9a
DN
7238 }
7239
a8b3b0b6 7240 if (found_match != MATCH_YES)
6de9cd9a 7241 {
a8b3b0b6
CR
7242 /* If we haven't found the end-of-statement, look for a suffix. */
7243 suffix_match = gfc_match_suffix (sym, &result);
7244 if (suffix_match == MATCH_YES)
7245 /* Need to get the eos now. */
7246 found_match = gfc_match_eos ();
7247 else
7248 found_match = suffix_match;
6de9cd9a
DN
7249 }
7250
a8b3b0b6
CR
7251 if(found_match != MATCH_YES)
7252 m = MATCH_ERROR;
6de9cd9a
DN
7253 else
7254 {
a8b3b0b6
CR
7255 /* Make changes to the symbol. */
7256 m = MATCH_ERROR;
f5acf0f2 7257
524af0d6 7258 if (!gfc_add_function (&sym->attr, sym->name, NULL))
a8b3b0b6 7259 goto cleanup;
f5acf0f2 7260
70112e2a 7261 if (!gfc_missing_attr (&sym->attr, NULL))
a8b3b0b6 7262 goto cleanup;
6de9cd9a 7263
70112e2a
PT
7264 if (!copy_prefix (&sym->attr, &sym->declared_at))
7265 {
7266 if(!sym->attr.module_procedure)
7267 goto cleanup;
7268 else
7269 gfc_error_check ();
7270 }
7271
a99d95a2 7272 /* Delay matching the function characteristics until after the
1c8bcdf7 7273 specification block by signalling kind=-1. */
a99d95a2
PT
7274 sym->declared_at = old_loc;
7275 if (current_ts.type != BT_UNKNOWN)
7276 current_ts.kind = -1;
7277 else
7278 current_ts.kind = 0;
1c8bcdf7 7279
a8b3b0b6
CR
7280 if (result == NULL)
7281 {
6de7294f 7282 if (current_ts.type != BT_UNKNOWN
524af0d6 7283 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
6de7294f 7284 goto cleanup;
a8b3b0b6
CR
7285 sym->result = sym;
7286 }
7287 else
7288 {
6de7294f 7289 if (current_ts.type != BT_UNKNOWN
524af0d6 7290 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
6de7294f 7291 goto cleanup;
a8b3b0b6
CR
7292 sym->result = result;
7293 }
7294
c3005b0f 7295 /* Warn if this procedure has the same name as an intrinsic. */
73e42eef 7296 do_warn_intrinsic_shadow (sym, true);
c3005b0f 7297
a8b3b0b6
CR
7298 return MATCH_YES;
7299 }
6de9cd9a
DN
7300
7301cleanup:
63645982 7302 gfc_current_locus = old_loc;
6de9cd9a
DN
7303 return m;
7304}
7305
636dff67
SK
7306
7307/* This is mostly a copy of parse.c(add_global_procedure) but modified to
7308 pass the name of the entry, rather than the gfc_current_block name, and
7309 to return false upon finding an existing global entry. */
68ea355b
PT
7310
7311static bool
3a43b5b3
TB
7312add_global_entry (const char *name, const char *binding_label, bool sub,
7313 locus *where)
68ea355b
PT
7314{
7315 gfc_gsymbol *s;
32e8bb8e 7316 enum gfc_symbol_type type;
68ea355b 7317
7389bce6 7318 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
68ea355b 7319
f11de7c5
TB
7320 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7321 name is a global identifier. */
7322 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
68ea355b 7323 {
55b9c612 7324 s = gfc_get_gsymbol (name, false);
f11de7c5
TB
7325
7326 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7327 {
3a43b5b3 7328 gfc_global_used (s, where);
f11de7c5
TB
7329 return false;
7330 }
7331 else
7332 {
7333 s->type = type;
77f8682b 7334 s->sym_name = name;
3a43b5b3 7335 s->where = *where;
f11de7c5
TB
7336 s->defined = 1;
7337 s->ns = gfc_current_ns;
7338 }
68ea355b 7339 }
f11de7c5
TB
7340
7341 /* Don't add the symbol multiple times. */
7342 if (binding_label
7343 && (!gfc_notification_std (GFC_STD_F2008)
7344 || strcmp (name, binding_label) != 0))
7345 {
55b9c612 7346 s = gfc_get_gsymbol (binding_label, true);
f11de7c5
TB
7347
7348 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7349 {
3a43b5b3 7350 gfc_global_used (s, where);
f11de7c5
TB
7351 return false;
7352 }
7353 else
7354 {
7355 s->type = type;
77f8682b 7356 s->sym_name = name;
f11de7c5 7357 s->binding_label = binding_label;
3a43b5b3 7358 s->where = *where;
f11de7c5
TB
7359 s->defined = 1;
7360 s->ns = gfc_current_ns;
7361 }
7362 }
7363
7364 return true;
68ea355b 7365}
6de9cd9a 7366
636dff67 7367
6de9cd9a
DN
7368/* Match an ENTRY statement. */
7369
7370match
7371gfc_match_entry (void)
7372{
3d79abbd
PB
7373 gfc_symbol *proc;
7374 gfc_symbol *result;
7375 gfc_symbol *entry;
6de9cd9a
DN
7376 char name[GFC_MAX_SYMBOL_LEN + 1];
7377 gfc_compile_state state;
7378 match m;
3d79abbd 7379 gfc_entry_list *el;
c96cfa49 7380 locus old_loc;
1a492601 7381 bool module_procedure;
bc3e7a8c
TB
7382 char peek_char;
7383 match is_bind_c;
6de9cd9a
DN
7384
7385 m = gfc_match_name (name);
7386 if (m != MATCH_YES)
7387 return m;
7388
524af0d6 7389 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
58fc89f6
TB
7390 return MATCH_ERROR;
7391
3d79abbd 7392 state = gfc_current_state ();
4c93c95a 7393 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 7394 {
4c93c95a
FXC
7395 switch (state)
7396 {
7397 case COMP_PROGRAM:
7398 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7399 break;
7400 case COMP_MODULE:
7401 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7402 break;
4668d6f9
PT
7403 case COMP_SUBMODULE:
7404 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7405 break;
4c93c95a 7406 case COMP_BLOCK_DATA:
636dff67
SK
7407 gfc_error ("ENTRY statement at %C cannot appear within "
7408 "a BLOCK DATA");
4c93c95a
FXC
7409 break;
7410 case COMP_INTERFACE:
636dff67
SK
7411 gfc_error ("ENTRY statement at %C cannot appear within "
7412 "an INTERFACE");
4c93c95a 7413 break;
f6288c24
FR
7414 case COMP_STRUCTURE:
7415 gfc_error ("ENTRY statement at %C cannot appear within "
7416 "a STRUCTURE block");
7417 break;
4c93c95a 7418 case COMP_DERIVED:
636dff67
SK
7419 gfc_error ("ENTRY statement at %C cannot appear within "
7420 "a DERIVED TYPE block");
4c93c95a
FXC
7421 break;
7422 case COMP_IF:
636dff67
SK
7423 gfc_error ("ENTRY statement at %C cannot appear within "
7424 "an IF-THEN block");
4c93c95a
FXC
7425 break;
7426 case COMP_DO:
8c6a85e3 7427 case COMP_DO_CONCURRENT:
636dff67
SK
7428 gfc_error ("ENTRY statement at %C cannot appear within "
7429 "a DO block");
4c93c95a
FXC
7430 break;
7431 case COMP_SELECT:
636dff67
SK
7432 gfc_error ("ENTRY statement at %C cannot appear within "
7433 "a SELECT block");
4c93c95a
FXC
7434 break;
7435 case COMP_FORALL:
636dff67
SK
7436 gfc_error ("ENTRY statement at %C cannot appear within "
7437 "a FORALL block");
4c93c95a
FXC
7438 break;
7439 case COMP_WHERE:
636dff67
SK
7440 gfc_error ("ENTRY statement at %C cannot appear within "
7441 "a WHERE block");
4c93c95a
FXC
7442 break;
7443 case COMP_CONTAINS:
636dff67
SK
7444 gfc_error ("ENTRY statement at %C cannot appear within "
7445 "a contained subprogram");
4c93c95a
FXC
7446 break;
7447 default:
fce523bf 7448 gfc_error ("Unexpected ENTRY statement at %C");
4c93c95a 7449 }
3d79abbd
PB
7450 return MATCH_ERROR;
7451 }
7452
5f0ba745
SK
7453 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7454 && gfc_state_stack->previous->state == COMP_INTERFACE)
7455 {
7456 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7457 return MATCH_ERROR;
7458 }
7459
1a492601 7460 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
7461 && gfc_current_ns->parent->proc_name
7462 && gfc_current_ns->parent->proc_name->attr.flavor
7463 == FL_MODULE;
1a492601 7464
3d79abbd
PB
7465 if (gfc_current_ns->parent != NULL
7466 && gfc_current_ns->parent->proc_name
1a492601 7467 && !module_procedure)
3d79abbd
PB
7468 {
7469 gfc_error("ENTRY statement at %C cannot appear in a "
7470 "contained procedure");
7471 return MATCH_ERROR;
7472 }
7473
1a492601
PT
7474 /* Module function entries need special care in get_proc_name
7475 because previous references within the function will have
7476 created symbols attached to the current namespace. */
7477 if (get_proc_name (name, &entry,
7478 gfc_current_ns->parent != NULL
ecd3b73c 7479 && module_procedure))
6de9cd9a
DN
7480 return MATCH_ERROR;
7481
3d79abbd
PB
7482 proc = gfc_current_block ();
7483
bc3e7a8c
TB
7484 /* Make sure that it isn't already declared as BIND(C). If it is, it
7485 must have been marked BIND(C) with a BIND(C) attribute and that is
7486 not allowed for procedures. */
7487 if (entry->attr.is_bind_c == 1)
7488 {
7489 entry->attr.is_bind_c = 0;
7490 if (entry->old_symbol != NULL)
7491 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7492 "variables or common blocks",
7493 &(entry->old_symbol->declared_at));
7494 else
7495 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7496 "variables or common blocks", &gfc_current_locus);
7497 }
f5acf0f2 7498
bc3e7a8c
TB
7499 /* Check what next non-whitespace character is so we can tell if there
7500 is the required parens if we have a BIND(C). */
3a43b5b3 7501 old_loc = gfc_current_locus;
bc3e7a8c 7502 gfc_gobble_whitespace ();
8fc541d3 7503 peek_char = gfc_peek_ascii_char ();
bc3e7a8c 7504
3d79abbd 7505 if (state == COMP_SUBROUTINE)
6de9cd9a 7506 {
6de9cd9a
DN
7507 m = gfc_match_formal_arglist (entry, 0, 1);
7508 if (m != MATCH_YES)
7509 return MATCH_ERROR;
7510
1eabf70a
TB
7511 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7512 never be an internal procedure. */
7513 is_bind_c = gfc_match_bind_c (entry, true);
bc3e7a8c
TB
7514 if (is_bind_c == MATCH_ERROR)
7515 return MATCH_ERROR;
7516 if (is_bind_c == MATCH_YES)
7517 {
7518 if (peek_char != '(')
7519 {
7520 gfc_error ("Missing required parentheses before BIND(C) at %C");
7521 return MATCH_ERROR;
7522 }
89508a3f
SK
7523
7524 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7525 &(entry->declared_at), 1))
7526 return MATCH_ERROR;
0a524296 7527
bc3e7a8c
TB
7528 }
7529
f11de7c5 7530 if (!gfc_current_ns->parent
3a43b5b3
TB
7531 && !add_global_entry (name, entry->binding_label, true,
7532 &old_loc))
f11de7c5
TB
7533 return MATCH_ERROR;
7534
7535 /* An entry in a subroutine. */
524af0d6
JB
7536 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7537 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
6de9cd9a 7538 return MATCH_ERROR;
3d79abbd
PB
7539 }
7540 else
7541 {
c96cfa49 7542 /* An entry in a function.
636dff67
SK
7543 We need to take special care because writing
7544 ENTRY f()
7545 as
7546 ENTRY f
7547 is allowed, whereas
7548 ENTRY f() RESULT (r)
7549 can't be written as
7550 ENTRY f RESULT (r). */
c96cfa49
TS
7551 if (gfc_match_eos () == MATCH_YES)
7552 {
7553 gfc_current_locus = old_loc;
7554 /* Match the empty argument list, and add the interface to
7555 the symbol. */
7556 m = gfc_match_formal_arglist (entry, 0, 1);
7557 }
7558 else
7559 m = gfc_match_formal_arglist (entry, 0, 0);
7560
6de9cd9a
DN
7561 if (m != MATCH_YES)
7562 return MATCH_ERROR;
7563
6de9cd9a
DN
7564 result = NULL;
7565
7566 if (gfc_match_eos () == MATCH_YES)
7567 {
524af0d6
JB
7568 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7569 || !gfc_add_function (&entry->attr, entry->name, NULL))
6de9cd9a
DN
7570 return MATCH_ERROR;
7571
d198b59a 7572 entry->result = entry;
6de9cd9a
DN
7573 }
7574 else
7575 {
bc3e7a8c 7576 m = gfc_match_suffix (entry, &result);
6de9cd9a
DN
7577 if (m == MATCH_NO)
7578 gfc_syntax_error (ST_ENTRY);
7579 if (m != MATCH_YES)
7580 return MATCH_ERROR;
7581
bc3e7a8c
TB
7582 if (result)
7583 {
524af0d6
JB
7584 if (!gfc_add_result (&result->attr, result->name, NULL)
7585 || !gfc_add_entry (&entry->attr, result->name, NULL)
7586 || !gfc_add_function (&entry->attr, result->name, NULL))
bc3e7a8c
TB
7587 return MATCH_ERROR;
7588 entry->result = result;
7589 }
7590 else
7591 {
524af0d6
JB
7592 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7593 || !gfc_add_function (&entry->attr, entry->name, NULL))
bc3e7a8c
TB
7594 return MATCH_ERROR;
7595 entry->result = entry;
7596 }
6de9cd9a 7597 }
f11de7c5
TB
7598
7599 if (!gfc_current_ns->parent
3a43b5b3
TB
7600 && !add_global_entry (name, entry->binding_label, false,
7601 &old_loc))
f11de7c5 7602 return MATCH_ERROR;
6de9cd9a
DN
7603 }
7604
7605 if (gfc_match_eos () != MATCH_YES)
7606 {
7607 gfc_syntax_error (ST_ENTRY);
7608 return MATCH_ERROR;
7609 }
7610
89508a3f
SK
7611 /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
7612 if (proc->attr.elemental && entry->attr.is_bind_c)
7613 {
7614 gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
7615 "elemental procedure", &entry->declared_at);
7616 return MATCH_ERROR;
7617 }
7618
3d79abbd
PB
7619 entry->attr.recursive = proc->attr.recursive;
7620 entry->attr.elemental = proc->attr.elemental;
7621 entry->attr.pure = proc->attr.pure;
6de9cd9a 7622
3d79abbd
PB
7623 el = gfc_get_entry_list ();
7624 el->sym = entry;
7625 el->next = gfc_current_ns->entries;
7626 gfc_current_ns->entries = el;
7627 if (el->next)
7628 el->id = el->next->id + 1;
7629 else
7630 el->id = 1;
6de9cd9a 7631
3d79abbd
PB
7632 new_st.op = EXEC_ENTRY;
7633 new_st.ext.entry = el;
7634
7635 return MATCH_YES;
6de9cd9a
DN
7636}
7637
7638
7639/* Match a subroutine statement, including optional prefixes. */
7640
7641match
7642gfc_match_subroutine (void)
7643{
7644 char name[GFC_MAX_SYMBOL_LEN + 1];
7645 gfc_symbol *sym;
7646 match m;
a8b3b0b6
CR
7647 match is_bind_c;
7648 char peek_char;
1eabf70a 7649 bool allow_binding_name;
f28c46cd 7650 locus loc;
6de9cd9a
DN
7651
7652 if (gfc_current_state () != COMP_NONE
7653 && gfc_current_state () != COMP_INTERFACE
7654 && gfc_current_state () != COMP_CONTAINS)
7655 return MATCH_NO;
7656
1c8bcdf7 7657 m = gfc_match_prefix (NULL);
6de9cd9a
DN
7658 if (m != MATCH_YES)
7659 return m;
7660
7661 m = gfc_match ("subroutine% %n", name);
7662 if (m != MATCH_YES)
7663 return m;
7664
1a492601 7665 if (get_proc_name (name, &sym, false))
6de9cd9a 7666 return MATCH_ERROR;
3070bab4 7667
7fcd5ad5 7668 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
1cc0e193 7669 the symbol existed before. */
7fcd5ad5
TB
7670 sym->declared_at = gfc_current_locus;
7671
4668d6f9
PT
7672 if (current_attr.module_procedure)
7673 sym->attr.module_procedure = 1;
7674
524af0d6 7675 if (add_hidden_procptr_result (sym))
3070bab4
JW
7676 sym = sym->result;
7677
6de9cd9a
DN
7678 gfc_new_block = sym;
7679
a8b3b0b6 7680 /* Check what next non-whitespace character is so we can tell if there
bc3e7a8c 7681 is the required parens if we have a BIND(C). */
a8b3b0b6 7682 gfc_gobble_whitespace ();
8fc541d3 7683 peek_char = gfc_peek_ascii_char ();
f5acf0f2 7684
524af0d6 7685 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6de9cd9a
DN
7686 return MATCH_ERROR;
7687
7688 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7689 return MATCH_ERROR;
7690
a8b3b0b6
CR
7691 /* Make sure that it isn't already declared as BIND(C). If it is, it
7692 must have been marked BIND(C) with a BIND(C) attribute and that is
7693 not allowed for procedures. */
7694 if (sym->attr.is_bind_c == 1)
7695 {
7696 sym->attr.is_bind_c = 0;
7697 if (sym->old_symbol != NULL)
7698 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7699 "variables or common blocks",
7700 &(sym->old_symbol->declared_at));
7701 else
7702 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7703 "variables or common blocks", &gfc_current_locus);
7704 }
1eabf70a
TB
7705
7706 /* C binding names are not allowed for internal procedures. */
7707 if (gfc_current_state () == COMP_CONTAINS
7708 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7709 allow_binding_name = false;
7710 else
7711 allow_binding_name = true;
7712
a8b3b0b6
CR
7713 /* Here, we are just checking if it has the bind(c) attribute, and if
7714 so, then we need to make sure it's all correct. If it doesn't,
7715 we still need to continue matching the rest of the subroutine line. */
f28c46cd
SK
7716 gfc_gobble_whitespace ();
7717 loc = gfc_current_locus;
1eabf70a 7718 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
7719 if (is_bind_c == MATCH_ERROR)
7720 {
7721 /* There was an attempt at the bind(c), but it was wrong. An
7722 error message should have been printed w/in the gfc_match_bind_c
7723 so here we'll just return the MATCH_ERROR. */
7724 return MATCH_ERROR;
7725 }
7726
7727 if (is_bind_c == MATCH_YES)
7728 {
f28c46cd
SK
7729 gfc_formal_arglist *arg;
7730
1eabf70a 7731 /* The following is allowed in the Fortran 2008 draft. */
01f4fff1 7732 if (gfc_current_state () == COMP_CONTAINS
1eabf70a 7733 && sym->ns->proc_name->attr.flavor != FL_MODULE
524af0d6
JB
7734 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7735 "at %L may not be specified for an internal "
7736 "procedure", &gfc_current_locus))
1eabf70a
TB
7737 return MATCH_ERROR;
7738
a8b3b0b6
CR
7739 if (peek_char != '(')
7740 {
7741 gfc_error ("Missing required parentheses before BIND(C) at %C");
7742 return MATCH_ERROR;
7743 }
f28c46cd
SK
7744
7745 /* Scan the dummy arguments for an alternate return. */
7746 for (arg = sym->formal; arg; arg = arg->next)
7747 if (!arg->sym)
7748 {
7749 gfc_error ("Alternate return dummy argument cannot appear in a "
7750 "SUBROUTINE with the BIND(C) attribute at %L", &loc);
7751 return MATCH_ERROR;
7752 }
7753
7754 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
a8b3b0b6
CR
7755 return MATCH_ERROR;
7756 }
f5acf0f2 7757
6de9cd9a
DN
7758 if (gfc_match_eos () != MATCH_YES)
7759 {
7760 gfc_syntax_error (ST_SUBROUTINE);
7761 return MATCH_ERROR;
7762 }
7763
524af0d6 7764 if (!copy_prefix (&sym->attr, &sym->declared_at))
70112e2a
PT
7765 {
7766 if(!sym->attr.module_procedure)
7767 return MATCH_ERROR;
7768 else
7769 gfc_error_check ();
7770 }
6de9cd9a 7771
c3005b0f 7772 /* Warn if it has the same name as an intrinsic. */
73e42eef 7773 do_warn_intrinsic_shadow (sym, false);
c3005b0f 7774
6de9cd9a
DN
7775 return MATCH_YES;
7776}
7777
7778
3b37ccd4
FXC
7779/* Check that the NAME identifier in a BIND attribute or statement
7780 is conform to C identifier rules. */
7781
7782match
7783check_bind_name_identifier (char **name)
7784{
7785 char *n = *name, *p;
7786
7787 /* Remove leading spaces. */
7788 while (*n == ' ')
7789 n++;
7790
7791 /* On an empty string, free memory and set name to NULL. */
7792 if (*n == '\0')
7793 {
7794 free (*name);
7795 *name = NULL;
7796 return MATCH_YES;
7797 }
7798
7799 /* Remove trailing spaces. */
7800 p = n + strlen(n) - 1;
7801 while (*p == ' ')
7802 *(p--) = '\0';
7803
7804 /* Insert the identifier into the symbol table. */
7805 p = xstrdup (n);
7806 free (*name);
7807 *name = p;
7808
7809 /* Now check that identifier is valid under C rules. */
7810 if (ISDIGIT (*p))
7811 {
7812 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7813 return MATCH_ERROR;
7814 }
7815
7816 for (; *p; p++)
7817 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
7818 {
7819 gfc_error ("Invalid C identifier in NAME= specifier at %C");
7820 return MATCH_ERROR;
7821 }
7822
7823 return MATCH_YES;
7824}
7825
7826
a8b3b0b6
CR
7827/* Match a BIND(C) specifier, with the optional 'name=' specifier if
7828 given, and set the binding label in either the given symbol (if not
86bf520d 7829 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
7830 encounter the BIND(C) before the declaration itself. Return
7831 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
7832 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
7833 or MATCH_YES if the specifier was correct and the binding label and
7834 bind(c) fields were set correctly for the given symbol or the
1eabf70a
TB
7835 current_ts. If allow_binding_name is false, no binding name may be
7836 given. */
a8b3b0b6
CR
7837
7838match
1eabf70a 7839gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
a8b3b0b6 7840{
3b37ccd4
FXC
7841 char *binding_label = NULL;
7842 gfc_expr *e = NULL;
a8b3b0b6 7843
f5acf0f2 7844 /* Initialize the flag that specifies whether we encountered a NAME=
a8b3b0b6
CR
7845 specifier or not. */
7846 has_name_equals = 0;
7847
a8b3b0b6
CR
7848 /* This much we have to be able to match, in this order, if
7849 there is a bind(c) label. */
7850 if (gfc_match (" bind ( c ") != MATCH_YES)
7851 return MATCH_NO;
7852
7853 /* Now see if there is a binding label, or if we've reached the
7854 end of the bind(c) attribute without one. */
7855 if (gfc_match_char (',') == MATCH_YES)
7856 {
7857 if (gfc_match (" name = ") != MATCH_YES)
7858 {
7859 gfc_error ("Syntax error in NAME= specifier for binding label "
7860 "at %C");
7861 /* should give an error message here */
7862 return MATCH_ERROR;
7863 }
7864
7865 has_name_equals = 1;
7866
3b37ccd4
FXC
7867 if (gfc_match_init_expr (&e) != MATCH_YES)
7868 {
7869 gfc_free_expr (e);
7870 return MATCH_ERROR;
7871 }
f5acf0f2 7872
3b37ccd4 7873 if (!gfc_simplify_expr(e, 0))
a8b3b0b6 7874 {
3b37ccd4
FXC
7875 gfc_error ("NAME= specifier at %C should be a constant expression");
7876 gfc_free_expr (e);
7877 return MATCH_ERROR;
a8b3b0b6 7878 }
3b37ccd4
FXC
7879
7880 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
7881 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
a8b3b0b6 7882 {
3b37ccd4
FXC
7883 gfc_error ("NAME= specifier at %C should be a scalar of "
7884 "default character kind");
7885 gfc_free_expr(e);
7886 return MATCH_ERROR;
a8b3b0b6 7887 }
3b37ccd4
FXC
7888
7889 // Get a C string from the Fortran string constant
7890 binding_label = gfc_widechar_to_char (e->value.character.string,
7891 e->value.character.length);
7892 gfc_free_expr(e);
7893
7894 // Check that it is valid (old gfc_match_name_C)
7895 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
7896 return MATCH_ERROR;
7897 }
a8b3b0b6
CR
7898
7899 /* Get the required right paren. */
7900 if (gfc_match_char (')') != MATCH_YES)
7901 {
7902 gfc_error ("Missing closing paren for binding label at %C");
7903 return MATCH_ERROR;
7904 }
7905
1eabf70a
TB
7906 if (has_name_equals && !allow_binding_name)
7907 {
7908 gfc_error ("No binding name is allowed in BIND(C) at %C");
7909 return MATCH_ERROR;
7910 }
7911
7912 if (has_name_equals && sym != NULL && sym->attr.dummy)
7913 {
7914 gfc_error ("For dummy procedure %s, no binding name is "
7915 "allowed in BIND(C) at %C", sym->name);
7916 return MATCH_ERROR;
7917 }
7918
7919
a8b3b0b6
CR
7920 /* Save the binding label to the symbol. If sym is null, we're
7921 probably matching the typespec attributes of a declaration and
7922 haven't gotten the name yet, and therefore, no symbol yet. */
62603fae 7923 if (binding_label)
a8b3b0b6
CR
7924 {
7925 if (sym != NULL)
62603fae 7926 sym->binding_label = binding_label;
a8b3b0b6 7927 else
62603fae 7928 curr_binding_label = binding_label;
a8b3b0b6 7929 }
1eabf70a 7930 else if (allow_binding_name)
a8b3b0b6
CR
7931 {
7932 /* No binding label, but if symbol isn't null, we
1eabf70a
TB
7933 can set the label for it here.
7934 If name="" or allow_binding_name is false, no C binding name is
1cc0e193 7935 created. */
a8b3b0b6 7936 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
62603fae 7937 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
a8b3b0b6 7938 }
9e1d712c 7939
129d15a3
JW
7940 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
7941 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
7942 {
7943 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
7944 return MATCH_ERROR;
7945 }
7946
a8b3b0b6
CR
7947 return MATCH_YES;
7948}
7949
7950
1f2959f0 7951/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
7952
7953static int
7954contained_procedure (void)
7955{
083de129 7956 gfc_state_data *s = gfc_state_stack;
ddc9ce91 7957
083de129
TB
7958 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
7959 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
7960 return 1;
ddc9ce91
TS
7961
7962 return 0;
7963}
7964
d51347f9 7965/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
7966 interoperable with the corresponding C enumeration type, making
7967 sure that -fshort-enums is honored. */
7968
7969static void
7970set_enum_kind(void)
7971{
7972 enumerator_history *current_history = NULL;
7973 int kind;
7974 int i;
7975
7976 if (max_enum == NULL || enum_history == NULL)
7977 return;
7978
cab129d1 7979 if (!flag_short_enums)
d51347f9
TB
7980 return;
7981
25d8f0a2
TS
7982 i = 0;
7983 do
7984 {
7985 kind = gfc_integer_kinds[i++].kind;
7986 }
d51347f9 7987 while (kind < gfc_c_int_kind
25d8f0a2
TS
7988 && gfc_check_integer_range (max_enum->initializer->value.integer,
7989 kind) != ARITH_OK);
7990
7991 current_history = enum_history;
7992 while (current_history != NULL)
7993 {
7994 current_history->sym->ts.kind = kind;
7995 current_history = current_history->next;
7996 }
7997}
7998
636dff67 7999
6de9cd9a 8000/* Match any of the various end-block statements. Returns the type of
9abe5e56
DK
8001 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
8002 and END BLOCK statements cannot be replaced by a single END statement. */
6de9cd9a
DN
8003
8004match
636dff67 8005gfc_match_end (gfc_statement *st)
6de9cd9a
DN
8006{
8007 char name[GFC_MAX_SYMBOL_LEN + 1];
8008 gfc_compile_state state;
8009 locus old_loc;
8010 const char *block_name;
8011 const char *target;
ddc9ce91 8012 int eos_ok;
6de9cd9a 8013 match m;
0cab6b73
TK
8014 gfc_namespace *parent_ns, *ns, *prev_ns;
8015 gfc_namespace **nsp;
63af1586 8016 bool abreviated_modproc_decl = false;
874108a9 8017 bool got_matching_end = false;
6de9cd9a 8018
63645982 8019 old_loc = gfc_current_locus;
6de9cd9a
DN
8020 if (gfc_match ("end") != MATCH_YES)
8021 return MATCH_NO;
8022
8023 state = gfc_current_state ();
636dff67
SK
8024 block_name = gfc_current_block () == NULL
8025 ? NULL : gfc_current_block ()->name;
6de9cd9a 8026
03af1e4c 8027 switch (state)
6de9cd9a 8028 {
03af1e4c
DK
8029 case COMP_ASSOCIATE:
8030 case COMP_BLOCK:
2eb3745a 8031 if (gfc_str_startswith (block_name, "block@"))
03af1e4c
DK
8032 block_name = NULL;
8033 break;
8034
8035 case COMP_CONTAINS:
8036 case COMP_DERIVED_CONTAINS:
6de9cd9a 8037 state = gfc_state_stack->previous->state;
636dff67
SK
8038 block_name = gfc_state_stack->previous->sym == NULL
8039 ? NULL : gfc_state_stack->previous->sym->name;
63af1586
PT
8040 abreviated_modproc_decl = gfc_state_stack->previous->sym
8041 && gfc_state_stack->previous->sym->abr_modproc_decl;
03af1e4c
DK
8042 break;
8043
8044 default:
8045 break;
6de9cd9a
DN
8046 }
8047
63af1586
PT
8048 if (!abreviated_modproc_decl)
8049 abreviated_modproc_decl = gfc_current_block ()
8050 && gfc_current_block ()->abr_modproc_decl;
4668d6f9 8051
6de9cd9a
DN
8052 switch (state)
8053 {
8054 case COMP_NONE:
8055 case COMP_PROGRAM:
8056 *st = ST_END_PROGRAM;
8057 target = " program";
ddc9ce91 8058 eos_ok = 1;
6de9cd9a
DN
8059 break;
8060
8061 case COMP_SUBROUTINE:
8062 *st = ST_END_SUBROUTINE;
4668d6f9 8063 if (!abreviated_modproc_decl)
6de9cd9a 8064 target = " subroutine";
4668d6f9
PT
8065 else
8066 target = " procedure";
ddc9ce91 8067 eos_ok = !contained_procedure ();
6de9cd9a
DN
8068 break;
8069
8070 case COMP_FUNCTION:
8071 *st = ST_END_FUNCTION;
4668d6f9 8072 if (!abreviated_modproc_decl)
6de9cd9a 8073 target = " function";
4668d6f9
PT
8074 else
8075 target = " procedure";
ddc9ce91 8076 eos_ok = !contained_procedure ();
6de9cd9a
DN
8077 break;
8078
8079 case COMP_BLOCK_DATA:
8080 *st = ST_END_BLOCK_DATA;
8081 target = " block data";
ddc9ce91 8082 eos_ok = 1;
6de9cd9a
DN
8083 break;
8084
8085 case COMP_MODULE:
8086 *st = ST_END_MODULE;
8087 target = " module";
ddc9ce91 8088 eos_ok = 1;
6de9cd9a
DN
8089 break;
8090
4668d6f9
PT
8091 case COMP_SUBMODULE:
8092 *st = ST_END_SUBMODULE;
8093 target = " submodule";
8094 eos_ok = 1;
8095 break;
8096
6de9cd9a
DN
8097 case COMP_INTERFACE:
8098 *st = ST_END_INTERFACE;
8099 target = " interface";
ddc9ce91 8100 eos_ok = 0;
6de9cd9a
DN
8101 break;
8102
f6288c24
FR
8103 case COMP_MAP:
8104 *st = ST_END_MAP;
8105 target = " map";
8106 eos_ok = 0;
8107 break;
8108
8109 case COMP_UNION:
8110 *st = ST_END_UNION;
8111 target = " union";
8112 eos_ok = 0;
8113 break;
8114
8115 case COMP_STRUCTURE:
8116 *st = ST_END_STRUCTURE;
8117 target = " structure";
8118 eos_ok = 0;
8119 break;
8120
6de9cd9a 8121 case COMP_DERIVED:
30b608eb 8122 case COMP_DERIVED_CONTAINS:
6de9cd9a
DN
8123 *st = ST_END_TYPE;
8124 target = " type";
ddc9ce91 8125 eos_ok = 0;
6de9cd9a
DN
8126 break;
8127
03af1e4c
DK
8128 case COMP_ASSOCIATE:
8129 *st = ST_END_ASSOCIATE;
8130 target = " associate";
8131 eos_ok = 0;
8132 break;
8133
9abe5e56
DK
8134 case COMP_BLOCK:
8135 *st = ST_END_BLOCK;
8136 target = " block";
8137 eos_ok = 0;
8138 break;
8139
6de9cd9a
DN
8140 case COMP_IF:
8141 *st = ST_ENDIF;
8142 target = " if";
ddc9ce91 8143 eos_ok = 0;
6de9cd9a
DN
8144 break;
8145
8146 case COMP_DO:
8c6a85e3 8147 case COMP_DO_CONCURRENT:
6de9cd9a
DN
8148 *st = ST_ENDDO;
8149 target = " do";
ddc9ce91 8150 eos_ok = 0;
6de9cd9a
DN
8151 break;
8152
d0a4a61c
TB
8153 case COMP_CRITICAL:
8154 *st = ST_END_CRITICAL;
8155 target = " critical";
8156 eos_ok = 0;
8157 break;
8158
6de9cd9a 8159 case COMP_SELECT:
cf2b3c22 8160 case COMP_SELECT_TYPE:
6de9cd9a
DN
8161 *st = ST_END_SELECT;
8162 target = " select";
ddc9ce91 8163 eos_ok = 0;
6de9cd9a
DN
8164 break;
8165
8166 case COMP_FORALL:
8167 *st = ST_END_FORALL;
8168 target = " forall";
ddc9ce91 8169 eos_ok = 0;
6de9cd9a
DN
8170 break;
8171
8172 case COMP_WHERE:
8173 *st = ST_END_WHERE;
8174 target = " where";
ddc9ce91 8175 eos_ok = 0;
6de9cd9a
DN
8176 break;
8177
25d8f0a2
TS
8178 case COMP_ENUM:
8179 *st = ST_END_ENUM;
8180 target = " enum";
8181 eos_ok = 0;
8182 last_initializer = NULL;
8183 set_enum_kind ();
8184 gfc_free_enum_history ();
8185 break;
8186
6de9cd9a
DN
8187 default:
8188 gfc_error ("Unexpected END statement at %C");
8189 goto cleanup;
8190 }
8191
3a43b5b3 8192 old_loc = gfc_current_locus;
6de9cd9a
DN
8193 if (gfc_match_eos () == MATCH_YES)
8194 {
272001a2
TB
8195 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8196 {
524af0d6 8197 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
70112e2a 8198 "instead of %s statement at %L",
4668d6f9
PT
8199 abreviated_modproc_decl ? "END PROCEDURE"
8200 : gfc_ascii_statement(*st), &old_loc))
272001a2
TB
8201 goto cleanup;
8202 }
8203 else if (!eos_ok)
6de9cd9a 8204 {
66e4ab31 8205 /* We would have required END [something]. */
59ce85b5
TS
8206 gfc_error ("%s statement expected at %L",
8207 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
8208 goto cleanup;
8209 }
8210
8211 return MATCH_YES;
8212 }
8213
8214 /* Verify that we've got the sort of end-block that we're expecting. */
8215 if (gfc_match (target) != MATCH_YES)
8216 {
4668d6f9
PT
8217 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8218 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
6de9cd9a
DN
8219 goto cleanup;
8220 }
874108a9
AV
8221 else
8222 got_matching_end = true;
6de9cd9a 8223
3a43b5b3 8224 old_loc = gfc_current_locus;
6de9cd9a
DN
8225 /* If we're at the end, make sure a block name wasn't required. */
8226 if (gfc_match_eos () == MATCH_YES)
8227 {
8228
690af379 8229 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
d0a4a61c 8230 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
03af1e4c 8231 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6de9cd9a
DN
8232 return MATCH_YES;
8233
9abe5e56 8234 if (!block_name)
6de9cd9a
DN
8235 return MATCH_YES;
8236
c4100eae 8237 gfc_error ("Expected block name of %qs in %s statement at %L",
3a43b5b3 8238 block_name, gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
8239
8240 return MATCH_ERROR;
8241 }
8242
8243 /* END INTERFACE has a special handler for its several possible endings. */
8244 if (*st == ST_END_INTERFACE)
8245 return gfc_match_end_interface ();
8246
66e4ab31
SK
8247 /* We haven't hit the end of statement, so what is left must be an
8248 end-name. */
6de9cd9a
DN
8249 m = gfc_match_space ();
8250 if (m == MATCH_YES)
8251 m = gfc_match_name (name);
8252
8253 if (m == MATCH_NO)
8254 gfc_error ("Expected terminating name at %C");
8255 if (m != MATCH_YES)
8256 goto cleanup;
8257
8258 if (block_name == NULL)
8259 goto syntax;
8260
3d5dc929
PT
8261 /* We have to pick out the declared submodule name from the composite
8262 required by F2008:11.2.3 para 2, which ends in the declared name. */
8263 if (state == COMP_SUBMODULE)
8264 block_name = strchr (block_name, '.') + 1;
8265
3070bab4 8266 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6de9cd9a 8267 {
c4100eae 8268 gfc_error ("Expected label %qs for %s statement at %C", block_name,
6de9cd9a
DN
8269 gfc_ascii_statement (*st));
8270 goto cleanup;
8271 }
3070bab4
JW
8272 /* Procedure pointer as function result. */
8273 else if (strcmp (block_name, "ppr@") == 0
8274 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8275 {
c4100eae 8276 gfc_error ("Expected label %qs for %s statement at %C",
3070bab4
JW
8277 gfc_current_block ()->ns->proc_name->name,
8278 gfc_ascii_statement (*st));
8279 goto cleanup;
8280 }
6de9cd9a
DN
8281
8282 if (gfc_match_eos () == MATCH_YES)
8283 return MATCH_YES;
8284
8285syntax:
8286 gfc_syntax_error (*st);
8287
8288cleanup:
63645982 8289 gfc_current_locus = old_loc;
0cab6b73
TK
8290
8291 /* If we are missing an END BLOCK, we created a half-ready namespace.
8292 Remove it from the parent namespace's sibling list. */
8293
874108a9 8294 while (state == COMP_BLOCK && !got_matching_end)
0cab6b73
TK
8295 {
8296 parent_ns = gfc_current_ns->parent;
8297
8298 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8299
8300 prev_ns = NULL;
8301 ns = *nsp;
8302 while (ns)
8303 {
8304 if (ns == gfc_current_ns)
8305 {
8306 if (prev_ns == NULL)
8307 *nsp = NULL;
8308 else
8309 prev_ns->sibling = ns->sibling;
8310 }
8311 prev_ns = ns;
8312 ns = ns->sibling;
8313 }
874108a9 8314
0cab6b73
TK
8315 gfc_free_namespace (gfc_current_ns);
8316 gfc_current_ns = parent_ns;
9f7ba208
LK
8317 gfc_state_stack = gfc_state_stack->previous;
8318 state = gfc_current_state ();
0cab6b73
TK
8319 }
8320
6de9cd9a
DN
8321 return MATCH_ERROR;
8322}
8323
8324
8325
8326/***************** Attribute declaration statements ****************/
8327
8328/* Set the attribute of a single variable. */
8329
8330static match
8331attr_decl1 (void)
8332{
8333 char name[GFC_MAX_SYMBOL_LEN + 1];
8334 gfc_array_spec *as;
97440db5
ML
8335
8336 /* Workaround -Wmaybe-uninitialized false positive during
8337 profiledbootstrap by initializing them. */
8338 gfc_symbol *sym = NULL;
6de9cd9a
DN
8339 locus var_locus;
8340 match m;
8341
8342 as = NULL;
8343
8344 m = gfc_match_name (name);
8345 if (m != MATCH_YES)
8346 goto cleanup;
8347
08a6b8e0 8348 if (find_special (name, &sym, false))
6de9cd9a
DN
8349 return MATCH_ERROR;
8350
524af0d6 8351 if (!check_function_name (name))
bb9de0c4
JW
8352 {
8353 m = MATCH_ERROR;
8354 goto cleanup;
8355 }
f5acf0f2 8356
63645982 8357 var_locus = gfc_current_locus;
6de9cd9a
DN
8358
8359 /* Deal with possible array specification for certain attributes. */
8360 if (current_attr.dimension
be59db2d 8361 || current_attr.codimension
6de9cd9a
DN
8362 || current_attr.allocatable
8363 || current_attr.pointer
8364 || current_attr.target)
8365 {
be59db2d
TB
8366 m = gfc_match_array_spec (&as, !current_attr.codimension,
8367 !current_attr.dimension
8368 && !current_attr.pointer
8369 && !current_attr.target);
6de9cd9a
DN
8370 if (m == MATCH_ERROR)
8371 goto cleanup;
8372
8373 if (current_attr.dimension && m == MATCH_NO)
8374 {
636dff67
SK
8375 gfc_error ("Missing array specification at %L in DIMENSION "
8376 "statement", &var_locus);
6de9cd9a
DN
8377 m = MATCH_ERROR;
8378 goto cleanup;
8379 }
8380
1283ab12
TB
8381 if (current_attr.dimension && sym->value)
8382 {
8383 gfc_error ("Dimensions specified for %s at %L after its "
bd2c6270 8384 "initialization", sym->name, &var_locus);
1283ab12
TB
8385 m = MATCH_ERROR;
8386 goto cleanup;
8387 }
8388
be59db2d
TB
8389 if (current_attr.codimension && m == MATCH_NO)
8390 {
8391 gfc_error ("Missing array specification at %L in CODIMENSION "
8392 "statement", &var_locus);
8393 m = MATCH_ERROR;
8394 goto cleanup;
8395 }
8396
6de9cd9a
DN
8397 if ((current_attr.allocatable || current_attr.pointer)
8398 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8399 {
636dff67 8400 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
8401 m = MATCH_ERROR;
8402 goto cleanup;
8403 }
8404 }
8405
2e23972e
JW
8406 /* Update symbol table. DIMENSION attribute is set in
8407 gfc_set_array_spec(). For CLASS variables, this must be applied
b04533af 8408 to the first component, or '_data' field. */
d40477b4 8409 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6de9cd9a 8410 {
c9935123
SK
8411 /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr. Check
8412 for duplicate attribute here. */
8413 if (CLASS_DATA(sym)->attr.dimension == 1 && as)
8414 {
8415 gfc_error ("Duplicate DIMENSION attribute at %C");
8416 m = MATCH_ERROR;
8417 goto cleanup;
8418 }
8419
524af0d6 8420 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
2e23972e
JW
8421 {
8422 m = MATCH_ERROR;
8423 goto cleanup;
8424 }
2e23972e
JW
8425 }
8426 else
8427 {
be59db2d 8428 if (current_attr.dimension == 0 && current_attr.codimension == 0
524af0d6 8429 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
2e23972e
JW
8430 {
8431 m = MATCH_ERROR;
8432 goto cleanup;
8433 }
6de9cd9a 8434 }
f5acf0f2 8435
528622fd 8436 if (sym->ts.type == BT_CLASS
9b6da3c7 8437 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
96d9b22c
JW
8438 {
8439 m = MATCH_ERROR;
8440 goto cleanup;
8441 }
6de9cd9a 8442
524af0d6 8443 if (!gfc_set_array_spec (sym, as, &var_locus))
6de9cd9a
DN
8444 {
8445 m = MATCH_ERROR;
8446 goto cleanup;
8447 }
d51347f9 8448
83d890b9
AL
8449 if (sym->attr.cray_pointee && sym->as != NULL)
8450 {
8451 /* Fix the array spec. */
f5acf0f2 8452 m = gfc_mod_pointee_as (sym->as);
83d890b9
AL
8453 if (m == MATCH_ERROR)
8454 goto cleanup;
8455 }
6de9cd9a 8456
524af0d6 8457 if (!gfc_add_attribute (&sym->attr, &var_locus))
1902704e
PT
8458 {
8459 m = MATCH_ERROR;
8460 goto cleanup;
8461 }
8462
6de9cd9a
DN
8463 if ((current_attr.external || current_attr.intrinsic)
8464 && sym->attr.flavor != FL_PROCEDURE
524af0d6 8465 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6de9cd9a
DN
8466 {
8467 m = MATCH_ERROR;
8468 goto cleanup;
8469 }
8470
3070bab4
JW
8471 add_hidden_procptr_result (sym);
8472
6de9cd9a
DN
8473 return MATCH_YES;
8474
8475cleanup:
8476 gfc_free_array_spec (as);
8477 return m;
8478}
8479
8480
8481/* Generic attribute declaration subroutine. Used for attributes that
8482 just have a list of names. */
8483
8484static match
8485attr_decl (void)
8486{
8487 match m;
8488
8489 /* Gobble the optional double colon, by simply ignoring the result
8490 of gfc_match(). */
8491 gfc_match (" ::");
8492
8493 for (;;)
8494 {
8495 m = attr_decl1 ();
8496 if (m != MATCH_YES)
8497 break;
8498
8499 if (gfc_match_eos () == MATCH_YES)
8500 {
8501 m = MATCH_YES;
8502 break;
8503 }
8504
8505 if (gfc_match_char (',') != MATCH_YES)
8506 {
8507 gfc_error ("Unexpected character in variable list at %C");
8508 m = MATCH_ERROR;
8509 break;
8510 }
8511 }
8512
8513 return m;
8514}
8515
8516
83d890b9
AL
8517/* This routine matches Cray Pointer declarations of the form:
8518 pointer ( <pointer>, <pointee> )
8519 or
d51347f9
TB
8520 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8521 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
8522 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8523 be either a scalar, or an array declaration. No space is allocated for
d51347f9 8524 the pointee. For the statement
83d890b9
AL
8525 pointer (ipt, ar(10))
8526 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 8527 ar(i) => ((<type> *) ipt)(i)
b122dc6a 8528 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
8529
8530static match
8531cray_pointer_decl (void)
8532{
8533 match m;
be59db2d 8534 gfc_array_spec *as = NULL;
83d890b9
AL
8535 gfc_symbol *cptr; /* Pointer symbol. */
8536 gfc_symbol *cpte; /* Pointee symbol. */
8537 locus var_locus;
8538 bool done = false;
8539
8540 while (!done)
8541 {
8542 if (gfc_match_char ('(') != MATCH_YES)
8543 {
a4d9b221 8544 gfc_error ("Expected %<(%> at %C");
d51347f9 8545 return MATCH_ERROR;
83d890b9 8546 }
d51347f9 8547
83d890b9
AL
8548 /* Match pointer. */
8549 var_locus = gfc_current_locus;
8550 gfc_clear_attr (&current_attr);
8551 gfc_add_cray_pointer (&current_attr, &var_locus);
8552 current_ts.type = BT_INTEGER;
8553 current_ts.kind = gfc_index_integer_kind;
8554
d51347f9 8555 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
8556 if (m != MATCH_YES)
8557 {
8558 gfc_error ("Expected variable name at %C");
8559 return m;
8560 }
d51347f9 8561
524af0d6 8562 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
83d890b9
AL
8563 return MATCH_ERROR;
8564
d51347f9 8565 gfc_set_sym_referenced (cptr);
83d890b9
AL
8566
8567 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8568 {
8569 cptr->ts.type = BT_INTEGER;
d51347f9 8570 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
8571 }
8572 else if (cptr->ts.type != BT_INTEGER)
8573 {
e25a0da3 8574 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
8575 return MATCH_ERROR;
8576 }
8577 else if (cptr->ts.kind < gfc_index_integer_kind)
db30e21c 8578 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
e25a0da3 8579 " memory addresses require %d bytes",
636dff67 8580 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
8581
8582 if (gfc_match_char (',') != MATCH_YES)
8583 {
8584 gfc_error ("Expected \",\" at %C");
d51347f9 8585 return MATCH_ERROR;
83d890b9
AL
8586 }
8587
d51347f9 8588 /* Match Pointee. */
83d890b9
AL
8589 var_locus = gfc_current_locus;
8590 gfc_clear_attr (&current_attr);
8591 gfc_add_cray_pointee (&current_attr, &var_locus);
8592 current_ts.type = BT_UNKNOWN;
8593 current_ts.kind = 0;
8594
8595 m = gfc_match_symbol (&cpte, 0);
8596 if (m != MATCH_YES)
8597 {
8598 gfc_error ("Expected variable name at %C");
8599 return m;
8600 }
d51347f9 8601
83d890b9 8602 /* Check for an optional array spec. */
be59db2d 8603 m = gfc_match_array_spec (&as, true, false);
83d890b9
AL
8604 if (m == MATCH_ERROR)
8605 {
8606 gfc_free_array_spec (as);
8607 return m;
8608 }
8609 else if (m == MATCH_NO)
8610 {
8611 gfc_free_array_spec (as);
8612 as = NULL;
f5acf0f2 8613 }
83d890b9 8614
524af0d6 8615 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
83d890b9
AL
8616 return MATCH_ERROR;
8617
8618 gfc_set_sym_referenced (cpte);
8619
8620 if (cpte->as == NULL)
8621 {
524af0d6 8622 if (!gfc_set_array_spec (cpte, as, &var_locus))
1fe61adf 8623 gfc_internal_error ("Cannot set Cray pointee array spec.");
83d890b9
AL
8624 }
8625 else if (as != NULL)
8626 {
e25a0da3 8627 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
8628 gfc_free_array_spec (as);
8629 return MATCH_ERROR;
8630 }
f5acf0f2 8631
83d890b9 8632 as = NULL;
f5acf0f2 8633
83d890b9
AL
8634 if (cpte->as != NULL)
8635 {
8636 /* Fix array spec. */
8637 m = gfc_mod_pointee_as (cpte->as);
8638 if (m == MATCH_ERROR)
8639 return m;
f5acf0f2
PT
8640 }
8641
83d890b9 8642 /* Point the Pointee at the Pointer. */
b122dc6a 8643 cpte->cp_pointer = cptr;
83d890b9
AL
8644
8645 if (gfc_match_char (')') != MATCH_YES)
8646 {
8647 gfc_error ("Expected \")\" at %C");
f5acf0f2 8648 return MATCH_ERROR;
83d890b9
AL
8649 }
8650 m = gfc_match_char (',');
8651 if (m != MATCH_YES)
8652 done = true; /* Stop searching for more declarations. */
8653
8654 }
f5acf0f2 8655
83d890b9
AL
8656 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8657 || gfc_match_eos () != MATCH_YES)
8658 {
a4d9b221 8659 gfc_error ("Expected %<,%> or end of statement at %C");
83d890b9
AL
8660 return MATCH_ERROR;
8661 }
8662 return MATCH_YES;
8663}
8664
8665
6de9cd9a
DN
8666match
8667gfc_match_external (void)
8668{
8669
8670 gfc_clear_attr (&current_attr);
1902704e 8671 current_attr.external = 1;
6de9cd9a
DN
8672
8673 return attr_decl ();
8674}
8675
8676
6de9cd9a
DN
8677match
8678gfc_match_intent (void)
8679{
8680 sym_intent intent;
8681
9abe5e56
DK
8682 /* This is not allowed within a BLOCK construct! */
8683 if (gfc_current_state () == COMP_BLOCK)
8684 {
8685 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8686 return MATCH_ERROR;
8687 }
8688
6de9cd9a
DN
8689 intent = match_intent_spec ();
8690 if (intent == INTENT_UNKNOWN)
8691 return MATCH_ERROR;
8692
8693 gfc_clear_attr (&current_attr);
1902704e 8694 current_attr.intent = intent;
6de9cd9a
DN
8695
8696 return attr_decl ();
8697}
8698
8699
8700match
8701gfc_match_intrinsic (void)
8702{
8703
8704 gfc_clear_attr (&current_attr);
1902704e 8705 current_attr.intrinsic = 1;
6de9cd9a
DN
8706
8707 return attr_decl ();
8708}
8709
8710
8711match
8712gfc_match_optional (void)
8713{
9abe5e56
DK
8714 /* This is not allowed within a BLOCK construct! */
8715 if (gfc_current_state () == COMP_BLOCK)
8716 {
8717 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8718 return MATCH_ERROR;
8719 }
6de9cd9a
DN
8720
8721 gfc_clear_attr (&current_attr);
1902704e 8722 current_attr.optional = 1;
6de9cd9a
DN
8723
8724 return attr_decl ();
8725}
8726
8727
8728match
8729gfc_match_pointer (void)
8730{
83d890b9 8731 gfc_gobble_whitespace ();
8fc541d3 8732 if (gfc_peek_ascii_char () == '(')
83d890b9 8733 {
c61819ff 8734 if (!flag_cray_pointer)
83d890b9 8735 {
a3f9f006
ML
8736 gfc_error ("Cray pointer declaration at %C requires "
8737 "%<-fcray-pointer%> flag");
83d890b9
AL
8738 return MATCH_ERROR;
8739 }
8740 return cray_pointer_decl ();
8741 }
8742 else
8743 {
8744 gfc_clear_attr (&current_attr);
1902704e 8745 current_attr.pointer = 1;
f5acf0f2 8746
83d890b9
AL
8747 return attr_decl ();
8748 }
6de9cd9a
DN
8749}
8750
8751
8752match
8753gfc_match_allocatable (void)
8754{
6de9cd9a 8755 gfc_clear_attr (&current_attr);
1902704e 8756 current_attr.allocatable = 1;
6de9cd9a
DN
8757
8758 return attr_decl ();
8759}
8760
8761
be59db2d
TB
8762match
8763gfc_match_codimension (void)
8764{
8765 gfc_clear_attr (&current_attr);
8766 current_attr.codimension = 1;
8767
8768 return attr_decl ();
8769}
8770
8771
fe4e525c
TB
8772match
8773gfc_match_contiguous (void)
8774{
524af0d6 8775 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
fe4e525c
TB
8776 return MATCH_ERROR;
8777
8778 gfc_clear_attr (&current_attr);
8779 current_attr.contiguous = 1;
8780
8781 return attr_decl ();
8782}
8783
8784
6de9cd9a
DN
8785match
8786gfc_match_dimension (void)
8787{
6de9cd9a 8788 gfc_clear_attr (&current_attr);
1902704e 8789 current_attr.dimension = 1;
6de9cd9a
DN
8790
8791 return attr_decl ();
8792}
8793
8794
8795match
8796gfc_match_target (void)
8797{
6de9cd9a 8798 gfc_clear_attr (&current_attr);
1902704e 8799 current_attr.target = 1;
6de9cd9a
DN
8800
8801 return attr_decl ();
8802}
8803
8804
8805/* Match the list of entities being specified in a PUBLIC or PRIVATE
8806 statement. */
8807
8808static match
8809access_attr_decl (gfc_statement st)
8810{
8811 char name[GFC_MAX_SYMBOL_LEN + 1];
8812 interface_type type;
8813 gfc_user_op *uop;
c3f34952 8814 gfc_symbol *sym, *dt_sym;
a1ee985f 8815 gfc_intrinsic_op op;
6de9cd9a 8816 match m;
b4882698 8817 gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
8818
8819 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
8820 goto done;
8821
8822 for (;;)
8823 {
a1ee985f 8824 m = gfc_match_generic_spec (&type, name, &op);
6de9cd9a
DN
8825 if (m == MATCH_NO)
8826 goto syntax;
8827 if (m == MATCH_ERROR)
b4882698 8828 goto done;
6de9cd9a
DN
8829
8830 switch (type)
8831 {
8832 case INTERFACE_NAMELESS:
9e1d712c 8833 case INTERFACE_ABSTRACT:
6de9cd9a
DN
8834 goto syntax;
8835
8836 case INTERFACE_GENERIC:
e73d3ca6 8837 case INTERFACE_DTIO:
dc42a736 8838
6de9cd9a
DN
8839 if (gfc_get_symbol (name, NULL, &sym))
8840 goto done;
8841
41036686
PT
8842 if (type == INTERFACE_DTIO
8843 && gfc_current_ns->proc_name
8844 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
8845 && sym->attr.flavor == FL_UNKNOWN)
8846 sym->attr.flavor = FL_PROCEDURE;
8847
b4882698
SK
8848 if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
8849 goto done;
6de9cd9a 8850
c3f34952 8851 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
b4882698
SK
8852 && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
8853 goto done;
c3f34952 8854
6de9cd9a
DN
8855 break;
8856
8857 case INTERFACE_INTRINSIC_OP:
a1ee985f 8858 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6de9cd9a 8859 {
fb03a37e
TK
8860 gfc_intrinsic_op other_op;
8861
b4882698 8862 gfc_current_ns->operator_access[op] = access;
fb03a37e
TK
8863
8864 /* Handle the case if there is another op with the same
8865 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
8866 other_op = gfc_equivalent_op (op);
8867
8868 if (other_op != INTRINSIC_NONE)
b4882698 8869 gfc_current_ns->operator_access[other_op] = access;
6de9cd9a
DN
8870 }
8871 else
8872 {
8873 gfc_error ("Access specification of the %s operator at %C has "
a1ee985f 8874 "already been specified", gfc_op2string (op));
6de9cd9a
DN
8875 goto done;
8876 }
8877
8878 break;
8879
8880 case INTERFACE_USER_OP:
8881 uop = gfc_get_uop (name);
8882
8883 if (uop->access == ACCESS_UNKNOWN)
8884 {
b4882698 8885 uop->access = access;
6de9cd9a
DN
8886 }
8887 else
8888 {
636dff67
SK
8889 gfc_error ("Access specification of the .%s. operator at %C "
8890 "has already been specified", sym->name);
6de9cd9a
DN
8891 goto done;
8892 }
8893
8894 break;
8895 }
8896
8897 if (gfc_match_char (',') == MATCH_NO)
8898 break;
8899 }
8900
8901 if (gfc_match_eos () != MATCH_YES)
8902 goto syntax;
8903 return MATCH_YES;
8904
8905syntax:
8906 gfc_syntax_error (st);
8907
8908done:
8909 return MATCH_ERROR;
8910}
8911
8912
ee7e677f
TB
8913match
8914gfc_match_protected (void)
8915{
8916 gfc_symbol *sym;
8917 match m;
b4882698
SK
8918 char c;
8919
8920 /* PROTECTED has already been seen, but must be followed by whitespace
8921 or ::. */
8922 c = gfc_peek_ascii_char ();
8923 if (!gfc_is_whitespace (c) && c != ':')
8924 return MATCH_NO;
ee7e677f 8925
73641c88
SK
8926 if (!gfc_current_ns->proc_name
8927 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
ee7e677f
TB
8928 {
8929 gfc_error ("PROTECTED at %C only allowed in specification "
8930 "part of a module");
8931 return MATCH_ERROR;
8932
8933 }
8934
b4882698
SK
8935 gfc_match (" ::");
8936
524af0d6 8937 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
ee7e677f
TB
8938 return MATCH_ERROR;
8939
b4882698 8940 /* PROTECTED has an entity-list. */
ee7e677f
TB
8941 if (gfc_match_eos () == MATCH_YES)
8942 goto syntax;
8943
8944 for(;;)
8945 {
8946 m = gfc_match_symbol (&sym, 0);
8947 switch (m)
8948 {
8949 case MATCH_YES:
524af0d6 8950 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
ee7e677f
TB
8951 return MATCH_ERROR;
8952 goto next_item;
8953
8954 case MATCH_NO:
8955 break;
8956
8957 case MATCH_ERROR:
8958 return MATCH_ERROR;
8959 }
8960
8961 next_item:
8962 if (gfc_match_eos () == MATCH_YES)
8963 break;
8964 if (gfc_match_char (',') != MATCH_YES)
8965 goto syntax;
8966 }
8967
8968 return MATCH_YES;
8969
8970syntax:
8971 gfc_error ("Syntax error in PROTECTED statement at %C");
8972 return MATCH_ERROR;
8973}
8974
8975
86bf520d 8976/* The PRIVATE statement is a bit weird in that it can be an attribute
df2fba9e 8977 declaration, but also works as a standalone statement inside of a
6de9cd9a
DN
8978 type declaration or a module. */
8979
8980match
636dff67 8981gfc_match_private (gfc_statement *st)
6de9cd9a 8982{
b4882698
SK
8983 gfc_state_data *prev;
8984 char c;
6de9cd9a
DN
8985
8986 if (gfc_match ("private") != MATCH_YES)
8987 return MATCH_NO;
8988
b4882698
SK
8989 /* Try matching PRIVATE without an access-list. */
8990 if (gfc_match_eos () == MATCH_YES)
6de9cd9a 8991 {
b4882698
SK
8992 prev = gfc_state_stack->previous;
8993 if (gfc_current_state () != COMP_MODULE
8994 && !(gfc_current_state () == COMP_DERIVED
8995 && prev && prev->state == COMP_MODULE)
8996 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
8997 && prev->previous && prev->previous->state == COMP_MODULE))
6de9cd9a 8998 {
b4882698
SK
8999 gfc_error ("PRIVATE statement at %C is only allowed in the "
9000 "specification part of a module");
9001 return MATCH_ERROR;
6de9cd9a
DN
9002 }
9003
b4882698
SK
9004 *st = ST_PRIVATE;
9005 return MATCH_YES;
6de9cd9a
DN
9006 }
9007
b4882698
SK
9008 /* At this point, PRIVATE must be followed by whitespace or ::. */
9009 c = gfc_peek_ascii_char ();
9010 if (!gfc_is_whitespace (c) && c != ':')
9011 return MATCH_NO;
9012
9013 prev = gfc_state_stack->previous;
9014 if (gfc_current_state () != COMP_MODULE
9015 && !(gfc_current_state () == COMP_DERIVED
9016 && prev && prev->state == COMP_MODULE)
9017 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9018 && prev->previous && prev->previous->state == COMP_MODULE))
6de9cd9a 9019 {
b4882698
SK
9020 gfc_error ("PRIVATE statement at %C is only allowed in the "
9021 "specification part of a module");
9022 return MATCH_ERROR;
6de9cd9a
DN
9023 }
9024
9025 *st = ST_ATTR_DECL;
9026 return access_attr_decl (ST_PRIVATE);
9027}
9028
9029
9030match
636dff67 9031gfc_match_public (gfc_statement *st)
6de9cd9a 9032{
b4882698 9033 char c;
6de9cd9a
DN
9034
9035 if (gfc_match ("public") != MATCH_YES)
9036 return MATCH_NO;
9037
b4882698
SK
9038 /* Try matching PUBLIC without an access-list. */
9039 if (gfc_match_eos () == MATCH_YES)
9040 {
9041 if (gfc_current_state () != COMP_MODULE)
9042 {
9043 gfc_error ("PUBLIC statement at %C is only allowed in the "
9044 "specification part of a module");
9045 return MATCH_ERROR;
9046 }
9047
9048 *st = ST_PUBLIC;
9049 return MATCH_YES;
9050 }
9051
9052 /* At this point, PUBLIC must be followed by whitespace or ::. */
9053 c = gfc_peek_ascii_char ();
9054 if (!gfc_is_whitespace (c) && c != ':')
9055 return MATCH_NO;
9056
d51347f9
TB
9057 if (gfc_current_state () != COMP_MODULE)
9058 {
9059 gfc_error ("PUBLIC statement at %C is only allowed in the "
9060 "specification part of a module");
9061 return MATCH_ERROR;
9062 }
9063
6de9cd9a
DN
9064 *st = ST_ATTR_DECL;
9065 return access_attr_decl (ST_PUBLIC);
9066}
9067
9068
9069/* Workhorse for gfc_match_parameter. */
9070
9071static match
9072do_parm (void)
9073{
9074 gfc_symbol *sym;
9075 gfc_expr *init;
9076 match m;
524af0d6 9077 bool t;
6de9cd9a
DN
9078
9079 m = gfc_match_symbol (&sym, 0);
9080 if (m == MATCH_NO)
9081 gfc_error ("Expected variable name at %C in PARAMETER statement");
9082
9083 if (m != MATCH_YES)
9084 return m;
9085
9086 if (gfc_match_char ('=') == MATCH_NO)
9087 {
9088 gfc_error ("Expected = sign in PARAMETER statement at %C");
9089 return MATCH_ERROR;
9090 }
9091
9092 m = gfc_match_init_expr (&init);
9093 if (m == MATCH_NO)
9094 gfc_error ("Expected expression at %C in PARAMETER statement");
9095 if (m != MATCH_YES)
9096 return m;
9097
9098 if (sym->ts.type == BT_UNKNOWN
524af0d6 9099 && !gfc_set_default_type (sym, 1, NULL))
6de9cd9a
DN
9100 {
9101 m = MATCH_ERROR;
9102 goto cleanup;
9103 }
9104
524af0d6
JB
9105 if (!gfc_check_assign_symbol (sym, NULL, init)
9106 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
6de9cd9a
DN
9107 {
9108 m = MATCH_ERROR;
9109 goto cleanup;
9110 }
9111
1283ab12
TB
9112 if (sym->value)
9113 {
9114 gfc_error ("Initializing already initialized variable at %C");
9115 m = MATCH_ERROR;
9116 goto cleanup;
9117 }
9118
7919373d 9119 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
524af0d6 9120 return (t) ? MATCH_YES : MATCH_ERROR;
6de9cd9a
DN
9121
9122cleanup:
9123 gfc_free_expr (init);
9124 return m;
9125}
9126
9127
9128/* Match a parameter statement, with the weird syntax that these have. */
9129
9130match
9131gfc_match_parameter (void)
9132{
35ea947f 9133 const char *term = " )%t";
6de9cd9a
DN
9134 match m;
9135
9136 if (gfc_match_char ('(') == MATCH_NO)
35ea947f
FR
9137 {
9138 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
9139 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9140 return MATCH_NO;
9141 term = " %t";
9142 }
6de9cd9a
DN
9143
9144 for (;;)
9145 {
9146 m = do_parm ();
9147 if (m != MATCH_YES)
9148 break;
9149
35ea947f 9150 if (gfc_match (term) == MATCH_YES)
6de9cd9a
DN
9151 break;
9152
9153 if (gfc_match_char (',') != MATCH_YES)
9154 {
9155 gfc_error ("Unexpected characters in PARAMETER statement at %C");
9156 m = MATCH_ERROR;
9157 break;
9158 }
9159 }
9160
9161 return m;
9162}
9163
9164
34d567d1
FR
9165match
9166gfc_match_automatic (void)
9167{
9168 gfc_symbol *sym;
9169 match m;
9170 bool seen_symbol = false;
9171
9172 if (!flag_dec_static)
9173 {
cf004230
FR
9174 gfc_error ("%s at %C is a DEC extension, enable with "
9175 "%<-fdec-static%>",
9176 "AUTOMATIC"
9177 );
34d567d1
FR
9178 return MATCH_ERROR;
9179 }
9180
9181 gfc_match (" ::");
9182
9183 for (;;)
9184 {
9185 m = gfc_match_symbol (&sym, 0);
9186 switch (m)
9187 {
9188 case MATCH_NO:
9189 break;
9190
9191 case MATCH_ERROR:
9192 return MATCH_ERROR;
9193
9194 case MATCH_YES:
9195 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9196 return MATCH_ERROR;
9197 seen_symbol = true;
9198 break;
9199 }
9200
9201 if (gfc_match_eos () == MATCH_YES)
9202 break;
9203 if (gfc_match_char (',') != MATCH_YES)
9204 goto syntax;
9205 }
9206
9207 if (!seen_symbol)
9208 {
9209 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9210 return MATCH_ERROR;
9211 }
9212
9213 return MATCH_YES;
9214
9215syntax:
9216 gfc_error ("Syntax error in AUTOMATIC statement at %C");
9217 return MATCH_ERROR;
9218}
9219
9220
9221match
9222gfc_match_static (void)
9223{
9224 gfc_symbol *sym;
9225 match m;
9226 bool seen_symbol = false;
9227
9228 if (!flag_dec_static)
9229 {
cf004230
FR
9230 gfc_error ("%s at %C is a DEC extension, enable with "
9231 "%<-fdec-static%>",
9232 "STATIC");
34d567d1
FR
9233 return MATCH_ERROR;
9234 }
9235
9236 gfc_match (" ::");
9237
9238 for (;;)
9239 {
9240 m = gfc_match_symbol (&sym, 0);
9241 switch (m)
9242 {
9243 case MATCH_NO:
9244 break;
9245
9246 case MATCH_ERROR:
9247 return MATCH_ERROR;
9248
9249 case MATCH_YES:
9250 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9251 &gfc_current_locus))
9252 return MATCH_ERROR;
9253 seen_symbol = true;
9254 break;
9255 }
9256
9257 if (gfc_match_eos () == MATCH_YES)
9258 break;
9259 if (gfc_match_char (',') != MATCH_YES)
9260 goto syntax;
9261 }
9262
9263 if (!seen_symbol)
9264 {
9265 gfc_error ("Expected entity-list in STATIC statement at %C");
9266 return MATCH_ERROR;
9267 }
9268
9269 return MATCH_YES;
9270
9271syntax:
9272 gfc_error ("Syntax error in STATIC statement at %C");
9273 return MATCH_ERROR;
9274}
9275
9276
6de9cd9a
DN
9277/* Save statements have a special syntax. */
9278
9279match
9280gfc_match_save (void)
9281{
9056bd70
TS
9282 char n[GFC_MAX_SYMBOL_LEN+1];
9283 gfc_common_head *c;
6de9cd9a
DN
9284 gfc_symbol *sym;
9285 match m;
9286
9287 if (gfc_match_eos () == MATCH_YES)
9288 {
9289 if (gfc_current_ns->seen_save)
9290 {
524af0d6
JB
9291 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9292 "follows previous SAVE statement"))
09e87839 9293 return MATCH_ERROR;
6de9cd9a
DN
9294 }
9295
9296 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9297 return MATCH_YES;
9298 }
9299
9300 if (gfc_current_ns->save_all)
9301 {
524af0d6
JB
9302 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9303 "blanket SAVE statement"))
09e87839 9304 return MATCH_ERROR;
6de9cd9a
DN
9305 }
9306
9307 gfc_match (" ::");
9308
9309 for (;;)
9310 {
9311 m = gfc_match_symbol (&sym, 0);
9312 switch (m)
9313 {
9314 case MATCH_YES:
70112e2a 9315 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
524af0d6 9316 &gfc_current_locus))
6de9cd9a
DN
9317 return MATCH_ERROR;
9318 goto next_item;
9319
9320 case MATCH_NO:
9321 break;
9322
9323 case MATCH_ERROR:
9324 return MATCH_ERROR;
9325 }
9326
9056bd70 9327 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
9328 if (m == MATCH_ERROR)
9329 return MATCH_ERROR;
9330 if (m == MATCH_NO)
9331 goto syntax;
9332
53814b8f 9333 c = gfc_get_common (n, 0);
9056bd70
TS
9334 c->saved = 1;
9335
6de9cd9a
DN
9336 gfc_current_ns->seen_save = 1;
9337
9338 next_item:
9339 if (gfc_match_eos () == MATCH_YES)
9340 break;
9341 if (gfc_match_char (',') != MATCH_YES)
9342 goto syntax;
9343 }
9344
9345 return MATCH_YES;
9346
9347syntax:
69352933
SK
9348 if (gfc_current_ns->seen_save)
9349 {
9350 gfc_error ("Syntax error in SAVE statement at %C");
9351 return MATCH_ERROR;
9352 }
9353 else
9354 return MATCH_NO;
6de9cd9a
DN
9355}
9356
9357
06469efd
PT
9358match
9359gfc_match_value (void)
9360{
9361 gfc_symbol *sym;
9362 match m;
9363
9abe5e56
DK
9364 /* This is not allowed within a BLOCK construct! */
9365 if (gfc_current_state () == COMP_BLOCK)
9366 {
9367 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9368 return MATCH_ERROR;
9369 }
9370
524af0d6 9371 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
06469efd
PT
9372 return MATCH_ERROR;
9373
9374 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9375 {
9376 return MATCH_ERROR;
9377 }
9378
9379 if (gfc_match_eos () == MATCH_YES)
9380 goto syntax;
9381
9382 for(;;)
9383 {
9384 m = gfc_match_symbol (&sym, 0);
9385 switch (m)
9386 {
9387 case MATCH_YES:
524af0d6 9388 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
06469efd
PT
9389 return MATCH_ERROR;
9390 goto next_item;
9391
9392 case MATCH_NO:
9393 break;
9394
9395 case MATCH_ERROR:
9396 return MATCH_ERROR;
9397 }
9398
9399 next_item:
9400 if (gfc_match_eos () == MATCH_YES)
9401 break;
9402 if (gfc_match_char (',') != MATCH_YES)
9403 goto syntax;
9404 }
9405
9406 return MATCH_YES;
9407
9408syntax:
9409 gfc_error ("Syntax error in VALUE statement at %C");
9410 return MATCH_ERROR;
9411}
9412
66e4ab31 9413
775e6c3a
TB
9414match
9415gfc_match_volatile (void)
9416{
9417 gfc_symbol *sym;
ba77f7ba 9418 char *name;
775e6c3a
TB
9419 match m;
9420
524af0d6 9421 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
775e6c3a
TB
9422 return MATCH_ERROR;
9423
9424 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9425 {
9426 return MATCH_ERROR;
9427 }
9428
9429 if (gfc_match_eos () == MATCH_YES)
9430 goto syntax;
9431
9432 for(;;)
9433 {
f5acf0f2 9434 /* VOLATILE is special because it can be added to host-associated
1cc0e193 9435 symbols locally. Except for coarrays. */
9bce3c1c 9436 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
9437 switch (m)
9438 {
9439 case MATCH_YES:
ba77f7ba
SK
9440 name = XCNEWVAR (char, strlen (sym->name) + 1);
9441 strcpy (name, sym->name);
9442 if (!check_function_name (name))
9443 return MATCH_ERROR;
be59db2d
TB
9444 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9445 for variable in a BLOCK which is defined outside of the BLOCK. */
9446 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9447 {
c4100eae 9448 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
be59db2d
TB
9449 "%C, which is use-/host-associated", sym->name);
9450 return MATCH_ERROR;
9451 }
524af0d6 9452 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
775e6c3a
TB
9453 return MATCH_ERROR;
9454 goto next_item;
9455
9456 case MATCH_NO:
9457 break;
9458
9459 case MATCH_ERROR:
9460 return MATCH_ERROR;
9461 }
9462
9463 next_item:
9464 if (gfc_match_eos () == MATCH_YES)
9465 break;
9466 if (gfc_match_char (',') != MATCH_YES)
9467 goto syntax;
9468 }
9469
9470 return MATCH_YES;
9471
9472syntax:
9473 gfc_error ("Syntax error in VOLATILE statement at %C");
9474 return MATCH_ERROR;
9475}
9476
9477
1eee5628
TB
9478match
9479gfc_match_asynchronous (void)
9480{
9481 gfc_symbol *sym;
ba77f7ba 9482 char *name;
1eee5628
TB
9483 match m;
9484
524af0d6 9485 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
1eee5628
TB
9486 return MATCH_ERROR;
9487
9488 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9489 {
9490 return MATCH_ERROR;
9491 }
9492
9493 if (gfc_match_eos () == MATCH_YES)
9494 goto syntax;
9495
9496 for(;;)
9497 {
f5acf0f2 9498 /* ASYNCHRONOUS is special because it can be added to host-associated
1eee5628
TB
9499 symbols locally. */
9500 m = gfc_match_symbol (&sym, 1);
9501 switch (m)
9502 {
9503 case MATCH_YES:
ba77f7ba
SK
9504 name = XCNEWVAR (char, strlen (sym->name) + 1);
9505 strcpy (name, sym->name);
9506 if (!check_function_name (name))
9507 return MATCH_ERROR;
524af0d6 9508 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
1eee5628
TB
9509 return MATCH_ERROR;
9510 goto next_item;
9511
9512 case MATCH_NO:
9513 break;
9514
9515 case MATCH_ERROR:
9516 return MATCH_ERROR;
9517 }
9518
9519 next_item:
9520 if (gfc_match_eos () == MATCH_YES)
9521 break;
9522 if (gfc_match_char (',') != MATCH_YES)
9523 goto syntax;
9524 }
9525
9526 return MATCH_YES;
9527
9528syntax:
9529 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9530 return MATCH_ERROR;
9531}
9532
9533
4668d6f9
PT
9534/* Match a module procedure statement in a submodule. */
9535
9536match
9537gfc_match_submod_proc (void)
9538{
9539 char name[GFC_MAX_SYMBOL_LEN + 1];
9540 gfc_symbol *sym, *fsym;
9541 match m;
9542 gfc_formal_arglist *formal, *head, *tail;
9543
9544 if (gfc_current_state () != COMP_CONTAINS
9545 || !(gfc_state_stack->previous
70112e2a
PT
9546 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9547 || gfc_state_stack->previous->state == COMP_MODULE)))
4668d6f9
PT
9548 return MATCH_NO;
9549
9550 m = gfc_match (" module% procedure% %n", name);
9551 if (m != MATCH_YES)
9552 return m;
9553
9554 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9555 "at %C"))
9556 return MATCH_ERROR;
9557
9558 if (get_proc_name (name, &sym, false))
9559 return MATCH_ERROR;
9560
9561 /* Make sure that the result field is appropriately filled, even though
9562 the result symbol will be replaced later on. */
c064374d 9563 if (sym->tlink && sym->tlink->attr.function)
4668d6f9 9564 {
c064374d
PT
9565 if (sym->tlink->result
9566 && sym->tlink->result != sym->tlink)
9567 sym->result= sym->tlink->result;
4668d6f9
PT
9568 else
9569 sym->result = sym;
9570 }
9571
9572 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9573 the symbol existed before. */
9574 sym->declared_at = gfc_current_locus;
9575
9576 if (!sym->attr.module_procedure)
9577 return MATCH_ERROR;
9578
9579 /* Signal match_end to expect "end procedure". */
9580 sym->abr_modproc_decl = 1;
9581
9582 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9583 sym->attr.if_source = IFSRC_DECL;
9584
9585 gfc_new_block = sym;
9586
9587 /* Make a new formal arglist with the symbols in the procedure
9588 namespace. */
9589 head = tail = NULL;
9590 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9591 {
9592 if (formal == sym->formal)
9593 head = tail = gfc_get_formal_arglist ();
9594 else
9595 {
9596 tail->next = gfc_get_formal_arglist ();
9597 tail = tail->next;
9598 }
9599
9600 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9601 goto cleanup;
9602
9603 tail->sym = fsym;
9604 gfc_set_sym_referenced (fsym);
9605 }
9606
9607 /* The dummy symbols get cleaned up, when the formal_namespace of the
9608 interface declaration is cleared. This allows us to add the
9609 explicit interface as is done for other type of procedure. */
9610 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9611 &gfc_current_locus))
9612 return MATCH_ERROR;
9613
9614 if (gfc_match_eos () != MATCH_YES)
9615 {
9616 gfc_syntax_error (ST_MODULE_PROC);
9617 return MATCH_ERROR;
9618 }
9619
9620 return MATCH_YES;
9621
9622cleanup:
9623 gfc_free_formal_arglist (head);
9624 return MATCH_ERROR;
9625}
9626
9627
6de9cd9a
DN
9628/* Match a module procedure statement. Note that we have to modify
9629 symbols in the parent's namespace because the current one was there
49de9e73 9630 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
9631
9632match
9633gfc_match_modproc (void)
9634{
9635 char name[GFC_MAX_SYMBOL_LEN + 1];
9636 gfc_symbol *sym;
9637 match m;
162b5a21 9638 locus old_locus;
060fca4a 9639 gfc_namespace *module_ns;
2b77e908 9640 gfc_interface *old_interface_head, *interface;
6de9cd9a
DN
9641
9642 if (gfc_state_stack->state != COMP_INTERFACE
9643 || gfc_state_stack->previous == NULL
129d15a3
JW
9644 || current_interface.type == INTERFACE_NAMELESS
9645 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 9646 {
636dff67
SK
9647 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9648 "interface");
6de9cd9a
DN
9649 return MATCH_ERROR;
9650 }
9651
060fca4a
PT
9652 module_ns = gfc_current_ns->parent;
9653 for (; module_ns; module_ns = module_ns->parent)
43dfd40c
SK
9654 if (module_ns->proc_name->attr.flavor == FL_MODULE
9655 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9656 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9657 && !module_ns->proc_name->attr.contained))
060fca4a
PT
9658 break;
9659
9660 if (module_ns == NULL)
9661 return MATCH_ERROR;
9662
2b77e908
FXC
9663 /* Store the current state of the interface. We will need it if we
9664 end up with a syntax error and need to recover. */
9665 old_interface_head = gfc_current_interface_head ();
9666
162b5a21
SK
9667 /* Check if the F2008 optional double colon appears. */
9668 gfc_gobble_whitespace ();
9669 old_locus = gfc_current_locus;
9670 if (gfc_match ("::") == MATCH_YES)
9671 {
524af0d6
JB
9672 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9673 "MODULE PROCEDURE statement at %L", &old_locus))
162b5a21
SK
9674 return MATCH_ERROR;
9675 }
9676 else
9677 gfc_current_locus = old_locus;
f5acf0f2 9678
6de9cd9a
DN
9679 for (;;)
9680 {
2b77e908 9681 bool last = false;
162b5a21 9682 old_locus = gfc_current_locus;
2b77e908 9683
6de9cd9a
DN
9684 m = gfc_match_name (name);
9685 if (m == MATCH_NO)
9686 goto syntax;
9687 if (m != MATCH_YES)
9688 return MATCH_ERROR;
9689
2b77e908
FXC
9690 /* Check for syntax error before starting to add symbols to the
9691 current namespace. */
9692 if (gfc_match_eos () == MATCH_YES)
9693 last = true;
162b5a21 9694
2b77e908
FXC
9695 if (!last && gfc_match_char (',') != MATCH_YES)
9696 goto syntax;
9697
9698 /* Now we're sure the syntax is valid, we process this item
9699 further. */
060fca4a 9700 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
9701 return MATCH_ERROR;
9702
43dfd40c
SK
9703 if (sym->attr.intrinsic)
9704 {
9705 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9706 "PROCEDURE", &old_locus);
9707 return MATCH_ERROR;
9708 }
9709
6de9cd9a 9710 if (sym->attr.proc != PROC_MODULE
524af0d6 9711 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
6de9cd9a
DN
9712 return MATCH_ERROR;
9713
524af0d6 9714 if (!gfc_add_interface (sym))
6de9cd9a
DN
9715 return MATCH_ERROR;
9716
71f77fd7 9717 sym->attr.mod_proc = 1;
43dfd40c 9718 sym->declared_at = old_locus;
71f77fd7 9719
2b77e908 9720 if (last)
6de9cd9a 9721 break;
6de9cd9a
DN
9722 }
9723
9724 return MATCH_YES;
9725
9726syntax:
2b77e908
FXC
9727 /* Restore the previous state of the interface. */
9728 interface = gfc_current_interface_head ();
9729 gfc_set_current_interface_head (old_interface_head);
9730
9731 /* Free the new interfaces. */
9732 while (interface != old_interface_head)
9733 {
9734 gfc_interface *i = interface->next;
cede9502 9735 free (interface);
2b77e908
FXC
9736 interface = i;
9737 }
9738
9739 /* And issue a syntax error. */
6de9cd9a
DN
9740 gfc_syntax_error (ST_MODULE_PROC);
9741 return MATCH_ERROR;
9742}
9743
9744
7d1f1e61 9745/* Check a derived type that is being extended. */
42e3d759 9746
7d1f1e61
PT
9747static gfc_symbol*
9748check_extended_derived_type (char *name)
9749{
9750 gfc_symbol *extended;
9751
9752 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9753 {
9754 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9755 return NULL;
9756 }
9757
42e3d759
JW
9758 extended = gfc_find_dt_in_generic (extended);
9759
9760 /* F08:C428. */
7d1f1e61
PT
9761 if (!extended)
9762 {
c4100eae 9763 gfc_error ("Symbol %qs at %C has not been previously defined", name);
7d1f1e61
PT
9764 return NULL;
9765 }
9766
9767 if (extended->attr.flavor != FL_DERIVED)
9768 {
c4100eae 9769 gfc_error ("%qs in EXTENDS expression at %C is not a "
7d1f1e61
PT
9770 "derived type", name);
9771 return NULL;
9772 }
9773
9774 if (extended->attr.is_bind_c)
9775 {
c4100eae 9776 gfc_error ("%qs cannot be extended at %C because it "
7d1f1e61
PT
9777 "is BIND(C)", extended->name);
9778 return NULL;
9779 }
9780
9781 if (extended->attr.sequence)
9782 {
c4100eae 9783 gfc_error ("%qs cannot be extended at %C because it "
7d1f1e61
PT
9784 "is a SEQUENCE type", extended->name);
9785 return NULL;
9786 }
9787
9788 return extended;
9789}
9790
9791
a8b3b0b6
CR
9792/* Match the optional attribute specifiers for a type declaration.
9793 Return MATCH_ERROR if an error is encountered in one of the handled
9794 attributes (public, private, bind(c)), MATCH_NO if what's found is
9795 not a handled attribute, and MATCH_YES otherwise. TODO: More error
9796 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
9797
9798match
7d1f1e61 9799gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6de9cd9a 9800{
a8b3b0b6 9801 /* See if the derived type is marked as private. */
6de9cd9a
DN
9802 if (gfc_match (" , private") == MATCH_YES)
9803 {
d51347f9 9804 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 9805 {
d51347f9
TB
9806 gfc_error ("Derived type at %C can only be PRIVATE in the "
9807 "specification part of a module");
6de9cd9a
DN
9808 return MATCH_ERROR;
9809 }
9810
524af0d6 9811 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
6de9cd9a 9812 return MATCH_ERROR;
6de9cd9a 9813 }
a8b3b0b6 9814 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 9815 {
d51347f9 9816 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 9817 {
d51347f9
TB
9818 gfc_error ("Derived type at %C can only be PUBLIC in the "
9819 "specification part of a module");
6de9cd9a
DN
9820 return MATCH_ERROR;
9821 }
9822
524af0d6 9823 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
6de9cd9a 9824 return MATCH_ERROR;
6de9cd9a 9825 }
52f49934 9826 else if (gfc_match (" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
9827 {
9828 /* If the type is defined to be bind(c) it then needs to make
9829 sure that all fields are interoperable. This will
9830 need to be a semantic check on the finished derived type.
9831 See 15.2.3 (lines 9-12) of F2003 draft. */
524af0d6 9832 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
a8b3b0b6
CR
9833 return MATCH_ERROR;
9834
9835 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
9836 }
52f49934
DK
9837 else if (gfc_match (" , abstract") == MATCH_YES)
9838 {
524af0d6 9839 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
52f49934
DK
9840 return MATCH_ERROR;
9841
524af0d6 9842 if (!gfc_add_abstract (attr, &gfc_current_locus))
52f49934
DK
9843 return MATCH_ERROR;
9844 }
524af0d6 9845 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7d1f1e61 9846 {
524af0d6 9847 if (!gfc_add_extension (attr, &gfc_current_locus))
7d1f1e61
PT
9848 return MATCH_ERROR;
9849 }
a8b3b0b6
CR
9850 else
9851 return MATCH_NO;
9852
9853 /* If we get here, something matched. */
9854 return MATCH_YES;
9855}
9856
9857
f6288c24
FR
9858/* Common function for type declaration blocks similar to derived types, such
9859 as STRUCTURES and MAPs. Unlike derived types, a structure type
9860 does NOT have a generic symbol matching the name given by the user.
9861 STRUCTUREs can share names with variables and PARAMETERs so we must allow
9862 for the creation of an independent symbol.
6442a6f4 9863 Other parameters are a message to prefix errors with, the name of the new
f6288c24
FR
9864 type to be created, and the flavor to add to the resulting symbol. */
9865
9866static bool
9867get_struct_decl (const char *name, sym_flavor fl, locus *decl,
9868 gfc_symbol **result)
9869{
9870 gfc_symbol *sym;
9871 locus where;
9872
9873 gcc_assert (name[0] == (char) TOUPPER (name[0]));
9874
9875 if (decl)
9876 where = *decl;
9877 else
9878 where = gfc_current_locus;
9879
9880 if (gfc_get_symbol (name, NULL, &sym))
9881 return false;
9882
9883 if (!sym)
9884 {
9885 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
9886 return false;
9887 }
9888
9889 if (sym->components != NULL || sym->attr.zero_comp)
9890 {
2f029c08 9891 gfc_error ("Type definition of %qs at %C was already defined at %L",
f6288c24
FR
9892 sym->name, &sym->declared_at);
9893 return false;
9894 }
9895
9896 sym->declared_at = where;
9897
9898 if (sym->attr.flavor != fl
9899 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
9900 return false;
9901
9902 if (!sym->hash_value)
9903 /* Set the hash for the compound name for this type. */
9904 sym->hash_value = gfc_hash_value (sym);
9905
9906 /* Normally the type is expected to have been completely parsed by the time
9907 a field declaration with this type is seen. For unions, maps, and nested
9908 structure declarations, we need to indicate that it is okay that we
9909 haven't seen any components yet. This will be updated after the structure
9910 is fully parsed. */
9911 sym->attr.zero_comp = 0;
9912
9913 /* Structures always act like derived-types with the SEQUENCE attribute */
9914 gfc_add_sequence (&sym->attr, sym->name, NULL);
9915
9916 if (result) *result = sym;
9917
9918 return true;
9919}
9920
9921
9922/* Match the opening of a MAP block. Like a struct within a union in C;
9923 behaves identical to STRUCTURE blocks. */
9924
9925match
9926gfc_match_map (void)
9927{
05b8fcb4
FR
9928 /* Counter used to give unique internal names to map structures. */
9929 static unsigned int gfc_map_id = 0;
9930 char name[GFC_MAX_SYMBOL_LEN + 1];
9931 gfc_symbol *sym;
9932 locus old_loc;
f6288c24 9933
05b8fcb4 9934 old_loc = gfc_current_locus;
f6288c24 9935
05b8fcb4
FR
9936 if (gfc_match_eos () != MATCH_YES)
9937 {
9938 gfc_error ("Junk after MAP statement at %C");
9939 gfc_current_locus = old_loc;
9940 return MATCH_ERROR;
9941 }
f6288c24 9942
05b8fcb4
FR
9943 /* Map blocks are anonymous so we make up unique names for the symbol table
9944 which are invalid Fortran identifiers. */
9945 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
f6288c24 9946
05b8fcb4
FR
9947 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
9948 return MATCH_ERROR;
f6288c24 9949
05b8fcb4 9950 gfc_new_block = sym;
f6288c24 9951
05b8fcb4 9952 return MATCH_YES;
f6288c24
FR
9953}
9954
9955
9956/* Match the opening of a UNION block. */
9957
9958match
9959gfc_match_union (void)
9960{
05b8fcb4
FR
9961 /* Counter used to give unique internal names to union types. */
9962 static unsigned int gfc_union_id = 0;
9963 char name[GFC_MAX_SYMBOL_LEN + 1];
9964 gfc_symbol *sym;
9965 locus old_loc;
f6288c24 9966
05b8fcb4 9967 old_loc = gfc_current_locus;
f6288c24 9968
05b8fcb4
FR
9969 if (gfc_match_eos () != MATCH_YES)
9970 {
9971 gfc_error ("Junk after UNION statement at %C");
9972 gfc_current_locus = old_loc;
9973 return MATCH_ERROR;
9974 }
f6288c24 9975
05b8fcb4
FR
9976 /* Unions are anonymous so we make up unique names for the symbol table
9977 which are invalid Fortran identifiers. */
9978 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
f6288c24 9979
05b8fcb4
FR
9980 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
9981 return MATCH_ERROR;
f6288c24 9982
05b8fcb4 9983 gfc_new_block = sym;
f6288c24 9984
05b8fcb4 9985 return MATCH_YES;
f6288c24
FR
9986}
9987
9988
9989/* Match the beginning of a STRUCTURE declaration. This is similar to
9990 matching the beginning of a derived type declaration with a few
9991 twists. The resulting type symbol has no access control or other
9992 interesting attributes. */
9993
9994match
9995gfc_match_structure_decl (void)
9996{
05b8fcb4
FR
9997 /* Counter used to give unique internal names to anonymous structures. */
9998 static unsigned int gfc_structure_id = 0;
9999 char name[GFC_MAX_SYMBOL_LEN + 1];
10000 gfc_symbol *sym;
10001 match m;
10002 locus where;
f6288c24 10003
05b8fcb4
FR
10004 if (!flag_dec_structure)
10005 {
cf004230
FR
10006 gfc_error ("%s at %C is a DEC extension, enable with "
10007 "%<-fdec-structure%>",
10008 "STRUCTURE");
05b8fcb4
FR
10009 return MATCH_ERROR;
10010 }
f6288c24 10011
05b8fcb4 10012 name[0] = '\0';
f6288c24 10013
05b8fcb4
FR
10014 m = gfc_match (" /%n/", name);
10015 if (m != MATCH_YES)
10016 {
10017 /* Non-nested structure declarations require a structure name. */
10018 if (!gfc_comp_struct (gfc_current_state ()))
10019 {
10020 gfc_error ("Structure name expected in non-nested structure "
10021 "declaration at %C");
10022 return MATCH_ERROR;
10023 }
10024 /* This is an anonymous structure; make up a unique name for it
10025 (upper-case letters never make it to symbol names from the source).
10026 The important thing is initializing the type variable
10027 and setting gfc_new_symbol, which is immediately used by
10028 parse_structure () and variable_decl () to add components of
10029 this type. */
10030 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
10031 }
f6288c24 10032
05b8fcb4
FR
10033 where = gfc_current_locus;
10034 /* No field list allowed after non-nested structure declaration. */
10035 if (!gfc_comp_struct (gfc_current_state ())
10036 && gfc_match_eos () != MATCH_YES)
10037 {
10038 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
10039 return MATCH_ERROR;
10040 }
f6288c24 10041
05b8fcb4
FR
10042 /* Make sure the name is not the name of an intrinsic type. */
10043 if (gfc_is_intrinsic_typename (name))
10044 {
2f029c08 10045 gfc_error ("Structure name %qs at %C cannot be the same as an"
05b8fcb4
FR
10046 " intrinsic type", name);
10047 return MATCH_ERROR;
10048 }
f6288c24 10049
05b8fcb4
FR
10050 /* Store the actual type symbol for the structure with an upper-case first
10051 letter (an invalid Fortran identifier). */
f6288c24 10052
51f03c6b 10053 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
05b8fcb4 10054 return MATCH_ERROR;
f6288c24 10055
05b8fcb4
FR
10056 gfc_new_block = sym;
10057 return MATCH_YES;
f6288c24
FR
10058}
10059
90051c26
FR
10060
10061/* This function does some work to determine which matcher should be used to
58b9de9e 10062 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
90051c26 10063 * as an alias for PRINT from derived type declarations, TYPE IS statements,
58b9de9e 10064 * and [parameterized] derived type declarations. */
90051c26
FR
10065
10066match
10067gfc_match_type (gfc_statement *st)
10068{
10069 char name[GFC_MAX_SYMBOL_LEN + 1];
10070 match m;
10071 locus old_loc;
10072
10073 /* Requires -fdec. */
10074 if (!flag_dec)
10075 return MATCH_NO;
10076
10077 m = gfc_match ("type");
10078 if (m != MATCH_YES)
10079 return m;
10080 /* If we already have an error in the buffer, it is probably from failing to
10081 * match a derived type data declaration. Let it happen. */
10082 else if (gfc_error_flag_test ())
10083 return MATCH_NO;
10084
10085 old_loc = gfc_current_locus;
10086 *st = ST_NONE;
10087
10088 /* If we see an attribute list before anything else it's definitely a derived
10089 * type declaration. */
10090 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
58b9de9e 10091 goto derived;
90051c26
FR
10092
10093 /* By now "TYPE" has already been matched. If we do not see a name, this may
10094 * be something like "TYPE *" or "TYPE <fmt>". */
10095 m = gfc_match_name (name);
10096 if (m != MATCH_YES)
10097 {
10098 /* Let print match if it can, otherwise throw an error from
10099 * gfc_match_derived_decl. */
10100 gfc_current_locus = old_loc;
10101 if (gfc_match_print () == MATCH_YES)
10102 {
10103 *st = ST_WRITE;
10104 return MATCH_YES;
10105 }
58b9de9e 10106 goto derived;
90051c26
FR
10107 }
10108
58b9de9e
FR
10109 /* Check for EOS. */
10110 if (gfc_match_eos () == MATCH_YES)
90051c26
FR
10111 {
10112 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10113 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10114 * Otherwise if gfc_match_derived_decl fails it's probably an existing
10115 * symbol which can be printed. */
10116 gfc_current_locus = old_loc;
10117 m = gfc_match_derived_decl ();
10118 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
10119 {
10120 *st = ST_DERIVED_DECL;
10121 return m;
10122 }
90051c26 10123 }
58b9de9e
FR
10124 else
10125 {
10126 /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
10127 like <type name(parameter)>. */
10128 gfc_gobble_whitespace ();
10129 bool paren = gfc_peek_ascii_char () == '(';
10130 if (paren)
10131 {
10132 if (strcmp ("is", name) == 0)
10133 goto typeis;
10134 else
10135 goto derived;
10136 }
10137 }
10138
10139 /* Treat TYPE... like PRINT... */
10140 gfc_current_locus = old_loc;
10141 *st = ST_WRITE;
10142 return gfc_match_print ();
90051c26 10143
58b9de9e
FR
10144derived:
10145 gfc_current_locus = old_loc;
10146 *st = ST_DERIVED_DECL;
10147 return gfc_match_derived_decl ();
10148
10149typeis:
10150 gfc_current_locus = old_loc;
10151 *st = ST_TYPE_IS;
10152 return gfc_match_type_is ();
90051c26
FR
10153}
10154
10155
a8b3b0b6
CR
10156/* Match the beginning of a derived type declaration. If a type name
10157 was the result of a function, then it is possible to have a symbol
10158 already to be known as a derived type yet have no components. */
10159
10160match
10161gfc_match_derived_decl (void)
10162{
10163 char name[GFC_MAX_SYMBOL_LEN + 1];
7d1f1e61 10164 char parent[GFC_MAX_SYMBOL_LEN + 1];
a8b3b0b6 10165 symbol_attribute attr;
c3f34952 10166 gfc_symbol *sym, *gensym;
7d1f1e61 10167 gfc_symbol *extended;
a8b3b0b6
CR
10168 match m;
10169 match is_type_attr_spec = MATCH_NO;
e7303e85 10170 bool seen_attr = false;
c3f34952 10171 gfc_interface *intr = NULL, *head;
5bab4c96
PT
10172 bool parameterized_type = false;
10173 bool seen_colons = false;
a8b3b0b6 10174
f6288c24 10175 if (gfc_comp_struct (gfc_current_state ()))
a8b3b0b6
CR
10176 return MATCH_NO;
10177
7d1f1e61
PT
10178 name[0] = '\0';
10179 parent[0] = '\0';
a8b3b0b6 10180 gfc_clear_attr (&attr);
7d1f1e61 10181 extended = NULL;
a8b3b0b6
CR
10182
10183 do
10184 {
7d1f1e61 10185 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
a8b3b0b6
CR
10186 if (is_type_attr_spec == MATCH_ERROR)
10187 return MATCH_ERROR;
e7303e85
FXC
10188 if (is_type_attr_spec == MATCH_YES)
10189 seen_attr = true;
a8b3b0b6 10190 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 10191
63a3341a
PT
10192 /* Deal with derived type extensions. The extension attribute has
10193 been added to 'attr' but now the parent type must be found and
10194 checked. */
7d1f1e61
PT
10195 if (parent[0])
10196 extended = check_extended_derived_type (parent);
10197
10198 if (parent[0] && !extended)
10199 return MATCH_ERROR;
10200
5bab4c96
PT
10201 m = gfc_match (" ::");
10202 if (m == MATCH_YES)
10203 {
10204 seen_colons = true;
10205 }
10206 else if (seen_attr)
6de9cd9a
DN
10207 {
10208 gfc_error ("Expected :: in TYPE definition at %C");
10209 return MATCH_ERROR;
10210 }
10211
5bab4c96 10212 m = gfc_match (" %n ", name);
6de9cd9a
DN
10213 if (m != MATCH_YES)
10214 return m;
10215
5bab4c96
PT
10216 /* Make sure that we don't identify TYPE IS (...) as a parameterized
10217 derived type named 'is'.
10218 TODO Expand the check, when 'name' = "is" by matching " (tname) "
10219 and checking if this is a(n intrinsic) typename. his picks up
10220 misplaced TYPE IS statements such as in select_type_1.f03. */
10221 if (gfc_peek_ascii_char () == '(')
10222 {
10223 if (gfc_current_state () == COMP_SELECT_TYPE
10224 || (!seen_colons && !strcmp (name, "is")))
10225 return MATCH_NO;
10226 parameterized_type = true;
10227 }
10228
10229 m = gfc_match_eos ();
10230 if (m != MATCH_YES && !parameterized_type)
10231 return m;
10232
e9c06563
TB
10233 /* Make sure the name is not the name of an intrinsic type. */
10234 if (gfc_is_intrinsic_typename (name))
6de9cd9a 10235 {
c4100eae 10236 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
636dff67 10237 "type", name);
6de9cd9a
DN
10238 return MATCH_ERROR;
10239 }
10240
c3f34952 10241 if (gfc_get_symbol (name, NULL, &gensym))
6de9cd9a
DN
10242 return MATCH_ERROR;
10243
c3f34952 10244 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
6de9cd9a 10245 {
1072bff8
SK
10246 if (gensym->ts.u.derived)
10247 gfc_error ("Derived type name %qs at %C already has a basic type "
10248 "of %s", gensym->name, gfc_typename (&gensym->ts));
10249 else
10250 gfc_error ("Derived type name %qs at %C already has a basic type",
10251 gensym->name);
c3f34952
TB
10252 return MATCH_ERROR;
10253 }
10254
10255 if (!gensym->attr.generic
524af0d6 10256 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
c3f34952
TB
10257 return MATCH_ERROR;
10258
10259 if (!gensym->attr.function
524af0d6 10260 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
c3f34952
TB
10261 return MATCH_ERROR;
10262
e00e1954
SK
10263 if (gensym->attr.dummy)
10264 {
10265 gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
10266 name, &gensym->declared_at);
10267 return MATCH_ERROR;
10268 }
10269
c3f34952
TB
10270 sym = gfc_find_dt_in_generic (gensym);
10271
10272 if (sym && (sym->components != NULL || sym->attr.zero_comp))
10273 {
c4100eae 10274 gfc_error ("Derived type definition of %qs at %C has already been "
c3f34952 10275 "defined", sym->name);
6de9cd9a
DN
10276 return MATCH_ERROR;
10277 }
10278
c3f34952
TB
10279 if (!sym)
10280 {
10281 /* Use upper case to save the actual derived-type symbol. */
f6288c24 10282 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
51f03c6b 10283 sym->name = gfc_get_string ("%s", gensym->name);
c3f34952
TB
10284 head = gensym->generic;
10285 intr = gfc_get_interface ();
10286 intr->sym = sym;
10287 intr->where = gfc_current_locus;
10288 intr->sym->declared_at = gfc_current_locus;
10289 intr->next = head;
10290 gensym->generic = intr;
10291 gensym->attr.if_source = IFSRC_DECL;
10292 }
10293
6de9cd9a
DN
10294 /* The symbol may already have the derived attribute without the
10295 components. The ways this can happen is via a function
10296 definition, an INTRINSIC statement or a subtype in another
10297 derived type that is a pointer. The first part of the AND clause
df2fba9e 10298 is true if the symbol is not the return value of a function. */
6de9cd9a 10299 if (sym->attr.flavor != FL_DERIVED
524af0d6 10300 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
6de9cd9a
DN
10301 return MATCH_ERROR;
10302
6de9cd9a 10303 if (attr.access != ACCESS_UNKNOWN
524af0d6 10304 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
6de9cd9a 10305 return MATCH_ERROR;
c3f34952
TB
10306 else if (sym->attr.access == ACCESS_UNKNOWN
10307 && gensym->attr.access != ACCESS_UNKNOWN
70112e2a 10308 && !gfc_add_access (&sym->attr, gensym->attr.access,
524af0d6 10309 sym->name, NULL))
c3f34952
TB
10310 return MATCH_ERROR;
10311
10312 if (sym->attr.access != ACCESS_UNKNOWN
10313 && gensym->attr.access == ACCESS_UNKNOWN)
10314 gensym->attr.access = sym->attr.access;
6de9cd9a 10315
a8b3b0b6
CR
10316 /* See if the derived type was labeled as bind(c). */
10317 if (attr.is_bind_c != 0)
10318 sym->attr.is_bind_c = attr.is_bind_c;
10319
34523524
DK
10320 /* Construct the f2k_derived namespace if it is not yet there. */
10321 if (!sym->f2k_derived)
10322 sym->f2k_derived = gfc_get_namespace (NULL, 0);
f5acf0f2 10323
5bab4c96
PT
10324 if (parameterized_type)
10325 {
276515e6
PT
10326 /* Ignore error or mismatches by going to the end of the statement
10327 in order to avoid the component declarations causing problems. */
10328 m = gfc_match_formal_arglist (sym, 0, 0, true);
10329 if (m != MATCH_YES)
10330 gfc_error_recovery ();
84083a71
JW
10331 else
10332 sym->attr.pdt_template = 1;
5bab4c96
PT
10333 m = gfc_match_eos ();
10334 if (m != MATCH_YES)
f59986b2
PT
10335 {
10336 gfc_error_recovery ();
10337 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10338 }
5bab4c96
PT
10339 }
10340
7d1f1e61
PT
10341 if (extended && !sym->components)
10342 {
10343 gfc_component *p;
5bab4c96 10344 gfc_formal_arglist *f, *g, *h;
7d1f1e61
PT
10345
10346 /* Add the extended derived type as the first component. */
10347 gfc_add_component (sym, parent, &p);
7d1f1e61
PT
10348 extended->refs++;
10349 gfc_set_sym_referenced (extended);
10350
10351 p->ts.type = BT_DERIVED;
bc21d315 10352 p->ts.u.derived = extended;
7d1f1e61 10353 p->initializer = gfc_default_initializer (&p->ts);
f5acf0f2 10354
7c1dab0d
JW
10355 /* Set extension level. */
10356 if (extended->attr.extension == 255)
10357 {
10358 /* Since the extension field is 8 bit wide, we can only have
10359 up to 255 extension levels. */
c4100eae 10360 gfc_error ("Maximum extension level reached with type %qs at %L",
7c1dab0d
JW
10361 extended->name, &extended->declared_at);
10362 return MATCH_ERROR;
10363 }
10364 sym->attr.extension = extended->attr.extension + 1;
7d1f1e61
PT
10365
10366 /* Provide the links between the extended type and its extension. */
10367 if (!extended->f2k_derived)
10368 extended->f2k_derived = gfc_get_namespace (NULL, 0);
5bab4c96
PT
10369
10370 /* Copy the extended type-param-name-list from the extended type,
10371 append those of the extension and add the whole lot to the
10372 extension. */
10373 if (extended->attr.pdt_template)
10374 {
10375 g = h = NULL;
10376 sym->attr.pdt_template = 1;
10377 for (f = extended->formal; f; f = f->next)
10378 {
10379 if (f == extended->formal)
10380 {
10381 g = gfc_get_formal_arglist ();
10382 h = g;
10383 }
10384 else
10385 {
10386 g->next = gfc_get_formal_arglist ();
10387 g = g->next;
10388 }
10389 g->sym = f->sym;
10390 }
10391 g->next = sym->formal;
10392 sym->formal = h;
10393 }
7d1f1e61
PT
10394 }
10395
7c1dab0d
JW
10396 if (!sym->hash_value)
10397 /* Set the hash for the compound name for this type. */
4fa02692 10398 sym->hash_value = gfc_hash_value (sym);
cf2b3c22 10399
52f49934
DK
10400 /* Take over the ABSTRACT attribute. */
10401 sym->attr.abstract = attr.abstract;
10402
6de9cd9a
DN
10403 gfc_new_block = sym;
10404
10405 return MATCH_YES;
10406}
83d890b9
AL
10407
10408
f5acf0f2 10409/* Cray Pointees can be declared as:
b3aefde2 10410 pointer (ipt, a (n,m,...,*)) */
83d890b9 10411
32e8bb8e 10412match
83d890b9
AL
10413gfc_mod_pointee_as (gfc_array_spec *as)
10414{
10415 as->cray_pointee = true; /* This will be useful to know later. */
10416 if (as->type == AS_ASSUMED_SIZE)
b3aefde2 10417 as->cp_was_assumed = true;
83d890b9
AL
10418 else if (as->type == AS_ASSUMED_SHAPE)
10419 {
10420 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10421 return MATCH_ERROR;
10422 }
10423 return MATCH_YES;
10424}
25d8f0a2
TS
10425
10426
f5acf0f2
PT
10427/* Match the enum definition statement, here we are trying to match
10428 the first line of enum definition statement.
25d8f0a2
TS
10429 Returns MATCH_YES if match is found. */
10430
10431match
10432gfc_match_enum (void)
10433{
10434 match m;
f5acf0f2 10435
25d8f0a2
TS
10436 m = gfc_match_eos ();
10437 if (m != MATCH_YES)
10438 return m;
10439
524af0d6 10440 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
25d8f0a2
TS
10441 return MATCH_ERROR;
10442
10443 return MATCH_YES;
10444}
10445
10446
31224396
SK
10447/* Returns an initializer whose value is one higher than the value of the
10448 LAST_INITIALIZER argument. If the argument is NULL, the
10449 initializers value will be set to zero. The initializer's kind
10450 will be set to gfc_c_int_kind.
10451
10452 If -fshort-enums is given, the appropriate kind will be selected
10453 later after all enumerators have been parsed. A warning is issued
10454 here if an initializer exceeds gfc_c_int_kind. */
10455
10456static gfc_expr *
10457enum_initializer (gfc_expr *last_initializer, locus where)
10458{
10459 gfc_expr *result;
b7e75771 10460 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
31224396
SK
10461
10462 mpz_init (result->value.integer);
10463
10464 if (last_initializer != NULL)
10465 {
10466 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10467 result->where = last_initializer->where;
10468
10469 if (gfc_check_integer_range (result->value.integer,
10470 gfc_c_int_kind) != ARITH_OK)
10471 {
10472 gfc_error ("Enumerator exceeds the C integer type at %C");
10473 return NULL;
10474 }
10475 }
10476 else
10477 {
10478 /* Control comes here, if it's the very first enumerator and no
10479 initializer has been given. It will be initialized to zero. */
10480 mpz_set_si (result->value.integer, 0);
10481 }
10482
10483 return result;
10484}
10485
10486
6133c68a
TS
10487/* Match a variable name with an optional initializer. When this
10488 subroutine is called, a variable is expected to be parsed next.
10489 Depending on what is happening at the moment, updates either the
10490 symbol table or the current interface. */
10491
10492static match
10493enumerator_decl (void)
10494{
10495 char name[GFC_MAX_SYMBOL_LEN + 1];
10496 gfc_expr *initializer;
10497 gfc_array_spec *as = NULL;
10498 gfc_symbol *sym;
10499 locus var_locus;
10500 match m;
524af0d6 10501 bool t;
6133c68a
TS
10502 locus old_locus;
10503
10504 initializer = NULL;
10505 old_locus = gfc_current_locus;
10506
10507 /* When we get here, we've just matched a list of attributes and
10508 maybe a type and a double colon. The next thing we expect to see
10509 is the name of the symbol. */
10510 m = gfc_match_name (name);
10511 if (m != MATCH_YES)
10512 goto cleanup;
10513
10514 var_locus = gfc_current_locus;
10515
10516 /* OK, we've successfully matched the declaration. Now put the
10517 symbol in the current namespace. If we fail to create the symbol,
10518 bail out. */
524af0d6 10519 if (!build_sym (name, NULL, false, &as, &var_locus))
6133c68a
TS
10520 {
10521 m = MATCH_ERROR;
10522 goto cleanup;
10523 }
10524
10525 /* The double colon must be present in order to have initializers.
10526 Otherwise the statement is ambiguous with an assignment statement. */
10527 if (colon_seen)
10528 {
10529 if (gfc_match_char ('=') == MATCH_YES)
10530 {
10531 m = gfc_match_init_expr (&initializer);
10532 if (m == MATCH_NO)
10533 {
10534 gfc_error ("Expected an initialization expression at %C");
10535 m = MATCH_ERROR;
10536 }
10537
10538 if (m != MATCH_YES)
10539 goto cleanup;
10540 }
10541 }
10542
10543 /* If we do not have an initializer, the initialization value of the
10544 previous enumerator (stored in last_initializer) is incremented
10545 by 1 and is used to initialize the current enumerator. */
10546 if (initializer == NULL)
31224396 10547 initializer = enum_initializer (last_initializer, old_locus);
d51347f9 10548
6133c68a
TS
10549 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10550 {
01e64c3d
JJ
10551 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10552 &var_locus);
d51347f9 10553 m = MATCH_ERROR;
6133c68a
TS
10554 goto cleanup;
10555 }
10556
10557 /* Store this current initializer, for the next enumerator variable
10558 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10559 use last_initializer below. */
10560 last_initializer = initializer;
10561 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10562
10563 /* Maintain enumerator history. */
10564 gfc_find_symbol (name, NULL, 0, &sym);
10565 create_enum_history (sym, last_initializer);
10566
524af0d6 10567 return (t) ? MATCH_YES : MATCH_ERROR;
6133c68a
TS
10568
10569cleanup:
10570 /* Free stuff up and return. */
10571 gfc_free_expr (initializer);
10572
10573 return m;
10574}
10575
10576
66e4ab31 10577/* Match the enumerator definition statement. */
25d8f0a2
TS
10578
10579match
10580gfc_match_enumerator_def (void)
10581{
10582 match m;
524af0d6 10583 bool t;
d51347f9 10584
25d8f0a2 10585 gfc_clear_ts (&current_ts);
d51347f9 10586
25d8f0a2
TS
10587 m = gfc_match (" enumerator");
10588 if (m != MATCH_YES)
10589 return m;
6133c68a
TS
10590
10591 m = gfc_match (" :: ");
10592 if (m == MATCH_ERROR)
10593 return m;
10594
10595 colon_seen = (m == MATCH_YES);
d51347f9 10596
25d8f0a2
TS
10597 if (gfc_current_state () != COMP_ENUM)
10598 {
10599 gfc_error ("ENUM definition statement expected before %C");
10600 gfc_free_enum_history ();
10601 return MATCH_ERROR;
10602 }
10603
10604 (&current_ts)->type = BT_INTEGER;
10605 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 10606
6133c68a
TS
10607 gfc_clear_attr (&current_attr);
10608 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
524af0d6 10609 if (!t)
25d8f0a2 10610 {
6133c68a 10611 m = MATCH_ERROR;
25d8f0a2
TS
10612 goto cleanup;
10613 }
10614
25d8f0a2
TS
10615 for (;;)
10616 {
6133c68a 10617 m = enumerator_decl ();
25d8f0a2 10618 if (m == MATCH_ERROR)
01e64c3d
JJ
10619 {
10620 gfc_free_enum_history ();
10621 goto cleanup;
10622 }
25d8f0a2
TS
10623 if (m == MATCH_NO)
10624 break;
10625
10626 if (gfc_match_eos () == MATCH_YES)
10627 goto cleanup;
10628 if (gfc_match_char (',') != MATCH_YES)
10629 break;
10630 }
10631
10632 if (gfc_current_state () == COMP_ENUM)
10633 {
10634 gfc_free_enum_history ();
10635 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10636 m = MATCH_ERROR;
10637 }
10638
10639cleanup:
10640 gfc_free_array_spec (current_as);
10641 current_as = NULL;
10642 return m;
10643
10644}
10645
f6fad28e 10646
30b608eb
DK
10647/* Match binding attributes. */
10648
10649static match
713485cc 10650match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
30b608eb
DK
10651{
10652 bool found_passing = false;
713485cc 10653 bool seen_ptr = false;
90661f26 10654 match m = MATCH_YES;
30b608eb 10655
eea58adb 10656 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
30b608eb
DK
10657 this case the defaults are in there. */
10658 ba->access = ACCESS_UNKNOWN;
10659 ba->pass_arg = NULL;
10660 ba->pass_arg_num = 0;
10661 ba->nopass = 0;
10662 ba->non_overridable = 0;
b0e5fa94 10663 ba->deferred = 0;
90661f26 10664 ba->ppc = ppc;
30b608eb
DK
10665
10666 /* If we find a comma, we believe there are binding attributes. */
90661f26
JW
10667 m = gfc_match_char (',');
10668 if (m == MATCH_NO)
10669 goto done;
30b608eb
DK
10670
10671 do
10672 {
e157f736
DK
10673 /* Access specifier. */
10674
10675 m = gfc_match (" public");
30b608eb
DK
10676 if (m == MATCH_ERROR)
10677 goto error;
10678 if (m == MATCH_YES)
10679 {
e157f736 10680 if (ba->access != ACCESS_UNKNOWN)
30b608eb 10681 {
e157f736 10682 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
10683 goto error;
10684 }
10685
e157f736 10686 ba->access = ACCESS_PUBLIC;
30b608eb
DK
10687 continue;
10688 }
10689
e157f736 10690 m = gfc_match (" private");
30b608eb
DK
10691 if (m == MATCH_ERROR)
10692 goto error;
10693 if (m == MATCH_YES)
10694 {
e157f736 10695 if (ba->access != ACCESS_UNKNOWN)
30b608eb 10696 {
e157f736 10697 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
10698 goto error;
10699 }
10700
e157f736 10701 ba->access = ACCESS_PRIVATE;
30b608eb
DK
10702 continue;
10703 }
10704
e157f736
DK
10705 /* If inside GENERIC, the following is not allowed. */
10706 if (!generic)
30b608eb 10707 {
30b608eb 10708
e157f736
DK
10709 /* NOPASS flag. */
10710 m = gfc_match (" nopass");
10711 if (m == MATCH_ERROR)
10712 goto error;
10713 if (m == MATCH_YES)
30b608eb 10714 {
e157f736
DK
10715 if (found_passing)
10716 {
10717 gfc_error ("Binding attributes already specify passing,"
10718 " illegal NOPASS at %C");
10719 goto error;
10720 }
10721
10722 found_passing = true;
10723 ba->nopass = 1;
10724 continue;
30b608eb
DK
10725 }
10726
e157f736
DK
10727 /* PASS possibly including argument. */
10728 m = gfc_match (" pass");
10729 if (m == MATCH_ERROR)
10730 goto error;
10731 if (m == MATCH_YES)
30b608eb 10732 {
e157f736
DK
10733 char arg[GFC_MAX_SYMBOL_LEN + 1];
10734
10735 if (found_passing)
10736 {
10737 gfc_error ("Binding attributes already specify passing,"
10738 " illegal PASS at %C");
10739 goto error;
10740 }
10741
10742 m = gfc_match (" ( %n )", arg);
10743 if (m == MATCH_ERROR)
10744 goto error;
10745 if (m == MATCH_YES)
51f03c6b 10746 ba->pass_arg = gfc_get_string ("%s", arg);
e157f736
DK
10747 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10748
10749 found_passing = true;
10750 ba->nopass = 0;
10751 continue;
30b608eb
DK
10752 }
10753
713485cc
JW
10754 if (ppc)
10755 {
10756 /* POINTER flag. */
10757 m = gfc_match (" pointer");
10758 if (m == MATCH_ERROR)
10759 goto error;
10760 if (m == MATCH_YES)
10761 {
10762 if (seen_ptr)
10763 {
10764 gfc_error ("Duplicate POINTER attribute at %C");
10765 goto error;
10766 }
10767
10768 seen_ptr = true;
713485cc
JW
10769 continue;
10770 }
10771 }
10772 else
10773 {
10774 /* NON_OVERRIDABLE flag. */
10775 m = gfc_match (" non_overridable");
10776 if (m == MATCH_ERROR)
10777 goto error;
10778 if (m == MATCH_YES)
10779 {
10780 if (ba->non_overridable)
10781 {
10782 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
10783 goto error;
10784 }
10785
10786 ba->non_overridable = 1;
10787 continue;
10788 }
10789
10790 /* DEFERRED flag. */
10791 m = gfc_match (" deferred");
10792 if (m == MATCH_ERROR)
10793 goto error;
10794 if (m == MATCH_YES)
10795 {
10796 if (ba->deferred)
10797 {
10798 gfc_error ("Duplicate DEFERRED at %C");
10799 goto error;
10800 }
10801
10802 ba->deferred = 1;
10803 continue;
10804 }
10805 }
10806
30b608eb
DK
10807 }
10808
10809 /* Nothing matching found. */
e157f736
DK
10810 if (generic)
10811 gfc_error ("Expected access-specifier at %C");
10812 else
10813 gfc_error ("Expected binding attribute at %C");
30b608eb
DK
10814 goto error;
10815 }
10816 while (gfc_match_char (',') == MATCH_YES);
10817
b0e5fa94
DK
10818 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
10819 if (ba->non_overridable && ba->deferred)
10820 {
1fe61adf 10821 gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
b0e5fa94
DK
10822 goto error;
10823 }
10824
90661f26
JW
10825 m = MATCH_YES;
10826
10827done:
e157f736 10828 if (ba->access == ACCESS_UNKNOWN)
d4beaf2a
JW
10829 ba->access = ppc ? gfc_current_block()->component_access
10830 : gfc_typebound_default_access;
e157f736 10831
713485cc
JW
10832 if (ppc && !seen_ptr)
10833 {
10834 gfc_error ("POINTER attribute is required for procedure pointer component"
10835 " at %C");
10836 goto error;
10837 }
10838
90661f26 10839 return m;
30b608eb
DK
10840
10841error:
30b608eb
DK
10842 return MATCH_ERROR;
10843}
10844
10845
10846/* Match a PROCEDURE specific binding inside a derived type. */
10847
10848static match
10849match_procedure_in_type (void)
10850{
10851 char name[GFC_MAX_SYMBOL_LEN + 1];
10852 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
1be17993 10853 char* target = NULL, *ifc = NULL;
3e15518b 10854 gfc_typebound_proc tb;
30b608eb
DK
10855 bool seen_colons;
10856 bool seen_attrs;
10857 match m;
10858 gfc_symtree* stree;
10859 gfc_namespace* ns;
10860 gfc_symbol* block;
1be17993 10861 int num;
30b608eb
DK
10862
10863 /* Check current state. */
10864 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
10865 block = gfc_state_stack->previous->sym;
10866 gcc_assert (block);
10867
b0e5fa94 10868 /* Try to match PROCEDURE(interface). */
30b608eb
DK
10869 if (gfc_match (" (") == MATCH_YES)
10870 {
b0e5fa94
DK
10871 m = gfc_match_name (target_buf);
10872 if (m == MATCH_ERROR)
10873 return m;
10874 if (m != MATCH_YES)
10875 {
a4d9b221 10876 gfc_error ("Interface-name expected after %<(%> at %C");
b0e5fa94
DK
10877 return MATCH_ERROR;
10878 }
10879
10880 if (gfc_match (" )") != MATCH_YES)
10881 {
a4d9b221 10882 gfc_error ("%<)%> expected at %C");
b0e5fa94
DK
10883 return MATCH_ERROR;
10884 }
10885
1be17993 10886 ifc = target_buf;
30b608eb
DK
10887 }
10888
10889 /* Construct the data structure. */
ff5b6492 10890 memset (&tb, 0, sizeof (tb));
3e15518b 10891 tb.where = gfc_current_locus;
30b608eb
DK
10892
10893 /* Match binding attributes. */
3e15518b 10894 m = match_binding_attributes (&tb, false, false);
30b608eb
DK
10895 if (m == MATCH_ERROR)
10896 return m;
10897 seen_attrs = (m == MATCH_YES);
10898
1be17993 10899 /* Check that attribute DEFERRED is given if an interface is specified. */
3e15518b 10900 if (tb.deferred && !ifc)
b0e5fa94
DK
10901 {
10902 gfc_error ("Interface must be specified for DEFERRED binding at %C");
10903 return MATCH_ERROR;
10904 }
3e15518b 10905 if (ifc && !tb.deferred)
b0e5fa94
DK
10906 {
10907 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
10908 return MATCH_ERROR;
10909 }
10910
30b608eb
DK
10911 /* Match the colons. */
10912 m = gfc_match (" ::");
10913 if (m == MATCH_ERROR)
10914 return m;
10915 seen_colons = (m == MATCH_YES);
10916 if (seen_attrs && !seen_colons)
10917 {
a4d9b221 10918 gfc_error ("Expected %<::%> after binding-attributes at %C");
30b608eb
DK
10919 return MATCH_ERROR;
10920 }
10921
f5acf0f2 10922 /* Match the binding names. */
1be17993 10923 for(num=1;;num++)
30b608eb 10924 {
1be17993
JW
10925 m = gfc_match_name (name);
10926 if (m == MATCH_ERROR)
10927 return m;
10928 if (m == MATCH_NO)
b0e5fa94 10929 {
1be17993 10930 gfc_error ("Expected binding name at %C");
b0e5fa94
DK
10931 return MATCH_ERROR;
10932 }
10933
524af0d6 10934 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
1be17993 10935 return MATCH_ERROR;
30b608eb 10936
1be17993
JW
10937 /* Try to match the '=> target', if it's there. */
10938 target = ifc;
10939 m = gfc_match (" =>");
30b608eb
DK
10940 if (m == MATCH_ERROR)
10941 return m;
1be17993 10942 if (m == MATCH_YES)
30b608eb 10943 {
3e15518b 10944 if (tb.deferred)
1be17993 10945 {
a4d9b221 10946 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
1be17993
JW
10947 return MATCH_ERROR;
10948 }
10949
10950 if (!seen_colons)
10951 {
a4d9b221 10952 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
1be17993
JW
10953 " at %C");
10954 return MATCH_ERROR;
10955 }
10956
10957 m = gfc_match_name (target_buf);
10958 if (m == MATCH_ERROR)
10959 return m;
10960 if (m == MATCH_NO)
10961 {
a4d9b221 10962 gfc_error ("Expected binding target after %<=>%> at %C");
1be17993
JW
10963 return MATCH_ERROR;
10964 }
10965 target = target_buf;
30b608eb 10966 }
30b608eb 10967
1be17993
JW
10968 /* If no target was found, it has the same name as the binding. */
10969 if (!target)
10970 target = name;
30b608eb 10971
1be17993
JW
10972 /* Get the namespace to insert the symbols into. */
10973 ns = block->f2k_derived;
10974 gcc_assert (ns);
30b608eb 10975
1be17993 10976 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
3e15518b 10977 if (tb.deferred && !block->attr.abstract)
1be17993 10978 {
c4100eae 10979 gfc_error ("Type %qs containing DEFERRED binding at %C "
1be17993
JW
10980 "is not ABSTRACT", block->name);
10981 return MATCH_ERROR;
10982 }
30b608eb 10983
1be17993 10984 /* See if we already have a binding with this name in the symtree which
6bd2c800 10985 would be an error. If a GENERIC already targeted this binding, it may
1be17993
JW
10986 be already there but then typebound is still NULL. */
10987 stree = gfc_find_symtree (ns->tb_sym_root, name);
9f23af48 10988 if (stree && stree->n.tb)
1be17993 10989 {
c4100eae
MLI
10990 gfc_error ("There is already a procedure with binding name %qs for "
10991 "the derived type %qs at %C", name, block->name);
1be17993
JW
10992 return MATCH_ERROR;
10993 }
b0e5fa94 10994
1be17993 10995 /* Insert it and set attributes. */
30b608eb 10996
9f23af48
MM
10997 if (!stree)
10998 {
10999 stree = gfc_new_symtree (&ns->tb_sym_root, name);
11000 gcc_assert (stree);
11001 }
3e15518b 11002 stree->n.tb = gfc_get_typebound_proc (&tb);
e34ccb4c 11003
3e15518b
JW
11004 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
11005 false))
1be17993 11006 return MATCH_ERROR;
3e15518b 11007 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
f9d49cd1
JW
11008 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
11009 target, &stree->n.tb->u.specific->n.sym->declared_at);
f5acf0f2 11010
1be17993
JW
11011 if (gfc_match_eos () == MATCH_YES)
11012 return MATCH_YES;
11013 if (gfc_match_char (',') != MATCH_YES)
11014 goto syntax;
e34ccb4c 11015 }
30b608eb 11016
1be17993
JW
11017syntax:
11018 gfc_error ("Syntax error in PROCEDURE statement at %C");
11019 return MATCH_ERROR;
30b608eb
DK
11020}
11021
11022
e157f736
DK
11023/* Match a GENERIC procedure binding inside a derived type. */
11024
11025match
11026gfc_match_generic (void)
11027{
11028 char name[GFC_MAX_SYMBOL_LEN + 1];
94747289 11029 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
e157f736
DK
11030 gfc_symbol* block;
11031 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
11032 gfc_typebound_proc* tb;
e157f736 11033 gfc_namespace* ns;
94747289
DK
11034 interface_type op_type;
11035 gfc_intrinsic_op op;
e157f736
DK
11036 match m;
11037
11038 /* Check current state. */
11039 if (gfc_current_state () == COMP_DERIVED)
11040 {
11041 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
11042 return MATCH_ERROR;
11043 }
11044 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
11045 return MATCH_NO;
11046 block = gfc_state_stack->previous->sym;
11047 ns = block->f2k_derived;
11048 gcc_assert (block && ns);
11049
ff5b6492
MM
11050 memset (&tbattr, 0, sizeof (tbattr));
11051 tbattr.where = gfc_current_locus;
11052
e157f736 11053 /* See if we get an access-specifier. */
713485cc 11054 m = match_binding_attributes (&tbattr, true, false);
e157f736
DK
11055 if (m == MATCH_ERROR)
11056 goto error;
11057
11058 /* Now the colons, those are required. */
11059 if (gfc_match (" ::") != MATCH_YES)
11060 {
a4d9b221 11061 gfc_error ("Expected %<::%> at %C");
e157f736
DK
11062 goto error;
11063 }
11064
94747289
DK
11065 /* Match the binding name; depending on type (operator / generic) format
11066 it for future error messages into bind_name. */
f5acf0f2 11067
94747289 11068 m = gfc_match_generic_spec (&op_type, name, &op);
e157f736
DK
11069 if (m == MATCH_ERROR)
11070 return MATCH_ERROR;
11071 if (m == MATCH_NO)
11072 {
94747289 11073 gfc_error ("Expected generic name or operator descriptor at %C");
e157f736
DK
11074 goto error;
11075 }
11076
94747289 11077 switch (op_type)
e157f736 11078 {
94747289 11079 case INTERFACE_GENERIC:
e73d3ca6 11080 case INTERFACE_DTIO:
94747289
DK
11081 snprintf (bind_name, sizeof (bind_name), "%s", name);
11082 break;
f5acf0f2 11083
94747289
DK
11084 case INTERFACE_USER_OP:
11085 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
11086 break;
f5acf0f2 11087
94747289
DK
11088 case INTERFACE_INTRINSIC_OP:
11089 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
11090 gfc_op2string (op));
11091 break;
11092
377e37c1
SK
11093 case INTERFACE_NAMELESS:
11094 gfc_error ("Malformed GENERIC statement at %C");
11095 goto error;
11096 break;
11097
94747289
DK
11098 default:
11099 gcc_unreachable ();
11100 }
e34ccb4c 11101
94747289
DK
11102 /* Match the required =>. */
11103 if (gfc_match (" =>") != MATCH_YES)
11104 {
a4d9b221 11105 gfc_error ("Expected %<=>%> at %C");
94747289
DK
11106 goto error;
11107 }
f5acf0f2 11108
94747289
DK
11109 /* Try to find existing GENERIC binding with this name / for this operator;
11110 if there is something, check that it is another GENERIC and then extend
11111 it rather than building a new node. Otherwise, create it and put it
11112 at the right position. */
11113
11114 switch (op_type)
11115 {
e73d3ca6 11116 case INTERFACE_DTIO:
94747289
DK
11117 case INTERFACE_USER_OP:
11118 case INTERFACE_GENERIC:
11119 {
11120 const bool is_op = (op_type == INTERFACE_USER_OP);
11121 gfc_symtree* st;
11122
11123 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
b93d8a3f 11124 tb = st ? st->n.tb : NULL;
94747289
DK
11125 break;
11126 }
11127
11128 case INTERFACE_INTRINSIC_OP:
11129 tb = ns->tb_op[op];
11130 break;
11131
11132 default:
11133 gcc_unreachable ();
11134 }
11135
11136 if (tb)
11137 {
e34ccb4c 11138 if (!tb->is_generic)
e157f736 11139 {
94747289 11140 gcc_assert (op_type == INTERFACE_GENERIC);
e157f736 11141 gfc_error ("There's already a non-generic procedure with binding name"
c4100eae 11142 " %qs for the derived type %qs at %C",
94747289 11143 bind_name, block->name);
e157f736
DK
11144 goto error;
11145 }
11146
e157f736
DK
11147 if (tb->access != tbattr.access)
11148 {
11149 gfc_error ("Binding at %C must have the same access as already"
c4100eae 11150 " defined binding %qs", bind_name);
e157f736
DK
11151 goto error;
11152 }
11153 }
11154 else
11155 {
3e15518b 11156 tb = gfc_get_typebound_proc (NULL);
e157f736
DK
11157 tb->where = gfc_current_locus;
11158 tb->access = tbattr.access;
11159 tb->is_generic = 1;
11160 tb->u.generic = NULL;
94747289
DK
11161
11162 switch (op_type)
11163 {
e73d3ca6 11164 case INTERFACE_DTIO:
94747289
DK
11165 case INTERFACE_GENERIC:
11166 case INTERFACE_USER_OP:
11167 {
11168 const bool is_op = (op_type == INTERFACE_USER_OP);
b93d8a3f
JW
11169 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
11170 &ns->tb_sym_root, name);
94747289
DK
11171 gcc_assert (st);
11172 st->n.tb = tb;
11173
11174 break;
11175 }
f5acf0f2 11176
94747289
DK
11177 case INTERFACE_INTRINSIC_OP:
11178 ns->tb_op[op] = tb;
11179 break;
11180
11181 default:
11182 gcc_unreachable ();
11183 }
e157f736
DK
11184 }
11185
11186 /* Now, match all following names as specific targets. */
11187 do
11188 {
11189 gfc_symtree* target_st;
11190 gfc_tbp_generic* target;
11191
11192 m = gfc_match_name (name);
11193 if (m == MATCH_ERROR)
11194 goto error;
11195 if (m == MATCH_NO)
11196 {
11197 gfc_error ("Expected specific binding name at %C");
11198 goto error;
11199 }
11200
e34ccb4c 11201 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
e157f736
DK
11202
11203 /* See if this is a duplicate specification. */
11204 for (target = tb->u.generic; target; target = target->next)
11205 if (target_st == target->specific_st)
11206 {
c4100eae
MLI
11207 gfc_error ("%qs already defined as specific binding for the"
11208 " generic %qs at %C", name, bind_name);
e157f736
DK
11209 goto error;
11210 }
11211
e157f736
DK
11212 target = gfc_get_tbp_generic ();
11213 target->specific_st = target_st;
11214 target->specific = NULL;
11215 target->next = tb->u.generic;
218e1228
TB
11216 target->is_operator = ((op_type == INTERFACE_USER_OP)
11217 || (op_type == INTERFACE_INTRINSIC_OP));
e157f736
DK
11218 tb->u.generic = target;
11219 }
11220 while (gfc_match (" ,") == MATCH_YES);
11221
11222 /* Here should be the end. */
11223 if (gfc_match_eos () != MATCH_YES)
11224 {
11225 gfc_error ("Junk after GENERIC binding at %C");
11226 goto error;
11227 }
11228
11229 return MATCH_YES;
11230
11231error:
11232 return MATCH_ERROR;
11233}
11234
11235
34523524
DK
11236/* Match a FINAL declaration inside a derived type. */
11237
11238match
11239gfc_match_final_decl (void)
11240{
11241 char name[GFC_MAX_SYMBOL_LEN + 1];
11242 gfc_symbol* sym;
11243 match m;
11244 gfc_namespace* module_ns;
11245 bool first, last;
30b608eb 11246 gfc_symbol* block;
34523524 11247
33344e0f
JW
11248 if (gfc_current_form == FORM_FREE)
11249 {
11250 char c = gfc_peek_ascii_char ();
11251 if (!gfc_is_whitespace (c) && c != ':')
11252 return MATCH_NO;
11253 }
f5acf0f2 11254
30b608eb 11255 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
34523524 11256 {
33344e0f
JW
11257 if (gfc_current_form == FORM_FIXED)
11258 return MATCH_NO;
11259
34523524 11260 gfc_error ("FINAL declaration at %C must be inside a derived type "
30b608eb 11261 "CONTAINS section");
34523524
DK
11262 return MATCH_ERROR;
11263 }
11264
30b608eb
DK
11265 block = gfc_state_stack->previous->sym;
11266 gcc_assert (block);
34523524 11267
30b608eb
DK
11268 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11269 || gfc_state_stack->previous->previous->state != COMP_MODULE)
34523524
DK
11270 {
11271 gfc_error ("Derived type declaration with FINAL at %C must be in the"
11272 " specification part of a MODULE");
11273 return MATCH_ERROR;
11274 }
11275
11276 module_ns = gfc_current_ns;
11277 gcc_assert (module_ns);
11278 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11279
11280 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
11281 if (gfc_match (" ::") == MATCH_ERROR)
11282 return MATCH_ERROR;
11283
11284 /* Match the sequence of procedure names. */
11285 first = true;
11286 last = false;
11287 do
11288 {
11289 gfc_finalizer* f;
11290
11291 if (first && gfc_match_eos () == MATCH_YES)
11292 {
11293 gfc_error ("Empty FINAL at %C");
11294 return MATCH_ERROR;
11295 }
11296
11297 m = gfc_match_name (name);
11298 if (m == MATCH_NO)
11299 {
11300 gfc_error ("Expected module procedure name at %C");
11301 return MATCH_ERROR;
11302 }
11303 else if (m != MATCH_YES)
11304 return MATCH_ERROR;
11305
11306 if (gfc_match_eos () == MATCH_YES)
11307 last = true;
11308 if (!last && gfc_match_char (',') != MATCH_YES)
11309 {
a4d9b221 11310 gfc_error ("Expected %<,%> at %C");
34523524
DK
11311 return MATCH_ERROR;
11312 }
11313
11314 if (gfc_get_symbol (name, module_ns, &sym))
11315 {
c4100eae 11316 gfc_error ("Unknown procedure name %qs at %C", name);
34523524
DK
11317 return MATCH_ERROR;
11318 }
11319
11320 /* Mark the symbol as module procedure. */
11321 if (sym->attr.proc != PROC_MODULE
524af0d6 11322 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
34523524
DK
11323 return MATCH_ERROR;
11324
11325 /* Check if we already have this symbol in the list, this is an error. */
30b608eb 11326 for (f = block->f2k_derived->finalizers; f; f = f->next)
f6fad28e 11327 if (f->proc_sym == sym)
34523524 11328 {
546c8974 11329 gfc_error ("%qs at %C is already defined as FINAL procedure",
34523524
DK
11330 name);
11331 return MATCH_ERROR;
11332 }
11333
11334 /* Add this symbol to the list of finalizers. */
30b608eb 11335 gcc_assert (block->f2k_derived);
2050626a 11336 sym->refs++;
ece3f663 11337 f = XCNEW (gfc_finalizer);
f6fad28e
DK
11338 f->proc_sym = sym;
11339 f->proc_tree = NULL;
34523524 11340 f->where = gfc_current_locus;
30b608eb
DK
11341 f->next = block->f2k_derived->finalizers;
11342 block->f2k_derived->finalizers = f;
34523524
DK
11343
11344 first = false;
11345 }
11346 while (!last);
11347
11348 return MATCH_YES;
11349}
08a6b8e0
TB
11350
11351
11352const ext_attr_t ext_attr_list[] = {
e7ac6a7c
TB
11353 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11354 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11355 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11356 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11357 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11358 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11359 { NULL, EXT_ATTR_LAST, NULL }
08a6b8e0
TB
11360};
11361
11362/* Match a !GCC$ ATTRIBUTES statement of the form:
11363 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11364 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11365
11366 TODO: We should support all GCC attributes using the same syntax for
11367 the attribute list, i.e. the list in C
11368 __attributes(( attribute-list ))
11369 matches then
11370 !GCC$ ATTRIBUTES attribute-list ::
11371 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11372 saved into a TREE.
11373
11374 As there is absolutely no risk of confusion, we should never return
11375 MATCH_NO. */
11376match
11377gfc_match_gcc_attributes (void)
f5acf0f2 11378{
08a6b8e0
TB
11379 symbol_attribute attr;
11380 char name[GFC_MAX_SYMBOL_LEN + 1];
11381 unsigned id;
11382 gfc_symbol *sym;
11383 match m;
11384
11385 gfc_clear_attr (&attr);
11386 for(;;)
11387 {
11388 char ch;
11389
11390 if (gfc_match_name (name) != MATCH_YES)
11391 return MATCH_ERROR;
11392
11393 for (id = 0; id < EXT_ATTR_LAST; id++)
11394 if (strcmp (name, ext_attr_list[id].name) == 0)
11395 break;
11396
11397 if (id == EXT_ATTR_LAST)
11398 {
11399 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11400 return MATCH_ERROR;
11401 }
11402
524af0d6 11403 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
08a6b8e0
TB
11404 return MATCH_ERROR;
11405
11406 gfc_gobble_whitespace ();
11407 ch = gfc_next_ascii_char ();
11408 if (ch == ':')
11409 {
11410 /* This is the successful exit condition for the loop. */
11411 if (gfc_next_ascii_char () == ':')
11412 break;
11413 }
11414
11415 if (ch == ',')
11416 continue;
11417
11418 goto syntax;
11419 }
11420
11421 if (gfc_match_eos () == MATCH_YES)
11422 goto syntax;
11423
11424 for(;;)
11425 {
11426 m = gfc_match_name (name);
11427 if (m != MATCH_YES)
11428 return m;
11429
11430 if (find_special (name, &sym, true))
11431 return MATCH_ERROR;
f5acf0f2 11432
08a6b8e0
TB
11433 sym->attr.ext_attr |= attr.ext_attr;
11434
11435 if (gfc_match_eos () == MATCH_YES)
11436 break;
11437
11438 if (gfc_match_char (',') != MATCH_YES)
11439 goto syntax;
11440 }
11441
11442 return MATCH_YES;
11443
11444syntax:
11445 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11446 return MATCH_ERROR;
11447}
170a8bd6
EB
11448
11449
11450/* Match a !GCC$ UNROLL statement of the form:
11451 !GCC$ UNROLL n
11452
11453 The parameter n is the number of times we are supposed to unroll.
11454
11455 When we come here, we have already matched the !GCC$ UNROLL string. */
11456match
11457gfc_match_gcc_unroll (void)
11458{
11459 int value;
11460
11461 if (gfc_match_small_int (&value) == MATCH_YES)
11462 {
11463 if (value < 0 || value > USHRT_MAX)
11464 {
11465 gfc_error ("%<GCC unroll%> directive requires a"
11466 " non-negative integral constant"
11467 " less than or equal to %u at %C",
11468 USHRT_MAX
11469 );
11470 return MATCH_ERROR;
11471 }
11472 if (gfc_match_eos () == MATCH_YES)
11473 {
11474 directive_unroll = value == 0 ? 1 : value;
11475 return MATCH_YES;
11476 }
11477 }
11478
11479 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11480 return MATCH_ERROR;
11481}
facf0354 11482
e8cecccc 11483/* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
facf0354
ML
11484
11485 The parameter b is name of a middle-end built-in.
e8cecccc
ML
11486 FLAGS is optional and must be one of:
11487 - (inbranch)
11488 - (notinbranch)
11489
11490 IF('target') is optional and TARGET is a name of a multilib ABI.
facf0354
ML
11491
11492 When we come here, we have already matched the !GCC$ builtin string. */
e8cecccc 11493
facf0354
ML
11494match
11495gfc_match_gcc_builtin (void)
11496{
11497 char builtin[GFC_MAX_SYMBOL_LEN + 1];
e8cecccc 11498 char target[GFC_MAX_SYMBOL_LEN + 1];
facf0354
ML
11499
11500 if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11501 return MATCH_ERROR;
11502
11503 gfc_simd_clause clause = SIMD_NONE;
11504 if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11505 clause = SIMD_NOTINBRANCH;
11506 else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11507 clause = SIMD_INBRANCH;
11508
e8cecccc
ML
11509 if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
11510 {
11511 const char *abi = targetm.get_multilib_abi_name ();
11512 if (abi == NULL || strcmp (abi, target) != 0)
11513 return MATCH_YES;
11514 }
11515
facf0354
ML
11516 if (gfc_vectorized_builtins == NULL)
11517 gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11518
11519 char *r = XNEWVEC (char, strlen (builtin) + 32);
11520 sprintf (r, "__builtin_%s", builtin);
11521
11522 bool existed;
11523 int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11524 value |= clause;
11525 if (existed)
11526 free (r);
11527
11528 return MATCH_YES;
11529}