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