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