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