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