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