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