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