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