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