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