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