]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
* sched-vis.c (print_exp): Handle FMA explicitly. Simplify handling
[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
9b58b4c7 592static gfc_try
e97ac7c0 593merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
594{
595 int i;
596
9b58b4c7 597 if ((from->type == AS_ASSUMED_RANK && to->corank)
598 || (to->type == AS_ASSUMED_RANK && from->corank))
599 {
600 gfc_error ("The assumed-rank array at %C shall not have a codimension");
601 return FAILURE;
602 }
f00f6dd6 603
e97ac7c0 604 if (to->rank == 0 && from->rank > 0)
605 {
606 to->rank = from->rank;
607 to->type = from->type;
608 to->cray_pointee = from->cray_pointee;
609 to->cp_was_assumed = from->cp_was_assumed;
610
611 for (i = 0; i < to->corank; i++)
612 {
613 to->lower[from->rank + i] = to->lower[i];
614 to->upper[from->rank + i] = to->upper[i];
615 }
616 for (i = 0; i < from->rank; i++)
617 {
618 if (copy)
619 {
620 to->lower[i] = gfc_copy_expr (from->lower[i]);
621 to->upper[i] = gfc_copy_expr (from->upper[i]);
622 }
623 else
624 {
625 to->lower[i] = from->lower[i];
626 to->upper[i] = from->upper[i];
627 }
628 }
629 }
630 else if (to->corank == 0 && from->corank > 0)
631 {
632 to->corank = from->corank;
633 to->cotype = from->cotype;
634
635 for (i = 0; i < from->corank; i++)
636 {
637 if (copy)
638 {
639 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
640 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
641 }
642 else
643 {
644 to->lower[to->rank + i] = from->lower[i];
645 to->upper[to->rank + i] = from->upper[i];
646 }
647 }
648 }
9b58b4c7 649
650 return SUCCESS;
e97ac7c0 651}
652
653
4ee9c684 654/* Match an intent specification. Since this can only happen after an
655 INTENT word, a legal intent-spec must follow. */
656
657static sym_intent
658match_intent_spec (void)
659{
660
661 if (gfc_match (" ( in out )") == MATCH_YES)
662 return INTENT_INOUT;
663 if (gfc_match (" ( in )") == MATCH_YES)
664 return INTENT_IN;
665 if (gfc_match (" ( out )") == MATCH_YES)
666 return INTENT_OUT;
667
668 gfc_error ("Bad INTENT specification at %C");
669 return INTENT_UNKNOWN;
670}
671
672
673/* Matches a character length specification, which is either a
3e715c81 674 specification expression, '*', or ':'. */
4ee9c684 675
676static match
3e715c81 677char_len_param_value (gfc_expr **expr, bool *deferred)
4ee9c684 678{
294d58d3 679 match m;
680
3e715c81 681 *expr = NULL;
682 *deferred = false;
683
4ee9c684 684 if (gfc_match_char ('*') == MATCH_YES)
3e715c81 685 return MATCH_YES;
686
687 if (gfc_match_char (':') == MATCH_YES)
4ee9c684 688 {
f25dbbf7 689 if (gfc_notify_std (GFC_STD_F2003, "deferred type "
3e715c81 690 "parameter at %C") == FAILURE)
691 return MATCH_ERROR;
692
693 *deferred = true;
694
4ee9c684 695 return MATCH_YES;
696 }
697
294d58d3 698 m = gfc_match_expr (expr);
40de255b 699
700 if (m == MATCH_YES
701 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
702 return MATCH_ERROR;
703
294d58d3 704 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
705 {
706 if ((*expr)->value.function.actual
707 && (*expr)->value.function.actual->expr->symtree)
708 {
709 gfc_expr *e;
710 e = (*expr)->value.function.actual->expr;
711 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
712 && e->expr_type == EXPR_VARIABLE)
713 {
714 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
715 goto syntax;
716 if (e->symtree->n.sym->ts.type == BT_CHARACTER
eeebe20b 717 && e->symtree->n.sym->ts.u.cl
718 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
294d58d3 719 goto syntax;
720 }
721 }
722 }
723 return m;
724
725syntax:
726 gfc_error ("Conflict in attributes of function argument at %C");
727 return MATCH_ERROR;
4ee9c684 728}
729
730
731/* A character length is a '*' followed by a literal integer or a
732 char_len_param_value in parenthesis. */
733
734static match
ae0426ce 735match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
4ee9c684 736{
3bd3b616 737 int length;
4ee9c684 738 match m;
739
3e715c81 740 *deferred = false;
4ee9c684 741 m = gfc_match_char ('*');
742 if (m != MATCH_YES)
743 return m;
744
3bd3b616 745 m = gfc_match_small_literal_int (&length, NULL);
4ee9c684 746 if (m == MATCH_ERROR)
747 return m;
748
749 if (m == MATCH_YES)
750 {
ae0426ce 751 if (obsolescent_check
f25dbbf7 752 && gfc_notify_std (GFC_STD_F95_OBS,
926b8757 753 "Old-style character length at %C") == FAILURE)
8e9b41f0 754 return MATCH_ERROR;
126387b5 755 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
4ee9c684 756 return m;
757 }
758
759 if (gfc_match_char ('(') == MATCH_NO)
760 goto syntax;
761
3e715c81 762 m = char_len_param_value (expr, deferred);
077932f9 763 if (m != MATCH_YES && gfc_matching_function)
764 {
765 gfc_undo_symbols ();
766 m = MATCH_YES;
767 }
768
4ee9c684 769 if (m == MATCH_ERROR)
770 return m;
771 if (m == MATCH_NO)
772 goto syntax;
773
774 if (gfc_match_char (')') == MATCH_NO)
775 {
776 gfc_free_expr (*expr);
777 *expr = NULL;
778 goto syntax;
779 }
780
781 return MATCH_YES;
782
783syntax:
784 gfc_error ("Syntax error in character length specification at %C");
785 return MATCH_ERROR;
786}
787
788
ee893be6 789/* Special subroutine for finding a symbol. Check if the name is found
790 in the current name space. If not, and we're compiling a function or
791 subroutine and the parent compilation unit is an interface, then check
792 to see if the name we've been given is the name of the interface
793 (located in another namespace). */
4ee9c684 794
795static int
36b0a1b0 796find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
4ee9c684 797{
798 gfc_state_data *s;
36b0a1b0 799 gfc_symtree *st;
ee893be6 800 int i;
4ee9c684 801
36b0a1b0 802 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
e14bee04 803 if (i == 0)
36b0a1b0 804 {
805 *result = st ? st->n.sym : NULL;
806 goto end;
807 }
e14bee04 808
4ee9c684 809 if (gfc_current_state () != COMP_SUBROUTINE
810 && gfc_current_state () != COMP_FUNCTION)
ee893be6 811 goto end;
4ee9c684 812
813 s = gfc_state_stack->previous;
814 if (s == NULL)
ee893be6 815 goto end;
4ee9c684 816
817 if (s->state != COMP_INTERFACE)
ee893be6 818 goto end;
4ee9c684 819 if (s->sym == NULL)
f6d0e37a 820 goto end; /* Nameless interface. */
4ee9c684 821
822 if (strcmp (name, s->sym->name) == 0)
823 {
824 *result = s->sym;
825 return 0;
826 }
827
ee893be6 828end:
829 return i;
4ee9c684 830}
831
832
833/* Special subroutine for getting a symbol node associated with a
834 procedure name, used in SUBROUTINE and FUNCTION statements. The
835 symbol is created in the parent using with symtree node in the
836 child unit pointing to the symbol. If the current namespace has no
837 parent, then the symbol is just created in the current unit. */
838
839static int
1a9745d2 840get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
4ee9c684 841{
842 gfc_symtree *st;
843 gfc_symbol *sym;
2ddb8ed9 844 int rc = 0;
4ee9c684 845
d77f260f 846 /* Module functions have to be left in their own namespace because
847 they have potentially (almost certainly!) already been referenced.
848 In this sense, they are rather like external functions. This is
849 fixed up in resolve.c(resolve_entries), where the symbol name-
850 space is set to point to the master function, so that the fake
851 result mechanism can work. */
852 if (module_fcn_entry)
861d824f 853 {
854 /* Present if entry is declared to be a module procedure. */
855 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
c6a05992 856
861d824f 857 if (*result == NULL)
858 rc = gfc_get_symbol (name, NULL, result);
a78f714d 859 else if (!gfc_get_symbol (name, NULL, &sym) && sym
c6a05992 860 && (*result)->ts.type == BT_UNKNOWN
861 && sym->attr.flavor == FL_UNKNOWN)
862 /* Pick up the typespec for the entry, if declared in the function
863 body. Note that this symbol is FL_UNKNOWN because it will
864 only have appeared in a type declaration. The local symtree
865 is set to point to the module symbol and a unique symtree
866 to the local version. This latter ensures a correct clearing
867 of the symbols. */
a78f714d 868 {
869 /* If the ENTRY proceeds its specification, we need to ensure
870 that this does not raise a "has no IMPLICIT type" error. */
871 if (sym->ts.type == BT_UNKNOWN)
2168078b 872 sym->attr.untyped = 1;
a78f714d 873
2168078b 874 (*result)->ts = sym->ts;
a78f714d 875
876 /* Put the symbol in the procedure namespace so that, should
69b1505f 877 the ENTRY precede its specification, the specification
a78f714d 878 can be applied. */
879 (*result)->ns = gfc_current_ns;
880
881 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
882 st->n.sym = *result;
883 st = gfc_get_unique_symtree (gfc_current_ns);
884 st->n.sym = sym;
885 }
861d824f 886 }
858f9894 887 else
888 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
4ee9c684 889
2ddb8ed9 890 if (rc)
891 return rc;
892
858f9894 893 sym = *result;
c717e399 894 gfc_current_ns->refs++;
4ee9c684 895
c1977dbe 896 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
858f9894 897 {
16f49153 898 /* Trap another encompassed procedure with the same name. All
899 these conditions are necessary to avoid picking up an entry
900 whose name clashes with that of the encompassing procedure;
901 this is handled using gsymbols to register unique,globally
902 accessible names. */
858f9894 903 if (sym->attr.flavor != 0
1a9745d2 904 && sym->attr.proc != 0
905 && (sym->attr.subroutine || sym->attr.function)
906 && sym->attr.if_source != IFSRC_UNKNOWN)
858f9894 907 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
908 name, &sym->declared_at);
909
5720fd2f 910 /* Trap a procedure with a name the same as interface in the
911 encompassing scope. */
912 if (sym->attr.generic != 0
db697236 913 && (sym->attr.subroutine || sym->attr.function)
914 && !sym->attr.mod_proc)
5720fd2f 915 gfc_error_now ("Name '%s' at %C is already defined"
916 " as a generic interface at %L",
917 name, &sym->declared_at);
918
858f9894 919 /* Trap declarations of attributes in encompassing scope. The
920 signature for this is that ts.kind is set. Legitimate
921 references only set ts.type. */
922 if (sym->ts.kind != 0
1a9745d2 923 && !sym->attr.implicit_type
924 && sym->attr.proc == 0
925 && gfc_current_ns->parent != NULL
926 && sym->attr.access == 0
927 && !module_fcn_entry)
928 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
929 "and must not have attributes declared at %L",
858f9894 930 name, &sym->declared_at);
931 }
932
933 if (gfc_current_ns->parent == NULL || *result == NULL)
934 return rc;
4ee9c684 935
d77f260f 936 /* Module function entries will already have a symtree in
937 the current namespace but will need one at module level. */
938 if (module_fcn_entry)
861d824f 939 {
940 /* Present if entry is declared to be a module procedure. */
941 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
942 if (st == NULL)
943 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
944 }
d77f260f 945 else
946 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4ee9c684 947
4ee9c684 948 st->n.sym = sym;
949 sym->refs++;
950
f6d0e37a 951 /* See if the procedure should be a module procedure. */
4ee9c684 952
d77f260f 953 if (((sym->ns->proc_name != NULL
861d824f 954 && sym->ns->proc_name->attr.flavor == FL_MODULE
955 && sym->attr.proc != PROC_MODULE)
956 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
957 && gfc_add_procedure (&sym->attr, PROC_MODULE,
958 sym->name, NULL) == FAILURE)
4ee9c684 959 rc = 2;
960
961 return rc;
962}
963
964
c5d33754 965/* Verify that the given symbol representing a parameter is C
966 interoperable, by checking to see if it was marked as such after
967 its declaration. If the given symbol is not interoperable, a
968 warning is reported, thus removing the need to return the status to
969 the calling function. The standard does not require the user use
970 one of the iso_c_binding named constants to declare an
971 interoperable parameter, but we can't be sure if the param is C
972 interop or not if the user doesn't. For example, integer(4) may be
973 legal Fortran, but doesn't have meaning in C. It may interop with
974 a number of the C types, which causes a problem because the
975 compiler can't know which one. This code is almost certainly not
976 portable, and the user will get what they deserve if the C type
977 across platforms isn't always interoperable with integer(4). If
978 the user had used something like integer(c_int) or integer(c_long),
979 the compiler could have automatically handled the varying sizes
980 across platforms. */
981
f6bb8f4b 982gfc_try
2564c57a 983gfc_verify_c_interop_param (gfc_symbol *sym)
c5d33754 984{
985 int is_c_interop = 0;
f6bb8f4b 986 gfc_try retval = SUCCESS;
c5d33754 987
988 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
989 Don't repeat the checks here. */
990 if (sym->attr.implicit_type)
991 return SUCCESS;
992
993 /* For subroutines or functions that are passed to a BIND(C) procedure,
994 they're interoperable if they're BIND(C) and their params are all
995 interoperable. */
996 if (sym->attr.flavor == FL_PROCEDURE)
997 {
998 if (sym->attr.is_bind_c == 0)
999 {
1000 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
1001 "attribute to be C interoperable", sym->name,
1002 &(sym->declared_at));
1003
1004 return FAILURE;
1005 }
1006 else
1007 {
1008 if (sym->attr.is_c_interop == 1)
1009 /* We've already checked this procedure; don't check it again. */
1010 return SUCCESS;
1011 else
1012 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1013 sym->common_block);
1014 }
1015 }
1016
1017 /* See if we've stored a reference to a procedure that owns sym. */
1018 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1019 {
1020 if (sym->ns->proc_name->attr.is_bind_c == 1)
1021 {
2564c57a 1022 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
c5d33754 1023
1024 if (is_c_interop != 1)
1025 {
1026 /* Make personalized messages to give better feedback. */
1027 if (sym->ts.type == BT_DERIVED)
2564c57a 1028 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1029 "BIND(C) procedure '%s' but is not C interoperable "
c5d33754 1030 "because derived type '%s' is not C interoperable",
1031 sym->name, &(sym->declared_at),
1032 sym->ns->proc_name->name,
eeebe20b 1033 sym->ts.u.derived->name);
2564c57a 1034 else if (sym->ts.type == BT_CLASS)
1035 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1036 "BIND(C) procedure '%s' but is not C interoperable "
1037 "because it is polymorphic",
1038 sym->name, &(sym->declared_at),
1039 sym->ns->proc_name->name);
e25ecd2b 1040 else if (gfc_option.warn_c_binding_type)
1041 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
c5d33754 1042 "BIND(C) procedure '%s' but may not be C "
1043 "interoperable",
1044 sym->name, &(sym->declared_at),
1045 sym->ns->proc_name->name);
1046 }
e4eda3ec 1047
1048 /* Character strings are only C interoperable if they have a
1049 length of 1. */
1050 if (sym->ts.type == BT_CHARACTER)
1051 {
eeebe20b 1052 gfc_charlen *cl = sym->ts.u.cl;
e4eda3ec 1053 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1054 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1055 {
1056 gfc_error ("Character argument '%s' at %L "
1057 "must be length 1 because "
1058 "procedure '%s' is BIND(C)",
1059 sym->name, &sym->declared_at,
1060 sym->ns->proc_name->name);
1061 retval = FAILURE;
1062 }
1063 }
1064
c5d33754 1065 /* We have to make sure that any param to a bind(c) routine does
1066 not have the allocatable, pointer, or optional attributes,
1067 according to J3/04-007, section 5.1. */
1068 if (sym->attr.allocatable == 1)
1069 {
1070 gfc_error ("Variable '%s' at %L cannot have the "
1071 "ALLOCATABLE attribute because procedure '%s'"
1072 " is BIND(C)", sym->name, &(sym->declared_at),
1073 sym->ns->proc_name->name);
1074 retval = FAILURE;
1075 }
1076
1077 if (sym->attr.pointer == 1)
1078 {
1079 gfc_error ("Variable '%s' at %L cannot have the "
1080 "POINTER attribute because procedure '%s'"
1081 " is BIND(C)", sym->name, &(sym->declared_at),
1082 sym->ns->proc_name->name);
1083 retval = FAILURE;
1084 }
1085
495e197c 1086 if (sym->attr.optional == 1 && sym->attr.value)
c5d33754 1087 {
495e197c 1088 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1089 "and the VALUE attribute because procedure '%s' "
1090 "is BIND(C)", sym->name, &(sym->declared_at),
c5d33754 1091 sym->ns->proc_name->name);
1092 retval = FAILURE;
1093 }
495e197c 1094 else if (sym->attr.optional == 1
f25dbbf7 1095 && gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' "
495e197c 1096 "at %L with OPTIONAL attribute in "
1097 "procedure '%s' which is BIND(C)",
1098 sym->name, &(sym->declared_at),
1099 sym->ns->proc_name->name)
1100 == FAILURE)
1101 retval = FAILURE;
c5d33754 1102
1103 /* Make sure that if it has the dimension attribute, that it is
9350ae4e 1104 either assumed size or explicit shape. Deferred shape is already
1105 covered by the pointer/allocatable attribute. */
1106 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1107 && gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array '%s' "
1108 "at %L as dummy argument to the BIND(C) "
1109 "procedure '%s' at %L", sym->name,
1110 &(sym->declared_at), sym->ns->proc_name->name,
1111 &(sym->ns->proc_name->declared_at)) == FAILURE)
1112 retval = FAILURE;
c5d33754 1113 }
1114 }
1115
1116 return retval;
1117}
1118
1119
1de1b1a9 1120
c5d33754 1121/* Function called by variable_decl() that adds a name to the symbol table. */
4ee9c684 1122
f6bb8f4b 1123static gfc_try
3e715c81 1124build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1a9745d2 1125 gfc_array_spec **as, locus *var_locus)
4ee9c684 1126{
1127 symbol_attribute attr;
1128 gfc_symbol *sym;
1129
ee893be6 1130 if (gfc_get_symbol (name, NULL, &sym))
4ee9c684 1131 return FAILURE;
1132
f6d0e37a 1133 /* Start updating the symbol table. Add basic type attribute if present. */
4ee9c684 1134 if (current_ts.type != BT_UNKNOWN
1a9745d2 1135 && (sym->attr.implicit_type == 0
1136 || !gfc_compare_types (&sym->ts, &current_ts))
4ee9c684 1137 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1138 return FAILURE;
1139
1140 if (sym->ts.type == BT_CHARACTER)
3e715c81 1141 {
1142 sym->ts.u.cl = cl;
1143 sym->ts.deferred = cl_deferred;
1144 }
4ee9c684 1145
1146 /* Add dimension attribute if present. */
1147 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1148 return FAILURE;
1149 *as = NULL;
1150
1151 /* Add attribute to symbol. The copy is so that we can reset the
1152 dimension attribute. */
1153 attr = current_attr;
1154 attr.dimension = 0;
aff518b0 1155 attr.codimension = 0;
4ee9c684 1156
1157 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1158 return FAILURE;
1159
c5d33754 1160 /* Finish any work that may need to be done for the binding label,
1161 if it's a bind(c). The bind(c) attr is found before the symbol
1162 is made, and before the symbol name (for data decls), so the
1163 current_ts is holding the binding label, or nothing if the
1164 name= attr wasn't given. Therefore, test here if we're dealing
1165 with a bind(c) and make sure the binding label is set correctly. */
1166 if (sym->attr.is_bind_c == 1)
1167 {
7b2060ba 1168 if (!sym->binding_label)
c5d33754 1169 {
825718f9 1170 /* Set the binding label and verify that if a NAME= was specified
1171 then only one identifier was in the entity-decl-list. */
7b2060ba 1172 if (set_binding_label (&sym->binding_label, sym->name,
825718f9 1173 num_idents_on_line) == FAILURE)
c5d33754 1174 return FAILURE;
1175 }
1176 }
1177
1178 /* See if we know we're in a common block, and if it's a bind(c)
1179 common then we need to make sure we're an interoperable type. */
1180 if (sym->attr.in_common == 1)
1181 {
1182 /* Test the common block object. */
1183 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1184 && sym->ts.is_c_interop != 1)
1185 {
1186 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1187 "must be declared with a C interoperable "
1188 "kind since common block '%s' is BIND(C)",
1189 sym->name, sym->common_block->name,
1190 sym->common_block->name);
1191 gfc_clear_error ();
1192 }
1193 }
1194
2457a77e 1195 sym->attr.implied_index = 0;
1196
fa102e56 1197 if (sym->ts.type == BT_CLASS)
29dae2bf 1198 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1de1b1a9 1199
4ee9c684 1200 return SUCCESS;
1201}
1202
1a9745d2 1203
a270dc8e 1204/* Set character constant to the given length. The constant will be padded or
d1a39099 1205 truncated. If we're inside an array constructor without a typespec, we
1206 additionally check that all elements have the same length; check_len -1
1207 means no checking. */
a270dc8e 1208
1209void
d1a39099 1210gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
a270dc8e 1211{
c32f863c 1212 gfc_char_t *s;
a270dc8e 1213 int slen;
1214
1215 gcc_assert (expr->expr_type == EXPR_CONSTANT);
b44437b9 1216 gcc_assert (expr->ts.type == BT_CHARACTER);
a270dc8e 1217
1218 slen = expr->value.character.length;
1219 if (len != slen)
1220 {
c32f863c 1221 s = gfc_get_wide_string (len + 1);
1222 memcpy (s, expr->value.character.string,
1223 MIN (len, slen) * sizeof (gfc_char_t));
a270dc8e 1224 if (len > slen)
c32f863c 1225 gfc_wide_memset (&s[slen], ' ', len - slen);
1bfea7e8 1226
1227 if (gfc_option.warn_character_truncation && slen > len)
1228 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1229 "(%d/%d)", &expr->where, slen, len);
1230
1231 /* Apply the standard by 'hand' otherwise it gets cleared for
1232 initializers. */
d1a39099 1233 if (check_len != -1 && slen != check_len
1234 && !(gfc_option.allow_std & GFC_STD_GNU))
1bfea7e8 1235 gfc_error_now ("The CHARACTER elements of the array constructor "
1236 "at %L must have the same length (%d/%d)",
d1a39099 1237 &expr->where, slen, check_len);
1bfea7e8 1238
89f528df 1239 s[len] = '\0';
434f0922 1240 free (expr->value.character.string);
a270dc8e 1241 expr->value.character.string = s;
1242 expr->value.character.length = len;
1243 }
1244}
4ee9c684 1245
3b6a4b41 1246
e14bee04 1247/* Function to create and update the enumerator history
3b6a4b41 1248 using the information passed as arguments.
e14bee04 1249 Pointer "max_enum" is also updated, to point to
1250 enum history node containing largest initializer.
3b6a4b41 1251
1252 SYM points to the symbol node of enumerator.
f6d0e37a 1253 INIT points to its enumerator value. */
3b6a4b41 1254
e14bee04 1255static void
1a9745d2 1256create_enum_history (gfc_symbol *sym, gfc_expr *init)
3b6a4b41 1257{
1258 enumerator_history *new_enum_history;
1259 gcc_assert (sym != NULL && init != NULL);
1260
48d8ad5a 1261 new_enum_history = XCNEW (enumerator_history);
3b6a4b41 1262
1263 new_enum_history->sym = sym;
1264 new_enum_history->initializer = init;
1265 new_enum_history->next = NULL;
1266
1267 if (enum_history == NULL)
1268 {
1269 enum_history = new_enum_history;
1270 max_enum = enum_history;
1271 }
1272 else
1273 {
1274 new_enum_history->next = enum_history;
1275 enum_history = new_enum_history;
1276
e14bee04 1277 if (mpz_cmp (max_enum->initializer->value.integer,
3b6a4b41 1278 new_enum_history->initializer->value.integer) < 0)
1a9745d2 1279 max_enum = new_enum_history;
3b6a4b41 1280 }
1281}
1282
1283
e14bee04 1284/* Function to free enum kind history. */
3b6a4b41 1285
e14bee04 1286void
1a9745d2 1287gfc_free_enum_history (void)
3b6a4b41 1288{
e14bee04 1289 enumerator_history *current = enum_history;
1290 enumerator_history *next;
3b6a4b41 1291
1292 while (current != NULL)
1293 {
1294 next = current->next;
434f0922 1295 free (current);
3b6a4b41 1296 current = next;
1297 }
1298 max_enum = NULL;
1299 enum_history = NULL;
1300}
1301
1302
4ee9c684 1303/* Function called by variable_decl() that adds an initialization
1304 expression to a symbol. */
1305
f6bb8f4b 1306static gfc_try
f6d0e37a 1307add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
4ee9c684 1308{
1309 symbol_attribute attr;
1310 gfc_symbol *sym;
1311 gfc_expr *init;
1312
1313 init = *initp;
36b0a1b0 1314 if (find_special (name, &sym, false))
4ee9c684 1315 return FAILURE;
1316
1317 attr = sym->attr;
1318
1319 /* If this symbol is confirming an implicit parameter type,
1320 then an initialization expression is not allowed. */
1321 if (attr.flavor == FL_PARAMETER
1322 && sym->value != NULL
1323 && *initp != NULL)
1324 {
1325 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1326 sym->name);
1327 return FAILURE;
1328 }
1329
1330 if (init == NULL)
1331 {
1332 /* An initializer is required for PARAMETER declarations. */
1333 if (attr.flavor == FL_PARAMETER)
1334 {
1335 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1336 return FAILURE;
1337 }
1338 }
1339 else
1340 {
1341 /* If a variable appears in a DATA block, it cannot have an
b97f1a18 1342 initializer. */
4ee9c684 1343 if (sym->attr.data)
1344 {
1a9745d2 1345 gfc_error ("Variable '%s' at %C with an initializer already "
1346 "appears in a DATA statement", sym->name);
4ee9c684 1347 return FAILURE;
1348 }
1349
cca3db55 1350 /* Check if the assignment can happen. This has to be put off
23d075f4 1351 until later for derived type variables and procedure pointers. */
4ee9c684 1352 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1de1b1a9 1353 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
23d075f4 1354 && !sym->attr.proc_pointer
4ee9c684 1355 && gfc_check_assign_symbol (sym, init) == FAILURE)
1356 return FAILURE;
1357
eeebe20b 1358 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
da540712 1359 && init->ts.type == BT_CHARACTER)
a270dc8e 1360 {
1361 /* Update symbol character length according initializer. */
da540712 1362 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1363 return FAILURE;
1364
eeebe20b 1365 if (sym->ts.u.cl->length == NULL)
a270dc8e 1366 {
03c2a028 1367 int clen;
f6d0e37a 1368 /* If there are multiple CHARACTER variables declared on the
1369 same line, we don't want them to share the same length. */
d270ce52 1370 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
e9c873a4 1371
03c2a028 1372 if (sym->attr.flavor == FL_PARAMETER)
1373 {
1374 if (init->expr_type == EXPR_CONSTANT)
1375 {
1376 clen = init->value.character.length;
126387b5 1377 sym->ts.u.cl->length
1378 = gfc_get_int_expr (gfc_default_integer_kind,
1379 NULL, clen);
03c2a028 1380 }
1381 else if (init->expr_type == EXPR_ARRAY)
1382 {
126387b5 1383 gfc_constructor *c;
1384 c = gfc_constructor_first (init->value.constructor);
1385 clen = c->expr->value.character.length;
1386 sym->ts.u.cl->length
1387 = gfc_get_int_expr (gfc_default_integer_kind,
1388 NULL, clen);
03c2a028 1389 }
eeebe20b 1390 else if (init->ts.u.cl && init->ts.u.cl->length)
1391 sym->ts.u.cl->length =
1392 gfc_copy_expr (sym->value->ts.u.cl->length);
03c2a028 1393 }
a270dc8e 1394 }
1395 /* Update initializer character length according symbol. */
eeebe20b 1396 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
a270dc8e 1397 {
eeebe20b 1398 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
a270dc8e 1399
1400 if (init->expr_type == EXPR_CONSTANT)
d1a39099 1401 gfc_set_constant_character_len (len, init, -1);
a270dc8e 1402 else if (init->expr_type == EXPR_ARRAY)
1403 {
126387b5 1404 gfc_constructor *c;
1405
39908fd9 1406 /* Build a new charlen to prevent simplification from
1407 deleting the length before it is resolved. */
d270ce52 1408 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
eeebe20b 1409 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
39908fd9 1410
126387b5 1411 for (c = gfc_constructor_first (init->value.constructor);
1412 c; c = gfc_constructor_next (c))
1413 gfc_set_constant_character_len (len, c->expr, -1);
a270dc8e 1414 }
1415 }
1416 }
1417
ae2864a8 1418 /* If sym is implied-shape, set its upper bounds from init. */
1419 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1420 && sym->as->type == AS_IMPLIED_SHAPE)
1421 {
1422 int dim;
1423
1424 if (init->rank == 0)
1425 {
1426 gfc_error ("Can't initialize implied-shape array at %L"
1427 " with scalar", &sym->declared_at);
1428 return FAILURE;
1429 }
1430 gcc_assert (sym->as->rank == init->rank);
1431
1432 /* Shape should be present, we get an initialization expression. */
1433 gcc_assert (init->shape);
1434
1435 for (dim = 0; dim < sym->as->rank; ++dim)
1436 {
1437 int k;
1438 gfc_expr* lower;
1439 gfc_expr* e;
1440
1441 lower = sym->as->lower[dim];
1442 if (lower->expr_type != EXPR_CONSTANT)
1443 {
1444 gfc_error ("Non-constant lower bound in implied-shape"
1445 " declaration at %L", &lower->where);
1446 return FAILURE;
1447 }
1448
1449 /* All dimensions must be without upper bound. */
1450 gcc_assert (!sym->as->upper[dim]);
1451
1452 k = lower->ts.kind;
1453 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1454 mpz_add (e->value.integer,
1455 lower->value.integer, init->shape[dim]);
1456 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1457 sym->as->upper[dim] = e;
1458 }
1459
1460 sym->as->type = AS_EXPLICIT;
1461 }
1462
c5d33754 1463 /* Need to check if the expression we initialized this
1464 to was one of the iso_c_binding named constants. If so,
1465 and we're a parameter (constant), let it be iso_c.
1466 For example:
1467 integer(c_int), parameter :: my_int = c_int
1468 integer(my_int) :: my_int_2
1469 If we mark my_int as iso_c (since we can see it's value
1470 is equal to one of the named constants), then my_int_2
1471 will be considered C interoperable. */
1472 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1473 {
1474 sym->ts.is_iso_c |= init->ts.is_iso_c;
1475 sym->ts.is_c_interop |= init->ts.is_c_interop;
1476 /* attr bits needed for module files. */
1477 sym->attr.is_iso_c |= init->ts.is_iso_c;
1478 sym->attr.is_c_interop |= init->ts.is_c_interop;
1479 if (init->ts.is_iso_c)
1480 sym->ts.f90_type = init->ts.f90_type;
1481 }
126387b5 1482
4ee9c684 1483 /* Add initializer. Make sure we keep the ranks sane. */
1484 if (sym->attr.dimension && init->rank == 0)
7baa3fb4 1485 {
1486 mpz_t size;
1487 gfc_expr *array;
7baa3fb4 1488 int n;
1489 if (sym->attr.flavor == FL_PARAMETER
1490 && init->expr_type == EXPR_CONSTANT
1491 && spec_size (sym->as, &size) == SUCCESS
1492 && mpz_cmp_si (size, 0) > 0)
1493 {
126387b5 1494 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1495 &init->where);
7baa3fb4 1496 for (n = 0; n < (int)mpz_get_si (size); n++)
126387b5 1497 gfc_constructor_append_expr (&array->value.constructor,
1498 n == 0
1499 ? init
1500 : gfc_copy_expr (init),
1501 &init->where);
1502
7baa3fb4 1503 array->shape = gfc_get_shape (sym->as->rank);
1504 for (n = 0; n < sym->as->rank; n++)
1505 spec_dimen_size (sym->as, n, &array->shape[n]);
1506
1507 init = array;
1508 mpz_clear (size);
1509 }
1510 init->rank = sym->as->rank;
1511 }
4ee9c684 1512
1513 sym->value = init;
3cd3c667 1514 if (sym->attr.save == SAVE_NONE)
1515 sym->attr.save = SAVE_IMPLICIT;
4ee9c684 1516 *initp = NULL;
1517 }
1518
1519 return SUCCESS;
1520}
1521
1522
1523/* Function called by variable_decl() that adds a name to a structure
1524 being built. */
1525
f6bb8f4b 1526static gfc_try
1a9745d2 1527build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1528 gfc_array_spec **as)
4ee9c684 1529{
1530 gfc_component *c;
b3704193 1531 gfc_try t = SUCCESS;
4ee9c684 1532
53d2e124 1533 /* F03:C438/C439. If the current symbol is of the same derived type that we're
4ee9c684 1534 constructing, it must have the pointer attribute. */
53d2e124 1535 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
eeebe20b 1536 && current_ts.u.derived == gfc_current_block ()
4ee9c684 1537 && current_attr.pointer == 0)
1538 {
1539 gfc_error ("Component at %C must have the POINTER attribute");
1540 return FAILURE;
1541 }
1542
1a9745d2 1543 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
4ee9c684 1544 {
1545 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1546 {
1547 gfc_error ("Array component of structure at %C must have explicit "
1548 "or deferred shape");
1549 return FAILURE;
1550 }
1551 }
1552
1553 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1554 return FAILURE;
1555
1556 c->ts = current_ts;
eeebe20b 1557 if (c->ts.type == BT_CHARACTER)
1558 c->ts.u.cl = cl;
3be2b8d5 1559 c->attr = current_attr;
4ee9c684 1560
1561 c->initializer = *init;
1562 *init = NULL;
1563
1564 c->as = *as;
1565 if (c->as != NULL)
aff518b0 1566 {
1567 if (c->as->corank)
1568 c->attr.codimension = 1;
1569 if (c->as->rank)
1570 c->attr.dimension = 1;
1571 }
4ee9c684 1572 *as = NULL;
1573
a2f51d5f 1574 /* Should this ever get more complicated, combine with similar section
1575 in add_init_expr_to_sym into a separate function. */
2d76519f 1576 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1577 && c->ts.u.cl
eeebe20b 1578 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
a2f51d5f 1579 {
d1a39099 1580 int len;
1581
eeebe20b 1582 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1583 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1584 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
d1a39099 1585
eeebe20b 1586 len = mpz_get_si (c->ts.u.cl->length->value.integer);
a2f51d5f 1587
1588 if (c->initializer->expr_type == EXPR_CONSTANT)
d1a39099 1589 gfc_set_constant_character_len (len, c->initializer, -1);
eeebe20b 1590 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1591 c->initializer->ts.u.cl->length->value.integer))
a2f51d5f 1592 {
126387b5 1593 gfc_constructor *ctor;
1594 ctor = gfc_constructor_first (c->initializer->value.constructor);
d1a39099 1595
6e4faf80 1596 if (ctor)
d1a39099 1597 {
6e4faf80 1598 int first_len;
126387b5 1599 bool has_ts = (c->initializer->ts.u.cl
1600 && c->initializer->ts.u.cl->length_from_typespec);
6e4faf80 1601
1602 /* Remember the length of the first element for checking
1603 that all elements *in the constructor* have the same
1604 length. This need not be the length of the LHS! */
1605 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1606 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1607 first_len = ctor->expr->value.character.length;
1608
126387b5 1609 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1610 if (ctor->expr->expr_type == EXPR_CONSTANT)
d1a39099 1611 {
126387b5 1612 gfc_set_constant_character_len (len, ctor->expr,
1613 has_ts ? -1 : first_len);
1614 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
d1a39099 1615 }
d1a39099 1616 }
a2f51d5f 1617 }
1618 }
1619
4ee9c684 1620 /* Check array components. */
3be2b8d5 1621 if (!c->attr.dimension)
b3704193 1622 goto scalar;
4ee9c684 1623
3be2b8d5 1624 if (c->attr.pointer)
4ee9c684 1625 {
1626 if (c->as->type != AS_DEFERRED)
1627 {
2294b616 1628 gfc_error ("Pointer array component of structure at %C must have a "
1629 "deferred shape");
b3704193 1630 t = FAILURE;
2294b616 1631 }
1632 }
3be2b8d5 1633 else if (c->attr.allocatable)
2294b616 1634 {
1635 if (c->as->type != AS_DEFERRED)
1636 {
1637 gfc_error ("Allocatable component of structure at %C must have a "
1638 "deferred shape");
b3704193 1639 t = FAILURE;
4ee9c684 1640 }
1641 }
1642 else
1643 {
1644 if (c->as->type != AS_EXPLICIT)
1645 {
1a9745d2 1646 gfc_error ("Array component of structure at %C must have an "
1647 "explicit shape");
b3704193 1648 t = FAILURE;
4ee9c684 1649 }
1650 }
1651
b3704193 1652scalar:
1653 if (c->ts.type == BT_CLASS)
d9c9afea 1654 {
1655 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1656 || (!c->ts.u.derived->components
1657 && !c->ts.u.derived->attr.zero_comp);
431a19be 1658 gfc_try t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1659
1660 if (t != FAILURE)
1661 t = t2;
d9c9afea 1662 }
1663
b3704193 1664 return t;
4ee9c684 1665}
1666
1667
1668/* Match a 'NULL()', and possibly take care of some side effects. */
1669
1670match
1a9745d2 1671gfc_match_null (gfc_expr **result)
4ee9c684 1672{
1673 gfc_symbol *sym;
4ee9c684 1674 match m;
1675
1676 m = gfc_match (" null ( )");
1677 if (m != MATCH_YES)
1678 return m;
1679
1680 /* The NULL symbol now has to be/become an intrinsic function. */
1681 if (gfc_get_symbol ("null", NULL, &sym))
1682 {
1683 gfc_error ("NULL() initialization at %C is ambiguous");
1684 return MATCH_ERROR;
1685 }
1686
1687 gfc_intrinsic_symbol (sym);
1688
1689 if (sym->attr.proc != PROC_INTRINSIC
950683ed 1690 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1691 sym->name, NULL) == FAILURE
1692 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
4ee9c684 1693 return MATCH_ERROR;
1694
126387b5 1695 *result = gfc_get_null_expr (&gfc_current_locus);
4ee9c684 1696
1697 return MATCH_YES;
1698}
1699
1700
23d075f4 1701/* Match the initialization expr for a data pointer or procedure pointer. */
1702
1703static match
1704match_pointer_init (gfc_expr **init, int procptr)
1705{
1706 match m;
1707
1708 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1709 {
1710 gfc_error ("Initialization of pointer at %C is not allowed in "
1711 "a PURE procedure");
1712 return MATCH_ERROR;
1713 }
1714
df084314 1715 /* Match NULL() initialization. */
23d075f4 1716 m = gfc_match_null (init);
1717 if (m != MATCH_NO)
1718 return m;
1719
1720 /* Match non-NULL initialization. */
f3efaada 1721 gfc_matching_ptr_assignment = !procptr;
23d075f4 1722 gfc_matching_procptr_assignment = procptr;
1723 m = gfc_match_rvalue (init);
f3efaada 1724 gfc_matching_ptr_assignment = 0;
23d075f4 1725 gfc_matching_procptr_assignment = 0;
1726 if (m == MATCH_ERROR)
1727 return MATCH_ERROR;
1728 else if (m == MATCH_NO)
1729 {
1730 gfc_error ("Error in pointer initialization at %C");
1731 return MATCH_ERROR;
1732 }
1733
1734 if (!procptr)
1735 gfc_resolve_expr (*init);
1736
f25dbbf7 1737 if (gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
23d075f4 1738 "initialization at %C") == FAILURE)
1739 return MATCH_ERROR;
1740
1741 return MATCH_YES;
1742}
1743
1744
f3e89339 1745static gfc_try
1746check_function_name (char *name)
1747{
1748 /* In functions that have a RESULT variable defined, the function name always
1749 refers to function calls. Therefore, the name is not allowed to appear in
1750 specification statements. When checking this, be careful about
1751 'hidden' procedure pointer results ('ppr@'). */
1752
1753 if (gfc_current_state () == COMP_FUNCTION)
1754 {
1755 gfc_symbol *block = gfc_current_block ();
1756 if (block && block->result && block->result != block
1757 && strcmp (block->result->name, "ppr@") != 0
1758 && strcmp (block->name, name) == 0)
1759 {
1760 gfc_error ("Function name '%s' not allowed at %C", name);
1761 return FAILURE;
1762 }
1763 }
1764
1765 return SUCCESS;
1766}
1767
1768
4ee9c684 1769/* Match a variable name with an optional initializer. When this
1770 subroutine is called, a variable is expected to be parsed next.
1771 Depending on what is happening at the moment, updates either the
1772 symbol table or the current interface. */
1773
1774static match
3923b69f 1775variable_decl (int elem)
4ee9c684 1776{
1777 char name[GFC_MAX_SYMBOL_LEN + 1];
1778 gfc_expr *initializer, *char_len;
1779 gfc_array_spec *as;
b549d2a5 1780 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
4ee9c684 1781 gfc_charlen *cl;
3e715c81 1782 bool cl_deferred;
4ee9c684 1783 locus var_locus;
1784 match m;
f6bb8f4b 1785 gfc_try t;
b549d2a5 1786 gfc_symbol *sym;
4ee9c684 1787
1788 initializer = NULL;
1789 as = NULL;
b549d2a5 1790 cp_as = NULL;
4ee9c684 1791
1792 /* When we get here, we've just matched a list of attributes and
1793 maybe a type and a double colon. The next thing we expect to see
1794 is the name of the symbol. */
1795 m = gfc_match_name (name);
1796 if (m != MATCH_YES)
1797 goto cleanup;
1798
cbb9e6aa 1799 var_locus = gfc_current_locus;
4ee9c684 1800
1801 /* Now we could see the optional array spec. or character length. */
aff518b0 1802 m = gfc_match_array_spec (&as, true, true);
eae6c6c1 1803 if (m == MATCH_ERROR)
4ee9c684 1804 goto cleanup;
3b6a4b41 1805
4ee9c684 1806 if (m == MATCH_NO)
1807 as = gfc_copy_array_spec (current_as);
9b58b4c7 1808 else if (current_as
1809 && merge_array_spec (current_as, as, true) == FAILURE)
1810 {
1811 m = MATCH_ERROR;
1812 goto cleanup;
1813 }
4ee9c684 1814
eae6c6c1 1815 if (gfc_option.flag_cray_pointer)
1816 cp_as = gfc_copy_array_spec (as);
1817
ae2864a8 1818 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1819 determine (and check) whether it can be implied-shape. If it
1820 was parsed as assumed-size, change it because PARAMETERs can not
1821 be assumed-size. */
1822 if (as)
1823 {
1824 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1825 {
1826 m = MATCH_ERROR;
1827 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1828 name, &var_locus);
1829 goto cleanup;
1830 }
1831
1832 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1833 && current_attr.flavor == FL_PARAMETER)
1834 as->type = AS_IMPLIED_SHAPE;
1835
1836 if (as->type == AS_IMPLIED_SHAPE
1837 && gfc_notify_std (GFC_STD_F2008,
f25dbbf7 1838 "Implied-shape array at %L",
ae2864a8 1839 &var_locus) == FAILURE)
1840 {
1841 m = MATCH_ERROR;
1842 goto cleanup;
1843 }
1844 }
1845
4ee9c684 1846 char_len = NULL;
1847 cl = NULL;
3e715c81 1848 cl_deferred = false;
4ee9c684 1849
1850 if (current_ts.type == BT_CHARACTER)
1851 {
926b8757 1852 switch (match_char_length (&char_len, &cl_deferred, false))
4ee9c684 1853 {
1854 case MATCH_YES:
d270ce52 1855 cl = gfc_new_charlen (gfc_current_ns, NULL);
4ee9c684 1856
1857 cl->length = char_len;
1858 break;
1859
3923b69f 1860 /* Non-constant lengths need to be copied after the first
04b61f60 1861 element. Also copy assumed lengths. */
4ee9c684 1862 case MATCH_NO:
04b61f60 1863 if (elem > 1
eeebe20b 1864 && (current_ts.u.cl->length == NULL
1865 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
3923b69f 1866 {
d270ce52 1867 cl = gfc_new_charlen (gfc_current_ns, NULL);
eeebe20b 1868 cl->length = gfc_copy_expr (current_ts.u.cl->length);
3923b69f 1869 }
1870 else
eeebe20b 1871 cl = current_ts.u.cl;
3923b69f 1872
3e715c81 1873 cl_deferred = current_ts.deferred;
1874
4ee9c684 1875 break;
1876
1877 case MATCH_ERROR:
1878 goto cleanup;
1879 }
1880 }
1881
b549d2a5 1882 /* If this symbol has already shown up in a Cray Pointer declaration,
f6d0e37a 1883 then we want to set the type & bail out. */
b549d2a5 1884 if (gfc_option.flag_cray_pointer)
1885 {
1886 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1887 if (sym != NULL && sym->attr.cray_pointee)
1888 {
1889 sym->ts.type = current_ts.type;
1890 sym->ts.kind = current_ts.kind;
eeebe20b 1891 sym->ts.u.cl = cl;
1892 sym->ts.u.derived = current_ts.u.derived;
c5d33754 1893 sym->ts.is_c_interop = current_ts.is_c_interop;
1894 sym->ts.is_iso_c = current_ts.is_iso_c;
b549d2a5 1895 m = MATCH_YES;
1896
1897 /* Check to see if we have an array specification. */
1898 if (cp_as != NULL)
1899 {
1900 if (sym->as != NULL)
1901 {
7698a624 1902 gfc_error ("Duplicate array spec for Cray pointee at %C");
b549d2a5 1903 gfc_free_array_spec (cp_as);
1904 m = MATCH_ERROR;
1905 goto cleanup;
1906 }
1907 else
1908 {
1909 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1910 gfc_internal_error ("Couldn't set pointee array spec.");
e14bee04 1911
b549d2a5 1912 /* Fix the array spec. */
e14bee04 1913 m = gfc_mod_pointee_as (sym->as);
b549d2a5 1914 if (m == MATCH_ERROR)
1915 goto cleanup;
1916 }
e14bee04 1917 }
b549d2a5 1918 goto cleanup;
1919 }
1920 else
1921 {
1922 gfc_free_array_spec (cp_as);
1923 }
1924 }
e14bee04 1925
1e057e9b 1926 /* Procedure pointer as function result. */
1927 if (gfc_current_state () == COMP_FUNCTION
1928 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1929 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1930 strcpy (name, "ppr@");
1931
1932 if (gfc_current_state () == COMP_FUNCTION
1933 && strcmp (name, gfc_current_block ()->name) == 0
1934 && gfc_current_block ()->result
1935 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1936 strcpy (name, "ppr@");
e14bee04 1937
4ee9c684 1938 /* OK, we've successfully matched the declaration. Now put the
1939 symbol in the current namespace, because it might be used in the
fe06c0d5 1940 optional initialization expression for this symbol, e.g. this is
4ee9c684 1941 perfectly legal:
1942
1943 integer, parameter :: i = huge(i)
1944
1945 This is only true for parameters or variables of a basic type.
1946 For components of derived types, it is not true, so we don't
1947 create a symbol for those yet. If we fail to create the symbol,
1948 bail out. */
1949 if (gfc_current_state () != COMP_DERIVED
3e715c81 1950 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
4ee9c684 1951 {
b8a51d79 1952 m = MATCH_ERROR;
1953 goto cleanup;
1954 }
1955
60fbbf9e 1956 /* An interface body specifies all of the procedure's
1957 characteristics and these shall be consistent with those
1958 specified in the procedure definition, except that the interface
1959 may specify a procedure that is not pure if the procedure is
1960 defined to be pure(12.3.2). */
71c22dfb 1961 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1a9745d2 1962 && gfc_current_ns->proc_name
1963 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
eeebe20b 1964 && current_ts.u.derived->ns != gfc_current_ns)
c723595c 1965 {
1966 gfc_symtree *st;
eeebe20b 1967 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1968 if (!(current_ts.u.derived->attr.imported
c723595c 1969 && st != NULL
c2958b6b 1970 && gfc_find_dt_in_generic (st->n.sym) == current_ts.u.derived)
c723595c 1971 && !gfc_current_ns->has_import_set)
1972 {
c2958b6b 1973 gfc_error ("The type of '%s' at %C has not been declared within the "
c723595c 1974 "interface", name);
1975 m = MATCH_ERROR;
1976 goto cleanup;
1977 }
4ee9c684 1978 }
f3e89339 1979
1980 if (check_function_name (name) == FAILURE)
4ee9c684 1981 {
4ee9c684 1982 m = MATCH_ERROR;
1983 goto cleanup;
1984 }
1985
b4f45d02 1986 /* We allow old-style initializations of the form
1987 integer i /2/, j(4) /3*3, 1/
1988 (if no colon has been seen). These are different from data
1989 statements in that initializers are only allowed to apply to the
1990 variable immediately preceding, i.e.
1991 integer i, j /1, 2/
1992 is not allowed. Therefore we have to do some work manually, that
cca3db55 1993 could otherwise be left to the matchers for DATA statements. */
b4f45d02 1994
1995 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1996 {
f25dbbf7 1997 if (gfc_notify_std (GFC_STD_GNU, "Old-style "
b4f45d02 1998 "initialization at %C") == FAILURE)
1999 return MATCH_ERROR;
e14bee04 2000
b4f45d02 2001 return match_old_style_init (name);
2002 }
2003
4ee9c684 2004 /* The double colon must be present in order to have initializers.
2005 Otherwise the statement is ambiguous with an assignment statement. */
2006 if (colon_seen)
2007 {
2008 if (gfc_match (" =>") == MATCH_YES)
2009 {
4ee9c684 2010 if (!current_attr.pointer)
2011 {
2012 gfc_error ("Initialization at %C isn't for a pointer variable");
2013 m = MATCH_ERROR;
2014 goto cleanup;
2015 }
2016
23d075f4 2017 m = match_pointer_init (&initializer, 0);
4ee9c684 2018 if (m != MATCH_YES)
2019 goto cleanup;
4ee9c684 2020 }
2021 else if (gfc_match_char ('=') == MATCH_YES)
2022 {
2023 if (current_attr.pointer)
2024 {
1a9745d2 2025 gfc_error ("Pointer initialization at %C requires '=>', "
2026 "not '='");
4ee9c684 2027 m = MATCH_ERROR;
2028 goto cleanup;
2029 }
2030
2031 m = gfc_match_init_expr (&initializer);
2032 if (m == MATCH_NO)
2033 {
2034 gfc_error ("Expected an initialization expression at %C");
2035 m = MATCH_ERROR;
2036 }
2037
b3a420c5 2038 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2039 && gfc_state_stack->state != COMP_DERIVED)
4ee9c684 2040 {
1a9745d2 2041 gfc_error ("Initialization of variable at %C is not allowed in "
2042 "a PURE procedure");
4ee9c684 2043 m = MATCH_ERROR;
2044 }
2045
2046 if (m != MATCH_YES)
2047 goto cleanup;
2048 }
8ffad0f9 2049 }
2050
2294b616 2051 if (initializer != NULL && current_attr.allocatable
2052 && gfc_current_state () == COMP_DERIVED)
2053 {
1a9745d2 2054 gfc_error ("Initialization of allocatable component at %C is not "
2055 "allowed");
2294b616 2056 m = MATCH_ERROR;
2057 goto cleanup;
2058 }
2059
d9b3f26b 2060 /* Add the initializer. Note that it is fine if initializer is
4ee9c684 2061 NULL here, because we sometimes also need to check if a
2062 declaration *must* have an initialization expression. */
2063 if (gfc_current_state () != COMP_DERIVED)
2064 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2065 else
d9b3f26b 2066 {
2294b616 2067 if (current_ts.type == BT_DERIVED
1a9745d2 2068 && !current_attr.pointer && !initializer)
d9b3f26b 2069 initializer = gfc_default_initializer (&current_ts);
2070 t = build_struct (name, cl, &initializer, &as);
2071 }
4ee9c684 2072
2073 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2074
2075cleanup:
2076 /* Free stuff up and return. */
2077 gfc_free_expr (initializer);
2078 gfc_free_array_spec (as);
2079
2080 return m;
2081}
2082
2083
d10f89ee 2084/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2085 This assumes that the byte size is equal to the kind number for
2086 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
4ee9c684 2087
2088match
1a9745d2 2089gfc_match_old_kind_spec (gfc_typespec *ts)
4ee9c684 2090{
2091 match m;
3bd3b616 2092 int original_kind;
4ee9c684 2093
2094 if (gfc_match_char ('*') != MATCH_YES)
2095 return MATCH_NO;
2096
3bd3b616 2097 m = gfc_match_small_literal_int (&ts->kind, NULL);
4ee9c684 2098 if (m != MATCH_YES)
2099 return MATCH_ERROR;
2100
b118a35b 2101 original_kind = ts->kind;
2102
4ee9c684 2103 /* Massage the kind numbers for complex types. */
b118a35b 2104 if (ts->type == BT_COMPLEX)
2105 {
2106 if (ts->kind % 2)
1a9745d2 2107 {
2108 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2109 gfc_basic_typename (ts->type), original_kind);
2110 return MATCH_ERROR;
2111 }
b118a35b 2112 ts->kind /= 2;
2d76519f 2113
2114 }
2115
2116 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2117 ts->kind = 8;
2118
2119 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2120 {
2121 if (ts->kind == 4)
2122 {
2123 if (gfc_option.flag_real4_kind == 8)
2124 ts->kind = 8;
2125 if (gfc_option.flag_real4_kind == 10)
2126 ts->kind = 10;
2127 if (gfc_option.flag_real4_kind == 16)
2128 ts->kind = 16;
2129 }
2130
2131 if (ts->kind == 8)
2132 {
2133 if (gfc_option.flag_real8_kind == 4)
2134 ts->kind = 4;
2135 if (gfc_option.flag_real8_kind == 10)
2136 ts->kind = 10;
2137 if (gfc_option.flag_real8_kind == 16)
2138 ts->kind = 16;
2139 }
b118a35b 2140 }
4ee9c684 2141
f2d4ef3b 2142 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 2143 {
b118a35b 2144 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1a9745d2 2145 gfc_basic_typename (ts->type), original_kind);
4ee9c684 2146 return MATCH_ERROR;
2147 }
2148
be7f01a1 2149 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2150 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2151 return MATCH_ERROR;
2152
4ee9c684 2153 return MATCH_YES;
2154}
2155
2156
2157/* Match a kind specification. Since kinds are generally optional, we
2158 usually return MATCH_NO if something goes wrong. If a "kind="
2159 string is found, then we know we have an error. */
2160
2161match
67a51c8e 2162gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
4ee9c684 2163{
67a51c8e 2164 locus where, loc;
4ee9c684 2165 gfc_expr *e;
2166 match m, n;
c632ff3d 2167 char c;
4ee9c684 2168 const char *msg;
2169
2170 m = MATCH_NO;
67a51c8e 2171 n = MATCH_YES;
4ee9c684 2172 e = NULL;
2173
67a51c8e 2174 where = loc = gfc_current_locus;
2175
2176 if (kind_expr_only)
2177 goto kind_expr;
4ee9c684 2178
2179 if (gfc_match_char ('(') == MATCH_NO)
2180 return MATCH_NO;
2181
2182 /* Also gobbles optional text. */
2183 if (gfc_match (" kind = ") == MATCH_YES)
2184 m = MATCH_ERROR;
2185
67a51c8e 2186 loc = gfc_current_locus;
2187
2188kind_expr:
4ee9c684 2189 n = gfc_match_init_expr (&e);
67a51c8e 2190
4ee9c684 2191 if (n != MATCH_YES)
67a51c8e 2192 {
077932f9 2193 if (gfc_matching_function)
67a51c8e 2194 {
077932f9 2195 /* The function kind expression might include use associated or
2196 imported parameters and try again after the specification
2197 expressions..... */
67a51c8e 2198 if (gfc_match_char (')') != MATCH_YES)
2199 {
2200 gfc_error ("Missing right parenthesis at %C");
2201 m = MATCH_ERROR;
2202 goto no_match;
2203 }
2204
2205 gfc_free_expr (e);
67a51c8e 2206 gfc_undo_symbols ();
2207 return MATCH_YES;
2208 }
2209 else
2210 {
2211 /* ....or else, the match is real. */
2212 if (n == MATCH_NO)
2213 gfc_error ("Expected initialization expression at %C");
2214 if (n != MATCH_YES)
2215 return MATCH_ERROR;
2216 }
2217 }
4ee9c684 2218
2219 if (e->rank != 0)
2220 {
2221 gfc_error ("Expected scalar initialization expression at %C");
2222 m = MATCH_ERROR;
2223 goto no_match;
2224 }
2225
2226 msg = gfc_extract_int (e, &ts->kind);
077932f9 2227
4ee9c684 2228 if (msg != NULL)
2229 {
2230 gfc_error (msg);
2231 m = MATCH_ERROR;
2232 goto no_match;
2233 }
2234
c5d33754 2235 /* Before throwing away the expression, let's see if we had a
2236 C interoperable kind (and store the fact). */
2237 if (e->ts.is_c_interop == 1)
2238 {
df084314 2239 /* Mark this as C interoperable if being declared with one
c5d33754 2240 of the named constants from iso_c_binding. */
2241 ts->is_c_interop = e->ts.is_iso_c;
2242 ts->f90_type = e->ts.f90_type;
2243 }
2244
4ee9c684 2245 gfc_free_expr (e);
2246 e = NULL;
2247
c5d33754 2248 /* Ignore errors to this point, if we've gotten here. This means
2249 we ignore the m=MATCH_ERROR from above. */
f2d4ef3b 2250 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 2251 {
2252 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2253 gfc_basic_typename (ts->type));
c632ff3d 2254 gfc_current_locus = where;
2255 return MATCH_ERROR;
4ee9c684 2256 }
c632ff3d 2257
209d3778 2258 /* Warn if, e.g., c_int is used for a REAL variable, but not
2259 if, e.g., c_double is used for COMPLEX as the standard
2260 explicitly says that the kind type parameter for complex and real
2261 variable is the same, i.e. c_float == c_float_complex. */
2262 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2263 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2264 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
ded233a6 2265 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2266 "is %s", gfc_basic_typename (ts->f90_type), &where,
2267 gfc_basic_typename (ts->type));
209d3778 2268
c632ff3d 2269 gfc_gobble_whitespace ();
e0be6f02 2270 if ((c = gfc_next_ascii_char ()) != ')'
2271 && (ts->type != BT_CHARACTER || c != ','))
4ee9c684 2272 {
c632ff3d 2273 if (ts->type == BT_CHARACTER)
2274 gfc_error ("Missing right parenthesis or comma at %C");
2275 else
2276 gfc_error ("Missing right parenthesis at %C");
67a51c8e 2277 m = MATCH_ERROR;
4ee9c684 2278 }
c5d33754 2279 else
2280 /* All tests passed. */
2281 m = MATCH_YES;
4ee9c684 2282
c5d33754 2283 if(m == MATCH_ERROR)
2284 gfc_current_locus = where;
2d76519f 2285
2286 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2287 ts->kind = 8;
2288
2289 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2290 {
2291 if (ts->kind == 4)
2292 {
2293 if (gfc_option.flag_real4_kind == 8)
2294 ts->kind = 8;
2295 if (gfc_option.flag_real4_kind == 10)
2296 ts->kind = 10;
2297 if (gfc_option.flag_real4_kind == 16)
2298 ts->kind = 16;
2299 }
2300
2301 if (ts->kind == 8)
2302 {
2303 if (gfc_option.flag_real8_kind == 4)
2304 ts->kind = 4;
2305 if (gfc_option.flag_real8_kind == 10)
2306 ts->kind = 10;
2307 if (gfc_option.flag_real8_kind == 16)
2308 ts->kind = 16;
2309 }
2310 }
2311
c5d33754 2312 /* Return what we know from the test(s). */
2313 return m;
4ee9c684 2314
2315no_match:
2316 gfc_free_expr (e);
cbb9e6aa 2317 gfc_current_locus = where;
4ee9c684 2318 return m;
2319}
2320
2321
33399208 2322static match
2323match_char_kind (int * kind, int * is_iso_c)
2324{
2325 locus where;
2326 gfc_expr *e;
2327 match m, n;
2328 const char *msg;
2329
2330 m = MATCH_NO;
2331 e = NULL;
2332 where = gfc_current_locus;
2333
2334 n = gfc_match_init_expr (&e);
c632ff3d 2335
077932f9 2336 if (n != MATCH_YES && gfc_matching_function)
c632ff3d 2337 {
077932f9 2338 /* The expression might include use-associated or imported
2339 parameters and try again after the specification
2340 expressions. */
c632ff3d 2341 gfc_free_expr (e);
c632ff3d 2342 gfc_undo_symbols ();
2343 return MATCH_YES;
2344 }
2345
33399208 2346 if (n == MATCH_NO)
2347 gfc_error ("Expected initialization expression at %C");
2348 if (n != MATCH_YES)
2349 return MATCH_ERROR;
2350
2351 if (e->rank != 0)
2352 {
2353 gfc_error ("Expected scalar initialization expression at %C");
2354 m = MATCH_ERROR;
2355 goto no_match;
2356 }
2357
2358 msg = gfc_extract_int (e, kind);
2359 *is_iso_c = e->ts.is_iso_c;
2360 if (msg != NULL)
2361 {
2362 gfc_error (msg);
2363 m = MATCH_ERROR;
2364 goto no_match;
2365 }
2366
2367 gfc_free_expr (e);
2368
2369 /* Ignore errors to this point, if we've gotten here. This means
2370 we ignore the m=MATCH_ERROR from above. */
2371 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2372 {
2373 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2374 m = MATCH_ERROR;
2375 }
2376 else
2377 /* All tests passed. */
2378 m = MATCH_YES;
2379
2380 if (m == MATCH_ERROR)
2381 gfc_current_locus = where;
2382
2383 /* Return what we know from the test(s). */
2384 return m;
2385
2386no_match:
2387 gfc_free_expr (e);
2388 gfc_current_locus = where;
2389 return m;
2390}
2391
10f5dcc0 2392
4ee9c684 2393/* Match the various kind/length specifications in a CHARACTER
2394 declaration. We don't return MATCH_NO. */
2395
10f5dcc0 2396match
2397gfc_match_char_spec (gfc_typespec *ts)
4ee9c684 2398{
33399208 2399 int kind, seen_length, is_iso_c;
4ee9c684 2400 gfc_charlen *cl;
2401 gfc_expr *len;
2402 match m;
3e715c81 2403 bool deferred;
33399208 2404
4ee9c684 2405 len = NULL;
2406 seen_length = 0;
33399208 2407 kind = 0;
2408 is_iso_c = 0;
3e715c81 2409 deferred = false;
4ee9c684 2410
2411 /* Try the old-style specification first. */
2412 old_char_selector = 0;
2413
926b8757 2414 m = match_char_length (&len, &deferred, true);
4ee9c684 2415 if (m != MATCH_NO)
2416 {
2417 if (m == MATCH_YES)
2418 old_char_selector = 1;
2419 seen_length = 1;
2420 goto done;
2421 }
2422
2423 m = gfc_match_char ('(');
2424 if (m != MATCH_YES)
2425 {
c5d33754 2426 m = MATCH_YES; /* Character without length is a single char. */
4ee9c684 2427 goto done;
2428 }
2429
c5d33754 2430 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
4ee9c684 2431 if (gfc_match (" kind =") == MATCH_YES)
2432 {
33399208 2433 m = match_char_kind (&kind, &is_iso_c);
c5d33754 2434
4ee9c684 2435 if (m == MATCH_ERROR)
2436 goto done;
2437 if (m == MATCH_NO)
2438 goto syntax;
2439
2440 if (gfc_match (" , len =") == MATCH_NO)
2441 goto rparen;
2442
3e715c81 2443 m = char_len_param_value (&len, &deferred);
4ee9c684 2444 if (m == MATCH_NO)
2445 goto syntax;
2446 if (m == MATCH_ERROR)
2447 goto done;
2448 seen_length = 1;
2449
2450 goto rparen;
2451 }
2452
f6d0e37a 2453 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
4ee9c684 2454 if (gfc_match (" len =") == MATCH_YES)
2455 {
3e715c81 2456 m = char_len_param_value (&len, &deferred);
4ee9c684 2457 if (m == MATCH_NO)
2458 goto syntax;
2459 if (m == MATCH_ERROR)
2460 goto done;
2461 seen_length = 1;
2462
2463 if (gfc_match_char (')') == MATCH_YES)
2464 goto done;
2465
2466 if (gfc_match (" , kind =") != MATCH_YES)
2467 goto syntax;
2468
33399208 2469 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2470 goto done;
4ee9c684 2471
2472 goto rparen;
2473 }
2474
f6d0e37a 2475 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3e715c81 2476 m = char_len_param_value (&len, &deferred);
4ee9c684 2477 if (m == MATCH_NO)
2478 goto syntax;
2479 if (m == MATCH_ERROR)
2480 goto done;
2481 seen_length = 1;
2482
2483 m = gfc_match_char (')');
2484 if (m == MATCH_YES)
2485 goto done;
2486
2487 if (gfc_match_char (',') != MATCH_YES)
2488 goto syntax;
2489
c5d33754 2490 gfc_match (" kind ="); /* Gobble optional text. */
4ee9c684 2491
33399208 2492 m = match_char_kind (&kind, &is_iso_c);
4ee9c684 2493 if (m == MATCH_ERROR)
2494 goto done;
2495 if (m == MATCH_NO)
2496 goto syntax;
2497
2498rparen:
2499 /* Require a right-paren at this point. */
2500 m = gfc_match_char (')');
2501 if (m == MATCH_YES)
2502 goto done;
2503
2504syntax:
2505 gfc_error ("Syntax error in CHARACTER declaration at %C");
2506 m = MATCH_ERROR;
a3cbe8cc 2507 gfc_free_expr (len);
2508 return m;
4ee9c684 2509
2510done:
8d39570e 2511 /* Deal with character functions after USE and IMPORT statements. */
2512 if (gfc_matching_function)
077932f9 2513 {
8d39570e 2514 gfc_free_expr (len);
077932f9 2515 gfc_undo_symbols ();
2516 return MATCH_YES;
2517 }
2518
4ee9c684 2519 if (m != MATCH_YES)
2520 {
2521 gfc_free_expr (len);
2522 return m;
2523 }
2524
2525 /* Do some final massaging of the length values. */
d270ce52 2526 cl = gfc_new_charlen (gfc_current_ns, NULL);
4ee9c684 2527
2528 if (seen_length == 0)
126387b5 2529 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4ee9c684 2530 else
2fe2caa6 2531 cl->length = len;
4ee9c684 2532
eeebe20b 2533 ts->u.cl = cl;
33399208 2534 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3e715c81 2535 ts->deferred = deferred;
4ee9c684 2536
df084314 2537 /* We have to know if it was a C interoperable kind so we can
c5d33754 2538 do accurate type checking of bind(c) procs, etc. */
33399208 2539 if (kind != 0)
df084314 2540 /* Mark this as C interoperable if being declared with one
33399208 2541 of the named constants from iso_c_binding. */
2542 ts->is_c_interop = is_iso_c;
c5d33754 2543 else if (len != NULL)
33399208 2544 /* Here, we might have parsed something such as: character(c_char)
2545 In this case, the parsing code above grabs the c_char when
2546 looking for the length (line 1690, roughly). it's the last
2547 testcase for parsing the kind params of a character variable.
2548 However, it's not actually the length. this seems like it
2549 could be an error.
2550 To see if the user used a C interop kind, test the expr
2551 of the so called length, and see if it's C interoperable. */
2552 ts->is_c_interop = len->ts.is_iso_c;
c5d33754 2553
4ee9c684 2554 return MATCH_YES;
2555}
2556
2557
e8152f13 2558/* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2559 structure to the matched specification. This is necessary for FUNCTION and
4ee9c684 2560 IMPLICIT statements.
2561
e14bee04 2562 If implicit_flag is nonzero, then we don't check for the optional
39351103 2563 kind specification. Not doing so is needed for matching an IMPLICIT
4ee9c684 2564 statement correctly. */
2565
67a51c8e 2566match
e8152f13 2567gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4ee9c684 2568{
2569 char name[GFC_MAX_SYMBOL_LEN + 1];
c2958b6b 2570 gfc_symbol *sym, *dt_sym;
4ee9c684 2571 match m;
e0be6f02 2572 char c;
f446fb22 2573 bool seen_deferred_kind, matched_type;
c2958b6b 2574 const char *dt_name;
4ee9c684 2575
077932f9 2576 /* A belt and braces check that the typespec is correctly being treated
2577 as a deferred characteristic association. */
2578 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
8d39570e 2579 && (gfc_current_block ()->result->ts.kind == -1)
2580 && (ts->kind == -1);
4ee9c684 2581 gfc_clear_ts (ts);
077932f9 2582 if (seen_deferred_kind)
2583 ts->kind = -1;
4ee9c684 2584
c5d33754 2585 /* Clear the current binding label, in case one is given. */
7b2060ba 2586 curr_binding_label = NULL;
c5d33754 2587
25b29122 2588 if (gfc_match (" byte") == MATCH_YES)
2589 {
f25dbbf7 2590 if (gfc_notify_std (GFC_STD_GNU, "BYTE type at %C")
25b29122 2591 == FAILURE)
2592 return MATCH_ERROR;
2593
2594 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2595 {
2596 gfc_error ("BYTE type used at %C "
2597 "is not available on the target machine");
2598 return MATCH_ERROR;
2599 }
e14bee04 2600
25b29122 2601 ts->type = BT_INTEGER;
2602 ts->kind = 1;
2603 return MATCH_YES;
2604 }
2605
f446fb22 2606
8c2d8d6d 2607 m = gfc_match (" type (");
f446fb22 2608 matched_type = (m == MATCH_YES);
8c2d8d6d 2609 if (matched_type)
2610 {
2611 gfc_gobble_whitespace ();
2612 if (gfc_peek_ascii_char () == '*')
2613 {
2614 if ((m = gfc_match ("*)")) != MATCH_YES)
2615 return m;
2616 if (gfc_current_state () == COMP_DERIVED)
2617 {
2618 gfc_error ("Assumed type at %C is not allowed for components");
2619 return MATCH_ERROR;
2620 }
f25dbbf7 2621 if (gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
8c2d8d6d 2622 "at %C") == FAILURE)
2623 return MATCH_ERROR;
2624 ts->type = BT_ASSUMED;
2625 return MATCH_YES;
2626 }
2627
2628 m = gfc_match ("%n", name);
2629 matched_type = (m == MATCH_YES);
2630 }
2631
f446fb22 2632 if ((matched_type && strcmp ("integer", name) == 0)
2633 || (!matched_type && gfc_match (" integer") == MATCH_YES))
4ee9c684 2634 {
2635 ts->type = BT_INTEGER;
b8a891cb 2636 ts->kind = gfc_default_integer_kind;
4ee9c684 2637 goto get_kind;
2638 }
2639
f446fb22 2640 if ((matched_type && strcmp ("character", name) == 0)
2641 || (!matched_type && gfc_match (" character") == MATCH_YES))
4ee9c684 2642 {
f446fb22 2643 if (matched_type
f25dbbf7 2644 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
f446fb22 2645 "intrinsic-type-spec at %C") == FAILURE)
2646 return MATCH_ERROR;
2647
4ee9c684 2648 ts->type = BT_CHARACTER;
39351103 2649 if (implicit_flag == 0)
f446fb22 2650 m = gfc_match_char_spec (ts);
39351103 2651 else
f446fb22 2652 m = MATCH_YES;
2653
2654 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2655 m = MATCH_ERROR;
2656
2657 return m;
4ee9c684 2658 }
2659
f446fb22 2660 if ((matched_type && strcmp ("real", name) == 0)
2661 || (!matched_type && gfc_match (" real") == MATCH_YES))
4ee9c684 2662 {
2663 ts->type = BT_REAL;
b8a891cb 2664 ts->kind = gfc_default_real_kind;
4ee9c684 2665 goto get_kind;
2666 }
2667
f446fb22 2668 if ((matched_type
2669 && (strcmp ("doubleprecision", name) == 0
2670 || (strcmp ("double", name) == 0
2671 && gfc_match (" precision") == MATCH_YES)))
2672 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4ee9c684 2673 {
f446fb22 2674 if (matched_type
f25dbbf7 2675 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
f446fb22 2676 "intrinsic-type-spec at %C") == FAILURE)
2677 return MATCH_ERROR;
2678 if (matched_type && gfc_match_char (')') != MATCH_YES)
2679 return MATCH_ERROR;
2680
4ee9c684 2681 ts->type = BT_REAL;
b8a891cb 2682 ts->kind = gfc_default_double_kind;
4ee9c684 2683 return MATCH_YES;
2684 }
2685
f446fb22 2686 if ((matched_type && strcmp ("complex", name) == 0)
2687 || (!matched_type && gfc_match (" complex") == MATCH_YES))
4ee9c684 2688 {
2689 ts->type = BT_COMPLEX;
b8a891cb 2690 ts->kind = gfc_default_complex_kind;
4ee9c684 2691 goto get_kind;
2692 }
2693
f446fb22 2694 if ((matched_type
2695 && (strcmp ("doublecomplex", name) == 0
2696 || (strcmp ("double", name) == 0
2697 && gfc_match (" complex") == MATCH_YES)))
2698 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4ee9c684 2699 {
f25dbbf7 2700 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C")
f446fb22 2701 == FAILURE)
2702 return MATCH_ERROR;
2703
2704 if (matched_type
f25dbbf7 2705 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
f446fb22 2706 "intrinsic-type-spec at %C") == FAILURE)
2707 return MATCH_ERROR;
2708
2709 if (matched_type && gfc_match_char (')') != MATCH_YES)
be7f01a1 2710 return MATCH_ERROR;
2711
4ee9c684 2712 ts->type = BT_COMPLEX;
b8a891cb 2713 ts->kind = gfc_default_double_kind;
4ee9c684 2714 return MATCH_YES;
2715 }
2716
f446fb22 2717 if ((matched_type && strcmp ("logical", name) == 0)
2718 || (!matched_type && gfc_match (" logical") == MATCH_YES))
4ee9c684 2719 {
2720 ts->type = BT_LOGICAL;
b8a891cb 2721 ts->kind = gfc_default_logical_kind;
4ee9c684 2722 goto get_kind;
2723 }
2724
f446fb22 2725 if (matched_type)
2726 m = gfc_match_char (')');
2727
1de1b1a9 2728 if (m == MATCH_YES)
2729 ts->type = BT_DERIVED;
2730 else
8ca7f89c 2731 {
fa102e56 2732 /* Match CLASS declarations. */
2733 m = gfc_match (" class ( * )");
2734 if (m == MATCH_ERROR)
2735 return MATCH_ERROR;
2736 else if (m == MATCH_YES)
2737 {
2738 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2739 return MATCH_ERROR;
2740 }
2741
8ca7f89c 2742 m = gfc_match (" class ( %n )", name);
2743 if (m != MATCH_YES)
2744 return m;
1de1b1a9 2745 ts->type = BT_CLASS;
8ca7f89c 2746
f25dbbf7 2747 if (gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C")
e8152f13 2748 == FAILURE)
2749 return MATCH_ERROR;
8ca7f89c 2750 }
4ee9c684 2751
077932f9 2752 /* Defer association of the derived type until the end of the
2753 specification block. However, if the derived type can be
2754 found, add it to the typespec. */
2755 if (gfc_matching_function)
67a51c8e 2756 {
eeebe20b 2757 ts->u.derived = NULL;
077932f9 2758 if (gfc_current_state () != COMP_INTERFACE
2759 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
c2958b6b 2760 {
2761 sym = gfc_find_dt_in_generic (sym);
2762 ts->u.derived = sym;
2763 }
67a51c8e 2764 return MATCH_YES;
2765 }
2766
2767 /* Search for the name but allow the components to be defined later. If
2768 type = -1, this typespec has been seen in a function declaration but
c2958b6b 2769 the type could not be accessed at that point. The actual derived type is
df084314 2770 stored in a symtree with the first letter of the name capitalized; the
c2958b6b 2771 symtree with the all lower-case name contains the associated
2772 generic function. */
2773 dt_name = gfc_get_string ("%c%s",
2774 (char) TOUPPER ((unsigned char) name[0]),
2775 (const char*)&name[1]);
077932f9 2776 sym = NULL;
c2958b6b 2777 dt_sym = NULL;
2778 if (ts->kind != -1)
4ee9c684 2779 {
c2958b6b 2780 gfc_get_ha_symbol (name, &sym);
2781 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2782 {
2783 gfc_error ("Type name '%s' at %C is ambiguous", name);
2784 return MATCH_ERROR;
2785 }
2786 if (sym->generic && !dt_sym)
2787 dt_sym = gfc_find_dt_in_generic (sym);
4ee9c684 2788 }
67a51c8e 2789 else if (ts->kind == -1)
2790 {
077932f9 2791 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2792 || gfc_current_ns->has_import_set;
c2958b6b 2793 gfc_find_symbol (name, NULL, iface, &sym);
2794 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
67a51c8e 2795 {
2796 gfc_error ("Type name '%s' at %C is ambiguous", name);
2797 return MATCH_ERROR;
2798 }
c2958b6b 2799 if (sym && sym->generic && !dt_sym)
2800 dt_sym = gfc_find_dt_in_generic (sym);
67a51c8e 2801
077932f9 2802 ts->kind = 0;
67a51c8e 2803 if (sym == NULL)
2804 return MATCH_NO;
2805 }
4ee9c684 2806
c2958b6b 2807 if ((sym->attr.flavor != FL_UNKNOWN
2808 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2809 || sym->attr.subroutine)
2810 {
2811 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2812 "entity at %L, which has the same name", name,
2813 &sym->declared_at);
2814 return MATCH_ERROR;
2815 }
4ee9c684 2816
077932f9 2817 gfc_set_sym_referenced (sym);
c2958b6b 2818 if (!sym->attr.generic
2819 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2820 return MATCH_ERROR;
2821
2822 if (!sym->attr.function
2823 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2824 return MATCH_ERROR;
2825
2826 if (!dt_sym)
2827 {
2828 gfc_interface *intr, *head;
2829
2830 /* Use upper case to save the actual derived-type symbol. */
2831 gfc_get_symbol (dt_name, NULL, &dt_sym);
2832 dt_sym->name = gfc_get_string (sym->name);
2833 head = sym->generic;
2834 intr = gfc_get_interface ();
2835 intr->sym = dt_sym;
2836 intr->where = gfc_current_locus;
2837 intr->next = head;
2838 sym->generic = intr;
2839 sym->attr.if_source = IFSRC_DECL;
2840 }
2841
2842 gfc_set_sym_referenced (dt_sym);
2843
2844 if (dt_sym->attr.flavor != FL_DERIVED
2845 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2846 == FAILURE)
2847 return MATCH_ERROR;
2848
2849 ts->u.derived = dt_sym;
4ee9c684 2850
2851 return MATCH_YES;
2852
2853get_kind:
f446fb22 2854 if (matched_type
f25dbbf7 2855 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
f446fb22 2856 "intrinsic-type-spec at %C") == FAILURE)
2857 return MATCH_ERROR;
2858
4ee9c684 2859 /* For all types except double, derived and character, look for an
2860 optional kind specifier. MATCH_NO is actually OK at this point. */
39351103 2861 if (implicit_flag == 1)
f446fb22 2862 {
2863 if (matched_type && gfc_match_char (')') != MATCH_YES)
2864 return MATCH_ERROR;
2865
2866 return MATCH_YES;
2867 }
4ee9c684 2868
18f3698a 2869 if (gfc_current_form == FORM_FREE)
2870 {
2c89e2a0 2871 c = gfc_peek_ascii_char ();
2872 if (!gfc_is_whitespace (c) && c != '*' && c != '('
1a9745d2 2873 && c != ':' && c != ',')
f446fb22 2874 {
2875 if (matched_type && c == ')')
2876 {
2877 gfc_next_ascii_char ();
2878 return MATCH_YES;
2879 }
2880 return MATCH_NO;
2881 }
18f3698a 2882 }
2883
67a51c8e 2884 m = gfc_match_kind_spec (ts, false);
4ee9c684 2885 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2886 m = gfc_match_old_kind_spec (ts);
2887
f446fb22 2888 if (matched_type && gfc_match_char (')') != MATCH_YES)
2889 return MATCH_ERROR;
2890
077932f9 2891 /* Defer association of the KIND expression of function results
2892 until after USE and IMPORT statements. */
2893 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2894 || gfc_matching_function)
2895 return MATCH_YES;
2896
4ee9c684 2897 if (m == MATCH_NO)
2898 m = MATCH_YES; /* No kind specifier found. */
2899
2900 return m;
2901}
2902
2903
39351103 2904/* Match an IMPLICIT NONE statement. Actually, this statement is
2905 already matched in parse.c, or we would not end up here in the
2906 first place. So the only thing we need to check, is if there is
2907 trailing garbage. If not, the match is successful. */
2908
2909match
2910gfc_match_implicit_none (void)
2911{
39351103 2912 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2913}
2914
2915
2916/* Match the letter range(s) of an IMPLICIT statement. */
2917
2918static match
b70528c7 2919match_implicit_range (void)
39351103 2920{
e0be6f02 2921 char c, c1, c2;
2922 int inner;
39351103 2923 locus cur_loc;
2924
2925 cur_loc = gfc_current_locus;
2926
2927 gfc_gobble_whitespace ();
e0be6f02 2928 c = gfc_next_ascii_char ();
39351103 2929 if (c != '(')
2930 {
2931 gfc_error ("Missing character range in IMPLICIT at %C");
2932 goto bad;
2933 }
2934
2935 inner = 1;
2936 while (inner)
2937 {
2938 gfc_gobble_whitespace ();
e0be6f02 2939 c1 = gfc_next_ascii_char ();
39351103 2940 if (!ISALPHA (c1))
2941 goto bad;
2942
2943 gfc_gobble_whitespace ();
e0be6f02 2944 c = gfc_next_ascii_char ();
39351103 2945
2946 switch (c)
2947 {
2948 case ')':
f6d0e37a 2949 inner = 0; /* Fall through. */
39351103 2950
2951 case ',':
2952 c2 = c1;
2953 break;
2954
2955 case '-':
2956 gfc_gobble_whitespace ();
e0be6f02 2957 c2 = gfc_next_ascii_char ();
39351103 2958 if (!ISALPHA (c2))
2959 goto bad;
2960
2961 gfc_gobble_whitespace ();
e0be6f02 2962 c = gfc_next_ascii_char ();
39351103 2963
2964 if ((c != ',') && (c != ')'))
2965 goto bad;
2966 if (c == ')')
2967 inner = 0;
2968
2969 break;
2970
2971 default:
2972 goto bad;
2973 }
2974
2975 if (c1 > c2)
2976 {
2977 gfc_error ("Letters must be in alphabetic order in "
2978 "IMPLICIT statement at %C");
2979 goto bad;
2980 }
2981
2982 /* See if we can add the newly matched range to the pending
1a9745d2 2983 implicits from this IMPLICIT statement. We do not check for
2984 conflicts with whatever earlier IMPLICIT statements may have
2985 set. This is done when we've successfully finished matching
2986 the current one. */
b70528c7 2987 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
39351103 2988 goto bad;
2989 }
2990
2991 return MATCH_YES;
2992
2993bad:
2994 gfc_syntax_error (ST_IMPLICIT);
2995
2996 gfc_current_locus = cur_loc;
2997 return MATCH_ERROR;
2998}
2999
3000
3001/* Match an IMPLICIT statement, storing the types for
3002 gfc_set_implicit() if the statement is accepted by the parser.
3003 There is a strange looking, but legal syntactic construction
3004 possible. It looks like:
3005
3006 IMPLICIT INTEGER (a-b) (c-d)
3007
3008 This is legal if "a-b" is a constant expression that happens to
3009 equal one of the legal kinds for integers. The real problem
3010 happens with an implicit specification that looks like:
3011
3012 IMPLICIT INTEGER (a-b)
3013
3014 In this case, a typespec matcher that is "greedy" (as most of the
3015 matchers are) gobbles the character range as a kindspec, leaving
3016 nothing left. We therefore have to go a bit more slowly in the
3017 matching process by inhibiting the kindspec checking during
3018 typespec matching and checking for a kind later. */
3019
3020match
3021gfc_match_implicit (void)
3022{
3023 gfc_typespec ts;
3024 locus cur_loc;
e0be6f02 3025 char c;
39351103 3026 match m;
3027
52179f31 3028 gfc_clear_ts (&ts);
3029
39351103 3030 /* We don't allow empty implicit statements. */
3031 if (gfc_match_eos () == MATCH_YES)
3032 {
3033 gfc_error ("Empty IMPLICIT statement at %C");
3034 return MATCH_ERROR;
3035 }
3036
39351103 3037 do
3038 {
b70528c7 3039 /* First cleanup. */
3040 gfc_clear_new_implicit ();
3041
39351103 3042 /* A basic type is mandatory here. */
e8152f13 3043 m = gfc_match_decl_type_spec (&ts, 1);
39351103 3044 if (m == MATCH_ERROR)
3045 goto error;
3046 if (m == MATCH_NO)
3047 goto syntax;
3048
3049 cur_loc = gfc_current_locus;
b70528c7 3050 m = match_implicit_range ();
39351103 3051
3052 if (m == MATCH_YES)
3053 {
b70528c7 3054 /* We may have <TYPE> (<RANGE>). */
39351103 3055 gfc_gobble_whitespace ();
e0be6f02 3056 c = gfc_next_ascii_char ();
39351103 3057 if ((c == '\n') || (c == ','))
b70528c7 3058 {
3059 /* Check for CHARACTER with no length parameter. */
eeebe20b 3060 if (ts.type == BT_CHARACTER && !ts.u.cl)
b70528c7 3061 {
b8a891cb 3062 ts.kind = gfc_default_character_kind;
d270ce52 3063 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
126387b5 3064 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3065 NULL, 1);
b70528c7 3066 }
3067
3068 /* Record the Successful match. */
3069 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3070 return MATCH_ERROR;
3071 continue;
3072 }
39351103 3073
3074 gfc_current_locus = cur_loc;
3075 }
3076
b70528c7 3077 /* Discard the (incorrectly) matched range. */
3078 gfc_clear_new_implicit ();
3079
3080 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3081 if (ts.type == BT_CHARACTER)
10f5dcc0 3082 m = gfc_match_char_spec (&ts);
b70528c7 3083 else
39351103 3084 {
67a51c8e 3085 m = gfc_match_kind_spec (&ts, false);
39351103 3086 if (m == MATCH_NO)
b70528c7 3087 {
3088 m = gfc_match_old_kind_spec (&ts);
3089 if (m == MATCH_ERROR)
3090 goto error;
3091 if (m == MATCH_NO)
3092 goto syntax;
3093 }
39351103 3094 }
b70528c7 3095 if (m == MATCH_ERROR)
3096 goto error;
39351103 3097
b70528c7 3098 m = match_implicit_range ();
39351103 3099 if (m == MATCH_ERROR)
3100 goto error;
3101 if (m == MATCH_NO)
3102 goto syntax;
3103
3104 gfc_gobble_whitespace ();
e0be6f02 3105 c = gfc_next_ascii_char ();
39351103 3106 if ((c != '\n') && (c != ','))
3107 goto syntax;
3108
b70528c7 3109 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3110 return MATCH_ERROR;
39351103 3111 }
3112 while (c == ',');
3113
b70528c7 3114 return MATCH_YES;
39351103 3115
3116syntax:
3117 gfc_syntax_error (ST_IMPLICIT);
3118
3119error:
3120 return MATCH_ERROR;
3121}
3122
f6d0e37a 3123
d67fc9ae 3124match
3125gfc_match_import (void)
3126{
3127 char name[GFC_MAX_SYMBOL_LEN + 1];
3128 match m;
3129 gfc_symbol *sym;
3130 gfc_symtree *st;
3131
f6d0e37a 3132 if (gfc_current_ns->proc_name == NULL
3133 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
d67fc9ae 3134 {
3135 gfc_error ("IMPORT statement at %C only permitted in "
3136 "an INTERFACE body");
3137 return MATCH_ERROR;
3138 }
3139
f25dbbf7 3140 if (gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C")
d67fc9ae 3141 == FAILURE)
3142 return MATCH_ERROR;
3143
3144 if (gfc_match_eos () == MATCH_YES)
3145 {
3146 /* All host variables should be imported. */
3147 gfc_current_ns->has_import_set = 1;
3148 return MATCH_YES;
3149 }
3150
3151 if (gfc_match (" ::") == MATCH_YES)
3152 {
3153 if (gfc_match_eos () == MATCH_YES)
1a9745d2 3154 {
3155 gfc_error ("Expecting list of named entities at %C");
3156 return MATCH_ERROR;
3157 }
d67fc9ae 3158 }
3159
3160 for(;;)
3161 {
495e197c 3162 sym = NULL;
d67fc9ae 3163 m = gfc_match (" %n", name);
3164 switch (m)
3165 {
3166 case MATCH_YES:
096d4ad9 3167 if (gfc_current_ns->parent != NULL
f6d0e37a 3168 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
096d4ad9 3169 {
3170 gfc_error ("Type name '%s' at %C is ambiguous", name);
3171 return MATCH_ERROR;
3172 }
6b010511 3173 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
f6d0e37a 3174 && gfc_find_symbol (name,
3175 gfc_current_ns->proc_name->ns->parent,
3176 1, &sym))
1a9745d2 3177 {
3178 gfc_error ("Type name '%s' at %C is ambiguous", name);
3179 return MATCH_ERROR;
3180 }
3181
3182 if (sym == NULL)
3183 {
3184 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3185 "at %C - does not exist.", name);
3186 return MATCH_ERROR;
3187 }
3188
e14bee04 3189 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
1a9745d2 3190 {
3191 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3192 "at %C.", name);
3193 goto next_item;
3194 }
3195
a15d1caf 3196 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
1a9745d2 3197 st->n.sym = sym;
3198 sym->refs++;
c723595c 3199 sym->attr.imported = 1;
d67fc9ae 3200
c2958b6b 3201 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3202 {
3203 /* The actual derived type is stored in a symtree with the first
df084314 3204 letter of the name capitalized; the symtree with the all
c2958b6b 3205 lower-case name contains the associated generic function. */
3206 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3207 gfc_get_string ("%c%s",
3208 (char) TOUPPER ((unsigned char) sym->name[0]),
3209 &sym->name[1]));
3210 st->n.sym = sym;
3211 sym->refs++;
3212 sym->attr.imported = 1;
3213 }
3214
d67fc9ae 3215 goto next_item;
3216
3217 case MATCH_NO:
3218 break;
3219
3220 case MATCH_ERROR:
3221 return MATCH_ERROR;
3222 }
3223
3224 next_item:
3225 if (gfc_match_eos () == MATCH_YES)
3226 break;
3227 if (gfc_match_char (',') != MATCH_YES)
3228 goto syntax;
3229 }
3230
3231 return MATCH_YES;
3232
3233syntax:
3234 gfc_error ("Syntax error in IMPORT statement at %C");
3235 return MATCH_ERROR;
3236}
39351103 3237
f6d0e37a 3238
c72e5f7e 3239/* A minimal implementation of gfc_match without whitespace, escape
3240 characters or variable arguments. Returns true if the next
3241 characters match the TARGET template exactly. */
3242
3243static bool
3244match_string_p (const char *target)
3245{
3246 const char *p;
3247
3248 for (p = target; *p; p++)
e0be6f02 3249 if ((char) gfc_next_ascii_char () != *p)
c72e5f7e 3250 return false;
3251 return true;
3252}
3253
4ee9c684 3254/* Matches an attribute specification including array specs. If
3255 successful, leaves the variables current_attr and current_as
3256 holding the specification. Also sets the colon_seen variable for
3257 later use by matchers associated with initializations.
3258
3259 This subroutine is a little tricky in the sense that we don't know
3260 if we really have an attr-spec until we hit the double colon.
3261 Until that time, we can only return MATCH_NO. This forces us to
3262 check for duplicate specification at this level. */
3263
3264static match
3265match_attr_spec (void)
3266{
4ee9c684 3267 /* Modifiers that can exist in a type statement. */
d55c3689 3268 enum
4ee9c684 3269 { GFC_DECL_BEGIN = 0,
3270 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3271 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3ea52af3 3272 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3273 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
b3c3927c 3274 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3275 DECL_NONE, GFC_DECL_END /* Sentinel */
d55c3689 3276 };
4ee9c684 3277
3278/* GFC_DECL_END is the sentinel, index starts at 0. */
3279#define NUM_DECL GFC_DECL_END
3280
4ee9c684 3281 locus start, seen_at[NUM_DECL];
3282 int seen[NUM_DECL];
9f1b7d17 3283 unsigned int d;
4ee9c684 3284 const char *attr;
3285 match m;
f6bb8f4b 3286 gfc_try t;
4ee9c684 3287
3288 gfc_clear_attr (&current_attr);
cbb9e6aa 3289 start = gfc_current_locus;
4ee9c684 3290
3291 current_as = NULL;
3292 colon_seen = 0;
3293
3294 /* See if we get all of the keywords up to the final double colon. */
3295 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3296 seen[d] = 0;
3297
3298 for (;;)
3299 {
e0be6f02 3300 char ch;
c5d33754 3301
c72e5f7e 3302 d = DECL_NONE;
3303 gfc_gobble_whitespace ();
3304
e0be6f02 3305 ch = gfc_next_ascii_char ();
c72e5f7e 3306 if (ch == ':')
3307 {
3308 /* This is the successful exit condition for the loop. */
e0be6f02 3309 if (gfc_next_ascii_char () == ':')
c72e5f7e 3310 break;
3311 }
3312 else if (ch == ',')
c5d33754 3313 {
c5d33754 3314 gfc_gobble_whitespace ();
e0be6f02 3315 switch (gfc_peek_ascii_char ())
c5d33754 3316 {
c72e5f7e 3317 case 'a':
738928be 3318 gfc_next_ascii_char ();
3319 switch (gfc_next_ascii_char ())
3320 {
3321 case 'l':
3322 if (match_string_p ("locatable"))
3323 {
3324 /* Matched "allocatable". */
3325 d = DECL_ALLOCATABLE;
3326 }
3327 break;
3328
3329 case 's':
3330 if (match_string_p ("ynchronous"))
3331 {
3332 /* Matched "asynchronous". */
3333 d = DECL_ASYNCHRONOUS;
3334 }
3335 break;
3336 }
b3c3927c 3337 break;
c72e5f7e 3338
3339 case 'b':
c5d33754 3340 /* Try and match the bind(c). */
75ae7f6c 3341 m = gfc_match_bind_c (NULL, true);
5cf92482 3342 if (m == MATCH_YES)
c5d33754 3343 d = DECL_IS_BIND_C;
5cf92482 3344 else if (m == MATCH_ERROR)
3345 goto cleanup;
c72e5f7e 3346 break;
3347
aff518b0 3348 case 'c':
b3c3927c 3349 gfc_next_ascii_char ();
3350 if ('o' != gfc_next_ascii_char ())
3351 break;
3352 switch (gfc_next_ascii_char ())
3353 {
3354 case 'd':
3355 if (match_string_p ("imension"))
3356 {
3357 d = DECL_CODIMENSION;
3358 break;
3359 }
3360 case 'n':
3361 if (match_string_p ("tiguous"))
3362 {
3363 d = DECL_CONTIGUOUS;
3364 break;
3365 }
3366 }
aff518b0 3367 break;
3368
c72e5f7e 3369 case 'd':
3370 if (match_string_p ("dimension"))
3371 d = DECL_DIMENSION;
3372 break;
3373
3374 case 'e':
3375 if (match_string_p ("external"))
3376 d = DECL_EXTERNAL;
3377 break;
3378
3379 case 'i':
3380 if (match_string_p ("int"))
3381 {
e0be6f02 3382 ch = gfc_next_ascii_char ();
c72e5f7e 3383 if (ch == 'e')
3384 {
3385 if (match_string_p ("nt"))
3386 {
3387 /* Matched "intent". */
3388 /* TODO: Call match_intent_spec from here. */
3389 if (gfc_match (" ( in out )") == MATCH_YES)
3390 d = DECL_INOUT;
3391 else if (gfc_match (" ( in )") == MATCH_YES)
3392 d = DECL_IN;
3393 else if (gfc_match (" ( out )") == MATCH_YES)
3394 d = DECL_OUT;
3395 }
3396 }
3397 else if (ch == 'r')
3398 {
3399 if (match_string_p ("insic"))
3400 {
3401 /* Matched "intrinsic". */
3402 d = DECL_INTRINSIC;
3403 }
3404 }
3405 }
3406 break;
3407
3408 case 'o':
3409 if (match_string_p ("optional"))
3410 d = DECL_OPTIONAL;
3411 break;
3412
3413 case 'p':
e0be6f02 3414 gfc_next_ascii_char ();
3415 switch (gfc_next_ascii_char ())
c72e5f7e 3416 {
3417 case 'a':
3418 if (match_string_p ("rameter"))
3419 {
3420 /* Matched "parameter". */
3421 d = DECL_PARAMETER;
3422 }
3423 break;
3424
3425 case 'o':
3426 if (match_string_p ("inter"))
3427 {
3428 /* Matched "pointer". */
3429 d = DECL_POINTER;
3430 }
3431 break;
3432
3433 case 'r':
e0be6f02 3434 ch = gfc_next_ascii_char ();
c72e5f7e 3435 if (ch == 'i')
3436 {
3437 if (match_string_p ("vate"))
3438 {
3439 /* Matched "private". */
3440 d = DECL_PRIVATE;
3441 }
3442 }
3443 else if (ch == 'o')
3444 {
3445 if (match_string_p ("tected"))
3446 {
3447 /* Matched "protected". */
3448 d = DECL_PROTECTED;
3449 }
3450 }
3451 break;
3452
3453 case 'u':
3454 if (match_string_p ("blic"))
3455 {
3456 /* Matched "public". */
3457 d = DECL_PUBLIC;
3458 }
3459 break;
3460 }
3461 break;
3462
3463 case 's':
3464 if (match_string_p ("save"))
3465 d = DECL_SAVE;
3466 break;
3467
3468 case 't':
3469 if (match_string_p ("target"))
3470 d = DECL_TARGET;
3471 break;
3472
3473 case 'v':
e0be6f02 3474 gfc_next_ascii_char ();
3475 ch = gfc_next_ascii_char ();
c72e5f7e 3476 if (ch == 'a')
3477 {
3478 if (match_string_p ("lue"))
3479 {
3480 /* Matched "value". */
3481 d = DECL_VALUE;
3482 }
3483 }
3484 else if (ch == 'o')
3485 {
3486 if (match_string_p ("latile"))
3487 {
3488 /* Matched "volatile". */
3489 d = DECL_VOLATILE;
3490 }
3491 }
3492 break;
c5d33754 3493 }
3494 }
f3f9b222 3495
c72e5f7e 3496 /* No double colon and no recognizable decl_type, so assume that
3497 we've been looking at something else the whole time. */
3498 if (d == DECL_NONE)
3499 {
3500 m = MATCH_NO;
3501 goto cleanup;
3502 }
e14bee04 3503
7e221851 3504 /* Check to make sure any parens are paired up correctly. */
3505 if (gfc_match_parens () == MATCH_ERROR)
3506 {
3507 m = MATCH_ERROR;
3508 goto cleanup;
3509 }
3510
4ee9c684 3511 seen[d]++;
cbb9e6aa 3512 seen_at[d] = gfc_current_locus;
4ee9c684 3513
e97ac7c0 3514 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
4ee9c684 3515 {
e97ac7c0 3516 gfc_array_spec *as = NULL;
4ee9c684 3517
e97ac7c0 3518 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3519 d == DECL_CODIMENSION);
3520
3521 if (current_as == NULL)
3522 current_as = as;
3523 else if (m == MATCH_YES)
4ee9c684 3524 {
9b58b4c7 3525 if (merge_array_spec (as, current_as, false) == FAILURE)
3526 m = MATCH_ERROR;
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}