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