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