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