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