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