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