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