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