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