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