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