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