]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/decl.c
re PR fortran/48858 (Incorrect error for same binding label on two generic interface...
[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
f11de7c5 5357add_global_entry (const char *name, const char *binding_label, bool sub)
68ea355b
PT
5358{
5359 gfc_gsymbol *s;
32e8bb8e 5360 enum gfc_symbol_type type;
68ea355b 5361
7389bce6 5362 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
68ea355b 5363
f11de7c5
TB
5364 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5365 name is a global identifier. */
5366 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
68ea355b 5367 {
f11de7c5
TB
5368 s = gfc_get_gsymbol (name);
5369
5370 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5371 {
5372 gfc_global_used(s, NULL);
5373 return false;
5374 }
5375 else
5376 {
5377 s->type = type;
5378 s->where = gfc_current_locus;
5379 s->defined = 1;
5380 s->ns = gfc_current_ns;
5381 }
68ea355b 5382 }
f11de7c5
TB
5383
5384 /* Don't add the symbol multiple times. */
5385 if (binding_label
5386 && (!gfc_notification_std (GFC_STD_F2008)
5387 || strcmp (name, binding_label) != 0))
5388 {
5389 s = gfc_get_gsymbol (binding_label);
5390
5391 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5392 {
5393 gfc_global_used(s, NULL);
5394 return false;
5395 }
5396 else
5397 {
5398 s->type = type;
5399 s->binding_label = binding_label;
5400 s->where = gfc_current_locus;
5401 s->defined = 1;
5402 s->ns = gfc_current_ns;
5403 }
5404 }
5405
5406 return true;
68ea355b 5407}
6de9cd9a 5408
636dff67 5409
6de9cd9a
DN
5410/* Match an ENTRY statement. */
5411
5412match
5413gfc_match_entry (void)
5414{
3d79abbd
PB
5415 gfc_symbol *proc;
5416 gfc_symbol *result;
5417 gfc_symbol *entry;
6de9cd9a
DN
5418 char name[GFC_MAX_SYMBOL_LEN + 1];
5419 gfc_compile_state state;
5420 match m;
3d79abbd 5421 gfc_entry_list *el;
c96cfa49 5422 locus old_loc;
1a492601 5423 bool module_procedure;
bc3e7a8c
TB
5424 char peek_char;
5425 match is_bind_c;
6de9cd9a
DN
5426
5427 m = gfc_match_name (name);
5428 if (m != MATCH_YES)
5429 return m;
5430
524af0d6 5431 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
58fc89f6
TB
5432 return MATCH_ERROR;
5433
3d79abbd 5434 state = gfc_current_state ();
4c93c95a 5435 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 5436 {
4c93c95a
FXC
5437 switch (state)
5438 {
5439 case COMP_PROGRAM:
5440 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5441 break;
5442 case COMP_MODULE:
5443 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5444 break;
5445 case COMP_BLOCK_DATA:
636dff67
SK
5446 gfc_error ("ENTRY statement at %C cannot appear within "
5447 "a BLOCK DATA");
4c93c95a
FXC
5448 break;
5449 case COMP_INTERFACE:
636dff67
SK
5450 gfc_error ("ENTRY statement at %C cannot appear within "
5451 "an INTERFACE");
4c93c95a
FXC
5452 break;
5453 case COMP_DERIVED:
636dff67
SK
5454 gfc_error ("ENTRY statement at %C cannot appear within "
5455 "a DERIVED TYPE block");
4c93c95a
FXC
5456 break;
5457 case COMP_IF:
636dff67
SK
5458 gfc_error ("ENTRY statement at %C cannot appear within "
5459 "an IF-THEN block");
4c93c95a
FXC
5460 break;
5461 case COMP_DO:
8c6a85e3 5462 case COMP_DO_CONCURRENT:
636dff67
SK
5463 gfc_error ("ENTRY statement at %C cannot appear within "
5464 "a DO block");
4c93c95a
FXC
5465 break;
5466 case COMP_SELECT:
636dff67
SK
5467 gfc_error ("ENTRY statement at %C cannot appear within "
5468 "a SELECT block");
4c93c95a
FXC
5469 break;
5470 case COMP_FORALL:
636dff67
SK
5471 gfc_error ("ENTRY statement at %C cannot appear within "
5472 "a FORALL block");
4c93c95a
FXC
5473 break;
5474 case COMP_WHERE:
636dff67
SK
5475 gfc_error ("ENTRY statement at %C cannot appear within "
5476 "a WHERE block");
4c93c95a
FXC
5477 break;
5478 case COMP_CONTAINS:
636dff67
SK
5479 gfc_error ("ENTRY statement at %C cannot appear within "
5480 "a contained subprogram");
4c93c95a
FXC
5481 break;
5482 default:
5483 gfc_internal_error ("gfc_match_entry(): Bad state");
5484 }
3d79abbd
PB
5485 return MATCH_ERROR;
5486 }
5487
1a492601 5488 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
5489 && gfc_current_ns->parent->proc_name
5490 && gfc_current_ns->parent->proc_name->attr.flavor
5491 == FL_MODULE;
1a492601 5492
3d79abbd
PB
5493 if (gfc_current_ns->parent != NULL
5494 && gfc_current_ns->parent->proc_name
1a492601 5495 && !module_procedure)
3d79abbd
PB
5496 {
5497 gfc_error("ENTRY statement at %C cannot appear in a "
5498 "contained procedure");
5499 return MATCH_ERROR;
5500 }
5501
1a492601
PT
5502 /* Module function entries need special care in get_proc_name
5503 because previous references within the function will have
5504 created symbols attached to the current namespace. */
5505 if (get_proc_name (name, &entry,
5506 gfc_current_ns->parent != NULL
ecd3b73c 5507 && module_procedure))
6de9cd9a
DN
5508 return MATCH_ERROR;
5509
3d79abbd
PB
5510 proc = gfc_current_block ();
5511
bc3e7a8c
TB
5512 /* Make sure that it isn't already declared as BIND(C). If it is, it
5513 must have been marked BIND(C) with a BIND(C) attribute and that is
5514 not allowed for procedures. */
5515 if (entry->attr.is_bind_c == 1)
5516 {
5517 entry->attr.is_bind_c = 0;
5518 if (entry->old_symbol != NULL)
5519 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5520 "variables or common blocks",
5521 &(entry->old_symbol->declared_at));
5522 else
5523 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5524 "variables or common blocks", &gfc_current_locus);
5525 }
f5acf0f2 5526
bc3e7a8c
TB
5527 /* Check what next non-whitespace character is so we can tell if there
5528 is the required parens if we have a BIND(C). */
5529 gfc_gobble_whitespace ();
8fc541d3 5530 peek_char = gfc_peek_ascii_char ();
bc3e7a8c 5531
3d79abbd 5532 if (state == COMP_SUBROUTINE)
6de9cd9a 5533 {
6de9cd9a
DN
5534 m = gfc_match_formal_arglist (entry, 0, 1);
5535 if (m != MATCH_YES)
5536 return MATCH_ERROR;
5537
1eabf70a
TB
5538 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5539 never be an internal procedure. */
5540 is_bind_c = gfc_match_bind_c (entry, true);
bc3e7a8c
TB
5541 if (is_bind_c == MATCH_ERROR)
5542 return MATCH_ERROR;
5543 if (is_bind_c == MATCH_YES)
5544 {
5545 if (peek_char != '(')
5546 {
5547 gfc_error ("Missing required parentheses before BIND(C) at %C");
5548 return MATCH_ERROR;
5549 }
524af0d6
JB
5550 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5551 &(entry->declared_at), 1))
bc3e7a8c
TB
5552 return MATCH_ERROR;
5553 }
5554
f11de7c5
TB
5555 if (!gfc_current_ns->parent
5556 && !add_global_entry (name, entry->binding_label, true))
5557 return MATCH_ERROR;
5558
5559 /* An entry in a subroutine. */
524af0d6
JB
5560 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5561 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
6de9cd9a 5562 return MATCH_ERROR;
3d79abbd
PB
5563 }
5564 else
5565 {
c96cfa49 5566 /* An entry in a function.
636dff67
SK
5567 We need to take special care because writing
5568 ENTRY f()
5569 as
5570 ENTRY f
5571 is allowed, whereas
5572 ENTRY f() RESULT (r)
5573 can't be written as
5574 ENTRY f RESULT (r). */
c96cfa49
TS
5575 old_loc = gfc_current_locus;
5576 if (gfc_match_eos () == MATCH_YES)
5577 {
5578 gfc_current_locus = old_loc;
5579 /* Match the empty argument list, and add the interface to
5580 the symbol. */
5581 m = gfc_match_formal_arglist (entry, 0, 1);
5582 }
5583 else
5584 m = gfc_match_formal_arglist (entry, 0, 0);
5585
6de9cd9a
DN
5586 if (m != MATCH_YES)
5587 return MATCH_ERROR;
5588
6de9cd9a
DN
5589 result = NULL;
5590
5591 if (gfc_match_eos () == MATCH_YES)
5592 {
524af0d6
JB
5593 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5594 || !gfc_add_function (&entry->attr, entry->name, NULL))
6de9cd9a
DN
5595 return MATCH_ERROR;
5596
d198b59a 5597 entry->result = entry;
6de9cd9a
DN
5598 }
5599 else
5600 {
bc3e7a8c 5601 m = gfc_match_suffix (entry, &result);
6de9cd9a
DN
5602 if (m == MATCH_NO)
5603 gfc_syntax_error (ST_ENTRY);
5604 if (m != MATCH_YES)
5605 return MATCH_ERROR;
5606
bc3e7a8c
TB
5607 if (result)
5608 {
524af0d6
JB
5609 if (!gfc_add_result (&result->attr, result->name, NULL)
5610 || !gfc_add_entry (&entry->attr, result->name, NULL)
5611 || !gfc_add_function (&entry->attr, result->name, NULL))
bc3e7a8c
TB
5612 return MATCH_ERROR;
5613 entry->result = result;
5614 }
5615 else
5616 {
524af0d6
JB
5617 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5618 || !gfc_add_function (&entry->attr, entry->name, NULL))
bc3e7a8c
TB
5619 return MATCH_ERROR;
5620 entry->result = entry;
5621 }
6de9cd9a 5622 }
f11de7c5
TB
5623
5624 if (!gfc_current_ns->parent
5625 && !add_global_entry (name, entry->binding_label, false))
5626 return MATCH_ERROR;
6de9cd9a
DN
5627 }
5628
5629 if (gfc_match_eos () != MATCH_YES)
5630 {
5631 gfc_syntax_error (ST_ENTRY);
5632 return MATCH_ERROR;
5633 }
5634
3d79abbd
PB
5635 entry->attr.recursive = proc->attr.recursive;
5636 entry->attr.elemental = proc->attr.elemental;
5637 entry->attr.pure = proc->attr.pure;
6de9cd9a 5638
3d79abbd
PB
5639 el = gfc_get_entry_list ();
5640 el->sym = entry;
5641 el->next = gfc_current_ns->entries;
5642 gfc_current_ns->entries = el;
5643 if (el->next)
5644 el->id = el->next->id + 1;
5645 else
5646 el->id = 1;
6de9cd9a 5647
3d79abbd
PB
5648 new_st.op = EXEC_ENTRY;
5649 new_st.ext.entry = el;
5650
5651 return MATCH_YES;
6de9cd9a
DN
5652}
5653
5654
5655/* Match a subroutine statement, including optional prefixes. */
5656
5657match
5658gfc_match_subroutine (void)
5659{
5660 char name[GFC_MAX_SYMBOL_LEN + 1];
5661 gfc_symbol *sym;
5662 match m;
a8b3b0b6
CR
5663 match is_bind_c;
5664 char peek_char;
1eabf70a 5665 bool allow_binding_name;
6de9cd9a
DN
5666
5667 if (gfc_current_state () != COMP_NONE
5668 && gfc_current_state () != COMP_INTERFACE
5669 && gfc_current_state () != COMP_CONTAINS)
5670 return MATCH_NO;
5671
1c8bcdf7 5672 m = gfc_match_prefix (NULL);
6de9cd9a
DN
5673 if (m != MATCH_YES)
5674 return m;
5675
5676 m = gfc_match ("subroutine% %n", name);
5677 if (m != MATCH_YES)
5678 return m;
5679
1a492601 5680 if (get_proc_name (name, &sym, false))
6de9cd9a 5681 return MATCH_ERROR;
3070bab4 5682
7fcd5ad5
TB
5683 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5684 the symbol existed before. */
5685 sym->declared_at = gfc_current_locus;
5686
524af0d6 5687 if (add_hidden_procptr_result (sym))
3070bab4
JW
5688 sym = sym->result;
5689
6de9cd9a
DN
5690 gfc_new_block = sym;
5691
a8b3b0b6 5692 /* Check what next non-whitespace character is so we can tell if there
bc3e7a8c 5693 is the required parens if we have a BIND(C). */
a8b3b0b6 5694 gfc_gobble_whitespace ();
8fc541d3 5695 peek_char = gfc_peek_ascii_char ();
f5acf0f2 5696
524af0d6 5697 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
6de9cd9a
DN
5698 return MATCH_ERROR;
5699
5700 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5701 return MATCH_ERROR;
5702
a8b3b0b6
CR
5703 /* Make sure that it isn't already declared as BIND(C). If it is, it
5704 must have been marked BIND(C) with a BIND(C) attribute and that is
5705 not allowed for procedures. */
5706 if (sym->attr.is_bind_c == 1)
5707 {
5708 sym->attr.is_bind_c = 0;
5709 if (sym->old_symbol != NULL)
5710 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5711 "variables or common blocks",
5712 &(sym->old_symbol->declared_at));
5713 else
5714 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5715 "variables or common blocks", &gfc_current_locus);
5716 }
1eabf70a
TB
5717
5718 /* C binding names are not allowed for internal procedures. */
5719 if (gfc_current_state () == COMP_CONTAINS
5720 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5721 allow_binding_name = false;
5722 else
5723 allow_binding_name = true;
5724
a8b3b0b6
CR
5725 /* Here, we are just checking if it has the bind(c) attribute, and if
5726 so, then we need to make sure it's all correct. If it doesn't,
5727 we still need to continue matching the rest of the subroutine line. */
1eabf70a 5728 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
5729 if (is_bind_c == MATCH_ERROR)
5730 {
5731 /* There was an attempt at the bind(c), but it was wrong. An
5732 error message should have been printed w/in the gfc_match_bind_c
5733 so here we'll just return the MATCH_ERROR. */
5734 return MATCH_ERROR;
5735 }
5736
5737 if (is_bind_c == MATCH_YES)
5738 {
1eabf70a 5739 /* The following is allowed in the Fortran 2008 draft. */
01f4fff1 5740 if (gfc_current_state () == COMP_CONTAINS
1eabf70a 5741 && sym->ns->proc_name->attr.flavor != FL_MODULE
524af0d6
JB
5742 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5743 "at %L may not be specified for an internal "
5744 "procedure", &gfc_current_locus))
1eabf70a
TB
5745 return MATCH_ERROR;
5746
a8b3b0b6
CR
5747 if (peek_char != '(')
5748 {
5749 gfc_error ("Missing required parentheses before BIND(C) at %C");
5750 return MATCH_ERROR;
5751 }
524af0d6
JB
5752 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5753 &(sym->declared_at), 1))
a8b3b0b6
CR
5754 return MATCH_ERROR;
5755 }
f5acf0f2 5756
6de9cd9a
DN
5757 if (gfc_match_eos () != MATCH_YES)
5758 {
5759 gfc_syntax_error (ST_SUBROUTINE);
5760 return MATCH_ERROR;
5761 }
5762
524af0d6 5763 if (!copy_prefix (&sym->attr, &sym->declared_at))
6de9cd9a
DN
5764 return MATCH_ERROR;
5765
c3005b0f
DK
5766 /* Warn if it has the same name as an intrinsic. */
5767 warn_intrinsic_shadow (sym, false);
5768
6de9cd9a
DN
5769 return MATCH_YES;
5770}
5771
5772
a8b3b0b6
CR
5773/* Match a BIND(C) specifier, with the optional 'name=' specifier if
5774 given, and set the binding label in either the given symbol (if not
86bf520d 5775 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
5776 encounter the BIND(C) before the declaration itself. Return
5777 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5778 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5779 or MATCH_YES if the specifier was correct and the binding label and
5780 bind(c) fields were set correctly for the given symbol or the
1eabf70a
TB
5781 current_ts. If allow_binding_name is false, no binding name may be
5782 given. */
a8b3b0b6
CR
5783
5784match
1eabf70a 5785gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
a8b3b0b6 5786{
f5acf0f2 5787 /* binding label, if exists */
9975a30b 5788 const char* binding_label = NULL;
a8b3b0b6
CR
5789 match double_quote;
5790 match single_quote;
a8b3b0b6 5791
f5acf0f2 5792 /* Initialize the flag that specifies whether we encountered a NAME=
a8b3b0b6
CR
5793 specifier or not. */
5794 has_name_equals = 0;
5795
a8b3b0b6
CR
5796 /* This much we have to be able to match, in this order, if
5797 there is a bind(c) label. */
5798 if (gfc_match (" bind ( c ") != MATCH_YES)
5799 return MATCH_NO;
5800
5801 /* Now see if there is a binding label, or if we've reached the
5802 end of the bind(c) attribute without one. */
5803 if (gfc_match_char (',') == MATCH_YES)
5804 {
5805 if (gfc_match (" name = ") != MATCH_YES)
5806 {
5807 gfc_error ("Syntax error in NAME= specifier for binding label "
5808 "at %C");
5809 /* should give an error message here */
5810 return MATCH_ERROR;
5811 }
5812
5813 has_name_equals = 1;
5814
5815 /* Get the opening quote. */
5816 double_quote = MATCH_YES;
5817 single_quote = MATCH_YES;
5818 double_quote = gfc_match_char ('"');
5819 if (double_quote != MATCH_YES)
5820 single_quote = gfc_match_char ('\'');
5821 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5822 {
5823 gfc_error ("Syntax error in NAME= specifier for binding label "
5824 "at %C");
5825 return MATCH_ERROR;
5826 }
f5acf0f2 5827
a8b3b0b6
CR
5828 /* Grab the binding label, using functions that will not lower
5829 case the names automatically. */
62603fae 5830 if (gfc_match_name_C (&binding_label) != MATCH_YES)
a8b3b0b6 5831 return MATCH_ERROR;
f5acf0f2 5832
a8b3b0b6
CR
5833 /* Get the closing quotation. */
5834 if (double_quote == MATCH_YES)
5835 {
5836 if (gfc_match_char ('"') != MATCH_YES)
5837 {
5838 gfc_error ("Missing closing quote '\"' for binding label at %C");
5839 /* User started string with '"' so looked to match it. */
5840 return MATCH_ERROR;
5841 }
5842 }
5843 else
5844 {
5845 if (gfc_match_char ('\'') != MATCH_YES)
5846 {
5847 gfc_error ("Missing closing quote '\'' for binding label at %C");
5848 /* User started string with "'" char. */
5849 return MATCH_ERROR;
5850 }
5851 }
5852 }
5853
5854 /* Get the required right paren. */
5855 if (gfc_match_char (')') != MATCH_YES)
5856 {
5857 gfc_error ("Missing closing paren for binding label at %C");
5858 return MATCH_ERROR;
5859 }
5860
1eabf70a
TB
5861 if (has_name_equals && !allow_binding_name)
5862 {
5863 gfc_error ("No binding name is allowed in BIND(C) at %C");
5864 return MATCH_ERROR;
5865 }
5866
5867 if (has_name_equals && sym != NULL && sym->attr.dummy)
5868 {
5869 gfc_error ("For dummy procedure %s, no binding name is "
5870 "allowed in BIND(C) at %C", sym->name);
5871 return MATCH_ERROR;
5872 }
5873
5874
a8b3b0b6
CR
5875 /* Save the binding label to the symbol. If sym is null, we're
5876 probably matching the typespec attributes of a declaration and
5877 haven't gotten the name yet, and therefore, no symbol yet. */
62603fae 5878 if (binding_label)
a8b3b0b6
CR
5879 {
5880 if (sym != NULL)
62603fae 5881 sym->binding_label = binding_label;
a8b3b0b6 5882 else
62603fae 5883 curr_binding_label = binding_label;
a8b3b0b6 5884 }
1eabf70a 5885 else if (allow_binding_name)
a8b3b0b6
CR
5886 {
5887 /* No binding label, but if symbol isn't null, we
1eabf70a
TB
5888 can set the label for it here.
5889 If name="" or allow_binding_name is false, no C binding name is
5890 created. */
a8b3b0b6 5891 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
62603fae 5892 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
a8b3b0b6 5893 }
9e1d712c 5894
129d15a3
JW
5895 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5896 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
5897 {
5898 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5899 return MATCH_ERROR;
5900 }
5901
a8b3b0b6
CR
5902 return MATCH_YES;
5903}
5904
5905
1f2959f0 5906/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
5907
5908static int
5909contained_procedure (void)
5910{
083de129 5911 gfc_state_data *s = gfc_state_stack;
ddc9ce91 5912
083de129
TB
5913 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5914 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5915 return 1;
ddc9ce91
TS
5916
5917 return 0;
5918}
5919
d51347f9 5920/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
5921 interoperable with the corresponding C enumeration type, making
5922 sure that -fshort-enums is honored. */
5923
5924static void
5925set_enum_kind(void)
5926{
5927 enumerator_history *current_history = NULL;
5928 int kind;
5929 int i;
5930
5931 if (max_enum == NULL || enum_history == NULL)
5932 return;
5933
cab129d1 5934 if (!flag_short_enums)
d51347f9
TB
5935 return;
5936
25d8f0a2
TS
5937 i = 0;
5938 do
5939 {
5940 kind = gfc_integer_kinds[i++].kind;
5941 }
d51347f9 5942 while (kind < gfc_c_int_kind
25d8f0a2
TS
5943 && gfc_check_integer_range (max_enum->initializer->value.integer,
5944 kind) != ARITH_OK);
5945
5946 current_history = enum_history;
5947 while (current_history != NULL)
5948 {
5949 current_history->sym->ts.kind = kind;
5950 current_history = current_history->next;
5951 }
5952}
5953
636dff67 5954
6de9cd9a 5955/* Match any of the various end-block statements. Returns the type of
9abe5e56
DK
5956 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5957 and END BLOCK statements cannot be replaced by a single END statement. */
6de9cd9a
DN
5958
5959match
636dff67 5960gfc_match_end (gfc_statement *st)
6de9cd9a
DN
5961{
5962 char name[GFC_MAX_SYMBOL_LEN + 1];
5963 gfc_compile_state state;
5964 locus old_loc;
5965 const char *block_name;
5966 const char *target;
ddc9ce91 5967 int eos_ok;
6de9cd9a 5968 match m;
0cab6b73
TK
5969 gfc_namespace *parent_ns, *ns, *prev_ns;
5970 gfc_namespace **nsp;
6de9cd9a 5971
63645982 5972 old_loc = gfc_current_locus;
6de9cd9a
DN
5973 if (gfc_match ("end") != MATCH_YES)
5974 return MATCH_NO;
5975
5976 state = gfc_current_state ();
636dff67
SK
5977 block_name = gfc_current_block () == NULL
5978 ? NULL : gfc_current_block ()->name;
6de9cd9a 5979
03af1e4c 5980 switch (state)
6de9cd9a 5981 {
03af1e4c
DK
5982 case COMP_ASSOCIATE:
5983 case COMP_BLOCK:
3a1fd30c 5984 if (!strncmp (block_name, "block@", strlen("block@")))
03af1e4c
DK
5985 block_name = NULL;
5986 break;
5987
5988 case COMP_CONTAINS:
5989 case COMP_DERIVED_CONTAINS:
6de9cd9a 5990 state = gfc_state_stack->previous->state;
636dff67
SK
5991 block_name = gfc_state_stack->previous->sym == NULL
5992 ? NULL : gfc_state_stack->previous->sym->name;
03af1e4c
DK
5993 break;
5994
5995 default:
5996 break;
6de9cd9a
DN
5997 }
5998
5999 switch (state)
6000 {
6001 case COMP_NONE:
6002 case COMP_PROGRAM:
6003 *st = ST_END_PROGRAM;
6004 target = " program";
ddc9ce91 6005 eos_ok = 1;
6de9cd9a
DN
6006 break;
6007
6008 case COMP_SUBROUTINE:
6009 *st = ST_END_SUBROUTINE;
6010 target = " subroutine";
ddc9ce91 6011 eos_ok = !contained_procedure ();
6de9cd9a
DN
6012 break;
6013
6014 case COMP_FUNCTION:
6015 *st = ST_END_FUNCTION;
6016 target = " function";
ddc9ce91 6017 eos_ok = !contained_procedure ();
6de9cd9a
DN
6018 break;
6019
6020 case COMP_BLOCK_DATA:
6021 *st = ST_END_BLOCK_DATA;
6022 target = " block data";
ddc9ce91 6023 eos_ok = 1;
6de9cd9a
DN
6024 break;
6025
6026 case COMP_MODULE:
6027 *st = ST_END_MODULE;
6028 target = " module";
ddc9ce91 6029 eos_ok = 1;
6de9cd9a
DN
6030 break;
6031
6032 case COMP_INTERFACE:
6033 *st = ST_END_INTERFACE;
6034 target = " interface";
ddc9ce91 6035 eos_ok = 0;
6de9cd9a
DN
6036 break;
6037
6038 case COMP_DERIVED:
30b608eb 6039 case COMP_DERIVED_CONTAINS:
6de9cd9a
DN
6040 *st = ST_END_TYPE;
6041 target = " type";
ddc9ce91 6042 eos_ok = 0;
6de9cd9a
DN
6043 break;
6044
03af1e4c
DK
6045 case COMP_ASSOCIATE:
6046 *st = ST_END_ASSOCIATE;
6047 target = " associate";
6048 eos_ok = 0;
6049 break;
6050
9abe5e56
DK
6051 case COMP_BLOCK:
6052 *st = ST_END_BLOCK;
6053 target = " block";
6054 eos_ok = 0;
6055 break;
6056
6de9cd9a
DN
6057 case COMP_IF:
6058 *st = ST_ENDIF;
6059 target = " if";
ddc9ce91 6060 eos_ok = 0;
6de9cd9a
DN
6061 break;
6062
6063 case COMP_DO:
8c6a85e3 6064 case COMP_DO_CONCURRENT:
6de9cd9a
DN
6065 *st = ST_ENDDO;
6066 target = " do";
ddc9ce91 6067 eos_ok = 0;
6de9cd9a
DN
6068 break;
6069
d0a4a61c
TB
6070 case COMP_CRITICAL:
6071 *st = ST_END_CRITICAL;
6072 target = " critical";
6073 eos_ok = 0;
6074 break;
6075
6de9cd9a 6076 case COMP_SELECT:
cf2b3c22 6077 case COMP_SELECT_TYPE:
6de9cd9a
DN
6078 *st = ST_END_SELECT;
6079 target = " select";
ddc9ce91 6080 eos_ok = 0;
6de9cd9a
DN
6081 break;
6082
6083 case COMP_FORALL:
6084 *st = ST_END_FORALL;
6085 target = " forall";
ddc9ce91 6086 eos_ok = 0;
6de9cd9a
DN
6087 break;
6088
6089 case COMP_WHERE:
6090 *st = ST_END_WHERE;
6091 target = " where";
ddc9ce91 6092 eos_ok = 0;
6de9cd9a
DN
6093 break;
6094
25d8f0a2
TS
6095 case COMP_ENUM:
6096 *st = ST_END_ENUM;
6097 target = " enum";
6098 eos_ok = 0;
6099 last_initializer = NULL;
6100 set_enum_kind ();
6101 gfc_free_enum_history ();
6102 break;
6103
6de9cd9a
DN
6104 default:
6105 gfc_error ("Unexpected END statement at %C");
6106 goto cleanup;
6107 }
6108
6109 if (gfc_match_eos () == MATCH_YES)
6110 {
272001a2
TB
6111 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6112 {
524af0d6
JB
6113 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6114 "instead of %s statement at %L",
6115 gfc_ascii_statement(*st), &old_loc))
272001a2
TB
6116 goto cleanup;
6117 }
6118 else if (!eos_ok)
6de9cd9a 6119 {
66e4ab31 6120 /* We would have required END [something]. */
59ce85b5
TS
6121 gfc_error ("%s statement expected at %L",
6122 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
6123 goto cleanup;
6124 }
6125
6126 return MATCH_YES;
6127 }
6128
6129 /* Verify that we've got the sort of end-block that we're expecting. */
6130 if (gfc_match (target) != MATCH_YES)
6131 {
6132 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
6133 goto cleanup;
6134 }
6135
6136 /* If we're at the end, make sure a block name wasn't required. */
6137 if (gfc_match_eos () == MATCH_YES)
6138 {
6139
690af379 6140 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
d0a4a61c 6141 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
03af1e4c 6142 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6de9cd9a
DN
6143 return MATCH_YES;
6144
9abe5e56 6145 if (!block_name)
6de9cd9a
DN
6146 return MATCH_YES;
6147
6148 gfc_error ("Expected block name of '%s' in %s statement at %C",
6149 block_name, gfc_ascii_statement (*st));
6150
6151 return MATCH_ERROR;
6152 }
6153
6154 /* END INTERFACE has a special handler for its several possible endings. */
6155 if (*st == ST_END_INTERFACE)
6156 return gfc_match_end_interface ();
6157
66e4ab31
SK
6158 /* We haven't hit the end of statement, so what is left must be an
6159 end-name. */
6de9cd9a
DN
6160 m = gfc_match_space ();
6161 if (m == MATCH_YES)
6162 m = gfc_match_name (name);
6163
6164 if (m == MATCH_NO)
6165 gfc_error ("Expected terminating name at %C");
6166 if (m != MATCH_YES)
6167 goto cleanup;
6168
6169 if (block_name == NULL)
6170 goto syntax;
6171
3070bab4 6172 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6de9cd9a
DN
6173 {
6174 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6175 gfc_ascii_statement (*st));
6176 goto cleanup;
6177 }
3070bab4
JW
6178 /* Procedure pointer as function result. */
6179 else if (strcmp (block_name, "ppr@") == 0
6180 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6181 {
6182 gfc_error ("Expected label '%s' for %s statement at %C",
6183 gfc_current_block ()->ns->proc_name->name,
6184 gfc_ascii_statement (*st));
6185 goto cleanup;
6186 }
6de9cd9a
DN
6187
6188 if (gfc_match_eos () == MATCH_YES)
6189 return MATCH_YES;
6190
6191syntax:
6192 gfc_syntax_error (*st);
6193
6194cleanup:
63645982 6195 gfc_current_locus = old_loc;
0cab6b73
TK
6196
6197 /* If we are missing an END BLOCK, we created a half-ready namespace.
6198 Remove it from the parent namespace's sibling list. */
6199
6200 if (state == COMP_BLOCK)
6201 {
6202 parent_ns = gfc_current_ns->parent;
6203
6204 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6205
6206 prev_ns = NULL;
6207 ns = *nsp;
6208 while (ns)
6209 {
6210 if (ns == gfc_current_ns)
6211 {
6212 if (prev_ns == NULL)
6213 *nsp = NULL;
6214 else
6215 prev_ns->sibling = ns->sibling;
6216 }
6217 prev_ns = ns;
6218 ns = ns->sibling;
6219 }
6220
6221 gfc_free_namespace (gfc_current_ns);
6222 gfc_current_ns = parent_ns;
6223 }
6224
6de9cd9a
DN
6225 return MATCH_ERROR;
6226}
6227
6228
6229
6230/***************** Attribute declaration statements ****************/
6231
6232/* Set the attribute of a single variable. */
6233
6234static match
6235attr_decl1 (void)
6236{
6237 char name[GFC_MAX_SYMBOL_LEN + 1];
6238 gfc_array_spec *as;
6239 gfc_symbol *sym;
6240 locus var_locus;
6241 match m;
6242
6243 as = NULL;
6244
6245 m = gfc_match_name (name);
6246 if (m != MATCH_YES)
6247 goto cleanup;
6248
08a6b8e0 6249 if (find_special (name, &sym, false))
6de9cd9a
DN
6250 return MATCH_ERROR;
6251
524af0d6 6252 if (!check_function_name (name))
bb9de0c4
JW
6253 {
6254 m = MATCH_ERROR;
6255 goto cleanup;
6256 }
f5acf0f2 6257
63645982 6258 var_locus = gfc_current_locus;
6de9cd9a
DN
6259
6260 /* Deal with possible array specification for certain attributes. */
6261 if (current_attr.dimension
be59db2d 6262 || current_attr.codimension
6de9cd9a
DN
6263 || current_attr.allocatable
6264 || current_attr.pointer
6265 || current_attr.target)
6266 {
be59db2d
TB
6267 m = gfc_match_array_spec (&as, !current_attr.codimension,
6268 !current_attr.dimension
6269 && !current_attr.pointer
6270 && !current_attr.target);
6de9cd9a
DN
6271 if (m == MATCH_ERROR)
6272 goto cleanup;
6273
6274 if (current_attr.dimension && m == MATCH_NO)
6275 {
636dff67
SK
6276 gfc_error ("Missing array specification at %L in DIMENSION "
6277 "statement", &var_locus);
6de9cd9a
DN
6278 m = MATCH_ERROR;
6279 goto cleanup;
6280 }
6281
1283ab12
TB
6282 if (current_attr.dimension && sym->value)
6283 {
6284 gfc_error ("Dimensions specified for %s at %L after its "
6285 "initialisation", sym->name, &var_locus);
6286 m = MATCH_ERROR;
6287 goto cleanup;
6288 }
6289
be59db2d
TB
6290 if (current_attr.codimension && m == MATCH_NO)
6291 {
6292 gfc_error ("Missing array specification at %L in CODIMENSION "
6293 "statement", &var_locus);
6294 m = MATCH_ERROR;
6295 goto cleanup;
6296 }
6297
6de9cd9a
DN
6298 if ((current_attr.allocatable || current_attr.pointer)
6299 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6300 {
636dff67 6301 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
6302 m = MATCH_ERROR;
6303 goto cleanup;
6304 }
6305 }
6306
2e23972e
JW
6307 /* Update symbol table. DIMENSION attribute is set in
6308 gfc_set_array_spec(). For CLASS variables, this must be applied
b04533af 6309 to the first component, or '_data' field. */
d40477b4 6310 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6de9cd9a 6311 {
524af0d6 6312 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
2e23972e
JW
6313 {
6314 m = MATCH_ERROR;
6315 goto cleanup;
6316 }
2e23972e
JW
6317 }
6318 else
6319 {
be59db2d 6320 if (current_attr.dimension == 0 && current_attr.codimension == 0
524af0d6 6321 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
2e23972e
JW
6322 {
6323 m = MATCH_ERROR;
6324 goto cleanup;
6325 }
6de9cd9a 6326 }
f5acf0f2 6327
528622fd 6328 if (sym->ts.type == BT_CLASS
524af0d6 6329 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false))
96d9b22c
JW
6330 {
6331 m = MATCH_ERROR;
6332 goto cleanup;
6333 }
6de9cd9a 6334
524af0d6 6335 if (!gfc_set_array_spec (sym, as, &var_locus))
6de9cd9a
DN
6336 {
6337 m = MATCH_ERROR;
6338 goto cleanup;
6339 }
d51347f9 6340
83d890b9
AL
6341 if (sym->attr.cray_pointee && sym->as != NULL)
6342 {
6343 /* Fix the array spec. */
f5acf0f2 6344 m = gfc_mod_pointee_as (sym->as);
83d890b9
AL
6345 if (m == MATCH_ERROR)
6346 goto cleanup;
6347 }
6de9cd9a 6348
524af0d6 6349 if (!gfc_add_attribute (&sym->attr, &var_locus))
1902704e
PT
6350 {
6351 m = MATCH_ERROR;
6352 goto cleanup;
6353 }
6354
6de9cd9a
DN
6355 if ((current_attr.external || current_attr.intrinsic)
6356 && sym->attr.flavor != FL_PROCEDURE
524af0d6 6357 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6de9cd9a
DN
6358 {
6359 m = MATCH_ERROR;
6360 goto cleanup;
6361 }
6362
3070bab4
JW
6363 add_hidden_procptr_result (sym);
6364
6de9cd9a
DN
6365 return MATCH_YES;
6366
6367cleanup:
6368 gfc_free_array_spec (as);
6369 return m;
6370}
6371
6372
6373/* Generic attribute declaration subroutine. Used for attributes that
6374 just have a list of names. */
6375
6376static match
6377attr_decl (void)
6378{
6379 match m;
6380
6381 /* Gobble the optional double colon, by simply ignoring the result
6382 of gfc_match(). */
6383 gfc_match (" ::");
6384
6385 for (;;)
6386 {
6387 m = attr_decl1 ();
6388 if (m != MATCH_YES)
6389 break;
6390
6391 if (gfc_match_eos () == MATCH_YES)
6392 {
6393 m = MATCH_YES;
6394 break;
6395 }
6396
6397 if (gfc_match_char (',') != MATCH_YES)
6398 {
6399 gfc_error ("Unexpected character in variable list at %C");
6400 m = MATCH_ERROR;
6401 break;
6402 }
6403 }
6404
6405 return m;
6406}
6407
6408
83d890b9
AL
6409/* This routine matches Cray Pointer declarations of the form:
6410 pointer ( <pointer>, <pointee> )
6411 or
d51347f9
TB
6412 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6413 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
6414 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6415 be either a scalar, or an array declaration. No space is allocated for
d51347f9 6416 the pointee. For the statement
83d890b9
AL
6417 pointer (ipt, ar(10))
6418 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 6419 ar(i) => ((<type> *) ipt)(i)
b122dc6a 6420 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
6421
6422static match
6423cray_pointer_decl (void)
6424{
6425 match m;
be59db2d 6426 gfc_array_spec *as = NULL;
83d890b9
AL
6427 gfc_symbol *cptr; /* Pointer symbol. */
6428 gfc_symbol *cpte; /* Pointee symbol. */
6429 locus var_locus;
6430 bool done = false;
6431
6432 while (!done)
6433 {
6434 if (gfc_match_char ('(') != MATCH_YES)
6435 {
6436 gfc_error ("Expected '(' at %C");
d51347f9 6437 return MATCH_ERROR;
83d890b9 6438 }
d51347f9 6439
83d890b9
AL
6440 /* Match pointer. */
6441 var_locus = gfc_current_locus;
6442 gfc_clear_attr (&current_attr);
6443 gfc_add_cray_pointer (&current_attr, &var_locus);
6444 current_ts.type = BT_INTEGER;
6445 current_ts.kind = gfc_index_integer_kind;
6446
d51347f9 6447 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
6448 if (m != MATCH_YES)
6449 {
6450 gfc_error ("Expected variable name at %C");
6451 return m;
6452 }
d51347f9 6453
524af0d6 6454 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
83d890b9
AL
6455 return MATCH_ERROR;
6456
d51347f9 6457 gfc_set_sym_referenced (cptr);
83d890b9
AL
6458
6459 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6460 {
6461 cptr->ts.type = BT_INTEGER;
d51347f9 6462 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
6463 }
6464 else if (cptr->ts.type != BT_INTEGER)
6465 {
e25a0da3 6466 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
6467 return MATCH_ERROR;
6468 }
6469 else if (cptr->ts.kind < gfc_index_integer_kind)
6470 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 6471 " memory addresses require %d bytes",
636dff67 6472 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
6473
6474 if (gfc_match_char (',') != MATCH_YES)
6475 {
6476 gfc_error ("Expected \",\" at %C");
d51347f9 6477 return MATCH_ERROR;
83d890b9
AL
6478 }
6479
d51347f9 6480 /* Match Pointee. */
83d890b9
AL
6481 var_locus = gfc_current_locus;
6482 gfc_clear_attr (&current_attr);
6483 gfc_add_cray_pointee (&current_attr, &var_locus);
6484 current_ts.type = BT_UNKNOWN;
6485 current_ts.kind = 0;
6486
6487 m = gfc_match_symbol (&cpte, 0);
6488 if (m != MATCH_YES)
6489 {
6490 gfc_error ("Expected variable name at %C");
6491 return m;
6492 }
d51347f9 6493
83d890b9 6494 /* Check for an optional array spec. */
be59db2d 6495 m = gfc_match_array_spec (&as, true, false);
83d890b9
AL
6496 if (m == MATCH_ERROR)
6497 {
6498 gfc_free_array_spec (as);
6499 return m;
6500 }
6501 else if (m == MATCH_NO)
6502 {
6503 gfc_free_array_spec (as);
6504 as = NULL;
f5acf0f2 6505 }
83d890b9 6506
524af0d6 6507 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
83d890b9
AL
6508 return MATCH_ERROR;
6509
6510 gfc_set_sym_referenced (cpte);
6511
6512 if (cpte->as == NULL)
6513 {
524af0d6 6514 if (!gfc_set_array_spec (cpte, as, &var_locus))
83d890b9
AL
6515 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6516 }
6517 else if (as != NULL)
6518 {
e25a0da3 6519 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
6520 gfc_free_array_spec (as);
6521 return MATCH_ERROR;
6522 }
f5acf0f2 6523
83d890b9 6524 as = NULL;
f5acf0f2 6525
83d890b9
AL
6526 if (cpte->as != NULL)
6527 {
6528 /* Fix array spec. */
6529 m = gfc_mod_pointee_as (cpte->as);
6530 if (m == MATCH_ERROR)
6531 return m;
f5acf0f2
PT
6532 }
6533
83d890b9 6534 /* Point the Pointee at the Pointer. */
b122dc6a 6535 cpte->cp_pointer = cptr;
83d890b9
AL
6536
6537 if (gfc_match_char (')') != MATCH_YES)
6538 {
6539 gfc_error ("Expected \")\" at %C");
f5acf0f2 6540 return MATCH_ERROR;
83d890b9
AL
6541 }
6542 m = gfc_match_char (',');
6543 if (m != MATCH_YES)
6544 done = true; /* Stop searching for more declarations. */
6545
6546 }
f5acf0f2 6547
83d890b9
AL
6548 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6549 || gfc_match_eos () != MATCH_YES)
6550 {
6551 gfc_error ("Expected \",\" or end of statement at %C");
6552 return MATCH_ERROR;
6553 }
6554 return MATCH_YES;
6555}
6556
6557
6de9cd9a
DN
6558match
6559gfc_match_external (void)
6560{
6561
6562 gfc_clear_attr (&current_attr);
1902704e 6563 current_attr.external = 1;
6de9cd9a
DN
6564
6565 return attr_decl ();
6566}
6567
6568
6de9cd9a
DN
6569match
6570gfc_match_intent (void)
6571{
6572 sym_intent intent;
6573
9abe5e56
DK
6574 /* This is not allowed within a BLOCK construct! */
6575 if (gfc_current_state () == COMP_BLOCK)
6576 {
6577 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6578 return MATCH_ERROR;
6579 }
6580
6de9cd9a
DN
6581 intent = match_intent_spec ();
6582 if (intent == INTENT_UNKNOWN)
6583 return MATCH_ERROR;
6584
6585 gfc_clear_attr (&current_attr);
1902704e 6586 current_attr.intent = intent;
6de9cd9a
DN
6587
6588 return attr_decl ();
6589}
6590
6591
6592match
6593gfc_match_intrinsic (void)
6594{
6595
6596 gfc_clear_attr (&current_attr);
1902704e 6597 current_attr.intrinsic = 1;
6de9cd9a
DN
6598
6599 return attr_decl ();
6600}
6601
6602
6603match
6604gfc_match_optional (void)
6605{
9abe5e56
DK
6606 /* This is not allowed within a BLOCK construct! */
6607 if (gfc_current_state () == COMP_BLOCK)
6608 {
6609 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6610 return MATCH_ERROR;
6611 }
6de9cd9a
DN
6612
6613 gfc_clear_attr (&current_attr);
1902704e 6614 current_attr.optional = 1;
6de9cd9a
DN
6615
6616 return attr_decl ();
6617}
6618
6619
6620match
6621gfc_match_pointer (void)
6622{
83d890b9 6623 gfc_gobble_whitespace ();
8fc541d3 6624 if (gfc_peek_ascii_char () == '(')
83d890b9
AL
6625 {
6626 if (!gfc_option.flag_cray_pointer)
6627 {
636dff67
SK
6628 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6629 "flag");
83d890b9
AL
6630 return MATCH_ERROR;
6631 }
6632 return cray_pointer_decl ();
6633 }
6634 else
6635 {
6636 gfc_clear_attr (&current_attr);
1902704e 6637 current_attr.pointer = 1;
f5acf0f2 6638
83d890b9
AL
6639 return attr_decl ();
6640 }
6de9cd9a
DN
6641}
6642
6643
6644match
6645gfc_match_allocatable (void)
6646{
6de9cd9a 6647 gfc_clear_attr (&current_attr);
1902704e 6648 current_attr.allocatable = 1;
6de9cd9a
DN
6649
6650 return attr_decl ();
6651}
6652
6653
be59db2d
TB
6654match
6655gfc_match_codimension (void)
6656{
6657 gfc_clear_attr (&current_attr);
6658 current_attr.codimension = 1;
6659
6660 return attr_decl ();
6661}
6662
6663
fe4e525c
TB
6664match
6665gfc_match_contiguous (void)
6666{
524af0d6 6667 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
fe4e525c
TB
6668 return MATCH_ERROR;
6669
6670 gfc_clear_attr (&current_attr);
6671 current_attr.contiguous = 1;
6672
6673 return attr_decl ();
6674}
6675
6676
6de9cd9a
DN
6677match
6678gfc_match_dimension (void)
6679{
6de9cd9a 6680 gfc_clear_attr (&current_attr);
1902704e 6681 current_attr.dimension = 1;
6de9cd9a
DN
6682
6683 return attr_decl ();
6684}
6685
6686
6687match
6688gfc_match_target (void)
6689{
6de9cd9a 6690 gfc_clear_attr (&current_attr);
1902704e 6691 current_attr.target = 1;
6de9cd9a
DN
6692
6693 return attr_decl ();
6694}
6695
6696
6697/* Match the list of entities being specified in a PUBLIC or PRIVATE
6698 statement. */
6699
6700static match
6701access_attr_decl (gfc_statement st)
6702{
6703 char name[GFC_MAX_SYMBOL_LEN + 1];
6704 interface_type type;
6705 gfc_user_op *uop;
c3f34952 6706 gfc_symbol *sym, *dt_sym;
a1ee985f 6707 gfc_intrinsic_op op;
6de9cd9a
DN
6708 match m;
6709
6710 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6711 goto done;
6712
6713 for (;;)
6714 {
a1ee985f 6715 m = gfc_match_generic_spec (&type, name, &op);
6de9cd9a
DN
6716 if (m == MATCH_NO)
6717 goto syntax;
6718 if (m == MATCH_ERROR)
6719 return MATCH_ERROR;
6720
6721 switch (type)
6722 {
6723 case INTERFACE_NAMELESS:
9e1d712c 6724 case INTERFACE_ABSTRACT:
6de9cd9a
DN
6725 goto syntax;
6726
6727 case INTERFACE_GENERIC:
6728 if (gfc_get_symbol (name, NULL, &sym))
6729 goto done;
6730
524af0d6
JB
6731 if (!gfc_add_access (&sym->attr,
6732 (st == ST_PUBLIC)
6733 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6734 sym->name, NULL))
6de9cd9a
DN
6735 return MATCH_ERROR;
6736
c3f34952 6737 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
524af0d6
JB
6738 && !gfc_add_access (&dt_sym->attr,
6739 (st == ST_PUBLIC)
6740 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6741 sym->name, NULL))
c3f34952
TB
6742 return MATCH_ERROR;
6743
6de9cd9a
DN
6744 break;
6745
6746 case INTERFACE_INTRINSIC_OP:
a1ee985f 6747 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6de9cd9a 6748 {
fb03a37e
TK
6749 gfc_intrinsic_op other_op;
6750
a1ee985f 6751 gfc_current_ns->operator_access[op] =
6de9cd9a 6752 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
fb03a37e
TK
6753
6754 /* Handle the case if there is another op with the same
6755 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6756 other_op = gfc_equivalent_op (op);
6757
6758 if (other_op != INTRINSIC_NONE)
6759 gfc_current_ns->operator_access[other_op] =
6760 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6761
6de9cd9a
DN
6762 }
6763 else
6764 {
6765 gfc_error ("Access specification of the %s operator at %C has "
a1ee985f 6766 "already been specified", gfc_op2string (op));
6de9cd9a
DN
6767 goto done;
6768 }
6769
6770 break;
6771
6772 case INTERFACE_USER_OP:
6773 uop = gfc_get_uop (name);
6774
6775 if (uop->access == ACCESS_UNKNOWN)
6776 {
636dff67
SK
6777 uop->access = (st == ST_PUBLIC)
6778 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
6779 }
6780 else
6781 {
636dff67
SK
6782 gfc_error ("Access specification of the .%s. operator at %C "
6783 "has already been specified", sym->name);
6de9cd9a
DN
6784 goto done;
6785 }
6786
6787 break;
6788 }
6789
6790 if (gfc_match_char (',') == MATCH_NO)
6791 break;
6792 }
6793
6794 if (gfc_match_eos () != MATCH_YES)
6795 goto syntax;
6796 return MATCH_YES;
6797
6798syntax:
6799 gfc_syntax_error (st);
6800
6801done:
6802 return MATCH_ERROR;
6803}
6804
6805
ee7e677f
TB
6806match
6807gfc_match_protected (void)
6808{
6809 gfc_symbol *sym;
6810 match m;
6811
6812 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6813 {
6814 gfc_error ("PROTECTED at %C only allowed in specification "
6815 "part of a module");
6816 return MATCH_ERROR;
6817
6818 }
6819
524af0d6 6820 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
ee7e677f
TB
6821 return MATCH_ERROR;
6822
6823 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6824 {
6825 return MATCH_ERROR;
6826 }
6827
6828 if (gfc_match_eos () == MATCH_YES)
6829 goto syntax;
6830
6831 for(;;)
6832 {
6833 m = gfc_match_symbol (&sym, 0);
6834 switch (m)
6835 {
6836 case MATCH_YES:
524af0d6 6837 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
ee7e677f
TB
6838 return MATCH_ERROR;
6839 goto next_item;
6840
6841 case MATCH_NO:
6842 break;
6843
6844 case MATCH_ERROR:
6845 return MATCH_ERROR;
6846 }
6847
6848 next_item:
6849 if (gfc_match_eos () == MATCH_YES)
6850 break;
6851 if (gfc_match_char (',') != MATCH_YES)
6852 goto syntax;
6853 }
6854
6855 return MATCH_YES;
6856
6857syntax:
6858 gfc_error ("Syntax error in PROTECTED statement at %C");
6859 return MATCH_ERROR;
6860}
6861
6862
86bf520d 6863/* The PRIVATE statement is a bit weird in that it can be an attribute
df2fba9e 6864 declaration, but also works as a standalone statement inside of a
6de9cd9a
DN
6865 type declaration or a module. */
6866
6867match
636dff67 6868gfc_match_private (gfc_statement *st)
6de9cd9a
DN
6869{
6870
6871 if (gfc_match ("private") != MATCH_YES)
6872 return MATCH_NO;
6873
d51347f9 6874 if (gfc_current_state () != COMP_MODULE
30b608eb
DK
6875 && !(gfc_current_state () == COMP_DERIVED
6876 && gfc_state_stack->previous
6877 && gfc_state_stack->previous->state == COMP_MODULE)
6878 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6879 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6880 && gfc_state_stack->previous->previous->state == COMP_MODULE))
d51347f9
TB
6881 {
6882 gfc_error ("PRIVATE statement at %C is only allowed in the "
6883 "specification part of a module");
6884 return MATCH_ERROR;
6885 }
6886
6de9cd9a
DN
6887 if (gfc_current_state () == COMP_DERIVED)
6888 {
6889 if (gfc_match_eos () == MATCH_YES)
6890 {
6891 *st = ST_PRIVATE;
6892 return MATCH_YES;
6893 }
6894
6895 gfc_syntax_error (ST_PRIVATE);
6896 return MATCH_ERROR;
6897 }
6898
6899 if (gfc_match_eos () == MATCH_YES)
6900 {
6901 *st = ST_PRIVATE;
6902 return MATCH_YES;
6903 }
6904
6905 *st = ST_ATTR_DECL;
6906 return access_attr_decl (ST_PRIVATE);
6907}
6908
6909
6910match
636dff67 6911gfc_match_public (gfc_statement *st)
6de9cd9a
DN
6912{
6913
6914 if (gfc_match ("public") != MATCH_YES)
6915 return MATCH_NO;
6916
d51347f9
TB
6917 if (gfc_current_state () != COMP_MODULE)
6918 {
6919 gfc_error ("PUBLIC statement at %C is only allowed in the "
6920 "specification part of a module");
6921 return MATCH_ERROR;
6922 }
6923
6de9cd9a
DN
6924 if (gfc_match_eos () == MATCH_YES)
6925 {
6926 *st = ST_PUBLIC;
6927 return MATCH_YES;
6928 }
6929
6930 *st = ST_ATTR_DECL;
6931 return access_attr_decl (ST_PUBLIC);
6932}
6933
6934
6935/* Workhorse for gfc_match_parameter. */
6936
6937static match
6938do_parm (void)
6939{
6940 gfc_symbol *sym;
6941 gfc_expr *init;
6942 match m;
524af0d6 6943 bool t;
6de9cd9a
DN
6944
6945 m = gfc_match_symbol (&sym, 0);
6946 if (m == MATCH_NO)
6947 gfc_error ("Expected variable name at %C in PARAMETER statement");
6948
6949 if (m != MATCH_YES)
6950 return m;
6951
6952 if (gfc_match_char ('=') == MATCH_NO)
6953 {
6954 gfc_error ("Expected = sign in PARAMETER statement at %C");
6955 return MATCH_ERROR;
6956 }
6957
6958 m = gfc_match_init_expr (&init);
6959 if (m == MATCH_NO)
6960 gfc_error ("Expected expression at %C in PARAMETER statement");
6961 if (m != MATCH_YES)
6962 return m;
6963
6964 if (sym->ts.type == BT_UNKNOWN
524af0d6 6965 && !gfc_set_default_type (sym, 1, NULL))
6de9cd9a
DN
6966 {
6967 m = MATCH_ERROR;
6968 goto cleanup;
6969 }
6970
524af0d6
JB
6971 if (!gfc_check_assign_symbol (sym, NULL, init)
6972 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
6de9cd9a
DN
6973 {
6974 m = MATCH_ERROR;
6975 goto cleanup;
6976 }
6977
1283ab12
TB
6978 if (sym->value)
6979 {
6980 gfc_error ("Initializing already initialized variable at %C");
6981 m = MATCH_ERROR;
6982 goto cleanup;
6983 }
6984
7919373d 6985 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
524af0d6 6986 return (t) ? MATCH_YES : MATCH_ERROR;
6de9cd9a
DN
6987
6988cleanup:
6989 gfc_free_expr (init);
6990 return m;
6991}
6992
6993
6994/* Match a parameter statement, with the weird syntax that these have. */
6995
6996match
6997gfc_match_parameter (void)
6998{
6999 match m;
7000
7001 if (gfc_match_char ('(') == MATCH_NO)
7002 return MATCH_NO;
7003
7004 for (;;)
7005 {
7006 m = do_parm ();
7007 if (m != MATCH_YES)
7008 break;
7009
7010 if (gfc_match (" )%t") == MATCH_YES)
7011 break;
7012
7013 if (gfc_match_char (',') != MATCH_YES)
7014 {
7015 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7016 m = MATCH_ERROR;
7017 break;
7018 }
7019 }
7020
7021 return m;
7022}
7023
7024
7025/* Save statements have a special syntax. */
7026
7027match
7028gfc_match_save (void)
7029{
9056bd70
TS
7030 char n[GFC_MAX_SYMBOL_LEN+1];
7031 gfc_common_head *c;
6de9cd9a
DN
7032 gfc_symbol *sym;
7033 match m;
7034
7035 if (gfc_match_eos () == MATCH_YES)
7036 {
7037 if (gfc_current_ns->seen_save)
7038 {
524af0d6
JB
7039 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7040 "follows previous SAVE statement"))
09e87839 7041 return MATCH_ERROR;
6de9cd9a
DN
7042 }
7043
7044 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7045 return MATCH_YES;
7046 }
7047
7048 if (gfc_current_ns->save_all)
7049 {
524af0d6
JB
7050 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7051 "blanket SAVE statement"))
09e87839 7052 return MATCH_ERROR;
6de9cd9a
DN
7053 }
7054
7055 gfc_match (" ::");
7056
7057 for (;;)
7058 {
7059 m = gfc_match_symbol (&sym, 0);
7060 switch (m)
7061 {
7062 case MATCH_YES:
524af0d6
JB
7063 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7064 &gfc_current_locus))
6de9cd9a
DN
7065 return MATCH_ERROR;
7066 goto next_item;
7067
7068 case MATCH_NO:
7069 break;
7070
7071 case MATCH_ERROR:
7072 return MATCH_ERROR;
7073 }
7074
9056bd70 7075 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
7076 if (m == MATCH_ERROR)
7077 return MATCH_ERROR;
7078 if (m == MATCH_NO)
7079 goto syntax;
7080
53814b8f 7081 c = gfc_get_common (n, 0);
9056bd70
TS
7082 c->saved = 1;
7083
6de9cd9a
DN
7084 gfc_current_ns->seen_save = 1;
7085
7086 next_item:
7087 if (gfc_match_eos () == MATCH_YES)
7088 break;
7089 if (gfc_match_char (',') != MATCH_YES)
7090 goto syntax;
7091 }
7092
7093 return MATCH_YES;
7094
7095syntax:
7096 gfc_error ("Syntax error in SAVE statement at %C");
7097 return MATCH_ERROR;
7098}
7099
7100
06469efd
PT
7101match
7102gfc_match_value (void)
7103{
7104 gfc_symbol *sym;
7105 match m;
7106
9abe5e56
DK
7107 /* This is not allowed within a BLOCK construct! */
7108 if (gfc_current_state () == COMP_BLOCK)
7109 {
7110 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7111 return MATCH_ERROR;
7112 }
7113
524af0d6 7114 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
06469efd
PT
7115 return MATCH_ERROR;
7116
7117 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7118 {
7119 return MATCH_ERROR;
7120 }
7121
7122 if (gfc_match_eos () == MATCH_YES)
7123 goto syntax;
7124
7125 for(;;)
7126 {
7127 m = gfc_match_symbol (&sym, 0);
7128 switch (m)
7129 {
7130 case MATCH_YES:
524af0d6 7131 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
06469efd
PT
7132 return MATCH_ERROR;
7133 goto next_item;
7134
7135 case MATCH_NO:
7136 break;
7137
7138 case MATCH_ERROR:
7139 return MATCH_ERROR;
7140 }
7141
7142 next_item:
7143 if (gfc_match_eos () == MATCH_YES)
7144 break;
7145 if (gfc_match_char (',') != MATCH_YES)
7146 goto syntax;
7147 }
7148
7149 return MATCH_YES;
7150
7151syntax:
7152 gfc_error ("Syntax error in VALUE statement at %C");
7153 return MATCH_ERROR;
7154}
7155
66e4ab31 7156
775e6c3a
TB
7157match
7158gfc_match_volatile (void)
7159{
7160 gfc_symbol *sym;
7161 match m;
7162
524af0d6 7163 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
775e6c3a
TB
7164 return MATCH_ERROR;
7165
7166 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7167 {
7168 return MATCH_ERROR;
7169 }
7170
7171 if (gfc_match_eos () == MATCH_YES)
7172 goto syntax;
7173
7174 for(;;)
7175 {
f5acf0f2 7176 /* VOLATILE is special because it can be added to host-associated
be59db2d 7177 symbols locally. Except for coarrays. */
9bce3c1c 7178 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
7179 switch (m)
7180 {
7181 case MATCH_YES:
be59db2d
TB
7182 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7183 for variable in a BLOCK which is defined outside of the BLOCK. */
7184 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7185 {
7186 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7187 "%C, which is use-/host-associated", sym->name);
7188 return MATCH_ERROR;
7189 }
524af0d6 7190 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
775e6c3a
TB
7191 return MATCH_ERROR;
7192 goto next_item;
7193
7194 case MATCH_NO:
7195 break;
7196
7197 case MATCH_ERROR:
7198 return MATCH_ERROR;
7199 }
7200
7201 next_item:
7202 if (gfc_match_eos () == MATCH_YES)
7203 break;
7204 if (gfc_match_char (',') != MATCH_YES)
7205 goto syntax;
7206 }
7207
7208 return MATCH_YES;
7209
7210syntax:
7211 gfc_error ("Syntax error in VOLATILE statement at %C");
7212 return MATCH_ERROR;
7213}
7214
7215
1eee5628
TB
7216match
7217gfc_match_asynchronous (void)
7218{
7219 gfc_symbol *sym;
7220 match m;
7221
524af0d6 7222 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
1eee5628
TB
7223 return MATCH_ERROR;
7224
7225 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7226 {
7227 return MATCH_ERROR;
7228 }
7229
7230 if (gfc_match_eos () == MATCH_YES)
7231 goto syntax;
7232
7233 for(;;)
7234 {
f5acf0f2 7235 /* ASYNCHRONOUS is special because it can be added to host-associated
1eee5628
TB
7236 symbols locally. */
7237 m = gfc_match_symbol (&sym, 1);
7238 switch (m)
7239 {
7240 case MATCH_YES:
524af0d6 7241 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
1eee5628
TB
7242 return MATCH_ERROR;
7243 goto next_item;
7244
7245 case MATCH_NO:
7246 break;
7247
7248 case MATCH_ERROR:
7249 return MATCH_ERROR;
7250 }
7251
7252 next_item:
7253 if (gfc_match_eos () == MATCH_YES)
7254 break;
7255 if (gfc_match_char (',') != MATCH_YES)
7256 goto syntax;
7257 }
7258
7259 return MATCH_YES;
7260
7261syntax:
7262 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7263 return MATCH_ERROR;
7264}
7265
7266
6de9cd9a
DN
7267/* Match a module procedure statement. Note that we have to modify
7268 symbols in the parent's namespace because the current one was there
49de9e73 7269 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
7270
7271match
7272gfc_match_modproc (void)
7273{
7274 char name[GFC_MAX_SYMBOL_LEN + 1];
7275 gfc_symbol *sym;
7276 match m;
162b5a21 7277 locus old_locus;
060fca4a 7278 gfc_namespace *module_ns;
2b77e908 7279 gfc_interface *old_interface_head, *interface;
6de9cd9a
DN
7280
7281 if (gfc_state_stack->state != COMP_INTERFACE
7282 || gfc_state_stack->previous == NULL
129d15a3
JW
7283 || current_interface.type == INTERFACE_NAMELESS
7284 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 7285 {
636dff67
SK
7286 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7287 "interface");
6de9cd9a
DN
7288 return MATCH_ERROR;
7289 }
7290
060fca4a
PT
7291 module_ns = gfc_current_ns->parent;
7292 for (; module_ns; module_ns = module_ns->parent)
43dfd40c
SK
7293 if (module_ns->proc_name->attr.flavor == FL_MODULE
7294 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7295 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7296 && !module_ns->proc_name->attr.contained))
060fca4a
PT
7297 break;
7298
7299 if (module_ns == NULL)
7300 return MATCH_ERROR;
7301
2b77e908
FXC
7302 /* Store the current state of the interface. We will need it if we
7303 end up with a syntax error and need to recover. */
7304 old_interface_head = gfc_current_interface_head ();
7305
162b5a21
SK
7306 /* Check if the F2008 optional double colon appears. */
7307 gfc_gobble_whitespace ();
7308 old_locus = gfc_current_locus;
7309 if (gfc_match ("::") == MATCH_YES)
7310 {
524af0d6
JB
7311 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7312 "MODULE PROCEDURE statement at %L", &old_locus))
162b5a21
SK
7313 return MATCH_ERROR;
7314 }
7315 else
7316 gfc_current_locus = old_locus;
f5acf0f2 7317
6de9cd9a
DN
7318 for (;;)
7319 {
2b77e908 7320 bool last = false;
162b5a21 7321 old_locus = gfc_current_locus;
2b77e908 7322
6de9cd9a
DN
7323 m = gfc_match_name (name);
7324 if (m == MATCH_NO)
7325 goto syntax;
7326 if (m != MATCH_YES)
7327 return MATCH_ERROR;
7328
2b77e908
FXC
7329 /* Check for syntax error before starting to add symbols to the
7330 current namespace. */
7331 if (gfc_match_eos () == MATCH_YES)
7332 last = true;
162b5a21 7333
2b77e908
FXC
7334 if (!last && gfc_match_char (',') != MATCH_YES)
7335 goto syntax;
7336
7337 /* Now we're sure the syntax is valid, we process this item
7338 further. */
060fca4a 7339 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
7340 return MATCH_ERROR;
7341
43dfd40c
SK
7342 if (sym->attr.intrinsic)
7343 {
7344 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7345 "PROCEDURE", &old_locus);
7346 return MATCH_ERROR;
7347 }
7348
6de9cd9a 7349 if (sym->attr.proc != PROC_MODULE
524af0d6 7350 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
6de9cd9a
DN
7351 return MATCH_ERROR;
7352
524af0d6 7353 if (!gfc_add_interface (sym))
6de9cd9a
DN
7354 return MATCH_ERROR;
7355
71f77fd7 7356 sym->attr.mod_proc = 1;
43dfd40c 7357 sym->declared_at = old_locus;
71f77fd7 7358
2b77e908 7359 if (last)
6de9cd9a 7360 break;
6de9cd9a
DN
7361 }
7362
7363 return MATCH_YES;
7364
7365syntax:
2b77e908
FXC
7366 /* Restore the previous state of the interface. */
7367 interface = gfc_current_interface_head ();
7368 gfc_set_current_interface_head (old_interface_head);
7369
7370 /* Free the new interfaces. */
7371 while (interface != old_interface_head)
7372 {
7373 gfc_interface *i = interface->next;
cede9502 7374 free (interface);
2b77e908
FXC
7375 interface = i;
7376 }
7377
7378 /* And issue a syntax error. */
6de9cd9a
DN
7379 gfc_syntax_error (ST_MODULE_PROC);
7380 return MATCH_ERROR;
7381}
7382
7383
7d1f1e61
PT
7384/* Check a derived type that is being extended. */
7385static gfc_symbol*
7386check_extended_derived_type (char *name)
7387{
7388 gfc_symbol *extended;
7389
7390 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7391 {
7392 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7393 return NULL;
7394 }
7395
7396 if (!extended)
7397 {
7398 gfc_error ("No such symbol in TYPE definition at %C");
7399 return NULL;
7400 }
7401
c3f34952
TB
7402 extended = gfc_find_dt_in_generic (extended);
7403
7d1f1e61
PT
7404 if (extended->attr.flavor != FL_DERIVED)
7405 {
7406 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7407 "derived type", name);
7408 return NULL;
7409 }
7410
7411 if (extended->attr.is_bind_c)
7412 {
7413 gfc_error ("'%s' cannot be extended at %C because it "
7414 "is BIND(C)", extended->name);
7415 return NULL;
7416 }
7417
7418 if (extended->attr.sequence)
7419 {
7420 gfc_error ("'%s' cannot be extended at %C because it "
7421 "is a SEQUENCE type", extended->name);
7422 return NULL;
7423 }
7424
7425 return extended;
7426}
7427
7428
a8b3b0b6
CR
7429/* Match the optional attribute specifiers for a type declaration.
7430 Return MATCH_ERROR if an error is encountered in one of the handled
7431 attributes (public, private, bind(c)), MATCH_NO if what's found is
7432 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7433 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
7434
7435match
7d1f1e61 7436gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6de9cd9a 7437{
a8b3b0b6 7438 /* See if the derived type is marked as private. */
6de9cd9a
DN
7439 if (gfc_match (" , private") == MATCH_YES)
7440 {
d51347f9 7441 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 7442 {
d51347f9
TB
7443 gfc_error ("Derived type at %C can only be PRIVATE in the "
7444 "specification part of a module");
6de9cd9a
DN
7445 return MATCH_ERROR;
7446 }
7447
524af0d6 7448 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
6de9cd9a 7449 return MATCH_ERROR;
6de9cd9a 7450 }
a8b3b0b6 7451 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 7452 {
d51347f9 7453 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 7454 {
d51347f9
TB
7455 gfc_error ("Derived type at %C can only be PUBLIC in the "
7456 "specification part of a module");
6de9cd9a
DN
7457 return MATCH_ERROR;
7458 }
7459
524af0d6 7460 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
6de9cd9a 7461 return MATCH_ERROR;
6de9cd9a 7462 }
52f49934 7463 else if (gfc_match (" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
7464 {
7465 /* If the type is defined to be bind(c) it then needs to make
7466 sure that all fields are interoperable. This will
7467 need to be a semantic check on the finished derived type.
7468 See 15.2.3 (lines 9-12) of F2003 draft. */
524af0d6 7469 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
a8b3b0b6
CR
7470 return MATCH_ERROR;
7471
7472 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7473 }
52f49934
DK
7474 else if (gfc_match (" , abstract") == MATCH_YES)
7475 {
524af0d6 7476 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
52f49934
DK
7477 return MATCH_ERROR;
7478
524af0d6 7479 if (!gfc_add_abstract (attr, &gfc_current_locus))
52f49934
DK
7480 return MATCH_ERROR;
7481 }
524af0d6 7482 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7d1f1e61 7483 {
524af0d6 7484 if (!gfc_add_extension (attr, &gfc_current_locus))
7d1f1e61
PT
7485 return MATCH_ERROR;
7486 }
a8b3b0b6
CR
7487 else
7488 return MATCH_NO;
7489
7490 /* If we get here, something matched. */
7491 return MATCH_YES;
7492}
7493
7494
7495/* Match the beginning of a derived type declaration. If a type name
7496 was the result of a function, then it is possible to have a symbol
7497 already to be known as a derived type yet have no components. */
7498
7499match
7500gfc_match_derived_decl (void)
7501{
7502 char name[GFC_MAX_SYMBOL_LEN + 1];
7d1f1e61 7503 char parent[GFC_MAX_SYMBOL_LEN + 1];
a8b3b0b6 7504 symbol_attribute attr;
c3f34952 7505 gfc_symbol *sym, *gensym;
7d1f1e61 7506 gfc_symbol *extended;
a8b3b0b6
CR
7507 match m;
7508 match is_type_attr_spec = MATCH_NO;
e7303e85 7509 bool seen_attr = false;
c3f34952 7510 gfc_interface *intr = NULL, *head;
a8b3b0b6
CR
7511
7512 if (gfc_current_state () == COMP_DERIVED)
7513 return MATCH_NO;
7514
7d1f1e61
PT
7515 name[0] = '\0';
7516 parent[0] = '\0';
a8b3b0b6 7517 gfc_clear_attr (&attr);
7d1f1e61 7518 extended = NULL;
a8b3b0b6
CR
7519
7520 do
7521 {
7d1f1e61 7522 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
a8b3b0b6
CR
7523 if (is_type_attr_spec == MATCH_ERROR)
7524 return MATCH_ERROR;
e7303e85
FXC
7525 if (is_type_attr_spec == MATCH_YES)
7526 seen_attr = true;
a8b3b0b6 7527 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 7528
63a3341a
PT
7529 /* Deal with derived type extensions. The extension attribute has
7530 been added to 'attr' but now the parent type must be found and
7531 checked. */
7d1f1e61
PT
7532 if (parent[0])
7533 extended = check_extended_derived_type (parent);
7534
7535 if (parent[0] && !extended)
7536 return MATCH_ERROR;
7537
e7303e85 7538 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6de9cd9a
DN
7539 {
7540 gfc_error ("Expected :: in TYPE definition at %C");
7541 return MATCH_ERROR;
7542 }
7543
7544 m = gfc_match (" %n%t", name);
7545 if (m != MATCH_YES)
7546 return m;
7547
e9c06563
TB
7548 /* Make sure the name is not the name of an intrinsic type. */
7549 if (gfc_is_intrinsic_typename (name))
6de9cd9a 7550 {
636dff67
SK
7551 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7552 "type", name);
6de9cd9a
DN
7553 return MATCH_ERROR;
7554 }
7555
c3f34952 7556 if (gfc_get_symbol (name, NULL, &gensym))
6de9cd9a
DN
7557 return MATCH_ERROR;
7558
c3f34952 7559 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
6de9cd9a
DN
7560 {
7561 gfc_error ("Derived type name '%s' at %C already has a basic type "
c3f34952
TB
7562 "of %s", gensym->name, gfc_typename (&gensym->ts));
7563 return MATCH_ERROR;
7564 }
7565
7566 if (!gensym->attr.generic
524af0d6 7567 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
c3f34952
TB
7568 return MATCH_ERROR;
7569
7570 if (!gensym->attr.function
524af0d6 7571 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
c3f34952
TB
7572 return MATCH_ERROR;
7573
7574 sym = gfc_find_dt_in_generic (gensym);
7575
7576 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7577 {
7578 gfc_error ("Derived type definition of '%s' at %C has already been "
7579 "defined", sym->name);
6de9cd9a
DN
7580 return MATCH_ERROR;
7581 }
7582
c3f34952
TB
7583 if (!sym)
7584 {
7585 /* Use upper case to save the actual derived-type symbol. */
7586 gfc_get_symbol (gfc_get_string ("%c%s",
7587 (char) TOUPPER ((unsigned char) gensym->name[0]),
7588 &gensym->name[1]), NULL, &sym);
7589 sym->name = gfc_get_string (gensym->name);
7590 head = gensym->generic;
7591 intr = gfc_get_interface ();
7592 intr->sym = sym;
7593 intr->where = gfc_current_locus;
7594 intr->sym->declared_at = gfc_current_locus;
7595 intr->next = head;
7596 gensym->generic = intr;
7597 gensym->attr.if_source = IFSRC_DECL;
7598 }
7599
6de9cd9a
DN
7600 /* The symbol may already have the derived attribute without the
7601 components. The ways this can happen is via a function
7602 definition, an INTRINSIC statement or a subtype in another
7603 derived type that is a pointer. The first part of the AND clause
df2fba9e 7604 is true if the symbol is not the return value of a function. */
6de9cd9a 7605 if (sym->attr.flavor != FL_DERIVED
524af0d6 7606 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
6de9cd9a
DN
7607 return MATCH_ERROR;
7608
6de9cd9a 7609 if (attr.access != ACCESS_UNKNOWN
524af0d6 7610 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
6de9cd9a 7611 return MATCH_ERROR;
c3f34952
TB
7612 else if (sym->attr.access == ACCESS_UNKNOWN
7613 && gensym->attr.access != ACCESS_UNKNOWN
524af0d6
JB
7614 && !gfc_add_access (&sym->attr, gensym->attr.access,
7615 sym->name, NULL))
c3f34952
TB
7616 return MATCH_ERROR;
7617
7618 if (sym->attr.access != ACCESS_UNKNOWN
7619 && gensym->attr.access == ACCESS_UNKNOWN)
7620 gensym->attr.access = sym->attr.access;
6de9cd9a 7621
a8b3b0b6
CR
7622 /* See if the derived type was labeled as bind(c). */
7623 if (attr.is_bind_c != 0)
7624 sym->attr.is_bind_c = attr.is_bind_c;
7625
34523524
DK
7626 /* Construct the f2k_derived namespace if it is not yet there. */
7627 if (!sym->f2k_derived)
7628 sym->f2k_derived = gfc_get_namespace (NULL, 0);
f5acf0f2 7629
7d1f1e61
PT
7630 if (extended && !sym->components)
7631 {
7632 gfc_component *p;
7633 gfc_symtree *st;
7634
7635 /* Add the extended derived type as the first component. */
7636 gfc_add_component (sym, parent, &p);
7d1f1e61
PT
7637 extended->refs++;
7638 gfc_set_sym_referenced (extended);
7639
7640 p->ts.type = BT_DERIVED;
bc21d315 7641 p->ts.u.derived = extended;
7d1f1e61 7642 p->initializer = gfc_default_initializer (&p->ts);
f5acf0f2 7643
7c1dab0d
JW
7644 /* Set extension level. */
7645 if (extended->attr.extension == 255)
7646 {
7647 /* Since the extension field is 8 bit wide, we can only have
7648 up to 255 extension levels. */
7649 gfc_error ("Maximum extension level reached with type '%s' at %L",
7650 extended->name, &extended->declared_at);
7651 return MATCH_ERROR;
7652 }
7653 sym->attr.extension = extended->attr.extension + 1;
7d1f1e61
PT
7654
7655 /* Provide the links between the extended type and its extension. */
7656 if (!extended->f2k_derived)
7657 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7658 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7659 st->n.sym = sym;
7660 }
7661
7c1dab0d
JW
7662 if (!sym->hash_value)
7663 /* Set the hash for the compound name for this type. */
4fa02692 7664 sym->hash_value = gfc_hash_value (sym);
cf2b3c22 7665
52f49934
DK
7666 /* Take over the ABSTRACT attribute. */
7667 sym->attr.abstract = attr.abstract;
7668
6de9cd9a
DN
7669 gfc_new_block = sym;
7670
7671 return MATCH_YES;
7672}
83d890b9
AL
7673
7674
f5acf0f2 7675/* Cray Pointees can be declared as:
b3aefde2 7676 pointer (ipt, a (n,m,...,*)) */
83d890b9 7677
32e8bb8e 7678match
83d890b9
AL
7679gfc_mod_pointee_as (gfc_array_spec *as)
7680{
7681 as->cray_pointee = true; /* This will be useful to know later. */
7682 if (as->type == AS_ASSUMED_SIZE)
b3aefde2 7683 as->cp_was_assumed = true;
83d890b9
AL
7684 else if (as->type == AS_ASSUMED_SHAPE)
7685 {
7686 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7687 return MATCH_ERROR;
7688 }
7689 return MATCH_YES;
7690}
25d8f0a2
TS
7691
7692
f5acf0f2
PT
7693/* Match the enum definition statement, here we are trying to match
7694 the first line of enum definition statement.
25d8f0a2
TS
7695 Returns MATCH_YES if match is found. */
7696
7697match
7698gfc_match_enum (void)
7699{
7700 match m;
f5acf0f2 7701
25d8f0a2
TS
7702 m = gfc_match_eos ();
7703 if (m != MATCH_YES)
7704 return m;
7705
524af0d6 7706 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
25d8f0a2
TS
7707 return MATCH_ERROR;
7708
7709 return MATCH_YES;
7710}
7711
7712
31224396
SK
7713/* Returns an initializer whose value is one higher than the value of the
7714 LAST_INITIALIZER argument. If the argument is NULL, the
7715 initializers value will be set to zero. The initializer's kind
7716 will be set to gfc_c_int_kind.
7717
7718 If -fshort-enums is given, the appropriate kind will be selected
7719 later after all enumerators have been parsed. A warning is issued
7720 here if an initializer exceeds gfc_c_int_kind. */
7721
7722static gfc_expr *
7723enum_initializer (gfc_expr *last_initializer, locus where)
7724{
7725 gfc_expr *result;
b7e75771 7726 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
31224396
SK
7727
7728 mpz_init (result->value.integer);
7729
7730 if (last_initializer != NULL)
7731 {
7732 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7733 result->where = last_initializer->where;
7734
7735 if (gfc_check_integer_range (result->value.integer,
7736 gfc_c_int_kind) != ARITH_OK)
7737 {
7738 gfc_error ("Enumerator exceeds the C integer type at %C");
7739 return NULL;
7740 }
7741 }
7742 else
7743 {
7744 /* Control comes here, if it's the very first enumerator and no
7745 initializer has been given. It will be initialized to zero. */
7746 mpz_set_si (result->value.integer, 0);
7747 }
7748
7749 return result;
7750}
7751
7752
6133c68a
TS
7753/* Match a variable name with an optional initializer. When this
7754 subroutine is called, a variable is expected to be parsed next.
7755 Depending on what is happening at the moment, updates either the
7756 symbol table or the current interface. */
7757
7758static match
7759enumerator_decl (void)
7760{
7761 char name[GFC_MAX_SYMBOL_LEN + 1];
7762 gfc_expr *initializer;
7763 gfc_array_spec *as = NULL;
7764 gfc_symbol *sym;
7765 locus var_locus;
7766 match m;
524af0d6 7767 bool t;
6133c68a
TS
7768 locus old_locus;
7769
7770 initializer = NULL;
7771 old_locus = gfc_current_locus;
7772
7773 /* When we get here, we've just matched a list of attributes and
7774 maybe a type and a double colon. The next thing we expect to see
7775 is the name of the symbol. */
7776 m = gfc_match_name (name);
7777 if (m != MATCH_YES)
7778 goto cleanup;
7779
7780 var_locus = gfc_current_locus;
7781
7782 /* OK, we've successfully matched the declaration. Now put the
7783 symbol in the current namespace. If we fail to create the symbol,
7784 bail out. */
524af0d6 7785 if (!build_sym (name, NULL, false, &as, &var_locus))
6133c68a
TS
7786 {
7787 m = MATCH_ERROR;
7788 goto cleanup;
7789 }
7790
7791 /* The double colon must be present in order to have initializers.
7792 Otherwise the statement is ambiguous with an assignment statement. */
7793 if (colon_seen)
7794 {
7795 if (gfc_match_char ('=') == MATCH_YES)
7796 {
7797 m = gfc_match_init_expr (&initializer);
7798 if (m == MATCH_NO)
7799 {
7800 gfc_error ("Expected an initialization expression at %C");
7801 m = MATCH_ERROR;
7802 }
7803
7804 if (m != MATCH_YES)
7805 goto cleanup;
7806 }
7807 }
7808
7809 /* If we do not have an initializer, the initialization value of the
7810 previous enumerator (stored in last_initializer) is incremented
7811 by 1 and is used to initialize the current enumerator. */
7812 if (initializer == NULL)
31224396 7813 initializer = enum_initializer (last_initializer, old_locus);
d51347f9 7814
6133c68a
TS
7815 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7816 {
01e64c3d
JJ
7817 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7818 &var_locus);
d51347f9 7819 m = MATCH_ERROR;
6133c68a
TS
7820 goto cleanup;
7821 }
7822
7823 /* Store this current initializer, for the next enumerator variable
7824 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7825 use last_initializer below. */
7826 last_initializer = initializer;
7827 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7828
7829 /* Maintain enumerator history. */
7830 gfc_find_symbol (name, NULL, 0, &sym);
7831 create_enum_history (sym, last_initializer);
7832
524af0d6 7833 return (t) ? MATCH_YES : MATCH_ERROR;
6133c68a
TS
7834
7835cleanup:
7836 /* Free stuff up and return. */
7837 gfc_free_expr (initializer);
7838
7839 return m;
7840}
7841
7842
66e4ab31 7843/* Match the enumerator definition statement. */
25d8f0a2
TS
7844
7845match
7846gfc_match_enumerator_def (void)
7847{
7848 match m;
524af0d6 7849 bool t;
d51347f9 7850
25d8f0a2 7851 gfc_clear_ts (&current_ts);
d51347f9 7852
25d8f0a2
TS
7853 m = gfc_match (" enumerator");
7854 if (m != MATCH_YES)
7855 return m;
6133c68a
TS
7856
7857 m = gfc_match (" :: ");
7858 if (m == MATCH_ERROR)
7859 return m;
7860
7861 colon_seen = (m == MATCH_YES);
d51347f9 7862
25d8f0a2
TS
7863 if (gfc_current_state () != COMP_ENUM)
7864 {
7865 gfc_error ("ENUM definition statement expected before %C");
7866 gfc_free_enum_history ();
7867 return MATCH_ERROR;
7868 }
7869
7870 (&current_ts)->type = BT_INTEGER;
7871 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 7872
6133c68a
TS
7873 gfc_clear_attr (&current_attr);
7874 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
524af0d6 7875 if (!t)
25d8f0a2 7876 {
6133c68a 7877 m = MATCH_ERROR;
25d8f0a2
TS
7878 goto cleanup;
7879 }
7880
25d8f0a2
TS
7881 for (;;)
7882 {
6133c68a 7883 m = enumerator_decl ();
25d8f0a2 7884 if (m == MATCH_ERROR)
01e64c3d
JJ
7885 {
7886 gfc_free_enum_history ();
7887 goto cleanup;
7888 }
25d8f0a2
TS
7889 if (m == MATCH_NO)
7890 break;
7891
7892 if (gfc_match_eos () == MATCH_YES)
7893 goto cleanup;
7894 if (gfc_match_char (',') != MATCH_YES)
7895 break;
7896 }
7897
7898 if (gfc_current_state () == COMP_ENUM)
7899 {
7900 gfc_free_enum_history ();
7901 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7902 m = MATCH_ERROR;
7903 }
7904
7905cleanup:
7906 gfc_free_array_spec (current_as);
7907 current_as = NULL;
7908 return m;
7909
7910}
7911
f6fad28e 7912
30b608eb
DK
7913/* Match binding attributes. */
7914
7915static match
713485cc 7916match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
30b608eb
DK
7917{
7918 bool found_passing = false;
713485cc 7919 bool seen_ptr = false;
90661f26 7920 match m = MATCH_YES;
30b608eb 7921
eea58adb 7922 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
30b608eb
DK
7923 this case the defaults are in there. */
7924 ba->access = ACCESS_UNKNOWN;
7925 ba->pass_arg = NULL;
7926 ba->pass_arg_num = 0;
7927 ba->nopass = 0;
7928 ba->non_overridable = 0;
b0e5fa94 7929 ba->deferred = 0;
90661f26 7930 ba->ppc = ppc;
30b608eb
DK
7931
7932 /* If we find a comma, we believe there are binding attributes. */
90661f26
JW
7933 m = gfc_match_char (',');
7934 if (m == MATCH_NO)
7935 goto done;
30b608eb
DK
7936
7937 do
7938 {
e157f736
DK
7939 /* Access specifier. */
7940
7941 m = gfc_match (" public");
30b608eb
DK
7942 if (m == MATCH_ERROR)
7943 goto error;
7944 if (m == MATCH_YES)
7945 {
e157f736 7946 if (ba->access != ACCESS_UNKNOWN)
30b608eb 7947 {
e157f736 7948 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
7949 goto error;
7950 }
7951
e157f736 7952 ba->access = ACCESS_PUBLIC;
30b608eb
DK
7953 continue;
7954 }
7955
e157f736 7956 m = gfc_match (" private");
30b608eb
DK
7957 if (m == MATCH_ERROR)
7958 goto error;
7959 if (m == MATCH_YES)
7960 {
e157f736 7961 if (ba->access != ACCESS_UNKNOWN)
30b608eb 7962 {
e157f736 7963 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
7964 goto error;
7965 }
7966
e157f736 7967 ba->access = ACCESS_PRIVATE;
30b608eb
DK
7968 continue;
7969 }
7970
e157f736
DK
7971 /* If inside GENERIC, the following is not allowed. */
7972 if (!generic)
30b608eb 7973 {
30b608eb 7974
e157f736
DK
7975 /* NOPASS flag. */
7976 m = gfc_match (" nopass");
7977 if (m == MATCH_ERROR)
7978 goto error;
7979 if (m == MATCH_YES)
30b608eb 7980 {
e157f736
DK
7981 if (found_passing)
7982 {
7983 gfc_error ("Binding attributes already specify passing,"
7984 " illegal NOPASS at %C");
7985 goto error;
7986 }
7987
7988 found_passing = true;
7989 ba->nopass = 1;
7990 continue;
30b608eb
DK
7991 }
7992
e157f736
DK
7993 /* PASS possibly including argument. */
7994 m = gfc_match (" pass");
7995 if (m == MATCH_ERROR)
7996 goto error;
7997 if (m == MATCH_YES)
30b608eb 7998 {
e157f736
DK
7999 char arg[GFC_MAX_SYMBOL_LEN + 1];
8000
8001 if (found_passing)
8002 {
8003 gfc_error ("Binding attributes already specify passing,"
8004 " illegal PASS at %C");
8005 goto error;
8006 }
8007
8008 m = gfc_match (" ( %n )", arg);
8009 if (m == MATCH_ERROR)
8010 goto error;
8011 if (m == MATCH_YES)
90661f26 8012 ba->pass_arg = gfc_get_string (arg);
e157f736
DK
8013 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8014
8015 found_passing = true;
8016 ba->nopass = 0;
8017 continue;
30b608eb
DK
8018 }
8019
713485cc
JW
8020 if (ppc)
8021 {
8022 /* POINTER flag. */
8023 m = gfc_match (" pointer");
8024 if (m == MATCH_ERROR)
8025 goto error;
8026 if (m == MATCH_YES)
8027 {
8028 if (seen_ptr)
8029 {
8030 gfc_error ("Duplicate POINTER attribute at %C");
8031 goto error;
8032 }
8033
8034 seen_ptr = true;
713485cc
JW
8035 continue;
8036 }
8037 }
8038 else
8039 {
8040 /* NON_OVERRIDABLE flag. */
8041 m = gfc_match (" non_overridable");
8042 if (m == MATCH_ERROR)
8043 goto error;
8044 if (m == MATCH_YES)
8045 {
8046 if (ba->non_overridable)
8047 {
8048 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8049 goto error;
8050 }
8051
8052 ba->non_overridable = 1;
8053 continue;
8054 }
8055
8056 /* DEFERRED flag. */
8057 m = gfc_match (" deferred");
8058 if (m == MATCH_ERROR)
8059 goto error;
8060 if (m == MATCH_YES)
8061 {
8062 if (ba->deferred)
8063 {
8064 gfc_error ("Duplicate DEFERRED at %C");
8065 goto error;
8066 }
8067
8068 ba->deferred = 1;
8069 continue;
8070 }
8071 }
8072
30b608eb
DK
8073 }
8074
8075 /* Nothing matching found. */
e157f736
DK
8076 if (generic)
8077 gfc_error ("Expected access-specifier at %C");
8078 else
8079 gfc_error ("Expected binding attribute at %C");
30b608eb
DK
8080 goto error;
8081 }
8082 while (gfc_match_char (',') == MATCH_YES);
8083
b0e5fa94
DK
8084 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8085 if (ba->non_overridable && ba->deferred)
8086 {
8087 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8088 goto error;
8089 }
8090
90661f26
JW
8091 m = MATCH_YES;
8092
8093done:
e157f736
DK
8094 if (ba->access == ACCESS_UNKNOWN)
8095 ba->access = gfc_typebound_default_access;
8096
713485cc
JW
8097 if (ppc && !seen_ptr)
8098 {
8099 gfc_error ("POINTER attribute is required for procedure pointer component"
8100 " at %C");
8101 goto error;
8102 }
8103
90661f26 8104 return m;
30b608eb
DK
8105
8106error:
30b608eb
DK
8107 return MATCH_ERROR;
8108}
8109
8110
8111/* Match a PROCEDURE specific binding inside a derived type. */
8112
8113static match
8114match_procedure_in_type (void)
8115{
8116 char name[GFC_MAX_SYMBOL_LEN + 1];
8117 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
1be17993 8118 char* target = NULL, *ifc = NULL;
3e15518b 8119 gfc_typebound_proc tb;
30b608eb
DK
8120 bool seen_colons;
8121 bool seen_attrs;
8122 match m;
8123 gfc_symtree* stree;
8124 gfc_namespace* ns;
8125 gfc_symbol* block;
1be17993 8126 int num;
30b608eb
DK
8127
8128 /* Check current state. */
8129 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8130 block = gfc_state_stack->previous->sym;
8131 gcc_assert (block);
8132
b0e5fa94 8133 /* Try to match PROCEDURE(interface). */
30b608eb
DK
8134 if (gfc_match (" (") == MATCH_YES)
8135 {
b0e5fa94
DK
8136 m = gfc_match_name (target_buf);
8137 if (m == MATCH_ERROR)
8138 return m;
8139 if (m != MATCH_YES)
8140 {
8141 gfc_error ("Interface-name expected after '(' at %C");
8142 return MATCH_ERROR;
8143 }
8144
8145 if (gfc_match (" )") != MATCH_YES)
8146 {
8147 gfc_error ("')' expected at %C");
8148 return MATCH_ERROR;
8149 }
8150
1be17993 8151 ifc = target_buf;
30b608eb
DK
8152 }
8153
8154 /* Construct the data structure. */
ff5b6492 8155 memset (&tb, 0, sizeof (tb));
3e15518b 8156 tb.where = gfc_current_locus;
30b608eb
DK
8157
8158 /* Match binding attributes. */
3e15518b 8159 m = match_binding_attributes (&tb, false, false);
30b608eb
DK
8160 if (m == MATCH_ERROR)
8161 return m;
8162 seen_attrs = (m == MATCH_YES);
8163
1be17993 8164 /* Check that attribute DEFERRED is given if an interface is specified. */
3e15518b 8165 if (tb.deferred && !ifc)
b0e5fa94
DK
8166 {
8167 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8168 return MATCH_ERROR;
8169 }
3e15518b 8170 if (ifc && !tb.deferred)
b0e5fa94
DK
8171 {
8172 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8173 return MATCH_ERROR;
8174 }
8175
30b608eb
DK
8176 /* Match the colons. */
8177 m = gfc_match (" ::");
8178 if (m == MATCH_ERROR)
8179 return m;
8180 seen_colons = (m == MATCH_YES);
8181 if (seen_attrs && !seen_colons)
8182 {
8183 gfc_error ("Expected '::' after binding-attributes at %C");
8184 return MATCH_ERROR;
8185 }
8186
f5acf0f2 8187 /* Match the binding names. */
1be17993 8188 for(num=1;;num++)
30b608eb 8189 {
1be17993
JW
8190 m = gfc_match_name (name);
8191 if (m == MATCH_ERROR)
8192 return m;
8193 if (m == MATCH_NO)
b0e5fa94 8194 {
1be17993 8195 gfc_error ("Expected binding name at %C");
b0e5fa94
DK
8196 return MATCH_ERROR;
8197 }
8198
524af0d6 8199 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
1be17993 8200 return MATCH_ERROR;
30b608eb 8201
1be17993
JW
8202 /* Try to match the '=> target', if it's there. */
8203 target = ifc;
8204 m = gfc_match (" =>");
30b608eb
DK
8205 if (m == MATCH_ERROR)
8206 return m;
1be17993 8207 if (m == MATCH_YES)
30b608eb 8208 {
3e15518b 8209 if (tb.deferred)
1be17993
JW
8210 {
8211 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8212 return MATCH_ERROR;
8213 }
8214
8215 if (!seen_colons)
8216 {
8217 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8218 " at %C");
8219 return MATCH_ERROR;
8220 }
8221
8222 m = gfc_match_name (target_buf);
8223 if (m == MATCH_ERROR)
8224 return m;
8225 if (m == MATCH_NO)
8226 {
8227 gfc_error ("Expected binding target after '=>' at %C");
8228 return MATCH_ERROR;
8229 }
8230 target = target_buf;
30b608eb 8231 }
30b608eb 8232
1be17993
JW
8233 /* If no target was found, it has the same name as the binding. */
8234 if (!target)
8235 target = name;
30b608eb 8236
1be17993
JW
8237 /* Get the namespace to insert the symbols into. */
8238 ns = block->f2k_derived;
8239 gcc_assert (ns);
30b608eb 8240
1be17993 8241 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
3e15518b 8242 if (tb.deferred && !block->attr.abstract)
1be17993
JW
8243 {
8244 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8245 "is not ABSTRACT", block->name);
8246 return MATCH_ERROR;
8247 }
30b608eb 8248
1be17993
JW
8249 /* See if we already have a binding with this name in the symtree which
8250 would be an error. If a GENERIC already targetted this binding, it may
8251 be already there but then typebound is still NULL. */
8252 stree = gfc_find_symtree (ns->tb_sym_root, name);
9f23af48 8253 if (stree && stree->n.tb)
1be17993
JW
8254 {
8255 gfc_error ("There is already a procedure with binding name '%s' for "
8256 "the derived type '%s' at %C", name, block->name);
8257 return MATCH_ERROR;
8258 }
b0e5fa94 8259
1be17993 8260 /* Insert it and set attributes. */
30b608eb 8261
9f23af48
MM
8262 if (!stree)
8263 {
8264 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8265 gcc_assert (stree);
8266 }
3e15518b 8267 stree->n.tb = gfc_get_typebound_proc (&tb);
e34ccb4c 8268
3e15518b
JW
8269 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8270 false))
1be17993 8271 return MATCH_ERROR;
3e15518b 8272 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
f5acf0f2 8273
1be17993
JW
8274 if (gfc_match_eos () == MATCH_YES)
8275 return MATCH_YES;
8276 if (gfc_match_char (',') != MATCH_YES)
8277 goto syntax;
e34ccb4c 8278 }
30b608eb 8279
1be17993
JW
8280syntax:
8281 gfc_error ("Syntax error in PROCEDURE statement at %C");
8282 return MATCH_ERROR;
30b608eb
DK
8283}
8284
8285
e157f736
DK
8286/* Match a GENERIC procedure binding inside a derived type. */
8287
8288match
8289gfc_match_generic (void)
8290{
8291 char name[GFC_MAX_SYMBOL_LEN + 1];
94747289 8292 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
e157f736
DK
8293 gfc_symbol* block;
8294 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8295 gfc_typebound_proc* tb;
e157f736 8296 gfc_namespace* ns;
94747289
DK
8297 interface_type op_type;
8298 gfc_intrinsic_op op;
e157f736
DK
8299 match m;
8300
8301 /* Check current state. */
8302 if (gfc_current_state () == COMP_DERIVED)
8303 {
8304 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8305 return MATCH_ERROR;
8306 }
8307 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8308 return MATCH_NO;
8309 block = gfc_state_stack->previous->sym;
8310 ns = block->f2k_derived;
8311 gcc_assert (block && ns);
8312
ff5b6492
MM
8313 memset (&tbattr, 0, sizeof (tbattr));
8314 tbattr.where = gfc_current_locus;
8315
e157f736 8316 /* See if we get an access-specifier. */
713485cc 8317 m = match_binding_attributes (&tbattr, true, false);
e157f736
DK
8318 if (m == MATCH_ERROR)
8319 goto error;
8320
8321 /* Now the colons, those are required. */
8322 if (gfc_match (" ::") != MATCH_YES)
8323 {
8324 gfc_error ("Expected '::' at %C");
8325 goto error;
8326 }
8327
94747289
DK
8328 /* Match the binding name; depending on type (operator / generic) format
8329 it for future error messages into bind_name. */
f5acf0f2 8330
94747289 8331 m = gfc_match_generic_spec (&op_type, name, &op);
e157f736
DK
8332 if (m == MATCH_ERROR)
8333 return MATCH_ERROR;
8334 if (m == MATCH_NO)
8335 {
94747289 8336 gfc_error ("Expected generic name or operator descriptor at %C");
e157f736
DK
8337 goto error;
8338 }
8339
94747289 8340 switch (op_type)
e157f736 8341 {
94747289
DK
8342 case INTERFACE_GENERIC:
8343 snprintf (bind_name, sizeof (bind_name), "%s", name);
8344 break;
f5acf0f2 8345
94747289
DK
8346 case INTERFACE_USER_OP:
8347 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8348 break;
f5acf0f2 8349
94747289
DK
8350 case INTERFACE_INTRINSIC_OP:
8351 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8352 gfc_op2string (op));
8353 break;
8354
8355 default:
8356 gcc_unreachable ();
8357 }
e34ccb4c 8358
94747289
DK
8359 /* Match the required =>. */
8360 if (gfc_match (" =>") != MATCH_YES)
8361 {
8362 gfc_error ("Expected '=>' at %C");
8363 goto error;
8364 }
f5acf0f2 8365
94747289
DK
8366 /* Try to find existing GENERIC binding with this name / for this operator;
8367 if there is something, check that it is another GENERIC and then extend
8368 it rather than building a new node. Otherwise, create it and put it
8369 at the right position. */
8370
8371 switch (op_type)
8372 {
8373 case INTERFACE_USER_OP:
8374 case INTERFACE_GENERIC:
8375 {
8376 const bool is_op = (op_type == INTERFACE_USER_OP);
8377 gfc_symtree* st;
8378
8379 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8380 if (st)
8381 {
8382 tb = st->n.tb;
8383 gcc_assert (tb);
8384 }
8385 else
8386 tb = NULL;
8387
8388 break;
8389 }
8390
8391 case INTERFACE_INTRINSIC_OP:
8392 tb = ns->tb_op[op];
8393 break;
8394
8395 default:
8396 gcc_unreachable ();
8397 }
8398
8399 if (tb)
8400 {
e34ccb4c 8401 if (!tb->is_generic)
e157f736 8402 {
94747289 8403 gcc_assert (op_type == INTERFACE_GENERIC);
e157f736
DK
8404 gfc_error ("There's already a non-generic procedure with binding name"
8405 " '%s' for the derived type '%s' at %C",
94747289 8406 bind_name, block->name);
e157f736
DK
8407 goto error;
8408 }
8409
e157f736
DK
8410 if (tb->access != tbattr.access)
8411 {
8412 gfc_error ("Binding at %C must have the same access as already"
94747289 8413 " defined binding '%s'", bind_name);
e157f736
DK
8414 goto error;
8415 }
8416 }
8417 else
8418 {
3e15518b 8419 tb = gfc_get_typebound_proc (NULL);
e157f736
DK
8420 tb->where = gfc_current_locus;
8421 tb->access = tbattr.access;
8422 tb->is_generic = 1;
8423 tb->u.generic = NULL;
94747289
DK
8424
8425 switch (op_type)
8426 {
8427 case INTERFACE_GENERIC:
8428 case INTERFACE_USER_OP:
8429 {
8430 const bool is_op = (op_type == INTERFACE_USER_OP);
8431 gfc_symtree* st;
8432
8433 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8434 name);
8435 gcc_assert (st);
8436 st->n.tb = tb;
8437
8438 break;
8439 }
f5acf0f2 8440
94747289
DK
8441 case INTERFACE_INTRINSIC_OP:
8442 ns->tb_op[op] = tb;
8443 break;
8444
8445 default:
8446 gcc_unreachable ();
8447 }
e157f736
DK
8448 }
8449
8450 /* Now, match all following names as specific targets. */
8451 do
8452 {
8453 gfc_symtree* target_st;
8454 gfc_tbp_generic* target;
8455
8456 m = gfc_match_name (name);
8457 if (m == MATCH_ERROR)
8458 goto error;
8459 if (m == MATCH_NO)
8460 {
8461 gfc_error ("Expected specific binding name at %C");
8462 goto error;
8463 }
8464
e34ccb4c 8465 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
e157f736
DK
8466
8467 /* See if this is a duplicate specification. */
8468 for (target = tb->u.generic; target; target = target->next)
8469 if (target_st == target->specific_st)
8470 {
8471 gfc_error ("'%s' already defined as specific binding for the"
94747289 8472 " generic '%s' at %C", name, bind_name);
e157f736
DK
8473 goto error;
8474 }
8475
e157f736
DK
8476 target = gfc_get_tbp_generic ();
8477 target->specific_st = target_st;
8478 target->specific = NULL;
8479 target->next = tb->u.generic;
218e1228
TB
8480 target->is_operator = ((op_type == INTERFACE_USER_OP)
8481 || (op_type == INTERFACE_INTRINSIC_OP));
e157f736
DK
8482 tb->u.generic = target;
8483 }
8484 while (gfc_match (" ,") == MATCH_YES);
8485
8486 /* Here should be the end. */
8487 if (gfc_match_eos () != MATCH_YES)
8488 {
8489 gfc_error ("Junk after GENERIC binding at %C");
8490 goto error;
8491 }
8492
8493 return MATCH_YES;
8494
8495error:
8496 return MATCH_ERROR;
8497}
8498
8499
34523524
DK
8500/* Match a FINAL declaration inside a derived type. */
8501
8502match
8503gfc_match_final_decl (void)
8504{
8505 char name[GFC_MAX_SYMBOL_LEN + 1];
8506 gfc_symbol* sym;
8507 match m;
8508 gfc_namespace* module_ns;
8509 bool first, last;
30b608eb 8510 gfc_symbol* block;
34523524 8511
33344e0f
JW
8512 if (gfc_current_form == FORM_FREE)
8513 {
8514 char c = gfc_peek_ascii_char ();
8515 if (!gfc_is_whitespace (c) && c != ':')
8516 return MATCH_NO;
8517 }
f5acf0f2 8518
30b608eb 8519 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
34523524 8520 {
33344e0f
JW
8521 if (gfc_current_form == FORM_FIXED)
8522 return MATCH_NO;
8523
34523524 8524 gfc_error ("FINAL declaration at %C must be inside a derived type "
30b608eb 8525 "CONTAINS section");
34523524
DK
8526 return MATCH_ERROR;
8527 }
8528
30b608eb
DK
8529 block = gfc_state_stack->previous->sym;
8530 gcc_assert (block);
34523524 8531
30b608eb
DK
8532 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8533 || gfc_state_stack->previous->previous->state != COMP_MODULE)
34523524
DK
8534 {
8535 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8536 " specification part of a MODULE");
8537 return MATCH_ERROR;
8538 }
8539
8540 module_ns = gfc_current_ns;
8541 gcc_assert (module_ns);
8542 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8543
8544 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8545 if (gfc_match (" ::") == MATCH_ERROR)
8546 return MATCH_ERROR;
8547
8548 /* Match the sequence of procedure names. */
8549 first = true;
8550 last = false;
8551 do
8552 {
8553 gfc_finalizer* f;
8554
8555 if (first && gfc_match_eos () == MATCH_YES)
8556 {
8557 gfc_error ("Empty FINAL at %C");
8558 return MATCH_ERROR;
8559 }
8560
8561 m = gfc_match_name (name);
8562 if (m == MATCH_NO)
8563 {
8564 gfc_error ("Expected module procedure name at %C");
8565 return MATCH_ERROR;
8566 }
8567 else if (m != MATCH_YES)
8568 return MATCH_ERROR;
8569
8570 if (gfc_match_eos () == MATCH_YES)
8571 last = true;
8572 if (!last && gfc_match_char (',') != MATCH_YES)
8573 {
8574 gfc_error ("Expected ',' at %C");
8575 return MATCH_ERROR;
8576 }
8577
8578 if (gfc_get_symbol (name, module_ns, &sym))
8579 {
8580 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8581 return MATCH_ERROR;
8582 }
8583
8584 /* Mark the symbol as module procedure. */
8585 if (sym->attr.proc != PROC_MODULE
524af0d6 8586 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
34523524
DK
8587 return MATCH_ERROR;
8588
8589 /* Check if we already have this symbol in the list, this is an error. */
30b608eb 8590 for (f = block->f2k_derived->finalizers; f; f = f->next)
f6fad28e 8591 if (f->proc_sym == sym)
34523524
DK
8592 {
8593 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8594 name);
8595 return MATCH_ERROR;
8596 }
8597
8598 /* Add this symbol to the list of finalizers. */
30b608eb 8599 gcc_assert (block->f2k_derived);
34523524 8600 ++sym->refs;
ece3f663 8601 f = XCNEW (gfc_finalizer);
f6fad28e
DK
8602 f->proc_sym = sym;
8603 f->proc_tree = NULL;
34523524 8604 f->where = gfc_current_locus;
30b608eb
DK
8605 f->next = block->f2k_derived->finalizers;
8606 block->f2k_derived->finalizers = f;
34523524
DK
8607
8608 first = false;
8609 }
8610 while (!last);
8611
8612 return MATCH_YES;
8613}
08a6b8e0
TB
8614
8615
8616const ext_attr_t ext_attr_list[] = {
e7ac6a7c
TB
8617 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8618 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8619 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8620 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8621 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8622 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
8623 { NULL, EXT_ATTR_LAST, NULL }
08a6b8e0
TB
8624};
8625
8626/* Match a !GCC$ ATTRIBUTES statement of the form:
8627 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8628 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8629
8630 TODO: We should support all GCC attributes using the same syntax for
8631 the attribute list, i.e. the list in C
8632 __attributes(( attribute-list ))
8633 matches then
8634 !GCC$ ATTRIBUTES attribute-list ::
8635 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8636 saved into a TREE.
8637
8638 As there is absolutely no risk of confusion, we should never return
8639 MATCH_NO. */
8640match
8641gfc_match_gcc_attributes (void)
f5acf0f2 8642{
08a6b8e0
TB
8643 symbol_attribute attr;
8644 char name[GFC_MAX_SYMBOL_LEN + 1];
8645 unsigned id;
8646 gfc_symbol *sym;
8647 match m;
8648
8649 gfc_clear_attr (&attr);
8650 for(;;)
8651 {
8652 char ch;
8653
8654 if (gfc_match_name (name) != MATCH_YES)
8655 return MATCH_ERROR;
8656
8657 for (id = 0; id < EXT_ATTR_LAST; id++)
8658 if (strcmp (name, ext_attr_list[id].name) == 0)
8659 break;
8660
8661 if (id == EXT_ATTR_LAST)
8662 {
8663 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8664 return MATCH_ERROR;
8665 }
8666
524af0d6 8667 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
08a6b8e0
TB
8668 return MATCH_ERROR;
8669
8670 gfc_gobble_whitespace ();
8671 ch = gfc_next_ascii_char ();
8672 if (ch == ':')
8673 {
8674 /* This is the successful exit condition for the loop. */
8675 if (gfc_next_ascii_char () == ':')
8676 break;
8677 }
8678
8679 if (ch == ',')
8680 continue;
8681
8682 goto syntax;
8683 }
8684
8685 if (gfc_match_eos () == MATCH_YES)
8686 goto syntax;
8687
8688 for(;;)
8689 {
8690 m = gfc_match_name (name);
8691 if (m != MATCH_YES)
8692 return m;
8693
8694 if (find_special (name, &sym, true))
8695 return MATCH_ERROR;
f5acf0f2 8696
08a6b8e0
TB
8697 sym->attr.ext_attr |= attr.ext_attr;
8698
8699 if (gfc_match_eos () == MATCH_YES)
8700 break;
8701
8702 if (gfc_match_char (',') != MATCH_YES)
8703 goto syntax;
8704 }
8705
8706 return MATCH_YES;
8707
8708syntax:
8709 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8710 return MATCH_ERROR;
8711}
This page took 4.127349 seconds and 5 git commands to generate.