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