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