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