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