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