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