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