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