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