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