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