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