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