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