]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
gfortran.h (struct gfc_namespace): New member `implicit_loc'.
[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 =
921 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
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
TB
1984
1985 gfc_gobble_whitespace ();
8fc541d3
FXC
1986 if ((c = gfc_next_ascii_char ()) != ')'
1987 && (ts->type != BT_CHARACTER || c != ','))
6de9cd9a 1988 {
96ee3a4a
TB
1989 if (ts->type == BT_CHARACTER)
1990 gfc_error ("Missing right parenthesis or comma at %C");
1991 else
1992 gfc_error ("Missing right parenthesis at %C");
e2d29968 1993 m = MATCH_ERROR;
6de9cd9a 1994 }
a8b3b0b6
CR
1995 else
1996 /* All tests passed. */
1997 m = MATCH_YES;
6de9cd9a 1998
a8b3b0b6
CR
1999 if(m == MATCH_ERROR)
2000 gfc_current_locus = where;
2001
2002 /* Return what we know from the test(s). */
2003 return m;
6de9cd9a
DN
2004
2005no_match:
2006 gfc_free_expr (e);
63645982 2007 gfc_current_locus = where;
6de9cd9a
DN
2008 return m;
2009}
2010
2011
187de1ed
FXC
2012static match
2013match_char_kind (int * kind, int * is_iso_c)
2014{
2015 locus where;
2016 gfc_expr *e;
2017 match m, n;
2018 const char *msg;
2019
2020 m = MATCH_NO;
2021 e = NULL;
2022 where = gfc_current_locus;
2023
2024 n = gfc_match_init_expr (&e);
96ee3a4a 2025
1c8bcdf7 2026 if (n != MATCH_YES && gfc_matching_function)
96ee3a4a 2027 {
1c8bcdf7
PT
2028 /* The expression might include use-associated or imported
2029 parameters and try again after the specification
2030 expressions. */
96ee3a4a 2031 gfc_free_expr (e);
96ee3a4a
TB
2032 gfc_undo_symbols ();
2033 return MATCH_YES;
2034 }
2035
187de1ed
FXC
2036 if (n == MATCH_NO)
2037 gfc_error ("Expected initialization expression at %C");
2038 if (n != MATCH_YES)
2039 return MATCH_ERROR;
2040
2041 if (e->rank != 0)
2042 {
2043 gfc_error ("Expected scalar initialization expression at %C");
2044 m = MATCH_ERROR;
2045 goto no_match;
2046 }
2047
2048 msg = gfc_extract_int (e, kind);
2049 *is_iso_c = e->ts.is_iso_c;
2050 if (msg != NULL)
2051 {
2052 gfc_error (msg);
2053 m = MATCH_ERROR;
2054 goto no_match;
2055 }
2056
2057 gfc_free_expr (e);
2058
2059 /* Ignore errors to this point, if we've gotten here. This means
2060 we ignore the m=MATCH_ERROR from above. */
2061 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2062 {
2063 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2064 m = MATCH_ERROR;
2065 }
2066 else
2067 /* All tests passed. */
2068 m = MATCH_YES;
2069
2070 if (m == MATCH_ERROR)
2071 gfc_current_locus = where;
2072
2073 /* Return what we know from the test(s). */
2074 return m;
2075
2076no_match:
2077 gfc_free_expr (e);
2078 gfc_current_locus = where;
2079 return m;
2080}
2081
6de9cd9a
DN
2082/* Match the various kind/length specifications in a CHARACTER
2083 declaration. We don't return MATCH_NO. */
2084
2085static match
636dff67 2086match_char_spec (gfc_typespec *ts)
6de9cd9a 2087{
187de1ed 2088 int kind, seen_length, is_iso_c;
6de9cd9a
DN
2089 gfc_charlen *cl;
2090 gfc_expr *len;
2091 match m;
187de1ed 2092
6de9cd9a
DN
2093 len = NULL;
2094 seen_length = 0;
187de1ed
FXC
2095 kind = 0;
2096 is_iso_c = 0;
6de9cd9a
DN
2097
2098 /* Try the old-style specification first. */
2099 old_char_selector = 0;
2100
2101 m = match_char_length (&len);
2102 if (m != MATCH_NO)
2103 {
2104 if (m == MATCH_YES)
2105 old_char_selector = 1;
2106 seen_length = 1;
2107 goto done;
2108 }
2109
2110 m = gfc_match_char ('(');
2111 if (m != MATCH_YES)
2112 {
a8b3b0b6 2113 m = MATCH_YES; /* Character without length is a single char. */
6de9cd9a
DN
2114 goto done;
2115 }
2116
a8b3b0b6 2117 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
6de9cd9a
DN
2118 if (gfc_match (" kind =") == MATCH_YES)
2119 {
187de1ed 2120 m = match_char_kind (&kind, &is_iso_c);
a8b3b0b6 2121
6de9cd9a
DN
2122 if (m == MATCH_ERROR)
2123 goto done;
2124 if (m == MATCH_NO)
2125 goto syntax;
2126
2127 if (gfc_match (" , len =") == MATCH_NO)
2128 goto rparen;
2129
2130 m = char_len_param_value (&len);
2131 if (m == MATCH_NO)
2132 goto syntax;
2133 if (m == MATCH_ERROR)
2134 goto done;
2135 seen_length = 1;
2136
2137 goto rparen;
2138 }
2139
66e4ab31 2140 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
6de9cd9a
DN
2141 if (gfc_match (" len =") == MATCH_YES)
2142 {
2143 m = char_len_param_value (&len);
2144 if (m == MATCH_NO)
2145 goto syntax;
2146 if (m == MATCH_ERROR)
2147 goto done;
2148 seen_length = 1;
2149
2150 if (gfc_match_char (')') == MATCH_YES)
2151 goto done;
2152
2153 if (gfc_match (" , kind =") != MATCH_YES)
2154 goto syntax;
2155
187de1ed
FXC
2156 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2157 goto done;
6de9cd9a
DN
2158
2159 goto rparen;
2160 }
2161
66e4ab31 2162 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
6de9cd9a
DN
2163 m = char_len_param_value (&len);
2164 if (m == MATCH_NO)
2165 goto syntax;
2166 if (m == MATCH_ERROR)
2167 goto done;
2168 seen_length = 1;
2169
2170 m = gfc_match_char (')');
2171 if (m == MATCH_YES)
2172 goto done;
2173
2174 if (gfc_match_char (',') != MATCH_YES)
2175 goto syntax;
2176
a8b3b0b6 2177 gfc_match (" kind ="); /* Gobble optional text. */
6de9cd9a 2178
187de1ed 2179 m = match_char_kind (&kind, &is_iso_c);
6de9cd9a
DN
2180 if (m == MATCH_ERROR)
2181 goto done;
2182 if (m == MATCH_NO)
2183 goto syntax;
2184
2185rparen:
2186 /* Require a right-paren at this point. */
2187 m = gfc_match_char (')');
2188 if (m == MATCH_YES)
2189 goto done;
2190
2191syntax:
2192 gfc_error ("Syntax error in CHARACTER declaration at %C");
2193 m = MATCH_ERROR;
16f8ffc8
JD
2194 gfc_free_expr (len);
2195 return m;
6de9cd9a
DN
2196
2197done:
a99d95a2
PT
2198 /* Deal with character functions after USE and IMPORT statements. */
2199 if (gfc_matching_function)
1c8bcdf7 2200 {
a99d95a2 2201 gfc_free_expr (len);
1c8bcdf7
PT
2202 gfc_undo_symbols ();
2203 return MATCH_YES;
2204 }
2205
6de9cd9a
DN
2206 if (m != MATCH_YES)
2207 {
2208 gfc_free_expr (len);
2209 return m;
2210 }
2211
2212 /* Do some final massaging of the length values. */
2213 cl = gfc_get_charlen ();
2214 cl->next = gfc_current_ns->cl_list;
2215 gfc_current_ns->cl_list = cl;
2216
2217 if (seen_length == 0)
2218 cl->length = gfc_int_expr (1);
2219 else
5cd09fac 2220 cl->length = len;
6de9cd9a
DN
2221
2222 ts->cl = cl;
187de1ed 2223 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
6de9cd9a 2224
a8b3b0b6
CR
2225 /* We have to know if it was a c interoperable kind so we can
2226 do accurate type checking of bind(c) procs, etc. */
187de1ed
FXC
2227 if (kind != 0)
2228 /* Mark this as c interoperable if being declared with one
2229 of the named constants from iso_c_binding. */
2230 ts->is_c_interop = is_iso_c;
a8b3b0b6 2231 else if (len != NULL)
187de1ed
FXC
2232 /* Here, we might have parsed something such as: character(c_char)
2233 In this case, the parsing code above grabs the c_char when
2234 looking for the length (line 1690, roughly). it's the last
2235 testcase for parsing the kind params of a character variable.
2236 However, it's not actually the length. this seems like it
2237 could be an error.
2238 To see if the user used a C interop kind, test the expr
2239 of the so called length, and see if it's C interoperable. */
2240 ts->is_c_interop = len->ts.is_iso_c;
a8b3b0b6 2241
6de9cd9a
DN
2242 return MATCH_YES;
2243}
2244
2245
2246/* Matches a type specification. If successful, sets the ts structure
2247 to the matched specification. This is necessary for FUNCTION and
2248 IMPLICIT statements.
2249
d51347f9 2250 If implicit_flag is nonzero, then we don't check for the optional
e5ddaa24 2251 kind specification. Not doing so is needed for matching an IMPLICIT
6de9cd9a
DN
2252 statement correctly. */
2253
e2d29968
PT
2254match
2255gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
6de9cd9a
DN
2256{
2257 char name[GFC_MAX_SYMBOL_LEN + 1];
2258 gfc_symbol *sym;
2259 match m;
8fc541d3 2260 char c;
1c8bcdf7 2261 bool seen_deferred_kind;
6de9cd9a 2262
1c8bcdf7
PT
2263 /* A belt and braces check that the typespec is correctly being treated
2264 as a deferred characteristic association. */
2265 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
a99d95a2
PT
2266 && (gfc_current_block ()->result->ts.kind == -1)
2267 && (ts->kind == -1);
6de9cd9a 2268 gfc_clear_ts (ts);
1c8bcdf7
PT
2269 if (seen_deferred_kind)
2270 ts->kind = -1;
6de9cd9a 2271
a8b3b0b6
CR
2272 /* Clear the current binding label, in case one is given. */
2273 curr_binding_label[0] = '\0';
2274
5f700e6d
AL
2275 if (gfc_match (" byte") == MATCH_YES)
2276 {
d51347f9 2277 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
5f700e6d
AL
2278 == FAILURE)
2279 return MATCH_ERROR;
2280
2281 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2282 {
2283 gfc_error ("BYTE type used at %C "
2284 "is not available on the target machine");
2285 return MATCH_ERROR;
2286 }
d51347f9 2287
5f700e6d
AL
2288 ts->type = BT_INTEGER;
2289 ts->kind = 1;
2290 return MATCH_YES;
2291 }
2292
6de9cd9a
DN
2293 if (gfc_match (" integer") == MATCH_YES)
2294 {
2295 ts->type = BT_INTEGER;
9d64df18 2296 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
2297 goto get_kind;
2298 }
2299
2300 if (gfc_match (" character") == MATCH_YES)
2301 {
2302 ts->type = BT_CHARACTER;
e5ddaa24
TS
2303 if (implicit_flag == 0)
2304 return match_char_spec (ts);
2305 else
2306 return MATCH_YES;
6de9cd9a
DN
2307 }
2308
2309 if (gfc_match (" real") == MATCH_YES)
2310 {
2311 ts->type = BT_REAL;
9d64df18 2312 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
2313 goto get_kind;
2314 }
2315
2316 if (gfc_match (" double precision") == MATCH_YES)
2317 {
2318 ts->type = BT_REAL;
9d64df18 2319 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2320 return MATCH_YES;
2321 }
2322
2323 if (gfc_match (" complex") == MATCH_YES)
2324 {
2325 ts->type = BT_COMPLEX;
9d64df18 2326 ts->kind = gfc_default_complex_kind;
6de9cd9a
DN
2327 goto get_kind;
2328 }
2329
2330 if (gfc_match (" double complex") == MATCH_YES)
2331 {
df8652dc
SK
2332 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2333 "conform to the Fortran 95 standard") == FAILURE)
2334 return MATCH_ERROR;
2335
6de9cd9a 2336 ts->type = BT_COMPLEX;
9d64df18 2337 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2338 return MATCH_YES;
2339 }
2340
2341 if (gfc_match (" logical") == MATCH_YES)
2342 {
2343 ts->type = BT_LOGICAL;
9d64df18 2344 ts->kind = gfc_default_logical_kind;
6de9cd9a
DN
2345 goto get_kind;
2346 }
2347
2348 m = gfc_match (" type ( %n )", name);
2349 if (m != MATCH_YES)
2350 return m;
2351
1c8bcdf7
PT
2352 ts->type = BT_DERIVED;
2353
2354 /* Defer association of the derived type until the end of the
2355 specification block. However, if the derived type can be
2356 found, add it to the typespec. */
2357 if (gfc_matching_function)
e2d29968 2358 {
1c8bcdf7
PT
2359 ts->derived = NULL;
2360 if (gfc_current_state () != COMP_INTERFACE
2361 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2362 ts->derived = sym;
e2d29968
PT
2363 return MATCH_YES;
2364 }
2365
2366 /* Search for the name but allow the components to be defined later. If
2367 type = -1, this typespec has been seen in a function declaration but
1c8bcdf7
PT
2368 the type could not be accessed at that point. */
2369 sym = NULL;
e2d29968 2370 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
6de9cd9a
DN
2371 {
2372 gfc_error ("Type name '%s' at %C is ambiguous", name);
2373 return MATCH_ERROR;
2374 }
e2d29968
PT
2375 else if (ts->kind == -1)
2376 {
1c8bcdf7
PT
2377 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2378 || gfc_current_ns->has_import_set;
2379 if (gfc_find_symbol (name, NULL, iface, &sym))
e2d29968
PT
2380 {
2381 gfc_error ("Type name '%s' at %C is ambiguous", name);
2382 return MATCH_ERROR;
2383 }
2384
1c8bcdf7 2385 ts->kind = 0;
e2d29968
PT
2386 if (sym == NULL)
2387 return MATCH_NO;
2388 }
6de9cd9a
DN
2389
2390 if (sym->attr.flavor != FL_DERIVED
231b2fcc 2391 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2392 return MATCH_ERROR;
2393
1c8bcdf7 2394 gfc_set_sym_referenced (sym);
6de9cd9a
DN
2395 ts->derived = sym;
2396
2397 return MATCH_YES;
2398
2399get_kind:
2400 /* For all types except double, derived and character, look for an
2401 optional kind specifier. MATCH_NO is actually OK at this point. */
e5ddaa24 2402 if (implicit_flag == 1)
6de9cd9a
DN
2403 return MATCH_YES;
2404
0ff0dfbf
TS
2405 if (gfc_current_form == FORM_FREE)
2406 {
8fc541d3 2407 c = gfc_peek_ascii_char();
0ff0dfbf 2408 if (!gfc_is_whitespace(c) && c != '*' && c != '('
636dff67 2409 && c != ':' && c != ',')
0ff0dfbf
TS
2410 return MATCH_NO;
2411 }
2412
e2d29968 2413 m = gfc_match_kind_spec (ts, false);
6de9cd9a
DN
2414 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2415 m = gfc_match_old_kind_spec (ts);
2416
1c8bcdf7
PT
2417 /* Defer association of the KIND expression of function results
2418 until after USE and IMPORT statements. */
2419 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2420 || gfc_matching_function)
2421 return MATCH_YES;
2422
6de9cd9a
DN
2423 if (m == MATCH_NO)
2424 m = MATCH_YES; /* No kind specifier found. */
2425
2426 return m;
2427}
2428
2429
e5ddaa24
TS
2430/* Match an IMPLICIT NONE statement. Actually, this statement is
2431 already matched in parse.c, or we would not end up here in the
2432 first place. So the only thing we need to check, is if there is
2433 trailing garbage. If not, the match is successful. */
2434
2435match
2436gfc_match_implicit_none (void)
2437{
e5ddaa24
TS
2438 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2439}
2440
2441
2442/* Match the letter range(s) of an IMPLICIT statement. */
2443
2444static match
1107b970 2445match_implicit_range (void)
e5ddaa24 2446{
8fc541d3
FXC
2447 char c, c1, c2;
2448 int inner;
e5ddaa24
TS
2449 locus cur_loc;
2450
2451 cur_loc = gfc_current_locus;
2452
2453 gfc_gobble_whitespace ();
8fc541d3 2454 c = gfc_next_ascii_char ();
e5ddaa24
TS
2455 if (c != '(')
2456 {
2457 gfc_error ("Missing character range in IMPLICIT at %C");
2458 goto bad;
2459 }
2460
2461 inner = 1;
2462 while (inner)
2463 {
2464 gfc_gobble_whitespace ();
8fc541d3 2465 c1 = gfc_next_ascii_char ();
e5ddaa24
TS
2466 if (!ISALPHA (c1))
2467 goto bad;
2468
2469 gfc_gobble_whitespace ();
8fc541d3 2470 c = gfc_next_ascii_char ();
e5ddaa24
TS
2471
2472 switch (c)
2473 {
2474 case ')':
66e4ab31 2475 inner = 0; /* Fall through. */
e5ddaa24
TS
2476
2477 case ',':
2478 c2 = c1;
2479 break;
2480
2481 case '-':
2482 gfc_gobble_whitespace ();
8fc541d3 2483 c2 = gfc_next_ascii_char ();
e5ddaa24
TS
2484 if (!ISALPHA (c2))
2485 goto bad;
2486
2487 gfc_gobble_whitespace ();
8fc541d3 2488 c = gfc_next_ascii_char ();
e5ddaa24
TS
2489
2490 if ((c != ',') && (c != ')'))
2491 goto bad;
2492 if (c == ')')
2493 inner = 0;
2494
2495 break;
2496
2497 default:
2498 goto bad;
2499 }
2500
2501 if (c1 > c2)
2502 {
2503 gfc_error ("Letters must be in alphabetic order in "
2504 "IMPLICIT statement at %C");
2505 goto bad;
2506 }
2507
2508 /* See if we can add the newly matched range to the pending
636dff67
SK
2509 implicits from this IMPLICIT statement. We do not check for
2510 conflicts with whatever earlier IMPLICIT statements may have
2511 set. This is done when we've successfully finished matching
2512 the current one. */
1107b970 2513 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
e5ddaa24
TS
2514 goto bad;
2515 }
2516
2517 return MATCH_YES;
2518
2519bad:
2520 gfc_syntax_error (ST_IMPLICIT);
2521
2522 gfc_current_locus = cur_loc;
2523 return MATCH_ERROR;
2524}
2525
2526
2527/* Match an IMPLICIT statement, storing the types for
2528 gfc_set_implicit() if the statement is accepted by the parser.
2529 There is a strange looking, but legal syntactic construction
2530 possible. It looks like:
2531
2532 IMPLICIT INTEGER (a-b) (c-d)
2533
2534 This is legal if "a-b" is a constant expression that happens to
2535 equal one of the legal kinds for integers. The real problem
2536 happens with an implicit specification that looks like:
2537
2538 IMPLICIT INTEGER (a-b)
2539
2540 In this case, a typespec matcher that is "greedy" (as most of the
2541 matchers are) gobbles the character range as a kindspec, leaving
2542 nothing left. We therefore have to go a bit more slowly in the
2543 matching process by inhibiting the kindspec checking during
2544 typespec matching and checking for a kind later. */
2545
2546match
2547gfc_match_implicit (void)
2548{
2549 gfc_typespec ts;
2550 locus cur_loc;
8fc541d3 2551 char c;
e5ddaa24
TS
2552 match m;
2553
44000dbb
JD
2554 gfc_clear_ts (&ts);
2555
e5ddaa24
TS
2556 /* We don't allow empty implicit statements. */
2557 if (gfc_match_eos () == MATCH_YES)
2558 {
2559 gfc_error ("Empty IMPLICIT statement at %C");
2560 return MATCH_ERROR;
2561 }
2562
e5ddaa24
TS
2563 do
2564 {
1107b970
PB
2565 /* First cleanup. */
2566 gfc_clear_new_implicit ();
2567
e5ddaa24 2568 /* A basic type is mandatory here. */
e2d29968 2569 m = gfc_match_type_spec (&ts, 1);
e5ddaa24
TS
2570 if (m == MATCH_ERROR)
2571 goto error;
2572 if (m == MATCH_NO)
2573 goto syntax;
2574
2575 cur_loc = gfc_current_locus;
1107b970 2576 m = match_implicit_range ();
e5ddaa24
TS
2577
2578 if (m == MATCH_YES)
2579 {
1107b970 2580 /* We may have <TYPE> (<RANGE>). */
e5ddaa24 2581 gfc_gobble_whitespace ();
8fc541d3 2582 c = gfc_next_ascii_char ();
e5ddaa24 2583 if ((c == '\n') || (c == ','))
1107b970
PB
2584 {
2585 /* Check for CHARACTER with no length parameter. */
2586 if (ts.type == BT_CHARACTER && !ts.cl)
2587 {
9d64df18 2588 ts.kind = gfc_default_character_kind;
1107b970
PB
2589 ts.cl = gfc_get_charlen ();
2590 ts.cl->next = gfc_current_ns->cl_list;
2591 gfc_current_ns->cl_list = ts.cl;
2592 ts.cl->length = gfc_int_expr (1);
2593 }
2594
2595 /* Record the Successful match. */
2596 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2597 return MATCH_ERROR;
2598 continue;
2599 }
e5ddaa24
TS
2600
2601 gfc_current_locus = cur_loc;
2602 }
2603
1107b970
PB
2604 /* Discard the (incorrectly) matched range. */
2605 gfc_clear_new_implicit ();
2606
2607 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2608 if (ts.type == BT_CHARACTER)
2609 m = match_char_spec (&ts);
2610 else
e5ddaa24 2611 {
e2d29968 2612 m = gfc_match_kind_spec (&ts, false);
e5ddaa24 2613 if (m == MATCH_NO)
1107b970
PB
2614 {
2615 m = gfc_match_old_kind_spec (&ts);
2616 if (m == MATCH_ERROR)
2617 goto error;
2618 if (m == MATCH_NO)
2619 goto syntax;
2620 }
e5ddaa24 2621 }
1107b970
PB
2622 if (m == MATCH_ERROR)
2623 goto error;
e5ddaa24 2624
1107b970 2625 m = match_implicit_range ();
e5ddaa24
TS
2626 if (m == MATCH_ERROR)
2627 goto error;
2628 if (m == MATCH_NO)
2629 goto syntax;
2630
2631 gfc_gobble_whitespace ();
8fc541d3 2632 c = gfc_next_ascii_char ();
e5ddaa24
TS
2633 if ((c != '\n') && (c != ','))
2634 goto syntax;
2635
1107b970
PB
2636 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2637 return MATCH_ERROR;
e5ddaa24
TS
2638 }
2639 while (c == ',');
2640
1107b970 2641 return MATCH_YES;
e5ddaa24
TS
2642
2643syntax:
2644 gfc_syntax_error (ST_IMPLICIT);
2645
2646error:
2647 return MATCH_ERROR;
2648}
2649
66e4ab31 2650
8998be20
TB
2651match
2652gfc_match_import (void)
2653{
2654 char name[GFC_MAX_SYMBOL_LEN + 1];
2655 match m;
2656 gfc_symbol *sym;
2657 gfc_symtree *st;
2658
66e4ab31
SK
2659 if (gfc_current_ns->proc_name == NULL
2660 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
8998be20
TB
2661 {
2662 gfc_error ("IMPORT statement at %C only permitted in "
2663 "an INTERFACE body");
2664 return MATCH_ERROR;
2665 }
2666
636dff67 2667 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
8998be20
TB
2668 == FAILURE)
2669 return MATCH_ERROR;
2670
2671 if (gfc_match_eos () == MATCH_YES)
2672 {
2673 /* All host variables should be imported. */
2674 gfc_current_ns->has_import_set = 1;
2675 return MATCH_YES;
2676 }
2677
2678 if (gfc_match (" ::") == MATCH_YES)
2679 {
2680 if (gfc_match_eos () == MATCH_YES)
636dff67
SK
2681 {
2682 gfc_error ("Expecting list of named entities at %C");
2683 return MATCH_ERROR;
2684 }
8998be20
TB
2685 }
2686
2687 for(;;)
2688 {
2689 m = gfc_match (" %n", name);
2690 switch (m)
2691 {
2692 case MATCH_YES:
36d3fb4c 2693 if (gfc_current_ns->parent != NULL
66e4ab31 2694 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
36d3fb4c
PT
2695 {
2696 gfc_error ("Type name '%s' at %C is ambiguous", name);
2697 return MATCH_ERROR;
2698 }
2699 else if (gfc_current_ns->proc_name->ns->parent != NULL
66e4ab31
SK
2700 && gfc_find_symbol (name,
2701 gfc_current_ns->proc_name->ns->parent,
2702 1, &sym))
636dff67
SK
2703 {
2704 gfc_error ("Type name '%s' at %C is ambiguous", name);
2705 return MATCH_ERROR;
2706 }
2707
2708 if (sym == NULL)
2709 {
2710 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2711 "at %C - does not exist.", name);
2712 return MATCH_ERROR;
2713 }
2714
d51347f9 2715 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
636dff67
SK
2716 {
2717 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2718 "at %C.", name);
2719 goto next_item;
2720 }
2721
2722 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2723 st->n.sym = sym;
2724 sym->refs++;
5a8af0b4 2725 sym->attr.imported = 1;
8998be20
TB
2726
2727 goto next_item;
2728
2729 case MATCH_NO:
2730 break;
2731
2732 case MATCH_ERROR:
2733 return MATCH_ERROR;
2734 }
2735
2736 next_item:
2737 if (gfc_match_eos () == MATCH_YES)
2738 break;
2739 if (gfc_match_char (',') != MATCH_YES)
2740 goto syntax;
2741 }
2742
2743 return MATCH_YES;
2744
2745syntax:
2746 gfc_error ("Syntax error in IMPORT statement at %C");
2747 return MATCH_ERROR;
2748}
e5ddaa24 2749
66e4ab31 2750
f2449db4
RS
2751/* A minimal implementation of gfc_match without whitespace, escape
2752 characters or variable arguments. Returns true if the next
2753 characters match the TARGET template exactly. */
2754
2755static bool
2756match_string_p (const char *target)
2757{
2758 const char *p;
2759
2760 for (p = target; *p; p++)
8fc541d3 2761 if ((char) gfc_next_ascii_char () != *p)
f2449db4
RS
2762 return false;
2763 return true;
2764}
2765
6de9cd9a
DN
2766/* Matches an attribute specification including array specs. If
2767 successful, leaves the variables current_attr and current_as
2768 holding the specification. Also sets the colon_seen variable for
2769 later use by matchers associated with initializations.
2770
2771 This subroutine is a little tricky in the sense that we don't know
2772 if we really have an attr-spec until we hit the double colon.
2773 Until that time, we can only return MATCH_NO. This forces us to
2774 check for duplicate specification at this level. */
2775
2776static match
2777match_attr_spec (void)
2778{
6de9cd9a
DN
2779 /* Modifiers that can exist in a type statement. */
2780 typedef enum
2781 { GFC_DECL_BEGIN = 0,
2782 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2783 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
ee7e677f
TB
2784 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2785 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
f2449db4 2786 DECL_IS_BIND_C, DECL_NONE,
6de9cd9a
DN
2787 GFC_DECL_END /* Sentinel */
2788 }
2789 decl_types;
2790
2791/* GFC_DECL_END is the sentinel, index starts at 0. */
2792#define NUM_DECL GFC_DECL_END
2793
6de9cd9a
DN
2794 locus start, seen_at[NUM_DECL];
2795 int seen[NUM_DECL];
2796 decl_types d;
2797 const char *attr;
2798 match m;
17b1d2a0 2799 gfc_try t;
6de9cd9a
DN
2800
2801 gfc_clear_attr (&current_attr);
63645982 2802 start = gfc_current_locus;
6de9cd9a
DN
2803
2804 current_as = NULL;
2805 colon_seen = 0;
2806
2807 /* See if we get all of the keywords up to the final double colon. */
2808 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2809 seen[d] = 0;
2810
2811 for (;;)
2812 {
8fc541d3 2813 char ch;
a8b3b0b6 2814
f2449db4
RS
2815 d = DECL_NONE;
2816 gfc_gobble_whitespace ();
2817
8fc541d3 2818 ch = gfc_next_ascii_char ();
f2449db4
RS
2819 if (ch == ':')
2820 {
2821 /* This is the successful exit condition for the loop. */
8fc541d3 2822 if (gfc_next_ascii_char () == ':')
f2449db4
RS
2823 break;
2824 }
2825 else if (ch == ',')
a8b3b0b6 2826 {
a8b3b0b6 2827 gfc_gobble_whitespace ();
8fc541d3 2828 switch (gfc_peek_ascii_char ())
a8b3b0b6 2829 {
f2449db4
RS
2830 case 'a':
2831 if (match_string_p ("allocatable"))
2832 d = DECL_ALLOCATABLE;
2833 break;
2834
2835 case 'b':
a8b3b0b6 2836 /* Try and match the bind(c). */
1eabf70a 2837 m = gfc_match_bind_c (NULL, true);
129d15a3 2838 if (m == MATCH_YES)
a8b3b0b6 2839 d = DECL_IS_BIND_C;
129d15a3
JW
2840 else if (m == MATCH_ERROR)
2841 goto cleanup;
f2449db4
RS
2842 break;
2843
2844 case 'd':
2845 if (match_string_p ("dimension"))
2846 d = DECL_DIMENSION;
2847 break;
2848
2849 case 'e':
2850 if (match_string_p ("external"))
2851 d = DECL_EXTERNAL;
2852 break;
2853
2854 case 'i':
2855 if (match_string_p ("int"))
2856 {
8fc541d3 2857 ch = gfc_next_ascii_char ();
f2449db4
RS
2858 if (ch == 'e')
2859 {
2860 if (match_string_p ("nt"))
2861 {
2862 /* Matched "intent". */
2863 /* TODO: Call match_intent_spec from here. */
2864 if (gfc_match (" ( in out )") == MATCH_YES)
2865 d = DECL_INOUT;
2866 else if (gfc_match (" ( in )") == MATCH_YES)
2867 d = DECL_IN;
2868 else if (gfc_match (" ( out )") == MATCH_YES)
2869 d = DECL_OUT;
2870 }
2871 }
2872 else if (ch == 'r')
2873 {
2874 if (match_string_p ("insic"))
2875 {
2876 /* Matched "intrinsic". */
2877 d = DECL_INTRINSIC;
2878 }
2879 }
2880 }
2881 break;
2882
2883 case 'o':
2884 if (match_string_p ("optional"))
2885 d = DECL_OPTIONAL;
2886 break;
2887
2888 case 'p':
8fc541d3
FXC
2889 gfc_next_ascii_char ();
2890 switch (gfc_next_ascii_char ())
f2449db4
RS
2891 {
2892 case 'a':
2893 if (match_string_p ("rameter"))
2894 {
2895 /* Matched "parameter". */
2896 d = DECL_PARAMETER;
2897 }
2898 break;
2899
2900 case 'o':
2901 if (match_string_p ("inter"))
2902 {
2903 /* Matched "pointer". */
2904 d = DECL_POINTER;
2905 }
2906 break;
2907
2908 case 'r':
8fc541d3 2909 ch = gfc_next_ascii_char ();
f2449db4
RS
2910 if (ch == 'i')
2911 {
2912 if (match_string_p ("vate"))
2913 {
2914 /* Matched "private". */
2915 d = DECL_PRIVATE;
2916 }
2917 }
2918 else if (ch == 'o')
2919 {
2920 if (match_string_p ("tected"))
2921 {
2922 /* Matched "protected". */
2923 d = DECL_PROTECTED;
2924 }
2925 }
2926 break;
2927
2928 case 'u':
2929 if (match_string_p ("blic"))
2930 {
2931 /* Matched "public". */
2932 d = DECL_PUBLIC;
2933 }
2934 break;
2935 }
2936 break;
2937
2938 case 's':
2939 if (match_string_p ("save"))
2940 d = DECL_SAVE;
2941 break;
2942
2943 case 't':
2944 if (match_string_p ("target"))
2945 d = DECL_TARGET;
2946 break;
2947
2948 case 'v':
8fc541d3
FXC
2949 gfc_next_ascii_char ();
2950 ch = gfc_next_ascii_char ();
f2449db4
RS
2951 if (ch == 'a')
2952 {
2953 if (match_string_p ("lue"))
2954 {
2955 /* Matched "value". */
2956 d = DECL_VALUE;
2957 }
2958 }
2959 else if (ch == 'o')
2960 {
2961 if (match_string_p ("latile"))
2962 {
2963 /* Matched "volatile". */
2964 d = DECL_VOLATILE;
2965 }
2966 }
2967 break;
a8b3b0b6
CR
2968 }
2969 }
d468bcdb 2970
f2449db4
RS
2971 /* No double colon and no recognizable decl_type, so assume that
2972 we've been looking at something else the whole time. */
2973 if (d == DECL_NONE)
2974 {
2975 m = MATCH_NO;
2976 goto cleanup;
2977 }
d51347f9 2978
acb388a0
JD
2979 /* Check to make sure any parens are paired up correctly. */
2980 if (gfc_match_parens () == MATCH_ERROR)
2981 {
2982 m = MATCH_ERROR;
2983 goto cleanup;
2984 }
2985
6de9cd9a 2986 seen[d]++;
63645982 2987 seen_at[d] = gfc_current_locus;
6de9cd9a
DN
2988
2989 if (d == DECL_DIMENSION)
2990 {
2991 m = gfc_match_array_spec (&current_as);
2992
2993 if (m == MATCH_NO)
2994 {
2995 gfc_error ("Missing dimension specification at %C");
2996 m = MATCH_ERROR;
2997 }
2998
2999 if (m == MATCH_ERROR)
3000 goto cleanup;
3001 }
3002 }
3003
6de9cd9a
DN
3004 /* Since we've seen a double colon, we have to be looking at an
3005 attr-spec. This means that we can now issue errors. */
3006 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3007 if (seen[d] > 1)
3008 {
3009 switch (d)
3010 {
3011 case DECL_ALLOCATABLE:
3012 attr = "ALLOCATABLE";
3013 break;
3014 case DECL_DIMENSION:
3015 attr = "DIMENSION";
3016 break;
3017 case DECL_EXTERNAL:
3018 attr = "EXTERNAL";
3019 break;
3020 case DECL_IN:
3021 attr = "INTENT (IN)";
3022 break;
3023 case DECL_OUT:
3024 attr = "INTENT (OUT)";
3025 break;
3026 case DECL_INOUT:
3027 attr = "INTENT (IN OUT)";
3028 break;
3029 case DECL_INTRINSIC:
3030 attr = "INTRINSIC";
3031 break;
3032 case DECL_OPTIONAL:
3033 attr = "OPTIONAL";
3034 break;
3035 case DECL_PARAMETER:
3036 attr = "PARAMETER";
3037 break;
3038 case DECL_POINTER:
3039 attr = "POINTER";
3040 break;
ee7e677f
TB
3041 case DECL_PROTECTED:
3042 attr = "PROTECTED";
3043 break;
6de9cd9a
DN
3044 case DECL_PRIVATE:
3045 attr = "PRIVATE";
3046 break;
3047 case DECL_PUBLIC:
3048 attr = "PUBLIC";
3049 break;
3050 case DECL_SAVE:
3051 attr = "SAVE";
3052 break;
3053 case DECL_TARGET:
3054 attr = "TARGET";
3055 break;
a8b3b0b6
CR
3056 case DECL_IS_BIND_C:
3057 attr = "IS_BIND_C";
3058 break;
3059 case DECL_VALUE:
3060 attr = "VALUE";
3061 break;
775e6c3a
TB
3062 case DECL_VOLATILE:
3063 attr = "VOLATILE";
3064 break;
6de9cd9a 3065 default:
66e4ab31 3066 attr = NULL; /* This shouldn't happen. */
6de9cd9a
DN
3067 }
3068
3069 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3070 m = MATCH_ERROR;
3071 goto cleanup;
3072 }
3073
3074 /* Now that we've dealt with duplicate attributes, add the attributes
3075 to the current attribute. */
3076 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3077 {
3078 if (seen[d] == 0)
3079 continue;
3080
3081 if (gfc_current_state () == COMP_DERIVED
3082 && d != DECL_DIMENSION && d != DECL_POINTER
f2449db4
RS
3083 && d != DECL_PRIVATE && d != DECL_PUBLIC
3084 && d != DECL_NONE)
6de9cd9a 3085 {
5046aff5
PT
3086 if (d == DECL_ALLOCATABLE)
3087 {
636dff67
SK
3088 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3089 "attribute at %C in a TYPE definition")
d51347f9 3090 == FAILURE)
5046aff5
PT
3091 {
3092 m = MATCH_ERROR;
3093 goto cleanup;
3094 }
636dff67
SK
3095 }
3096 else
5046aff5
PT
3097 {
3098 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
d51347f9 3099 &seen_at[d]);
5046aff5
PT
3100 m = MATCH_ERROR;
3101 goto cleanup;
3102 }
6de9cd9a
DN
3103 }
3104
4213f93b 3105 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
636dff67 3106 && gfc_current_state () != COMP_MODULE)
4213f93b
PT
3107 {
3108 if (d == DECL_PRIVATE)
3109 attr = "PRIVATE";
3110 else
3111 attr = "PUBLIC";
d51347f9
TB
3112 if (gfc_current_state () == COMP_DERIVED
3113 && gfc_state_stack->previous
3114 && gfc_state_stack->previous->state == COMP_MODULE)
3115 {
3116 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3117 "at %L in a TYPE definition", attr,
3118 &seen_at[d])
3119 == FAILURE)
3120 {
3121 m = MATCH_ERROR;
3122 goto cleanup;
3123 }
3124 }
3125 else
3126 {
3127 gfc_error ("%s attribute at %L is not allowed outside of the "
3128 "specification part of a module", attr, &seen_at[d]);
3129 m = MATCH_ERROR;
3130 goto cleanup;
3131 }
4213f93b
PT
3132 }
3133
6de9cd9a
DN
3134 switch (d)
3135 {
3136 case DECL_ALLOCATABLE:
3137 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3138 break;
3139
3140 case DECL_DIMENSION:
231b2fcc 3141 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
3142 break;
3143
3144 case DECL_EXTERNAL:
3145 t = gfc_add_external (&current_attr, &seen_at[d]);
3146 break;
3147
3148 case DECL_IN:
3149 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3150 break;
3151
3152 case DECL_OUT:
3153 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3154 break;
3155
3156 case DECL_INOUT:
3157 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3158 break;
3159
3160 case DECL_INTRINSIC:
3161 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3162 break;
3163
3164 case DECL_OPTIONAL:
3165 t = gfc_add_optional (&current_attr, &seen_at[d]);
3166 break;
3167
3168 case DECL_PARAMETER:
231b2fcc 3169 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
6de9cd9a
DN
3170 break;
3171
3172 case DECL_POINTER:
3173 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3174 break;
3175
ee7e677f
TB
3176 case DECL_PROTECTED:
3177 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3178 {
3179 gfc_error ("PROTECTED at %C only allowed in specification "
3180 "part of a module");
3181 t = FAILURE;
3182 break;
3183 }
3184
636dff67
SK
3185 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3186 "attribute at %C")
ee7e677f
TB
3187 == FAILURE)
3188 t = FAILURE;
3189 else
3190 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3191 break;
3192
6de9cd9a 3193 case DECL_PRIVATE:
231b2fcc
TS
3194 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3195 &seen_at[d]);
6de9cd9a
DN
3196 break;
3197
3198 case DECL_PUBLIC:
231b2fcc
TS
3199 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3200 &seen_at[d]);
6de9cd9a
DN
3201 break;
3202
3203 case DECL_SAVE:
231b2fcc 3204 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
3205 break;
3206
3207 case DECL_TARGET:
3208 t = gfc_add_target (&current_attr, &seen_at[d]);
3209 break;
3210
a8b3b0b6
CR
3211 case DECL_IS_BIND_C:
3212 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3213 break;
3214
06469efd 3215 case DECL_VALUE:
636dff67
SK
3216 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3217 "at %C")
06469efd
PT
3218 == FAILURE)
3219 t = FAILURE;
3220 else
3221 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3222 break;
3223
775e6c3a
TB
3224 case DECL_VOLATILE:
3225 if (gfc_notify_std (GFC_STD_F2003,
636dff67 3226 "Fortran 2003: VOLATILE attribute at %C")
775e6c3a
TB
3227 == FAILURE)
3228 t = FAILURE;
3229 else
3230 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3231 break;
3232
6de9cd9a
DN
3233 default:
3234 gfc_internal_error ("match_attr_spec(): Bad attribute");
3235 }
3236
3237 if (t == FAILURE)
3238 {
3239 m = MATCH_ERROR;
3240 goto cleanup;
3241 }
3242 }
3243
3244 colon_seen = 1;
3245 return MATCH_YES;
3246
3247cleanup:
63645982 3248 gfc_current_locus = start;
6de9cd9a
DN
3249 gfc_free_array_spec (current_as);
3250 current_as = NULL;
3251 return m;
3252}
3253
3254
a8b3b0b6
CR
3255/* Set the binding label, dest_label, either with the binding label
3256 stored in the given gfc_typespec, ts, or if none was provided, it
3257 will be the symbol name in all lower case, as required by the draft
3258 (J3/04-007, section 15.4.1). If a binding label was given and
3259 there is more than one argument (num_idents), it is an error. */
3260
17b1d2a0 3261gfc_try
a8b3b0b6
CR
3262set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3263{
ad4a2f64 3264 if (num_idents > 1 && has_name_equals)
a8b3b0b6 3265 {
ad4a2f64
TB
3266 gfc_error ("Multiple identifiers provided with "
3267 "single NAME= specifier at %C");
3268 return FAILURE;
3269 }
a8b3b0b6 3270
ad4a2f64
TB
3271 if (curr_binding_label[0] != '\0')
3272 {
a8b3b0b6 3273 /* Binding label given; store in temp holder til have sym. */
c5b5a17a 3274 strcpy (dest_label, curr_binding_label);
a8b3b0b6
CR
3275 }
3276 else
3277 {
3278 /* No binding label given, and the NAME= specifier did not exist,
3279 which means there was no NAME="". */
3280 if (sym_name != NULL && has_name_equals == 0)
c5b5a17a 3281 strcpy (dest_label, sym_name);
a8b3b0b6
CR
3282 }
3283
3284 return SUCCESS;
3285}
3286
3287
3288/* Set the status of the given common block as being BIND(C) or not,
3289 depending on the given parameter, is_bind_c. */
3290
3291void
3292set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3293{
3294 com_block->is_bind_c = is_bind_c;
3295 return;
3296}
3297
3298
3299/* Verify that the given gfc_typespec is for a C interoperable type. */
3300
17b1d2a0 3301gfc_try
a8b3b0b6
CR
3302verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3303{
17b1d2a0 3304 gfc_try t;
a8b3b0b6
CR
3305
3306 /* Make sure the kind used is appropriate for the type.
3307 The f90_type is unknown if an integer constant was
3308 used (e.g., real(4), bind(c) :: myFloat). */
3309 if (ts->f90_type != BT_UNKNOWN)
3310 {
3311 t = gfc_validate_c_kind (ts);
3312 if (t != SUCCESS)
3313 {
3314 /* Print an error, but continue parsing line. */
3315 gfc_error_now ("C kind parameter is for type %s but "
3316 "symbol '%s' at %L is of type %s",
3317 gfc_basic_typename (ts->f90_type),
3318 name, where,
3319 gfc_basic_typename (ts->type));
3320 }
3321 }
3322
3323 /* Make sure the kind is C interoperable. This does not care about the
3324 possible error above. */
3325 if (ts->type == BT_DERIVED && ts->derived != NULL)
3326 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3327 else if (ts->is_c_interop != 1)
3328 return FAILURE;
3329
3330 return SUCCESS;
3331}
3332
3333
3334/* Verify that the variables of a given common block, which has been
3335 defined with the attribute specifier bind(c), to be of a C
3336 interoperable type. Errors will be reported here, if
3337 encountered. */
3338
17b1d2a0 3339gfc_try
a8b3b0b6
CR
3340verify_com_block_vars_c_interop (gfc_common_head *com_block)
3341{
3342 gfc_symbol *curr_sym = NULL;
17b1d2a0 3343 gfc_try retval = SUCCESS;
a8b3b0b6
CR
3344
3345 curr_sym = com_block->head;
3346
3347 /* Make sure we have at least one symbol. */
3348 if (curr_sym == NULL)
3349 return retval;
3350
3351 /* Here we know we have a symbol, so we'll execute this loop
3352 at least once. */
3353 do
3354 {
3355 /* The second to last param, 1, says this is in a common block. */
3356 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3357 curr_sym = curr_sym->common_next;
3358 } while (curr_sym != NULL);
3359
3360 return retval;
3361}
3362
3363
3364/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3365 an appropriate error message is reported. */
3366
17b1d2a0 3367gfc_try
a8b3b0b6
CR
3368verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3369 int is_in_common, gfc_common_head *com_block)
3370{
8327f9c2 3371 bool bind_c_function = false;
17b1d2a0 3372 gfc_try retval = SUCCESS;
d8fa96e0 3373
8327f9c2
TB
3374 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3375 bind_c_function = true;
3376
d8fa96e0
CR
3377 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3378 {
3379 tmp_sym = tmp_sym->result;
3380 /* Make sure it wasn't an implicitly typed result. */
3381 if (tmp_sym->attr.implicit_type)
3382 {
3383 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3384 "%L may not be C interoperable", tmp_sym->name,
3385 &tmp_sym->declared_at);
3386 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3387 /* Mark it as C interoperable to prevent duplicate warnings. */
3388 tmp_sym->ts.is_c_interop = 1;
3389 tmp_sym->attr.is_c_interop = 1;
3390 }
3391 }
8327f9c2 3392
a8b3b0b6
CR
3393 /* Here, we know we have the bind(c) attribute, so if we have
3394 enough type info, then verify that it's a C interop kind.
3395 The info could be in the symbol already, or possibly still in
3396 the given ts (current_ts), so look in both. */
3397 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3398 {
3399 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3400 &(tmp_sym->declared_at)) != SUCCESS)
3401 {
3402 /* See if we're dealing with a sym in a common block or not. */
3403 if (is_in_common == 1)
3404 {
3405 gfc_warning ("Variable '%s' in common block '%s' at %L "
3406 "may not be a C interoperable "
3407 "kind though common block '%s' is BIND(C)",
3408 tmp_sym->name, com_block->name,
3409 &(tmp_sym->declared_at), com_block->name);
3410 }
3411 else
3412 {
3413 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3414 gfc_error ("Type declaration '%s' at %L is not C "
3415 "interoperable but it is BIND(C)",
3416 tmp_sym->name, &(tmp_sym->declared_at));
3417 else
3418 gfc_warning ("Variable '%s' at %L "
3419 "may not be a C interoperable "
3420 "kind but it is bind(c)",
3421 tmp_sym->name, &(tmp_sym->declared_at));
3422 }
3423 }
3424
3425 /* Variables declared w/in a common block can't be bind(c)
3426 since there's no way for C to see these variables, so there's
3427 semantically no reason for the attribute. */
3428 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3429 {
3430 gfc_error ("Variable '%s' in common block '%s' at "
3431 "%L cannot be declared with BIND(C) "
3432 "since it is not a global",
3433 tmp_sym->name, com_block->name,
3434 &(tmp_sym->declared_at));
3435 retval = FAILURE;
3436 }
3437
3438 /* Scalar variables that are bind(c) can not have the pointer
3439 or allocatable attributes. */
3440 if (tmp_sym->attr.is_bind_c == 1)
3441 {
3442 if (tmp_sym->attr.pointer == 1)
3443 {
3444 gfc_error ("Variable '%s' at %L cannot have both the "
3445 "POINTER and BIND(C) attributes",
3446 tmp_sym->name, &(tmp_sym->declared_at));
3447 retval = FAILURE;
3448 }
3449
3450 if (tmp_sym->attr.allocatable == 1)
3451 {
3452 gfc_error ("Variable '%s' at %L cannot have both the "
3453 "ALLOCATABLE and BIND(C) attributes",
3454 tmp_sym->name, &(tmp_sym->declared_at));
3455 retval = FAILURE;
3456 }
3457
8327f9c2
TB
3458 }
3459
3460 /* If it is a BIND(C) function, make sure the return value is a
3461 scalar value. The previous tests in this function made sure
3462 the type is interoperable. */
3463 if (bind_c_function && tmp_sym->as != NULL)
3464 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3465 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3466
3467 /* BIND(C) functions can not return a character string. */
3468 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3469 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3470 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3471 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3472 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
a8b3b0b6
CR
3473 "be a character string", tmp_sym->name,
3474 &(tmp_sym->declared_at));
a8b3b0b6
CR
3475 }
3476
3477 /* See if the symbol has been marked as private. If it has, make sure
3478 there is no binding label and warn the user if there is one. */
3479 if (tmp_sym->attr.access == ACCESS_PRIVATE
3480 && tmp_sym->binding_label[0] != '\0')
3481 /* Use gfc_warning_now because we won't say that the symbol fails
3482 just because of this. */
3483 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3484 "given the binding label '%s'", tmp_sym->name,
3485 &(tmp_sym->declared_at), tmp_sym->binding_label);
3486
3487 return retval;
3488}
3489
3490
3491/* Set the appropriate fields for a symbol that's been declared as
3492 BIND(C) (the is_bind_c flag and the binding label), and verify that
3493 the type is C interoperable. Errors are reported by the functions
3494 used to set/test these fields. */
3495
17b1d2a0 3496gfc_try
a8b3b0b6
CR
3497set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3498{
17b1d2a0 3499 gfc_try retval = SUCCESS;
a8b3b0b6
CR
3500
3501 /* TODO: Do we need to make sure the vars aren't marked private? */
3502
3503 /* Set the is_bind_c bit in symbol_attribute. */
3504 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3505
3506 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3507 num_idents) != SUCCESS)
3508 return FAILURE;
3509
3510 return retval;
3511}
3512
3513
3514/* Set the fields marking the given common block as BIND(C), including
3515 a binding label, and report any errors encountered. */
3516
17b1d2a0 3517gfc_try
a8b3b0b6
CR
3518set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3519{
17b1d2a0 3520 gfc_try retval = SUCCESS;
a8b3b0b6
CR
3521
3522 /* destLabel, common name, typespec (which may have binding label). */
3523 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3524 != SUCCESS)
3525 return FAILURE;
3526
3527 /* Set the given common block (com_block) to being bind(c) (1). */
3528 set_com_block_bind_c (com_block, 1);
3529
3530 return retval;
3531}
3532
3533
3534/* Retrieve the list of one or more identifiers that the given bind(c)
3535 attribute applies to. */
3536
17b1d2a0 3537gfc_try
a8b3b0b6
CR
3538get_bind_c_idents (void)
3539{
3540 char name[GFC_MAX_SYMBOL_LEN + 1];
3541 int num_idents = 0;
3542 gfc_symbol *tmp_sym = NULL;
3543 match found_id;
3544 gfc_common_head *com_block = NULL;
3545
3546 if (gfc_match_name (name) == MATCH_YES)
3547 {
3548 found_id = MATCH_YES;
3549 gfc_get_ha_symbol (name, &tmp_sym);
3550 }
3551 else if (match_common_name (name) == MATCH_YES)
3552 {
3553 found_id = MATCH_YES;
3554 com_block = gfc_get_common (name, 0);
3555 }
3556 else
3557 {
3558 gfc_error ("Need either entity or common block name for "
3559 "attribute specification statement at %C");
3560 return FAILURE;
3561 }
3562
3563 /* Save the current identifier and look for more. */
3564 do
3565 {
3566 /* Increment the number of identifiers found for this spec stmt. */
3567 num_idents++;
3568
3569 /* Make sure we have a sym or com block, and verify that it can
3570 be bind(c). Set the appropriate field(s) and look for more
3571 identifiers. */
3572 if (tmp_sym != NULL || com_block != NULL)
3573 {
3574 if (tmp_sym != NULL)
3575 {
3576 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3577 != SUCCESS)
3578 return FAILURE;
3579 }
3580 else
3581 {
3582 if (set_verify_bind_c_com_block(com_block, num_idents)
3583 != SUCCESS)
3584 return FAILURE;
3585 }
3586
3587 /* Look to see if we have another identifier. */
3588 tmp_sym = NULL;
3589 if (gfc_match_eos () == MATCH_YES)
3590 found_id = MATCH_NO;
3591 else if (gfc_match_char (',') != MATCH_YES)
3592 found_id = MATCH_NO;
3593 else if (gfc_match_name (name) == MATCH_YES)
3594 {
3595 found_id = MATCH_YES;
3596 gfc_get_ha_symbol (name, &tmp_sym);
3597 }
3598 else if (match_common_name (name) == MATCH_YES)
3599 {
3600 found_id = MATCH_YES;
3601 com_block = gfc_get_common (name, 0);
3602 }
3603 else
3604 {
3605 gfc_error ("Missing entity or common block name for "
3606 "attribute specification statement at %C");
3607 return FAILURE;
3608 }
3609 }
3610 else
3611 {
3612 gfc_internal_error ("Missing symbol");
3613 }
3614 } while (found_id == MATCH_YES);
3615
3616 /* if we get here we were successful */
3617 return SUCCESS;
3618}
3619
3620
3621/* Try and match a BIND(C) attribute specification statement. */
3622
3623match
3624gfc_match_bind_c_stmt (void)
3625{
3626 match found_match = MATCH_NO;
3627 gfc_typespec *ts;
3628
3629 ts = &current_ts;
3630
3631 /* This may not be necessary. */
3632 gfc_clear_ts (ts);
3633 /* Clear the temporary binding label holder. */
3634 curr_binding_label[0] = '\0';
3635
3636 /* Look for the bind(c). */
1eabf70a 3637 found_match = gfc_match_bind_c (NULL, true);
a8b3b0b6
CR
3638
3639 if (found_match == MATCH_YES)
3640 {
3641 /* Look for the :: now, but it is not required. */
3642 gfc_match (" :: ");
3643
3644 /* Get the identifier(s) that needs to be updated. This may need to
3645 change to hand the flag(s) for the attr specified so all identifiers
3646 found can have all appropriate parts updated (assuming that the same
3647 spec stmt can have multiple attrs, such as both bind(c) and
3648 allocatable...). */
3649 if (get_bind_c_idents () != SUCCESS)
3650 /* Error message should have printed already. */
3651 return MATCH_ERROR;
3652 }
3653
3654 return found_match;
3655}
3656
3657
6de9cd9a
DN
3658/* Match a data declaration statement. */
3659
3660match
3661gfc_match_data_decl (void)
3662{
3663 gfc_symbol *sym;
3664 match m;
949d5b72 3665 int elem;
6de9cd9a 3666
a8b3b0b6
CR
3667 num_idents_on_line = 0;
3668
e2d29968 3669 m = gfc_match_type_spec (&current_ts, 0);
6de9cd9a
DN
3670 if (m != MATCH_YES)
3671 return m;
3672
3673 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3674 {
3675 sym = gfc_use_derived (current_ts.derived);
3676
3677 if (sym == NULL)
3678 {
3679 m = MATCH_ERROR;
3680 goto cleanup;
3681 }
3682
3683 current_ts.derived = sym;
3684 }
3685
3686 m = match_attr_spec ();
3687 if (m == MATCH_ERROR)
3688 {
3689 m = MATCH_NO;
3690 goto cleanup;
3691 }
3692
9fa6b0af
FXC
3693 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3694 && !current_ts.derived->attr.zero_comp)
6de9cd9a
DN
3695 {
3696
3697 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3698 goto ok;
3699
976e21f6 3700 gfc_find_symbol (current_ts.derived->name,
636dff67 3701 current_ts.derived->ns->parent, 1, &sym);
6de9cd9a 3702
976e21f6 3703 /* Any symbol that we find had better be a type definition
636dff67 3704 which has its components defined. */
976e21f6 3705 if (sym != NULL && sym->attr.flavor == FL_DERIVED
9fa6b0af
FXC
3706 && (current_ts.derived->components != NULL
3707 || current_ts.derived->attr.zero_comp))
6de9cd9a
DN
3708 goto ok;
3709
976e21f6
PT
3710 /* Now we have an error, which we signal, and then fix up
3711 because the knock-on is plain and simple confusing. */
3712 gfc_error_now ("Derived type at %C has not been previously defined "
636dff67 3713 "and so cannot appear in a derived type definition");
976e21f6
PT
3714 current_attr.pointer = 1;
3715 goto ok;
6de9cd9a
DN
3716 }
3717
3718ok:
3719 /* If we have an old-style character declaration, and no new-style
3720 attribute specifications, then there a comma is optional between
3721 the type specification and the variable list. */
3722 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3723 gfc_match_char (',');
3724
949d5b72
PT
3725 /* Give the types/attributes to symbols that follow. Give the element
3726 a number so that repeat character length expressions can be copied. */
3727 elem = 1;
6de9cd9a
DN
3728 for (;;)
3729 {
a8b3b0b6 3730 num_idents_on_line++;
949d5b72 3731 m = variable_decl (elem++);
6de9cd9a
DN
3732 if (m == MATCH_ERROR)
3733 goto cleanup;
3734 if (m == MATCH_NO)
3735 break;
3736
3737 if (gfc_match_eos () == MATCH_YES)
3738 goto cleanup;
3739 if (gfc_match_char (',') != MATCH_YES)
3740 break;
3741 }
3742
8f81c3c6
PT
3743 if (gfc_error_flag_test () == 0)
3744 gfc_error ("Syntax error in data declaration at %C");
6de9cd9a
DN
3745 m = MATCH_ERROR;
3746
a9f6f1f2
JD
3747 gfc_free_data_all (gfc_current_ns);
3748
6de9cd9a
DN
3749cleanup:
3750 gfc_free_array_spec (current_as);
3751 current_as = NULL;
3752 return m;
3753}
3754
3755
3756/* Match a prefix associated with a function or subroutine
3757 declaration. If the typespec pointer is nonnull, then a typespec
3758 can be matched. Note that if nothing matches, MATCH_YES is
3759 returned (the null string was matched). */
3760
1c8bcdf7
PT
3761match
3762gfc_match_prefix (gfc_typespec *ts)
6de9cd9a 3763{
7389bce6 3764 bool seen_type;
6de9cd9a
DN
3765
3766 gfc_clear_attr (&current_attr);
3767 seen_type = 0;
3768
3df684e2
DK
3769 gcc_assert (!gfc_matching_prefix);
3770 gfc_matching_prefix = true;
f37e928c 3771
6de9cd9a
DN
3772loop:
3773 if (!seen_type && ts != NULL
e2d29968 3774 && gfc_match_type_spec (ts, 0) == MATCH_YES
6de9cd9a
DN
3775 && gfc_match_space () == MATCH_YES)
3776 {
3777
3778 seen_type = 1;
3779 goto loop;
3780 }
3781
3782 if (gfc_match ("elemental% ") == MATCH_YES)
3783 {
3784 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
f37e928c 3785 goto error;
6de9cd9a
DN
3786
3787 goto loop;
3788 }
3789
3790 if (gfc_match ("pure% ") == MATCH_YES)
3791 {
3792 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
f37e928c 3793 goto error;
6de9cd9a
DN
3794
3795 goto loop;
3796 }
3797
3798 if (gfc_match ("recursive% ") == MATCH_YES)
3799 {
3800 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
f37e928c 3801 goto error;
6de9cd9a
DN
3802
3803 goto loop;
3804 }
3805
3806 /* At this point, the next item is not a prefix. */
3df684e2
DK
3807 gcc_assert (gfc_matching_prefix);
3808 gfc_matching_prefix = false;
6de9cd9a 3809 return MATCH_YES;
f37e928c
DK
3810
3811error:
3df684e2
DK
3812 gcc_assert (gfc_matching_prefix);
3813 gfc_matching_prefix = false;
f37e928c 3814 return MATCH_ERROR;
6de9cd9a
DN
3815}
3816
3817
1c8bcdf7 3818/* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6de9cd9a 3819
17b1d2a0 3820static gfc_try
636dff67 3821copy_prefix (symbol_attribute *dest, locus *where)
6de9cd9a 3822{
6de9cd9a
DN
3823 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3824 return FAILURE;
3825
3826 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3827 return FAILURE;
3828
3829 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3830 return FAILURE;
3831
3832 return SUCCESS;
3833}
3834
3835
3836/* Match a formal argument list. */
3837
3838match
636dff67 3839gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
6de9cd9a
DN
3840{
3841 gfc_formal_arglist *head, *tail, *p, *q;
3842 char name[GFC_MAX_SYMBOL_LEN + 1];
3843 gfc_symbol *sym;
3844 match m;
3845
3846 head = tail = NULL;
3847
3848 if (gfc_match_char ('(') != MATCH_YES)
3849 {
3850 if (null_flag)
3851 goto ok;
3852 return MATCH_NO;
3853 }
3854
3855 if (gfc_match_char (')') == MATCH_YES)
3856 goto ok;
3857
3858 for (;;)
3859 {
3860 if (gfc_match_char ('*') == MATCH_YES)
3861 sym = NULL;
3862 else
3863 {
3864 m = gfc_match_name (name);
3865 if (m != MATCH_YES)
3866 goto cleanup;
3867
3868 if (gfc_get_symbol (name, NULL, &sym))
3869 goto cleanup;
3870 }
3871
3872 p = gfc_get_formal_arglist ();
3873
3874 if (head == NULL)
3875 head = tail = p;
3876 else
3877 {
3878 tail->next = p;
3879 tail = p;
3880 }
3881
3882 tail->sym = sym;
3883
3884 /* We don't add the VARIABLE flavor because the name could be a
636dff67
SK
3885 dummy procedure. We don't apply these attributes to formal
3886 arguments of statement functions. */
6de9cd9a 3887 if (sym != NULL && !st_flag
231b2fcc 3888 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
6de9cd9a
DN
3889 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3890 {
3891 m = MATCH_ERROR;
3892 goto cleanup;
3893 }
3894
3895 /* The name of a program unit can be in a different namespace,
636dff67
SK
3896 so check for it explicitly. After the statement is accepted,
3897 the name is checked for especially in gfc_get_symbol(). */
6de9cd9a
DN
3898 if (gfc_new_block != NULL && sym != NULL
3899 && strcmp (sym->name, gfc_new_block->name) == 0)
3900 {
3901 gfc_error ("Name '%s' at %C is the name of the procedure",
3902 sym->name);
3903 m = MATCH_ERROR;
3904 goto cleanup;
3905 }
3906
3907 if (gfc_match_char (')') == MATCH_YES)
3908 goto ok;
3909
3910 m = gfc_match_char (',');
3911 if (m != MATCH_YES)
3912 {
3913 gfc_error ("Unexpected junk in formal argument list at %C");
3914 goto cleanup;
3915 }
3916 }
3917
3918ok:
3919 /* Check for duplicate symbols in the formal argument list. */
3920 if (head != NULL)
3921 {
3922 for (p = head; p->next; p = p->next)
3923 {
3924 if (p->sym == NULL)
3925 continue;
3926
3927 for (q = p->next; q; q = q->next)
3928 if (p->sym == q->sym)
3929 {
636dff67
SK
3930 gfc_error ("Duplicate symbol '%s' in formal argument list "
3931 "at %C", p->sym->name);
6de9cd9a
DN
3932
3933 m = MATCH_ERROR;
3934 goto cleanup;
3935 }
3936 }
3937 }
3938
66e4ab31
SK
3939 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3940 == FAILURE)
6de9cd9a
DN
3941 {
3942 m = MATCH_ERROR;
3943 goto cleanup;
3944 }
3945
3946 return MATCH_YES;
3947
3948cleanup:
3949 gfc_free_formal_arglist (head);
3950 return m;
3951}
3952
3953
3954/* Match a RESULT specification following a function declaration or
3955 ENTRY statement. Also matches the end-of-statement. */
3956
3957static match
66e4ab31 3958match_result (gfc_symbol *function, gfc_symbol **result)
6de9cd9a
DN
3959{
3960 char name[GFC_MAX_SYMBOL_LEN + 1];
3961 gfc_symbol *r;
3962 match m;
3963
3964 if (gfc_match (" result (") != MATCH_YES)
3965 return MATCH_NO;
3966
3967 m = gfc_match_name (name);
3968 if (m != MATCH_YES)
3969 return m;
3970
a8b3b0b6
CR
3971 /* Get the right paren, and that's it because there could be the
3972 bind(c) attribute after the result clause. */
3973 if (gfc_match_char(')') != MATCH_YES)
6de9cd9a 3974 {
a8b3b0b6 3975 /* TODO: should report the missing right paren here. */
6de9cd9a
DN
3976 return MATCH_ERROR;
3977 }
3978
3979 if (strcmp (function->name, name) == 0)
3980 {
636dff67 3981 gfc_error ("RESULT variable at %C must be different than function name");
6de9cd9a
DN
3982 return MATCH_ERROR;
3983 }
3984
3985 if (gfc_get_symbol (name, NULL, &r))
3986 return MATCH_ERROR;
3987
231b2fcc
TS
3988 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3989 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
6de9cd9a
DN
3990 return MATCH_ERROR;
3991
3992 *result = r;
3993
3994 return MATCH_YES;
3995}
3996
3997
a8b3b0b6
CR
3998/* Match a function suffix, which could be a combination of a result
3999 clause and BIND(C), either one, or neither. The draft does not
4000 require them to come in a specific order. */
4001
4002match
4003gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4004{
4005 match is_bind_c; /* Found bind(c). */
4006 match is_result; /* Found result clause. */
4007 match found_match; /* Status of whether we've found a good match. */
8fc541d3 4008 char peek_char; /* Character we're going to peek at. */
1eabf70a 4009 bool allow_binding_name;
a8b3b0b6
CR
4010
4011 /* Initialize to having found nothing. */
4012 found_match = MATCH_NO;
4013 is_bind_c = MATCH_NO;
4014 is_result = MATCH_NO;
4015
4016 /* Get the next char to narrow between result and bind(c). */
4017 gfc_gobble_whitespace ();
8fc541d3 4018 peek_char = gfc_peek_ascii_char ();
a8b3b0b6 4019
1eabf70a
TB
4020 /* C binding names are not allowed for internal procedures. */
4021 if (gfc_current_state () == COMP_CONTAINS
4022 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4023 allow_binding_name = false;
4024 else
4025 allow_binding_name = true;
4026
a8b3b0b6
CR
4027 switch (peek_char)
4028 {
4029 case 'r':
4030 /* Look for result clause. */
4031 is_result = match_result (sym, result);
4032 if (is_result == MATCH_YES)
4033 {
4034 /* Now see if there is a bind(c) after it. */
1eabf70a 4035 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
4036 /* We've found the result clause and possibly bind(c). */
4037 found_match = MATCH_YES;
4038 }
4039 else
4040 /* This should only be MATCH_ERROR. */
4041 found_match = is_result;
4042 break;
4043 case 'b':
4044 /* Look for bind(c) first. */
1eabf70a 4045 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
4046 if (is_bind_c == MATCH_YES)
4047 {
4048 /* Now see if a result clause followed it. */
4049 is_result = match_result (sym, result);
4050 found_match = MATCH_YES;
4051 }
4052 else
4053 {
4054 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4055 found_match = MATCH_ERROR;
4056 }
4057 break;
4058 default:
4059 gfc_error ("Unexpected junk after function declaration at %C");
4060 found_match = MATCH_ERROR;
4061 break;
4062 }
4063
a8b3b0b6 4064 if (is_bind_c == MATCH_YES)
01f4fff1 4065 {
1eabf70a 4066 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
01f4fff1 4067 if (gfc_current_state () == COMP_CONTAINS
1eabf70a 4068 && sym->ns->proc_name->attr.flavor != FL_MODULE
fdc54e88
FXC
4069 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4070 "at %L may not be specified for an internal "
4071 "procedure", &gfc_current_locus)
1eabf70a
TB
4072 == FAILURE)
4073 return MATCH_ERROR;
4074
01f4fff1
TB
4075 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4076 == FAILURE)
4077 return MATCH_ERROR;
4078 }
a8b3b0b6
CR
4079
4080 return found_match;
4081}
4082
4083
69773742
JW
4084/* Match a PROCEDURE declaration (R1211). */
4085
4086static match
4087match_procedure_decl (void)
4088{
4089 match m;
4090 locus old_loc, entry_loc;
4091 gfc_symbol *sym, *proc_if = NULL;
4092 int num;
8fb74da4 4093 gfc_expr *initializer = NULL;
69773742
JW
4094
4095 old_loc = entry_loc = gfc_current_locus;
4096
4097 gfc_clear_ts (&current_ts);
4098
4099 if (gfc_match (" (") != MATCH_YES)
4100 {
4101 gfc_current_locus = entry_loc;
4102 return MATCH_NO;
4103 }
4104
4105 /* Get the type spec. for the procedure interface. */
4106 old_loc = gfc_current_locus;
e2d29968 4107 m = gfc_match_type_spec (&current_ts, 0);
8fc541d3 4108 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
69773742
JW
4109 goto got_ts;
4110
4111 if (m == MATCH_ERROR)
4112 return m;
4113
4114 gfc_current_locus = old_loc;
4115
4116 /* Get the name of the procedure or abstract interface
4117 to inherit the interface from. */
4118 m = gfc_match_symbol (&proc_if, 1);
4119
4120 if (m == MATCH_NO)
4121 goto syntax;
4122 else if (m == MATCH_ERROR)
4123 return m;
4124
4125 /* Various interface checks. */
4126 if (proc_if)
4127 {
bb343a6c
TB
4128 /* Resolve interface if possible. That way, attr.procedure is only set
4129 if it is declared by a later procedure-declaration-stmt, which is
4130 invalid per C1212. */
32d99e68
JW
4131 while (proc_if->ts.interface)
4132 proc_if = proc_if->ts.interface;
bb343a6c 4133
69773742
JW
4134 if (proc_if->generic)
4135 {
4136 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4137 return MATCH_ERROR;
4138 }
4139 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4140 {
4141 gfc_error ("Interface '%s' at %C may not be a statement function",
4142 proc_if->name);
4143 return MATCH_ERROR;
4144 }
4145 /* Handle intrinsic procedures. */
c1db9545
JW
4146 if (!(proc_if->attr.external || proc_if->attr.use_assoc
4147 || proc_if->attr.if_source == IFSRC_IFBODY)
c3005b0f
DK
4148 && (gfc_is_intrinsic (proc_if, 0, gfc_current_locus)
4149 || gfc_is_intrinsic (proc_if, 1, gfc_current_locus)))
69773742
JW
4150 proc_if->attr.intrinsic = 1;
4151 if (proc_if->attr.intrinsic
4152 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4153 {
4154 gfc_error ("Intrinsic procedure '%s' not allowed "
4155 "in PROCEDURE statement at %C", proc_if->name);
4156 return MATCH_ERROR;
4157 }
69773742
JW
4158 }
4159
4160got_ts:
69773742
JW
4161 if (gfc_match (" )") != MATCH_YES)
4162 {
4163 gfc_current_locus = entry_loc;
4164 return MATCH_NO;
4165 }
4166
4167 /* Parse attributes. */
4168 m = match_attr_spec();
4169 if (m == MATCH_ERROR)
4170 return MATCH_ERROR;
4171
4172 /* Get procedure symbols. */
4173 for(num=1;;num++)
4174 {
69773742
JW
4175 m = gfc_match_symbol (&sym, 0);
4176 if (m == MATCH_NO)
4177 goto syntax;
4178 else if (m == MATCH_ERROR)
4179 return m;
4180
4181 /* Add current_attr to the symbol attributes. */
4182 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4183 return MATCH_ERROR;
4184
4185 if (sym->attr.is_bind_c)
4186 {
4187 /* Check for C1218. */
4188 if (!proc_if || !proc_if->attr.is_bind_c)
4189 {
4190 gfc_error ("BIND(C) attribute at %C requires "
4191 "an interface with BIND(C)");
4192 return MATCH_ERROR;
4193 }
4194 /* Check for C1217. */
4195 if (has_name_equals && sym->attr.pointer)
4196 {
4197 gfc_error ("BIND(C) procedure with NAME may not have "
4198 "POINTER attribute at %C");
4199 return MATCH_ERROR;
4200 }
4201 if (has_name_equals && sym->attr.dummy)
4202 {
4203 gfc_error ("Dummy procedure at %C may not have "
4204 "BIND(C) attribute with NAME");
4205 return MATCH_ERROR;
4206 }
4207 /* Set binding label for BIND(C). */
4208 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4209 return MATCH_ERROR;
4210 }
4211
8fb74da4 4212 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
69773742
JW
4213 return MATCH_ERROR;
4214 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4215 return MATCH_ERROR;
4216
4217 /* Set interface. */
4218 if (proc_if != NULL)
6cc309c9 4219 {
32d99e68 4220 sym->ts.interface = proc_if;
6cc309c9
JD
4221 sym->attr.untyped = 1;
4222 }
69773742
JW
4223 else if (current_ts.type != BT_UNKNOWN)
4224 {
32d99e68
JW
4225 sym->ts = current_ts;
4226 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4227 sym->ts.interface->ts = current_ts;
4228 sym->ts.interface->attr.function = 1;
4229 sym->attr.function = sym->ts.interface->attr.function;
69773742
JW
4230 }
4231
8fb74da4
JW
4232 if (gfc_match (" =>") == MATCH_YES)
4233 {
4234 if (!current_attr.pointer)
4235 {
4236 gfc_error ("Initialization at %C isn't for a pointer variable");
4237 m = MATCH_ERROR;
4238 goto cleanup;
4239 }
4240
4241 m = gfc_match_null (&initializer);
4242 if (m == MATCH_NO)
4243 {
4244 gfc_error ("Pointer initialization requires a NULL() at %C");
4245 m = MATCH_ERROR;
4246 }
4247
4248 if (gfc_pure (NULL))
4249 {
4250 gfc_error ("Initialization of pointer at %C is not allowed in "
4251 "a PURE procedure");
4252 m = MATCH_ERROR;
4253 }
4254
4255 if (m != MATCH_YES)
4256 goto cleanup;
4257
4258 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4259 != SUCCESS)
4260 goto cleanup;
4261
4262 }
4263
4264 gfc_set_sym_referenced (sym);
4265
69773742
JW
4266 if (gfc_match_eos () == MATCH_YES)
4267 return MATCH_YES;
4268 if (gfc_match_char (',') != MATCH_YES)
4269 goto syntax;
4270 }
4271
4272syntax:
4273 gfc_error ("Syntax error in PROCEDURE statement at %C");
4274 return MATCH_ERROR;
8fb74da4
JW
4275
4276cleanup:
4277 /* Free stuff up and return. */
4278 gfc_free_expr (initializer);
4279 return m;
69773742
JW
4280}
4281
4282
4283/* Match a PROCEDURE declaration inside an interface (R1206). */
4284
4285static match
4286match_procedure_in_interface (void)
4287{
4288 match m;
4289 gfc_symbol *sym;
4290 char name[GFC_MAX_SYMBOL_LEN + 1];
4291
4292 if (current_interface.type == INTERFACE_NAMELESS
4293 || current_interface.type == INTERFACE_ABSTRACT)
4294 {
4295 gfc_error ("PROCEDURE at %C must be in a generic interface");
4296 return MATCH_ERROR;
4297 }
4298
4299 for(;;)
4300 {
4301 m = gfc_match_name (name);
4302 if (m == MATCH_NO)
4303 goto syntax;
4304 else if (m == MATCH_ERROR)
4305 return m;
4306 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4307 return MATCH_ERROR;
4308
4309 if (gfc_add_interface (sym) == FAILURE)
4310 return MATCH_ERROR;
4311
69773742
JW
4312 if (gfc_match_eos () == MATCH_YES)
4313 break;
4314 if (gfc_match_char (',') != MATCH_YES)
4315 goto syntax;
4316 }
4317
4318 return MATCH_YES;
4319
4320syntax:
4321 gfc_error ("Syntax error in PROCEDURE statement at %C");
4322 return MATCH_ERROR;
4323}
4324
4325
4326/* General matcher for PROCEDURE declarations. */
4327
30b608eb
DK
4328static match match_procedure_in_type (void);
4329
69773742
JW
4330match
4331gfc_match_procedure (void)
4332{
4333 match m;
4334
4335 switch (gfc_current_state ())
4336 {
4337 case COMP_NONE:
4338 case COMP_PROGRAM:
4339 case COMP_MODULE:
4340 case COMP_SUBROUTINE:
4341 case COMP_FUNCTION:
4342 m = match_procedure_decl ();
4343 break;
4344 case COMP_INTERFACE:
4345 m = match_procedure_in_interface ();
4346 break;
4347 case COMP_DERIVED:
30b608eb
DK
4348 gfc_error ("Fortran 2003: Procedure components at %C are not yet"
4349 " implemented in gfortran");
69773742 4350 return MATCH_ERROR;
30b608eb
DK
4351 case COMP_DERIVED_CONTAINS:
4352 m = match_procedure_in_type ();
4353 break;
69773742
JW
4354 default:
4355 return MATCH_NO;
4356 }
4357
4358 if (m != MATCH_YES)
4359 return m;
4360
4361 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4362 == FAILURE)
4363 return MATCH_ERROR;
4364
4365 return m;
4366}
4367
4368
c3005b0f
DK
4369/* Warn if a matched procedure has the same name as an intrinsic; this is
4370 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4371 parser-state-stack to find out whether we're in a module. */
4372
4373static void
4374warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4375{
4376 bool in_module;
4377
4378 in_module = (gfc_state_stack->previous
4379 && gfc_state_stack->previous->state == COMP_MODULE);
4380
4381 gfc_warn_intrinsic_shadow (sym, in_module, func);
4382}
4383
4384
6de9cd9a
DN
4385/* Match a function declaration. */
4386
4387match
4388gfc_match_function_decl (void)
4389{
4390 char name[GFC_MAX_SYMBOL_LEN + 1];
4391 gfc_symbol *sym, *result;
4392 locus old_loc;
4393 match m;
a8b3b0b6
CR
4394 match suffix_match;
4395 match found_match; /* Status returned by match func. */
6de9cd9a
DN
4396
4397 if (gfc_current_state () != COMP_NONE
4398 && gfc_current_state () != COMP_INTERFACE
4399 && gfc_current_state () != COMP_CONTAINS)
4400 return MATCH_NO;
4401
4402 gfc_clear_ts (&current_ts);
4403
63645982 4404 old_loc = gfc_current_locus;
6de9cd9a 4405
1c8bcdf7 4406 m = gfc_match_prefix (&current_ts);
6de9cd9a
DN
4407 if (m != MATCH_YES)
4408 {
63645982 4409 gfc_current_locus = old_loc;
6de9cd9a
DN
4410 return m;
4411 }
4412
4413 if (gfc_match ("function% %n", name) != MATCH_YES)
4414 {
63645982 4415 gfc_current_locus = old_loc;
6de9cd9a
DN
4416 return MATCH_NO;
4417 }
1a492601 4418 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4419 return MATCH_ERROR;
4420 gfc_new_block = sym;
4421
4422 m = gfc_match_formal_arglist (sym, 0, 0);
4423 if (m == MATCH_NO)
2b9a33ae
TS
4424 {
4425 gfc_error ("Expected formal argument list in function "
636dff67 4426 "definition at %C");
2b9a33ae
TS
4427 m = MATCH_ERROR;
4428 goto cleanup;
4429 }
6de9cd9a
DN
4430 else if (m == MATCH_ERROR)
4431 goto cleanup;
4432
4433 result = NULL;
4434
a8b3b0b6
CR
4435 /* According to the draft, the bind(c) and result clause can
4436 come in either order after the formal_arg_list (i.e., either
4437 can be first, both can exist together or by themselves or neither
4438 one). Therefore, the match_result can't match the end of the
4439 string, and check for the bind(c) or result clause in either order. */
4440 found_match = gfc_match_eos ();
4441
4442 /* Make sure that it isn't already declared as BIND(C). If it is, it
4443 must have been marked BIND(C) with a BIND(C) attribute and that is
4444 not allowed for procedures. */
4445 if (sym->attr.is_bind_c == 1)
4446 {
4447 sym->attr.is_bind_c = 0;
4448 if (sym->old_symbol != NULL)
4449 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4450 "variables or common blocks",
4451 &(sym->old_symbol->declared_at));
4452 else
4453 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4454 "variables or common blocks", &gfc_current_locus);
6de9cd9a
DN
4455 }
4456
a8b3b0b6 4457 if (found_match != MATCH_YES)
6de9cd9a 4458 {
a8b3b0b6
CR
4459 /* If we haven't found the end-of-statement, look for a suffix. */
4460 suffix_match = gfc_match_suffix (sym, &result);
4461 if (suffix_match == MATCH_YES)
4462 /* Need to get the eos now. */
4463 found_match = gfc_match_eos ();
4464 else
4465 found_match = suffix_match;
6de9cd9a
DN
4466 }
4467
a8b3b0b6
CR
4468 if(found_match != MATCH_YES)
4469 m = MATCH_ERROR;
6de9cd9a
DN
4470 else
4471 {
a8b3b0b6
CR
4472 /* Make changes to the symbol. */
4473 m = MATCH_ERROR;
4474
4475 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4476 goto cleanup;
4477
4478 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4479 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4480 goto cleanup;
6de9cd9a 4481
a8b3b0b6
CR
4482 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4483 && !sym->attr.implicit_type)
4484 {
4485 gfc_error ("Function '%s' at %C already has a type of %s", name,
4486 gfc_basic_typename (sym->ts.type));
4487 goto cleanup;
4488 }
4489
a99d95a2 4490 /* Delay matching the function characteristics until after the
1c8bcdf7 4491 specification block by signalling kind=-1. */
a99d95a2
PT
4492 sym->declared_at = old_loc;
4493 if (current_ts.type != BT_UNKNOWN)
4494 current_ts.kind = -1;
4495 else
4496 current_ts.kind = 0;
1c8bcdf7 4497
a8b3b0b6
CR
4498 if (result == NULL)
4499 {
4500 sym->ts = current_ts;
4501 sym->result = sym;
4502 }
4503 else
4504 {
4505 result->ts = current_ts;
4506 sym->result = result;
4507 }
4508
c3005b0f
DK
4509 /* Warn if this procedure has the same name as an intrinsic. */
4510 warn_intrinsic_shadow (sym, true);
4511
a8b3b0b6
CR
4512 return MATCH_YES;
4513 }
6de9cd9a
DN
4514
4515cleanup:
63645982 4516 gfc_current_locus = old_loc;
6de9cd9a
DN
4517 return m;
4518}
4519
636dff67
SK
4520
4521/* This is mostly a copy of parse.c(add_global_procedure) but modified to
4522 pass the name of the entry, rather than the gfc_current_block name, and
4523 to return false upon finding an existing global entry. */
68ea355b
PT
4524
4525static bool
636dff67 4526add_global_entry (const char *name, int sub)
68ea355b
PT
4527{
4528 gfc_gsymbol *s;
fcd5a113 4529 unsigned int type;
68ea355b
PT
4530
4531 s = gfc_get_gsymbol(name);
7389bce6 4532 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
68ea355b
PT
4533
4534 if (s->defined
636dff67 4535 || (s->type != GSYM_UNKNOWN
7389bce6 4536 && s->type != type))
ca39e6f2 4537 gfc_global_used(s, NULL);
68ea355b
PT
4538 else
4539 {
7389bce6 4540 s->type = type;
68ea355b
PT
4541 s->where = gfc_current_locus;
4542 s->defined = 1;
4543 return true;
4544 }
4545 return false;
4546}
6de9cd9a 4547
636dff67 4548
6de9cd9a
DN
4549/* Match an ENTRY statement. */
4550
4551match
4552gfc_match_entry (void)
4553{
3d79abbd
PB
4554 gfc_symbol *proc;
4555 gfc_symbol *result;
4556 gfc_symbol *entry;
6de9cd9a
DN
4557 char name[GFC_MAX_SYMBOL_LEN + 1];
4558 gfc_compile_state state;
4559 match m;
3d79abbd 4560 gfc_entry_list *el;
c96cfa49 4561 locus old_loc;
1a492601 4562 bool module_procedure;
bc3e7a8c
TB
4563 char peek_char;
4564 match is_bind_c;
6de9cd9a
DN
4565
4566 m = gfc_match_name (name);
4567 if (m != MATCH_YES)
4568 return m;
4569
3d79abbd 4570 state = gfc_current_state ();
4c93c95a 4571 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 4572 {
4c93c95a
FXC
4573 switch (state)
4574 {
4575 case COMP_PROGRAM:
4576 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4577 break;
4578 case COMP_MODULE:
4579 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4580 break;
4581 case COMP_BLOCK_DATA:
636dff67
SK
4582 gfc_error ("ENTRY statement at %C cannot appear within "
4583 "a BLOCK DATA");
4c93c95a
FXC
4584 break;
4585 case COMP_INTERFACE:
636dff67
SK
4586 gfc_error ("ENTRY statement at %C cannot appear within "
4587 "an INTERFACE");
4c93c95a
FXC
4588 break;
4589 case COMP_DERIVED:
636dff67
SK
4590 gfc_error ("ENTRY statement at %C cannot appear within "
4591 "a DERIVED TYPE block");
4c93c95a
FXC
4592 break;
4593 case COMP_IF:
636dff67
SK
4594 gfc_error ("ENTRY statement at %C cannot appear within "
4595 "an IF-THEN block");
4c93c95a
FXC
4596 break;
4597 case COMP_DO:
636dff67
SK
4598 gfc_error ("ENTRY statement at %C cannot appear within "
4599 "a DO block");
4c93c95a
FXC
4600 break;
4601 case COMP_SELECT:
636dff67
SK
4602 gfc_error ("ENTRY statement at %C cannot appear within "
4603 "a SELECT block");
4c93c95a
FXC
4604 break;
4605 case COMP_FORALL:
636dff67
SK
4606 gfc_error ("ENTRY statement at %C cannot appear within "
4607 "a FORALL block");
4c93c95a
FXC
4608 break;
4609 case COMP_WHERE:
636dff67
SK
4610 gfc_error ("ENTRY statement at %C cannot appear within "
4611 "a WHERE block");
4c93c95a
FXC
4612 break;
4613 case COMP_CONTAINS:
636dff67
SK
4614 gfc_error ("ENTRY statement at %C cannot appear within "
4615 "a contained subprogram");
4c93c95a
FXC
4616 break;
4617 default:
4618 gfc_internal_error ("gfc_match_entry(): Bad state");
4619 }
3d79abbd
PB
4620 return MATCH_ERROR;
4621 }
4622
1a492601 4623 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
4624 && gfc_current_ns->parent->proc_name
4625 && gfc_current_ns->parent->proc_name->attr.flavor
4626 == FL_MODULE;
1a492601 4627
3d79abbd
PB
4628 if (gfc_current_ns->parent != NULL
4629 && gfc_current_ns->parent->proc_name
1a492601 4630 && !module_procedure)
3d79abbd
PB
4631 {
4632 gfc_error("ENTRY statement at %C cannot appear in a "
4633 "contained procedure");
4634 return MATCH_ERROR;
4635 }
4636
1a492601
PT
4637 /* Module function entries need special care in get_proc_name
4638 because previous references within the function will have
4639 created symbols attached to the current namespace. */
4640 if (get_proc_name (name, &entry,
4641 gfc_current_ns->parent != NULL
4642 && module_procedure
4643 && gfc_current_ns->proc_name->attr.function))
6de9cd9a
DN
4644 return MATCH_ERROR;
4645
3d79abbd
PB
4646 proc = gfc_current_block ();
4647
bc3e7a8c
TB
4648 /* Make sure that it isn't already declared as BIND(C). If it is, it
4649 must have been marked BIND(C) with a BIND(C) attribute and that is
4650 not allowed for procedures. */
4651 if (entry->attr.is_bind_c == 1)
4652 {
4653 entry->attr.is_bind_c = 0;
4654 if (entry->old_symbol != NULL)
4655 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4656 "variables or common blocks",
4657 &(entry->old_symbol->declared_at));
4658 else
4659 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4660 "variables or common blocks", &gfc_current_locus);
4661 }
4662
4663 /* Check what next non-whitespace character is so we can tell if there
4664 is the required parens if we have a BIND(C). */
4665 gfc_gobble_whitespace ();
8fc541d3 4666 peek_char = gfc_peek_ascii_char ();
bc3e7a8c 4667
3d79abbd 4668 if (state == COMP_SUBROUTINE)
6de9cd9a 4669 {
231b2fcc 4670 /* An entry in a subroutine. */
182393f4 4671 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
68ea355b
PT
4672 return MATCH_ERROR;
4673
6de9cd9a
DN
4674 m = gfc_match_formal_arglist (entry, 0, 1);
4675 if (m != MATCH_YES)
4676 return MATCH_ERROR;
4677
1eabf70a
TB
4678 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4679 never be an internal procedure. */
4680 is_bind_c = gfc_match_bind_c (entry, true);
bc3e7a8c
TB
4681 if (is_bind_c == MATCH_ERROR)
4682 return MATCH_ERROR;
4683 if (is_bind_c == MATCH_YES)
4684 {
4685 if (peek_char != '(')
4686 {
4687 gfc_error ("Missing required parentheses before BIND(C) at %C");
4688 return MATCH_ERROR;
4689 }
4690 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4691 == FAILURE)
4692 return MATCH_ERROR;
4693 }
4694
231b2fcc
TS
4695 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4696 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a 4697 return MATCH_ERROR;
3d79abbd
PB
4698 }
4699 else
4700 {
c96cfa49 4701 /* An entry in a function.
636dff67
SK
4702 We need to take special care because writing
4703 ENTRY f()
4704 as
4705 ENTRY f
4706 is allowed, whereas
4707 ENTRY f() RESULT (r)
4708 can't be written as
4709 ENTRY f RESULT (r). */
182393f4 4710 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
68ea355b
PT
4711 return MATCH_ERROR;
4712
c96cfa49
TS
4713 old_loc = gfc_current_locus;
4714 if (gfc_match_eos () == MATCH_YES)
4715 {
4716 gfc_current_locus = old_loc;
4717 /* Match the empty argument list, and add the interface to
4718 the symbol. */
4719 m = gfc_match_formal_arglist (entry, 0, 1);
4720 }
4721 else
4722 m = gfc_match_formal_arglist (entry, 0, 0);
4723
6de9cd9a
DN
4724 if (m != MATCH_YES)
4725 return MATCH_ERROR;
4726
6de9cd9a
DN
4727 result = NULL;
4728
4729 if (gfc_match_eos () == MATCH_YES)
4730 {
231b2fcc
TS
4731 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4732 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a
DN
4733 return MATCH_ERROR;
4734
d198b59a 4735 entry->result = entry;
6de9cd9a
DN
4736 }
4737 else
4738 {
bc3e7a8c 4739 m = gfc_match_suffix (entry, &result);
6de9cd9a
DN
4740 if (m == MATCH_NO)
4741 gfc_syntax_error (ST_ENTRY);
4742 if (m != MATCH_YES)
4743 return MATCH_ERROR;
4744
bc3e7a8c
TB
4745 if (result)
4746 {
4747 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4748 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4749 || gfc_add_function (&entry->attr, result->name, NULL)
4750 == FAILURE)
4751 return MATCH_ERROR;
4752 entry->result = result;
4753 }
4754 else
4755 {
4756 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4757 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4758 return MATCH_ERROR;
4759 entry->result = entry;
4760 }
6de9cd9a 4761 }
6de9cd9a
DN
4762 }
4763
4764 if (gfc_match_eos () != MATCH_YES)
4765 {
4766 gfc_syntax_error (ST_ENTRY);
4767 return MATCH_ERROR;
4768 }
4769
3d79abbd
PB
4770 entry->attr.recursive = proc->attr.recursive;
4771 entry->attr.elemental = proc->attr.elemental;
4772 entry->attr.pure = proc->attr.pure;
6de9cd9a 4773
3d79abbd
PB
4774 el = gfc_get_entry_list ();
4775 el->sym = entry;
4776 el->next = gfc_current_ns->entries;
4777 gfc_current_ns->entries = el;
4778 if (el->next)
4779 el->id = el->next->id + 1;
4780 else
4781 el->id = 1;
6de9cd9a 4782
3d79abbd
PB
4783 new_st.op = EXEC_ENTRY;
4784 new_st.ext.entry = el;
4785
4786 return MATCH_YES;
6de9cd9a
DN
4787}
4788
4789
4790/* Match a subroutine statement, including optional prefixes. */
4791
4792match
4793gfc_match_subroutine (void)
4794{
4795 char name[GFC_MAX_SYMBOL_LEN + 1];
4796 gfc_symbol *sym;
4797 match m;
a8b3b0b6
CR
4798 match is_bind_c;
4799 char peek_char;
1eabf70a 4800 bool allow_binding_name;
6de9cd9a
DN
4801
4802 if (gfc_current_state () != COMP_NONE
4803 && gfc_current_state () != COMP_INTERFACE
4804 && gfc_current_state () != COMP_CONTAINS)
4805 return MATCH_NO;
4806
1c8bcdf7 4807 m = gfc_match_prefix (NULL);
6de9cd9a
DN
4808 if (m != MATCH_YES)
4809 return m;
4810
4811 m = gfc_match ("subroutine% %n", name);
4812 if (m != MATCH_YES)
4813 return m;
4814
1a492601 4815 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4816 return MATCH_ERROR;
4817 gfc_new_block = sym;
4818
a8b3b0b6 4819 /* Check what next non-whitespace character is so we can tell if there
bc3e7a8c 4820 is the required parens if we have a BIND(C). */
a8b3b0b6 4821 gfc_gobble_whitespace ();
8fc541d3 4822 peek_char = gfc_peek_ascii_char ();
a8b3b0b6 4823
231b2fcc 4824 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4825 return MATCH_ERROR;
4826
4827 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4828 return MATCH_ERROR;
4829
a8b3b0b6
CR
4830 /* Make sure that it isn't already declared as BIND(C). If it is, it
4831 must have been marked BIND(C) with a BIND(C) attribute and that is
4832 not allowed for procedures. */
4833 if (sym->attr.is_bind_c == 1)
4834 {
4835 sym->attr.is_bind_c = 0;
4836 if (sym->old_symbol != NULL)
4837 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4838 "variables or common blocks",
4839 &(sym->old_symbol->declared_at));
4840 else
4841 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4842 "variables or common blocks", &gfc_current_locus);
4843 }
1eabf70a
TB
4844
4845 /* C binding names are not allowed for internal procedures. */
4846 if (gfc_current_state () == COMP_CONTAINS
4847 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4848 allow_binding_name = false;
4849 else
4850 allow_binding_name = true;
4851
a8b3b0b6
CR
4852 /* Here, we are just checking if it has the bind(c) attribute, and if
4853 so, then we need to make sure it's all correct. If it doesn't,
4854 we still need to continue matching the rest of the subroutine line. */
1eabf70a 4855 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
4856 if (is_bind_c == MATCH_ERROR)
4857 {
4858 /* There was an attempt at the bind(c), but it was wrong. An
4859 error message should have been printed w/in the gfc_match_bind_c
4860 so here we'll just return the MATCH_ERROR. */
4861 return MATCH_ERROR;
4862 }
4863
4864 if (is_bind_c == MATCH_YES)
4865 {
1eabf70a 4866 /* The following is allowed in the Fortran 2008 draft. */
01f4fff1 4867 if (gfc_current_state () == COMP_CONTAINS
1eabf70a 4868 && sym->ns->proc_name->attr.flavor != FL_MODULE
fdc54e88
FXC
4869 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4870 "at %L may not be specified for an internal "
4871 "procedure", &gfc_current_locus)
1eabf70a
TB
4872 == FAILURE)
4873 return MATCH_ERROR;
4874
a8b3b0b6
CR
4875 if (peek_char != '(')
4876 {
4877 gfc_error ("Missing required parentheses before BIND(C) at %C");
4878 return MATCH_ERROR;
4879 }
4880 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4881 == FAILURE)
4882 return MATCH_ERROR;
4883 }
4884
6de9cd9a
DN
4885 if (gfc_match_eos () != MATCH_YES)
4886 {
4887 gfc_syntax_error (ST_SUBROUTINE);
4888 return MATCH_ERROR;
4889 }
4890
4891 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4892 return MATCH_ERROR;
4893
c3005b0f
DK
4894 /* Warn if it has the same name as an intrinsic. */
4895 warn_intrinsic_shadow (sym, false);
4896
6de9cd9a
DN
4897 return MATCH_YES;
4898}
4899
4900
a8b3b0b6
CR
4901/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4902 given, and set the binding label in either the given symbol (if not
86bf520d 4903 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
4904 encounter the BIND(C) before the declaration itself. Return
4905 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4906 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4907 or MATCH_YES if the specifier was correct and the binding label and
4908 bind(c) fields were set correctly for the given symbol or the
1eabf70a
TB
4909 current_ts. If allow_binding_name is false, no binding name may be
4910 given. */
a8b3b0b6
CR
4911
4912match
1eabf70a 4913gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
a8b3b0b6
CR
4914{
4915 /* binding label, if exists */
4916 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4917 match double_quote;
4918 match single_quote;
a8b3b0b6
CR
4919
4920 /* Initialize the flag that specifies whether we encountered a NAME=
4921 specifier or not. */
4922 has_name_equals = 0;
4923
4924 /* Init the first char to nil so we can catch if we don't have
4925 the label (name attr) or the symbol name yet. */
4926 binding_label[0] = '\0';
4927
4928 /* This much we have to be able to match, in this order, if
4929 there is a bind(c) label. */
4930 if (gfc_match (" bind ( c ") != MATCH_YES)
4931 return MATCH_NO;
4932
4933 /* Now see if there is a binding label, or if we've reached the
4934 end of the bind(c) attribute without one. */
4935 if (gfc_match_char (',') == MATCH_YES)
4936 {
4937 if (gfc_match (" name = ") != MATCH_YES)
4938 {
4939 gfc_error ("Syntax error in NAME= specifier for binding label "
4940 "at %C");
4941 /* should give an error message here */
4942 return MATCH_ERROR;
4943 }
4944
4945 has_name_equals = 1;
4946
4947 /* Get the opening quote. */
4948 double_quote = MATCH_YES;
4949 single_quote = MATCH_YES;
4950 double_quote = gfc_match_char ('"');
4951 if (double_quote != MATCH_YES)
4952 single_quote = gfc_match_char ('\'');
4953 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4954 {
4955 gfc_error ("Syntax error in NAME= specifier for binding label "
4956 "at %C");
4957 return MATCH_ERROR;
4958 }
4959
4960 /* Grab the binding label, using functions that will not lower
4961 case the names automatically. */
4962 if (gfc_match_name_C (binding_label) != MATCH_YES)
4963 return MATCH_ERROR;
4964
4965 /* Get the closing quotation. */
4966 if (double_quote == MATCH_YES)
4967 {
4968 if (gfc_match_char ('"') != MATCH_YES)
4969 {
4970 gfc_error ("Missing closing quote '\"' for binding label at %C");
4971 /* User started string with '"' so looked to match it. */
4972 return MATCH_ERROR;
4973 }
4974 }
4975 else
4976 {
4977 if (gfc_match_char ('\'') != MATCH_YES)
4978 {
4979 gfc_error ("Missing closing quote '\'' for binding label at %C");
4980 /* User started string with "'" char. */
4981 return MATCH_ERROR;
4982 }
4983 }
4984 }
4985
4986 /* Get the required right paren. */
4987 if (gfc_match_char (')') != MATCH_YES)
4988 {
4989 gfc_error ("Missing closing paren for binding label at %C");
4990 return MATCH_ERROR;
4991 }
4992
1eabf70a
TB
4993 if (has_name_equals && !allow_binding_name)
4994 {
4995 gfc_error ("No binding name is allowed in BIND(C) at %C");
4996 return MATCH_ERROR;
4997 }
4998
4999 if (has_name_equals && sym != NULL && sym->attr.dummy)
5000 {
5001 gfc_error ("For dummy procedure %s, no binding name is "
5002 "allowed in BIND(C) at %C", sym->name);
5003 return MATCH_ERROR;
5004 }
5005
5006
a8b3b0b6
CR
5007 /* Save the binding label to the symbol. If sym is null, we're
5008 probably matching the typespec attributes of a declaration and
5009 haven't gotten the name yet, and therefore, no symbol yet. */
5010 if (binding_label[0] != '\0')
5011 {
5012 if (sym != NULL)
5013 {
c5b5a17a 5014 strcpy (sym->binding_label, binding_label);
a8b3b0b6
CR
5015 }
5016 else
c5b5a17a 5017 strcpy (curr_binding_label, binding_label);
a8b3b0b6 5018 }
1eabf70a 5019 else if (allow_binding_name)
a8b3b0b6
CR
5020 {
5021 /* No binding label, but if symbol isn't null, we
1eabf70a
TB
5022 can set the label for it here.
5023 If name="" or allow_binding_name is false, no C binding name is
5024 created. */
a8b3b0b6
CR
5025 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5026 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5027 }
9e1d712c 5028
129d15a3
JW
5029 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5030 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
5031 {
5032 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5033 return MATCH_ERROR;
5034 }
5035
a8b3b0b6
CR
5036 return MATCH_YES;
5037}
5038
5039
1f2959f0 5040/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
5041
5042static int
5043contained_procedure (void)
5044{
083de129 5045 gfc_state_data *s = gfc_state_stack;
ddc9ce91 5046
083de129
TB
5047 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5048 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5049 return 1;
ddc9ce91
TS
5050
5051 return 0;
5052}
5053
d51347f9 5054/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
5055 interoperable with the corresponding C enumeration type, making
5056 sure that -fshort-enums is honored. */
5057
5058static void
5059set_enum_kind(void)
5060{
5061 enumerator_history *current_history = NULL;
5062 int kind;
5063 int i;
5064
5065 if (max_enum == NULL || enum_history == NULL)
5066 return;
5067
5068 if (!gfc_option.fshort_enums)
d51347f9
TB
5069 return;
5070
25d8f0a2
TS
5071 i = 0;
5072 do
5073 {
5074 kind = gfc_integer_kinds[i++].kind;
5075 }
d51347f9 5076 while (kind < gfc_c_int_kind
25d8f0a2
TS
5077 && gfc_check_integer_range (max_enum->initializer->value.integer,
5078 kind) != ARITH_OK);
5079
5080 current_history = enum_history;
5081 while (current_history != NULL)
5082 {
5083 current_history->sym->ts.kind = kind;
5084 current_history = current_history->next;
5085 }
5086}
5087
636dff67 5088
6de9cd9a
DN
5089/* Match any of the various end-block statements. Returns the type of
5090 END to the caller. The END INTERFACE, END IF, END DO and END
5091 SELECT statements cannot be replaced by a single END statement. */
5092
5093match
636dff67 5094gfc_match_end (gfc_statement *st)
6de9cd9a
DN
5095{
5096 char name[GFC_MAX_SYMBOL_LEN + 1];
5097 gfc_compile_state state;
5098 locus old_loc;
5099 const char *block_name;
5100 const char *target;
ddc9ce91 5101 int eos_ok;
6de9cd9a
DN
5102 match m;
5103
63645982 5104 old_loc = gfc_current_locus;
6de9cd9a
DN
5105 if (gfc_match ("end") != MATCH_YES)
5106 return MATCH_NO;
5107
5108 state = gfc_current_state ();
636dff67
SK
5109 block_name = gfc_current_block () == NULL
5110 ? NULL : gfc_current_block ()->name;
6de9cd9a 5111
30b608eb 5112 if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
6de9cd9a
DN
5113 {
5114 state = gfc_state_stack->previous->state;
636dff67
SK
5115 block_name = gfc_state_stack->previous->sym == NULL
5116 ? NULL : gfc_state_stack->previous->sym->name;
6de9cd9a
DN
5117 }
5118
5119 switch (state)
5120 {
5121 case COMP_NONE:
5122 case COMP_PROGRAM:
5123 *st = ST_END_PROGRAM;
5124 target = " program";
ddc9ce91 5125 eos_ok = 1;
6de9cd9a
DN
5126 break;
5127
5128 case COMP_SUBROUTINE:
5129 *st = ST_END_SUBROUTINE;
5130 target = " subroutine";
ddc9ce91 5131 eos_ok = !contained_procedure ();
6de9cd9a
DN
5132 break;
5133
5134 case COMP_FUNCTION:
5135 *st = ST_END_FUNCTION;
5136 target = " function";
ddc9ce91 5137 eos_ok = !contained_procedure ();
6de9cd9a
DN
5138 break;
5139
5140 case COMP_BLOCK_DATA:
5141 *st = ST_END_BLOCK_DATA;
5142 target = " block data";
ddc9ce91 5143 eos_ok = 1;
6de9cd9a
DN
5144 break;
5145
5146 case COMP_MODULE:
5147 *st = ST_END_MODULE;
5148 target = " module";
ddc9ce91 5149 eos_ok = 1;
6de9cd9a
DN
5150 break;
5151
5152 case COMP_INTERFACE:
5153 *st = ST_END_INTERFACE;
5154 target = " interface";
ddc9ce91 5155 eos_ok = 0;
6de9cd9a
DN
5156 break;
5157
5158 case COMP_DERIVED:
30b608eb 5159 case COMP_DERIVED_CONTAINS:
6de9cd9a
DN
5160 *st = ST_END_TYPE;
5161 target = " type";
ddc9ce91 5162 eos_ok = 0;
6de9cd9a
DN
5163 break;
5164
5165 case COMP_IF:
5166 *st = ST_ENDIF;
5167 target = " if";
ddc9ce91 5168 eos_ok = 0;
6de9cd9a
DN
5169 break;
5170
5171 case COMP_DO:
5172 *st = ST_ENDDO;
5173 target = " do";
ddc9ce91 5174 eos_ok = 0;
6de9cd9a
DN
5175 break;
5176
5177 case COMP_SELECT:
5178 *st = ST_END_SELECT;
5179 target = " select";
ddc9ce91 5180 eos_ok = 0;
6de9cd9a
DN
5181 break;
5182
5183 case COMP_FORALL:
5184 *st = ST_END_FORALL;
5185 target = " forall";
ddc9ce91 5186 eos_ok = 0;
6de9cd9a
DN
5187 break;
5188
5189 case COMP_WHERE:
5190 *st = ST_END_WHERE;
5191 target = " where";
ddc9ce91 5192 eos_ok = 0;
6de9cd9a
DN
5193 break;
5194
25d8f0a2
TS
5195 case COMP_ENUM:
5196 *st = ST_END_ENUM;
5197 target = " enum";
5198 eos_ok = 0;
5199 last_initializer = NULL;
5200 set_enum_kind ();
5201 gfc_free_enum_history ();
5202 break;
5203
6de9cd9a
DN
5204 default:
5205 gfc_error ("Unexpected END statement at %C");
5206 goto cleanup;
5207 }
5208
5209 if (gfc_match_eos () == MATCH_YES)
5210 {
ddc9ce91 5211 if (!eos_ok)
6de9cd9a 5212 {
66e4ab31 5213 /* We would have required END [something]. */
59ce85b5
TS
5214 gfc_error ("%s statement expected at %L",
5215 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
5216 goto cleanup;
5217 }
5218
5219 return MATCH_YES;
5220 }
5221
5222 /* Verify that we've got the sort of end-block that we're expecting. */
5223 if (gfc_match (target) != MATCH_YES)
5224 {
5225 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5226 goto cleanup;
5227 }
5228
5229 /* If we're at the end, make sure a block name wasn't required. */
5230 if (gfc_match_eos () == MATCH_YES)
5231 {
5232
690af379
TS
5233 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5234 && *st != ST_END_FORALL && *st != ST_END_WHERE)
6de9cd9a
DN
5235 return MATCH_YES;
5236
5237 if (gfc_current_block () == NULL)
5238 return MATCH_YES;
5239
5240 gfc_error ("Expected block name of '%s' in %s statement at %C",
5241 block_name, gfc_ascii_statement (*st));
5242
5243 return MATCH_ERROR;
5244 }
5245
5246 /* END INTERFACE has a special handler for its several possible endings. */
5247 if (*st == ST_END_INTERFACE)
5248 return gfc_match_end_interface ();
5249
66e4ab31
SK
5250 /* We haven't hit the end of statement, so what is left must be an
5251 end-name. */
6de9cd9a
DN
5252 m = gfc_match_space ();
5253 if (m == MATCH_YES)
5254 m = gfc_match_name (name);
5255
5256 if (m == MATCH_NO)
5257 gfc_error ("Expected terminating name at %C");
5258 if (m != MATCH_YES)
5259 goto cleanup;
5260
5261 if (block_name == NULL)
5262 goto syntax;
5263
5264 if (strcmp (name, block_name) != 0)
5265 {
5266 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5267 gfc_ascii_statement (*st));
5268 goto cleanup;
5269 }
5270
5271 if (gfc_match_eos () == MATCH_YES)
5272 return MATCH_YES;
5273
5274syntax:
5275 gfc_syntax_error (*st);
5276
5277cleanup:
63645982 5278 gfc_current_locus = old_loc;
6de9cd9a
DN
5279 return MATCH_ERROR;
5280}
5281
5282
5283
5284/***************** Attribute declaration statements ****************/
5285
5286/* Set the attribute of a single variable. */
5287
5288static match
5289attr_decl1 (void)
5290{
5291 char name[GFC_MAX_SYMBOL_LEN + 1];
5292 gfc_array_spec *as;
5293 gfc_symbol *sym;
5294 locus var_locus;
5295 match m;
5296
5297 as = NULL;
5298
5299 m = gfc_match_name (name);
5300 if (m != MATCH_YES)
5301 goto cleanup;
5302
5303 if (find_special (name, &sym))
5304 return MATCH_ERROR;
5305
63645982 5306 var_locus = gfc_current_locus;
6de9cd9a
DN
5307
5308 /* Deal with possible array specification for certain attributes. */
5309 if (current_attr.dimension
5310 || current_attr.allocatable
5311 || current_attr.pointer
5312 || current_attr.target)
5313 {
5314 m = gfc_match_array_spec (&as);
5315 if (m == MATCH_ERROR)
5316 goto cleanup;
5317
5318 if (current_attr.dimension && m == MATCH_NO)
5319 {
636dff67
SK
5320 gfc_error ("Missing array specification at %L in DIMENSION "
5321 "statement", &var_locus);
6de9cd9a
DN
5322 m = MATCH_ERROR;
5323 goto cleanup;
5324 }
5325
1283ab12
TB
5326 if (current_attr.dimension && sym->value)
5327 {
5328 gfc_error ("Dimensions specified for %s at %L after its "
5329 "initialisation", sym->name, &var_locus);
5330 m = MATCH_ERROR;
5331 goto cleanup;
5332 }
5333
6de9cd9a
DN
5334 if ((current_attr.allocatable || current_attr.pointer)
5335 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5336 {
636dff67 5337 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
5338 m = MATCH_ERROR;
5339 goto cleanup;
5340 }
5341 }
5342
636dff67
SK
5343 /* Update symbol table. DIMENSION attribute is set
5344 in gfc_set_array_spec(). */
6de9cd9a 5345 if (current_attr.dimension == 0
e62532af 5346 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6de9cd9a
DN
5347 {
5348 m = MATCH_ERROR;
5349 goto cleanup;
5350 }
5351
5352 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5353 {
5354 m = MATCH_ERROR;
5355 goto cleanup;
5356 }
d51347f9 5357
83d890b9
AL
5358 if (sym->attr.cray_pointee && sym->as != NULL)
5359 {
5360 /* Fix the array spec. */
5361 m = gfc_mod_pointee_as (sym->as);
5362 if (m == MATCH_ERROR)
5363 goto cleanup;
5364 }
6de9cd9a 5365
7114edca 5366 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
1902704e
PT
5367 {
5368 m = MATCH_ERROR;
5369 goto cleanup;
5370 }
5371
6de9cd9a
DN
5372 if ((current_attr.external || current_attr.intrinsic)
5373 && sym->attr.flavor != FL_PROCEDURE
231b2fcc 5374 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5375 {
5376 m = MATCH_ERROR;
5377 goto cleanup;
5378 }
5379
5380 return MATCH_YES;
5381
5382cleanup:
5383 gfc_free_array_spec (as);
5384 return m;
5385}
5386
5387
5388/* Generic attribute declaration subroutine. Used for attributes that
5389 just have a list of names. */
5390
5391static match
5392attr_decl (void)
5393{
5394 match m;
5395
5396 /* Gobble the optional double colon, by simply ignoring the result
5397 of gfc_match(). */
5398 gfc_match (" ::");
5399
5400 for (;;)
5401 {
5402 m = attr_decl1 ();
5403 if (m != MATCH_YES)
5404 break;
5405
5406 if (gfc_match_eos () == MATCH_YES)
5407 {
5408 m = MATCH_YES;
5409 break;
5410 }
5411
5412 if (gfc_match_char (',') != MATCH_YES)
5413 {
5414 gfc_error ("Unexpected character in variable list at %C");
5415 m = MATCH_ERROR;
5416 break;
5417 }
5418 }
5419
5420 return m;
5421}
5422
5423
83d890b9
AL
5424/* This routine matches Cray Pointer declarations of the form:
5425 pointer ( <pointer>, <pointee> )
5426 or
d51347f9
TB
5427 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5428 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
5429 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5430 be either a scalar, or an array declaration. No space is allocated for
d51347f9 5431 the pointee. For the statement
83d890b9
AL
5432 pointer (ipt, ar(10))
5433 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 5434 ar(i) => ((<type> *) ipt)(i)
b122dc6a 5435 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
5436
5437static match
5438cray_pointer_decl (void)
5439{
5440 match m;
5441 gfc_array_spec *as;
5442 gfc_symbol *cptr; /* Pointer symbol. */
5443 gfc_symbol *cpte; /* Pointee symbol. */
5444 locus var_locus;
5445 bool done = false;
5446
5447 while (!done)
5448 {
5449 if (gfc_match_char ('(') != MATCH_YES)
5450 {
5451 gfc_error ("Expected '(' at %C");
d51347f9 5452 return MATCH_ERROR;
83d890b9 5453 }
d51347f9 5454
83d890b9
AL
5455 /* Match pointer. */
5456 var_locus = gfc_current_locus;
5457 gfc_clear_attr (&current_attr);
5458 gfc_add_cray_pointer (&current_attr, &var_locus);
5459 current_ts.type = BT_INTEGER;
5460 current_ts.kind = gfc_index_integer_kind;
5461
d51347f9 5462 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
5463 if (m != MATCH_YES)
5464 {
5465 gfc_error ("Expected variable name at %C");
5466 return m;
5467 }
d51347f9 5468
83d890b9
AL
5469 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5470 return MATCH_ERROR;
5471
d51347f9 5472 gfc_set_sym_referenced (cptr);
83d890b9
AL
5473
5474 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5475 {
5476 cptr->ts.type = BT_INTEGER;
d51347f9 5477 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
5478 }
5479 else if (cptr->ts.type != BT_INTEGER)
5480 {
e25a0da3 5481 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
5482 return MATCH_ERROR;
5483 }
5484 else if (cptr->ts.kind < gfc_index_integer_kind)
5485 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 5486 " memory addresses require %d bytes",
636dff67 5487 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
5488
5489 if (gfc_match_char (',') != MATCH_YES)
5490 {
5491 gfc_error ("Expected \",\" at %C");
d51347f9 5492 return MATCH_ERROR;
83d890b9
AL
5493 }
5494
d51347f9 5495 /* Match Pointee. */
83d890b9
AL
5496 var_locus = gfc_current_locus;
5497 gfc_clear_attr (&current_attr);
5498 gfc_add_cray_pointee (&current_attr, &var_locus);
5499 current_ts.type = BT_UNKNOWN;
5500 current_ts.kind = 0;
5501
5502 m = gfc_match_symbol (&cpte, 0);
5503 if (m != MATCH_YES)
5504 {
5505 gfc_error ("Expected variable name at %C");
5506 return m;
5507 }
d51347f9 5508
83d890b9
AL
5509 /* Check for an optional array spec. */
5510 m = gfc_match_array_spec (&as);
5511 if (m == MATCH_ERROR)
5512 {
5513 gfc_free_array_spec (as);
5514 return m;
5515 }
5516 else if (m == MATCH_NO)
5517 {
5518 gfc_free_array_spec (as);
5519 as = NULL;
5520 }
5521
5522 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5523 return MATCH_ERROR;
5524
5525 gfc_set_sym_referenced (cpte);
5526
5527 if (cpte->as == NULL)
5528 {
5529 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5530 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5531 }
5532 else if (as != NULL)
5533 {
e25a0da3 5534 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
5535 gfc_free_array_spec (as);
5536 return MATCH_ERROR;
5537 }
5538
5539 as = NULL;
5540
5541 if (cpte->as != NULL)
5542 {
5543 /* Fix array spec. */
5544 m = gfc_mod_pointee_as (cpte->as);
5545 if (m == MATCH_ERROR)
5546 return m;
5547 }
5548
5549 /* Point the Pointee at the Pointer. */
b122dc6a 5550 cpte->cp_pointer = cptr;
83d890b9
AL
5551
5552 if (gfc_match_char (')') != MATCH_YES)
5553 {
5554 gfc_error ("Expected \")\" at %C");
5555 return MATCH_ERROR;
5556 }
5557 m = gfc_match_char (',');
5558 if (m != MATCH_YES)
5559 done = true; /* Stop searching for more declarations. */
5560
5561 }
5562
5563 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5564 || gfc_match_eos () != MATCH_YES)
5565 {
5566 gfc_error ("Expected \",\" or end of statement at %C");
5567 return MATCH_ERROR;
5568 }
5569 return MATCH_YES;
5570}
5571
5572
6de9cd9a
DN
5573match
5574gfc_match_external (void)
5575{
5576
5577 gfc_clear_attr (&current_attr);
1902704e 5578 current_attr.external = 1;
6de9cd9a
DN
5579
5580 return attr_decl ();
5581}
5582
5583
6de9cd9a
DN
5584match
5585gfc_match_intent (void)
5586{
5587 sym_intent intent;
5588
5589 intent = match_intent_spec ();
5590 if (intent == INTENT_UNKNOWN)
5591 return MATCH_ERROR;
5592
5593 gfc_clear_attr (&current_attr);
1902704e 5594 current_attr.intent = intent;
6de9cd9a
DN
5595
5596 return attr_decl ();
5597}
5598
5599
5600match
5601gfc_match_intrinsic (void)
5602{
5603
5604 gfc_clear_attr (&current_attr);
1902704e 5605 current_attr.intrinsic = 1;
6de9cd9a
DN
5606
5607 return attr_decl ();
5608}
5609
5610
5611match
5612gfc_match_optional (void)
5613{
5614
5615 gfc_clear_attr (&current_attr);
1902704e 5616 current_attr.optional = 1;
6de9cd9a
DN
5617
5618 return attr_decl ();
5619}
5620
5621
5622match
5623gfc_match_pointer (void)
5624{
83d890b9 5625 gfc_gobble_whitespace ();
8fc541d3 5626 if (gfc_peek_ascii_char () == '(')
83d890b9
AL
5627 {
5628 if (!gfc_option.flag_cray_pointer)
5629 {
636dff67
SK
5630 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5631 "flag");
83d890b9
AL
5632 return MATCH_ERROR;
5633 }
5634 return cray_pointer_decl ();
5635 }
5636 else
5637 {
5638 gfc_clear_attr (&current_attr);
1902704e 5639 current_attr.pointer = 1;
83d890b9
AL
5640
5641 return attr_decl ();
5642 }
6de9cd9a
DN
5643}
5644
5645
5646match
5647gfc_match_allocatable (void)
5648{
6de9cd9a 5649 gfc_clear_attr (&current_attr);
1902704e 5650 current_attr.allocatable = 1;
6de9cd9a
DN
5651
5652 return attr_decl ();
5653}
5654
5655
5656match
5657gfc_match_dimension (void)
5658{
6de9cd9a 5659 gfc_clear_attr (&current_attr);
1902704e 5660 current_attr.dimension = 1;
6de9cd9a
DN
5661
5662 return attr_decl ();
5663}
5664
5665
5666match
5667gfc_match_target (void)
5668{
6de9cd9a 5669 gfc_clear_attr (&current_attr);
1902704e 5670 current_attr.target = 1;
6de9cd9a
DN
5671
5672 return attr_decl ();
5673}
5674
5675
5676/* Match the list of entities being specified in a PUBLIC or PRIVATE
5677 statement. */
5678
5679static match
5680access_attr_decl (gfc_statement st)
5681{
5682 char name[GFC_MAX_SYMBOL_LEN + 1];
5683 interface_type type;
5684 gfc_user_op *uop;
5685 gfc_symbol *sym;
a1ee985f 5686 gfc_intrinsic_op op;
6de9cd9a
DN
5687 match m;
5688
5689 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5690 goto done;
5691
5692 for (;;)
5693 {
a1ee985f 5694 m = gfc_match_generic_spec (&type, name, &op);
6de9cd9a
DN
5695 if (m == MATCH_NO)
5696 goto syntax;
5697 if (m == MATCH_ERROR)
5698 return MATCH_ERROR;
5699
5700 switch (type)
5701 {
5702 case INTERFACE_NAMELESS:
9e1d712c 5703 case INTERFACE_ABSTRACT:
6de9cd9a
DN
5704 goto syntax;
5705
5706 case INTERFACE_GENERIC:
5707 if (gfc_get_symbol (name, NULL, &sym))
5708 goto done;
5709
636dff67
SK
5710 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5711 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
231b2fcc 5712 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5713 return MATCH_ERROR;
5714
5715 break;
5716
5717 case INTERFACE_INTRINSIC_OP:
a1ee985f 5718 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6de9cd9a 5719 {
a1ee985f 5720 gfc_current_ns->operator_access[op] =
6de9cd9a
DN
5721 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5722 }
5723 else
5724 {
5725 gfc_error ("Access specification of the %s operator at %C has "
a1ee985f 5726 "already been specified", gfc_op2string (op));
6de9cd9a
DN
5727 goto done;
5728 }
5729
5730 break;
5731
5732 case INTERFACE_USER_OP:
5733 uop = gfc_get_uop (name);
5734
5735 if (uop->access == ACCESS_UNKNOWN)
5736 {
636dff67
SK
5737 uop->access = (st == ST_PUBLIC)
5738 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
5739 }
5740 else
5741 {
636dff67
SK
5742 gfc_error ("Access specification of the .%s. operator at %C "
5743 "has already been specified", sym->name);
6de9cd9a
DN
5744 goto done;
5745 }
5746
5747 break;
5748 }
5749
5750 if (gfc_match_char (',') == MATCH_NO)
5751 break;
5752 }
5753
5754 if (gfc_match_eos () != MATCH_YES)
5755 goto syntax;
5756 return MATCH_YES;
5757
5758syntax:
5759 gfc_syntax_error (st);
5760
5761done:
5762 return MATCH_ERROR;
5763}
5764
5765
ee7e677f
TB
5766match
5767gfc_match_protected (void)
5768{
5769 gfc_symbol *sym;
5770 match m;
5771
5772 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5773 {
5774 gfc_error ("PROTECTED at %C only allowed in specification "
5775 "part of a module");
5776 return MATCH_ERROR;
5777
5778 }
5779
636dff67 5780 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
ee7e677f
TB
5781 == FAILURE)
5782 return MATCH_ERROR;
5783
5784 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5785 {
5786 return MATCH_ERROR;
5787 }
5788
5789 if (gfc_match_eos () == MATCH_YES)
5790 goto syntax;
5791
5792 for(;;)
5793 {
5794 m = gfc_match_symbol (&sym, 0);
5795 switch (m)
5796 {
5797 case MATCH_YES:
636dff67
SK
5798 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5799 == FAILURE)
ee7e677f
TB
5800 return MATCH_ERROR;
5801 goto next_item;
5802
5803 case MATCH_NO:
5804 break;
5805
5806 case MATCH_ERROR:
5807 return MATCH_ERROR;
5808 }
5809
5810 next_item:
5811 if (gfc_match_eos () == MATCH_YES)
5812 break;
5813 if (gfc_match_char (',') != MATCH_YES)
5814 goto syntax;
5815 }
5816
5817 return MATCH_YES;
5818
5819syntax:
5820 gfc_error ("Syntax error in PROTECTED statement at %C");
5821 return MATCH_ERROR;
5822}
5823
5824
86bf520d 5825/* The PRIVATE statement is a bit weird in that it can be an attribute
df2fba9e 5826 declaration, but also works as a standalone statement inside of a
6de9cd9a
DN
5827 type declaration or a module. */
5828
5829match
636dff67 5830gfc_match_private (gfc_statement *st)
6de9cd9a
DN
5831{
5832
5833 if (gfc_match ("private") != MATCH_YES)
5834 return MATCH_NO;
5835
d51347f9 5836 if (gfc_current_state () != COMP_MODULE
30b608eb
DK
5837 && !(gfc_current_state () == COMP_DERIVED
5838 && gfc_state_stack->previous
5839 && gfc_state_stack->previous->state == COMP_MODULE)
5840 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
5841 && gfc_state_stack->previous && gfc_state_stack->previous->previous
5842 && gfc_state_stack->previous->previous->state == COMP_MODULE))
d51347f9
TB
5843 {
5844 gfc_error ("PRIVATE statement at %C is only allowed in the "
5845 "specification part of a module");
5846 return MATCH_ERROR;
5847 }
5848
6de9cd9a
DN
5849 if (gfc_current_state () == COMP_DERIVED)
5850 {
5851 if (gfc_match_eos () == MATCH_YES)
5852 {
5853 *st = ST_PRIVATE;
5854 return MATCH_YES;
5855 }
5856
5857 gfc_syntax_error (ST_PRIVATE);
5858 return MATCH_ERROR;
5859 }
5860
5861 if (gfc_match_eos () == MATCH_YES)
5862 {
5863 *st = ST_PRIVATE;
5864 return MATCH_YES;
5865 }
5866
5867 *st = ST_ATTR_DECL;
5868 return access_attr_decl (ST_PRIVATE);
5869}
5870
5871
5872match
636dff67 5873gfc_match_public (gfc_statement *st)
6de9cd9a
DN
5874{
5875
5876 if (gfc_match ("public") != MATCH_YES)
5877 return MATCH_NO;
5878
d51347f9
TB
5879 if (gfc_current_state () != COMP_MODULE)
5880 {
5881 gfc_error ("PUBLIC statement at %C is only allowed in the "
5882 "specification part of a module");
5883 return MATCH_ERROR;
5884 }
5885
6de9cd9a
DN
5886 if (gfc_match_eos () == MATCH_YES)
5887 {
5888 *st = ST_PUBLIC;
5889 return MATCH_YES;
5890 }
5891
5892 *st = ST_ATTR_DECL;
5893 return access_attr_decl (ST_PUBLIC);
5894}
5895
5896
5897/* Workhorse for gfc_match_parameter. */
5898
5899static match
5900do_parm (void)
5901{
5902 gfc_symbol *sym;
5903 gfc_expr *init;
5904 match m;
5905
5906 m = gfc_match_symbol (&sym, 0);
5907 if (m == MATCH_NO)
5908 gfc_error ("Expected variable name at %C in PARAMETER statement");
5909
5910 if (m != MATCH_YES)
5911 return m;
5912
5913 if (gfc_match_char ('=') == MATCH_NO)
5914 {
5915 gfc_error ("Expected = sign in PARAMETER statement at %C");
5916 return MATCH_ERROR;
5917 }
5918
5919 m = gfc_match_init_expr (&init);
5920 if (m == MATCH_NO)
5921 gfc_error ("Expected expression at %C in PARAMETER statement");
5922 if (m != MATCH_YES)
5923 return m;
5924
5925 if (sym->ts.type == BT_UNKNOWN
5926 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5927 {
5928 m = MATCH_ERROR;
5929 goto cleanup;
5930 }
5931
5932 if (gfc_check_assign_symbol (sym, init) == FAILURE
231b2fcc 5933 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5934 {
5935 m = MATCH_ERROR;
5936 goto cleanup;
5937 }
5938
1283ab12
TB
5939 if (sym->value)
5940 {
5941 gfc_error ("Initializing already initialized variable at %C");
5942 m = MATCH_ERROR;
5943 goto cleanup;
5944 }
5945
7e2eba4b
DE
5946 if (sym->ts.type == BT_CHARACTER
5947 && sym->ts.cl != NULL
5948 && sym->ts.cl->length != NULL
5949 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5950 && init->expr_type == EXPR_CONSTANT
357ad64f 5951 && init->ts.type == BT_CHARACTER)
7e2eba4b 5952 gfc_set_constant_character_len (
d2848082 5953 mpz_get_si (sym->ts.cl->length->value.integer), init, -1);
357ad64f
TB
5954 else if (sym->ts.type == BT_CHARACTER && sym->ts.cl != NULL
5955 && sym->ts.cl->length == NULL)
5956 {
5957 int clen;
5958 if (init->expr_type == EXPR_CONSTANT)
5959 {
5960 clen = init->value.character.length;
5961 sym->ts.cl->length = gfc_int_expr (clen);
5962 }
5963 else if (init->expr_type == EXPR_ARRAY)
5964 {
5965 gfc_expr *p = init->value.constructor->expr;
5966 clen = p->value.character.length;
5967 sym->ts.cl->length = gfc_int_expr (clen);
5968 }
5969 else if (init->ts.cl && init->ts.cl->length)
5970 sym->ts.cl->length = gfc_copy_expr (sym->value->ts.cl->length);
5971 }
7e2eba4b 5972
6de9cd9a
DN
5973 sym->value = init;
5974 return MATCH_YES;
5975
5976cleanup:
5977 gfc_free_expr (init);
5978 return m;
5979}
5980
5981
5982/* Match a parameter statement, with the weird syntax that these have. */
5983
5984match
5985gfc_match_parameter (void)
5986{
5987 match m;
5988
5989 if (gfc_match_char ('(') == MATCH_NO)
5990 return MATCH_NO;
5991
5992 for (;;)
5993 {
5994 m = do_parm ();
5995 if (m != MATCH_YES)
5996 break;
5997
5998 if (gfc_match (" )%t") == MATCH_YES)
5999 break;
6000
6001 if (gfc_match_char (',') != MATCH_YES)
6002 {
6003 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6004 m = MATCH_ERROR;
6005 break;
6006 }
6007 }
6008
6009 return m;
6010}
6011
6012
6013/* Save statements have a special syntax. */
6014
6015match
6016gfc_match_save (void)
6017{
9056bd70
TS
6018 char n[GFC_MAX_SYMBOL_LEN+1];
6019 gfc_common_head *c;
6de9cd9a
DN
6020 gfc_symbol *sym;
6021 match m;
6022
6023 if (gfc_match_eos () == MATCH_YES)
6024 {
6025 if (gfc_current_ns->seen_save)
6026 {
636dff67
SK
6027 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6028 "follows previous SAVE statement")
09e87839
AL
6029 == FAILURE)
6030 return MATCH_ERROR;
6de9cd9a
DN
6031 }
6032
6033 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6034 return MATCH_YES;
6035 }
6036
6037 if (gfc_current_ns->save_all)
6038 {
636dff67
SK
6039 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6040 "blanket SAVE statement")
09e87839
AL
6041 == FAILURE)
6042 return MATCH_ERROR;
6de9cd9a
DN
6043 }
6044
6045 gfc_match (" ::");
6046
6047 for (;;)
6048 {
6049 m = gfc_match_symbol (&sym, 0);
6050 switch (m)
6051 {
6052 case MATCH_YES:
636dff67
SK
6053 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6054 == FAILURE)
6de9cd9a
DN
6055 return MATCH_ERROR;
6056 goto next_item;
6057
6058 case MATCH_NO:
6059 break;
6060
6061 case MATCH_ERROR:
6062 return MATCH_ERROR;
6063 }
6064
9056bd70 6065 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
6066 if (m == MATCH_ERROR)
6067 return MATCH_ERROR;
6068 if (m == MATCH_NO)
6069 goto syntax;
6070
53814b8f 6071 c = gfc_get_common (n, 0);
9056bd70
TS
6072 c->saved = 1;
6073
6de9cd9a
DN
6074 gfc_current_ns->seen_save = 1;
6075
6076 next_item:
6077 if (gfc_match_eos () == MATCH_YES)
6078 break;
6079 if (gfc_match_char (',') != MATCH_YES)
6080 goto syntax;
6081 }
6082
6083 return MATCH_YES;
6084
6085syntax:
6086 gfc_error ("Syntax error in SAVE statement at %C");
6087 return MATCH_ERROR;
6088}
6089
6090
06469efd
PT
6091match
6092gfc_match_value (void)
6093{
6094 gfc_symbol *sym;
6095 match m;
6096
636dff67 6097 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
06469efd
PT
6098 == FAILURE)
6099 return MATCH_ERROR;
6100
6101 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6102 {
6103 return MATCH_ERROR;
6104 }
6105
6106 if (gfc_match_eos () == MATCH_YES)
6107 goto syntax;
6108
6109 for(;;)
6110 {
6111 m = gfc_match_symbol (&sym, 0);
6112 switch (m)
6113 {
6114 case MATCH_YES:
636dff67
SK
6115 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6116 == FAILURE)
06469efd
PT
6117 return MATCH_ERROR;
6118 goto next_item;
6119
6120 case MATCH_NO:
6121 break;
6122
6123 case MATCH_ERROR:
6124 return MATCH_ERROR;
6125 }
6126
6127 next_item:
6128 if (gfc_match_eos () == MATCH_YES)
6129 break;
6130 if (gfc_match_char (',') != MATCH_YES)
6131 goto syntax;
6132 }
6133
6134 return MATCH_YES;
6135
6136syntax:
6137 gfc_error ("Syntax error in VALUE statement at %C");
6138 return MATCH_ERROR;
6139}
6140
66e4ab31 6141
775e6c3a
TB
6142match
6143gfc_match_volatile (void)
6144{
6145 gfc_symbol *sym;
6146 match m;
6147
636dff67 6148 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
775e6c3a
TB
6149 == FAILURE)
6150 return MATCH_ERROR;
6151
6152 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6153 {
6154 return MATCH_ERROR;
6155 }
6156
6157 if (gfc_match_eos () == MATCH_YES)
6158 goto syntax;
6159
6160 for(;;)
6161 {
9bce3c1c
TB
6162 /* VOLATILE is special because it can be added to host-associated
6163 symbols locally. */
6164 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
6165 switch (m)
6166 {
6167 case MATCH_YES:
636dff67
SK
6168 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6169 == FAILURE)
775e6c3a
TB
6170 return MATCH_ERROR;
6171 goto next_item;
6172
6173 case MATCH_NO:
6174 break;
6175
6176 case MATCH_ERROR:
6177 return MATCH_ERROR;
6178 }
6179
6180 next_item:
6181 if (gfc_match_eos () == MATCH_YES)
6182 break;
6183 if (gfc_match_char (',') != MATCH_YES)
6184 goto syntax;
6185 }
6186
6187 return MATCH_YES;
6188
6189syntax:
6190 gfc_error ("Syntax error in VOLATILE statement at %C");
6191 return MATCH_ERROR;
6192}
6193
6194
6de9cd9a
DN
6195/* Match a module procedure statement. Note that we have to modify
6196 symbols in the parent's namespace because the current one was there
49de9e73 6197 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
6198
6199match
6200gfc_match_modproc (void)
6201{
6202 char name[GFC_MAX_SYMBOL_LEN + 1];
6203 gfc_symbol *sym;
6204 match m;
060fca4a 6205 gfc_namespace *module_ns;
2b77e908 6206 gfc_interface *old_interface_head, *interface;
6de9cd9a
DN
6207
6208 if (gfc_state_stack->state != COMP_INTERFACE
6209 || gfc_state_stack->previous == NULL
129d15a3
JW
6210 || current_interface.type == INTERFACE_NAMELESS
6211 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 6212 {
636dff67
SK
6213 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6214 "interface");
6de9cd9a
DN
6215 return MATCH_ERROR;
6216 }
6217
060fca4a
PT
6218 module_ns = gfc_current_ns->parent;
6219 for (; module_ns; module_ns = module_ns->parent)
6220 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6221 break;
6222
6223 if (module_ns == NULL)
6224 return MATCH_ERROR;
6225
2b77e908
FXC
6226 /* Store the current state of the interface. We will need it if we
6227 end up with a syntax error and need to recover. */
6228 old_interface_head = gfc_current_interface_head ();
6229
6de9cd9a
DN
6230 for (;;)
6231 {
2b77e908
FXC
6232 bool last = false;
6233
6de9cd9a
DN
6234 m = gfc_match_name (name);
6235 if (m == MATCH_NO)
6236 goto syntax;
6237 if (m != MATCH_YES)
6238 return MATCH_ERROR;
6239
2b77e908
FXC
6240 /* Check for syntax error before starting to add symbols to the
6241 current namespace. */
6242 if (gfc_match_eos () == MATCH_YES)
6243 last = true;
6244 if (!last && gfc_match_char (',') != MATCH_YES)
6245 goto syntax;
6246
6247 /* Now we're sure the syntax is valid, we process this item
6248 further. */
060fca4a 6249 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
6250 return MATCH_ERROR;
6251
6252 if (sym->attr.proc != PROC_MODULE
231b2fcc
TS
6253 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6254 sym->name, NULL) == FAILURE)
6de9cd9a
DN
6255 return MATCH_ERROR;
6256
6257 if (gfc_add_interface (sym) == FAILURE)
6258 return MATCH_ERROR;
6259
71f77fd7
PT
6260 sym->attr.mod_proc = 1;
6261
2b77e908 6262 if (last)
6de9cd9a 6263 break;
6de9cd9a
DN
6264 }
6265
6266 return MATCH_YES;
6267
6268syntax:
2b77e908
FXC
6269 /* Restore the previous state of the interface. */
6270 interface = gfc_current_interface_head ();
6271 gfc_set_current_interface_head (old_interface_head);
6272
6273 /* Free the new interfaces. */
6274 while (interface != old_interface_head)
6275 {
6276 gfc_interface *i = interface->next;
6277 gfc_free (interface);
6278 interface = i;
6279 }
6280
6281 /* And issue a syntax error. */
6de9cd9a
DN
6282 gfc_syntax_error (ST_MODULE_PROC);
6283 return MATCH_ERROR;
6284}
6285
6286
7d1f1e61
PT
6287/* Check a derived type that is being extended. */
6288static gfc_symbol*
6289check_extended_derived_type (char *name)
6290{
6291 gfc_symbol *extended;
6292
6293 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6294 {
6295 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6296 return NULL;
6297 }
6298
6299 if (!extended)
6300 {
6301 gfc_error ("No such symbol in TYPE definition at %C");
6302 return NULL;
6303 }
6304
6305 if (extended->attr.flavor != FL_DERIVED)
6306 {
6307 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6308 "derived type", name);
6309 return NULL;
6310 }
6311
6312 if (extended->attr.is_bind_c)
6313 {
6314 gfc_error ("'%s' cannot be extended at %C because it "
6315 "is BIND(C)", extended->name);
6316 return NULL;
6317 }
6318
6319 if (extended->attr.sequence)
6320 {
6321 gfc_error ("'%s' cannot be extended at %C because it "
6322 "is a SEQUENCE type", extended->name);
6323 return NULL;
6324 }
6325
6326 return extended;
6327}
6328
6329
a8b3b0b6
CR
6330/* Match the optional attribute specifiers for a type declaration.
6331 Return MATCH_ERROR if an error is encountered in one of the handled
6332 attributes (public, private, bind(c)), MATCH_NO if what's found is
6333 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6334 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
6335
6336match
7d1f1e61 6337gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6de9cd9a 6338{
a8b3b0b6 6339 /* See if the derived type is marked as private. */
6de9cd9a
DN
6340 if (gfc_match (" , private") == MATCH_YES)
6341 {
d51347f9 6342 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 6343 {
d51347f9
TB
6344 gfc_error ("Derived type at %C can only be PRIVATE in the "
6345 "specification part of a module");
6de9cd9a
DN
6346 return MATCH_ERROR;
6347 }
6348
a8b3b0b6 6349 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6de9cd9a 6350 return MATCH_ERROR;
6de9cd9a 6351 }
a8b3b0b6 6352 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 6353 {
d51347f9 6354 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 6355 {
d51347f9
TB
6356 gfc_error ("Derived type at %C can only be PUBLIC in the "
6357 "specification part of a module");
6de9cd9a
DN
6358 return MATCH_ERROR;
6359 }
6360
a8b3b0b6 6361 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6de9cd9a 6362 return MATCH_ERROR;
6de9cd9a 6363 }
52f49934 6364 else if (gfc_match (" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
6365 {
6366 /* If the type is defined to be bind(c) it then needs to make
6367 sure that all fields are interoperable. This will
6368 need to be a semantic check on the finished derived type.
6369 See 15.2.3 (lines 9-12) of F2003 draft. */
6370 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6371 return MATCH_ERROR;
6372
6373 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6374 }
52f49934
DK
6375 else if (gfc_match (" , abstract") == MATCH_YES)
6376 {
6377 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6378 == FAILURE)
6379 return MATCH_ERROR;
6380
6381 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6382 return MATCH_ERROR;
6383 }
7d1f1e61
PT
6384 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6385 {
63a3341a 6386 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7d1f1e61
PT
6387 return MATCH_ERROR;
6388 }
a8b3b0b6
CR
6389 else
6390 return MATCH_NO;
6391
6392 /* If we get here, something matched. */
6393 return MATCH_YES;
6394}
6395
6396
6397/* Match the beginning of a derived type declaration. If a type name
6398 was the result of a function, then it is possible to have a symbol
6399 already to be known as a derived type yet have no components. */
6400
6401match
6402gfc_match_derived_decl (void)
6403{
6404 char name[GFC_MAX_SYMBOL_LEN + 1];
7d1f1e61 6405 char parent[GFC_MAX_SYMBOL_LEN + 1];
a8b3b0b6
CR
6406 symbol_attribute attr;
6407 gfc_symbol *sym;
7d1f1e61 6408 gfc_symbol *extended;
a8b3b0b6
CR
6409 match m;
6410 match is_type_attr_spec = MATCH_NO;
e7303e85 6411 bool seen_attr = false;
a8b3b0b6
CR
6412
6413 if (gfc_current_state () == COMP_DERIVED)
6414 return MATCH_NO;
6415
7d1f1e61
PT
6416 name[0] = '\0';
6417 parent[0] = '\0';
a8b3b0b6 6418 gfc_clear_attr (&attr);
7d1f1e61 6419 extended = NULL;
a8b3b0b6
CR
6420
6421 do
6422 {
7d1f1e61 6423 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
a8b3b0b6
CR
6424 if (is_type_attr_spec == MATCH_ERROR)
6425 return MATCH_ERROR;
e7303e85
FXC
6426 if (is_type_attr_spec == MATCH_YES)
6427 seen_attr = true;
a8b3b0b6 6428 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 6429
63a3341a
PT
6430 /* Deal with derived type extensions. The extension attribute has
6431 been added to 'attr' but now the parent type must be found and
6432 checked. */
7d1f1e61
PT
6433 if (parent[0])
6434 extended = check_extended_derived_type (parent);
6435
6436 if (parent[0] && !extended)
6437 return MATCH_ERROR;
6438
e7303e85 6439 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6de9cd9a
DN
6440 {
6441 gfc_error ("Expected :: in TYPE definition at %C");
6442 return MATCH_ERROR;
6443 }
6444
6445 m = gfc_match (" %n%t", name);
6446 if (m != MATCH_YES)
6447 return m;
6448
e9c06563
TB
6449 /* Make sure the name is not the name of an intrinsic type. */
6450 if (gfc_is_intrinsic_typename (name))
6de9cd9a 6451 {
636dff67
SK
6452 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6453 "type", name);
6de9cd9a
DN
6454 return MATCH_ERROR;
6455 }
6456
6457 if (gfc_get_symbol (name, NULL, &sym))
6458 return MATCH_ERROR;
6459
6460 if (sym->ts.type != BT_UNKNOWN)
6461 {
6462 gfc_error ("Derived type name '%s' at %C already has a basic type "
6463 "of %s", sym->name, gfc_typename (&sym->ts));
6464 return MATCH_ERROR;
6465 }
6466
6467 /* The symbol may already have the derived attribute without the
6468 components. The ways this can happen is via a function
6469 definition, an INTRINSIC statement or a subtype in another
6470 derived type that is a pointer. The first part of the AND clause
df2fba9e 6471 is true if the symbol is not the return value of a function. */
6de9cd9a 6472 if (sym->attr.flavor != FL_DERIVED
231b2fcc 6473 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
6474 return MATCH_ERROR;
6475
9fa6b0af 6476 if (sym->components != NULL || sym->attr.zero_comp)
6de9cd9a 6477 {
636dff67
SK
6478 gfc_error ("Derived type definition of '%s' at %C has already been "
6479 "defined", sym->name);
6de9cd9a
DN
6480 return MATCH_ERROR;
6481 }
6482
6483 if (attr.access != ACCESS_UNKNOWN
231b2fcc 6484 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6de9cd9a
DN
6485 return MATCH_ERROR;
6486
a8b3b0b6
CR
6487 /* See if the derived type was labeled as bind(c). */
6488 if (attr.is_bind_c != 0)
6489 sym->attr.is_bind_c = attr.is_bind_c;
6490
34523524
DK
6491 /* Construct the f2k_derived namespace if it is not yet there. */
6492 if (!sym->f2k_derived)
6493 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7d1f1e61
PT
6494
6495 if (extended && !sym->components)
6496 {
6497 gfc_component *p;
6498 gfc_symtree *st;
6499
6500 /* Add the extended derived type as the first component. */
6501 gfc_add_component (sym, parent, &p);
63a3341a 6502 sym->attr.extension = attr.extension;
7d1f1e61
PT
6503 extended->refs++;
6504 gfc_set_sym_referenced (extended);
6505
6506 p->ts.type = BT_DERIVED;
6507 p->ts.derived = extended;
6508 p->initializer = gfc_default_initializer (&p->ts);
6509
6510 /* Provide the links between the extended type and its extension. */
6511 if (!extended->f2k_derived)
6512 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6513 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6514 st->n.sym = sym;
6515 }
6516
52f49934
DK
6517 /* Take over the ABSTRACT attribute. */
6518 sym->attr.abstract = attr.abstract;
6519
6de9cd9a
DN
6520 gfc_new_block = sym;
6521
6522 return MATCH_YES;
6523}
83d890b9
AL
6524
6525
6526/* Cray Pointees can be declared as:
6527 pointer (ipt, a (n,m,...,*))
6528 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6529 cheat and set a constant bound of 1 for the last dimension, if this
6530 is the case. Since there is no bounds-checking for Cray Pointees,
6531 this will be okay. */
6532
17b1d2a0 6533gfc_try
83d890b9
AL
6534gfc_mod_pointee_as (gfc_array_spec *as)
6535{
6536 as->cray_pointee = true; /* This will be useful to know later. */
6537 if (as->type == AS_ASSUMED_SIZE)
6538 {
6539 as->type = AS_EXPLICIT;
6540 as->upper[as->rank - 1] = gfc_int_expr (1);
6541 as->cp_was_assumed = true;
6542 }
6543 else if (as->type == AS_ASSUMED_SHAPE)
6544 {
6545 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6546 return MATCH_ERROR;
6547 }
6548 return MATCH_YES;
6549}
25d8f0a2
TS
6550
6551
6552/* Match the enum definition statement, here we are trying to match
6553 the first line of enum definition statement.
6554 Returns MATCH_YES if match is found. */
6555
6556match
6557gfc_match_enum (void)
6558{
6559 match m;
6560
6561 m = gfc_match_eos ();
6562 if (m != MATCH_YES)
6563 return m;
6564
6133c68a 6565 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
25d8f0a2
TS
6566 == FAILURE)
6567 return MATCH_ERROR;
6568
6569 return MATCH_YES;
6570}
6571
6572
6133c68a
TS
6573/* Match a variable name with an optional initializer. When this
6574 subroutine is called, a variable is expected to be parsed next.
6575 Depending on what is happening at the moment, updates either the
6576 symbol table or the current interface. */
6577
6578static match
6579enumerator_decl (void)
6580{
6581 char name[GFC_MAX_SYMBOL_LEN + 1];
6582 gfc_expr *initializer;
6583 gfc_array_spec *as = NULL;
6584 gfc_symbol *sym;
6585 locus var_locus;
6586 match m;
17b1d2a0 6587 gfc_try t;
6133c68a
TS
6588 locus old_locus;
6589
6590 initializer = NULL;
6591 old_locus = gfc_current_locus;
6592
6593 /* When we get here, we've just matched a list of attributes and
6594 maybe a type and a double colon. The next thing we expect to see
6595 is the name of the symbol. */
6596 m = gfc_match_name (name);
6597 if (m != MATCH_YES)
6598 goto cleanup;
6599
6600 var_locus = gfc_current_locus;
6601
6602 /* OK, we've successfully matched the declaration. Now put the
6603 symbol in the current namespace. If we fail to create the symbol,
6604 bail out. */
6605 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6606 {
6607 m = MATCH_ERROR;
6608 goto cleanup;
6609 }
6610
6611 /* The double colon must be present in order to have initializers.
6612 Otherwise the statement is ambiguous with an assignment statement. */
6613 if (colon_seen)
6614 {
6615 if (gfc_match_char ('=') == MATCH_YES)
6616 {
6617 m = gfc_match_init_expr (&initializer);
6618 if (m == MATCH_NO)
6619 {
6620 gfc_error ("Expected an initialization expression at %C");
6621 m = MATCH_ERROR;
6622 }
6623
6624 if (m != MATCH_YES)
6625 goto cleanup;
6626 }
6627 }
6628
6629 /* If we do not have an initializer, the initialization value of the
6630 previous enumerator (stored in last_initializer) is incremented
6631 by 1 and is used to initialize the current enumerator. */
6632 if (initializer == NULL)
6633 initializer = gfc_enum_initializer (last_initializer, old_locus);
d51347f9 6634
6133c68a
TS
6635 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6636 {
6637 gfc_error("ENUMERATOR %L not initialized with integer expression",
6638 &var_locus);
d51347f9 6639 m = MATCH_ERROR;
6133c68a
TS
6640 gfc_free_enum_history ();
6641 goto cleanup;
6642 }
6643
6644 /* Store this current initializer, for the next enumerator variable
6645 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6646 use last_initializer below. */
6647 last_initializer = initializer;
6648 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6649
6650 /* Maintain enumerator history. */
6651 gfc_find_symbol (name, NULL, 0, &sym);
6652 create_enum_history (sym, last_initializer);
6653
6654 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6655
6656cleanup:
6657 /* Free stuff up and return. */
6658 gfc_free_expr (initializer);
6659
6660 return m;
6661}
6662
6663
66e4ab31 6664/* Match the enumerator definition statement. */
25d8f0a2
TS
6665
6666match
6667gfc_match_enumerator_def (void)
6668{
6669 match m;
17b1d2a0 6670 gfc_try t;
d51347f9 6671
25d8f0a2 6672 gfc_clear_ts (&current_ts);
d51347f9 6673
25d8f0a2
TS
6674 m = gfc_match (" enumerator");
6675 if (m != MATCH_YES)
6676 return m;
6133c68a
TS
6677
6678 m = gfc_match (" :: ");
6679 if (m == MATCH_ERROR)
6680 return m;
6681
6682 colon_seen = (m == MATCH_YES);
d51347f9 6683
25d8f0a2
TS
6684 if (gfc_current_state () != COMP_ENUM)
6685 {
6686 gfc_error ("ENUM definition statement expected before %C");
6687 gfc_free_enum_history ();
6688 return MATCH_ERROR;
6689 }
6690
6691 (&current_ts)->type = BT_INTEGER;
6692 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 6693
6133c68a
TS
6694 gfc_clear_attr (&current_attr);
6695 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6696 if (t == FAILURE)
25d8f0a2 6697 {
6133c68a 6698 m = MATCH_ERROR;
25d8f0a2
TS
6699 goto cleanup;
6700 }
6701
25d8f0a2
TS
6702 for (;;)
6703 {
6133c68a 6704 m = enumerator_decl ();
25d8f0a2
TS
6705 if (m == MATCH_ERROR)
6706 goto cleanup;
6707 if (m == MATCH_NO)
6708 break;
6709
6710 if (gfc_match_eos () == MATCH_YES)
6711 goto cleanup;
6712 if (gfc_match_char (',') != MATCH_YES)
6713 break;
6714 }
6715
6716 if (gfc_current_state () == COMP_ENUM)
6717 {
6718 gfc_free_enum_history ();
6719 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6720 m = MATCH_ERROR;
6721 }
6722
6723cleanup:
6724 gfc_free_array_spec (current_as);
6725 current_as = NULL;
6726 return m;
6727
6728}
6729
f6fad28e 6730
30b608eb
DK
6731/* Match binding attributes. */
6732
6733static match
e157f736 6734match_binding_attributes (gfc_typebound_proc* ba, bool generic)
30b608eb
DK
6735{
6736 bool found_passing = false;
6737 match m;
6738
6739 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
6740 this case the defaults are in there. */
6741 ba->access = ACCESS_UNKNOWN;
6742 ba->pass_arg = NULL;
6743 ba->pass_arg_num = 0;
6744 ba->nopass = 0;
6745 ba->non_overridable = 0;
6746
6747 /* If we find a comma, we believe there are binding attributes. */
6748 if (gfc_match_char (',') == MATCH_NO)
e157f736
DK
6749 {
6750 ba->access = gfc_typebound_default_access;
6751 return MATCH_NO;
6752 }
30b608eb
DK
6753
6754 do
6755 {
e157f736
DK
6756 /* Access specifier. */
6757
6758 m = gfc_match (" public");
30b608eb
DK
6759 if (m == MATCH_ERROR)
6760 goto error;
6761 if (m == MATCH_YES)
6762 {
e157f736 6763 if (ba->access != ACCESS_UNKNOWN)
30b608eb 6764 {
e157f736 6765 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
6766 goto error;
6767 }
6768
e157f736 6769 ba->access = ACCESS_PUBLIC;
30b608eb
DK
6770 continue;
6771 }
6772
e157f736 6773 m = gfc_match (" private");
30b608eb
DK
6774 if (m == MATCH_ERROR)
6775 goto error;
6776 if (m == MATCH_YES)
6777 {
e157f736 6778 if (ba->access != ACCESS_UNKNOWN)
30b608eb 6779 {
e157f736 6780 gfc_error ("Duplicate access-specifier at %C");
30b608eb
DK
6781 goto error;
6782 }
6783
e157f736 6784 ba->access = ACCESS_PRIVATE;
30b608eb
DK
6785 continue;
6786 }
6787
e157f736
DK
6788 /* If inside GENERIC, the following is not allowed. */
6789 if (!generic)
30b608eb 6790 {
30b608eb 6791
e157f736
DK
6792 /* NOPASS flag. */
6793 m = gfc_match (" nopass");
6794 if (m == MATCH_ERROR)
6795 goto error;
6796 if (m == MATCH_YES)
30b608eb 6797 {
e157f736
DK
6798 if (found_passing)
6799 {
6800 gfc_error ("Binding attributes already specify passing,"
6801 " illegal NOPASS at %C");
6802 goto error;
6803 }
6804
6805 found_passing = true;
6806 ba->nopass = 1;
6807 continue;
30b608eb
DK
6808 }
6809
e157f736
DK
6810 /* NON_OVERRIDABLE flag. */
6811 m = gfc_match (" non_overridable");
30b608eb
DK
6812 if (m == MATCH_ERROR)
6813 goto error;
6814 if (m == MATCH_YES)
e157f736
DK
6815 {
6816 if (ba->non_overridable)
6817 {
6818 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
6819 goto error;
6820 }
30b608eb 6821
e157f736
DK
6822 ba->non_overridable = 1;
6823 continue;
6824 }
30b608eb 6825
e157f736
DK
6826 /* DEFERRED flag. */
6827 /* TODO: Handle really once implemented. */
6828 m = gfc_match (" deferred");
6829 if (m == MATCH_ERROR)
6830 goto error;
6831 if (m == MATCH_YES)
30b608eb 6832 {
e157f736 6833 gfc_error ("DEFERRED not yet implemented at %C");
30b608eb
DK
6834 goto error;
6835 }
6836
e157f736
DK
6837 /* PASS possibly including argument. */
6838 m = gfc_match (" pass");
6839 if (m == MATCH_ERROR)
6840 goto error;
6841 if (m == MATCH_YES)
30b608eb 6842 {
e157f736
DK
6843 char arg[GFC_MAX_SYMBOL_LEN + 1];
6844
6845 if (found_passing)
6846 {
6847 gfc_error ("Binding attributes already specify passing,"
6848 " illegal PASS at %C");
6849 goto error;
6850 }
6851
6852 m = gfc_match (" ( %n )", arg);
6853 if (m == MATCH_ERROR)
6854 goto error;
6855 if (m == MATCH_YES)
6856 ba->pass_arg = xstrdup (arg);
6857 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
6858
6859 found_passing = true;
6860 ba->nopass = 0;
6861 continue;
30b608eb
DK
6862 }
6863
30b608eb
DK
6864 }
6865
6866 /* Nothing matching found. */
e157f736
DK
6867 if (generic)
6868 gfc_error ("Expected access-specifier at %C");
6869 else
6870 gfc_error ("Expected binding attribute at %C");
30b608eb
DK
6871 goto error;
6872 }
6873 while (gfc_match_char (',') == MATCH_YES);
6874
e157f736
DK
6875 if (ba->access == ACCESS_UNKNOWN)
6876 ba->access = gfc_typebound_default_access;
6877
30b608eb
DK
6878 return MATCH_YES;
6879
6880error:
6881 gfc_free (ba->pass_arg);
6882 return MATCH_ERROR;
6883}
6884
6885
6886/* Match a PROCEDURE specific binding inside a derived type. */
6887
6888static match
6889match_procedure_in_type (void)
6890{
6891 char name[GFC_MAX_SYMBOL_LEN + 1];
6892 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
6893 char* target;
6894 gfc_typebound_proc* tb;
6895 bool seen_colons;
6896 bool seen_attrs;
6897 match m;
6898 gfc_symtree* stree;
6899 gfc_namespace* ns;
6900 gfc_symbol* block;
6901
6902 /* Check current state. */
6903 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
6904 block = gfc_state_stack->previous->sym;
6905 gcc_assert (block);
6906
6907 /* TODO: Really implement PROCEDURE(interface). */
6908 if (gfc_match (" (") == MATCH_YES)
6909 {
6910 gfc_error ("Procedure with interface only allowed in abstract types at"
6911 " %C");
6912 return MATCH_ERROR;
6913 }
6914
6915 /* Construct the data structure. */
8e1f752a 6916 tb = gfc_get_typebound_proc ();
30b608eb 6917 tb->where = gfc_current_locus;
e157f736 6918 tb->is_generic = 0;
30b608eb
DK
6919
6920 /* Match binding attributes. */
e157f736 6921 m = match_binding_attributes (tb, false);
30b608eb
DK
6922 if (m == MATCH_ERROR)
6923 return m;
6924 seen_attrs = (m == MATCH_YES);
6925
6926 /* Match the colons. */
6927 m = gfc_match (" ::");
6928 if (m == MATCH_ERROR)
6929 return m;
6930 seen_colons = (m == MATCH_YES);
6931 if (seen_attrs && !seen_colons)
6932 {
6933 gfc_error ("Expected '::' after binding-attributes at %C");
6934 return MATCH_ERROR;
6935 }
6936
6937 /* Match the binding name. */
6938 m = gfc_match_name (name);
6939 if (m == MATCH_ERROR)
6940 return m;
6941 if (m == MATCH_NO)
6942 {
6943 gfc_error ("Expected binding name at %C");
6944 return MATCH_ERROR;
6945 }
6946
6947 /* Try to match the '=> target', if it's there. */
6948 target = NULL;
6949 m = gfc_match (" =>");
6950 if (m == MATCH_ERROR)
6951 return m;
6952 if (m == MATCH_YES)
6953 {
6954 if (!seen_colons)
6955 {
6956 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
6957 " at %C");
6958 return MATCH_ERROR;
6959 }
6960
6961 m = gfc_match_name (target_buf);
6962 if (m == MATCH_ERROR)
6963 return m;
6964 if (m == MATCH_NO)
6965 {
6966 gfc_error ("Expected binding target after '=>' at %C");
6967 return MATCH_ERROR;
6968 }
6969 target = target_buf;
6970 }
6971
6972 /* Now we should have the end. */
6973 m = gfc_match_eos ();
6974 if (m == MATCH_ERROR)
6975 return m;
6976 if (m == MATCH_NO)
6977 {
6978 gfc_error ("Junk after PROCEDURE declaration at %C");
6979 return MATCH_ERROR;
6980 }
6981
6982 /* If no target was found, it has the same name as the binding. */
6983 if (!target)
6984 target = name;
6985
6986 /* Get the namespace to insert the symbols into. */
6987 ns = block->f2k_derived;
6988 gcc_assert (ns);
6989
6990 /* See if we already have a binding with this name in the symtree which would
e157f736
DK
6991 be an error. If a GENERIC already targetted this binding, it may be
6992 already there but then typebound is still NULL. */
30b608eb 6993 stree = gfc_find_symtree (ns->sym_root, name);
e157f736 6994 if (stree && stree->typebound)
30b608eb
DK
6995 {
6996 gfc_error ("There's already a procedure with binding name '%s' for the"
6997 " derived type '%s' at %C", name, block->name);
6998 return MATCH_ERROR;
6999 }
7000
7001 /* Insert it and set attributes. */
7002 if (gfc_get_sym_tree (name, ns, &stree))
7003 return MATCH_ERROR;
e157f736 7004 if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific))
30b608eb 7005 return MATCH_ERROR;
e157f736 7006 gfc_set_sym_referenced (tb->u.specific->n.sym);
30b608eb
DK
7007 stree->typebound = tb;
7008
7009 return MATCH_YES;
7010}
7011
7012
e157f736
DK
7013/* Match a GENERIC procedure binding inside a derived type. */
7014
7015match
7016gfc_match_generic (void)
7017{
7018 char name[GFC_MAX_SYMBOL_LEN + 1];
7019 gfc_symbol* block;
7020 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7021 gfc_typebound_proc* tb;
7022 gfc_symtree* st;
7023 gfc_namespace* ns;
7024 match m;
7025
7026 /* Check current state. */
7027 if (gfc_current_state () == COMP_DERIVED)
7028 {
7029 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7030 return MATCH_ERROR;
7031 }
7032 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7033 return MATCH_NO;
7034 block = gfc_state_stack->previous->sym;
7035 ns = block->f2k_derived;
7036 gcc_assert (block && ns);
7037
7038 /* See if we get an access-specifier. */
7039 m = match_binding_attributes (&tbattr, true);
7040 if (m == MATCH_ERROR)
7041 goto error;
7042
7043 /* Now the colons, those are required. */
7044 if (gfc_match (" ::") != MATCH_YES)
7045 {
7046 gfc_error ("Expected '::' at %C");
7047 goto error;
7048 }
7049
7050 /* The binding name and =>. */
7051 m = gfc_match (" %n =>", name);
7052 if (m == MATCH_ERROR)
7053 return MATCH_ERROR;
7054 if (m == MATCH_NO)
7055 {
7056 gfc_error ("Expected generic name at %C");
7057 goto error;
7058 }
7059
7060 /* If there's already something with this name, check that it is another
7061 GENERIC and then extend that rather than build a new node. */
7062 st = gfc_find_symtree (ns->sym_root, name);
7063 if (st)
7064 {
7065 if (!st->typebound || !st->typebound->is_generic)
7066 {
7067 gfc_error ("There's already a non-generic procedure with binding name"
7068 " '%s' for the derived type '%s' at %C",
7069 name, block->name);
7070 goto error;
7071 }
7072
7073 tb = st->typebound;
7074 if (tb->access != tbattr.access)
7075 {
7076 gfc_error ("Binding at %C must have the same access as already"
7077 " defined binding '%s'", name);
7078 goto error;
7079 }
7080 }
7081 else
7082 {
7083 if (gfc_get_sym_tree (name, ns, &st))
7084 return MATCH_ERROR;
7085
7086 st->typebound = tb = gfc_get_typebound_proc ();
7087 tb->where = gfc_current_locus;
7088 tb->access = tbattr.access;
7089 tb->is_generic = 1;
7090 tb->u.generic = NULL;
7091 }
7092
7093 /* Now, match all following names as specific targets. */
7094 do
7095 {
7096 gfc_symtree* target_st;
7097 gfc_tbp_generic* target;
7098
7099 m = gfc_match_name (name);
7100 if (m == MATCH_ERROR)
7101 goto error;
7102 if (m == MATCH_NO)
7103 {
7104 gfc_error ("Expected specific binding name at %C");
7105 goto error;
7106 }
7107
7108 if (gfc_get_sym_tree (name, ns, &target_st))
7109 goto error;
7110
7111 /* See if this is a duplicate specification. */
7112 for (target = tb->u.generic; target; target = target->next)
7113 if (target_st == target->specific_st)
7114 {
7115 gfc_error ("'%s' already defined as specific binding for the"
7116 " generic '%s' at %C", name, st->n.sym->name);
7117 goto error;
7118 }
7119
7120 gfc_set_sym_referenced (target_st->n.sym);
7121
7122 target = gfc_get_tbp_generic ();
7123 target->specific_st = target_st;
7124 target->specific = NULL;
7125 target->next = tb->u.generic;
7126 tb->u.generic = target;
7127 }
7128 while (gfc_match (" ,") == MATCH_YES);
7129
7130 /* Here should be the end. */
7131 if (gfc_match_eos () != MATCH_YES)
7132 {
7133 gfc_error ("Junk after GENERIC binding at %C");
7134 goto error;
7135 }
7136
7137 return MATCH_YES;
7138
7139error:
7140 return MATCH_ERROR;
7141}
7142
7143
34523524
DK
7144/* Match a FINAL declaration inside a derived type. */
7145
7146match
7147gfc_match_final_decl (void)
7148{
7149 char name[GFC_MAX_SYMBOL_LEN + 1];
7150 gfc_symbol* sym;
7151 match m;
7152 gfc_namespace* module_ns;
7153 bool first, last;
30b608eb 7154 gfc_symbol* block;
34523524 7155
30b608eb 7156 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
34523524
DK
7157 {
7158 gfc_error ("FINAL declaration at %C must be inside a derived type "
30b608eb 7159 "CONTAINS section");
34523524
DK
7160 return MATCH_ERROR;
7161 }
7162
30b608eb
DK
7163 block = gfc_state_stack->previous->sym;
7164 gcc_assert (block);
34523524 7165
30b608eb
DK
7166 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7167 || gfc_state_stack->previous->previous->state != COMP_MODULE)
34523524
DK
7168 {
7169 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7170 " specification part of a MODULE");
7171 return MATCH_ERROR;
7172 }
7173
7174 module_ns = gfc_current_ns;
7175 gcc_assert (module_ns);
7176 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7177
7178 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7179 if (gfc_match (" ::") == MATCH_ERROR)
7180 return MATCH_ERROR;
7181
7182 /* Match the sequence of procedure names. */
7183 first = true;
7184 last = false;
7185 do
7186 {
7187 gfc_finalizer* f;
7188
7189 if (first && gfc_match_eos () == MATCH_YES)
7190 {
7191 gfc_error ("Empty FINAL at %C");
7192 return MATCH_ERROR;
7193 }
7194
7195 m = gfc_match_name (name);
7196 if (m == MATCH_NO)
7197 {
7198 gfc_error ("Expected module procedure name at %C");
7199 return MATCH_ERROR;
7200 }
7201 else if (m != MATCH_YES)
7202 return MATCH_ERROR;
7203
7204 if (gfc_match_eos () == MATCH_YES)
7205 last = true;
7206 if (!last && gfc_match_char (',') != MATCH_YES)
7207 {
7208 gfc_error ("Expected ',' at %C");
7209 return MATCH_ERROR;
7210 }
7211
7212 if (gfc_get_symbol (name, module_ns, &sym))
7213 {
7214 gfc_error ("Unknown procedure name \"%s\" at %C", name);
7215 return MATCH_ERROR;
7216 }
7217
7218 /* Mark the symbol as module procedure. */
7219 if (sym->attr.proc != PROC_MODULE
7220 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7221 sym->name, NULL) == FAILURE)
7222 return MATCH_ERROR;
7223
7224 /* Check if we already have this symbol in the list, this is an error. */
30b608eb 7225 for (f = block->f2k_derived->finalizers; f; f = f->next)
f6fad28e 7226 if (f->proc_sym == sym)
34523524
DK
7227 {
7228 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7229 name);
7230 return MATCH_ERROR;
7231 }
7232
7233 /* Add this symbol to the list of finalizers. */
30b608eb 7234 gcc_assert (block->f2k_derived);
34523524 7235 ++sym->refs;
ece3f663 7236 f = XCNEW (gfc_finalizer);
f6fad28e
DK
7237 f->proc_sym = sym;
7238 f->proc_tree = NULL;
34523524 7239 f->where = gfc_current_locus;
30b608eb
DK
7240 f->next = block->f2k_derived->finalizers;
7241 block->f2k_derived->finalizers = f;
34523524
DK
7242
7243 first = false;
7244 }
7245 while (!last);
7246
7247 return MATCH_YES;
7248}