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