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