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