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