Line data Source code
1 : /* Declaration statement matcher
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Andy Vaught
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "options.h"
25 : #include "tree.h"
26 : #include "gfortran.h"
27 : #include "stringpool.h"
28 : #include "match.h"
29 : #include "parse.h"
30 : #include "constructor.h"
31 : #include "target.h"
32 : #include "flags.h"
33 :
34 : /* Macros to access allocate memory for gfc_data_variable,
35 : gfc_data_value and gfc_data. */
36 : #define gfc_get_data_variable() XCNEW (gfc_data_variable)
37 : #define gfc_get_data_value() XCNEW (gfc_data_value)
38 : #define gfc_get_data() XCNEW (gfc_data)
39 :
40 :
41 : static bool set_binding_label (const char **, const char *, int);
42 :
43 :
44 : /* This flag is set if an old-style length selector is matched
45 : during a type-declaration statement. */
46 :
47 : static int old_char_selector;
48 :
49 : /* When variables acquire types and attributes from a declaration
50 : statement, they get them from the following static variables. The
51 : first part of a declaration sets these variables and the second
52 : part copies these into symbol structures. */
53 :
54 : static gfc_typespec current_ts;
55 :
56 : static symbol_attribute current_attr;
57 : static gfc_array_spec *current_as;
58 : static int colon_seen;
59 : static int attr_seen;
60 :
61 : /* The current binding label (if any). */
62 : static const char* curr_binding_label;
63 : /* Need to know how many identifiers are on the current data declaration
64 : line in case we're given the BIND(C) attribute with a NAME= specifier. */
65 : static int num_idents_on_line;
66 : /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
67 : can supply a name if the curr_binding_label is nil and NAME= was not. */
68 : static int has_name_equals = 0;
69 :
70 : /* Initializer of the previous enumerator. */
71 :
72 : static gfc_expr *last_initializer;
73 :
74 : /* History of all the enumerators is maintained, so that
75 : kind values of all the enumerators could be updated depending
76 : upon the maximum initialized value. */
77 :
78 : typedef struct enumerator_history
79 : {
80 : gfc_symbol *sym;
81 : gfc_expr *initializer;
82 : struct enumerator_history *next;
83 : }
84 : enumerator_history;
85 :
86 : /* Header of enum history chain. */
87 :
88 : static enumerator_history *enum_history = NULL;
89 :
90 : /* Pointer of enum history node containing largest initializer. */
91 :
92 : static enumerator_history *max_enum = NULL;
93 :
94 : /* gfc_new_block points to the symbol of a newly matched block. */
95 :
96 : gfc_symbol *gfc_new_block;
97 :
98 : bool gfc_matching_function;
99 :
100 : /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
101 : int directive_unroll = -1;
102 :
103 : /* Set upon parsing supported !GCC$ pragmas for use in the next loop. */
104 : bool directive_ivdep = false;
105 : bool directive_vector = false;
106 : bool directive_novector = false;
107 :
108 : /* Map of middle-end built-ins that should be vectorized. */
109 : hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
110 :
111 : /* If a kind expression of a component of a parameterized derived type is
112 : parameterized, temporarily store the expression here. */
113 : static gfc_expr *saved_kind_expr = NULL;
114 :
115 : /* Used to store the parameter list arising in a PDT declaration and
116 : in the typespec of a PDT variable or component. */
117 : static gfc_actual_arglist *decl_type_param_list;
118 : static gfc_actual_arglist *type_param_spec_list;
119 :
120 : /* Drop an unattached gfc_charlen node from the current namespace. This is
121 : used when declaration processing created a length node for a symbol that is
122 : rejected before the node is attached to any surviving symbol. */
123 : static void
124 1 : discard_pending_charlen (gfc_charlen *cl)
125 : {
126 1 : if (!cl || !gfc_current_ns || gfc_current_ns->cl_list != cl)
127 : return;
128 :
129 1 : gfc_current_ns->cl_list = cl->next;
130 1 : gfc_free_expr (cl->length);
131 1 : free (cl);
132 : }
133 :
134 : /* Drop the charlen nodes created while matching a declaration that is about
135 : to be rejected. Callers must clear any surviving owners before using this
136 : helper, so only the statement-local nodes remain on the namespace list. */
137 :
138 : static void
139 3 : discard_pending_charlens (gfc_charlen *saved_cl)
140 : {
141 3 : if (!gfc_current_ns)
142 : return;
143 :
144 14 : while (gfc_current_ns->cl_list != saved_cl)
145 : {
146 11 : gfc_charlen *cl = gfc_current_ns->cl_list;
147 :
148 11 : gcc_assert (cl);
149 11 : gfc_current_ns->cl_list = cl->next;
150 11 : gfc_free_expr (cl->length);
151 11 : free (cl);
152 : }
153 : }
154 :
155 : /********************* DATA statement subroutines *********************/
156 :
157 : static bool in_match_data = false;
158 :
159 : bool
160 8313 : gfc_in_match_data (void)
161 : {
162 8313 : return in_match_data;
163 : }
164 :
165 : static void
166 4840 : set_in_match_data (bool set_value)
167 : {
168 4840 : in_match_data = set_value;
169 2420 : }
170 :
171 : /* Free a gfc_data_variable structure and everything beneath it. */
172 :
173 : static void
174 5663 : free_variable (gfc_data_variable *p)
175 : {
176 5663 : gfc_data_variable *q;
177 :
178 8752 : for (; p; p = q)
179 : {
180 3089 : q = p->next;
181 3089 : gfc_free_expr (p->expr);
182 3089 : gfc_free_iterator (&p->iter, 0);
183 3089 : free_variable (p->list);
184 3089 : free (p);
185 : }
186 5663 : }
187 :
188 :
189 : /* Free a gfc_data_value structure and everything beneath it. */
190 :
191 : static void
192 2574 : free_value (gfc_data_value *p)
193 : {
194 2574 : gfc_data_value *q;
195 :
196 10886 : for (; p; p = q)
197 : {
198 8312 : q = p->next;
199 8312 : mpz_clear (p->repeat);
200 8312 : gfc_free_expr (p->expr);
201 8312 : free (p);
202 : }
203 2574 : }
204 :
205 :
206 : /* Free a list of gfc_data structures. */
207 :
208 : void
209 543447 : gfc_free_data (gfc_data *p)
210 : {
211 543447 : gfc_data *q;
212 :
213 546021 : for (; p; p = q)
214 : {
215 2574 : q = p->next;
216 2574 : free_variable (p->var);
217 2574 : free_value (p->value);
218 2574 : free (p);
219 : }
220 543447 : }
221 :
222 :
223 : /* Free all data in a namespace. */
224 :
225 : static void
226 41 : gfc_free_data_all (gfc_namespace *ns)
227 : {
228 41 : gfc_data *d;
229 :
230 47 : for (;ns->data;)
231 : {
232 6 : d = ns->data->next;
233 6 : free (ns->data);
234 6 : ns->data = d;
235 : }
236 41 : }
237 :
238 : /* Reject data parsed since the last restore point was marked. */
239 :
240 : void
241 9144837 : gfc_reject_data (gfc_namespace *ns)
242 : {
243 9144837 : gfc_data *d;
244 :
245 9144839 : while (ns->data && ns->data != ns->old_data)
246 : {
247 2 : d = ns->data->next;
248 2 : free (ns->data);
249 2 : ns->data = d;
250 : }
251 9144837 : }
252 :
253 : static match var_element (gfc_data_variable *);
254 :
255 : /* Match a list of variables terminated by an iterator and a right
256 : parenthesis. */
257 :
258 : static match
259 154 : var_list (gfc_data_variable *parent)
260 : {
261 154 : gfc_data_variable *tail, var;
262 154 : match m;
263 :
264 154 : m = var_element (&var);
265 154 : if (m == MATCH_ERROR)
266 : return MATCH_ERROR;
267 154 : if (m == MATCH_NO)
268 0 : goto syntax;
269 :
270 154 : tail = gfc_get_data_variable ();
271 154 : *tail = var;
272 :
273 154 : parent->list = tail;
274 :
275 156 : for (;;)
276 : {
277 155 : if (gfc_match_char (',') != MATCH_YES)
278 0 : goto syntax;
279 :
280 155 : m = gfc_match_iterator (&parent->iter, 1);
281 155 : if (m == MATCH_YES)
282 : break;
283 1 : if (m == MATCH_ERROR)
284 : return MATCH_ERROR;
285 :
286 1 : m = var_element (&var);
287 1 : if (m == MATCH_ERROR)
288 : return MATCH_ERROR;
289 1 : if (m == MATCH_NO)
290 0 : goto syntax;
291 :
292 1 : tail->next = gfc_get_data_variable ();
293 1 : tail = tail->next;
294 :
295 1 : *tail = var;
296 : }
297 :
298 154 : if (gfc_match_char (')') != MATCH_YES)
299 0 : goto syntax;
300 : return MATCH_YES;
301 :
302 0 : syntax:
303 0 : gfc_syntax_error (ST_DATA);
304 0 : return MATCH_ERROR;
305 : }
306 :
307 :
308 : /* Match a single element in a data variable list, which can be a
309 : variable-iterator list. */
310 :
311 : static match
312 3047 : var_element (gfc_data_variable *new_var)
313 : {
314 3047 : match m;
315 3047 : gfc_symbol *sym;
316 :
317 3047 : memset (new_var, 0, sizeof (gfc_data_variable));
318 :
319 3047 : if (gfc_match_char ('(') == MATCH_YES)
320 154 : return var_list (new_var);
321 :
322 2893 : m = gfc_match_variable (&new_var->expr, 0);
323 2893 : if (m != MATCH_YES)
324 : return m;
325 :
326 2889 : if (new_var->expr->expr_type == EXPR_CONSTANT
327 2 : && new_var->expr->symtree == NULL)
328 : {
329 2 : gfc_error ("Inquiry parameter cannot appear in a "
330 : "data-stmt-object-list at %C");
331 2 : return MATCH_ERROR;
332 : }
333 :
334 2887 : sym = new_var->expr->symtree->n.sym;
335 :
336 : /* Symbol should already have an associated type. */
337 2887 : if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
338 : return MATCH_ERROR;
339 :
340 2886 : if (!sym->attr.function && gfc_current_ns->parent
341 148 : && gfc_current_ns->parent == sym->ns)
342 : {
343 1 : gfc_error ("Host associated variable %qs may not be in the DATA "
344 : "statement at %C", sym->name);
345 1 : return MATCH_ERROR;
346 : }
347 :
348 2885 : if (gfc_current_state () != COMP_BLOCK_DATA
349 2732 : && sym->attr.in_common
350 2914 : && !gfc_notify_std (GFC_STD_GNU, "initialization of "
351 : "common block variable %qs in DATA statement at %C",
352 : sym->name))
353 : return MATCH_ERROR;
354 :
355 2883 : if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
356 : return MATCH_ERROR;
357 :
358 : return MATCH_YES;
359 : }
360 :
361 :
362 : /* Match the top-level list of data variables. */
363 :
364 : static match
365 2517 : top_var_list (gfc_data *d)
366 : {
367 2517 : gfc_data_variable var, *tail, *new_var;
368 2517 : match m;
369 :
370 2517 : tail = NULL;
371 :
372 2892 : for (;;)
373 : {
374 2892 : m = var_element (&var);
375 2892 : if (m == MATCH_NO)
376 0 : goto syntax;
377 2892 : if (m == MATCH_ERROR)
378 : return MATCH_ERROR;
379 :
380 2877 : new_var = gfc_get_data_variable ();
381 2877 : *new_var = var;
382 2877 : if (new_var->expr)
383 2751 : new_var->expr->where = gfc_current_locus;
384 :
385 2877 : if (tail == NULL)
386 2502 : d->var = new_var;
387 : else
388 375 : tail->next = new_var;
389 :
390 2877 : tail = new_var;
391 :
392 2877 : if (gfc_match_char ('/') == MATCH_YES)
393 : break;
394 378 : if (gfc_match_char (',') != MATCH_YES)
395 3 : goto syntax;
396 : }
397 :
398 : return MATCH_YES;
399 :
400 3 : syntax:
401 3 : gfc_syntax_error (ST_DATA);
402 3 : gfc_free_data_all (gfc_current_ns);
403 3 : return MATCH_ERROR;
404 : }
405 :
406 :
407 : static match
408 8713 : match_data_constant (gfc_expr **result)
409 : {
410 8713 : char name[GFC_MAX_SYMBOL_LEN + 1];
411 8713 : gfc_symbol *sym, *dt_sym = NULL;
412 8713 : gfc_expr *expr;
413 8713 : match m;
414 8713 : locus old_loc;
415 8713 : gfc_symtree *symtree;
416 :
417 8713 : m = gfc_match_literal_constant (&expr, 1);
418 8713 : if (m == MATCH_YES)
419 : {
420 8368 : *result = expr;
421 8368 : return MATCH_YES;
422 : }
423 :
424 345 : if (m == MATCH_ERROR)
425 : return MATCH_ERROR;
426 :
427 337 : m = gfc_match_null (result);
428 337 : if (m != MATCH_NO)
429 : return m;
430 :
431 329 : old_loc = gfc_current_locus;
432 :
433 : /* Should this be a structure component, try to match it
434 : before matching a name. */
435 329 : m = gfc_match_rvalue (result);
436 329 : if (m == MATCH_ERROR)
437 : return m;
438 :
439 329 : if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
440 : {
441 4 : if (!gfc_simplify_expr (*result, 0))
442 0 : m = MATCH_ERROR;
443 4 : return m;
444 : }
445 319 : else if (m == MATCH_YES)
446 : {
447 : /* If a parameter inquiry ends up here, symtree is NULL but **result
448 : contains the right constant expression. Check here. */
449 319 : if ((*result)->symtree == NULL
450 37 : && (*result)->expr_type == EXPR_CONSTANT
451 37 : && ((*result)->ts.type == BT_INTEGER
452 1 : || (*result)->ts.type == BT_REAL))
453 : return m;
454 :
455 : /* F2018:R845 data-stmt-constant is initial-data-target.
456 : A data-stmt-constant shall be ... initial-data-target if and
457 : only if the corresponding data-stmt-object has the POINTER
458 : attribute. ... If data-stmt-constant is initial-data-target
459 : the corresponding data statement object shall be
460 : data-pointer-initialization compatible (7.5.4.6) with the initial
461 : data target; the data statement object is initially associated
462 : with the target. */
463 283 : if ((*result)->symtree
464 282 : && (*result)->symtree->n.sym->attr.save
465 218 : && (*result)->symtree->n.sym->attr.target)
466 : return m;
467 250 : gfc_free_expr (*result);
468 : }
469 :
470 256 : gfc_current_locus = old_loc;
471 :
472 256 : m = gfc_match_name (name);
473 256 : if (m != MATCH_YES)
474 : return m;
475 :
476 250 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
477 : return MATCH_ERROR;
478 :
479 250 : sym = symtree->n.sym;
480 :
481 250 : if (sym && sym->attr.generic)
482 60 : dt_sym = gfc_find_dt_in_generic (sym);
483 :
484 60 : if (sym == NULL
485 250 : || (sym->attr.flavor != FL_PARAMETER
486 65 : && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
487 : {
488 5 : gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
489 : name);
490 5 : *result = NULL;
491 5 : return MATCH_ERROR;
492 : }
493 245 : else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
494 60 : return gfc_match_structure_constructor (dt_sym, symtree, result);
495 :
496 : /* Check to see if the value is an initialization array expression. */
497 185 : if (sym->value->expr_type == EXPR_ARRAY)
498 : {
499 67 : gfc_current_locus = old_loc;
500 :
501 67 : m = gfc_match_init_expr (result);
502 67 : if (m == MATCH_ERROR)
503 : return m;
504 :
505 66 : if (m == MATCH_YES)
506 : {
507 66 : if (!gfc_simplify_expr (*result, 0))
508 0 : m = MATCH_ERROR;
509 :
510 66 : if ((*result)->expr_type == EXPR_CONSTANT)
511 : return m;
512 : else
513 : {
514 2 : gfc_error ("Invalid initializer %s in Data statement at %C", name);
515 2 : return MATCH_ERROR;
516 : }
517 : }
518 : }
519 :
520 118 : *result = gfc_copy_expr (sym->value);
521 118 : return MATCH_YES;
522 : }
523 :
524 :
525 : /* Match a list of values in a DATA statement. The leading '/' has
526 : already been seen at this point. */
527 :
528 : static match
529 2560 : top_val_list (gfc_data *data)
530 : {
531 2560 : gfc_data_value *new_val, *tail;
532 2560 : gfc_expr *expr;
533 2560 : match m;
534 :
535 2560 : tail = NULL;
536 :
537 8349 : for (;;)
538 : {
539 8349 : m = match_data_constant (&expr);
540 8349 : if (m == MATCH_NO)
541 3 : goto syntax;
542 8346 : if (m == MATCH_ERROR)
543 : return MATCH_ERROR;
544 :
545 8324 : new_val = gfc_get_data_value ();
546 8324 : mpz_init (new_val->repeat);
547 :
548 8324 : if (tail == NULL)
549 2535 : data->value = new_val;
550 : else
551 5789 : tail->next = new_val;
552 :
553 8324 : tail = new_val;
554 :
555 8324 : if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
556 : {
557 8119 : tail->expr = expr;
558 8119 : mpz_set_ui (tail->repeat, 1);
559 : }
560 : else
561 : {
562 205 : mpz_set (tail->repeat, expr->value.integer);
563 205 : gfc_free_expr (expr);
564 :
565 205 : m = match_data_constant (&tail->expr);
566 205 : if (m == MATCH_NO)
567 0 : goto syntax;
568 205 : if (m == MATCH_ERROR)
569 : return MATCH_ERROR;
570 : }
571 :
572 8320 : if (gfc_match_char ('/') == MATCH_YES)
573 : break;
574 5790 : if (gfc_match_char (',') == MATCH_NO)
575 1 : goto syntax;
576 : }
577 :
578 : return MATCH_YES;
579 :
580 4 : syntax:
581 4 : gfc_syntax_error (ST_DATA);
582 4 : gfc_free_data_all (gfc_current_ns);
583 4 : return MATCH_ERROR;
584 : }
585 :
586 :
587 : /* Matches an old style initialization. */
588 :
589 : static match
590 70 : match_old_style_init (const char *name)
591 : {
592 70 : match m;
593 70 : gfc_symtree *st;
594 70 : gfc_symbol *sym;
595 70 : gfc_data *newdata, *nd;
596 :
597 : /* Set up data structure to hold initializers. */
598 70 : gfc_find_sym_tree (name, NULL, 0, &st);
599 70 : sym = st->n.sym;
600 :
601 70 : newdata = gfc_get_data ();
602 70 : newdata->var = gfc_get_data_variable ();
603 70 : newdata->var->expr = gfc_get_variable_expr (st);
604 70 : newdata->var->expr->where = sym->declared_at;
605 70 : newdata->where = gfc_current_locus;
606 :
607 : /* Match initial value list. This also eats the terminal '/'. */
608 70 : m = top_val_list (newdata);
609 70 : if (m != MATCH_YES)
610 : {
611 1 : free (newdata);
612 1 : return m;
613 : }
614 :
615 : /* Check that a BOZ did not creep into an old-style initialization. */
616 137 : for (nd = newdata; nd; nd = nd->next)
617 : {
618 69 : if (nd->value->expr->ts.type == BT_BOZ
619 69 : && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
620 : "initialization"), &nd->value->expr->where))
621 : return MATCH_ERROR;
622 :
623 68 : if (nd->var->expr->ts.type != BT_INTEGER
624 27 : && nd->var->expr->ts.type != BT_REAL
625 21 : && nd->value->expr->ts.type == BT_BOZ)
626 : {
627 0 : gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
628 : "a %qs variable in an old-style initialization"),
629 0 : &nd->value->expr->where,
630 : gfc_typename (&nd->value->expr->ts));
631 0 : return MATCH_ERROR;
632 : }
633 : }
634 :
635 68 : if (gfc_pure (NULL))
636 : {
637 1 : gfc_error ("Initialization at %C is not allowed in a PURE procedure");
638 1 : free (newdata);
639 1 : return MATCH_ERROR;
640 : }
641 67 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
642 :
643 : /* Mark the variable as having appeared in a data statement. */
644 67 : if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
645 : {
646 2 : free (newdata);
647 2 : return MATCH_ERROR;
648 : }
649 :
650 : /* Chain in namespace list of DATA initializers. */
651 65 : newdata->next = gfc_current_ns->data;
652 65 : gfc_current_ns->data = newdata;
653 :
654 65 : return m;
655 : }
656 :
657 :
658 : /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
659 : we are matching a DATA statement and are therefore issuing an error
660 : if we encounter something unexpected, if not, we're trying to match
661 : an old-style initialization expression of the form INTEGER I /2/. */
662 :
663 : match
664 2422 : gfc_match_data (void)
665 : {
666 2422 : gfc_data *new_data;
667 2422 : gfc_expr *e;
668 2422 : gfc_ref *ref;
669 2422 : match m;
670 2422 : char c;
671 :
672 : /* DATA has been matched. In free form source code, the next character
673 : needs to be whitespace or '(' from an implied do-loop. Check that
674 : here. */
675 2422 : c = gfc_peek_ascii_char ();
676 2422 : if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
677 : return MATCH_NO;
678 :
679 : /* Before parsing the rest of a DATA statement, check F2008:c1206. */
680 2421 : if ((gfc_current_state () == COMP_FUNCTION
681 2421 : || gfc_current_state () == COMP_SUBROUTINE)
682 1153 : && gfc_state_stack->previous->state == COMP_INTERFACE)
683 : {
684 1 : gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
685 1 : return MATCH_ERROR;
686 : }
687 :
688 2420 : set_in_match_data (true);
689 :
690 2614 : for (;;)
691 : {
692 2517 : new_data = gfc_get_data ();
693 2517 : new_data->where = gfc_current_locus;
694 :
695 2517 : m = top_var_list (new_data);
696 2517 : if (m != MATCH_YES)
697 18 : goto cleanup;
698 :
699 2499 : if (new_data->var->iter.var
700 117 : && new_data->var->iter.var->ts.type == BT_INTEGER
701 74 : && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
702 68 : && new_data->var->list
703 68 : && new_data->var->list->expr
704 55 : && new_data->var->list->expr->ts.type == BT_CHARACTER
705 3 : && new_data->var->list->expr->ref
706 3 : && new_data->var->list->expr->ref->type == REF_SUBSTRING)
707 : {
708 1 : gfc_error ("Invalid substring in data-implied-do at %L in DATA "
709 : "statement", &new_data->var->list->expr->where);
710 1 : goto cleanup;
711 : }
712 :
713 : /* Check for an entity with an allocatable component, which is not
714 : allowed. */
715 2498 : e = new_data->var->expr;
716 2498 : if (e)
717 : {
718 2382 : bool invalid;
719 :
720 2382 : invalid = false;
721 3606 : for (ref = e->ref; ref; ref = ref->next)
722 1224 : if ((ref->type == REF_COMPONENT
723 140 : && ref->u.c.component->attr.allocatable)
724 1222 : || (ref->type == REF_ARRAY
725 1034 : && e->symtree->n.sym->attr.pointer != 1
726 1031 : && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
727 1224 : invalid = true;
728 :
729 2382 : if (invalid)
730 : {
731 2 : gfc_error ("Allocatable component or deferred-shaped array "
732 : "near %C in DATA statement");
733 2 : goto cleanup;
734 : }
735 :
736 : /* F2008:C567 (R536) A data-i-do-object or a variable that appears
737 : as a data-stmt-object shall not be an object designator in which
738 : a pointer appears other than as the entire rightmost part-ref. */
739 2380 : if (!e->ref && e->ts.type == BT_DERIVED
740 43 : && e->symtree->n.sym->attr.pointer)
741 4 : goto partref;
742 :
743 2376 : ref = e->ref;
744 2376 : if (e->symtree->n.sym->ts.type == BT_DERIVED
745 125 : && e->symtree->n.sym->attr.pointer
746 1 : && ref->type == REF_COMPONENT)
747 1 : goto partref;
748 :
749 3591 : for (; ref; ref = ref->next)
750 1217 : if (ref->type == REF_COMPONENT
751 135 : && ref->u.c.component->attr.pointer
752 27 : && ref->next)
753 1 : goto partref;
754 : }
755 :
756 2490 : m = top_val_list (new_data);
757 2490 : if (m != MATCH_YES)
758 29 : goto cleanup;
759 :
760 2461 : new_data->next = gfc_current_ns->data;
761 2461 : gfc_current_ns->data = new_data;
762 :
763 : /* A BOZ literal constant cannot appear in a structure constructor.
764 : Check for that here for a data statement value. */
765 2461 : if (new_data->value->expr->ts.type == BT_DERIVED
766 37 : && new_data->value->expr->value.constructor)
767 : {
768 35 : gfc_constructor *c;
769 35 : c = gfc_constructor_first (new_data->value->expr->value.constructor);
770 106 : for (; c; c = gfc_constructor_next (c))
771 36 : if (c->expr && c->expr->ts.type == BT_BOZ)
772 : {
773 0 : gfc_error ("BOZ literal constant at %L cannot appear in a "
774 : "structure constructor", &c->expr->where);
775 0 : return MATCH_ERROR;
776 : }
777 : }
778 :
779 2461 : if (gfc_match_eos () == MATCH_YES)
780 : break;
781 :
782 97 : gfc_match_char (','); /* Optional comma */
783 97 : }
784 :
785 2364 : set_in_match_data (false);
786 :
787 2364 : if (gfc_pure (NULL))
788 : {
789 0 : gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
790 0 : return MATCH_ERROR;
791 : }
792 2364 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
793 :
794 2364 : return MATCH_YES;
795 :
796 6 : partref:
797 :
798 6 : gfc_error ("part-ref with pointer attribute near %L is not "
799 : "rightmost part-ref of data-stmt-object",
800 : &e->where);
801 :
802 56 : cleanup:
803 56 : set_in_match_data (false);
804 56 : gfc_free_data (new_data);
805 56 : return MATCH_ERROR;
806 : }
807 :
808 :
809 : /************************ Declaration statements *********************/
810 :
811 :
812 : /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
813 : list). The difference here is the expression is a list of constants
814 : and is surrounded by '/'.
815 : The typespec ts must match the typespec of the variable which the
816 : clist is initializing.
817 : The arrayspec tells whether this should match a list of constants
818 : corresponding to array elements or a scalar (as == NULL). */
819 :
820 : static match
821 74 : match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
822 : {
823 74 : gfc_constructor_base array_head = NULL;
824 74 : gfc_expr *expr = NULL;
825 74 : match m = MATCH_ERROR;
826 74 : locus where;
827 74 : mpz_t repeat, cons_size, as_size;
828 74 : bool scalar;
829 74 : int cmp;
830 :
831 74 : gcc_assert (ts);
832 :
833 : /* We have already matched '/' - now look for a constant list, as with
834 : top_val_list from decl.cc, but append the result to an array. */
835 74 : if (gfc_match ("/") == MATCH_YES)
836 : {
837 1 : gfc_error ("Empty old style initializer list at %C");
838 1 : return MATCH_ERROR;
839 : }
840 :
841 73 : where = gfc_current_locus;
842 73 : scalar = !as || !as->rank;
843 :
844 42 : if (!scalar && !spec_size (as, &as_size))
845 : {
846 2 : gfc_error ("Array in initializer list at %L must have an explicit shape",
847 1 : as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
848 : /* Nothing to cleanup yet. */
849 1 : return MATCH_ERROR;
850 : }
851 :
852 72 : mpz_init_set_ui (repeat, 0);
853 :
854 143 : for (;;)
855 : {
856 143 : m = match_data_constant (&expr);
857 143 : if (m != MATCH_YES)
858 3 : expr = NULL; /* match_data_constant may set expr to garbage */
859 3 : if (m == MATCH_NO)
860 2 : goto syntax;
861 141 : if (m == MATCH_ERROR)
862 1 : goto cleanup;
863 :
864 : /* Found r in repeat spec r*c; look for the constant to repeat. */
865 140 : if ( gfc_match_char ('*') == MATCH_YES)
866 : {
867 18 : if (scalar)
868 : {
869 1 : gfc_error ("Repeat spec invalid in scalar initializer at %C");
870 1 : goto cleanup;
871 : }
872 17 : if (expr->ts.type != BT_INTEGER)
873 : {
874 1 : gfc_error ("Repeat spec must be an integer at %C");
875 1 : goto cleanup;
876 : }
877 16 : mpz_set (repeat, expr->value.integer);
878 16 : gfc_free_expr (expr);
879 16 : expr = NULL;
880 :
881 16 : m = match_data_constant (&expr);
882 16 : if (m == MATCH_NO)
883 : {
884 1 : m = MATCH_ERROR;
885 1 : gfc_error ("Expected data constant after repeat spec at %C");
886 : }
887 16 : if (m != MATCH_YES)
888 1 : goto cleanup;
889 : }
890 : /* No repeat spec, we matched the data constant itself. */
891 : else
892 122 : mpz_set_ui (repeat, 1);
893 :
894 137 : if (!scalar)
895 : {
896 : /* Add the constant initializer as many times as repeated. */
897 251 : for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
898 : {
899 : /* Make sure types of elements match */
900 144 : if(ts && !gfc_compare_types (&expr->ts, ts)
901 12 : && !gfc_convert_type (expr, ts, 1))
902 0 : goto cleanup;
903 :
904 144 : gfc_constructor_append_expr (&array_head,
905 : gfc_copy_expr (expr), &gfc_current_locus);
906 : }
907 :
908 107 : gfc_free_expr (expr);
909 107 : expr = NULL;
910 : }
911 :
912 : /* For scalar initializers quit after one element. */
913 : else
914 : {
915 30 : if(gfc_match_char ('/') != MATCH_YES)
916 : {
917 1 : gfc_error ("End of scalar initializer expected at %C");
918 1 : goto cleanup;
919 : }
920 : break;
921 : }
922 :
923 107 : if (gfc_match_char ('/') == MATCH_YES)
924 : break;
925 72 : if (gfc_match_char (',') == MATCH_NO)
926 1 : goto syntax;
927 : }
928 :
929 : /* If we break early from here out, we encountered an error. */
930 64 : m = MATCH_ERROR;
931 :
932 : /* Set up expr as an array constructor. */
933 64 : if (!scalar)
934 : {
935 35 : expr = gfc_get_array_expr (ts->type, ts->kind, &where);
936 35 : expr->ts = *ts;
937 35 : expr->value.constructor = array_head;
938 :
939 : /* Validate sizes. We built expr ourselves, so cons_size will be
940 : constant (we fail above for non-constant expressions).
941 : We still need to verify that the sizes match. */
942 35 : gcc_assert (gfc_array_size (expr, &cons_size));
943 35 : cmp = mpz_cmp (cons_size, as_size);
944 35 : if (cmp < 0)
945 2 : gfc_error ("Not enough elements in array initializer at %C");
946 33 : else if (cmp > 0)
947 3 : gfc_error ("Too many elements in array initializer at %C");
948 35 : mpz_clear (cons_size);
949 35 : if (cmp)
950 5 : goto cleanup;
951 :
952 : /* Set the rank/shape to match the LHS as auto-reshape is implied. */
953 30 : expr->rank = as->rank;
954 30 : expr->corank = as->corank;
955 30 : expr->shape = gfc_get_shape (as->rank);
956 66 : for (int i = 0; i < as->rank; ++i)
957 36 : spec_dimen_size (as, i, &expr->shape[i]);
958 : }
959 :
960 : /* Make sure scalar types match. */
961 29 : else if (!gfc_compare_types (&expr->ts, ts)
962 29 : && !gfc_convert_type (expr, ts, 1))
963 2 : goto cleanup;
964 :
965 57 : if (expr->ts.u.cl)
966 1 : expr->ts.u.cl->length_from_typespec = 1;
967 :
968 57 : *result = expr;
969 57 : m = MATCH_YES;
970 57 : goto done;
971 :
972 3 : syntax:
973 3 : m = MATCH_ERROR;
974 3 : gfc_error ("Syntax error in old style initializer list at %C");
975 :
976 15 : cleanup:
977 15 : if (expr)
978 10 : expr->value.constructor = NULL;
979 15 : gfc_free_expr (expr);
980 15 : gfc_constructor_free (array_head);
981 :
982 72 : done:
983 72 : mpz_clear (repeat);
984 72 : if (!scalar)
985 41 : mpz_clear (as_size);
986 : return m;
987 : }
988 :
989 :
990 : /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
991 :
992 : static bool
993 114 : merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
994 : {
995 114 : if ((from->type == AS_ASSUMED_RANK && to->corank)
996 112 : || (to->type == AS_ASSUMED_RANK && from->corank))
997 : {
998 5 : gfc_error ("The assumed-rank array at %C shall not have a codimension");
999 5 : return false;
1000 : }
1001 :
1002 109 : if (to->rank == 0 && from->rank > 0)
1003 : {
1004 48 : to->rank = from->rank;
1005 48 : to->type = from->type;
1006 48 : to->cray_pointee = from->cray_pointee;
1007 48 : to->cp_was_assumed = from->cp_was_assumed;
1008 :
1009 152 : for (int i = to->corank - 1; i >= 0; i--)
1010 : {
1011 : /* Do not exceed the limits on lower[] and upper[]. gfortran
1012 : cleans up elsewhere. */
1013 104 : int j = from->rank + i;
1014 104 : if (j >= GFC_MAX_DIMENSIONS)
1015 : break;
1016 :
1017 104 : to->lower[j] = to->lower[i];
1018 104 : to->upper[j] = to->upper[i];
1019 : }
1020 115 : for (int i = 0; i < from->rank; i++)
1021 : {
1022 67 : if (copy)
1023 : {
1024 43 : to->lower[i] = gfc_copy_expr (from->lower[i]);
1025 43 : to->upper[i] = gfc_copy_expr (from->upper[i]);
1026 : }
1027 : else
1028 : {
1029 24 : to->lower[i] = from->lower[i];
1030 24 : to->upper[i] = from->upper[i];
1031 : }
1032 : }
1033 : }
1034 61 : else if (to->corank == 0 && from->corank > 0)
1035 : {
1036 34 : to->corank = from->corank;
1037 34 : to->cotype = from->cotype;
1038 :
1039 104 : for (int i = 0; i < from->corank; i++)
1040 : {
1041 : /* Do not exceed the limits on lower[] and upper[]. gfortran
1042 : cleans up elsewhere. */
1043 71 : int k = from->rank + i;
1044 71 : int j = to->rank + i;
1045 71 : if (j >= GFC_MAX_DIMENSIONS)
1046 : break;
1047 :
1048 70 : if (copy)
1049 : {
1050 37 : to->lower[j] = gfc_copy_expr (from->lower[k]);
1051 37 : to->upper[j] = gfc_copy_expr (from->upper[k]);
1052 : }
1053 : else
1054 : {
1055 33 : to->lower[j] = from->lower[k];
1056 33 : to->upper[j] = from->upper[k];
1057 : }
1058 : }
1059 : }
1060 :
1061 109 : if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
1062 : {
1063 1 : gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1064 : "allowed dimensions of %d",
1065 : to->rank, to->corank, GFC_MAX_DIMENSIONS);
1066 1 : to->corank = GFC_MAX_DIMENSIONS - to->rank;
1067 1 : return false;
1068 : }
1069 : return true;
1070 : }
1071 :
1072 :
1073 : /* Match an intent specification. Since this can only happen after an
1074 : INTENT word, a legal intent-spec must follow. */
1075 :
1076 : static sym_intent
1077 28169 : match_intent_spec (void)
1078 : {
1079 :
1080 28169 : if (gfc_match (" ( in out )") == MATCH_YES)
1081 : return INTENT_INOUT;
1082 24977 : if (gfc_match (" ( in )") == MATCH_YES)
1083 : return INTENT_IN;
1084 3694 : if (gfc_match (" ( out )") == MATCH_YES)
1085 : return INTENT_OUT;
1086 :
1087 2 : gfc_error ("Bad INTENT specification at %C");
1088 2 : return INTENT_UNKNOWN;
1089 : }
1090 :
1091 :
1092 : /* Matches a character length specification, which is either a
1093 : specification expression, '*', or ':'. */
1094 :
1095 : static match
1096 27855 : char_len_param_value (gfc_expr **expr, bool *deferred)
1097 : {
1098 27855 : match m;
1099 27855 : gfc_expr *p;
1100 :
1101 27855 : *expr = NULL;
1102 27855 : *deferred = false;
1103 :
1104 27855 : if (gfc_match_char ('*') == MATCH_YES)
1105 : return MATCH_YES;
1106 :
1107 21326 : if (gfc_match_char (':') == MATCH_YES)
1108 : {
1109 3372 : if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1110 : return MATCH_ERROR;
1111 :
1112 3370 : *deferred = true;
1113 :
1114 3370 : return MATCH_YES;
1115 : }
1116 :
1117 17954 : m = gfc_match_expr (expr);
1118 :
1119 17954 : if (m == MATCH_NO || m == MATCH_ERROR)
1120 : return m;
1121 :
1122 17949 : if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1123 : return MATCH_ERROR;
1124 :
1125 : /* Try to simplify the expression to catch things like CHARACTER(([1])). */
1126 17943 : p = gfc_copy_expr (*expr);
1127 17943 : if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
1128 14903 : gfc_replace_expr (*expr, p);
1129 : else
1130 3040 : gfc_free_expr (p);
1131 :
1132 17943 : if ((*expr)->expr_type == EXPR_FUNCTION)
1133 : {
1134 1021 : if ((*expr)->ts.type == BT_INTEGER
1135 1020 : || ((*expr)->ts.type == BT_UNKNOWN
1136 1020 : && strcmp((*expr)->symtree->name, "null") != 0))
1137 : return MATCH_YES;
1138 :
1139 2 : goto syntax;
1140 : }
1141 16922 : else if ((*expr)->expr_type == EXPR_CONSTANT)
1142 : {
1143 : /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1144 : processor dependent and its value is greater than or equal to zero.
1145 : F2008, 4.4.3.2: If the character length parameter value evaluates
1146 : to a negative value, the length of character entities declared
1147 : is zero. */
1148 :
1149 14831 : if ((*expr)->ts.type == BT_INTEGER)
1150 : {
1151 14813 : if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1152 4 : mpz_set_si ((*expr)->value.integer, 0);
1153 : }
1154 : else
1155 18 : goto syntax;
1156 : }
1157 2091 : else if ((*expr)->expr_type == EXPR_ARRAY)
1158 8 : goto syntax;
1159 2083 : else if ((*expr)->expr_type == EXPR_VARIABLE)
1160 : {
1161 1514 : bool t;
1162 1514 : gfc_expr *e;
1163 :
1164 1514 : e = gfc_copy_expr (*expr);
1165 :
1166 : /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1167 : which causes an ICE if gfc_reduce_init_expr() is called. */
1168 1514 : if (e->ref && e->ref->type == REF_ARRAY
1169 8 : && e->ref->u.ar.type == AR_UNKNOWN
1170 7 : && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1171 2 : goto syntax;
1172 :
1173 1512 : t = gfc_reduce_init_expr (e);
1174 :
1175 1512 : if (!t && e->ts.type == BT_UNKNOWN
1176 7 : && e->symtree->n.sym->attr.untyped == 1
1177 7 : && (flag_implicit_none
1178 5 : || e->symtree->n.sym->ns->seen_implicit_none == 1
1179 1 : || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1180 : {
1181 7 : gfc_free_expr (e);
1182 7 : goto syntax;
1183 : }
1184 :
1185 1505 : if ((e->ref && e->ref->type == REF_ARRAY
1186 4 : && e->ref->u.ar.type != AR_ELEMENT)
1187 1504 : || (!e->ref && e->expr_type == EXPR_ARRAY))
1188 : {
1189 2 : gfc_free_expr (e);
1190 2 : goto syntax;
1191 : }
1192 :
1193 1503 : gfc_free_expr (e);
1194 : }
1195 :
1196 16885 : if (gfc_seen_div0)
1197 52 : m = MATCH_ERROR;
1198 :
1199 : return m;
1200 :
1201 39 : syntax:
1202 39 : gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1203 39 : return MATCH_ERROR;
1204 : }
1205 :
1206 :
1207 : /* A character length is a '*' followed by a literal integer or a
1208 : char_len_param_value in parenthesis. */
1209 :
1210 : static match
1211 63083 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1212 : {
1213 63083 : int length;
1214 63083 : match m;
1215 :
1216 63083 : *deferred = false;
1217 63083 : m = gfc_match_char ('*');
1218 63083 : if (m != MATCH_YES)
1219 : return m;
1220 :
1221 2641 : m = gfc_match_small_literal_int (&length, NULL);
1222 2641 : if (m == MATCH_ERROR)
1223 : return m;
1224 :
1225 2641 : if (m == MATCH_YES)
1226 : {
1227 2137 : if (obsolescent_check
1228 2137 : && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1229 : return MATCH_ERROR;
1230 2137 : *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1231 2137 : return m;
1232 : }
1233 :
1234 504 : if (gfc_match_char ('(') == MATCH_NO)
1235 0 : goto syntax;
1236 :
1237 504 : m = char_len_param_value (expr, deferred);
1238 504 : if (m != MATCH_YES && gfc_matching_function)
1239 : {
1240 0 : gfc_undo_symbols ();
1241 0 : m = MATCH_YES;
1242 : }
1243 :
1244 1 : if (m == MATCH_ERROR)
1245 : return m;
1246 503 : if (m == MATCH_NO)
1247 0 : goto syntax;
1248 :
1249 503 : if (gfc_match_char (')') == MATCH_NO)
1250 : {
1251 0 : gfc_free_expr (*expr);
1252 0 : *expr = NULL;
1253 0 : goto syntax;
1254 : }
1255 :
1256 503 : if (obsolescent_check
1257 503 : && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1258 : return MATCH_ERROR;
1259 :
1260 : return MATCH_YES;
1261 :
1262 0 : syntax:
1263 0 : gfc_error ("Syntax error in character length specification at %C");
1264 0 : return MATCH_ERROR;
1265 : }
1266 :
1267 :
1268 : /* Special subroutine for finding a symbol. Check if the name is found
1269 : in the current name space. If not, and we're compiling a function or
1270 : subroutine and the parent compilation unit is an interface, then check
1271 : to see if the name we've been given is the name of the interface
1272 : (located in another namespace). */
1273 :
1274 : static int
1275 285709 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1276 : {
1277 285709 : gfc_state_data *s;
1278 285709 : gfc_symtree *st;
1279 285709 : int i;
1280 :
1281 285709 : i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1282 285709 : if (i == 0)
1283 : {
1284 285709 : *result = st ? st->n.sym : NULL;
1285 285709 : goto end;
1286 : }
1287 :
1288 0 : if (gfc_current_state () != COMP_SUBROUTINE
1289 0 : && gfc_current_state () != COMP_FUNCTION)
1290 0 : goto end;
1291 :
1292 0 : s = gfc_state_stack->previous;
1293 0 : if (s == NULL)
1294 0 : goto end;
1295 :
1296 0 : if (s->state != COMP_INTERFACE)
1297 0 : goto end;
1298 0 : if (s->sym == NULL)
1299 0 : goto end; /* Nameless interface. */
1300 :
1301 0 : if (strcmp (name, s->sym->name) == 0)
1302 : {
1303 0 : *result = s->sym;
1304 0 : return 0;
1305 : }
1306 :
1307 0 : end:
1308 : return i;
1309 : }
1310 :
1311 :
1312 : /* Special subroutine for getting a symbol node associated with a
1313 : procedure name, used in SUBROUTINE and FUNCTION statements. The
1314 : symbol is created in the parent using with symtree node in the
1315 : child unit pointing to the symbol. If the current namespace has no
1316 : parent, then the symbol is just created in the current unit. */
1317 :
1318 : static int
1319 64539 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1320 : {
1321 64539 : gfc_symtree *st;
1322 64539 : gfc_symbol *sym;
1323 64539 : int rc = 0;
1324 :
1325 : /* Module functions have to be left in their own namespace because
1326 : they have potentially (almost certainly!) already been referenced.
1327 : In this sense, they are rather like external functions. This is
1328 : fixed up in resolve.cc(resolve_entries), where the symbol name-
1329 : space is set to point to the master function, so that the fake
1330 : result mechanism can work. */
1331 64539 : if (module_fcn_entry)
1332 : {
1333 : /* Present if entry is declared to be a module procedure. */
1334 260 : rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1335 :
1336 260 : if (*result == NULL)
1337 217 : rc = gfc_get_symbol (name, NULL, result);
1338 86 : else if (!gfc_get_symbol (name, NULL, &sym) && sym
1339 43 : && (*result)->ts.type == BT_UNKNOWN
1340 86 : && sym->attr.flavor == FL_UNKNOWN)
1341 : /* Pick up the typespec for the entry, if declared in the function
1342 : body. Note that this symbol is FL_UNKNOWN because it will
1343 : only have appeared in a type declaration. The local symtree
1344 : is set to point to the module symbol and a unique symtree
1345 : to the local version. This latter ensures a correct clearing
1346 : of the symbols. */
1347 : {
1348 : /* If the ENTRY proceeds its specification, we need to ensure
1349 : that this does not raise a "has no IMPLICIT type" error. */
1350 43 : if (sym->ts.type == BT_UNKNOWN)
1351 23 : sym->attr.untyped = 1;
1352 :
1353 43 : (*result)->ts = sym->ts;
1354 :
1355 : /* Put the symbol in the procedure namespace so that, should
1356 : the ENTRY precede its specification, the specification
1357 : can be applied. */
1358 43 : (*result)->ns = gfc_current_ns;
1359 :
1360 43 : gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1361 43 : st->n.sym = *result;
1362 43 : st = gfc_get_unique_symtree (gfc_current_ns);
1363 43 : sym->refs++;
1364 43 : st->n.sym = sym;
1365 : }
1366 : }
1367 : else
1368 64279 : rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1369 :
1370 64539 : if (rc)
1371 : return rc;
1372 :
1373 64538 : sym = *result;
1374 64538 : if (sym->attr.proc == PROC_ST_FUNCTION)
1375 : return rc;
1376 :
1377 64537 : if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1378 : {
1379 : /* Create a partially populated interface symbol to carry the
1380 : characteristics of the procedure and the result. */
1381 472 : sym->tlink = gfc_new_symbol (name, sym->ns);
1382 472 : gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1383 472 : gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1384 472 : if (sym->attr.dimension)
1385 17 : sym->tlink->as = gfc_copy_array_spec (sym->as);
1386 :
1387 : /* Ideally, at this point, a copy would be made of the formal
1388 : arguments and their namespace. However, this does not appear
1389 : to be necessary, albeit at the expense of not being able to
1390 : use gfc_compare_interfaces directly. */
1391 :
1392 472 : if (sym->result && sym->result != sym)
1393 : {
1394 105 : sym->tlink->result = sym->result;
1395 105 : sym->result = NULL;
1396 : }
1397 367 : else if (sym->result)
1398 : {
1399 93 : sym->tlink->result = sym->tlink;
1400 : }
1401 : }
1402 64065 : else if (sym && !sym->gfc_new
1403 24725 : && gfc_current_state () != COMP_INTERFACE)
1404 : {
1405 : /* Trap another encompassed procedure with the same name. All
1406 : these conditions are necessary to avoid picking up an entry
1407 : whose name clashes with that of the encompassing procedure;
1408 : this is handled using gsymbols to register unique, globally
1409 : accessible names. */
1410 23390 : if (sym->attr.flavor != 0
1411 21307 : && sym->attr.proc != 0
1412 2392 : && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1413 7 : && sym->attr.if_source != IFSRC_UNKNOWN)
1414 : {
1415 7 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1416 : name, &sym->declared_at);
1417 7 : return true;
1418 : }
1419 23383 : if (sym->attr.flavor != 0
1420 21300 : && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1421 : {
1422 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1423 : name, &sym->declared_at);
1424 1 : return true;
1425 : }
1426 :
1427 23382 : if (sym->attr.external && sym->attr.procedure
1428 2 : && gfc_current_state () == COMP_CONTAINS)
1429 : {
1430 1 : gfc_error_now ("Contained procedure %qs at %C clashes with "
1431 : "procedure defined at %L",
1432 : name, &sym->declared_at);
1433 1 : return true;
1434 : }
1435 :
1436 : /* Trap a procedure with a name the same as interface in the
1437 : encompassing scope. */
1438 23381 : if (sym->attr.generic != 0
1439 60 : && (sym->attr.subroutine || sym->attr.function)
1440 1 : && !sym->attr.mod_proc)
1441 : {
1442 1 : gfc_error_now ("Name %qs at %C is already defined"
1443 : " as a generic interface at %L",
1444 : name, &sym->declared_at);
1445 1 : return true;
1446 : }
1447 :
1448 : /* Trap declarations of attributes in encompassing scope. The
1449 : signature for this is that ts.kind is nonzero for no-CLASS
1450 : entity. For a CLASS entity, ts.kind is zero. */
1451 23380 : if ((sym->ts.kind != 0
1452 23007 : || sym->ts.type == BT_CLASS
1453 23006 : || sym->ts.type == BT_DERIVED)
1454 397 : && !sym->attr.implicit_type
1455 396 : && sym->attr.proc == 0
1456 378 : && gfc_current_ns->parent != NULL
1457 138 : && sym->attr.access == 0
1458 136 : && !module_fcn_entry)
1459 : {
1460 5 : gfc_error_now ("Procedure %qs at %C has an explicit interface "
1461 : "from a previous declaration", name);
1462 5 : return true;
1463 : }
1464 : }
1465 :
1466 : /* F2023: C1247 (R1526) MODULE shall appear only in the function-stmt or
1467 : subroutine-stmt of a module subprogram or of a nonabstract interface
1468 : body that is declared in the scoping unit of a module or submodule. */
1469 64522 : if (sym->attr.external
1470 92 : && (sym->attr.subroutine || sym->attr.function)
1471 91 : && sym->attr.if_source == IFSRC_IFBODY
1472 91 : && !current_attr.module_procedure
1473 3 : && sym->attr.proc == PROC_MODULE
1474 3 : && gfc_state_stack->state == COMP_CONTAINS)
1475 1 : gfc_error_now ("Procedure %qs defined in interface body at %L "
1476 : "clashes with internal procedure defined at %C",
1477 : name, &sym->declared_at);
1478 :
1479 : /* This is the converse requirement: The separate-module-subprogram for a
1480 : module procedure shall have the MODULE prefix or be declared a MODULE
1481 : PROCEDURE, otherwise it would be ambiguous. */
1482 64522 : if (sym->attr.module_procedure
1483 472 : && (sym->attr.subroutine || sym->attr.function)
1484 472 : && sym->attr.if_source == IFSRC_IFBODY
1485 472 : && !current_attr.module_procedure
1486 4 : && sym->attr.proc == PROC_MODULE
1487 4 : && gfc_state_stack->state == COMP_CONTAINS
1488 2 : && gfc_state_stack->previous
1489 2 : && gfc_state_stack->previous->state == COMP_SUBMODULE)
1490 1 : gfc_error_now ("Procedure %qs at %C requires the MODULE prefix because "
1491 : "it is a module procedure declared in module %qs",
1492 1 : name, sym->module ? sym->module : "");
1493 :
1494 64522 : if (sym && !sym->gfc_new
1495 25182 : && sym->attr.flavor != FL_UNKNOWN
1496 22699 : && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1497 244 : && gfc_state_stack->state == COMP_CONTAINS
1498 239 : && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1499 : {
1500 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1501 : name, &sym->declared_at);
1502 1 : return true;
1503 : }
1504 :
1505 64521 : if (gfc_current_ns->parent == NULL || *result == NULL)
1506 : return rc;
1507 :
1508 : /* Module function entries will already have a symtree in
1509 : the current namespace but will need one at module level. */
1510 52235 : if (module_fcn_entry)
1511 : {
1512 : /* Present if entry is declared to be a module procedure. */
1513 258 : rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1514 258 : if (st == NULL)
1515 217 : st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1516 : }
1517 : else
1518 51977 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1519 :
1520 52235 : st->n.sym = sym;
1521 52235 : sym->refs++;
1522 :
1523 : /* See if the procedure should be a module procedure. */
1524 :
1525 52235 : if (((sym->ns->proc_name != NULL
1526 52235 : && sym->ns->proc_name->attr.flavor == FL_MODULE
1527 21175 : && sym->attr.proc != PROC_MODULE)
1528 52235 : || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1529 70575 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1530 : rc = 2;
1531 :
1532 : return rc;
1533 : }
1534 :
1535 :
1536 : /* Verify that the given symbol representing a parameter is C
1537 : interoperable, by checking to see if it was marked as such after
1538 : its declaration. If the given symbol is not interoperable, a
1539 : warning is reported, thus removing the need to return the status to
1540 : the calling function. The standard does not require the user use
1541 : one of the iso_c_binding named constants to declare an
1542 : interoperable parameter, but we can't be sure if the param is C
1543 : interop or not if the user doesn't. For example, integer(4) may be
1544 : legal Fortran, but doesn't have meaning in C. It may interop with
1545 : a number of the C types, which causes a problem because the
1546 : compiler can't know which one. This code is almost certainly not
1547 : portable, and the user will get what they deserve if the C type
1548 : across platforms isn't always interoperable with integer(4). If
1549 : the user had used something like integer(c_int) or integer(c_long),
1550 : the compiler could have automatically handled the varying sizes
1551 : across platforms. */
1552 :
1553 : bool
1554 17316 : gfc_verify_c_interop_param (gfc_symbol *sym)
1555 : {
1556 17316 : int is_c_interop = 0;
1557 17316 : bool retval = true;
1558 :
1559 : /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
1560 : Don't repeat the checks here. */
1561 17316 : if (sym->attr.implicit_type)
1562 : return true;
1563 :
1564 : /* For subroutines or functions that are passed to a BIND(C) procedure,
1565 : they're interoperable if they're BIND(C) and their params are all
1566 : interoperable. */
1567 17316 : if (sym->attr.flavor == FL_PROCEDURE)
1568 : {
1569 4 : if (sym->attr.is_bind_c == 0)
1570 : {
1571 0 : gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1572 : "attribute to be C interoperable", sym->name,
1573 : &(sym->declared_at));
1574 0 : return false;
1575 : }
1576 : else
1577 : {
1578 4 : if (sym->attr.is_c_interop == 1)
1579 : /* We've already checked this procedure; don't check it again. */
1580 : return true;
1581 : else
1582 4 : return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1583 4 : sym->common_block);
1584 : }
1585 : }
1586 :
1587 : /* See if we've stored a reference to a procedure that owns sym. */
1588 17312 : if (sym->ns != NULL && sym->ns->proc_name != NULL)
1589 : {
1590 17312 : if (sym->ns->proc_name->attr.is_bind_c == 1)
1591 : {
1592 17273 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
1593 17273 : bool f2018_added = false;
1594 :
1595 17273 : is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1596 :
1597 : /* F2018:18.3.6 has the following text:
1598 : "(5) any dummy argument without the VALUE attribute corresponds to
1599 : a formal parameter of the prototype that is of a pointer type, and
1600 : either
1601 : • the dummy argument is interoperable with an entity of the
1602 : referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
1603 : the formal parameter (this is equivalent to the F2008 text),
1604 : • the dummy argument is a nonallocatable nonpointer variable of
1605 : type CHARACTER with assumed character length and the formal
1606 : parameter is a pointer to CFI_cdesc_t,
1607 : • the dummy argument is allocatable, assumed-shape, assumed-rank,
1608 : or a pointer without the CONTIGUOUS attribute, and the formal
1609 : parameter is a pointer to CFI_cdesc_t, or
1610 : • the dummy argument is assumed-type and not allocatable,
1611 : assumed-shape, assumed-rank, or a pointer, and the formal
1612 : parameter is a pointer to void," */
1613 3731 : if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
1614 : {
1615 2364 : bool as_ar = (sym->as
1616 2364 : && (sym->as->type == AS_ASSUMED_SHAPE
1617 2117 : || sym->as->type == AS_ASSUMED_RANK));
1618 4728 : bool cond1 = (sym->ts.type == BT_CHARACTER
1619 1565 : && !(sym->ts.u.cl && sym->ts.u.cl->length)
1620 905 : && !sym->attr.allocatable
1621 3251 : && !sym->attr.pointer);
1622 4728 : bool cond2 = (sym->attr.allocatable
1623 2267 : || as_ar
1624 3389 : || (IS_POINTER (sym) && !sym->attr.contiguous));
1625 4728 : bool cond3 = (sym->ts.type == BT_ASSUMED
1626 0 : && !sym->attr.allocatable
1627 0 : && !sym->attr.pointer
1628 2364 : && !as_ar);
1629 2364 : f2018_added = cond1 || cond2 || cond3;
1630 : }
1631 :
1632 17273 : if (is_c_interop != 1 && !f2018_added)
1633 : {
1634 : /* Make personalized messages to give better feedback. */
1635 1837 : if (sym->ts.type == BT_DERIVED)
1636 1 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1637 : "BIND(C) procedure %qs but is not C interoperable "
1638 : "because derived type %qs is not C interoperable",
1639 : sym->name, &(sym->declared_at),
1640 1 : sym->ns->proc_name->name,
1641 1 : sym->ts.u.derived->name);
1642 1836 : else if (sym->ts.type == BT_CLASS)
1643 6 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1644 : "BIND(C) procedure %qs but is not C interoperable "
1645 : "because it is polymorphic",
1646 : sym->name, &(sym->declared_at),
1647 6 : sym->ns->proc_name->name);
1648 1830 : else if (warn_c_binding_type)
1649 39 : gfc_warning (OPT_Wc_binding_type,
1650 : "Variable %qs at %L is a dummy argument of the "
1651 : "BIND(C) procedure %qs but may not be C "
1652 : "interoperable",
1653 : sym->name, &(sym->declared_at),
1654 39 : sym->ns->proc_name->name);
1655 : }
1656 :
1657 : /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted. */
1658 17273 : if (sym->attr.pointer && sym->attr.contiguous)
1659 2 : gfc_error ("Dummy argument %qs at %L may not be a pointer with "
1660 : "CONTIGUOUS attribute as procedure %qs is BIND(C)",
1661 2 : sym->name, &sym->declared_at, sym->ns->proc_name->name);
1662 :
1663 : /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
1664 : procedure that are default-initialized are not permitted. */
1665 16633 : if ((sym->attr.pointer || sym->attr.allocatable)
1666 1041 : && sym->ts.type == BT_DERIVED
1667 17651 : && gfc_has_default_initializer (sym->ts.u.derived))
1668 : {
1669 8 : gfc_error ("Default-initialized dummy argument %qs with %s "
1670 : "attribute at %L is not permitted in BIND(C) "
1671 : "procedure %qs", sym->name,
1672 4 : (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
1673 4 : &sym->declared_at, sym->ns->proc_name->name);
1674 4 : retval = false;
1675 : }
1676 :
1677 : /* Character strings are only C interoperable if they have a
1678 : length of 1. However, as an argument they are also interoperable
1679 : when passed as descriptor (which requires len=: or len=*). */
1680 17273 : if (sym->ts.type == BT_CHARACTER)
1681 : {
1682 2344 : gfc_charlen *cl = sym->ts.u.cl;
1683 :
1684 2344 : if (sym->attr.allocatable || sym->attr.pointer)
1685 : {
1686 : /* F2018, 18.3.6 (6). */
1687 195 : if (!sym->ts.deferred)
1688 : {
1689 64 : if (sym->attr.allocatable)
1690 32 : gfc_error ("Allocatable character dummy argument %qs "
1691 : "at %L must have deferred length as "
1692 : "procedure %qs is BIND(C)", sym->name,
1693 32 : &sym->declared_at, sym->ns->proc_name->name);
1694 : else
1695 32 : gfc_error ("Pointer character dummy argument %qs at %L "
1696 : "must have deferred length as procedure %qs "
1697 : "is BIND(C)", sym->name, &sym->declared_at,
1698 32 : sym->ns->proc_name->name);
1699 : retval = false;
1700 : }
1701 131 : else if (!gfc_notify_std (GFC_STD_F2018,
1702 : "Deferred-length character dummy "
1703 : "argument %qs at %L of procedure "
1704 : "%qs with BIND(C) attribute",
1705 : sym->name, &sym->declared_at,
1706 131 : sym->ns->proc_name->name))
1707 102 : retval = false;
1708 : }
1709 2149 : else if (sym->attr.value
1710 354 : && (!cl || !cl->length
1711 354 : || cl->length->expr_type != EXPR_CONSTANT
1712 354 : || mpz_cmp_si (cl->length->value.integer, 1) != 0))
1713 : {
1714 1 : gfc_error ("Character dummy argument %qs at %L must be "
1715 : "of length 1 as it has the VALUE attribute",
1716 : sym->name, &sym->declared_at);
1717 1 : retval = false;
1718 : }
1719 2148 : else if (!cl || !cl->length)
1720 : {
1721 : /* Assumed length; F2018, 18.3.6 (5)(2).
1722 : Uses the CFI array descriptor - also for scalars and
1723 : explicit-size/assumed-size arrays. */
1724 959 : if (!gfc_notify_std (GFC_STD_F2018,
1725 : "Assumed-length character dummy argument "
1726 : "%qs at %L of procedure %qs with BIND(C) "
1727 : "attribute", sym->name, &sym->declared_at,
1728 959 : sym->ns->proc_name->name))
1729 102 : retval = false;
1730 : }
1731 1189 : else if (cl->length->expr_type != EXPR_CONSTANT
1732 875 : || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1733 : {
1734 : /* F2018, 18.3.6, (5), item 4. */
1735 653 : if (!sym->attr.dimension
1736 645 : || sym->as->type == AS_ASSUMED_SIZE
1737 639 : || sym->as->type == AS_EXPLICIT)
1738 : {
1739 20 : gfc_error ("Character dummy argument %qs at %L must be "
1740 : "of constant length of one or assumed length, "
1741 : "unless it has assumed shape or assumed rank, "
1742 : "as procedure %qs has the BIND(C) attribute",
1743 : sym->name, &sym->declared_at,
1744 20 : sym->ns->proc_name->name);
1745 20 : retval = false;
1746 : }
1747 : /* else: valid only since F2018 - and an assumed-shape/rank
1748 : array; however, gfc_notify_std is already called when
1749 : those array types are used. Thus, silently accept F200x. */
1750 : }
1751 : }
1752 :
1753 : /* We have to make sure that any param to a bind(c) routine does
1754 : not have the allocatable, pointer, or optional attributes,
1755 : according to J3/04-007, section 5.1. */
1756 17273 : if (sym->attr.allocatable == 1
1757 17674 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1758 : "ALLOCATABLE attribute in procedure %qs "
1759 : "with BIND(C)", sym->name,
1760 : &(sym->declared_at),
1761 401 : sym->ns->proc_name->name))
1762 : retval = false;
1763 :
1764 17273 : if (sym->attr.pointer == 1
1765 17913 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1766 : "POINTER attribute in procedure %qs "
1767 : "with BIND(C)", sym->name,
1768 : &(sym->declared_at),
1769 640 : sym->ns->proc_name->name))
1770 : retval = false;
1771 :
1772 17273 : if (sym->attr.optional == 1 && sym->attr.value)
1773 : {
1774 9 : gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1775 : "and the VALUE attribute because procedure %qs "
1776 : "is BIND(C)", sym->name, &(sym->declared_at),
1777 9 : sym->ns->proc_name->name);
1778 9 : retval = false;
1779 : }
1780 17264 : else if (sym->attr.optional == 1
1781 18218 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1782 : "at %L with OPTIONAL attribute in "
1783 : "procedure %qs which is BIND(C)",
1784 : sym->name, &(sym->declared_at),
1785 954 : sym->ns->proc_name->name))
1786 : retval = false;
1787 :
1788 : /* Make sure that if it has the dimension attribute, that it is
1789 : either assumed size or explicit shape. Deferred shape is already
1790 : covered by the pointer/allocatable attribute. */
1791 5551 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1792 18606 : && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1793 : "at %L as dummy argument to the BIND(C) "
1794 : "procedure %qs at %L", sym->name,
1795 : &(sym->declared_at),
1796 : sym->ns->proc_name->name,
1797 1333 : &(sym->ns->proc_name->declared_at)))
1798 : retval = false;
1799 : }
1800 : }
1801 :
1802 : return retval;
1803 : }
1804 :
1805 :
1806 :
1807 : /* Function called by variable_decl() that adds a name to the symbol table. */
1808 :
1809 : static bool
1810 263934 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
1811 : gfc_array_spec **as, locus *var_locus)
1812 : {
1813 263934 : symbol_attribute attr;
1814 263934 : gfc_symbol *sym;
1815 263934 : int upper;
1816 263934 : gfc_symtree *st, *host_st = NULL;
1817 :
1818 : /* Symbols in a submodule are host associated from the parent module or
1819 : submodules. Therefore, they can be overridden by declarations in the
1820 : submodule scope. Deal with this by attaching the existing symbol to
1821 : a new symtree and recycling the old symtree with a new symbol... */
1822 263934 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1823 263934 : if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
1824 3 : && gfc_current_ns->parent)
1825 3 : host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
1826 :
1827 263934 : if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1828 12 : && st->n.sym != NULL
1829 12 : && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1830 : {
1831 12 : gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1832 12 : s->n.sym = st->n.sym;
1833 12 : sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
1834 :
1835 12 : st->n.sym = sym;
1836 12 : sym->refs++;
1837 12 : gfc_set_sym_referenced (sym);
1838 12 : }
1839 : /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
1840 : current scope are not violated by local redeclarations. Note that there is
1841 : no need to guard for std >= F2018 because import_only and IMPORT_ALL are
1842 : only set for these standards. */
1843 263922 : else if (host_st && host_st->n.sym
1844 2 : && host_st->n.sym != gfc_current_ns->proc_name
1845 2 : && !(st && st->n.sym
1846 1 : && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
1847 : {
1848 2 : gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
1849 : "statement and must not be re-declared", name, var_locus,
1850 1 : (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
1851 2 : return false;
1852 : }
1853 : /* ...Otherwise generate a new symtree and new symbol. */
1854 263920 : else if (gfc_get_symbol (name, NULL, &sym, var_locus))
1855 : return false;
1856 :
1857 : /* Check if the name has already been defined as a type. The
1858 : first letter of the symtree will be in upper case then. Of
1859 : course, this is only necessary if the upper case letter is
1860 : actually different. */
1861 :
1862 263932 : upper = TOUPPER(name[0]);
1863 263932 : if (upper != name[0])
1864 : {
1865 263182 : char u_name[GFC_MAX_SYMBOL_LEN + 1];
1866 263182 : gfc_symtree *st;
1867 :
1868 263182 : gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1869 263182 : strcpy (u_name, name);
1870 263182 : u_name[0] = upper;
1871 :
1872 263182 : st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1873 :
1874 : /* STRUCTURE types can alias symbol names */
1875 263182 : if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1876 : {
1877 1 : gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1878 : &st->n.sym->declared_at);
1879 1 : return false;
1880 : }
1881 : }
1882 :
1883 : /* Start updating the symbol table. Add basic type attribute if present. */
1884 263931 : if (current_ts.type != BT_UNKNOWN
1885 263931 : && (sym->attr.implicit_type == 0
1886 186 : || !gfc_compare_types (&sym->ts, ¤t_ts))
1887 527680 : && !gfc_add_type (sym, ¤t_ts, var_locus))
1888 : {
1889 : /* Duplicate-type rejection can leave a fresh CHARACTER length node on
1890 : the namespace list before it is attached to any surviving symbol.
1891 : Drop only that unattached node; shared constant charlen nodes are
1892 : already reachable from earlier declarations. PR82721. */
1893 27 : if (current_ts.type == BT_CHARACTER && cl && elem == 1)
1894 : {
1895 1 : discard_pending_charlen (cl);
1896 1 : gfc_clear_ts (¤t_ts);
1897 : }
1898 26 : else if (current_ts.type == BT_CHARACTER && cl && cl != current_ts.u.cl)
1899 0 : discard_pending_charlen (cl);
1900 27 : return false;
1901 : }
1902 :
1903 263904 : if (sym->ts.type == BT_CHARACTER)
1904 : {
1905 29084 : if (elem > 1)
1906 4154 : sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
1907 : else
1908 24930 : sym->ts.u.cl = cl;
1909 29084 : sym->ts.deferred = cl_deferred;
1910 : }
1911 :
1912 : /* Add dimension attribute if present. */
1913 263904 : if (!gfc_set_array_spec (sym, *as, var_locus))
1914 : return false;
1915 263902 : *as = NULL;
1916 :
1917 : /* Add attribute to symbol. The copy is so that we can reset the
1918 : dimension attribute. */
1919 263902 : attr = current_attr;
1920 263902 : attr.dimension = 0;
1921 263902 : attr.codimension = 0;
1922 :
1923 263902 : if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1924 : return false;
1925 :
1926 : /* Finish any work that may need to be done for the binding label,
1927 : if it's a bind(c). The bind(c) attr is found before the symbol
1928 : is made, and before the symbol name (for data decls), so the
1929 : current_ts is holding the binding label, or nothing if the
1930 : name= attr wasn't given. Therefore, test here if we're dealing
1931 : with a bind(c) and make sure the binding label is set correctly. */
1932 263888 : if (sym->attr.is_bind_c == 1)
1933 : {
1934 1787 : if (!sym->binding_label)
1935 : {
1936 : /* Set the binding label and verify that if a NAME= was specified
1937 : then only one identifier was in the entity-decl-list. */
1938 136 : if (!set_binding_label (&sym->binding_label, sym->name,
1939 : num_idents_on_line))
1940 : return false;
1941 : }
1942 : }
1943 :
1944 : /* See if we know we're in a common block, and if it's a bind(c)
1945 : common then we need to make sure we're an interoperable type. */
1946 263886 : if (sym->attr.in_common == 1)
1947 : {
1948 : /* Test the common block object. */
1949 614 : if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1950 6 : && sym->ts.is_c_interop != 1)
1951 : {
1952 0 : gfc_error_now ("Variable %qs in common block %qs at %C "
1953 : "must be declared with a C interoperable "
1954 : "kind since common block %qs is BIND(C)",
1955 : sym->name, sym->common_block->name,
1956 0 : sym->common_block->name);
1957 0 : gfc_clear_error ();
1958 : }
1959 : }
1960 :
1961 263886 : sym->attr.implied_index = 0;
1962 :
1963 : /* Use the parameter expressions for a parameterized derived type. */
1964 263886 : if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1965 37156 : && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1966 1122 : sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1967 :
1968 263886 : if (sym->ts.type == BT_CLASS)
1969 11134 : return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1970 :
1971 : return true;
1972 : }
1973 :
1974 :
1975 : /* Set character constant to the given length. The constant will be padded or
1976 : truncated. If we're inside an array constructor without a typespec, we
1977 : additionally check that all elements have the same length; check_len -1
1978 : means no checking. */
1979 :
1980 : void
1981 14485 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1982 : gfc_charlen_t check_len)
1983 : {
1984 14485 : gfc_char_t *s;
1985 14485 : gfc_charlen_t slen;
1986 :
1987 14485 : if (expr->ts.type != BT_CHARACTER)
1988 : return;
1989 :
1990 14483 : if (expr->expr_type != EXPR_CONSTANT)
1991 : {
1992 1 : gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1993 1 : return;
1994 : }
1995 :
1996 14482 : slen = expr->value.character.length;
1997 14482 : if (len != slen)
1998 : {
1999 2147 : s = gfc_get_wide_string (len + 1);
2000 2147 : memcpy (s, expr->value.character.string,
2001 2147 : MIN (len, slen) * sizeof (gfc_char_t));
2002 2147 : if (len > slen)
2003 1856 : gfc_wide_memset (&s[slen], ' ', len - slen);
2004 :
2005 2147 : if (warn_character_truncation && slen > len)
2006 1 : gfc_warning_now (OPT_Wcharacter_truncation,
2007 : "CHARACTER expression at %L is being truncated "
2008 : "(%ld/%ld)", &expr->where,
2009 : (long) slen, (long) len);
2010 :
2011 : /* Apply the standard by 'hand' otherwise it gets cleared for
2012 : initializers. */
2013 2147 : if (check_len != -1 && slen != check_len)
2014 : {
2015 3 : if (!(gfc_option.allow_std & GFC_STD_GNU))
2016 0 : gfc_error_now ("The CHARACTER elements of the array constructor "
2017 : "at %L must have the same length (%ld/%ld)",
2018 : &expr->where, (long) slen,
2019 : (long) check_len);
2020 : else
2021 3 : gfc_notify_std (GFC_STD_LEGACY,
2022 : "The CHARACTER elements of the array constructor "
2023 : "at %L must have the same length (%ld/%ld)",
2024 : &expr->where, (long) slen,
2025 : (long) check_len);
2026 : }
2027 :
2028 2147 : s[len] = '\0';
2029 2147 : free (expr->value.character.string);
2030 2147 : expr->value.character.string = s;
2031 2147 : expr->value.character.length = len;
2032 : /* If explicit representation was given, clear it
2033 : as it is no longer needed after padding. */
2034 2147 : if (expr->representation.length)
2035 : {
2036 45 : expr->representation.length = 0;
2037 45 : free (expr->representation.string);
2038 45 : expr->representation.string = NULL;
2039 : }
2040 : }
2041 : }
2042 :
2043 :
2044 : /* Function to create and update the enumerator history
2045 : using the information passed as arguments.
2046 : Pointer "max_enum" is also updated, to point to
2047 : enum history node containing largest initializer.
2048 :
2049 : SYM points to the symbol node of enumerator.
2050 : INIT points to its enumerator value. */
2051 :
2052 : static void
2053 543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
2054 : {
2055 543 : enumerator_history *new_enum_history;
2056 543 : gcc_assert (sym != NULL && init != NULL);
2057 :
2058 543 : new_enum_history = XCNEW (enumerator_history);
2059 :
2060 543 : new_enum_history->sym = sym;
2061 543 : new_enum_history->initializer = init;
2062 543 : new_enum_history->next = NULL;
2063 :
2064 543 : if (enum_history == NULL)
2065 : {
2066 160 : enum_history = new_enum_history;
2067 160 : max_enum = enum_history;
2068 : }
2069 : else
2070 : {
2071 383 : new_enum_history->next = enum_history;
2072 383 : enum_history = new_enum_history;
2073 :
2074 383 : if (mpz_cmp (max_enum->initializer->value.integer,
2075 383 : new_enum_history->initializer->value.integer) < 0)
2076 381 : max_enum = new_enum_history;
2077 : }
2078 543 : }
2079 :
2080 :
2081 : /* Function to free enum kind history. */
2082 :
2083 : void
2084 175 : gfc_free_enum_history (void)
2085 : {
2086 175 : enumerator_history *current = enum_history;
2087 175 : enumerator_history *next;
2088 :
2089 718 : while (current != NULL)
2090 : {
2091 543 : next = current->next;
2092 543 : free (current);
2093 543 : current = next;
2094 : }
2095 175 : max_enum = NULL;
2096 175 : enum_history = NULL;
2097 175 : }
2098 :
2099 :
2100 : /* Function to fix initializer character length if the length of the
2101 : symbol or component is constant. */
2102 :
2103 : static bool
2104 2753 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
2105 : {
2106 2753 : if (!gfc_specification_expr (ts->u.cl->length))
2107 : return false;
2108 :
2109 2753 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
2110 :
2111 : /* resolve_charlen will complain later on if the length
2112 : is too large. Just skip the initialization in that case. */
2113 2753 : if (mpz_cmp (ts->u.cl->length->value.integer,
2114 2753 : gfc_integer_kinds[k].huge) <= 0)
2115 : {
2116 2752 : HOST_WIDE_INT len
2117 2752 : = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
2118 :
2119 2752 : if (init->expr_type == EXPR_CONSTANT)
2120 2006 : gfc_set_constant_character_len (len, init, -1);
2121 746 : else if (init->expr_type == EXPR_ARRAY)
2122 : {
2123 745 : gfc_constructor *cons;
2124 :
2125 : /* Build a new charlen to prevent simplification from
2126 : deleting the length before it is resolved. */
2127 745 : init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2128 745 : init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
2129 745 : cons = gfc_constructor_first (init->value.constructor);
2130 5049 : for (; cons; cons = gfc_constructor_next (cons))
2131 3559 : gfc_set_constant_character_len (len, cons->expr, -1);
2132 : }
2133 : }
2134 :
2135 : return true;
2136 : }
2137 :
2138 :
2139 : /* Function called by variable_decl() that adds an initialization
2140 : expression to a symbol. */
2141 :
2142 : static bool
2143 272283 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus,
2144 : gfc_charlen *saved_cl_list)
2145 : {
2146 272283 : symbol_attribute attr;
2147 272283 : gfc_symbol *sym;
2148 272283 : gfc_expr *init;
2149 :
2150 272283 : init = *initp;
2151 272283 : if (find_special (name, &sym, false))
2152 : return false;
2153 :
2154 272283 : attr = sym->attr;
2155 :
2156 : /* If this symbol is confirming an implicit parameter type,
2157 : then an initialization expression is not allowed. */
2158 272283 : if (attr.flavor == FL_PARAMETER && sym->value != NULL)
2159 : {
2160 1 : if (*initp != NULL)
2161 : {
2162 0 : gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
2163 : sym->name);
2164 0 : return false;
2165 : }
2166 : else
2167 : return true;
2168 : }
2169 :
2170 272282 : if (init == NULL)
2171 : {
2172 : /* An initializer is required for PARAMETER declarations. */
2173 238895 : if (attr.flavor == FL_PARAMETER)
2174 : {
2175 1 : gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
2176 1 : return false;
2177 : }
2178 : }
2179 : else
2180 : {
2181 : /* If a variable appears in a DATA block, it cannot have an
2182 : initializer. */
2183 33387 : if (sym->attr.data)
2184 : {
2185 0 : gfc_error ("Variable %qs at %C with an initializer already "
2186 : "appears in a DATA statement", sym->name);
2187 0 : return false;
2188 : }
2189 :
2190 : /* Check if the assignment can happen. This has to be put off
2191 : until later for derived type variables and procedure pointers. */
2192 32206 : if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
2193 32183 : && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
2194 32133 : && !sym->attr.proc_pointer
2195 65411 : && !gfc_check_assign_symbol (sym, NULL, init))
2196 : return false;
2197 :
2198 33356 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
2199 3446 : && init->ts.type == BT_CHARACTER)
2200 : {
2201 : /* Update symbol character length according initializer. */
2202 3282 : if (!gfc_check_assign_symbol (sym, NULL, init))
2203 : return false;
2204 :
2205 3282 : if (sym->ts.u.cl->length == NULL)
2206 : {
2207 851 : gfc_charlen_t clen;
2208 : /* If there are multiple CHARACTER variables declared on the
2209 : same line, we don't want them to share the same length. */
2210 851 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2211 :
2212 851 : if (sym->attr.flavor == FL_PARAMETER)
2213 : {
2214 842 : if (init->expr_type == EXPR_CONSTANT)
2215 : {
2216 557 : clen = init->value.character.length;
2217 557 : sym->ts.u.cl->length
2218 557 : = gfc_get_int_expr (gfc_charlen_int_kind,
2219 : NULL, clen);
2220 : }
2221 285 : else if (init->expr_type == EXPR_ARRAY)
2222 : {
2223 285 : if (init->ts.u.cl && init->ts.u.cl->length)
2224 : {
2225 273 : const gfc_expr *length = init->ts.u.cl->length;
2226 273 : if (length->expr_type != EXPR_CONSTANT)
2227 : {
2228 3 : gfc_error ("Cannot initialize parameter array "
2229 : "at %L "
2230 : "with variable length elements",
2231 : &sym->declared_at);
2232 :
2233 : /* This rejection path can leave several
2234 : declaration-local charlens on cl_list,
2235 : including the replacement symbol charlen and
2236 : the array-constructor typespec charlen.
2237 : Clear the surviving owners first, then drop
2238 : only the nodes created by this declaration. */
2239 3 : sym->ts.u.cl = NULL;
2240 3 : init->ts.u.cl = NULL;
2241 3 : discard_pending_charlens (saved_cl_list);
2242 3 : return false;
2243 : }
2244 270 : clen = mpz_get_si (length->value.integer);
2245 270 : }
2246 12 : else if (init->value.constructor)
2247 : {
2248 12 : gfc_constructor *c;
2249 12 : c = gfc_constructor_first (init->value.constructor);
2250 12 : clen = c->expr->value.character.length;
2251 : }
2252 : else
2253 0 : gcc_unreachable ();
2254 282 : sym->ts.u.cl->length
2255 282 : = gfc_get_int_expr (gfc_charlen_int_kind,
2256 : NULL, clen);
2257 : }
2258 0 : else if (init->ts.u.cl && init->ts.u.cl->length)
2259 0 : sym->ts.u.cl->length =
2260 0 : gfc_copy_expr (init->ts.u.cl->length);
2261 : }
2262 : }
2263 : /* Update initializer character length according to symbol. */
2264 2431 : else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2265 2431 : && !fix_initializer_charlen (&sym->ts, init))
2266 : return false;
2267 : }
2268 :
2269 33353 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
2270 3802 : && sym->as->rank && init->rank && init->rank != sym->as->rank)
2271 : {
2272 3 : gfc_error ("Rank mismatch of array at %L and its initializer "
2273 : "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
2274 3 : return false;
2275 : }
2276 :
2277 : /* If sym is implied-shape, set its upper bounds from init. */
2278 33350 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2279 3799 : && sym->as && sym->as->type == AS_IMPLIED_SHAPE)
2280 : {
2281 1041 : int dim;
2282 :
2283 1041 : if (init->rank == 0)
2284 : {
2285 1 : gfc_error ("Cannot initialize implied-shape array at %L"
2286 : " with scalar", &sym->declared_at);
2287 1 : return false;
2288 : }
2289 :
2290 : /* The shape may be NULL for EXPR_ARRAY, set it. */
2291 1040 : if (init->shape == NULL)
2292 : {
2293 5 : if (init->expr_type != EXPR_ARRAY)
2294 : {
2295 2 : gfc_error ("Bad shape of initializer at %L", &init->where);
2296 2 : return false;
2297 : }
2298 :
2299 3 : init->shape = gfc_get_shape (1);
2300 3 : if (!gfc_array_size (init, &init->shape[0]))
2301 : {
2302 1 : gfc_error ("Cannot determine shape of initializer at %L",
2303 : &init->where);
2304 1 : free (init->shape);
2305 1 : init->shape = NULL;
2306 1 : return false;
2307 : }
2308 : }
2309 :
2310 2175 : for (dim = 0; dim < sym->as->rank; ++dim)
2311 : {
2312 1139 : int k;
2313 1139 : gfc_expr *e, *lower;
2314 :
2315 1139 : lower = sym->as->lower[dim];
2316 :
2317 : /* If the lower bound is an array element from another
2318 : parameterized array, then it is marked with EXPR_VARIABLE and
2319 : is an initialization expression. Try to reduce it. */
2320 1139 : if (lower->expr_type == EXPR_VARIABLE)
2321 7 : gfc_reduce_init_expr (lower);
2322 :
2323 1139 : if (lower->expr_type == EXPR_CONSTANT)
2324 : {
2325 : /* All dimensions must be without upper bound. */
2326 1138 : gcc_assert (!sym->as->upper[dim]);
2327 :
2328 1138 : k = lower->ts.kind;
2329 1138 : e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2330 1138 : mpz_add (e->value.integer, lower->value.integer,
2331 1138 : init->shape[dim]);
2332 1138 : mpz_sub_ui (e->value.integer, e->value.integer, 1);
2333 1138 : sym->as->upper[dim] = e;
2334 : }
2335 : else
2336 : {
2337 1 : gfc_error ("Non-constant lower bound in implied-shape"
2338 : " declaration at %L", &lower->where);
2339 1 : return false;
2340 : }
2341 : }
2342 :
2343 1036 : sym->as->type = AS_EXPLICIT;
2344 : }
2345 :
2346 : /* Ensure that explicit bounds are simplified. */
2347 33345 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2348 3794 : && sym->as && sym->as->type == AS_EXPLICIT)
2349 : {
2350 8422 : for (int dim = 0; dim < sym->as->rank; ++dim)
2351 : {
2352 4640 : gfc_expr *e;
2353 :
2354 4640 : e = sym->as->lower[dim];
2355 4640 : if (e->expr_type != EXPR_CONSTANT)
2356 12 : gfc_reduce_init_expr (e);
2357 :
2358 4640 : e = sym->as->upper[dim];
2359 4640 : if (e->expr_type != EXPR_CONSTANT)
2360 106 : gfc_reduce_init_expr (e);
2361 : }
2362 : }
2363 :
2364 : /* Need to check if the expression we initialized this
2365 : to was one of the iso_c_binding named constants. If so,
2366 : and we're a parameter (constant), let it be iso_c.
2367 : For example:
2368 : integer(c_int), parameter :: my_int = c_int
2369 : integer(my_int) :: my_int_2
2370 : If we mark my_int as iso_c (since we can see it's value
2371 : is equal to one of the named constants), then my_int_2
2372 : will be considered C interoperable. */
2373 33345 : if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2374 : {
2375 28724 : sym->ts.is_iso_c |= init->ts.is_iso_c;
2376 28724 : sym->ts.is_c_interop |= init->ts.is_c_interop;
2377 : /* attr bits needed for module files. */
2378 28724 : sym->attr.is_iso_c |= init->ts.is_iso_c;
2379 28724 : sym->attr.is_c_interop |= init->ts.is_c_interop;
2380 28724 : if (init->ts.is_iso_c)
2381 118 : sym->ts.f90_type = init->ts.f90_type;
2382 : }
2383 :
2384 : /* Catch the case: type(t), parameter :: x = z'1'. */
2385 33345 : if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
2386 : {
2387 1 : gfc_error ("Entity %qs at %L is incompatible with a BOZ "
2388 : "literal constant", name, &sym->declared_at);
2389 1 : return false;
2390 : }
2391 :
2392 : /* Add initializer. Make sure we keep the ranks sane. */
2393 33344 : if (sym->attr.dimension && init->rank == 0)
2394 : {
2395 1271 : mpz_t size;
2396 1271 : gfc_expr *array;
2397 1271 : int n;
2398 1271 : if (sym->attr.flavor == FL_PARAMETER
2399 468 : && gfc_is_constant_expr (init)
2400 467 : && (init->expr_type == EXPR_CONSTANT
2401 48 : || init->expr_type == EXPR_STRUCTURE)
2402 1738 : && spec_size (sym->as, &size))
2403 : {
2404 463 : array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2405 : &init->where);
2406 463 : if (init->ts.type == BT_DERIVED)
2407 48 : array->ts.u.derived = init->ts.u.derived;
2408 67619 : for (n = 0; n < (int)mpz_get_si (size); n++)
2409 133990 : gfc_constructor_append_expr (&array->value.constructor,
2410 : n == 0
2411 : ? init
2412 66834 : : gfc_copy_expr (init),
2413 : &init->where);
2414 :
2415 463 : array->shape = gfc_get_shape (sym->as->rank);
2416 1052 : for (n = 0; n < sym->as->rank; n++)
2417 589 : spec_dimen_size (sym->as, n, &array->shape[n]);
2418 :
2419 463 : init = array;
2420 463 : mpz_clear (size);
2421 : }
2422 1271 : init->rank = sym->as->rank;
2423 1271 : init->corank = sym->as->corank;
2424 : }
2425 :
2426 33344 : sym->value = init;
2427 33344 : if (sym->attr.save == SAVE_NONE)
2428 28790 : sym->attr.save = SAVE_IMPLICIT;
2429 33344 : *initp = NULL;
2430 : }
2431 :
2432 : return true;
2433 : }
2434 :
2435 :
2436 : /* Function called by variable_decl() that adds a name to a structure
2437 : being built. */
2438 :
2439 : static bool
2440 18223 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2441 : gfc_array_spec **as)
2442 : {
2443 18223 : gfc_state_data *s;
2444 18223 : gfc_component *c;
2445 :
2446 : /* F03:C438/C439. If the current symbol is of the same derived type that we're
2447 : constructing, it must have the pointer attribute. */
2448 18223 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2449 3449 : && current_ts.u.derived == gfc_current_block ()
2450 267 : && current_attr.pointer == 0)
2451 : {
2452 106 : if (current_attr.allocatable
2453 106 : && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2454 : "must have the POINTER attribute"))
2455 : {
2456 : return false;
2457 : }
2458 105 : else if (current_attr.allocatable == 0)
2459 : {
2460 0 : gfc_error ("Component at %C must have the POINTER attribute");
2461 0 : return false;
2462 : }
2463 : }
2464 :
2465 : /* F03:C437. */
2466 18222 : if (current_ts.type == BT_CLASS
2467 851 : && !(current_attr.pointer || current_attr.allocatable))
2468 : {
2469 5 : gfc_error ("Component %qs with CLASS at %C must be allocatable "
2470 : "or pointer", name);
2471 5 : return false;
2472 : }
2473 :
2474 18217 : if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2475 : {
2476 0 : if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2477 : {
2478 0 : gfc_error ("Array component of structure at %C must have explicit "
2479 : "or deferred shape");
2480 0 : return false;
2481 : }
2482 : }
2483 :
2484 : /* If we are in a nested union/map definition, gfc_add_component will not
2485 : properly find repeated components because:
2486 : (i) gfc_add_component does a flat search, where components of unions
2487 : and maps are implicity chained so nested components may conflict.
2488 : (ii) Unions and maps are not linked as components of their parent
2489 : structures until after they are parsed.
2490 : For (i) we use gfc_find_component which searches recursively, and for (ii)
2491 : we search each block directly from the parse stack until we find the top
2492 : level structure. */
2493 :
2494 18217 : s = gfc_state_stack;
2495 18217 : if (s->state == COMP_UNION || s->state == COMP_MAP)
2496 : {
2497 1434 : while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2498 : {
2499 1434 : c = gfc_find_component (s->sym, name, true, true, NULL);
2500 1434 : if (c != NULL)
2501 : {
2502 0 : gfc_error_now ("Component %qs at %C already declared at %L",
2503 : name, &c->loc);
2504 0 : return false;
2505 : }
2506 : /* Break after we've searched the entire chain. */
2507 1434 : if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2508 : break;
2509 1000 : s = s->previous;
2510 : }
2511 : }
2512 :
2513 18217 : if (!gfc_add_component (gfc_current_block(), name, &c))
2514 : return false;
2515 :
2516 18211 : c->ts = current_ts;
2517 18211 : if (c->ts.type == BT_CHARACTER)
2518 1952 : c->ts.u.cl = cl;
2519 :
2520 18211 : if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2521 14768 : && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2522 2138 : && saved_kind_expr != NULL)
2523 200 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
2524 :
2525 18211 : c->attr = current_attr;
2526 :
2527 18211 : c->initializer = *init;
2528 18211 : *init = NULL;
2529 :
2530 : /* Update initializer character length according to component. */
2531 1952 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
2532 1545 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
2533 1480 : && c->initializer && c->initializer->ts.type == BT_CHARACTER
2534 18536 : && !fix_initializer_charlen (&c->ts, c->initializer))
2535 : return false;
2536 :
2537 18211 : c->as = *as;
2538 18211 : if (c->as != NULL)
2539 : {
2540 4903 : if (c->as->corank)
2541 113 : c->attr.codimension = 1;
2542 4903 : if (c->as->rank)
2543 4822 : c->attr.dimension = 1;
2544 : }
2545 18211 : *as = NULL;
2546 :
2547 18211 : gfc_apply_init (&c->ts, &c->attr, c->initializer);
2548 :
2549 : /* Convert a class, PDT component of a non-derived type to a specific instance
2550 : before gfc_build_class_symbol gets to work on it. */
2551 18211 : if (c->ts.type == BT_CLASS
2552 846 : && !(gfc_current_block ()->attr.pdt_template
2553 846 : || gfc_current_block ()->attr.pdt_type)
2554 846 : && c->ts.u.derived->attr.pdt_template)
2555 : {
2556 12 : match m = gfc_get_pdt_instance (decl_type_param_list, &c->ts.u.derived, NULL);
2557 12 : if (m != MATCH_YES)
2558 : {
2559 0 : if (!gfc_error_check ())
2560 0 : gfc_error ("Parameterized component of a non-parameterized "
2561 : "derived type at %C could not be converted to a valid "
2562 : "instance");
2563 0 : return false;
2564 : }
2565 : }
2566 :
2567 : /* Check array components. */
2568 18211 : if (!c->attr.dimension)
2569 13389 : goto scalar;
2570 :
2571 4822 : if (c->attr.pointer)
2572 : {
2573 732 : if (c->as->type != AS_DEFERRED)
2574 : {
2575 5 : gfc_error ("Pointer array component of structure at %C must have a "
2576 : "deferred shape");
2577 5 : return false;
2578 : }
2579 : }
2580 4090 : else if (c->attr.allocatable)
2581 : {
2582 2429 : const char *err = G_("Allocatable component of structure at %C must have "
2583 : "a deferred shape");
2584 2429 : if (c->as->type != AS_DEFERRED)
2585 : {
2586 14 : if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
2587 : {
2588 : /* Issue an immediate error and allow this component to pass for
2589 : the sake of clean error recovery. Set the error flag for the
2590 : containing derived type so that finalizers are not built. */
2591 4 : gfc_error_now (err);
2592 4 : s->sym->error = 1;
2593 4 : c->as->type = AS_DEFERRED;
2594 : }
2595 : else
2596 : {
2597 10 : gfc_error (err);
2598 10 : return false;
2599 : }
2600 : }
2601 : }
2602 : else
2603 : {
2604 1661 : if (c->as->type != AS_EXPLICIT)
2605 : {
2606 7 : gfc_error ("Array component of structure at %C must have an "
2607 : "explicit shape");
2608 7 : return false;
2609 : }
2610 : }
2611 :
2612 1654 : scalar:
2613 18189 : if (c->ts.type == BT_CLASS)
2614 843 : return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2615 :
2616 17346 : if (c->attr.pdt_kind || c->attr.pdt_len)
2617 : {
2618 604 : gfc_symbol *sym;
2619 604 : gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2620 : 0, &sym);
2621 604 : if (sym == NULL)
2622 : {
2623 0 : gfc_error ("Type parameter %qs at %C has no corresponding entry "
2624 : "in the type parameter name list at %L",
2625 0 : c->name, &gfc_current_block ()->declared_at);
2626 0 : return false;
2627 : }
2628 604 : sym->ts = c->ts;
2629 604 : sym->attr.pdt_kind = c->attr.pdt_kind;
2630 604 : sym->attr.pdt_len = c->attr.pdt_len;
2631 604 : if (c->initializer)
2632 246 : sym->value = gfc_copy_expr (c->initializer);
2633 604 : sym->attr.flavor = FL_VARIABLE;
2634 : }
2635 :
2636 17346 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2637 2597 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2638 130 : && decl_type_param_list)
2639 130 : c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2640 :
2641 : return true;
2642 : }
2643 :
2644 :
2645 : /* Match a 'NULL()', and possibly take care of some side effects. */
2646 :
2647 : match
2648 1716 : gfc_match_null (gfc_expr **result)
2649 : {
2650 1716 : gfc_symbol *sym;
2651 1716 : match m, m2 = MATCH_NO;
2652 :
2653 1716 : if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2654 : return MATCH_ERROR;
2655 :
2656 1716 : if (m == MATCH_NO)
2657 : {
2658 511 : locus old_loc;
2659 511 : char name[GFC_MAX_SYMBOL_LEN + 1];
2660 :
2661 511 : if ((m2 = gfc_match (" null (")) != MATCH_YES)
2662 505 : return m2;
2663 :
2664 6 : old_loc = gfc_current_locus;
2665 6 : if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2666 : return MATCH_ERROR;
2667 6 : if (m2 != MATCH_YES
2668 6 : && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2669 : return MATCH_ERROR;
2670 6 : if (m2 == MATCH_NO)
2671 : {
2672 0 : gfc_current_locus = old_loc;
2673 0 : return MATCH_NO;
2674 : }
2675 : }
2676 :
2677 : /* The NULL symbol now has to be/become an intrinsic function. */
2678 1211 : if (gfc_get_symbol ("null", NULL, &sym))
2679 : {
2680 0 : gfc_error ("NULL() initialization at %C is ambiguous");
2681 0 : return MATCH_ERROR;
2682 : }
2683 :
2684 1211 : gfc_intrinsic_symbol (sym);
2685 :
2686 1211 : if (sym->attr.proc != PROC_INTRINSIC
2687 853 : && !(sym->attr.use_assoc && sym->attr.intrinsic)
2688 2063 : && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2689 852 : || !gfc_add_function (&sym->attr, sym->name, NULL)))
2690 0 : return MATCH_ERROR;
2691 :
2692 1211 : *result = gfc_get_null_expr (&gfc_current_locus);
2693 :
2694 : /* Invalid per F2008, C512. */
2695 1211 : if (m2 == MATCH_YES)
2696 : {
2697 6 : gfc_error ("NULL() initialization at %C may not have MOLD");
2698 6 : return MATCH_ERROR;
2699 : }
2700 :
2701 : return MATCH_YES;
2702 : }
2703 :
2704 :
2705 : /* Match the initialization expr for a data pointer or procedure pointer. */
2706 :
2707 : static match
2708 1380 : match_pointer_init (gfc_expr **init, int procptr)
2709 : {
2710 1380 : match m;
2711 :
2712 1380 : if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2713 : {
2714 1 : gfc_error ("Initialization of pointer at %C is not allowed in "
2715 : "a PURE procedure");
2716 1 : return MATCH_ERROR;
2717 : }
2718 1379 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2719 :
2720 : /* Match NULL() initialization. */
2721 1379 : m = gfc_match_null (init);
2722 1379 : if (m != MATCH_NO)
2723 : return m;
2724 :
2725 : /* Match non-NULL initialization. */
2726 176 : gfc_matching_ptr_assignment = !procptr;
2727 176 : gfc_matching_procptr_assignment = procptr;
2728 176 : m = gfc_match_rvalue (init);
2729 176 : gfc_matching_ptr_assignment = 0;
2730 176 : gfc_matching_procptr_assignment = 0;
2731 176 : if (m == MATCH_ERROR)
2732 : return MATCH_ERROR;
2733 175 : else if (m == MATCH_NO)
2734 : {
2735 2 : gfc_error ("Error in pointer initialization at %C");
2736 2 : return MATCH_ERROR;
2737 : }
2738 :
2739 173 : if (!procptr && !gfc_resolve_expr (*init))
2740 : return MATCH_ERROR;
2741 :
2742 172 : if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2743 : "initialization at %C"))
2744 : return MATCH_ERROR;
2745 :
2746 : return MATCH_YES;
2747 : }
2748 :
2749 :
2750 : static bool
2751 292075 : check_function_name (char *name)
2752 : {
2753 : /* In functions that have a RESULT variable defined, the function name always
2754 : refers to function calls. Therefore, the name is not allowed to appear in
2755 : specification statements. When checking this, be careful about
2756 : 'hidden' procedure pointer results ('ppr@'). */
2757 :
2758 292075 : if (gfc_current_state () == COMP_FUNCTION)
2759 : {
2760 47991 : gfc_symbol *block = gfc_current_block ();
2761 47991 : if (block && block->result && block->result != block
2762 15418 : && strcmp (block->result->name, "ppr@") != 0
2763 15359 : && strcmp (block->name, name) == 0)
2764 : {
2765 9 : gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2766 : "from appearing in a specification statement",
2767 : block->result->name, &block->result->declared_at, name);
2768 9 : return false;
2769 : }
2770 : }
2771 :
2772 : return true;
2773 : }
2774 :
2775 :
2776 : /* Match a variable name with an optional initializer. When this
2777 : subroutine is called, a variable is expected to be parsed next.
2778 : Depending on what is happening at the moment, updates either the
2779 : symbol table or the current interface. */
2780 :
2781 : static match
2782 281844 : variable_decl (int elem)
2783 : {
2784 281844 : char name[GFC_MAX_SYMBOL_LEN + 1];
2785 281844 : static unsigned int fill_id = 0;
2786 281844 : gfc_expr *initializer, *char_len;
2787 281844 : gfc_array_spec *as;
2788 281844 : gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2789 281844 : gfc_charlen *cl;
2790 281844 : gfc_charlen *saved_cl_list;
2791 281844 : bool cl_deferred;
2792 281844 : locus var_locus;
2793 281844 : match m;
2794 281844 : bool t;
2795 281844 : gfc_symbol *sym;
2796 281844 : char c;
2797 :
2798 281844 : initializer = NULL;
2799 281844 : as = NULL;
2800 281844 : cp_as = NULL;
2801 281844 : saved_cl_list = gfc_current_ns->cl_list;
2802 :
2803 : /* When we get here, we've just matched a list of attributes and
2804 : maybe a type and a double colon. The next thing we expect to see
2805 : is the name of the symbol. */
2806 :
2807 : /* If we are parsing a structure with legacy support, we allow the symbol
2808 : name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2809 281844 : m = MATCH_NO;
2810 281844 : gfc_gobble_whitespace ();
2811 281844 : var_locus = gfc_current_locus;
2812 281844 : c = gfc_peek_ascii_char ();
2813 281844 : if (c == '%')
2814 : {
2815 12 : gfc_next_ascii_char (); /* Burn % character. */
2816 12 : m = gfc_match ("fill");
2817 12 : if (m == MATCH_YES)
2818 : {
2819 11 : if (gfc_current_state () != COMP_STRUCTURE)
2820 : {
2821 2 : if (flag_dec_structure)
2822 1 : gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2823 : else
2824 1 : gfc_error ("%qs at %C is a DEC extension, enable with "
2825 : "%<-fdec-structure%>", "%FILL");
2826 2 : m = MATCH_ERROR;
2827 2 : goto cleanup;
2828 : }
2829 :
2830 9 : if (attr_seen)
2831 : {
2832 1 : gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2833 1 : m = MATCH_ERROR;
2834 1 : goto cleanup;
2835 : }
2836 :
2837 : /* %FILL components are given invalid fortran names. */
2838 8 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2839 : }
2840 : else
2841 : {
2842 1 : gfc_error ("Invalid character %qc in variable name at %C", c);
2843 1 : return MATCH_ERROR;
2844 : }
2845 : }
2846 : else
2847 : {
2848 281832 : m = gfc_match_name (name);
2849 281831 : if (m != MATCH_YES)
2850 10 : goto cleanup;
2851 : }
2852 :
2853 : /* Now we could see the optional array spec. or character length. */
2854 281829 : m = gfc_match_array_spec (&as, true, true);
2855 281828 : if (m == MATCH_ERROR)
2856 57 : goto cleanup;
2857 :
2858 281771 : if (m == MATCH_NO)
2859 220298 : as = gfc_copy_array_spec (current_as);
2860 61473 : else if (current_as
2861 61473 : && !merge_array_spec (current_as, as, true))
2862 : {
2863 4 : m = MATCH_ERROR;
2864 4 : goto cleanup;
2865 : }
2866 :
2867 281767 : var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
2868 : &gfc_current_locus);
2869 281767 : if (flag_cray_pointer)
2870 3063 : cp_as = gfc_copy_array_spec (as);
2871 :
2872 : /* At this point, we know for sure if the symbol is PARAMETER and can thus
2873 : determine (and check) whether it can be implied-shape. If it
2874 : was parsed as assumed-size, change it because PARAMETERs cannot
2875 : be assumed-size.
2876 :
2877 : An explicit-shape-array cannot appear under several conditions.
2878 : That check is done here as well. */
2879 281767 : if (as)
2880 : {
2881 84121 : if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2882 : {
2883 2 : m = MATCH_ERROR;
2884 2 : gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2885 : name, &var_locus);
2886 2 : goto cleanup;
2887 : }
2888 :
2889 84119 : if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2890 6509 : && current_attr.flavor == FL_PARAMETER)
2891 993 : as->type = AS_IMPLIED_SHAPE;
2892 :
2893 84119 : if (as->type == AS_IMPLIED_SHAPE
2894 84119 : && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2895 : &var_locus))
2896 : {
2897 1 : m = MATCH_ERROR;
2898 1 : goto cleanup;
2899 : }
2900 :
2901 84118 : gfc_seen_div0 = false;
2902 :
2903 : /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2904 : constant expressions shall appear only in a subprogram, derived
2905 : type definition, BLOCK construct, or interface body. */
2906 84118 : if (as->type == AS_EXPLICIT
2907 42055 : && gfc_current_state () != COMP_BLOCK
2908 : && gfc_current_state () != COMP_DERIVED
2909 : && gfc_current_state () != COMP_FUNCTION
2910 : && gfc_current_state () != COMP_INTERFACE
2911 : && gfc_current_state () != COMP_SUBROUTINE)
2912 : {
2913 : gfc_expr *e;
2914 49954 : bool not_constant = false;
2915 :
2916 49954 : for (int i = 0; i < as->rank; i++)
2917 : {
2918 28436 : e = gfc_copy_expr (as->lower[i]);
2919 28436 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2920 : {
2921 0 : m = MATCH_ERROR;
2922 0 : goto cleanup;
2923 : }
2924 :
2925 28436 : gfc_simplify_expr (e, 0);
2926 28436 : if (e && (e->expr_type != EXPR_CONSTANT))
2927 : {
2928 : not_constant = true;
2929 : break;
2930 : }
2931 28436 : gfc_free_expr (e);
2932 :
2933 28436 : e = gfc_copy_expr (as->upper[i]);
2934 28436 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2935 : {
2936 4 : m = MATCH_ERROR;
2937 4 : goto cleanup;
2938 : }
2939 :
2940 28432 : gfc_simplify_expr (e, 0);
2941 28432 : if (e && (e->expr_type != EXPR_CONSTANT))
2942 : {
2943 : not_constant = true;
2944 : break;
2945 : }
2946 28419 : gfc_free_expr (e);
2947 : }
2948 :
2949 21531 : if (not_constant && e->ts.type != BT_INTEGER)
2950 : {
2951 4 : gfc_error ("Explicit array shape at %C must be constant of "
2952 : "INTEGER type and not %s type",
2953 : gfc_basic_typename (e->ts.type));
2954 4 : m = MATCH_ERROR;
2955 4 : goto cleanup;
2956 : }
2957 9 : if (not_constant)
2958 : {
2959 9 : gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2960 9 : m = MATCH_ERROR;
2961 9 : goto cleanup;
2962 : }
2963 : }
2964 84101 : if (as->type == AS_EXPLICIT)
2965 : {
2966 100578 : for (int i = 0; i < as->rank; i++)
2967 : {
2968 58540 : gfc_expr *e, *n;
2969 58540 : e = as->lower[i];
2970 58540 : if (e->expr_type != EXPR_CONSTANT)
2971 : {
2972 452 : n = gfc_copy_expr (e);
2973 452 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2974 : {
2975 0 : m = MATCH_ERROR;
2976 0 : goto cleanup;
2977 : }
2978 :
2979 452 : if (n->expr_type == EXPR_CONSTANT)
2980 22 : gfc_replace_expr (e, n);
2981 : else
2982 430 : gfc_free_expr (n);
2983 : }
2984 58540 : e = as->upper[i];
2985 58540 : if (e->expr_type != EXPR_CONSTANT)
2986 : {
2987 6750 : n = gfc_copy_expr (e);
2988 6750 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2989 : {
2990 0 : m = MATCH_ERROR;
2991 0 : goto cleanup;
2992 : }
2993 :
2994 6750 : if (n->expr_type == EXPR_CONSTANT)
2995 45 : gfc_replace_expr (e, n);
2996 : else
2997 6705 : gfc_free_expr (n);
2998 : }
2999 : /* For an explicit-shape spec with constant bounds, ensure
3000 : that the effective upper bound is not lower than the
3001 : respective lower bound minus one. Otherwise adjust it so
3002 : that the extent is trivially derived to be zero. */
3003 58540 : if (as->lower[i]->expr_type == EXPR_CONSTANT
3004 58110 : && as->upper[i]->expr_type == EXPR_CONSTANT
3005 51829 : && as->lower[i]->ts.type == BT_INTEGER
3006 51829 : && as->upper[i]->ts.type == BT_INTEGER
3007 51824 : && mpz_cmp (as->upper[i]->value.integer,
3008 51824 : as->lower[i]->value.integer) < 0)
3009 1212 : mpz_sub_ui (as->upper[i]->value.integer,
3010 : as->lower[i]->value.integer, 1);
3011 : }
3012 : }
3013 : }
3014 :
3015 281747 : char_len = NULL;
3016 281747 : cl = NULL;
3017 281747 : cl_deferred = false;
3018 :
3019 281747 : if (current_ts.type == BT_CHARACTER)
3020 : {
3021 31077 : switch (match_char_length (&char_len, &cl_deferred, false))
3022 : {
3023 435 : case MATCH_YES:
3024 435 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3025 :
3026 435 : cl->length = char_len;
3027 435 : break;
3028 :
3029 : /* Non-constant lengths need to be copied after the first
3030 : element. Also copy assumed lengths. */
3031 30641 : case MATCH_NO:
3032 30641 : if (elem > 1
3033 3923 : && (current_ts.u.cl->length == NULL
3034 2703 : || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
3035 : {
3036 1275 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3037 1275 : cl->length = gfc_copy_expr (current_ts.u.cl->length);
3038 : }
3039 : else
3040 29366 : cl = current_ts.u.cl;
3041 :
3042 30641 : cl_deferred = current_ts.deferred;
3043 :
3044 30641 : break;
3045 :
3046 1 : case MATCH_ERROR:
3047 1 : goto cleanup;
3048 : }
3049 : }
3050 :
3051 : /* The dummy arguments and result of the abbreviated form of MODULE
3052 : PROCEDUREs, used in SUBMODULES should not be redefined. */
3053 281746 : if (gfc_current_ns->proc_name
3054 277259 : && gfc_current_ns->proc_name->abr_modproc_decl)
3055 : {
3056 44 : gfc_find_symbol (name, gfc_current_ns, 1, &sym);
3057 44 : if (sym != NULL && (sym->attr.dummy || sym->attr.result))
3058 : {
3059 2 : m = MATCH_ERROR;
3060 2 : gfc_error ("%qs at %L is a redefinition of the declaration "
3061 : "in the corresponding interface for MODULE "
3062 : "PROCEDURE %qs", sym->name, &var_locus,
3063 2 : gfc_current_ns->proc_name->name);
3064 2 : goto cleanup;
3065 : }
3066 : }
3067 :
3068 : /* %FILL components may not have initializers. */
3069 281744 : if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
3070 : {
3071 1 : gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
3072 : &var_locus);
3073 1 : m = MATCH_ERROR;
3074 1 : goto cleanup;
3075 : }
3076 :
3077 : /* If this symbol has already shown up in a Cray Pointer declaration,
3078 : and this is not a component declaration,
3079 : then we want to set the type & bail out. */
3080 281743 : if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
3081 : {
3082 2959 : gfc_find_symbol (name, gfc_current_ns, 0, &sym);
3083 2959 : if (sym != NULL && sym->attr.cray_pointee)
3084 : {
3085 101 : m = MATCH_YES;
3086 101 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
3087 : {
3088 1 : m = MATCH_ERROR;
3089 1 : goto cleanup;
3090 : }
3091 :
3092 : /* Check to see if we have an array specification. */
3093 100 : if (cp_as != NULL)
3094 : {
3095 49 : if (sym->as != NULL)
3096 : {
3097 1 : gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
3098 1 : gfc_free_array_spec (cp_as);
3099 1 : m = MATCH_ERROR;
3100 1 : goto cleanup;
3101 : }
3102 : else
3103 : {
3104 48 : if (!gfc_set_array_spec (sym, cp_as, &var_locus))
3105 0 : gfc_internal_error ("Cannot set pointee array spec.");
3106 :
3107 : /* Fix the array spec. */
3108 48 : m = gfc_mod_pointee_as (sym->as);
3109 48 : if (m == MATCH_ERROR)
3110 0 : goto cleanup;
3111 : }
3112 : }
3113 99 : goto cleanup;
3114 : }
3115 : else
3116 : {
3117 2858 : gfc_free_array_spec (cp_as);
3118 : }
3119 : }
3120 : else
3121 : {
3122 : /* Check to see if this is the declaration of the type and/or attributes
3123 : of an implicit function result, emanating from a module function
3124 : interface declared within the parent module or submodule of a
3125 : containing submodule. */
3126 278784 : gfc_find_symbol (name, gfc_current_ns, 0, &sym);
3127 278784 : if (gfc_current_state () == COMP_FUNCTION
3128 46509 : && sym == gfc_current_block ()
3129 8266 : && sym->attr.if_source == IFSRC_DECL
3130 4946 : && sym->attr.used_in_submodule
3131 4 : && sym == sym->result
3132 4 : && sym->ts.type != BT_UNKNOWN)
3133 : {
3134 4 : m = MATCH_YES;
3135 4 : goto cleanup;
3136 : }
3137 278780 : sym = NULL;
3138 : }
3139 :
3140 : /* Procedure pointer as function result. */
3141 281638 : if (gfc_current_state () == COMP_FUNCTION
3142 46619 : && strcmp ("ppr@", gfc_current_block ()->name) == 0
3143 25 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
3144 7 : strcpy (name, "ppr@");
3145 :
3146 281638 : if (gfc_current_state () == COMP_FUNCTION
3147 46619 : && strcmp (name, gfc_current_block ()->name) == 0
3148 8282 : && gfc_current_block ()->result
3149 8282 : && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
3150 16 : strcpy (name, "ppr@");
3151 :
3152 : /* OK, we've successfully matched the declaration. Now put the
3153 : symbol in the current namespace, because it might be used in the
3154 : optional initialization expression for this symbol, e.g. this is
3155 : perfectly legal:
3156 :
3157 : integer, parameter :: i = huge(i)
3158 :
3159 : This is only true for parameters or variables of a basic type.
3160 : For components of derived types, it is not true, so we don't
3161 : create a symbol for those yet. If we fail to create the symbol,
3162 : bail out. */
3163 281638 : if (!gfc_comp_struct (gfc_current_state ())
3164 263386 : && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
3165 : {
3166 48 : m = MATCH_ERROR;
3167 48 : goto cleanup;
3168 : }
3169 :
3170 281590 : if (!check_function_name (name))
3171 : {
3172 0 : m = MATCH_ERROR;
3173 0 : goto cleanup;
3174 : }
3175 :
3176 : /* We allow old-style initializations of the form
3177 : integer i /2/, j(4) /3*3, 1/
3178 : (if no colon has been seen). These are different from data
3179 : statements in that initializers are only allowed to apply to the
3180 : variable immediately preceding, i.e.
3181 : integer i, j /1, 2/
3182 : is not allowed. Therefore we have to do some work manually, that
3183 : could otherwise be left to the matchers for DATA statements. */
3184 :
3185 281590 : if (!colon_seen && gfc_match (" /") == MATCH_YES)
3186 : {
3187 146 : if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
3188 : "initialization at %C"))
3189 : return MATCH_ERROR;
3190 :
3191 : /* Allow old style initializations for components of STRUCTUREs and MAPs
3192 : but not components of derived types. */
3193 146 : else if (gfc_current_state () == COMP_DERIVED)
3194 : {
3195 2 : gfc_error ("Invalid old style initialization for derived type "
3196 : "component at %C");
3197 2 : m = MATCH_ERROR;
3198 2 : goto cleanup;
3199 : }
3200 :
3201 : /* For structure components, read the initializer as a special
3202 : expression and let the rest of this function apply the initializer
3203 : as usual. */
3204 144 : else if (gfc_comp_struct (gfc_current_state ()))
3205 : {
3206 74 : m = match_clist_expr (&initializer, ¤t_ts, as);
3207 74 : if (m == MATCH_NO)
3208 : gfc_error ("Syntax error in old style initialization of %s at %C",
3209 : name);
3210 74 : if (m != MATCH_YES)
3211 14 : goto cleanup;
3212 : }
3213 :
3214 : /* Otherwise we treat the old style initialization just like a
3215 : DATA declaration for the current variable. */
3216 : else
3217 70 : return match_old_style_init (name);
3218 : }
3219 :
3220 : /* The double colon must be present in order to have initializers.
3221 : Otherwise the statement is ambiguous with an assignment statement. */
3222 281504 : if (colon_seen)
3223 : {
3224 235259 : if (gfc_match (" =>") == MATCH_YES)
3225 : {
3226 1197 : if (!current_attr.pointer)
3227 : {
3228 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
3229 0 : m = MATCH_ERROR;
3230 0 : goto cleanup;
3231 : }
3232 :
3233 1197 : m = match_pointer_init (&initializer, 0);
3234 1197 : if (m != MATCH_YES)
3235 10 : goto cleanup;
3236 :
3237 : /* The target of a pointer initialization must have the SAVE
3238 : attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
3239 : is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
3240 1187 : if (initializer->expr_type == EXPR_VARIABLE
3241 128 : && initializer->symtree->n.sym->attr.save == SAVE_NONE
3242 25 : && (gfc_current_state () == COMP_PROGRAM
3243 : || gfc_current_state () == COMP_MODULE
3244 25 : || gfc_current_state () == COMP_SUBMODULE))
3245 11 : initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
3246 : }
3247 234062 : else if (gfc_match_char ('=') == MATCH_YES)
3248 : {
3249 26294 : if (current_attr.pointer)
3250 : {
3251 0 : gfc_error ("Pointer initialization at %C requires %<=>%>, "
3252 : "not %<=%>");
3253 0 : m = MATCH_ERROR;
3254 0 : goto cleanup;
3255 : }
3256 :
3257 26294 : if (gfc_comp_struct (gfc_current_state ())
3258 2514 : && gfc_current_block ()->attr.pdt_template)
3259 : {
3260 269 : m = gfc_match_expr (&initializer);
3261 269 : if (initializer && initializer->ts.type == BT_UNKNOWN)
3262 115 : initializer->ts = current_ts;
3263 : }
3264 : else
3265 26025 : m = gfc_match_init_expr (&initializer);
3266 :
3267 26294 : if (m == MATCH_NO)
3268 : {
3269 1 : gfc_error ("Expected an initialization expression at %C");
3270 1 : m = MATCH_ERROR;
3271 : }
3272 :
3273 10245 : if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
3274 26296 : && !gfc_comp_struct (gfc_state_stack->state))
3275 : {
3276 1 : gfc_error ("Initialization of variable at %C is not allowed in "
3277 : "a PURE procedure");
3278 1 : m = MATCH_ERROR;
3279 : }
3280 :
3281 26294 : if (current_attr.flavor != FL_PARAMETER
3282 10245 : && !gfc_comp_struct (gfc_state_stack->state))
3283 7731 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
3284 :
3285 26294 : if (m != MATCH_YES)
3286 160 : goto cleanup;
3287 : }
3288 : }
3289 :
3290 281334 : if (initializer != NULL && current_attr.allocatable
3291 3 : && gfc_comp_struct (gfc_current_state ()))
3292 : {
3293 2 : gfc_error ("Initialization of allocatable component at %C is not "
3294 : "allowed");
3295 2 : m = MATCH_ERROR;
3296 2 : goto cleanup;
3297 : }
3298 :
3299 281332 : if (gfc_current_state () == COMP_DERIVED
3300 17210 : && initializer && initializer->ts.type == BT_HOLLERITH)
3301 : {
3302 1 : gfc_error ("Initialization of structure component with a HOLLERITH "
3303 : "constant at %L is not allowed", &initializer->where);
3304 1 : m = MATCH_ERROR;
3305 1 : goto cleanup;
3306 : }
3307 :
3308 281331 : if (gfc_current_state () == COMP_DERIVED
3309 17209 : && gfc_current_block ()->attr.pdt_template)
3310 : {
3311 1146 : gfc_symbol *param;
3312 1146 : gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
3313 : 0, ¶m);
3314 1146 : if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
3315 : {
3316 1 : gfc_error ("The component with KIND or LEN attribute at %C does not "
3317 : "not appear in the type parameter list at %L",
3318 1 : &gfc_current_block ()->declared_at);
3319 1 : m = MATCH_ERROR;
3320 4 : goto cleanup;
3321 : }
3322 1145 : else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
3323 : {
3324 1 : gfc_error ("The component at %C that appears in the type parameter "
3325 : "list at %L has neither the KIND nor LEN attribute",
3326 1 : &gfc_current_block ()->declared_at);
3327 1 : m = MATCH_ERROR;
3328 1 : goto cleanup;
3329 : }
3330 1144 : else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
3331 : {
3332 1 : gfc_error ("The component at %C which is a type parameter must be "
3333 : "a scalar");
3334 1 : m = MATCH_ERROR;
3335 1 : goto cleanup;
3336 : }
3337 1143 : else if (param && initializer)
3338 : {
3339 247 : if (initializer->ts.type == BT_BOZ)
3340 : {
3341 1 : gfc_error ("BOZ literal constant at %L cannot appear as an "
3342 : "initializer", &initializer->where);
3343 1 : m = MATCH_ERROR;
3344 1 : goto cleanup;
3345 : }
3346 246 : param->value = gfc_copy_expr (initializer);
3347 : }
3348 : }
3349 :
3350 : /* Before adding a possible initializer, do a simple check for compatibility
3351 : of lhs and rhs types. Assigning a REAL value to a derived type is not a
3352 : good thing. */
3353 28596 : if (current_ts.type == BT_DERIVED && initializer
3354 282778 : && (gfc_numeric_ts (&initializer->ts)
3355 1449 : || initializer->ts.type == BT_LOGICAL
3356 1449 : || initializer->ts.type == BT_CHARACTER))
3357 : {
3358 2 : gfc_error ("Incompatible initialization between a derived type "
3359 : "entity and an entity with %qs type at %C",
3360 : gfc_typename (initializer));
3361 2 : m = MATCH_ERROR;
3362 2 : goto cleanup;
3363 : }
3364 :
3365 :
3366 : /* Add the initializer. Note that it is fine if initializer is
3367 : NULL here, because we sometimes also need to check if a
3368 : declaration *must* have an initialization expression. */
3369 281325 : if (!gfc_comp_struct (gfc_current_state ()))
3370 263102 : t = add_init_expr_to_sym (name, &initializer, &var_locus,
3371 : saved_cl_list);
3372 : else
3373 : {
3374 18223 : if (current_ts.type == BT_DERIVED
3375 2597 : && !current_attr.pointer && !initializer)
3376 2038 : initializer = gfc_default_initializer (¤t_ts);
3377 18223 : t = build_struct (name, cl, &initializer, &as);
3378 :
3379 : /* If we match a nested structure definition we expect to see the
3380 : * body even if the variable declarations blow up, so we need to keep
3381 : * the structure declaration around. */
3382 18223 : if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
3383 34 : gfc_commit_symbol (gfc_new_block);
3384 : }
3385 :
3386 281473 : m = (t) ? MATCH_YES : MATCH_ERROR;
3387 :
3388 281771 : cleanup:
3389 : /* Free stuff up and return. */
3390 281771 : gfc_seen_div0 = false;
3391 281771 : gfc_free_expr (initializer);
3392 281771 : gfc_free_array_spec (as);
3393 :
3394 281771 : return m;
3395 : }
3396 :
3397 :
3398 : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
3399 : This assumes that the byte size is equal to the kind number for
3400 : non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
3401 :
3402 : static match
3403 107770 : gfc_match_old_kind_spec (gfc_typespec *ts)
3404 : {
3405 107770 : match m;
3406 107770 : int original_kind;
3407 :
3408 107770 : if (gfc_match_char ('*') != MATCH_YES)
3409 : return MATCH_NO;
3410 :
3411 1150 : m = gfc_match_small_literal_int (&ts->kind, NULL);
3412 1150 : if (m != MATCH_YES)
3413 : return MATCH_ERROR;
3414 :
3415 1150 : original_kind = ts->kind;
3416 :
3417 : /* Massage the kind numbers for complex types. */
3418 1150 : if (ts->type == BT_COMPLEX)
3419 : {
3420 79 : if (ts->kind % 2)
3421 : {
3422 0 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3423 : gfc_basic_typename (ts->type), original_kind);
3424 0 : return MATCH_ERROR;
3425 : }
3426 79 : ts->kind /= 2;
3427 :
3428 : }
3429 :
3430 1150 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3431 0 : ts->kind = 8;
3432 :
3433 1150 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3434 : {
3435 858 : if (ts->kind == 4)
3436 : {
3437 224 : if (flag_real4_kind == 8)
3438 24 : ts->kind = 8;
3439 224 : if (flag_real4_kind == 10)
3440 24 : ts->kind = 10;
3441 224 : if (flag_real4_kind == 16)
3442 24 : ts->kind = 16;
3443 : }
3444 634 : else if (ts->kind == 8)
3445 : {
3446 629 : if (flag_real8_kind == 4)
3447 24 : ts->kind = 4;
3448 629 : if (flag_real8_kind == 10)
3449 24 : ts->kind = 10;
3450 629 : if (flag_real8_kind == 16)
3451 24 : ts->kind = 16;
3452 : }
3453 : }
3454 :
3455 1150 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3456 : {
3457 8 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3458 : gfc_basic_typename (ts->type), original_kind);
3459 8 : return MATCH_ERROR;
3460 : }
3461 :
3462 1142 : if (!gfc_notify_std (GFC_STD_GNU,
3463 : "Nonstandard type declaration %s*%d at %C",
3464 : gfc_basic_typename(ts->type), original_kind))
3465 : return MATCH_ERROR;
3466 :
3467 : return MATCH_YES;
3468 : }
3469 :
3470 :
3471 : /* Match a kind specification. Since kinds are generally optional, we
3472 : usually return MATCH_NO if something goes wrong. If a "kind="
3473 : string is found, then we know we have an error. */
3474 :
3475 : match
3476 160874 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3477 : {
3478 160874 : locus where, loc;
3479 160874 : gfc_expr *e;
3480 160874 : match m, n;
3481 160874 : char c;
3482 :
3483 160874 : m = MATCH_NO;
3484 160874 : n = MATCH_YES;
3485 160874 : e = NULL;
3486 160874 : saved_kind_expr = NULL;
3487 :
3488 160874 : where = loc = gfc_current_locus;
3489 :
3490 160874 : if (kind_expr_only)
3491 0 : goto kind_expr;
3492 :
3493 160874 : if (gfc_match_char ('(') == MATCH_NO)
3494 : return MATCH_NO;
3495 :
3496 : /* Also gobbles optional text. */
3497 51627 : if (gfc_match (" kind = ") == MATCH_YES)
3498 51627 : m = MATCH_ERROR;
3499 :
3500 51627 : loc = gfc_current_locus;
3501 :
3502 51627 : kind_expr:
3503 :
3504 51627 : n = gfc_match_init_expr (&e);
3505 :
3506 51627 : if (gfc_derived_parameter_expr (e))
3507 : {
3508 166 : ts->kind = 0;
3509 166 : saved_kind_expr = gfc_copy_expr (e);
3510 166 : goto close_brackets;
3511 : }
3512 :
3513 51461 : if (n != MATCH_YES)
3514 : {
3515 465 : if (gfc_matching_function)
3516 : {
3517 : /* The function kind expression might include use associated or
3518 : imported parameters and try again after the specification
3519 : expressions..... */
3520 437 : if (gfc_match_char (')') != MATCH_YES)
3521 : {
3522 1 : gfc_error ("Missing right parenthesis at %C");
3523 1 : m = MATCH_ERROR;
3524 1 : goto no_match;
3525 : }
3526 :
3527 436 : gfc_free_expr (e);
3528 436 : gfc_undo_symbols ();
3529 436 : return MATCH_YES;
3530 : }
3531 : else
3532 : {
3533 : /* ....or else, the match is real. */
3534 28 : if (n == MATCH_NO)
3535 0 : gfc_error ("Expected initialization expression at %C");
3536 28 : if (n != MATCH_YES)
3537 28 : return MATCH_ERROR;
3538 : }
3539 : }
3540 :
3541 50996 : if (e->rank != 0)
3542 : {
3543 0 : gfc_error ("Expected scalar initialization expression at %C");
3544 0 : m = MATCH_ERROR;
3545 0 : goto no_match;
3546 : }
3547 :
3548 50996 : if (gfc_extract_int (e, &ts->kind, 1))
3549 : {
3550 0 : m = MATCH_ERROR;
3551 0 : goto no_match;
3552 : }
3553 :
3554 : /* Before throwing away the expression, let's see if we had a
3555 : C interoperable kind (and store the fact). */
3556 50996 : if (e->ts.is_c_interop == 1)
3557 : {
3558 : /* Mark this as C interoperable if being declared with one
3559 : of the named constants from iso_c_binding. */
3560 18867 : ts->is_c_interop = e->ts.is_iso_c;
3561 18867 : ts->f90_type = e->ts.f90_type;
3562 18867 : if (e->symtree)
3563 18866 : ts->interop_kind = e->symtree->n.sym;
3564 : }
3565 :
3566 50996 : gfc_free_expr (e);
3567 50996 : e = NULL;
3568 :
3569 : /* Ignore errors to this point, if we've gotten here. This means
3570 : we ignore the m=MATCH_ERROR from above. */
3571 50996 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3572 : {
3573 7 : gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3574 : gfc_basic_typename (ts->type));
3575 7 : gfc_current_locus = where;
3576 7 : return MATCH_ERROR;
3577 : }
3578 :
3579 : /* Warn if, e.g., c_int is used for a REAL variable, but not
3580 : if, e.g., c_double is used for COMPLEX as the standard
3581 : explicitly says that the kind type parameter for complex and real
3582 : variable is the same, i.e. c_float == c_float_complex. */
3583 50989 : if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3584 17 : && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3585 1 : || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3586 13 : gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3587 : "is %s", gfc_basic_typename (ts->f90_type), &where,
3588 : gfc_basic_typename (ts->type));
3589 :
3590 50976 : close_brackets:
3591 :
3592 51155 : gfc_gobble_whitespace ();
3593 51155 : if ((c = gfc_next_ascii_char ()) != ')'
3594 51155 : && (ts->type != BT_CHARACTER || c != ','))
3595 : {
3596 0 : if (ts->type == BT_CHARACTER)
3597 0 : gfc_error ("Missing right parenthesis or comma at %C");
3598 : else
3599 0 : gfc_error ("Missing right parenthesis at %C");
3600 0 : m = MATCH_ERROR;
3601 0 : goto no_match;
3602 : }
3603 : else
3604 : /* All tests passed. */
3605 51155 : m = MATCH_YES;
3606 :
3607 51155 : if(m == MATCH_ERROR)
3608 : gfc_current_locus = where;
3609 :
3610 51155 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3611 0 : ts->kind = 8;
3612 :
3613 51155 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3614 : {
3615 14419 : if (ts->kind == 4)
3616 : {
3617 4605 : if (flag_real4_kind == 8)
3618 54 : ts->kind = 8;
3619 4605 : if (flag_real4_kind == 10)
3620 54 : ts->kind = 10;
3621 4605 : if (flag_real4_kind == 16)
3622 54 : ts->kind = 16;
3623 : }
3624 9814 : else if (ts->kind == 8)
3625 : {
3626 6658 : if (flag_real8_kind == 4)
3627 48 : ts->kind = 4;
3628 6658 : if (flag_real8_kind == 10)
3629 48 : ts->kind = 10;
3630 6658 : if (flag_real8_kind == 16)
3631 48 : ts->kind = 16;
3632 : }
3633 : }
3634 :
3635 : /* Return what we know from the test(s). */
3636 : return m;
3637 :
3638 1 : no_match:
3639 1 : gfc_free_expr (e);
3640 1 : gfc_current_locus = where;
3641 1 : return m;
3642 : }
3643 :
3644 :
3645 : static match
3646 4870 : match_char_kind (int * kind, int * is_iso_c)
3647 : {
3648 4870 : locus where;
3649 4870 : gfc_expr *e;
3650 4870 : match m, n;
3651 4870 : bool fail;
3652 :
3653 4870 : m = MATCH_NO;
3654 4870 : e = NULL;
3655 4870 : where = gfc_current_locus;
3656 :
3657 4870 : n = gfc_match_init_expr (&e);
3658 :
3659 4870 : if (n != MATCH_YES && gfc_matching_function)
3660 : {
3661 : /* The expression might include use-associated or imported
3662 : parameters and try again after the specification
3663 : expressions. */
3664 7 : gfc_free_expr (e);
3665 7 : gfc_undo_symbols ();
3666 7 : return MATCH_YES;
3667 : }
3668 :
3669 7 : if (n == MATCH_NO)
3670 2 : gfc_error ("Expected initialization expression at %C");
3671 4863 : if (n != MATCH_YES)
3672 : return MATCH_ERROR;
3673 :
3674 4856 : if (e->rank != 0)
3675 : {
3676 0 : gfc_error ("Expected scalar initialization expression at %C");
3677 0 : m = MATCH_ERROR;
3678 0 : goto no_match;
3679 : }
3680 :
3681 4856 : if (gfc_derived_parameter_expr (e))
3682 : {
3683 14 : saved_kind_expr = e;
3684 14 : *kind = 0;
3685 14 : return MATCH_YES;
3686 : }
3687 :
3688 4842 : fail = gfc_extract_int (e, kind, 1);
3689 4842 : *is_iso_c = e->ts.is_iso_c;
3690 4842 : if (fail)
3691 : {
3692 0 : m = MATCH_ERROR;
3693 0 : goto no_match;
3694 : }
3695 :
3696 4842 : gfc_free_expr (e);
3697 :
3698 : /* Ignore errors to this point, if we've gotten here. This means
3699 : we ignore the m=MATCH_ERROR from above. */
3700 4842 : if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3701 : {
3702 14 : gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3703 14 : m = MATCH_ERROR;
3704 : }
3705 : else
3706 : /* All tests passed. */
3707 : m = MATCH_YES;
3708 :
3709 14 : if (m == MATCH_ERROR)
3710 14 : gfc_current_locus = where;
3711 :
3712 : /* Return what we know from the test(s). */
3713 : return m;
3714 :
3715 0 : no_match:
3716 0 : gfc_free_expr (e);
3717 0 : gfc_current_locus = where;
3718 0 : return m;
3719 : }
3720 :
3721 :
3722 : /* Match the various kind/length specifications in a CHARACTER
3723 : declaration. We don't return MATCH_NO. */
3724 :
3725 : match
3726 32006 : gfc_match_char_spec (gfc_typespec *ts)
3727 : {
3728 32006 : int kind, seen_length, is_iso_c;
3729 32006 : gfc_charlen *cl;
3730 32006 : gfc_expr *len;
3731 32006 : match m;
3732 32006 : bool deferred;
3733 :
3734 32006 : len = NULL;
3735 32006 : seen_length = 0;
3736 32006 : kind = 0;
3737 32006 : is_iso_c = 0;
3738 32006 : deferred = false;
3739 :
3740 : /* Try the old-style specification first. */
3741 32006 : old_char_selector = 0;
3742 :
3743 32006 : m = match_char_length (&len, &deferred, true);
3744 32006 : if (m != MATCH_NO)
3745 : {
3746 2205 : if (m == MATCH_YES)
3747 2205 : old_char_selector = 1;
3748 2205 : seen_length = 1;
3749 2205 : goto done;
3750 : }
3751 :
3752 29801 : m = gfc_match_char ('(');
3753 29801 : if (m != MATCH_YES)
3754 : {
3755 1916 : m = MATCH_YES; /* Character without length is a single char. */
3756 1916 : goto done;
3757 : }
3758 :
3759 : /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3760 27885 : if (gfc_match (" kind =") == MATCH_YES)
3761 : {
3762 3391 : m = match_char_kind (&kind, &is_iso_c);
3763 :
3764 3391 : if (m == MATCH_ERROR)
3765 16 : goto done;
3766 3375 : if (m == MATCH_NO)
3767 : goto syntax;
3768 :
3769 3375 : if (gfc_match (" , len =") == MATCH_NO)
3770 518 : goto rparen;
3771 :
3772 2857 : m = char_len_param_value (&len, &deferred);
3773 2857 : if (m == MATCH_NO)
3774 0 : goto syntax;
3775 2857 : if (m == MATCH_ERROR)
3776 2 : goto done;
3777 2855 : seen_length = 1;
3778 :
3779 2855 : goto rparen;
3780 : }
3781 :
3782 : /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3783 24494 : if (gfc_match (" len =") == MATCH_YES)
3784 : {
3785 13963 : m = char_len_param_value (&len, &deferred);
3786 13963 : if (m == MATCH_NO)
3787 2 : goto syntax;
3788 13961 : if (m == MATCH_ERROR)
3789 8 : goto done;
3790 13953 : seen_length = 1;
3791 :
3792 13953 : if (gfc_match_char (')') == MATCH_YES)
3793 12648 : goto done;
3794 :
3795 1305 : if (gfc_match (" , kind =") != MATCH_YES)
3796 0 : goto syntax;
3797 :
3798 1305 : if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3799 2 : goto done;
3800 :
3801 1303 : goto rparen;
3802 : }
3803 :
3804 : /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3805 10531 : m = char_len_param_value (&len, &deferred);
3806 10531 : if (m == MATCH_NO)
3807 0 : goto syntax;
3808 10531 : if (m == MATCH_ERROR)
3809 44 : goto done;
3810 10487 : seen_length = 1;
3811 :
3812 10487 : m = gfc_match_char (')');
3813 10487 : if (m == MATCH_YES)
3814 10311 : goto done;
3815 :
3816 176 : if (gfc_match_char (',') != MATCH_YES)
3817 2 : goto syntax;
3818 :
3819 174 : gfc_match (" kind ="); /* Gobble optional text. */
3820 :
3821 174 : m = match_char_kind (&kind, &is_iso_c);
3822 174 : if (m == MATCH_ERROR)
3823 3 : goto done;
3824 : if (m == MATCH_NO)
3825 : goto syntax;
3826 :
3827 4847 : rparen:
3828 : /* Require a right-paren at this point. */
3829 4847 : m = gfc_match_char (')');
3830 4847 : if (m == MATCH_YES)
3831 4847 : goto done;
3832 :
3833 0 : syntax:
3834 4 : gfc_error ("Syntax error in CHARACTER declaration at %C");
3835 4 : m = MATCH_ERROR;
3836 4 : gfc_free_expr (len);
3837 4 : return m;
3838 :
3839 32002 : done:
3840 : /* Deal with character functions after USE and IMPORT statements. */
3841 32002 : if (gfc_matching_function)
3842 : {
3843 1431 : gfc_free_expr (len);
3844 1431 : gfc_undo_symbols ();
3845 1431 : return MATCH_YES;
3846 : }
3847 :
3848 30571 : if (m != MATCH_YES)
3849 : {
3850 65 : gfc_free_expr (len);
3851 65 : return m;
3852 : }
3853 :
3854 : /* Do some final massaging of the length values. */
3855 30506 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3856 :
3857 30506 : if (seen_length == 0)
3858 2382 : cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3859 : else
3860 : {
3861 : /* If gfortran ends up here, then len may be reducible to a constant.
3862 : Try to do that here. If it does not reduce, simply assign len to
3863 : charlen. A complication occurs with user-defined generic functions,
3864 : which are not resolved. Use a private namespace to deal with
3865 : generic functions. */
3866 :
3867 28124 : if (len && len->expr_type != EXPR_CONSTANT)
3868 : {
3869 3053 : gfc_namespace *old_ns;
3870 3053 : gfc_expr *e;
3871 :
3872 3053 : old_ns = gfc_current_ns;
3873 3053 : gfc_current_ns = gfc_get_namespace (NULL, 0);
3874 :
3875 3053 : e = gfc_copy_expr (len);
3876 3053 : gfc_push_suppress_errors ();
3877 3053 : gfc_reduce_init_expr (e);
3878 3053 : gfc_pop_suppress_errors ();
3879 3053 : if (e->expr_type == EXPR_CONSTANT)
3880 : {
3881 294 : gfc_replace_expr (len, e);
3882 294 : if (mpz_cmp_si (len->value.integer, 0) < 0)
3883 7 : mpz_set_ui (len->value.integer, 0);
3884 : }
3885 : else
3886 2759 : gfc_free_expr (e);
3887 :
3888 3053 : gfc_free_namespace (gfc_current_ns);
3889 3053 : gfc_current_ns = old_ns;
3890 : }
3891 :
3892 28124 : cl->length = len;
3893 : }
3894 :
3895 30506 : ts->u.cl = cl;
3896 30506 : ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3897 30506 : ts->deferred = deferred;
3898 :
3899 : /* We have to know if it was a C interoperable kind so we can
3900 : do accurate type checking of bind(c) procs, etc. */
3901 30506 : if (kind != 0)
3902 : /* Mark this as C interoperable if being declared with one
3903 : of the named constants from iso_c_binding. */
3904 4753 : ts->is_c_interop = is_iso_c;
3905 25753 : else if (len != NULL)
3906 : /* Here, we might have parsed something such as: character(c_char)
3907 : In this case, the parsing code above grabs the c_char when
3908 : looking for the length (line 1690, roughly). it's the last
3909 : testcase for parsing the kind params of a character variable.
3910 : However, it's not actually the length. this seems like it
3911 : could be an error.
3912 : To see if the user used a C interop kind, test the expr
3913 : of the so called length, and see if it's C interoperable. */
3914 16585 : ts->is_c_interop = len->ts.is_iso_c;
3915 :
3916 : return MATCH_YES;
3917 : }
3918 :
3919 :
3920 : /* Matches a RECORD declaration. */
3921 :
3922 : static match
3923 968198 : match_record_decl (char *name)
3924 : {
3925 968198 : locus old_loc;
3926 968198 : old_loc = gfc_current_locus;
3927 968198 : match m;
3928 :
3929 968198 : m = gfc_match (" record /");
3930 968198 : if (m == MATCH_YES)
3931 : {
3932 353 : if (!flag_dec_structure)
3933 : {
3934 6 : gfc_current_locus = old_loc;
3935 6 : gfc_error ("RECORD at %C is an extension, enable it with "
3936 : "%<-fdec-structure%>");
3937 6 : return MATCH_ERROR;
3938 : }
3939 347 : m = gfc_match (" %n/", name);
3940 347 : if (m == MATCH_YES)
3941 : return MATCH_YES;
3942 : }
3943 :
3944 967848 : gfc_current_locus = old_loc;
3945 967848 : if (flag_dec_structure
3946 967848 : && (gfc_match (" record% ") == MATCH_YES
3947 8026 : || gfc_match (" record%t") == MATCH_YES))
3948 6 : gfc_error ("Structure name expected after RECORD at %C");
3949 967848 : if (m == MATCH_NO)
3950 : return MATCH_NO;
3951 :
3952 : return MATCH_ERROR;
3953 : }
3954 :
3955 :
3956 : /* In parsing a PDT, it is possible that one of the type parameters has the
3957 : same name as a previously declared symbol that is not a type parameter.
3958 : Intercept this now by looking for the symtree in f2k_derived. */
3959 :
3960 : static bool
3961 880 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
3962 : {
3963 880 : if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
3964 : return false;
3965 :
3966 711 : if (!(e->symtree->n.sym->attr.pdt_len
3967 122 : || e->symtree->n.sym->attr.pdt_kind))
3968 : {
3969 38 : gfc_symtree *st;
3970 38 : st = gfc_find_symtree (pdt->f2k_derived->sym_root,
3971 : e->symtree->n.sym->name);
3972 38 : if (st && st->n.sym
3973 30 : && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
3974 : {
3975 30 : gfc_expr *new_expr;
3976 30 : gfc_set_sym_referenced (st->n.sym);
3977 30 : new_expr = gfc_get_expr ();
3978 30 : new_expr->ts = st->n.sym->ts;
3979 30 : new_expr->expr_type = EXPR_VARIABLE;
3980 30 : new_expr->symtree = st;
3981 30 : new_expr->where = e->where;
3982 30 : gfc_replace_expr (e, new_expr);
3983 : }
3984 : }
3985 :
3986 : return false;
3987 : }
3988 :
3989 :
3990 : void
3991 648 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
3992 : {
3993 648 : if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
3994 : return;
3995 617 : gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
3996 : }
3997 :
3998 : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3999 : of expressions to substitute into the possibly parameterized expression
4000 : 'e'. Using a list is inefficient but should not be too bad since the
4001 : number of type parameters is not likely to be large. */
4002 : static bool
4003 3205 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
4004 : int* f)
4005 : {
4006 3205 : gfc_actual_arglist *param;
4007 3205 : gfc_expr *copy;
4008 :
4009 3205 : if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
4010 : return false;
4011 :
4012 1429 : gcc_assert (e->symtree);
4013 1429 : if (e->symtree->n.sym->attr.pdt_kind
4014 1050 : || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
4015 513 : || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
4016 : {
4017 1414 : for (param = type_param_spec_list; param; param = param->next)
4018 1366 : if (!strcmp (e->symtree->n.sym->name, param->name))
4019 : break;
4020 :
4021 963 : if (param && param->expr)
4022 : {
4023 914 : copy = gfc_copy_expr (param->expr);
4024 914 : gfc_replace_expr (e, copy);
4025 : /* Catch variables declared without a value expression. */
4026 914 : if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
4027 21 : e->ts = e->symtree->n.sym->ts;
4028 : }
4029 : }
4030 :
4031 : return false;
4032 : }
4033 :
4034 :
4035 : static bool
4036 953 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
4037 : {
4038 953 : return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
4039 : }
4040 :
4041 :
4042 : bool
4043 1803 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
4044 : {
4045 1803 : gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
4046 1803 : type_param_spec_list = param_list;
4047 1803 : bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
4048 1803 : type_param_spec_list = old_param_spec_list;
4049 1803 : return res;
4050 : }
4051 :
4052 : /* Determines the instance of a parameterized derived type to be used by
4053 : matching determining the values of the kind parameters and using them
4054 : in the name of the instance. If the instance exists, it is used, otherwise
4055 : a new derived type is created. */
4056 : match
4057 2795 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
4058 : gfc_actual_arglist **ext_param_list)
4059 : {
4060 : /* The PDT template symbol. */
4061 2795 : gfc_symbol *pdt = *sym;
4062 : /* The symbol for the parameter in the template f2k_namespace. */
4063 2795 : gfc_symbol *param;
4064 : /* The hoped for instance of the PDT. */
4065 2795 : gfc_symbol *instance = NULL;
4066 : /* The list of parameters appearing in the PDT declaration. */
4067 2795 : gfc_formal_arglist *type_param_name_list;
4068 : /* Used to store the parameter specification list during recursive calls. */
4069 2795 : gfc_actual_arglist *old_param_spec_list;
4070 : /* Pointers to the parameter specification being used. */
4071 2795 : gfc_actual_arglist *actual_param;
4072 2795 : gfc_actual_arglist *tail = NULL;
4073 : /* Used to build up the name of the PDT instance. */
4074 2795 : char *name;
4075 2795 : bool name_seen = (param_list == NULL);
4076 2795 : bool assumed_seen = false;
4077 2795 : bool deferred_seen = false;
4078 2795 : bool spec_error = false;
4079 2795 : bool alloc_seen = false;
4080 2795 : bool ptr_seen = false;
4081 2795 : int i;
4082 2795 : gfc_expr *kind_expr;
4083 2795 : gfc_component *c1, *c2;
4084 2795 : match m;
4085 2795 : gfc_symtree *s = NULL;
4086 :
4087 2795 : type_param_spec_list = NULL;
4088 :
4089 2795 : type_param_name_list = pdt->formal;
4090 2795 : actual_param = param_list;
4091 :
4092 : /* Prevent a PDT component of the same type as the template from being
4093 : converted into an instance. Doing this results in the component being
4094 : lost. */
4095 2795 : if (gfc_current_state () == COMP_DERIVED
4096 113 : && !(gfc_state_stack->previous
4097 113 : && gfc_state_stack->previous->state == COMP_DERIVED)
4098 113 : && gfc_current_block ()->attr.pdt_template)
4099 : {
4100 100 : if (ext_param_list)
4101 100 : *ext_param_list = gfc_copy_actual_arglist (param_list);
4102 100 : return MATCH_YES;
4103 : }
4104 :
4105 2695 : name = xasprintf ("%s%s", PDT_PREFIX, pdt->name);
4106 :
4107 : /* Run through the parameter name list and pick up the actual
4108 : parameter values or use the default values in the PDT declaration. */
4109 6265 : for (; type_param_name_list;
4110 3570 : type_param_name_list = type_param_name_list->next)
4111 : {
4112 3638 : if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
4113 : {
4114 3236 : if (actual_param->spec_type == SPEC_ASSUMED)
4115 : spec_error = deferred_seen;
4116 : else
4117 3236 : spec_error = assumed_seen;
4118 :
4119 3236 : if (spec_error)
4120 : {
4121 : gfc_error ("The type parameter spec list at %C cannot contain "
4122 : "both ASSUMED and DEFERRED parameters");
4123 : goto error_return;
4124 : }
4125 : }
4126 :
4127 3236 : if (actual_param && actual_param->name)
4128 3638 : name_seen = true;
4129 3638 : param = type_param_name_list->sym;
4130 :
4131 3638 : if (!param || !param->name)
4132 2 : continue;
4133 :
4134 3636 : c1 = gfc_find_component (pdt, param->name, false, true, NULL);
4135 : /* An error should already have been thrown in resolve.cc
4136 : (resolve_fl_derived0). */
4137 3636 : if (!pdt->attr.use_assoc && !c1)
4138 8 : goto error_return;
4139 :
4140 : /* Resolution PDT class components of derived types are handled here.
4141 : They can arrive without a parameter list and no KIND parameters. */
4142 3628 : if (!param_list && (!c1->attr.pdt_kind && !c1->initializer))
4143 20 : continue;
4144 :
4145 3608 : kind_expr = NULL;
4146 3608 : if (!name_seen)
4147 : {
4148 2116 : if (!actual_param && !(c1 && c1->initializer))
4149 : {
4150 2 : gfc_error ("The type parameter spec list at %C does not contain "
4151 : "enough parameter expressions");
4152 2 : goto error_return;
4153 : }
4154 2114 : else if (!actual_param && c1 && c1->initializer)
4155 5 : kind_expr = gfc_copy_expr (c1->initializer);
4156 2109 : else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4157 1866 : kind_expr = gfc_copy_expr (actual_param->expr);
4158 : }
4159 : else
4160 : {
4161 : actual_param = param_list;
4162 1952 : for (;actual_param; actual_param = actual_param->next)
4163 1562 : if (actual_param->name
4164 1542 : && strcmp (actual_param->name, param->name) == 0)
4165 : break;
4166 1492 : if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4167 935 : kind_expr = gfc_copy_expr (actual_param->expr);
4168 : else
4169 : {
4170 557 : if (c1->initializer)
4171 493 : kind_expr = gfc_copy_expr (c1->initializer);
4172 64 : else if (!(actual_param && param->attr.pdt_len))
4173 : {
4174 9 : gfc_error ("The derived parameter %qs at %C does not "
4175 : "have a default value", param->name);
4176 9 : goto error_return;
4177 : }
4178 : }
4179 : }
4180 :
4181 3299 : if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
4182 282 : && kind_expr->ts.type != BT_INTEGER
4183 136 : && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
4184 : {
4185 12 : gfc_error ("The type parameter expression at %L must be of INTEGER "
4186 : "type and not %s", &kind_expr->where,
4187 : gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
4188 12 : goto error_return;
4189 : }
4190 :
4191 : /* Store the current parameter expressions in a temporary actual
4192 : arglist 'list' so that they can be substituted in the corresponding
4193 : expressions in the PDT instance. */
4194 3585 : if (type_param_spec_list == NULL)
4195 : {
4196 2652 : type_param_spec_list = gfc_get_actual_arglist ();
4197 2652 : tail = type_param_spec_list;
4198 : }
4199 : else
4200 : {
4201 933 : tail->next = gfc_get_actual_arglist ();
4202 933 : tail = tail->next;
4203 : }
4204 3585 : tail->name = param->name;
4205 :
4206 3585 : if (kind_expr)
4207 : {
4208 : /* Try simplification even for LEN expressions. */
4209 3287 : bool ok;
4210 3287 : gfc_resolve_expr (kind_expr);
4211 :
4212 3287 : if (c1->attr.pdt_kind
4213 1646 : && kind_expr->expr_type != EXPR_CONSTANT
4214 28 : && type_param_spec_list)
4215 28 : gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
4216 :
4217 3287 : ok = gfc_simplify_expr (kind_expr, 1);
4218 : /* Variable expressions default to BT_PROCEDURE in the absence of an
4219 : initializer so allow for this. */
4220 3287 : if (kind_expr->ts.type != BT_INTEGER
4221 153 : && kind_expr->ts.type != BT_PROCEDURE)
4222 : {
4223 29 : gfc_error ("The parameter expression at %C must be of "
4224 : "INTEGER type and not %s type",
4225 : gfc_basic_typename (kind_expr->ts.type));
4226 29 : goto error_return;
4227 : }
4228 3258 : if (kind_expr->ts.type == BT_INTEGER && !ok)
4229 : {
4230 4 : gfc_error ("The parameter expression at %C does not "
4231 : "simplify to an INTEGER constant");
4232 4 : goto error_return;
4233 : }
4234 :
4235 3254 : tail->expr = gfc_copy_expr (kind_expr);
4236 : }
4237 :
4238 3552 : if (actual_param)
4239 3164 : tail->spec_type = actual_param->spec_type;
4240 :
4241 3552 : if (!param->attr.pdt_kind)
4242 : {
4243 1931 : if (!name_seen && actual_param)
4244 1162 : actual_param = actual_param->next;
4245 1931 : if (kind_expr)
4246 : {
4247 1635 : gfc_free_expr (kind_expr);
4248 1635 : kind_expr = NULL;
4249 : }
4250 1931 : continue;
4251 : }
4252 :
4253 1621 : if (actual_param
4254 1277 : && (actual_param->spec_type == SPEC_ASSUMED
4255 1277 : || actual_param->spec_type == SPEC_DEFERRED))
4256 : {
4257 2 : gfc_error ("The KIND parameter %qs at %C cannot either be "
4258 : "ASSUMED or DEFERRED", param->name);
4259 2 : goto error_return;
4260 : }
4261 :
4262 1619 : if (!kind_expr || !gfc_is_constant_expr (kind_expr))
4263 : {
4264 2 : gfc_error ("The value for the KIND parameter %qs at %C does not "
4265 : "reduce to a constant expression", param->name);
4266 2 : goto error_return;
4267 : }
4268 :
4269 : /* This can come about during the parsing of nested pdt_templates. An
4270 : error arises because the KIND parameter expression has not been
4271 : provided. Use the template instead of an incorrect instance. */
4272 1617 : if (kind_expr->expr_type != EXPR_CONSTANT
4273 1617 : || kind_expr->ts.type != BT_INTEGER)
4274 : {
4275 0 : gfc_free_actual_arglist (type_param_spec_list);
4276 0 : free (name);
4277 0 : return MATCH_YES;
4278 : }
4279 :
4280 1617 : char *kind_value = mpz_get_str (NULL, 10, kind_expr->value.integer);
4281 1617 : char *old_name = name;
4282 1617 : name = xasprintf ("%s_%s", old_name, kind_value);
4283 1617 : free (old_name);
4284 1617 : free (kind_value);
4285 :
4286 1617 : if (!name_seen && actual_param)
4287 898 : actual_param = actual_param->next;
4288 1617 : gfc_free_expr (kind_expr);
4289 : }
4290 :
4291 2627 : if (!name_seen && actual_param)
4292 : {
4293 2 : gfc_error ("The type parameter spec list at %C contains too many "
4294 : "parameter expressions");
4295 2 : goto error_return;
4296 : }
4297 :
4298 : /* Now we search for the PDT instance 'name'. If it doesn't exist, we
4299 : build it, using 'pdt' as a template. */
4300 2625 : if (gfc_get_symbol (name, pdt->ns, &instance))
4301 : {
4302 0 : gfc_error ("Parameterized derived type at %C is ambiguous");
4303 0 : goto error_return;
4304 : }
4305 :
4306 : /* If we are in an interface body, the instance will not have been imported.
4307 : Make sure that it is imported implicitly. */
4308 2625 : s = gfc_find_symtree (gfc_current_ns->sym_root, pdt->name);
4309 2625 : if (gfc_current_ns->proc_name
4310 2578 : && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
4311 93 : && s && s->import_only && pdt->attr.imported)
4312 : {
4313 2 : s = gfc_find_symtree (gfc_current_ns->sym_root, instance->name);
4314 2 : if (!s)
4315 : {
4316 1 : gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false,
4317 : &gfc_current_locus);
4318 1 : s->n.sym = instance;
4319 : }
4320 2 : s->n.sym->attr.imported = 1;
4321 2 : s->import_only = 1;
4322 : }
4323 :
4324 2625 : m = MATCH_YES;
4325 :
4326 2625 : if (instance->attr.flavor == FL_DERIVED
4327 2086 : && instance->attr.pdt_type
4328 2086 : && instance->components)
4329 : {
4330 2086 : instance->refs++;
4331 2086 : if (ext_param_list)
4332 990 : *ext_param_list = type_param_spec_list;
4333 2086 : *sym = instance;
4334 2086 : gfc_commit_symbols ();
4335 2086 : free (name);
4336 2086 : return m;
4337 : }
4338 :
4339 : /* Start building the new instance of the parameterized type. */
4340 539 : gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
4341 539 : if (pdt->attr.use_assoc)
4342 60 : instance->module = pdt->module;
4343 539 : instance->attr.pdt_template = 0;
4344 539 : instance->attr.pdt_type = 1;
4345 539 : instance->declared_at = gfc_current_locus;
4346 :
4347 : /* In resolution, the finalizers are copied, according to the type of the
4348 : argument, to the instance finalizers. However, they are retained by the
4349 : template and procedures are freed there. */
4350 539 : if (pdt->f2k_derived && pdt->f2k_derived->finalizers)
4351 : {
4352 24 : instance->f2k_derived = gfc_get_namespace (NULL, 0);
4353 24 : instance->template_sym = pdt;
4354 24 : *instance->f2k_derived = *pdt->f2k_derived;
4355 : }
4356 :
4357 : /* Add the components, replacing the parameters in all expressions
4358 : with the expressions for their values in 'type_param_spec_list'. */
4359 539 : c1 = pdt->components;
4360 539 : tail = type_param_spec_list;
4361 1972 : for (; c1; c1 = c1->next)
4362 : {
4363 1435 : gfc_add_component (instance, c1->name, &c2);
4364 :
4365 1435 : c2->ts = c1->ts;
4366 1435 : c2->attr = c1->attr;
4367 1435 : if (c1->tb)
4368 : {
4369 6 : c2->tb = gfc_get_tbp ();
4370 6 : *c2->tb = *c1->tb;
4371 : }
4372 :
4373 : /* The order of declaration of the type_specs might not be the
4374 : same as that of the components. */
4375 1435 : if (c1->attr.pdt_kind || c1->attr.pdt_len)
4376 : {
4377 1010 : for (tail = type_param_spec_list; tail; tail = tail->next)
4378 1006 : if (strcmp (c1->name, tail->name) == 0)
4379 : break;
4380 : }
4381 :
4382 : /* Deal with type extension by recursively calling this function
4383 : to obtain the instance of the extended type. */
4384 1435 : if (gfc_current_state () != COMP_DERIVED
4385 1421 : && c1 == pdt->components
4386 526 : && c1->ts.type == BT_DERIVED
4387 48 : && c1->ts.u.derived
4388 1483 : && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
4389 : {
4390 48 : if (c1->ts.u.derived->attr.pdt_template)
4391 : {
4392 41 : gfc_formal_arglist *f;
4393 :
4394 41 : old_param_spec_list = type_param_spec_list;
4395 :
4396 : /* Obtain a spec list appropriate to the extended type..*/
4397 41 : actual_param = gfc_copy_actual_arglist (type_param_spec_list);
4398 41 : type_param_spec_list = actual_param;
4399 73 : for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
4400 32 : actual_param = actual_param->next;
4401 41 : if (actual_param)
4402 : {
4403 41 : gfc_free_actual_arglist (actual_param->next);
4404 41 : actual_param->next = NULL;
4405 : }
4406 :
4407 : /* Now obtain the PDT instance for the extended type. */
4408 41 : c2->param_list = type_param_spec_list;
4409 41 : m = gfc_get_pdt_instance (type_param_spec_list,
4410 : &c2->ts.u.derived,
4411 : &c2->param_list);
4412 41 : type_param_spec_list = old_param_spec_list;
4413 : }
4414 : else
4415 7 : c2->ts = c1->ts;
4416 :
4417 48 : c2->ts.u.derived->refs++;
4418 48 : gfc_set_sym_referenced (c2->ts.u.derived);
4419 :
4420 : /* If the component is allocatable or the parent has allocatable
4421 : components, make sure that the new instance also is marked as
4422 : having allocatable components. */
4423 48 : if (c2->attr.allocatable || c2->ts.u.derived->attr.alloc_comp)
4424 6 : instance->attr.alloc_comp = 1;
4425 :
4426 : /* Set extension level. */
4427 48 : if (c2->ts.u.derived->attr.extension == 255)
4428 : {
4429 : /* Since the extension field is 8 bit wide, we can only have
4430 : up to 255 extension levels. */
4431 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
4432 : c2->ts.u.derived->name,
4433 : &c2->ts.u.derived->declared_at);
4434 0 : goto error_return;
4435 : }
4436 48 : instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
4437 :
4438 48 : continue;
4439 48 : }
4440 :
4441 : /* Addressing PR82943, this will fix the issue where a function or
4442 : subroutine is declared as not a member of the PDT instance.
4443 : The reason for this is because the PDT instance did not have access
4444 : to its template's f2k_derived namespace in order to find the
4445 : typebound procedures.
4446 :
4447 : The number of references to the PDT template's f2k_derived will
4448 : ensure that f2k_derived is properly freed later on. */
4449 :
4450 1387 : if (!instance->f2k_derived && pdt->f2k_derived)
4451 : {
4452 508 : instance->f2k_derived = pdt->f2k_derived;
4453 508 : instance->f2k_derived->refs++;
4454 : }
4455 :
4456 : /* Set the component kind using the parameterized expression. */
4457 1387 : if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
4458 471 : && c1->kind_expr != NULL)
4459 : {
4460 278 : gfc_expr *e = gfc_copy_expr (c1->kind_expr);
4461 278 : gfc_insert_kind_parameter_exprs (e);
4462 278 : gfc_simplify_expr (e, 1);
4463 278 : gfc_extract_int (e, &c2->ts.kind);
4464 278 : gfc_free_expr (e);
4465 278 : if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
4466 : {
4467 2 : gfc_error ("Kind %d not supported for type %s at %C",
4468 : c2->ts.kind, gfc_basic_typename (c2->ts.type));
4469 2 : goto error_return;
4470 : }
4471 276 : if (c2->attr.proc_pointer && c2->attr.function
4472 0 : && c1->ts.interface && c1->ts.interface->ts.kind == 0)
4473 : {
4474 0 : c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4475 0 : c2->ts.interface->result = c2->ts.interface;
4476 0 : c2->ts.interface->ts = c2->ts;
4477 0 : c2->ts.interface->attr.flavor = FL_PROCEDURE;
4478 0 : c2->ts.interface->attr.function = 1;
4479 0 : c2->attr.function = 1;
4480 0 : c2->attr.if_source = IFSRC_UNKNOWN;
4481 : }
4482 : }
4483 :
4484 : /* Set up either the KIND/LEN initializer, if constant,
4485 : or the parameterized expression. Use the template
4486 : initializer if one is not already set in this instance. */
4487 1385 : if (c2->attr.pdt_kind || c2->attr.pdt_len)
4488 : {
4489 718 : if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
4490 590 : c2->initializer = gfc_copy_expr (tail->expr);
4491 128 : else if (tail && tail->expr)
4492 : {
4493 10 : c2->param_list = gfc_get_actual_arglist ();
4494 10 : c2->param_list->name = tail->name;
4495 10 : c2->param_list->expr = gfc_copy_expr (tail->expr);
4496 10 : c2->param_list->next = NULL;
4497 : }
4498 :
4499 718 : if (!c2->initializer && c1->initializer)
4500 24 : c2->initializer = gfc_copy_expr (c1->initializer);
4501 :
4502 718 : if (c2->initializer)
4503 614 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4504 : }
4505 :
4506 : /* Copy the array spec. */
4507 1385 : c2->as = gfc_copy_array_spec (c1->as);
4508 1385 : if (c1->ts.type == BT_CLASS)
4509 0 : CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
4510 :
4511 1385 : if (c1->attr.allocatable)
4512 76 : alloc_seen = true;
4513 :
4514 1385 : if (c1->attr.pointer)
4515 20 : ptr_seen = true;
4516 :
4517 : /* Determine if an array spec is parameterized. If so, substitute
4518 : in the parameter expressions for the bounds and set the pdt_array
4519 : attribute. Notice that this attribute must be unconditionally set
4520 : if this is an array of parameterized character length. */
4521 1385 : if (c1->as && c1->as->type == AS_EXPLICIT)
4522 : {
4523 : bool pdt_array = false;
4524 514 : bool all_constant = true;
4525 :
4526 : /* Are the bounds of the array parameterized? */
4527 514 : for (i = 0; i < c1->as->rank; i++)
4528 : {
4529 305 : if (gfc_derived_parameter_expr (c1->as->lower[i]))
4530 6 : pdt_array = true;
4531 305 : if (gfc_derived_parameter_expr (c1->as->upper[i]))
4532 291 : pdt_array = true;
4533 : }
4534 :
4535 : /* If they are, free the expressions for the bounds and
4536 : replace them with the template expressions with substitute
4537 : values. */
4538 500 : for (i = 0; pdt_array && i < c1->as->rank; i++)
4539 : {
4540 291 : gfc_expr *e;
4541 291 : e = gfc_copy_expr (c1->as->lower[i]);
4542 291 : gfc_insert_kind_parameter_exprs (e);
4543 291 : if (gfc_simplify_expr (e, 1))
4544 291 : gfc_replace_expr (c2->as->lower[i], e);
4545 : else
4546 0 : gfc_free_expr (e);
4547 291 : if (c2->as->lower[i]->expr_type != EXPR_CONSTANT)
4548 6 : all_constant = false;
4549 291 : e = gfc_copy_expr (c1->as->upper[i]);
4550 291 : gfc_insert_kind_parameter_exprs (e);
4551 291 : if (gfc_simplify_expr (e, 1))
4552 291 : gfc_replace_expr (c2->as->upper[i], e);
4553 : else
4554 0 : gfc_free_expr (e);
4555 291 : if (c2->as->upper[i]->expr_type != EXPR_CONSTANT)
4556 289 : all_constant = false;
4557 : }
4558 :
4559 209 : c2->attr.pdt_array = all_constant ? 0 : 1;
4560 209 : if (c1->initializer)
4561 : {
4562 7 : c2->initializer = gfc_copy_expr (c1->initializer);
4563 7 : gfc_insert_kind_parameter_exprs (c2->initializer);
4564 7 : gfc_simplify_expr (c2->initializer, 1);
4565 : }
4566 : }
4567 :
4568 : /* Similarly, set the string length if parameterized. */
4569 1385 : if (c1->ts.type == BT_CHARACTER
4570 87 : && c1->ts.u.cl->length
4571 1471 : && gfc_derived_parameter_expr (c1->ts.u.cl->length))
4572 : {
4573 86 : gfc_expr *e;
4574 86 : e = gfc_copy_expr (c1->ts.u.cl->length);
4575 86 : gfc_insert_kind_parameter_exprs (e);
4576 86 : if (gfc_simplify_expr (e, 1))
4577 86 : gfc_replace_expr (c2->ts.u.cl->length, e);
4578 : else
4579 0 : gfc_free_expr (e);
4580 86 : if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
4581 83 : c2->attr.pdt_string = 1;
4582 : }
4583 :
4584 : /* Recurse into this function for PDT components. */
4585 1385 : if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
4586 131 : && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
4587 : {
4588 123 : gfc_actual_arglist *params;
4589 : /* The component in the template has a list of specification
4590 : expressions derived from its declaration. */
4591 123 : params = gfc_copy_actual_arglist (c1->param_list);
4592 123 : actual_param = params;
4593 : /* Substitute the template parameters with the expressions
4594 : from the specification list. */
4595 384 : for (;actual_param; actual_param = actual_param->next)
4596 : {
4597 138 : gfc_correct_parm_expr (pdt, &actual_param->expr);
4598 138 : gfc_insert_parameter_exprs (actual_param->expr,
4599 : type_param_spec_list);
4600 : }
4601 :
4602 : /* Now obtain the PDT instance for the component. */
4603 123 : old_param_spec_list = type_param_spec_list;
4604 246 : m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
4605 123 : &c2->param_list);
4606 123 : type_param_spec_list = old_param_spec_list;
4607 :
4608 123 : if (!(c2->attr.pointer || c2->attr.allocatable))
4609 : {
4610 83 : if (!c1->initializer
4611 58 : || c1->initializer->expr_type != EXPR_FUNCTION)
4612 82 : c2->initializer = gfc_default_initializer (&c2->ts);
4613 : else
4614 : {
4615 1 : gfc_symtree *s;
4616 1 : c2->initializer = gfc_copy_expr (c1->initializer);
4617 1 : s = gfc_find_symtree (pdt->ns->sym_root,
4618 1 : gfc_dt_lower_string (c2->ts.u.derived->name));
4619 1 : if (s)
4620 0 : c2->initializer->symtree = s;
4621 1 : c2->initializer->ts = c2->ts;
4622 1 : if (!s)
4623 1 : gfc_insert_parameter_exprs (c2->initializer,
4624 : type_param_spec_list);
4625 1 : gfc_simplify_expr (c2->initializer, 1);
4626 : }
4627 : }
4628 :
4629 123 : if (c2->attr.allocatable
4630 91 : || (c2->ts.type == BT_DERIVED && c2->ts.u.derived
4631 91 : && c2->ts.u.derived->attr.alloc_comp && !c2->attr.pointer))
4632 61 : instance->attr.alloc_comp = 1;
4633 : }
4634 1262 : else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
4635 461 : || c2->attr.pdt_array) && c1->initializer)
4636 : {
4637 32 : c2->initializer = gfc_copy_expr (c1->initializer);
4638 32 : if (c2->initializer->ts.type == BT_UNKNOWN)
4639 12 : c2->initializer->ts = c2->ts;
4640 32 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4641 : /* The template initializers are parsed using gfc_match_expr rather
4642 : than gfc_match_init_expr. Apply the missing reduction to the
4643 : PDT instance initializers. */
4644 32 : if (!gfc_reduce_init_expr (c2->initializer))
4645 : {
4646 0 : gfc_free_expr (c2->initializer);
4647 0 : goto error_return;
4648 : }
4649 32 : gfc_simplify_expr (c2->initializer, 1);
4650 : }
4651 : }
4652 :
4653 537 : if (alloc_seen)
4654 73 : instance->attr.alloc_comp = 1;
4655 537 : if (ptr_seen)
4656 20 : instance->attr.pointer_comp = 1;
4657 :
4658 :
4659 537 : gfc_commit_symbol (instance);
4660 537 : if (ext_param_list)
4661 330 : *ext_param_list = type_param_spec_list;
4662 537 : *sym = instance;
4663 537 : free (name);
4664 537 : return m;
4665 :
4666 72 : error_return:
4667 72 : gfc_free_actual_arglist (type_param_spec_list);
4668 72 : free (name);
4669 72 : return MATCH_ERROR;
4670 : }
4671 :
4672 :
4673 : /* Match a legacy nonstandard BYTE type-spec. */
4674 :
4675 : static match
4676 1191063 : match_byte_typespec (gfc_typespec *ts)
4677 : {
4678 1191063 : if (gfc_match (" byte") == MATCH_YES)
4679 : {
4680 33 : if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4681 : return MATCH_ERROR;
4682 :
4683 31 : if (gfc_current_form == FORM_FREE)
4684 : {
4685 19 : char c = gfc_peek_ascii_char ();
4686 19 : if (!gfc_is_whitespace (c) && c != ',')
4687 : return MATCH_NO;
4688 : }
4689 :
4690 29 : if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4691 : {
4692 0 : gfc_error ("BYTE type used at %C "
4693 : "is not available on the target machine");
4694 0 : return MATCH_ERROR;
4695 : }
4696 :
4697 29 : ts->type = BT_INTEGER;
4698 29 : ts->kind = 1;
4699 29 : return MATCH_YES;
4700 : }
4701 : return MATCH_NO;
4702 : }
4703 :
4704 :
4705 : /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
4706 : structure to the matched specification. This is necessary for FUNCTION and
4707 : IMPLICIT statements.
4708 :
4709 : If implicit_flag is nonzero, then we don't check for the optional
4710 : kind specification. Not doing so is needed for matching an IMPLICIT
4711 : statement correctly. */
4712 :
4713 : match
4714 1191063 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4715 : {
4716 : /* Provide sufficient space to hold "pdtsymbol". */
4717 1191063 : char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
4718 1191063 : gfc_symbol *sym, *dt_sym;
4719 1191063 : match m;
4720 1191063 : char c;
4721 1191063 : bool seen_deferred_kind, matched_type;
4722 1191063 : const char *dt_name;
4723 :
4724 1191063 : decl_type_param_list = NULL;
4725 :
4726 : /* A belt and braces check that the typespec is correctly being treated
4727 : as a deferred characteristic association. */
4728 2382126 : seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4729 84258 : && (gfc_current_block ()->result->ts.kind == -1)
4730 1202966 : && (ts->kind == -1);
4731 1191063 : gfc_clear_ts (ts);
4732 1191063 : if (seen_deferred_kind)
4733 9668 : ts->kind = -1;
4734 :
4735 : /* Clear the current binding label, in case one is given. */
4736 1191063 : curr_binding_label = NULL;
4737 :
4738 : /* Match BYTE type-spec. */
4739 1191063 : m = match_byte_typespec (ts);
4740 1191063 : if (m != MATCH_NO)
4741 : return m;
4742 :
4743 1191032 : m = gfc_match (" type (");
4744 1191032 : matched_type = (m == MATCH_YES);
4745 1191032 : if (matched_type)
4746 : {
4747 31574 : gfc_gobble_whitespace ();
4748 31574 : if (gfc_peek_ascii_char () == '*')
4749 : {
4750 5617 : if ((m = gfc_match ("* ) ")) != MATCH_YES)
4751 : return m;
4752 5617 : if (gfc_comp_struct (gfc_current_state ()))
4753 : {
4754 2 : gfc_error ("Assumed type at %C is not allowed for components");
4755 2 : return MATCH_ERROR;
4756 : }
4757 5615 : if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4758 : return MATCH_ERROR;
4759 5613 : ts->type = BT_ASSUMED;
4760 5613 : return MATCH_YES;
4761 : }
4762 :
4763 25957 : m = gfc_match ("%n", name);
4764 25957 : matched_type = (m == MATCH_YES);
4765 : }
4766 :
4767 25957 : if ((matched_type && strcmp ("integer", name) == 0)
4768 1185415 : || (!matched_type && gfc_match (" integer") == MATCH_YES))
4769 : {
4770 112452 : ts->type = BT_INTEGER;
4771 112452 : ts->kind = gfc_default_integer_kind;
4772 112452 : goto get_kind;
4773 : }
4774 :
4775 1072963 : if (flag_unsigned)
4776 : {
4777 0 : if ((matched_type && strcmp ("unsigned", name) == 0)
4778 22489 : || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
4779 : {
4780 1036 : ts->type = BT_UNSIGNED;
4781 1036 : ts->kind = gfc_default_integer_kind;
4782 1036 : goto get_kind;
4783 : }
4784 : }
4785 :
4786 25951 : if ((matched_type && strcmp ("character", name) == 0)
4787 1071927 : || (!matched_type && gfc_match (" character") == MATCH_YES))
4788 : {
4789 29044 : if (matched_type
4790 29044 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4791 : "intrinsic-type-spec at %C"))
4792 : return MATCH_ERROR;
4793 :
4794 29043 : ts->type = BT_CHARACTER;
4795 29043 : if (implicit_flag == 0)
4796 28937 : m = gfc_match_char_spec (ts);
4797 : else
4798 : m = MATCH_YES;
4799 :
4800 29043 : if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4801 : {
4802 1 : gfc_error ("Malformed type-spec at %C");
4803 1 : return MATCH_ERROR;
4804 : }
4805 :
4806 29042 : return m;
4807 : }
4808 :
4809 25947 : if ((matched_type && strcmp ("real", name) == 0)
4810 1042883 : || (!matched_type && gfc_match (" real") == MATCH_YES))
4811 : {
4812 30320 : ts->type = BT_REAL;
4813 30320 : ts->kind = gfc_default_real_kind;
4814 30320 : goto get_kind;
4815 : }
4816 :
4817 1012563 : if ((matched_type
4818 25944 : && (strcmp ("doubleprecision", name) == 0
4819 25943 : || (strcmp ("double", name) == 0
4820 5 : && gfc_match (" precision") == MATCH_YES)))
4821 1012563 : || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4822 : {
4823 2614 : if (matched_type
4824 2614 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4825 : "intrinsic-type-spec at %C"))
4826 : return MATCH_ERROR;
4827 :
4828 2613 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4829 : {
4830 2 : gfc_error ("Malformed type-spec at %C");
4831 2 : return MATCH_ERROR;
4832 : }
4833 :
4834 2611 : ts->type = BT_REAL;
4835 2611 : ts->kind = gfc_default_double_kind;
4836 2611 : return MATCH_YES;
4837 : }
4838 :
4839 25940 : if ((matched_type && strcmp ("complex", name) == 0)
4840 1009949 : || (!matched_type && gfc_match (" complex") == MATCH_YES))
4841 : {
4842 4057 : ts->type = BT_COMPLEX;
4843 4057 : ts->kind = gfc_default_complex_kind;
4844 4057 : goto get_kind;
4845 : }
4846 :
4847 1005892 : if ((matched_type
4848 25940 : && (strcmp ("doublecomplex", name) == 0
4849 25939 : || (strcmp ("double", name) == 0
4850 2 : && gfc_match (" complex") == MATCH_YES)))
4851 1005892 : || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4852 : {
4853 204 : if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4854 : return MATCH_ERROR;
4855 :
4856 203 : if (matched_type
4857 203 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4858 : "intrinsic-type-spec at %C"))
4859 : return MATCH_ERROR;
4860 :
4861 203 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4862 : {
4863 2 : gfc_error ("Malformed type-spec at %C");
4864 2 : return MATCH_ERROR;
4865 : }
4866 :
4867 201 : ts->type = BT_COMPLEX;
4868 201 : ts->kind = gfc_default_double_kind;
4869 201 : return MATCH_YES;
4870 : }
4871 :
4872 25937 : if ((matched_type && strcmp ("logical", name) == 0)
4873 1005688 : || (!matched_type && gfc_match (" logical") == MATCH_YES))
4874 : {
4875 11556 : ts->type = BT_LOGICAL;
4876 11556 : ts->kind = gfc_default_logical_kind;
4877 11556 : goto get_kind;
4878 : }
4879 :
4880 994132 : if (matched_type)
4881 : {
4882 25934 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4883 25934 : if (m == MATCH_ERROR)
4884 : return m;
4885 :
4886 25934 : gfc_gobble_whitespace ();
4887 25934 : if (gfc_peek_ascii_char () != ')')
4888 : {
4889 1 : gfc_error ("Malformed type-spec at %C");
4890 1 : return MATCH_ERROR;
4891 : }
4892 25933 : m = gfc_match_char (')'); /* Burn closing ')'. */
4893 : }
4894 :
4895 994131 : if (m != MATCH_YES)
4896 968198 : m = match_record_decl (name);
4897 :
4898 994131 : if (matched_type || m == MATCH_YES)
4899 : {
4900 26277 : ts->type = BT_DERIVED;
4901 : /* We accept record/s/ or type(s) where s is a structure, but we
4902 : * don't need all the extra derived-type stuff for structures. */
4903 26277 : if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4904 : {
4905 1 : gfc_error ("Type name %qs at %C is ambiguous", name);
4906 1 : return MATCH_ERROR;
4907 : }
4908 :
4909 26276 : if (sym && sym->attr.flavor == FL_DERIVED
4910 25126 : && sym->attr.pdt_template
4911 1036 : && gfc_current_state () != COMP_DERIVED)
4912 : {
4913 921 : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4914 921 : if (m != MATCH_YES)
4915 : return m;
4916 906 : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4917 906 : ts->u.derived = sym;
4918 906 : const char* lower = gfc_dt_lower_string (sym->name);
4919 906 : size_t len = strlen (lower);
4920 : /* Reallocate with sufficient size. */
4921 906 : if (len > GFC_MAX_SYMBOL_LEN)
4922 2 : name = XALLOCAVEC (char, len + 1);
4923 906 : memcpy (name, lower, len);
4924 906 : name[len] = '\0';
4925 : }
4926 :
4927 26261 : if (sym && sym->attr.flavor == FL_STRUCT)
4928 : {
4929 361 : ts->u.derived = sym;
4930 361 : return MATCH_YES;
4931 : }
4932 : /* Actually a derived type. */
4933 : }
4934 :
4935 : else
4936 : {
4937 : /* Match nested STRUCTURE declarations; only valid within another
4938 : structure declaration. */
4939 967854 : if (flag_dec_structure
4940 8032 : && (gfc_current_state () == COMP_STRUCTURE
4941 7570 : || gfc_current_state () == COMP_MAP))
4942 : {
4943 732 : m = gfc_match (" structure");
4944 732 : if (m == MATCH_YES)
4945 : {
4946 27 : m = gfc_match_structure_decl ();
4947 27 : if (m == MATCH_YES)
4948 : {
4949 : /* gfc_new_block is updated by match_structure_decl. */
4950 26 : ts->type = BT_DERIVED;
4951 26 : ts->u.derived = gfc_new_block;
4952 26 : return MATCH_YES;
4953 : }
4954 : }
4955 706 : if (m == MATCH_ERROR)
4956 : return MATCH_ERROR;
4957 : }
4958 :
4959 : /* Match CLASS declarations. */
4960 967827 : m = gfc_match (" class ( * )");
4961 967827 : if (m == MATCH_ERROR)
4962 : return MATCH_ERROR;
4963 967827 : else if (m == MATCH_YES)
4964 : {
4965 1942 : gfc_symbol *upe;
4966 1942 : gfc_symtree *st;
4967 1942 : ts->type = BT_CLASS;
4968 1942 : gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4969 1942 : if (upe == NULL)
4970 : {
4971 1188 : upe = gfc_new_symbol ("STAR", gfc_current_ns);
4972 1188 : st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4973 1188 : st->n.sym = upe;
4974 1188 : gfc_set_sym_referenced (upe);
4975 1188 : upe->refs++;
4976 1188 : upe->ts.type = BT_VOID;
4977 1188 : upe->attr.unlimited_polymorphic = 1;
4978 : /* This is essential to force the construction of
4979 : unlimited polymorphic component class containers. */
4980 1188 : upe->attr.zero_comp = 1;
4981 1188 : if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4982 : &gfc_current_locus))
4983 : return MATCH_ERROR;
4984 : }
4985 : else
4986 : {
4987 754 : st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4988 754 : st->n.sym = upe;
4989 754 : upe->refs++;
4990 : }
4991 1942 : ts->u.derived = upe;
4992 1942 : return m;
4993 : }
4994 :
4995 965885 : m = gfc_match (" class (");
4996 :
4997 965885 : if (m == MATCH_YES)
4998 9046 : m = gfc_match ("%n", name);
4999 : else
5000 : return m;
5001 :
5002 9046 : if (m != MATCH_YES)
5003 : return m;
5004 9046 : ts->type = BT_CLASS;
5005 :
5006 9046 : if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
5007 : return MATCH_ERROR;
5008 :
5009 9045 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
5010 9045 : if (m == MATCH_ERROR)
5011 : return m;
5012 :
5013 9045 : m = gfc_match_char (')');
5014 9045 : if (m != MATCH_YES)
5015 : return m;
5016 : }
5017 :
5018 : /* This picks up function declarations with a PDT typespec. Since a
5019 : pdt_type has been generated, there is no more to do. Within the
5020 : function body, this type must be used for the typespec so that
5021 : the "being used before it is defined warning" does not arise. */
5022 34945 : if (ts->type == BT_DERIVED
5023 25900 : && sym && sym->attr.pdt_type
5024 35851 : && (gfc_current_state () == COMP_CONTAINS
5025 890 : || (gfc_current_state () == COMP_FUNCTION
5026 280 : && gfc_current_block ()->ts.type == BT_DERIVED
5027 60 : && gfc_current_block ()->ts.u.derived == sym
5028 30 : && !gfc_find_symtree (gfc_current_ns->sym_root,
5029 : sym->name))))
5030 : {
5031 42 : if (gfc_current_state () == COMP_FUNCTION)
5032 : {
5033 26 : gfc_symtree *pdt_st;
5034 26 : pdt_st = gfc_new_symtree (&gfc_current_ns->sym_root,
5035 : sym->name);
5036 26 : pdt_st->n.sym = sym;
5037 26 : sym->refs++;
5038 : }
5039 42 : ts->u.derived = sym;
5040 42 : return MATCH_YES;
5041 : }
5042 :
5043 : /* Defer association of the derived type until the end of the
5044 : specification block. However, if the derived type can be
5045 : found, add it to the typespec. */
5046 34903 : if (gfc_matching_function)
5047 : {
5048 1043 : ts->u.derived = NULL;
5049 1043 : if (gfc_current_state () != COMP_INTERFACE
5050 1043 : && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
5051 : {
5052 512 : sym = gfc_find_dt_in_generic (sym);
5053 512 : ts->u.derived = sym;
5054 : }
5055 1043 : return MATCH_YES;
5056 : }
5057 :
5058 : /* Search for the name but allow the components to be defined later. If
5059 : type = -1, this typespec has been seen in a function declaration but
5060 : the type could not be accessed at that point. The actual derived type is
5061 : stored in a symtree with the first letter of the name capitalized; the
5062 : symtree with the all lower-case name contains the associated
5063 : generic function. */
5064 33860 : dt_name = gfc_dt_upper_string (name);
5065 33860 : sym = NULL;
5066 33860 : dt_sym = NULL;
5067 33860 : if (ts->kind != -1)
5068 : {
5069 32648 : gfc_get_ha_symbol (name, &sym);
5070 32648 : if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
5071 : {
5072 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5073 0 : return MATCH_ERROR;
5074 : }
5075 32648 : if (sym->generic && !dt_sym)
5076 14408 : dt_sym = gfc_find_dt_in_generic (sym);
5077 :
5078 : /* Host associated PDTs can get confused with their constructors
5079 : because they are instantiated in the template's namespace. */
5080 32648 : if (!dt_sym)
5081 : {
5082 968 : if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
5083 : {
5084 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5085 0 : return MATCH_ERROR;
5086 : }
5087 968 : if (dt_sym && !dt_sym->attr.pdt_type)
5088 0 : dt_sym = NULL;
5089 : }
5090 : }
5091 1212 : else if (ts->kind == -1)
5092 : {
5093 2424 : int iface = gfc_state_stack->previous->state != COMP_INTERFACE
5094 1212 : || gfc_current_ns->has_import_set;
5095 1212 : gfc_find_symbol (name, NULL, iface, &sym);
5096 1212 : if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
5097 : {
5098 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5099 0 : return MATCH_ERROR;
5100 : }
5101 1212 : if (sym && sym->generic && !dt_sym)
5102 2 : dt_sym = gfc_find_dt_in_generic (sym);
5103 :
5104 1212 : ts->kind = 0;
5105 1212 : if (sym == NULL)
5106 : return MATCH_NO;
5107 : }
5108 :
5109 33843 : if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
5110 33097 : && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
5111 33841 : || sym->attr.subroutine)
5112 : {
5113 2 : gfc_error ("Type name %qs at %C conflicts with previously declared "
5114 : "entity at %L, which has the same name", name,
5115 : &sym->declared_at);
5116 2 : return MATCH_ERROR;
5117 : }
5118 :
5119 33841 : if (dt_sym && decl_type_param_list
5120 940 : && dt_sym->attr.flavor == FL_DERIVED
5121 940 : && !dt_sym->attr.pdt_type
5122 250 : && !dt_sym->attr.pdt_template)
5123 : {
5124 1 : gfc_error ("Type %qs is not parameterized and so the type parameter spec "
5125 : "list at %C may not appear", dt_sym->name);
5126 1 : return MATCH_ERROR;
5127 : }
5128 :
5129 33840 : if (sym && sym->attr.flavor == FL_DERIVED
5130 : && sym->attr.pdt_template
5131 : && gfc_current_state () != COMP_DERIVED)
5132 : {
5133 : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
5134 : if (m != MATCH_YES)
5135 : return m;
5136 : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
5137 : ts->u.derived = sym;
5138 : strcpy (name, gfc_dt_lower_string (sym->name));
5139 : }
5140 :
5141 33840 : gfc_save_symbol_data (sym);
5142 33840 : gfc_set_sym_referenced (sym);
5143 33840 : if (!sym->attr.generic
5144 33840 : && !gfc_add_generic (&sym->attr, sym->name, NULL))
5145 : return MATCH_ERROR;
5146 :
5147 33840 : if (!sym->attr.function
5148 33840 : && !gfc_add_function (&sym->attr, sym->name, NULL))
5149 : return MATCH_ERROR;
5150 :
5151 33840 : if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
5152 33708 : && dt_sym->attr.pdt_template
5153 260 : && gfc_current_state () != COMP_DERIVED)
5154 : {
5155 133 : m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
5156 133 : if (m != MATCH_YES)
5157 : return m;
5158 133 : gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
5159 : }
5160 :
5161 33840 : if (!dt_sym)
5162 : {
5163 132 : gfc_interface *intr, *head;
5164 :
5165 : /* Use upper case to save the actual derived-type symbol. */
5166 132 : gfc_get_symbol (dt_name, NULL, &dt_sym);
5167 132 : dt_sym->name = gfc_get_string ("%s", sym->name);
5168 132 : head = sym->generic;
5169 132 : intr = gfc_get_interface ();
5170 132 : intr->sym = dt_sym;
5171 132 : intr->where = gfc_current_locus;
5172 132 : intr->next = head;
5173 132 : sym->generic = intr;
5174 132 : sym->attr.if_source = IFSRC_DECL;
5175 : }
5176 : else
5177 33708 : gfc_save_symbol_data (dt_sym);
5178 :
5179 33840 : gfc_set_sym_referenced (dt_sym);
5180 :
5181 132 : if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
5182 33972 : && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
5183 : return MATCH_ERROR;
5184 :
5185 33840 : ts->u.derived = dt_sym;
5186 :
5187 33840 : return MATCH_YES;
5188 :
5189 159421 : get_kind:
5190 159421 : if (matched_type
5191 159421 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
5192 : "intrinsic-type-spec at %C"))
5193 : return MATCH_ERROR;
5194 :
5195 : /* For all types except double, derived and character, look for an
5196 : optional kind specifier. MATCH_NO is actually OK at this point. */
5197 159418 : if (implicit_flag == 1)
5198 : {
5199 223 : if (matched_type && gfc_match_char (')') != MATCH_YES)
5200 : return MATCH_ERROR;
5201 :
5202 223 : return MATCH_YES;
5203 : }
5204 :
5205 159195 : if (gfc_current_form == FORM_FREE)
5206 : {
5207 143980 : c = gfc_peek_ascii_char ();
5208 143980 : if (!gfc_is_whitespace (c) && c != '*' && c != '('
5209 71067 : && c != ':' && c != ',')
5210 : {
5211 167 : if (matched_type && c == ')')
5212 : {
5213 3 : gfc_next_ascii_char ();
5214 3 : return MATCH_YES;
5215 : }
5216 164 : gfc_error ("Malformed type-spec at %C");
5217 164 : return MATCH_NO;
5218 : }
5219 : }
5220 :
5221 159028 : m = gfc_match_kind_spec (ts, false);
5222 159028 : if (m == MATCH_ERROR)
5223 : return MATCH_ERROR;
5224 :
5225 158992 : if (m == MATCH_NO && ts->type != BT_CHARACTER)
5226 : {
5227 107730 : m = gfc_match_old_kind_spec (ts);
5228 107730 : if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
5229 : return MATCH_ERROR;
5230 : }
5231 :
5232 158984 : if (matched_type && gfc_match_char (')') != MATCH_YES)
5233 : {
5234 0 : gfc_error ("Malformed type-spec at %C");
5235 0 : return MATCH_ERROR;
5236 : }
5237 :
5238 : /* Defer association of the KIND expression of function results
5239 : until after USE and IMPORT statements. */
5240 4444 : if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
5241 163401 : || gfc_matching_function)
5242 7232 : return MATCH_YES;
5243 :
5244 151752 : if (m == MATCH_NO)
5245 152306 : m = MATCH_YES; /* No kind specifier found. */
5246 :
5247 : return m;
5248 : }
5249 :
5250 :
5251 : /* Match an IMPLICIT NONE statement. Actually, this statement is
5252 : already matched in parse.cc, or we would not end up here in the
5253 : first place. So the only thing we need to check, is if there is
5254 : trailing garbage. If not, the match is successful. */
5255 :
5256 : match
5257 24094 : gfc_match_implicit_none (void)
5258 : {
5259 24094 : char c;
5260 24094 : match m;
5261 24094 : char name[GFC_MAX_SYMBOL_LEN + 1];
5262 24094 : bool type = false;
5263 24094 : bool external = false;
5264 24094 : locus cur_loc = gfc_current_locus;
5265 :
5266 24094 : if (gfc_current_ns->seen_implicit_none
5267 24092 : || gfc_current_ns->has_implicit_none_export)
5268 : {
5269 4 : gfc_error ("Duplicate IMPLICIT NONE statement at %C");
5270 4 : return MATCH_ERROR;
5271 : }
5272 :
5273 24090 : gfc_gobble_whitespace ();
5274 24090 : c = gfc_peek_ascii_char ();
5275 24090 : if (c == '(')
5276 : {
5277 1109 : (void) gfc_next_ascii_char ();
5278 1109 : if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
5279 : return MATCH_ERROR;
5280 :
5281 1108 : gfc_gobble_whitespace ();
5282 1108 : if (gfc_peek_ascii_char () == ')')
5283 : {
5284 1 : (void) gfc_next_ascii_char ();
5285 1 : type = true;
5286 : }
5287 : else
5288 3297 : for(;;)
5289 : {
5290 2202 : m = gfc_match (" %n", name);
5291 2202 : if (m != MATCH_YES)
5292 : return MATCH_ERROR;
5293 :
5294 2202 : if (strcmp (name, "type") == 0)
5295 : type = true;
5296 1107 : else if (strcmp (name, "external") == 0)
5297 : external = true;
5298 : else
5299 : return MATCH_ERROR;
5300 :
5301 2202 : gfc_gobble_whitespace ();
5302 2202 : c = gfc_next_ascii_char ();
5303 2202 : if (c == ',')
5304 1095 : continue;
5305 1107 : if (c == ')')
5306 : break;
5307 : return MATCH_ERROR;
5308 : }
5309 : }
5310 : else
5311 : type = true;
5312 :
5313 24089 : if (gfc_match_eos () != MATCH_YES)
5314 : return MATCH_ERROR;
5315 :
5316 24089 : gfc_set_implicit_none (type, external, &cur_loc);
5317 :
5318 24089 : return MATCH_YES;
5319 : }
5320 :
5321 :
5322 : /* Match the letter range(s) of an IMPLICIT statement. */
5323 :
5324 : static match
5325 600 : match_implicit_range (void)
5326 : {
5327 600 : char c, c1, c2;
5328 600 : int inner;
5329 600 : locus cur_loc;
5330 :
5331 600 : cur_loc = gfc_current_locus;
5332 :
5333 600 : gfc_gobble_whitespace ();
5334 600 : c = gfc_next_ascii_char ();
5335 600 : if (c != '(')
5336 : {
5337 59 : gfc_error ("Missing character range in IMPLICIT at %C");
5338 59 : goto bad;
5339 : }
5340 :
5341 : inner = 1;
5342 1195 : while (inner)
5343 : {
5344 722 : gfc_gobble_whitespace ();
5345 722 : c1 = gfc_next_ascii_char ();
5346 722 : if (!ISALPHA (c1))
5347 33 : goto bad;
5348 :
5349 689 : gfc_gobble_whitespace ();
5350 689 : c = gfc_next_ascii_char ();
5351 :
5352 689 : switch (c)
5353 : {
5354 201 : case ')':
5355 201 : inner = 0; /* Fall through. */
5356 :
5357 : case ',':
5358 : c2 = c1;
5359 : break;
5360 :
5361 439 : case '-':
5362 439 : gfc_gobble_whitespace ();
5363 439 : c2 = gfc_next_ascii_char ();
5364 439 : if (!ISALPHA (c2))
5365 0 : goto bad;
5366 :
5367 439 : gfc_gobble_whitespace ();
5368 439 : c = gfc_next_ascii_char ();
5369 :
5370 439 : if ((c != ',') && (c != ')'))
5371 0 : goto bad;
5372 439 : if (c == ')')
5373 272 : inner = 0;
5374 :
5375 : break;
5376 :
5377 35 : default:
5378 35 : goto bad;
5379 : }
5380 :
5381 654 : if (c1 > c2)
5382 : {
5383 0 : gfc_error ("Letters must be in alphabetic order in "
5384 : "IMPLICIT statement at %C");
5385 0 : goto bad;
5386 : }
5387 :
5388 : /* See if we can add the newly matched range to the pending
5389 : implicits from this IMPLICIT statement. We do not check for
5390 : conflicts with whatever earlier IMPLICIT statements may have
5391 : set. This is done when we've successfully finished matching
5392 : the current one. */
5393 654 : if (!gfc_add_new_implicit_range (c1, c2))
5394 0 : goto bad;
5395 : }
5396 :
5397 : return MATCH_YES;
5398 :
5399 127 : bad:
5400 127 : gfc_syntax_error (ST_IMPLICIT);
5401 :
5402 127 : gfc_current_locus = cur_loc;
5403 127 : return MATCH_ERROR;
5404 : }
5405 :
5406 :
5407 : /* Match an IMPLICIT statement, storing the types for
5408 : gfc_set_implicit() if the statement is accepted by the parser.
5409 : There is a strange looking, but legal syntactic construction
5410 : possible. It looks like:
5411 :
5412 : IMPLICIT INTEGER (a-b) (c-d)
5413 :
5414 : This is legal if "a-b" is a constant expression that happens to
5415 : equal one of the legal kinds for integers. The real problem
5416 : happens with an implicit specification that looks like:
5417 :
5418 : IMPLICIT INTEGER (a-b)
5419 :
5420 : In this case, a typespec matcher that is "greedy" (as most of the
5421 : matchers are) gobbles the character range as a kindspec, leaving
5422 : nothing left. We therefore have to go a bit more slowly in the
5423 : matching process by inhibiting the kindspec checking during
5424 : typespec matching and checking for a kind later. */
5425 :
5426 : match
5427 24520 : gfc_match_implicit (void)
5428 : {
5429 24520 : gfc_typespec ts;
5430 24520 : locus cur_loc;
5431 24520 : char c;
5432 24520 : match m;
5433 :
5434 24520 : if (gfc_current_ns->seen_implicit_none)
5435 : {
5436 4 : gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
5437 : "statement");
5438 4 : return MATCH_ERROR;
5439 : }
5440 :
5441 24516 : gfc_clear_ts (&ts);
5442 :
5443 : /* We don't allow empty implicit statements. */
5444 24516 : if (gfc_match_eos () == MATCH_YES)
5445 : {
5446 0 : gfc_error ("Empty IMPLICIT statement at %C");
5447 0 : return MATCH_ERROR;
5448 : }
5449 :
5450 24545 : do
5451 : {
5452 : /* First cleanup. */
5453 24545 : gfc_clear_new_implicit ();
5454 :
5455 : /* A basic type is mandatory here. */
5456 24545 : m = gfc_match_decl_type_spec (&ts, 1);
5457 24545 : if (m == MATCH_ERROR)
5458 0 : goto error;
5459 24545 : if (m == MATCH_NO)
5460 24092 : goto syntax;
5461 :
5462 453 : cur_loc = gfc_current_locus;
5463 453 : m = match_implicit_range ();
5464 :
5465 453 : if (m == MATCH_YES)
5466 : {
5467 : /* We may have <TYPE> (<RANGE>). */
5468 326 : gfc_gobble_whitespace ();
5469 326 : c = gfc_peek_ascii_char ();
5470 326 : if (c == ',' || c == '\n' || c == ';' || c == '!')
5471 : {
5472 : /* Check for CHARACTER with no length parameter. */
5473 299 : if (ts.type == BT_CHARACTER && !ts.u.cl)
5474 : {
5475 32 : ts.kind = gfc_default_character_kind;
5476 32 : ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5477 32 : ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
5478 : NULL, 1);
5479 : }
5480 :
5481 : /* Record the Successful match. */
5482 299 : if (!gfc_merge_new_implicit (&ts))
5483 : return MATCH_ERROR;
5484 297 : if (c == ',')
5485 28 : c = gfc_next_ascii_char ();
5486 269 : else if (gfc_match_eos () == MATCH_ERROR)
5487 0 : goto error;
5488 297 : continue;
5489 : }
5490 :
5491 27 : gfc_current_locus = cur_loc;
5492 : }
5493 :
5494 : /* Discard the (incorrectly) matched range. */
5495 154 : gfc_clear_new_implicit ();
5496 :
5497 : /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
5498 154 : if (ts.type == BT_CHARACTER)
5499 74 : m = gfc_match_char_spec (&ts);
5500 80 : else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
5501 : {
5502 76 : m = gfc_match_kind_spec (&ts, false);
5503 76 : if (m == MATCH_NO)
5504 : {
5505 40 : m = gfc_match_old_kind_spec (&ts);
5506 40 : if (m == MATCH_ERROR)
5507 0 : goto error;
5508 40 : if (m == MATCH_NO)
5509 0 : goto syntax;
5510 : }
5511 : }
5512 154 : if (m == MATCH_ERROR)
5513 7 : goto error;
5514 :
5515 147 : m = match_implicit_range ();
5516 147 : if (m == MATCH_ERROR)
5517 0 : goto error;
5518 147 : if (m == MATCH_NO)
5519 : goto syntax;
5520 :
5521 147 : gfc_gobble_whitespace ();
5522 147 : c = gfc_next_ascii_char ();
5523 147 : if (c != ',' && gfc_match_eos () != MATCH_YES)
5524 0 : goto syntax;
5525 :
5526 147 : if (!gfc_merge_new_implicit (&ts))
5527 : return MATCH_ERROR;
5528 : }
5529 444 : while (c == ',');
5530 :
5531 : return MATCH_YES;
5532 :
5533 24092 : syntax:
5534 24092 : gfc_syntax_error (ST_IMPLICIT);
5535 :
5536 : error:
5537 : return MATCH_ERROR;
5538 : }
5539 :
5540 :
5541 : /* Match the IMPORT statement. IMPORT was added to F2003 as
5542 :
5543 : R1209 import-stmt is IMPORT [[ :: ] import-name-list ]
5544 :
5545 : C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
5546 :
5547 : C1211 (R1209) Each import-name shall be the name of an entity in the
5548 : host scoping unit.
5549 :
5550 : under the description of an interface block. Under F2008, IMPORT was
5551 : split out of the interface block description to 12.4.3.3 and C1210
5552 : became
5553 :
5554 : C1210 (R1209) The IMPORT statement is allowed only in an interface-body
5555 : that is not a module procedure interface body.
5556 :
5557 : Finally, F2018, section 8.8, has changed the IMPORT statement to
5558 :
5559 : R867 import-stmt is IMPORT [[ :: ] import-name-list ]
5560 : or IMPORT, ONLY : import-name-list
5561 : or IMPORT, NONE
5562 : or IMPORT, ALL
5563 :
5564 : C896 (R867) An IMPORT statement shall not appear in the scoping unit of
5565 : a main-program, external-subprogram, module, or block-data.
5566 :
5567 : C897 (R867) Each import-name shall be the name of an entity in the host
5568 : scoping unit.
5569 :
5570 : C898 If any IMPORT statement in a scoping unit has an ONLY specifier,
5571 : all IMPORT statements in that scoping unit shall have an ONLY
5572 : specifier.
5573 :
5574 : C899 IMPORT, NONE shall not appear in the scoping unit of a submodule.
5575 :
5576 : C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
5577 : unit, no other IMPORT statement shall appear in that scoping unit.
5578 :
5579 : C8101 Within an interface body, an entity that is accessed by host
5580 : association shall be accessible by host or use association within
5581 : the host scoping unit, or explicitly declared prior to the interface
5582 : body.
5583 :
5584 : C8102 An entity whose name appears as an import-name or which is made
5585 : accessible by an IMPORT, ALL statement shall not appear in any
5586 : context described in 19.5.1.4 that would cause the host entity
5587 : of that name to be inaccessible. */
5588 :
5589 : match
5590 4032 : gfc_match_import (void)
5591 : {
5592 4032 : char name[GFC_MAX_SYMBOL_LEN + 1];
5593 4032 : match m;
5594 4032 : gfc_symbol *sym;
5595 4032 : gfc_symtree *st;
5596 4032 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
5597 4032 : importstate current_import_state = gfc_current_ns->import_state;
5598 :
5599 4032 : if (!f2018_allowed
5600 13 : && (gfc_current_ns->proc_name == NULL
5601 12 : || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
5602 : {
5603 3 : gfc_error ("IMPORT statement at %C only permitted in "
5604 : "an INTERFACE body");
5605 3 : return MATCH_ERROR;
5606 : }
5607 : else if (f2018_allowed
5608 4019 : && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
5609 4 : goto C897;
5610 :
5611 4015 : if (f2018_allowed
5612 4015 : && (current_import_state == IMPORT_ALL
5613 4015 : || current_import_state == IMPORT_NONE))
5614 2 : goto C8100;
5615 :
5616 4023 : if (gfc_current_ns->proc_name
5617 4022 : && gfc_current_ns->proc_name->attr.module_procedure)
5618 : {
5619 1 : gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
5620 : "in a module procedure interface body");
5621 1 : return MATCH_ERROR;
5622 : }
5623 :
5624 4022 : if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
5625 : return MATCH_ERROR;
5626 :
5627 4018 : gfc_current_ns->import_state = IMPORT_NOT_SET;
5628 4018 : if (f2018_allowed)
5629 : {
5630 4012 : if (gfc_match (" , none") == MATCH_YES)
5631 : {
5632 8 : if (current_import_state == IMPORT_ONLY)
5633 0 : goto C898;
5634 8 : if (gfc_current_state () == COMP_SUBMODULE)
5635 0 : goto C899;
5636 8 : gfc_current_ns->import_state = IMPORT_NONE;
5637 : }
5638 4004 : else if (gfc_match (" , only :") == MATCH_YES)
5639 : {
5640 19 : if (current_import_state != IMPORT_NOT_SET
5641 19 : && current_import_state != IMPORT_ONLY)
5642 0 : goto C898;
5643 19 : gfc_current_ns->import_state = IMPORT_ONLY;
5644 : }
5645 3985 : else if (gfc_match (" , all") == MATCH_YES)
5646 : {
5647 1 : if (current_import_state == IMPORT_ONLY)
5648 0 : goto C898;
5649 1 : gfc_current_ns->import_state = IMPORT_ALL;
5650 : }
5651 :
5652 4012 : if (current_import_state != IMPORT_NOT_SET
5653 6 : && (gfc_current_ns->import_state == IMPORT_NONE
5654 6 : || gfc_current_ns->import_state == IMPORT_ALL))
5655 0 : goto C8100;
5656 : }
5657 :
5658 : /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL. */
5659 4018 : if (gfc_match_eos () == MATCH_YES)
5660 : {
5661 : /* This is the F2008 variant. */
5662 340 : if (gfc_current_ns->import_state == IMPORT_NOT_SET)
5663 : {
5664 331 : if (current_import_state == IMPORT_ONLY)
5665 0 : goto C898;
5666 331 : gfc_current_ns->import_state = IMPORT_F2008;
5667 : }
5668 :
5669 : /* Host variables should be imported. */
5670 340 : if (gfc_current_ns->import_state != IMPORT_NONE)
5671 332 : gfc_current_ns->has_import_set = 1;
5672 340 : return MATCH_YES;
5673 : }
5674 :
5675 3678 : if (gfc_match (" ::") == MATCH_YES
5676 3678 : && gfc_current_ns->import_state != IMPORT_ONLY)
5677 : {
5678 1170 : if (gfc_match_eos () == MATCH_YES)
5679 1 : goto expecting_list;
5680 1169 : gfc_current_ns->import_state = IMPORT_F2008;
5681 : }
5682 2508 : else if (gfc_current_ns->import_state == IMPORT_ONLY)
5683 : {
5684 19 : if (gfc_match_eos () == MATCH_YES)
5685 0 : goto expecting_list;
5686 : }
5687 :
5688 4366 : for(;;)
5689 : {
5690 4366 : sym = NULL;
5691 4366 : m = gfc_match (" %n", name);
5692 4366 : switch (m)
5693 : {
5694 4366 : case MATCH_YES:
5695 : /* Before checking if the symbol is available from host
5696 : association into a SUBROUTINE or FUNCTION within an
5697 : INTERFACE, check if it is already in local scope. */
5698 4366 : gfc_find_symbol (name, gfc_current_ns, 1, &sym);
5699 4366 : if (sym
5700 25 : && gfc_state_stack->previous
5701 25 : && gfc_state_stack->previous->state == COMP_INTERFACE)
5702 : {
5703 2 : gfc_error ("import-name %qs at %C is in the "
5704 : "local scope", name);
5705 2 : return MATCH_ERROR;
5706 : }
5707 :
5708 4364 : if (gfc_current_ns->parent != NULL
5709 4364 : && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
5710 : {
5711 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5712 0 : return MATCH_ERROR;
5713 : }
5714 4364 : else if (!sym
5715 5 : && gfc_current_ns->proc_name
5716 4 : && gfc_current_ns->proc_name->ns->parent
5717 4365 : && gfc_find_symbol (name,
5718 : gfc_current_ns->proc_name->ns->parent,
5719 : 1, &sym))
5720 : {
5721 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5722 0 : return MATCH_ERROR;
5723 : }
5724 :
5725 4364 : if (sym == NULL)
5726 : {
5727 5 : if (gfc_current_ns->proc_name
5728 4 : && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
5729 : {
5730 1 : gfc_error ("Cannot IMPORT %qs from host scoping unit "
5731 : "at %C - does not exist.", name);
5732 1 : return MATCH_ERROR;
5733 : }
5734 : else
5735 : {
5736 : /* This might be a procedure that has not yet been parsed. If
5737 : so gfc_fixup_sibling_symbols will replace this symbol with
5738 : that of the procedure. */
5739 4 : gfc_get_sym_tree (name, gfc_current_ns, &st, false,
5740 : &gfc_current_locus);
5741 4 : st->n.sym->refs++;
5742 4 : st->n.sym->attr.imported = 1;
5743 4 : st->import_only = 1;
5744 4 : goto next_item;
5745 : }
5746 : }
5747 :
5748 4359 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5749 4359 : if (st && st->n.sym && st->n.sym->attr.imported)
5750 : {
5751 0 : gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
5752 : "at %C", name);
5753 0 : goto next_item;
5754 : }
5755 :
5756 4359 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
5757 4359 : st->n.sym = sym;
5758 4359 : sym->refs++;
5759 4359 : sym->attr.imported = 1;
5760 4359 : st->import_only = 1;
5761 :
5762 4359 : if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
5763 : {
5764 : /* The actual derived type is stored in a symtree with the first
5765 : letter of the name capitalized; the symtree with the all
5766 : lower-case name contains the associated generic function. */
5767 599 : st = gfc_new_symtree (&gfc_current_ns->sym_root,
5768 : gfc_dt_upper_string (name));
5769 599 : st->n.sym = sym;
5770 599 : sym->refs++;
5771 599 : sym->attr.imported = 1;
5772 599 : st->import_only = 1;
5773 : }
5774 :
5775 4359 : goto next_item;
5776 :
5777 : case MATCH_NO:
5778 : break;
5779 :
5780 : case MATCH_ERROR:
5781 : return MATCH_ERROR;
5782 : }
5783 :
5784 4363 : next_item:
5785 4363 : if (gfc_match_eos () == MATCH_YES)
5786 : break;
5787 689 : if (gfc_match_char (',') != MATCH_YES)
5788 0 : goto syntax;
5789 : }
5790 :
5791 : return MATCH_YES;
5792 :
5793 0 : syntax:
5794 0 : gfc_error ("Syntax error in IMPORT statement at %C");
5795 0 : return MATCH_ERROR;
5796 :
5797 4 : C897:
5798 4 : gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
5799 : "program, an external subprogram, a module or block data");
5800 4 : return MATCH_ERROR;
5801 :
5802 0 : C898:
5803 0 : gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
5804 : "a scoping unit has an ONLY specifier, can only have IMPORT "
5805 : "with an ONLY specifier");
5806 0 : return MATCH_ERROR;
5807 :
5808 0 : C899:
5809 0 : gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
5810 : " of a submodule as at %C");
5811 0 : return MATCH_ERROR;
5812 :
5813 2 : C8100:
5814 4 : gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
5815 : "%s has already been declared, which must be unique in the "
5816 : "scoping unit",
5817 2 : gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
5818 : "IMPORT, NONE");
5819 2 : return MATCH_ERROR;
5820 :
5821 1 : expecting_list:
5822 1 : gfc_error ("Expecting list of named entities at %C");
5823 1 : return MATCH_ERROR;
5824 : }
5825 :
5826 :
5827 : /* A minimal implementation of gfc_match without whitespace, escape
5828 : characters or variable arguments. Returns true if the next
5829 : characters match the TARGET template exactly. */
5830 :
5831 : static bool
5832 146990 : match_string_p (const char *target)
5833 : {
5834 146990 : const char *p;
5835 :
5836 923277 : for (p = target; *p; p++)
5837 776288 : if ((char) gfc_next_ascii_char () != *p)
5838 : return false;
5839 : return true;
5840 : }
5841 :
5842 : /* Matches an attribute specification including array specs. If
5843 : successful, leaves the variables current_attr and current_as
5844 : holding the specification. Also sets the colon_seen variable for
5845 : later use by matchers associated with initializations.
5846 :
5847 : This subroutine is a little tricky in the sense that we don't know
5848 : if we really have an attr-spec until we hit the double colon.
5849 : Until that time, we can only return MATCH_NO. This forces us to
5850 : check for duplicate specification at this level. */
5851 :
5852 : static match
5853 217855 : match_attr_spec (void)
5854 : {
5855 : /* Modifiers that can exist in a type statement. */
5856 217855 : enum
5857 : { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
5858 : DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
5859 : DECL_DIMENSION, DECL_EXTERNAL,
5860 : DECL_INTRINSIC, DECL_OPTIONAL,
5861 : DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
5862 : DECL_STATIC, DECL_AUTOMATIC,
5863 : DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
5864 : DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5865 : DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
5866 : };
5867 :
5868 : /* GFC_DECL_END is the sentinel, index starts at 0. */
5869 : #define NUM_DECL GFC_DECL_END
5870 :
5871 : /* Make sure that values from sym_intent are safe to be used here. */
5872 217855 : gcc_assert (INTENT_IN > 0);
5873 :
5874 217855 : locus start, seen_at[NUM_DECL];
5875 217855 : int seen[NUM_DECL];
5876 217855 : unsigned int d;
5877 217855 : const char *attr;
5878 217855 : match m;
5879 217855 : bool t;
5880 :
5881 217855 : gfc_clear_attr (¤t_attr);
5882 217855 : start = gfc_current_locus;
5883 :
5884 217855 : current_as = NULL;
5885 217855 : colon_seen = 0;
5886 217855 : attr_seen = 0;
5887 :
5888 : /* See if we get all of the keywords up to the final double colon. */
5889 5882085 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5890 5664230 : seen[d] = 0;
5891 :
5892 337027 : for (;;)
5893 : {
5894 337027 : char ch;
5895 :
5896 337027 : d = DECL_NONE;
5897 337027 : gfc_gobble_whitespace ();
5898 :
5899 337027 : ch = gfc_next_ascii_char ();
5900 337027 : if (ch == ':')
5901 : {
5902 : /* This is the successful exit condition for the loop. */
5903 184004 : if (gfc_next_ascii_char () == ':')
5904 : break;
5905 : }
5906 153023 : else if (ch == ',')
5907 : {
5908 119184 : gfc_gobble_whitespace ();
5909 119184 : switch (gfc_peek_ascii_char ())
5910 : {
5911 18499 : case 'a':
5912 18499 : gfc_next_ascii_char ();
5913 18499 : switch (gfc_next_ascii_char ())
5914 : {
5915 18433 : case 'l':
5916 18433 : if (match_string_p ("locatable"))
5917 : {
5918 : /* Matched "allocatable". */
5919 : d = DECL_ALLOCATABLE;
5920 : }
5921 : break;
5922 :
5923 25 : case 's':
5924 25 : if (match_string_p ("ynchronous"))
5925 : {
5926 : /* Matched "asynchronous". */
5927 : d = DECL_ASYNCHRONOUS;
5928 : }
5929 : break;
5930 :
5931 41 : case 'u':
5932 41 : if (match_string_p ("tomatic"))
5933 : {
5934 : /* Matched "automatic". */
5935 : d = DECL_AUTOMATIC;
5936 : }
5937 : break;
5938 : }
5939 : break;
5940 :
5941 163 : case 'b':
5942 : /* Try and match the bind(c). */
5943 163 : m = gfc_match_bind_c (NULL, true);
5944 163 : if (m == MATCH_YES)
5945 : d = DECL_IS_BIND_C;
5946 0 : else if (m == MATCH_ERROR)
5947 0 : goto cleanup;
5948 : break;
5949 :
5950 2164 : case 'c':
5951 2164 : gfc_next_ascii_char ();
5952 2164 : if ('o' != gfc_next_ascii_char ())
5953 : break;
5954 2163 : switch (gfc_next_ascii_char ())
5955 : {
5956 68 : case 'd':
5957 68 : if (match_string_p ("imension"))
5958 : {
5959 : d = DECL_CODIMENSION;
5960 : break;
5961 : }
5962 : /* FALLTHRU */
5963 2095 : case 'n':
5964 2095 : if (match_string_p ("tiguous"))
5965 : {
5966 : d = DECL_CONTIGUOUS;
5967 : break;
5968 : }
5969 : }
5970 : break;
5971 :
5972 19713 : case 'd':
5973 19713 : if (match_string_p ("dimension"))
5974 : d = DECL_DIMENSION;
5975 : break;
5976 :
5977 177 : case 'e':
5978 177 : if (match_string_p ("external"))
5979 : d = DECL_EXTERNAL;
5980 : break;
5981 :
5982 27970 : case 'i':
5983 27970 : if (match_string_p ("int"))
5984 : {
5985 27970 : ch = gfc_next_ascii_char ();
5986 27970 : if (ch == 'e')
5987 : {
5988 27964 : if (match_string_p ("nt"))
5989 : {
5990 : /* Matched "intent". */
5991 27963 : d = match_intent_spec ();
5992 27963 : if (d == INTENT_UNKNOWN)
5993 : {
5994 2 : m = MATCH_ERROR;
5995 2 : goto cleanup;
5996 : }
5997 : }
5998 : }
5999 6 : else if (ch == 'r')
6000 : {
6001 6 : if (match_string_p ("insic"))
6002 : {
6003 : /* Matched "intrinsic". */
6004 : d = DECL_INTRINSIC;
6005 : }
6006 : }
6007 : }
6008 : break;
6009 :
6010 293 : case 'k':
6011 293 : if (match_string_p ("kind"))
6012 : d = DECL_KIND;
6013 : break;
6014 :
6015 313 : case 'l':
6016 313 : if (match_string_p ("len"))
6017 : d = DECL_LEN;
6018 : break;
6019 :
6020 5060 : case 'o':
6021 5060 : if (match_string_p ("optional"))
6022 : d = DECL_OPTIONAL;
6023 : break;
6024 :
6025 27115 : case 'p':
6026 27115 : gfc_next_ascii_char ();
6027 27115 : switch (gfc_next_ascii_char ())
6028 : {
6029 14304 : case 'a':
6030 14304 : if (match_string_p ("rameter"))
6031 : {
6032 : /* Matched "parameter". */
6033 : d = DECL_PARAMETER;
6034 : }
6035 : break;
6036 :
6037 12290 : case 'o':
6038 12290 : if (match_string_p ("inter"))
6039 : {
6040 : /* Matched "pointer". */
6041 : d = DECL_POINTER;
6042 : }
6043 : break;
6044 :
6045 268 : case 'r':
6046 268 : ch = gfc_next_ascii_char ();
6047 268 : if (ch == 'i')
6048 : {
6049 217 : if (match_string_p ("vate"))
6050 : {
6051 : /* Matched "private". */
6052 : d = DECL_PRIVATE;
6053 : }
6054 : }
6055 51 : else if (ch == 'o')
6056 : {
6057 51 : if (match_string_p ("tected"))
6058 : {
6059 : /* Matched "protected". */
6060 : d = DECL_PROTECTED;
6061 : }
6062 : }
6063 : break;
6064 :
6065 253 : case 'u':
6066 253 : if (match_string_p ("blic"))
6067 : {
6068 : /* Matched "public". */
6069 : d = DECL_PUBLIC;
6070 : }
6071 : break;
6072 : }
6073 : break;
6074 :
6075 1216 : case 's':
6076 1216 : gfc_next_ascii_char ();
6077 1216 : switch (gfc_next_ascii_char ())
6078 : {
6079 1203 : case 'a':
6080 1203 : if (match_string_p ("ve"))
6081 : {
6082 : /* Matched "save". */
6083 : d = DECL_SAVE;
6084 : }
6085 : break;
6086 :
6087 13 : case 't':
6088 13 : if (match_string_p ("atic"))
6089 : {
6090 : /* Matched "static". */
6091 : d = DECL_STATIC;
6092 : }
6093 : break;
6094 : }
6095 : break;
6096 :
6097 5367 : case 't':
6098 5367 : if (match_string_p ("target"))
6099 : d = DECL_TARGET;
6100 : break;
6101 :
6102 11134 : case 'v':
6103 11134 : gfc_next_ascii_char ();
6104 11134 : ch = gfc_next_ascii_char ();
6105 11134 : if (ch == 'a')
6106 : {
6107 10625 : if (match_string_p ("lue"))
6108 : {
6109 : /* Matched "value". */
6110 : d = DECL_VALUE;
6111 : }
6112 : }
6113 509 : else if (ch == 'o')
6114 : {
6115 509 : if (match_string_p ("latile"))
6116 : {
6117 : /* Matched "volatile". */
6118 : d = DECL_VOLATILE;
6119 : }
6120 : }
6121 : break;
6122 : }
6123 : }
6124 :
6125 : /* No double colon and no recognizable decl_type, so assume that
6126 : we've been looking at something else the whole time. */
6127 : if (d == DECL_NONE)
6128 : {
6129 33842 : m = MATCH_NO;
6130 33842 : goto cleanup;
6131 : }
6132 :
6133 : /* Check to make sure any parens are paired up correctly. */
6134 119180 : if (gfc_match_parens () == MATCH_ERROR)
6135 : {
6136 1 : m = MATCH_ERROR;
6137 1 : goto cleanup;
6138 : }
6139 :
6140 119179 : seen[d]++;
6141 119179 : seen_at[d] = gfc_current_locus;
6142 :
6143 119179 : if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
6144 : {
6145 19780 : gfc_array_spec *as = NULL;
6146 :
6147 19780 : m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
6148 : d == DECL_CODIMENSION);
6149 :
6150 19780 : if (current_as == NULL)
6151 19755 : current_as = as;
6152 25 : else if (m == MATCH_YES)
6153 : {
6154 25 : if (!merge_array_spec (as, current_as, false))
6155 2 : m = MATCH_ERROR;
6156 25 : free (as);
6157 : }
6158 :
6159 19780 : if (m == MATCH_NO)
6160 : {
6161 0 : if (d == DECL_CODIMENSION)
6162 0 : gfc_error ("Missing codimension specification at %C");
6163 : else
6164 0 : gfc_error ("Missing dimension specification at %C");
6165 : m = MATCH_ERROR;
6166 : }
6167 :
6168 19780 : if (m == MATCH_ERROR)
6169 7 : goto cleanup;
6170 : }
6171 : }
6172 :
6173 : /* Since we've seen a double colon, we have to be looking at an
6174 : attr-spec. This means that we can now issue errors. */
6175 4968060 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
6176 4784059 : if (seen[d] > 1)
6177 : {
6178 2 : switch (d)
6179 : {
6180 : case DECL_ALLOCATABLE:
6181 : attr = "ALLOCATABLE";
6182 : break;
6183 0 : case DECL_ASYNCHRONOUS:
6184 0 : attr = "ASYNCHRONOUS";
6185 0 : break;
6186 0 : case DECL_CODIMENSION:
6187 0 : attr = "CODIMENSION";
6188 0 : break;
6189 0 : case DECL_CONTIGUOUS:
6190 0 : attr = "CONTIGUOUS";
6191 0 : break;
6192 0 : case DECL_DIMENSION:
6193 0 : attr = "DIMENSION";
6194 0 : break;
6195 0 : case DECL_EXTERNAL:
6196 0 : attr = "EXTERNAL";
6197 0 : break;
6198 0 : case DECL_IN:
6199 0 : attr = "INTENT (IN)";
6200 0 : break;
6201 0 : case DECL_OUT:
6202 0 : attr = "INTENT (OUT)";
6203 0 : break;
6204 0 : case DECL_INOUT:
6205 0 : attr = "INTENT (IN OUT)";
6206 0 : break;
6207 0 : case DECL_INTRINSIC:
6208 0 : attr = "INTRINSIC";
6209 0 : break;
6210 0 : case DECL_OPTIONAL:
6211 0 : attr = "OPTIONAL";
6212 0 : break;
6213 0 : case DECL_KIND:
6214 0 : attr = "KIND";
6215 0 : break;
6216 0 : case DECL_LEN:
6217 0 : attr = "LEN";
6218 0 : break;
6219 0 : case DECL_PARAMETER:
6220 0 : attr = "PARAMETER";
6221 0 : break;
6222 0 : case DECL_POINTER:
6223 0 : attr = "POINTER";
6224 0 : break;
6225 0 : case DECL_PROTECTED:
6226 0 : attr = "PROTECTED";
6227 0 : break;
6228 0 : case DECL_PRIVATE:
6229 0 : attr = "PRIVATE";
6230 0 : break;
6231 0 : case DECL_PUBLIC:
6232 0 : attr = "PUBLIC";
6233 0 : break;
6234 0 : case DECL_SAVE:
6235 0 : attr = "SAVE";
6236 0 : break;
6237 0 : case DECL_STATIC:
6238 0 : attr = "STATIC";
6239 0 : break;
6240 1 : case DECL_AUTOMATIC:
6241 1 : attr = "AUTOMATIC";
6242 1 : break;
6243 0 : case DECL_TARGET:
6244 0 : attr = "TARGET";
6245 0 : break;
6246 0 : case DECL_IS_BIND_C:
6247 0 : attr = "IS_BIND_C";
6248 0 : break;
6249 0 : case DECL_VALUE:
6250 0 : attr = "VALUE";
6251 0 : break;
6252 1 : case DECL_VOLATILE:
6253 1 : attr = "VOLATILE";
6254 1 : break;
6255 0 : default:
6256 0 : attr = NULL; /* This shouldn't happen. */
6257 : }
6258 :
6259 2 : gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
6260 2 : m = MATCH_ERROR;
6261 2 : goto cleanup;
6262 : }
6263 :
6264 : /* Now that we've dealt with duplicate attributes, add the attributes
6265 : to the current attribute. */
6266 4967240 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
6267 : {
6268 4783312 : if (seen[d] == 0)
6269 4664149 : continue;
6270 : else
6271 119163 : attr_seen = 1;
6272 :
6273 119163 : if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
6274 52 : && !flag_dec_static)
6275 : {
6276 3 : gfc_error ("%s at %L is a DEC extension, enable with "
6277 : "%<-fdec-static%>",
6278 : d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
6279 2 : m = MATCH_ERROR;
6280 2 : goto cleanup;
6281 : }
6282 : /* Allow SAVE with STATIC, but don't complain. */
6283 50 : if (d == DECL_STATIC && seen[DECL_SAVE])
6284 0 : continue;
6285 :
6286 119161 : if (gfc_comp_struct (gfc_current_state ())
6287 6887 : && d != DECL_DIMENSION && d != DECL_CODIMENSION
6288 5923 : && d != DECL_POINTER && d != DECL_PRIVATE
6289 4233 : && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
6290 : {
6291 4176 : bool is_derived = gfc_current_state () == COMP_DERIVED;
6292 4176 : if (d == DECL_ALLOCATABLE)
6293 : {
6294 3557 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6295 : ? G_("ALLOCATABLE attribute at %C in a "
6296 : "TYPE definition")
6297 : : G_("ALLOCATABLE attribute at %C in a "
6298 : "STRUCTURE definition")))
6299 : {
6300 2 : m = MATCH_ERROR;
6301 2 : goto cleanup;
6302 : }
6303 : }
6304 619 : else if (d == DECL_KIND)
6305 : {
6306 291 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6307 : ? G_("KIND attribute at %C in a "
6308 : "TYPE definition")
6309 : : G_("KIND attribute at %C in a "
6310 : "STRUCTURE definition")))
6311 : {
6312 1 : m = MATCH_ERROR;
6313 1 : goto cleanup;
6314 : }
6315 290 : if (current_ts.type != BT_INTEGER)
6316 : {
6317 2 : gfc_error ("Component with KIND attribute at %C must be "
6318 : "INTEGER");
6319 2 : m = MATCH_ERROR;
6320 2 : goto cleanup;
6321 : }
6322 : }
6323 328 : else if (d == DECL_LEN)
6324 : {
6325 312 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6326 : ? G_("LEN attribute at %C in a "
6327 : "TYPE definition")
6328 : : G_("LEN attribute at %C in a "
6329 : "STRUCTURE definition")))
6330 : {
6331 0 : m = MATCH_ERROR;
6332 0 : goto cleanup;
6333 : }
6334 312 : if (current_ts.type != BT_INTEGER)
6335 : {
6336 1 : gfc_error ("Component with LEN attribute at %C must be "
6337 : "INTEGER");
6338 1 : m = MATCH_ERROR;
6339 1 : goto cleanup;
6340 : }
6341 : }
6342 : else
6343 : {
6344 32 : gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
6345 : "TYPE definition")
6346 : : G_("Attribute at %L is not allowed in a "
6347 : "STRUCTURE definition"), &seen_at[d]);
6348 16 : m = MATCH_ERROR;
6349 16 : goto cleanup;
6350 : }
6351 : }
6352 :
6353 119139 : if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
6354 470 : && gfc_current_state () != COMP_MODULE)
6355 : {
6356 147 : if (d == DECL_PRIVATE)
6357 : attr = "PRIVATE";
6358 : else
6359 43 : attr = "PUBLIC";
6360 147 : if (gfc_current_state () == COMP_DERIVED
6361 141 : && gfc_state_stack->previous
6362 141 : && gfc_state_stack->previous->state == COMP_MODULE)
6363 : {
6364 138 : if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
6365 : "at %L in a TYPE definition", attr,
6366 : &seen_at[d]))
6367 : {
6368 2 : m = MATCH_ERROR;
6369 2 : goto cleanup;
6370 : }
6371 : }
6372 : else
6373 : {
6374 9 : gfc_error ("%s attribute at %L is not allowed outside of the "
6375 : "specification part of a module", attr, &seen_at[d]);
6376 9 : m = MATCH_ERROR;
6377 9 : goto cleanup;
6378 : }
6379 : }
6380 :
6381 119128 : if (gfc_current_state () != COMP_DERIVED
6382 112272 : && (d == DECL_KIND || d == DECL_LEN))
6383 : {
6384 3 : gfc_error ("Attribute at %L is not allowed outside a TYPE "
6385 : "definition", &seen_at[d]);
6386 3 : m = MATCH_ERROR;
6387 3 : goto cleanup;
6388 : }
6389 :
6390 119125 : switch (d)
6391 : {
6392 18431 : case DECL_ALLOCATABLE:
6393 18431 : t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
6394 18431 : break;
6395 :
6396 24 : case DECL_ASYNCHRONOUS:
6397 24 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
6398 : t = false;
6399 : else
6400 24 : t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
6401 : break;
6402 :
6403 66 : case DECL_CODIMENSION:
6404 66 : t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
6405 66 : break;
6406 :
6407 2095 : case DECL_CONTIGUOUS:
6408 2095 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
6409 : t = false;
6410 : else
6411 2094 : t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
6412 : break;
6413 :
6414 19705 : case DECL_DIMENSION:
6415 19705 : t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
6416 19705 : break;
6417 :
6418 176 : case DECL_EXTERNAL:
6419 176 : t = gfc_add_external (¤t_attr, &seen_at[d]);
6420 176 : break;
6421 :
6422 21111 : case DECL_IN:
6423 21111 : t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
6424 21111 : break;
6425 :
6426 3688 : case DECL_OUT:
6427 3688 : t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
6428 3688 : break;
6429 :
6430 3158 : case DECL_INOUT:
6431 3158 : t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
6432 3158 : break;
6433 :
6434 5 : case DECL_INTRINSIC:
6435 5 : t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
6436 5 : break;
6437 :
6438 5059 : case DECL_OPTIONAL:
6439 5059 : t = gfc_add_optional (¤t_attr, &seen_at[d]);
6440 5059 : break;
6441 :
6442 288 : case DECL_KIND:
6443 288 : t = gfc_add_kind (¤t_attr, &seen_at[d]);
6444 288 : break;
6445 :
6446 311 : case DECL_LEN:
6447 311 : t = gfc_add_len (¤t_attr, &seen_at[d]);
6448 311 : break;
6449 :
6450 14303 : case DECL_PARAMETER:
6451 14303 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
6452 14303 : break;
6453 :
6454 12289 : case DECL_POINTER:
6455 12289 : t = gfc_add_pointer (¤t_attr, &seen_at[d]);
6456 12289 : break;
6457 :
6458 50 : case DECL_PROTECTED:
6459 50 : if (gfc_current_state () != COMP_MODULE
6460 48 : || (gfc_current_ns->proc_name
6461 48 : && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
6462 : {
6463 2 : gfc_error ("PROTECTED at %C only allowed in specification "
6464 : "part of a module");
6465 2 : t = false;
6466 2 : break;
6467 : }
6468 :
6469 48 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
6470 : t = false;
6471 : else
6472 44 : t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
6473 : break;
6474 :
6475 214 : case DECL_PRIVATE:
6476 214 : t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
6477 : &seen_at[d]);
6478 214 : break;
6479 :
6480 245 : case DECL_PUBLIC:
6481 245 : t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
6482 : &seen_at[d]);
6483 245 : break;
6484 :
6485 1213 : case DECL_STATIC:
6486 1213 : case DECL_SAVE:
6487 1213 : t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
6488 1213 : break;
6489 :
6490 37 : case DECL_AUTOMATIC:
6491 37 : t = gfc_add_automatic (¤t_attr, NULL, &seen_at[d]);
6492 37 : break;
6493 :
6494 5365 : case DECL_TARGET:
6495 5365 : t = gfc_add_target (¤t_attr, &seen_at[d]);
6496 5365 : break;
6497 :
6498 162 : case DECL_IS_BIND_C:
6499 162 : t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
6500 162 : break;
6501 :
6502 10624 : case DECL_VALUE:
6503 10624 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
6504 : t = false;
6505 : else
6506 10624 : t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
6507 : break;
6508 :
6509 506 : case DECL_VOLATILE:
6510 506 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
6511 : t = false;
6512 : else
6513 505 : t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
6514 : break;
6515 :
6516 0 : default:
6517 0 : gfc_internal_error ("match_attr_spec(): Bad attribute");
6518 : }
6519 :
6520 119119 : if (!t)
6521 : {
6522 35 : m = MATCH_ERROR;
6523 35 : goto cleanup;
6524 : }
6525 : }
6526 :
6527 : /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
6528 183928 : if ((gfc_current_state () == COMP_MODULE
6529 183928 : || gfc_current_state () == COMP_SUBMODULE)
6530 5791 : && !current_attr.save
6531 5609 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
6532 5517 : current_attr.save = SAVE_IMPLICIT;
6533 :
6534 183928 : colon_seen = 1;
6535 183928 : return MATCH_YES;
6536 :
6537 33927 : cleanup:
6538 33927 : gfc_current_locus = start;
6539 33927 : gfc_free_array_spec (current_as);
6540 33927 : current_as = NULL;
6541 33927 : attr_seen = 0;
6542 33927 : return m;
6543 : }
6544 :
6545 :
6546 : /* Set the binding label, dest_label, either with the binding label
6547 : stored in the given gfc_typespec, ts, or if none was provided, it
6548 : will be the symbol name in all lower case, as required by the draft
6549 : (J3/04-007, section 15.4.1). If a binding label was given and
6550 : there is more than one argument (num_idents), it is an error. */
6551 :
6552 : static bool
6553 346 : set_binding_label (const char **dest_label, const char *sym_name,
6554 : int num_idents)
6555 : {
6556 346 : if (num_idents > 1 && has_name_equals)
6557 : {
6558 4 : gfc_error ("Multiple identifiers provided with "
6559 : "single NAME= specifier at %C");
6560 4 : return false;
6561 : }
6562 :
6563 342 : if (curr_binding_label)
6564 : /* Binding label given; store in temp holder till have sym. */
6565 107 : *dest_label = curr_binding_label;
6566 : else
6567 : {
6568 : /* No binding label given, and the NAME= specifier did not exist,
6569 : which means there was no NAME="". */
6570 235 : if (sym_name != NULL && has_name_equals == 0)
6571 205 : *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
6572 : }
6573 :
6574 : return true;
6575 : }
6576 :
6577 :
6578 : /* Set the status of the given common block as being BIND(C) or not,
6579 : depending on the given parameter, is_bind_c. */
6580 :
6581 : static void
6582 76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
6583 : {
6584 76 : com_block->is_bind_c = is_bind_c;
6585 76 : return;
6586 : }
6587 :
6588 :
6589 : /* Verify that the given gfc_typespec is for a C interoperable type. */
6590 :
6591 : bool
6592 21418 : gfc_verify_c_interop (gfc_typespec *ts)
6593 : {
6594 21418 : if (ts->type == BT_DERIVED && ts->u.derived != NULL)
6595 8629 : return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
6596 17098 : else if (ts->type == BT_CLASS)
6597 : return false;
6598 17090 : else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
6599 3983 : return false;
6600 :
6601 : return true;
6602 : }
6603 :
6604 :
6605 : /* Verify that the variables of a given common block, which has been
6606 : defined with the attribute specifier bind(c), to be of a C
6607 : interoperable type. Errors will be reported here, if
6608 : encountered. */
6609 :
6610 : bool
6611 1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
6612 : {
6613 1 : gfc_symbol *curr_sym = NULL;
6614 1 : bool retval = true;
6615 :
6616 1 : curr_sym = com_block->head;
6617 :
6618 : /* Make sure we have at least one symbol. */
6619 1 : if (curr_sym == NULL)
6620 : return retval;
6621 :
6622 : /* Here we know we have a symbol, so we'll execute this loop
6623 : at least once. */
6624 1 : do
6625 : {
6626 : /* The second to last param, 1, says this is in a common block. */
6627 1 : retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
6628 1 : curr_sym = curr_sym->common_next;
6629 1 : } while (curr_sym != NULL);
6630 :
6631 : return retval;
6632 : }
6633 :
6634 :
6635 : /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
6636 : an appropriate error message is reported. */
6637 :
6638 : bool
6639 7396 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
6640 : int is_in_common, gfc_common_head *com_block)
6641 : {
6642 7396 : bool bind_c_function = false;
6643 7396 : bool retval = true;
6644 :
6645 7396 : if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
6646 7396 : bind_c_function = true;
6647 :
6648 7396 : if (tmp_sym->attr.function && tmp_sym->result != NULL)
6649 : {
6650 3150 : tmp_sym = tmp_sym->result;
6651 : /* Make sure it wasn't an implicitly typed result. */
6652 3150 : if (tmp_sym->attr.implicit_type && warn_c_binding_type)
6653 : {
6654 1 : gfc_warning (OPT_Wc_binding_type,
6655 : "Implicitly declared BIND(C) function %qs at "
6656 : "%L may not be C interoperable", tmp_sym->name,
6657 : &tmp_sym->declared_at);
6658 1 : tmp_sym->ts.f90_type = tmp_sym->ts.type;
6659 : /* Mark it as C interoperable to prevent duplicate warnings. */
6660 1 : tmp_sym->ts.is_c_interop = 1;
6661 1 : tmp_sym->attr.is_c_interop = 1;
6662 : }
6663 : }
6664 :
6665 : /* Here, we know we have the bind(c) attribute, so if we have
6666 : enough type info, then verify that it's a C interop kind.
6667 : The info could be in the symbol already, or possibly still in
6668 : the given ts (current_ts), so look in both. */
6669 7396 : if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
6670 : {
6671 3308 : if (!gfc_verify_c_interop (&(tmp_sym->ts)))
6672 : {
6673 : /* See if we're dealing with a sym in a common block or not. */
6674 237 : if (is_in_common == 1 && warn_c_binding_type)
6675 : {
6676 0 : gfc_warning (OPT_Wc_binding_type,
6677 : "Variable %qs in common block %qs at %L "
6678 : "may not be a C interoperable "
6679 : "kind though common block %qs is BIND(C)",
6680 : tmp_sym->name, com_block->name,
6681 0 : &(tmp_sym->declared_at), com_block->name);
6682 : }
6683 : else
6684 : {
6685 237 : if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
6686 235 : || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
6687 : {
6688 3 : gfc_error ("Type declaration %qs at %L is not C "
6689 : "interoperable but it is BIND(C)",
6690 : tmp_sym->name, &(tmp_sym->declared_at));
6691 3 : retval = false;
6692 : }
6693 234 : else if (warn_c_binding_type)
6694 3 : gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
6695 : "may not be a C interoperable "
6696 : "kind but it is BIND(C)",
6697 : tmp_sym->name, &(tmp_sym->declared_at));
6698 : }
6699 : }
6700 :
6701 : /* Variables declared w/in a common block can't be bind(c)
6702 : since there's no way for C to see these variables, so there's
6703 : semantically no reason for the attribute. */
6704 3308 : if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
6705 : {
6706 1 : gfc_error ("Variable %qs in common block %qs at "
6707 : "%L cannot be declared with BIND(C) "
6708 : "since it is not a global",
6709 1 : tmp_sym->name, com_block->name,
6710 : &(tmp_sym->declared_at));
6711 1 : retval = false;
6712 : }
6713 :
6714 : /* Scalar variables that are bind(c) cannot have the pointer
6715 : or allocatable attributes. */
6716 3308 : if (tmp_sym->attr.is_bind_c == 1)
6717 : {
6718 2770 : if (tmp_sym->attr.pointer == 1)
6719 : {
6720 1 : gfc_error ("Variable %qs at %L cannot have both the "
6721 : "POINTER and BIND(C) attributes",
6722 : tmp_sym->name, &(tmp_sym->declared_at));
6723 1 : retval = false;
6724 : }
6725 :
6726 2770 : if (tmp_sym->attr.allocatable == 1)
6727 : {
6728 0 : gfc_error ("Variable %qs at %L cannot have both the "
6729 : "ALLOCATABLE and BIND(C) attributes",
6730 : tmp_sym->name, &(tmp_sym->declared_at));
6731 0 : retval = false;
6732 : }
6733 :
6734 : }
6735 :
6736 : /* If it is a BIND(C) function, make sure the return value is a
6737 : scalar value. The previous tests in this function made sure
6738 : the type is interoperable. */
6739 3308 : if (bind_c_function && tmp_sym->as != NULL)
6740 2 : gfc_error ("Return type of BIND(C) function %qs at %L cannot "
6741 : "be an array", tmp_sym->name, &(tmp_sym->declared_at));
6742 :
6743 : /* BIND(C) functions cannot return a character string. */
6744 3150 : if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
6745 116 : if (!gfc_length_one_character_type_p (&tmp_sym->ts))
6746 4 : gfc_error ("Return type of BIND(C) function %qs of character "
6747 : "type at %L must have length 1", tmp_sym->name,
6748 : &(tmp_sym->declared_at));
6749 : }
6750 :
6751 : /* See if the symbol has been marked as private. If it has, warn if
6752 : there is a binding label with default binding name. */
6753 7396 : if (tmp_sym->attr.access == ACCESS_PRIVATE
6754 11 : && tmp_sym->binding_label
6755 8 : && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
6756 5 : && (tmp_sym->attr.flavor == FL_VARIABLE
6757 4 : || tmp_sym->attr.if_source == IFSRC_DECL))
6758 4 : gfc_warning (OPT_Wsurprising,
6759 : "Symbol %qs at %L is marked PRIVATE but is accessible "
6760 : "via its default binding name %qs", tmp_sym->name,
6761 : &(tmp_sym->declared_at), tmp_sym->binding_label);
6762 :
6763 7396 : return retval;
6764 : }
6765 :
6766 :
6767 : /* Set the appropriate fields for a symbol that's been declared as
6768 : BIND(C) (the is_bind_c flag and the binding label), and verify that
6769 : the type is C interoperable. Errors are reported by the functions
6770 : used to set/test these fields. */
6771 :
6772 : static bool
6773 47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
6774 : {
6775 47 : bool retval = true;
6776 :
6777 : /* TODO: Do we need to make sure the vars aren't marked private? */
6778 :
6779 : /* Set the is_bind_c bit in symbol_attribute. */
6780 47 : gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
6781 :
6782 47 : if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
6783 : return false;
6784 :
6785 : return retval;
6786 : }
6787 :
6788 :
6789 : /* Set the fields marking the given common block as BIND(C), including
6790 : a binding label, and report any errors encountered. */
6791 :
6792 : static bool
6793 76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
6794 : {
6795 76 : bool retval = true;
6796 :
6797 : /* destLabel, common name, typespec (which may have binding label). */
6798 76 : if (!set_binding_label (&com_block->binding_label, com_block->name,
6799 : num_idents))
6800 : return false;
6801 :
6802 : /* Set the given common block (com_block) to being bind(c) (1). */
6803 76 : set_com_block_bind_c (com_block, 1);
6804 :
6805 76 : return retval;
6806 : }
6807 :
6808 :
6809 : /* Retrieve the list of one or more identifiers that the given bind(c)
6810 : attribute applies to. */
6811 :
6812 : static bool
6813 102 : get_bind_c_idents (void)
6814 : {
6815 102 : char name[GFC_MAX_SYMBOL_LEN + 1];
6816 102 : int num_idents = 0;
6817 102 : gfc_symbol *tmp_sym = NULL;
6818 102 : match found_id;
6819 102 : gfc_common_head *com_block = NULL;
6820 :
6821 102 : if (gfc_match_name (name) == MATCH_YES)
6822 : {
6823 38 : found_id = MATCH_YES;
6824 38 : gfc_get_ha_symbol (name, &tmp_sym);
6825 : }
6826 64 : else if (gfc_match_common_name (name) == MATCH_YES)
6827 : {
6828 64 : found_id = MATCH_YES;
6829 64 : com_block = gfc_get_common (name, 0);
6830 : }
6831 : else
6832 : {
6833 0 : gfc_error ("Need either entity or common block name for "
6834 : "attribute specification statement at %C");
6835 0 : return false;
6836 : }
6837 :
6838 : /* Save the current identifier and look for more. */
6839 123 : do
6840 : {
6841 : /* Increment the number of identifiers found for this spec stmt. */
6842 123 : num_idents++;
6843 :
6844 : /* Make sure we have a sym or com block, and verify that it can
6845 : be bind(c). Set the appropriate field(s) and look for more
6846 : identifiers. */
6847 123 : if (tmp_sym != NULL || com_block != NULL)
6848 : {
6849 123 : if (tmp_sym != NULL)
6850 : {
6851 47 : if (!set_verify_bind_c_sym (tmp_sym, num_idents))
6852 : return false;
6853 : }
6854 : else
6855 : {
6856 76 : if (!set_verify_bind_c_com_block (com_block, num_idents))
6857 : return false;
6858 : }
6859 :
6860 : /* Look to see if we have another identifier. */
6861 122 : tmp_sym = NULL;
6862 122 : if (gfc_match_eos () == MATCH_YES)
6863 : found_id = MATCH_NO;
6864 21 : else if (gfc_match_char (',') != MATCH_YES)
6865 : found_id = MATCH_NO;
6866 21 : else if (gfc_match_name (name) == MATCH_YES)
6867 : {
6868 9 : found_id = MATCH_YES;
6869 9 : gfc_get_ha_symbol (name, &tmp_sym);
6870 : }
6871 12 : else if (gfc_match_common_name (name) == MATCH_YES)
6872 : {
6873 12 : found_id = MATCH_YES;
6874 12 : com_block = gfc_get_common (name, 0);
6875 : }
6876 : else
6877 : {
6878 0 : gfc_error ("Missing entity or common block name for "
6879 : "attribute specification statement at %C");
6880 0 : return false;
6881 : }
6882 : }
6883 : else
6884 : {
6885 0 : gfc_internal_error ("Missing symbol");
6886 : }
6887 122 : } while (found_id == MATCH_YES);
6888 :
6889 : /* if we get here we were successful */
6890 : return true;
6891 : }
6892 :
6893 :
6894 : /* Try and match a BIND(C) attribute specification statement. */
6895 :
6896 : match
6897 140 : gfc_match_bind_c_stmt (void)
6898 : {
6899 140 : match found_match = MATCH_NO;
6900 140 : gfc_typespec *ts;
6901 :
6902 140 : ts = ¤t_ts;
6903 :
6904 : /* This may not be necessary. */
6905 140 : gfc_clear_ts (ts);
6906 : /* Clear the temporary binding label holder. */
6907 140 : curr_binding_label = NULL;
6908 :
6909 : /* Look for the bind(c). */
6910 140 : found_match = gfc_match_bind_c (NULL, true);
6911 :
6912 140 : if (found_match == MATCH_YES)
6913 : {
6914 103 : if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6915 : return MATCH_ERROR;
6916 :
6917 : /* Look for the :: now, but it is not required. */
6918 102 : gfc_match (" :: ");
6919 :
6920 : /* Get the identifier(s) that needs to be updated. This may need to
6921 : change to hand the flag(s) for the attr specified so all identifiers
6922 : found can have all appropriate parts updated (assuming that the same
6923 : spec stmt can have multiple attrs, such as both bind(c) and
6924 : allocatable...). */
6925 102 : if (!get_bind_c_idents ())
6926 : /* Error message should have printed already. */
6927 : return MATCH_ERROR;
6928 : }
6929 :
6930 : return found_match;
6931 : }
6932 :
6933 :
6934 : /* Match a data declaration statement. */
6935 :
6936 : match
6937 1028418 : gfc_match_data_decl (void)
6938 : {
6939 1028418 : gfc_symbol *sym;
6940 1028418 : match m;
6941 1028418 : int elem;
6942 1028418 : gfc_component *comp_tail = NULL;
6943 :
6944 1028418 : type_param_spec_list = NULL;
6945 1028418 : decl_type_param_list = NULL;
6946 :
6947 1028418 : num_idents_on_line = 0;
6948 :
6949 : /* Record the last component before we start, so that we can roll back
6950 : any components added during this statement on error. PR106946.
6951 : Must be set before any 'goto cleanup' with m == MATCH_ERROR. */
6952 1028418 : if (gfc_comp_struct (gfc_current_state ()))
6953 : {
6954 31793 : gfc_symbol *block = gfc_current_block ();
6955 31793 : if (block)
6956 : {
6957 31793 : comp_tail = block->components;
6958 31793 : if (comp_tail)
6959 33065 : while (comp_tail->next)
6960 : comp_tail = comp_tail->next;
6961 : }
6962 : }
6963 :
6964 1028418 : m = gfc_match_decl_type_spec (¤t_ts, 0);
6965 1028418 : if (m != MATCH_YES)
6966 : return m;
6967 :
6968 216688 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6969 35097 : && !gfc_comp_struct (gfc_current_state ()))
6970 : {
6971 31729 : sym = gfc_use_derived (current_ts.u.derived);
6972 :
6973 31729 : if (sym == NULL)
6974 : {
6975 22 : m = MATCH_ERROR;
6976 22 : goto cleanup;
6977 : }
6978 :
6979 31707 : current_ts.u.derived = sym;
6980 : }
6981 :
6982 216666 : m = match_attr_spec ();
6983 216666 : if (m == MATCH_ERROR)
6984 : {
6985 84 : m = MATCH_NO;
6986 84 : goto cleanup;
6987 : }
6988 :
6989 : /* F2018:C708. */
6990 216582 : if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
6991 : {
6992 6 : gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
6993 6 : m = MATCH_ERROR;
6994 6 : goto cleanup;
6995 : }
6996 :
6997 216576 : if (current_ts.type == BT_CLASS
6998 10914 : && current_ts.u.derived->attr.unlimited_polymorphic)
6999 1914 : goto ok;
7000 :
7001 214662 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
7002 33154 : && current_ts.u.derived->components == NULL
7003 2823 : && !current_ts.u.derived->attr.zero_comp)
7004 : {
7005 :
7006 210 : if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
7007 136 : goto ok;
7008 :
7009 74 : if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
7010 47 : goto ok;
7011 :
7012 27 : gfc_find_symbol (current_ts.u.derived->name,
7013 27 : current_ts.u.derived->ns, 1, &sym);
7014 :
7015 : /* Any symbol that we find had better be a type definition
7016 : which has its components defined, or be a structure definition
7017 : actively being parsed. */
7018 27 : if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
7019 26 : && (current_ts.u.derived->components != NULL
7020 26 : || current_ts.u.derived->attr.zero_comp
7021 26 : || current_ts.u.derived == gfc_new_block))
7022 26 : goto ok;
7023 :
7024 1 : gfc_error ("Derived type at %C has not been previously defined "
7025 : "and so cannot appear in a derived type definition");
7026 1 : m = MATCH_ERROR;
7027 1 : goto cleanup;
7028 : }
7029 :
7030 214452 : ok:
7031 : /* If we have an old-style character declaration, and no new-style
7032 : attribute specifications, then there a comma is optional between
7033 : the type specification and the variable list. */
7034 216575 : if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
7035 1407 : gfc_match_char (',');
7036 :
7037 : /* Give the types/attributes to symbols that follow. Give the element
7038 : a number so that repeat character length expressions can be copied. */
7039 : elem = 1;
7040 281844 : for (;;)
7041 : {
7042 281844 : num_idents_on_line++;
7043 281844 : m = variable_decl (elem++);
7044 281842 : if (m == MATCH_ERROR)
7045 415 : goto cleanup;
7046 281427 : if (m == MATCH_NO)
7047 : break;
7048 :
7049 281416 : if (gfc_match_eos () == MATCH_YES)
7050 216123 : goto cleanup;
7051 65293 : if (gfc_match_char (',') != MATCH_YES)
7052 : break;
7053 : }
7054 :
7055 35 : if (!gfc_error_flag_test ())
7056 : {
7057 : /* An anonymous structure declaration is unambiguous; if we matched one
7058 : according to gfc_match_structure_decl, we need to return MATCH_YES
7059 : here to avoid confusing the remaining matchers, even if there was an
7060 : error during variable_decl. We must flush any such errors. Note this
7061 : causes the parser to gracefully continue parsing the remaining input
7062 : as a structure body, which likely follows. */
7063 11 : if (current_ts.type == BT_DERIVED && current_ts.u.derived
7064 1 : && gfc_fl_struct (current_ts.u.derived->attr.flavor))
7065 : {
7066 1 : gfc_error_now ("Syntax error in anonymous structure declaration"
7067 : " at %C");
7068 : /* Skip the bad variable_decl and line up for the start of the
7069 : structure body. */
7070 1 : gfc_error_recovery ();
7071 1 : m = MATCH_YES;
7072 1 : goto cleanup;
7073 : }
7074 :
7075 10 : gfc_error ("Syntax error in data declaration at %C");
7076 : }
7077 :
7078 34 : m = MATCH_ERROR;
7079 :
7080 34 : gfc_free_data_all (gfc_current_ns);
7081 :
7082 216686 : cleanup:
7083 : /* If we failed inside a derived type definition, remove any CLASS
7084 : components that were added during this failed statement. For CLASS
7085 : components, gfc_build_class_symbol creates an extra container symbol in
7086 : the namespace outside the normal undo machinery. When reject_statement
7087 : later calls gfc_undo_symbols, the declaration state is rolled back but
7088 : that helper symbol survives and leaves the component dangling. Ordinary
7089 : components do not create that extra helper symbol, so leave them in
7090 : place for the usual follow-up diagnostics. PR106946.
7091 :
7092 : CLASS containers are shared between components of the same class type
7093 : and attributes (gfc_build_class_symbol reuses existing containers).
7094 : We must not free a container that is still referenced by a previously
7095 : committed component. Unlink and free the components first, then clean
7096 : up only orphaned containers. PR124482. */
7097 216686 : if (m == MATCH_ERROR && gfc_comp_struct (gfc_current_state ()))
7098 : {
7099 86 : gfc_symbol *block = gfc_current_block ();
7100 86 : if (block)
7101 : {
7102 86 : gfc_component **prev;
7103 86 : if (comp_tail)
7104 43 : prev = &comp_tail->next;
7105 : else
7106 43 : prev = &block->components;
7107 :
7108 : /* Record the CLASS container from the removed components.
7109 : Normally all components in one declaration share a single
7110 : container, but per-variable array specs can produce
7111 : additional ones; any beyond the first are harmlessly
7112 : leaked until namespace destruction. */
7113 86 : gfc_symbol *fclass_container = NULL;
7114 :
7115 120 : while (*prev)
7116 : {
7117 34 : gfc_component *c = *prev;
7118 34 : if (c->ts.type == BT_CLASS && c->ts.u.derived
7119 6 : && c->ts.u.derived->attr.is_class)
7120 : {
7121 3 : *prev = c->next;
7122 3 : if (!fclass_container)
7123 3 : fclass_container = c->ts.u.derived;
7124 3 : c->ts.u.derived = NULL;
7125 3 : gfc_free_component (c);
7126 : }
7127 : else
7128 31 : prev = &c->next;
7129 : }
7130 :
7131 : /* Free the container only if no remaining component still
7132 : references it. CLASS containers are shared between
7133 : components of the same class type and attributes
7134 : (gfc_build_class_symbol reuses existing ones). */
7135 86 : if (fclass_container)
7136 : {
7137 3 : bool shared = false;
7138 3 : for (gfc_component *q = block->components; q; q = q->next)
7139 1 : if (q->ts.type == BT_CLASS
7140 1 : && q->ts.u.derived == fclass_container)
7141 : {
7142 : shared = true;
7143 : break;
7144 : }
7145 3 : if (!shared)
7146 : {
7147 2 : if (gfc_find_symtree (fclass_container->ns->sym_root,
7148 : fclass_container->name))
7149 2 : gfc_delete_symtree (&fclass_container->ns->sym_root,
7150 : fclass_container->name);
7151 2 : gfc_release_symbol (fclass_container);
7152 : }
7153 : }
7154 : }
7155 : }
7156 :
7157 216686 : if (saved_kind_expr)
7158 180 : gfc_free_expr (saved_kind_expr);
7159 216686 : if (type_param_spec_list)
7160 985 : gfc_free_actual_arglist (type_param_spec_list);
7161 216686 : if (decl_type_param_list)
7162 942 : gfc_free_actual_arglist (decl_type_param_list);
7163 216686 : saved_kind_expr = NULL;
7164 216686 : gfc_free_array_spec (current_as);
7165 216686 : current_as = NULL;
7166 216686 : return m;
7167 : }
7168 :
7169 : static bool
7170 24567 : in_module_or_interface(void)
7171 : {
7172 24567 : if (gfc_current_state () == COMP_MODULE
7173 24567 : || gfc_current_state () == COMP_SUBMODULE
7174 24567 : || gfc_current_state () == COMP_INTERFACE)
7175 : return true;
7176 :
7177 20560 : if (gfc_state_stack->state == COMP_CONTAINS
7178 19678 : || gfc_state_stack->state == COMP_FUNCTION
7179 19572 : || gfc_state_stack->state == COMP_SUBROUTINE)
7180 : {
7181 988 : gfc_state_data *p;
7182 1032 : for (p = gfc_state_stack->previous; p ; p = p->previous)
7183 : {
7184 1028 : if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
7185 118 : || p->state == COMP_INTERFACE)
7186 : return true;
7187 : }
7188 : }
7189 : return false;
7190 : }
7191 :
7192 : /* Match a prefix associated with a function or subroutine
7193 : declaration. If the typespec pointer is nonnull, then a typespec
7194 : can be matched. Note that if nothing matches, MATCH_YES is
7195 : returned (the null string was matched). */
7196 :
7197 : match
7198 243357 : gfc_match_prefix (gfc_typespec *ts)
7199 : {
7200 243357 : bool seen_type;
7201 243357 : bool seen_impure;
7202 243357 : bool found_prefix;
7203 :
7204 243357 : gfc_clear_attr (¤t_attr);
7205 243357 : seen_type = false;
7206 243357 : seen_impure = false;
7207 :
7208 243357 : gcc_assert (!gfc_matching_prefix);
7209 243357 : gfc_matching_prefix = true;
7210 :
7211 253260 : do
7212 : {
7213 273136 : found_prefix = false;
7214 :
7215 : /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
7216 : corresponding attribute seems natural and distinguishes these
7217 : procedures from procedure types of PROC_MODULE, which these are
7218 : as well. */
7219 273136 : if (gfc_match ("module% ") == MATCH_YES)
7220 : {
7221 24842 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
7222 275 : goto error;
7223 :
7224 24567 : if (!in_module_or_interface ())
7225 : {
7226 19576 : gfc_error ("MODULE prefix at %C found outside of a module, "
7227 : "submodule, or interface");
7228 19576 : goto error;
7229 : }
7230 :
7231 4991 : current_attr.module_procedure = 1;
7232 4991 : found_prefix = true;
7233 : }
7234 :
7235 253285 : if (!seen_type && ts != NULL)
7236 : {
7237 136479 : match m;
7238 136479 : m = gfc_match_decl_type_spec (ts, 0);
7239 136479 : if (m == MATCH_ERROR)
7240 15 : goto error;
7241 136464 : if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
7242 : {
7243 : seen_type = true;
7244 : found_prefix = true;
7245 : }
7246 : }
7247 :
7248 253270 : if (gfc_match ("elemental% ") == MATCH_YES)
7249 : {
7250 5377 : if (!gfc_add_elemental (¤t_attr, NULL))
7251 2 : goto error;
7252 :
7253 : found_prefix = true;
7254 : }
7255 :
7256 253268 : if (gfc_match ("pure% ") == MATCH_YES)
7257 : {
7258 2454 : if (!gfc_add_pure (¤t_attr, NULL))
7259 2 : goto error;
7260 :
7261 : found_prefix = true;
7262 : }
7263 :
7264 253266 : if (gfc_match ("recursive% ") == MATCH_YES)
7265 : {
7266 469 : if (!gfc_add_recursive (¤t_attr, NULL))
7267 2 : goto error;
7268 :
7269 : found_prefix = true;
7270 : }
7271 :
7272 : /* IMPURE is a somewhat special case, as it needs not set an actual
7273 : attribute but rather only prevents ELEMENTAL routines from being
7274 : automatically PURE. */
7275 253264 : if (gfc_match ("impure% ") == MATCH_YES)
7276 : {
7277 729 : if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
7278 4 : goto error;
7279 :
7280 : seen_impure = true;
7281 : found_prefix = true;
7282 : }
7283 : }
7284 : while (found_prefix);
7285 :
7286 : /* IMPURE and PURE must not both appear, of course. */
7287 223481 : if (seen_impure && current_attr.pure)
7288 : {
7289 4 : gfc_error ("PURE and IMPURE must not appear both at %C");
7290 4 : goto error;
7291 : }
7292 :
7293 : /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
7294 222756 : if (!seen_impure && current_attr.elemental && !current_attr.pure)
7295 : {
7296 4682 : if (!gfc_add_pure (¤t_attr, NULL))
7297 0 : goto error;
7298 : }
7299 :
7300 : /* At this point, the next item is not a prefix. */
7301 223477 : gcc_assert (gfc_matching_prefix);
7302 :
7303 223477 : gfc_matching_prefix = false;
7304 223477 : return MATCH_YES;
7305 :
7306 19880 : error:
7307 19880 : gcc_assert (gfc_matching_prefix);
7308 19880 : gfc_matching_prefix = false;
7309 19880 : return MATCH_ERROR;
7310 : }
7311 :
7312 :
7313 : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
7314 :
7315 : static bool
7316 63416 : copy_prefix (symbol_attribute *dest, locus *where)
7317 : {
7318 63416 : if (dest->module_procedure)
7319 : {
7320 732 : if (current_attr.elemental)
7321 13 : dest->elemental = 1;
7322 :
7323 732 : if (current_attr.pure)
7324 61 : dest->pure = 1;
7325 :
7326 732 : if (current_attr.recursive)
7327 8 : dest->recursive = 1;
7328 :
7329 : /* Module procedures are unusual in that the 'dest' is copied from
7330 : the interface declaration. However, this is an opportunity to
7331 : check that the submodule declaration is compliant with the
7332 : interface. */
7333 732 : if (dest->elemental && !current_attr.elemental)
7334 : {
7335 1 : gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
7336 : "missing at %L", where);
7337 1 : return false;
7338 : }
7339 :
7340 731 : if (dest->pure && !current_attr.pure)
7341 : {
7342 1 : gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
7343 : "missing at %L", where);
7344 1 : return false;
7345 : }
7346 :
7347 730 : if (dest->recursive && !current_attr.recursive)
7348 : {
7349 1 : gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
7350 : "missing at %L", where);
7351 1 : return false;
7352 : }
7353 :
7354 : return true;
7355 : }
7356 :
7357 62684 : if (current_attr.elemental && !gfc_add_elemental (dest, where))
7358 : return false;
7359 :
7360 62682 : if (current_attr.pure && !gfc_add_pure (dest, where))
7361 : return false;
7362 :
7363 62682 : if (current_attr.recursive && !gfc_add_recursive (dest, where))
7364 : return false;
7365 :
7366 : return true;
7367 : }
7368 :
7369 :
7370 : /* Match a formal argument list or, if typeparam is true, a
7371 : type_param_name_list. */
7372 :
7373 : match
7374 489269 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
7375 : int null_flag, bool typeparam)
7376 : {
7377 489269 : gfc_formal_arglist *head, *tail, *p, *q;
7378 489269 : char name[GFC_MAX_SYMBOL_LEN + 1];
7379 489269 : gfc_symbol *sym;
7380 489269 : match m;
7381 489269 : gfc_formal_arglist *formal = NULL;
7382 :
7383 489269 : head = tail = NULL;
7384 :
7385 : /* Keep the interface formal argument list and null it so that the
7386 : matching for the new declaration can be done. The numbers and
7387 : names of the arguments are checked here. The interface formal
7388 : arguments are retained in formal_arglist and the characteristics
7389 : are compared in resolve.cc(resolve_fl_procedure). See the remark
7390 : in get_proc_name about the eventual need to copy the formal_arglist
7391 : and populate the formal namespace of the interface symbol. */
7392 489269 : if (progname->attr.module_procedure
7393 736 : && progname->attr.host_assoc)
7394 : {
7395 196 : formal = progname->formal;
7396 196 : progname->formal = NULL;
7397 : }
7398 :
7399 489269 : if (gfc_match_char ('(') != MATCH_YES)
7400 : {
7401 289251 : if (null_flag)
7402 6696 : goto ok;
7403 : return MATCH_NO;
7404 : }
7405 :
7406 200018 : if (gfc_match_char (')') == MATCH_YES)
7407 : {
7408 10461 : if (typeparam)
7409 : {
7410 1 : gfc_error_now ("A type parameter list is required at %C");
7411 1 : m = MATCH_ERROR;
7412 1 : goto cleanup;
7413 : }
7414 : else
7415 10460 : goto ok;
7416 : }
7417 :
7418 251878 : for (;;)
7419 : {
7420 251878 : gfc_gobble_whitespace ();
7421 251878 : if (gfc_match_char ('*') == MATCH_YES)
7422 : {
7423 10356 : sym = NULL;
7424 10356 : if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
7425 : "Alternate-return argument at %C"))
7426 : {
7427 1 : m = MATCH_ERROR;
7428 1 : goto cleanup;
7429 : }
7430 10355 : else if (typeparam)
7431 2 : gfc_error_now ("A parameter name is required at %C");
7432 : }
7433 : else
7434 : {
7435 241522 : locus loc = gfc_current_locus;
7436 241522 : m = gfc_match_name (name);
7437 241522 : if (m != MATCH_YES)
7438 : {
7439 16662 : if(typeparam)
7440 1 : gfc_error_now ("A parameter name is required at %C");
7441 16678 : goto cleanup;
7442 : }
7443 224860 : loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
7444 :
7445 224860 : if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
7446 16 : goto cleanup;
7447 224844 : else if (typeparam
7448 224844 : && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
7449 0 : goto cleanup;
7450 : }
7451 :
7452 235199 : p = gfc_get_formal_arglist ();
7453 :
7454 235199 : if (head == NULL)
7455 : head = tail = p;
7456 : else
7457 : {
7458 61618 : tail->next = p;
7459 61618 : tail = p;
7460 : }
7461 :
7462 235199 : tail->sym = sym;
7463 :
7464 : /* We don't add the VARIABLE flavor because the name could be a
7465 : dummy procedure. We don't apply these attributes to formal
7466 : arguments of statement functions. */
7467 224844 : if (sym != NULL && !st_flag
7468 336397 : && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
7469 101198 : || !gfc_missing_attr (&sym->attr, NULL)))
7470 : {
7471 0 : m = MATCH_ERROR;
7472 0 : goto cleanup;
7473 : }
7474 :
7475 : /* The name of a program unit can be in a different namespace,
7476 : so check for it explicitly. After the statement is accepted,
7477 : the name is checked for especially in gfc_get_symbol(). */
7478 235199 : if (gfc_new_block != NULL && sym != NULL && !typeparam
7479 99934 : && strcmp (sym->name, gfc_new_block->name) == 0)
7480 : {
7481 0 : gfc_error ("Name %qs at %C is the name of the procedure",
7482 : sym->name);
7483 0 : m = MATCH_ERROR;
7484 0 : goto cleanup;
7485 : }
7486 :
7487 235199 : if (gfc_match_char (')') == MATCH_YES)
7488 124691 : goto ok;
7489 :
7490 110508 : m = gfc_match_char (',');
7491 110508 : if (m != MATCH_YES)
7492 : {
7493 48187 : if (typeparam)
7494 1 : gfc_error_now ("Expected parameter list in type declaration "
7495 : "at %C");
7496 : else
7497 48186 : gfc_error ("Unexpected junk in formal argument list at %C");
7498 48187 : goto cleanup;
7499 : }
7500 : }
7501 :
7502 141847 : ok:
7503 : /* Check for duplicate symbols in the formal argument list. */
7504 141847 : if (head != NULL)
7505 : {
7506 184686 : for (p = head; p->next; p = p->next)
7507 : {
7508 60043 : if (p->sym == NULL)
7509 338 : continue;
7510 :
7511 236791 : for (q = p->next; q; q = q->next)
7512 177134 : if (p->sym == q->sym)
7513 : {
7514 48 : if (typeparam)
7515 1 : gfc_error_now ("Duplicate name %qs in parameter "
7516 : "list at %C", p->sym->name);
7517 : else
7518 47 : gfc_error ("Duplicate symbol %qs in formal argument "
7519 : "list at %C", p->sym->name);
7520 :
7521 48 : m = MATCH_ERROR;
7522 48 : goto cleanup;
7523 : }
7524 : }
7525 : }
7526 :
7527 141799 : if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
7528 : {
7529 0 : m = MATCH_ERROR;
7530 0 : goto cleanup;
7531 : }
7532 :
7533 : /* gfc_error_now used in following and return with MATCH_YES because
7534 : doing otherwise results in a cascade of extraneous errors and in
7535 : some cases an ICE in symbol.cc(gfc_release_symbol). */
7536 141799 : if (progname->attr.module_procedure && progname->attr.host_assoc)
7537 : {
7538 195 : bool arg_count_mismatch = false;
7539 :
7540 195 : if (!formal && head)
7541 : arg_count_mismatch = true;
7542 :
7543 : /* Abbreviated module procedure declaration is not meant to have any
7544 : formal arguments! */
7545 195 : if (!progname->abr_modproc_decl && formal && !head)
7546 1 : arg_count_mismatch = true;
7547 :
7548 377 : for (p = formal, q = head; p && q; p = p->next, q = q->next)
7549 : {
7550 182 : if ((p->next != NULL && q->next == NULL)
7551 181 : || (p->next == NULL && q->next != NULL))
7552 : arg_count_mismatch = true;
7553 180 : else if ((p->sym == NULL && q->sym == NULL)
7554 180 : || (p->sym && q->sym
7555 178 : && strcmp (p->sym->name, q->sym->name) == 0))
7556 176 : continue;
7557 : else
7558 : {
7559 4 : if (q->sym == NULL)
7560 1 : gfc_error_now ("MODULE PROCEDURE formal argument %qs "
7561 : "conflicts with alternate return at %C",
7562 : p->sym->name);
7563 3 : else if (p->sym == NULL)
7564 1 : gfc_error_now ("MODULE PROCEDURE formal argument is "
7565 : "alternate return and conflicts with "
7566 : "%qs in the separate declaration at %C",
7567 : q->sym->name);
7568 : else
7569 2 : gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
7570 : "argument names (%s/%s) at %C",
7571 : p->sym->name, q->sym->name);
7572 : }
7573 : }
7574 :
7575 195 : if (arg_count_mismatch)
7576 4 : gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
7577 : "formal arguments at %C");
7578 : }
7579 :
7580 : return MATCH_YES;
7581 :
7582 64915 : cleanup:
7583 64915 : gfc_free_formal_arglist (head);
7584 64915 : return m;
7585 : }
7586 :
7587 :
7588 : /* Match a RESULT specification following a function declaration or
7589 : ENTRY statement. Also matches the end-of-statement. */
7590 :
7591 : static match
7592 8607 : match_result (gfc_symbol *function, gfc_symbol **result)
7593 : {
7594 8607 : char name[GFC_MAX_SYMBOL_LEN + 1];
7595 8607 : gfc_symbol *r;
7596 8607 : match m;
7597 :
7598 8607 : if (gfc_match (" result (") != MATCH_YES)
7599 : return MATCH_NO;
7600 :
7601 6051 : m = gfc_match_name (name);
7602 6051 : if (m != MATCH_YES)
7603 : return m;
7604 :
7605 : /* Get the right paren, and that's it because there could be the
7606 : bind(c) attribute after the result clause. */
7607 6051 : if (gfc_match_char (')') != MATCH_YES)
7608 : {
7609 : /* TODO: should report the missing right paren here. */
7610 : return MATCH_ERROR;
7611 : }
7612 :
7613 6051 : if (strcmp (function->name, name) == 0)
7614 : {
7615 1 : gfc_error ("RESULT variable at %C must be different than function name");
7616 1 : return MATCH_ERROR;
7617 : }
7618 :
7619 6050 : if (gfc_get_symbol (name, NULL, &r))
7620 : return MATCH_ERROR;
7621 :
7622 6050 : if (!gfc_add_result (&r->attr, r->name, NULL))
7623 : return MATCH_ERROR;
7624 :
7625 6050 : *result = r;
7626 :
7627 6050 : return MATCH_YES;
7628 : }
7629 :
7630 :
7631 : /* Match a function suffix, which could be a combination of a result
7632 : clause and BIND(C), either one, or neither. The draft does not
7633 : require them to come in a specific order. */
7634 :
7635 : static match
7636 8611 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
7637 : {
7638 8611 : match is_bind_c; /* Found bind(c). */
7639 8611 : match is_result; /* Found result clause. */
7640 8611 : match found_match; /* Status of whether we've found a good match. */
7641 8611 : char peek_char; /* Character we're going to peek at. */
7642 8611 : bool allow_binding_name;
7643 :
7644 : /* Initialize to having found nothing. */
7645 8611 : found_match = MATCH_NO;
7646 8611 : is_bind_c = MATCH_NO;
7647 8611 : is_result = MATCH_NO;
7648 :
7649 : /* Get the next char to narrow between result and bind(c). */
7650 8611 : gfc_gobble_whitespace ();
7651 8611 : peek_char = gfc_peek_ascii_char ();
7652 :
7653 : /* C binding names are not allowed for internal procedures. */
7654 8611 : if (gfc_current_state () == COMP_CONTAINS
7655 4797 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
7656 : allow_binding_name = false;
7657 : else
7658 6913 : allow_binding_name = true;
7659 :
7660 8611 : switch (peek_char)
7661 : {
7662 5680 : case 'r':
7663 : /* Look for result clause. */
7664 5680 : is_result = match_result (sym, result);
7665 5680 : if (is_result == MATCH_YES)
7666 : {
7667 : /* Now see if there is a bind(c) after it. */
7668 5679 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7669 : /* We've found the result clause and possibly bind(c). */
7670 5679 : found_match = MATCH_YES;
7671 : }
7672 : else
7673 : /* This should only be MATCH_ERROR. */
7674 : found_match = is_result;
7675 : break;
7676 2931 : case 'b':
7677 : /* Look for bind(c) first. */
7678 2931 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7679 2931 : if (is_bind_c == MATCH_YES)
7680 : {
7681 : /* Now see if a result clause followed it. */
7682 2927 : is_result = match_result (sym, result);
7683 2927 : found_match = MATCH_YES;
7684 : }
7685 : else
7686 : {
7687 : /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
7688 : found_match = MATCH_ERROR;
7689 : }
7690 : break;
7691 0 : default:
7692 0 : gfc_error ("Unexpected junk after function declaration at %C");
7693 0 : found_match = MATCH_ERROR;
7694 0 : break;
7695 : }
7696 :
7697 8606 : if (is_bind_c == MATCH_YES)
7698 : {
7699 : /* Fortran 2008 draft allows BIND(C) for internal procedures. */
7700 3094 : if (gfc_current_state () == COMP_CONTAINS
7701 423 : && sym->ns->proc_name->attr.flavor != FL_MODULE
7702 3112 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7703 : "at %L may not be specified for an internal "
7704 : "procedure", &gfc_current_locus))
7705 : return MATCH_ERROR;
7706 :
7707 3091 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
7708 : return MATCH_ERROR;
7709 : }
7710 :
7711 : return found_match;
7712 : }
7713 :
7714 :
7715 : /* Procedure pointer return value without RESULT statement:
7716 : Add "hidden" result variable named "ppr@". */
7717 :
7718 : static bool
7719 75088 : add_hidden_procptr_result (gfc_symbol *sym)
7720 : {
7721 75088 : bool case1,case2;
7722 :
7723 75088 : if (gfc_notification_std (GFC_STD_F2003) == ERROR)
7724 : return false;
7725 :
7726 : /* First usage case: PROCEDURE and EXTERNAL statements. */
7727 1538 : case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
7728 1538 : && strcmp (gfc_current_block ()->name, sym->name) == 0
7729 75486 : && sym->attr.external;
7730 : /* Second usage case: INTERFACE statements. */
7731 14862 : case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
7732 14862 : && gfc_state_stack->previous->state == COMP_FUNCTION
7733 75135 : && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
7734 :
7735 74904 : if (case1 || case2)
7736 : {
7737 124 : gfc_symtree *stree;
7738 124 : if (case1)
7739 94 : gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
7740 : else
7741 : {
7742 30 : gfc_symtree *st2;
7743 30 : gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
7744 30 : st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
7745 30 : st2->n.sym = stree->n.sym;
7746 30 : stree->n.sym->refs++;
7747 : }
7748 124 : sym->result = stree->n.sym;
7749 :
7750 124 : sym->result->attr.proc_pointer = sym->attr.proc_pointer;
7751 124 : sym->result->attr.pointer = sym->attr.pointer;
7752 124 : sym->result->attr.external = sym->attr.external;
7753 124 : sym->result->attr.referenced = sym->attr.referenced;
7754 124 : sym->result->ts = sym->ts;
7755 124 : sym->attr.proc_pointer = 0;
7756 124 : sym->attr.pointer = 0;
7757 124 : sym->attr.external = 0;
7758 124 : if (sym->result->attr.external && sym->result->attr.pointer)
7759 : {
7760 4 : sym->result->attr.pointer = 0;
7761 4 : sym->result->attr.proc_pointer = 1;
7762 : }
7763 :
7764 124 : return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
7765 : }
7766 : /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
7767 74810 : else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
7768 411 : && sym->result && sym->result != sym && sym->result->attr.external
7769 28 : && sym == gfc_current_ns->proc_name
7770 28 : && sym == sym->result->ns->proc_name
7771 28 : && strcmp ("ppr@", sym->result->name) == 0)
7772 : {
7773 28 : sym->result->attr.proc_pointer = 1;
7774 28 : sym->attr.pointer = 0;
7775 28 : return true;
7776 : }
7777 : else
7778 : return false;
7779 : }
7780 :
7781 :
7782 : /* Match the interface for a PROCEDURE declaration,
7783 : including brackets (R1212). */
7784 :
7785 : static match
7786 1622 : match_procedure_interface (gfc_symbol **proc_if)
7787 : {
7788 1622 : match m;
7789 1622 : gfc_symtree *st;
7790 1622 : locus old_loc, entry_loc;
7791 1622 : gfc_namespace *old_ns = gfc_current_ns;
7792 1622 : char name[GFC_MAX_SYMBOL_LEN + 1];
7793 :
7794 1622 : old_loc = entry_loc = gfc_current_locus;
7795 1622 : gfc_clear_ts (¤t_ts);
7796 :
7797 1622 : if (gfc_match (" (") != MATCH_YES)
7798 : {
7799 1 : gfc_current_locus = entry_loc;
7800 1 : return MATCH_NO;
7801 : }
7802 :
7803 : /* Get the type spec. for the procedure interface. */
7804 1621 : old_loc = gfc_current_locus;
7805 1621 : m = gfc_match_decl_type_spec (¤t_ts, 0);
7806 1621 : gfc_gobble_whitespace ();
7807 1621 : if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
7808 395 : goto got_ts;
7809 :
7810 1226 : if (m == MATCH_ERROR)
7811 : return m;
7812 :
7813 : /* Procedure interface is itself a procedure. */
7814 1226 : gfc_current_locus = old_loc;
7815 1226 : m = gfc_match_name (name);
7816 :
7817 : /* First look to see if it is already accessible in the current
7818 : namespace because it is use associated or contained. */
7819 1226 : st = NULL;
7820 1226 : if (gfc_find_sym_tree (name, NULL, 0, &st))
7821 : return MATCH_ERROR;
7822 :
7823 : /* If it is still not found, then try the parent namespace, if it
7824 : exists and create the symbol there if it is still not found. */
7825 1226 : if (gfc_current_ns->parent)
7826 427 : gfc_current_ns = gfc_current_ns->parent;
7827 1226 : if (st == NULL && gfc_get_ha_sym_tree (name, &st))
7828 : return MATCH_ERROR;
7829 :
7830 1226 : gfc_current_ns = old_ns;
7831 1226 : *proc_if = st->n.sym;
7832 :
7833 1226 : if (*proc_if)
7834 : {
7835 1226 : (*proc_if)->refs++;
7836 : /* Resolve interface if possible. That way, attr.procedure is only set
7837 : if it is declared by a later procedure-declaration-stmt, which is
7838 : invalid per F08:C1216 (cf. resolve_procedure_interface). */
7839 1226 : while ((*proc_if)->ts.interface
7840 1233 : && *proc_if != (*proc_if)->ts.interface)
7841 7 : *proc_if = (*proc_if)->ts.interface;
7842 :
7843 1226 : if ((*proc_if)->attr.flavor == FL_UNKNOWN
7844 389 : && (*proc_if)->ts.type == BT_UNKNOWN
7845 1615 : && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
7846 : (*proc_if)->name, NULL))
7847 : return MATCH_ERROR;
7848 : }
7849 :
7850 0 : got_ts:
7851 1621 : if (gfc_match (" )") != MATCH_YES)
7852 : {
7853 0 : gfc_current_locus = entry_loc;
7854 0 : return MATCH_NO;
7855 : }
7856 :
7857 : return MATCH_YES;
7858 : }
7859 :
7860 :
7861 : /* Match a PROCEDURE declaration (R1211). */
7862 :
7863 : static match
7864 1189 : match_procedure_decl (void)
7865 : {
7866 1189 : match m;
7867 1189 : gfc_symbol *sym, *proc_if = NULL;
7868 1189 : int num;
7869 1189 : gfc_expr *initializer = NULL;
7870 :
7871 : /* Parse interface (with brackets). */
7872 1189 : m = match_procedure_interface (&proc_if);
7873 1189 : if (m != MATCH_YES)
7874 : return m;
7875 :
7876 : /* Parse attributes (with colons). */
7877 1189 : m = match_attr_spec();
7878 1189 : if (m == MATCH_ERROR)
7879 : return MATCH_ERROR;
7880 :
7881 1188 : if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
7882 : {
7883 53 : current_attr.is_bind_c = 1;
7884 53 : has_name_equals = 0;
7885 53 : curr_binding_label = NULL;
7886 : }
7887 :
7888 : /* Get procedure symbols. */
7889 79 : for(num=1;;num++)
7890 : {
7891 1267 : m = gfc_match_symbol (&sym, 0);
7892 1267 : if (m == MATCH_NO)
7893 1 : goto syntax;
7894 1266 : else if (m == MATCH_ERROR)
7895 : return m;
7896 :
7897 : /* Add current_attr to the symbol attributes. */
7898 1266 : if (!gfc_copy_attr (&sym->attr, ¤t_attr, NULL))
7899 : return MATCH_ERROR;
7900 :
7901 1264 : if (sym->attr.is_bind_c)
7902 : {
7903 : /* Check for C1218. */
7904 90 : if (!proc_if || !proc_if->attr.is_bind_c)
7905 : {
7906 1 : gfc_error ("BIND(C) attribute at %C requires "
7907 : "an interface with BIND(C)");
7908 1 : return MATCH_ERROR;
7909 : }
7910 : /* Check for C1217. */
7911 89 : if (has_name_equals && sym->attr.pointer)
7912 : {
7913 1 : gfc_error ("BIND(C) procedure with NAME may not have "
7914 : "POINTER attribute at %C");
7915 1 : return MATCH_ERROR;
7916 : }
7917 88 : if (has_name_equals && sym->attr.dummy)
7918 : {
7919 1 : gfc_error ("Dummy procedure at %C may not have "
7920 : "BIND(C) attribute with NAME");
7921 1 : return MATCH_ERROR;
7922 : }
7923 : /* Set binding label for BIND(C). */
7924 87 : if (!set_binding_label (&sym->binding_label, sym->name, num))
7925 : return MATCH_ERROR;
7926 : }
7927 :
7928 1260 : if (!gfc_add_external (&sym->attr, NULL))
7929 : return MATCH_ERROR;
7930 :
7931 1256 : if (add_hidden_procptr_result (sym))
7932 67 : sym = sym->result;
7933 :
7934 1256 : if (!gfc_add_proc (&sym->attr, sym->name, NULL))
7935 : return MATCH_ERROR;
7936 :
7937 : /* Set interface. */
7938 1255 : if (proc_if != NULL)
7939 : {
7940 912 : if (sym->ts.type != BT_UNKNOWN)
7941 : {
7942 1 : gfc_error ("Procedure %qs at %L already has basic type of %s",
7943 : sym->name, &gfc_current_locus,
7944 : gfc_basic_typename (sym->ts.type));
7945 1 : return MATCH_ERROR;
7946 : }
7947 911 : sym->ts.interface = proc_if;
7948 911 : sym->attr.untyped = 1;
7949 911 : sym->attr.if_source = IFSRC_IFBODY;
7950 : }
7951 343 : else if (current_ts.type != BT_UNKNOWN)
7952 : {
7953 199 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
7954 : return MATCH_ERROR;
7955 198 : sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7956 198 : sym->ts.interface->ts = current_ts;
7957 198 : sym->ts.interface->attr.flavor = FL_PROCEDURE;
7958 198 : sym->ts.interface->attr.function = 1;
7959 198 : sym->attr.function = 1;
7960 198 : sym->attr.if_source = IFSRC_UNKNOWN;
7961 : }
7962 :
7963 1253 : if (gfc_match (" =>") == MATCH_YES)
7964 : {
7965 110 : if (!current_attr.pointer)
7966 : {
7967 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
7968 0 : m = MATCH_ERROR;
7969 0 : goto cleanup;
7970 : }
7971 :
7972 110 : m = match_pointer_init (&initializer, 1);
7973 110 : if (m != MATCH_YES)
7974 1 : goto cleanup;
7975 :
7976 109 : if (!add_init_expr_to_sym (sym->name, &initializer,
7977 : &gfc_current_locus,
7978 : gfc_current_ns->cl_list))
7979 0 : goto cleanup;
7980 :
7981 : }
7982 :
7983 1252 : if (gfc_match_eos () == MATCH_YES)
7984 : return MATCH_YES;
7985 79 : if (gfc_match_char (',') != MATCH_YES)
7986 0 : goto syntax;
7987 : }
7988 :
7989 1 : syntax:
7990 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
7991 1 : return MATCH_ERROR;
7992 :
7993 1 : cleanup:
7994 : /* Free stuff up and return. */
7995 1 : gfc_free_expr (initializer);
7996 1 : return m;
7997 : }
7998 :
7999 :
8000 : static match
8001 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
8002 :
8003 :
8004 : /* Match a procedure pointer component declaration (R445). */
8005 :
8006 : static match
8007 433 : match_ppc_decl (void)
8008 : {
8009 433 : match m;
8010 433 : gfc_symbol *proc_if = NULL;
8011 433 : gfc_typespec ts;
8012 433 : int num;
8013 433 : gfc_component *c;
8014 433 : gfc_expr *initializer = NULL;
8015 433 : gfc_typebound_proc* tb;
8016 433 : char name[GFC_MAX_SYMBOL_LEN + 1];
8017 :
8018 : /* Parse interface (with brackets). */
8019 433 : m = match_procedure_interface (&proc_if);
8020 433 : if (m != MATCH_YES)
8021 1 : goto syntax;
8022 :
8023 : /* Parse attributes. */
8024 432 : tb = XCNEW (gfc_typebound_proc);
8025 432 : tb->where = gfc_current_locus;
8026 432 : m = match_binding_attributes (tb, false, true);
8027 432 : if (m == MATCH_ERROR)
8028 : return m;
8029 :
8030 429 : gfc_clear_attr (¤t_attr);
8031 429 : current_attr.procedure = 1;
8032 429 : current_attr.proc_pointer = 1;
8033 429 : current_attr.access = tb->access;
8034 429 : current_attr.flavor = FL_PROCEDURE;
8035 :
8036 : /* Match the colons (required). */
8037 429 : if (gfc_match (" ::") != MATCH_YES)
8038 : {
8039 1 : gfc_error ("Expected %<::%> after binding-attributes at %C");
8040 1 : return MATCH_ERROR;
8041 : }
8042 :
8043 : /* Check for C450. */
8044 428 : if (!tb->nopass && proc_if == NULL)
8045 : {
8046 2 : gfc_error("NOPASS or explicit interface required at %C");
8047 2 : return MATCH_ERROR;
8048 : }
8049 :
8050 426 : if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
8051 : return MATCH_ERROR;
8052 :
8053 : /* Match PPC names. */
8054 425 : ts = current_ts;
8055 425 : for(num=1;;num++)
8056 : {
8057 426 : m = gfc_match_name (name);
8058 426 : if (m == MATCH_NO)
8059 0 : goto syntax;
8060 426 : else if (m == MATCH_ERROR)
8061 : return m;
8062 :
8063 426 : if (!gfc_add_component (gfc_current_block(), name, &c))
8064 : return MATCH_ERROR;
8065 :
8066 : /* Add current_attr to the symbol attributes. */
8067 426 : if (!gfc_copy_attr (&c->attr, ¤t_attr, NULL))
8068 : return MATCH_ERROR;
8069 :
8070 426 : if (!gfc_add_external (&c->attr, NULL))
8071 : return MATCH_ERROR;
8072 :
8073 426 : if (!gfc_add_proc (&c->attr, name, NULL))
8074 : return MATCH_ERROR;
8075 :
8076 426 : if (num == 1)
8077 425 : c->tb = tb;
8078 : else
8079 : {
8080 1 : c->tb = XCNEW (gfc_typebound_proc);
8081 1 : c->tb->where = gfc_current_locus;
8082 1 : *c->tb = *tb;
8083 : }
8084 :
8085 426 : if (saved_kind_expr)
8086 0 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
8087 :
8088 : /* Set interface. */
8089 426 : if (proc_if != NULL)
8090 : {
8091 359 : c->ts.interface = proc_if;
8092 359 : c->attr.untyped = 1;
8093 359 : c->attr.if_source = IFSRC_IFBODY;
8094 : }
8095 67 : else if (ts.type != BT_UNKNOWN)
8096 : {
8097 29 : c->ts = ts;
8098 29 : c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
8099 29 : c->ts.interface->result = c->ts.interface;
8100 29 : c->ts.interface->ts = ts;
8101 29 : c->ts.interface->attr.flavor = FL_PROCEDURE;
8102 29 : c->ts.interface->attr.function = 1;
8103 29 : c->attr.function = 1;
8104 29 : c->attr.if_source = IFSRC_UNKNOWN;
8105 : }
8106 :
8107 426 : if (gfc_match (" =>") == MATCH_YES)
8108 : {
8109 73 : m = match_pointer_init (&initializer, 1);
8110 73 : if (m != MATCH_YES)
8111 : {
8112 0 : gfc_free_expr (initializer);
8113 0 : return m;
8114 : }
8115 73 : c->initializer = initializer;
8116 : }
8117 :
8118 426 : if (gfc_match_eos () == MATCH_YES)
8119 : return MATCH_YES;
8120 1 : if (gfc_match_char (',') != MATCH_YES)
8121 0 : goto syntax;
8122 : }
8123 :
8124 1 : syntax:
8125 1 : gfc_error ("Syntax error in procedure pointer component at %C");
8126 1 : return MATCH_ERROR;
8127 : }
8128 :
8129 :
8130 : /* Match a PROCEDURE declaration inside an interface (R1206). */
8131 :
8132 : static match
8133 1561 : match_procedure_in_interface (void)
8134 : {
8135 1561 : match m;
8136 1561 : gfc_symbol *sym;
8137 1561 : char name[GFC_MAX_SYMBOL_LEN + 1];
8138 1561 : locus old_locus;
8139 :
8140 1561 : if (current_interface.type == INTERFACE_NAMELESS
8141 1561 : || current_interface.type == INTERFACE_ABSTRACT)
8142 : {
8143 1 : gfc_error ("PROCEDURE at %C must be in a generic interface");
8144 1 : return MATCH_ERROR;
8145 : }
8146 :
8147 : /* Check if the F2008 optional double colon appears. */
8148 1560 : gfc_gobble_whitespace ();
8149 1560 : old_locus = gfc_current_locus;
8150 1560 : if (gfc_match ("::") == MATCH_YES)
8151 : {
8152 875 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
8153 : "MODULE PROCEDURE statement at %L", &old_locus))
8154 : return MATCH_ERROR;
8155 : }
8156 : else
8157 685 : gfc_current_locus = old_locus;
8158 :
8159 2214 : for(;;)
8160 : {
8161 2214 : m = gfc_match_name (name);
8162 2214 : if (m == MATCH_NO)
8163 0 : goto syntax;
8164 2214 : else if (m == MATCH_ERROR)
8165 : return m;
8166 2214 : if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
8167 : return MATCH_ERROR;
8168 :
8169 2214 : if (!gfc_add_interface (sym))
8170 : return MATCH_ERROR;
8171 :
8172 2213 : if (gfc_match_eos () == MATCH_YES)
8173 : break;
8174 655 : if (gfc_match_char (',') != MATCH_YES)
8175 0 : goto syntax;
8176 : }
8177 :
8178 : return MATCH_YES;
8179 :
8180 0 : syntax:
8181 0 : gfc_error ("Syntax error in PROCEDURE statement at %C");
8182 0 : return MATCH_ERROR;
8183 : }
8184 :
8185 :
8186 : /* General matcher for PROCEDURE declarations. */
8187 :
8188 : static match match_procedure_in_type (void);
8189 :
8190 : match
8191 6419 : gfc_match_procedure (void)
8192 : {
8193 6419 : match m;
8194 :
8195 6419 : switch (gfc_current_state ())
8196 : {
8197 1189 : case COMP_NONE:
8198 1189 : case COMP_PROGRAM:
8199 1189 : case COMP_MODULE:
8200 1189 : case COMP_SUBMODULE:
8201 1189 : case COMP_SUBROUTINE:
8202 1189 : case COMP_FUNCTION:
8203 1189 : case COMP_BLOCK:
8204 1189 : m = match_procedure_decl ();
8205 1189 : break;
8206 1561 : case COMP_INTERFACE:
8207 1561 : m = match_procedure_in_interface ();
8208 1561 : break;
8209 433 : case COMP_DERIVED:
8210 433 : m = match_ppc_decl ();
8211 433 : break;
8212 3236 : case COMP_DERIVED_CONTAINS:
8213 3236 : m = match_procedure_in_type ();
8214 3236 : break;
8215 : default:
8216 : return MATCH_NO;
8217 : }
8218 :
8219 6419 : if (m != MATCH_YES)
8220 : return m;
8221 :
8222 6363 : if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
8223 4 : return MATCH_ERROR;
8224 :
8225 : return m;
8226 : }
8227 :
8228 :
8229 : /* Warn if a matched procedure has the same name as an intrinsic; this is
8230 : simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
8231 : parser-state-stack to find out whether we're in a module. */
8232 :
8233 : static void
8234 63413 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
8235 : {
8236 63413 : bool in_module;
8237 :
8238 126826 : in_module = (gfc_state_stack->previous
8239 63413 : && (gfc_state_stack->previous->state == COMP_MODULE
8240 51675 : || gfc_state_stack->previous->state == COMP_SUBMODULE));
8241 :
8242 63413 : gfc_warn_intrinsic_shadow (sym, in_module, func);
8243 63413 : }
8244 :
8245 :
8246 : /* Match a function declaration. */
8247 :
8248 : match
8249 129727 : gfc_match_function_decl (void)
8250 : {
8251 129727 : char name[GFC_MAX_SYMBOL_LEN + 1];
8252 129727 : gfc_symbol *sym, *result;
8253 129727 : locus old_loc;
8254 129727 : match m;
8255 129727 : match suffix_match;
8256 129727 : match found_match; /* Status returned by match func. */
8257 :
8258 129727 : if (gfc_current_state () != COMP_NONE
8259 81837 : && gfc_current_state () != COMP_INTERFACE
8260 52440 : && gfc_current_state () != COMP_CONTAINS)
8261 : return MATCH_NO;
8262 :
8263 129727 : gfc_clear_ts (¤t_ts);
8264 :
8265 129727 : old_loc = gfc_current_locus;
8266 :
8267 129727 : m = gfc_match_prefix (¤t_ts);
8268 129727 : if (m != MATCH_YES)
8269 : {
8270 9942 : gfc_current_locus = old_loc;
8271 9942 : return m;
8272 : }
8273 :
8274 119785 : if (gfc_match ("function% %n", name) != MATCH_YES)
8275 : {
8276 99659 : gfc_current_locus = old_loc;
8277 99659 : return MATCH_NO;
8278 : }
8279 :
8280 20126 : if (get_proc_name (name, &sym, false))
8281 : return MATCH_ERROR;
8282 :
8283 20121 : if (add_hidden_procptr_result (sym))
8284 20 : sym = sym->result;
8285 :
8286 20121 : if (current_attr.module_procedure)
8287 : {
8288 304 : sym->attr.module_procedure = 1;
8289 304 : if (gfc_current_state () == COMP_INTERFACE)
8290 215 : gfc_current_ns->has_import_set = 1;
8291 : }
8292 :
8293 20121 : gfc_new_block = sym;
8294 :
8295 20121 : m = gfc_match_formal_arglist (sym, 0, 0);
8296 20121 : if (m == MATCH_NO)
8297 : {
8298 6 : gfc_error ("Expected formal argument list in function "
8299 : "definition at %C");
8300 6 : m = MATCH_ERROR;
8301 6 : goto cleanup;
8302 : }
8303 20115 : else if (m == MATCH_ERROR)
8304 0 : goto cleanup;
8305 :
8306 20115 : result = NULL;
8307 :
8308 : /* According to the draft, the bind(c) and result clause can
8309 : come in either order after the formal_arg_list (i.e., either
8310 : can be first, both can exist together or by themselves or neither
8311 : one). Therefore, the match_result can't match the end of the
8312 : string, and check for the bind(c) or result clause in either order. */
8313 20115 : found_match = gfc_match_eos ();
8314 :
8315 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8316 : must have been marked BIND(C) with a BIND(C) attribute and that is
8317 : not allowed for procedures. */
8318 20115 : if (sym->attr.is_bind_c == 1)
8319 : {
8320 3 : sym->attr.is_bind_c = 0;
8321 :
8322 3 : if (gfc_state_stack->previous
8323 3 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8324 : {
8325 1 : locus loc;
8326 1 : loc = sym->old_symbol != NULL
8327 1 : ? sym->old_symbol->declared_at : gfc_current_locus;
8328 1 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8329 : "variables or common blocks", &loc);
8330 : }
8331 : }
8332 :
8333 20115 : if (found_match != MATCH_YES)
8334 : {
8335 : /* If we haven't found the end-of-statement, look for a suffix. */
8336 8362 : suffix_match = gfc_match_suffix (sym, &result);
8337 8362 : if (suffix_match == MATCH_YES)
8338 : /* Need to get the eos now. */
8339 8354 : found_match = gfc_match_eos ();
8340 : else
8341 : found_match = suffix_match;
8342 : }
8343 :
8344 : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8345 : subprogram and a binding label is specified, it shall be the
8346 : same as the binding label specified in the corresponding module
8347 : procedure interface body. */
8348 20115 : if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
8349 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8350 3 : && sym->binding_label && sym->old_symbol->binding_label
8351 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8352 : {
8353 1 : const char *null = "NULL", *s1, *s2;
8354 1 : s1 = sym->binding_label;
8355 1 : if (!s1) s1 = null;
8356 1 : s2 = sym->old_symbol->binding_label;
8357 1 : if (!s2) s2 = null;
8358 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8359 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8360 1 : return MATCH_ERROR;
8361 : }
8362 :
8363 20114 : if(found_match != MATCH_YES)
8364 : m = MATCH_ERROR;
8365 : else
8366 : {
8367 : /* Make changes to the symbol. */
8368 20106 : m = MATCH_ERROR;
8369 :
8370 20106 : if (!gfc_add_function (&sym->attr, sym->name, NULL))
8371 0 : goto cleanup;
8372 :
8373 20106 : if (!gfc_missing_attr (&sym->attr, NULL))
8374 0 : goto cleanup;
8375 :
8376 20106 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8377 : {
8378 1 : if(!sym->attr.module_procedure)
8379 1 : goto cleanup;
8380 : else
8381 0 : gfc_error_check ();
8382 : }
8383 :
8384 : /* Delay matching the function characteristics until after the
8385 : specification block by signalling kind=-1. */
8386 20105 : sym->declared_at = old_loc;
8387 20105 : if (current_ts.type != BT_UNKNOWN)
8388 6936 : current_ts.kind = -1;
8389 : else
8390 13169 : current_ts.kind = 0;
8391 :
8392 20105 : if (result == NULL)
8393 : {
8394 14267 : if (current_ts.type != BT_UNKNOWN
8395 14267 : && !gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
8396 1 : goto cleanup;
8397 14266 : sym->result = sym;
8398 : }
8399 : else
8400 : {
8401 5838 : if (current_ts.type != BT_UNKNOWN
8402 5838 : && !gfc_add_type (result, ¤t_ts, &gfc_current_locus))
8403 0 : goto cleanup;
8404 5838 : sym->result = result;
8405 : }
8406 :
8407 : /* Warn if this procedure has the same name as an intrinsic. */
8408 20104 : do_warn_intrinsic_shadow (sym, true);
8409 :
8410 20104 : return MATCH_YES;
8411 : }
8412 :
8413 16 : cleanup:
8414 16 : gfc_current_locus = old_loc;
8415 16 : return m;
8416 : }
8417 :
8418 :
8419 : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
8420 : pass the name of the entry, rather than the gfc_current_block name, and
8421 : to return false upon finding an existing global entry. */
8422 :
8423 : static bool
8424 539 : add_global_entry (const char *name, const char *binding_label, bool sub,
8425 : locus *where)
8426 : {
8427 539 : gfc_gsymbol *s;
8428 539 : enum gfc_symbol_type type;
8429 :
8430 539 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
8431 :
8432 : /* Only in Fortran 2003: For procedures with a binding label also the Fortran
8433 : name is a global identifier. */
8434 539 : if (!binding_label || gfc_notification_std (GFC_STD_F2008))
8435 : {
8436 516 : s = gfc_get_gsymbol (name, false);
8437 :
8438 516 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8439 : {
8440 2 : gfc_global_used (s, where);
8441 2 : return false;
8442 : }
8443 : else
8444 : {
8445 514 : s->type = type;
8446 514 : s->sym_name = name;
8447 514 : s->where = *where;
8448 514 : s->defined = 1;
8449 514 : s->ns = gfc_current_ns;
8450 : }
8451 : }
8452 :
8453 : /* Don't add the symbol multiple times. */
8454 537 : if (binding_label
8455 537 : && (!gfc_notification_std (GFC_STD_F2008)
8456 0 : || strcmp (name, binding_label) != 0))
8457 : {
8458 23 : s = gfc_get_gsymbol (binding_label, true);
8459 :
8460 23 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8461 : {
8462 1 : gfc_global_used (s, where);
8463 1 : return false;
8464 : }
8465 : else
8466 : {
8467 22 : s->type = type;
8468 22 : s->sym_name = gfc_get_string ("%s", name);
8469 22 : s->binding_label = binding_label;
8470 22 : s->where = *where;
8471 22 : s->defined = 1;
8472 22 : s->ns = gfc_current_ns;
8473 : }
8474 : }
8475 :
8476 : return true;
8477 : }
8478 :
8479 :
8480 : /* Match an ENTRY statement. */
8481 :
8482 : match
8483 805 : gfc_match_entry (void)
8484 : {
8485 805 : gfc_symbol *proc;
8486 805 : gfc_symbol *result;
8487 805 : gfc_symbol *entry;
8488 805 : char name[GFC_MAX_SYMBOL_LEN + 1];
8489 805 : gfc_compile_state state;
8490 805 : match m;
8491 805 : gfc_entry_list *el;
8492 805 : locus old_loc;
8493 805 : bool module_procedure;
8494 805 : char peek_char;
8495 805 : match is_bind_c;
8496 :
8497 805 : m = gfc_match_name (name);
8498 805 : if (m != MATCH_YES)
8499 : return m;
8500 :
8501 805 : if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
8502 : return MATCH_ERROR;
8503 :
8504 805 : state = gfc_current_state ();
8505 805 : if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
8506 : {
8507 3 : switch (state)
8508 : {
8509 0 : case COMP_PROGRAM:
8510 0 : gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
8511 0 : break;
8512 0 : case COMP_MODULE:
8513 0 : gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
8514 0 : break;
8515 0 : case COMP_SUBMODULE:
8516 0 : gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
8517 0 : break;
8518 0 : case COMP_BLOCK_DATA:
8519 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8520 : "a BLOCK DATA");
8521 0 : break;
8522 0 : case COMP_INTERFACE:
8523 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8524 : "an INTERFACE");
8525 0 : break;
8526 1 : case COMP_STRUCTURE:
8527 1 : gfc_error ("ENTRY statement at %C cannot appear within "
8528 : "a STRUCTURE block");
8529 1 : break;
8530 0 : case COMP_DERIVED:
8531 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8532 : "a DERIVED TYPE block");
8533 0 : break;
8534 0 : case COMP_IF:
8535 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8536 : "an IF-THEN block");
8537 0 : break;
8538 0 : case COMP_DO:
8539 0 : case COMP_DO_CONCURRENT:
8540 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8541 : "a DO block");
8542 0 : break;
8543 0 : case COMP_SELECT:
8544 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8545 : "a SELECT block");
8546 0 : break;
8547 0 : case COMP_FORALL:
8548 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8549 : "a FORALL block");
8550 0 : break;
8551 0 : case COMP_WHERE:
8552 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8553 : "a WHERE block");
8554 0 : break;
8555 0 : case COMP_CONTAINS:
8556 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8557 : "a contained subprogram");
8558 0 : break;
8559 2 : default:
8560 2 : gfc_error ("Unexpected ENTRY statement at %C");
8561 : }
8562 3 : return MATCH_ERROR;
8563 : }
8564 :
8565 802 : if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
8566 802 : && gfc_state_stack->previous->state == COMP_INTERFACE)
8567 : {
8568 1 : gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
8569 1 : return MATCH_ERROR;
8570 : }
8571 :
8572 1602 : module_procedure = gfc_current_ns->parent != NULL
8573 260 : && gfc_current_ns->parent->proc_name
8574 801 : && gfc_current_ns->parent->proc_name->attr.flavor
8575 260 : == FL_MODULE;
8576 :
8577 801 : if (gfc_current_ns->parent != NULL
8578 260 : && gfc_current_ns->parent->proc_name
8579 260 : && !module_procedure)
8580 : {
8581 0 : gfc_error("ENTRY statement at %C cannot appear in a "
8582 : "contained procedure");
8583 0 : return MATCH_ERROR;
8584 : }
8585 :
8586 : /* Module function entries need special care in get_proc_name
8587 : because previous references within the function will have
8588 : created symbols attached to the current namespace. */
8589 801 : if (get_proc_name (name, &entry,
8590 : gfc_current_ns->parent != NULL
8591 801 : && module_procedure))
8592 : return MATCH_ERROR;
8593 :
8594 799 : proc = gfc_current_block ();
8595 :
8596 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8597 : must have been marked BIND(C) with a BIND(C) attribute and that is
8598 : not allowed for procedures. */
8599 799 : if (entry->attr.is_bind_c == 1)
8600 : {
8601 0 : locus loc;
8602 :
8603 0 : entry->attr.is_bind_c = 0;
8604 :
8605 0 : loc = entry->old_symbol != NULL
8606 0 : ? entry->old_symbol->declared_at : gfc_current_locus;
8607 0 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8608 : "variables or common blocks", &loc);
8609 : }
8610 :
8611 : /* Check what next non-whitespace character is so we can tell if there
8612 : is the required parens if we have a BIND(C). */
8613 799 : old_loc = gfc_current_locus;
8614 799 : gfc_gobble_whitespace ();
8615 799 : peek_char = gfc_peek_ascii_char ();
8616 :
8617 799 : if (state == COMP_SUBROUTINE)
8618 : {
8619 138 : m = gfc_match_formal_arglist (entry, 0, 1);
8620 138 : if (m != MATCH_YES)
8621 : return MATCH_ERROR;
8622 :
8623 : /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
8624 : never be an internal procedure. */
8625 138 : is_bind_c = gfc_match_bind_c (entry, true);
8626 138 : if (is_bind_c == MATCH_ERROR)
8627 : return MATCH_ERROR;
8628 138 : if (is_bind_c == MATCH_YES)
8629 : {
8630 22 : if (peek_char != '(')
8631 : {
8632 0 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8633 0 : return MATCH_ERROR;
8634 : }
8635 :
8636 22 : if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
8637 22 : &(entry->declared_at), 1))
8638 : return MATCH_ERROR;
8639 :
8640 : }
8641 :
8642 138 : if (!gfc_current_ns->parent
8643 138 : && !add_global_entry (name, entry->binding_label, true,
8644 : &old_loc))
8645 : return MATCH_ERROR;
8646 :
8647 : /* An entry in a subroutine. */
8648 135 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8649 135 : || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
8650 3 : return MATCH_ERROR;
8651 : }
8652 : else
8653 : {
8654 : /* An entry in a function.
8655 : We need to take special care because writing
8656 : ENTRY f()
8657 : as
8658 : ENTRY f
8659 : is allowed, whereas
8660 : ENTRY f() RESULT (r)
8661 : can't be written as
8662 : ENTRY f RESULT (r). */
8663 661 : if (gfc_match_eos () == MATCH_YES)
8664 : {
8665 24 : gfc_current_locus = old_loc;
8666 : /* Match the empty argument list, and add the interface to
8667 : the symbol. */
8668 24 : m = gfc_match_formal_arglist (entry, 0, 1);
8669 : }
8670 : else
8671 637 : m = gfc_match_formal_arglist (entry, 0, 0);
8672 :
8673 661 : if (m != MATCH_YES)
8674 : return MATCH_ERROR;
8675 :
8676 660 : result = NULL;
8677 :
8678 660 : if (gfc_match_eos () == MATCH_YES)
8679 : {
8680 411 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8681 411 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8682 2 : return MATCH_ERROR;
8683 :
8684 409 : entry->result = entry;
8685 : }
8686 : else
8687 : {
8688 249 : m = gfc_match_suffix (entry, &result);
8689 249 : if (m == MATCH_NO)
8690 0 : gfc_syntax_error (ST_ENTRY);
8691 249 : if (m != MATCH_YES)
8692 : return MATCH_ERROR;
8693 :
8694 249 : if (result)
8695 : {
8696 212 : if (!gfc_add_result (&result->attr, result->name, NULL)
8697 212 : || !gfc_add_entry (&entry->attr, result->name, NULL)
8698 424 : || !gfc_add_function (&entry->attr, result->name, NULL))
8699 0 : return MATCH_ERROR;
8700 212 : entry->result = result;
8701 : }
8702 : else
8703 : {
8704 37 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8705 37 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8706 0 : return MATCH_ERROR;
8707 37 : entry->result = entry;
8708 : }
8709 : }
8710 :
8711 658 : if (!gfc_current_ns->parent
8712 658 : && !add_global_entry (name, entry->binding_label, false,
8713 : &old_loc))
8714 : return MATCH_ERROR;
8715 : }
8716 :
8717 790 : if (gfc_match_eos () != MATCH_YES)
8718 : {
8719 0 : gfc_syntax_error (ST_ENTRY);
8720 0 : return MATCH_ERROR;
8721 : }
8722 :
8723 : /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
8724 790 : if (proc->attr.elemental && entry->attr.is_bind_c)
8725 : {
8726 2 : gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
8727 : "elemental procedure", &entry->declared_at);
8728 2 : return MATCH_ERROR;
8729 : }
8730 :
8731 788 : entry->attr.recursive = proc->attr.recursive;
8732 788 : entry->attr.elemental = proc->attr.elemental;
8733 788 : entry->attr.pure = proc->attr.pure;
8734 :
8735 788 : el = gfc_get_entry_list ();
8736 788 : el->sym = entry;
8737 788 : el->next = gfc_current_ns->entries;
8738 788 : gfc_current_ns->entries = el;
8739 788 : if (el->next)
8740 85 : el->id = el->next->id + 1;
8741 : else
8742 703 : el->id = 1;
8743 :
8744 788 : new_st.op = EXEC_ENTRY;
8745 788 : new_st.ext.entry = el;
8746 :
8747 788 : return MATCH_YES;
8748 : }
8749 :
8750 :
8751 : /* Match a subroutine statement, including optional prefixes. */
8752 :
8753 : match
8754 810757 : gfc_match_subroutine (void)
8755 : {
8756 810757 : char name[GFC_MAX_SYMBOL_LEN + 1];
8757 810757 : gfc_symbol *sym;
8758 810757 : match m;
8759 810757 : match is_bind_c;
8760 810757 : char peek_char;
8761 810757 : bool allow_binding_name;
8762 810757 : locus loc;
8763 :
8764 810757 : if (gfc_current_state () != COMP_NONE
8765 768759 : && gfc_current_state () != COMP_INTERFACE
8766 745936 : && gfc_current_state () != COMP_CONTAINS)
8767 : return MATCH_NO;
8768 :
8769 106696 : m = gfc_match_prefix (NULL);
8770 106696 : if (m != MATCH_YES)
8771 : return m;
8772 :
8773 96764 : loc = gfc_current_locus;
8774 96764 : m = gfc_match ("subroutine% %n", name);
8775 96764 : if (m != MATCH_YES)
8776 : return m;
8777 :
8778 43345 : if (get_proc_name (name, &sym, false))
8779 : return MATCH_ERROR;
8780 :
8781 : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8782 : the symbol existed before. */
8783 43334 : sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
8784 : &gfc_current_locus);
8785 :
8786 43334 : if (current_attr.module_procedure)
8787 : {
8788 429 : sym->attr.module_procedure = 1;
8789 429 : if (gfc_current_state () == COMP_INTERFACE)
8790 302 : gfc_current_ns->has_import_set = 1;
8791 : }
8792 :
8793 43334 : if (add_hidden_procptr_result (sym))
8794 9 : sym = sym->result;
8795 :
8796 43334 : gfc_new_block = sym;
8797 :
8798 : /* Check what next non-whitespace character is so we can tell if there
8799 : is the required parens if we have a BIND(C). */
8800 43334 : gfc_gobble_whitespace ();
8801 43334 : peek_char = gfc_peek_ascii_char ();
8802 :
8803 43334 : if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
8804 : return MATCH_ERROR;
8805 :
8806 43331 : if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
8807 : return MATCH_ERROR;
8808 :
8809 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8810 : must have been marked BIND(C) with a BIND(C) attribute and that is
8811 : not allowed for procedures. */
8812 43331 : if (sym->attr.is_bind_c == 1)
8813 : {
8814 4 : sym->attr.is_bind_c = 0;
8815 :
8816 4 : if (gfc_state_stack->previous
8817 4 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8818 : {
8819 2 : locus loc;
8820 2 : loc = sym->old_symbol != NULL
8821 2 : ? sym->old_symbol->declared_at : gfc_current_locus;
8822 2 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8823 : "variables or common blocks", &loc);
8824 : }
8825 : }
8826 :
8827 : /* C binding names are not allowed for internal procedures. */
8828 43331 : if (gfc_current_state () == COMP_CONTAINS
8829 26275 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
8830 : allow_binding_name = false;
8831 : else
8832 28314 : allow_binding_name = true;
8833 :
8834 : /* Here, we are just checking if it has the bind(c) attribute, and if
8835 : so, then we need to make sure it's all correct. If it doesn't,
8836 : we still need to continue matching the rest of the subroutine line. */
8837 43331 : gfc_gobble_whitespace ();
8838 43331 : loc = gfc_current_locus;
8839 43331 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
8840 43331 : if (is_bind_c == MATCH_ERROR)
8841 : {
8842 : /* There was an attempt at the bind(c), but it was wrong. An
8843 : error message should have been printed w/in the gfc_match_bind_c
8844 : so here we'll just return the MATCH_ERROR. */
8845 : return MATCH_ERROR;
8846 : }
8847 :
8848 43318 : if (is_bind_c == MATCH_YES)
8849 : {
8850 4051 : gfc_formal_arglist *arg;
8851 :
8852 : /* The following is allowed in the Fortran 2008 draft. */
8853 4051 : if (gfc_current_state () == COMP_CONTAINS
8854 1297 : && sym->ns->proc_name->attr.flavor != FL_MODULE
8855 4462 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
8856 : "at %L may not be specified for an internal "
8857 : "procedure", &gfc_current_locus))
8858 : return MATCH_ERROR;
8859 :
8860 4048 : if (peek_char != '(')
8861 : {
8862 1 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8863 1 : return MATCH_ERROR;
8864 : }
8865 :
8866 : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8867 : subprogram and a binding label is specified, it shall be the
8868 : same as the binding label specified in the corresponding module
8869 : procedure interface body. */
8870 4047 : if (sym->attr.module_procedure && sym->old_symbol
8871 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8872 3 : && sym->binding_label && sym->old_symbol->binding_label
8873 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8874 : {
8875 1 : const char *null = "NULL", *s1, *s2;
8876 1 : s1 = sym->binding_label;
8877 1 : if (!s1) s1 = null;
8878 1 : s2 = sym->old_symbol->binding_label;
8879 1 : if (!s2) s2 = null;
8880 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8881 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8882 1 : return MATCH_ERROR;
8883 : }
8884 :
8885 : /* Scan the dummy arguments for an alternate return. */
8886 12533 : for (arg = sym->formal; arg; arg = arg->next)
8887 8488 : if (!arg->sym)
8888 : {
8889 1 : gfc_error ("Alternate return dummy argument cannot appear in a "
8890 : "SUBROUTINE with the BIND(C) attribute at %L", &loc);
8891 1 : return MATCH_ERROR;
8892 : }
8893 :
8894 4045 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
8895 : return MATCH_ERROR;
8896 : }
8897 :
8898 43311 : if (gfc_match_eos () != MATCH_YES)
8899 : {
8900 1 : gfc_syntax_error (ST_SUBROUTINE);
8901 1 : return MATCH_ERROR;
8902 : }
8903 :
8904 43310 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8905 : {
8906 4 : if(!sym->attr.module_procedure)
8907 : return MATCH_ERROR;
8908 : else
8909 3 : gfc_error_check ();
8910 : }
8911 :
8912 : /* Warn if it has the same name as an intrinsic. */
8913 43309 : do_warn_intrinsic_shadow (sym, false);
8914 :
8915 43309 : return MATCH_YES;
8916 : }
8917 :
8918 :
8919 : /* Check that the NAME identifier in a BIND attribute or statement
8920 : is conform to C identifier rules. */
8921 :
8922 : match
8923 1185 : check_bind_name_identifier (char **name)
8924 : {
8925 1185 : char *n = *name, *p;
8926 :
8927 : /* Remove leading spaces. */
8928 1211 : while (*n == ' ')
8929 26 : n++;
8930 :
8931 : /* On an empty string, free memory and set name to NULL. */
8932 1185 : if (*n == '\0')
8933 : {
8934 42 : free (*name);
8935 42 : *name = NULL;
8936 42 : return MATCH_YES;
8937 : }
8938 :
8939 : /* Remove trailing spaces. */
8940 1143 : p = n + strlen(n) - 1;
8941 1159 : while (*p == ' ')
8942 16 : *(p--) = '\0';
8943 :
8944 : /* Insert the identifier into the symbol table. */
8945 1143 : p = xstrdup (n);
8946 1143 : free (*name);
8947 1143 : *name = p;
8948 :
8949 : /* Now check that identifier is valid under C rules. */
8950 1143 : if (ISDIGIT (*p))
8951 : {
8952 2 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
8953 2 : return MATCH_ERROR;
8954 : }
8955 :
8956 12496 : for (; *p; p++)
8957 11358 : if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
8958 : {
8959 3 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
8960 3 : return MATCH_ERROR;
8961 : }
8962 :
8963 : return MATCH_YES;
8964 : }
8965 :
8966 :
8967 : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
8968 : given, and set the binding label in either the given symbol (if not
8969 : NULL), or in the current_ts. The symbol may be NULL because we may
8970 : encounter the BIND(C) before the declaration itself. Return
8971 : MATCH_NO if what we're looking at isn't a BIND(C) specifier,
8972 : MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
8973 : or MATCH_YES if the specifier was correct and the binding label and
8974 : bind(c) fields were set correctly for the given symbol or the
8975 : current_ts. If allow_binding_name is false, no binding name may be
8976 : given. */
8977 :
8978 : match
8979 52382 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
8980 : {
8981 52382 : char *binding_label = NULL;
8982 52382 : gfc_expr *e = NULL;
8983 :
8984 : /* Initialize the flag that specifies whether we encountered a NAME=
8985 : specifier or not. */
8986 52382 : has_name_equals = 0;
8987 :
8988 : /* This much we have to be able to match, in this order, if
8989 : there is a bind(c) label. */
8990 52382 : if (gfc_match (" bind ( c ") != MATCH_YES)
8991 : return MATCH_NO;
8992 :
8993 : /* Now see if there is a binding label, or if we've reached the
8994 : end of the bind(c) attribute without one. */
8995 7455 : if (gfc_match_char (',') == MATCH_YES)
8996 : {
8997 1192 : if (gfc_match (" name = ") != MATCH_YES)
8998 : {
8999 1 : gfc_error ("Syntax error in NAME= specifier for binding label "
9000 : "at %C");
9001 : /* should give an error message here */
9002 1 : return MATCH_ERROR;
9003 : }
9004 :
9005 1191 : has_name_equals = 1;
9006 :
9007 1191 : if (gfc_match_init_expr (&e) != MATCH_YES)
9008 : {
9009 2 : gfc_free_expr (e);
9010 2 : return MATCH_ERROR;
9011 : }
9012 :
9013 1189 : if (!gfc_simplify_expr(e, 0))
9014 : {
9015 0 : gfc_error ("NAME= specifier at %C should be a constant expression");
9016 0 : gfc_free_expr (e);
9017 0 : return MATCH_ERROR;
9018 : }
9019 :
9020 1189 : if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
9021 1186 : || e->ts.kind != gfc_default_character_kind || e->rank != 0)
9022 : {
9023 4 : gfc_error ("NAME= specifier at %C should be a scalar of "
9024 : "default character kind");
9025 4 : gfc_free_expr(e);
9026 4 : return MATCH_ERROR;
9027 : }
9028 :
9029 : // Get a C string from the Fortran string constant
9030 2370 : binding_label = gfc_widechar_to_char (e->value.character.string,
9031 1185 : e->value.character.length);
9032 1185 : gfc_free_expr(e);
9033 :
9034 : // Check that it is valid (old gfc_match_name_C)
9035 1185 : if (check_bind_name_identifier (&binding_label) != MATCH_YES)
9036 : return MATCH_ERROR;
9037 : }
9038 :
9039 : /* Get the required right paren. */
9040 7443 : if (gfc_match_char (')') != MATCH_YES)
9041 : {
9042 1 : gfc_error ("Missing closing paren for binding label at %C");
9043 1 : return MATCH_ERROR;
9044 : }
9045 :
9046 7442 : if (has_name_equals && !allow_binding_name)
9047 : {
9048 6 : gfc_error ("No binding name is allowed in BIND(C) at %C");
9049 6 : return MATCH_ERROR;
9050 : }
9051 :
9052 7436 : if (has_name_equals && sym != NULL && sym->attr.dummy)
9053 : {
9054 2 : gfc_error ("For dummy procedure %s, no binding name is "
9055 : "allowed in BIND(C) at %C", sym->name);
9056 2 : return MATCH_ERROR;
9057 : }
9058 :
9059 :
9060 : /* Save the binding label to the symbol. If sym is null, we're
9061 : probably matching the typespec attributes of a declaration and
9062 : haven't gotten the name yet, and therefore, no symbol yet. */
9063 7434 : if (binding_label)
9064 : {
9065 1131 : if (sym != NULL)
9066 1022 : sym->binding_label = binding_label;
9067 : else
9068 109 : curr_binding_label = binding_label;
9069 : }
9070 6303 : else if (allow_binding_name)
9071 : {
9072 : /* No binding label, but if symbol isn't null, we
9073 : can set the label for it here.
9074 : If name="" or allow_binding_name is false, no C binding name is
9075 : created. */
9076 5874 : if (sym != NULL && sym->name != NULL && has_name_equals == 0)
9077 5707 : sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
9078 : }
9079 :
9080 7434 : if (has_name_equals && gfc_current_state () == COMP_INTERFACE
9081 741 : && current_interface.type == INTERFACE_ABSTRACT)
9082 : {
9083 1 : gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
9084 1 : return MATCH_ERROR;
9085 : }
9086 :
9087 : return MATCH_YES;
9088 : }
9089 :
9090 :
9091 : /* Return nonzero if we're currently compiling a contained procedure. */
9092 :
9093 : static int
9094 63737 : contained_procedure (void)
9095 : {
9096 63737 : gfc_state_data *s = gfc_state_stack;
9097 :
9098 63737 : if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
9099 62815 : && s->previous != NULL && s->previous->state == COMP_CONTAINS)
9100 36761 : return 1;
9101 :
9102 : return 0;
9103 : }
9104 :
9105 : /* Set the kind of each enumerator. The kind is selected such that it is
9106 : interoperable with the corresponding C enumeration type, making
9107 : sure that -fshort-enums is honored. */
9108 :
9109 : static void
9110 158 : set_enum_kind(void)
9111 : {
9112 158 : enumerator_history *current_history = NULL;
9113 158 : int kind;
9114 158 : int i;
9115 :
9116 158 : if (max_enum == NULL || enum_history == NULL)
9117 : return;
9118 :
9119 150 : if (!flag_short_enums)
9120 : return;
9121 :
9122 : i = 0;
9123 48 : do
9124 : {
9125 48 : kind = gfc_integer_kinds[i++].kind;
9126 : }
9127 48 : while (kind < gfc_c_int_kind
9128 72 : && gfc_check_integer_range (max_enum->initializer->value.integer,
9129 : kind) != ARITH_OK);
9130 :
9131 24 : current_history = enum_history;
9132 96 : while (current_history != NULL)
9133 : {
9134 72 : current_history->sym->ts.kind = kind;
9135 72 : current_history = current_history->next;
9136 : }
9137 : }
9138 :
9139 :
9140 : /* Match any of the various end-block statements. Returns the type of
9141 : END to the caller. The END INTERFACE, END IF, END DO, END SELECT
9142 : and END BLOCK statements cannot be replaced by a single END statement. */
9143 :
9144 : match
9145 187000 : gfc_match_end (gfc_statement *st)
9146 : {
9147 187000 : char name[GFC_MAX_SYMBOL_LEN + 1];
9148 187000 : gfc_compile_state state;
9149 187000 : locus old_loc;
9150 187000 : const char *block_name;
9151 187000 : const char *target;
9152 187000 : int eos_ok;
9153 187000 : match m;
9154 187000 : gfc_namespace *parent_ns, *ns, *prev_ns;
9155 187000 : gfc_namespace **nsp;
9156 187000 : bool abbreviated_modproc_decl = false;
9157 187000 : bool got_matching_end = false;
9158 :
9159 187000 : old_loc = gfc_current_locus;
9160 187000 : if (gfc_match ("end") != MATCH_YES)
9161 : return MATCH_NO;
9162 :
9163 181854 : state = gfc_current_state ();
9164 99613 : block_name = gfc_current_block () == NULL
9165 181854 : ? NULL : gfc_current_block ()->name;
9166 :
9167 181854 : switch (state)
9168 : {
9169 3118 : case COMP_ASSOCIATE:
9170 3118 : case COMP_BLOCK:
9171 3118 : case COMP_CHANGE_TEAM:
9172 3118 : if (startswith (block_name, "block@"))
9173 : block_name = NULL;
9174 : break;
9175 :
9176 17627 : case COMP_CONTAINS:
9177 17627 : case COMP_DERIVED_CONTAINS:
9178 17627 : case COMP_OMP_BEGIN_METADIRECTIVE:
9179 17627 : state = gfc_state_stack->previous->state;
9180 16073 : block_name = gfc_state_stack->previous->sym == NULL
9181 17627 : ? NULL : gfc_state_stack->previous->sym->name;
9182 17627 : abbreviated_modproc_decl = gfc_state_stack->previous->sym
9183 17627 : && gfc_state_stack->previous->sym->abr_modproc_decl;
9184 : break;
9185 :
9186 : case COMP_OMP_METADIRECTIVE:
9187 : {
9188 : /* Metadirectives can be nested, so we need to drill down to the
9189 : first state that is not COMP_OMP_METADIRECTIVE. */
9190 : gfc_state_data *state_data = gfc_state_stack;
9191 :
9192 85 : do
9193 : {
9194 85 : state_data = state_data->previous;
9195 85 : state = state_data->state;
9196 77 : block_name = (state_data->sym == NULL
9197 85 : ? NULL : state_data->sym->name);
9198 170 : abbreviated_modproc_decl = (state_data->sym
9199 85 : && state_data->sym->abr_modproc_decl);
9200 : }
9201 85 : while (state == COMP_OMP_METADIRECTIVE);
9202 :
9203 83 : if (block_name && startswith (block_name, "block@"))
9204 : block_name = NULL;
9205 : }
9206 : break;
9207 :
9208 : default:
9209 : break;
9210 : }
9211 :
9212 83 : if (!abbreviated_modproc_decl)
9213 181853 : abbreviated_modproc_decl = gfc_current_block ()
9214 181853 : && gfc_current_block ()->abr_modproc_decl;
9215 :
9216 181854 : switch (state)
9217 : {
9218 28113 : case COMP_NONE:
9219 28113 : case COMP_PROGRAM:
9220 28113 : *st = ST_END_PROGRAM;
9221 28113 : target = " program";
9222 28113 : eos_ok = 1;
9223 28113 : break;
9224 :
9225 43499 : case COMP_SUBROUTINE:
9226 43499 : *st = ST_END_SUBROUTINE;
9227 43499 : if (!abbreviated_modproc_decl)
9228 : target = " subroutine";
9229 : else
9230 148 : target = " procedure";
9231 43499 : eos_ok = !contained_procedure ();
9232 43499 : break;
9233 :
9234 20238 : case COMP_FUNCTION:
9235 20238 : *st = ST_END_FUNCTION;
9236 20238 : if (!abbreviated_modproc_decl)
9237 : target = " function";
9238 : else
9239 117 : target = " procedure";
9240 20238 : eos_ok = !contained_procedure ();
9241 20238 : break;
9242 :
9243 87 : case COMP_BLOCK_DATA:
9244 87 : *st = ST_END_BLOCK_DATA;
9245 87 : target = " block data";
9246 87 : eos_ok = 1;
9247 87 : break;
9248 :
9249 9924 : case COMP_MODULE:
9250 9924 : *st = ST_END_MODULE;
9251 9924 : target = " module";
9252 9924 : eos_ok = 1;
9253 9924 : break;
9254 :
9255 268 : case COMP_SUBMODULE:
9256 268 : *st = ST_END_SUBMODULE;
9257 268 : target = " submodule";
9258 268 : eos_ok = 1;
9259 268 : break;
9260 :
9261 11341 : case COMP_INTERFACE:
9262 11341 : *st = ST_END_INTERFACE;
9263 11341 : target = " interface";
9264 11341 : eos_ok = 0;
9265 11341 : break;
9266 :
9267 257 : case COMP_MAP:
9268 257 : *st = ST_END_MAP;
9269 257 : target = " map";
9270 257 : eos_ok = 0;
9271 257 : break;
9272 :
9273 132 : case COMP_UNION:
9274 132 : *st = ST_END_UNION;
9275 132 : target = " union";
9276 132 : eos_ok = 0;
9277 132 : break;
9278 :
9279 313 : case COMP_STRUCTURE:
9280 313 : *st = ST_END_STRUCTURE;
9281 313 : target = " structure";
9282 313 : eos_ok = 0;
9283 313 : break;
9284 :
9285 13062 : case COMP_DERIVED:
9286 13062 : case COMP_DERIVED_CONTAINS:
9287 13062 : *st = ST_END_TYPE;
9288 13062 : target = " type";
9289 13062 : eos_ok = 0;
9290 13062 : break;
9291 :
9292 1549 : case COMP_ASSOCIATE:
9293 1549 : *st = ST_END_ASSOCIATE;
9294 1549 : target = " associate";
9295 1549 : eos_ok = 0;
9296 1549 : break;
9297 :
9298 1501 : case COMP_BLOCK:
9299 1501 : case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
9300 1501 : *st = ST_END_BLOCK;
9301 1501 : target = " block";
9302 1501 : eos_ok = 0;
9303 1501 : break;
9304 :
9305 14941 : case COMP_IF:
9306 14941 : *st = ST_ENDIF;
9307 14941 : target = " if";
9308 14941 : eos_ok = 0;
9309 14941 : break;
9310 :
9311 30774 : case COMP_DO:
9312 30774 : case COMP_DO_CONCURRENT:
9313 30774 : *st = ST_ENDDO;
9314 30774 : target = " do";
9315 30774 : eos_ok = 0;
9316 30774 : break;
9317 :
9318 54 : case COMP_CRITICAL:
9319 54 : *st = ST_END_CRITICAL;
9320 54 : target = " critical";
9321 54 : eos_ok = 0;
9322 54 : break;
9323 :
9324 4654 : case COMP_SELECT:
9325 4654 : case COMP_SELECT_TYPE:
9326 4654 : case COMP_SELECT_RANK:
9327 4654 : *st = ST_END_SELECT;
9328 4654 : target = " select";
9329 4654 : eos_ok = 0;
9330 4654 : break;
9331 :
9332 509 : case COMP_FORALL:
9333 509 : *st = ST_END_FORALL;
9334 509 : target = " forall";
9335 509 : eos_ok = 0;
9336 509 : break;
9337 :
9338 373 : case COMP_WHERE:
9339 373 : *st = ST_END_WHERE;
9340 373 : target = " where";
9341 373 : eos_ok = 0;
9342 373 : break;
9343 :
9344 158 : case COMP_ENUM:
9345 158 : *st = ST_END_ENUM;
9346 158 : target = " enum";
9347 158 : eos_ok = 0;
9348 158 : last_initializer = NULL;
9349 158 : set_enum_kind ();
9350 158 : gfc_free_enum_history ();
9351 158 : break;
9352 :
9353 0 : case COMP_OMP_BEGIN_METADIRECTIVE:
9354 0 : *st = ST_OMP_END_METADIRECTIVE;
9355 0 : target = " metadirective";
9356 0 : eos_ok = 0;
9357 0 : break;
9358 :
9359 98 : case COMP_CHANGE_TEAM:
9360 98 : *st = ST_END_TEAM;
9361 98 : target = " team";
9362 98 : eos_ok = 0;
9363 98 : break;
9364 :
9365 9 : default:
9366 9 : gfc_error ("Unexpected END statement at %C");
9367 9 : goto cleanup;
9368 : }
9369 :
9370 181845 : old_loc = gfc_current_locus;
9371 181845 : if (gfc_match_eos () == MATCH_YES)
9372 : {
9373 20782 : if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
9374 : {
9375 8143 : if (!gfc_notify_std (GFC_STD_F2008, "END statement "
9376 : "instead of %s statement at %L",
9377 : abbreviated_modproc_decl ? "END PROCEDURE"
9378 4059 : : gfc_ascii_statement(*st), &old_loc))
9379 4 : goto cleanup;
9380 : }
9381 9 : else if (!eos_ok)
9382 : {
9383 : /* We would have required END [something]. */
9384 9 : gfc_error ("%s statement expected at %L",
9385 : gfc_ascii_statement (*st), &old_loc);
9386 9 : goto cleanup;
9387 : }
9388 :
9389 20769 : return MATCH_YES;
9390 : }
9391 :
9392 : /* Verify that we've got the sort of end-block that we're expecting. */
9393 161063 : if (gfc_match (target) != MATCH_YES)
9394 : {
9395 331 : gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
9396 165 : ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
9397 166 : goto cleanup;
9398 : }
9399 : else
9400 160897 : got_matching_end = true;
9401 :
9402 160897 : if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
9403 : /* Emit errors of stat and errmsg parsing now to finish the block and
9404 : continue analysis of compilation unit. */
9405 2 : gfc_error_check ();
9406 :
9407 160897 : old_loc = gfc_current_locus;
9408 : /* If we're at the end, make sure a block name wasn't required. */
9409 160897 : if (gfc_match_eos () == MATCH_YES)
9410 : {
9411 106255 : if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
9412 : && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
9413 : && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
9414 : && *st != ST_END_TEAM)
9415 : return MATCH_YES;
9416 :
9417 53954 : if (!block_name)
9418 : return MATCH_YES;
9419 :
9420 8 : gfc_error ("Expected block name of %qs in %s statement at %L",
9421 : block_name, gfc_ascii_statement (*st), &old_loc);
9422 :
9423 8 : return MATCH_ERROR;
9424 : }
9425 :
9426 : /* END INTERFACE has a special handler for its several possible endings. */
9427 54642 : if (*st == ST_END_INTERFACE)
9428 696 : return gfc_match_end_interface ();
9429 :
9430 : /* We haven't hit the end of statement, so what is left must be an
9431 : end-name. */
9432 53946 : m = gfc_match_space ();
9433 53946 : if (m == MATCH_YES)
9434 53946 : m = gfc_match_name (name);
9435 :
9436 53946 : if (m == MATCH_NO)
9437 0 : gfc_error ("Expected terminating name at %C");
9438 53946 : if (m != MATCH_YES)
9439 0 : goto cleanup;
9440 :
9441 53946 : if (block_name == NULL)
9442 15 : goto syntax;
9443 :
9444 : /* We have to pick out the declared submodule name from the composite
9445 : required by F2008:11.2.3 para 2, which ends in the declared name. */
9446 53931 : if (state == COMP_SUBMODULE)
9447 137 : block_name = strchr (block_name, '.') + 1;
9448 :
9449 53931 : if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
9450 : {
9451 8 : gfc_error ("Expected label %qs for %s statement at %C", block_name,
9452 : gfc_ascii_statement (*st));
9453 8 : goto cleanup;
9454 : }
9455 : /* Procedure pointer as function result. */
9456 53923 : else if (strcmp (block_name, "ppr@") == 0
9457 21 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
9458 : {
9459 0 : gfc_error ("Expected label %qs for %s statement at %C",
9460 0 : gfc_current_block ()->ns->proc_name->name,
9461 : gfc_ascii_statement (*st));
9462 0 : goto cleanup;
9463 : }
9464 :
9465 53923 : if (gfc_match_eos () == MATCH_YES)
9466 : return MATCH_YES;
9467 :
9468 0 : syntax:
9469 15 : gfc_syntax_error (*st);
9470 :
9471 211 : cleanup:
9472 211 : gfc_current_locus = old_loc;
9473 :
9474 : /* If we are missing an END BLOCK, we created a half-ready namespace.
9475 : Remove it from the parent namespace's sibling list. */
9476 :
9477 211 : if (state == COMP_BLOCK && !got_matching_end)
9478 : {
9479 7 : parent_ns = gfc_current_ns->parent;
9480 :
9481 7 : nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
9482 :
9483 7 : prev_ns = NULL;
9484 7 : ns = *nsp;
9485 14 : while (ns)
9486 : {
9487 7 : if (ns == gfc_current_ns)
9488 : {
9489 7 : if (prev_ns == NULL)
9490 7 : *nsp = NULL;
9491 : else
9492 0 : prev_ns->sibling = ns->sibling;
9493 : }
9494 7 : prev_ns = ns;
9495 7 : ns = ns->sibling;
9496 : }
9497 :
9498 : /* The namespace can still be referenced by parser state and code nodes;
9499 : let normal block unwinding/freeing own its lifetime. */
9500 7 : gfc_current_ns = parent_ns;
9501 7 : gfc_state_stack = gfc_state_stack->previous;
9502 7 : state = gfc_current_state ();
9503 : }
9504 :
9505 : return MATCH_ERROR;
9506 : }
9507 :
9508 :
9509 :
9510 : /***************** Attribute declaration statements ****************/
9511 :
9512 : /* Set the attribute of a single variable. */
9513 :
9514 : static match
9515 10427 : attr_decl1 (void)
9516 : {
9517 10427 : char name[GFC_MAX_SYMBOL_LEN + 1];
9518 10427 : gfc_array_spec *as;
9519 :
9520 : /* Workaround -Wmaybe-uninitialized false positive during
9521 : profiledbootstrap by initializing them. */
9522 10427 : gfc_symbol *sym = NULL;
9523 10427 : locus var_locus;
9524 10427 : match m;
9525 :
9526 10427 : as = NULL;
9527 :
9528 10427 : m = gfc_match_name (name);
9529 10427 : if (m != MATCH_YES)
9530 0 : goto cleanup;
9531 :
9532 10427 : if (find_special (name, &sym, false))
9533 : return MATCH_ERROR;
9534 :
9535 10427 : if (!check_function_name (name))
9536 : {
9537 7 : m = MATCH_ERROR;
9538 7 : goto cleanup;
9539 : }
9540 :
9541 10420 : var_locus = gfc_current_locus;
9542 :
9543 : /* Deal with possible array specification for certain attributes. */
9544 10420 : if (current_attr.dimension
9545 8841 : || current_attr.codimension
9546 8819 : || current_attr.allocatable
9547 8395 : || current_attr.pointer
9548 7672 : || current_attr.target)
9549 : {
9550 2974 : m = gfc_match_array_spec (&as, !current_attr.codimension,
9551 : !current_attr.dimension
9552 1395 : && !current_attr.pointer
9553 3646 : && !current_attr.target);
9554 2974 : if (m == MATCH_ERROR)
9555 2 : goto cleanup;
9556 :
9557 2972 : if (current_attr.dimension && m == MATCH_NO)
9558 : {
9559 0 : gfc_error ("Missing array specification at %L in DIMENSION "
9560 : "statement", &var_locus);
9561 0 : m = MATCH_ERROR;
9562 0 : goto cleanup;
9563 : }
9564 :
9565 2972 : if (current_attr.dimension && sym->value)
9566 : {
9567 1 : gfc_error ("Dimensions specified for %s at %L after its "
9568 : "initialization", sym->name, &var_locus);
9569 1 : m = MATCH_ERROR;
9570 1 : goto cleanup;
9571 : }
9572 :
9573 2971 : if (current_attr.codimension && m == MATCH_NO)
9574 : {
9575 0 : gfc_error ("Missing array specification at %L in CODIMENSION "
9576 : "statement", &var_locus);
9577 0 : m = MATCH_ERROR;
9578 0 : goto cleanup;
9579 : }
9580 :
9581 2971 : if ((current_attr.allocatable || current_attr.pointer)
9582 1147 : && (m == MATCH_YES) && (as->type != AS_DEFERRED))
9583 : {
9584 0 : gfc_error ("Array specification must be deferred at %L", &var_locus);
9585 0 : m = MATCH_ERROR;
9586 0 : goto cleanup;
9587 : }
9588 : }
9589 :
9590 10417 : if (sym->ts.type == BT_CLASS
9591 200 : && sym->ts.u.derived
9592 200 : && sym->ts.u.derived->attr.is_class)
9593 : {
9594 177 : sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
9595 177 : sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
9596 177 : sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
9597 177 : sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
9598 177 : if (CLASS_DATA (sym)->as)
9599 123 : sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
9600 : }
9601 8840 : if (current_attr.dimension == 0 && current_attr.codimension == 0
9602 19236 : && !gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus))
9603 : {
9604 22 : m = MATCH_ERROR;
9605 22 : goto cleanup;
9606 : }
9607 10395 : if (!gfc_set_array_spec (sym, as, &var_locus))
9608 : {
9609 18 : m = MATCH_ERROR;
9610 18 : goto cleanup;
9611 : }
9612 :
9613 10377 : if (sym->attr.cray_pointee && sym->as != NULL)
9614 : {
9615 : /* Fix the array spec. */
9616 2 : m = gfc_mod_pointee_as (sym->as);
9617 2 : if (m == MATCH_ERROR)
9618 0 : goto cleanup;
9619 : }
9620 :
9621 10377 : if (!gfc_add_attribute (&sym->attr, &var_locus))
9622 : {
9623 0 : m = MATCH_ERROR;
9624 0 : goto cleanup;
9625 : }
9626 :
9627 5740 : if ((current_attr.external || current_attr.intrinsic)
9628 6289 : && sym->attr.flavor != FL_PROCEDURE
9629 16634 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
9630 : {
9631 0 : m = MATCH_ERROR;
9632 0 : goto cleanup;
9633 : }
9634 :
9635 10377 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
9636 169 : && !as && !current_attr.pointer && !current_attr.allocatable
9637 136 : && !current_attr.external)
9638 : {
9639 136 : sym->attr.pointer = 0;
9640 136 : sym->attr.allocatable = 0;
9641 136 : sym->attr.dimension = 0;
9642 136 : sym->attr.codimension = 0;
9643 136 : gfc_free_array_spec (sym->as);
9644 136 : sym->as = NULL;
9645 : }
9646 10241 : else if (sym->ts.type == BT_CLASS
9647 10241 : && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
9648 : {
9649 0 : m = MATCH_ERROR;
9650 0 : goto cleanup;
9651 : }
9652 :
9653 10377 : add_hidden_procptr_result (sym);
9654 :
9655 10377 : return MATCH_YES;
9656 :
9657 50 : cleanup:
9658 50 : gfc_free_array_spec (as);
9659 50 : return m;
9660 : }
9661 :
9662 :
9663 : /* Generic attribute declaration subroutine. Used for attributes that
9664 : just have a list of names. */
9665 :
9666 : static match
9667 6712 : attr_decl (void)
9668 : {
9669 6712 : match m;
9670 :
9671 : /* Gobble the optional double colon, by simply ignoring the result
9672 : of gfc_match(). */
9673 6712 : gfc_match (" ::");
9674 :
9675 10427 : for (;;)
9676 : {
9677 10427 : m = attr_decl1 ();
9678 10427 : if (m != MATCH_YES)
9679 : break;
9680 :
9681 10377 : if (gfc_match_eos () == MATCH_YES)
9682 : {
9683 : m = MATCH_YES;
9684 : break;
9685 : }
9686 :
9687 3715 : if (gfc_match_char (',') != MATCH_YES)
9688 : {
9689 0 : gfc_error ("Unexpected character in variable list at %C");
9690 0 : m = MATCH_ERROR;
9691 0 : break;
9692 : }
9693 : }
9694 :
9695 6712 : return m;
9696 : }
9697 :
9698 :
9699 : /* This routine matches Cray Pointer declarations of the form:
9700 : pointer ( <pointer>, <pointee> )
9701 : or
9702 : pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
9703 : The pointer, if already declared, should be an integer. Otherwise, we
9704 : set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
9705 : be either a scalar, or an array declaration. No space is allocated for
9706 : the pointee. For the statement
9707 : pointer (ipt, ar(10))
9708 : any subsequent uses of ar will be translated (in C-notation) as
9709 : ar(i) => ((<type> *) ipt)(i)
9710 : After gimplification, pointee variable will disappear in the code. */
9711 :
9712 : static match
9713 334 : cray_pointer_decl (void)
9714 : {
9715 334 : match m;
9716 334 : gfc_array_spec *as = NULL;
9717 334 : gfc_symbol *cptr; /* Pointer symbol. */
9718 334 : gfc_symbol *cpte; /* Pointee symbol. */
9719 334 : locus var_locus;
9720 334 : bool done = false;
9721 :
9722 334 : while (!done)
9723 : {
9724 347 : if (gfc_match_char ('(') != MATCH_YES)
9725 : {
9726 1 : gfc_error ("Expected %<(%> at %C");
9727 1 : return MATCH_ERROR;
9728 : }
9729 :
9730 : /* Match pointer. */
9731 346 : var_locus = gfc_current_locus;
9732 346 : gfc_clear_attr (¤t_attr);
9733 346 : gfc_add_cray_pointer (¤t_attr, &var_locus);
9734 346 : current_ts.type = BT_INTEGER;
9735 346 : current_ts.kind = gfc_index_integer_kind;
9736 :
9737 346 : m = gfc_match_symbol (&cptr, 0);
9738 346 : if (m != MATCH_YES)
9739 : {
9740 2 : gfc_error ("Expected variable name at %C");
9741 2 : return m;
9742 : }
9743 :
9744 344 : if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
9745 : return MATCH_ERROR;
9746 :
9747 341 : gfc_set_sym_referenced (cptr);
9748 :
9749 341 : if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
9750 : {
9751 327 : cptr->ts.type = BT_INTEGER;
9752 327 : cptr->ts.kind = gfc_index_integer_kind;
9753 : }
9754 14 : else if (cptr->ts.type != BT_INTEGER)
9755 : {
9756 1 : gfc_error ("Cray pointer at %C must be an integer");
9757 1 : return MATCH_ERROR;
9758 : }
9759 13 : else if (cptr->ts.kind < gfc_index_integer_kind)
9760 0 : gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
9761 : " memory addresses require %d bytes",
9762 : cptr->ts.kind, gfc_index_integer_kind);
9763 :
9764 340 : if (gfc_match_char (',') != MATCH_YES)
9765 : {
9766 2 : gfc_error ("Expected \",\" at %C");
9767 2 : return MATCH_ERROR;
9768 : }
9769 :
9770 : /* Match Pointee. */
9771 338 : var_locus = gfc_current_locus;
9772 338 : gfc_clear_attr (¤t_attr);
9773 338 : gfc_add_cray_pointee (¤t_attr, &var_locus);
9774 338 : current_ts.type = BT_UNKNOWN;
9775 338 : current_ts.kind = 0;
9776 :
9777 338 : m = gfc_match_symbol (&cpte, 0);
9778 338 : if (m != MATCH_YES)
9779 : {
9780 2 : gfc_error ("Expected variable name at %C");
9781 2 : return m;
9782 : }
9783 :
9784 : /* Check for an optional array spec. */
9785 336 : m = gfc_match_array_spec (&as, true, false);
9786 336 : if (m == MATCH_ERROR)
9787 : {
9788 0 : gfc_free_array_spec (as);
9789 0 : return m;
9790 : }
9791 336 : else if (m == MATCH_NO)
9792 : {
9793 226 : gfc_free_array_spec (as);
9794 226 : as = NULL;
9795 : }
9796 :
9797 336 : if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
9798 : return MATCH_ERROR;
9799 :
9800 329 : gfc_set_sym_referenced (cpte);
9801 :
9802 329 : if (cpte->as == NULL)
9803 : {
9804 247 : if (!gfc_set_array_spec (cpte, as, &var_locus))
9805 0 : gfc_internal_error ("Cannot set Cray pointee array spec.");
9806 : }
9807 82 : else if (as != NULL)
9808 : {
9809 1 : gfc_error ("Duplicate array spec for Cray pointee at %C");
9810 1 : gfc_free_array_spec (as);
9811 1 : return MATCH_ERROR;
9812 : }
9813 :
9814 328 : as = NULL;
9815 :
9816 328 : if (cpte->as != NULL)
9817 : {
9818 : /* Fix array spec. */
9819 190 : m = gfc_mod_pointee_as (cpte->as);
9820 190 : if (m == MATCH_ERROR)
9821 : return m;
9822 : }
9823 :
9824 : /* Point the Pointee at the Pointer. */
9825 328 : cpte->cp_pointer = cptr;
9826 :
9827 328 : if (gfc_match_char (')') != MATCH_YES)
9828 : {
9829 2 : gfc_error ("Expected \")\" at %C");
9830 2 : return MATCH_ERROR;
9831 : }
9832 326 : m = gfc_match_char (',');
9833 326 : if (m != MATCH_YES)
9834 313 : done = true; /* Stop searching for more declarations. */
9835 :
9836 : }
9837 :
9838 313 : if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
9839 313 : || gfc_match_eos () != MATCH_YES)
9840 : {
9841 0 : gfc_error ("Expected %<,%> or end of statement at %C");
9842 0 : return MATCH_ERROR;
9843 : }
9844 : return MATCH_YES;
9845 : }
9846 :
9847 :
9848 : match
9849 3215 : gfc_match_external (void)
9850 : {
9851 :
9852 3215 : gfc_clear_attr (¤t_attr);
9853 3215 : current_attr.external = 1;
9854 :
9855 3215 : return attr_decl ();
9856 : }
9857 :
9858 :
9859 : match
9860 208 : gfc_match_intent (void)
9861 : {
9862 208 : sym_intent intent;
9863 :
9864 : /* This is not allowed within a BLOCK construct! */
9865 208 : if (gfc_current_state () == COMP_BLOCK)
9866 : {
9867 2 : gfc_error ("INTENT is not allowed inside of BLOCK at %C");
9868 2 : return MATCH_ERROR;
9869 : }
9870 :
9871 206 : intent = match_intent_spec ();
9872 206 : if (intent == INTENT_UNKNOWN)
9873 : return MATCH_ERROR;
9874 :
9875 206 : gfc_clear_attr (¤t_attr);
9876 206 : current_attr.intent = intent;
9877 :
9878 206 : return attr_decl ();
9879 : }
9880 :
9881 :
9882 : match
9883 1482 : gfc_match_intrinsic (void)
9884 : {
9885 :
9886 1482 : gfc_clear_attr (¤t_attr);
9887 1482 : current_attr.intrinsic = 1;
9888 :
9889 1482 : return attr_decl ();
9890 : }
9891 :
9892 :
9893 : match
9894 220 : gfc_match_optional (void)
9895 : {
9896 : /* This is not allowed within a BLOCK construct! */
9897 220 : if (gfc_current_state () == COMP_BLOCK)
9898 : {
9899 2 : gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
9900 2 : return MATCH_ERROR;
9901 : }
9902 :
9903 218 : gfc_clear_attr (¤t_attr);
9904 218 : current_attr.optional = 1;
9905 :
9906 218 : return attr_decl ();
9907 : }
9908 :
9909 :
9910 : match
9911 915 : gfc_match_pointer (void)
9912 : {
9913 915 : gfc_gobble_whitespace ();
9914 915 : if (gfc_peek_ascii_char () == '(')
9915 : {
9916 335 : if (!flag_cray_pointer)
9917 : {
9918 1 : gfc_error ("Cray pointer declaration at %C requires "
9919 : "%<-fcray-pointer%> flag");
9920 1 : return MATCH_ERROR;
9921 : }
9922 334 : return cray_pointer_decl ();
9923 : }
9924 : else
9925 : {
9926 580 : gfc_clear_attr (¤t_attr);
9927 580 : current_attr.pointer = 1;
9928 :
9929 580 : return attr_decl ();
9930 : }
9931 : }
9932 :
9933 :
9934 : match
9935 162 : gfc_match_allocatable (void)
9936 : {
9937 162 : gfc_clear_attr (¤t_attr);
9938 162 : current_attr.allocatable = 1;
9939 :
9940 162 : return attr_decl ();
9941 : }
9942 :
9943 :
9944 : match
9945 23 : gfc_match_codimension (void)
9946 : {
9947 23 : gfc_clear_attr (¤t_attr);
9948 23 : current_attr.codimension = 1;
9949 :
9950 23 : return attr_decl ();
9951 : }
9952 :
9953 :
9954 : match
9955 80 : gfc_match_contiguous (void)
9956 : {
9957 80 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
9958 : return MATCH_ERROR;
9959 :
9960 79 : gfc_clear_attr (¤t_attr);
9961 79 : current_attr.contiguous = 1;
9962 :
9963 79 : return attr_decl ();
9964 : }
9965 :
9966 :
9967 : match
9968 648 : gfc_match_dimension (void)
9969 : {
9970 648 : gfc_clear_attr (¤t_attr);
9971 648 : current_attr.dimension = 1;
9972 :
9973 648 : return attr_decl ();
9974 : }
9975 :
9976 :
9977 : match
9978 99 : gfc_match_target (void)
9979 : {
9980 99 : gfc_clear_attr (¤t_attr);
9981 99 : current_attr.target = 1;
9982 :
9983 99 : return attr_decl ();
9984 : }
9985 :
9986 :
9987 : /* Match the list of entities being specified in a PUBLIC or PRIVATE
9988 : statement. */
9989 :
9990 : static match
9991 1766 : access_attr_decl (gfc_statement st)
9992 : {
9993 1766 : char name[GFC_MAX_SYMBOL_LEN + 1];
9994 1766 : interface_type type;
9995 1766 : gfc_user_op *uop;
9996 1766 : gfc_symbol *sym, *dt_sym;
9997 1766 : gfc_intrinsic_op op;
9998 1766 : match m;
9999 1766 : gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
10000 :
10001 1766 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10002 0 : goto done;
10003 :
10004 2916 : for (;;)
10005 : {
10006 2916 : m = gfc_match_generic_spec (&type, name, &op);
10007 2916 : if (m == MATCH_NO)
10008 0 : goto syntax;
10009 2916 : if (m == MATCH_ERROR)
10010 0 : goto done;
10011 :
10012 2916 : switch (type)
10013 : {
10014 0 : case INTERFACE_NAMELESS:
10015 0 : case INTERFACE_ABSTRACT:
10016 0 : goto syntax;
10017 :
10018 2839 : case INTERFACE_GENERIC:
10019 2839 : case INTERFACE_DTIO:
10020 :
10021 2839 : if (gfc_get_symbol (name, NULL, &sym))
10022 0 : goto done;
10023 :
10024 2839 : if (type == INTERFACE_DTIO
10025 26 : && gfc_current_ns->proc_name
10026 26 : && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
10027 26 : && sym->attr.flavor == FL_UNKNOWN)
10028 2 : sym->attr.flavor = FL_PROCEDURE;
10029 :
10030 2839 : if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
10031 4 : goto done;
10032 :
10033 330 : if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
10034 2892 : && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
10035 0 : goto done;
10036 :
10037 : break;
10038 :
10039 72 : case INTERFACE_INTRINSIC_OP:
10040 72 : if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
10041 : {
10042 72 : gfc_intrinsic_op other_op;
10043 :
10044 72 : gfc_current_ns->operator_access[op] = access;
10045 :
10046 : /* Handle the case if there is another op with the same
10047 : function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
10048 72 : other_op = gfc_equivalent_op (op);
10049 :
10050 72 : if (other_op != INTRINSIC_NONE)
10051 21 : gfc_current_ns->operator_access[other_op] = access;
10052 : }
10053 : else
10054 : {
10055 0 : gfc_error ("Access specification of the %s operator at %C has "
10056 : "already been specified", gfc_op2string (op));
10057 0 : goto done;
10058 : }
10059 :
10060 : break;
10061 :
10062 5 : case INTERFACE_USER_OP:
10063 5 : uop = gfc_get_uop (name);
10064 :
10065 5 : if (uop->access == ACCESS_UNKNOWN)
10066 : {
10067 4 : uop->access = access;
10068 : }
10069 : else
10070 : {
10071 1 : gfc_error ("Access specification of the .%s. operator at %C "
10072 : "has already been specified", uop->name);
10073 1 : goto done;
10074 : }
10075 :
10076 4 : break;
10077 : }
10078 :
10079 2911 : if (gfc_match_char (',') == MATCH_NO)
10080 : break;
10081 : }
10082 :
10083 1761 : if (gfc_match_eos () != MATCH_YES)
10084 0 : goto syntax;
10085 : return MATCH_YES;
10086 :
10087 0 : syntax:
10088 0 : gfc_syntax_error (st);
10089 :
10090 : done:
10091 : return MATCH_ERROR;
10092 : }
10093 :
10094 :
10095 : match
10096 23 : gfc_match_protected (void)
10097 : {
10098 23 : gfc_symbol *sym;
10099 23 : match m;
10100 23 : char c;
10101 :
10102 : /* PROTECTED has already been seen, but must be followed by whitespace
10103 : or ::. */
10104 23 : c = gfc_peek_ascii_char ();
10105 23 : if (!gfc_is_whitespace (c) && c != ':')
10106 : return MATCH_NO;
10107 :
10108 22 : if (!gfc_current_ns->proc_name
10109 20 : || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
10110 : {
10111 3 : gfc_error ("PROTECTED at %C only allowed in specification "
10112 : "part of a module");
10113 3 : return MATCH_ERROR;
10114 :
10115 : }
10116 :
10117 19 : gfc_match (" ::");
10118 :
10119 19 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
10120 : return MATCH_ERROR;
10121 :
10122 : /* PROTECTED has an entity-list. */
10123 18 : if (gfc_match_eos () == MATCH_YES)
10124 0 : goto syntax;
10125 :
10126 26 : for(;;)
10127 : {
10128 26 : m = gfc_match_symbol (&sym, 0);
10129 26 : switch (m)
10130 : {
10131 26 : case MATCH_YES:
10132 26 : if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
10133 : return MATCH_ERROR;
10134 25 : goto next_item;
10135 :
10136 : case MATCH_NO:
10137 : break;
10138 :
10139 : case MATCH_ERROR:
10140 : return MATCH_ERROR;
10141 : }
10142 :
10143 25 : next_item:
10144 25 : if (gfc_match_eos () == MATCH_YES)
10145 : break;
10146 8 : if (gfc_match_char (',') != MATCH_YES)
10147 0 : goto syntax;
10148 : }
10149 :
10150 : return MATCH_YES;
10151 :
10152 0 : syntax:
10153 0 : gfc_error ("Syntax error in PROTECTED statement at %C");
10154 0 : return MATCH_ERROR;
10155 : }
10156 :
10157 :
10158 : /* The PRIVATE statement is a bit weird in that it can be an attribute
10159 : declaration, but also works as a standalone statement inside of a
10160 : type declaration or a module. */
10161 :
10162 : match
10163 29249 : gfc_match_private (gfc_statement *st)
10164 : {
10165 29249 : gfc_state_data *prev;
10166 :
10167 29249 : if (gfc_match ("private") != MATCH_YES)
10168 : return MATCH_NO;
10169 :
10170 : /* Try matching PRIVATE without an access-list. */
10171 1634 : if (gfc_match_eos () == MATCH_YES)
10172 : {
10173 1347 : prev = gfc_state_stack->previous;
10174 1347 : if (gfc_current_state () != COMP_MODULE
10175 367 : && !(gfc_current_state () == COMP_DERIVED
10176 334 : && prev && prev->state == COMP_MODULE)
10177 34 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
10178 32 : && prev->previous && prev->previous->state == COMP_MODULE))
10179 : {
10180 2 : gfc_error ("PRIVATE statement at %C is only allowed in the "
10181 : "specification part of a module");
10182 2 : return MATCH_ERROR;
10183 : }
10184 :
10185 1345 : *st = ST_PRIVATE;
10186 1345 : return MATCH_YES;
10187 : }
10188 :
10189 : /* At this point in free-form source code, PRIVATE must be followed
10190 : by whitespace or ::. */
10191 287 : if (gfc_current_form == FORM_FREE)
10192 : {
10193 285 : char c = gfc_peek_ascii_char ();
10194 285 : if (!gfc_is_whitespace (c) && c != ':')
10195 : return MATCH_NO;
10196 : }
10197 :
10198 286 : prev = gfc_state_stack->previous;
10199 286 : if (gfc_current_state () != COMP_MODULE
10200 1 : && !(gfc_current_state () == COMP_DERIVED
10201 0 : && prev && prev->state == COMP_MODULE)
10202 1 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
10203 0 : && prev->previous && prev->previous->state == COMP_MODULE))
10204 : {
10205 1 : gfc_error ("PRIVATE statement at %C is only allowed in the "
10206 : "specification part of a module");
10207 1 : return MATCH_ERROR;
10208 : }
10209 :
10210 285 : *st = ST_ATTR_DECL;
10211 285 : return access_attr_decl (ST_PRIVATE);
10212 : }
10213 :
10214 :
10215 : match
10216 1879 : gfc_match_public (gfc_statement *st)
10217 : {
10218 1879 : if (gfc_match ("public") != MATCH_YES)
10219 : return MATCH_NO;
10220 :
10221 : /* Try matching PUBLIC without an access-list. */
10222 1528 : if (gfc_match_eos () == MATCH_YES)
10223 : {
10224 45 : if (gfc_current_state () != COMP_MODULE)
10225 : {
10226 2 : gfc_error ("PUBLIC statement at %C is only allowed in the "
10227 : "specification part of a module");
10228 2 : return MATCH_ERROR;
10229 : }
10230 :
10231 43 : *st = ST_PUBLIC;
10232 43 : return MATCH_YES;
10233 : }
10234 :
10235 : /* At this point in free-form source code, PUBLIC must be followed
10236 : by whitespace or ::. */
10237 1483 : if (gfc_current_form == FORM_FREE)
10238 : {
10239 1481 : char c = gfc_peek_ascii_char ();
10240 1481 : if (!gfc_is_whitespace (c) && c != ':')
10241 : return MATCH_NO;
10242 : }
10243 :
10244 1482 : if (gfc_current_state () != COMP_MODULE)
10245 : {
10246 1 : gfc_error ("PUBLIC statement at %C is only allowed in the "
10247 : "specification part of a module");
10248 1 : return MATCH_ERROR;
10249 : }
10250 :
10251 1481 : *st = ST_ATTR_DECL;
10252 1481 : return access_attr_decl (ST_PUBLIC);
10253 : }
10254 :
10255 :
10256 : /* Workhorse for gfc_match_parameter. */
10257 :
10258 : static match
10259 8533 : do_parm (void)
10260 : {
10261 8533 : gfc_symbol *sym;
10262 8533 : gfc_expr *init;
10263 8533 : gfc_charlen *saved_cl_list;
10264 8533 : match m;
10265 8533 : bool t;
10266 :
10267 8533 : saved_cl_list = gfc_current_ns->cl_list;
10268 :
10269 8533 : m = gfc_match_symbol (&sym, 0);
10270 8533 : if (m == MATCH_NO)
10271 0 : gfc_error ("Expected variable name at %C in PARAMETER statement");
10272 :
10273 8533 : if (m != MATCH_YES)
10274 : return m;
10275 :
10276 8533 : if (gfc_match_char ('=') == MATCH_NO)
10277 : {
10278 0 : gfc_error ("Expected = sign in PARAMETER statement at %C");
10279 0 : return MATCH_ERROR;
10280 : }
10281 :
10282 8533 : m = gfc_match_init_expr (&init);
10283 8533 : if (m == MATCH_NO)
10284 0 : gfc_error ("Expected expression at %C in PARAMETER statement");
10285 8533 : if (m != MATCH_YES)
10286 : return m;
10287 :
10288 8532 : if (sym->ts.type == BT_UNKNOWN
10289 8532 : && !gfc_set_default_type (sym, 1, NULL))
10290 : {
10291 1 : m = MATCH_ERROR;
10292 1 : goto cleanup;
10293 : }
10294 :
10295 8531 : if (!gfc_check_assign_symbol (sym, NULL, init)
10296 8531 : || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
10297 : {
10298 1 : m = MATCH_ERROR;
10299 1 : goto cleanup;
10300 : }
10301 :
10302 8530 : if (sym->value)
10303 : {
10304 1 : gfc_error ("Initializing already initialized variable at %C");
10305 1 : m = MATCH_ERROR;
10306 1 : goto cleanup;
10307 : }
10308 :
10309 8529 : t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus,
10310 : saved_cl_list);
10311 8529 : return (t) ? MATCH_YES : MATCH_ERROR;
10312 :
10313 3 : cleanup:
10314 3 : gfc_free_expr (init);
10315 3 : return m;
10316 : }
10317 :
10318 :
10319 : /* Match a parameter statement, with the weird syntax that these have. */
10320 :
10321 : match
10322 7820 : gfc_match_parameter (void)
10323 : {
10324 7820 : const char *term = " )%t";
10325 7820 : match m;
10326 :
10327 7820 : if (gfc_match_char ('(') == MATCH_NO)
10328 : {
10329 : /* With legacy PARAMETER statements, don't expect a terminating ')'. */
10330 28 : if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
10331 : return MATCH_NO;
10332 7819 : term = " %t";
10333 : }
10334 :
10335 8533 : for (;;)
10336 : {
10337 8533 : m = do_parm ();
10338 8533 : if (m != MATCH_YES)
10339 : break;
10340 :
10341 8529 : if (gfc_match (term) == MATCH_YES)
10342 : break;
10343 :
10344 714 : if (gfc_match_char (',') != MATCH_YES)
10345 : {
10346 0 : gfc_error ("Unexpected characters in PARAMETER statement at %C");
10347 0 : m = MATCH_ERROR;
10348 0 : break;
10349 : }
10350 : }
10351 :
10352 : return m;
10353 : }
10354 :
10355 :
10356 : match
10357 8 : gfc_match_automatic (void)
10358 : {
10359 8 : gfc_symbol *sym;
10360 8 : match m;
10361 8 : bool seen_symbol = false;
10362 :
10363 8 : if (!flag_dec_static)
10364 : {
10365 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10366 : "%<-fdec-static%>",
10367 : "AUTOMATIC"
10368 : );
10369 2 : return MATCH_ERROR;
10370 : }
10371 :
10372 6 : gfc_match (" ::");
10373 :
10374 6 : for (;;)
10375 : {
10376 6 : m = gfc_match_symbol (&sym, 0);
10377 6 : switch (m)
10378 : {
10379 : case MATCH_NO:
10380 : break;
10381 :
10382 : case MATCH_ERROR:
10383 : return MATCH_ERROR;
10384 :
10385 4 : case MATCH_YES:
10386 4 : if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
10387 : return MATCH_ERROR;
10388 : seen_symbol = true;
10389 : break;
10390 : }
10391 :
10392 4 : if (gfc_match_eos () == MATCH_YES)
10393 : break;
10394 0 : if (gfc_match_char (',') != MATCH_YES)
10395 0 : goto syntax;
10396 : }
10397 :
10398 4 : if (!seen_symbol)
10399 : {
10400 2 : gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
10401 2 : return MATCH_ERROR;
10402 : }
10403 :
10404 : return MATCH_YES;
10405 :
10406 0 : syntax:
10407 0 : gfc_error ("Syntax error in AUTOMATIC statement at %C");
10408 0 : return MATCH_ERROR;
10409 : }
10410 :
10411 :
10412 : match
10413 7 : gfc_match_static (void)
10414 : {
10415 7 : gfc_symbol *sym;
10416 7 : match m;
10417 7 : bool seen_symbol = false;
10418 :
10419 7 : if (!flag_dec_static)
10420 : {
10421 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10422 : "%<-fdec-static%>",
10423 : "STATIC");
10424 2 : return MATCH_ERROR;
10425 : }
10426 :
10427 5 : gfc_match (" ::");
10428 :
10429 5 : for (;;)
10430 : {
10431 5 : m = gfc_match_symbol (&sym, 0);
10432 5 : switch (m)
10433 : {
10434 : case MATCH_NO:
10435 : break;
10436 :
10437 : case MATCH_ERROR:
10438 : return MATCH_ERROR;
10439 :
10440 3 : case MATCH_YES:
10441 3 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10442 : &gfc_current_locus))
10443 : return MATCH_ERROR;
10444 : seen_symbol = true;
10445 : break;
10446 : }
10447 :
10448 3 : if (gfc_match_eos () == MATCH_YES)
10449 : break;
10450 0 : if (gfc_match_char (',') != MATCH_YES)
10451 0 : goto syntax;
10452 : }
10453 :
10454 3 : if (!seen_symbol)
10455 : {
10456 2 : gfc_error ("Expected entity-list in STATIC statement at %C");
10457 2 : return MATCH_ERROR;
10458 : }
10459 :
10460 : return MATCH_YES;
10461 :
10462 0 : syntax:
10463 0 : gfc_error ("Syntax error in STATIC statement at %C");
10464 0 : return MATCH_ERROR;
10465 : }
10466 :
10467 :
10468 : /* Save statements have a special syntax. */
10469 :
10470 : match
10471 272 : gfc_match_save (void)
10472 : {
10473 272 : char n[GFC_MAX_SYMBOL_LEN+1];
10474 272 : gfc_common_head *c;
10475 272 : gfc_symbol *sym;
10476 272 : match m;
10477 :
10478 272 : if (gfc_match_eos () == MATCH_YES)
10479 : {
10480 150 : if (gfc_current_ns->seen_save)
10481 : {
10482 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
10483 : "follows previous SAVE statement"))
10484 : return MATCH_ERROR;
10485 : }
10486 :
10487 149 : gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
10488 149 : return MATCH_YES;
10489 : }
10490 :
10491 122 : if (gfc_current_ns->save_all)
10492 : {
10493 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
10494 : "blanket SAVE statement"))
10495 : return MATCH_ERROR;
10496 : }
10497 :
10498 121 : gfc_match (" ::");
10499 :
10500 183 : for (;;)
10501 : {
10502 183 : m = gfc_match_symbol (&sym, 0);
10503 183 : switch (m)
10504 : {
10505 181 : case MATCH_YES:
10506 181 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10507 : &gfc_current_locus))
10508 : return MATCH_ERROR;
10509 179 : goto next_item;
10510 :
10511 : case MATCH_NO:
10512 : break;
10513 :
10514 : case MATCH_ERROR:
10515 : return MATCH_ERROR;
10516 : }
10517 :
10518 2 : m = gfc_match (" / %n /", &n);
10519 2 : if (m == MATCH_ERROR)
10520 : return MATCH_ERROR;
10521 2 : if (m == MATCH_NO)
10522 0 : goto syntax;
10523 :
10524 : /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
10525 : saved-entity-list that does not specify a common-block-name. */
10526 2 : if (gfc_current_state () == COMP_BLOCK)
10527 : {
10528 1 : gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
10529 : "in a BLOCK construct", n);
10530 1 : return MATCH_ERROR;
10531 : }
10532 :
10533 1 : c = gfc_get_common (n, 0);
10534 1 : c->saved = 1;
10535 :
10536 1 : gfc_current_ns->seen_save = 1;
10537 :
10538 180 : next_item:
10539 180 : if (gfc_match_eos () == MATCH_YES)
10540 : break;
10541 62 : if (gfc_match_char (',') != MATCH_YES)
10542 0 : goto syntax;
10543 : }
10544 :
10545 : return MATCH_YES;
10546 :
10547 0 : syntax:
10548 0 : if (gfc_current_ns->seen_save)
10549 : {
10550 0 : gfc_error ("Syntax error in SAVE statement at %C");
10551 0 : return MATCH_ERROR;
10552 : }
10553 : else
10554 : return MATCH_NO;
10555 : }
10556 :
10557 :
10558 : match
10559 93 : gfc_match_value (void)
10560 : {
10561 93 : gfc_symbol *sym;
10562 93 : match m;
10563 :
10564 : /* This is not allowed within a BLOCK construct! */
10565 93 : if (gfc_current_state () == COMP_BLOCK)
10566 : {
10567 2 : gfc_error ("VALUE is not allowed inside of BLOCK at %C");
10568 2 : return MATCH_ERROR;
10569 : }
10570 :
10571 91 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
10572 : return MATCH_ERROR;
10573 :
10574 90 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10575 : {
10576 : return MATCH_ERROR;
10577 : }
10578 :
10579 90 : if (gfc_match_eos () == MATCH_YES)
10580 0 : goto syntax;
10581 :
10582 116 : for(;;)
10583 : {
10584 116 : m = gfc_match_symbol (&sym, 0);
10585 116 : switch (m)
10586 : {
10587 116 : case MATCH_YES:
10588 116 : if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
10589 : return MATCH_ERROR;
10590 109 : goto next_item;
10591 :
10592 : case MATCH_NO:
10593 : break;
10594 :
10595 : case MATCH_ERROR:
10596 : return MATCH_ERROR;
10597 : }
10598 :
10599 109 : next_item:
10600 109 : if (gfc_match_eos () == MATCH_YES)
10601 : break;
10602 26 : if (gfc_match_char (',') != MATCH_YES)
10603 0 : goto syntax;
10604 : }
10605 :
10606 : return MATCH_YES;
10607 :
10608 0 : syntax:
10609 0 : gfc_error ("Syntax error in VALUE statement at %C");
10610 0 : return MATCH_ERROR;
10611 : }
10612 :
10613 :
10614 : match
10615 45 : gfc_match_volatile (void)
10616 : {
10617 45 : gfc_symbol *sym;
10618 45 : char *name;
10619 45 : match m;
10620 :
10621 45 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
10622 : return MATCH_ERROR;
10623 :
10624 44 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10625 : {
10626 : return MATCH_ERROR;
10627 : }
10628 :
10629 44 : if (gfc_match_eos () == MATCH_YES)
10630 1 : goto syntax;
10631 :
10632 48 : for(;;)
10633 : {
10634 : /* VOLATILE is special because it can be added to host-associated
10635 : symbols locally. Except for coarrays. */
10636 48 : m = gfc_match_symbol (&sym, 1);
10637 48 : switch (m)
10638 : {
10639 48 : case MATCH_YES:
10640 48 : name = XALLOCAVAR (char, strlen (sym->name) + 1);
10641 48 : strcpy (name, sym->name);
10642 48 : if (!check_function_name (name))
10643 : return MATCH_ERROR;
10644 : /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
10645 : for variable in a BLOCK which is defined outside of the BLOCK. */
10646 47 : if (sym->ns != gfc_current_ns && sym->attr.codimension)
10647 : {
10648 2 : gfc_error ("Specifying VOLATILE for coarray variable %qs at "
10649 : "%C, which is use-/host-associated", sym->name);
10650 2 : return MATCH_ERROR;
10651 : }
10652 45 : if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
10653 : return MATCH_ERROR;
10654 42 : goto next_item;
10655 :
10656 : case MATCH_NO:
10657 : break;
10658 :
10659 : case MATCH_ERROR:
10660 : return MATCH_ERROR;
10661 : }
10662 :
10663 42 : next_item:
10664 42 : if (gfc_match_eos () == MATCH_YES)
10665 : break;
10666 5 : if (gfc_match_char (',') != MATCH_YES)
10667 0 : goto syntax;
10668 : }
10669 :
10670 : return MATCH_YES;
10671 :
10672 1 : syntax:
10673 1 : gfc_error ("Syntax error in VOLATILE statement at %C");
10674 1 : return MATCH_ERROR;
10675 : }
10676 :
10677 :
10678 : match
10679 11 : gfc_match_asynchronous (void)
10680 : {
10681 11 : gfc_symbol *sym;
10682 11 : char *name;
10683 11 : match m;
10684 :
10685 11 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
10686 : return MATCH_ERROR;
10687 :
10688 10 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10689 : {
10690 : return MATCH_ERROR;
10691 : }
10692 :
10693 10 : if (gfc_match_eos () == MATCH_YES)
10694 0 : goto syntax;
10695 :
10696 10 : for(;;)
10697 : {
10698 : /* ASYNCHRONOUS is special because it can be added to host-associated
10699 : symbols locally. */
10700 10 : m = gfc_match_symbol (&sym, 1);
10701 10 : switch (m)
10702 : {
10703 10 : case MATCH_YES:
10704 10 : name = XALLOCAVAR (char, strlen (sym->name) + 1);
10705 10 : strcpy (name, sym->name);
10706 10 : if (!check_function_name (name))
10707 : return MATCH_ERROR;
10708 9 : if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
10709 : return MATCH_ERROR;
10710 7 : goto next_item;
10711 :
10712 : case MATCH_NO:
10713 : break;
10714 :
10715 : case MATCH_ERROR:
10716 : return MATCH_ERROR;
10717 : }
10718 :
10719 7 : next_item:
10720 7 : if (gfc_match_eos () == MATCH_YES)
10721 : break;
10722 0 : if (gfc_match_char (',') != MATCH_YES)
10723 0 : goto syntax;
10724 : }
10725 :
10726 : return MATCH_YES;
10727 :
10728 0 : syntax:
10729 0 : gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
10730 0 : return MATCH_ERROR;
10731 : }
10732 :
10733 :
10734 : /* Match a module procedure statement in a submodule. */
10735 :
10736 : match
10737 767448 : gfc_match_submod_proc (void)
10738 : {
10739 767448 : char name[GFC_MAX_SYMBOL_LEN + 1];
10740 767448 : gfc_symbol *sym, *fsym;
10741 767448 : match m;
10742 767448 : gfc_formal_arglist *formal, *head, *tail;
10743 :
10744 767448 : if (gfc_current_state () != COMP_CONTAINS
10745 15615 : || !(gfc_state_stack->previous
10746 15615 : && (gfc_state_stack->previous->state == COMP_SUBMODULE
10747 15615 : || gfc_state_stack->previous->state == COMP_MODULE)))
10748 : return MATCH_NO;
10749 :
10750 7808 : m = gfc_match (" module% procedure% %n", name);
10751 7808 : if (m != MATCH_YES)
10752 : return m;
10753 :
10754 267 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
10755 : "at %C"))
10756 : return MATCH_ERROR;
10757 :
10758 267 : if (get_proc_name (name, &sym, false))
10759 : return MATCH_ERROR;
10760 :
10761 : /* Make sure that the result field is appropriately filled. */
10762 267 : if (sym->tlink && sym->tlink->attr.function)
10763 : {
10764 117 : if (sym->tlink->result && sym->tlink->result != sym->tlink)
10765 : {
10766 67 : sym->result = sym->tlink->result;
10767 67 : if (!sym->result->attr.use_assoc)
10768 : {
10769 20 : gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
10770 : sym->result->name);
10771 20 : st->n.sym = sym->result;
10772 20 : sym->result->refs++;
10773 : }
10774 : }
10775 : else
10776 50 : sym->result = sym;
10777 : }
10778 :
10779 : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
10780 : the symbol existed before. */
10781 267 : sym->declared_at = gfc_current_locus;
10782 :
10783 267 : if (!sym->attr.module_procedure)
10784 : return MATCH_ERROR;
10785 :
10786 : /* Signal match_end to expect "end procedure". */
10787 265 : sym->abr_modproc_decl = 1;
10788 :
10789 : /* Change from IFSRC_IFBODY coming from the interface declaration. */
10790 265 : sym->attr.if_source = IFSRC_DECL;
10791 :
10792 265 : gfc_new_block = sym;
10793 :
10794 : /* Make a new formal arglist with the symbols in the procedure
10795 : namespace. */
10796 265 : head = tail = NULL;
10797 600 : for (formal = sym->formal; formal && formal->sym; formal = formal->next)
10798 : {
10799 335 : if (formal == sym->formal)
10800 238 : head = tail = gfc_get_formal_arglist ();
10801 : else
10802 : {
10803 97 : tail->next = gfc_get_formal_arglist ();
10804 97 : tail = tail->next;
10805 : }
10806 :
10807 335 : if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
10808 0 : goto cleanup;
10809 :
10810 335 : tail->sym = fsym;
10811 335 : gfc_set_sym_referenced (fsym);
10812 : }
10813 :
10814 : /* The dummy symbols get cleaned up, when the formal_namespace of the
10815 : interface declaration is cleared. This allows us to add the
10816 : explicit interface as is done for other type of procedure. */
10817 265 : if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
10818 : &gfc_current_locus))
10819 : return MATCH_ERROR;
10820 :
10821 265 : if (gfc_match_eos () != MATCH_YES)
10822 : {
10823 : /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
10824 : undone, such that the st->n.sym->formal points to the original symbol;
10825 : if now this namespace is finalized, the formal namespace is freed,
10826 : but it might be still needed in the parent namespace. */
10827 1 : gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
10828 1 : st->n.sym = NULL;
10829 1 : gfc_free_symbol (sym->tlink);
10830 1 : sym->tlink = NULL;
10831 1 : sym->refs--;
10832 1 : gfc_syntax_error (ST_MODULE_PROC);
10833 1 : return MATCH_ERROR;
10834 : }
10835 :
10836 : return MATCH_YES;
10837 :
10838 0 : cleanup:
10839 0 : gfc_free_formal_arglist (head);
10840 0 : return MATCH_ERROR;
10841 : }
10842 :
10843 :
10844 : /* Match a module procedure statement. Note that we have to modify
10845 : symbols in the parent's namespace because the current one was there
10846 : to receive symbols that are in an interface's formal argument list. */
10847 :
10848 : match
10849 1620 : gfc_match_modproc (void)
10850 : {
10851 1620 : char name[GFC_MAX_SYMBOL_LEN + 1];
10852 1620 : gfc_symbol *sym;
10853 1620 : match m;
10854 1620 : locus old_locus;
10855 1620 : gfc_namespace *module_ns;
10856 1620 : gfc_interface *old_interface_head, *interface;
10857 :
10858 1620 : if (gfc_state_stack->previous == NULL
10859 1618 : || (gfc_state_stack->state != COMP_INTERFACE
10860 5 : && (gfc_state_stack->state != COMP_CONTAINS
10861 4 : || gfc_state_stack->previous->state != COMP_INTERFACE))
10862 1613 : || current_interface.type == INTERFACE_NAMELESS
10863 1613 : || current_interface.type == INTERFACE_ABSTRACT)
10864 : {
10865 8 : gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
10866 : "interface");
10867 8 : return MATCH_ERROR;
10868 : }
10869 :
10870 1612 : module_ns = gfc_current_ns->parent;
10871 1618 : for (; module_ns; module_ns = module_ns->parent)
10872 1618 : if (module_ns->proc_name->attr.flavor == FL_MODULE
10873 29 : || module_ns->proc_name->attr.flavor == FL_PROGRAM
10874 12 : || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
10875 12 : && !module_ns->proc_name->attr.contained))
10876 : break;
10877 :
10878 1612 : if (module_ns == NULL)
10879 : return MATCH_ERROR;
10880 :
10881 : /* Store the current state of the interface. We will need it if we
10882 : end up with a syntax error and need to recover. */
10883 1612 : old_interface_head = gfc_current_interface_head ();
10884 :
10885 : /* Check if the F2008 optional double colon appears. */
10886 1612 : gfc_gobble_whitespace ();
10887 1612 : old_locus = gfc_current_locus;
10888 1612 : if (gfc_match ("::") == MATCH_YES)
10889 : {
10890 25 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
10891 : "MODULE PROCEDURE statement at %L", &old_locus))
10892 : return MATCH_ERROR;
10893 : }
10894 : else
10895 1587 : gfc_current_locus = old_locus;
10896 :
10897 1967 : for (;;)
10898 : {
10899 1967 : bool last = false;
10900 1967 : old_locus = gfc_current_locus;
10901 :
10902 1967 : m = gfc_match_name (name);
10903 1967 : if (m == MATCH_NO)
10904 1 : goto syntax;
10905 1966 : if (m != MATCH_YES)
10906 : return MATCH_ERROR;
10907 :
10908 : /* Check for syntax error before starting to add symbols to the
10909 : current namespace. */
10910 1966 : if (gfc_match_eos () == MATCH_YES)
10911 : last = true;
10912 :
10913 360 : if (!last && gfc_match_char (',') != MATCH_YES)
10914 2 : goto syntax;
10915 :
10916 : /* Now we're sure the syntax is valid, we process this item
10917 : further. */
10918 1964 : if (gfc_get_symbol (name, module_ns, &sym))
10919 : return MATCH_ERROR;
10920 :
10921 1964 : if (sym->attr.intrinsic)
10922 : {
10923 1 : gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
10924 : "PROCEDURE", &old_locus);
10925 1 : return MATCH_ERROR;
10926 : }
10927 :
10928 1963 : if (sym->attr.proc != PROC_MODULE
10929 1963 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
10930 : return MATCH_ERROR;
10931 :
10932 1960 : if (!gfc_add_interface (sym))
10933 : return MATCH_ERROR;
10934 :
10935 1957 : sym->attr.mod_proc = 1;
10936 1957 : sym->declared_at = old_locus;
10937 :
10938 1957 : if (last)
10939 : break;
10940 : }
10941 :
10942 : return MATCH_YES;
10943 :
10944 3 : syntax:
10945 : /* Restore the previous state of the interface. */
10946 3 : interface = gfc_current_interface_head ();
10947 3 : gfc_set_current_interface_head (old_interface_head);
10948 :
10949 : /* Free the new interfaces. */
10950 10 : while (interface != old_interface_head)
10951 : {
10952 4 : gfc_interface *i = interface->next;
10953 4 : free (interface);
10954 4 : interface = i;
10955 : }
10956 :
10957 : /* And issue a syntax error. */
10958 3 : gfc_syntax_error (ST_MODULE_PROC);
10959 3 : return MATCH_ERROR;
10960 : }
10961 :
10962 :
10963 : /* Check a derived type that is being extended. */
10964 :
10965 : static gfc_symbol*
10966 1491 : check_extended_derived_type (char *name)
10967 : {
10968 1491 : gfc_symbol *extended;
10969 :
10970 1491 : if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
10971 : {
10972 0 : gfc_error ("Ambiguous symbol in TYPE definition at %C");
10973 0 : return NULL;
10974 : }
10975 :
10976 1491 : extended = gfc_find_dt_in_generic (extended);
10977 :
10978 : /* F08:C428. */
10979 1491 : if (!extended)
10980 : {
10981 2 : gfc_error ("Symbol %qs at %C has not been previously defined", name);
10982 2 : return NULL;
10983 : }
10984 :
10985 1489 : if (extended->attr.flavor != FL_DERIVED)
10986 : {
10987 0 : gfc_error ("%qs in EXTENDS expression at %C is not a "
10988 : "derived type", name);
10989 0 : return NULL;
10990 : }
10991 :
10992 1489 : if (extended->attr.is_bind_c)
10993 : {
10994 1 : gfc_error ("%qs cannot be extended at %C because it "
10995 : "is BIND(C)", extended->name);
10996 1 : return NULL;
10997 : }
10998 :
10999 1488 : if (extended->attr.sequence)
11000 : {
11001 1 : gfc_error ("%qs cannot be extended at %C because it "
11002 : "is a SEQUENCE type", extended->name);
11003 1 : return NULL;
11004 : }
11005 :
11006 : return extended;
11007 : }
11008 :
11009 :
11010 : /* Match the optional attribute specifiers for a type declaration.
11011 : Return MATCH_ERROR if an error is encountered in one of the handled
11012 : attributes (public, private, bind(c)), MATCH_NO if what's found is
11013 : not a handled attribute, and MATCH_YES otherwise. TODO: More error
11014 : checking on attribute conflicts needs to be done. */
11015 :
11016 : static match
11017 19484 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
11018 : {
11019 : /* See if the derived type is marked as private. */
11020 19484 : if (gfc_match (" , private") == MATCH_YES)
11021 : {
11022 15 : if (gfc_current_state () != COMP_MODULE)
11023 : {
11024 1 : gfc_error ("Derived type at %C can only be PRIVATE in the "
11025 : "specification part of a module");
11026 1 : return MATCH_ERROR;
11027 : }
11028 :
11029 14 : if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
11030 : return MATCH_ERROR;
11031 : }
11032 19469 : else if (gfc_match (" , public") == MATCH_YES)
11033 : {
11034 546 : if (gfc_current_state () != COMP_MODULE)
11035 : {
11036 0 : gfc_error ("Derived type at %C can only be PUBLIC in the "
11037 : "specification part of a module");
11038 0 : return MATCH_ERROR;
11039 : }
11040 :
11041 546 : if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
11042 : return MATCH_ERROR;
11043 : }
11044 18923 : else if (gfc_match (" , bind ( c )") == MATCH_YES)
11045 : {
11046 : /* If the type is defined to be bind(c) it then needs to make
11047 : sure that all fields are interoperable. This will
11048 : need to be a semantic check on the finished derived type.
11049 : See 15.2.3 (lines 9-12) of F2003 draft. */
11050 407 : if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
11051 : return MATCH_ERROR;
11052 :
11053 : /* TODO: attr conflicts need to be checked, probably in symbol.cc. */
11054 : }
11055 18516 : else if (gfc_match (" , abstract") == MATCH_YES)
11056 : {
11057 337 : if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
11058 : return MATCH_ERROR;
11059 :
11060 336 : if (!gfc_add_abstract (attr, &gfc_current_locus))
11061 : return MATCH_ERROR;
11062 : }
11063 18179 : else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
11064 : {
11065 1492 : if (!gfc_add_extension (attr, &gfc_current_locus))
11066 : return MATCH_ERROR;
11067 : }
11068 : else
11069 16687 : return MATCH_NO;
11070 :
11071 : /* If we get here, something matched. */
11072 : return MATCH_YES;
11073 : }
11074 :
11075 :
11076 : /* Common function for type declaration blocks similar to derived types, such
11077 : as STRUCTURES and MAPs. Unlike derived types, a structure type
11078 : does NOT have a generic symbol matching the name given by the user.
11079 : STRUCTUREs can share names with variables and PARAMETERs so we must allow
11080 : for the creation of an independent symbol.
11081 : Other parameters are a message to prefix errors with, the name of the new
11082 : type to be created, and the flavor to add to the resulting symbol. */
11083 :
11084 : static bool
11085 717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
11086 : gfc_symbol **result)
11087 : {
11088 717 : gfc_symbol *sym;
11089 717 : locus where;
11090 :
11091 717 : gcc_assert (name[0] == (char) TOUPPER (name[0]));
11092 :
11093 717 : if (decl)
11094 717 : where = *decl;
11095 : else
11096 0 : where = gfc_current_locus;
11097 :
11098 717 : if (gfc_get_symbol (name, NULL, &sym))
11099 : return false;
11100 :
11101 717 : if (!sym)
11102 : {
11103 0 : gfc_internal_error ("Failed to create structure type '%s' at %C", name);
11104 : return false;
11105 : }
11106 :
11107 717 : if (sym->components != NULL || sym->attr.zero_comp)
11108 : {
11109 3 : gfc_error ("Type definition of %qs at %C was already defined at %L",
11110 : sym->name, &sym->declared_at);
11111 3 : return false;
11112 : }
11113 :
11114 714 : sym->declared_at = where;
11115 :
11116 714 : if (sym->attr.flavor != fl
11117 714 : && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
11118 : return false;
11119 :
11120 714 : if (!sym->hash_value)
11121 : /* Set the hash for the compound name for this type. */
11122 713 : sym->hash_value = gfc_hash_value (sym);
11123 :
11124 : /* Normally the type is expected to have been completely parsed by the time
11125 : a field declaration with this type is seen. For unions, maps, and nested
11126 : structure declarations, we need to indicate that it is okay that we
11127 : haven't seen any components yet. This will be updated after the structure
11128 : is fully parsed. */
11129 714 : sym->attr.zero_comp = 0;
11130 :
11131 : /* Structures always act like derived-types with the SEQUENCE attribute */
11132 714 : gfc_add_sequence (&sym->attr, sym->name, NULL);
11133 :
11134 714 : if (result) *result = sym;
11135 :
11136 : return true;
11137 : }
11138 :
11139 :
11140 : /* Match the opening of a MAP block. Like a struct within a union in C;
11141 : behaves identical to STRUCTURE blocks. */
11142 :
11143 : match
11144 259 : gfc_match_map (void)
11145 : {
11146 : /* Counter used to give unique internal names to map structures. */
11147 259 : static unsigned int gfc_map_id = 0;
11148 259 : char name[GFC_MAX_SYMBOL_LEN + 1];
11149 259 : gfc_symbol *sym;
11150 259 : locus old_loc;
11151 :
11152 259 : old_loc = gfc_current_locus;
11153 :
11154 259 : if (gfc_match_eos () != MATCH_YES)
11155 : {
11156 1 : gfc_error ("Junk after MAP statement at %C");
11157 1 : gfc_current_locus = old_loc;
11158 1 : return MATCH_ERROR;
11159 : }
11160 :
11161 : /* Map blocks are anonymous so we make up unique names for the symbol table
11162 : which are invalid Fortran identifiers. */
11163 258 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
11164 :
11165 258 : if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
11166 : return MATCH_ERROR;
11167 :
11168 258 : gfc_new_block = sym;
11169 :
11170 258 : return MATCH_YES;
11171 : }
11172 :
11173 :
11174 : /* Match the opening of a UNION block. */
11175 :
11176 : match
11177 133 : gfc_match_union (void)
11178 : {
11179 : /* Counter used to give unique internal names to union types. */
11180 133 : static unsigned int gfc_union_id = 0;
11181 133 : char name[GFC_MAX_SYMBOL_LEN + 1];
11182 133 : gfc_symbol *sym;
11183 133 : locus old_loc;
11184 :
11185 133 : old_loc = gfc_current_locus;
11186 :
11187 133 : if (gfc_match_eos () != MATCH_YES)
11188 : {
11189 1 : gfc_error ("Junk after UNION statement at %C");
11190 1 : gfc_current_locus = old_loc;
11191 1 : return MATCH_ERROR;
11192 : }
11193 :
11194 : /* Unions are anonymous so we make up unique names for the symbol table
11195 : which are invalid Fortran identifiers. */
11196 132 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
11197 :
11198 132 : if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
11199 : return MATCH_ERROR;
11200 :
11201 132 : gfc_new_block = sym;
11202 :
11203 132 : return MATCH_YES;
11204 : }
11205 :
11206 :
11207 : /* Match the beginning of a STRUCTURE declaration. This is similar to
11208 : matching the beginning of a derived type declaration with a few
11209 : twists. The resulting type symbol has no access control or other
11210 : interesting attributes. */
11211 :
11212 : match
11213 336 : gfc_match_structure_decl (void)
11214 : {
11215 : /* Counter used to give unique internal names to anonymous structures. */
11216 336 : static unsigned int gfc_structure_id = 0;
11217 336 : char name[GFC_MAX_SYMBOL_LEN + 1];
11218 336 : gfc_symbol *sym;
11219 336 : match m;
11220 336 : locus where;
11221 :
11222 336 : if (!flag_dec_structure)
11223 : {
11224 3 : gfc_error ("%s at %C is a DEC extension, enable with "
11225 : "%<-fdec-structure%>",
11226 : "STRUCTURE");
11227 3 : return MATCH_ERROR;
11228 : }
11229 :
11230 333 : name[0] = '\0';
11231 :
11232 333 : m = gfc_match (" /%n/", name);
11233 333 : if (m != MATCH_YES)
11234 : {
11235 : /* Non-nested structure declarations require a structure name. */
11236 24 : if (!gfc_comp_struct (gfc_current_state ()))
11237 : {
11238 4 : gfc_error ("Structure name expected in non-nested structure "
11239 : "declaration at %C");
11240 4 : return MATCH_ERROR;
11241 : }
11242 : /* This is an anonymous structure; make up a unique name for it
11243 : (upper-case letters never make it to symbol names from the source).
11244 : The important thing is initializing the type variable
11245 : and setting gfc_new_symbol, which is immediately used by
11246 : parse_structure () and variable_decl () to add components of
11247 : this type. */
11248 20 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
11249 : }
11250 :
11251 329 : where = gfc_current_locus;
11252 : /* No field list allowed after non-nested structure declaration. */
11253 329 : if (!gfc_comp_struct (gfc_current_state ())
11254 296 : && gfc_match_eos () != MATCH_YES)
11255 : {
11256 1 : gfc_error ("Junk after non-nested STRUCTURE statement at %C");
11257 1 : return MATCH_ERROR;
11258 : }
11259 :
11260 : /* Make sure the name is not the name of an intrinsic type. */
11261 328 : if (gfc_is_intrinsic_typename (name))
11262 : {
11263 1 : gfc_error ("Structure name %qs at %C cannot be the same as an"
11264 : " intrinsic type", name);
11265 1 : return MATCH_ERROR;
11266 : }
11267 :
11268 : /* Store the actual type symbol for the structure with an upper-case first
11269 : letter (an invalid Fortran identifier). */
11270 :
11271 327 : if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
11272 : return MATCH_ERROR;
11273 :
11274 324 : gfc_new_block = sym;
11275 324 : return MATCH_YES;
11276 : }
11277 :
11278 :
11279 : /* This function does some work to determine which matcher should be used to
11280 : * match a statement beginning with "TYPE". This is used to disambiguate TYPE
11281 : * as an alias for PRINT from derived type declarations, TYPE IS statements,
11282 : * and [parameterized] derived type declarations. */
11283 :
11284 : match
11285 532268 : gfc_match_type (gfc_statement *st)
11286 : {
11287 532268 : char name[GFC_MAX_SYMBOL_LEN + 1];
11288 532268 : match m;
11289 532268 : locus old_loc;
11290 :
11291 : /* Requires -fdec. */
11292 532268 : if (!flag_dec)
11293 : return MATCH_NO;
11294 :
11295 2483 : m = gfc_match ("type");
11296 2483 : if (m != MATCH_YES)
11297 : return m;
11298 : /* If we already have an error in the buffer, it is probably from failing to
11299 : * match a derived type data declaration. Let it happen. */
11300 20 : else if (gfc_error_flag_test ())
11301 : return MATCH_NO;
11302 :
11303 20 : old_loc = gfc_current_locus;
11304 20 : *st = ST_NONE;
11305 :
11306 : /* If we see an attribute list before anything else it's definitely a derived
11307 : * type declaration. */
11308 20 : if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
11309 8 : goto derived;
11310 :
11311 : /* By now "TYPE" has already been matched. If we do not see a name, this may
11312 : * be something like "TYPE *" or "TYPE <fmt>". */
11313 12 : m = gfc_match_name (name);
11314 12 : if (m != MATCH_YES)
11315 : {
11316 : /* Let print match if it can, otherwise throw an error from
11317 : * gfc_match_derived_decl. */
11318 7 : gfc_current_locus = old_loc;
11319 7 : if (gfc_match_print () == MATCH_YES)
11320 : {
11321 7 : *st = ST_WRITE;
11322 7 : return MATCH_YES;
11323 : }
11324 0 : goto derived;
11325 : }
11326 :
11327 : /* Check for EOS. */
11328 5 : if (gfc_match_eos () == MATCH_YES)
11329 : {
11330 : /* By now we have "TYPE <name> <EOS>". Check first if the name is an
11331 : * intrinsic typename - if so let gfc_match_derived_decl dump an error.
11332 : * Otherwise if gfc_match_derived_decl fails it's probably an existing
11333 : * symbol which can be printed. */
11334 3 : gfc_current_locus = old_loc;
11335 3 : m = gfc_match_derived_decl ();
11336 3 : if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
11337 : {
11338 2 : *st = ST_DERIVED_DECL;
11339 2 : return m;
11340 : }
11341 : }
11342 : else
11343 : {
11344 : /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
11345 : like <type name(parameter)>. */
11346 2 : gfc_gobble_whitespace ();
11347 2 : bool paren = gfc_peek_ascii_char () == '(';
11348 2 : if (paren)
11349 : {
11350 1 : if (strcmp ("is", name) == 0)
11351 1 : goto typeis;
11352 : else
11353 0 : goto derived;
11354 : }
11355 : }
11356 :
11357 : /* Treat TYPE... like PRINT... */
11358 2 : gfc_current_locus = old_loc;
11359 2 : *st = ST_WRITE;
11360 2 : return gfc_match_print ();
11361 :
11362 8 : derived:
11363 8 : gfc_current_locus = old_loc;
11364 8 : *st = ST_DERIVED_DECL;
11365 8 : return gfc_match_derived_decl ();
11366 :
11367 1 : typeis:
11368 1 : gfc_current_locus = old_loc;
11369 1 : *st = ST_TYPE_IS;
11370 1 : return gfc_match_type_is ();
11371 : }
11372 :
11373 :
11374 : /* Match the beginning of a derived type declaration. If a type name
11375 : was the result of a function, then it is possible to have a symbol
11376 : already to be known as a derived type yet have no components. */
11377 :
11378 : match
11379 16694 : gfc_match_derived_decl (void)
11380 : {
11381 16694 : char name[GFC_MAX_SYMBOL_LEN + 1];
11382 16694 : char parent[GFC_MAX_SYMBOL_LEN + 1];
11383 16694 : symbol_attribute attr;
11384 16694 : gfc_symbol *sym, *gensym;
11385 16694 : gfc_symbol *extended;
11386 16694 : match m;
11387 16694 : match is_type_attr_spec = MATCH_NO;
11388 16694 : bool seen_attr = false;
11389 16694 : gfc_interface *intr = NULL, *head;
11390 16694 : bool parameterized_type = false;
11391 16694 : bool seen_colons = false;
11392 :
11393 16694 : if (gfc_comp_struct (gfc_current_state ()))
11394 : return MATCH_NO;
11395 :
11396 16690 : name[0] = '\0';
11397 16690 : parent[0] = '\0';
11398 16690 : gfc_clear_attr (&attr);
11399 16690 : extended = NULL;
11400 :
11401 19484 : do
11402 : {
11403 19484 : is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
11404 19484 : if (is_type_attr_spec == MATCH_ERROR)
11405 : return MATCH_ERROR;
11406 19481 : if (is_type_attr_spec == MATCH_YES)
11407 2794 : seen_attr = true;
11408 19481 : } while (is_type_attr_spec == MATCH_YES);
11409 :
11410 : /* Deal with derived type extensions. The extension attribute has
11411 : been added to 'attr' but now the parent type must be found and
11412 : checked. */
11413 16687 : if (parent[0])
11414 1491 : extended = check_extended_derived_type (parent);
11415 :
11416 16687 : if (parent[0] && !extended)
11417 : return MATCH_ERROR;
11418 :
11419 16683 : m = gfc_match (" ::");
11420 16683 : if (m == MATCH_YES)
11421 : {
11422 : seen_colons = true;
11423 : }
11424 10509 : else if (seen_attr)
11425 : {
11426 5 : gfc_error ("Expected :: in TYPE definition at %C");
11427 5 : return MATCH_ERROR;
11428 : }
11429 :
11430 : /* In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
11431 : But, we need to simply return for TYPE(. */
11432 10504 : if (m == MATCH_NO && gfc_current_form == FORM_FREE)
11433 : {
11434 10455 : char c = gfc_peek_ascii_char ();
11435 10455 : if (c == '(')
11436 : return m;
11437 10374 : if (!gfc_is_whitespace (c))
11438 : {
11439 4 : gfc_error ("Mangled derived type definition at %C");
11440 4 : return MATCH_NO;
11441 : }
11442 : }
11443 :
11444 16593 : m = gfc_match (" %n ", name);
11445 16593 : if (m != MATCH_YES)
11446 : return m;
11447 :
11448 : /* Make sure that we don't identify TYPE IS (...) as a parameterized
11449 : derived type named 'is'.
11450 : TODO Expand the check, when 'name' = "is" by matching " (tname) "
11451 : and checking if this is a(n intrinsic) typename. This picks up
11452 : misplaced TYPE IS statements such as in select_type_1.f03. */
11453 16581 : if (gfc_peek_ascii_char () == '(')
11454 : {
11455 3941 : if (gfc_current_state () == COMP_SELECT_TYPE
11456 459 : || (!seen_colons && !strcmp (name, "is")))
11457 : return MATCH_NO;
11458 : parameterized_type = true;
11459 : }
11460 :
11461 13097 : m = gfc_match_eos ();
11462 13097 : if (m != MATCH_YES && !parameterized_type)
11463 : return m;
11464 :
11465 : /* Make sure the name is not the name of an intrinsic type. */
11466 13094 : if (gfc_is_intrinsic_typename (name))
11467 : {
11468 18 : gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
11469 : "type", name);
11470 18 : return MATCH_ERROR;
11471 : }
11472 :
11473 13076 : if (gfc_get_symbol (name, NULL, &gensym))
11474 : return MATCH_ERROR;
11475 :
11476 13076 : if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
11477 : {
11478 5 : if (gensym->ts.u.derived)
11479 0 : gfc_error ("Derived type name %qs at %C already has a basic type "
11480 : "of %s", gensym->name, gfc_typename (&gensym->ts));
11481 : else
11482 5 : gfc_error ("Derived type name %qs at %C already has a basic type",
11483 : gensym->name);
11484 5 : return MATCH_ERROR;
11485 : }
11486 :
11487 13071 : if (!gensym->attr.generic
11488 13071 : && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
11489 : return MATCH_ERROR;
11490 :
11491 13067 : if (!gensym->attr.function
11492 13067 : && !gfc_add_function (&gensym->attr, gensym->name, NULL))
11493 : return MATCH_ERROR;
11494 :
11495 13066 : if (gensym->attr.dummy)
11496 : {
11497 1 : gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
11498 : name, &gensym->declared_at);
11499 1 : return MATCH_ERROR;
11500 : }
11501 :
11502 13065 : sym = gfc_find_dt_in_generic (gensym);
11503 :
11504 13065 : if (sym && (sym->components != NULL || sym->attr.zero_comp))
11505 : {
11506 1 : gfc_error ("Derived type definition of %qs at %C has already been "
11507 : "defined", sym->name);
11508 1 : return MATCH_ERROR;
11509 : }
11510 :
11511 13064 : if (!sym)
11512 : {
11513 : /* Use upper case to save the actual derived-type symbol. */
11514 12974 : gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
11515 12974 : sym->name = gfc_get_string ("%s", gensym->name);
11516 12974 : head = gensym->generic;
11517 12974 : intr = gfc_get_interface ();
11518 12974 : intr->sym = sym;
11519 12974 : intr->where = gfc_current_locus;
11520 12974 : intr->sym->declared_at = gfc_current_locus;
11521 12974 : intr->next = head;
11522 12974 : gensym->generic = intr;
11523 12974 : gensym->attr.if_source = IFSRC_DECL;
11524 : }
11525 :
11526 : /* The symbol may already have the derived attribute without the
11527 : components. The ways this can happen is via a function
11528 : definition, an INTRINSIC statement or a subtype in another
11529 : derived type that is a pointer. The first part of the AND clause
11530 : is true if the symbol is not the return value of a function. */
11531 13064 : if (sym->attr.flavor != FL_DERIVED
11532 13064 : && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
11533 : return MATCH_ERROR;
11534 :
11535 13064 : if (attr.access != ACCESS_UNKNOWN
11536 13064 : && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
11537 : return MATCH_ERROR;
11538 13064 : else if (sym->attr.access == ACCESS_UNKNOWN
11539 12508 : && gensym->attr.access != ACCESS_UNKNOWN
11540 13412 : && !gfc_add_access (&sym->attr, gensym->attr.access,
11541 : sym->name, NULL))
11542 : return MATCH_ERROR;
11543 :
11544 13064 : if (sym->attr.access != ACCESS_UNKNOWN
11545 904 : && gensym->attr.access == ACCESS_UNKNOWN)
11546 556 : gensym->attr.access = sym->attr.access;
11547 :
11548 : /* See if the derived type was labeled as bind(c). */
11549 13064 : if (attr.is_bind_c != 0)
11550 404 : sym->attr.is_bind_c = attr.is_bind_c;
11551 :
11552 : /* Construct the f2k_derived namespace if it is not yet there. */
11553 13064 : if (!sym->f2k_derived)
11554 13064 : sym->f2k_derived = gfc_get_namespace (NULL, 0);
11555 :
11556 13064 : if (parameterized_type)
11557 : {
11558 : /* Ignore error or mismatches by going to the end of the statement
11559 : in order to avoid the component declarations causing problems. */
11560 457 : m = gfc_match_formal_arglist (sym, 0, 0, true);
11561 457 : if (m != MATCH_YES)
11562 4 : gfc_error_recovery ();
11563 : else
11564 453 : sym->attr.pdt_template = 1;
11565 457 : m = gfc_match_eos ();
11566 457 : if (m != MATCH_YES)
11567 : {
11568 1 : gfc_error_recovery ();
11569 1 : gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
11570 : }
11571 : }
11572 :
11573 13064 : if (extended && !sym->components)
11574 : {
11575 1487 : gfc_component *p;
11576 1487 : gfc_formal_arglist *f, *g, *h;
11577 :
11578 : /* Add the extended derived type as the first component. */
11579 1487 : gfc_add_component (sym, parent, &p);
11580 1487 : extended->refs++;
11581 1487 : gfc_set_sym_referenced (extended);
11582 :
11583 1487 : p->ts.type = BT_DERIVED;
11584 1487 : p->ts.u.derived = extended;
11585 1487 : p->initializer = gfc_default_initializer (&p->ts);
11586 :
11587 : /* Set extension level. */
11588 1487 : if (extended->attr.extension == 255)
11589 : {
11590 : /* Since the extension field is 8 bit wide, we can only have
11591 : up to 255 extension levels. */
11592 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
11593 : extended->name, &extended->declared_at);
11594 0 : return MATCH_ERROR;
11595 : }
11596 1487 : sym->attr.extension = extended->attr.extension + 1;
11597 :
11598 : /* Provide the links between the extended type and its extension. */
11599 1487 : if (!extended->f2k_derived)
11600 1 : extended->f2k_derived = gfc_get_namespace (NULL, 0);
11601 :
11602 : /* Copy the extended type-param-name-list from the extended type,
11603 : append those of the extension and add the whole lot to the
11604 : extension. */
11605 1487 : if (extended->attr.pdt_template)
11606 : {
11607 40 : g = h = NULL;
11608 40 : sym->attr.pdt_template = 1;
11609 111 : for (f = extended->formal; f; f = f->next)
11610 : {
11611 71 : if (f == extended->formal)
11612 : {
11613 40 : g = gfc_get_formal_arglist ();
11614 40 : h = g;
11615 : }
11616 : else
11617 : {
11618 31 : g->next = gfc_get_formal_arglist ();
11619 31 : g = g->next;
11620 : }
11621 71 : g->sym = f->sym;
11622 : }
11623 40 : g->next = sym->formal;
11624 40 : sym->formal = h;
11625 : }
11626 : }
11627 :
11628 13064 : if (!sym->hash_value)
11629 : /* Set the hash for the compound name for this type. */
11630 13064 : sym->hash_value = gfc_hash_value (sym);
11631 :
11632 : /* Take over the ABSTRACT attribute. */
11633 13064 : sym->attr.abstract = attr.abstract;
11634 :
11635 13064 : gfc_new_block = sym;
11636 :
11637 13064 : return MATCH_YES;
11638 : }
11639 :
11640 :
11641 : /* Cray Pointees can be declared as:
11642 : pointer (ipt, a (n,m,...,*)) */
11643 :
11644 : match
11645 240 : gfc_mod_pointee_as (gfc_array_spec *as)
11646 : {
11647 240 : as->cray_pointee = true; /* This will be useful to know later. */
11648 240 : if (as->type == AS_ASSUMED_SIZE)
11649 72 : as->cp_was_assumed = true;
11650 168 : else if (as->type == AS_ASSUMED_SHAPE)
11651 : {
11652 0 : gfc_error ("Cray Pointee at %C cannot be assumed shape array");
11653 0 : return MATCH_ERROR;
11654 : }
11655 : return MATCH_YES;
11656 : }
11657 :
11658 :
11659 : /* Match the enum definition statement, here we are trying to match
11660 : the first line of enum definition statement.
11661 : Returns MATCH_YES if match is found. */
11662 :
11663 : match
11664 158 : gfc_match_enum (void)
11665 : {
11666 158 : match m;
11667 :
11668 158 : m = gfc_match_eos ();
11669 158 : if (m != MATCH_YES)
11670 : return m;
11671 :
11672 158 : if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
11673 0 : return MATCH_ERROR;
11674 :
11675 : return MATCH_YES;
11676 : }
11677 :
11678 :
11679 : /* Returns an initializer whose value is one higher than the value of the
11680 : LAST_INITIALIZER argument. If the argument is NULL, the
11681 : initializers value will be set to zero. The initializer's kind
11682 : will be set to gfc_c_int_kind.
11683 :
11684 : If -fshort-enums is given, the appropriate kind will be selected
11685 : later after all enumerators have been parsed. A warning is issued
11686 : here if an initializer exceeds gfc_c_int_kind. */
11687 :
11688 : static gfc_expr *
11689 377 : enum_initializer (gfc_expr *last_initializer, locus where)
11690 : {
11691 377 : gfc_expr *result;
11692 377 : result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
11693 :
11694 377 : mpz_init (result->value.integer);
11695 :
11696 377 : if (last_initializer != NULL)
11697 : {
11698 266 : mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
11699 266 : result->where = last_initializer->where;
11700 :
11701 266 : if (gfc_check_integer_range (result->value.integer,
11702 : gfc_c_int_kind) != ARITH_OK)
11703 : {
11704 0 : gfc_error ("Enumerator exceeds the C integer type at %C");
11705 0 : return NULL;
11706 : }
11707 : }
11708 : else
11709 : {
11710 : /* Control comes here, if it's the very first enumerator and no
11711 : initializer has been given. It will be initialized to zero. */
11712 111 : mpz_set_si (result->value.integer, 0);
11713 : }
11714 :
11715 : return result;
11716 : }
11717 :
11718 :
11719 : /* Match a variable name with an optional initializer. When this
11720 : subroutine is called, a variable is expected to be parsed next.
11721 : Depending on what is happening at the moment, updates either the
11722 : symbol table or the current interface. */
11723 :
11724 : static match
11725 549 : enumerator_decl (void)
11726 : {
11727 549 : char name[GFC_MAX_SYMBOL_LEN + 1];
11728 549 : gfc_expr *initializer;
11729 549 : gfc_array_spec *as = NULL;
11730 549 : gfc_charlen *saved_cl_list;
11731 549 : gfc_symbol *sym;
11732 549 : locus var_locus;
11733 549 : match m;
11734 549 : bool t;
11735 549 : locus old_locus;
11736 :
11737 549 : initializer = NULL;
11738 549 : saved_cl_list = gfc_current_ns->cl_list;
11739 549 : old_locus = gfc_current_locus;
11740 :
11741 : /* When we get here, we've just matched a list of attributes and
11742 : maybe a type and a double colon. The next thing we expect to see
11743 : is the name of the symbol. */
11744 549 : m = gfc_match_name (name);
11745 549 : if (m != MATCH_YES)
11746 1 : goto cleanup;
11747 :
11748 548 : var_locus = gfc_current_locus;
11749 :
11750 : /* OK, we've successfully matched the declaration. Now put the
11751 : symbol in the current namespace. If we fail to create the symbol,
11752 : bail out. */
11753 548 : if (!build_sym (name, 1, NULL, false, &as, &var_locus))
11754 : {
11755 1 : m = MATCH_ERROR;
11756 1 : goto cleanup;
11757 : }
11758 :
11759 : /* The double colon must be present in order to have initializers.
11760 : Otherwise the statement is ambiguous with an assignment statement. */
11761 547 : if (colon_seen)
11762 : {
11763 471 : if (gfc_match_char ('=') == MATCH_YES)
11764 : {
11765 170 : m = gfc_match_init_expr (&initializer);
11766 170 : if (m == MATCH_NO)
11767 : {
11768 0 : gfc_error ("Expected an initialization expression at %C");
11769 0 : m = MATCH_ERROR;
11770 : }
11771 :
11772 170 : if (m != MATCH_YES)
11773 2 : goto cleanup;
11774 : }
11775 : }
11776 :
11777 : /* If we do not have an initializer, the initialization value of the
11778 : previous enumerator (stored in last_initializer) is incremented
11779 : by 1 and is used to initialize the current enumerator. */
11780 545 : if (initializer == NULL)
11781 377 : initializer = enum_initializer (last_initializer, old_locus);
11782 :
11783 545 : if (initializer == NULL || initializer->ts.type != BT_INTEGER)
11784 : {
11785 2 : gfc_error ("ENUMERATOR %L not initialized with integer expression",
11786 : &var_locus);
11787 2 : m = MATCH_ERROR;
11788 2 : goto cleanup;
11789 : }
11790 :
11791 : /* Store this current initializer, for the next enumerator variable
11792 : to be parsed. add_init_expr_to_sym() zeros initializer, so we
11793 : use last_initializer below. */
11794 543 : last_initializer = initializer;
11795 543 : t = add_init_expr_to_sym (name, &initializer, &var_locus,
11796 : saved_cl_list);
11797 :
11798 : /* Maintain enumerator history. */
11799 543 : gfc_find_symbol (name, NULL, 0, &sym);
11800 543 : create_enum_history (sym, last_initializer);
11801 :
11802 543 : return (t) ? MATCH_YES : MATCH_ERROR;
11803 :
11804 6 : cleanup:
11805 : /* Free stuff up and return. */
11806 6 : gfc_free_expr (initializer);
11807 :
11808 6 : return m;
11809 : }
11810 :
11811 :
11812 : /* Match the enumerator definition statement. */
11813 :
11814 : match
11815 812292 : gfc_match_enumerator_def (void)
11816 : {
11817 812292 : match m;
11818 812292 : bool t;
11819 :
11820 812292 : gfc_clear_ts (¤t_ts);
11821 :
11822 812292 : m = gfc_match (" enumerator");
11823 812292 : if (m != MATCH_YES)
11824 : return m;
11825 :
11826 269 : m = gfc_match (" :: ");
11827 269 : if (m == MATCH_ERROR)
11828 : return m;
11829 :
11830 269 : colon_seen = (m == MATCH_YES);
11831 :
11832 269 : if (gfc_current_state () != COMP_ENUM)
11833 : {
11834 4 : gfc_error ("ENUM definition statement expected before %C");
11835 4 : gfc_free_enum_history ();
11836 4 : return MATCH_ERROR;
11837 : }
11838 :
11839 265 : (¤t_ts)->type = BT_INTEGER;
11840 265 : (¤t_ts)->kind = gfc_c_int_kind;
11841 :
11842 265 : gfc_clear_attr (¤t_attr);
11843 265 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
11844 265 : if (!t)
11845 : {
11846 0 : m = MATCH_ERROR;
11847 0 : goto cleanup;
11848 : }
11849 :
11850 549 : for (;;)
11851 : {
11852 549 : m = enumerator_decl ();
11853 549 : if (m == MATCH_ERROR)
11854 : {
11855 6 : gfc_free_enum_history ();
11856 6 : goto cleanup;
11857 : }
11858 543 : if (m == MATCH_NO)
11859 : break;
11860 :
11861 542 : if (gfc_match_eos () == MATCH_YES)
11862 256 : goto cleanup;
11863 286 : if (gfc_match_char (',') != MATCH_YES)
11864 : break;
11865 : }
11866 :
11867 3 : if (gfc_current_state () == COMP_ENUM)
11868 : {
11869 3 : gfc_free_enum_history ();
11870 3 : gfc_error ("Syntax error in ENUMERATOR definition at %C");
11871 3 : m = MATCH_ERROR;
11872 : }
11873 :
11874 0 : cleanup:
11875 265 : gfc_free_array_spec (current_as);
11876 265 : current_as = NULL;
11877 265 : return m;
11878 :
11879 : }
11880 :
11881 :
11882 : /* Match binding attributes. */
11883 :
11884 : static match
11885 4714 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
11886 : {
11887 4714 : bool found_passing = false;
11888 4714 : bool seen_ptr = false;
11889 4714 : match m = MATCH_YES;
11890 :
11891 : /* Initialize to defaults. Do so even before the MATCH_NO check so that in
11892 : this case the defaults are in there. */
11893 4714 : ba->access = ACCESS_UNKNOWN;
11894 4714 : ba->pass_arg = NULL;
11895 4714 : ba->pass_arg_num = 0;
11896 4714 : ba->nopass = 0;
11897 4714 : ba->non_overridable = 0;
11898 4714 : ba->deferred = 0;
11899 4714 : ba->ppc = ppc;
11900 :
11901 : /* If we find a comma, we believe there are binding attributes. */
11902 4714 : m = gfc_match_char (',');
11903 4714 : if (m == MATCH_NO)
11904 2470 : goto done;
11905 :
11906 2793 : do
11907 : {
11908 : /* Access specifier. */
11909 :
11910 2793 : m = gfc_match (" public");
11911 2793 : if (m == MATCH_ERROR)
11912 0 : goto error;
11913 2793 : if (m == MATCH_YES)
11914 : {
11915 250 : if (ba->access != ACCESS_UNKNOWN)
11916 : {
11917 0 : gfc_error ("Duplicate access-specifier at %C");
11918 0 : goto error;
11919 : }
11920 :
11921 250 : ba->access = ACCESS_PUBLIC;
11922 250 : continue;
11923 : }
11924 :
11925 2543 : m = gfc_match (" private");
11926 2543 : if (m == MATCH_ERROR)
11927 0 : goto error;
11928 2543 : if (m == MATCH_YES)
11929 : {
11930 181 : if (ba->access != ACCESS_UNKNOWN)
11931 : {
11932 1 : gfc_error ("Duplicate access-specifier at %C");
11933 1 : goto error;
11934 : }
11935 :
11936 180 : ba->access = ACCESS_PRIVATE;
11937 180 : continue;
11938 : }
11939 :
11940 : /* If inside GENERIC, the following is not allowed. */
11941 2362 : if (!generic)
11942 : {
11943 :
11944 : /* NOPASS flag. */
11945 2361 : m = gfc_match (" nopass");
11946 2361 : if (m == MATCH_ERROR)
11947 0 : goto error;
11948 2361 : if (m == MATCH_YES)
11949 : {
11950 707 : if (found_passing)
11951 : {
11952 1 : gfc_error ("Binding attributes already specify passing,"
11953 : " illegal NOPASS at %C");
11954 1 : goto error;
11955 : }
11956 :
11957 706 : found_passing = true;
11958 706 : ba->nopass = 1;
11959 706 : continue;
11960 : }
11961 :
11962 : /* PASS possibly including argument. */
11963 1654 : m = gfc_match (" pass");
11964 1654 : if (m == MATCH_ERROR)
11965 0 : goto error;
11966 1654 : if (m == MATCH_YES)
11967 : {
11968 901 : char arg[GFC_MAX_SYMBOL_LEN + 1];
11969 :
11970 901 : if (found_passing)
11971 : {
11972 2 : gfc_error ("Binding attributes already specify passing,"
11973 : " illegal PASS at %C");
11974 2 : goto error;
11975 : }
11976 :
11977 899 : m = gfc_match (" ( %n )", arg);
11978 899 : if (m == MATCH_ERROR)
11979 0 : goto error;
11980 899 : if (m == MATCH_YES)
11981 490 : ba->pass_arg = gfc_get_string ("%s", arg);
11982 899 : gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
11983 :
11984 899 : found_passing = true;
11985 899 : ba->nopass = 0;
11986 899 : continue;
11987 899 : }
11988 :
11989 753 : if (ppc)
11990 : {
11991 : /* POINTER flag. */
11992 431 : m = gfc_match (" pointer");
11993 431 : if (m == MATCH_ERROR)
11994 0 : goto error;
11995 431 : if (m == MATCH_YES)
11996 : {
11997 431 : if (seen_ptr)
11998 : {
11999 1 : gfc_error ("Duplicate POINTER attribute at %C");
12000 1 : goto error;
12001 : }
12002 :
12003 430 : seen_ptr = true;
12004 430 : continue;
12005 : }
12006 : }
12007 : else
12008 : {
12009 : /* NON_OVERRIDABLE flag. */
12010 322 : m = gfc_match (" non_overridable");
12011 322 : if (m == MATCH_ERROR)
12012 0 : goto error;
12013 322 : if (m == MATCH_YES)
12014 : {
12015 62 : if (ba->non_overridable)
12016 : {
12017 1 : gfc_error ("Duplicate NON_OVERRIDABLE at %C");
12018 1 : goto error;
12019 : }
12020 :
12021 61 : ba->non_overridable = 1;
12022 61 : continue;
12023 : }
12024 :
12025 : /* DEFERRED flag. */
12026 260 : m = gfc_match (" deferred");
12027 260 : if (m == MATCH_ERROR)
12028 0 : goto error;
12029 260 : if (m == MATCH_YES)
12030 : {
12031 260 : if (ba->deferred)
12032 : {
12033 1 : gfc_error ("Duplicate DEFERRED at %C");
12034 1 : goto error;
12035 : }
12036 :
12037 259 : ba->deferred = 1;
12038 259 : continue;
12039 : }
12040 : }
12041 :
12042 : }
12043 :
12044 : /* Nothing matching found. */
12045 1 : if (generic)
12046 1 : gfc_error ("Expected access-specifier at %C");
12047 : else
12048 0 : gfc_error ("Expected binding attribute at %C");
12049 1 : goto error;
12050 : }
12051 2785 : while (gfc_match_char (',') == MATCH_YES);
12052 :
12053 : /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
12054 2236 : if (ba->non_overridable && ba->deferred)
12055 : {
12056 1 : gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
12057 1 : goto error;
12058 : }
12059 :
12060 : m = MATCH_YES;
12061 :
12062 4705 : done:
12063 4705 : if (ba->access == ACCESS_UNKNOWN)
12064 4276 : ba->access = ppc ? gfc_current_block()->component_access
12065 : : gfc_typebound_default_access;
12066 :
12067 4705 : if (ppc && !seen_ptr)
12068 : {
12069 2 : gfc_error ("POINTER attribute is required for procedure pointer component"
12070 : " at %C");
12071 2 : goto error;
12072 : }
12073 :
12074 : return m;
12075 :
12076 : error:
12077 : return MATCH_ERROR;
12078 : }
12079 :
12080 :
12081 : /* Match a PROCEDURE specific binding inside a derived type. */
12082 :
12083 : static match
12084 3236 : match_procedure_in_type (void)
12085 : {
12086 3236 : char name[GFC_MAX_SYMBOL_LEN + 1];
12087 3236 : char target_buf[GFC_MAX_SYMBOL_LEN + 1];
12088 3236 : char* target = NULL, *ifc = NULL;
12089 3236 : gfc_typebound_proc tb;
12090 3236 : bool seen_colons;
12091 3236 : bool seen_attrs;
12092 3236 : match m;
12093 3236 : gfc_symtree* stree;
12094 3236 : gfc_namespace* ns;
12095 3236 : gfc_symbol* block;
12096 3236 : int num;
12097 :
12098 : /* Check current state. */
12099 3236 : gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
12100 3236 : block = gfc_state_stack->previous->sym;
12101 3236 : gcc_assert (block);
12102 :
12103 : /* Try to match PROCEDURE(interface). */
12104 3236 : if (gfc_match (" (") == MATCH_YES)
12105 : {
12106 261 : m = gfc_match_name (target_buf);
12107 261 : if (m == MATCH_ERROR)
12108 : return m;
12109 261 : if (m != MATCH_YES)
12110 : {
12111 1 : gfc_error ("Interface-name expected after %<(%> at %C");
12112 1 : return MATCH_ERROR;
12113 : }
12114 :
12115 260 : if (gfc_match (" )") != MATCH_YES)
12116 : {
12117 1 : gfc_error ("%<)%> expected at %C");
12118 1 : return MATCH_ERROR;
12119 : }
12120 :
12121 : ifc = target_buf;
12122 : }
12123 :
12124 : /* Construct the data structure. */
12125 3234 : memset (&tb, 0, sizeof (tb));
12126 3234 : tb.where = gfc_current_locus;
12127 :
12128 : /* Match binding attributes. */
12129 3234 : m = match_binding_attributes (&tb, false, false);
12130 3234 : if (m == MATCH_ERROR)
12131 : return m;
12132 3227 : seen_attrs = (m == MATCH_YES);
12133 :
12134 : /* Check that attribute DEFERRED is given if an interface is specified. */
12135 3227 : if (tb.deferred && !ifc)
12136 : {
12137 1 : gfc_error ("Interface must be specified for DEFERRED binding at %C");
12138 1 : return MATCH_ERROR;
12139 : }
12140 3226 : if (ifc && !tb.deferred)
12141 : {
12142 1 : gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
12143 1 : return MATCH_ERROR;
12144 : }
12145 :
12146 : /* Match the colons. */
12147 3225 : m = gfc_match (" ::");
12148 3225 : if (m == MATCH_ERROR)
12149 : return m;
12150 3225 : seen_colons = (m == MATCH_YES);
12151 3225 : if (seen_attrs && !seen_colons)
12152 : {
12153 4 : gfc_error ("Expected %<::%> after binding-attributes at %C");
12154 4 : return MATCH_ERROR;
12155 : }
12156 :
12157 : /* Match the binding names. */
12158 19 : for(num=1;;num++)
12159 : {
12160 3240 : m = gfc_match_name (name);
12161 3240 : if (m == MATCH_ERROR)
12162 : return m;
12163 3240 : if (m == MATCH_NO)
12164 : {
12165 5 : gfc_error ("Expected binding name at %C");
12166 5 : return MATCH_ERROR;
12167 : }
12168 :
12169 3235 : if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
12170 : return MATCH_ERROR;
12171 :
12172 : /* Try to match the '=> target', if it's there. */
12173 3234 : target = ifc;
12174 3234 : m = gfc_match (" =>");
12175 3234 : if (m == MATCH_ERROR)
12176 : return m;
12177 3234 : if (m == MATCH_YES)
12178 : {
12179 1250 : if (tb.deferred)
12180 : {
12181 1 : gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
12182 1 : return MATCH_ERROR;
12183 : }
12184 :
12185 1249 : if (!seen_colons)
12186 : {
12187 1 : gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
12188 : " at %C");
12189 1 : return MATCH_ERROR;
12190 : }
12191 :
12192 1248 : m = gfc_match_name (target_buf);
12193 1248 : if (m == MATCH_ERROR)
12194 : return m;
12195 1248 : if (m == MATCH_NO)
12196 : {
12197 2 : gfc_error ("Expected binding target after %<=>%> at %C");
12198 2 : return MATCH_ERROR;
12199 : }
12200 : target = target_buf;
12201 : }
12202 :
12203 : /* If no target was found, it has the same name as the binding. */
12204 1984 : if (!target)
12205 1729 : target = name;
12206 :
12207 : /* Get the namespace to insert the symbols into. */
12208 3230 : ns = block->f2k_derived;
12209 3230 : gcc_assert (ns);
12210 :
12211 : /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
12212 3230 : if (tb.deferred && !block->attr.abstract)
12213 : {
12214 1 : gfc_error ("Type %qs containing DEFERRED binding at %C "
12215 : "is not ABSTRACT", block->name);
12216 1 : return MATCH_ERROR;
12217 : }
12218 :
12219 : /* See if we already have a binding with this name in the symtree which
12220 : would be an error. If a GENERIC already targeted this binding, it may
12221 : be already there but then typebound is still NULL. */
12222 3229 : stree = gfc_find_symtree (ns->tb_sym_root, name);
12223 3229 : if (stree && stree->n.tb)
12224 : {
12225 2 : gfc_error ("There is already a procedure with binding name %qs for "
12226 : "the derived type %qs at %C", name, block->name);
12227 2 : return MATCH_ERROR;
12228 : }
12229 :
12230 : /* Insert it and set attributes. */
12231 :
12232 3108 : if (!stree)
12233 : {
12234 3108 : stree = gfc_new_symtree (&ns->tb_sym_root, name);
12235 3108 : gcc_assert (stree);
12236 : }
12237 3227 : stree->n.tb = gfc_get_typebound_proc (&tb);
12238 :
12239 3227 : if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
12240 : false))
12241 : return MATCH_ERROR;
12242 3227 : gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
12243 3227 : gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
12244 3227 : target, &stree->n.tb->u.specific->n.sym->declared_at);
12245 :
12246 3227 : if (gfc_match_eos () == MATCH_YES)
12247 : return MATCH_YES;
12248 20 : if (gfc_match_char (',') != MATCH_YES)
12249 1 : goto syntax;
12250 : }
12251 :
12252 1 : syntax:
12253 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
12254 1 : return MATCH_ERROR;
12255 : }
12256 :
12257 :
12258 : /* Match a GENERIC statement.
12259 : F2018 15.4.3.3 GENERIC statement
12260 :
12261 : A GENERIC statement specifies a generic identifier for one or more specific
12262 : procedures, in the same way as a generic interface block that does not contain
12263 : interface bodies.
12264 :
12265 : R1510 generic-stmt is:
12266 : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
12267 :
12268 : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
12269 : procedure that was specified previously in any accessible interface with the
12270 : same generic identifier.
12271 :
12272 : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
12273 :
12274 : For GENERIC statements outside of a derived type, use is made of the existing,
12275 : typebound matching functions to obtain access-spec and generic-spec. After
12276 : this the standard INTERFACE machinery is used. */
12277 :
12278 : static match
12279 100 : match_generic_stmt (void)
12280 : {
12281 100 : char name[GFC_MAX_SYMBOL_LEN + 1];
12282 : /* Allow space for OPERATOR(...). */
12283 100 : char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
12284 : /* Generics other than uops */
12285 100 : gfc_symbol* generic_spec = NULL;
12286 : /* Generic uops */
12287 100 : gfc_user_op *generic_uop = NULL;
12288 : /* For the matching calls */
12289 100 : gfc_typebound_proc tbattr;
12290 100 : gfc_namespace* ns = gfc_current_ns;
12291 100 : interface_type op_type;
12292 100 : gfc_intrinsic_op op;
12293 100 : match m;
12294 100 : gfc_symtree* st;
12295 : /* The specific-procedure-list */
12296 100 : gfc_interface *generic = NULL;
12297 : /* The head of the specific-procedure-list */
12298 100 : gfc_interface **generic_tail = NULL;
12299 :
12300 100 : memset (&tbattr, 0, sizeof (tbattr));
12301 100 : tbattr.where = gfc_current_locus;
12302 :
12303 : /* See if we get an access-specifier. */
12304 100 : m = match_binding_attributes (&tbattr, true, false);
12305 100 : tbattr.where = gfc_current_locus;
12306 100 : if (m == MATCH_ERROR)
12307 0 : goto error;
12308 :
12309 : /* Now the colons, those are required. */
12310 100 : if (gfc_match (" ::") != MATCH_YES)
12311 : {
12312 0 : gfc_error ("Expected %<::%> at %C");
12313 0 : goto error;
12314 : }
12315 :
12316 : /* Match the generic-spec name; depending on type (operator / generic) format
12317 : it for future error messages in 'generic_spec_name'. */
12318 100 : m = gfc_match_generic_spec (&op_type, name, &op);
12319 100 : if (m == MATCH_ERROR)
12320 : return MATCH_ERROR;
12321 100 : if (m == MATCH_NO)
12322 : {
12323 0 : gfc_error ("Expected generic name or operator descriptor at %C");
12324 0 : goto error;
12325 : }
12326 :
12327 100 : switch (op_type)
12328 : {
12329 63 : case INTERFACE_GENERIC:
12330 63 : case INTERFACE_DTIO:
12331 63 : snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
12332 63 : break;
12333 :
12334 22 : case INTERFACE_USER_OP:
12335 22 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
12336 22 : break;
12337 :
12338 13 : case INTERFACE_INTRINSIC_OP:
12339 13 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
12340 : gfc_op2string (op));
12341 13 : break;
12342 :
12343 2 : case INTERFACE_NAMELESS:
12344 2 : gfc_error ("Malformed GENERIC statement at %C");
12345 2 : goto error;
12346 0 : break;
12347 :
12348 0 : default:
12349 0 : gcc_unreachable ();
12350 : }
12351 :
12352 : /* Match the required =>. */
12353 98 : if (gfc_match (" =>") != MATCH_YES)
12354 : {
12355 1 : gfc_error ("Expected %<=>%> at %C");
12356 1 : goto error;
12357 : }
12358 :
12359 :
12360 97 : if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
12361 : {
12362 1 : gfc_error ("The access specification at %L not in a module",
12363 : &tbattr.where);
12364 1 : goto error;
12365 : }
12366 :
12367 : /* Try to find existing generic-spec with this name for this operator;
12368 : if there is something, check that it is another generic-spec and then
12369 : extend it rather than building a new symbol. Otherwise, create a new
12370 : one with the right attributes. */
12371 :
12372 96 : switch (op_type)
12373 : {
12374 61 : case INTERFACE_DTIO:
12375 61 : case INTERFACE_GENERIC:
12376 61 : st = gfc_find_symtree (ns->sym_root, name);
12377 61 : generic_spec = st ? st->n.sym : NULL;
12378 61 : if (generic_spec)
12379 : {
12380 25 : if (generic_spec->attr.flavor != FL_PROCEDURE
12381 11 : && generic_spec->attr.flavor != FL_UNKNOWN)
12382 : {
12383 1 : gfc_error ("The generic-spec name %qs at %C clashes with the "
12384 : "name of an entity declared at %L that is not a "
12385 : "procedure", name, &generic_spec->declared_at);
12386 1 : goto error;
12387 : }
12388 :
12389 24 : if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
12390 10 : && generic_spec->attr.flavor != FL_UNKNOWN)
12391 : {
12392 0 : gfc_error ("There's already a non-generic procedure with "
12393 : "name %qs at %C", generic_spec->name);
12394 0 : goto error;
12395 : }
12396 :
12397 24 : if (tbattr.access != ACCESS_UNKNOWN)
12398 : {
12399 2 : if (generic_spec->attr.access != tbattr.access)
12400 : {
12401 1 : gfc_error ("The access specification at %L conflicts with "
12402 : "that already given to %qs", &tbattr.where,
12403 : generic_spec->name);
12404 1 : goto error;
12405 : }
12406 : else
12407 : {
12408 1 : gfc_error ("The access specification at %L repeats that "
12409 : "already given to %qs", &tbattr.where,
12410 : generic_spec->name);
12411 1 : goto error;
12412 : }
12413 : }
12414 :
12415 22 : if (generic_spec->ts.type != BT_UNKNOWN)
12416 : {
12417 1 : gfc_error ("The generic-spec in the generic statement at %C "
12418 : "has a type from the declaration at %L",
12419 : &generic_spec->declared_at);
12420 1 : goto error;
12421 : }
12422 : }
12423 :
12424 : /* Now create the generic_spec if it doesn't already exist and provide
12425 : is with the appropriate attributes. */
12426 57 : if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
12427 : {
12428 45 : if (!generic_spec)
12429 : {
12430 36 : gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
12431 36 : gfc_set_sym_referenced (generic_spec);
12432 36 : generic_spec->attr.access = tbattr.access;
12433 : }
12434 9 : else if (generic_spec->attr.access == ACCESS_UNKNOWN)
12435 0 : generic_spec->attr.access = tbattr.access;
12436 45 : generic_spec->refs++;
12437 45 : generic_spec->attr.generic = 1;
12438 45 : generic_spec->attr.flavor = FL_PROCEDURE;
12439 :
12440 45 : generic_spec->declared_at = gfc_current_locus;
12441 : }
12442 :
12443 : /* Prepare to add the specific procedures. */
12444 57 : generic = generic_spec->generic;
12445 57 : generic_tail = &generic_spec->generic;
12446 57 : break;
12447 :
12448 22 : case INTERFACE_USER_OP:
12449 22 : st = gfc_find_symtree (ns->uop_root, name);
12450 22 : generic_uop = st ? st->n.uop : NULL;
12451 2 : if (generic_uop)
12452 : {
12453 2 : if (generic_uop->access != ACCESS_UNKNOWN
12454 2 : && tbattr.access != ACCESS_UNKNOWN)
12455 : {
12456 2 : if (generic_uop->access != tbattr.access)
12457 : {
12458 1 : gfc_error ("The user operator at %L must have the same "
12459 : "access specification as already defined user "
12460 : "operator %qs", &tbattr.where, generic_spec_name);
12461 1 : goto error;
12462 : }
12463 : else
12464 : {
12465 1 : gfc_error ("The user operator at %L repeats the access "
12466 : "specification of already defined user operator " "%qs", &tbattr.where, generic_spec_name);
12467 1 : goto error;
12468 : }
12469 : }
12470 0 : else if (generic_uop->access == ACCESS_UNKNOWN)
12471 0 : generic_uop->access = tbattr.access;
12472 : }
12473 : else
12474 : {
12475 20 : generic_uop = gfc_get_uop (name);
12476 20 : generic_uop->access = tbattr.access;
12477 : }
12478 :
12479 : /* Prepare to add the specific procedures. */
12480 20 : generic = generic_uop->op;
12481 20 : generic_tail = &generic_uop->op;
12482 20 : break;
12483 :
12484 13 : case INTERFACE_INTRINSIC_OP:
12485 13 : generic = ns->op[op];
12486 13 : generic_tail = &ns->op[op];
12487 13 : break;
12488 :
12489 0 : default:
12490 0 : gcc_unreachable ();
12491 : }
12492 :
12493 : /* Now, match all following names in the specific-procedure-list. */
12494 154 : do
12495 : {
12496 154 : m = gfc_match_name (name);
12497 154 : if (m == MATCH_ERROR)
12498 0 : goto error;
12499 154 : if (m == MATCH_NO)
12500 : {
12501 0 : gfc_error ("Expected specific procedure name at %C");
12502 0 : goto error;
12503 : }
12504 :
12505 154 : if (op_type == INTERFACE_GENERIC
12506 95 : && !strcmp (generic_spec->name, name))
12507 : {
12508 2 : gfc_error ("The name %qs of the specific procedure at %C conflicts "
12509 : "with that of the generic-spec", name);
12510 2 : goto error;
12511 : }
12512 :
12513 152 : generic = *generic_tail;
12514 242 : for (; generic; generic = generic->next)
12515 : {
12516 90 : if (!strcmp (generic->sym->name, name))
12517 : {
12518 0 : gfc_error ("%qs already defined as a specific procedure for the"
12519 : " generic %qs at %C", name, generic_spec->name);
12520 0 : goto error;
12521 : }
12522 : }
12523 :
12524 152 : gfc_find_sym_tree (name, ns, 1, &st);
12525 152 : if (!st)
12526 : {
12527 : /* This might be a procedure that has not yet been parsed. If
12528 : so gfc_fixup_sibling_symbols will replace this symbol with
12529 : that of the procedure. */
12530 75 : gfc_get_sym_tree (name, ns, &st, false);
12531 75 : st->n.sym->refs++;
12532 : }
12533 :
12534 152 : generic = gfc_get_interface();
12535 152 : generic->next = *generic_tail;
12536 152 : *generic_tail = generic;
12537 152 : generic->where = gfc_current_locus;
12538 152 : generic->sym = st->n.sym;
12539 : }
12540 152 : while (gfc_match (" ,") == MATCH_YES);
12541 :
12542 88 : if (gfc_match_eos () != MATCH_YES)
12543 : {
12544 0 : gfc_error ("Junk after GENERIC statement at %C");
12545 0 : goto error;
12546 : }
12547 :
12548 88 : gfc_commit_symbols ();
12549 88 : return MATCH_YES;
12550 :
12551 : error:
12552 : return MATCH_ERROR;
12553 : }
12554 :
12555 :
12556 : /* Match a GENERIC procedure binding inside a derived type. */
12557 :
12558 : static match
12559 948 : match_typebound_generic (void)
12560 : {
12561 948 : char name[GFC_MAX_SYMBOL_LEN + 1];
12562 948 : char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
12563 948 : gfc_symbol* block;
12564 948 : gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
12565 948 : gfc_typebound_proc* tb;
12566 948 : gfc_namespace* ns;
12567 948 : interface_type op_type;
12568 948 : gfc_intrinsic_op op;
12569 948 : match m;
12570 :
12571 : /* Check current state. */
12572 948 : if (gfc_current_state () == COMP_DERIVED)
12573 : {
12574 0 : gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
12575 0 : return MATCH_ERROR;
12576 : }
12577 948 : if (gfc_current_state () != COMP_DERIVED_CONTAINS)
12578 : return MATCH_NO;
12579 948 : block = gfc_state_stack->previous->sym;
12580 948 : ns = block->f2k_derived;
12581 948 : gcc_assert (block && ns);
12582 :
12583 948 : memset (&tbattr, 0, sizeof (tbattr));
12584 948 : tbattr.where = gfc_current_locus;
12585 :
12586 : /* See if we get an access-specifier. */
12587 948 : m = match_binding_attributes (&tbattr, true, false);
12588 948 : if (m == MATCH_ERROR)
12589 1 : goto error;
12590 :
12591 : /* Now the colons, those are required. */
12592 947 : if (gfc_match (" ::") != MATCH_YES)
12593 : {
12594 0 : gfc_error ("Expected %<::%> at %C");
12595 0 : goto error;
12596 : }
12597 :
12598 : /* Match the binding name; depending on type (operator / generic) format
12599 : it for future error messages into bind_name. */
12600 :
12601 947 : m = gfc_match_generic_spec (&op_type, name, &op);
12602 947 : if (m == MATCH_ERROR)
12603 : return MATCH_ERROR;
12604 947 : if (m == MATCH_NO)
12605 : {
12606 0 : gfc_error ("Expected generic name or operator descriptor at %C");
12607 0 : goto error;
12608 : }
12609 :
12610 947 : switch (op_type)
12611 : {
12612 470 : case INTERFACE_GENERIC:
12613 470 : case INTERFACE_DTIO:
12614 470 : snprintf (bind_name, sizeof (bind_name), "%s", name);
12615 470 : break;
12616 :
12617 47 : case INTERFACE_USER_OP:
12618 47 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
12619 47 : break;
12620 :
12621 429 : case INTERFACE_INTRINSIC_OP:
12622 429 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
12623 : gfc_op2string (op));
12624 429 : break;
12625 :
12626 1 : case INTERFACE_NAMELESS:
12627 1 : gfc_error ("Malformed GENERIC statement at %C");
12628 1 : goto error;
12629 0 : break;
12630 :
12631 0 : default:
12632 0 : gcc_unreachable ();
12633 : }
12634 :
12635 : /* Match the required =>. */
12636 946 : if (gfc_match (" =>") != MATCH_YES)
12637 : {
12638 0 : gfc_error ("Expected %<=>%> at %C");
12639 0 : goto error;
12640 : }
12641 :
12642 : /* Try to find existing GENERIC binding with this name / for this operator;
12643 : if there is something, check that it is another GENERIC and then extend
12644 : it rather than building a new node. Otherwise, create it and put it
12645 : at the right position. */
12646 :
12647 946 : switch (op_type)
12648 : {
12649 517 : case INTERFACE_DTIO:
12650 517 : case INTERFACE_USER_OP:
12651 517 : case INTERFACE_GENERIC:
12652 517 : {
12653 517 : const bool is_op = (op_type == INTERFACE_USER_OP);
12654 517 : gfc_symtree* st;
12655 :
12656 517 : st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
12657 517 : tb = st ? st->n.tb : NULL;
12658 : break;
12659 : }
12660 :
12661 429 : case INTERFACE_INTRINSIC_OP:
12662 429 : tb = ns->tb_op[op];
12663 429 : break;
12664 :
12665 0 : default:
12666 0 : gcc_unreachable ();
12667 : }
12668 :
12669 440 : if (tb)
12670 : {
12671 9 : if (!tb->is_generic)
12672 : {
12673 1 : gcc_assert (op_type == INTERFACE_GENERIC);
12674 1 : gfc_error ("There's already a non-generic procedure with binding name"
12675 : " %qs for the derived type %qs at %C",
12676 : bind_name, block->name);
12677 1 : goto error;
12678 : }
12679 :
12680 8 : if (tb->access != tbattr.access)
12681 : {
12682 2 : gfc_error ("Binding at %C must have the same access as already"
12683 : " defined binding %qs", bind_name);
12684 2 : goto error;
12685 : }
12686 : }
12687 : else
12688 : {
12689 937 : tb = gfc_get_typebound_proc (NULL);
12690 937 : tb->where = gfc_current_locus;
12691 937 : tb->access = tbattr.access;
12692 937 : tb->is_generic = 1;
12693 937 : tb->u.generic = NULL;
12694 :
12695 937 : switch (op_type)
12696 : {
12697 508 : case INTERFACE_DTIO:
12698 508 : case INTERFACE_GENERIC:
12699 508 : case INTERFACE_USER_OP:
12700 508 : {
12701 508 : const bool is_op = (op_type == INTERFACE_USER_OP);
12702 508 : gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
12703 : &ns->tb_sym_root, name);
12704 508 : gcc_assert (st);
12705 508 : st->n.tb = tb;
12706 :
12707 508 : break;
12708 : }
12709 :
12710 429 : case INTERFACE_INTRINSIC_OP:
12711 429 : ns->tb_op[op] = tb;
12712 429 : break;
12713 :
12714 0 : default:
12715 0 : gcc_unreachable ();
12716 : }
12717 : }
12718 :
12719 : /* Now, match all following names as specific targets. */
12720 1100 : do
12721 : {
12722 1100 : gfc_symtree* target_st;
12723 1100 : gfc_tbp_generic* target;
12724 :
12725 1100 : m = gfc_match_name (name);
12726 1100 : if (m == MATCH_ERROR)
12727 0 : goto error;
12728 1100 : if (m == MATCH_NO)
12729 : {
12730 1 : gfc_error ("Expected specific binding name at %C");
12731 1 : goto error;
12732 : }
12733 :
12734 1099 : target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
12735 :
12736 : /* See if this is a duplicate specification. */
12737 1334 : for (target = tb->u.generic; target; target = target->next)
12738 236 : if (target_st == target->specific_st)
12739 : {
12740 1 : gfc_error ("%qs already defined as specific binding for the"
12741 : " generic %qs at %C", name, bind_name);
12742 1 : goto error;
12743 : }
12744 :
12745 1098 : target = gfc_get_tbp_generic ();
12746 1098 : target->specific_st = target_st;
12747 1098 : target->specific = NULL;
12748 1098 : target->next = tb->u.generic;
12749 1098 : target->is_operator = ((op_type == INTERFACE_USER_OP)
12750 1098 : || (op_type == INTERFACE_INTRINSIC_OP));
12751 1098 : tb->u.generic = target;
12752 : }
12753 1098 : while (gfc_match (" ,") == MATCH_YES);
12754 :
12755 : /* Here should be the end. */
12756 941 : if (gfc_match_eos () != MATCH_YES)
12757 : {
12758 1 : gfc_error ("Junk after GENERIC binding at %C");
12759 1 : goto error;
12760 : }
12761 :
12762 : return MATCH_YES;
12763 :
12764 : error:
12765 : return MATCH_ERROR;
12766 : }
12767 :
12768 :
12769 : match
12770 1048 : gfc_match_generic ()
12771 : {
12772 1048 : if (gfc_option.allow_std & ~GFC_STD_OPT_F08
12773 1046 : && gfc_current_state () != COMP_DERIVED_CONTAINS)
12774 100 : return match_generic_stmt ();
12775 : else
12776 948 : return match_typebound_generic ();
12777 : }
12778 :
12779 :
12780 : /* Match a FINAL declaration inside a derived type. */
12781 :
12782 : match
12783 478 : gfc_match_final_decl (void)
12784 : {
12785 478 : char name[GFC_MAX_SYMBOL_LEN + 1];
12786 478 : gfc_symbol* sym;
12787 478 : match m;
12788 478 : gfc_namespace* module_ns;
12789 478 : bool first, last;
12790 478 : gfc_symbol* block;
12791 :
12792 478 : if (gfc_current_form == FORM_FREE)
12793 : {
12794 478 : char c = gfc_peek_ascii_char ();
12795 478 : if (!gfc_is_whitespace (c) && c != ':')
12796 : return MATCH_NO;
12797 : }
12798 :
12799 477 : if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
12800 : {
12801 1 : if (gfc_current_form == FORM_FIXED)
12802 : return MATCH_NO;
12803 :
12804 1 : gfc_error ("FINAL declaration at %C must be inside a derived type "
12805 : "CONTAINS section");
12806 1 : return MATCH_ERROR;
12807 : }
12808 :
12809 476 : block = gfc_state_stack->previous->sym;
12810 476 : gcc_assert (block);
12811 :
12812 476 : if (gfc_state_stack->previous->previous
12813 476 : && gfc_state_stack->previous->previous->state != COMP_MODULE
12814 6 : && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
12815 : {
12816 0 : gfc_error ("Derived type declaration with FINAL at %C must be in the"
12817 : " specification part of a MODULE");
12818 0 : return MATCH_ERROR;
12819 : }
12820 :
12821 476 : module_ns = gfc_current_ns;
12822 476 : gcc_assert (module_ns);
12823 476 : gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
12824 :
12825 : /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
12826 476 : if (gfc_match (" ::") == MATCH_ERROR)
12827 : return MATCH_ERROR;
12828 :
12829 : /* Match the sequence of procedure names. */
12830 : first = true;
12831 : last = false;
12832 568 : do
12833 : {
12834 568 : gfc_finalizer* f;
12835 :
12836 568 : if (first && gfc_match_eos () == MATCH_YES)
12837 : {
12838 2 : gfc_error ("Empty FINAL at %C");
12839 2 : return MATCH_ERROR;
12840 : }
12841 :
12842 566 : m = gfc_match_name (name);
12843 566 : if (m == MATCH_NO)
12844 : {
12845 1 : gfc_error ("Expected module procedure name at %C");
12846 1 : return MATCH_ERROR;
12847 : }
12848 565 : else if (m != MATCH_YES)
12849 : return MATCH_ERROR;
12850 :
12851 565 : if (gfc_match_eos () == MATCH_YES)
12852 : last = true;
12853 93 : if (!last && gfc_match_char (',') != MATCH_YES)
12854 : {
12855 1 : gfc_error ("Expected %<,%> at %C");
12856 1 : return MATCH_ERROR;
12857 : }
12858 :
12859 564 : if (gfc_get_symbol (name, module_ns, &sym))
12860 : {
12861 0 : gfc_error ("Unknown procedure name %qs at %C", name);
12862 0 : return MATCH_ERROR;
12863 : }
12864 :
12865 : /* Mark the symbol as module procedure. */
12866 564 : if (sym->attr.proc != PROC_MODULE
12867 564 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
12868 : return MATCH_ERROR;
12869 :
12870 : /* Check if we already have this symbol in the list, this is an error. */
12871 763 : for (f = block->f2k_derived->finalizers; f; f = f->next)
12872 200 : if (f->proc_sym == sym)
12873 : {
12874 1 : gfc_error ("%qs at %C is already defined as FINAL procedure",
12875 : name);
12876 1 : return MATCH_ERROR;
12877 : }
12878 :
12879 : /* Add this symbol to the list of finalizers. */
12880 563 : gcc_assert (block->f2k_derived);
12881 563 : sym->refs++;
12882 563 : f = XCNEW (gfc_finalizer);
12883 563 : f->proc_sym = sym;
12884 563 : f->proc_tree = NULL;
12885 563 : f->where = gfc_current_locus;
12886 563 : f->next = block->f2k_derived->finalizers;
12887 563 : block->f2k_derived->finalizers = f;
12888 :
12889 563 : first = false;
12890 : }
12891 563 : while (!last);
12892 :
12893 : return MATCH_YES;
12894 : }
12895 :
12896 :
12897 : const ext_attr_t ext_attr_list[] = {
12898 : { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
12899 : { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
12900 : { "cdecl", EXT_ATTR_CDECL, "cdecl" },
12901 : { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
12902 : { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
12903 : { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
12904 : { "deprecated", EXT_ATTR_DEPRECATED, NULL },
12905 : { "noinline", EXT_ATTR_NOINLINE, NULL },
12906 : { "noreturn", EXT_ATTR_NORETURN, NULL },
12907 : { "weak", EXT_ATTR_WEAK, NULL },
12908 : { "inline", EXT_ATTR_INLINE, NULL },
12909 : { "always_inline",EXT_ATTR_ALWAYS_INLINE,NULL },
12910 : { NULL, EXT_ATTR_LAST, NULL }
12911 : };
12912 :
12913 : /* Match a !GCC$ ATTRIBUTES statement of the form:
12914 : !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
12915 : When we come here, we have already matched the !GCC$ ATTRIBUTES string.
12916 :
12917 : TODO: We should support all GCC attributes using the same syntax for
12918 : the attribute list, i.e. the list in C
12919 : __attributes(( attribute-list ))
12920 : matches then
12921 : !GCC$ ATTRIBUTES attribute-list ::
12922 : Cf. c-parser.cc's c_parser_attributes; the data can then directly be
12923 : saved into a TREE.
12924 :
12925 : As there is absolutely no risk of confusion, we should never return
12926 : MATCH_NO. */
12927 : match
12928 2984 : gfc_match_gcc_attributes (void)
12929 : {
12930 2984 : symbol_attribute attr;
12931 2984 : char name[GFC_MAX_SYMBOL_LEN + 1];
12932 2984 : unsigned id;
12933 2984 : gfc_symbol *sym;
12934 2984 : match m;
12935 :
12936 2984 : gfc_clear_attr (&attr);
12937 2988 : for(;;)
12938 : {
12939 2986 : char ch;
12940 :
12941 2986 : if (gfc_match_name (name) != MATCH_YES)
12942 : return MATCH_ERROR;
12943 :
12944 18042 : for (id = 0; id < EXT_ATTR_LAST; id++)
12945 18042 : if (strcmp (name, ext_attr_list[id].name) == 0)
12946 : break;
12947 :
12948 2986 : if (id == EXT_ATTR_LAST)
12949 : {
12950 0 : gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
12951 0 : return MATCH_ERROR;
12952 : }
12953 :
12954 2986 : if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
12955 : return MATCH_ERROR;
12956 :
12957 2986 : gfc_gobble_whitespace ();
12958 2986 : ch = gfc_next_ascii_char ();
12959 2986 : if (ch == ':')
12960 : {
12961 : /* This is the successful exit condition for the loop. */
12962 2984 : if (gfc_next_ascii_char () == ':')
12963 : break;
12964 : }
12965 :
12966 2 : if (ch == ',')
12967 2 : continue;
12968 :
12969 0 : goto syntax;
12970 2 : }
12971 :
12972 2984 : if (gfc_match_eos () == MATCH_YES)
12973 0 : goto syntax;
12974 :
12975 2999 : for(;;)
12976 : {
12977 2999 : m = gfc_match_name (name);
12978 2999 : if (m != MATCH_YES)
12979 : return m;
12980 :
12981 2999 : if (find_special (name, &sym, true))
12982 : return MATCH_ERROR;
12983 :
12984 2999 : sym->attr.ext_attr |= attr.ext_attr;
12985 :
12986 : /* INLINE and ALWAYS_INLINE are incompatible with NOINLINE. In the
12987 : middle-end the DECL_UNINLINABLE flag set by NOINLINE always wins, so
12988 : the inline request would be silently ignored. Warn and drop it. */
12989 2999 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
12990 : {
12991 5 : if (sym->attr.ext_attr & (1 << EXT_ATTR_ALWAYS_INLINE))
12992 : {
12993 2 : gfc_warning (0, "Attribute %<ALWAYS_INLINE%> at %C is "
12994 : "incompatible with %<NOINLINE%> for %qs and will "
12995 : "be ignored", sym->name);
12996 2 : sym->attr.ext_attr &= ~(1 << EXT_ATTR_ALWAYS_INLINE);
12997 : }
12998 5 : if (sym->attr.ext_attr & (1 << EXT_ATTR_INLINE))
12999 : {
13000 2 : gfc_warning (0, "Attribute %<INLINE%> at %C is incompatible "
13001 : "with %<NOINLINE%> for %qs and will be ignored",
13002 : sym->name);
13003 2 : sym->attr.ext_attr &= ~(1 << EXT_ATTR_INLINE);
13004 : }
13005 : }
13006 :
13007 2999 : if (gfc_match_eos () == MATCH_YES)
13008 : break;
13009 :
13010 15 : if (gfc_match_char (',') != MATCH_YES)
13011 0 : goto syntax;
13012 : }
13013 :
13014 : return MATCH_YES;
13015 :
13016 0 : syntax:
13017 0 : gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
13018 0 : return MATCH_ERROR;
13019 : }
13020 :
13021 :
13022 : /* Match a !GCC$ UNROLL statement of the form:
13023 : !GCC$ UNROLL n
13024 :
13025 : The parameter n is the number of times we are supposed to unroll.
13026 :
13027 : When we come here, we have already matched the !GCC$ UNROLL string. */
13028 : match
13029 19 : gfc_match_gcc_unroll (void)
13030 : {
13031 19 : int value;
13032 :
13033 : /* FIXME: use gfc_match_small_literal_int instead, delete small_int */
13034 19 : if (gfc_match_small_int (&value) == MATCH_YES)
13035 : {
13036 19 : if (value < 0 || value > USHRT_MAX)
13037 : {
13038 2 : gfc_error ("%<GCC unroll%> directive requires a"
13039 : " non-negative integral constant"
13040 : " less than or equal to %u at %C",
13041 : USHRT_MAX
13042 : );
13043 2 : return MATCH_ERROR;
13044 : }
13045 17 : if (gfc_match_eos () == MATCH_YES)
13046 : {
13047 17 : directive_unroll = value == 0 ? 1 : value;
13048 17 : return MATCH_YES;
13049 : }
13050 : }
13051 :
13052 0 : gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
13053 0 : return MATCH_ERROR;
13054 : }
13055 :
13056 : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
13057 :
13058 : The parameter b is name of a middle-end built-in.
13059 : FLAGS is optional and must be one of:
13060 : - (inbranch)
13061 : - (notinbranch)
13062 :
13063 : IF('target') is optional and TARGET is a name of a multilib ABI.
13064 :
13065 : When we come here, we have already matched the !GCC$ builtin string. */
13066 :
13067 : match
13068 3451389 : gfc_match_gcc_builtin (void)
13069 : {
13070 3451389 : char builtin[GFC_MAX_SYMBOL_LEN + 1];
13071 3451389 : char target[GFC_MAX_SYMBOL_LEN + 1];
13072 :
13073 3451389 : if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
13074 : return MATCH_ERROR;
13075 :
13076 3451389 : gfc_simd_clause clause = SIMD_NONE;
13077 3451389 : if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
13078 : clause = SIMD_NOTINBRANCH;
13079 21 : else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
13080 15 : clause = SIMD_INBRANCH;
13081 :
13082 3451389 : if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
13083 : {
13084 3451359 : if (strcmp (target, "fastmath") == 0)
13085 : {
13086 0 : if (!fast_math_flags_set_p (&global_options))
13087 : return MATCH_YES;
13088 : }
13089 : else
13090 : {
13091 3451359 : const char *abi = targetm.get_multilib_abi_name ();
13092 3451359 : if (abi == NULL || strcmp (abi, target) != 0)
13093 : return MATCH_YES;
13094 : }
13095 : }
13096 :
13097 1703624 : if (gfc_vectorized_builtins == NULL)
13098 31554 : gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
13099 :
13100 1703624 : char *r = XNEWVEC (char, strlen (builtin) + 32);
13101 1703624 : sprintf (r, "__builtin_%s", builtin);
13102 :
13103 1703624 : bool existed;
13104 1703624 : int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
13105 1703624 : value |= clause;
13106 1703624 : if (existed)
13107 23 : free (r);
13108 :
13109 : return MATCH_YES;
13110 : }
13111 :
13112 : /* Match an !GCC$ IVDEP statement.
13113 : When we come here, we have already matched the !GCC$ IVDEP string. */
13114 :
13115 : match
13116 3 : gfc_match_gcc_ivdep (void)
13117 : {
13118 3 : if (gfc_match_eos () == MATCH_YES)
13119 : {
13120 3 : directive_ivdep = true;
13121 3 : return MATCH_YES;
13122 : }
13123 :
13124 0 : gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
13125 0 : return MATCH_ERROR;
13126 : }
13127 :
13128 : /* Match an !GCC$ VECTOR statement.
13129 : When we come here, we have already matched the !GCC$ VECTOR string. */
13130 :
13131 : match
13132 3 : gfc_match_gcc_vector (void)
13133 : {
13134 3 : if (gfc_match_eos () == MATCH_YES)
13135 : {
13136 3 : directive_vector = true;
13137 3 : directive_novector = false;
13138 3 : return MATCH_YES;
13139 : }
13140 :
13141 0 : gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
13142 0 : return MATCH_ERROR;
13143 : }
13144 :
13145 : /* Match an !GCC$ NOVECTOR statement.
13146 : When we come here, we have already matched the !GCC$ NOVECTOR string. */
13147 :
13148 : match
13149 3 : gfc_match_gcc_novector (void)
13150 : {
13151 3 : if (gfc_match_eos () == MATCH_YES)
13152 : {
13153 3 : directive_novector = true;
13154 3 : directive_vector = false;
13155 3 : return MATCH_YES;
13156 : }
13157 :
13158 0 : gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
13159 0 : return MATCH_ERROR;
13160 : }
|