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