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