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