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_remove_saved_charlen (cl);
130 1 : gfc_current_ns->cl_list = cl->next;
131 1 : gfc_free_expr (cl->length);
132 1 : free (cl);
133 : }
134 :
135 : /* Drop the charlen nodes created while matching a declaration that is about
136 : to be rejected. Callers must clear any surviving owners before using this
137 : helper, so only the statement-local nodes remain on the namespace list. */
138 :
139 : static void
140 3 : discard_pending_charlens (gfc_charlen *saved_cl)
141 : {
142 3 : if (!gfc_current_ns)
143 : return;
144 :
145 14 : while (gfc_current_ns->cl_list != saved_cl)
146 : {
147 11 : gfc_charlen *cl = gfc_current_ns->cl_list;
148 :
149 11 : gcc_assert (cl);
150 11 : gfc_remove_saved_charlen (cl);
151 11 : gfc_current_ns->cl_list = cl->next;
152 11 : gfc_free_expr (cl->length);
153 11 : free (cl);
154 : }
155 : }
156 :
157 : /********************* DATA statement subroutines *********************/
158 :
159 : static bool in_match_data = false;
160 :
161 : bool
162 8548 : gfc_in_match_data (void)
163 : {
164 8548 : return in_match_data;
165 : }
166 :
167 : static void
168 4876 : set_in_match_data (bool set_value)
169 : {
170 4876 : in_match_data = set_value;
171 0 : }
172 :
173 : /* Free a gfc_data_variable structure and everything beneath it. */
174 :
175 : static void
176 5699 : free_variable (gfc_data_variable *p)
177 : {
178 5699 : gfc_data_variable *q;
179 :
180 8806 : for (; p; p = q)
181 : {
182 3107 : q = p->next;
183 3107 : gfc_free_expr (p->expr);
184 3107 : gfc_free_iterator (&p->iter, 0);
185 3107 : free_variable (p->list);
186 3107 : free (p);
187 : }
188 5699 : }
189 :
190 :
191 : /* Free a gfc_data_value structure and everything beneath it. */
192 :
193 : static void
194 2592 : free_value (gfc_data_value *p)
195 : {
196 2592 : gfc_data_value *q;
197 :
198 10922 : for (; p; p = q)
199 : {
200 8330 : q = p->next;
201 8330 : mpz_clear (p->repeat);
202 8330 : gfc_free_expr (p->expr);
203 8330 : free (p);
204 : }
205 2592 : }
206 :
207 :
208 : /* Free a list of gfc_data structures. */
209 :
210 : void
211 559049 : gfc_free_data (gfc_data *p)
212 : {
213 559049 : gfc_data *q;
214 :
215 561641 : for (; p; p = q)
216 : {
217 2592 : q = p->next;
218 2592 : free_variable (p->var);
219 2592 : free_value (p->value);
220 2592 : free (p);
221 : }
222 559049 : }
223 :
224 :
225 : /* Free all data in a namespace. */
226 :
227 : static void
228 41 : gfc_free_data_all (gfc_namespace *ns)
229 : {
230 41 : gfc_data *d;
231 :
232 47 : for (;ns->data;)
233 : {
234 6 : d = ns->data->next;
235 6 : free (ns->data);
236 6 : ns->data = d;
237 : }
238 41 : }
239 :
240 : /* Reject data parsed since the last restore point was marked. */
241 :
242 : void
243 9385740 : gfc_reject_data (gfc_namespace *ns)
244 : {
245 9385740 : gfc_data *d;
246 :
247 9385742 : while (ns->data && ns->data != ns->old_data)
248 : {
249 2 : d = ns->data->next;
250 2 : free (ns->data);
251 2 : ns->data = d;
252 : }
253 9385740 : }
254 :
255 : static match var_element (gfc_data_variable *);
256 :
257 : /* Match a list of variables terminated by an iterator and a right
258 : parenthesis. */
259 :
260 : static match
261 154 : var_list (gfc_data_variable *parent)
262 : {
263 154 : gfc_data_variable *tail, var;
264 154 : match m;
265 :
266 154 : m = var_element (&var);
267 154 : if (m == MATCH_ERROR)
268 : return MATCH_ERROR;
269 154 : if (m == MATCH_NO)
270 0 : goto syntax;
271 :
272 154 : tail = gfc_get_data_variable ();
273 154 : *tail = var;
274 :
275 154 : parent->list = tail;
276 :
277 156 : for (;;)
278 : {
279 155 : if (gfc_match_char (',') != MATCH_YES)
280 0 : goto syntax;
281 :
282 155 : m = gfc_match_iterator (&parent->iter, 1);
283 155 : if (m == MATCH_YES)
284 : break;
285 1 : if (m == MATCH_ERROR)
286 : return MATCH_ERROR;
287 :
288 1 : m = var_element (&var);
289 1 : if (m == MATCH_ERROR)
290 : return MATCH_ERROR;
291 1 : if (m == MATCH_NO)
292 0 : goto syntax;
293 :
294 1 : tail->next = gfc_get_data_variable ();
295 1 : tail = tail->next;
296 :
297 1 : *tail = var;
298 : }
299 :
300 154 : if (gfc_match_char (')') != MATCH_YES)
301 0 : goto syntax;
302 : return MATCH_YES;
303 :
304 0 : syntax:
305 0 : gfc_syntax_error (ST_DATA);
306 0 : return MATCH_ERROR;
307 : }
308 :
309 :
310 : /* Match a single element in a data variable list, which can be a
311 : variable-iterator list. */
312 :
313 : static match
314 3065 : var_element (gfc_data_variable *new_var)
315 : {
316 3065 : match m;
317 3065 : gfc_symbol *sym;
318 :
319 3065 : memset (new_var, 0, sizeof (gfc_data_variable));
320 :
321 3065 : if (gfc_match_char ('(') == MATCH_YES)
322 154 : return var_list (new_var);
323 :
324 2911 : m = gfc_match_variable (&new_var->expr, 0);
325 2911 : if (m != MATCH_YES)
326 : return m;
327 :
328 2907 : if (new_var->expr->expr_type == EXPR_CONSTANT
329 2 : && new_var->expr->symtree == NULL)
330 : {
331 2 : gfc_error ("Inquiry parameter cannot appear in a "
332 : "data-stmt-object-list at %C");
333 2 : return MATCH_ERROR;
334 : }
335 :
336 2905 : sym = new_var->expr->symtree->n.sym;
337 :
338 : /* Symbol should already have an associated type. */
339 2905 : if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
340 : return MATCH_ERROR;
341 :
342 2904 : if (!sym->attr.function && gfc_current_ns->parent
343 148 : && gfc_current_ns->parent == sym->ns)
344 : {
345 1 : gfc_error ("Host associated variable %qs may not be in the DATA "
346 : "statement at %C", sym->name);
347 1 : return MATCH_ERROR;
348 : }
349 :
350 2903 : if (gfc_current_state () != COMP_BLOCK_DATA
351 2750 : && sym->attr.in_common
352 2932 : && !gfc_notify_std (GFC_STD_GNU, "initialization of "
353 : "common block variable %qs in DATA statement at %C",
354 : sym->name))
355 : return MATCH_ERROR;
356 :
357 2901 : if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
358 5 : return MATCH_ERROR;
359 :
360 : return MATCH_YES;
361 : }
362 :
363 :
364 : /* Match the top-level list of data variables. */
365 :
366 : static match
367 2535 : top_var_list (gfc_data *d)
368 : {
369 2535 : gfc_data_variable var, *tail, *new_var;
370 2535 : match m;
371 :
372 2535 : tail = NULL;
373 :
374 2910 : for (;;)
375 : {
376 2910 : m = var_element (&var);
377 2910 : if (m == MATCH_NO)
378 0 : goto syntax;
379 2910 : if (m == MATCH_ERROR)
380 : return MATCH_ERROR;
381 :
382 2895 : new_var = gfc_get_data_variable ();
383 2895 : *new_var = var;
384 2895 : if (new_var->expr)
385 2769 : new_var->expr->where = gfc_current_locus;
386 :
387 2895 : if (tail == NULL)
388 2520 : d->var = new_var;
389 : else
390 375 : tail->next = new_var;
391 :
392 2895 : tail = new_var;
393 :
394 2895 : if (gfc_match_char ('/') == MATCH_YES)
395 : break;
396 378 : if (gfc_match_char (',') != MATCH_YES)
397 3 : goto syntax;
398 : }
399 :
400 : return MATCH_YES;
401 :
402 3 : syntax:
403 3 : gfc_syntax_error (ST_DATA);
404 3 : gfc_free_data_all (gfc_current_ns);
405 3 : return MATCH_ERROR;
406 : }
407 :
408 :
409 : static match
410 8731 : match_data_constant (gfc_expr **result)
411 : {
412 8731 : char name[GFC_MAX_SYMBOL_LEN + 1];
413 8731 : gfc_symbol *sym, *dt_sym = NULL;
414 8731 : gfc_expr *expr;
415 8731 : match m;
416 8731 : locus old_loc;
417 8731 : gfc_symtree *symtree;
418 :
419 8731 : m = gfc_match_literal_constant (&expr, 1);
420 8731 : if (m == MATCH_YES)
421 : {
422 8368 : *result = expr;
423 8368 : return MATCH_YES;
424 : }
425 :
426 363 : if (m == MATCH_ERROR)
427 : return MATCH_ERROR;
428 :
429 355 : m = gfc_match_null (result);
430 355 : if (m != MATCH_NO)
431 : return m;
432 :
433 347 : old_loc = gfc_current_locus;
434 :
435 : /* Should this be a structure component, try to match it
436 : before matching a name. */
437 347 : m = gfc_match_rvalue (result);
438 347 : if (m == MATCH_ERROR)
439 : return m;
440 :
441 347 : if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
442 : {
443 4 : if (!gfc_simplify_expr (*result, 0))
444 0 : m = MATCH_ERROR;
445 : return m;
446 : }
447 337 : else if (m == MATCH_YES)
448 : {
449 : /* If a parameter inquiry ends up here, symtree is NULL but **result
450 : contains the right constant expression. Check here. */
451 337 : if ((*result)->symtree == NULL
452 37 : && (*result)->expr_type == EXPR_CONSTANT
453 37 : && ((*result)->ts.type == BT_INTEGER
454 1 : || (*result)->ts.type == BT_REAL))
455 : return m;
456 :
457 : /* F2018:R845 data-stmt-constant is initial-data-target.
458 : A data-stmt-constant shall be ... initial-data-target if and
459 : only if the corresponding data-stmt-object has the POINTER
460 : attribute. ... If data-stmt-constant is initial-data-target
461 : the corresponding data statement object shall be
462 : data-pointer-initialization compatible (7.5.4.6) with the initial
463 : data target; the data statement object is initially associated
464 : with the target. */
465 301 : if ((*result)->symtree
466 300 : && (*result)->symtree->n.sym->attr.save
467 236 : && (*result)->symtree->n.sym->attr.target)
468 : return m;
469 250 : gfc_free_expr (*result);
470 : }
471 :
472 256 : gfc_current_locus = old_loc;
473 :
474 256 : m = gfc_match_name (name);
475 256 : if (m != MATCH_YES)
476 : return m;
477 :
478 250 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
479 : return MATCH_ERROR;
480 :
481 250 : sym = symtree->n.sym;
482 :
483 250 : if (sym && sym->attr.generic)
484 60 : dt_sym = gfc_find_dt_in_generic (sym);
485 :
486 60 : if (sym == NULL
487 250 : || (sym->attr.flavor != FL_PARAMETER
488 65 : && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
489 : {
490 5 : gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
491 : name);
492 5 : *result = NULL;
493 5 : return MATCH_ERROR;
494 : }
495 245 : else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
496 60 : return gfc_match_structure_constructor (dt_sym, symtree, result);
497 :
498 : /* Check to see if the value is an initialization array expression. */
499 185 : if (sym->value->expr_type == EXPR_ARRAY)
500 : {
501 67 : gfc_current_locus = old_loc;
502 :
503 67 : m = gfc_match_init_expr (result);
504 67 : if (m == MATCH_ERROR)
505 : return m;
506 :
507 66 : if (m == MATCH_YES)
508 : {
509 66 : if (!gfc_simplify_expr (*result, 0))
510 0 : m = MATCH_ERROR;
511 :
512 66 : if ((*result)->expr_type == EXPR_CONSTANT)
513 : return m;
514 : else
515 : {
516 2 : gfc_error ("Invalid initializer %s in Data statement at %C", name);
517 2 : return MATCH_ERROR;
518 : }
519 : }
520 : }
521 :
522 118 : *result = gfc_copy_expr (sym->value);
523 118 : return MATCH_YES;
524 : }
525 :
526 :
527 : /* Match a list of values in a DATA statement. The leading '/' has
528 : already been seen at this point. */
529 :
530 : static match
531 2578 : top_val_list (gfc_data *data)
532 : {
533 2578 : gfc_data_value *new_val, *tail;
534 2578 : gfc_expr *expr;
535 2578 : match m;
536 :
537 2578 : tail = NULL;
538 :
539 8367 : for (;;)
540 : {
541 8367 : m = match_data_constant (&expr);
542 8367 : if (m == MATCH_NO)
543 3 : goto syntax;
544 8364 : if (m == MATCH_ERROR)
545 : return MATCH_ERROR;
546 :
547 8342 : new_val = gfc_get_data_value ();
548 8342 : mpz_init (new_val->repeat);
549 :
550 8342 : if (tail == NULL)
551 2553 : data->value = new_val;
552 : else
553 5789 : tail->next = new_val;
554 :
555 8342 : tail = new_val;
556 :
557 8342 : if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
558 : {
559 8137 : tail->expr = expr;
560 8137 : mpz_set_ui (tail->repeat, 1);
561 : }
562 : else
563 : {
564 205 : mpz_set (tail->repeat, expr->value.integer);
565 205 : gfc_free_expr (expr);
566 :
567 205 : m = match_data_constant (&tail->expr);
568 205 : if (m == MATCH_NO)
569 0 : goto syntax;
570 205 : if (m == MATCH_ERROR)
571 : return MATCH_ERROR;
572 : }
573 :
574 8338 : if (gfc_match_char ('/') == MATCH_YES)
575 : break;
576 5790 : if (gfc_match_char (',') == MATCH_NO)
577 1 : goto syntax;
578 : }
579 :
580 : return MATCH_YES;
581 :
582 4 : syntax:
583 4 : gfc_syntax_error (ST_DATA);
584 4 : gfc_free_data_all (gfc_current_ns);
585 4 : return MATCH_ERROR;
586 : }
587 :
588 :
589 : /* Matches an old style initialization. */
590 :
591 : static match
592 70 : match_old_style_init (const char *name)
593 : {
594 70 : match m;
595 70 : gfc_symtree *st;
596 70 : gfc_symbol *sym;
597 70 : gfc_data *newdata, *nd;
598 :
599 : /* Set up data structure to hold initializers. */
600 70 : gfc_find_sym_tree (name, NULL, 0, &st);
601 70 : sym = st->n.sym;
602 :
603 70 : newdata = gfc_get_data ();
604 70 : newdata->var = gfc_get_data_variable ();
605 70 : newdata->var->expr = gfc_get_variable_expr (st);
606 70 : newdata->var->expr->where = sym->declared_at;
607 70 : newdata->where = gfc_current_locus;
608 :
609 : /* Match initial value list. This also eats the terminal '/'. */
610 70 : m = top_val_list (newdata);
611 70 : if (m != MATCH_YES)
612 : {
613 1 : free (newdata);
614 1 : return m;
615 : }
616 :
617 : /* Check that a BOZ did not creep into an old-style initialization. */
618 137 : for (nd = newdata; nd; nd = nd->next)
619 : {
620 69 : if (nd->value->expr->ts.type == BT_BOZ
621 69 : && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
622 : "initialization"), &nd->value->expr->where))
623 : return MATCH_ERROR;
624 :
625 68 : if (nd->var->expr->ts.type != BT_INTEGER
626 27 : && nd->var->expr->ts.type != BT_REAL
627 21 : && nd->value->expr->ts.type == BT_BOZ)
628 : {
629 0 : gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
630 : "a %qs variable in an old-style initialization"),
631 0 : &nd->value->expr->where,
632 : gfc_typename (&nd->value->expr->ts));
633 0 : return MATCH_ERROR;
634 : }
635 : }
636 :
637 68 : if (gfc_pure (NULL))
638 : {
639 1 : gfc_error ("Initialization at %C is not allowed in a PURE procedure");
640 1 : free (newdata);
641 1 : return MATCH_ERROR;
642 : }
643 67 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
644 :
645 : /* Mark the variable as having appeared in a data statement. */
646 67 : if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
647 : {
648 2 : free (newdata);
649 2 : return MATCH_ERROR;
650 : }
651 :
652 : /* Chain in namespace list of DATA initializers. */
653 65 : newdata->next = gfc_current_ns->data;
654 65 : gfc_current_ns->data = newdata;
655 :
656 65 : return m;
657 : }
658 :
659 :
660 : /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
661 : we are matching a DATA statement and are therefore issuing an error
662 : if we encounter something unexpected, if not, we're trying to match
663 : an old-style initialization expression of the form INTEGER I /2/. */
664 :
665 : match
666 2440 : gfc_match_data (void)
667 : {
668 2440 : gfc_data *new_data;
669 2440 : gfc_expr *e;
670 2440 : gfc_ref *ref;
671 2440 : match m;
672 2440 : char c;
673 :
674 : /* DATA has been matched. In free form source code, the next character
675 : needs to be whitespace or '(' from an implied do-loop. Check that
676 : here. */
677 2440 : c = gfc_peek_ascii_char ();
678 2440 : if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
679 : return MATCH_NO;
680 :
681 : /* Before parsing the rest of a DATA statement, check F2008:c1206. */
682 2439 : if ((gfc_current_state () == COMP_FUNCTION
683 2439 : || gfc_current_state () == COMP_SUBROUTINE)
684 1153 : && gfc_state_stack->previous->state == COMP_INTERFACE)
685 : {
686 1 : gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
687 1 : return MATCH_ERROR;
688 : }
689 :
690 2438 : set_in_match_data (true);
691 :
692 2632 : for (;;)
693 : {
694 2535 : new_data = gfc_get_data ();
695 2535 : new_data->where = gfc_current_locus;
696 :
697 2535 : m = top_var_list (new_data);
698 2535 : if (m != MATCH_YES)
699 18 : goto cleanup;
700 :
701 2517 : if (new_data->var->iter.var
702 117 : && new_data->var->iter.var->ts.type == BT_INTEGER
703 74 : && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
704 68 : && new_data->var->list
705 68 : && new_data->var->list->expr
706 55 : && new_data->var->list->expr->ts.type == BT_CHARACTER
707 3 : && new_data->var->list->expr->ref
708 3 : && new_data->var->list->expr->ref->type == REF_SUBSTRING)
709 : {
710 1 : gfc_error ("Invalid substring in data-implied-do at %L in DATA "
711 : "statement", &new_data->var->list->expr->where);
712 1 : goto cleanup;
713 : }
714 :
715 : /* Check for an entity with an allocatable component, which is not
716 : allowed. */
717 2516 : e = new_data->var->expr;
718 2516 : if (e)
719 : {
720 2400 : bool invalid;
721 :
722 2400 : invalid = false;
723 3624 : for (ref = e->ref; ref; ref = ref->next)
724 1224 : if ((ref->type == REF_COMPONENT
725 140 : && ref->u.c.component->attr.allocatable)
726 1222 : || (ref->type == REF_ARRAY
727 1034 : && e->symtree->n.sym->attr.pointer != 1
728 1031 : && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
729 1224 : invalid = true;
730 :
731 2400 : if (invalid)
732 : {
733 2 : gfc_error ("Allocatable component or deferred-shaped array "
734 : "near %C in DATA statement");
735 2 : goto cleanup;
736 : }
737 :
738 : /* F2008:C567 (R536) A data-i-do-object or a variable that appears
739 : as a data-stmt-object shall not be an object designator in which
740 : a pointer appears other than as the entire rightmost part-ref. */
741 2398 : if (!e->ref && e->ts.type == BT_DERIVED
742 43 : && e->symtree->n.sym->attr.pointer)
743 4 : goto partref;
744 :
745 2394 : ref = e->ref;
746 2394 : if (e->symtree->n.sym->ts.type == BT_DERIVED
747 125 : && e->symtree->n.sym->attr.pointer
748 1 : && ref->type == REF_COMPONENT)
749 1 : goto partref;
750 :
751 3609 : for (; ref; ref = ref->next)
752 1217 : if (ref->type == REF_COMPONENT
753 135 : && ref->u.c.component->attr.pointer
754 27 : && ref->next)
755 1 : goto partref;
756 : }
757 :
758 2508 : m = top_val_list (new_data);
759 2508 : if (m != MATCH_YES)
760 29 : goto cleanup;
761 :
762 2479 : new_data->next = gfc_current_ns->data;
763 2479 : gfc_current_ns->data = new_data;
764 :
765 : /* A BOZ literal constant cannot appear in a structure constructor.
766 : Check for that here for a data statement value. */
767 2479 : if (new_data->value->expr->ts.type == BT_DERIVED
768 37 : && new_data->value->expr->value.constructor)
769 : {
770 35 : gfc_constructor *c;
771 35 : c = gfc_constructor_first (new_data->value->expr->value.constructor);
772 106 : for (; c; c = gfc_constructor_next (c))
773 36 : if (c->expr && c->expr->ts.type == BT_BOZ)
774 : {
775 0 : gfc_error ("BOZ literal constant at %L cannot appear in a "
776 : "structure constructor", &c->expr->where);
777 0 : return MATCH_ERROR;
778 : }
779 : }
780 :
781 2479 : if (gfc_match_eos () == MATCH_YES)
782 : break;
783 :
784 97 : gfc_match_char (','); /* Optional comma */
785 97 : }
786 :
787 2382 : set_in_match_data (false);
788 :
789 2382 : if (gfc_pure (NULL))
790 : {
791 0 : gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
792 0 : return MATCH_ERROR;
793 : }
794 2382 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
795 :
796 2382 : return MATCH_YES;
797 :
798 6 : partref:
799 :
800 6 : gfc_error ("part-ref with pointer attribute near %L is not "
801 : "rightmost part-ref of data-stmt-object",
802 : &e->where);
803 :
804 56 : cleanup:
805 56 : set_in_match_data (false);
806 56 : gfc_free_data (new_data);
807 56 : return MATCH_ERROR;
808 : }
809 :
810 :
811 : /************************ Declaration statements *********************/
812 :
813 :
814 : /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
815 : list). The difference here is the expression is a list of constants
816 : and is surrounded by '/'.
817 : The typespec ts must match the typespec of the variable which the
818 : clist is initializing.
819 : The arrayspec tells whether this should match a list of constants
820 : corresponding to array elements or a scalar (as == NULL). */
821 :
822 : static match
823 74 : match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
824 : {
825 74 : gfc_constructor_base array_head = NULL;
826 74 : gfc_expr *expr = NULL;
827 74 : match m = MATCH_ERROR;
828 74 : locus where;
829 74 : mpz_t repeat, cons_size, as_size;
830 74 : bool scalar;
831 74 : int cmp;
832 :
833 74 : gcc_assert (ts);
834 :
835 : /* We have already matched '/' - now look for a constant list, as with
836 : top_val_list from decl.cc, but append the result to an array. */
837 74 : if (gfc_match ("/") == MATCH_YES)
838 : {
839 1 : gfc_error ("Empty old style initializer list at %C");
840 1 : return MATCH_ERROR;
841 : }
842 :
843 73 : where = gfc_current_locus;
844 73 : scalar = !as || !as->rank;
845 :
846 42 : if (!scalar && !spec_size (as, &as_size))
847 : {
848 2 : gfc_error ("Array in initializer list at %L must have an explicit shape",
849 1 : as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
850 : /* Nothing to cleanup yet. */
851 1 : return MATCH_ERROR;
852 : }
853 :
854 72 : mpz_init_set_ui (repeat, 0);
855 :
856 143 : for (;;)
857 : {
858 143 : m = match_data_constant (&expr);
859 143 : if (m != MATCH_YES)
860 3 : expr = NULL; /* match_data_constant may set expr to garbage */
861 3 : if (m == MATCH_NO)
862 2 : goto syntax;
863 141 : if (m == MATCH_ERROR)
864 1 : goto cleanup;
865 :
866 : /* Found r in repeat spec r*c; look for the constant to repeat. */
867 140 : if ( gfc_match_char ('*') == MATCH_YES)
868 : {
869 18 : if (scalar)
870 : {
871 1 : gfc_error ("Repeat spec invalid in scalar initializer at %C");
872 1 : goto cleanup;
873 : }
874 17 : if (expr->ts.type != BT_INTEGER)
875 : {
876 1 : gfc_error ("Repeat spec must be an integer at %C");
877 1 : goto cleanup;
878 : }
879 16 : mpz_set (repeat, expr->value.integer);
880 16 : gfc_free_expr (expr);
881 16 : expr = NULL;
882 :
883 16 : m = match_data_constant (&expr);
884 16 : if (m == MATCH_NO)
885 : {
886 1 : m = MATCH_ERROR;
887 1 : gfc_error ("Expected data constant after repeat spec at %C");
888 : }
889 16 : if (m != MATCH_YES)
890 1 : goto cleanup;
891 : }
892 : /* No repeat spec, we matched the data constant itself. */
893 : else
894 122 : mpz_set_ui (repeat, 1);
895 :
896 137 : if (!scalar)
897 : {
898 : /* Add the constant initializer as many times as repeated. */
899 251 : for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
900 : {
901 : /* Make sure types of elements match */
902 144 : if(ts && !gfc_compare_types (&expr->ts, ts)
903 12 : && !gfc_convert_type (expr, ts, 1))
904 0 : goto cleanup;
905 :
906 144 : gfc_constructor_append_expr (&array_head,
907 : gfc_copy_expr (expr), &gfc_current_locus);
908 : }
909 :
910 107 : gfc_free_expr (expr);
911 107 : expr = NULL;
912 : }
913 :
914 : /* For scalar initializers quit after one element. */
915 : else
916 : {
917 30 : if(gfc_match_char ('/') != MATCH_YES)
918 : {
919 1 : gfc_error ("End of scalar initializer expected at %C");
920 1 : goto cleanup;
921 : }
922 : break;
923 : }
924 :
925 107 : if (gfc_match_char ('/') == MATCH_YES)
926 : break;
927 72 : if (gfc_match_char (',') == MATCH_NO)
928 1 : goto syntax;
929 : }
930 :
931 : /* If we break early from here out, we encountered an error. */
932 64 : m = MATCH_ERROR;
933 :
934 : /* Set up expr as an array constructor. */
935 64 : if (!scalar)
936 : {
937 35 : expr = gfc_get_array_expr (ts->type, ts->kind, &where);
938 35 : expr->ts = *ts;
939 35 : expr->value.constructor = array_head;
940 :
941 : /* Validate sizes. We built expr ourselves, so cons_size will be
942 : constant (we fail above for non-constant expressions).
943 : We still need to verify that the sizes match. */
944 35 : gcc_assert (gfc_array_size (expr, &cons_size));
945 35 : cmp = mpz_cmp (cons_size, as_size);
946 35 : if (cmp < 0)
947 2 : gfc_error ("Not enough elements in array initializer at %C");
948 33 : else if (cmp > 0)
949 3 : gfc_error ("Too many elements in array initializer at %C");
950 35 : mpz_clear (cons_size);
951 35 : if (cmp)
952 5 : goto cleanup;
953 :
954 : /* Set the rank/shape to match the LHS as auto-reshape is implied. */
955 30 : expr->rank = as->rank;
956 30 : expr->corank = as->corank;
957 30 : expr->shape = gfc_get_shape (as->rank);
958 66 : for (int i = 0; i < as->rank; ++i)
959 36 : spec_dimen_size (as, i, &expr->shape[i]);
960 : }
961 :
962 : /* Make sure scalar types match. */
963 29 : else if (!gfc_compare_types (&expr->ts, ts)
964 29 : && !gfc_convert_type (expr, ts, 1))
965 2 : goto cleanup;
966 :
967 57 : if (expr->ts.u.cl)
968 1 : expr->ts.u.cl->length_from_typespec = 1;
969 :
970 57 : *result = expr;
971 57 : m = MATCH_YES;
972 57 : goto done;
973 :
974 3 : syntax:
975 3 : m = MATCH_ERROR;
976 3 : gfc_error ("Syntax error in old style initializer list at %C");
977 :
978 15 : cleanup:
979 15 : if (expr)
980 10 : expr->value.constructor = NULL;
981 15 : gfc_free_expr (expr);
982 15 : gfc_constructor_free (array_head);
983 :
984 72 : done:
985 72 : mpz_clear (repeat);
986 72 : if (!scalar)
987 41 : mpz_clear (as_size);
988 : return m;
989 : }
990 :
991 :
992 : /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
993 :
994 : static bool
995 114 : merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
996 : {
997 114 : if ((from->type == AS_ASSUMED_RANK && to->corank)
998 112 : || (to->type == AS_ASSUMED_RANK && from->corank))
999 : {
1000 5 : gfc_error ("The assumed-rank array at %C shall not have a codimension");
1001 5 : return false;
1002 : }
1003 :
1004 109 : if (to->rank == 0 && from->rank > 0)
1005 : {
1006 48 : to->rank = from->rank;
1007 48 : to->type = from->type;
1008 48 : to->cray_pointee = from->cray_pointee;
1009 48 : to->cp_was_assumed = from->cp_was_assumed;
1010 :
1011 152 : for (int i = to->corank - 1; i >= 0; i--)
1012 : {
1013 : /* Do not exceed the limits on lower[] and upper[]. gfortran
1014 : cleans up elsewhere. */
1015 104 : int j = from->rank + i;
1016 104 : if (j >= GFC_MAX_DIMENSIONS)
1017 : break;
1018 :
1019 104 : to->lower[j] = to->lower[i];
1020 104 : to->upper[j] = to->upper[i];
1021 : }
1022 115 : for (int i = 0; i < from->rank; i++)
1023 : {
1024 67 : if (copy)
1025 : {
1026 43 : to->lower[i] = gfc_copy_expr (from->lower[i]);
1027 43 : to->upper[i] = gfc_copy_expr (from->upper[i]);
1028 : }
1029 : else
1030 : {
1031 24 : to->lower[i] = from->lower[i];
1032 24 : to->upper[i] = from->upper[i];
1033 : }
1034 : }
1035 : }
1036 61 : else if (to->corank == 0 && from->corank > 0)
1037 : {
1038 34 : to->corank = from->corank;
1039 34 : to->cotype = from->cotype;
1040 :
1041 104 : for (int i = 0; i < from->corank; i++)
1042 : {
1043 : /* Do not exceed the limits on lower[] and upper[]. gfortran
1044 : cleans up elsewhere. */
1045 71 : int k = from->rank + i;
1046 71 : int j = to->rank + i;
1047 71 : if (j >= GFC_MAX_DIMENSIONS)
1048 : break;
1049 :
1050 70 : if (copy)
1051 : {
1052 37 : to->lower[j] = gfc_copy_expr (from->lower[k]);
1053 37 : to->upper[j] = gfc_copy_expr (from->upper[k]);
1054 : }
1055 : else
1056 : {
1057 33 : to->lower[j] = from->lower[k];
1058 33 : to->upper[j] = from->upper[k];
1059 : }
1060 : }
1061 : }
1062 :
1063 109 : if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
1064 : {
1065 1 : gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1066 : "allowed dimensions of %d",
1067 : to->rank, to->corank, GFC_MAX_DIMENSIONS);
1068 1 : to->corank = GFC_MAX_DIMENSIONS - to->rank;
1069 1 : return false;
1070 : }
1071 : return true;
1072 : }
1073 :
1074 :
1075 : /* Match an intent specification. Since this can only happen after an
1076 : INTENT word, a legal intent-spec must follow. */
1077 :
1078 : static sym_intent
1079 29279 : match_intent_spec (void)
1080 : {
1081 :
1082 29279 : if (gfc_match (" ( in out )") == MATCH_YES)
1083 : return INTENT_INOUT;
1084 26026 : if (gfc_match (" ( in )") == MATCH_YES)
1085 : return INTENT_IN;
1086 3838 : if (gfc_match (" ( out )") == MATCH_YES)
1087 : return INTENT_OUT;
1088 :
1089 2 : gfc_error ("Bad INTENT specification at %C");
1090 2 : return INTENT_UNKNOWN;
1091 : }
1092 :
1093 :
1094 : /* Matches a character length specification, which is either a
1095 : specification expression, '*', or ':'. */
1096 :
1097 : static match
1098 28571 : char_len_param_value (gfc_expr **expr, bool *deferred)
1099 : {
1100 28571 : match m;
1101 28571 : gfc_expr *p;
1102 :
1103 28571 : *expr = NULL;
1104 28571 : *deferred = false;
1105 :
1106 28571 : if (gfc_match_char ('*') == MATCH_YES)
1107 : return MATCH_YES;
1108 :
1109 21949 : if (gfc_match_char (':') == MATCH_YES)
1110 : {
1111 3450 : if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1112 : return MATCH_ERROR;
1113 :
1114 3448 : *deferred = true;
1115 :
1116 3448 : return MATCH_YES;
1117 : }
1118 :
1119 18499 : m = gfc_match_expr (expr);
1120 :
1121 18499 : if (m == MATCH_NO || m == MATCH_ERROR)
1122 : return m;
1123 :
1124 18494 : if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1125 : return MATCH_ERROR;
1126 :
1127 : /* Try to simplify the expression to catch things like CHARACTER(([1])). */
1128 18488 : p = gfc_copy_expr (*expr);
1129 18488 : if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
1130 15318 : gfc_replace_expr (*expr, p);
1131 : else
1132 3170 : gfc_free_expr (p);
1133 :
1134 18488 : if ((*expr)->expr_type == EXPR_FUNCTION)
1135 : {
1136 1021 : if ((*expr)->ts.type == BT_INTEGER
1137 1020 : || ((*expr)->ts.type == BT_UNKNOWN
1138 1020 : && strcmp((*expr)->symtree->name, "null") != 0))
1139 : return MATCH_YES;
1140 :
1141 2 : goto syntax;
1142 : }
1143 17467 : else if ((*expr)->expr_type == EXPR_CONSTANT)
1144 : {
1145 : /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1146 : processor dependent and its value is greater than or equal to zero.
1147 : F2008, 4.4.3.2: If the character length parameter value evaluates
1148 : to a negative value, the length of character entities declared
1149 : is zero. */
1150 :
1151 15210 : if ((*expr)->ts.type == BT_INTEGER)
1152 : {
1153 15192 : if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1154 4 : mpz_set_si ((*expr)->value.integer, 0);
1155 : }
1156 : else
1157 18 : goto syntax;
1158 : }
1159 2257 : else if ((*expr)->expr_type == EXPR_ARRAY)
1160 8 : goto syntax;
1161 2249 : else if ((*expr)->expr_type == EXPR_VARIABLE)
1162 : {
1163 1588 : bool t;
1164 1588 : gfc_expr *e;
1165 :
1166 1588 : e = gfc_copy_expr (*expr);
1167 :
1168 : /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1169 : which causes an ICE if gfc_reduce_init_expr() is called. */
1170 1588 : if (e->ref && e->ref->type == REF_ARRAY
1171 8 : && e->ref->u.ar.type == AR_UNKNOWN
1172 7 : && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1173 2 : goto syntax;
1174 :
1175 1586 : t = gfc_reduce_init_expr (e);
1176 :
1177 1586 : if (!t && e->ts.type == BT_UNKNOWN
1178 7 : && e->symtree->n.sym->attr.untyped == 1
1179 7 : && (flag_implicit_none
1180 5 : || e->symtree->n.sym->ns->seen_implicit_none == 1
1181 1 : || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1182 : {
1183 7 : gfc_free_expr (e);
1184 7 : goto syntax;
1185 : }
1186 :
1187 1579 : if ((e->ref && e->ref->type == REF_ARRAY
1188 4 : && e->ref->u.ar.type != AR_ELEMENT)
1189 1578 : || (!e->ref && e->expr_type == EXPR_ARRAY))
1190 : {
1191 2 : gfc_free_expr (e);
1192 2 : goto syntax;
1193 : }
1194 :
1195 1577 : gfc_free_expr (e);
1196 : }
1197 :
1198 17430 : if (gfc_seen_div0)
1199 52 : m = MATCH_ERROR;
1200 :
1201 : return m;
1202 :
1203 39 : syntax:
1204 39 : gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1205 39 : return MATCH_ERROR;
1206 : }
1207 :
1208 :
1209 : /* A character length is a '*' followed by a literal integer or a
1210 : char_len_param_value in parenthesis. */
1211 :
1212 : static match
1213 64623 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1214 : {
1215 64623 : int length;
1216 64623 : match m;
1217 :
1218 64623 : *deferred = false;
1219 64623 : m = gfc_match_char ('*');
1220 64623 : if (m != MATCH_YES)
1221 : return m;
1222 :
1223 2641 : m = gfc_match_small_literal_int (&length, NULL);
1224 2641 : if (m == MATCH_ERROR)
1225 : return m;
1226 :
1227 2641 : if (m == MATCH_YES)
1228 : {
1229 2137 : if (obsolescent_check
1230 2137 : && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1231 : return MATCH_ERROR;
1232 2137 : *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1233 2137 : return m;
1234 : }
1235 :
1236 504 : if (gfc_match_char ('(') == MATCH_NO)
1237 0 : goto syntax;
1238 :
1239 504 : m = char_len_param_value (expr, deferred);
1240 504 : if (m != MATCH_YES && gfc_matching_function)
1241 : {
1242 0 : gfc_undo_symbols ();
1243 0 : m = MATCH_YES;
1244 : }
1245 :
1246 1 : if (m == MATCH_ERROR)
1247 : return m;
1248 503 : if (m == MATCH_NO)
1249 0 : goto syntax;
1250 :
1251 503 : if (gfc_match_char (')') == MATCH_NO)
1252 : {
1253 0 : gfc_free_expr (*expr);
1254 0 : *expr = NULL;
1255 0 : goto syntax;
1256 : }
1257 :
1258 503 : if (obsolescent_check
1259 503 : && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1260 0 : return MATCH_ERROR;
1261 :
1262 : return MATCH_YES;
1263 :
1264 0 : syntax:
1265 0 : gfc_error ("Syntax error in character length specification at %C");
1266 0 : return MATCH_ERROR;
1267 : }
1268 :
1269 :
1270 : /* Special subroutine for finding a symbol. Check if the name is found
1271 : in the current name space. If not, and we're compiling a function or
1272 : subroutine and the parent compilation unit is an interface, then check
1273 : to see if the name we've been given is the name of the interface
1274 : (located in another namespace). */
1275 :
1276 : static int
1277 291786 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1278 : {
1279 291786 : gfc_state_data *s;
1280 291786 : gfc_symtree *st;
1281 291786 : int i;
1282 :
1283 291786 : i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1284 291786 : if (i == 0)
1285 : {
1286 291786 : *result = st ? st->n.sym : NULL;
1287 291786 : goto end;
1288 : }
1289 :
1290 0 : if (gfc_current_state () != COMP_SUBROUTINE
1291 0 : && gfc_current_state () != COMP_FUNCTION)
1292 0 : goto end;
1293 :
1294 0 : s = gfc_state_stack->previous;
1295 0 : if (s == NULL)
1296 0 : goto end;
1297 :
1298 0 : if (s->state != COMP_INTERFACE)
1299 0 : goto end;
1300 0 : if (s->sym == NULL)
1301 0 : goto end; /* Nameless interface. */
1302 :
1303 0 : if (strcmp (name, s->sym->name) == 0)
1304 : {
1305 0 : *result = s->sym;
1306 0 : return 0;
1307 : }
1308 :
1309 0 : end:
1310 : return i;
1311 : }
1312 :
1313 :
1314 : /* Special subroutine for getting a symbol node associated with a
1315 : procedure name, used in SUBROUTINE and FUNCTION statements. The
1316 : symbol is created in the parent using with symtree node in the
1317 : child unit pointing to the symbol. If the current namespace has no
1318 : parent, then the symbol is just created in the current unit. */
1319 :
1320 : static int
1321 66407 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1322 : {
1323 66407 : gfc_symtree *st;
1324 66407 : gfc_symbol *sym;
1325 66407 : int rc = 0;
1326 :
1327 : /* Module functions have to be left in their own namespace because
1328 : they have potentially (almost certainly!) already been referenced.
1329 : In this sense, they are rather like external functions. This is
1330 : fixed up in resolve.cc(resolve_entries), where the symbol name-
1331 : space is set to point to the master function, so that the fake
1332 : result mechanism can work. */
1333 66407 : if (module_fcn_entry)
1334 : {
1335 : /* Present if entry is declared to be a module procedure. */
1336 260 : rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1337 :
1338 260 : if (*result == NULL)
1339 217 : rc = gfc_get_symbol (name, NULL, result);
1340 86 : else if (!gfc_get_symbol (name, NULL, &sym) && sym
1341 43 : && (*result)->ts.type == BT_UNKNOWN
1342 86 : && sym->attr.flavor == FL_UNKNOWN)
1343 : /* Pick up the typespec for the entry, if declared in the function
1344 : body. Note that this symbol is FL_UNKNOWN because it will
1345 : only have appeared in a type declaration. The local symtree
1346 : is set to point to the module symbol and a unique symtree
1347 : to the local version. This latter ensures a correct clearing
1348 : of the symbols. */
1349 : {
1350 : /* If the ENTRY proceeds its specification, we need to ensure
1351 : that this does not raise a "has no IMPLICIT type" error. */
1352 43 : if (sym->ts.type == BT_UNKNOWN)
1353 23 : sym->attr.untyped = 1;
1354 :
1355 43 : (*result)->ts = sym->ts;
1356 :
1357 : /* Put the symbol in the procedure namespace so that, should
1358 : the ENTRY precede its specification, the specification
1359 : can be applied. */
1360 43 : (*result)->ns = gfc_current_ns;
1361 :
1362 43 : gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1363 43 : st->n.sym = *result;
1364 43 : st = gfc_get_unique_symtree (gfc_current_ns);
1365 43 : sym->refs++;
1366 43 : st->n.sym = sym;
1367 : }
1368 : }
1369 : else
1370 66147 : rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1371 :
1372 66407 : if (rc)
1373 : return rc;
1374 :
1375 66406 : sym = *result;
1376 66406 : if (sym->attr.proc == PROC_ST_FUNCTION)
1377 : return rc;
1378 :
1379 66405 : if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1380 : {
1381 508 : gfc_actual_arglist *parms = NULL;
1382 : /* Create a partially populated interface symbol to carry the
1383 : characteristics of the procedure and the result. */
1384 508 : sym->tlink = gfc_new_symbol (name, sym->ns);
1385 508 : gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1386 508 : gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1387 508 : if (sym->attr.dimension)
1388 17 : sym->tlink->as = gfc_copy_array_spec (sym->as);
1389 :
1390 : /* The result parameters coming from the interface have to be copied
1391 : to the procedure symbol itself because of the modifications to
1392 : the result further downstream. This is required in trans-decl.cc
1393 : when the result is initialized. */
1394 508 : if (sym->result && IS_PDT (sym->result) && sym->result->param_list)
1395 40 : parms = gfc_copy_actual_arglist (sym->result->param_list);
1396 :
1397 : /* Ideally, at this point, a copy would be made of the formal
1398 : arguments and their namespace. However, this does not appear
1399 : to be necessary, albeit at the expense of not being able to
1400 : use gfc_compare_interfaces directly. */
1401 :
1402 508 : if (sym->result && sym->result != sym)
1403 : {
1404 141 : sym->tlink->result = sym->result;
1405 141 : sym->tlink->param_list = parms;
1406 141 : sym->result = NULL;
1407 : }
1408 367 : else if (sym->result)
1409 : {
1410 93 : sym->tlink->result = sym->tlink;
1411 93 : sym->tlink->param_list = parms;
1412 : }
1413 : }
1414 65897 : else if (sym && !sym->gfc_new
1415 25385 : && gfc_current_state () != COMP_INTERFACE)
1416 : {
1417 : /* Trap another encompassed procedure with the same name. All
1418 : these conditions are necessary to avoid picking up an entry
1419 : whose name clashes with that of the encompassing procedure;
1420 : this is handled using gsymbols to register unique, globally
1421 : accessible names. */
1422 24044 : if (sym->attr.flavor != 0
1423 21961 : && sym->attr.proc != 0
1424 2422 : && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1425 7 : && sym->attr.if_source != IFSRC_UNKNOWN)
1426 : {
1427 7 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1428 : name, &sym->declared_at);
1429 7 : return true;
1430 : }
1431 24037 : if (sym->attr.flavor != 0
1432 21954 : && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1433 : {
1434 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1435 : name, &sym->declared_at);
1436 1 : return true;
1437 : }
1438 :
1439 24036 : if (sym->attr.external && sym->attr.procedure
1440 2 : && gfc_current_state () == COMP_CONTAINS)
1441 : {
1442 1 : gfc_error_now ("Contained procedure %qs at %C clashes with "
1443 : "procedure defined at %L",
1444 : name, &sym->declared_at);
1445 1 : return true;
1446 : }
1447 :
1448 : /* Trap a procedure with a name the same as interface in the
1449 : encompassing scope. */
1450 24035 : if (sym->attr.generic != 0
1451 60 : && (sym->attr.subroutine || sym->attr.function)
1452 1 : && !sym->attr.mod_proc)
1453 : {
1454 1 : gfc_error_now ("Name %qs at %C is already defined"
1455 : " as a generic interface at %L",
1456 : name, &sym->declared_at);
1457 1 : return true;
1458 : }
1459 :
1460 : /* Trap declarations of attributes in encompassing scope. The
1461 : signature for this is that ts.kind is nonzero for no-CLASS
1462 : entity. For a CLASS entity, ts.kind is zero. */
1463 24034 : if ((sym->ts.kind != 0
1464 23661 : || sym->ts.type == BT_CLASS
1465 23660 : || sym->ts.type == BT_DERIVED)
1466 397 : && !sym->attr.implicit_type
1467 396 : && sym->attr.proc == 0
1468 378 : && gfc_current_ns->parent != NULL
1469 138 : && sym->attr.access == 0
1470 136 : && !module_fcn_entry)
1471 : {
1472 5 : gfc_error_now ("Procedure %qs at %C has an explicit interface "
1473 : "from a previous declaration", name);
1474 5 : return true;
1475 : }
1476 : }
1477 :
1478 : /* F2023: C1247 (R1526) MODULE shall appear only in the function-stmt or
1479 : subroutine-stmt of a module subprogram or of a nonabstract interface
1480 : body that is declared in the scoping unit of a module or submodule. */
1481 66390 : if (sym->attr.external
1482 92 : && (sym->attr.subroutine || sym->attr.function)
1483 91 : && sym->attr.if_source == IFSRC_IFBODY
1484 91 : && !current_attr.module_procedure
1485 3 : && sym->attr.proc == PROC_MODULE
1486 3 : && gfc_state_stack->state == COMP_CONTAINS)
1487 1 : gfc_error_now ("Procedure %qs defined in interface body at %L "
1488 : "clashes with internal procedure defined at %C",
1489 : name, &sym->declared_at);
1490 :
1491 : /* This is the converse requirement: The separate-module-subprogram for a
1492 : module procedure shall have the MODULE prefix or be declared a MODULE
1493 : PROCEDURE, otherwise it would be ambiguous. */
1494 66390 : if (sym->attr.module_procedure
1495 508 : && (sym->attr.subroutine || sym->attr.function)
1496 508 : && sym->attr.if_source == IFSRC_IFBODY
1497 508 : && !current_attr.module_procedure
1498 4 : && sym->attr.proc == PROC_MODULE
1499 4 : && gfc_state_stack->state == COMP_CONTAINS
1500 2 : && gfc_state_stack->previous
1501 2 : && gfc_state_stack->previous->state == COMP_SUBMODULE)
1502 1 : gfc_error_now ("Procedure %qs at %C requires the MODULE prefix because "
1503 : "it is a module procedure declared in module %qs",
1504 1 : name, sym->module ? sym->module : "");
1505 :
1506 66390 : if (sym && !sym->gfc_new
1507 25878 : && sym->attr.flavor != FL_UNKNOWN
1508 23395 : && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1509 244 : && gfc_state_stack->state == COMP_CONTAINS
1510 239 : && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1511 : {
1512 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1513 : name, &sym->declared_at);
1514 1 : return true;
1515 : }
1516 :
1517 66389 : if (gfc_current_ns->parent == NULL || *result == NULL)
1518 : return rc;
1519 :
1520 : /* Module function entries will already have a symtree in
1521 : the current namespace but will need one at module level. */
1522 54028 : if (module_fcn_entry)
1523 : {
1524 : /* Present if entry is declared to be a module procedure. */
1525 258 : rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1526 258 : if (st == NULL)
1527 217 : st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1528 : }
1529 : else
1530 53770 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1531 :
1532 54028 : st->n.sym = sym;
1533 54028 : sym->refs++;
1534 :
1535 : /* See if the procedure should be a module procedure. */
1536 :
1537 54028 : if (((sym->ns->proc_name != NULL
1538 54028 : && sym->ns->proc_name->attr.flavor == FL_MODULE
1539 22070 : && sym->attr.proc != PROC_MODULE)
1540 54028 : || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1541 73197 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1542 : rc = 2;
1543 :
1544 : return rc;
1545 : }
1546 :
1547 :
1548 : /* Verify that the given symbol representing a parameter is C
1549 : interoperable, by checking to see if it was marked as such after
1550 : its declaration. If the given symbol is not interoperable, a
1551 : warning is reported, thus removing the need to return the status to
1552 : the calling function. The standard does not require the user use
1553 : one of the iso_c_binding named constants to declare an
1554 : interoperable parameter, but we can't be sure if the param is C
1555 : interop or not if the user doesn't. For example, integer(4) may be
1556 : legal Fortran, but doesn't have meaning in C. It may interop with
1557 : a number of the C types, which causes a problem because the
1558 : compiler can't know which one. This code is almost certainly not
1559 : portable, and the user will get what they deserve if the C type
1560 : across platforms isn't always interoperable with integer(4). If
1561 : the user had used something like integer(c_int) or integer(c_long),
1562 : the compiler could have automatically handled the varying sizes
1563 : across platforms. */
1564 :
1565 : bool
1566 17666 : gfc_verify_c_interop_param (gfc_symbol *sym)
1567 : {
1568 17666 : int is_c_interop = 0;
1569 17666 : bool retval = true;
1570 :
1571 : /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
1572 : Don't repeat the checks here. */
1573 17666 : if (sym->attr.implicit_type)
1574 : return true;
1575 :
1576 : /* For subroutines or functions that are passed to a BIND(C) procedure,
1577 : they're interoperable if they're BIND(C) and their params are all
1578 : interoperable. */
1579 17666 : if (sym->attr.flavor == FL_PROCEDURE)
1580 : {
1581 4 : if (sym->attr.is_bind_c == 0)
1582 : {
1583 0 : gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1584 : "attribute to be C interoperable", sym->name,
1585 : &(sym->declared_at));
1586 0 : return false;
1587 : }
1588 : else
1589 : {
1590 4 : if (sym->attr.is_c_interop == 1)
1591 : /* We've already checked this procedure; don't check it again. */
1592 : return true;
1593 : else
1594 4 : return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1595 4 : sym->common_block);
1596 : }
1597 : }
1598 :
1599 : /* See if we've stored a reference to a procedure that owns sym. */
1600 17662 : if (sym->ns != NULL && sym->ns->proc_name != NULL)
1601 : {
1602 17662 : if (sym->ns->proc_name->attr.is_bind_c == 1)
1603 : {
1604 17623 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
1605 17623 : bool f2018_added = false;
1606 :
1607 17623 : is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1608 :
1609 : /* F2018:18.3.6 has the following text:
1610 : "(5) any dummy argument without the VALUE attribute corresponds to
1611 : a formal parameter of the prototype that is of a pointer type, and
1612 : either
1613 : • the dummy argument is interoperable with an entity of the
1614 : referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
1615 : the formal parameter (this is equivalent to the F2008 text),
1616 : • the dummy argument is a nonallocatable nonpointer variable of
1617 : type CHARACTER with assumed character length and the formal
1618 : parameter is a pointer to CFI_cdesc_t,
1619 : • the dummy argument is allocatable, assumed-shape, assumed-rank,
1620 : or a pointer without the CONTIGUOUS attribute, and the formal
1621 : parameter is a pointer to CFI_cdesc_t, or
1622 : • the dummy argument is assumed-type and not allocatable,
1623 : assumed-shape, assumed-rank, or a pointer, and the formal
1624 : parameter is a pointer to void," */
1625 3731 : if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
1626 : {
1627 2364 : bool as_ar = (sym->as
1628 2364 : && (sym->as->type == AS_ASSUMED_SHAPE
1629 2117 : || sym->as->type == AS_ASSUMED_RANK));
1630 4728 : bool cond1 = (sym->ts.type == BT_CHARACTER
1631 1565 : && !(sym->ts.u.cl && sym->ts.u.cl->length)
1632 905 : && !sym->attr.allocatable
1633 3251 : && !sym->attr.pointer);
1634 4728 : bool cond2 = (sym->attr.allocatable
1635 2267 : || as_ar
1636 3389 : || (IS_POINTER (sym) && !sym->attr.contiguous));
1637 4728 : bool cond3 = (sym->ts.type == BT_ASSUMED
1638 0 : && !sym->attr.allocatable
1639 0 : && !sym->attr.pointer
1640 2364 : && !as_ar);
1641 2364 : f2018_added = cond1 || cond2 || cond3;
1642 : }
1643 :
1644 17623 : if (is_c_interop != 1 && !f2018_added)
1645 : {
1646 : /* Make personalized messages to give better feedback. */
1647 1837 : if (sym->ts.type == BT_DERIVED)
1648 1 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1649 : "BIND(C) procedure %qs but is not C interoperable "
1650 : "because derived type %qs is not C interoperable",
1651 : sym->name, &(sym->declared_at),
1652 1 : sym->ns->proc_name->name,
1653 1 : sym->ts.u.derived->name);
1654 1836 : else if (sym->ts.type == BT_CLASS)
1655 6 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1656 : "BIND(C) procedure %qs but is not C interoperable "
1657 : "because it is polymorphic",
1658 : sym->name, &(sym->declared_at),
1659 6 : sym->ns->proc_name->name);
1660 1830 : else if (warn_c_binding_type)
1661 39 : gfc_warning (OPT_Wc_binding_type,
1662 : "Variable %qs at %L is a dummy argument of the "
1663 : "BIND(C) procedure %qs but may not be C "
1664 : "interoperable",
1665 : sym->name, &(sym->declared_at),
1666 39 : sym->ns->proc_name->name);
1667 : }
1668 :
1669 : /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted. */
1670 17623 : if (sym->attr.pointer && sym->attr.contiguous)
1671 2 : gfc_error ("Dummy argument %qs at %L may not be a pointer with "
1672 : "CONTIGUOUS attribute as procedure %qs is BIND(C)",
1673 2 : sym->name, &sym->declared_at, sym->ns->proc_name->name);
1674 :
1675 : /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
1676 : procedure that are default-initialized are not permitted. */
1677 16983 : if ((sym->attr.pointer || sym->attr.allocatable)
1678 1041 : && sym->ts.type == BT_DERIVED
1679 18001 : && gfc_has_default_initializer (sym->ts.u.derived))
1680 : {
1681 8 : gfc_error ("Default-initialized dummy argument %qs with %s "
1682 : "attribute at %L is not permitted in BIND(C) "
1683 : "procedure %qs", sym->name,
1684 4 : (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
1685 4 : &sym->declared_at, sym->ns->proc_name->name);
1686 4 : retval = false;
1687 : }
1688 :
1689 : /* Character strings are only C interoperable if they have a
1690 : length of 1. However, as an argument they are also interoperable
1691 : when passed as descriptor (which requires len=: or len=*). */
1692 17623 : if (sym->ts.type == BT_CHARACTER)
1693 : {
1694 2350 : gfc_charlen *cl = sym->ts.u.cl;
1695 :
1696 2350 : if (sym->attr.allocatable || sym->attr.pointer)
1697 : {
1698 : /* F2018, 18.3.6 (6). */
1699 195 : if (!sym->ts.deferred)
1700 : {
1701 64 : if (sym->attr.allocatable)
1702 32 : gfc_error ("Allocatable character dummy argument %qs "
1703 : "at %L must have deferred length as "
1704 : "procedure %qs is BIND(C)", sym->name,
1705 32 : &sym->declared_at, sym->ns->proc_name->name);
1706 : else
1707 32 : gfc_error ("Pointer character dummy argument %qs at %L "
1708 : "must have deferred length as procedure %qs "
1709 : "is BIND(C)", sym->name, &sym->declared_at,
1710 32 : sym->ns->proc_name->name);
1711 : retval = false;
1712 : }
1713 131 : else if (!gfc_notify_std (GFC_STD_F2018,
1714 : "Deferred-length character dummy "
1715 : "argument %qs at %L of procedure "
1716 : "%qs with BIND(C) attribute",
1717 : sym->name, &sym->declared_at,
1718 131 : sym->ns->proc_name->name))
1719 17623 : retval = false;
1720 : }
1721 2155 : else if (sym->attr.value
1722 360 : && (!cl || !cl->length
1723 360 : || cl->length->expr_type != EXPR_CONSTANT
1724 360 : || mpz_cmp_si (cl->length->value.integer, 1) != 0))
1725 : {
1726 1 : gfc_error ("Character dummy argument %qs at %L must be "
1727 : "of length 1 as it has the VALUE attribute",
1728 : sym->name, &sym->declared_at);
1729 1 : retval = false;
1730 : }
1731 2154 : else if (!cl || !cl->length)
1732 : {
1733 : /* Assumed length; F2018, 18.3.6 (5)(2).
1734 : Uses the CFI array descriptor - also for scalars and
1735 : explicit-size/assumed-size arrays. */
1736 959 : if (!gfc_notify_std (GFC_STD_F2018,
1737 : "Assumed-length character dummy argument "
1738 : "%qs at %L of procedure %qs with BIND(C) "
1739 : "attribute", sym->name, &sym->declared_at,
1740 959 : sym->ns->proc_name->name))
1741 17623 : retval = false;
1742 : }
1743 1195 : else if (cl->length->expr_type != EXPR_CONSTANT
1744 881 : || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1745 : {
1746 : /* F2018, 18.3.6, (5), item 4. */
1747 653 : if (!sym->attr.dimension
1748 645 : || sym->as->type == AS_ASSUMED_SIZE
1749 639 : || sym->as->type == AS_EXPLICIT)
1750 : {
1751 20 : gfc_error ("Character dummy argument %qs at %L must be "
1752 : "of constant length of one or assumed length, "
1753 : "unless it has assumed shape or assumed rank, "
1754 : "as procedure %qs has the BIND(C) attribute",
1755 : sym->name, &sym->declared_at,
1756 20 : sym->ns->proc_name->name);
1757 20 : retval = false;
1758 : }
1759 : /* else: valid only since F2018 - and an assumed-shape/rank
1760 : array; however, gfc_notify_std is already called when
1761 : those array types are used. Thus, silently accept F200x. */
1762 : }
1763 : }
1764 :
1765 : /* We have to make sure that any param to a bind(c) routine does
1766 : not have the allocatable, pointer, or optional attributes,
1767 : according to J3/04-007, section 5.1. */
1768 17623 : if (sym->attr.allocatable == 1
1769 18024 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1770 : "ALLOCATABLE attribute in procedure %qs "
1771 : "with BIND(C)", sym->name,
1772 : &(sym->declared_at),
1773 401 : sym->ns->proc_name->name))
1774 : retval = false;
1775 :
1776 17623 : if (sym->attr.pointer == 1
1777 18263 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1778 : "POINTER attribute in procedure %qs "
1779 : "with BIND(C)", sym->name,
1780 : &(sym->declared_at),
1781 640 : sym->ns->proc_name->name))
1782 : retval = false;
1783 :
1784 17623 : if (sym->attr.optional == 1 && sym->attr.value)
1785 : {
1786 9 : gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1787 : "and the VALUE attribute because procedure %qs "
1788 : "is BIND(C)", sym->name, &(sym->declared_at),
1789 9 : sym->ns->proc_name->name);
1790 9 : retval = false;
1791 : }
1792 17614 : else if (sym->attr.optional == 1
1793 18568 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1794 : "at %L with OPTIONAL attribute in "
1795 : "procedure %qs which is BIND(C)",
1796 : sym->name, &(sym->declared_at),
1797 954 : sym->ns->proc_name->name))
1798 : retval = false;
1799 :
1800 : /* Make sure that if it has the dimension attribute, that it is
1801 : either assumed size or explicit shape. Deferred shape is already
1802 : covered by the pointer/allocatable attribute. */
1803 5553 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1804 18957 : && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1805 : "at %L as dummy argument to the BIND(C) "
1806 : "procedure %qs at %L", sym->name,
1807 : &(sym->declared_at),
1808 : sym->ns->proc_name->name,
1809 1334 : &(sym->ns->proc_name->declared_at)))
1810 : retval = false;
1811 : }
1812 : }
1813 :
1814 : return retval;
1815 : }
1816 :
1817 :
1818 :
1819 : /* Function called by variable_decl() that adds a name to the symbol table. */
1820 :
1821 : static bool
1822 269822 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
1823 : gfc_array_spec **as, locus *var_locus)
1824 : {
1825 269822 : symbol_attribute attr;
1826 269822 : gfc_symbol *sym;
1827 269822 : int upper;
1828 269822 : gfc_symtree *st, *host_st = NULL;
1829 :
1830 : /* Symbols in a submodule are host associated from the parent module or
1831 : submodules. Therefore, they can be overridden by declarations in the
1832 : submodule scope. Deal with this by attaching the existing symbol to
1833 : a new symtree and recycling the old symtree with a new symbol... */
1834 269822 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1835 269822 : if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
1836 3 : && gfc_current_ns->parent)
1837 3 : host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
1838 :
1839 269822 : if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1840 12 : && st->n.sym != NULL
1841 12 : && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1842 : {
1843 12 : gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1844 12 : s->n.sym = st->n.sym;
1845 12 : sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
1846 :
1847 12 : st->n.sym = sym;
1848 12 : sym->refs++;
1849 12 : gfc_set_sym_referenced (sym);
1850 12 : }
1851 : /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
1852 : current scope are not violated by local redeclarations. Note that there is
1853 : no need to guard for std >= F2018 because import_only and IMPORT_ALL are
1854 : only set for these standards. */
1855 269810 : else if (host_st && host_st->n.sym
1856 2 : && host_st->n.sym != gfc_current_ns->proc_name
1857 2 : && !(st && st->n.sym
1858 1 : && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
1859 : {
1860 2 : gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
1861 : "statement and must not be re-declared", name, var_locus,
1862 1 : (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
1863 2 : return false;
1864 : }
1865 : /* ...Otherwise generate a new symtree and new symbol. */
1866 269808 : else if (gfc_get_symbol (name, NULL, &sym, var_locus))
1867 : return false;
1868 :
1869 : /* Check if the name has already been defined as a type. The
1870 : first letter of the symtree will be in upper case then. Of
1871 : course, this is only necessary if the upper case letter is
1872 : actually different. */
1873 :
1874 269820 : upper = TOUPPER(name[0]);
1875 269820 : if (upper != name[0])
1876 : {
1877 269070 : char u_name[GFC_MAX_SYMBOL_LEN + 1];
1878 269070 : gfc_symtree *st;
1879 :
1880 269070 : gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1881 269070 : strcpy (u_name, name);
1882 269070 : u_name[0] = upper;
1883 :
1884 269070 : st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1885 :
1886 : /* STRUCTURE types can alias symbol names */
1887 269070 : if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1888 : {
1889 1 : gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1890 : &st->n.sym->declared_at);
1891 1 : return false;
1892 : }
1893 : }
1894 :
1895 : /* Start updating the symbol table. Add basic type attribute if present. */
1896 269819 : if (current_ts.type != BT_UNKNOWN
1897 269819 : && (sym->attr.implicit_type == 0
1898 186 : || !gfc_compare_types (&sym->ts, ¤t_ts))
1899 539456 : && !gfc_add_type (sym, ¤t_ts, var_locus))
1900 : {
1901 : /* Duplicate-type rejection can leave a fresh CHARACTER length node on
1902 : the namespace list before it is attached to any surviving symbol.
1903 : Drop only that unattached node; shared constant charlen nodes are
1904 : already reachable from earlier declarations. PR82721. */
1905 27 : if (current_ts.type == BT_CHARACTER && cl && elem == 1)
1906 : {
1907 1 : discard_pending_charlen (cl);
1908 1 : gfc_clear_ts (¤t_ts);
1909 : }
1910 26 : else if (current_ts.type == BT_CHARACTER && cl && cl != current_ts.u.cl)
1911 0 : discard_pending_charlen (cl);
1912 : return false;
1913 : }
1914 :
1915 269792 : if (sym->ts.type == BT_CHARACTER)
1916 : {
1917 29692 : if (elem > 1)
1918 4238 : sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
1919 : else
1920 : sym->ts.u.cl = cl;
1921 29692 : sym->ts.deferred = cl_deferred;
1922 : }
1923 :
1924 : /* Add dimension attribute if present. */
1925 269792 : if (!gfc_set_array_spec (sym, *as, var_locus))
1926 : return false;
1927 269790 : *as = NULL;
1928 :
1929 : /* Add attribute to symbol. The copy is so that we can reset the
1930 : dimension attribute. */
1931 269790 : attr = current_attr;
1932 269790 : attr.dimension = 0;
1933 269790 : attr.codimension = 0;
1934 :
1935 269790 : if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1936 : return false;
1937 :
1938 : /* Finish any work that may need to be done for the binding label,
1939 : if it's a bind(c). The bind(c) attr is found before the symbol
1940 : is made, and before the symbol name (for data decls), so the
1941 : current_ts is holding the binding label, or nothing if the
1942 : name= attr wasn't given. Therefore, test here if we're dealing
1943 : with a bind(c) and make sure the binding label is set correctly. */
1944 269778 : if (sym->attr.is_bind_c == 1)
1945 : {
1946 1902 : if (!sym->binding_label)
1947 : {
1948 : /* Set the binding label and verify that if a NAME= was specified
1949 : then only one identifier was in the entity-decl-list. */
1950 137 : if (!set_binding_label (&sym->binding_label, sym->name,
1951 : num_idents_on_line))
1952 : return false;
1953 : }
1954 : }
1955 :
1956 : /* See if we know we're in a common block, and if it's a bind(c)
1957 : common then we need to make sure we're an interoperable type. */
1958 269776 : if (sym->attr.in_common == 1)
1959 : {
1960 : /* Test the common block object. */
1961 614 : if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1962 6 : && sym->ts.is_c_interop != 1)
1963 : {
1964 0 : gfc_error_now ("Variable %qs in common block %qs at %C "
1965 : "must be declared with a C interoperable "
1966 : "kind since common block %qs is BIND(C)",
1967 : sym->name, sym->common_block->name,
1968 0 : sym->common_block->name);
1969 0 : gfc_clear_error ();
1970 : }
1971 : }
1972 :
1973 269776 : sym->attr.implied_index = 0;
1974 :
1975 : /* Use the parameter expressions for a parameterized derived type. */
1976 269776 : if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1977 38880 : && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1978 1668 : sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1979 :
1980 269776 : if (sym->ts.type == BT_CLASS)
1981 11655 : return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1982 :
1983 : return true;
1984 : }
1985 :
1986 :
1987 : /* Set character constant to the given length. The constant will be padded or
1988 : truncated. If we're inside an array constructor without a typespec, we
1989 : additionally check that all elements have the same length; check_len -1
1990 : means no checking. */
1991 :
1992 : void
1993 15052 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1994 : gfc_charlen_t check_len)
1995 : {
1996 15052 : gfc_char_t *s;
1997 15052 : gfc_charlen_t slen;
1998 :
1999 15052 : if (expr->ts.type != BT_CHARACTER)
2000 : return;
2001 :
2002 15050 : if (expr->expr_type != EXPR_CONSTANT)
2003 : {
2004 1 : gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
2005 1 : return;
2006 : }
2007 :
2008 15049 : slen = expr->value.character.length;
2009 15049 : if (len != slen)
2010 : {
2011 2194 : s = gfc_get_wide_string (len + 1);
2012 2194 : memcpy (s, expr->value.character.string,
2013 2194 : MIN (len, slen) * sizeof (gfc_char_t));
2014 2194 : if (len > slen)
2015 1903 : gfc_wide_memset (&s[slen], ' ', len - slen);
2016 :
2017 2194 : if (warn_character_truncation && slen > len)
2018 1 : gfc_warning_now (OPT_Wcharacter_truncation,
2019 : "CHARACTER expression at %L is being truncated "
2020 : "(%ld/%ld)", &expr->where,
2021 : (long) slen, (long) len);
2022 :
2023 : /* Apply the standard by 'hand' otherwise it gets cleared for
2024 : initializers. */
2025 2194 : if (check_len != -1 && slen != check_len)
2026 : {
2027 3 : if (!(gfc_option.allow_std & GFC_STD_GNU))
2028 0 : gfc_error_now ("The CHARACTER elements of the array constructor "
2029 : "at %L must have the same length (%ld/%ld)",
2030 : &expr->where, (long) slen,
2031 : (long) check_len);
2032 : else
2033 3 : gfc_notify_std (GFC_STD_LEGACY,
2034 : "The CHARACTER elements of the array constructor "
2035 : "at %L must have the same length (%ld/%ld)",
2036 : &expr->where, (long) slen,
2037 : (long) check_len);
2038 : }
2039 :
2040 2194 : s[len] = '\0';
2041 2194 : free (expr->value.character.string);
2042 2194 : expr->value.character.string = s;
2043 2194 : expr->value.character.length = len;
2044 : /* If explicit representation was given, clear it
2045 : as it is no longer needed after padding. */
2046 2194 : if (expr->representation.length)
2047 : {
2048 45 : expr->representation.length = 0;
2049 45 : free (expr->representation.string);
2050 45 : expr->representation.string = NULL;
2051 : }
2052 : }
2053 : }
2054 :
2055 :
2056 : /* Function to create and update the enumerator history
2057 : using the information passed as arguments.
2058 : Pointer "max_enum" is also updated, to point to
2059 : enum history node containing largest initializer.
2060 :
2061 : SYM points to the symbol node of enumerator.
2062 : INIT points to its enumerator value. */
2063 :
2064 : static void
2065 543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
2066 : {
2067 543 : enumerator_history *new_enum_history;
2068 543 : gcc_assert (sym != NULL && init != NULL);
2069 :
2070 543 : new_enum_history = XCNEW (enumerator_history);
2071 :
2072 543 : new_enum_history->sym = sym;
2073 543 : new_enum_history->initializer = init;
2074 543 : new_enum_history->next = NULL;
2075 :
2076 543 : if (enum_history == NULL)
2077 : {
2078 160 : enum_history = new_enum_history;
2079 160 : max_enum = enum_history;
2080 : }
2081 : else
2082 : {
2083 383 : new_enum_history->next = enum_history;
2084 383 : enum_history = new_enum_history;
2085 :
2086 383 : if (mpz_cmp (max_enum->initializer->value.integer,
2087 383 : new_enum_history->initializer->value.integer) < 0)
2088 381 : max_enum = new_enum_history;
2089 : }
2090 543 : }
2091 :
2092 :
2093 : /* Function to free enum kind history. */
2094 :
2095 : void
2096 175 : gfc_free_enum_history (void)
2097 : {
2098 175 : enumerator_history *current = enum_history;
2099 175 : enumerator_history *next;
2100 :
2101 718 : while (current != NULL)
2102 : {
2103 543 : next = current->next;
2104 543 : free (current);
2105 543 : current = next;
2106 : }
2107 175 : max_enum = NULL;
2108 175 : enum_history = NULL;
2109 175 : }
2110 :
2111 :
2112 : /* Function to fix initializer character length if the length of the
2113 : symbol or component is constant. */
2114 :
2115 : static bool
2116 2903 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
2117 : {
2118 2903 : if (!gfc_specification_expr (ts->u.cl->length))
2119 : return false;
2120 :
2121 2903 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
2122 :
2123 : /* resolve_charlen will complain later on if the length
2124 : is too large. Just skip the initialization in that case. */
2125 2903 : if (mpz_cmp (ts->u.cl->length->value.integer,
2126 2903 : gfc_integer_kinds[k].huge) <= 0)
2127 : {
2128 2902 : HOST_WIDE_INT len
2129 2902 : = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
2130 :
2131 2902 : if (init->expr_type == EXPR_CONSTANT)
2132 2101 : gfc_set_constant_character_len (len, init, -1);
2133 801 : else if (init->expr_type == EXPR_ARRAY)
2134 : {
2135 782 : gfc_constructor *cons;
2136 :
2137 : /* Build a new charlen to prevent simplification from
2138 : deleting the length before it is resolved. */
2139 782 : init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2140 782 : init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
2141 782 : cons = gfc_constructor_first (init->value.constructor);
2142 5329 : for (; cons; cons = gfc_constructor_next (cons))
2143 3765 : gfc_set_constant_character_len (len, cons->expr, -1);
2144 : }
2145 : }
2146 :
2147 : return true;
2148 : }
2149 :
2150 :
2151 : /* Function called by variable_decl() that adds an initialization
2152 : expression to a symbol. */
2153 :
2154 : static bool
2155 278359 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus,
2156 : gfc_charlen *saved_cl_list)
2157 : {
2158 278359 : symbol_attribute attr;
2159 278359 : gfc_symbol *sym;
2160 278359 : gfc_expr *init;
2161 :
2162 278359 : init = *initp;
2163 278359 : if (find_special (name, &sym, false))
2164 : return false;
2165 :
2166 278359 : attr = sym->attr;
2167 :
2168 : /* If this symbol is confirming an implicit parameter type,
2169 : then an initialization expression is not allowed. */
2170 278359 : if (attr.flavor == FL_PARAMETER && sym->value != NULL)
2171 : {
2172 1 : if (*initp != NULL)
2173 : {
2174 0 : gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
2175 : sym->name);
2176 0 : return false;
2177 : }
2178 : else
2179 : return true;
2180 : }
2181 :
2182 278358 : if (init == NULL)
2183 : {
2184 : /* An initializer is required for PARAMETER declarations. */
2185 244122 : if (attr.flavor == FL_PARAMETER)
2186 : {
2187 1 : gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
2188 1 : return false;
2189 : }
2190 : }
2191 : else
2192 : {
2193 : /* If a variable appears in a DATA block, it cannot have an
2194 : initializer. */
2195 34236 : if (sym->attr.data)
2196 : {
2197 0 : gfc_error ("Variable %qs at %C with an initializer already "
2198 : "appears in a DATA statement", sym->name);
2199 0 : return false;
2200 : }
2201 :
2202 : /* Check if the assignment can happen. This has to be put off
2203 : until later for derived type variables and procedure pointers. */
2204 32961 : if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
2205 32938 : && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
2206 32888 : && !sym->attr.proc_pointer
2207 67012 : && !gfc_check_assign_symbol (sym, NULL, init))
2208 : return false;
2209 :
2210 34205 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
2211 3596 : && init->ts.type == BT_CHARACTER)
2212 : {
2213 : /* Update symbol character length according initializer. */
2214 3432 : if (!gfc_check_assign_symbol (sym, NULL, init))
2215 : return false;
2216 :
2217 3432 : if (sym->ts.u.cl->length == NULL)
2218 : {
2219 893 : gfc_charlen_t clen;
2220 : /* If there are multiple CHARACTER variables declared on the
2221 : same line, we don't want them to share the same length. */
2222 893 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2223 :
2224 893 : if (sym->attr.flavor == FL_PARAMETER)
2225 : {
2226 860 : if (init->expr_type == EXPR_CONSTANT)
2227 : {
2228 569 : clen = init->value.character.length;
2229 569 : sym->ts.u.cl->length
2230 569 : = gfc_get_int_expr (gfc_charlen_int_kind,
2231 : NULL, clen);
2232 : }
2233 291 : else if (init->expr_type == EXPR_ARRAY)
2234 : {
2235 291 : if (init->ts.u.cl && init->ts.u.cl->length)
2236 : {
2237 279 : const gfc_expr *length = init->ts.u.cl->length;
2238 279 : if (length->expr_type != EXPR_CONSTANT)
2239 : {
2240 3 : gfc_error ("Cannot initialize parameter array "
2241 : "at %L "
2242 : "with variable length elements",
2243 : &sym->declared_at);
2244 :
2245 : /* This rejection path can leave several
2246 : declaration-local charlens on cl_list,
2247 : including the replacement symbol charlen and
2248 : the array-constructor typespec charlen.
2249 : Clear the surviving owners first, then drop
2250 : only the nodes created by this declaration. */
2251 3 : sym->ts.u.cl = NULL;
2252 3 : init->ts.u.cl = NULL;
2253 3 : discard_pending_charlens (saved_cl_list);
2254 3 : return false;
2255 : }
2256 276 : clen = mpz_get_si (length->value.integer);
2257 276 : }
2258 12 : else if (init->value.constructor)
2259 : {
2260 12 : gfc_constructor *c;
2261 12 : c = gfc_constructor_first (init->value.constructor);
2262 12 : clen = c->expr->value.character.length;
2263 : }
2264 : else
2265 0 : gcc_unreachable ();
2266 288 : sym->ts.u.cl->length
2267 288 : = gfc_get_int_expr (gfc_charlen_int_kind,
2268 : NULL, clen);
2269 : }
2270 0 : else if (init->ts.u.cl && init->ts.u.cl->length)
2271 0 : sym->ts.u.cl->length =
2272 0 : gfc_copy_expr (init->ts.u.cl->length);
2273 : }
2274 : }
2275 : /* Update initializer character length according to symbol. */
2276 2539 : else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2277 2539 : && !fix_initializer_charlen (&sym->ts, init))
2278 : return false;
2279 : }
2280 :
2281 34202 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
2282 3840 : && sym->as->rank && init->rank && init->rank != sym->as->rank)
2283 : {
2284 3 : gfc_error ("Rank mismatch of array at %L and its initializer "
2285 : "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
2286 3 : return false;
2287 : }
2288 :
2289 : /* If sym is implied-shape, set its upper bounds from init. */
2290 34199 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2291 3837 : && sym->as && sym->as->type == AS_IMPLIED_SHAPE)
2292 : {
2293 1043 : int dim;
2294 :
2295 1043 : if (init->rank == 0)
2296 : {
2297 1 : gfc_error ("Cannot initialize implied-shape array at %L"
2298 : " with scalar", &sym->declared_at);
2299 1 : return false;
2300 : }
2301 :
2302 : /* The shape may be NULL for EXPR_ARRAY, set it. */
2303 1042 : if (init->shape == NULL)
2304 : {
2305 5 : if (init->expr_type != EXPR_ARRAY)
2306 : {
2307 2 : gfc_error ("Bad shape of initializer at %L", &init->where);
2308 2 : return false;
2309 : }
2310 :
2311 3 : init->shape = gfc_get_shape (1);
2312 3 : if (!gfc_array_size (init, &init->shape[0]))
2313 : {
2314 1 : gfc_error ("Cannot determine shape of initializer at %L",
2315 : &init->where);
2316 1 : free (init->shape);
2317 1 : init->shape = NULL;
2318 1 : return false;
2319 : }
2320 : }
2321 :
2322 2179 : for (dim = 0; dim < sym->as->rank; ++dim)
2323 : {
2324 1141 : int k;
2325 1141 : gfc_expr *e, *lower;
2326 :
2327 1141 : lower = sym->as->lower[dim];
2328 :
2329 : /* If the lower bound is an array element from another
2330 : parameterized array, then it is marked with EXPR_VARIABLE and
2331 : is an initialization expression. Try to reduce it. */
2332 1141 : if (lower->expr_type == EXPR_VARIABLE)
2333 7 : gfc_reduce_init_expr (lower);
2334 :
2335 1141 : if (lower->expr_type == EXPR_CONSTANT)
2336 : {
2337 : /* All dimensions must be without upper bound. */
2338 1140 : gcc_assert (!sym->as->upper[dim]);
2339 :
2340 1140 : k = lower->ts.kind;
2341 1140 : e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2342 1140 : mpz_add (e->value.integer, lower->value.integer,
2343 1140 : init->shape[dim]);
2344 1140 : mpz_sub_ui (e->value.integer, e->value.integer, 1);
2345 1140 : sym->as->upper[dim] = e;
2346 : }
2347 : else
2348 : {
2349 1 : gfc_error ("Non-constant lower bound in implied-shape"
2350 : " declaration at %L", &lower->where);
2351 1 : return false;
2352 : }
2353 : }
2354 :
2355 1038 : sym->as->type = AS_EXPLICIT;
2356 : }
2357 :
2358 : /* Ensure that explicit bounds are simplified. */
2359 34194 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2360 3832 : && sym->as && sym->as->type == AS_EXPLICIT)
2361 : {
2362 8499 : for (int dim = 0; dim < sym->as->rank; ++dim)
2363 : {
2364 4679 : gfc_expr *e;
2365 :
2366 4679 : e = sym->as->lower[dim];
2367 4679 : if (e->expr_type != EXPR_CONSTANT)
2368 12 : gfc_reduce_init_expr (e);
2369 :
2370 4679 : e = sym->as->upper[dim];
2371 4679 : if (e->expr_type != EXPR_CONSTANT)
2372 106 : gfc_reduce_init_expr (e);
2373 : }
2374 : }
2375 :
2376 : /* Need to check if the expression we initialized this
2377 : to was one of the iso_c_binding named constants. If so,
2378 : and we're a parameter (constant), let it be iso_c.
2379 : For example:
2380 : integer(c_int), parameter :: my_int = c_int
2381 : integer(my_int) :: my_int_2
2382 : If we mark my_int as iso_c (since we can see it's value
2383 : is equal to one of the named constants), then my_int_2
2384 : will be considered C interoperable. */
2385 34194 : if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2386 : {
2387 29329 : sym->ts.is_iso_c |= init->ts.is_iso_c;
2388 29329 : sym->ts.is_c_interop |= init->ts.is_c_interop;
2389 : /* attr bits needed for module files. */
2390 29329 : sym->attr.is_iso_c |= init->ts.is_iso_c;
2391 29329 : sym->attr.is_c_interop |= init->ts.is_c_interop;
2392 29329 : if (init->ts.is_iso_c)
2393 120 : sym->ts.f90_type = init->ts.f90_type;
2394 : }
2395 :
2396 : /* Catch the case: type(t), parameter :: x = z'1'. */
2397 34194 : if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
2398 : {
2399 1 : gfc_error ("Entity %qs at %L is incompatible with a BOZ "
2400 : "literal constant", name, &sym->declared_at);
2401 1 : return false;
2402 : }
2403 :
2404 : /* Add initializer. Make sure we keep the ranks sane. */
2405 34193 : if (sym->attr.dimension && init->rank == 0)
2406 : {
2407 1319 : mpz_t size;
2408 1319 : gfc_expr *array;
2409 1319 : int n;
2410 1319 : if (sym->attr.flavor == FL_PARAMETER
2411 474 : && gfc_is_constant_expr (init)
2412 473 : && (init->expr_type == EXPR_CONSTANT
2413 48 : || init->expr_type == EXPR_STRUCTURE)
2414 1792 : && spec_size (sym->as, &size))
2415 : {
2416 469 : array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2417 : &init->where);
2418 469 : if (init->ts.type == BT_DERIVED)
2419 48 : array->ts.u.derived = init->ts.u.derived;
2420 67637 : for (n = 0; n < (int)mpz_get_si (size); n++)
2421 134008 : gfc_constructor_append_expr (&array->value.constructor,
2422 : n == 0
2423 : ? init
2424 66840 : : gfc_copy_expr (init),
2425 : &init->where);
2426 :
2427 469 : array->shape = gfc_get_shape (sym->as->rank);
2428 1064 : for (n = 0; n < sym->as->rank; n++)
2429 595 : spec_dimen_size (sym->as, n, &array->shape[n]);
2430 :
2431 469 : init = array;
2432 469 : mpz_clear (size);
2433 : }
2434 1319 : init->rank = sym->as->rank;
2435 1319 : init->corank = sym->as->corank;
2436 : }
2437 :
2438 34193 : sym->value = init;
2439 34193 : if (sym->attr.save == SAVE_NONE)
2440 29376 : sym->attr.save = SAVE_IMPLICIT;
2441 34193 : *initp = NULL;
2442 : }
2443 :
2444 : return true;
2445 : }
2446 :
2447 :
2448 : /* Function called by variable_decl() that adds a name to a structure
2449 : being built. */
2450 :
2451 : static bool
2452 20024 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2453 : gfc_array_spec **as)
2454 : {
2455 20024 : gfc_state_data *s;
2456 20024 : gfc_component *c;
2457 :
2458 : /* F03:C438/C439. If the current symbol is of the same derived type that we're
2459 : constructing, it must have the pointer attribute. */
2460 20024 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2461 3768 : && current_ts.u.derived == gfc_current_block ()
2462 297 : && current_attr.pointer == 0)
2463 : {
2464 136 : if (current_attr.allocatable
2465 136 : && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2466 : "must have the POINTER attribute"))
2467 : {
2468 : return false;
2469 : }
2470 135 : else if (current_attr.allocatable == 0)
2471 : {
2472 0 : gfc_error ("Component at %C must have the POINTER attribute");
2473 0 : return false;
2474 : }
2475 : }
2476 :
2477 : /* F03:C437. */
2478 20023 : if (current_ts.type == BT_CLASS
2479 985 : && !(current_attr.pointer || current_attr.allocatable))
2480 : {
2481 5 : gfc_error ("Component %qs with CLASS at %C must be allocatable "
2482 : "or pointer", name);
2483 5 : return false;
2484 : }
2485 :
2486 20018 : if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2487 : {
2488 0 : if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2489 : {
2490 0 : gfc_error ("Array component of structure at %C must have explicit "
2491 : "or deferred shape");
2492 0 : return false;
2493 : }
2494 : }
2495 :
2496 : /* If we are in a nested union/map definition, gfc_add_component will not
2497 : properly find repeated components because:
2498 : (i) gfc_add_component does a flat search, where components of unions
2499 : and maps are implicity chained so nested components may conflict.
2500 : (ii) Unions and maps are not linked as components of their parent
2501 : structures until after they are parsed.
2502 : For (i) we use gfc_find_component which searches recursively, and for (ii)
2503 : we search each block directly from the parse stack until we find the top
2504 : level structure. */
2505 :
2506 20018 : s = gfc_state_stack;
2507 20018 : if (s->state == COMP_UNION || s->state == COMP_MAP)
2508 : {
2509 1434 : while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2510 : {
2511 1434 : c = gfc_find_component (s->sym, name, true, true, NULL);
2512 1434 : if (c != NULL)
2513 : {
2514 0 : gfc_error_now ("Component %qs at %C already declared at %L",
2515 : name, &c->loc);
2516 0 : return false;
2517 : }
2518 : /* Break after we've searched the entire chain. */
2519 1434 : if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2520 : break;
2521 1000 : s = s->previous;
2522 : }
2523 : }
2524 :
2525 20018 : if (!gfc_add_component (gfc_current_block(), name, &c))
2526 : return false;
2527 :
2528 20012 : c->ts = current_ts;
2529 20012 : if (c->ts.type == BT_CHARACTER)
2530 : {
2531 2138 : c->ts.u.cl = cl;
2532 : /* The component struct is not tracked by the symbol undo mechanism,
2533 : so free the charlen here to prevent a double-free. */
2534 2138 : gfc_remove_saved_charlen (cl);
2535 : }
2536 :
2537 20012 : if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2538 16250 : && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2539 2462 : && saved_kind_expr != NULL)
2540 404 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
2541 :
2542 20012 : c->attr = current_attr;
2543 :
2544 20012 : c->initializer = *init;
2545 20012 : *init = NULL;
2546 :
2547 : /* Update initializer character length according to component. */
2548 2138 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
2549 1725 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
2550 1588 : && c->initializer && c->initializer->ts.type == BT_CHARACTER
2551 20379 : && !fix_initializer_charlen (&c->ts, c->initializer))
2552 : return false;
2553 :
2554 20012 : c->as = *as;
2555 20012 : if (c->as != NULL)
2556 : {
2557 5444 : if (c->as->corank)
2558 113 : c->attr.codimension = 1;
2559 5444 : if (c->as->rank)
2560 5363 : c->attr.dimension = 1;
2561 : }
2562 20012 : *as = NULL;
2563 :
2564 20012 : gfc_apply_init (&c->ts, &c->attr, c->initializer);
2565 :
2566 : /* Convert a class, PDT component of a non-derived type to a specific instance
2567 : before gfc_build_class_symbol gets to work on it. */
2568 20012 : if (c->ts.type == BT_CLASS
2569 980 : && !(gfc_current_block ()->attr.pdt_template
2570 980 : || gfc_current_block ()->attr.pdt_type)
2571 980 : && c->ts.u.derived->attr.pdt_template)
2572 : {
2573 12 : match m = gfc_get_pdt_instance (decl_type_param_list, &c->ts.u.derived, NULL);
2574 12 : if (m != MATCH_YES)
2575 : {
2576 0 : if (!gfc_error_check ())
2577 0 : gfc_error ("Parameterized component of a non-parameterized "
2578 : "derived type at %C could not be converted to a valid "
2579 : "instance");
2580 : return false;
2581 : }
2582 : }
2583 :
2584 : /* Check array components. */
2585 20012 : if (!c->attr.dimension)
2586 14649 : goto scalar;
2587 :
2588 5363 : if (c->attr.pointer)
2589 : {
2590 750 : if (c->as->type != AS_DEFERRED)
2591 : {
2592 5 : gfc_error ("Pointer array component of structure at %C must have a "
2593 : "deferred shape");
2594 5 : return false;
2595 : }
2596 : }
2597 4613 : else if (c->attr.allocatable)
2598 : {
2599 2573 : const char *err = G_("Allocatable component of structure at %C must have "
2600 : "a deferred shape");
2601 2573 : if (c->as->type != AS_DEFERRED)
2602 : {
2603 14 : if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
2604 : {
2605 : /* Issue an immediate error and allow this component to pass for
2606 : the sake of clean error recovery. Set the error flag for the
2607 : containing derived type so that finalizers are not built. */
2608 4 : gfc_error_now (err);
2609 4 : s->sym->error = 1;
2610 4 : c->as->type = AS_DEFERRED;
2611 : }
2612 : else
2613 : {
2614 10 : gfc_error (err);
2615 10 : return false;
2616 : }
2617 : }
2618 : }
2619 : else
2620 : {
2621 2040 : if (c->as->type != AS_EXPLICIT)
2622 : {
2623 7 : gfc_error ("Array component of structure at %C must have an "
2624 : "explicit shape");
2625 7 : return false;
2626 : }
2627 : }
2628 :
2629 2033 : scalar:
2630 19990 : if (c->ts.type == BT_CLASS)
2631 977 : return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2632 :
2633 19013 : if (c->attr.pdt_kind || c->attr.pdt_len)
2634 : {
2635 910 : gfc_symbol *sym;
2636 910 : gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2637 : 0, &sym);
2638 910 : if (sym == NULL)
2639 : {
2640 0 : gfc_error ("Type parameter %qs at %C has no corresponding entry "
2641 : "in the type parameter name list at %L",
2642 0 : c->name, &gfc_current_block ()->declared_at);
2643 0 : return false;
2644 : }
2645 910 : sym->ts = c->ts;
2646 910 : sym->attr.pdt_kind = c->attr.pdt_kind;
2647 910 : sym->attr.pdt_len = c->attr.pdt_len;
2648 910 : if (c->initializer)
2649 456 : sym->value = gfc_copy_expr (c->initializer);
2650 910 : sym->attr.flavor = FL_VARIABLE;
2651 : }
2652 :
2653 19013 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2654 2782 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2655 154 : && decl_type_param_list)
2656 154 : c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2657 :
2658 : return true;
2659 : }
2660 :
2661 :
2662 : /* Match a 'NULL()', and possibly take care of some side effects. */
2663 :
2664 : match
2665 1834 : gfc_match_null (gfc_expr **result)
2666 : {
2667 1834 : gfc_symbol *sym;
2668 1834 : match m, m2 = MATCH_NO;
2669 :
2670 1834 : if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2671 : return MATCH_ERROR;
2672 :
2673 1834 : if (m == MATCH_NO)
2674 : {
2675 565 : locus old_loc;
2676 565 : char name[GFC_MAX_SYMBOL_LEN + 1];
2677 :
2678 565 : if ((m2 = gfc_match (" null (")) != MATCH_YES)
2679 559 : return m2;
2680 :
2681 6 : old_loc = gfc_current_locus;
2682 6 : if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2683 : return MATCH_ERROR;
2684 6 : if (m2 != MATCH_YES
2685 6 : && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2686 : return MATCH_ERROR;
2687 6 : if (m2 == MATCH_NO)
2688 : {
2689 0 : gfc_current_locus = old_loc;
2690 0 : return MATCH_NO;
2691 : }
2692 : }
2693 :
2694 : /* The NULL symbol now has to be/become an intrinsic function. */
2695 1275 : if (gfc_get_symbol ("null", NULL, &sym))
2696 : {
2697 0 : gfc_error ("NULL() initialization at %C is ambiguous");
2698 0 : return MATCH_ERROR;
2699 : }
2700 :
2701 1275 : gfc_intrinsic_symbol (sym);
2702 :
2703 1275 : if (sym->attr.proc != PROC_INTRINSIC
2704 896 : && !(sym->attr.use_assoc && sym->attr.intrinsic)
2705 2170 : && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2706 895 : || !gfc_add_function (&sym->attr, sym->name, NULL)))
2707 : return MATCH_ERROR;
2708 :
2709 1275 : *result = gfc_get_null_expr (&gfc_current_locus);
2710 :
2711 : /* Invalid per F2008, C512. */
2712 1275 : if (m2 == MATCH_YES)
2713 : {
2714 6 : gfc_error ("NULL() initialization at %C may not have MOLD");
2715 6 : return MATCH_ERROR;
2716 : }
2717 :
2718 : return MATCH_YES;
2719 : }
2720 :
2721 :
2722 : /* Match the initialization expr for a data pointer or procedure pointer. */
2723 :
2724 : static match
2725 1480 : match_pointer_init (gfc_expr **init, int procptr)
2726 : {
2727 1480 : match m;
2728 :
2729 1480 : if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2730 : {
2731 1 : gfc_error ("Initialization of pointer at %C is not allowed in "
2732 : "a PURE procedure");
2733 1 : return MATCH_ERROR;
2734 : }
2735 1479 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2736 :
2737 : /* Match NULL() initialization. */
2738 1479 : m = gfc_match_null (init);
2739 1479 : if (m != MATCH_NO)
2740 : return m;
2741 :
2742 : /* Match non-NULL initialization. */
2743 212 : gfc_matching_ptr_assignment = !procptr;
2744 212 : gfc_matching_procptr_assignment = procptr;
2745 212 : m = gfc_match_rvalue (init);
2746 212 : gfc_matching_ptr_assignment = 0;
2747 212 : gfc_matching_procptr_assignment = 0;
2748 212 : if (m == MATCH_ERROR)
2749 : return MATCH_ERROR;
2750 211 : else if (m == MATCH_NO)
2751 : {
2752 2 : gfc_error ("Error in pointer initialization at %C");
2753 2 : return MATCH_ERROR;
2754 : }
2755 :
2756 209 : if (!procptr && !gfc_resolve_expr (*init))
2757 : return MATCH_ERROR;
2758 :
2759 208 : if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2760 : "initialization at %C"))
2761 0 : return MATCH_ERROR;
2762 :
2763 : return MATCH_YES;
2764 : }
2765 :
2766 :
2767 : static bool
2768 299767 : check_function_name (char *name)
2769 : {
2770 : /* In functions that have a RESULT variable defined, the function name always
2771 : refers to function calls. Therefore, the name is not allowed to appear in
2772 : specification statements. When checking this, be careful about
2773 : 'hidden' procedure pointer results ('ppr@'). */
2774 :
2775 299767 : if (gfc_current_state () == COMP_FUNCTION)
2776 : {
2777 49153 : gfc_symbol *block = gfc_current_block ();
2778 49153 : if (block && block->result && block->result != block
2779 15993 : && strcmp (block->result->name, "ppr@") != 0
2780 15934 : && strcmp (block->name, name) == 0)
2781 : {
2782 9 : gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2783 : "from appearing in a specification statement",
2784 : block->result->name, &block->result->declared_at, name);
2785 9 : return false;
2786 : }
2787 : }
2788 :
2789 : return true;
2790 : }
2791 :
2792 :
2793 : /* Match a variable name with an optional initializer. When this
2794 : subroutine is called, a variable is expected to be parsed next.
2795 : Depending on what is happening at the moment, updates either the
2796 : symbol table or the current interface. */
2797 :
2798 : static match
2799 289533 : variable_decl (int elem)
2800 : {
2801 289533 : char name[GFC_MAX_SYMBOL_LEN + 1];
2802 289533 : static unsigned int fill_id = 0;
2803 289533 : gfc_expr *initializer, *char_len;
2804 289533 : gfc_array_spec *as;
2805 289533 : gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2806 289533 : gfc_charlen *cl;
2807 289533 : gfc_charlen *saved_cl_list;
2808 289533 : bool cl_deferred;
2809 289533 : locus var_locus;
2810 289533 : match m;
2811 289533 : bool t;
2812 289533 : gfc_symbol *sym;
2813 289533 : char c;
2814 :
2815 289533 : initializer = NULL;
2816 289533 : as = NULL;
2817 289533 : cp_as = NULL;
2818 289533 : saved_cl_list = gfc_current_ns->cl_list;
2819 :
2820 : /* When we get here, we've just matched a list of attributes and
2821 : maybe a type and a double colon. The next thing we expect to see
2822 : is the name of the symbol. */
2823 :
2824 : /* If we are parsing a structure with legacy support, we allow the symbol
2825 : name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2826 289533 : m = MATCH_NO;
2827 289533 : gfc_gobble_whitespace ();
2828 289533 : var_locus = gfc_current_locus;
2829 289533 : c = gfc_peek_ascii_char ();
2830 289533 : if (c == '%')
2831 : {
2832 12 : gfc_next_ascii_char (); /* Burn % character. */
2833 12 : m = gfc_match ("fill");
2834 12 : if (m == MATCH_YES)
2835 : {
2836 11 : if (gfc_current_state () != COMP_STRUCTURE)
2837 : {
2838 2 : if (flag_dec_structure)
2839 1 : gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2840 : else
2841 1 : gfc_error ("%qs at %C is a DEC extension, enable with "
2842 : "%<-fdec-structure%>", "%FILL");
2843 2 : m = MATCH_ERROR;
2844 2 : goto cleanup;
2845 : }
2846 :
2847 9 : if (attr_seen)
2848 : {
2849 1 : gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2850 1 : m = MATCH_ERROR;
2851 1 : goto cleanup;
2852 : }
2853 :
2854 : /* %FILL components are given invalid fortran names. */
2855 8 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2856 : }
2857 : else
2858 : {
2859 1 : gfc_error ("Invalid character %qc in variable name at %C", c);
2860 1 : return MATCH_ERROR;
2861 : }
2862 : }
2863 : else
2864 : {
2865 289521 : m = gfc_match_name (name);
2866 289520 : if (m != MATCH_YES)
2867 10 : goto cleanup;
2868 : }
2869 :
2870 : /* Now we could see the optional array spec. or character length. */
2871 289518 : m = gfc_match_array_spec (&as, true, true);
2872 289517 : if (m == MATCH_ERROR)
2873 57 : goto cleanup;
2874 :
2875 289460 : if (m == MATCH_NO)
2876 225872 : as = gfc_copy_array_spec (current_as);
2877 63588 : else if (current_as
2878 63588 : && !merge_array_spec (current_as, as, true))
2879 : {
2880 4 : m = MATCH_ERROR;
2881 4 : goto cleanup;
2882 : }
2883 :
2884 289456 : var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
2885 : &gfc_current_locus);
2886 289456 : if (flag_cray_pointer)
2887 3063 : cp_as = gfc_copy_array_spec (as);
2888 :
2889 : /* At this point, we know for sure if the symbol is PARAMETER and can thus
2890 : determine (and check) whether it can be implied-shape. If it
2891 : was parsed as assumed-size, change it because PARAMETERs cannot
2892 : be assumed-size.
2893 :
2894 : An explicit-shape-array cannot appear under several conditions.
2895 : That check is done here as well. */
2896 289456 : if (as)
2897 : {
2898 86485 : if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2899 : {
2900 2 : m = MATCH_ERROR;
2901 2 : gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2902 : name, &var_locus);
2903 2 : goto cleanup;
2904 : }
2905 :
2906 86483 : if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2907 6530 : && current_attr.flavor == FL_PARAMETER)
2908 995 : as->type = AS_IMPLIED_SHAPE;
2909 :
2910 86483 : if (as->type == AS_IMPLIED_SHAPE
2911 86483 : && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2912 : &var_locus))
2913 : {
2914 1 : m = MATCH_ERROR;
2915 1 : goto cleanup;
2916 : }
2917 :
2918 86482 : gfc_seen_div0 = false;
2919 :
2920 : /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2921 : constant expressions shall appear only in a subprogram, derived
2922 : type definition, BLOCK construct, or interface body. */
2923 86482 : if (as->type == AS_EXPLICIT
2924 43110 : && gfc_current_state () != COMP_BLOCK
2925 : && gfc_current_state () != COMP_DERIVED
2926 : && gfc_current_state () != COMP_FUNCTION
2927 : && gfc_current_state () != COMP_INTERFACE
2928 : && gfc_current_state () != COMP_SUBROUTINE)
2929 : {
2930 : gfc_expr *e;
2931 50824 : bool not_constant = false;
2932 :
2933 50824 : for (int i = 0; i < as->rank; i++)
2934 : {
2935 28902 : e = gfc_copy_expr (as->lower[i]);
2936 28902 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2937 : {
2938 0 : m = MATCH_ERROR;
2939 0 : goto cleanup;
2940 : }
2941 :
2942 28902 : gfc_simplify_expr (e, 0);
2943 28902 : if (e && (e->expr_type != EXPR_CONSTANT))
2944 : {
2945 : not_constant = true;
2946 : break;
2947 : }
2948 28902 : gfc_free_expr (e);
2949 :
2950 28902 : e = gfc_copy_expr (as->upper[i]);
2951 28902 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2952 : {
2953 4 : m = MATCH_ERROR;
2954 4 : goto cleanup;
2955 : }
2956 :
2957 28898 : gfc_simplify_expr (e, 0);
2958 28898 : if (e && (e->expr_type != EXPR_CONSTANT))
2959 : {
2960 : not_constant = true;
2961 : break;
2962 : }
2963 28885 : gfc_free_expr (e);
2964 : }
2965 :
2966 21935 : if (not_constant && e->ts.type != BT_INTEGER)
2967 : {
2968 4 : gfc_error ("Explicit array shape at %C must be constant of "
2969 : "INTEGER type and not %s type",
2970 : gfc_basic_typename (e->ts.type));
2971 4 : m = MATCH_ERROR;
2972 4 : goto cleanup;
2973 : }
2974 9 : if (not_constant)
2975 : {
2976 9 : gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2977 9 : m = MATCH_ERROR;
2978 9 : goto cleanup;
2979 : }
2980 : }
2981 86465 : if (as->type == AS_EXPLICIT)
2982 : {
2983 102807 : for (int i = 0; i < as->rank; i++)
2984 : {
2985 59714 : gfc_expr *e, *n;
2986 59714 : e = as->lower[i];
2987 59714 : if (e->expr_type != EXPR_CONSTANT)
2988 : {
2989 458 : n = gfc_copy_expr (e);
2990 458 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2991 : {
2992 0 : m = MATCH_ERROR;
2993 0 : goto cleanup;
2994 : }
2995 :
2996 458 : if (n->expr_type == EXPR_CONSTANT)
2997 22 : gfc_replace_expr (e, n);
2998 : else
2999 436 : gfc_free_expr (n);
3000 : }
3001 59714 : e = as->upper[i];
3002 59714 : if (e->expr_type != EXPR_CONSTANT)
3003 : {
3004 7011 : n = gfc_copy_expr (e);
3005 7011 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
3006 : {
3007 0 : m = MATCH_ERROR;
3008 0 : goto cleanup;
3009 : }
3010 :
3011 7011 : if (n->expr_type == EXPR_CONSTANT)
3012 45 : gfc_replace_expr (e, n);
3013 : else
3014 6966 : gfc_free_expr (n);
3015 : }
3016 : /* For an explicit-shape spec with constant bounds, ensure
3017 : that the effective upper bound is not lower than the
3018 : respective lower bound minus one. Otherwise adjust it so
3019 : that the extent is trivially derived to be zero. */
3020 59714 : if (as->lower[i]->expr_type == EXPR_CONSTANT
3021 59278 : && as->upper[i]->expr_type == EXPR_CONSTANT
3022 52736 : && as->lower[i]->ts.type == BT_INTEGER
3023 52736 : && as->upper[i]->ts.type == BT_INTEGER
3024 52731 : && mpz_cmp (as->upper[i]->value.integer,
3025 52731 : as->lower[i]->value.integer) < 0)
3026 1218 : mpz_sub_ui (as->upper[i]->value.integer,
3027 : as->lower[i]->value.integer, 1);
3028 : }
3029 : }
3030 : }
3031 :
3032 289436 : char_len = NULL;
3033 289436 : cl = NULL;
3034 289436 : cl_deferred = false;
3035 :
3036 289436 : if (current_ts.type == BT_CHARACTER)
3037 : {
3038 31871 : switch (match_char_length (&char_len, &cl_deferred, false))
3039 : {
3040 435 : case MATCH_YES:
3041 435 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3042 :
3043 435 : cl->length = char_len;
3044 435 : break;
3045 :
3046 : /* Non-constant lengths need to be copied after the first
3047 : element. Also copy assumed lengths. */
3048 31435 : case MATCH_NO:
3049 31435 : if (elem > 1
3050 4007 : && (current_ts.u.cl->length == NULL
3051 2775 : || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
3052 : {
3053 1287 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3054 1287 : cl->length = gfc_copy_expr (current_ts.u.cl->length);
3055 : }
3056 : else
3057 30148 : cl = current_ts.u.cl;
3058 :
3059 31435 : cl_deferred = current_ts.deferred;
3060 :
3061 31435 : break;
3062 :
3063 1 : case MATCH_ERROR:
3064 1 : goto cleanup;
3065 : }
3066 : }
3067 :
3068 : /* The dummy arguments and result of the abbreviated form of MODULE
3069 : PROCEDUREs, used in SUBMODULES should not be redefined. */
3070 289435 : if (gfc_current_ns->proc_name
3071 284948 : && gfc_current_ns->proc_name->abr_modproc_decl)
3072 : {
3073 50 : gfc_find_symbol (name, gfc_current_ns, 1, &sym);
3074 50 : if (sym != NULL && (sym->attr.dummy || sym->attr.result))
3075 : {
3076 2 : m = MATCH_ERROR;
3077 2 : gfc_error ("%qs at %L is a redefinition of the declaration "
3078 : "in the corresponding interface for MODULE "
3079 : "PROCEDURE %qs", sym->name, &var_locus,
3080 2 : gfc_current_ns->proc_name->name);
3081 2 : goto cleanup;
3082 : }
3083 : }
3084 :
3085 : /* %FILL components may not have initializers. */
3086 289433 : if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
3087 : {
3088 1 : gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
3089 : &var_locus);
3090 1 : m = MATCH_ERROR;
3091 1 : goto cleanup;
3092 : }
3093 :
3094 : /* If this symbol has already shown up in a Cray Pointer declaration,
3095 : and this is not a component declaration,
3096 : then we want to set the type & bail out. */
3097 289432 : if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
3098 : {
3099 2959 : gfc_find_symbol (name, gfc_current_ns, 0, &sym);
3100 2959 : if (sym != NULL && sym->attr.cray_pointee)
3101 : {
3102 101 : m = MATCH_YES;
3103 101 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
3104 : {
3105 1 : m = MATCH_ERROR;
3106 1 : goto cleanup;
3107 : }
3108 :
3109 : /* Check to see if we have an array specification. */
3110 100 : if (cp_as != NULL)
3111 : {
3112 49 : if (sym->as != NULL)
3113 : {
3114 1 : gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
3115 1 : gfc_free_array_spec (cp_as);
3116 1 : m = MATCH_ERROR;
3117 1 : goto cleanup;
3118 : }
3119 : else
3120 : {
3121 48 : if (!gfc_set_array_spec (sym, cp_as, &var_locus))
3122 0 : gfc_internal_error ("Cannot set pointee array spec.");
3123 :
3124 : /* Fix the array spec. */
3125 48 : m = gfc_mod_pointee_as (sym->as);
3126 48 : if (m == MATCH_ERROR)
3127 0 : goto cleanup;
3128 : }
3129 : }
3130 99 : goto cleanup;
3131 : }
3132 : else
3133 : {
3134 2858 : gfc_free_array_spec (cp_as);
3135 : }
3136 : }
3137 : else
3138 : {
3139 : /* Check to see if this is the declaration of the type and/or attributes
3140 : of an implicit function result, emanating from a module function
3141 : interface declared within the parent module or submodule of a
3142 : containing submodule. */
3143 286473 : gfc_find_symbol (name, gfc_current_ns, 0, &sym);
3144 286473 : if (gfc_current_state () == COMP_FUNCTION
3145 47671 : && sym == gfc_current_block ()
3146 8392 : && sym->attr.if_source == IFSRC_DECL
3147 4952 : && sym->attr.used_in_submodule
3148 4 : && sym == sym->result
3149 4 : && sym->ts.type != BT_UNKNOWN)
3150 : {
3151 4 : m = MATCH_YES;
3152 4 : goto cleanup;
3153 : }
3154 286469 : sym = NULL;
3155 : }
3156 :
3157 : /* Procedure pointer as function result. */
3158 289327 : if (gfc_current_state () == COMP_FUNCTION
3159 47781 : && strcmp ("ppr@", gfc_current_block ()->name) == 0
3160 25 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
3161 7 : strcpy (name, "ppr@");
3162 :
3163 289327 : if (gfc_current_state () == COMP_FUNCTION
3164 47781 : && strcmp (name, gfc_current_block ()->name) == 0
3165 8408 : && gfc_current_block ()->result
3166 8408 : && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
3167 16 : strcpy (name, "ppr@");
3168 :
3169 : /* OK, we've successfully matched the declaration. Now put the
3170 : symbol in the current namespace, because it might be used in the
3171 : optional initialization expression for this symbol, e.g. this is
3172 : perfectly legal:
3173 :
3174 : integer, parameter :: i = huge(i)
3175 :
3176 : This is only true for parameters or variables of a basic type.
3177 : For components of derived types, it is not true, so we don't
3178 : create a symbol for those yet. If we fail to create the symbol,
3179 : bail out. */
3180 289327 : if (!gfc_comp_struct (gfc_current_state ())
3181 269274 : && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
3182 : {
3183 46 : m = MATCH_ERROR;
3184 46 : goto cleanup;
3185 : }
3186 :
3187 289281 : if (!check_function_name (name))
3188 : {
3189 0 : m = MATCH_ERROR;
3190 0 : goto cleanup;
3191 : }
3192 :
3193 : /* We allow old-style initializations of the form
3194 : integer i /2/, j(4) /3*3, 1/
3195 : (if no colon has been seen). These are different from data
3196 : statements in that initializers are only allowed to apply to the
3197 : variable immediately preceding, i.e.
3198 : integer i, j /1, 2/
3199 : is not allowed. Therefore we have to do some work manually, that
3200 : could otherwise be left to the matchers for DATA statements. */
3201 :
3202 289281 : if (!colon_seen && gfc_match (" /") == MATCH_YES)
3203 : {
3204 146 : if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
3205 : "initialization at %C"))
3206 : return MATCH_ERROR;
3207 :
3208 : /* Allow old style initializations for components of STRUCTUREs and MAPs
3209 : but not components of derived types. */
3210 146 : else if (gfc_current_state () == COMP_DERIVED)
3211 : {
3212 2 : gfc_error ("Invalid old style initialization for derived type "
3213 : "component at %C");
3214 2 : m = MATCH_ERROR;
3215 2 : goto cleanup;
3216 : }
3217 :
3218 : /* For structure components, read the initializer as a special
3219 : expression and let the rest of this function apply the initializer
3220 : as usual. */
3221 144 : else if (gfc_comp_struct (gfc_current_state ()))
3222 : {
3223 74 : m = match_clist_expr (&initializer, ¤t_ts, as);
3224 74 : if (m == MATCH_NO)
3225 : gfc_error ("Syntax error in old style initialization of %s at %C",
3226 : name);
3227 74 : if (m != MATCH_YES)
3228 14 : goto cleanup;
3229 : }
3230 :
3231 : /* Otherwise we treat the old style initialization just like a
3232 : DATA declaration for the current variable. */
3233 : else
3234 70 : return match_old_style_init (name);
3235 : }
3236 :
3237 : /* The double colon must be present in order to have initializers.
3238 : Otherwise the statement is ambiguous with an assignment statement. */
3239 289195 : if (colon_seen)
3240 : {
3241 242763 : if (gfc_match (" =>") == MATCH_YES)
3242 : {
3243 1269 : if (!current_attr.pointer)
3244 : {
3245 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
3246 0 : m = MATCH_ERROR;
3247 0 : goto cleanup;
3248 : }
3249 :
3250 1269 : m = match_pointer_init (&initializer, 0);
3251 1269 : if (m != MATCH_YES)
3252 10 : goto cleanup;
3253 :
3254 : /* The target of a pointer initialization must have the SAVE
3255 : attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
3256 : is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
3257 1259 : if (initializer->expr_type == EXPR_VARIABLE
3258 164 : && initializer->symtree->n.sym->attr.save == SAVE_NONE
3259 25 : && (gfc_current_state () == COMP_PROGRAM
3260 : || gfc_current_state () == COMP_MODULE
3261 25 : || gfc_current_state () == COMP_SUBMODULE))
3262 11 : initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
3263 : }
3264 241494 : else if (gfc_match_char ('=') == MATCH_YES)
3265 : {
3266 27378 : if (current_attr.pointer)
3267 : {
3268 0 : gfc_error ("Pointer initialization at %C requires %<=>%>, "
3269 : "not %<=%>");
3270 0 : m = MATCH_ERROR;
3271 0 : goto cleanup;
3272 : }
3273 :
3274 27378 : if (gfc_comp_struct (gfc_current_state ())
3275 3001 : && gfc_current_block ()->attr.pdt_template)
3276 : {
3277 509 : m = gfc_match_expr (&initializer);
3278 509 : if (initializer && initializer->ts.type == BT_UNKNOWN)
3279 139 : initializer->ts = current_ts;
3280 : }
3281 : else
3282 26869 : m = gfc_match_init_expr (&initializer);
3283 :
3284 27378 : if (m == MATCH_NO)
3285 : {
3286 1 : gfc_error ("Expected an initialization expression at %C");
3287 1 : m = MATCH_ERROR;
3288 : }
3289 :
3290 10976 : if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
3291 27380 : && !gfc_comp_struct (gfc_state_stack->state))
3292 : {
3293 1 : gfc_error ("Initialization of variable at %C is not allowed in "
3294 : "a PURE procedure");
3295 1 : m = MATCH_ERROR;
3296 : }
3297 :
3298 27378 : if (current_attr.flavor != FL_PARAMETER
3299 10976 : && !gfc_comp_struct (gfc_state_stack->state))
3300 7975 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
3301 :
3302 27378 : if (m != MATCH_YES)
3303 160 : goto cleanup;
3304 : }
3305 : }
3306 :
3307 289025 : if (initializer != NULL && current_attr.allocatable
3308 3 : && gfc_comp_struct (gfc_current_state ()))
3309 : {
3310 2 : gfc_error ("Initialization of allocatable component at %C is not "
3311 : "allowed");
3312 2 : m = MATCH_ERROR;
3313 2 : goto cleanup;
3314 : }
3315 :
3316 289023 : if (gfc_current_state () == COMP_DERIVED
3317 19011 : && initializer && initializer->ts.type == BT_HOLLERITH)
3318 : {
3319 1 : gfc_error ("Initialization of structure component with a HOLLERITH "
3320 : "constant at %L is not allowed", &initializer->where);
3321 1 : m = MATCH_ERROR;
3322 1 : goto cleanup;
3323 : }
3324 :
3325 289022 : if (gfc_current_state () == COMP_DERIVED
3326 19010 : && gfc_current_block ()->attr.pdt_template)
3327 : {
3328 1878 : gfc_symbol *param;
3329 1878 : gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
3330 : 0, ¶m);
3331 1878 : if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
3332 : {
3333 1 : gfc_error ("The component with KIND or LEN attribute at %C does not "
3334 : "not appear in the type parameter list at %L",
3335 1 : &gfc_current_block ()->declared_at);
3336 1 : m = MATCH_ERROR;
3337 4 : goto cleanup;
3338 : }
3339 1877 : else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
3340 : {
3341 1 : gfc_error ("The component at %C that appears in the type parameter "
3342 : "list at %L has neither the KIND nor LEN attribute",
3343 1 : &gfc_current_block ()->declared_at);
3344 1 : m = MATCH_ERROR;
3345 1 : goto cleanup;
3346 : }
3347 1876 : else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
3348 : {
3349 1 : gfc_error ("The component at %C which is a type parameter must be "
3350 : "a scalar");
3351 1 : m = MATCH_ERROR;
3352 1 : goto cleanup;
3353 : }
3354 1875 : else if (param && initializer)
3355 : {
3356 457 : if (initializer->ts.type == BT_BOZ)
3357 : {
3358 1 : gfc_error ("BOZ literal constant at %L cannot appear as an "
3359 : "initializer", &initializer->where);
3360 1 : m = MATCH_ERROR;
3361 1 : goto cleanup;
3362 : }
3363 456 : param->value = gfc_copy_expr (initializer);
3364 : }
3365 : }
3366 :
3367 : /* Before adding a possible initializer, do a simple check for compatibility
3368 : of lhs and rhs types. Assigning a REAL value to a derived type is not a
3369 : good thing. */
3370 29984 : if (current_ts.type == BT_DERIVED && initializer
3371 290563 : && (gfc_numeric_ts (&initializer->ts)
3372 1543 : || initializer->ts.type == BT_LOGICAL
3373 1543 : || initializer->ts.type == BT_CHARACTER))
3374 : {
3375 2 : gfc_error ("Incompatible initialization between a derived type "
3376 : "entity and an entity with %qs type at %C",
3377 : gfc_typename (initializer));
3378 2 : m = MATCH_ERROR;
3379 2 : goto cleanup;
3380 : }
3381 :
3382 :
3383 : /* Add the initializer. Note that it is fine if initializer is
3384 : NULL here, because we sometimes also need to check if a
3385 : declaration *must* have an initialization expression. */
3386 289016 : if (!gfc_comp_struct (gfc_current_state ()))
3387 268992 : t = add_init_expr_to_sym (name, &initializer, &var_locus,
3388 : saved_cl_list);
3389 : else
3390 : {
3391 20024 : if (current_ts.type == BT_DERIVED
3392 2782 : && !current_attr.pointer && !initializer)
3393 2217 : initializer = gfc_default_initializer (¤t_ts);
3394 20024 : t = build_struct (name, cl, &initializer, &as);
3395 :
3396 : /* If we match a nested structure definition we expect to see the
3397 : * body even if the variable declarations blow up, so we need to keep
3398 : * the structure declaration around. */
3399 20024 : if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
3400 34 : gfc_commit_symbol (gfc_new_block);
3401 : }
3402 :
3403 289016 : m = (t) ? MATCH_YES : MATCH_ERROR;
3404 :
3405 289460 : cleanup:
3406 : /* Free stuff up and return. */
3407 289460 : gfc_seen_div0 = false;
3408 289460 : gfc_free_expr (initializer);
3409 289460 : gfc_free_array_spec (as);
3410 :
3411 289460 : return m;
3412 : }
3413 :
3414 :
3415 : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
3416 : This assumes that the byte size is equal to the kind number for
3417 : non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
3418 :
3419 : static match
3420 110969 : gfc_match_old_kind_spec (gfc_typespec *ts)
3421 : {
3422 110969 : match m;
3423 110969 : int original_kind;
3424 :
3425 110969 : if (gfc_match_char ('*') != MATCH_YES)
3426 : return MATCH_NO;
3427 :
3428 1150 : m = gfc_match_small_literal_int (&ts->kind, NULL);
3429 1150 : if (m != MATCH_YES)
3430 : return MATCH_ERROR;
3431 :
3432 1150 : original_kind = ts->kind;
3433 :
3434 : /* Massage the kind numbers for complex types. */
3435 1150 : if (ts->type == BT_COMPLEX)
3436 : {
3437 79 : if (ts->kind % 2)
3438 : {
3439 0 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3440 : gfc_basic_typename (ts->type), original_kind);
3441 0 : return MATCH_ERROR;
3442 : }
3443 79 : ts->kind /= 2;
3444 :
3445 : }
3446 :
3447 1150 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3448 0 : ts->kind = 8;
3449 :
3450 1150 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3451 : {
3452 858 : if (ts->kind == 4)
3453 : {
3454 224 : if (flag_real4_kind == 8)
3455 24 : ts->kind = 8;
3456 224 : if (flag_real4_kind == 10)
3457 24 : ts->kind = 10;
3458 224 : if (flag_real4_kind == 16)
3459 24 : ts->kind = 16;
3460 : }
3461 634 : else if (ts->kind == 8)
3462 : {
3463 629 : if (flag_real8_kind == 4)
3464 24 : ts->kind = 4;
3465 629 : if (flag_real8_kind == 10)
3466 24 : ts->kind = 10;
3467 629 : if (flag_real8_kind == 16)
3468 24 : ts->kind = 16;
3469 : }
3470 : }
3471 :
3472 1150 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3473 : {
3474 8 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3475 : gfc_basic_typename (ts->type), original_kind);
3476 8 : return MATCH_ERROR;
3477 : }
3478 :
3479 1142 : if (!gfc_notify_std (GFC_STD_GNU,
3480 : "Nonstandard type declaration %s*%d at %C",
3481 : gfc_basic_typename(ts->type), original_kind))
3482 0 : return MATCH_ERROR;
3483 :
3484 : return MATCH_YES;
3485 : }
3486 :
3487 :
3488 : /* Match a kind specification. Since kinds are generally optional, we
3489 : usually return MATCH_NO if something goes wrong. If a "kind="
3490 : string is found, then we know we have an error. */
3491 :
3492 : match
3493 165505 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3494 : {
3495 165505 : locus where, loc;
3496 165505 : gfc_expr *e;
3497 165505 : match m, n;
3498 165505 : char c;
3499 :
3500 165505 : m = MATCH_NO;
3501 165505 : n = MATCH_YES;
3502 165505 : e = NULL;
3503 165505 : saved_kind_expr = NULL;
3504 :
3505 165505 : where = loc = gfc_current_locus;
3506 :
3507 165505 : if (kind_expr_only)
3508 0 : goto kind_expr;
3509 :
3510 165505 : if (gfc_match_char ('(') == MATCH_NO)
3511 : return MATCH_NO;
3512 :
3513 : /* Also gobbles optional text. */
3514 52902 : if (gfc_match (" kind = ") == MATCH_YES)
3515 52902 : m = MATCH_ERROR;
3516 :
3517 52902 : loc = gfc_current_locus;
3518 :
3519 52902 : kind_expr:
3520 :
3521 52902 : n = gfc_match_init_expr (&e);
3522 :
3523 52902 : if (gfc_derived_parameter_expr (e))
3524 : {
3525 304 : ts->kind = 0;
3526 304 : saved_kind_expr = gfc_copy_expr (e);
3527 304 : goto close_brackets;
3528 : }
3529 :
3530 52598 : if (n != MATCH_YES)
3531 : {
3532 522 : if (gfc_matching_function)
3533 : {
3534 : /* The function kind expression might include use associated or
3535 : imported parameters and try again after the specification
3536 : expressions..... */
3537 494 : if (gfc_match_char (')') != MATCH_YES)
3538 : {
3539 1 : gfc_error ("Missing right parenthesis at %C");
3540 1 : m = MATCH_ERROR;
3541 1 : goto no_match;
3542 : }
3543 :
3544 493 : gfc_free_expr (e);
3545 493 : gfc_undo_symbols ();
3546 493 : return MATCH_YES;
3547 : }
3548 : else
3549 : {
3550 : /* ....or else, the match is real. */
3551 28 : if (n == MATCH_NO)
3552 0 : gfc_error ("Expected initialization expression at %C");
3553 : if (n != MATCH_YES)
3554 : return MATCH_ERROR;
3555 : }
3556 : }
3557 :
3558 52076 : if (e->rank != 0)
3559 : {
3560 0 : gfc_error ("Expected scalar initialization expression at %C");
3561 0 : m = MATCH_ERROR;
3562 0 : goto no_match;
3563 : }
3564 :
3565 52076 : if (gfc_extract_int (e, &ts->kind, 1))
3566 : {
3567 0 : m = MATCH_ERROR;
3568 0 : goto no_match;
3569 : }
3570 :
3571 : /* Before throwing away the expression, let's see if we had a
3572 : C interoperable kind (and store the fact). */
3573 52076 : if (e->ts.is_c_interop == 1)
3574 : {
3575 : /* Mark this as C interoperable if being declared with one
3576 : of the named constants from iso_c_binding. */
3577 19342 : ts->is_c_interop = e->ts.is_iso_c;
3578 19342 : ts->f90_type = e->ts.f90_type;
3579 19342 : if (e->symtree)
3580 19341 : ts->interop_kind = e->symtree->n.sym;
3581 : }
3582 :
3583 52076 : gfc_free_expr (e);
3584 52076 : e = NULL;
3585 :
3586 : /* Ignore errors to this point, if we've gotten here. This means
3587 : we ignore the m=MATCH_ERROR from above. */
3588 52076 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3589 : {
3590 7 : gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3591 : gfc_basic_typename (ts->type));
3592 7 : gfc_current_locus = where;
3593 7 : return MATCH_ERROR;
3594 : }
3595 :
3596 : /* Warn if, e.g., c_int is used for a REAL variable, but not
3597 : if, e.g., c_double is used for COMPLEX as the standard
3598 : explicitly says that the kind type parameter for complex and real
3599 : variable is the same, i.e. c_float == c_float_complex. */
3600 52069 : if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3601 17 : && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3602 1 : || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3603 13 : gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3604 : "is %s", gfc_basic_typename (ts->f90_type), &where,
3605 : gfc_basic_typename (ts->type));
3606 :
3607 52056 : close_brackets:
3608 :
3609 52373 : gfc_gobble_whitespace ();
3610 52373 : if ((c = gfc_next_ascii_char ()) != ')'
3611 52373 : && (ts->type != BT_CHARACTER || c != ','))
3612 : {
3613 0 : if (ts->type == BT_CHARACTER)
3614 0 : gfc_error ("Missing right parenthesis or comma at %C");
3615 : else
3616 0 : gfc_error ("Missing right parenthesis at %C");
3617 0 : m = MATCH_ERROR;
3618 0 : goto no_match;
3619 : }
3620 : else
3621 : /* All tests passed. */
3622 52373 : m = MATCH_YES;
3623 :
3624 52373 : if(m == MATCH_ERROR)
3625 : gfc_current_locus = where;
3626 :
3627 52373 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3628 0 : ts->kind = 8;
3629 :
3630 52373 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3631 : {
3632 14643 : if (ts->kind == 4)
3633 : {
3634 4618 : if (flag_real4_kind == 8)
3635 54 : ts->kind = 8;
3636 4618 : if (flag_real4_kind == 10)
3637 54 : ts->kind = 10;
3638 4618 : if (flag_real4_kind == 16)
3639 54 : ts->kind = 16;
3640 : }
3641 10025 : else if (ts->kind == 8)
3642 : {
3643 6785 : if (flag_real8_kind == 4)
3644 48 : ts->kind = 4;
3645 6785 : if (flag_real8_kind == 10)
3646 48 : ts->kind = 10;
3647 6785 : if (flag_real8_kind == 16)
3648 48 : ts->kind = 16;
3649 : }
3650 : }
3651 :
3652 : /* Return what we know from the test(s). */
3653 : return m;
3654 :
3655 1 : no_match:
3656 1 : gfc_free_expr (e);
3657 1 : gfc_current_locus = where;
3658 1 : return m;
3659 : }
3660 :
3661 :
3662 : static match
3663 5056 : match_char_kind (int * kind, int * is_iso_c)
3664 : {
3665 5056 : locus where;
3666 5056 : gfc_expr *e;
3667 5056 : match m, n;
3668 5056 : bool fail;
3669 :
3670 5056 : m = MATCH_NO;
3671 5056 : e = NULL;
3672 5056 : where = gfc_current_locus;
3673 :
3674 5056 : n = gfc_match_init_expr (&e);
3675 :
3676 5056 : if (n != MATCH_YES && gfc_matching_function)
3677 : {
3678 : /* The expression might include use-associated or imported
3679 : parameters and try again after the specification
3680 : expressions. */
3681 7 : gfc_free_expr (e);
3682 7 : gfc_undo_symbols ();
3683 7 : return MATCH_YES;
3684 : }
3685 :
3686 7 : if (n == MATCH_NO)
3687 2 : gfc_error ("Expected initialization expression at %C");
3688 5049 : if (n != MATCH_YES)
3689 : return MATCH_ERROR;
3690 :
3691 5042 : if (e->rank != 0)
3692 : {
3693 0 : gfc_error ("Expected scalar initialization expression at %C");
3694 0 : m = MATCH_ERROR;
3695 0 : goto no_match;
3696 : }
3697 :
3698 5042 : if (gfc_derived_parameter_expr (e))
3699 : {
3700 80 : saved_kind_expr = e;
3701 80 : *kind = 0;
3702 80 : return MATCH_YES;
3703 : }
3704 :
3705 4962 : fail = gfc_extract_int (e, kind, 1);
3706 4962 : *is_iso_c = e->ts.is_iso_c;
3707 4962 : if (fail)
3708 : {
3709 0 : m = MATCH_ERROR;
3710 0 : goto no_match;
3711 : }
3712 :
3713 4962 : gfc_free_expr (e);
3714 :
3715 : /* Ignore errors to this point, if we've gotten here. This means
3716 : we ignore the m=MATCH_ERROR from above. */
3717 4962 : if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3718 : {
3719 14 : gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3720 14 : m = MATCH_ERROR;
3721 : }
3722 : else
3723 : /* All tests passed. */
3724 : m = MATCH_YES;
3725 :
3726 14 : if (m == MATCH_ERROR)
3727 14 : gfc_current_locus = where;
3728 :
3729 : /* Return what we know from the test(s). */
3730 : return m;
3731 :
3732 0 : no_match:
3733 0 : gfc_free_expr (e);
3734 0 : gfc_current_locus = where;
3735 0 : return m;
3736 : }
3737 :
3738 :
3739 : /* Match the various kind/length specifications in a CHARACTER
3740 : declaration. We don't return MATCH_NO. */
3741 :
3742 : match
3743 32752 : gfc_match_char_spec (gfc_typespec *ts)
3744 : {
3745 32752 : int kind, seen_length, is_iso_c;
3746 32752 : gfc_charlen *cl;
3747 32752 : gfc_expr *len;
3748 32752 : match m;
3749 32752 : bool deferred;
3750 :
3751 32752 : len = NULL;
3752 32752 : seen_length = 0;
3753 32752 : kind = 0;
3754 32752 : is_iso_c = 0;
3755 32752 : deferred = false;
3756 :
3757 : /* Try the old-style specification first. */
3758 32752 : old_char_selector = 0;
3759 :
3760 32752 : m = match_char_length (&len, &deferred, true);
3761 32752 : if (m != MATCH_NO)
3762 : {
3763 2205 : if (m == MATCH_YES)
3764 2205 : old_char_selector = 1;
3765 2205 : seen_length = 1;
3766 2205 : goto done;
3767 : }
3768 :
3769 30547 : m = gfc_match_char ('(');
3770 30547 : if (m != MATCH_YES)
3771 : {
3772 1928 : m = MATCH_YES; /* Character without length is a single char. */
3773 1928 : goto done;
3774 : }
3775 :
3776 : /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3777 28619 : if (gfc_match (" kind =") == MATCH_YES)
3778 : {
3779 3571 : m = match_char_kind (&kind, &is_iso_c);
3780 :
3781 3571 : if (m == MATCH_ERROR)
3782 16 : goto done;
3783 3555 : if (m == MATCH_NO)
3784 : goto syntax;
3785 :
3786 3555 : if (gfc_match (" , len =") == MATCH_NO)
3787 536 : goto rparen;
3788 :
3789 3019 : m = char_len_param_value (&len, &deferred);
3790 3019 : if (m == MATCH_NO)
3791 0 : goto syntax;
3792 3019 : if (m == MATCH_ERROR)
3793 2 : goto done;
3794 3017 : seen_length = 1;
3795 :
3796 3017 : goto rparen;
3797 : }
3798 :
3799 : /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3800 25048 : if (gfc_match (" len =") == MATCH_YES)
3801 : {
3802 14171 : m = char_len_param_value (&len, &deferred);
3803 14171 : if (m == MATCH_NO)
3804 2 : goto syntax;
3805 14169 : if (m == MATCH_ERROR)
3806 8 : goto done;
3807 14161 : seen_length = 1;
3808 :
3809 14161 : if (gfc_match_char (')') == MATCH_YES)
3810 12856 : goto done;
3811 :
3812 1305 : if (gfc_match (" , kind =") != MATCH_YES)
3813 0 : goto syntax;
3814 :
3815 1305 : if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3816 2 : goto done;
3817 :
3818 1303 : goto rparen;
3819 : }
3820 :
3821 : /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3822 10877 : m = char_len_param_value (&len, &deferred);
3823 10877 : if (m == MATCH_NO)
3824 0 : goto syntax;
3825 10877 : if (m == MATCH_ERROR)
3826 44 : goto done;
3827 10833 : seen_length = 1;
3828 :
3829 10833 : m = gfc_match_char (')');
3830 10833 : if (m == MATCH_YES)
3831 10651 : goto done;
3832 :
3833 182 : if (gfc_match_char (',') != MATCH_YES)
3834 2 : goto syntax;
3835 :
3836 180 : gfc_match (" kind ="); /* Gobble optional text. */
3837 :
3838 180 : m = match_char_kind (&kind, &is_iso_c);
3839 180 : if (m == MATCH_ERROR)
3840 3 : goto done;
3841 : if (m == MATCH_NO)
3842 : goto syntax;
3843 :
3844 5033 : rparen:
3845 : /* Require a right-paren at this point. */
3846 5033 : m = gfc_match_char (')');
3847 5033 : if (m == MATCH_YES)
3848 5033 : goto done;
3849 :
3850 0 : syntax:
3851 4 : gfc_error ("Syntax error in CHARACTER declaration at %C");
3852 4 : m = MATCH_ERROR;
3853 4 : gfc_free_expr (len);
3854 4 : return m;
3855 :
3856 32748 : done:
3857 : /* Deal with character functions after USE and IMPORT statements. */
3858 32748 : if (gfc_matching_function)
3859 : {
3860 1431 : gfc_free_expr (len);
3861 1431 : gfc_undo_symbols ();
3862 1431 : return MATCH_YES;
3863 : }
3864 :
3865 31317 : if (m != MATCH_YES)
3866 : {
3867 65 : gfc_free_expr (len);
3868 65 : return m;
3869 : }
3870 :
3871 : /* Do some final massaging of the length values. */
3872 31252 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3873 :
3874 31252 : if (seen_length == 0)
3875 2412 : cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3876 : else
3877 : {
3878 : /* If gfortran ends up here, then len may be reducible to a constant.
3879 : Try to do that here. If it does not reduce, simply assign len to
3880 : charlen. A complication occurs with user-defined generic functions,
3881 : which are not resolved. Use a private namespace to deal with
3882 : generic functions. */
3883 :
3884 28840 : if (len && len->expr_type != EXPR_CONSTANT)
3885 : {
3886 3219 : gfc_namespace *old_ns;
3887 3219 : gfc_expr *e;
3888 :
3889 3219 : old_ns = gfc_current_ns;
3890 3219 : gfc_current_ns = gfc_get_namespace (NULL, 0);
3891 :
3892 3219 : e = gfc_copy_expr (len);
3893 3219 : gfc_push_suppress_errors ();
3894 3219 : gfc_reduce_init_expr (e);
3895 3219 : gfc_pop_suppress_errors ();
3896 3219 : if (e->expr_type == EXPR_CONSTANT)
3897 : {
3898 318 : gfc_replace_expr (len, e);
3899 318 : if (mpz_cmp_si (len->value.integer, 0) < 0)
3900 7 : mpz_set_ui (len->value.integer, 0);
3901 : }
3902 : else
3903 2901 : gfc_free_expr (e);
3904 :
3905 3219 : gfc_free_namespace (gfc_current_ns);
3906 3219 : gfc_current_ns = old_ns;
3907 : }
3908 :
3909 28840 : cl->length = len;
3910 : }
3911 :
3912 31252 : ts->u.cl = cl;
3913 31252 : ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3914 31252 : ts->deferred = deferred;
3915 :
3916 : /* We have to know if it was a C interoperable kind so we can
3917 : do accurate type checking of bind(c) procs, etc. */
3918 31252 : if (kind != 0)
3919 : /* Mark this as C interoperable if being declared with one
3920 : of the named constants from iso_c_binding. */
3921 4873 : ts->is_c_interop = is_iso_c;
3922 26379 : else if (len != NULL)
3923 : /* Here, we might have parsed something such as: character(c_char)
3924 : In this case, the parsing code above grabs the c_char when
3925 : looking for the length (line 1690, roughly). it's the last
3926 : testcase for parsing the kind params of a character variable.
3927 : However, it's not actually the length. this seems like it
3928 : could be an error.
3929 : To see if the user used a C interop kind, test the expr
3930 : of the so called length, and see if it's C interoperable. */
3931 17058 : ts->is_c_interop = len->ts.is_iso_c;
3932 :
3933 : return MATCH_YES;
3934 : }
3935 :
3936 :
3937 : /* Matches a RECORD declaration. */
3938 :
3939 : static match
3940 993010 : match_record_decl (char *name)
3941 : {
3942 993010 : locus old_loc;
3943 993010 : old_loc = gfc_current_locus;
3944 993010 : match m;
3945 :
3946 993010 : m = gfc_match (" record /");
3947 993010 : if (m == MATCH_YES)
3948 : {
3949 353 : if (!flag_dec_structure)
3950 : {
3951 6 : gfc_current_locus = old_loc;
3952 6 : gfc_error ("RECORD at %C is an extension, enable it with "
3953 : "%<-fdec-structure%>");
3954 6 : return MATCH_ERROR;
3955 : }
3956 347 : m = gfc_match (" %n/", name);
3957 347 : if (m == MATCH_YES)
3958 : return MATCH_YES;
3959 : }
3960 :
3961 992660 : gfc_current_locus = old_loc;
3962 992660 : if (flag_dec_structure
3963 992660 : && (gfc_match (" record% ") == MATCH_YES
3964 8026 : || gfc_match (" record%t") == MATCH_YES))
3965 6 : gfc_error ("Structure name expected after RECORD at %C");
3966 992660 : if (m == MATCH_NO)
3967 992660 : return MATCH_NO;
3968 :
3969 : return MATCH_ERROR;
3970 : }
3971 :
3972 :
3973 : /* In parsing a PDT, it is possible that one of the type parameters has the
3974 : same name as a previously declared symbol that is not a type parameter.
3975 : Intercept this now by looking for the symtree in f2k_derived. */
3976 :
3977 : static bool
3978 1501 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
3979 : {
3980 1501 : if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
3981 : return false;
3982 :
3983 1173 : if (!(e->symtree->n.sym->attr.pdt_len
3984 254 : || e->symtree->n.sym->attr.pdt_kind))
3985 : {
3986 158 : gfc_symtree *st;
3987 158 : st = gfc_find_symtree (pdt->f2k_derived->sym_root,
3988 : e->symtree->n.sym->name);
3989 158 : if (st && st->n.sym
3990 30 : && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
3991 : {
3992 30 : gfc_expr *new_expr;
3993 30 : gfc_set_sym_referenced (st->n.sym);
3994 30 : new_expr = gfc_get_expr ();
3995 30 : new_expr->ts = st->n.sym->ts;
3996 30 : new_expr->expr_type = EXPR_VARIABLE;
3997 30 : new_expr->symtree = st;
3998 30 : new_expr->where = e->where;
3999 30 : gfc_replace_expr (e, new_expr);
4000 : }
4001 : }
4002 :
4003 : return false;
4004 : }
4005 :
4006 :
4007 : void
4008 1203 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
4009 : {
4010 1203 : if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
4011 : return;
4012 998 : gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
4013 : }
4014 :
4015 : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
4016 : of expressions to substitute into the possibly parameterized expression
4017 : 'e'. Using a list is inefficient but should not be too bad since the
4018 : number of type parameters is not likely to be large. */
4019 : static bool
4020 6193 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
4021 : int* f)
4022 : {
4023 6193 : gfc_actual_arglist *param;
4024 6193 : gfc_expr *copy;
4025 :
4026 6193 : if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
4027 : return false;
4028 :
4029 2767 : gcc_assert (e->symtree);
4030 2767 : if (e->symtree->n.sym->attr.pdt_kind
4031 2040 : || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
4032 921 : || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
4033 : {
4034 2974 : for (param = type_param_spec_list; param; param = param->next)
4035 2824 : if (!strcmp (e->symtree->n.sym->name, param->name))
4036 : break;
4037 :
4038 1995 : if (param && param->expr)
4039 : {
4040 1844 : copy = gfc_copy_expr (param->expr);
4041 1844 : gfc_replace_expr (e, copy);
4042 : /* Catch variables declared without a value expression. */
4043 1844 : if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
4044 33 : e->ts = e->symtree->n.sym->ts;
4045 : }
4046 : }
4047 :
4048 : return false;
4049 : }
4050 :
4051 :
4052 : static bool
4053 1685 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
4054 : {
4055 1685 : return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
4056 : }
4057 :
4058 :
4059 : bool
4060 3525 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
4061 : {
4062 3525 : gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
4063 3525 : type_param_spec_list = param_list;
4064 3525 : bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
4065 3525 : type_param_spec_list = old_param_spec_list;
4066 3525 : return res;
4067 : }
4068 :
4069 : /* Determines the instance of a parameterized derived type to be used by
4070 : matching determining the values of the kind parameters and using them
4071 : in the name of the instance. If the instance exists, it is used, otherwise
4072 : a new derived type is created. */
4073 : match
4074 4133 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
4075 : gfc_actual_arglist **ext_param_list)
4076 : {
4077 : /* The PDT template symbol. */
4078 4133 : gfc_symbol *pdt = *sym;
4079 : /* The symbol for the parameter in the template f2k_namespace. */
4080 4133 : gfc_symbol *param;
4081 : /* The hoped for instance of the PDT. */
4082 4133 : gfc_symbol *instance = NULL;
4083 : /* The list of parameters appearing in the PDT declaration. */
4084 4133 : gfc_formal_arglist *type_param_name_list;
4085 : /* Used to store the parameter specification list during recursive calls. */
4086 4133 : gfc_actual_arglist *old_param_spec_list;
4087 : /* Pointers to the parameter specification being used. */
4088 4133 : gfc_actual_arglist *actual_param;
4089 4133 : gfc_actual_arglist *tail = NULL;
4090 : /* Used to build up the name of the PDT instance. */
4091 4133 : char *name;
4092 4133 : bool name_seen = (param_list == NULL);
4093 4133 : bool assumed_seen = false;
4094 4133 : bool deferred_seen = false;
4095 4133 : bool spec_error = false;
4096 4133 : bool alloc_seen = false;
4097 4133 : bool ptr_seen = false;
4098 4133 : int i;
4099 4133 : gfc_expr *kind_expr;
4100 4133 : gfc_component *c1, *c2;
4101 4133 : match m;
4102 4133 : gfc_symtree *s = NULL;
4103 :
4104 4133 : type_param_spec_list = NULL;
4105 :
4106 4133 : type_param_name_list = pdt->formal;
4107 4133 : actual_param = param_list;
4108 :
4109 : /* Prevent a PDT component of the same type as the template from being
4110 : converted into an instance. Doing this results in the component being
4111 : lost. */
4112 4133 : if (gfc_current_state () == COMP_DERIVED
4113 131 : && !(gfc_state_stack->previous
4114 131 : && gfc_state_stack->previous->state == COMP_DERIVED)
4115 131 : && gfc_current_block ()->attr.pdt_template)
4116 : {
4117 118 : if (ext_param_list)
4118 118 : *ext_param_list = gfc_copy_actual_arglist (param_list);
4119 : return MATCH_YES;
4120 : }
4121 :
4122 4015 : name = xasprintf ("%s%s", PDT_PREFIX, pdt->name);
4123 :
4124 : /* Run through the parameter name list and pick up the actual
4125 : parameter values or use the default values in the PDT declaration. */
4126 13544 : for (; type_param_name_list;
4127 5514 : type_param_name_list = type_param_name_list->next)
4128 : {
4129 5582 : if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
4130 : {
4131 5132 : if (actual_param->spec_type == SPEC_ASSUMED)
4132 : spec_error = deferred_seen;
4133 : else
4134 5132 : spec_error = assumed_seen;
4135 :
4136 5132 : if (spec_error)
4137 : {
4138 : gfc_error ("The type parameter spec list at %C cannot contain "
4139 : "both ASSUMED and DEFERRED parameters");
4140 : goto error_return;
4141 : }
4142 : }
4143 :
4144 5132 : if (actual_param && actual_param->name)
4145 5582 : name_seen = true;
4146 5582 : param = type_param_name_list->sym;
4147 :
4148 5582 : if (!param || !param->name)
4149 2 : continue;
4150 :
4151 5580 : c1 = gfc_find_component (pdt, param->name, false, true, NULL);
4152 : /* An error should already have been thrown in resolve.cc
4153 : (resolve_fl_derived0). */
4154 5580 : if (!pdt->attr.use_assoc && !c1)
4155 8 : goto error_return;
4156 :
4157 : /* Resolution PDT class components of derived types are handled here.
4158 : They can arrive without a parameter list and no KIND parameters. */
4159 5572 : if (!param_list && (!c1->attr.pdt_kind && !c1->initializer))
4160 20 : continue;
4161 :
4162 5552 : kind_expr = NULL;
4163 5552 : if (!name_seen)
4164 : {
4165 3622 : if (!actual_param && !(c1 && c1->initializer))
4166 : {
4167 2 : gfc_error ("The type parameter spec list at %C does not contain "
4168 : "enough parameter expressions");
4169 2 : goto error_return;
4170 : }
4171 3620 : else if (!actual_param && c1 && c1->initializer)
4172 5 : kind_expr = gfc_copy_expr (c1->initializer);
4173 3615 : else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4174 3042 : kind_expr = gfc_copy_expr (actual_param->expr);
4175 : }
4176 : else
4177 : {
4178 : actual_param = param_list;
4179 2864 : for (;actual_param; actual_param = actual_param->next)
4180 2426 : if (actual_param->name
4181 2406 : && strcmp (actual_param->name, param->name) == 0)
4182 : break;
4183 1930 : if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4184 1307 : kind_expr = gfc_copy_expr (actual_param->expr);
4185 : else
4186 : {
4187 623 : if (c1->initializer)
4188 559 : kind_expr = gfc_copy_expr (c1->initializer);
4189 64 : else if (!(actual_param && param->attr.pdt_len))
4190 : {
4191 9 : gfc_error ("The derived parameter %qs at %C does not "
4192 : "have a default value", param->name);
4193 9 : goto error_return;
4194 : }
4195 : }
4196 : }
4197 :
4198 4913 : if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
4199 486 : && kind_expr->ts.type != BT_INTEGER
4200 172 : && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
4201 : {
4202 12 : gfc_error ("The type parameter expression at %L must be of INTEGER "
4203 : "type and not %s", &kind_expr->where,
4204 : gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
4205 12 : goto error_return;
4206 : }
4207 :
4208 : /* Store the current parameter expressions in a temporary actual
4209 : arglist 'list' so that they can be substituted in the corresponding
4210 : expressions in the PDT instance. */
4211 5529 : if (type_param_spec_list == NULL)
4212 : {
4213 3972 : type_param_spec_list = gfc_get_actual_arglist ();
4214 3972 : tail = type_param_spec_list;
4215 : }
4216 : else
4217 : {
4218 1557 : tail->next = gfc_get_actual_arglist ();
4219 1557 : tail = tail->next;
4220 : }
4221 5529 : tail->name = param->name;
4222 :
4223 5529 : if (kind_expr)
4224 : {
4225 : /* Try simplification even for LEN expressions. */
4226 4901 : bool ok;
4227 4901 : gfc_resolve_expr (kind_expr);
4228 :
4229 4901 : if (c1->attr.pdt_kind
4230 2366 : && kind_expr->expr_type != EXPR_CONSTANT
4231 28 : && type_param_spec_list)
4232 28 : gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
4233 :
4234 4901 : ok = gfc_simplify_expr (kind_expr, 1);
4235 : /* Variable expressions default to BT_PROCEDURE in the absence of an
4236 : initializer so allow for this. */
4237 4901 : if (kind_expr->ts.type != BT_INTEGER
4238 189 : && kind_expr->ts.type != BT_PROCEDURE)
4239 : {
4240 29 : gfc_error ("The parameter expression at %C must be of "
4241 : "INTEGER type and not %s type",
4242 : gfc_basic_typename (kind_expr->ts.type));
4243 29 : goto error_return;
4244 : }
4245 4872 : if (kind_expr->ts.type == BT_INTEGER && !ok)
4246 : {
4247 4 : gfc_error ("The parameter expression at %C does not "
4248 : "simplify to an INTEGER constant");
4249 4 : goto error_return;
4250 : }
4251 :
4252 4868 : tail->expr = gfc_copy_expr (kind_expr);
4253 : }
4254 :
4255 5496 : if (actual_param)
4256 5060 : tail->spec_type = actual_param->spec_type;
4257 :
4258 5496 : if (!param->attr.pdt_kind)
4259 : {
4260 3155 : if (!name_seen && actual_param)
4261 2314 : actual_param = actual_param->next;
4262 3155 : if (kind_expr)
4263 : {
4264 2529 : gfc_free_expr (kind_expr);
4265 2529 : kind_expr = NULL;
4266 : }
4267 3155 : continue;
4268 : }
4269 :
4270 2341 : if (actual_param
4271 1949 : && (actual_param->spec_type == SPEC_ASSUMED
4272 1949 : || actual_param->spec_type == SPEC_DEFERRED))
4273 : {
4274 2 : gfc_error ("The KIND parameter %qs at %C cannot either be "
4275 : "ASSUMED or DEFERRED", param->name);
4276 2 : goto error_return;
4277 : }
4278 :
4279 2339 : if (!kind_expr || !gfc_is_constant_expr (kind_expr))
4280 : {
4281 2 : gfc_error ("The value for the KIND parameter %qs at %C does not "
4282 : "reduce to a constant expression", param->name);
4283 2 : goto error_return;
4284 : }
4285 :
4286 : /* This can come about during the parsing of nested pdt_templates. An
4287 : error arises because the KIND parameter expression has not been
4288 : provided. Use the template instead of an incorrect instance. */
4289 2337 : if (kind_expr->expr_type != EXPR_CONSTANT
4290 2337 : || kind_expr->ts.type != BT_INTEGER)
4291 : {
4292 0 : gfc_free_actual_arglist (type_param_spec_list);
4293 0 : free (name);
4294 0 : return MATCH_YES;
4295 : }
4296 :
4297 2337 : char *kind_value = mpz_get_str (NULL, 10, kind_expr->value.integer);
4298 2337 : char *old_name = name;
4299 2337 : name = xasprintf ("%s_%s", old_name, kind_value);
4300 2337 : free (old_name);
4301 2337 : free (kind_value);
4302 :
4303 2337 : if (!name_seen && actual_param)
4304 1252 : actual_param = actual_param->next;
4305 2337 : gfc_free_expr (kind_expr);
4306 : }
4307 :
4308 3947 : if (!name_seen && actual_param)
4309 : {
4310 2 : gfc_error ("The type parameter spec list at %C contains too many "
4311 : "parameter expressions");
4312 2 : goto error_return;
4313 : }
4314 :
4315 : /* Now we search for the PDT instance 'name'. If it doesn't exist, we
4316 : build it, using 'pdt' as a template. */
4317 3945 : if (gfc_get_symbol (name, pdt->ns, &instance))
4318 : {
4319 0 : gfc_error ("Parameterized derived type at %C is ambiguous");
4320 0 : goto error_return;
4321 : }
4322 :
4323 : /* If we are in an interface body, the instance will not have been imported.
4324 : Make sure that it is imported implicitly. */
4325 3945 : s = gfc_find_symtree (gfc_current_ns->sym_root, pdt->name);
4326 3945 : if (gfc_current_ns->proc_name
4327 3898 : && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
4328 195 : && s && s->import_only && pdt->attr.imported)
4329 : {
4330 8 : s = gfc_find_symtree (gfc_current_ns->sym_root, instance->name);
4331 8 : if (!s)
4332 : {
4333 7 : gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false,
4334 : &gfc_current_locus);
4335 7 : s->n.sym = instance;
4336 7 : instance->refs++;
4337 : }
4338 8 : s->n.sym->attr.imported = 1;
4339 8 : s->import_only = 1;
4340 : }
4341 :
4342 3945 : m = MATCH_YES;
4343 :
4344 3945 : if (instance->attr.flavor == FL_DERIVED
4345 3130 : && instance->attr.pdt_type
4346 3130 : && instance->components)
4347 : {
4348 3130 : instance->refs++;
4349 3130 : if (ext_param_list)
4350 1518 : *ext_param_list = type_param_spec_list;
4351 3130 : *sym = instance;
4352 3130 : gfc_commit_symbols ();
4353 3130 : free (name);
4354 3130 : return m;
4355 : }
4356 :
4357 : /* Start building the new instance of the parameterized type. */
4358 815 : gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
4359 815 : if (pdt->attr.use_assoc)
4360 192 : instance->module = pdt->module;
4361 815 : instance->attr.pdt_template = 0;
4362 815 : instance->attr.pdt_type = 1;
4363 815 : instance->declared_at = gfc_current_locus;
4364 :
4365 : /* In resolution, the finalizers are copied, according to the type of the
4366 : argument, to the instance finalizers. However, they are retained by the
4367 : template and procedures are freed there. */
4368 815 : if (pdt->f2k_derived && pdt->f2k_derived->finalizers)
4369 : {
4370 36 : instance->f2k_derived = gfc_get_namespace (NULL, 0);
4371 36 : instance->template_sym = pdt;
4372 36 : *instance->f2k_derived = *pdt->f2k_derived;
4373 : }
4374 :
4375 : /* Add the components, replacing the parameters in all expressions
4376 : with the expressions for their values in 'type_param_spec_list'. */
4377 815 : c1 = pdt->components;
4378 815 : tail = type_param_spec_list;
4379 3154 : for (; c1; c1 = c1->next)
4380 : {
4381 2341 : gfc_add_component (instance, c1->name, &c2);
4382 :
4383 2341 : c2->ts = c1->ts;
4384 2341 : c2->attr = c1->attr;
4385 2341 : if (c1->tb)
4386 : {
4387 12 : c2->tb = gfc_get_tbp ();
4388 12 : *c2->tb = *c1->tb;
4389 : }
4390 :
4391 : /* The order of declaration of the type_specs might not be the
4392 : same as that of the components. */
4393 2341 : if (c1->attr.pdt_kind || c1->attr.pdt_len)
4394 : {
4395 1628 : for (tail = type_param_spec_list; tail; tail = tail->next)
4396 1624 : if (strcmp (c1->name, tail->name) == 0)
4397 : break;
4398 : }
4399 :
4400 : /* Deal with type extension by recursively calling this function
4401 : to obtain the instance of the extended type. */
4402 2341 : if (gfc_current_state () != COMP_DERIVED
4403 2327 : && c1 == pdt->components
4404 802 : && c1->ts.type == BT_DERIVED
4405 102 : && c1->ts.u.derived
4406 2443 : && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
4407 : {
4408 102 : if (c1->ts.u.derived->attr.pdt_template)
4409 : {
4410 89 : gfc_formal_arglist *f;
4411 :
4412 89 : old_param_spec_list = type_param_spec_list;
4413 :
4414 : /* Obtain a spec list appropriate to the extended type..*/
4415 89 : actual_param = gfc_copy_actual_arglist (type_param_spec_list);
4416 89 : type_param_spec_list = actual_param;
4417 169 : for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
4418 80 : actual_param = actual_param->next;
4419 89 : if (actual_param)
4420 : {
4421 89 : gfc_free_actual_arglist (actual_param->next);
4422 89 : actual_param->next = NULL;
4423 : }
4424 :
4425 : /* Now obtain the PDT instance for the extended type. */
4426 89 : c2->param_list = type_param_spec_list;
4427 89 : m = gfc_get_pdt_instance (type_param_spec_list,
4428 : &c2->ts.u.derived,
4429 : &c2->param_list);
4430 89 : type_param_spec_list = old_param_spec_list;
4431 : }
4432 : else
4433 13 : c2->ts = c1->ts;
4434 :
4435 102 : c2->ts.u.derived->refs++;
4436 102 : gfc_set_sym_referenced (c2->ts.u.derived);
4437 :
4438 : /* If the component is allocatable or the parent has allocatable
4439 : components, make sure that the new instance also is marked as
4440 : having allocatable components. */
4441 102 : if (c2->attr.allocatable || c2->ts.u.derived->attr.alloc_comp)
4442 6 : instance->attr.alloc_comp = 1;
4443 :
4444 : /* Set extension level. */
4445 102 : if (c2->ts.u.derived->attr.extension == 255)
4446 : {
4447 : /* Since the extension field is 8 bit wide, we can only have
4448 : up to 255 extension levels. */
4449 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
4450 : c2->ts.u.derived->name,
4451 : &c2->ts.u.derived->declared_at);
4452 0 : goto error_return;
4453 : }
4454 102 : instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
4455 :
4456 : /* Add the parent initializer. */
4457 102 : if (!IS_PDT (c2) && !c2->ts.u.derived->attr.alloc_comp)
4458 7 : c2->initializer = gfc_generate_initializer (&c2->ts, false);
4459 102 : continue;
4460 102 : }
4461 :
4462 : /* Addressing PR82943, this will fix the issue where a function or
4463 : subroutine is declared as not a member of the PDT instance.
4464 : The reason for this is because the PDT instance did not have access
4465 : to its template's f2k_derived namespace in order to find the
4466 : typebound procedures.
4467 :
4468 : The number of references to the PDT template's f2k_derived will
4469 : ensure that f2k_derived is properly freed later on. */
4470 :
4471 2239 : if (!instance->f2k_derived && pdt->f2k_derived)
4472 : {
4473 772 : instance->f2k_derived = pdt->f2k_derived;
4474 772 : instance->f2k_derived->refs++;
4475 : }
4476 :
4477 : /* Set the component kind using the parameterized expression. */
4478 2239 : if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
4479 783 : && c1->kind_expr != NULL)
4480 : {
4481 518 : gfc_expr *e = gfc_copy_expr (c1->kind_expr);
4482 518 : gfc_insert_kind_parameter_exprs (e);
4483 518 : gfc_simplify_expr (e, 1);
4484 518 : gfc_extract_int (e, &c2->ts.kind);
4485 518 : gfc_free_expr (e);
4486 518 : if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
4487 : {
4488 2 : gfc_error ("Kind %d not supported for type %s at %C",
4489 : c2->ts.kind, gfc_basic_typename (c2->ts.type));
4490 2 : goto error_return;
4491 : }
4492 516 : if (c2->attr.proc_pointer && c2->attr.function
4493 0 : && c1->ts.interface && c1->ts.interface->ts.kind == 0)
4494 : {
4495 0 : c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4496 0 : c2->ts.interface->result = c2->ts.interface;
4497 0 : c2->ts.interface->ts = c2->ts;
4498 0 : c2->ts.interface->attr.flavor = FL_PROCEDURE;
4499 0 : c2->ts.interface->attr.function = 1;
4500 0 : c2->attr.function = 1;
4501 0 : c2->attr.if_source = IFSRC_UNKNOWN;
4502 : }
4503 : }
4504 :
4505 : /* Set up either the KIND/LEN initializer, if constant,
4506 : or the parameterized expression. Use the template
4507 : initializer if one is not already set in this instance. */
4508 2237 : if (c2->attr.pdt_kind || c2->attr.pdt_len)
4509 : {
4510 1090 : if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
4511 854 : c2->initializer = gfc_copy_expr (tail->expr);
4512 236 : else if (tail && tail->expr)
4513 : {
4514 40 : c2->param_list = gfc_get_actual_arglist ();
4515 40 : c2->param_list->name = tail->name;
4516 40 : c2->param_list->expr = gfc_copy_expr (tail->expr);
4517 40 : c2->param_list->next = NULL;
4518 : }
4519 :
4520 : /* Initializer expressions in PDT templates, such as character_kinds(1),
4521 : can end up being mutilated when use associated. Simplify now. */
4522 1090 : if (c1->initializer && c1->initializer->expr_type != EXPR_CONSTANT)
4523 154 : gfc_simplify_expr (c1->initializer, 1);
4524 :
4525 1090 : if (!c2->initializer && c1->initializer)
4526 96 : c2->initializer = gfc_copy_expr (c1->initializer);
4527 :
4528 1090 : if (c2->initializer)
4529 950 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4530 : }
4531 :
4532 : /* Copy the array spec. */
4533 2237 : c2->as = gfc_copy_array_spec (c1->as);
4534 2237 : if (c1->ts.type == BT_CLASS)
4535 0 : CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
4536 :
4537 2237 : if (c1->attr.allocatable)
4538 94 : alloc_seen = true;
4539 :
4540 2237 : if (c1->attr.pointer)
4541 20 : ptr_seen = true;
4542 :
4543 : /* Determine if an array spec is parameterized. If so, substitute
4544 : in the parameter expressions for the bounds and set the pdt_array
4545 : attribute. Notice that this attribute must be unconditionally set
4546 : if this is an array of parameterized character length. */
4547 2237 : if (c1->as && c1->as->type == AS_EXPLICIT)
4548 : {
4549 : bool pdt_array = false;
4550 1054 : bool all_constant = true;
4551 :
4552 : /* Are the bounds of the array parameterized? */
4553 1054 : for (i = 0; i < c1->as->rank; i++)
4554 : {
4555 581 : if (gfc_derived_parameter_expr (c1->as->lower[i]))
4556 12 : pdt_array = true;
4557 581 : if (gfc_derived_parameter_expr (c1->as->upper[i]))
4558 489 : pdt_array = true;
4559 : }
4560 :
4561 : /* If they are, free the expressions for the bounds and
4562 : replace them with the template expressions with substitute
4563 : values. */
4564 968 : for (i = 0; pdt_array && i < c1->as->rank; i++)
4565 : {
4566 495 : gfc_expr *e;
4567 495 : e = gfc_copy_expr (c1->as->lower[i]);
4568 495 : gfc_insert_kind_parameter_exprs (e);
4569 495 : if (gfc_simplify_expr (e, 1))
4570 495 : gfc_replace_expr (c2->as->lower[i], e);
4571 : else
4572 0 : gfc_free_expr (e);
4573 495 : if (c2->as->lower[i]->expr_type != EXPR_CONSTANT)
4574 12 : all_constant = false;
4575 495 : e = gfc_copy_expr (c1->as->upper[i]);
4576 495 : gfc_insert_kind_parameter_exprs (e);
4577 495 : if (gfc_simplify_expr (e, 1))
4578 495 : gfc_replace_expr (c2->as->upper[i], e);
4579 : else
4580 0 : gfc_free_expr (e);
4581 495 : if (c2->as->upper[i]->expr_type != EXPR_CONSTANT)
4582 487 : all_constant = false;
4583 : }
4584 :
4585 473 : c2->attr.pdt_array = all_constant ? 0 : 1;
4586 473 : if (c1->initializer)
4587 : {
4588 13 : c2->initializer = gfc_copy_expr (c1->initializer);
4589 13 : gfc_insert_kind_parameter_exprs (c2->initializer);
4590 13 : gfc_simplify_expr (c2->initializer, 1);
4591 : }
4592 : }
4593 :
4594 : /* Similarly, set the string length if parameterized. */
4595 2237 : if (c1->ts.type == BT_CHARACTER
4596 201 : && c1->ts.u.cl->length
4597 2437 : && gfc_derived_parameter_expr (c1->ts.u.cl->length))
4598 : {
4599 164 : gfc_expr *e;
4600 164 : e = gfc_copy_expr (c1->ts.u.cl->length);
4601 164 : gfc_insert_kind_parameter_exprs (e);
4602 164 : if (gfc_simplify_expr (e, 1))
4603 164 : gfc_replace_expr (c2->ts.u.cl->length, e);
4604 : else
4605 0 : gfc_free_expr (e);
4606 164 : if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
4607 161 : c2->attr.pdt_string = 1;
4608 164 : if (c1->as && c1->as->type == AS_EXPLICIT)
4609 48 : c2->attr.pdt_array = 1;
4610 116 : else if (c1->attr.allocatable)
4611 6 : c2->ts.deferred = 1;
4612 : }
4613 :
4614 : /* Recurse into this function for PDT components. */
4615 2237 : if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
4616 161 : && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
4617 : {
4618 147 : gfc_actual_arglist *params;
4619 : /* The component in the template has a list of specification
4620 : expressions derived from its declaration. */
4621 147 : params = gfc_copy_actual_arglist (c1->param_list);
4622 147 : actual_param = params;
4623 : /* Substitute the template parameters with the expressions
4624 : from the specification list. */
4625 474 : for (;actual_param; actual_param = actual_param->next)
4626 : {
4627 180 : gfc_correct_parm_expr (pdt, &actual_param->expr);
4628 180 : gfc_insert_parameter_exprs (actual_param->expr,
4629 : type_param_spec_list);
4630 : }
4631 :
4632 : /* Now obtain the PDT instance for the component. */
4633 147 : old_param_spec_list = type_param_spec_list;
4634 294 : m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
4635 147 : &c2->param_list);
4636 147 : type_param_spec_list = old_param_spec_list;
4637 :
4638 147 : if (!(c2->attr.pointer || c2->attr.allocatable))
4639 : {
4640 101 : if (!c1->initializer
4641 76 : || c1->initializer->expr_type != EXPR_FUNCTION)
4642 100 : c2->initializer = gfc_default_initializer (&c2->ts);
4643 : else
4644 : {
4645 1 : gfc_symtree *s;
4646 1 : c2->initializer = gfc_copy_expr (c1->initializer);
4647 1 : s = gfc_find_symtree (pdt->ns->sym_root,
4648 1 : gfc_dt_lower_string (c2->ts.u.derived->name));
4649 1 : if (s)
4650 0 : c2->initializer->symtree = s;
4651 1 : c2->initializer->ts = c2->ts;
4652 1 : if (!s)
4653 1 : gfc_insert_parameter_exprs (c2->initializer,
4654 : type_param_spec_list);
4655 1 : gfc_simplify_expr (c2->initializer, 1);
4656 : }
4657 : }
4658 :
4659 147 : if (c2->attr.allocatable
4660 109 : || (c2->ts.type == BT_DERIVED && c2->ts.u.derived
4661 109 : && c2->ts.u.derived->attr.alloc_comp && !c2->attr.pointer))
4662 67 : instance->attr.alloc_comp = 1;
4663 : }
4664 2090 : else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
4665 839 : || c2->attr.pdt_array) && c1->initializer)
4666 : {
4667 74 : c2->initializer = gfc_copy_expr (c1->initializer);
4668 74 : if (c2->initializer->ts.type == BT_UNKNOWN)
4669 18 : c2->initializer->ts = c2->ts;
4670 74 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4671 : /* The template initializers are parsed using gfc_match_expr rather
4672 : than gfc_match_init_expr. Apply the missing reduction to the
4673 : PDT instance initializers. */
4674 74 : if (!gfc_reduce_init_expr (c2->initializer))
4675 : {
4676 0 : gfc_free_expr (c2->initializer);
4677 0 : goto error_return;
4678 : }
4679 74 : gfc_simplify_expr (c2->initializer, 1);
4680 : }
4681 :
4682 : /* Pick up any remaining initializers that could be simplified. */
4683 2237 : if (c1->initializer)
4684 : {
4685 751 : if (!c2->initializer)
4686 31 : c2->initializer = gfc_copy_expr (c1->initializer);
4687 751 : if (gfc_derived_parameter_expr (c2->initializer))
4688 0 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4689 751 : c2->initializer->ts = c2->ts;
4690 751 : if (!gfc_is_constant_expr (c2->initializer))
4691 19 : gfc_simplify_expr (c2->initializer, 1);
4692 : }
4693 : }
4694 :
4695 813 : if (alloc_seen)
4696 91 : instance->attr.alloc_comp = 1;
4697 813 : if (ptr_seen)
4698 20 : instance->attr.pointer_comp = 1;
4699 :
4700 813 : gfc_set_sym_referenced (instance);
4701 813 : gfc_commit_symbol (instance);
4702 :
4703 813 : if (ext_param_list)
4704 534 : *ext_param_list = type_param_spec_list;
4705 813 : *sym = instance;
4706 813 : free (name);
4707 813 : return m;
4708 :
4709 72 : error_return:
4710 72 : gfc_free_actual_arglist (type_param_spec_list);
4711 72 : free (name);
4712 72 : return MATCH_ERROR;
4713 : }
4714 :
4715 :
4716 : /* Match a legacy nonstandard BYTE type-spec. */
4717 :
4718 : static match
4719 1222333 : match_byte_typespec (gfc_typespec *ts)
4720 : {
4721 1222333 : if (gfc_match (" byte") == MATCH_YES)
4722 : {
4723 33 : if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4724 : return MATCH_ERROR;
4725 :
4726 31 : if (gfc_current_form == FORM_FREE)
4727 : {
4728 19 : char c = gfc_peek_ascii_char ();
4729 19 : if (!gfc_is_whitespace (c) && c != ',')
4730 : return MATCH_NO;
4731 : }
4732 :
4733 29 : if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4734 : {
4735 0 : gfc_error ("BYTE type used at %C "
4736 : "is not available on the target machine");
4737 0 : return MATCH_ERROR;
4738 : }
4739 :
4740 29 : ts->type = BT_INTEGER;
4741 29 : ts->kind = 1;
4742 29 : return MATCH_YES;
4743 : }
4744 : return MATCH_NO;
4745 : }
4746 :
4747 :
4748 : /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
4749 : structure to the matched specification. This is necessary for FUNCTION and
4750 : IMPLICIT statements.
4751 :
4752 : If implicit_flag is nonzero, then we don't check for the optional
4753 : kind specification. Not doing so is needed for matching an IMPLICIT
4754 : statement correctly. */
4755 :
4756 : match
4757 1222333 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4758 : {
4759 : /* Provide sufficient space to hold "pdtsymbol". */
4760 1222333 : char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
4761 1222333 : gfc_symbol *sym, *dt_sym;
4762 1222333 : match m;
4763 1222333 : char c;
4764 1222333 : bool seen_deferred_kind, matched_type;
4765 1222333 : const char *dt_name;
4766 :
4767 1222333 : decl_type_param_list = NULL;
4768 :
4769 : /* A belt and braces check that the typespec is correctly being treated
4770 : as a deferred characteristic association. */
4771 2444666 : seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4772 86539 : && (gfc_current_block ()->result->ts.kind == -1)
4773 1234497 : && (ts->kind == -1);
4774 1222333 : gfc_clear_ts (ts);
4775 1222333 : if (seen_deferred_kind)
4776 9929 : ts->kind = -1;
4777 :
4778 : /* Clear the current binding label, in case one is given. */
4779 1222333 : curr_binding_label = NULL;
4780 :
4781 : /* Match BYTE type-spec. */
4782 1222333 : m = match_byte_typespec (ts);
4783 1222333 : if (m != MATCH_NO)
4784 : return m;
4785 :
4786 1222302 : m = gfc_match (" type (");
4787 1222302 : matched_type = (m == MATCH_YES);
4788 1222302 : if (matched_type)
4789 : {
4790 32880 : gfc_gobble_whitespace ();
4791 32880 : if (gfc_peek_ascii_char () == '*')
4792 : {
4793 5617 : if ((m = gfc_match ("* ) ")) != MATCH_YES)
4794 : return m;
4795 5617 : if (gfc_comp_struct (gfc_current_state ()))
4796 : {
4797 2 : gfc_error ("Assumed type at %C is not allowed for components");
4798 2 : return MATCH_ERROR;
4799 : }
4800 5615 : if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4801 : return MATCH_ERROR;
4802 5613 : ts->type = BT_ASSUMED;
4803 5613 : return MATCH_YES;
4804 : }
4805 :
4806 27263 : m = gfc_match ("%n", name);
4807 27263 : matched_type = (m == MATCH_YES);
4808 : }
4809 :
4810 27263 : if ((matched_type && strcmp ("integer", name) == 0)
4811 1216685 : || (!matched_type && gfc_match (" integer") == MATCH_YES))
4812 : {
4813 116009 : ts->type = BT_INTEGER;
4814 116009 : ts->kind = gfc_default_integer_kind;
4815 116009 : goto get_kind;
4816 : }
4817 :
4818 1100676 : if (flag_unsigned)
4819 : {
4820 0 : if ((matched_type && strcmp ("unsigned", name) == 0)
4821 23032 : || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
4822 : {
4823 1090 : ts->type = BT_UNSIGNED;
4824 1090 : ts->kind = gfc_default_integer_kind;
4825 1090 : goto get_kind;
4826 : }
4827 : }
4828 :
4829 27257 : if ((matched_type && strcmp ("character", name) == 0)
4830 1099586 : || (!matched_type && gfc_match (" character") == MATCH_YES))
4831 : {
4832 29754 : if (matched_type
4833 29754 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4834 : "intrinsic-type-spec at %C"))
4835 : return MATCH_ERROR;
4836 :
4837 29753 : ts->type = BT_CHARACTER;
4838 29753 : if (implicit_flag == 0)
4839 29647 : m = gfc_match_char_spec (ts);
4840 : else
4841 : m = MATCH_YES;
4842 :
4843 29753 : if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4844 : {
4845 1 : gfc_error ("Malformed type-spec at %C");
4846 1 : return MATCH_ERROR;
4847 : }
4848 :
4849 : return m;
4850 : }
4851 :
4852 27253 : if ((matched_type && strcmp ("real", name) == 0)
4853 1069832 : || (!matched_type && gfc_match (" real") == MATCH_YES))
4854 : {
4855 30894 : ts->type = BT_REAL;
4856 30894 : ts->kind = gfc_default_real_kind;
4857 30894 : goto get_kind;
4858 : }
4859 :
4860 1038938 : if ((matched_type
4861 27250 : && (strcmp ("doubleprecision", name) == 0
4862 27249 : || (strcmp ("double", name) == 0
4863 5 : && gfc_match (" precision") == MATCH_YES)))
4864 1038938 : || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4865 : {
4866 2614 : if (matched_type
4867 2614 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4868 : "intrinsic-type-spec at %C"))
4869 : return MATCH_ERROR;
4870 :
4871 2613 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4872 : {
4873 2 : gfc_error ("Malformed type-spec at %C");
4874 2 : return MATCH_ERROR;
4875 : }
4876 :
4877 2611 : ts->type = BT_REAL;
4878 2611 : ts->kind = gfc_default_double_kind;
4879 2611 : return MATCH_YES;
4880 : }
4881 :
4882 27246 : if ((matched_type && strcmp ("complex", name) == 0)
4883 1036324 : || (!matched_type && gfc_match (" complex") == MATCH_YES))
4884 : {
4885 4172 : ts->type = BT_COMPLEX;
4886 4172 : ts->kind = gfc_default_complex_kind;
4887 4172 : goto get_kind;
4888 : }
4889 :
4890 1032152 : if ((matched_type
4891 27246 : && (strcmp ("doublecomplex", name) == 0
4892 27245 : || (strcmp ("double", name) == 0
4893 2 : && gfc_match (" complex") == MATCH_YES)))
4894 1032152 : || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4895 : {
4896 204 : if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4897 : return MATCH_ERROR;
4898 :
4899 203 : if (matched_type
4900 203 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4901 : "intrinsic-type-spec at %C"))
4902 : return MATCH_ERROR;
4903 :
4904 203 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4905 : {
4906 2 : gfc_error ("Malformed type-spec at %C");
4907 2 : return MATCH_ERROR;
4908 : }
4909 :
4910 201 : ts->type = BT_COMPLEX;
4911 201 : ts->kind = gfc_default_double_kind;
4912 201 : return MATCH_YES;
4913 : }
4914 :
4915 27243 : if ((matched_type && strcmp ("logical", name) == 0)
4916 1031948 : || (!matched_type && gfc_match (" logical") == MATCH_YES))
4917 : {
4918 11698 : ts->type = BT_LOGICAL;
4919 11698 : ts->kind = gfc_default_logical_kind;
4920 11698 : goto get_kind;
4921 : }
4922 :
4923 1020250 : if (matched_type)
4924 : {
4925 27240 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4926 27240 : if (m == MATCH_ERROR)
4927 : return m;
4928 :
4929 27240 : gfc_gobble_whitespace ();
4930 27240 : if (gfc_peek_ascii_char () != ')')
4931 : {
4932 1 : gfc_error ("Malformed type-spec at %C");
4933 1 : return MATCH_ERROR;
4934 : }
4935 27239 : m = gfc_match_char (')'); /* Burn closing ')'. */
4936 : }
4937 :
4938 1020249 : if (m != MATCH_YES)
4939 993010 : m = match_record_decl (name);
4940 :
4941 1020249 : if (matched_type || m == MATCH_YES)
4942 : {
4943 27583 : ts->type = BT_DERIVED;
4944 : /* We accept record/s/ or type(s) where s is a structure, but we
4945 : * don't need all the extra derived-type stuff for structures. */
4946 27583 : if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4947 : {
4948 1 : gfc_error ("Type name %qs at %C is ambiguous", name);
4949 1 : return MATCH_ERROR;
4950 : }
4951 :
4952 27582 : if (sym && sym->attr.flavor == FL_DERIVED
4953 26432 : && sym->attr.pdt_template
4954 1516 : && gfc_current_state () != COMP_DERIVED)
4955 : {
4956 1377 : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4957 1377 : if (m != MATCH_YES)
4958 : return m;
4959 1362 : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4960 1362 : ts->u.derived = sym;
4961 1362 : const char* lower = gfc_dt_lower_string (sym->name);
4962 1362 : size_t len = strlen (lower);
4963 : /* Reallocate with sufficient size. */
4964 1362 : if (len > GFC_MAX_SYMBOL_LEN)
4965 2 : name = XALLOCAVEC (char, len + 1);
4966 1362 : memcpy (name, lower, len);
4967 1362 : name[len] = '\0';
4968 : }
4969 :
4970 27567 : if (sym && sym->attr.flavor == FL_STRUCT)
4971 : {
4972 361 : ts->u.derived = sym;
4973 361 : return MATCH_YES;
4974 : }
4975 : /* Actually a derived type. */
4976 : }
4977 :
4978 : else
4979 : {
4980 : /* Match nested STRUCTURE declarations; only valid within another
4981 : structure declaration. */
4982 992666 : if (flag_dec_structure
4983 8032 : && (gfc_current_state () == COMP_STRUCTURE
4984 7570 : || gfc_current_state () == COMP_MAP))
4985 : {
4986 732 : m = gfc_match (" structure");
4987 732 : if (m == MATCH_YES)
4988 : {
4989 27 : m = gfc_match_structure_decl ();
4990 27 : if (m == MATCH_YES)
4991 : {
4992 : /* gfc_new_block is updated by match_structure_decl. */
4993 26 : ts->type = BT_DERIVED;
4994 26 : ts->u.derived = gfc_new_block;
4995 26 : return MATCH_YES;
4996 : }
4997 : }
4998 706 : if (m == MATCH_ERROR)
4999 : return MATCH_ERROR;
5000 : }
5001 :
5002 : /* Match CLASS declarations. */
5003 992639 : m = gfc_match (" class ( * )");
5004 992639 : if (m == MATCH_ERROR)
5005 : return MATCH_ERROR;
5006 992639 : else if (m == MATCH_YES)
5007 : {
5008 2087 : gfc_symbol *upe;
5009 2087 : gfc_symtree *st;
5010 2087 : ts->type = BT_CLASS;
5011 2087 : gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
5012 2087 : if (upe == NULL)
5013 : {
5014 1261 : upe = gfc_new_symbol ("STAR", gfc_current_ns);
5015 1261 : st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
5016 1261 : st->n.sym = upe;
5017 1261 : gfc_set_sym_referenced (upe);
5018 1261 : upe->refs++;
5019 1261 : upe->ts.type = BT_VOID;
5020 1261 : upe->attr.unlimited_polymorphic = 1;
5021 : /* This is essential to force the construction of
5022 : unlimited polymorphic component class containers. */
5023 1261 : upe->attr.zero_comp = 1;
5024 1261 : if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
5025 : &gfc_current_locus))
5026 : return MATCH_ERROR;
5027 : }
5028 : else
5029 : {
5030 826 : st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
5031 826 : st->n.sym = upe;
5032 826 : upe->refs++;
5033 : }
5034 2087 : ts->u.derived = upe;
5035 2087 : return m;
5036 : }
5037 :
5038 990552 : m = gfc_match (" class (");
5039 :
5040 990552 : if (m == MATCH_YES)
5041 9514 : m = gfc_match ("%n", name);
5042 : else
5043 : return m;
5044 :
5045 9514 : if (m != MATCH_YES)
5046 : return m;
5047 9514 : ts->type = BT_CLASS;
5048 :
5049 9514 : if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
5050 : return MATCH_ERROR;
5051 :
5052 9513 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
5053 9513 : if (m == MATCH_ERROR)
5054 : return m;
5055 :
5056 9513 : m = gfc_match_char (')');
5057 9513 : if (m != MATCH_YES)
5058 : return m;
5059 : }
5060 :
5061 : /* This picks up function declarations with a PDT typespec. Since a
5062 : pdt_type has been generated, there is no more to do. Within the
5063 : function body, this type must be used for the typespec so that
5064 : the "being used before it is defined warning" does not arise. */
5065 36719 : if (ts->type == BT_DERIVED
5066 27206 : && sym && sym->attr.pdt_type
5067 38081 : && (gfc_current_state () == COMP_CONTAINS
5068 1346 : || (gfc_current_state () == COMP_FUNCTION
5069 382 : && gfc_current_block ()->ts.type == BT_DERIVED
5070 90 : && gfc_current_block ()->ts.u.derived == sym
5071 60 : && !gfc_find_symtree (gfc_current_ns->sym_root,
5072 : sym->name))))
5073 : {
5074 60 : if (gfc_current_state () == COMP_FUNCTION)
5075 : {
5076 44 : gfc_symtree *pdt_st;
5077 44 : pdt_st = gfc_new_symtree (&gfc_current_ns->sym_root,
5078 : sym->name);
5079 44 : pdt_st->n.sym = sym;
5080 44 : sym->refs++;
5081 : }
5082 60 : ts->u.derived = sym;
5083 60 : return MATCH_YES;
5084 : }
5085 :
5086 : /* Defer association of the derived type until the end of the
5087 : specification block. However, if the derived type can be
5088 : found, add it to the typespec. */
5089 36659 : if (gfc_matching_function)
5090 : {
5091 1044 : ts->u.derived = NULL;
5092 1044 : if (gfc_current_state () != COMP_INTERFACE
5093 1044 : && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
5094 : {
5095 513 : sym = gfc_find_dt_in_generic (sym);
5096 513 : ts->u.derived = sym;
5097 : }
5098 : return MATCH_YES;
5099 : }
5100 :
5101 : /* Search for the name but allow the components to be defined later. If
5102 : type = -1, this typespec has been seen in a function declaration but
5103 : the type could not be accessed at that point. The actual derived type is
5104 : stored in a symtree with the first letter of the name capitalized; the
5105 : symtree with the all lower-case name contains the associated
5106 : generic function. */
5107 35615 : dt_name = gfc_dt_upper_string (name);
5108 35615 : sym = NULL;
5109 35615 : dt_sym = NULL;
5110 35615 : if (ts->kind != -1)
5111 : {
5112 34402 : gfc_get_ha_symbol (name, &sym);
5113 34402 : if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
5114 : {
5115 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5116 0 : return MATCH_ERROR;
5117 : }
5118 34402 : if (sym->generic && !dt_sym)
5119 14979 : dt_sym = gfc_find_dt_in_generic (sym);
5120 :
5121 : /* Host associated PDTs can get confused with their constructors
5122 : because they are instantiated in the template's namespace. */
5123 34402 : if (!dt_sym)
5124 : {
5125 1406 : if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
5126 : {
5127 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5128 0 : return MATCH_ERROR;
5129 : }
5130 1406 : if (dt_sym && !dt_sym->attr.pdt_type)
5131 0 : dt_sym = NULL;
5132 : }
5133 : }
5134 1213 : else if (ts->kind == -1)
5135 : {
5136 2426 : int iface = gfc_state_stack->previous->state != COMP_INTERFACE
5137 1213 : || gfc_current_ns->has_import_set;
5138 1213 : gfc_find_symbol (name, NULL, iface, &sym);
5139 1213 : if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
5140 : {
5141 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5142 0 : return MATCH_ERROR;
5143 : }
5144 1213 : if (sym && sym->generic && !dt_sym)
5145 2 : dt_sym = gfc_find_dt_in_generic (sym);
5146 :
5147 1213 : ts->kind = 0;
5148 1213 : if (sym == NULL)
5149 : return MATCH_NO;
5150 : }
5151 :
5152 35598 : if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
5153 34504 : && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
5154 35596 : || sym->attr.subroutine)
5155 : {
5156 2 : gfc_error ("Type name %qs at %C conflicts with previously declared "
5157 : "entity at %L, which has the same name", name,
5158 : &sym->declared_at);
5159 2 : return MATCH_ERROR;
5160 : }
5161 :
5162 35596 : if (dt_sym && decl_type_param_list
5163 1432 : && dt_sym->attr.flavor == FL_DERIVED
5164 1432 : && !dt_sym->attr.pdt_type
5165 316 : && !dt_sym->attr.pdt_template)
5166 : {
5167 1 : gfc_error ("Type %qs is not parameterized and so the type parameter spec "
5168 : "list at %C may not appear", dt_sym->name);
5169 1 : return MATCH_ERROR;
5170 : }
5171 :
5172 35595 : if (sym && sym->attr.flavor == FL_DERIVED
5173 : && sym->attr.pdt_template
5174 : && gfc_current_state () != COMP_DERIVED)
5175 : {
5176 : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
5177 : if (m != MATCH_YES)
5178 : return m;
5179 : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
5180 : ts->u.derived = sym;
5181 : strcpy (name, gfc_dt_lower_string (sym->name));
5182 : }
5183 :
5184 35595 : gfc_save_symbol_data (sym);
5185 35595 : gfc_set_sym_referenced (sym);
5186 35595 : if (!sym->attr.generic
5187 35595 : && !gfc_add_generic (&sym->attr, sym->name, NULL))
5188 : return MATCH_ERROR;
5189 :
5190 35595 : if (!sym->attr.function
5191 35595 : && !gfc_add_function (&sym->attr, sym->name, NULL))
5192 : return MATCH_ERROR;
5193 :
5194 35595 : if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
5195 35463 : && dt_sym->attr.pdt_template
5196 326 : && gfc_current_state () != COMP_DERIVED)
5197 : {
5198 175 : m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
5199 175 : if (m != MATCH_YES)
5200 : return m;
5201 175 : gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
5202 : }
5203 :
5204 35595 : if (!dt_sym)
5205 : {
5206 132 : gfc_interface *intr, *head;
5207 :
5208 : /* Use upper case to save the actual derived-type symbol. */
5209 132 : gfc_get_symbol (dt_name, NULL, &dt_sym);
5210 132 : dt_sym->name = gfc_get_string ("%s", sym->name);
5211 132 : head = sym->generic;
5212 132 : intr = gfc_get_interface ();
5213 132 : intr->sym = dt_sym;
5214 132 : intr->where = gfc_current_locus;
5215 132 : intr->next = head;
5216 132 : sym->generic = intr;
5217 132 : sym->attr.if_source = IFSRC_DECL;
5218 : }
5219 : else
5220 35463 : gfc_save_symbol_data (dt_sym);
5221 :
5222 35595 : gfc_set_sym_referenced (dt_sym);
5223 :
5224 132 : if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
5225 35727 : && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
5226 : return MATCH_ERROR;
5227 :
5228 35595 : ts->u.derived = dt_sym;
5229 :
5230 35595 : return MATCH_YES;
5231 :
5232 163863 : get_kind:
5233 163863 : if (matched_type
5234 163863 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
5235 : "intrinsic-type-spec at %C"))
5236 : return MATCH_ERROR;
5237 :
5238 : /* For all types except double, derived and character, look for an
5239 : optional kind specifier. MATCH_NO is actually OK at this point. */
5240 163860 : if (implicit_flag == 1)
5241 : {
5242 223 : if (matched_type && gfc_match_char (')') != MATCH_YES)
5243 : return MATCH_ERROR;
5244 :
5245 : return MATCH_YES;
5246 : }
5247 :
5248 163637 : if (gfc_current_form == FORM_FREE)
5249 : {
5250 148045 : c = gfc_peek_ascii_char ();
5251 148045 : if (!gfc_is_whitespace (c) && c != '*' && c != '('
5252 72989 : && c != ':' && c != ',')
5253 : {
5254 172 : if (matched_type && c == ')')
5255 : {
5256 3 : gfc_next_ascii_char ();
5257 3 : return MATCH_YES;
5258 : }
5259 169 : gfc_error ("Malformed type-spec at %C");
5260 169 : return MATCH_NO;
5261 : }
5262 : }
5263 :
5264 163465 : m = gfc_match_kind_spec (ts, false);
5265 163465 : if (m == MATCH_ERROR)
5266 : return MATCH_ERROR;
5267 :
5268 163429 : if (m == MATCH_NO && ts->type != BT_CHARACTER)
5269 : {
5270 110929 : m = gfc_match_old_kind_spec (ts);
5271 110929 : if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
5272 : return MATCH_ERROR;
5273 : }
5274 :
5275 163421 : if (matched_type && gfc_match_char (')') != MATCH_YES)
5276 : {
5277 0 : gfc_error ("Malformed type-spec at %C");
5278 0 : return MATCH_ERROR;
5279 : }
5280 :
5281 : /* Defer association of the KIND expression of function results
5282 : until after USE and IMPORT statements. */
5283 4444 : if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
5284 167838 : || gfc_matching_function)
5285 : return MATCH_YES;
5286 :
5287 155936 : if (m == MATCH_NO)
5288 157342 : m = MATCH_YES; /* No kind specifier found. */
5289 :
5290 : return m;
5291 : }
5292 :
5293 :
5294 : /* Match an IMPLICIT NONE statement. Actually, this statement is
5295 : already matched in parse.cc, or we would not end up here in the
5296 : first place. So the only thing we need to check, is if there is
5297 : trailing garbage. If not, the match is successful. */
5298 :
5299 : match
5300 24781 : gfc_match_implicit_none (void)
5301 : {
5302 24781 : char c;
5303 24781 : match m;
5304 24781 : char name[GFC_MAX_SYMBOL_LEN + 1];
5305 24781 : bool type = false;
5306 24781 : bool external = false;
5307 24781 : locus cur_loc = gfc_current_locus;
5308 :
5309 24781 : if (gfc_current_ns->seen_implicit_none
5310 24779 : || gfc_current_ns->has_implicit_none_export)
5311 : {
5312 4 : gfc_error ("Duplicate IMPLICIT NONE statement at %C");
5313 4 : return MATCH_ERROR;
5314 : }
5315 :
5316 24777 : gfc_gobble_whitespace ();
5317 24777 : c = gfc_peek_ascii_char ();
5318 24777 : if (c == '(')
5319 : {
5320 1112 : (void) gfc_next_ascii_char ();
5321 1112 : if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
5322 : return MATCH_ERROR;
5323 :
5324 1111 : gfc_gobble_whitespace ();
5325 1111 : if (gfc_peek_ascii_char () == ')')
5326 : {
5327 1 : (void) gfc_next_ascii_char ();
5328 1 : type = true;
5329 : }
5330 : else
5331 3306 : for(;;)
5332 : {
5333 2208 : m = gfc_match (" %n", name);
5334 2208 : if (m != MATCH_YES)
5335 : return MATCH_ERROR;
5336 :
5337 2208 : if (strcmp (name, "type") == 0)
5338 : type = true;
5339 1110 : else if (strcmp (name, "external") == 0)
5340 : external = true;
5341 : else
5342 : return MATCH_ERROR;
5343 :
5344 2208 : gfc_gobble_whitespace ();
5345 2208 : c = gfc_next_ascii_char ();
5346 2208 : if (c == ',')
5347 1098 : continue;
5348 1110 : if (c == ')')
5349 : break;
5350 : return MATCH_ERROR;
5351 : }
5352 : }
5353 : else
5354 : type = true;
5355 :
5356 24776 : if (gfc_match_eos () != MATCH_YES)
5357 : return MATCH_ERROR;
5358 :
5359 24776 : gfc_set_implicit_none (type, external, &cur_loc);
5360 :
5361 24776 : return MATCH_YES;
5362 : }
5363 :
5364 :
5365 : /* Match the letter range(s) of an IMPLICIT statement. */
5366 :
5367 : static match
5368 600 : match_implicit_range (void)
5369 : {
5370 600 : char c, c1, c2;
5371 600 : int inner;
5372 600 : locus cur_loc;
5373 :
5374 600 : cur_loc = gfc_current_locus;
5375 :
5376 600 : gfc_gobble_whitespace ();
5377 600 : c = gfc_next_ascii_char ();
5378 600 : if (c != '(')
5379 : {
5380 59 : gfc_error ("Missing character range in IMPLICIT at %C");
5381 59 : goto bad;
5382 : }
5383 :
5384 : inner = 1;
5385 1195 : while (inner)
5386 : {
5387 722 : gfc_gobble_whitespace ();
5388 722 : c1 = gfc_next_ascii_char ();
5389 722 : if (!ISALPHA (c1))
5390 33 : goto bad;
5391 :
5392 689 : gfc_gobble_whitespace ();
5393 689 : c = gfc_next_ascii_char ();
5394 :
5395 689 : switch (c)
5396 : {
5397 201 : case ')':
5398 201 : inner = 0; /* Fall through. */
5399 :
5400 : case ',':
5401 : c2 = c1;
5402 : break;
5403 :
5404 439 : case '-':
5405 439 : gfc_gobble_whitespace ();
5406 439 : c2 = gfc_next_ascii_char ();
5407 439 : if (!ISALPHA (c2))
5408 0 : goto bad;
5409 :
5410 439 : gfc_gobble_whitespace ();
5411 439 : c = gfc_next_ascii_char ();
5412 :
5413 439 : if ((c != ',') && (c != ')'))
5414 0 : goto bad;
5415 439 : if (c == ')')
5416 272 : inner = 0;
5417 :
5418 : break;
5419 :
5420 35 : default:
5421 35 : goto bad;
5422 : }
5423 :
5424 654 : if (c1 > c2)
5425 : {
5426 0 : gfc_error ("Letters must be in alphabetic order in "
5427 : "IMPLICIT statement at %C");
5428 0 : goto bad;
5429 : }
5430 :
5431 : /* See if we can add the newly matched range to the pending
5432 : implicits from this IMPLICIT statement. We do not check for
5433 : conflicts with whatever earlier IMPLICIT statements may have
5434 : set. This is done when we've successfully finished matching
5435 : the current one. */
5436 654 : if (!gfc_add_new_implicit_range (c1, c2))
5437 0 : goto bad;
5438 : }
5439 :
5440 : return MATCH_YES;
5441 :
5442 127 : bad:
5443 127 : gfc_syntax_error (ST_IMPLICIT);
5444 :
5445 127 : gfc_current_locus = cur_loc;
5446 127 : return MATCH_ERROR;
5447 : }
5448 :
5449 :
5450 : /* Match an IMPLICIT statement, storing the types for
5451 : gfc_set_implicit() if the statement is accepted by the parser.
5452 : There is a strange looking, but legal syntactic construction
5453 : possible. It looks like:
5454 :
5455 : IMPLICIT INTEGER (a-b) (c-d)
5456 :
5457 : This is legal if "a-b" is a constant expression that happens to
5458 : equal one of the legal kinds for integers. The real problem
5459 : happens with an implicit specification that looks like:
5460 :
5461 : IMPLICIT INTEGER (a-b)
5462 :
5463 : In this case, a typespec matcher that is "greedy" (as most of the
5464 : matchers are) gobbles the character range as a kindspec, leaving
5465 : nothing left. We therefore have to go a bit more slowly in the
5466 : matching process by inhibiting the kindspec checking during
5467 : typespec matching and checking for a kind later. */
5468 :
5469 : match
5470 25207 : gfc_match_implicit (void)
5471 : {
5472 25207 : gfc_typespec ts;
5473 25207 : locus cur_loc;
5474 25207 : char c;
5475 25207 : match m;
5476 :
5477 25207 : if (gfc_current_ns->seen_implicit_none)
5478 : {
5479 4 : gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
5480 : "statement");
5481 4 : return MATCH_ERROR;
5482 : }
5483 :
5484 25203 : gfc_clear_ts (&ts);
5485 :
5486 : /* We don't allow empty implicit statements. */
5487 25203 : if (gfc_match_eos () == MATCH_YES)
5488 : {
5489 0 : gfc_error ("Empty IMPLICIT statement at %C");
5490 0 : return MATCH_ERROR;
5491 : }
5492 :
5493 25232 : do
5494 : {
5495 : /* First cleanup. */
5496 25232 : gfc_clear_new_implicit ();
5497 :
5498 : /* A basic type is mandatory here. */
5499 25232 : m = gfc_match_decl_type_spec (&ts, 1);
5500 25232 : if (m == MATCH_ERROR)
5501 0 : goto error;
5502 25232 : if (m == MATCH_NO)
5503 24779 : goto syntax;
5504 :
5505 453 : cur_loc = gfc_current_locus;
5506 453 : m = match_implicit_range ();
5507 :
5508 453 : if (m == MATCH_YES)
5509 : {
5510 : /* We may have <TYPE> (<RANGE>). */
5511 326 : gfc_gobble_whitespace ();
5512 326 : c = gfc_peek_ascii_char ();
5513 326 : if (c == ',' || c == '\n' || c == ';' || c == '!')
5514 : {
5515 : /* Check for CHARACTER with no length parameter. */
5516 299 : if (ts.type == BT_CHARACTER && !ts.u.cl)
5517 : {
5518 32 : ts.kind = gfc_default_character_kind;
5519 32 : ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5520 32 : ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
5521 : NULL, 1);
5522 : }
5523 :
5524 : /* Record the Successful match. */
5525 299 : if (!gfc_merge_new_implicit (&ts))
5526 : return MATCH_ERROR;
5527 297 : if (c == ',')
5528 28 : c = gfc_next_ascii_char ();
5529 269 : else if (gfc_match_eos () == MATCH_ERROR)
5530 0 : goto error;
5531 297 : continue;
5532 : }
5533 :
5534 27 : gfc_current_locus = cur_loc;
5535 : }
5536 :
5537 : /* Discard the (incorrectly) matched range. */
5538 154 : gfc_clear_new_implicit ();
5539 :
5540 : /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
5541 154 : if (ts.type == BT_CHARACTER)
5542 74 : m = gfc_match_char_spec (&ts);
5543 80 : else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
5544 : {
5545 76 : m = gfc_match_kind_spec (&ts, false);
5546 76 : if (m == MATCH_NO)
5547 : {
5548 40 : m = gfc_match_old_kind_spec (&ts);
5549 40 : if (m == MATCH_ERROR)
5550 0 : goto error;
5551 40 : if (m == MATCH_NO)
5552 0 : goto syntax;
5553 : }
5554 : }
5555 154 : if (m == MATCH_ERROR)
5556 7 : goto error;
5557 :
5558 147 : m = match_implicit_range ();
5559 147 : if (m == MATCH_ERROR)
5560 0 : goto error;
5561 147 : if (m == MATCH_NO)
5562 : goto syntax;
5563 :
5564 147 : gfc_gobble_whitespace ();
5565 147 : c = gfc_next_ascii_char ();
5566 147 : if (c != ',' && gfc_match_eos () != MATCH_YES)
5567 0 : goto syntax;
5568 :
5569 147 : if (!gfc_merge_new_implicit (&ts))
5570 : return MATCH_ERROR;
5571 : }
5572 444 : while (c == ',');
5573 :
5574 : return MATCH_YES;
5575 :
5576 24779 : syntax:
5577 24779 : gfc_syntax_error (ST_IMPLICIT);
5578 :
5579 25207 : error:
5580 : return MATCH_ERROR;
5581 : }
5582 :
5583 :
5584 : /* Match the IMPORT statement. IMPORT was added to F2003 as
5585 :
5586 : R1209 import-stmt is IMPORT [[ :: ] import-name-list ]
5587 :
5588 : C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
5589 :
5590 : C1211 (R1209) Each import-name shall be the name of an entity in the
5591 : host scoping unit.
5592 :
5593 : under the description of an interface block. Under F2008, IMPORT was
5594 : split out of the interface block description to 12.4.3.3 and C1210
5595 : became
5596 :
5597 : C1210 (R1209) The IMPORT statement is allowed only in an interface-body
5598 : that is not a module procedure interface body.
5599 :
5600 : Finally, F2018, section 8.8, has changed the IMPORT statement to
5601 :
5602 : R867 import-stmt is IMPORT [[ :: ] import-name-list ]
5603 : or IMPORT, ONLY : import-name-list
5604 : or IMPORT, NONE
5605 : or IMPORT, ALL
5606 :
5607 : C896 (R867) An IMPORT statement shall not appear in the scoping unit of
5608 : a main-program, external-subprogram, module, or block-data.
5609 :
5610 : C897 (R867) Each import-name shall be the name of an entity in the host
5611 : scoping unit.
5612 :
5613 : C898 If any IMPORT statement in a scoping unit has an ONLY specifier,
5614 : all IMPORT statements in that scoping unit shall have an ONLY
5615 : specifier.
5616 :
5617 : C899 IMPORT, NONE shall not appear in the scoping unit of a submodule.
5618 :
5619 : C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
5620 : unit, no other IMPORT statement shall appear in that scoping unit.
5621 :
5622 : C8101 Within an interface body, an entity that is accessed by host
5623 : association shall be accessible by host or use association within
5624 : the host scoping unit, or explicitly declared prior to the interface
5625 : body.
5626 :
5627 : C8102 An entity whose name appears as an import-name or which is made
5628 : accessible by an IMPORT, ALL statement shall not appear in any
5629 : context described in 19.5.1.4 that would cause the host entity
5630 : of that name to be inaccessible. */
5631 :
5632 : match
5633 4101 : gfc_match_import (void)
5634 : {
5635 4101 : char name[GFC_MAX_SYMBOL_LEN + 1];
5636 4101 : match m;
5637 4101 : gfc_symbol *sym;
5638 4101 : gfc_symtree *st;
5639 4101 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
5640 4101 : importstate current_import_state = gfc_current_ns->import_state;
5641 :
5642 4101 : if (!f2018_allowed
5643 13 : && (gfc_current_ns->proc_name == NULL
5644 12 : || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
5645 : {
5646 3 : gfc_error ("IMPORT statement at %C only permitted in "
5647 : "an INTERFACE body");
5648 3 : return MATCH_ERROR;
5649 : }
5650 : else if (f2018_allowed
5651 4088 : && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
5652 4 : goto C897;
5653 :
5654 4084 : if (f2018_allowed
5655 4084 : && (current_import_state == IMPORT_ALL
5656 4084 : || current_import_state == IMPORT_NONE))
5657 2 : goto C8100;
5658 :
5659 4092 : if (gfc_current_ns->proc_name
5660 4091 : && gfc_current_ns->proc_name->attr.module_procedure)
5661 : {
5662 1 : gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
5663 : "in a module procedure interface body");
5664 1 : return MATCH_ERROR;
5665 : }
5666 :
5667 4091 : if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
5668 : return MATCH_ERROR;
5669 :
5670 4087 : gfc_current_ns->import_state = IMPORT_NOT_SET;
5671 4087 : if (f2018_allowed)
5672 : {
5673 4081 : if (gfc_match (" , none") == MATCH_YES)
5674 : {
5675 8 : if (current_import_state == IMPORT_ONLY)
5676 0 : goto C898;
5677 8 : if (gfc_current_state () == COMP_SUBMODULE)
5678 0 : goto C899;
5679 8 : gfc_current_ns->import_state = IMPORT_NONE;
5680 : }
5681 4073 : else if (gfc_match (" , only :") == MATCH_YES)
5682 : {
5683 19 : if (current_import_state != IMPORT_NOT_SET
5684 19 : && current_import_state != IMPORT_ONLY)
5685 0 : goto C898;
5686 19 : gfc_current_ns->import_state = IMPORT_ONLY;
5687 : }
5688 4054 : else if (gfc_match (" , all") == MATCH_YES)
5689 : {
5690 1 : if (current_import_state == IMPORT_ONLY)
5691 0 : goto C898;
5692 1 : gfc_current_ns->import_state = IMPORT_ALL;
5693 : }
5694 :
5695 4081 : if (current_import_state != IMPORT_NOT_SET
5696 6 : && (gfc_current_ns->import_state == IMPORT_NONE
5697 6 : || gfc_current_ns->import_state == IMPORT_ALL))
5698 0 : goto C8100;
5699 : }
5700 :
5701 : /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL. */
5702 4087 : if (gfc_match_eos () == MATCH_YES)
5703 : {
5704 : /* This is the F2008 variant. */
5705 340 : if (gfc_current_ns->import_state == IMPORT_NOT_SET)
5706 : {
5707 331 : if (current_import_state == IMPORT_ONLY)
5708 0 : goto C898;
5709 331 : gfc_current_ns->import_state = IMPORT_F2008;
5710 : }
5711 :
5712 : /* Host variables should be imported. */
5713 340 : if (gfc_current_ns->import_state != IMPORT_NONE)
5714 332 : gfc_current_ns->has_import_set = 1;
5715 : return MATCH_YES;
5716 : }
5717 :
5718 3747 : if (gfc_match (" ::") == MATCH_YES
5719 3747 : && gfc_current_ns->import_state != IMPORT_ONLY)
5720 : {
5721 1239 : if (gfc_match_eos () == MATCH_YES)
5722 1 : goto expecting_list;
5723 1238 : gfc_current_ns->import_state = IMPORT_F2008;
5724 : }
5725 2508 : else if (gfc_current_ns->import_state == IMPORT_ONLY)
5726 : {
5727 19 : if (gfc_match_eos () == MATCH_YES)
5728 0 : goto expecting_list;
5729 : }
5730 :
5731 4441 : for(;;)
5732 : {
5733 4441 : sym = NULL;
5734 4441 : m = gfc_match (" %n", name);
5735 4441 : switch (m)
5736 : {
5737 4441 : case MATCH_YES:
5738 : /* Before checking if the symbol is available from host
5739 : association into a SUBROUTINE or FUNCTION within an
5740 : INTERFACE, check if it is already in local scope. */
5741 4441 : gfc_find_symbol (name, gfc_current_ns, 1, &sym);
5742 4441 : if (sym
5743 25 : && gfc_state_stack->previous
5744 25 : && gfc_state_stack->previous->state == COMP_INTERFACE)
5745 : {
5746 2 : gfc_error ("import-name %qs at %C is in the "
5747 : "local scope", name);
5748 2 : return MATCH_ERROR;
5749 : }
5750 :
5751 4439 : if (gfc_current_ns->parent != NULL
5752 4439 : && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
5753 : {
5754 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5755 0 : return MATCH_ERROR;
5756 : }
5757 4439 : else if (!sym
5758 5 : && gfc_current_ns->proc_name
5759 4 : && gfc_current_ns->proc_name->ns->parent
5760 4440 : && gfc_find_symbol (name,
5761 : gfc_current_ns->proc_name->ns->parent,
5762 : 1, &sym))
5763 : {
5764 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5765 0 : return MATCH_ERROR;
5766 : }
5767 :
5768 4439 : if (sym == NULL)
5769 : {
5770 5 : if (gfc_current_ns->proc_name
5771 4 : && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
5772 : {
5773 1 : gfc_error ("Cannot IMPORT %qs from host scoping unit "
5774 : "at %C - does not exist.", name);
5775 1 : return MATCH_ERROR;
5776 : }
5777 : else
5778 : {
5779 : /* This might be a procedure that has not yet been parsed. If
5780 : so gfc_fixup_sibling_symbols will replace this symbol with
5781 : that of the procedure. */
5782 4 : gfc_get_sym_tree (name, gfc_current_ns, &st, false,
5783 : &gfc_current_locus);
5784 4 : st->n.sym->refs++;
5785 4 : st->n.sym->attr.imported = 1;
5786 4 : st->import_only = 1;
5787 4 : goto next_item;
5788 : }
5789 : }
5790 :
5791 4434 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5792 4434 : if (st && st->n.sym && st->n.sym->attr.imported)
5793 : {
5794 0 : gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
5795 : "at %C", name);
5796 0 : goto next_item;
5797 : }
5798 :
5799 4434 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
5800 4434 : st->n.sym = sym;
5801 4434 : sym->refs++;
5802 4434 : sym->attr.imported = 1;
5803 4434 : st->import_only = 1;
5804 :
5805 4434 : if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
5806 : {
5807 : /* The actual derived type is stored in a symtree with the first
5808 : letter of the name capitalized; the symtree with the all
5809 : lower-case name contains the associated generic function. */
5810 605 : st = gfc_new_symtree (&gfc_current_ns->sym_root,
5811 : gfc_dt_upper_string (name));
5812 605 : st->n.sym = sym;
5813 605 : sym->refs++;
5814 605 : sym->attr.imported = 1;
5815 605 : st->import_only = 1;
5816 : }
5817 :
5818 4434 : goto next_item;
5819 :
5820 : case MATCH_NO:
5821 : break;
5822 :
5823 : case MATCH_ERROR:
5824 : return MATCH_ERROR;
5825 : }
5826 :
5827 4438 : next_item:
5828 4438 : if (gfc_match_eos () == MATCH_YES)
5829 : break;
5830 695 : if (gfc_match_char (',') != MATCH_YES)
5831 0 : goto syntax;
5832 : }
5833 :
5834 : return MATCH_YES;
5835 :
5836 0 : syntax:
5837 0 : gfc_error ("Syntax error in IMPORT statement at %C");
5838 0 : return MATCH_ERROR;
5839 :
5840 4 : C897:
5841 4 : gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
5842 : "program, an external subprogram, a module or block data");
5843 4 : return MATCH_ERROR;
5844 :
5845 0 : C898:
5846 0 : gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
5847 : "a scoping unit has an ONLY specifier, can only have IMPORT "
5848 : "with an ONLY specifier");
5849 0 : return MATCH_ERROR;
5850 :
5851 0 : C899:
5852 0 : gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
5853 : " of a submodule as at %C");
5854 0 : return MATCH_ERROR;
5855 :
5856 2 : C8100:
5857 4 : gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
5858 : "%s has already been declared, which must be unique in the "
5859 : "scoping unit",
5860 2 : gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
5861 : "IMPORT, NONE");
5862 2 : return MATCH_ERROR;
5863 :
5864 1 : expecting_list:
5865 1 : gfc_error ("Expecting list of named entities at %C");
5866 1 : return MATCH_ERROR;
5867 : }
5868 :
5869 :
5870 : /* A minimal implementation of gfc_match without whitespace, escape
5871 : characters or variable arguments. Returns true if the next
5872 : characters match the TARGET template exactly. */
5873 :
5874 : static bool
5875 152213 : match_string_p (const char *target)
5876 : {
5877 152213 : const char *p;
5878 :
5879 952689 : for (p = target; *p; p++)
5880 800477 : if ((char) gfc_next_ascii_char () != *p)
5881 : return false;
5882 : return true;
5883 : }
5884 :
5885 : /* Matches an attribute specification including array specs. If
5886 : successful, leaves the variables current_attr and current_as
5887 : holding the specification. Also sets the colon_seen variable for
5888 : later use by matchers associated with initializations.
5889 :
5890 : This subroutine is a little tricky in the sense that we don't know
5891 : if we really have an attr-spec until we hit the double colon.
5892 : Until that time, we can only return MATCH_NO. This forces us to
5893 : check for duplicate specification at this level. */
5894 :
5895 : static match
5896 224430 : match_attr_spec (void)
5897 : {
5898 : /* Modifiers that can exist in a type statement. */
5899 224430 : enum
5900 : { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
5901 : DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
5902 : DECL_DIMENSION, DECL_EXTERNAL,
5903 : DECL_INTRINSIC, DECL_OPTIONAL,
5904 : DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
5905 : DECL_STATIC, DECL_AUTOMATIC,
5906 : DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
5907 : DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5908 : DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
5909 : };
5910 :
5911 : /* GFC_DECL_END is the sentinel, index starts at 0. */
5912 : #define NUM_DECL GFC_DECL_END
5913 :
5914 : /* Make sure that values from sym_intent are safe to be used here. */
5915 224430 : gcc_assert (INTENT_IN > 0);
5916 :
5917 224430 : locus start, seen_at[NUM_DECL];
5918 224430 : int seen[NUM_DECL];
5919 224430 : unsigned int d;
5920 224430 : const char *attr;
5921 224430 : match m;
5922 224430 : bool t;
5923 :
5924 224430 : gfc_clear_attr (¤t_attr);
5925 224430 : start = gfc_current_locus;
5926 :
5927 224430 : current_as = NULL;
5928 224430 : colon_seen = 0;
5929 224430 : attr_seen = 0;
5930 :
5931 : /* See if we get all of the keywords up to the final double colon. */
5932 6059610 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5933 5835180 : seen[d] = 0;
5934 :
5935 347716 : for (;;)
5936 : {
5937 347716 : char ch;
5938 :
5939 347716 : d = DECL_NONE;
5940 347716 : gfc_gobble_whitespace ();
5941 :
5942 347716 : ch = gfc_next_ascii_char ();
5943 347716 : if (ch == ':')
5944 : {
5945 : /* This is the successful exit condition for the loop. */
5946 190404 : if (gfc_next_ascii_char () == ':')
5947 : break;
5948 : }
5949 157312 : else if (ch == ',')
5950 : {
5951 123298 : gfc_gobble_whitespace ();
5952 123298 : switch (gfc_peek_ascii_char ())
5953 : {
5954 19248 : case 'a':
5955 19248 : gfc_next_ascii_char ();
5956 19248 : switch (gfc_next_ascii_char ())
5957 : {
5958 19177 : case 'l':
5959 19177 : if (match_string_p ("locatable"))
5960 : {
5961 : /* Matched "allocatable". */
5962 : d = DECL_ALLOCATABLE;
5963 : }
5964 : break;
5965 :
5966 30 : case 's':
5967 30 : if (match_string_p ("ynchronous"))
5968 : {
5969 : /* Matched "asynchronous". */
5970 : d = DECL_ASYNCHRONOUS;
5971 : }
5972 : break;
5973 :
5974 41 : case 'u':
5975 41 : if (match_string_p ("tomatic"))
5976 : {
5977 : /* Matched "automatic". */
5978 : d = DECL_AUTOMATIC;
5979 : }
5980 : break;
5981 : }
5982 : break;
5983 :
5984 164 : case 'b':
5985 : /* Try and match the bind(c). */
5986 164 : m = gfc_match_bind_c (NULL, true);
5987 164 : if (m == MATCH_YES)
5988 : d = DECL_IS_BIND_C;
5989 0 : else if (m == MATCH_ERROR)
5990 0 : goto cleanup;
5991 : break;
5992 :
5993 2194 : case 'c':
5994 2194 : gfc_next_ascii_char ();
5995 2194 : if ('o' != gfc_next_ascii_char ())
5996 : break;
5997 2193 : switch (gfc_next_ascii_char ())
5998 : {
5999 83 : case 'd':
6000 83 : if (match_string_p ("imension"))
6001 : {
6002 : d = DECL_CODIMENSION;
6003 : break;
6004 : }
6005 : /* FALLTHRU */
6006 2110 : case 'n':
6007 2110 : if (match_string_p ("tiguous"))
6008 : {
6009 : d = DECL_CONTIGUOUS;
6010 : break;
6011 : }
6012 : }
6013 : break;
6014 :
6015 19784 : case 'd':
6016 19784 : if (match_string_p ("dimension"))
6017 : d = DECL_DIMENSION;
6018 : break;
6019 :
6020 177 : case 'e':
6021 177 : if (match_string_p ("external"))
6022 : d = DECL_EXTERNAL;
6023 : break;
6024 :
6025 29080 : case 'i':
6026 29080 : if (match_string_p ("int"))
6027 : {
6028 29080 : ch = gfc_next_ascii_char ();
6029 29080 : if (ch == 'e')
6030 : {
6031 29074 : if (match_string_p ("nt"))
6032 : {
6033 : /* Matched "intent". */
6034 29073 : d = match_intent_spec ();
6035 29073 : if (d == INTENT_UNKNOWN)
6036 : {
6037 2 : m = MATCH_ERROR;
6038 2 : goto cleanup;
6039 : }
6040 : }
6041 : }
6042 6 : else if (ch == 'r')
6043 : {
6044 6 : if (match_string_p ("insic"))
6045 : {
6046 : /* Matched "intrinsic". */
6047 : d = DECL_INTRINSIC;
6048 : }
6049 : }
6050 : }
6051 : break;
6052 :
6053 401 : case 'k':
6054 401 : if (match_string_p ("kind"))
6055 : d = DECL_KIND;
6056 : break;
6057 :
6058 487 : case 'l':
6059 487 : if (match_string_p ("len"))
6060 : d = DECL_LEN;
6061 : break;
6062 :
6063 5292 : case 'o':
6064 5292 : if (match_string_p ("optional"))
6065 : d = DECL_OPTIONAL;
6066 : break;
6067 :
6068 27790 : case 'p':
6069 27790 : gfc_next_ascii_char ();
6070 27790 : switch (gfc_next_ascii_char ())
6071 : {
6072 14636 : case 'a':
6073 14636 : if (match_string_p ("rameter"))
6074 : {
6075 : /* Matched "parameter". */
6076 : d = DECL_PARAMETER;
6077 : }
6078 : break;
6079 :
6080 12632 : case 'o':
6081 12632 : if (match_string_p ("inter"))
6082 : {
6083 : /* Matched "pointer". */
6084 : d = DECL_POINTER;
6085 : }
6086 : break;
6087 :
6088 269 : case 'r':
6089 269 : ch = gfc_next_ascii_char ();
6090 269 : if (ch == 'i')
6091 : {
6092 218 : if (match_string_p ("vate"))
6093 : {
6094 : /* Matched "private". */
6095 : d = DECL_PRIVATE;
6096 : }
6097 : }
6098 51 : else if (ch == 'o')
6099 : {
6100 51 : if (match_string_p ("tected"))
6101 : {
6102 : /* Matched "protected". */
6103 : d = DECL_PROTECTED;
6104 : }
6105 : }
6106 : break;
6107 :
6108 253 : case 'u':
6109 253 : if (match_string_p ("blic"))
6110 : {
6111 : /* Matched "public". */
6112 : d = DECL_PUBLIC;
6113 : }
6114 : break;
6115 : }
6116 : break;
6117 :
6118 1223 : case 's':
6119 1223 : gfc_next_ascii_char ();
6120 1223 : switch (gfc_next_ascii_char ())
6121 : {
6122 1210 : case 'a':
6123 1210 : if (match_string_p ("ve"))
6124 : {
6125 : /* Matched "save". */
6126 : d = DECL_SAVE;
6127 : }
6128 : break;
6129 :
6130 13 : case 't':
6131 13 : if (match_string_p ("atic"))
6132 : {
6133 : /* Matched "static". */
6134 : d = DECL_STATIC;
6135 : }
6136 : break;
6137 : }
6138 : break;
6139 :
6140 5787 : case 't':
6141 5787 : if (match_string_p ("target"))
6142 : d = DECL_TARGET;
6143 : break;
6144 :
6145 11671 : case 'v':
6146 11671 : gfc_next_ascii_char ();
6147 11671 : ch = gfc_next_ascii_char ();
6148 11671 : if (ch == 'a')
6149 : {
6150 11150 : if (match_string_p ("lue"))
6151 : {
6152 : /* Matched "value". */
6153 : d = DECL_VALUE;
6154 : }
6155 : }
6156 521 : else if (ch == 'o')
6157 : {
6158 521 : if (match_string_p ("latile"))
6159 : {
6160 : /* Matched "volatile". */
6161 : d = DECL_VOLATILE;
6162 : }
6163 : }
6164 : break;
6165 : }
6166 : }
6167 :
6168 : /* No double colon and no recognizable decl_type, so assume that
6169 : we've been looking at something else the whole time. */
6170 : if (d == DECL_NONE)
6171 : {
6172 34017 : m = MATCH_NO;
6173 34017 : goto cleanup;
6174 : }
6175 :
6176 : /* Check to make sure any parens are paired up correctly. */
6177 123294 : if (gfc_match_parens () == MATCH_ERROR)
6178 : {
6179 1 : m = MATCH_ERROR;
6180 1 : goto cleanup;
6181 : }
6182 :
6183 123293 : seen[d]++;
6184 123293 : seen_at[d] = gfc_current_locus;
6185 :
6186 123293 : if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
6187 : {
6188 19866 : gfc_array_spec *as = NULL;
6189 :
6190 19866 : m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
6191 : d == DECL_CODIMENSION);
6192 :
6193 19866 : if (current_as == NULL)
6194 19841 : current_as = as;
6195 25 : else if (m == MATCH_YES)
6196 : {
6197 25 : if (!merge_array_spec (as, current_as, false))
6198 2 : m = MATCH_ERROR;
6199 25 : free (as);
6200 : }
6201 :
6202 19866 : if (m == MATCH_NO)
6203 : {
6204 0 : if (d == DECL_CODIMENSION)
6205 0 : gfc_error ("Missing codimension specification at %C");
6206 : else
6207 0 : gfc_error ("Missing dimension specification at %C");
6208 : m = MATCH_ERROR;
6209 : }
6210 :
6211 19866 : if (m == MATCH_ERROR)
6212 7 : goto cleanup;
6213 : }
6214 : }
6215 :
6216 : /* Since we've seen a double colon, we have to be looking at an
6217 : attr-spec. This means that we can now issue errors. */
6218 5140860 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
6219 4950459 : if (seen[d] > 1)
6220 : {
6221 2 : switch (d)
6222 : {
6223 : case DECL_ALLOCATABLE:
6224 : attr = "ALLOCATABLE";
6225 : break;
6226 0 : case DECL_ASYNCHRONOUS:
6227 0 : attr = "ASYNCHRONOUS";
6228 0 : break;
6229 0 : case DECL_CODIMENSION:
6230 0 : attr = "CODIMENSION";
6231 0 : break;
6232 0 : case DECL_CONTIGUOUS:
6233 0 : attr = "CONTIGUOUS";
6234 0 : break;
6235 0 : case DECL_DIMENSION:
6236 0 : attr = "DIMENSION";
6237 0 : break;
6238 0 : case DECL_EXTERNAL:
6239 0 : attr = "EXTERNAL";
6240 0 : break;
6241 0 : case DECL_IN:
6242 0 : attr = "INTENT (IN)";
6243 0 : break;
6244 0 : case DECL_OUT:
6245 0 : attr = "INTENT (OUT)";
6246 0 : break;
6247 0 : case DECL_INOUT:
6248 0 : attr = "INTENT (IN OUT)";
6249 0 : break;
6250 0 : case DECL_INTRINSIC:
6251 0 : attr = "INTRINSIC";
6252 0 : break;
6253 0 : case DECL_OPTIONAL:
6254 0 : attr = "OPTIONAL";
6255 0 : break;
6256 0 : case DECL_KIND:
6257 0 : attr = "KIND";
6258 0 : break;
6259 0 : case DECL_LEN:
6260 0 : attr = "LEN";
6261 0 : break;
6262 0 : case DECL_PARAMETER:
6263 0 : attr = "PARAMETER";
6264 0 : break;
6265 0 : case DECL_POINTER:
6266 0 : attr = "POINTER";
6267 0 : break;
6268 0 : case DECL_PROTECTED:
6269 0 : attr = "PROTECTED";
6270 0 : break;
6271 0 : case DECL_PRIVATE:
6272 0 : attr = "PRIVATE";
6273 0 : break;
6274 0 : case DECL_PUBLIC:
6275 0 : attr = "PUBLIC";
6276 0 : break;
6277 0 : case DECL_SAVE:
6278 0 : attr = "SAVE";
6279 0 : break;
6280 0 : case DECL_STATIC:
6281 0 : attr = "STATIC";
6282 0 : break;
6283 1 : case DECL_AUTOMATIC:
6284 1 : attr = "AUTOMATIC";
6285 1 : break;
6286 0 : case DECL_TARGET:
6287 0 : attr = "TARGET";
6288 0 : break;
6289 0 : case DECL_IS_BIND_C:
6290 0 : attr = "IS_BIND_C";
6291 0 : break;
6292 0 : case DECL_VALUE:
6293 0 : attr = "VALUE";
6294 0 : break;
6295 1 : case DECL_VOLATILE:
6296 1 : attr = "VOLATILE";
6297 1 : break;
6298 0 : default:
6299 0 : attr = NULL; /* This shouldn't happen. */
6300 : }
6301 :
6302 2 : gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
6303 2 : m = MATCH_ERROR;
6304 2 : goto cleanup;
6305 : }
6306 :
6307 : /* Now that we've dealt with duplicate attributes, add the attributes
6308 : to the current attribute. */
6309 5140040 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
6310 : {
6311 4949712 : if (seen[d] == 0)
6312 4826435 : continue;
6313 : else
6314 123277 : attr_seen = 1;
6315 :
6316 123277 : if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
6317 52 : && !flag_dec_static)
6318 : {
6319 3 : gfc_error ("%s at %L is a DEC extension, enable with "
6320 : "%<-fdec-static%>",
6321 : d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
6322 2 : m = MATCH_ERROR;
6323 2 : goto cleanup;
6324 : }
6325 : /* Allow SAVE with STATIC, but don't complain. */
6326 50 : if (d == DECL_STATIC && seen[DECL_SAVE])
6327 0 : continue;
6328 :
6329 123275 : if (gfc_comp_struct (gfc_current_state ())
6330 7435 : && d != DECL_DIMENSION && d != DECL_CODIMENSION
6331 6471 : && d != DECL_POINTER && d != DECL_PRIVATE
6332 4750 : && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
6333 : {
6334 4693 : bool is_derived = gfc_current_state () == COMP_DERIVED;
6335 4693 : if (d == DECL_ALLOCATABLE)
6336 : {
6337 3792 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6338 : ? G_("ALLOCATABLE attribute at %C in a "
6339 : "TYPE definition")
6340 : : G_("ALLOCATABLE attribute at %C in a "
6341 : "STRUCTURE definition")))
6342 : {
6343 2 : m = MATCH_ERROR;
6344 2 : goto cleanup;
6345 : }
6346 : }
6347 901 : else if (d == DECL_KIND)
6348 : {
6349 399 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6350 : ? G_("KIND attribute at %C in a "
6351 : "TYPE definition")
6352 : : G_("KIND attribute at %C in a "
6353 : "STRUCTURE definition")))
6354 : {
6355 1 : m = MATCH_ERROR;
6356 1 : goto cleanup;
6357 : }
6358 398 : if (current_ts.type != BT_INTEGER)
6359 : {
6360 2 : gfc_error ("Component with KIND attribute at %C must be "
6361 : "INTEGER");
6362 2 : m = MATCH_ERROR;
6363 2 : goto cleanup;
6364 : }
6365 : }
6366 502 : else if (d == DECL_LEN)
6367 : {
6368 486 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6369 : ? G_("LEN attribute at %C in a "
6370 : "TYPE definition")
6371 : : G_("LEN attribute at %C in a "
6372 : "STRUCTURE definition")))
6373 : {
6374 0 : m = MATCH_ERROR;
6375 0 : goto cleanup;
6376 : }
6377 486 : if (current_ts.type != BT_INTEGER)
6378 : {
6379 1 : gfc_error ("Component with LEN attribute at %C must be "
6380 : "INTEGER");
6381 1 : m = MATCH_ERROR;
6382 1 : goto cleanup;
6383 : }
6384 : }
6385 : else
6386 : {
6387 32 : gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
6388 : "TYPE definition")
6389 : : G_("Attribute at %L is not allowed in a "
6390 : "STRUCTURE definition"), &seen_at[d]);
6391 16 : m = MATCH_ERROR;
6392 16 : goto cleanup;
6393 : }
6394 : }
6395 :
6396 123253 : if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
6397 471 : && gfc_current_state () != COMP_MODULE)
6398 : {
6399 148 : if (d == DECL_PRIVATE)
6400 : attr = "PRIVATE";
6401 : else
6402 43 : attr = "PUBLIC";
6403 148 : if (gfc_current_state () == COMP_DERIVED
6404 142 : && gfc_state_stack->previous
6405 142 : && gfc_state_stack->previous->state == COMP_MODULE)
6406 : {
6407 139 : if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
6408 : "at %L in a TYPE definition", attr,
6409 : &seen_at[d]))
6410 : {
6411 2 : m = MATCH_ERROR;
6412 2 : goto cleanup;
6413 : }
6414 : }
6415 : else
6416 : {
6417 9 : gfc_error ("%s attribute at %L is not allowed outside of the "
6418 : "specification part of a module", attr, &seen_at[d]);
6419 9 : m = MATCH_ERROR;
6420 9 : goto cleanup;
6421 : }
6422 : }
6423 :
6424 123242 : if (gfc_current_state () != COMP_DERIVED
6425 115838 : && (d == DECL_KIND || d == DECL_LEN))
6426 : {
6427 3 : gfc_error ("Attribute at %L is not allowed outside a TYPE "
6428 : "definition", &seen_at[d]);
6429 3 : m = MATCH_ERROR;
6430 3 : goto cleanup;
6431 : }
6432 :
6433 123239 : switch (d)
6434 : {
6435 19175 : case DECL_ALLOCATABLE:
6436 19175 : t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
6437 19175 : break;
6438 :
6439 29 : case DECL_ASYNCHRONOUS:
6440 29 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
6441 : t = false;
6442 : else
6443 29 : t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
6444 : break;
6445 :
6446 81 : case DECL_CODIMENSION:
6447 81 : t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
6448 81 : break;
6449 :
6450 2110 : case DECL_CONTIGUOUS:
6451 2110 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
6452 : t = false;
6453 : else
6454 2109 : t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
6455 : break;
6456 :
6457 19776 : case DECL_DIMENSION:
6458 19776 : t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
6459 19776 : break;
6460 :
6461 176 : case DECL_EXTERNAL:
6462 176 : t = gfc_add_external (¤t_attr, &seen_at[d]);
6463 176 : break;
6464 :
6465 22016 : case DECL_IN:
6466 22016 : t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
6467 22016 : break;
6468 :
6469 3832 : case DECL_OUT:
6470 3832 : t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
6471 3832 : break;
6472 :
6473 3219 : case DECL_INOUT:
6474 3219 : t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
6475 3219 : break;
6476 :
6477 5 : case DECL_INTRINSIC:
6478 5 : t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
6479 5 : break;
6480 :
6481 5291 : case DECL_OPTIONAL:
6482 5291 : t = gfc_add_optional (¤t_attr, &seen_at[d]);
6483 5291 : break;
6484 :
6485 396 : case DECL_KIND:
6486 396 : t = gfc_add_kind (¤t_attr, &seen_at[d]);
6487 396 : break;
6488 :
6489 485 : case DECL_LEN:
6490 485 : t = gfc_add_len (¤t_attr, &seen_at[d]);
6491 485 : break;
6492 :
6493 14635 : case DECL_PARAMETER:
6494 14635 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
6495 14635 : break;
6496 :
6497 12631 : case DECL_POINTER:
6498 12631 : t = gfc_add_pointer (¤t_attr, &seen_at[d]);
6499 12631 : break;
6500 :
6501 50 : case DECL_PROTECTED:
6502 50 : if (gfc_current_state () != COMP_MODULE
6503 48 : || (gfc_current_ns->proc_name
6504 48 : && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
6505 : {
6506 2 : gfc_error ("PROTECTED at %C only allowed in specification "
6507 : "part of a module");
6508 2 : t = false;
6509 2 : break;
6510 : }
6511 :
6512 48 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
6513 : t = false;
6514 : else
6515 44 : t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
6516 : break;
6517 :
6518 215 : case DECL_PRIVATE:
6519 215 : t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
6520 : &seen_at[d]);
6521 215 : break;
6522 :
6523 245 : case DECL_PUBLIC:
6524 245 : t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
6525 : &seen_at[d]);
6526 245 : break;
6527 :
6528 1220 : case DECL_STATIC:
6529 1220 : case DECL_SAVE:
6530 1220 : t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
6531 1220 : break;
6532 :
6533 37 : case DECL_AUTOMATIC:
6534 37 : t = gfc_add_automatic (¤t_attr, NULL, &seen_at[d]);
6535 37 : break;
6536 :
6537 5785 : case DECL_TARGET:
6538 5785 : t = gfc_add_target (¤t_attr, &seen_at[d]);
6539 5785 : break;
6540 :
6541 163 : case DECL_IS_BIND_C:
6542 163 : t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
6543 163 : break;
6544 :
6545 11149 : case DECL_VALUE:
6546 11149 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
6547 : t = false;
6548 : else
6549 11149 : t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
6550 : break;
6551 :
6552 518 : case DECL_VOLATILE:
6553 518 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
6554 : t = false;
6555 : else
6556 517 : t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
6557 : break;
6558 :
6559 0 : default:
6560 0 : gfc_internal_error ("match_attr_spec(): Bad attribute");
6561 : }
6562 :
6563 123233 : if (!t)
6564 : {
6565 35 : m = MATCH_ERROR;
6566 35 : goto cleanup;
6567 : }
6568 : }
6569 :
6570 : /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
6571 190328 : if ((gfc_current_state () == COMP_MODULE
6572 190328 : || gfc_current_state () == COMP_SUBMODULE)
6573 6076 : && !current_attr.save
6574 5894 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
6575 5802 : current_attr.save = SAVE_IMPLICIT;
6576 :
6577 190328 : colon_seen = 1;
6578 190328 : return MATCH_YES;
6579 :
6580 34102 : cleanup:
6581 34102 : gfc_current_locus = start;
6582 34102 : gfc_free_array_spec (current_as);
6583 34102 : current_as = NULL;
6584 34102 : attr_seen = 0;
6585 34102 : return m;
6586 : }
6587 :
6588 :
6589 : /* Set the binding label, dest_label, either with the binding label
6590 : stored in the given gfc_typespec, ts, or if none was provided, it
6591 : will be the symbol name in all lower case, as required by the draft
6592 : (J3/04-007, section 15.4.1). If a binding label was given and
6593 : there is more than one argument (num_idents), it is an error. */
6594 :
6595 : static bool
6596 347 : set_binding_label (const char **dest_label, const char *sym_name,
6597 : int num_idents)
6598 : {
6599 347 : if (num_idents > 1 && has_name_equals)
6600 : {
6601 4 : gfc_error ("Multiple identifiers provided with "
6602 : "single NAME= specifier at %C");
6603 4 : return false;
6604 : }
6605 :
6606 343 : if (curr_binding_label)
6607 : /* Binding label given; store in temp holder till have sym. */
6608 108 : *dest_label = curr_binding_label;
6609 : else
6610 : {
6611 : /* No binding label given, and the NAME= specifier did not exist,
6612 : which means there was no NAME="". */
6613 235 : if (sym_name != NULL && has_name_equals == 0)
6614 205 : *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
6615 : }
6616 :
6617 : return true;
6618 : }
6619 :
6620 :
6621 : /* Set the status of the given common block as being BIND(C) or not,
6622 : depending on the given parameter, is_bind_c. */
6623 :
6624 : static void
6625 76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
6626 : {
6627 76 : com_block->is_bind_c = is_bind_c;
6628 76 : return;
6629 : }
6630 :
6631 :
6632 : /* Verify that the given gfc_typespec is for a C interoperable type. */
6633 :
6634 : bool
6635 21889 : gfc_verify_c_interop (gfc_typespec *ts)
6636 : {
6637 21889 : if (ts->type == BT_DERIVED && ts->u.derived != NULL)
6638 4320 : return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
6639 17569 : else if (ts->type == BT_CLASS)
6640 : return false;
6641 17561 : else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
6642 3983 : return false;
6643 :
6644 : return true;
6645 : }
6646 :
6647 :
6648 : /* Verify that the variables of a given common block, which has been
6649 : defined with the attribute specifier bind(c), to be of a C
6650 : interoperable type. Errors will be reported here, if
6651 : encountered. */
6652 :
6653 : bool
6654 1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
6655 : {
6656 1 : gfc_symbol *curr_sym = NULL;
6657 1 : bool retval = true;
6658 :
6659 1 : curr_sym = com_block->head;
6660 :
6661 : /* Make sure we have at least one symbol. */
6662 1 : if (curr_sym == NULL)
6663 : return retval;
6664 :
6665 : /* Here we know we have a symbol, so we'll execute this loop
6666 : at least once. */
6667 1 : do
6668 : {
6669 : /* The second to last param, 1, says this is in a common block. */
6670 1 : retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
6671 1 : curr_sym = curr_sym->common_next;
6672 1 : } while (curr_sym != NULL);
6673 :
6674 : return retval;
6675 : }
6676 :
6677 :
6678 : /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
6679 : an appropriate error message is reported. */
6680 :
6681 : bool
6682 7635 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
6683 : int is_in_common, gfc_common_head *com_block)
6684 : {
6685 7635 : bool bind_c_function = false;
6686 7635 : bool retval = true;
6687 :
6688 7635 : if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
6689 7635 : bind_c_function = true;
6690 :
6691 7635 : if (tmp_sym->attr.function && tmp_sym->result != NULL)
6692 : {
6693 3270 : tmp_sym = tmp_sym->result;
6694 : /* Make sure it wasn't an implicitly typed result. */
6695 3270 : if (tmp_sym->attr.implicit_type && warn_c_binding_type)
6696 : {
6697 1 : gfc_warning (OPT_Wc_binding_type,
6698 : "Implicitly declared BIND(C) function %qs at "
6699 : "%L may not be C interoperable", tmp_sym->name,
6700 : &tmp_sym->declared_at);
6701 1 : tmp_sym->ts.f90_type = tmp_sym->ts.type;
6702 : /* Mark it as C interoperable to prevent duplicate warnings. */
6703 1 : tmp_sym->ts.is_c_interop = 1;
6704 1 : tmp_sym->attr.is_c_interop = 1;
6705 : }
6706 : }
6707 :
6708 : /* Here, we know we have the bind(c) attribute, so if we have
6709 : enough type info, then verify that it's a C interop kind.
6710 : The info could be in the symbol already, or possibly still in
6711 : the given ts (current_ts), so look in both. */
6712 7635 : if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
6713 : {
6714 3429 : if (!gfc_verify_c_interop (&(tmp_sym->ts)))
6715 : {
6716 : /* See if we're dealing with a sym in a common block or not. */
6717 237 : if (is_in_common == 1 && warn_c_binding_type)
6718 : {
6719 0 : gfc_warning (OPT_Wc_binding_type,
6720 : "Variable %qs in common block %qs at %L "
6721 : "may not be a C interoperable "
6722 : "kind though common block %qs is BIND(C)",
6723 : tmp_sym->name, com_block->name,
6724 0 : &(tmp_sym->declared_at), com_block->name);
6725 : }
6726 : else
6727 : {
6728 237 : if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
6729 235 : || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
6730 : {
6731 3 : gfc_error ("Type declaration %qs at %L is not C "
6732 : "interoperable but it is BIND(C)",
6733 : tmp_sym->name, &(tmp_sym->declared_at));
6734 3 : retval = false;
6735 : }
6736 234 : else if (warn_c_binding_type)
6737 3 : gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
6738 : "may not be a C interoperable "
6739 : "kind but it is BIND(C)",
6740 : tmp_sym->name, &(tmp_sym->declared_at));
6741 : }
6742 : }
6743 :
6744 : /* Variables declared w/in a common block can't be bind(c)
6745 : since there's no way for C to see these variables, so there's
6746 : semantically no reason for the attribute. */
6747 3429 : if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
6748 : {
6749 1 : gfc_error ("Variable %qs in common block %qs at "
6750 : "%L cannot be declared with BIND(C) "
6751 : "since it is not a global",
6752 1 : tmp_sym->name, com_block->name,
6753 : &(tmp_sym->declared_at));
6754 1 : retval = false;
6755 : }
6756 :
6757 : /* Scalar variables that are bind(c) cannot have the pointer
6758 : or allocatable attributes. */
6759 3429 : if (tmp_sym->attr.is_bind_c == 1)
6760 : {
6761 2885 : if (tmp_sym->attr.pointer == 1)
6762 : {
6763 1 : gfc_error ("Variable %qs at %L cannot have both the "
6764 : "POINTER and BIND(C) attributes",
6765 : tmp_sym->name, &(tmp_sym->declared_at));
6766 1 : retval = false;
6767 : }
6768 :
6769 2885 : if (tmp_sym->attr.allocatable == 1)
6770 : {
6771 0 : gfc_error ("Variable %qs at %L cannot have both the "
6772 : "ALLOCATABLE and BIND(C) attributes",
6773 : tmp_sym->name, &(tmp_sym->declared_at));
6774 0 : retval = false;
6775 : }
6776 :
6777 : }
6778 :
6779 : /* If it is a BIND(C) function, make sure the return value is a
6780 : scalar value. The previous tests in this function made sure
6781 : the type is interoperable. */
6782 3429 : if (bind_c_function && tmp_sym->as != NULL)
6783 2 : gfc_error ("Return type of BIND(C) function %qs at %L cannot "
6784 : "be an array", tmp_sym->name, &(tmp_sym->declared_at));
6785 :
6786 : /* BIND(C) functions cannot return a character string. */
6787 3270 : if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
6788 116 : if (!gfc_length_one_character_type_p (&tmp_sym->ts))
6789 4 : gfc_error ("Return type of BIND(C) function %qs of character "
6790 : "type at %L must have length 1", tmp_sym->name,
6791 : &(tmp_sym->declared_at));
6792 : }
6793 :
6794 : /* See if the symbol has been marked as private. If it has, warn if
6795 : there is a binding label with default binding name. */
6796 7635 : if (tmp_sym->attr.access == ACCESS_PRIVATE
6797 11 : && tmp_sym->binding_label
6798 8 : && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
6799 5 : && (tmp_sym->attr.flavor == FL_VARIABLE
6800 4 : || tmp_sym->attr.if_source == IFSRC_DECL))
6801 4 : gfc_warning (OPT_Wsurprising,
6802 : "Symbol %qs at %L is marked PRIVATE but is accessible "
6803 : "via its default binding name %qs", tmp_sym->name,
6804 : &(tmp_sym->declared_at), tmp_sym->binding_label);
6805 :
6806 7635 : return retval;
6807 : }
6808 :
6809 :
6810 : /* Set the appropriate fields for a symbol that's been declared as
6811 : BIND(C) (the is_bind_c flag and the binding label), and verify that
6812 : the type is C interoperable. Errors are reported by the functions
6813 : used to set/test these fields. */
6814 :
6815 : static bool
6816 47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
6817 : {
6818 47 : bool retval = true;
6819 :
6820 : /* TODO: Do we need to make sure the vars aren't marked private? */
6821 :
6822 : /* Set the is_bind_c bit in symbol_attribute. */
6823 47 : gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
6824 :
6825 47 : if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
6826 : return false;
6827 :
6828 : return retval;
6829 : }
6830 :
6831 :
6832 : /* Set the fields marking the given common block as BIND(C), including
6833 : a binding label, and report any errors encountered. */
6834 :
6835 : static bool
6836 76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
6837 : {
6838 76 : bool retval = true;
6839 :
6840 : /* destLabel, common name, typespec (which may have binding label). */
6841 76 : if (!set_binding_label (&com_block->binding_label, com_block->name,
6842 : num_idents))
6843 : return false;
6844 :
6845 : /* Set the given common block (com_block) to being bind(c) (1). */
6846 76 : set_com_block_bind_c (com_block, 1);
6847 :
6848 76 : return retval;
6849 : }
6850 :
6851 :
6852 : /* Retrieve the list of one or more identifiers that the given bind(c)
6853 : attribute applies to. */
6854 :
6855 : static bool
6856 102 : get_bind_c_idents (void)
6857 : {
6858 102 : char name[GFC_MAX_SYMBOL_LEN + 1];
6859 102 : int num_idents = 0;
6860 102 : gfc_symbol *tmp_sym = NULL;
6861 102 : match found_id;
6862 102 : gfc_common_head *com_block = NULL;
6863 :
6864 102 : if (gfc_match_name (name) == MATCH_YES)
6865 : {
6866 38 : found_id = MATCH_YES;
6867 38 : gfc_get_ha_symbol (name, &tmp_sym);
6868 : }
6869 64 : else if (gfc_match_common_name (name) == MATCH_YES)
6870 : {
6871 64 : found_id = MATCH_YES;
6872 64 : com_block = gfc_get_common (name, 0);
6873 : }
6874 : else
6875 : {
6876 0 : gfc_error ("Need either entity or common block name for "
6877 : "attribute specification statement at %C");
6878 0 : return false;
6879 : }
6880 :
6881 : /* Save the current identifier and look for more. */
6882 123 : do
6883 : {
6884 : /* Increment the number of identifiers found for this spec stmt. */
6885 123 : num_idents++;
6886 :
6887 : /* Make sure we have a sym or com block, and verify that it can
6888 : be bind(c). Set the appropriate field(s) and look for more
6889 : identifiers. */
6890 123 : if (tmp_sym != NULL || com_block != NULL)
6891 : {
6892 123 : if (tmp_sym != NULL)
6893 : {
6894 47 : if (!set_verify_bind_c_sym (tmp_sym, num_idents))
6895 : return false;
6896 : }
6897 : else
6898 : {
6899 76 : if (!set_verify_bind_c_com_block (com_block, num_idents))
6900 : return false;
6901 : }
6902 :
6903 : /* Look to see if we have another identifier. */
6904 122 : tmp_sym = NULL;
6905 122 : if (gfc_match_eos () == MATCH_YES)
6906 : found_id = MATCH_NO;
6907 21 : else if (gfc_match_char (',') != MATCH_YES)
6908 : found_id = MATCH_NO;
6909 21 : else if (gfc_match_name (name) == MATCH_YES)
6910 : {
6911 9 : found_id = MATCH_YES;
6912 9 : gfc_get_ha_symbol (name, &tmp_sym);
6913 : }
6914 12 : else if (gfc_match_common_name (name) == MATCH_YES)
6915 : {
6916 12 : found_id = MATCH_YES;
6917 12 : com_block = gfc_get_common (name, 0);
6918 : }
6919 : else
6920 : {
6921 0 : gfc_error ("Missing entity or common block name for "
6922 : "attribute specification statement at %C");
6923 0 : return false;
6924 : }
6925 : }
6926 : else
6927 : {
6928 0 : gfc_internal_error ("Missing symbol");
6929 : }
6930 122 : } while (found_id == MATCH_YES);
6931 :
6932 : /* if we get here we were successful */
6933 : return true;
6934 : }
6935 :
6936 :
6937 : /* Try and match a BIND(C) attribute specification statement. */
6938 :
6939 : match
6940 140 : gfc_match_bind_c_stmt (void)
6941 : {
6942 140 : match found_match = MATCH_NO;
6943 140 : gfc_typespec *ts;
6944 :
6945 140 : ts = ¤t_ts;
6946 :
6947 : /* This may not be necessary. */
6948 140 : gfc_clear_ts (ts);
6949 : /* Clear the temporary binding label holder. */
6950 140 : curr_binding_label = NULL;
6951 :
6952 : /* Look for the bind(c). */
6953 140 : found_match = gfc_match_bind_c (NULL, true);
6954 :
6955 140 : if (found_match == MATCH_YES)
6956 : {
6957 103 : if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6958 : return MATCH_ERROR;
6959 :
6960 : /* Look for the :: now, but it is not required. */
6961 102 : gfc_match (" :: ");
6962 :
6963 : /* Get the identifier(s) that needs to be updated. This may need to
6964 : change to hand the flag(s) for the attr specified so all identifiers
6965 : found can have all appropriate parts updated (assuming that the same
6966 : spec stmt can have multiple attrs, such as both bind(c) and
6967 : allocatable...). */
6968 102 : if (!get_bind_c_idents ())
6969 : /* Error message should have printed already. */
6970 1 : return MATCH_ERROR;
6971 : }
6972 :
6973 : return found_match;
6974 : }
6975 :
6976 :
6977 : /* Match a data declaration statement. */
6978 :
6979 : match
6980 1055126 : gfc_match_data_decl (void)
6981 : {
6982 1055126 : gfc_symbol *sym;
6983 1055126 : match m;
6984 1055126 : int elem;
6985 1055126 : gfc_component *comp_tail = NULL;
6986 :
6987 1055126 : type_param_spec_list = NULL;
6988 1055126 : decl_type_param_list = NULL;
6989 :
6990 1055126 : num_idents_on_line = 0;
6991 :
6992 : /* Record the last component before we start, so that we can roll back
6993 : any components added during this statement on error. PR106946.
6994 : Must be set before any 'goto cleanup' with m == MATCH_ERROR. */
6995 1055126 : if (gfc_comp_struct (gfc_current_state ()))
6996 : {
6997 34466 : gfc_symbol *block = gfc_current_block ();
6998 34466 : if (block)
6999 : {
7000 34466 : comp_tail = block->components;
7001 34466 : if (comp_tail)
7002 36783 : while (comp_tail->next)
7003 : comp_tail = comp_tail->next;
7004 : }
7005 : }
7006 :
7007 1055126 : m = gfc_match_decl_type_spec (¤t_ts, 0);
7008 1055126 : if (m != MATCH_YES)
7009 : return m;
7010 :
7011 223246 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
7012 37014 : && !gfc_comp_struct (gfc_current_state ()))
7013 : {
7014 33327 : sym = gfc_use_derived (current_ts.u.derived);
7015 :
7016 33327 : if (sym == NULL)
7017 : {
7018 22 : m = MATCH_ERROR;
7019 22 : goto cleanup;
7020 : }
7021 :
7022 33305 : current_ts.u.derived = sym;
7023 : }
7024 :
7025 223224 : m = match_attr_spec ();
7026 223224 : if (m == MATCH_ERROR)
7027 : {
7028 84 : m = MATCH_NO;
7029 84 : goto cleanup;
7030 : }
7031 :
7032 : /* F2018:C708. */
7033 223140 : if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
7034 : {
7035 6 : gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
7036 6 : m = MATCH_ERROR;
7037 6 : goto cleanup;
7038 : }
7039 :
7040 223134 : if (current_ts.type == BT_CLASS
7041 11527 : && current_ts.u.derived->attr.unlimited_polymorphic)
7042 2059 : goto ok;
7043 :
7044 221075 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
7045 34926 : && current_ts.u.derived->components == NULL
7046 2885 : && !current_ts.u.derived->attr.zero_comp)
7047 : {
7048 :
7049 210 : if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
7050 136 : goto ok;
7051 :
7052 74 : if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
7053 47 : goto ok;
7054 :
7055 27 : gfc_find_symbol (current_ts.u.derived->name,
7056 27 : current_ts.u.derived->ns, 1, &sym);
7057 :
7058 : /* Any symbol that we find had better be a type definition
7059 : which has its components defined, or be a structure definition
7060 : actively being parsed. */
7061 27 : if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
7062 26 : && (current_ts.u.derived->components != NULL
7063 26 : || current_ts.u.derived->attr.zero_comp
7064 26 : || current_ts.u.derived == gfc_new_block))
7065 26 : goto ok;
7066 :
7067 1 : gfc_error ("Derived type at %C has not been previously defined "
7068 : "and so cannot appear in a derived type definition");
7069 1 : m = MATCH_ERROR;
7070 1 : goto cleanup;
7071 : }
7072 :
7073 220865 : ok:
7074 : /* If we have an old-style character declaration, and no new-style
7075 : attribute specifications, then there a comma is optional between
7076 : the type specification and the variable list. */
7077 223133 : if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
7078 1407 : gfc_match_char (',');
7079 :
7080 : /* Give the types/attributes to symbols that follow. Give the element
7081 : a number so that repeat character length expressions can be copied. */
7082 223133 : elem = 1;
7083 289533 : for (;;)
7084 : {
7085 289533 : num_idents_on_line++;
7086 289533 : m = variable_decl (elem++);
7087 289531 : if (m == MATCH_ERROR)
7088 413 : goto cleanup;
7089 289118 : if (m == MATCH_NO)
7090 : break;
7091 :
7092 289107 : if (gfc_match_eos () == MATCH_YES)
7093 222683 : goto cleanup;
7094 66424 : if (gfc_match_char (',') != MATCH_YES)
7095 : break;
7096 : }
7097 :
7098 35 : if (!gfc_error_flag_test ())
7099 : {
7100 : /* An anonymous structure declaration is unambiguous; if we matched one
7101 : according to gfc_match_structure_decl, we need to return MATCH_YES
7102 : here to avoid confusing the remaining matchers, even if there was an
7103 : error during variable_decl. We must flush any such errors. Note this
7104 : causes the parser to gracefully continue parsing the remaining input
7105 : as a structure body, which likely follows. */
7106 11 : if (current_ts.type == BT_DERIVED && current_ts.u.derived
7107 1 : && gfc_fl_struct (current_ts.u.derived->attr.flavor))
7108 : {
7109 1 : gfc_error_now ("Syntax error in anonymous structure declaration"
7110 : " at %C");
7111 : /* Skip the bad variable_decl and line up for the start of the
7112 : structure body. */
7113 1 : gfc_error_recovery ();
7114 1 : m = MATCH_YES;
7115 1 : goto cleanup;
7116 : }
7117 :
7118 10 : gfc_error ("Syntax error in data declaration at %C");
7119 : }
7120 :
7121 34 : m = MATCH_ERROR;
7122 :
7123 34 : gfc_free_data_all (gfc_current_ns);
7124 :
7125 223244 : cleanup:
7126 : /* If we failed inside a derived type definition, remove any CLASS
7127 : components that were added during this failed statement. For CLASS
7128 : components, gfc_build_class_symbol creates an extra container symbol in
7129 : the namespace outside the normal undo machinery. When reject_statement
7130 : later calls gfc_undo_symbols, the declaration state is rolled back but
7131 : that helper symbol survives and leaves the component dangling. Ordinary
7132 : components do not create that extra helper symbol, so leave them in
7133 : place for the usual follow-up diagnostics. PR106946.
7134 :
7135 : CLASS containers are shared between components of the same class type
7136 : and attributes (gfc_build_class_symbol reuses existing containers).
7137 : We must not free a container that is still referenced by a previously
7138 : committed component. Unlink and free the components first, then clean
7139 : up only orphaned containers. PR124482. */
7140 223244 : if (m == MATCH_ERROR && gfc_comp_struct (gfc_current_state ()))
7141 : {
7142 86 : gfc_symbol *block = gfc_current_block ();
7143 86 : if (block)
7144 : {
7145 86 : gfc_component **prev;
7146 86 : if (comp_tail)
7147 43 : prev = &comp_tail->next;
7148 : else
7149 43 : prev = &block->components;
7150 :
7151 : /* Record the CLASS container from the removed components.
7152 : Normally all components in one declaration share a single
7153 : container, but per-variable array specs can produce
7154 : additional ones; any beyond the first are harmlessly
7155 : leaked until namespace destruction. */
7156 86 : gfc_symbol *fclass_container = NULL;
7157 :
7158 120 : while (*prev)
7159 : {
7160 34 : gfc_component *c = *prev;
7161 34 : if (c->ts.type == BT_CLASS && c->ts.u.derived
7162 6 : && c->ts.u.derived->attr.is_class)
7163 : {
7164 3 : *prev = c->next;
7165 3 : if (!fclass_container)
7166 3 : fclass_container = c->ts.u.derived;
7167 3 : c->ts.u.derived = NULL;
7168 3 : gfc_free_component (c);
7169 : }
7170 : else
7171 31 : prev = &c->next;
7172 : }
7173 :
7174 : /* Free the container only if no remaining component still
7175 : references it. CLASS containers are shared between
7176 : components of the same class type and attributes
7177 : (gfc_build_class_symbol reuses existing ones). */
7178 86 : if (fclass_container)
7179 : {
7180 3 : bool shared = false;
7181 3 : for (gfc_component *q = block->components; q; q = q->next)
7182 1 : if (q->ts.type == BT_CLASS
7183 1 : && q->ts.u.derived == fclass_container)
7184 : {
7185 : shared = true;
7186 : break;
7187 : }
7188 3 : if (!shared)
7189 : {
7190 2 : if (gfc_find_symtree (fclass_container->ns->sym_root,
7191 : fclass_container->name))
7192 2 : gfc_delete_symtree (&fclass_container->ns->sym_root,
7193 : fclass_container->name);
7194 2 : gfc_release_symbol (fclass_container);
7195 : }
7196 : }
7197 : }
7198 : }
7199 :
7200 223244 : if (saved_kind_expr)
7201 384 : gfc_free_expr (saved_kind_expr);
7202 223244 : if (type_param_spec_list)
7203 1483 : gfc_free_actual_arglist (type_param_spec_list);
7204 223244 : if (decl_type_param_list)
7205 1452 : gfc_free_actual_arglist (decl_type_param_list);
7206 223244 : saved_kind_expr = NULL;
7207 223244 : gfc_free_array_spec (current_as);
7208 223244 : current_as = NULL;
7209 223244 : return m;
7210 : }
7211 :
7212 : static bool
7213 25419 : in_module_or_interface(void)
7214 : {
7215 25419 : if (gfc_current_state () == COMP_MODULE
7216 25419 : || gfc_current_state () == COMP_SUBMODULE
7217 25419 : || gfc_current_state () == COMP_INTERFACE)
7218 : return true;
7219 :
7220 21364 : if (gfc_state_stack->state == COMP_CONTAINS
7221 20428 : || gfc_state_stack->state == COMP_FUNCTION
7222 20322 : || gfc_state_stack->state == COMP_SUBROUTINE)
7223 : {
7224 1042 : gfc_state_data *p;
7225 1086 : for (p = gfc_state_stack->previous; p ; p = p->previous)
7226 : {
7227 1082 : if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
7228 118 : || p->state == COMP_INTERFACE)
7229 : return true;
7230 : }
7231 : }
7232 : return false;
7233 : }
7234 :
7235 : /* Match a prefix associated with a function or subroutine
7236 : declaration. If the typespec pointer is nonnull, then a typespec
7237 : can be matched. Note that if nothing matches, MATCH_YES is
7238 : returned (the null string was matched). */
7239 :
7240 : match
7241 250594 : gfc_match_prefix (gfc_typespec *ts)
7242 : {
7243 250594 : bool seen_type;
7244 250594 : bool seen_impure;
7245 250594 : bool found_prefix;
7246 :
7247 250594 : gfc_clear_attr (¤t_attr);
7248 250594 : seen_type = false;
7249 250594 : seen_impure = false;
7250 :
7251 250594 : gcc_assert (!gfc_matching_prefix);
7252 250594 : gfc_matching_prefix = true;
7253 :
7254 260424 : do
7255 : {
7256 281050 : found_prefix = false;
7257 :
7258 : /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
7259 : corresponding attribute seems natural and distinguishes these
7260 : procedures from procedure types of PROC_MODULE, which these are
7261 : as well. */
7262 281050 : if (gfc_match ("module% ") == MATCH_YES)
7263 : {
7264 25694 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
7265 275 : goto error;
7266 :
7267 25419 : if (!in_module_or_interface ())
7268 : {
7269 20326 : gfc_error ("MODULE prefix at %C found outside of a module, "
7270 : "submodule, or interface");
7271 20326 : goto error;
7272 : }
7273 :
7274 5093 : current_attr.module_procedure = 1;
7275 5093 : found_prefix = true;
7276 : }
7277 :
7278 260449 : if (!seen_type && ts != NULL)
7279 : {
7280 140312 : match m;
7281 140312 : m = gfc_match_decl_type_spec (ts, 0);
7282 140312 : if (m == MATCH_ERROR)
7283 15 : goto error;
7284 140297 : if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
7285 : {
7286 : seen_type = true;
7287 : found_prefix = true;
7288 : }
7289 : }
7290 :
7291 260434 : if (gfc_match ("elemental% ") == MATCH_YES)
7292 : {
7293 5407 : if (!gfc_add_elemental (¤t_attr, NULL))
7294 2 : goto error;
7295 :
7296 : found_prefix = true;
7297 : }
7298 :
7299 260432 : if (gfc_match ("pure% ") == MATCH_YES)
7300 : {
7301 2490 : if (!gfc_add_pure (¤t_attr, NULL))
7302 2 : goto error;
7303 :
7304 : found_prefix = true;
7305 : }
7306 :
7307 260430 : if (gfc_match ("recursive% ") == MATCH_YES)
7308 : {
7309 469 : if (!gfc_add_recursive (¤t_attr, NULL))
7310 2 : goto error;
7311 :
7312 : found_prefix = true;
7313 : }
7314 :
7315 : /* IMPURE is a somewhat special case, as it needs not set an actual
7316 : attribute but rather only prevents ELEMENTAL routines from being
7317 : automatically PURE. */
7318 260428 : if (gfc_match ("impure% ") == MATCH_YES)
7319 : {
7320 729 : if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
7321 4 : goto error;
7322 :
7323 : seen_impure = true;
7324 : found_prefix = true;
7325 : }
7326 : }
7327 : while (found_prefix);
7328 :
7329 : /* IMPURE and PURE must not both appear, of course. */
7330 229968 : if (seen_impure && current_attr.pure)
7331 : {
7332 4 : gfc_error ("PURE and IMPURE must not appear both at %C");
7333 4 : goto error;
7334 : }
7335 :
7336 : /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
7337 229243 : if (!seen_impure && current_attr.elemental && !current_attr.pure)
7338 : {
7339 4712 : if (!gfc_add_pure (¤t_attr, NULL))
7340 0 : goto error;
7341 : }
7342 :
7343 : /* At this point, the next item is not a prefix. */
7344 229964 : gcc_assert (gfc_matching_prefix);
7345 :
7346 229964 : gfc_matching_prefix = false;
7347 229964 : return MATCH_YES;
7348 :
7349 20630 : error:
7350 20630 : gcc_assert (gfc_matching_prefix);
7351 20630 : gfc_matching_prefix = false;
7352 20630 : return MATCH_ERROR;
7353 : }
7354 :
7355 :
7356 : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
7357 :
7358 : static bool
7359 65266 : copy_prefix (symbol_attribute *dest, locus *where)
7360 : {
7361 65266 : if (dest->module_procedure)
7362 : {
7363 786 : if (current_attr.elemental)
7364 13 : dest->elemental = 1;
7365 :
7366 786 : if (current_attr.pure)
7367 61 : dest->pure = 1;
7368 :
7369 786 : if (current_attr.recursive)
7370 8 : dest->recursive = 1;
7371 :
7372 : /* Module procedures are unusual in that the 'dest' is copied from
7373 : the interface declaration. However, this is an opportunity to
7374 : check that the submodule declaration is compliant with the
7375 : interface. */
7376 786 : if (dest->elemental && !current_attr.elemental)
7377 : {
7378 1 : gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
7379 : "missing at %L", where);
7380 1 : return false;
7381 : }
7382 :
7383 785 : if (dest->pure && !current_attr.pure)
7384 : {
7385 1 : gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
7386 : "missing at %L", where);
7387 1 : return false;
7388 : }
7389 :
7390 784 : if (dest->recursive && !current_attr.recursive)
7391 : {
7392 1 : gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
7393 : "missing at %L", where);
7394 1 : return false;
7395 : }
7396 :
7397 : return true;
7398 : }
7399 :
7400 64480 : if (current_attr.elemental && !gfc_add_elemental (dest, where))
7401 : return false;
7402 :
7403 64478 : if (current_attr.pure && !gfc_add_pure (dest, where))
7404 : return false;
7405 :
7406 64478 : if (current_attr.recursive && !gfc_add_recursive (dest, where))
7407 : return false;
7408 :
7409 : return true;
7410 : }
7411 :
7412 :
7413 : /* Match a formal argument list or, if typeparam is true, a
7414 : type_param_name_list. */
7415 :
7416 : match
7417 504248 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
7418 : int null_flag, bool typeparam)
7419 : {
7420 504248 : gfc_formal_arglist *head, *tail, *p, *q;
7421 504248 : char name[GFC_MAX_SYMBOL_LEN + 1];
7422 504248 : gfc_symbol *sym;
7423 504248 : match m;
7424 504248 : gfc_formal_arglist *formal = NULL;
7425 :
7426 504248 : head = tail = NULL;
7427 :
7428 : /* Keep the interface formal argument list and null it so that the
7429 : matching for the new declaration can be done. The numbers and
7430 : names of the arguments are checked here. The interface formal
7431 : arguments are retained in formal_arglist and the characteristics
7432 : are compared in resolve.cc(resolve_fl_procedure). See the remark
7433 : in get_proc_name about the eventual need to copy the formal_arglist
7434 : and populate the formal namespace of the interface symbol. */
7435 504248 : if (progname->attr.module_procedure
7436 790 : && progname->attr.host_assoc)
7437 : {
7438 214 : formal = progname->formal;
7439 214 : progname->formal = NULL;
7440 : }
7441 :
7442 504248 : if (gfc_match_char ('(') != MATCH_YES)
7443 : {
7444 297697 : if (null_flag)
7445 6826 : goto ok;
7446 : return MATCH_NO;
7447 : }
7448 :
7449 206551 : if (gfc_match_char (')') == MATCH_YES)
7450 : {
7451 10764 : if (typeparam)
7452 : {
7453 1 : gfc_error_now ("A type parameter list is required at %C");
7454 1 : m = MATCH_ERROR;
7455 1 : goto cleanup;
7456 : }
7457 : else
7458 10763 : goto ok;
7459 : }
7460 :
7461 258952 : for (;;)
7462 : {
7463 258952 : gfc_gobble_whitespace ();
7464 258952 : if (gfc_match_char ('*') == MATCH_YES)
7465 : {
7466 10527 : sym = NULL;
7467 10527 : if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
7468 : "Alternate-return argument at %C"))
7469 : {
7470 1 : m = MATCH_ERROR;
7471 1 : goto cleanup;
7472 : }
7473 10526 : else if (typeparam)
7474 2 : gfc_error_now ("A parameter name is required at %C");
7475 : }
7476 : else
7477 : {
7478 248425 : locus loc = gfc_current_locus;
7479 248425 : m = gfc_match_name (name);
7480 248425 : if (m != MATCH_YES)
7481 : {
7482 17090 : if(typeparam)
7483 1 : gfc_error_now ("A parameter name is required at %C");
7484 17106 : goto cleanup;
7485 : }
7486 231335 : loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
7487 :
7488 231335 : if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
7489 16 : goto cleanup;
7490 231319 : else if (typeparam
7491 231319 : && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
7492 0 : goto cleanup;
7493 : }
7494 :
7495 241845 : p = gfc_get_formal_arglist ();
7496 :
7497 241845 : if (head == NULL)
7498 : head = tail = p;
7499 : else
7500 : {
7501 62462 : tail->next = p;
7502 62462 : tail = p;
7503 : }
7504 :
7505 241845 : tail->sym = sym;
7506 :
7507 : /* We don't add the VARIABLE flavor because the name could be a
7508 : dummy procedure. We don't apply these attributes to formal
7509 : arguments of statement functions. */
7510 231319 : if (sym != NULL && !st_flag
7511 345439 : && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
7512 103594 : || !gfc_missing_attr (&sym->attr, NULL)))
7513 : {
7514 0 : m = MATCH_ERROR;
7515 0 : goto cleanup;
7516 : }
7517 :
7518 : /* The name of a program unit can be in a different namespace,
7519 : so check for it explicitly. After the statement is accepted,
7520 : the name is checked for especially in gfc_get_symbol(). */
7521 241845 : if (gfc_new_block != NULL && sym != NULL && !typeparam
7522 102024 : && strcmp (sym->name, gfc_new_block->name) == 0)
7523 : {
7524 0 : gfc_error ("Name %qs at %C is the name of the procedure",
7525 : sym->name);
7526 0 : m = MATCH_ERROR;
7527 0 : goto cleanup;
7528 : }
7529 :
7530 241845 : if (gfc_match_char (')') == MATCH_YES)
7531 128632 : goto ok;
7532 :
7533 113213 : m = gfc_match_char (',');
7534 113213 : if (m != MATCH_YES)
7535 : {
7536 50048 : if (typeparam)
7537 1 : gfc_error_now ("Expected parameter list in type declaration "
7538 : "at %C");
7539 : else
7540 50047 : gfc_error ("Unexpected junk in formal argument list at %C");
7541 50048 : goto cleanup;
7542 : }
7543 : }
7544 :
7545 146221 : ok:
7546 : /* Check for duplicate symbols in the formal argument list. */
7547 146221 : if (head != NULL)
7548 : {
7549 189447 : for (p = head; p->next; p = p->next)
7550 : {
7551 60863 : if (p->sym == NULL)
7552 338 : continue;
7553 :
7554 238751 : for (q = p->next; q; q = q->next)
7555 178274 : if (p->sym == q->sym)
7556 : {
7557 48 : if (typeparam)
7558 1 : gfc_error_now ("Duplicate name %qs in parameter "
7559 : "list at %C", p->sym->name);
7560 : else
7561 47 : gfc_error ("Duplicate symbol %qs in formal argument "
7562 : "list at %C", p->sym->name);
7563 :
7564 48 : m = MATCH_ERROR;
7565 48 : goto cleanup;
7566 : }
7567 : }
7568 : }
7569 :
7570 146173 : if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
7571 : {
7572 0 : m = MATCH_ERROR;
7573 0 : goto cleanup;
7574 : }
7575 :
7576 : /* gfc_error_now used in following and return with MATCH_YES because
7577 : doing otherwise results in a cascade of extraneous errors and in
7578 : some cases an ICE in symbol.cc(gfc_release_symbol). */
7579 146173 : if (progname->attr.module_procedure && progname->attr.host_assoc)
7580 : {
7581 213 : bool arg_count_mismatch = false;
7582 :
7583 213 : if (!formal && head)
7584 : arg_count_mismatch = true;
7585 :
7586 : /* Abbreviated module procedure declaration is not meant to have any
7587 : formal arguments! */
7588 213 : if (!progname->abr_modproc_decl && formal && !head)
7589 213 : arg_count_mismatch = true;
7590 :
7591 413 : for (p = formal, q = head; p && q; p = p->next, q = q->next)
7592 : {
7593 200 : if ((p->next != NULL && q->next == NULL)
7594 199 : || (p->next == NULL && q->next != NULL))
7595 : arg_count_mismatch = true;
7596 198 : else if ((p->sym == NULL && q->sym == NULL)
7597 198 : || (p->sym && q->sym
7598 196 : && strcmp (p->sym->name, q->sym->name) == 0))
7599 194 : continue;
7600 : else
7601 : {
7602 4 : if (q->sym == NULL)
7603 1 : gfc_error_now ("MODULE PROCEDURE formal argument %qs "
7604 : "conflicts with alternate return at %C",
7605 : p->sym->name);
7606 3 : else if (p->sym == NULL)
7607 1 : gfc_error_now ("MODULE PROCEDURE formal argument is "
7608 : "alternate return and conflicts with "
7609 : "%qs in the separate declaration at %C",
7610 : q->sym->name);
7611 : else
7612 2 : gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
7613 : "argument names (%s/%s) at %C",
7614 : p->sym->name, q->sym->name);
7615 : }
7616 : }
7617 :
7618 213 : if (arg_count_mismatch)
7619 4 : gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
7620 : "formal arguments at %C");
7621 : }
7622 :
7623 : return MATCH_YES;
7624 :
7625 67204 : cleanup:
7626 67204 : gfc_free_formal_arglist (head);
7627 67204 : return m;
7628 : }
7629 :
7630 :
7631 : /* Match a RESULT specification following a function declaration or
7632 : ENTRY statement. Also matches the end-of-statement. */
7633 :
7634 : static match
7635 9036 : match_result (gfc_symbol *function, gfc_symbol **result)
7636 : {
7637 9036 : char name[GFC_MAX_SYMBOL_LEN + 1];
7638 9036 : gfc_symbol *r;
7639 9036 : match m;
7640 :
7641 9036 : if (gfc_match (" result (") != MATCH_YES)
7642 : return MATCH_NO;
7643 :
7644 6366 : m = gfc_match_name (name);
7645 6366 : if (m != MATCH_YES)
7646 : return m;
7647 :
7648 : /* Get the right paren, and that's it because there could be the
7649 : bind(c) attribute after the result clause. */
7650 6366 : if (gfc_match_char (')') != MATCH_YES)
7651 : {
7652 : /* TODO: should report the missing right paren here. */
7653 : return MATCH_ERROR;
7654 : }
7655 :
7656 6366 : if (strcmp (function->name, name) == 0)
7657 : {
7658 1 : gfc_error ("RESULT variable at %C must be different than function name");
7659 1 : return MATCH_ERROR;
7660 : }
7661 :
7662 6365 : if (gfc_get_symbol (name, NULL, &r))
7663 : return MATCH_ERROR;
7664 :
7665 6365 : if (!gfc_add_result (&r->attr, r->name, NULL))
7666 : return MATCH_ERROR;
7667 :
7668 6365 : *result = r;
7669 :
7670 6365 : return MATCH_YES;
7671 : }
7672 :
7673 :
7674 : /* Match a function suffix, which could be a combination of a result
7675 : clause and BIND(C), either one, or neither. The draft does not
7676 : require them to come in a specific order. */
7677 :
7678 : static match
7679 9040 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
7680 : {
7681 9040 : match is_bind_c; /* Found bind(c). */
7682 9040 : match is_result; /* Found result clause. */
7683 9040 : match found_match; /* Status of whether we've found a good match. */
7684 9040 : char peek_char; /* Character we're going to peek at. */
7685 9040 : bool allow_binding_name;
7686 :
7687 : /* Initialize to having found nothing. */
7688 9040 : found_match = MATCH_NO;
7689 9040 : is_bind_c = MATCH_NO;
7690 9040 : is_result = MATCH_NO;
7691 :
7692 : /* Get the next char to narrow between result and bind(c). */
7693 9040 : gfc_gobble_whitespace ();
7694 9040 : peek_char = gfc_peek_ascii_char ();
7695 :
7696 : /* C binding names are not allowed for internal procedures. */
7697 9040 : if (gfc_current_state () == COMP_CONTAINS
7698 5050 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
7699 : allow_binding_name = false;
7700 : else
7701 7243 : allow_binding_name = true;
7702 :
7703 9040 : switch (peek_char)
7704 : {
7705 5989 : case 'r':
7706 : /* Look for result clause. */
7707 5989 : is_result = match_result (sym, result);
7708 5989 : if (is_result == MATCH_YES)
7709 : {
7710 : /* Now see if there is a bind(c) after it. */
7711 5988 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7712 : /* We've found the result clause and possibly bind(c). */
7713 5988 : found_match = MATCH_YES;
7714 : }
7715 : else
7716 : /* This should only be MATCH_ERROR. */
7717 : found_match = is_result;
7718 : break;
7719 3051 : case 'b':
7720 : /* Look for bind(c) first. */
7721 3051 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7722 3051 : if (is_bind_c == MATCH_YES)
7723 : {
7724 : /* Now see if a result clause followed it. */
7725 3047 : is_result = match_result (sym, result);
7726 3047 : found_match = MATCH_YES;
7727 : }
7728 : else
7729 : {
7730 : /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
7731 : found_match = MATCH_ERROR;
7732 : }
7733 : break;
7734 0 : default:
7735 0 : gfc_error ("Unexpected junk after function declaration at %C");
7736 0 : found_match = MATCH_ERROR;
7737 0 : break;
7738 : }
7739 :
7740 9035 : if (is_bind_c == MATCH_YES)
7741 : {
7742 : /* Fortran 2008 draft allows BIND(C) for internal procedures. */
7743 3214 : if (gfc_current_state () == COMP_CONTAINS
7744 423 : && sym->ns->proc_name->attr.flavor != FL_MODULE
7745 3232 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7746 : "at %L may not be specified for an internal "
7747 : "procedure", &gfc_current_locus))
7748 : return MATCH_ERROR;
7749 :
7750 3211 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
7751 0 : return MATCH_ERROR;
7752 : }
7753 :
7754 : return found_match;
7755 : }
7756 :
7757 :
7758 : /* Procedure pointer return value without RESULT statement:
7759 : Add "hidden" result variable named "ppr@". */
7760 :
7761 : static bool
7762 76955 : add_hidden_procptr_result (gfc_symbol *sym)
7763 : {
7764 76955 : bool case1,case2;
7765 :
7766 76955 : if (gfc_notification_std (GFC_STD_F2003) == ERROR)
7767 : return false;
7768 :
7769 : /* First usage case: PROCEDURE and EXTERNAL statements. */
7770 1543 : case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
7771 1543 : && strcmp (gfc_current_block ()->name, sym->name) == 0
7772 77354 : && sym->attr.external;
7773 : /* Second usage case: INTERFACE statements. */
7774 15255 : case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
7775 15255 : && gfc_state_stack->previous->state == COMP_FUNCTION
7776 77002 : && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
7777 :
7778 76771 : if (case1 || case2)
7779 : {
7780 125 : gfc_symtree *stree;
7781 125 : if (case1)
7782 95 : gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
7783 : else
7784 : {
7785 30 : gfc_symtree *st2;
7786 30 : gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
7787 30 : st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
7788 30 : st2->n.sym = stree->n.sym;
7789 30 : stree->n.sym->refs++;
7790 : }
7791 125 : sym->result = stree->n.sym;
7792 :
7793 125 : sym->result->attr.proc_pointer = sym->attr.proc_pointer;
7794 125 : sym->result->attr.pointer = sym->attr.pointer;
7795 125 : sym->result->attr.external = sym->attr.external;
7796 125 : sym->result->attr.referenced = sym->attr.referenced;
7797 125 : sym->result->ts = sym->ts;
7798 125 : sym->attr.proc_pointer = 0;
7799 125 : sym->attr.pointer = 0;
7800 125 : sym->attr.external = 0;
7801 125 : if (sym->result->attr.external && sym->result->attr.pointer)
7802 : {
7803 4 : sym->result->attr.pointer = 0;
7804 4 : sym->result->attr.proc_pointer = 1;
7805 : }
7806 :
7807 125 : return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
7808 : }
7809 : /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
7810 76676 : else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
7811 411 : && sym->result && sym->result != sym && sym->result->attr.external
7812 28 : && sym == gfc_current_ns->proc_name
7813 28 : && sym == sym->result->ns->proc_name
7814 28 : && strcmp ("ppr@", sym->result->name) == 0)
7815 : {
7816 28 : sym->result->attr.proc_pointer = 1;
7817 28 : sym->attr.pointer = 0;
7818 28 : return true;
7819 : }
7820 : else
7821 : return false;
7822 : }
7823 :
7824 :
7825 : /* Match the interface for a PROCEDURE declaration,
7826 : including brackets (R1212). */
7827 :
7828 : static match
7829 1664 : match_procedure_interface (gfc_symbol **proc_if)
7830 : {
7831 1664 : match m;
7832 1664 : gfc_symtree *st;
7833 1664 : locus old_loc, entry_loc;
7834 1664 : gfc_namespace *old_ns = gfc_current_ns;
7835 1664 : char name[GFC_MAX_SYMBOL_LEN + 1];
7836 :
7837 1664 : old_loc = entry_loc = gfc_current_locus;
7838 1664 : gfc_clear_ts (¤t_ts);
7839 :
7840 1664 : if (gfc_match (" (") != MATCH_YES)
7841 : {
7842 1 : gfc_current_locus = entry_loc;
7843 1 : return MATCH_NO;
7844 : }
7845 :
7846 : /* Get the type spec. for the procedure interface. */
7847 1663 : old_loc = gfc_current_locus;
7848 1663 : m = gfc_match_decl_type_spec (¤t_ts, 0);
7849 1663 : gfc_gobble_whitespace ();
7850 1663 : if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
7851 401 : goto got_ts;
7852 :
7853 1262 : if (m == MATCH_ERROR)
7854 : return m;
7855 :
7856 : /* Procedure interface is itself a procedure. */
7857 1262 : gfc_current_locus = old_loc;
7858 1262 : m = gfc_match_name (name);
7859 :
7860 : /* First look to see if it is already accessible in the current
7861 : namespace because it is use associated or contained. */
7862 1262 : st = NULL;
7863 1262 : if (gfc_find_sym_tree (name, NULL, 0, &st))
7864 : return MATCH_ERROR;
7865 :
7866 : /* If it is still not found, then try the parent namespace, if it
7867 : exists and create the symbol there if it is still not found. */
7868 1262 : if (gfc_current_ns->parent)
7869 435 : gfc_current_ns = gfc_current_ns->parent;
7870 1262 : if (st == NULL && gfc_get_ha_sym_tree (name, &st))
7871 : return MATCH_ERROR;
7872 :
7873 1262 : gfc_current_ns = old_ns;
7874 1262 : *proc_if = st->n.sym;
7875 :
7876 1262 : if (*proc_if)
7877 : {
7878 1262 : (*proc_if)->refs++;
7879 : /* Resolve interface if possible. That way, attr.procedure is only set
7880 : if it is declared by a later procedure-declaration-stmt, which is
7881 : invalid per F08:C1216 (cf. resolve_procedure_interface). */
7882 1262 : while ((*proc_if)->ts.interface
7883 1269 : && *proc_if != (*proc_if)->ts.interface)
7884 7 : *proc_if = (*proc_if)->ts.interface;
7885 :
7886 1262 : if ((*proc_if)->attr.flavor == FL_UNKNOWN
7887 395 : && (*proc_if)->ts.type == BT_UNKNOWN
7888 1657 : && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
7889 : (*proc_if)->name, NULL))
7890 : return MATCH_ERROR;
7891 : }
7892 :
7893 0 : got_ts:
7894 1663 : if (gfc_match (" )") != MATCH_YES)
7895 : {
7896 0 : gfc_current_locus = entry_loc;
7897 0 : return MATCH_NO;
7898 : }
7899 :
7900 : return MATCH_YES;
7901 : }
7902 :
7903 :
7904 : /* Match a PROCEDURE declaration (R1211). */
7905 :
7906 : static match
7907 1206 : match_procedure_decl (void)
7908 : {
7909 1206 : match m;
7910 1206 : gfc_symbol *sym, *proc_if = NULL;
7911 1206 : int num;
7912 1206 : gfc_expr *initializer = NULL;
7913 :
7914 : /* Parse interface (with brackets). */
7915 1206 : m = match_procedure_interface (&proc_if);
7916 1206 : if (m != MATCH_YES)
7917 : return m;
7918 :
7919 : /* Parse attributes (with colons). */
7920 1206 : m = match_attr_spec();
7921 1206 : if (m == MATCH_ERROR)
7922 : return MATCH_ERROR;
7923 :
7924 1205 : if (current_attr.allocatable)
7925 : {
7926 2 : current_attr.procedure = 1;
7927 2 : gfc_check_conflict (¤t_attr, NULL, &gfc_current_locus);
7928 2 : return MATCH_ERROR;
7929 : }
7930 :
7931 1203 : if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
7932 : {
7933 53 : current_attr.is_bind_c = 1;
7934 53 : has_name_equals = 0;
7935 53 : curr_binding_label = NULL;
7936 : }
7937 :
7938 : /* Get procedure symbols. */
7939 1203 : for(num=1;;num++)
7940 : {
7941 1282 : m = gfc_match_symbol (&sym, 0);
7942 1282 : if (m == MATCH_NO)
7943 1 : goto syntax;
7944 1281 : else if (m == MATCH_ERROR)
7945 : return m;
7946 :
7947 : /* Add current_attr to the symbol attributes. */
7948 1281 : if (!gfc_copy_attr (&sym->attr, ¤t_attr, NULL))
7949 : return MATCH_ERROR;
7950 :
7951 1279 : if (sym->attr.is_bind_c)
7952 : {
7953 : /* Check for C1218. */
7954 90 : if (!proc_if || !proc_if->attr.is_bind_c)
7955 : {
7956 1 : gfc_error ("BIND(C) attribute at %C requires "
7957 : "an interface with BIND(C)");
7958 1 : return MATCH_ERROR;
7959 : }
7960 : /* Check for C1217. */
7961 89 : if (has_name_equals && sym->attr.pointer)
7962 : {
7963 1 : gfc_error ("BIND(C) procedure with NAME may not have "
7964 : "POINTER attribute at %C");
7965 1 : return MATCH_ERROR;
7966 : }
7967 88 : if (has_name_equals && sym->attr.dummy)
7968 : {
7969 1 : gfc_error ("Dummy procedure at %C may not have "
7970 : "BIND(C) attribute with NAME");
7971 1 : return MATCH_ERROR;
7972 : }
7973 : /* Set binding label for BIND(C). */
7974 87 : if (!set_binding_label (&sym->binding_label, sym->name, num))
7975 : return MATCH_ERROR;
7976 : }
7977 :
7978 1275 : if (!gfc_add_external (&sym->attr, NULL))
7979 : return MATCH_ERROR;
7980 :
7981 1271 : if (add_hidden_procptr_result (sym))
7982 68 : sym = sym->result;
7983 :
7984 1271 : if (!gfc_add_proc (&sym->attr, sym->name, NULL))
7985 : return MATCH_ERROR;
7986 :
7987 : /* Set interface. */
7988 1271 : if (proc_if != NULL)
7989 : {
7990 923 : if (sym->ts.type != BT_UNKNOWN)
7991 : {
7992 1 : gfc_error ("Procedure %qs at %L already has basic type of %s",
7993 : sym->name, &gfc_current_locus,
7994 : gfc_basic_typename (sym->ts.type));
7995 1 : return MATCH_ERROR;
7996 : }
7997 922 : sym->ts.interface = proc_if;
7998 922 : sym->attr.untyped = 1;
7999 922 : sym->attr.if_source = IFSRC_IFBODY;
8000 : }
8001 348 : else if (current_ts.type != BT_UNKNOWN)
8002 : {
8003 203 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
8004 : return MATCH_ERROR;
8005 202 : sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
8006 202 : sym->ts.interface->ts = current_ts;
8007 202 : sym->ts.interface->attr.flavor = FL_PROCEDURE;
8008 202 : sym->ts.interface->attr.function = 1;
8009 202 : sym->attr.function = 1;
8010 202 : sym->attr.if_source = IFSRC_UNKNOWN;
8011 : }
8012 :
8013 1269 : if (gfc_match (" =>") == MATCH_YES)
8014 : {
8015 113 : if (!current_attr.pointer)
8016 : {
8017 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
8018 0 : m = MATCH_ERROR;
8019 0 : goto cleanup;
8020 : }
8021 :
8022 113 : m = match_pointer_init (&initializer, 1);
8023 113 : if (m != MATCH_YES)
8024 1 : goto cleanup;
8025 :
8026 112 : if (!add_init_expr_to_sym (sym->name, &initializer,
8027 : &gfc_current_locus,
8028 : gfc_current_ns->cl_list))
8029 0 : goto cleanup;
8030 :
8031 : }
8032 :
8033 1268 : if (gfc_match_eos () == MATCH_YES)
8034 : return MATCH_YES;
8035 79 : if (gfc_match_char (',') != MATCH_YES)
8036 0 : goto syntax;
8037 : }
8038 :
8039 1 : syntax:
8040 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
8041 1 : return MATCH_ERROR;
8042 :
8043 1 : cleanup:
8044 : /* Free stuff up and return. */
8045 1 : gfc_free_expr (initializer);
8046 1 : return m;
8047 : }
8048 :
8049 :
8050 : static match
8051 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
8052 :
8053 :
8054 : /* Match a procedure pointer component declaration (R445). */
8055 :
8056 : static match
8057 458 : match_ppc_decl (void)
8058 : {
8059 458 : match m;
8060 458 : gfc_symbol *proc_if = NULL;
8061 458 : gfc_typespec ts;
8062 458 : int num;
8063 458 : gfc_component *c;
8064 458 : gfc_expr *initializer = NULL;
8065 458 : gfc_typebound_proc* tb;
8066 458 : char name[GFC_MAX_SYMBOL_LEN + 1];
8067 :
8068 : /* Parse interface (with brackets). */
8069 458 : m = match_procedure_interface (&proc_if);
8070 458 : if (m != MATCH_YES)
8071 1 : goto syntax;
8072 :
8073 : /* Parse attributes. */
8074 457 : tb = XCNEW (gfc_typebound_proc);
8075 457 : tb->where = gfc_current_locus;
8076 457 : m = match_binding_attributes (tb, false, true);
8077 457 : if (m == MATCH_ERROR)
8078 : return m;
8079 :
8080 454 : gfc_clear_attr (¤t_attr);
8081 454 : current_attr.procedure = 1;
8082 454 : current_attr.proc_pointer = 1;
8083 454 : current_attr.access = tb->access;
8084 454 : current_attr.flavor = FL_PROCEDURE;
8085 :
8086 : /* Match the colons (required). */
8087 454 : if (gfc_match (" ::") != MATCH_YES)
8088 : {
8089 1 : gfc_error ("Expected %<::%> after binding-attributes at %C");
8090 1 : return MATCH_ERROR;
8091 : }
8092 :
8093 : /* Check for C450. */
8094 453 : if (!tb->nopass && proc_if == NULL)
8095 : {
8096 2 : gfc_error("NOPASS or explicit interface required at %C");
8097 2 : return MATCH_ERROR;
8098 : }
8099 :
8100 451 : if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
8101 : return MATCH_ERROR;
8102 :
8103 : /* Match PPC names. */
8104 450 : ts = current_ts;
8105 450 : for(num=1;;num++)
8106 : {
8107 451 : m = gfc_match_name (name);
8108 451 : if (m == MATCH_NO)
8109 0 : goto syntax;
8110 451 : else if (m == MATCH_ERROR)
8111 : return m;
8112 :
8113 451 : if (!gfc_add_component (gfc_current_block(), name, &c))
8114 : return MATCH_ERROR;
8115 :
8116 : /* Add current_attr to the symbol attributes. */
8117 451 : if (!gfc_copy_attr (&c->attr, ¤t_attr, NULL))
8118 : return MATCH_ERROR;
8119 :
8120 451 : if (!gfc_add_external (&c->attr, NULL))
8121 : return MATCH_ERROR;
8122 :
8123 451 : if (!gfc_add_proc (&c->attr, name, NULL))
8124 : return MATCH_ERROR;
8125 :
8126 451 : if (num == 1)
8127 450 : c->tb = tb;
8128 : else
8129 : {
8130 1 : c->tb = XCNEW (gfc_typebound_proc);
8131 1 : c->tb->where = gfc_current_locus;
8132 1 : *c->tb = *tb;
8133 : }
8134 :
8135 451 : if (saved_kind_expr)
8136 0 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
8137 :
8138 : /* Set interface. */
8139 451 : if (proc_if != NULL)
8140 : {
8141 383 : c->ts.interface = proc_if;
8142 383 : c->attr.untyped = 1;
8143 383 : c->attr.if_source = IFSRC_IFBODY;
8144 : }
8145 68 : else if (ts.type != BT_UNKNOWN)
8146 : {
8147 30 : c->ts = ts;
8148 30 : c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
8149 30 : c->ts.interface->result = c->ts.interface;
8150 30 : c->ts.interface->ts = ts;
8151 30 : c->ts.interface->attr.flavor = FL_PROCEDURE;
8152 30 : c->ts.interface->attr.function = 1;
8153 30 : c->attr.function = 1;
8154 30 : c->attr.if_source = IFSRC_UNKNOWN;
8155 : }
8156 :
8157 451 : if (gfc_match (" =>") == MATCH_YES)
8158 : {
8159 98 : m = match_pointer_init (&initializer, 1);
8160 98 : if (m != MATCH_YES)
8161 : {
8162 0 : gfc_free_expr (initializer);
8163 0 : return m;
8164 : }
8165 98 : c->initializer = initializer;
8166 : }
8167 :
8168 451 : if (gfc_match_eos () == MATCH_YES)
8169 : return MATCH_YES;
8170 1 : if (gfc_match_char (',') != MATCH_YES)
8171 0 : goto syntax;
8172 : }
8173 :
8174 1 : syntax:
8175 1 : gfc_error ("Syntax error in procedure pointer component at %C");
8176 1 : return MATCH_ERROR;
8177 : }
8178 :
8179 :
8180 : /* Match a PROCEDURE declaration inside an interface (R1206). */
8181 :
8182 : static match
8183 1561 : match_procedure_in_interface (void)
8184 : {
8185 1561 : match m;
8186 1561 : gfc_symbol *sym;
8187 1561 : char name[GFC_MAX_SYMBOL_LEN + 1];
8188 1561 : locus old_locus;
8189 :
8190 1561 : if (current_interface.type == INTERFACE_NAMELESS
8191 1561 : || current_interface.type == INTERFACE_ABSTRACT)
8192 : {
8193 1 : gfc_error ("PROCEDURE at %C must be in a generic interface");
8194 1 : return MATCH_ERROR;
8195 : }
8196 :
8197 : /* Check if the F2008 optional double colon appears. */
8198 1560 : gfc_gobble_whitespace ();
8199 1560 : old_locus = gfc_current_locus;
8200 1560 : if (gfc_match ("::") == MATCH_YES)
8201 : {
8202 875 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
8203 : "MODULE PROCEDURE statement at %L", &old_locus))
8204 : return MATCH_ERROR;
8205 : }
8206 : else
8207 685 : gfc_current_locus = old_locus;
8208 :
8209 2214 : for(;;)
8210 : {
8211 2214 : m = gfc_match_name (name);
8212 2214 : if (m == MATCH_NO)
8213 0 : goto syntax;
8214 2214 : else if (m == MATCH_ERROR)
8215 : return m;
8216 2214 : if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
8217 : return MATCH_ERROR;
8218 :
8219 2214 : if (!gfc_add_interface (sym))
8220 : return MATCH_ERROR;
8221 :
8222 2213 : if (gfc_match_eos () == MATCH_YES)
8223 : break;
8224 655 : if (gfc_match_char (',') != MATCH_YES)
8225 0 : goto syntax;
8226 : }
8227 :
8228 : return MATCH_YES;
8229 :
8230 0 : syntax:
8231 0 : gfc_error ("Syntax error in PROCEDURE statement at %C");
8232 0 : return MATCH_ERROR;
8233 : }
8234 :
8235 :
8236 : /* General matcher for PROCEDURE declarations. */
8237 :
8238 : static match match_procedure_in_type (void);
8239 :
8240 : match
8241 6533 : gfc_match_procedure (void)
8242 : {
8243 6533 : match m;
8244 :
8245 6533 : switch (gfc_current_state ())
8246 : {
8247 1206 : case COMP_NONE:
8248 1206 : case COMP_PROGRAM:
8249 1206 : case COMP_MODULE:
8250 1206 : case COMP_SUBMODULE:
8251 1206 : case COMP_SUBROUTINE:
8252 1206 : case COMP_FUNCTION:
8253 1206 : case COMP_BLOCK:
8254 1206 : m = match_procedure_decl ();
8255 1206 : break;
8256 1561 : case COMP_INTERFACE:
8257 1561 : m = match_procedure_in_interface ();
8258 1561 : break;
8259 458 : case COMP_DERIVED:
8260 458 : m = match_ppc_decl ();
8261 458 : break;
8262 3308 : case COMP_DERIVED_CONTAINS:
8263 3308 : m = match_procedure_in_type ();
8264 3308 : break;
8265 : default:
8266 : return MATCH_NO;
8267 : }
8268 :
8269 6533 : if (m != MATCH_YES)
8270 : return m;
8271 :
8272 6476 : if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
8273 4 : return MATCH_ERROR;
8274 :
8275 : return m;
8276 : }
8277 :
8278 :
8279 : /* Warn if a matched procedure has the same name as an intrinsic; this is
8280 : simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
8281 : parser-state-stack to find out whether we're in a module. */
8282 :
8283 : static void
8284 65263 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
8285 : {
8286 65263 : bool in_module;
8287 :
8288 130526 : in_module = (gfc_state_stack->previous
8289 65263 : && (gfc_state_stack->previous->state == COMP_MODULE
8290 53450 : || gfc_state_stack->previous->state == COMP_SUBMODULE));
8291 :
8292 65263 : gfc_warn_intrinsic_shadow (sym, in_module, func);
8293 65263 : }
8294 :
8295 :
8296 : /* Match a function declaration. */
8297 :
8298 : match
8299 133549 : gfc_match_function_decl (void)
8300 : {
8301 133549 : char name[GFC_MAX_SYMBOL_LEN + 1];
8302 133549 : gfc_symbol *sym, *result;
8303 133549 : locus old_loc;
8304 133549 : match m;
8305 133549 : match suffix_match;
8306 133549 : match found_match; /* Status returned by match func. */
8307 :
8308 133549 : if (gfc_current_state () != COMP_NONE
8309 84546 : && gfc_current_state () != COMP_INTERFACE
8310 54400 : && gfc_current_state () != COMP_CONTAINS)
8311 : return MATCH_NO;
8312 :
8313 133549 : gfc_clear_ts (¤t_ts);
8314 :
8315 133549 : old_loc = gfc_current_locus;
8316 :
8317 133549 : m = gfc_match_prefix (¤t_ts);
8318 133549 : if (m != MATCH_YES)
8319 : {
8320 10317 : gfc_current_locus = old_loc;
8321 10317 : return m;
8322 : }
8323 :
8324 123232 : if (gfc_match ("function% %n", name) != MATCH_YES)
8325 : {
8326 102445 : gfc_current_locus = old_loc;
8327 102445 : return MATCH_NO;
8328 : }
8329 :
8330 20787 : if (get_proc_name (name, &sym, false))
8331 : return MATCH_ERROR;
8332 :
8333 20782 : if (add_hidden_procptr_result (sym))
8334 20 : sym = sym->result;
8335 :
8336 20782 : if (current_attr.module_procedure)
8337 : {
8338 358 : sym->attr.module_procedure = 1;
8339 358 : if (gfc_current_state () == COMP_INTERFACE)
8340 251 : gfc_current_ns->has_import_set = 1;
8341 : }
8342 :
8343 20782 : gfc_new_block = sym;
8344 :
8345 20782 : m = gfc_match_formal_arglist (sym, 0, 0);
8346 20782 : if (m == MATCH_NO)
8347 : {
8348 6 : gfc_error ("Expected formal argument list in function "
8349 : "definition at %C");
8350 6 : m = MATCH_ERROR;
8351 6 : goto cleanup;
8352 : }
8353 20776 : else if (m == MATCH_ERROR)
8354 0 : goto cleanup;
8355 :
8356 20776 : result = NULL;
8357 :
8358 : /* According to the draft, the bind(c) and result clause can
8359 : come in either order after the formal_arg_list (i.e., either
8360 : can be first, both can exist together or by themselves or neither
8361 : one). Therefore, the match_result can't match the end of the
8362 : string, and check for the bind(c) or result clause in either order. */
8363 20776 : found_match = gfc_match_eos ();
8364 :
8365 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8366 : must have been marked BIND(C) with a BIND(C) attribute and that is
8367 : not allowed for procedures. */
8368 20776 : if (sym->attr.is_bind_c == 1)
8369 : {
8370 3 : sym->attr.is_bind_c = 0;
8371 :
8372 3 : if (gfc_state_stack->previous
8373 3 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8374 : {
8375 1 : locus loc;
8376 1 : loc = sym->old_symbol != NULL
8377 1 : ? sym->old_symbol->declared_at : gfc_current_locus;
8378 1 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8379 : "variables or common blocks", &loc);
8380 : }
8381 : }
8382 :
8383 20776 : if (found_match != MATCH_YES)
8384 : {
8385 : /* If we haven't found the end-of-statement, look for a suffix. */
8386 8791 : suffix_match = gfc_match_suffix (sym, &result);
8387 8791 : if (suffix_match == MATCH_YES)
8388 : /* Need to get the eos now. */
8389 8783 : found_match = gfc_match_eos ();
8390 : else
8391 : found_match = suffix_match;
8392 : }
8393 :
8394 : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8395 : subprogram and a binding label is specified, it shall be the
8396 : same as the binding label specified in the corresponding module
8397 : procedure interface body. */
8398 20776 : if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
8399 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8400 3 : && sym->binding_label && sym->old_symbol->binding_label
8401 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8402 : {
8403 1 : const char *null = "NULL", *s1, *s2;
8404 1 : s1 = sym->binding_label;
8405 1 : if (!s1) s1 = null;
8406 1 : s2 = sym->old_symbol->binding_label;
8407 1 : if (!s2) s2 = null;
8408 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8409 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8410 1 : return MATCH_ERROR;
8411 : }
8412 :
8413 20775 : if(found_match != MATCH_YES)
8414 : m = MATCH_ERROR;
8415 : else
8416 : {
8417 : /* Make changes to the symbol. */
8418 20767 : m = MATCH_ERROR;
8419 :
8420 20767 : if (!gfc_add_function (&sym->attr, sym->name, NULL))
8421 0 : goto cleanup;
8422 :
8423 20767 : if (!gfc_missing_attr (&sym->attr, NULL))
8424 0 : goto cleanup;
8425 :
8426 20767 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8427 : {
8428 1 : if(!sym->attr.module_procedure)
8429 1 : goto cleanup;
8430 : else
8431 0 : gfc_error_check ();
8432 : }
8433 :
8434 : /* Delay matching the function characteristics until after the
8435 : specification block by signalling kind=-1. */
8436 20766 : sym->declared_at = old_loc;
8437 20766 : if (current_ts.type != BT_UNKNOWN)
8438 : current_ts.kind = -1;
8439 : else
8440 13576 : current_ts.kind = 0;
8441 :
8442 20766 : if (result == NULL)
8443 : {
8444 14613 : if (current_ts.type != BT_UNKNOWN
8445 14613 : && !gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
8446 1 : goto cleanup;
8447 14612 : sym->result = sym;
8448 : }
8449 : else
8450 : {
8451 6153 : if (current_ts.type != BT_UNKNOWN
8452 6153 : && !gfc_add_type (result, ¤t_ts, &gfc_current_locus))
8453 0 : goto cleanup;
8454 6153 : sym->result = result;
8455 : }
8456 :
8457 : /* Warn if this procedure has the same name as an intrinsic. */
8458 20765 : do_warn_intrinsic_shadow (sym, true);
8459 :
8460 20765 : return MATCH_YES;
8461 : }
8462 :
8463 16 : cleanup:
8464 16 : gfc_current_locus = old_loc;
8465 16 : return m;
8466 : }
8467 :
8468 :
8469 : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
8470 : pass the name of the entry, rather than the gfc_current_block name, and
8471 : to return false upon finding an existing global entry. */
8472 :
8473 : static bool
8474 539 : add_global_entry (const char *name, const char *binding_label, bool sub,
8475 : locus *where)
8476 : {
8477 539 : gfc_gsymbol *s;
8478 539 : enum gfc_symbol_type type;
8479 :
8480 539 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
8481 :
8482 : /* Only in Fortran 2003: For procedures with a binding label also the Fortran
8483 : name is a global identifier. */
8484 539 : if (!binding_label || gfc_notification_std (GFC_STD_F2008))
8485 : {
8486 516 : s = gfc_get_gsymbol (name, false);
8487 :
8488 516 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8489 : {
8490 2 : gfc_global_used (s, where);
8491 2 : return false;
8492 : }
8493 : else
8494 : {
8495 514 : s->type = type;
8496 514 : s->sym_name = name;
8497 514 : s->where = *where;
8498 514 : s->defined = 1;
8499 514 : s->ns = gfc_current_ns;
8500 : }
8501 : }
8502 :
8503 : /* Don't add the symbol multiple times. */
8504 537 : if (binding_label
8505 537 : && (!gfc_notification_std (GFC_STD_F2008)
8506 0 : || strcmp (name, binding_label) != 0))
8507 : {
8508 23 : s = gfc_get_gsymbol (binding_label, true);
8509 :
8510 23 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8511 : {
8512 1 : gfc_global_used (s, where);
8513 1 : return false;
8514 : }
8515 : else
8516 : {
8517 22 : s->type = type;
8518 22 : s->sym_name = gfc_get_string ("%s", name);
8519 22 : s->binding_label = binding_label;
8520 22 : s->where = *where;
8521 22 : s->defined = 1;
8522 22 : s->ns = gfc_current_ns;
8523 : }
8524 : }
8525 :
8526 : return true;
8527 : }
8528 :
8529 :
8530 : /* Match an ENTRY statement. */
8531 :
8532 : match
8533 805 : gfc_match_entry (void)
8534 : {
8535 805 : gfc_symbol *proc;
8536 805 : gfc_symbol *result;
8537 805 : gfc_symbol *entry;
8538 805 : char name[GFC_MAX_SYMBOL_LEN + 1];
8539 805 : gfc_compile_state state;
8540 805 : match m;
8541 805 : gfc_entry_list *el;
8542 805 : locus old_loc;
8543 805 : bool module_procedure;
8544 805 : char peek_char;
8545 805 : match is_bind_c;
8546 :
8547 805 : m = gfc_match_name (name);
8548 805 : if (m != MATCH_YES)
8549 : return m;
8550 :
8551 805 : if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
8552 : return MATCH_ERROR;
8553 :
8554 805 : state = gfc_current_state ();
8555 805 : if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
8556 : {
8557 3 : switch (state)
8558 : {
8559 0 : case COMP_PROGRAM:
8560 0 : gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
8561 0 : break;
8562 0 : case COMP_MODULE:
8563 0 : gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
8564 0 : break;
8565 0 : case COMP_SUBMODULE:
8566 0 : gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
8567 0 : break;
8568 0 : case COMP_BLOCK_DATA:
8569 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8570 : "a BLOCK DATA");
8571 0 : break;
8572 0 : case COMP_INTERFACE:
8573 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8574 : "an INTERFACE");
8575 0 : break;
8576 1 : case COMP_STRUCTURE:
8577 1 : gfc_error ("ENTRY statement at %C cannot appear within "
8578 : "a STRUCTURE block");
8579 1 : break;
8580 0 : case COMP_DERIVED:
8581 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8582 : "a DERIVED TYPE block");
8583 0 : break;
8584 0 : case COMP_IF:
8585 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8586 : "an IF-THEN block");
8587 0 : break;
8588 0 : case COMP_DO:
8589 0 : case COMP_DO_CONCURRENT:
8590 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8591 : "a DO block");
8592 0 : break;
8593 0 : case COMP_SELECT:
8594 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8595 : "a SELECT block");
8596 0 : break;
8597 0 : case COMP_FORALL:
8598 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8599 : "a FORALL block");
8600 0 : break;
8601 0 : case COMP_WHERE:
8602 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8603 : "a WHERE block");
8604 0 : break;
8605 0 : case COMP_CONTAINS:
8606 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8607 : "a contained subprogram");
8608 0 : break;
8609 2 : default:
8610 2 : gfc_error ("Unexpected ENTRY statement at %C");
8611 : }
8612 : return MATCH_ERROR;
8613 : }
8614 :
8615 802 : if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
8616 802 : && gfc_state_stack->previous->state == COMP_INTERFACE)
8617 : {
8618 1 : gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
8619 1 : return MATCH_ERROR;
8620 : }
8621 :
8622 1602 : module_procedure = gfc_current_ns->parent != NULL
8623 260 : && gfc_current_ns->parent->proc_name
8624 801 : && gfc_current_ns->parent->proc_name->attr.flavor
8625 260 : == FL_MODULE;
8626 :
8627 801 : if (gfc_current_ns->parent != NULL
8628 260 : && gfc_current_ns->parent->proc_name
8629 260 : && !module_procedure)
8630 : {
8631 0 : gfc_error("ENTRY statement at %C cannot appear in a "
8632 : "contained procedure");
8633 0 : return MATCH_ERROR;
8634 : }
8635 :
8636 : /* Module function entries need special care in get_proc_name
8637 : because previous references within the function will have
8638 : created symbols attached to the current namespace. */
8639 1342 : if (get_proc_name (name, &entry,
8640 : gfc_current_ns->parent != NULL
8641 : && module_procedure))
8642 : return MATCH_ERROR;
8643 :
8644 799 : proc = gfc_current_block ();
8645 :
8646 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8647 : must have been marked BIND(C) with a BIND(C) attribute and that is
8648 : not allowed for procedures. */
8649 799 : if (entry->attr.is_bind_c == 1)
8650 : {
8651 0 : locus loc;
8652 :
8653 0 : entry->attr.is_bind_c = 0;
8654 :
8655 0 : loc = entry->old_symbol != NULL
8656 0 : ? entry->old_symbol->declared_at : gfc_current_locus;
8657 0 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8658 : "variables or common blocks", &loc);
8659 : }
8660 :
8661 : /* Check what next non-whitespace character is so we can tell if there
8662 : is the required parens if we have a BIND(C). */
8663 799 : old_loc = gfc_current_locus;
8664 799 : gfc_gobble_whitespace ();
8665 799 : peek_char = gfc_peek_ascii_char ();
8666 :
8667 799 : if (state == COMP_SUBROUTINE)
8668 : {
8669 138 : m = gfc_match_formal_arglist (entry, 0, 1);
8670 138 : if (m != MATCH_YES)
8671 : return MATCH_ERROR;
8672 :
8673 : /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
8674 : never be an internal procedure. */
8675 138 : is_bind_c = gfc_match_bind_c (entry, true);
8676 138 : if (is_bind_c == MATCH_ERROR)
8677 : return MATCH_ERROR;
8678 138 : if (is_bind_c == MATCH_YES)
8679 : {
8680 22 : if (peek_char != '(')
8681 : {
8682 0 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8683 0 : return MATCH_ERROR;
8684 : }
8685 :
8686 22 : if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
8687 22 : &(entry->declared_at), 1))
8688 : return MATCH_ERROR;
8689 :
8690 : }
8691 :
8692 138 : if (!gfc_current_ns->parent
8693 138 : && !add_global_entry (name, entry->binding_label, true,
8694 : &old_loc))
8695 : return MATCH_ERROR;
8696 :
8697 : /* An entry in a subroutine. */
8698 135 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8699 135 : || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
8700 : return MATCH_ERROR;
8701 : }
8702 : else
8703 : {
8704 : /* An entry in a function.
8705 : We need to take special care because writing
8706 : ENTRY f()
8707 : as
8708 : ENTRY f
8709 : is allowed, whereas
8710 : ENTRY f() RESULT (r)
8711 : can't be written as
8712 : ENTRY f RESULT (r). */
8713 661 : if (gfc_match_eos () == MATCH_YES)
8714 : {
8715 24 : gfc_current_locus = old_loc;
8716 : /* Match the empty argument list, and add the interface to
8717 : the symbol. */
8718 24 : m = gfc_match_formal_arglist (entry, 0, 1);
8719 : }
8720 : else
8721 637 : m = gfc_match_formal_arglist (entry, 0, 0);
8722 :
8723 661 : if (m != MATCH_YES)
8724 : return MATCH_ERROR;
8725 :
8726 660 : result = NULL;
8727 :
8728 660 : if (gfc_match_eos () == MATCH_YES)
8729 : {
8730 411 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8731 411 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8732 : return MATCH_ERROR;
8733 :
8734 409 : entry->result = entry;
8735 : }
8736 : else
8737 : {
8738 249 : m = gfc_match_suffix (entry, &result);
8739 249 : if (m == MATCH_NO)
8740 0 : gfc_syntax_error (ST_ENTRY);
8741 249 : if (m != MATCH_YES)
8742 : return MATCH_ERROR;
8743 :
8744 249 : if (result)
8745 : {
8746 212 : if (!gfc_add_result (&result->attr, result->name, NULL)
8747 212 : || !gfc_add_entry (&entry->attr, result->name, NULL)
8748 424 : || !gfc_add_function (&entry->attr, result->name, NULL))
8749 : return MATCH_ERROR;
8750 212 : entry->result = result;
8751 : }
8752 : else
8753 : {
8754 37 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8755 37 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8756 : return MATCH_ERROR;
8757 37 : entry->result = entry;
8758 : }
8759 : }
8760 :
8761 658 : if (!gfc_current_ns->parent
8762 658 : && !add_global_entry (name, entry->binding_label, false,
8763 : &old_loc))
8764 : return MATCH_ERROR;
8765 : }
8766 :
8767 790 : if (gfc_match_eos () != MATCH_YES)
8768 : {
8769 0 : gfc_syntax_error (ST_ENTRY);
8770 0 : return MATCH_ERROR;
8771 : }
8772 :
8773 : /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
8774 790 : if (proc->attr.elemental && entry->attr.is_bind_c)
8775 : {
8776 2 : gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
8777 : "elemental procedure", &entry->declared_at);
8778 2 : return MATCH_ERROR;
8779 : }
8780 :
8781 788 : entry->attr.recursive = proc->attr.recursive;
8782 788 : entry->attr.elemental = proc->attr.elemental;
8783 788 : entry->attr.pure = proc->attr.pure;
8784 :
8785 788 : el = gfc_get_entry_list ();
8786 788 : el->sym = entry;
8787 788 : el->next = gfc_current_ns->entries;
8788 788 : gfc_current_ns->entries = el;
8789 788 : if (el->next)
8790 85 : el->id = el->next->id + 1;
8791 : else
8792 : el->id = 1;
8793 :
8794 788 : new_st.op = EXEC_ENTRY;
8795 788 : new_st.ext.entry = el;
8796 :
8797 788 : return MATCH_YES;
8798 : }
8799 :
8800 :
8801 : /* Match a subroutine statement, including optional prefixes. */
8802 :
8803 : match
8804 830904 : gfc_match_subroutine (void)
8805 : {
8806 830904 : char name[GFC_MAX_SYMBOL_LEN + 1];
8807 830904 : gfc_symbol *sym;
8808 830904 : match m;
8809 830904 : match is_bind_c;
8810 830904 : char peek_char;
8811 830904 : bool allow_binding_name;
8812 830904 : locus loc;
8813 :
8814 830904 : if (gfc_current_state () != COMP_NONE
8815 787806 : && gfc_current_state () != COMP_INTERFACE
8816 764493 : && gfc_current_state () != COMP_CONTAINS)
8817 : return MATCH_NO;
8818 :
8819 109857 : m = gfc_match_prefix (NULL);
8820 109857 : if (m != MATCH_YES)
8821 : return m;
8822 :
8823 99550 : loc = gfc_current_locus;
8824 99550 : m = gfc_match ("subroutine% %n", name);
8825 99550 : if (m != MATCH_YES)
8826 : return m;
8827 :
8828 44534 : if (get_proc_name (name, &sym, false))
8829 : return MATCH_ERROR;
8830 :
8831 : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8832 : the symbol existed before. */
8833 44523 : sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
8834 : &gfc_current_locus);
8835 :
8836 44523 : if (current_attr.module_procedure)
8837 : {
8838 429 : sym->attr.module_procedure = 1;
8839 429 : if (gfc_current_state () == COMP_INTERFACE)
8840 302 : gfc_current_ns->has_import_set = 1;
8841 : }
8842 :
8843 44523 : if (add_hidden_procptr_result (sym))
8844 9 : sym = sym->result;
8845 :
8846 44523 : gfc_new_block = sym;
8847 :
8848 : /* Check what next non-whitespace character is so we can tell if there
8849 : is the required parens if we have a BIND(C). */
8850 44523 : gfc_gobble_whitespace ();
8851 44523 : peek_char = gfc_peek_ascii_char ();
8852 :
8853 44523 : if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
8854 : return MATCH_ERROR;
8855 :
8856 44520 : if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
8857 : return MATCH_ERROR;
8858 :
8859 : /* Make sure that it isn't already declared as BIND(C). If it is, it
8860 : must have been marked BIND(C) with a BIND(C) attribute and that is
8861 : not allowed for procedures. */
8862 44520 : if (sym->attr.is_bind_c == 1)
8863 : {
8864 4 : sym->attr.is_bind_c = 0;
8865 :
8866 4 : if (gfc_state_stack->previous
8867 4 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8868 : {
8869 2 : locus loc;
8870 2 : loc = sym->old_symbol != NULL
8871 2 : ? sym->old_symbol->declared_at : gfc_current_locus;
8872 2 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8873 : "variables or common blocks", &loc);
8874 : }
8875 : }
8876 :
8877 : /* C binding names are not allowed for internal procedures. */
8878 44520 : if (gfc_current_state () == COMP_CONTAINS
8879 27268 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
8880 : allow_binding_name = false;
8881 : else
8882 29008 : allow_binding_name = true;
8883 :
8884 : /* Here, we are just checking if it has the bind(c) attribute, and if
8885 : so, then we need to make sure it's all correct. If it doesn't,
8886 : we still need to continue matching the rest of the subroutine line. */
8887 44520 : gfc_gobble_whitespace ();
8888 44520 : loc = gfc_current_locus;
8889 44520 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
8890 44520 : if (is_bind_c == MATCH_ERROR)
8891 : {
8892 : /* There was an attempt at the bind(c), but it was wrong. An
8893 : error message should have been printed w/in the gfc_match_bind_c
8894 : so here we'll just return the MATCH_ERROR. */
8895 : return MATCH_ERROR;
8896 : }
8897 :
8898 44507 : if (is_bind_c == MATCH_YES)
8899 : {
8900 4169 : gfc_formal_arglist *arg;
8901 :
8902 : /* The following is allowed in the Fortran 2008 draft. */
8903 4169 : if (gfc_current_state () == COMP_CONTAINS
8904 1301 : && sym->ns->proc_name->attr.flavor != FL_MODULE
8905 4580 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
8906 : "at %L may not be specified for an internal "
8907 : "procedure", &gfc_current_locus))
8908 : return MATCH_ERROR;
8909 :
8910 4166 : if (peek_char != '(')
8911 : {
8912 1 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8913 1 : return MATCH_ERROR;
8914 : }
8915 :
8916 : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8917 : subprogram and a binding label is specified, it shall be the
8918 : same as the binding label specified in the corresponding module
8919 : procedure interface body. */
8920 4165 : if (sym->attr.module_procedure && sym->old_symbol
8921 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8922 3 : && sym->binding_label && sym->old_symbol->binding_label
8923 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8924 : {
8925 1 : const char *null = "NULL", *s1, *s2;
8926 1 : s1 = sym->binding_label;
8927 1 : if (!s1) s1 = null;
8928 1 : s2 = sym->old_symbol->binding_label;
8929 1 : if (!s2) s2 = null;
8930 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8931 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8932 1 : return MATCH_ERROR;
8933 : }
8934 :
8935 : /* Scan the dummy arguments for an alternate return. */
8936 12881 : for (arg = sym->formal; arg; arg = arg->next)
8937 8718 : if (!arg->sym)
8938 : {
8939 1 : gfc_error ("Alternate return dummy argument cannot appear in a "
8940 : "SUBROUTINE with the BIND(C) attribute at %L", &loc);
8941 1 : return MATCH_ERROR;
8942 : }
8943 :
8944 4163 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
8945 : return MATCH_ERROR;
8946 : }
8947 :
8948 44500 : if (gfc_match_eos () != MATCH_YES)
8949 : {
8950 1 : gfc_syntax_error (ST_SUBROUTINE);
8951 1 : return MATCH_ERROR;
8952 : }
8953 :
8954 44499 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8955 : {
8956 4 : if(!sym->attr.module_procedure)
8957 : return MATCH_ERROR;
8958 : else
8959 3 : gfc_error_check ();
8960 : }
8961 :
8962 : /* Warn if it has the same name as an intrinsic. */
8963 44498 : do_warn_intrinsic_shadow (sym, false);
8964 :
8965 44498 : return MATCH_YES;
8966 : }
8967 :
8968 :
8969 : /* Check that the NAME identifier in a BIND attribute or statement
8970 : is conform to C identifier rules. */
8971 :
8972 : match
8973 1193 : check_bind_name_identifier (char **name)
8974 : {
8975 1193 : char *n = *name, *p;
8976 :
8977 : /* Remove leading spaces. */
8978 1219 : while (*n == ' ')
8979 26 : n++;
8980 :
8981 : /* On an empty string, free memory and set name to NULL. */
8982 1193 : if (*n == '\0')
8983 : {
8984 42 : free (*name);
8985 42 : *name = NULL;
8986 42 : return MATCH_YES;
8987 : }
8988 :
8989 : /* Remove trailing spaces. */
8990 1151 : p = n + strlen(n) - 1;
8991 1167 : while (*p == ' ')
8992 16 : *(p--) = '\0';
8993 :
8994 : /* Insert the identifier into the symbol table. */
8995 1151 : p = xstrdup (n);
8996 1151 : free (*name);
8997 1151 : *name = p;
8998 :
8999 : /* Now check that identifier is valid under C rules. */
9000 1151 : if (ISDIGIT (*p))
9001 : {
9002 2 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
9003 2 : return MATCH_ERROR;
9004 : }
9005 :
9006 12572 : for (; *p; p++)
9007 11426 : if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
9008 : {
9009 3 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
9010 3 : return MATCH_ERROR;
9011 : }
9012 :
9013 : return MATCH_YES;
9014 : }
9015 :
9016 :
9017 : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
9018 : given, and set the binding label in either the given symbol (if not
9019 : NULL), or in the current_ts. The symbol may be NULL because we may
9020 : encounter the BIND(C) before the declaration itself. Return
9021 : MATCH_NO if what we're looking at isn't a BIND(C) specifier,
9022 : MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
9023 : or MATCH_YES if the specifier was correct and the binding label and
9024 : bind(c) fields were set correctly for the given symbol or the
9025 : current_ts. If allow_binding_name is false, no binding name may be
9026 : given. */
9027 :
9028 : match
9029 54001 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
9030 : {
9031 54001 : char *binding_label = NULL;
9032 54001 : gfc_expr *e = NULL;
9033 :
9034 : /* Initialize the flag that specifies whether we encountered a NAME=
9035 : specifier or not. */
9036 54001 : has_name_equals = 0;
9037 :
9038 : /* This much we have to be able to match, in this order, if
9039 : there is a bind(c) label. */
9040 54001 : if (gfc_match (" bind ( c ") != MATCH_YES)
9041 : return MATCH_NO;
9042 :
9043 : /* Now see if there is a binding label, or if we've reached the
9044 : end of the bind(c) attribute without one. */
9045 7694 : if (gfc_match_char (',') == MATCH_YES)
9046 : {
9047 1200 : if (gfc_match (" name = ") != MATCH_YES)
9048 : {
9049 1 : gfc_error ("Syntax error in NAME= specifier for binding label "
9050 : "at %C");
9051 : /* should give an error message here */
9052 1 : return MATCH_ERROR;
9053 : }
9054 :
9055 1199 : has_name_equals = 1;
9056 :
9057 1199 : if (gfc_match_init_expr (&e) != MATCH_YES)
9058 : {
9059 2 : gfc_free_expr (e);
9060 2 : return MATCH_ERROR;
9061 : }
9062 :
9063 1197 : if (!gfc_simplify_expr(e, 0))
9064 : {
9065 0 : gfc_error ("NAME= specifier at %C should be a constant expression");
9066 0 : gfc_free_expr (e);
9067 0 : return MATCH_ERROR;
9068 : }
9069 :
9070 1197 : if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
9071 1194 : || e->ts.kind != gfc_default_character_kind || e->rank != 0)
9072 : {
9073 4 : gfc_error ("NAME= specifier at %C should be a scalar of "
9074 : "default character kind");
9075 4 : gfc_free_expr(e);
9076 4 : return MATCH_ERROR;
9077 : }
9078 :
9079 : // Get a C string from the Fortran string constant
9080 2386 : binding_label = gfc_widechar_to_char (e->value.character.string,
9081 1193 : e->value.character.length);
9082 1193 : gfc_free_expr(e);
9083 :
9084 : // Check that it is valid (old gfc_match_name_C)
9085 1193 : if (check_bind_name_identifier (&binding_label) != MATCH_YES)
9086 : return MATCH_ERROR;
9087 : }
9088 :
9089 : /* Get the required right paren. */
9090 7682 : if (gfc_match_char (')') != MATCH_YES)
9091 : {
9092 1 : gfc_error ("Missing closing paren for binding label at %C");
9093 1 : return MATCH_ERROR;
9094 : }
9095 :
9096 7681 : if (has_name_equals && !allow_binding_name)
9097 : {
9098 6 : gfc_error ("No binding name is allowed in BIND(C) at %C");
9099 6 : return MATCH_ERROR;
9100 : }
9101 :
9102 7675 : if (has_name_equals && sym != NULL && sym->attr.dummy)
9103 : {
9104 2 : gfc_error ("For dummy procedure %s, no binding name is "
9105 : "allowed in BIND(C) at %C", sym->name);
9106 2 : return MATCH_ERROR;
9107 : }
9108 :
9109 :
9110 : /* Save the binding label to the symbol. If sym is null, we're
9111 : probably matching the typespec attributes of a declaration and
9112 : haven't gotten the name yet, and therefore, no symbol yet. */
9113 7673 : if (binding_label)
9114 : {
9115 1139 : if (sym != NULL)
9116 1029 : sym->binding_label = binding_label;
9117 : else
9118 110 : curr_binding_label = binding_label;
9119 : }
9120 6534 : else if (allow_binding_name)
9121 : {
9122 : /* No binding label, but if symbol isn't null, we
9123 : can set the label for it here.
9124 : If name="" or allow_binding_name is false, no C binding name is
9125 : created. */
9126 6105 : if (sym != NULL && sym->name != NULL && has_name_equals == 0)
9127 5938 : sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
9128 : }
9129 :
9130 7673 : if (has_name_equals && gfc_current_state () == COMP_INTERFACE
9131 747 : && current_interface.type == INTERFACE_ABSTRACT)
9132 : {
9133 1 : gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
9134 1 : return MATCH_ERROR;
9135 : }
9136 :
9137 : return MATCH_YES;
9138 : }
9139 :
9140 :
9141 : /* Return nonzero if we're currently compiling a contained procedure. */
9142 :
9143 : static int
9144 65608 : contained_procedure (void)
9145 : {
9146 65608 : gfc_state_data *s = gfc_state_stack;
9147 :
9148 65608 : if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
9149 64621 : && s->previous != NULL && s->previous->state == COMP_CONTAINS)
9150 38148 : return 1;
9151 :
9152 : return 0;
9153 : }
9154 :
9155 : /* Set the kind of each enumerator. The kind is selected such that it is
9156 : interoperable with the corresponding C enumeration type, making
9157 : sure that -fshort-enums is honored. */
9158 :
9159 : static void
9160 158 : set_enum_kind(void)
9161 : {
9162 158 : enumerator_history *current_history = NULL;
9163 158 : int kind;
9164 158 : int i;
9165 :
9166 158 : if (max_enum == NULL || enum_history == NULL)
9167 : return;
9168 :
9169 150 : if (!flag_short_enums)
9170 : return;
9171 :
9172 : i = 0;
9173 48 : do
9174 : {
9175 48 : kind = gfc_integer_kinds[i++].kind;
9176 : }
9177 48 : while (kind < gfc_c_int_kind
9178 72 : && gfc_check_integer_range (max_enum->initializer->value.integer,
9179 : kind) != ARITH_OK);
9180 :
9181 24 : current_history = enum_history;
9182 96 : while (current_history != NULL)
9183 : {
9184 72 : current_history->sym->ts.kind = kind;
9185 72 : current_history = current_history->next;
9186 : }
9187 : }
9188 :
9189 :
9190 : /* Match any of the various end-block statements. Returns the type of
9191 : END to the caller. The END INTERFACE, END IF, END DO, END SELECT
9192 : and END BLOCK statements cannot be replaced by a single END statement. */
9193 :
9194 : match
9195 192533 : gfc_match_end (gfc_statement *st)
9196 : {
9197 192533 : char name[GFC_MAX_SYMBOL_LEN + 1];
9198 192533 : gfc_compile_state state;
9199 192533 : locus old_loc;
9200 192533 : const char *block_name;
9201 192533 : const char *target;
9202 192533 : int eos_ok;
9203 192533 : match m;
9204 192533 : gfc_namespace *parent_ns, *ns, *prev_ns;
9205 192533 : gfc_namespace **nsp;
9206 192533 : bool abbreviated_modproc_decl = false;
9207 192533 : bool got_matching_end = false;
9208 :
9209 192533 : old_loc = gfc_current_locus;
9210 192533 : if (gfc_match ("end") != MATCH_YES)
9211 : return MATCH_NO;
9212 :
9213 187386 : state = gfc_current_state ();
9214 103189 : block_name = gfc_current_block () == NULL
9215 187386 : ? NULL : gfc_current_block ()->name;
9216 :
9217 187386 : switch (state)
9218 : {
9219 3434 : case COMP_ASSOCIATE:
9220 3434 : case COMP_BLOCK:
9221 3434 : case COMP_CHANGE_TEAM:
9222 3434 : if (startswith (block_name, "block@"))
9223 : block_name = NULL;
9224 : break;
9225 :
9226 18241 : case COMP_CONTAINS:
9227 18241 : case COMP_DERIVED_CONTAINS:
9228 18241 : case COMP_OMP_BEGIN_METADIRECTIVE:
9229 18241 : state = gfc_state_stack->previous->state;
9230 16663 : block_name = gfc_state_stack->previous->sym == NULL
9231 18241 : ? NULL : gfc_state_stack->previous->sym->name;
9232 18241 : abbreviated_modproc_decl = gfc_state_stack->previous->sym
9233 18241 : && gfc_state_stack->previous->sym->abr_modproc_decl;
9234 : break;
9235 :
9236 : case COMP_OMP_METADIRECTIVE:
9237 : {
9238 : /* Metadirectives can be nested, so we need to drill down to the
9239 : first state that is not COMP_OMP_METADIRECTIVE. */
9240 : gfc_state_data *state_data = gfc_state_stack;
9241 :
9242 93 : do
9243 : {
9244 93 : state_data = state_data->previous;
9245 93 : state = state_data->state;
9246 81 : block_name = (state_data->sym == NULL
9247 93 : ? NULL : state_data->sym->name);
9248 186 : abbreviated_modproc_decl = (state_data->sym
9249 93 : && state_data->sym->abr_modproc_decl);
9250 : }
9251 93 : while (state == COMP_OMP_METADIRECTIVE);
9252 :
9253 87 : if (block_name && startswith (block_name, "block@"))
9254 : block_name = NULL;
9255 : }
9256 : break;
9257 :
9258 : default:
9259 : break;
9260 : }
9261 :
9262 87 : if (!abbreviated_modproc_decl)
9263 187385 : abbreviated_modproc_decl = gfc_current_block ()
9264 187385 : && gfc_current_block ()->abr_modproc_decl;
9265 :
9266 187386 : switch (state)
9267 : {
9268 28794 : case COMP_NONE:
9269 28794 : case COMP_PROGRAM:
9270 28794 : *st = ST_END_PROGRAM;
9271 28794 : target = " program";
9272 28794 : eos_ok = 1;
9273 28794 : break;
9274 :
9275 44691 : case COMP_SUBROUTINE:
9276 44691 : *st = ST_END_SUBROUTINE;
9277 44691 : if (!abbreviated_modproc_decl)
9278 : target = " subroutine";
9279 : else
9280 148 : target = " procedure";
9281 44691 : eos_ok = !contained_procedure ();
9282 44691 : break;
9283 :
9284 20917 : case COMP_FUNCTION:
9285 20917 : *st = ST_END_FUNCTION;
9286 20917 : if (!abbreviated_modproc_decl)
9287 : target = " function";
9288 : else
9289 135 : target = " procedure";
9290 20917 : eos_ok = !contained_procedure ();
9291 20917 : break;
9292 :
9293 87 : case COMP_BLOCK_DATA:
9294 87 : *st = ST_END_BLOCK_DATA;
9295 87 : target = " block data";
9296 87 : eos_ok = 1;
9297 87 : break;
9298 :
9299 10299 : case COMP_MODULE:
9300 10299 : *st = ST_END_MODULE;
9301 10299 : target = " module";
9302 10299 : eos_ok = 1;
9303 10299 : break;
9304 :
9305 274 : case COMP_SUBMODULE:
9306 274 : *st = ST_END_SUBMODULE;
9307 274 : target = " submodule";
9308 274 : eos_ok = 1;
9309 274 : break;
9310 :
9311 11691 : case COMP_INTERFACE:
9312 11691 : *st = ST_END_INTERFACE;
9313 11691 : target = " interface";
9314 11691 : eos_ok = 0;
9315 11691 : break;
9316 :
9317 257 : case COMP_MAP:
9318 257 : *st = ST_END_MAP;
9319 257 : target = " map";
9320 257 : eos_ok = 0;
9321 257 : break;
9322 :
9323 132 : case COMP_UNION:
9324 132 : *st = ST_END_UNION;
9325 132 : target = " union";
9326 132 : eos_ok = 0;
9327 132 : break;
9328 :
9329 313 : case COMP_STRUCTURE:
9330 313 : *st = ST_END_STRUCTURE;
9331 313 : target = " structure";
9332 313 : eos_ok = 0;
9333 313 : break;
9334 :
9335 14022 : case COMP_DERIVED:
9336 14022 : case COMP_DERIVED_CONTAINS:
9337 14022 : *st = ST_END_TYPE;
9338 14022 : target = " type";
9339 14022 : eos_ok = 0;
9340 14022 : break;
9341 :
9342 1782 : case COMP_ASSOCIATE:
9343 1782 : *st = ST_END_ASSOCIATE;
9344 1782 : target = " associate";
9345 1782 : eos_ok = 0;
9346 1782 : break;
9347 :
9348 1550 : case COMP_BLOCK:
9349 1550 : case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
9350 1550 : *st = ST_END_BLOCK;
9351 1550 : target = " block";
9352 1550 : eos_ok = 0;
9353 1550 : break;
9354 :
9355 15117 : case COMP_IF:
9356 15117 : *st = ST_ENDIF;
9357 15117 : target = " if";
9358 15117 : eos_ok = 0;
9359 15117 : break;
9360 :
9361 31366 : case COMP_DO:
9362 31366 : case COMP_DO_CONCURRENT:
9363 31366 : *st = ST_ENDDO;
9364 31366 : target = " do";
9365 31366 : eos_ok = 0;
9366 31366 : break;
9367 :
9368 54 : case COMP_CRITICAL:
9369 54 : *st = ST_END_CRITICAL;
9370 54 : target = " critical";
9371 54 : eos_ok = 0;
9372 54 : break;
9373 :
9374 4859 : case COMP_SELECT:
9375 4859 : case COMP_SELECT_TYPE:
9376 4859 : case COMP_SELECT_RANK:
9377 4859 : *st = ST_END_SELECT;
9378 4859 : target = " select";
9379 4859 : eos_ok = 0;
9380 4859 : break;
9381 :
9382 509 : case COMP_FORALL:
9383 509 : *st = ST_END_FORALL;
9384 509 : target = " forall";
9385 509 : eos_ok = 0;
9386 509 : break;
9387 :
9388 373 : case COMP_WHERE:
9389 373 : *st = ST_END_WHERE;
9390 373 : target = " where";
9391 373 : eos_ok = 0;
9392 373 : break;
9393 :
9394 158 : case COMP_ENUM:
9395 158 : *st = ST_END_ENUM;
9396 158 : target = " enum";
9397 158 : eos_ok = 0;
9398 158 : last_initializer = NULL;
9399 158 : set_enum_kind ();
9400 158 : gfc_free_enum_history ();
9401 158 : break;
9402 :
9403 0 : case COMP_OMP_BEGIN_METADIRECTIVE:
9404 0 : *st = ST_OMP_END_METADIRECTIVE;
9405 0 : target = " metadirective";
9406 0 : eos_ok = 0;
9407 0 : break;
9408 :
9409 132 : case COMP_CHANGE_TEAM:
9410 132 : *st = ST_END_TEAM;
9411 132 : target = " team";
9412 132 : eos_ok = 0;
9413 132 : break;
9414 :
9415 9 : default:
9416 9 : gfc_error ("Unexpected END statement at %C");
9417 9 : goto cleanup;
9418 : }
9419 :
9420 187377 : old_loc = gfc_current_locus;
9421 187377 : if (gfc_match_eos () == MATCH_YES)
9422 : {
9423 20968 : if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
9424 : {
9425 8307 : if (!gfc_notify_std (GFC_STD_F2008, "END statement "
9426 : "instead of %s statement at %L",
9427 : abbreviated_modproc_decl ? "END PROCEDURE"
9428 4141 : : gfc_ascii_statement(*st), &old_loc))
9429 4 : goto cleanup;
9430 : }
9431 9 : else if (!eos_ok)
9432 : {
9433 : /* We would have required END [something]. */
9434 9 : gfc_error ("%s statement expected at %L",
9435 : gfc_ascii_statement (*st), &old_loc);
9436 9 : goto cleanup;
9437 : }
9438 :
9439 : return MATCH_YES;
9440 : }
9441 :
9442 : /* Verify that we've got the sort of end-block that we're expecting. */
9443 166409 : if (gfc_match (target) != MATCH_YES)
9444 : {
9445 331 : gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
9446 165 : ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
9447 166 : goto cleanup;
9448 : }
9449 : else
9450 166243 : got_matching_end = true;
9451 :
9452 166243 : if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
9453 : /* Emit errors of stat and errmsg parsing now to finish the block and
9454 : continue analysis of compilation unit. */
9455 2 : gfc_error_check ();
9456 :
9457 166243 : old_loc = gfc_current_locus;
9458 : /* If we're at the end, make sure a block name wasn't required. */
9459 166243 : if (gfc_match_eos () == MATCH_YES)
9460 : {
9461 110426 : if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
9462 : && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
9463 : && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
9464 : && *st != ST_END_TEAM)
9465 : return MATCH_YES;
9466 :
9467 55243 : if (!block_name)
9468 : return MATCH_YES;
9469 :
9470 8 : gfc_error ("Expected block name of %qs in %s statement at %L",
9471 : block_name, gfc_ascii_statement (*st), &old_loc);
9472 :
9473 8 : return MATCH_ERROR;
9474 : }
9475 :
9476 : /* END INTERFACE has a special handler for its several possible endings. */
9477 55817 : if (*st == ST_END_INTERFACE)
9478 696 : return gfc_match_end_interface ();
9479 :
9480 : /* We haven't hit the end of statement, so what is left must be an
9481 : end-name. */
9482 55121 : m = gfc_match_space ();
9483 55121 : if (m == MATCH_YES)
9484 55121 : m = gfc_match_name (name);
9485 :
9486 55121 : if (m == MATCH_NO)
9487 0 : gfc_error ("Expected terminating name at %C");
9488 55121 : if (m != MATCH_YES)
9489 0 : goto cleanup;
9490 :
9491 55121 : if (block_name == NULL)
9492 15 : goto syntax;
9493 :
9494 : /* We have to pick out the declared submodule name from the composite
9495 : required by F2008:11.2.3 para 2, which ends in the declared name. */
9496 55106 : if (state == COMP_SUBMODULE)
9497 137 : block_name = strchr (block_name, '.') + 1;
9498 :
9499 55106 : if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
9500 : {
9501 8 : gfc_error ("Expected label %qs for %s statement at %C", block_name,
9502 : gfc_ascii_statement (*st));
9503 8 : goto cleanup;
9504 : }
9505 : /* Procedure pointer as function result. */
9506 55098 : else if (strcmp (block_name, "ppr@") == 0
9507 21 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
9508 : {
9509 0 : gfc_error ("Expected label %qs for %s statement at %C",
9510 0 : gfc_current_block ()->ns->proc_name->name,
9511 : gfc_ascii_statement (*st));
9512 0 : goto cleanup;
9513 : }
9514 :
9515 55098 : if (gfc_match_eos () == MATCH_YES)
9516 : return MATCH_YES;
9517 :
9518 0 : syntax:
9519 15 : gfc_syntax_error (*st);
9520 :
9521 211 : cleanup:
9522 211 : gfc_current_locus = old_loc;
9523 :
9524 : /* If we are missing an END BLOCK, we created a half-ready namespace.
9525 : Remove it from the parent namespace's sibling list. */
9526 :
9527 211 : if (state == COMP_BLOCK && !got_matching_end)
9528 : {
9529 7 : parent_ns = gfc_current_ns->parent;
9530 :
9531 7 : nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
9532 :
9533 7 : prev_ns = NULL;
9534 7 : ns = *nsp;
9535 14 : while (ns)
9536 : {
9537 7 : if (ns == gfc_current_ns)
9538 : {
9539 7 : if (prev_ns == NULL)
9540 7 : *nsp = NULL;
9541 : else
9542 0 : prev_ns->sibling = ns->sibling;
9543 : }
9544 7 : prev_ns = ns;
9545 7 : ns = ns->sibling;
9546 : }
9547 :
9548 : /* The namespace can still be referenced by parser state and code nodes;
9549 : let normal block unwinding/freeing own its lifetime. */
9550 7 : gfc_current_ns = parent_ns;
9551 7 : gfc_state_stack = gfc_state_stack->previous;
9552 7 : state = gfc_current_state ();
9553 : }
9554 :
9555 : return MATCH_ERROR;
9556 : }
9557 :
9558 :
9559 :
9560 : /***************** Attribute declaration statements ****************/
9561 :
9562 : /* Set the attribute of a single variable. */
9563 :
9564 : static match
9565 10428 : attr_decl1 (void)
9566 : {
9567 10428 : char name[GFC_MAX_SYMBOL_LEN + 1];
9568 10428 : gfc_array_spec *as;
9569 :
9570 : /* Workaround -Wmaybe-uninitialized false positive during
9571 : profiledbootstrap by initializing them. */
9572 10428 : gfc_symbol *sym = NULL;
9573 10428 : locus var_locus;
9574 10428 : match m;
9575 :
9576 10428 : as = NULL;
9577 :
9578 10428 : m = gfc_match_name (name);
9579 10428 : if (m != MATCH_YES)
9580 0 : goto cleanup;
9581 :
9582 10428 : if (find_special (name, &sym, false))
9583 : return MATCH_ERROR;
9584 :
9585 10428 : if (!check_function_name (name))
9586 : {
9587 7 : m = MATCH_ERROR;
9588 7 : goto cleanup;
9589 : }
9590 :
9591 10421 : var_locus = gfc_current_locus;
9592 :
9593 : /* Deal with possible array specification for certain attributes. */
9594 10421 : if (current_attr.dimension
9595 8842 : || current_attr.codimension
9596 8820 : || current_attr.allocatable
9597 8396 : || current_attr.pointer
9598 7673 : || current_attr.target)
9599 : {
9600 6174 : m = gfc_match_array_spec (&as, !current_attr.codimension,
9601 : !current_attr.dimension
9602 1395 : && !current_attr.pointer
9603 : && !current_attr.target);
9604 2974 : if (m == MATCH_ERROR)
9605 2 : goto cleanup;
9606 :
9607 2972 : if (current_attr.dimension && m == MATCH_NO)
9608 : {
9609 0 : gfc_error ("Missing array specification at %L in DIMENSION "
9610 : "statement", &var_locus);
9611 0 : m = MATCH_ERROR;
9612 0 : goto cleanup;
9613 : }
9614 :
9615 2972 : if (current_attr.dimension && sym->value)
9616 : {
9617 1 : gfc_error ("Dimensions specified for %s at %L after its "
9618 : "initialization", sym->name, &var_locus);
9619 1 : m = MATCH_ERROR;
9620 1 : goto cleanup;
9621 : }
9622 :
9623 2971 : if (current_attr.codimension && m == MATCH_NO)
9624 : {
9625 0 : gfc_error ("Missing array specification at %L in CODIMENSION "
9626 : "statement", &var_locus);
9627 0 : m = MATCH_ERROR;
9628 0 : goto cleanup;
9629 : }
9630 :
9631 2971 : if ((current_attr.allocatable || current_attr.pointer)
9632 1147 : && (m == MATCH_YES) && (as->type != AS_DEFERRED))
9633 : {
9634 0 : gfc_error ("Array specification must be deferred at %L", &var_locus);
9635 0 : m = MATCH_ERROR;
9636 0 : goto cleanup;
9637 : }
9638 : }
9639 :
9640 10418 : if (sym->ts.type == BT_CLASS
9641 200 : && sym->ts.u.derived
9642 200 : && sym->ts.u.derived->attr.is_class)
9643 : {
9644 177 : sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
9645 177 : sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
9646 177 : sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
9647 177 : sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
9648 177 : if (CLASS_DATA (sym)->as)
9649 123 : sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
9650 : }
9651 8841 : if (current_attr.dimension == 0 && current_attr.codimension == 0
9652 19238 : && !gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus))
9653 : {
9654 22 : m = MATCH_ERROR;
9655 22 : goto cleanup;
9656 : }
9657 10396 : if (!gfc_set_array_spec (sym, as, &var_locus))
9658 : {
9659 17 : m = MATCH_ERROR;
9660 17 : goto cleanup;
9661 : }
9662 :
9663 10379 : if (sym->attr.cray_pointee && sym->as != NULL)
9664 : {
9665 : /* Fix the array spec. */
9666 2 : m = gfc_mod_pointee_as (sym->as);
9667 2 : if (m == MATCH_ERROR)
9668 0 : goto cleanup;
9669 : }
9670 :
9671 10379 : if (!gfc_add_attribute (&sym->attr, &var_locus))
9672 : {
9673 0 : m = MATCH_ERROR;
9674 0 : goto cleanup;
9675 : }
9676 :
9677 5741 : if ((current_attr.external || current_attr.intrinsic)
9678 6290 : && sym->attr.flavor != FL_PROCEDURE
9679 16637 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
9680 : {
9681 0 : m = MATCH_ERROR;
9682 0 : goto cleanup;
9683 : }
9684 :
9685 10379 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
9686 169 : && !as && !current_attr.pointer && !current_attr.allocatable
9687 136 : && !current_attr.external)
9688 : {
9689 136 : sym->attr.pointer = 0;
9690 136 : sym->attr.allocatable = 0;
9691 136 : sym->attr.dimension = 0;
9692 136 : sym->attr.codimension = 0;
9693 136 : gfc_free_array_spec (sym->as);
9694 136 : sym->as = NULL;
9695 : }
9696 10243 : else if (sym->ts.type == BT_CLASS
9697 10243 : && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
9698 : {
9699 0 : m = MATCH_ERROR;
9700 0 : goto cleanup;
9701 : }
9702 :
9703 10379 : add_hidden_procptr_result (sym);
9704 :
9705 10379 : return MATCH_YES;
9706 :
9707 49 : cleanup:
9708 49 : gfc_free_array_spec (as);
9709 49 : return m;
9710 : }
9711 :
9712 :
9713 : /* Generic attribute declaration subroutine. Used for attributes that
9714 : just have a list of names. */
9715 :
9716 : static match
9717 6713 : attr_decl (void)
9718 : {
9719 6713 : match m;
9720 :
9721 : /* Gobble the optional double colon, by simply ignoring the result
9722 : of gfc_match(). */
9723 6713 : gfc_match (" ::");
9724 :
9725 10428 : for (;;)
9726 : {
9727 10428 : m = attr_decl1 ();
9728 10428 : if (m != MATCH_YES)
9729 : break;
9730 :
9731 10379 : if (gfc_match_eos () == MATCH_YES)
9732 : {
9733 : m = MATCH_YES;
9734 : break;
9735 : }
9736 :
9737 3715 : if (gfc_match_char (',') != MATCH_YES)
9738 : {
9739 0 : gfc_error ("Unexpected character in variable list at %C");
9740 0 : m = MATCH_ERROR;
9741 0 : break;
9742 : }
9743 : }
9744 :
9745 6713 : return m;
9746 : }
9747 :
9748 :
9749 : /* This routine matches Cray Pointer declarations of the form:
9750 : pointer ( <pointer>, <pointee> )
9751 : or
9752 : pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
9753 : The pointer, if already declared, should be an integer. Otherwise, we
9754 : set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
9755 : be either a scalar, or an array declaration. No space is allocated for
9756 : the pointee. For the statement
9757 : pointer (ipt, ar(10))
9758 : any subsequent uses of ar will be translated (in C-notation) as
9759 : ar(i) => ((<type> *) ipt)(i)
9760 : After gimplification, pointee variable will disappear in the code. */
9761 :
9762 : static match
9763 334 : cray_pointer_decl (void)
9764 : {
9765 334 : match m;
9766 334 : gfc_array_spec *as = NULL;
9767 334 : gfc_symbol *cptr; /* Pointer symbol. */
9768 334 : gfc_symbol *cpte; /* Pointee symbol. */
9769 334 : locus var_locus;
9770 334 : bool done = false;
9771 :
9772 334 : while (!done)
9773 : {
9774 347 : if (gfc_match_char ('(') != MATCH_YES)
9775 : {
9776 1 : gfc_error ("Expected %<(%> at %C");
9777 1 : return MATCH_ERROR;
9778 : }
9779 :
9780 : /* Match pointer. */
9781 346 : var_locus = gfc_current_locus;
9782 346 : gfc_clear_attr (¤t_attr);
9783 346 : gfc_add_cray_pointer (¤t_attr, &var_locus);
9784 346 : current_ts.type = BT_INTEGER;
9785 346 : current_ts.kind = gfc_index_integer_kind;
9786 :
9787 346 : m = gfc_match_symbol (&cptr, 0);
9788 346 : if (m != MATCH_YES)
9789 : {
9790 2 : gfc_error ("Expected variable name at %C");
9791 2 : return m;
9792 : }
9793 :
9794 344 : if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
9795 : return MATCH_ERROR;
9796 :
9797 341 : gfc_set_sym_referenced (cptr);
9798 :
9799 341 : if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
9800 : {
9801 327 : cptr->ts.type = BT_INTEGER;
9802 327 : cptr->ts.kind = gfc_index_integer_kind;
9803 : }
9804 14 : else if (cptr->ts.type != BT_INTEGER)
9805 : {
9806 1 : gfc_error ("Cray pointer at %C must be an integer");
9807 1 : return MATCH_ERROR;
9808 : }
9809 13 : else if (cptr->ts.kind < gfc_index_integer_kind)
9810 0 : gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
9811 : " memory addresses require %d bytes",
9812 : cptr->ts.kind, gfc_index_integer_kind);
9813 :
9814 340 : if (gfc_match_char (',') != MATCH_YES)
9815 : {
9816 2 : gfc_error ("Expected \",\" at %C");
9817 2 : return MATCH_ERROR;
9818 : }
9819 :
9820 : /* Match Pointee. */
9821 338 : var_locus = gfc_current_locus;
9822 338 : gfc_clear_attr (¤t_attr);
9823 338 : gfc_add_cray_pointee (¤t_attr, &var_locus);
9824 338 : current_ts.type = BT_UNKNOWN;
9825 338 : current_ts.kind = 0;
9826 :
9827 338 : m = gfc_match_symbol (&cpte, 0);
9828 338 : if (m != MATCH_YES)
9829 : {
9830 2 : gfc_error ("Expected variable name at %C");
9831 2 : return m;
9832 : }
9833 :
9834 : /* Check for an optional array spec. */
9835 336 : m = gfc_match_array_spec (&as, true, false);
9836 336 : if (m == MATCH_ERROR)
9837 : {
9838 0 : gfc_free_array_spec (as);
9839 0 : return m;
9840 : }
9841 336 : else if (m == MATCH_NO)
9842 : {
9843 226 : gfc_free_array_spec (as);
9844 226 : as = NULL;
9845 : }
9846 :
9847 336 : if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
9848 : return MATCH_ERROR;
9849 :
9850 329 : gfc_set_sym_referenced (cpte);
9851 :
9852 329 : if (cpte->as == NULL)
9853 : {
9854 247 : if (!gfc_set_array_spec (cpte, as, &var_locus))
9855 0 : gfc_internal_error ("Cannot set Cray pointee array spec.");
9856 : }
9857 82 : else if (as != NULL)
9858 : {
9859 1 : gfc_error ("Duplicate array spec for Cray pointee at %C");
9860 1 : gfc_free_array_spec (as);
9861 1 : return MATCH_ERROR;
9862 : }
9863 :
9864 328 : as = NULL;
9865 :
9866 328 : if (cpte->as != NULL)
9867 : {
9868 : /* Fix array spec. */
9869 190 : m = gfc_mod_pointee_as (cpte->as);
9870 190 : if (m == MATCH_ERROR)
9871 : return m;
9872 : }
9873 :
9874 : /* Point the Pointee at the Pointer. */
9875 328 : cpte->cp_pointer = cptr;
9876 :
9877 328 : if (gfc_match_char (')') != MATCH_YES)
9878 : {
9879 2 : gfc_error ("Expected \")\" at %C");
9880 2 : return MATCH_ERROR;
9881 : }
9882 326 : m = gfc_match_char (',');
9883 326 : if (m != MATCH_YES)
9884 : done = true; /* Stop searching for more declarations. */
9885 :
9886 : }
9887 :
9888 313 : if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
9889 313 : || gfc_match_eos () != MATCH_YES)
9890 : {
9891 0 : gfc_error ("Expected %<,%> or end of statement at %C");
9892 0 : return MATCH_ERROR;
9893 : }
9894 : return MATCH_YES;
9895 : }
9896 :
9897 :
9898 : match
9899 3216 : gfc_match_external (void)
9900 : {
9901 :
9902 3216 : gfc_clear_attr (¤t_attr);
9903 3216 : current_attr.external = 1;
9904 :
9905 3216 : return attr_decl ();
9906 : }
9907 :
9908 :
9909 : match
9910 208 : gfc_match_intent (void)
9911 : {
9912 208 : sym_intent intent;
9913 :
9914 : /* This is not allowed within a BLOCK construct! */
9915 208 : if (gfc_current_state () == COMP_BLOCK)
9916 : {
9917 2 : gfc_error ("INTENT is not allowed inside of BLOCK at %C");
9918 2 : return MATCH_ERROR;
9919 : }
9920 :
9921 206 : intent = match_intent_spec ();
9922 206 : if (intent == INTENT_UNKNOWN)
9923 : return MATCH_ERROR;
9924 :
9925 206 : gfc_clear_attr (¤t_attr);
9926 206 : current_attr.intent = intent;
9927 :
9928 206 : return attr_decl ();
9929 : }
9930 :
9931 :
9932 : match
9933 1482 : gfc_match_intrinsic (void)
9934 : {
9935 :
9936 1482 : gfc_clear_attr (¤t_attr);
9937 1482 : current_attr.intrinsic = 1;
9938 :
9939 1482 : return attr_decl ();
9940 : }
9941 :
9942 :
9943 : match
9944 220 : gfc_match_optional (void)
9945 : {
9946 : /* This is not allowed within a BLOCK construct! */
9947 220 : if (gfc_current_state () == COMP_BLOCK)
9948 : {
9949 2 : gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
9950 2 : return MATCH_ERROR;
9951 : }
9952 :
9953 218 : gfc_clear_attr (¤t_attr);
9954 218 : current_attr.optional = 1;
9955 :
9956 218 : return attr_decl ();
9957 : }
9958 :
9959 :
9960 : match
9961 915 : gfc_match_pointer (void)
9962 : {
9963 915 : gfc_gobble_whitespace ();
9964 915 : if (gfc_peek_ascii_char () == '(')
9965 : {
9966 335 : if (!flag_cray_pointer)
9967 : {
9968 1 : gfc_error ("Cray pointer declaration at %C requires "
9969 : "%<-fcray-pointer%> flag");
9970 1 : return MATCH_ERROR;
9971 : }
9972 334 : return cray_pointer_decl ();
9973 : }
9974 : else
9975 : {
9976 580 : gfc_clear_attr (¤t_attr);
9977 580 : current_attr.pointer = 1;
9978 :
9979 580 : return attr_decl ();
9980 : }
9981 : }
9982 :
9983 :
9984 : match
9985 162 : gfc_match_allocatable (void)
9986 : {
9987 162 : gfc_clear_attr (¤t_attr);
9988 162 : current_attr.allocatable = 1;
9989 :
9990 162 : return attr_decl ();
9991 : }
9992 :
9993 :
9994 : match
9995 23 : gfc_match_codimension (void)
9996 : {
9997 23 : gfc_clear_attr (¤t_attr);
9998 23 : current_attr.codimension = 1;
9999 :
10000 23 : return attr_decl ();
10001 : }
10002 :
10003 :
10004 : match
10005 80 : gfc_match_contiguous (void)
10006 : {
10007 80 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
10008 : return MATCH_ERROR;
10009 :
10010 79 : gfc_clear_attr (¤t_attr);
10011 79 : current_attr.contiguous = 1;
10012 :
10013 79 : return attr_decl ();
10014 : }
10015 :
10016 :
10017 : match
10018 648 : gfc_match_dimension (void)
10019 : {
10020 648 : gfc_clear_attr (¤t_attr);
10021 648 : current_attr.dimension = 1;
10022 :
10023 648 : return attr_decl ();
10024 : }
10025 :
10026 :
10027 : match
10028 99 : gfc_match_target (void)
10029 : {
10030 99 : gfc_clear_attr (¤t_attr);
10031 99 : current_attr.target = 1;
10032 :
10033 99 : return attr_decl ();
10034 : }
10035 :
10036 :
10037 : /* Match the list of entities being specified in a PUBLIC or PRIVATE
10038 : statement. */
10039 :
10040 : static match
10041 1766 : access_attr_decl (gfc_statement st)
10042 : {
10043 1766 : char name[GFC_MAX_SYMBOL_LEN + 1];
10044 1766 : interface_type type;
10045 1766 : gfc_user_op *uop;
10046 1766 : gfc_symbol *sym, *dt_sym;
10047 1766 : gfc_intrinsic_op op;
10048 1766 : match m;
10049 1766 : gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
10050 :
10051 1766 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10052 0 : goto done;
10053 :
10054 2916 : for (;;)
10055 : {
10056 2916 : m = gfc_match_generic_spec (&type, name, &op);
10057 2916 : if (m == MATCH_NO)
10058 0 : goto syntax;
10059 2916 : if (m == MATCH_ERROR)
10060 0 : goto done;
10061 :
10062 2916 : switch (type)
10063 : {
10064 0 : case INTERFACE_NAMELESS:
10065 0 : case INTERFACE_ABSTRACT:
10066 0 : goto syntax;
10067 :
10068 2839 : case INTERFACE_GENERIC:
10069 2839 : case INTERFACE_DTIO:
10070 :
10071 2839 : if (gfc_get_symbol (name, NULL, &sym))
10072 0 : goto done;
10073 :
10074 2839 : if (type == INTERFACE_DTIO
10075 26 : && gfc_current_ns->proc_name
10076 26 : && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
10077 26 : && sym->attr.flavor == FL_UNKNOWN)
10078 2 : sym->attr.flavor = FL_PROCEDURE;
10079 :
10080 2839 : if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
10081 4 : goto done;
10082 :
10083 330 : if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
10084 2892 : && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
10085 0 : goto done;
10086 :
10087 : break;
10088 :
10089 72 : case INTERFACE_INTRINSIC_OP:
10090 72 : if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
10091 : {
10092 72 : gfc_intrinsic_op other_op;
10093 :
10094 72 : gfc_current_ns->operator_access[op] = access;
10095 :
10096 : /* Handle the case if there is another op with the same
10097 : function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
10098 72 : other_op = gfc_equivalent_op (op);
10099 :
10100 72 : if (other_op != INTRINSIC_NONE)
10101 21 : gfc_current_ns->operator_access[other_op] = access;
10102 : }
10103 : else
10104 : {
10105 0 : gfc_error ("Access specification of the %s operator at %C has "
10106 : "already been specified", gfc_op2string (op));
10107 0 : goto done;
10108 : }
10109 :
10110 : break;
10111 :
10112 5 : case INTERFACE_USER_OP:
10113 5 : uop = gfc_get_uop (name);
10114 :
10115 5 : if (uop->access == ACCESS_UNKNOWN)
10116 : {
10117 4 : uop->access = access;
10118 : }
10119 : else
10120 : {
10121 1 : gfc_error ("Access specification of the .%s. operator at %C "
10122 : "has already been specified", uop->name);
10123 1 : goto done;
10124 : }
10125 :
10126 4 : break;
10127 : }
10128 :
10129 2911 : if (gfc_match_char (',') == MATCH_NO)
10130 : break;
10131 : }
10132 :
10133 1761 : if (gfc_match_eos () != MATCH_YES)
10134 0 : goto syntax;
10135 : return MATCH_YES;
10136 :
10137 0 : syntax:
10138 0 : gfc_syntax_error (st);
10139 :
10140 1766 : done:
10141 : return MATCH_ERROR;
10142 : }
10143 :
10144 :
10145 : match
10146 23 : gfc_match_protected (void)
10147 : {
10148 23 : gfc_symbol *sym;
10149 23 : match m;
10150 23 : char c;
10151 :
10152 : /* PROTECTED has already been seen, but must be followed by whitespace
10153 : or ::. */
10154 23 : c = gfc_peek_ascii_char ();
10155 23 : if (!gfc_is_whitespace (c) && c != ':')
10156 : return MATCH_NO;
10157 :
10158 22 : if (!gfc_current_ns->proc_name
10159 20 : || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
10160 : {
10161 3 : gfc_error ("PROTECTED at %C only allowed in specification "
10162 : "part of a module");
10163 3 : return MATCH_ERROR;
10164 :
10165 : }
10166 :
10167 19 : gfc_match (" ::");
10168 :
10169 19 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
10170 : return MATCH_ERROR;
10171 :
10172 : /* PROTECTED has an entity-list. */
10173 18 : if (gfc_match_eos () == MATCH_YES)
10174 0 : goto syntax;
10175 :
10176 26 : for(;;)
10177 : {
10178 26 : m = gfc_match_symbol (&sym, 0);
10179 26 : switch (m)
10180 : {
10181 26 : case MATCH_YES:
10182 26 : if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
10183 : return MATCH_ERROR;
10184 25 : goto next_item;
10185 :
10186 : case MATCH_NO:
10187 : break;
10188 :
10189 : case MATCH_ERROR:
10190 : return MATCH_ERROR;
10191 : }
10192 :
10193 25 : next_item:
10194 25 : if (gfc_match_eos () == MATCH_YES)
10195 : break;
10196 8 : if (gfc_match_char (',') != MATCH_YES)
10197 0 : goto syntax;
10198 : }
10199 :
10200 : return MATCH_YES;
10201 :
10202 0 : syntax:
10203 0 : gfc_error ("Syntax error in PROTECTED statement at %C");
10204 0 : return MATCH_ERROR;
10205 : }
10206 :
10207 :
10208 : /* The PRIVATE statement is a bit weird in that it can be an attribute
10209 : declaration, but also works as a standalone statement inside of a
10210 : type declaration or a module. */
10211 :
10212 : match
10213 30002 : gfc_match_private (gfc_statement *st)
10214 : {
10215 30002 : gfc_state_data *prev;
10216 :
10217 30002 : if (gfc_match ("private") != MATCH_YES)
10218 : return MATCH_NO;
10219 :
10220 : /* Try matching PRIVATE without an access-list. */
10221 1635 : if (gfc_match_eos () == MATCH_YES)
10222 : {
10223 1348 : prev = gfc_state_stack->previous;
10224 1348 : if (gfc_current_state () != COMP_MODULE
10225 367 : && !(gfc_current_state () == COMP_DERIVED
10226 334 : && prev && prev->state == COMP_MODULE)
10227 34 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
10228 32 : && prev->previous && prev->previous->state == COMP_MODULE))
10229 : {
10230 2 : gfc_error ("PRIVATE statement at %C is only allowed in the "
10231 : "specification part of a module");
10232 2 : return MATCH_ERROR;
10233 : }
10234 :
10235 1346 : *st = ST_PRIVATE;
10236 1346 : return MATCH_YES;
10237 : }
10238 :
10239 : /* At this point in free-form source code, PRIVATE must be followed
10240 : by whitespace or ::. */
10241 287 : if (gfc_current_form == FORM_FREE)
10242 : {
10243 285 : char c = gfc_peek_ascii_char ();
10244 285 : if (!gfc_is_whitespace (c) && c != ':')
10245 : return MATCH_NO;
10246 : }
10247 :
10248 286 : prev = gfc_state_stack->previous;
10249 286 : if (gfc_current_state () != COMP_MODULE
10250 1 : && !(gfc_current_state () == COMP_DERIVED
10251 0 : && prev && prev->state == COMP_MODULE)
10252 1 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
10253 0 : && prev->previous && prev->previous->state == COMP_MODULE))
10254 : {
10255 1 : gfc_error ("PRIVATE statement at %C is only allowed in the "
10256 : "specification part of a module");
10257 1 : return MATCH_ERROR;
10258 : }
10259 :
10260 285 : *st = ST_ATTR_DECL;
10261 285 : return access_attr_decl (ST_PRIVATE);
10262 : }
10263 :
10264 :
10265 : match
10266 1882 : gfc_match_public (gfc_statement *st)
10267 : {
10268 1882 : if (gfc_match ("public") != MATCH_YES)
10269 : return MATCH_NO;
10270 :
10271 : /* Try matching PUBLIC without an access-list. */
10272 1528 : if (gfc_match_eos () == MATCH_YES)
10273 : {
10274 45 : if (gfc_current_state () != COMP_MODULE)
10275 : {
10276 2 : gfc_error ("PUBLIC statement at %C is only allowed in the "
10277 : "specification part of a module");
10278 2 : return MATCH_ERROR;
10279 : }
10280 :
10281 43 : *st = ST_PUBLIC;
10282 43 : return MATCH_YES;
10283 : }
10284 :
10285 : /* At this point in free-form source code, PUBLIC must be followed
10286 : by whitespace or ::. */
10287 1483 : if (gfc_current_form == FORM_FREE)
10288 : {
10289 1481 : char c = gfc_peek_ascii_char ();
10290 1481 : if (!gfc_is_whitespace (c) && c != ':')
10291 : return MATCH_NO;
10292 : }
10293 :
10294 1482 : if (gfc_current_state () != COMP_MODULE)
10295 : {
10296 1 : gfc_error ("PUBLIC statement at %C is only allowed in the "
10297 : "specification part of a module");
10298 1 : return MATCH_ERROR;
10299 : }
10300 :
10301 1481 : *st = ST_ATTR_DECL;
10302 1481 : return access_attr_decl (ST_PUBLIC);
10303 : }
10304 :
10305 :
10306 : /* Workhorse for gfc_match_parameter. */
10307 :
10308 : static match
10309 8716 : do_parm (void)
10310 : {
10311 8716 : gfc_symbol *sym;
10312 8716 : gfc_expr *init;
10313 8716 : gfc_charlen *saved_cl_list;
10314 8716 : match m;
10315 8716 : bool t;
10316 :
10317 8716 : saved_cl_list = gfc_current_ns->cl_list;
10318 :
10319 8716 : m = gfc_match_symbol (&sym, 0);
10320 8716 : if (m == MATCH_NO)
10321 0 : gfc_error ("Expected variable name at %C in PARAMETER statement");
10322 :
10323 8716 : if (m != MATCH_YES)
10324 : return m;
10325 :
10326 8716 : if (gfc_match_char ('=') == MATCH_NO)
10327 : {
10328 0 : gfc_error ("Expected = sign in PARAMETER statement at %C");
10329 0 : return MATCH_ERROR;
10330 : }
10331 :
10332 8716 : m = gfc_match_init_expr (&init);
10333 8716 : if (m == MATCH_NO)
10334 0 : gfc_error ("Expected expression at %C in PARAMETER statement");
10335 8716 : if (m != MATCH_YES)
10336 : return m;
10337 :
10338 8715 : if (sym->ts.type == BT_UNKNOWN
10339 8715 : && !gfc_set_default_type (sym, 1, NULL))
10340 : {
10341 1 : m = MATCH_ERROR;
10342 1 : goto cleanup;
10343 : }
10344 :
10345 8714 : if (!gfc_check_assign_symbol (sym, NULL, init)
10346 8714 : || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
10347 : {
10348 1 : m = MATCH_ERROR;
10349 1 : goto cleanup;
10350 : }
10351 :
10352 8713 : if (sym->value)
10353 : {
10354 1 : gfc_error ("Initializing already initialized variable at %C");
10355 1 : m = MATCH_ERROR;
10356 1 : goto cleanup;
10357 : }
10358 :
10359 8712 : t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus,
10360 : saved_cl_list);
10361 8712 : return (t) ? MATCH_YES : MATCH_ERROR;
10362 :
10363 3 : cleanup:
10364 3 : gfc_free_expr (init);
10365 3 : return m;
10366 : }
10367 :
10368 :
10369 : /* Match a parameter statement, with the weird syntax that these have. */
10370 :
10371 : match
10372 8003 : gfc_match_parameter (void)
10373 : {
10374 8003 : const char *term = " )%t";
10375 8003 : match m;
10376 :
10377 8003 : if (gfc_match_char ('(') == MATCH_NO)
10378 : {
10379 : /* With legacy PARAMETER statements, don't expect a terminating ')'. */
10380 28 : if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
10381 : return MATCH_NO;
10382 8002 : term = " %t";
10383 : }
10384 :
10385 8716 : for (;;)
10386 : {
10387 8716 : m = do_parm ();
10388 8716 : if (m != MATCH_YES)
10389 : break;
10390 :
10391 8712 : if (gfc_match (term) == MATCH_YES)
10392 : break;
10393 :
10394 714 : if (gfc_match_char (',') != MATCH_YES)
10395 : {
10396 0 : gfc_error ("Unexpected characters in PARAMETER statement at %C");
10397 0 : m = MATCH_ERROR;
10398 0 : break;
10399 : }
10400 : }
10401 :
10402 : return m;
10403 : }
10404 :
10405 :
10406 : match
10407 8 : gfc_match_automatic (void)
10408 : {
10409 8 : gfc_symbol *sym;
10410 8 : match m;
10411 8 : bool seen_symbol = false;
10412 :
10413 8 : if (!flag_dec_static)
10414 : {
10415 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10416 : "%<-fdec-static%>",
10417 : "AUTOMATIC"
10418 : );
10419 2 : return MATCH_ERROR;
10420 : }
10421 :
10422 6 : gfc_match (" ::");
10423 :
10424 6 : for (;;)
10425 : {
10426 6 : m = gfc_match_symbol (&sym, 0);
10427 6 : switch (m)
10428 : {
10429 : case MATCH_NO:
10430 : break;
10431 :
10432 : case MATCH_ERROR:
10433 : return MATCH_ERROR;
10434 :
10435 4 : case MATCH_YES:
10436 4 : if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
10437 : return MATCH_ERROR;
10438 : seen_symbol = true;
10439 : break;
10440 : }
10441 :
10442 4 : if (gfc_match_eos () == MATCH_YES)
10443 : break;
10444 0 : if (gfc_match_char (',') != MATCH_YES)
10445 0 : goto syntax;
10446 : }
10447 :
10448 4 : if (!seen_symbol)
10449 : {
10450 2 : gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
10451 2 : return MATCH_ERROR;
10452 : }
10453 :
10454 : return MATCH_YES;
10455 :
10456 0 : syntax:
10457 0 : gfc_error ("Syntax error in AUTOMATIC statement at %C");
10458 0 : return MATCH_ERROR;
10459 : }
10460 :
10461 :
10462 : match
10463 7 : gfc_match_static (void)
10464 : {
10465 7 : gfc_symbol *sym;
10466 7 : match m;
10467 7 : bool seen_symbol = false;
10468 :
10469 7 : if (!flag_dec_static)
10470 : {
10471 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10472 : "%<-fdec-static%>",
10473 : "STATIC");
10474 2 : return MATCH_ERROR;
10475 : }
10476 :
10477 5 : gfc_match (" ::");
10478 :
10479 5 : for (;;)
10480 : {
10481 5 : m = gfc_match_symbol (&sym, 0);
10482 5 : switch (m)
10483 : {
10484 : case MATCH_NO:
10485 : break;
10486 :
10487 : case MATCH_ERROR:
10488 : return MATCH_ERROR;
10489 :
10490 3 : case MATCH_YES:
10491 3 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10492 : &gfc_current_locus))
10493 : return MATCH_ERROR;
10494 : seen_symbol = true;
10495 : break;
10496 : }
10497 :
10498 3 : if (gfc_match_eos () == MATCH_YES)
10499 : break;
10500 0 : if (gfc_match_char (',') != MATCH_YES)
10501 0 : goto syntax;
10502 : }
10503 :
10504 3 : if (!seen_symbol)
10505 : {
10506 2 : gfc_error ("Expected entity-list in STATIC statement at %C");
10507 2 : return MATCH_ERROR;
10508 : }
10509 :
10510 : return MATCH_YES;
10511 :
10512 0 : syntax:
10513 0 : gfc_error ("Syntax error in STATIC statement at %C");
10514 0 : return MATCH_ERROR;
10515 : }
10516 :
10517 :
10518 : /* Save statements have a special syntax. */
10519 :
10520 : match
10521 272 : gfc_match_save (void)
10522 : {
10523 272 : char n[GFC_MAX_SYMBOL_LEN+1];
10524 272 : gfc_common_head *c;
10525 272 : gfc_symbol *sym;
10526 272 : match m;
10527 :
10528 272 : if (gfc_match_eos () == MATCH_YES)
10529 : {
10530 150 : if (gfc_current_ns->seen_save)
10531 : {
10532 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
10533 : "follows previous SAVE statement"))
10534 : return MATCH_ERROR;
10535 : }
10536 :
10537 149 : gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
10538 149 : return MATCH_YES;
10539 : }
10540 :
10541 122 : if (gfc_current_ns->save_all)
10542 : {
10543 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
10544 : "blanket SAVE statement"))
10545 : return MATCH_ERROR;
10546 : }
10547 :
10548 121 : gfc_match (" ::");
10549 :
10550 183 : for (;;)
10551 : {
10552 183 : m = gfc_match_symbol (&sym, 0);
10553 183 : switch (m)
10554 : {
10555 181 : case MATCH_YES:
10556 181 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10557 : &gfc_current_locus))
10558 : return MATCH_ERROR;
10559 179 : goto next_item;
10560 :
10561 : case MATCH_NO:
10562 : break;
10563 :
10564 : case MATCH_ERROR:
10565 : return MATCH_ERROR;
10566 : }
10567 :
10568 2 : m = gfc_match (" / %n /", &n);
10569 2 : if (m == MATCH_ERROR)
10570 : return MATCH_ERROR;
10571 2 : if (m == MATCH_NO)
10572 0 : goto syntax;
10573 :
10574 : /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
10575 : saved-entity-list that does not specify a common-block-name. */
10576 2 : if (gfc_current_state () == COMP_BLOCK)
10577 : {
10578 1 : gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
10579 : "in a BLOCK construct", n);
10580 1 : return MATCH_ERROR;
10581 : }
10582 :
10583 1 : c = gfc_get_common (n, 0);
10584 1 : c->saved = 1;
10585 :
10586 1 : gfc_current_ns->seen_save = 1;
10587 :
10588 180 : next_item:
10589 180 : if (gfc_match_eos () == MATCH_YES)
10590 : break;
10591 62 : if (gfc_match_char (',') != MATCH_YES)
10592 0 : goto syntax;
10593 : }
10594 :
10595 : return MATCH_YES;
10596 :
10597 0 : syntax:
10598 0 : if (gfc_current_ns->seen_save)
10599 : {
10600 0 : gfc_error ("Syntax error in SAVE statement at %C");
10601 0 : return MATCH_ERROR;
10602 : }
10603 : else
10604 : return MATCH_NO;
10605 : }
10606 :
10607 :
10608 : match
10609 93 : gfc_match_value (void)
10610 : {
10611 93 : gfc_symbol *sym;
10612 93 : match m;
10613 :
10614 : /* This is not allowed within a BLOCK construct! */
10615 93 : if (gfc_current_state () == COMP_BLOCK)
10616 : {
10617 2 : gfc_error ("VALUE is not allowed inside of BLOCK at %C");
10618 2 : return MATCH_ERROR;
10619 : }
10620 :
10621 91 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
10622 : return MATCH_ERROR;
10623 :
10624 90 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10625 : {
10626 : return MATCH_ERROR;
10627 : }
10628 :
10629 90 : if (gfc_match_eos () == MATCH_YES)
10630 0 : goto syntax;
10631 :
10632 116 : for(;;)
10633 : {
10634 116 : m = gfc_match_symbol (&sym, 0);
10635 116 : switch (m)
10636 : {
10637 116 : case MATCH_YES:
10638 116 : if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
10639 : return MATCH_ERROR;
10640 110 : goto next_item;
10641 :
10642 : case MATCH_NO:
10643 : break;
10644 :
10645 : case MATCH_ERROR:
10646 : return MATCH_ERROR;
10647 : }
10648 :
10649 110 : next_item:
10650 110 : if (gfc_match_eos () == MATCH_YES)
10651 : break;
10652 26 : if (gfc_match_char (',') != MATCH_YES)
10653 0 : goto syntax;
10654 : }
10655 :
10656 : return MATCH_YES;
10657 :
10658 0 : syntax:
10659 0 : gfc_error ("Syntax error in VALUE statement at %C");
10660 0 : return MATCH_ERROR;
10661 : }
10662 :
10663 :
10664 : match
10665 45 : gfc_match_volatile (void)
10666 : {
10667 45 : gfc_symbol *sym;
10668 45 : char *name;
10669 45 : match m;
10670 :
10671 45 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
10672 : return MATCH_ERROR;
10673 :
10674 44 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10675 : {
10676 : return MATCH_ERROR;
10677 : }
10678 :
10679 44 : if (gfc_match_eos () == MATCH_YES)
10680 1 : goto syntax;
10681 :
10682 48 : for(;;)
10683 : {
10684 : /* VOLATILE is special because it can be added to host-associated
10685 : symbols locally. Except for coarrays. */
10686 48 : m = gfc_match_symbol (&sym, 1);
10687 48 : switch (m)
10688 : {
10689 48 : case MATCH_YES:
10690 48 : name = XALLOCAVAR (char, strlen (sym->name) + 1);
10691 48 : strcpy (name, sym->name);
10692 48 : if (!check_function_name (name))
10693 : return MATCH_ERROR;
10694 : /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
10695 : for variable in a BLOCK which is defined outside of the BLOCK. */
10696 47 : if (sym->ns != gfc_current_ns && sym->attr.codimension)
10697 : {
10698 2 : gfc_error ("Specifying VOLATILE for coarray variable %qs at "
10699 : "%C, which is use-/host-associated", sym->name);
10700 2 : return MATCH_ERROR;
10701 : }
10702 45 : if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
10703 : return MATCH_ERROR;
10704 42 : goto next_item;
10705 :
10706 : case MATCH_NO:
10707 : break;
10708 :
10709 : case MATCH_ERROR:
10710 : return MATCH_ERROR;
10711 : }
10712 :
10713 42 : next_item:
10714 42 : if (gfc_match_eos () == MATCH_YES)
10715 : break;
10716 5 : if (gfc_match_char (',') != MATCH_YES)
10717 0 : goto syntax;
10718 : }
10719 :
10720 : return MATCH_YES;
10721 :
10722 1 : syntax:
10723 1 : gfc_error ("Syntax error in VOLATILE statement at %C");
10724 1 : return MATCH_ERROR;
10725 : }
10726 :
10727 :
10728 : match
10729 11 : gfc_match_asynchronous (void)
10730 : {
10731 11 : gfc_symbol *sym;
10732 11 : char *name;
10733 11 : match m;
10734 :
10735 11 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
10736 : return MATCH_ERROR;
10737 :
10738 10 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10739 : {
10740 : return MATCH_ERROR;
10741 : }
10742 :
10743 10 : if (gfc_match_eos () == MATCH_YES)
10744 0 : goto syntax;
10745 :
10746 10 : for(;;)
10747 : {
10748 : /* ASYNCHRONOUS is special because it can be added to host-associated
10749 : symbols locally. */
10750 10 : m = gfc_match_symbol (&sym, 1);
10751 10 : switch (m)
10752 : {
10753 10 : case MATCH_YES:
10754 10 : name = XALLOCAVAR (char, strlen (sym->name) + 1);
10755 10 : strcpy (name, sym->name);
10756 10 : if (!check_function_name (name))
10757 : return MATCH_ERROR;
10758 9 : if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
10759 : return MATCH_ERROR;
10760 7 : goto next_item;
10761 :
10762 : case MATCH_NO:
10763 : break;
10764 :
10765 : case MATCH_ERROR:
10766 : return MATCH_ERROR;
10767 : }
10768 :
10769 7 : next_item:
10770 7 : if (gfc_match_eos () == MATCH_YES)
10771 : break;
10772 0 : if (gfc_match_char (',') != MATCH_YES)
10773 0 : goto syntax;
10774 : }
10775 :
10776 : return MATCH_YES;
10777 :
10778 0 : syntax:
10779 0 : gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
10780 0 : return MATCH_ERROR;
10781 : }
10782 :
10783 :
10784 : /* Match a module procedure statement in a submodule. */
10785 :
10786 : match
10787 786406 : gfc_match_submod_proc (void)
10788 : {
10789 786406 : char name[GFC_MAX_SYMBOL_LEN + 1];
10790 786406 : gfc_symbol *sym, *fsym;
10791 786406 : match m;
10792 786406 : gfc_formal_arglist *formal, *head, *tail;
10793 :
10794 786406 : if (gfc_current_state () != COMP_CONTAINS
10795 16193 : || !(gfc_state_stack->previous
10796 16193 : && (gfc_state_stack->previous->state == COMP_SUBMODULE
10797 16193 : || gfc_state_stack->previous->state == COMP_MODULE)))
10798 : return MATCH_NO;
10799 :
10800 8076 : m = gfc_match (" module% procedure% %n", name);
10801 8076 : if (m != MATCH_YES)
10802 : return m;
10803 :
10804 285 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
10805 : "at %C"))
10806 : return MATCH_ERROR;
10807 :
10808 285 : if (get_proc_name (name, &sym, false))
10809 : return MATCH_ERROR;
10810 :
10811 : /* Make sure that the result field is appropriately filled. */
10812 285 : if (sym->tlink && sym->tlink->attr.function)
10813 : {
10814 135 : if (sym->tlink->result && sym->tlink->result != sym->tlink)
10815 : {
10816 85 : sym->result = sym->tlink->result;
10817 85 : if (!sym->result->attr.use_assoc)
10818 : {
10819 20 : gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
10820 : sym->result->name);
10821 20 : st->n.sym = sym->result;
10822 20 : sym->result->refs++;
10823 : }
10824 85 : sym->param_list = gfc_copy_actual_arglist (sym->tlink->param_list);
10825 85 : }
10826 : else
10827 50 : sym->result = sym;
10828 : }
10829 :
10830 : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
10831 : the symbol existed before. */
10832 285 : sym->declared_at = gfc_current_locus;
10833 :
10834 285 : if (!sym->attr.module_procedure)
10835 : return MATCH_ERROR;
10836 :
10837 : /* Signal match_end to expect "end procedure". */
10838 283 : sym->abr_modproc_decl = 1;
10839 :
10840 : /* Change from IFSRC_IFBODY coming from the interface declaration. */
10841 283 : sym->attr.if_source = IFSRC_DECL;
10842 :
10843 283 : gfc_new_block = sym;
10844 :
10845 : /* Make a new formal arglist with the symbols in the procedure
10846 : namespace. */
10847 283 : head = tail = NULL;
10848 636 : for (formal = sym->formal; formal && formal->sym; formal = formal->next)
10849 : {
10850 353 : if (formal == sym->formal)
10851 256 : head = tail = gfc_get_formal_arglist ();
10852 : else
10853 : {
10854 97 : tail->next = gfc_get_formal_arglist ();
10855 97 : tail = tail->next;
10856 : }
10857 :
10858 353 : if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
10859 0 : goto cleanup;
10860 :
10861 353 : tail->sym = fsym;
10862 353 : gfc_set_sym_referenced (fsym);
10863 : }
10864 :
10865 : /* The dummy symbols get cleaned up, when the formal_namespace of the
10866 : interface declaration is cleared. This allows us to add the
10867 : explicit interface as is done for other type of procedure. */
10868 283 : if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
10869 : &gfc_current_locus))
10870 : return MATCH_ERROR;
10871 :
10872 283 : if (gfc_match_eos () != MATCH_YES)
10873 : {
10874 : /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
10875 : undone, such that the st->n.sym->formal points to the original symbol;
10876 : if now this namespace is finalized, the formal namespace is freed,
10877 : but it might be still needed in the parent namespace. */
10878 1 : gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
10879 1 : st->n.sym = NULL;
10880 1 : gfc_free_symbol (sym->tlink);
10881 1 : sym->tlink = NULL;
10882 1 : sym->refs--;
10883 1 : gfc_syntax_error (ST_MODULE_PROC);
10884 1 : return MATCH_ERROR;
10885 : }
10886 :
10887 : return MATCH_YES;
10888 :
10889 0 : cleanup:
10890 0 : gfc_free_formal_arglist (head);
10891 0 : return MATCH_ERROR;
10892 : }
10893 :
10894 :
10895 : /* Match a module procedure statement. Note that we have to modify
10896 : symbols in the parent's namespace because the current one was there
10897 : to receive symbols that are in an interface's formal argument list. */
10898 :
10899 : match
10900 1626 : gfc_match_modproc (void)
10901 : {
10902 1626 : char name[GFC_MAX_SYMBOL_LEN + 1];
10903 1626 : gfc_symbol *sym;
10904 1626 : match m;
10905 1626 : locus old_locus;
10906 1626 : gfc_namespace *module_ns;
10907 1626 : gfc_interface *old_interface_head, *interface;
10908 :
10909 1626 : if (gfc_state_stack->previous == NULL
10910 1624 : || (gfc_state_stack->state != COMP_INTERFACE
10911 5 : && (gfc_state_stack->state != COMP_CONTAINS
10912 4 : || gfc_state_stack->previous->state != COMP_INTERFACE))
10913 1619 : || current_interface.type == INTERFACE_NAMELESS
10914 1619 : || current_interface.type == INTERFACE_ABSTRACT)
10915 : {
10916 8 : gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
10917 : "interface");
10918 8 : return MATCH_ERROR;
10919 : }
10920 :
10921 1618 : module_ns = gfc_current_ns->parent;
10922 1624 : for (; module_ns; module_ns = module_ns->parent)
10923 1624 : if (module_ns->proc_name->attr.flavor == FL_MODULE
10924 29 : || module_ns->proc_name->attr.flavor == FL_PROGRAM
10925 12 : || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
10926 12 : && !module_ns->proc_name->attr.contained))
10927 : break;
10928 :
10929 1618 : if (module_ns == NULL)
10930 : return MATCH_ERROR;
10931 :
10932 : /* Store the current state of the interface. We will need it if we
10933 : end up with a syntax error and need to recover. */
10934 1618 : old_interface_head = gfc_current_interface_head ();
10935 :
10936 : /* Check if the F2008 optional double colon appears. */
10937 1618 : gfc_gobble_whitespace ();
10938 1618 : old_locus = gfc_current_locus;
10939 1618 : if (gfc_match ("::") == MATCH_YES)
10940 : {
10941 31 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
10942 : "MODULE PROCEDURE statement at %L", &old_locus))
10943 : return MATCH_ERROR;
10944 : }
10945 : else
10946 1587 : gfc_current_locus = old_locus;
10947 :
10948 1973 : for (;;)
10949 : {
10950 1973 : bool last = false;
10951 1973 : old_locus = gfc_current_locus;
10952 :
10953 1973 : m = gfc_match_name (name);
10954 1973 : if (m == MATCH_NO)
10955 1 : goto syntax;
10956 1972 : if (m != MATCH_YES)
10957 : return MATCH_ERROR;
10958 :
10959 : /* Check for syntax error before starting to add symbols to the
10960 : current namespace. */
10961 1972 : if (gfc_match_eos () == MATCH_YES)
10962 : last = true;
10963 :
10964 360 : if (!last && gfc_match_char (',') != MATCH_YES)
10965 2 : goto syntax;
10966 :
10967 : /* Now we're sure the syntax is valid, we process this item
10968 : further. */
10969 1970 : if (gfc_get_symbol (name, module_ns, &sym))
10970 : return MATCH_ERROR;
10971 :
10972 1970 : if (sym->attr.intrinsic)
10973 : {
10974 1 : gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
10975 : "PROCEDURE", &old_locus);
10976 1 : return MATCH_ERROR;
10977 : }
10978 :
10979 1969 : if (sym->attr.proc != PROC_MODULE
10980 1969 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
10981 : return MATCH_ERROR;
10982 :
10983 1966 : if (!gfc_add_interface (sym))
10984 : return MATCH_ERROR;
10985 :
10986 1963 : sym->attr.mod_proc = 1;
10987 1963 : sym->declared_at = old_locus;
10988 :
10989 1963 : if (last)
10990 : break;
10991 : }
10992 :
10993 : return MATCH_YES;
10994 :
10995 3 : syntax:
10996 : /* Restore the previous state of the interface. */
10997 3 : interface = gfc_current_interface_head ();
10998 3 : gfc_set_current_interface_head (old_interface_head);
10999 :
11000 : /* Free the new interfaces. */
11001 10 : while (interface != old_interface_head)
11002 : {
11003 4 : gfc_interface *i = interface->next;
11004 4 : free (interface);
11005 4 : interface = i;
11006 : }
11007 :
11008 : /* And issue a syntax error. */
11009 3 : gfc_syntax_error (ST_MODULE_PROC);
11010 3 : return MATCH_ERROR;
11011 : }
11012 :
11013 :
11014 : /* Check a derived type that is being extended. */
11015 :
11016 : static gfc_symbol*
11017 1662 : check_extended_derived_type (char *name)
11018 : {
11019 1662 : gfc_symbol *extended;
11020 :
11021 1662 : if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
11022 : {
11023 0 : gfc_error ("Ambiguous symbol in TYPE definition at %C");
11024 0 : return NULL;
11025 : }
11026 :
11027 1662 : extended = gfc_find_dt_in_generic (extended);
11028 :
11029 : /* F08:C428. */
11030 1662 : if (!extended)
11031 : {
11032 2 : gfc_error ("Symbol %qs at %C has not been previously defined", name);
11033 2 : return NULL;
11034 : }
11035 :
11036 1660 : if (extended->attr.flavor != FL_DERIVED)
11037 : {
11038 0 : gfc_error ("%qs in EXTENDS expression at %C is not a "
11039 : "derived type", name);
11040 0 : return NULL;
11041 : }
11042 :
11043 1660 : if (extended->attr.is_bind_c)
11044 : {
11045 1 : gfc_error ("%qs cannot be extended at %C because it "
11046 : "is BIND(C)", extended->name);
11047 1 : return NULL;
11048 : }
11049 :
11050 1659 : if (extended->attr.sequence)
11051 : {
11052 1 : gfc_error ("%qs cannot be extended at %C because it "
11053 : "is a SEQUENCE type", extended->name);
11054 1 : return NULL;
11055 : }
11056 :
11057 : return extended;
11058 : }
11059 :
11060 :
11061 : /* Match the optional attribute specifiers for a type declaration.
11062 : Return MATCH_ERROR if an error is encountered in one of the handled
11063 : attributes (public, private, bind(c)), MATCH_NO if what's found is
11064 : not a handled attribute, and MATCH_YES otherwise. TODO: More error
11065 : checking on attribute conflicts needs to be done. */
11066 :
11067 : static match
11068 20813 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
11069 : {
11070 : /* See if the derived type is marked as private. */
11071 20813 : if (gfc_match (" , private") == MATCH_YES)
11072 : {
11073 15 : if (gfc_current_state () != COMP_MODULE)
11074 : {
11075 1 : gfc_error ("Derived type at %C can only be PRIVATE in the "
11076 : "specification part of a module");
11077 1 : return MATCH_ERROR;
11078 : }
11079 :
11080 14 : if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
11081 0 : return MATCH_ERROR;
11082 : }
11083 20798 : else if (gfc_match (" , public") == MATCH_YES)
11084 : {
11085 558 : if (gfc_current_state () != COMP_MODULE)
11086 : {
11087 0 : gfc_error ("Derived type at %C can only be PUBLIC in the "
11088 : "specification part of a module");
11089 0 : return MATCH_ERROR;
11090 : }
11091 :
11092 558 : if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
11093 0 : return MATCH_ERROR;
11094 : }
11095 20240 : else if (gfc_match (" , bind ( c )") == MATCH_YES)
11096 : {
11097 : /* If the type is defined to be bind(c) it then needs to make
11098 : sure that all fields are interoperable. This will
11099 : need to be a semantic check on the finished derived type.
11100 : See 15.2.3 (lines 9-12) of F2003 draft. */
11101 407 : if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
11102 0 : return MATCH_ERROR;
11103 :
11104 : /* TODO: attr conflicts need to be checked, probably in symbol.cc. */
11105 : }
11106 19833 : else if (gfc_match (" , abstract") == MATCH_YES)
11107 : {
11108 349 : if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
11109 : return MATCH_ERROR;
11110 :
11111 348 : if (!gfc_add_abstract (attr, &gfc_current_locus))
11112 1 : return MATCH_ERROR;
11113 : }
11114 19484 : else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
11115 : {
11116 1663 : if (!gfc_add_extension (attr, &gfc_current_locus))
11117 0 : return MATCH_ERROR;
11118 : }
11119 : else
11120 : return MATCH_NO;
11121 :
11122 : /* If we get here, something matched. */
11123 : return MATCH_YES;
11124 : }
11125 :
11126 :
11127 : /* Common function for type declaration blocks similar to derived types, such
11128 : as STRUCTURES and MAPs. Unlike derived types, a structure type
11129 : does NOT have a generic symbol matching the name given by the user.
11130 : STRUCTUREs can share names with variables and PARAMETERs so we must allow
11131 : for the creation of an independent symbol.
11132 : Other parameters are a message to prefix errors with, the name of the new
11133 : type to be created, and the flavor to add to the resulting symbol. */
11134 :
11135 : static bool
11136 717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
11137 : gfc_symbol **result)
11138 : {
11139 717 : gfc_symbol *sym;
11140 717 : locus where;
11141 :
11142 717 : gcc_assert (name[0] == (char) TOUPPER (name[0]));
11143 :
11144 717 : if (decl)
11145 717 : where = *decl;
11146 : else
11147 0 : where = gfc_current_locus;
11148 :
11149 717 : if (gfc_get_symbol (name, NULL, &sym))
11150 : return false;
11151 :
11152 717 : if (!sym)
11153 : {
11154 0 : gfc_internal_error ("Failed to create structure type '%s' at %C", name);
11155 : return false;
11156 : }
11157 :
11158 717 : if (sym->components != NULL || sym->attr.zero_comp)
11159 : {
11160 3 : gfc_error ("Type definition of %qs at %C was already defined at %L",
11161 : sym->name, &sym->declared_at);
11162 3 : return false;
11163 : }
11164 :
11165 714 : sym->declared_at = where;
11166 :
11167 714 : if (sym->attr.flavor != fl
11168 714 : && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
11169 : return false;
11170 :
11171 714 : if (!sym->hash_value)
11172 : /* Set the hash for the compound name for this type. */
11173 713 : sym->hash_value = gfc_hash_value (sym);
11174 :
11175 : /* Normally the type is expected to have been completely parsed by the time
11176 : a field declaration with this type is seen. For unions, maps, and nested
11177 : structure declarations, we need to indicate that it is okay that we
11178 : haven't seen any components yet. This will be updated after the structure
11179 : is fully parsed. */
11180 714 : sym->attr.zero_comp = 0;
11181 :
11182 : /* Structures always act like derived-types with the SEQUENCE attribute */
11183 714 : gfc_add_sequence (&sym->attr, sym->name, NULL);
11184 :
11185 714 : if (result) *result = sym;
11186 :
11187 : return true;
11188 : }
11189 :
11190 :
11191 : /* Match the opening of a MAP block. Like a struct within a union in C;
11192 : behaves identical to STRUCTURE blocks. */
11193 :
11194 : match
11195 259 : gfc_match_map (void)
11196 : {
11197 : /* Counter used to give unique internal names to map structures. */
11198 259 : static unsigned int gfc_map_id = 0;
11199 259 : char name[GFC_MAX_SYMBOL_LEN + 1];
11200 259 : gfc_symbol *sym;
11201 259 : locus old_loc;
11202 :
11203 259 : old_loc = gfc_current_locus;
11204 :
11205 259 : if (gfc_match_eos () != MATCH_YES)
11206 : {
11207 1 : gfc_error ("Junk after MAP statement at %C");
11208 1 : gfc_current_locus = old_loc;
11209 1 : return MATCH_ERROR;
11210 : }
11211 :
11212 : /* Map blocks are anonymous so we make up unique names for the symbol table
11213 : which are invalid Fortran identifiers. */
11214 258 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
11215 :
11216 258 : if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
11217 : return MATCH_ERROR;
11218 :
11219 258 : gfc_new_block = sym;
11220 :
11221 258 : return MATCH_YES;
11222 : }
11223 :
11224 :
11225 : /* Match the opening of a UNION block. */
11226 :
11227 : match
11228 133 : gfc_match_union (void)
11229 : {
11230 : /* Counter used to give unique internal names to union types. */
11231 133 : static unsigned int gfc_union_id = 0;
11232 133 : char name[GFC_MAX_SYMBOL_LEN + 1];
11233 133 : gfc_symbol *sym;
11234 133 : locus old_loc;
11235 :
11236 133 : old_loc = gfc_current_locus;
11237 :
11238 133 : if (gfc_match_eos () != MATCH_YES)
11239 : {
11240 1 : gfc_error ("Junk after UNION statement at %C");
11241 1 : gfc_current_locus = old_loc;
11242 1 : return MATCH_ERROR;
11243 : }
11244 :
11245 : /* Unions are anonymous so we make up unique names for the symbol table
11246 : which are invalid Fortran identifiers. */
11247 132 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
11248 :
11249 132 : if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
11250 : return MATCH_ERROR;
11251 :
11252 132 : gfc_new_block = sym;
11253 :
11254 132 : return MATCH_YES;
11255 : }
11256 :
11257 :
11258 : /* Match the beginning of a STRUCTURE declaration. This is similar to
11259 : matching the beginning of a derived type declaration with a few
11260 : twists. The resulting type symbol has no access control or other
11261 : interesting attributes. */
11262 :
11263 : match
11264 336 : gfc_match_structure_decl (void)
11265 : {
11266 : /* Counter used to give unique internal names to anonymous structures. */
11267 336 : static unsigned int gfc_structure_id = 0;
11268 336 : char name[GFC_MAX_SYMBOL_LEN + 1];
11269 336 : gfc_symbol *sym;
11270 336 : match m;
11271 336 : locus where;
11272 :
11273 336 : if (!flag_dec_structure)
11274 : {
11275 3 : gfc_error ("%s at %C is a DEC extension, enable with "
11276 : "%<-fdec-structure%>",
11277 : "STRUCTURE");
11278 3 : return MATCH_ERROR;
11279 : }
11280 :
11281 333 : name[0] = '\0';
11282 :
11283 333 : m = gfc_match (" /%n/", name);
11284 333 : if (m != MATCH_YES)
11285 : {
11286 : /* Non-nested structure declarations require a structure name. */
11287 24 : if (!gfc_comp_struct (gfc_current_state ()))
11288 : {
11289 4 : gfc_error ("Structure name expected in non-nested structure "
11290 : "declaration at %C");
11291 4 : return MATCH_ERROR;
11292 : }
11293 : /* This is an anonymous structure; make up a unique name for it
11294 : (upper-case letters never make it to symbol names from the source).
11295 : The important thing is initializing the type variable
11296 : and setting gfc_new_symbol, which is immediately used by
11297 : parse_structure () and variable_decl () to add components of
11298 : this type. */
11299 20 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
11300 : }
11301 :
11302 329 : where = gfc_current_locus;
11303 : /* No field list allowed after non-nested structure declaration. */
11304 329 : if (!gfc_comp_struct (gfc_current_state ())
11305 296 : && gfc_match_eos () != MATCH_YES)
11306 : {
11307 1 : gfc_error ("Junk after non-nested STRUCTURE statement at %C");
11308 1 : return MATCH_ERROR;
11309 : }
11310 :
11311 : /* Make sure the name is not the name of an intrinsic type. */
11312 328 : if (gfc_is_intrinsic_typename (name))
11313 : {
11314 1 : gfc_error ("Structure name %qs at %C cannot be the same as an"
11315 : " intrinsic type", name);
11316 1 : return MATCH_ERROR;
11317 : }
11318 :
11319 : /* Store the actual type symbol for the structure with an upper-case first
11320 : letter (an invalid Fortran identifier). */
11321 :
11322 327 : if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
11323 : return MATCH_ERROR;
11324 :
11325 324 : gfc_new_block = sym;
11326 324 : return MATCH_YES;
11327 : }
11328 :
11329 :
11330 : /* This function does some work to determine which matcher should be used to
11331 : * match a statement beginning with "TYPE". This is used to disambiguate TYPE
11332 : * as an alias for PRINT from derived type declarations, TYPE IS statements,
11333 : * and [parameterized] derived type declarations. */
11334 :
11335 : match
11336 546399 : gfc_match_type (gfc_statement *st)
11337 : {
11338 546399 : char name[GFC_MAX_SYMBOL_LEN + 1];
11339 546399 : match m;
11340 546399 : locus old_loc;
11341 :
11342 : /* Requires -fdec. */
11343 546399 : if (!flag_dec)
11344 : return MATCH_NO;
11345 :
11346 2483 : m = gfc_match ("type");
11347 2483 : if (m != MATCH_YES)
11348 : return m;
11349 : /* If we already have an error in the buffer, it is probably from failing to
11350 : * match a derived type data declaration. Let it happen. */
11351 20 : else if (gfc_error_flag_test ())
11352 : return MATCH_NO;
11353 :
11354 20 : old_loc = gfc_current_locus;
11355 20 : *st = ST_NONE;
11356 :
11357 : /* If we see an attribute list before anything else it's definitely a derived
11358 : * type declaration. */
11359 20 : if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
11360 8 : goto derived;
11361 :
11362 : /* By now "TYPE" has already been matched. If we do not see a name, this may
11363 : * be something like "TYPE *" or "TYPE <fmt>". */
11364 12 : m = gfc_match_name (name);
11365 12 : if (m != MATCH_YES)
11366 : {
11367 : /* Let print match if it can, otherwise throw an error from
11368 : * gfc_match_derived_decl. */
11369 7 : gfc_current_locus = old_loc;
11370 7 : if (gfc_match_print () == MATCH_YES)
11371 : {
11372 7 : *st = ST_WRITE;
11373 7 : return MATCH_YES;
11374 : }
11375 0 : goto derived;
11376 : }
11377 :
11378 : /* Check for EOS. */
11379 5 : if (gfc_match_eos () == MATCH_YES)
11380 : {
11381 : /* By now we have "TYPE <name> <EOS>". Check first if the name is an
11382 : * intrinsic typename - if so let gfc_match_derived_decl dump an error.
11383 : * Otherwise if gfc_match_derived_decl fails it's probably an existing
11384 : * symbol which can be printed. */
11385 3 : gfc_current_locus = old_loc;
11386 3 : m = gfc_match_derived_decl ();
11387 3 : if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
11388 : {
11389 2 : *st = ST_DERIVED_DECL;
11390 2 : return m;
11391 : }
11392 : }
11393 : else
11394 : {
11395 : /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
11396 : like <type name(parameter)>. */
11397 2 : gfc_gobble_whitespace ();
11398 2 : bool paren = gfc_peek_ascii_char () == '(';
11399 2 : if (paren)
11400 : {
11401 1 : if (strcmp ("is", name) == 0)
11402 1 : goto typeis;
11403 : else
11404 0 : goto derived;
11405 : }
11406 : }
11407 :
11408 : /* Treat TYPE... like PRINT... */
11409 2 : gfc_current_locus = old_loc;
11410 2 : *st = ST_WRITE;
11411 2 : return gfc_match_print ();
11412 :
11413 8 : derived:
11414 8 : gfc_current_locus = old_loc;
11415 8 : *st = ST_DERIVED_DECL;
11416 8 : return gfc_match_derived_decl ();
11417 :
11418 1 : typeis:
11419 1 : gfc_current_locus = old_loc;
11420 1 : *st = ST_TYPE_IS;
11421 1 : return gfc_match_type_is ();
11422 : }
11423 :
11424 :
11425 : /* Match the beginning of a derived type declaration. If a type name
11426 : was the result of a function, then it is possible to have a symbol
11427 : already to be known as a derived type yet have no components. */
11428 :
11429 : match
11430 17828 : gfc_match_derived_decl (void)
11431 : {
11432 17828 : char name[GFC_MAX_SYMBOL_LEN + 1];
11433 17828 : char parent[GFC_MAX_SYMBOL_LEN + 1];
11434 17828 : symbol_attribute attr;
11435 17828 : gfc_symbol *sym, *gensym;
11436 17828 : gfc_symbol *extended;
11437 17828 : match m;
11438 17828 : match is_type_attr_spec = MATCH_NO;
11439 17828 : bool seen_attr = false;
11440 17828 : gfc_interface *intr = NULL, *head;
11441 17828 : bool parameterized_type = false;
11442 17828 : bool seen_colons = false;
11443 :
11444 17828 : if (gfc_comp_struct (gfc_current_state ()))
11445 : return MATCH_NO;
11446 :
11447 17824 : name[0] = '\0';
11448 17824 : parent[0] = '\0';
11449 17824 : gfc_clear_attr (&attr);
11450 17824 : extended = NULL;
11451 :
11452 20813 : do
11453 : {
11454 20813 : is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
11455 20813 : if (is_type_attr_spec == MATCH_ERROR)
11456 : return MATCH_ERROR;
11457 20810 : if (is_type_attr_spec == MATCH_YES)
11458 2989 : seen_attr = true;
11459 20810 : } while (is_type_attr_spec == MATCH_YES);
11460 :
11461 : /* Deal with derived type extensions. The extension attribute has
11462 : been added to 'attr' but now the parent type must be found and
11463 : checked. */
11464 17821 : if (parent[0])
11465 1662 : extended = check_extended_derived_type (parent);
11466 :
11467 17821 : if (parent[0] && !extended)
11468 : return MATCH_ERROR;
11469 :
11470 17817 : m = gfc_match (" ::");
11471 17817 : if (m == MATCH_YES)
11472 : {
11473 : seen_colons = true;
11474 : }
11475 10893 : else if (seen_attr)
11476 : {
11477 5 : gfc_error ("Expected :: in TYPE definition at %C");
11478 5 : return MATCH_ERROR;
11479 : }
11480 :
11481 : /* In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
11482 : But, we need to simply return for TYPE(. */
11483 10888 : if (m == MATCH_NO && gfc_current_form == FORM_FREE)
11484 : {
11485 10839 : char c = gfc_peek_ascii_char ();
11486 10839 : if (c == '(')
11487 : return m;
11488 10758 : if (!gfc_is_whitespace (c))
11489 : {
11490 4 : gfc_error ("Mangled derived type definition at %C");
11491 4 : return MATCH_NO;
11492 : }
11493 : }
11494 :
11495 17727 : m = gfc_match (" %n ", name);
11496 17727 : if (m != MATCH_YES)
11497 : return m;
11498 :
11499 : /* Make sure that we don't identify TYPE IS (...) as a parameterized
11500 : derived type named 'is'.
11501 : TODO Expand the check, when 'name' = "is" by matching " (tname) "
11502 : and checking if this is a(n intrinsic) typename. This picks up
11503 : misplaced TYPE IS statements such as in select_type_1.f03. */
11504 17715 : if (gfc_peek_ascii_char () == '(')
11505 : {
11506 4343 : if (gfc_current_state () == COMP_SELECT_TYPE
11507 687 : || (!seen_colons && !strcmp (name, "is")))
11508 : return MATCH_NO;
11509 : parameterized_type = true;
11510 : }
11511 :
11512 14057 : m = gfc_match_eos ();
11513 14057 : if (m != MATCH_YES && !parameterized_type)
11514 : return m;
11515 :
11516 : /* Make sure the name is not the name of an intrinsic type. */
11517 14054 : if (gfc_is_intrinsic_typename (name))
11518 : {
11519 18 : gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
11520 : "type", name);
11521 18 : return MATCH_ERROR;
11522 : }
11523 :
11524 14036 : if (gfc_get_symbol (name, NULL, &gensym))
11525 : return MATCH_ERROR;
11526 :
11527 14036 : if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
11528 : {
11529 5 : if (gensym->ts.u.derived)
11530 0 : gfc_error ("Derived type name %qs at %C already has a basic type "
11531 : "of %s", gensym->name, gfc_typename (&gensym->ts));
11532 : else
11533 5 : gfc_error ("Derived type name %qs at %C already has a basic type",
11534 : gensym->name);
11535 : return MATCH_ERROR;
11536 : }
11537 :
11538 14031 : if (!gensym->attr.generic
11539 14031 : && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
11540 : return MATCH_ERROR;
11541 :
11542 14027 : if (!gensym->attr.function
11543 14027 : && !gfc_add_function (&gensym->attr, gensym->name, NULL))
11544 : return MATCH_ERROR;
11545 :
11546 14026 : if (gensym->attr.dummy)
11547 : {
11548 1 : gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
11549 : name, &gensym->declared_at);
11550 1 : return MATCH_ERROR;
11551 : }
11552 :
11553 14025 : sym = gfc_find_dt_in_generic (gensym);
11554 :
11555 14025 : if (sym && (sym->components != NULL || sym->attr.zero_comp))
11556 : {
11557 1 : gfc_error ("Derived type definition of %qs at %C has already been "
11558 : "defined", sym->name);
11559 1 : return MATCH_ERROR;
11560 : }
11561 :
11562 14024 : if (!sym)
11563 : {
11564 : /* Use upper case to save the actual derived-type symbol. */
11565 13934 : gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
11566 13934 : sym->name = gfc_get_string ("%s", gensym->name);
11567 13934 : head = gensym->generic;
11568 13934 : intr = gfc_get_interface ();
11569 13934 : intr->sym = sym;
11570 13934 : intr->where = gfc_current_locus;
11571 13934 : intr->sym->declared_at = gfc_current_locus;
11572 13934 : intr->next = head;
11573 13934 : gensym->generic = intr;
11574 13934 : gensym->attr.if_source = IFSRC_DECL;
11575 : }
11576 :
11577 : /* The symbol may already have the derived attribute without the
11578 : components. The ways this can happen is via a function
11579 : definition, an INTRINSIC statement or a subtype in another
11580 : derived type that is a pointer. The first part of the AND clause
11581 : is true if the symbol is not the return value of a function. */
11582 14024 : if (sym->attr.flavor != FL_DERIVED
11583 14024 : && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
11584 : return MATCH_ERROR;
11585 :
11586 14024 : if (attr.access != ACCESS_UNKNOWN
11587 14024 : && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
11588 : return MATCH_ERROR;
11589 14024 : else if (sym->attr.access == ACCESS_UNKNOWN
11590 13456 : && gensym->attr.access != ACCESS_UNKNOWN
11591 14372 : && !gfc_add_access (&sym->attr, gensym->attr.access,
11592 : sym->name, NULL))
11593 : return MATCH_ERROR;
11594 :
11595 14024 : if (sym->attr.access != ACCESS_UNKNOWN
11596 916 : && gensym->attr.access == ACCESS_UNKNOWN)
11597 568 : gensym->attr.access = sym->attr.access;
11598 :
11599 : /* See if the derived type was labeled as bind(c). */
11600 14024 : if (attr.is_bind_c != 0)
11601 404 : sym->attr.is_bind_c = attr.is_bind_c;
11602 :
11603 : /* Construct the f2k_derived namespace if it is not yet there. */
11604 14024 : if (!sym->f2k_derived)
11605 14024 : sym->f2k_derived = gfc_get_namespace (NULL, 0);
11606 :
11607 14024 : if (parameterized_type)
11608 : {
11609 : /* Ignore error or mismatches by going to the end of the statement
11610 : in order to avoid the component declarations causing problems. */
11611 685 : m = gfc_match_formal_arglist (sym, 0, 0, true);
11612 685 : if (m != MATCH_YES)
11613 4 : gfc_error_recovery ();
11614 : else
11615 681 : sym->attr.pdt_template = 1;
11616 685 : m = gfc_match_eos ();
11617 685 : if (m != MATCH_YES)
11618 : {
11619 1 : gfc_error_recovery ();
11620 1 : gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
11621 : }
11622 : }
11623 :
11624 14024 : if (extended && !sym->components)
11625 : {
11626 1658 : gfc_component *p;
11627 1658 : gfc_formal_arglist *f, *g, *h;
11628 :
11629 : /* Add the extended derived type as the first component. */
11630 1658 : gfc_add_component (sym, parent, &p);
11631 1658 : extended->refs++;
11632 1658 : gfc_set_sym_referenced (extended);
11633 :
11634 1658 : p->ts.type = BT_DERIVED;
11635 1658 : p->ts.u.derived = extended;
11636 1658 : p->initializer = gfc_default_initializer (&p->ts);
11637 :
11638 : /* Set extension level. */
11639 1658 : if (extended->attr.extension == 255)
11640 : {
11641 : /* Since the extension field is 8 bit wide, we can only have
11642 : up to 255 extension levels. */
11643 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
11644 : extended->name, &extended->declared_at);
11645 0 : return MATCH_ERROR;
11646 : }
11647 1658 : sym->attr.extension = extended->attr.extension + 1;
11648 :
11649 : /* Provide the links between the extended type and its extension. */
11650 1658 : if (!extended->f2k_derived)
11651 1 : extended->f2k_derived = gfc_get_namespace (NULL, 0);
11652 :
11653 : /* Copy the extended type-param-name-list from the extended type,
11654 : append those of the extension and add the whole lot to the
11655 : extension. */
11656 1658 : if (extended->attr.pdt_template)
11657 : {
11658 82 : g = h = NULL;
11659 82 : sym->attr.pdt_template = 1;
11660 243 : for (f = extended->formal; f; f = f->next)
11661 : {
11662 161 : if (f == extended->formal)
11663 : {
11664 82 : g = gfc_get_formal_arglist ();
11665 82 : h = g;
11666 : }
11667 : else
11668 : {
11669 79 : g->next = gfc_get_formal_arglist ();
11670 79 : g = g->next;
11671 : }
11672 161 : g->sym = f->sym;
11673 : }
11674 82 : g->next = sym->formal;
11675 82 : sym->formal = h;
11676 : }
11677 : }
11678 :
11679 14024 : if (!sym->hash_value)
11680 : /* Set the hash for the compound name for this type. */
11681 14024 : sym->hash_value = gfc_hash_value (sym);
11682 :
11683 : /* Take over the ABSTRACT attribute. */
11684 14024 : sym->attr.abstract = attr.abstract;
11685 :
11686 14024 : gfc_new_block = sym;
11687 :
11688 14024 : return MATCH_YES;
11689 : }
11690 :
11691 :
11692 : /* Cray Pointees can be declared as:
11693 : pointer (ipt, a (n,m,...,*)) */
11694 :
11695 : match
11696 240 : gfc_mod_pointee_as (gfc_array_spec *as)
11697 : {
11698 240 : as->cray_pointee = true; /* This will be useful to know later. */
11699 240 : if (as->type == AS_ASSUMED_SIZE)
11700 72 : as->cp_was_assumed = true;
11701 168 : else if (as->type == AS_ASSUMED_SHAPE)
11702 : {
11703 0 : gfc_error ("Cray Pointee at %C cannot be assumed shape array");
11704 0 : return MATCH_ERROR;
11705 : }
11706 : return MATCH_YES;
11707 : }
11708 :
11709 :
11710 : /* Match the enum definition statement, here we are trying to match
11711 : the first line of enum definition statement.
11712 : Returns MATCH_YES if match is found. */
11713 :
11714 : match
11715 158 : gfc_match_enum (void)
11716 : {
11717 158 : match m;
11718 :
11719 158 : m = gfc_match_eos ();
11720 158 : if (m != MATCH_YES)
11721 : return m;
11722 :
11723 158 : if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
11724 0 : return MATCH_ERROR;
11725 :
11726 : return MATCH_YES;
11727 : }
11728 :
11729 :
11730 : /* Returns an initializer whose value is one higher than the value of the
11731 : LAST_INITIALIZER argument. If the argument is NULL, the
11732 : initializers value will be set to zero. The initializer's kind
11733 : will be set to gfc_c_int_kind.
11734 :
11735 : If -fshort-enums is given, the appropriate kind will be selected
11736 : later after all enumerators have been parsed. A warning is issued
11737 : here if an initializer exceeds gfc_c_int_kind. */
11738 :
11739 : static gfc_expr *
11740 377 : enum_initializer (gfc_expr *last_initializer, locus where)
11741 : {
11742 377 : gfc_expr *result;
11743 377 : result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
11744 :
11745 377 : mpz_init (result->value.integer);
11746 :
11747 377 : if (last_initializer != NULL)
11748 : {
11749 266 : mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
11750 266 : result->where = last_initializer->where;
11751 :
11752 266 : if (gfc_check_integer_range (result->value.integer,
11753 : gfc_c_int_kind) != ARITH_OK)
11754 : {
11755 0 : gfc_error ("Enumerator exceeds the C integer type at %C");
11756 0 : return NULL;
11757 : }
11758 : }
11759 : else
11760 : {
11761 : /* Control comes here, if it's the very first enumerator and no
11762 : initializer has been given. It will be initialized to zero. */
11763 111 : mpz_set_si (result->value.integer, 0);
11764 : }
11765 :
11766 : return result;
11767 : }
11768 :
11769 :
11770 : /* Match a variable name with an optional initializer. When this
11771 : subroutine is called, a variable is expected to be parsed next.
11772 : Depending on what is happening at the moment, updates either the
11773 : symbol table or the current interface. */
11774 :
11775 : static match
11776 549 : enumerator_decl (void)
11777 : {
11778 549 : char name[GFC_MAX_SYMBOL_LEN + 1];
11779 549 : gfc_expr *initializer;
11780 549 : gfc_array_spec *as = NULL;
11781 549 : gfc_charlen *saved_cl_list;
11782 549 : gfc_symbol *sym;
11783 549 : locus var_locus;
11784 549 : match m;
11785 549 : bool t;
11786 549 : locus old_locus;
11787 :
11788 549 : initializer = NULL;
11789 549 : saved_cl_list = gfc_current_ns->cl_list;
11790 549 : old_locus = gfc_current_locus;
11791 :
11792 : /* When we get here, we've just matched a list of attributes and
11793 : maybe a type and a double colon. The next thing we expect to see
11794 : is the name of the symbol. */
11795 549 : m = gfc_match_name (name);
11796 549 : if (m != MATCH_YES)
11797 1 : goto cleanup;
11798 :
11799 548 : var_locus = gfc_current_locus;
11800 :
11801 : /* OK, we've successfully matched the declaration. Now put the
11802 : symbol in the current namespace. If we fail to create the symbol,
11803 : bail out. */
11804 548 : if (!build_sym (name, 1, NULL, false, &as, &var_locus))
11805 : {
11806 1 : m = MATCH_ERROR;
11807 1 : goto cleanup;
11808 : }
11809 :
11810 : /* The double colon must be present in order to have initializers.
11811 : Otherwise the statement is ambiguous with an assignment statement. */
11812 547 : if (colon_seen)
11813 : {
11814 471 : if (gfc_match_char ('=') == MATCH_YES)
11815 : {
11816 170 : m = gfc_match_init_expr (&initializer);
11817 170 : if (m == MATCH_NO)
11818 : {
11819 0 : gfc_error ("Expected an initialization expression at %C");
11820 0 : m = MATCH_ERROR;
11821 : }
11822 :
11823 170 : if (m != MATCH_YES)
11824 2 : goto cleanup;
11825 : }
11826 : }
11827 :
11828 : /* If we do not have an initializer, the initialization value of the
11829 : previous enumerator (stored in last_initializer) is incremented
11830 : by 1 and is used to initialize the current enumerator. */
11831 545 : if (initializer == NULL)
11832 377 : initializer = enum_initializer (last_initializer, old_locus);
11833 :
11834 545 : if (initializer == NULL || initializer->ts.type != BT_INTEGER)
11835 : {
11836 2 : gfc_error ("ENUMERATOR %L not initialized with integer expression",
11837 : &var_locus);
11838 2 : m = MATCH_ERROR;
11839 2 : goto cleanup;
11840 : }
11841 :
11842 : /* Store this current initializer, for the next enumerator variable
11843 : to be parsed. add_init_expr_to_sym() zeros initializer, so we
11844 : use last_initializer below. */
11845 543 : last_initializer = initializer;
11846 543 : t = add_init_expr_to_sym (name, &initializer, &var_locus,
11847 : saved_cl_list);
11848 :
11849 : /* Maintain enumerator history. */
11850 543 : gfc_find_symbol (name, NULL, 0, &sym);
11851 543 : create_enum_history (sym, last_initializer);
11852 :
11853 543 : return (t) ? MATCH_YES : MATCH_ERROR;
11854 :
11855 6 : cleanup:
11856 : /* Free stuff up and return. */
11857 6 : gfc_free_expr (initializer);
11858 :
11859 6 : return m;
11860 : }
11861 :
11862 :
11863 : /* Match the enumerator definition statement. */
11864 :
11865 : match
11866 832440 : gfc_match_enumerator_def (void)
11867 : {
11868 832440 : match m;
11869 832440 : bool t;
11870 :
11871 832440 : gfc_clear_ts (¤t_ts);
11872 :
11873 832440 : m = gfc_match (" enumerator");
11874 832440 : if (m != MATCH_YES)
11875 : return m;
11876 :
11877 269 : m = gfc_match (" :: ");
11878 269 : if (m == MATCH_ERROR)
11879 : return m;
11880 :
11881 269 : colon_seen = (m == MATCH_YES);
11882 :
11883 269 : if (gfc_current_state () != COMP_ENUM)
11884 : {
11885 4 : gfc_error ("ENUM definition statement expected before %C");
11886 4 : gfc_free_enum_history ();
11887 4 : return MATCH_ERROR;
11888 : }
11889 :
11890 265 : (¤t_ts)->type = BT_INTEGER;
11891 265 : (¤t_ts)->kind = gfc_c_int_kind;
11892 :
11893 265 : gfc_clear_attr (¤t_attr);
11894 265 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
11895 265 : if (!t)
11896 : {
11897 0 : m = MATCH_ERROR;
11898 0 : goto cleanup;
11899 : }
11900 :
11901 549 : for (;;)
11902 : {
11903 549 : m = enumerator_decl ();
11904 549 : if (m == MATCH_ERROR)
11905 : {
11906 6 : gfc_free_enum_history ();
11907 6 : goto cleanup;
11908 : }
11909 543 : if (m == MATCH_NO)
11910 : break;
11911 :
11912 542 : if (gfc_match_eos () == MATCH_YES)
11913 256 : goto cleanup;
11914 286 : if (gfc_match_char (',') != MATCH_YES)
11915 : break;
11916 : }
11917 :
11918 3 : if (gfc_current_state () == COMP_ENUM)
11919 : {
11920 3 : gfc_free_enum_history ();
11921 3 : gfc_error ("Syntax error in ENUMERATOR definition at %C");
11922 3 : m = MATCH_ERROR;
11923 : }
11924 :
11925 0 : cleanup:
11926 265 : gfc_free_array_spec (current_as);
11927 265 : current_as = NULL;
11928 265 : return m;
11929 :
11930 : }
11931 :
11932 :
11933 : /* Match binding attributes. */
11934 :
11935 : static match
11936 4817 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
11937 : {
11938 4817 : bool found_passing = false;
11939 4817 : bool seen_ptr = false;
11940 4817 : match m = MATCH_YES;
11941 :
11942 : /* Initialize to defaults. Do so even before the MATCH_NO check so that in
11943 : this case the defaults are in there. */
11944 4817 : ba->access = ACCESS_UNKNOWN;
11945 4817 : ba->pass_arg = NULL;
11946 4817 : ba->pass_arg_num = 0;
11947 4817 : ba->nopass = 0;
11948 4817 : ba->non_overridable = 0;
11949 4817 : ba->deferred = 0;
11950 4817 : ba->ppc = ppc;
11951 :
11952 : /* If we find a comma, we believe there are binding attributes. */
11953 4817 : m = gfc_match_char (',');
11954 4817 : if (m == MATCH_NO)
11955 2536 : goto done;
11956 :
11957 2855 : do
11958 : {
11959 : /* Access specifier. */
11960 :
11961 2855 : m = gfc_match (" public");
11962 2855 : if (m == MATCH_ERROR)
11963 0 : goto error;
11964 2855 : if (m == MATCH_YES)
11965 : {
11966 250 : if (ba->access != ACCESS_UNKNOWN)
11967 : {
11968 0 : gfc_error ("Duplicate access-specifier at %C");
11969 0 : goto error;
11970 : }
11971 :
11972 250 : ba->access = ACCESS_PUBLIC;
11973 250 : continue;
11974 : }
11975 :
11976 2605 : m = gfc_match (" private");
11977 2605 : if (m == MATCH_ERROR)
11978 0 : goto error;
11979 2605 : if (m == MATCH_YES)
11980 : {
11981 181 : if (ba->access != ACCESS_UNKNOWN)
11982 : {
11983 1 : gfc_error ("Duplicate access-specifier at %C");
11984 1 : goto error;
11985 : }
11986 :
11987 180 : ba->access = ACCESS_PRIVATE;
11988 180 : continue;
11989 : }
11990 :
11991 : /* If inside GENERIC, the following is not allowed. */
11992 2424 : if (!generic)
11993 : {
11994 :
11995 : /* NOPASS flag. */
11996 2423 : m = gfc_match (" nopass");
11997 2423 : if (m == MATCH_ERROR)
11998 0 : goto error;
11999 2423 : if (m == MATCH_YES)
12000 : {
12001 744 : if (found_passing)
12002 : {
12003 1 : gfc_error ("Binding attributes already specify passing,"
12004 : " illegal NOPASS at %C");
12005 1 : goto error;
12006 : }
12007 :
12008 743 : found_passing = true;
12009 743 : ba->nopass = 1;
12010 743 : continue;
12011 : }
12012 :
12013 : /* PASS possibly including argument. */
12014 1679 : m = gfc_match (" pass");
12015 1679 : if (m == MATCH_ERROR)
12016 0 : goto error;
12017 1679 : if (m == MATCH_YES)
12018 : {
12019 901 : char arg[GFC_MAX_SYMBOL_LEN + 1];
12020 :
12021 901 : if (found_passing)
12022 : {
12023 2 : gfc_error ("Binding attributes already specify passing,"
12024 : " illegal PASS at %C");
12025 2 : goto error;
12026 : }
12027 :
12028 899 : m = gfc_match (" ( %n )", arg);
12029 899 : if (m == MATCH_ERROR)
12030 0 : goto error;
12031 899 : if (m == MATCH_YES)
12032 490 : ba->pass_arg = gfc_get_string ("%s", arg);
12033 899 : gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
12034 :
12035 899 : found_passing = true;
12036 899 : ba->nopass = 0;
12037 899 : continue;
12038 899 : }
12039 :
12040 778 : if (ppc)
12041 : {
12042 : /* POINTER flag. */
12043 456 : m = gfc_match (" pointer");
12044 456 : if (m == MATCH_ERROR)
12045 0 : goto error;
12046 456 : if (m == MATCH_YES)
12047 : {
12048 456 : if (seen_ptr)
12049 : {
12050 1 : gfc_error ("Duplicate POINTER attribute at %C");
12051 1 : goto error;
12052 : }
12053 :
12054 455 : seen_ptr = true;
12055 455 : continue;
12056 : }
12057 : }
12058 : else
12059 : {
12060 : /* NON_OVERRIDABLE flag. */
12061 322 : m = gfc_match (" non_overridable");
12062 322 : if (m == MATCH_ERROR)
12063 0 : goto error;
12064 322 : if (m == MATCH_YES)
12065 : {
12066 62 : if (ba->non_overridable)
12067 : {
12068 1 : gfc_error ("Duplicate NON_OVERRIDABLE at %C");
12069 1 : goto error;
12070 : }
12071 :
12072 61 : ba->non_overridable = 1;
12073 61 : continue;
12074 : }
12075 :
12076 : /* DEFERRED flag. */
12077 260 : m = gfc_match (" deferred");
12078 260 : if (m == MATCH_ERROR)
12079 0 : goto error;
12080 260 : if (m == MATCH_YES)
12081 : {
12082 260 : if (ba->deferred)
12083 : {
12084 1 : gfc_error ("Duplicate DEFERRED at %C");
12085 1 : goto error;
12086 : }
12087 :
12088 259 : ba->deferred = 1;
12089 259 : continue;
12090 : }
12091 : }
12092 :
12093 : }
12094 :
12095 : /* Nothing matching found. */
12096 1 : if (generic)
12097 1 : gfc_error ("Expected access-specifier at %C");
12098 : else
12099 0 : gfc_error ("Expected binding attribute at %C");
12100 1 : goto error;
12101 : }
12102 2847 : while (gfc_match_char (',') == MATCH_YES);
12103 :
12104 : /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
12105 2273 : if (ba->non_overridable && ba->deferred)
12106 : {
12107 1 : gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
12108 1 : goto error;
12109 : }
12110 :
12111 : m = MATCH_YES;
12112 :
12113 4808 : done:
12114 4808 : if (ba->access == ACCESS_UNKNOWN)
12115 4379 : ba->access = ppc ? gfc_current_block()->component_access
12116 : : gfc_typebound_default_access;
12117 :
12118 4808 : if (ppc && !seen_ptr)
12119 : {
12120 2 : gfc_error ("POINTER attribute is required for procedure pointer component"
12121 : " at %C");
12122 2 : goto error;
12123 : }
12124 :
12125 : return m;
12126 :
12127 4817 : error:
12128 : return MATCH_ERROR;
12129 : }
12130 :
12131 :
12132 : /* Match a PROCEDURE specific binding inside a derived type. */
12133 :
12134 : static match
12135 3308 : match_procedure_in_type (void)
12136 : {
12137 3308 : char name[GFC_MAX_SYMBOL_LEN + 1];
12138 3308 : char target_buf[GFC_MAX_SYMBOL_LEN + 1];
12139 3308 : char* target = NULL, *ifc = NULL;
12140 3308 : gfc_typebound_proc tb;
12141 3308 : bool seen_colons;
12142 3308 : bool seen_attrs;
12143 3308 : match m;
12144 3308 : gfc_symtree* stree;
12145 3308 : gfc_namespace* ns;
12146 3308 : gfc_symbol* block;
12147 3308 : int num;
12148 :
12149 : /* Check current state. */
12150 3308 : gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
12151 3308 : block = gfc_state_stack->previous->sym;
12152 3308 : gcc_assert (block);
12153 :
12154 : /* Try to match PROCEDURE(interface). */
12155 3308 : if (gfc_match (" (") == MATCH_YES)
12156 : {
12157 261 : m = gfc_match_name (target_buf);
12158 261 : if (m == MATCH_ERROR)
12159 : return m;
12160 261 : if (m != MATCH_YES)
12161 : {
12162 1 : gfc_error ("Interface-name expected after %<(%> at %C");
12163 1 : return MATCH_ERROR;
12164 : }
12165 :
12166 260 : if (gfc_match (" )") != MATCH_YES)
12167 : {
12168 1 : gfc_error ("%<)%> expected at %C");
12169 1 : return MATCH_ERROR;
12170 : }
12171 :
12172 : ifc = target_buf;
12173 : }
12174 :
12175 : /* Construct the data structure. */
12176 3306 : memset (&tb, 0, sizeof (tb));
12177 3306 : tb.where = gfc_current_locus;
12178 :
12179 : /* Match binding attributes. */
12180 3306 : m = match_binding_attributes (&tb, false, false);
12181 3306 : if (m == MATCH_ERROR)
12182 : return m;
12183 3299 : seen_attrs = (m == MATCH_YES);
12184 :
12185 : /* Check that attribute DEFERRED is given if an interface is specified. */
12186 3299 : if (tb.deferred && !ifc)
12187 : {
12188 1 : gfc_error ("Interface must be specified for DEFERRED binding at %C");
12189 1 : return MATCH_ERROR;
12190 : }
12191 3298 : if (ifc && !tb.deferred)
12192 : {
12193 1 : gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
12194 1 : return MATCH_ERROR;
12195 : }
12196 :
12197 : /* Match the colons. */
12198 3297 : m = gfc_match (" ::");
12199 3297 : if (m == MATCH_ERROR)
12200 : return m;
12201 3297 : seen_colons = (m == MATCH_YES);
12202 3297 : if (seen_attrs && !seen_colons)
12203 : {
12204 4 : gfc_error ("Expected %<::%> after binding-attributes at %C");
12205 4 : return MATCH_ERROR;
12206 : }
12207 :
12208 : /* Match the binding names. */
12209 19 : for(num=1;;num++)
12210 : {
12211 3312 : m = gfc_match_name (name);
12212 3312 : if (m == MATCH_ERROR)
12213 : return m;
12214 3312 : if (m == MATCH_NO)
12215 : {
12216 5 : gfc_error ("Expected binding name at %C");
12217 5 : return MATCH_ERROR;
12218 : }
12219 :
12220 3307 : if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
12221 : return MATCH_ERROR;
12222 :
12223 : /* Try to match the '=> target', if it's there. */
12224 3306 : target = ifc;
12225 3306 : m = gfc_match (" =>");
12226 3306 : if (m == MATCH_ERROR)
12227 : return m;
12228 3306 : if (m == MATCH_YES)
12229 : {
12230 1274 : if (tb.deferred)
12231 : {
12232 1 : gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
12233 1 : return MATCH_ERROR;
12234 : }
12235 :
12236 1273 : if (!seen_colons)
12237 : {
12238 1 : gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
12239 : " at %C");
12240 1 : return MATCH_ERROR;
12241 : }
12242 :
12243 1272 : m = gfc_match_name (target_buf);
12244 1272 : if (m == MATCH_ERROR)
12245 : return m;
12246 1272 : if (m == MATCH_NO)
12247 : {
12248 2 : gfc_error ("Expected binding target after %<=>%> at %C");
12249 2 : return MATCH_ERROR;
12250 : }
12251 : target = target_buf;
12252 : }
12253 :
12254 : /* If no target was found, it has the same name as the binding. */
12255 2032 : if (!target)
12256 1777 : target = name;
12257 :
12258 : /* Get the namespace to insert the symbols into. */
12259 3302 : ns = block->f2k_derived;
12260 3302 : gcc_assert (ns);
12261 :
12262 : /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
12263 3302 : if (tb.deferred && !block->attr.abstract)
12264 : {
12265 1 : gfc_error ("Type %qs containing DEFERRED binding at %C "
12266 : "is not ABSTRACT", block->name);
12267 1 : return MATCH_ERROR;
12268 : }
12269 :
12270 : /* See if we already have a binding with this name in the symtree which
12271 : would be an error. If a GENERIC already targeted this binding, it may
12272 : be already there but then typebound is still NULL. */
12273 3301 : stree = gfc_find_symtree (ns->tb_sym_root, name);
12274 3301 : if (stree && stree->n.tb)
12275 : {
12276 2 : gfc_error ("There is already a procedure with binding name %qs for "
12277 : "the derived type %qs at %C", name, block->name);
12278 2 : return MATCH_ERROR;
12279 : }
12280 :
12281 : /* Insert it and set attributes. */
12282 :
12283 3180 : if (!stree)
12284 : {
12285 3180 : stree = gfc_new_symtree (&ns->tb_sym_root, name);
12286 3180 : gcc_assert (stree);
12287 : }
12288 3299 : stree->n.tb = gfc_get_typebound_proc (&tb);
12289 :
12290 3299 : if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
12291 : false))
12292 : return MATCH_ERROR;
12293 3299 : gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
12294 3299 : gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
12295 3299 : target, &stree->n.tb->u.specific->n.sym->declared_at);
12296 :
12297 3299 : if (gfc_match_eos () == MATCH_YES)
12298 : return MATCH_YES;
12299 20 : if (gfc_match_char (',') != MATCH_YES)
12300 1 : goto syntax;
12301 : }
12302 :
12303 1 : syntax:
12304 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
12305 1 : return MATCH_ERROR;
12306 : }
12307 :
12308 :
12309 : /* Match a GENERIC statement.
12310 : F2018 15.4.3.3 GENERIC statement
12311 :
12312 : A GENERIC statement specifies a generic identifier for one or more specific
12313 : procedures, in the same way as a generic interface block that does not contain
12314 : interface bodies.
12315 :
12316 : R1510 generic-stmt is:
12317 : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
12318 :
12319 : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
12320 : procedure that was specified previously in any accessible interface with the
12321 : same generic identifier.
12322 :
12323 : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
12324 :
12325 : For GENERIC statements outside of a derived type, use is made of the existing,
12326 : typebound matching functions to obtain access-spec and generic-spec. After
12327 : this the standard INTERFACE machinery is used. */
12328 :
12329 : static match
12330 100 : match_generic_stmt (void)
12331 : {
12332 100 : char name[GFC_MAX_SYMBOL_LEN + 1];
12333 : /* Allow space for OPERATOR(...). */
12334 100 : char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
12335 : /* Generics other than uops */
12336 100 : gfc_symbol* generic_spec = NULL;
12337 : /* Generic uops */
12338 100 : gfc_user_op *generic_uop = NULL;
12339 : /* For the matching calls */
12340 100 : gfc_typebound_proc tbattr;
12341 100 : gfc_namespace* ns = gfc_current_ns;
12342 100 : interface_type op_type;
12343 100 : gfc_intrinsic_op op;
12344 100 : match m;
12345 100 : gfc_symtree* st;
12346 : /* The specific-procedure-list */
12347 100 : gfc_interface *generic = NULL;
12348 : /* The head of the specific-procedure-list */
12349 100 : gfc_interface **generic_tail = NULL;
12350 :
12351 100 : memset (&tbattr, 0, sizeof (tbattr));
12352 100 : tbattr.where = gfc_current_locus;
12353 :
12354 : /* See if we get an access-specifier. */
12355 100 : m = match_binding_attributes (&tbattr, true, false);
12356 100 : tbattr.where = gfc_current_locus;
12357 100 : if (m == MATCH_ERROR)
12358 0 : goto error;
12359 :
12360 : /* Now the colons, those are required. */
12361 100 : if (gfc_match (" ::") != MATCH_YES)
12362 : {
12363 0 : gfc_error ("Expected %<::%> at %C");
12364 0 : goto error;
12365 : }
12366 :
12367 : /* Match the generic-spec name; depending on type (operator / generic) format
12368 : it for future error messages in 'generic_spec_name'. */
12369 100 : m = gfc_match_generic_spec (&op_type, name, &op);
12370 100 : if (m == MATCH_ERROR)
12371 : return MATCH_ERROR;
12372 100 : if (m == MATCH_NO)
12373 : {
12374 0 : gfc_error ("Expected generic name or operator descriptor at %C");
12375 0 : goto error;
12376 : }
12377 :
12378 100 : switch (op_type)
12379 : {
12380 63 : case INTERFACE_GENERIC:
12381 63 : case INTERFACE_DTIO:
12382 63 : snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
12383 63 : break;
12384 :
12385 22 : case INTERFACE_USER_OP:
12386 22 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
12387 22 : break;
12388 :
12389 13 : case INTERFACE_INTRINSIC_OP:
12390 13 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
12391 : gfc_op2string (op));
12392 13 : break;
12393 :
12394 2 : case INTERFACE_NAMELESS:
12395 2 : gfc_error ("Malformed GENERIC statement at %C");
12396 2 : goto error;
12397 0 : break;
12398 :
12399 0 : default:
12400 0 : gcc_unreachable ();
12401 : }
12402 :
12403 : /* Match the required =>. */
12404 98 : if (gfc_match (" =>") != MATCH_YES)
12405 : {
12406 1 : gfc_error ("Expected %<=>%> at %C");
12407 1 : goto error;
12408 : }
12409 :
12410 :
12411 97 : if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
12412 : {
12413 1 : gfc_error ("The access specification at %L not in a module",
12414 : &tbattr.where);
12415 1 : goto error;
12416 : }
12417 :
12418 : /* Try to find existing generic-spec with this name for this operator;
12419 : if there is something, check that it is another generic-spec and then
12420 : extend it rather than building a new symbol. Otherwise, create a new
12421 : one with the right attributes. */
12422 :
12423 96 : switch (op_type)
12424 : {
12425 61 : case INTERFACE_DTIO:
12426 61 : case INTERFACE_GENERIC:
12427 61 : st = gfc_find_symtree (ns->sym_root, name);
12428 61 : generic_spec = st ? st->n.sym : NULL;
12429 61 : if (generic_spec)
12430 : {
12431 25 : if (generic_spec->attr.flavor != FL_PROCEDURE
12432 11 : && generic_spec->attr.flavor != FL_UNKNOWN)
12433 : {
12434 1 : gfc_error ("The generic-spec name %qs at %C clashes with the "
12435 : "name of an entity declared at %L that is not a "
12436 : "procedure", name, &generic_spec->declared_at);
12437 1 : goto error;
12438 : }
12439 :
12440 24 : if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
12441 10 : && generic_spec->attr.flavor != FL_UNKNOWN)
12442 : {
12443 0 : gfc_error ("There's already a non-generic procedure with "
12444 : "name %qs at %C", generic_spec->name);
12445 0 : goto error;
12446 : }
12447 :
12448 24 : if (tbattr.access != ACCESS_UNKNOWN)
12449 : {
12450 2 : if (generic_spec->attr.access != tbattr.access)
12451 : {
12452 1 : gfc_error ("The access specification at %L conflicts with "
12453 : "that already given to %qs", &tbattr.where,
12454 : generic_spec->name);
12455 1 : goto error;
12456 : }
12457 : else
12458 : {
12459 1 : gfc_error ("The access specification at %L repeats that "
12460 : "already given to %qs", &tbattr.where,
12461 : generic_spec->name);
12462 1 : goto error;
12463 : }
12464 : }
12465 :
12466 22 : if (generic_spec->ts.type != BT_UNKNOWN)
12467 : {
12468 1 : gfc_error ("The generic-spec in the generic statement at %C "
12469 : "has a type from the declaration at %L",
12470 : &generic_spec->declared_at);
12471 1 : goto error;
12472 : }
12473 : }
12474 :
12475 : /* Now create the generic_spec if it doesn't already exist and provide
12476 : is with the appropriate attributes. */
12477 57 : if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
12478 : {
12479 45 : if (!generic_spec)
12480 : {
12481 36 : gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
12482 36 : gfc_set_sym_referenced (generic_spec);
12483 36 : generic_spec->attr.access = tbattr.access;
12484 : }
12485 9 : else if (generic_spec->attr.access == ACCESS_UNKNOWN)
12486 0 : generic_spec->attr.access = tbattr.access;
12487 45 : generic_spec->refs++;
12488 45 : generic_spec->attr.generic = 1;
12489 45 : generic_spec->attr.flavor = FL_PROCEDURE;
12490 :
12491 45 : generic_spec->declared_at = gfc_current_locus;
12492 : }
12493 :
12494 : /* Prepare to add the specific procedures. */
12495 57 : generic = generic_spec->generic;
12496 57 : generic_tail = &generic_spec->generic;
12497 57 : break;
12498 :
12499 22 : case INTERFACE_USER_OP:
12500 22 : st = gfc_find_symtree (ns->uop_root, name);
12501 22 : generic_uop = st ? st->n.uop : NULL;
12502 2 : if (generic_uop)
12503 : {
12504 2 : if (generic_uop->access != ACCESS_UNKNOWN
12505 2 : && tbattr.access != ACCESS_UNKNOWN)
12506 : {
12507 2 : if (generic_uop->access != tbattr.access)
12508 : {
12509 1 : gfc_error ("The user operator at %L must have the same "
12510 : "access specification as already defined user "
12511 : "operator %qs", &tbattr.where, generic_spec_name);
12512 1 : goto error;
12513 : }
12514 : else
12515 : {
12516 1 : gfc_error ("The user operator at %L repeats the access "
12517 : "specification of already defined user operator " "%qs", &tbattr.where, generic_spec_name);
12518 1 : goto error;
12519 : }
12520 : }
12521 0 : else if (generic_uop->access == ACCESS_UNKNOWN)
12522 0 : generic_uop->access = tbattr.access;
12523 : }
12524 : else
12525 : {
12526 20 : generic_uop = gfc_get_uop (name);
12527 20 : generic_uop->access = tbattr.access;
12528 : }
12529 :
12530 : /* Prepare to add the specific procedures. */
12531 20 : generic = generic_uop->op;
12532 20 : generic_tail = &generic_uop->op;
12533 20 : break;
12534 :
12535 13 : case INTERFACE_INTRINSIC_OP:
12536 13 : generic = ns->op[op];
12537 13 : generic_tail = &ns->op[op];
12538 13 : break;
12539 :
12540 0 : default:
12541 0 : gcc_unreachable ();
12542 : }
12543 :
12544 : /* Now, match all following names in the specific-procedure-list. */
12545 154 : do
12546 : {
12547 154 : m = gfc_match_name (name);
12548 154 : if (m == MATCH_ERROR)
12549 0 : goto error;
12550 154 : if (m == MATCH_NO)
12551 : {
12552 0 : gfc_error ("Expected specific procedure name at %C");
12553 0 : goto error;
12554 : }
12555 :
12556 154 : if (op_type == INTERFACE_GENERIC
12557 95 : && !strcmp (generic_spec->name, name))
12558 : {
12559 2 : gfc_error ("The name %qs of the specific procedure at %C conflicts "
12560 : "with that of the generic-spec", name);
12561 2 : goto error;
12562 : }
12563 :
12564 152 : generic = *generic_tail;
12565 242 : for (; generic; generic = generic->next)
12566 : {
12567 90 : if (!strcmp (generic->sym->name, name))
12568 : {
12569 0 : gfc_error ("%qs already defined as a specific procedure for the"
12570 : " generic %qs at %C", name, generic_spec->name);
12571 0 : goto error;
12572 : }
12573 : }
12574 :
12575 152 : gfc_find_sym_tree (name, ns, 1, &st);
12576 152 : if (!st)
12577 : {
12578 : /* This might be a procedure that has not yet been parsed. If
12579 : so gfc_fixup_sibling_symbols will replace this symbol with
12580 : that of the procedure. */
12581 75 : gfc_get_sym_tree (name, ns, &st, false);
12582 75 : st->n.sym->refs++;
12583 : }
12584 :
12585 152 : generic = gfc_get_interface();
12586 152 : generic->next = *generic_tail;
12587 152 : *generic_tail = generic;
12588 152 : generic->where = gfc_current_locus;
12589 152 : generic->sym = st->n.sym;
12590 : }
12591 152 : while (gfc_match (" ,") == MATCH_YES);
12592 :
12593 88 : if (gfc_match_eos () != MATCH_YES)
12594 : {
12595 0 : gfc_error ("Junk after GENERIC statement at %C");
12596 0 : goto error;
12597 : }
12598 :
12599 88 : gfc_commit_symbols ();
12600 88 : return MATCH_YES;
12601 :
12602 100 : error:
12603 : return MATCH_ERROR;
12604 : }
12605 :
12606 :
12607 : /* Match a GENERIC procedure binding inside a derived type. */
12608 :
12609 : static match
12610 954 : match_typebound_generic (void)
12611 : {
12612 954 : char name[GFC_MAX_SYMBOL_LEN + 1];
12613 954 : char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
12614 954 : gfc_symbol* block;
12615 954 : gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
12616 954 : gfc_typebound_proc* tb;
12617 954 : gfc_namespace* ns;
12618 954 : interface_type op_type;
12619 954 : gfc_intrinsic_op op;
12620 954 : match m;
12621 :
12622 : /* Check current state. */
12623 954 : if (gfc_current_state () == COMP_DERIVED)
12624 : {
12625 0 : gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
12626 0 : return MATCH_ERROR;
12627 : }
12628 954 : if (gfc_current_state () != COMP_DERIVED_CONTAINS)
12629 : return MATCH_NO;
12630 954 : block = gfc_state_stack->previous->sym;
12631 954 : ns = block->f2k_derived;
12632 954 : gcc_assert (block && ns);
12633 :
12634 954 : memset (&tbattr, 0, sizeof (tbattr));
12635 954 : tbattr.where = gfc_current_locus;
12636 :
12637 : /* See if we get an access-specifier. */
12638 954 : m = match_binding_attributes (&tbattr, true, false);
12639 954 : if (m == MATCH_ERROR)
12640 1 : goto error;
12641 :
12642 : /* Now the colons, those are required. */
12643 953 : if (gfc_match (" ::") != MATCH_YES)
12644 : {
12645 0 : gfc_error ("Expected %<::%> at %C");
12646 0 : goto error;
12647 : }
12648 :
12649 : /* Match the binding name; depending on type (operator / generic) format
12650 : it for future error messages into bind_name. */
12651 :
12652 953 : m = gfc_match_generic_spec (&op_type, name, &op);
12653 953 : if (m == MATCH_ERROR)
12654 : return MATCH_ERROR;
12655 953 : if (m == MATCH_NO)
12656 : {
12657 0 : gfc_error ("Expected generic name or operator descriptor at %C");
12658 0 : goto error;
12659 : }
12660 :
12661 953 : switch (op_type)
12662 : {
12663 470 : case INTERFACE_GENERIC:
12664 470 : case INTERFACE_DTIO:
12665 470 : snprintf (bind_name, sizeof (bind_name), "%s", name);
12666 470 : break;
12667 :
12668 47 : case INTERFACE_USER_OP:
12669 47 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
12670 47 : break;
12671 :
12672 435 : case INTERFACE_INTRINSIC_OP:
12673 435 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
12674 : gfc_op2string (op));
12675 435 : break;
12676 :
12677 1 : case INTERFACE_NAMELESS:
12678 1 : gfc_error ("Malformed GENERIC statement at %C");
12679 1 : goto error;
12680 0 : break;
12681 :
12682 0 : default:
12683 0 : gcc_unreachable ();
12684 : }
12685 :
12686 : /* Match the required =>. */
12687 952 : if (gfc_match (" =>") != MATCH_YES)
12688 : {
12689 0 : gfc_error ("Expected %<=>%> at %C");
12690 0 : goto error;
12691 : }
12692 :
12693 : /* Try to find existing GENERIC binding with this name / for this operator;
12694 : if there is something, check that it is another GENERIC and then extend
12695 : it rather than building a new node. Otherwise, create it and put it
12696 : at the right position. */
12697 :
12698 952 : switch (op_type)
12699 : {
12700 517 : case INTERFACE_DTIO:
12701 517 : case INTERFACE_USER_OP:
12702 517 : case INTERFACE_GENERIC:
12703 517 : {
12704 517 : const bool is_op = (op_type == INTERFACE_USER_OP);
12705 517 : gfc_symtree* st;
12706 :
12707 517 : st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
12708 517 : tb = st ? st->n.tb : NULL;
12709 : break;
12710 : }
12711 :
12712 435 : case INTERFACE_INTRINSIC_OP:
12713 435 : tb = ns->tb_op[op];
12714 435 : break;
12715 :
12716 0 : default:
12717 0 : gcc_unreachable ();
12718 : }
12719 :
12720 446 : if (tb)
12721 : {
12722 9 : if (!tb->is_generic)
12723 : {
12724 1 : gcc_assert (op_type == INTERFACE_GENERIC);
12725 1 : gfc_error ("There's already a non-generic procedure with binding name"
12726 : " %qs for the derived type %qs at %C",
12727 : bind_name, block->name);
12728 1 : goto error;
12729 : }
12730 :
12731 8 : if (tb->access != tbattr.access)
12732 : {
12733 2 : gfc_error ("Binding at %C must have the same access as already"
12734 : " defined binding %qs", bind_name);
12735 2 : goto error;
12736 : }
12737 : }
12738 : else
12739 : {
12740 943 : tb = gfc_get_typebound_proc (NULL);
12741 943 : tb->where = gfc_current_locus;
12742 943 : tb->access = tbattr.access;
12743 943 : tb->is_generic = 1;
12744 943 : tb->u.generic = NULL;
12745 :
12746 943 : switch (op_type)
12747 : {
12748 508 : case INTERFACE_DTIO:
12749 508 : case INTERFACE_GENERIC:
12750 508 : case INTERFACE_USER_OP:
12751 508 : {
12752 508 : const bool is_op = (op_type == INTERFACE_USER_OP);
12753 508 : gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
12754 : &ns->tb_sym_root, name);
12755 508 : gcc_assert (st);
12756 508 : st->n.tb = tb;
12757 :
12758 508 : break;
12759 : }
12760 :
12761 435 : case INTERFACE_INTRINSIC_OP:
12762 435 : ns->tb_op[op] = tb;
12763 435 : break;
12764 :
12765 0 : default:
12766 0 : gcc_unreachable ();
12767 : }
12768 : }
12769 :
12770 : /* Now, match all following names as specific targets. */
12771 1106 : do
12772 : {
12773 1106 : gfc_symtree* target_st;
12774 1106 : gfc_tbp_generic* target;
12775 :
12776 1106 : m = gfc_match_name (name);
12777 1106 : if (m == MATCH_ERROR)
12778 0 : goto error;
12779 1106 : if (m == MATCH_NO)
12780 : {
12781 1 : gfc_error ("Expected specific binding name at %C");
12782 1 : goto error;
12783 : }
12784 :
12785 1105 : target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
12786 :
12787 : /* See if this is a duplicate specification. */
12788 1340 : for (target = tb->u.generic; target; target = target->next)
12789 236 : if (target_st == target->specific_st)
12790 : {
12791 1 : gfc_error ("%qs already defined as specific binding for the"
12792 : " generic %qs at %C", name, bind_name);
12793 1 : goto error;
12794 : }
12795 :
12796 1104 : target = gfc_get_tbp_generic ();
12797 1104 : target->specific_st = target_st;
12798 1104 : target->specific = NULL;
12799 1104 : target->next = tb->u.generic;
12800 1104 : target->is_operator = ((op_type == INTERFACE_USER_OP)
12801 1104 : || (op_type == INTERFACE_INTRINSIC_OP));
12802 1104 : tb->u.generic = target;
12803 : }
12804 1104 : while (gfc_match (" ,") == MATCH_YES);
12805 :
12806 : /* Here should be the end. */
12807 947 : if (gfc_match_eos () != MATCH_YES)
12808 : {
12809 1 : gfc_error ("Junk after GENERIC binding at %C");
12810 1 : goto error;
12811 : }
12812 :
12813 : return MATCH_YES;
12814 :
12815 954 : error:
12816 : return MATCH_ERROR;
12817 : }
12818 :
12819 :
12820 : match
12821 1054 : gfc_match_generic ()
12822 : {
12823 1054 : if (gfc_option.allow_std & ~GFC_STD_OPT_F08
12824 1052 : && gfc_current_state () != COMP_DERIVED_CONTAINS)
12825 100 : return match_generic_stmt ();
12826 : else
12827 954 : return match_typebound_generic ();
12828 : }
12829 :
12830 :
12831 : /* Match a FINAL declaration inside a derived type. */
12832 :
12833 : match
12834 490 : gfc_match_final_decl (void)
12835 : {
12836 490 : char name[GFC_MAX_SYMBOL_LEN + 1];
12837 490 : gfc_symbol* sym;
12838 490 : match m;
12839 490 : gfc_namespace* module_ns;
12840 490 : bool first, last;
12841 490 : gfc_symbol* block;
12842 :
12843 490 : if (gfc_current_form == FORM_FREE)
12844 : {
12845 490 : char c = gfc_peek_ascii_char ();
12846 490 : if (!gfc_is_whitespace (c) && c != ':')
12847 : return MATCH_NO;
12848 : }
12849 :
12850 489 : if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
12851 : {
12852 1 : if (gfc_current_form == FORM_FIXED)
12853 : return MATCH_NO;
12854 :
12855 1 : gfc_error ("FINAL declaration at %C must be inside a derived type "
12856 : "CONTAINS section");
12857 1 : return MATCH_ERROR;
12858 : }
12859 :
12860 488 : block = gfc_state_stack->previous->sym;
12861 488 : gcc_assert (block);
12862 :
12863 488 : if (gfc_state_stack->previous->previous
12864 488 : && gfc_state_stack->previous->previous->state != COMP_MODULE
12865 6 : && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
12866 : {
12867 0 : gfc_error ("Derived type declaration with FINAL at %C must be in the"
12868 : " specification part of a MODULE");
12869 0 : return MATCH_ERROR;
12870 : }
12871 :
12872 488 : module_ns = gfc_current_ns;
12873 488 : gcc_assert (module_ns);
12874 488 : gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
12875 :
12876 : /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
12877 488 : if (gfc_match (" ::") == MATCH_ERROR)
12878 : return MATCH_ERROR;
12879 :
12880 : /* Match the sequence of procedure names. */
12881 : first = true;
12882 : last = false;
12883 592 : do
12884 : {
12885 592 : gfc_finalizer* f;
12886 :
12887 592 : if (first && gfc_match_eos () == MATCH_YES)
12888 : {
12889 2 : gfc_error ("Empty FINAL at %C");
12890 2 : return MATCH_ERROR;
12891 : }
12892 :
12893 590 : m = gfc_match_name (name);
12894 590 : if (m == MATCH_NO)
12895 : {
12896 1 : gfc_error ("Expected module procedure name at %C");
12897 1 : return MATCH_ERROR;
12898 : }
12899 589 : else if (m != MATCH_YES)
12900 : return MATCH_ERROR;
12901 :
12902 589 : if (gfc_match_eos () == MATCH_YES)
12903 : last = true;
12904 105 : if (!last && gfc_match_char (',') != MATCH_YES)
12905 : {
12906 1 : gfc_error ("Expected %<,%> at %C");
12907 1 : return MATCH_ERROR;
12908 : }
12909 :
12910 588 : if (gfc_get_symbol (name, module_ns, &sym))
12911 : {
12912 0 : gfc_error ("Unknown procedure name %qs at %C", name);
12913 0 : return MATCH_ERROR;
12914 : }
12915 :
12916 : /* Mark the symbol as module procedure. */
12917 588 : if (sym->attr.proc != PROC_MODULE
12918 588 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
12919 : return MATCH_ERROR;
12920 :
12921 : /* Check if we already have this symbol in the list, this is an error. */
12922 805 : for (f = block->f2k_derived->finalizers; f; f = f->next)
12923 218 : if (f->proc_sym == sym)
12924 : {
12925 1 : gfc_error ("%qs at %C is already defined as FINAL procedure",
12926 : name);
12927 1 : return MATCH_ERROR;
12928 : }
12929 :
12930 : /* Add this symbol to the list of finalizers. */
12931 587 : gcc_assert (block->f2k_derived);
12932 587 : sym->refs++;
12933 587 : f = XCNEW (gfc_finalizer);
12934 587 : f->proc_sym = sym;
12935 587 : f->proc_tree = NULL;
12936 587 : f->where = gfc_current_locus;
12937 587 : f->next = block->f2k_derived->finalizers;
12938 587 : block->f2k_derived->finalizers = f;
12939 :
12940 587 : first = false;
12941 : }
12942 587 : while (!last);
12943 :
12944 : return MATCH_YES;
12945 : }
12946 :
12947 :
12948 : const ext_attr_t ext_attr_list[] = {
12949 : { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
12950 : { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
12951 : { "cdecl", EXT_ATTR_CDECL, "cdecl" },
12952 : { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
12953 : { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
12954 : { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
12955 : { "deprecated", EXT_ATTR_DEPRECATED, NULL },
12956 : { "noinline", EXT_ATTR_NOINLINE, NULL },
12957 : { "noreturn", EXT_ATTR_NORETURN, NULL },
12958 : { "weak", EXT_ATTR_WEAK, NULL },
12959 : { "inline", EXT_ATTR_INLINE, NULL },
12960 : { "always_inline",EXT_ATTR_ALWAYS_INLINE,NULL },
12961 : { NULL, EXT_ATTR_LAST, NULL }
12962 : };
12963 :
12964 : /* Match a !GCC$ ATTRIBUTES statement of the form:
12965 : !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
12966 : When we come here, we have already matched the !GCC$ ATTRIBUTES string.
12967 :
12968 : TODO: We should support all GCC attributes using the same syntax for
12969 : the attribute list, i.e. the list in C
12970 : __attributes(( attribute-list ))
12971 : matches then
12972 : !GCC$ ATTRIBUTES attribute-list ::
12973 : Cf. c-parser.cc's c_parser_attributes; the data can then directly be
12974 : saved into a TREE.
12975 :
12976 : As there is absolutely no risk of confusion, we should never return
12977 : MATCH_NO. */
12978 : match
12979 2984 : gfc_match_gcc_attributes (void)
12980 : {
12981 2984 : symbol_attribute attr;
12982 2984 : char name[GFC_MAX_SYMBOL_LEN + 1];
12983 2984 : unsigned id;
12984 2984 : gfc_symbol *sym;
12985 2984 : match m;
12986 :
12987 2984 : gfc_clear_attr (&attr);
12988 2988 : for(;;)
12989 : {
12990 2986 : char ch;
12991 :
12992 2986 : if (gfc_match_name (name) != MATCH_YES)
12993 : return MATCH_ERROR;
12994 :
12995 18042 : for (id = 0; id < EXT_ATTR_LAST; id++)
12996 18042 : if (strcmp (name, ext_attr_list[id].name) == 0)
12997 : break;
12998 :
12999 2986 : if (id == EXT_ATTR_LAST)
13000 : {
13001 0 : gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
13002 0 : return MATCH_ERROR;
13003 : }
13004 :
13005 2986 : if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
13006 : return MATCH_ERROR;
13007 :
13008 2986 : gfc_gobble_whitespace ();
13009 2986 : ch = gfc_next_ascii_char ();
13010 2986 : if (ch == ':')
13011 : {
13012 : /* This is the successful exit condition for the loop. */
13013 2984 : if (gfc_next_ascii_char () == ':')
13014 : break;
13015 : }
13016 :
13017 2 : if (ch == ',')
13018 2 : continue;
13019 :
13020 0 : goto syntax;
13021 2 : }
13022 :
13023 2984 : if (gfc_match_eos () == MATCH_YES)
13024 0 : goto syntax;
13025 :
13026 2999 : for(;;)
13027 : {
13028 2999 : m = gfc_match_name (name);
13029 2999 : if (m != MATCH_YES)
13030 : return m;
13031 :
13032 2999 : if (find_special (name, &sym, true))
13033 : return MATCH_ERROR;
13034 :
13035 2999 : sym->attr.ext_attr |= attr.ext_attr;
13036 :
13037 : /* INLINE and ALWAYS_INLINE are incompatible with NOINLINE. In the
13038 : middle-end the DECL_UNINLINABLE flag set by NOINLINE always wins, so
13039 : the inline request would be silently ignored. Warn and drop it. */
13040 2999 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
13041 : {
13042 5 : if (sym->attr.ext_attr & (1 << EXT_ATTR_ALWAYS_INLINE))
13043 : {
13044 2 : gfc_warning (0, "Attribute %<ALWAYS_INLINE%> at %C is "
13045 : "incompatible with %<NOINLINE%> for %qs and will "
13046 : "be ignored", sym->name);
13047 2 : sym->attr.ext_attr &= ~(1 << EXT_ATTR_ALWAYS_INLINE);
13048 : }
13049 5 : if (sym->attr.ext_attr & (1 << EXT_ATTR_INLINE))
13050 : {
13051 2 : gfc_warning (0, "Attribute %<INLINE%> at %C is incompatible "
13052 : "with %<NOINLINE%> for %qs and will be ignored",
13053 : sym->name);
13054 2 : sym->attr.ext_attr &= ~(1 << EXT_ATTR_INLINE);
13055 : }
13056 : }
13057 :
13058 2999 : if (gfc_match_eos () == MATCH_YES)
13059 : break;
13060 :
13061 15 : if (gfc_match_char (',') != MATCH_YES)
13062 0 : goto syntax;
13063 : }
13064 :
13065 : return MATCH_YES;
13066 :
13067 0 : syntax:
13068 0 : gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
13069 0 : return MATCH_ERROR;
13070 : }
13071 :
13072 :
13073 : /* Match a !GCC$ UNROLL statement of the form:
13074 : !GCC$ UNROLL n
13075 :
13076 : The parameter n is the number of times we are supposed to unroll.
13077 :
13078 : When we come here, we have already matched the !GCC$ UNROLL string. */
13079 : match
13080 19 : gfc_match_gcc_unroll (void)
13081 : {
13082 19 : int value;
13083 :
13084 : /* FIXME: use gfc_match_small_literal_int instead, delete small_int */
13085 19 : if (gfc_match_small_int (&value) == MATCH_YES)
13086 : {
13087 19 : if (value < 0 || value > USHRT_MAX)
13088 : {
13089 2 : gfc_error ("%<GCC unroll%> directive requires a"
13090 : " non-negative integral constant"
13091 : " less than or equal to %u at %C",
13092 : USHRT_MAX
13093 : );
13094 2 : return MATCH_ERROR;
13095 : }
13096 17 : if (gfc_match_eos () == MATCH_YES)
13097 : {
13098 17 : directive_unroll = value == 0 ? 1 : value;
13099 17 : return MATCH_YES;
13100 : }
13101 : }
13102 :
13103 0 : gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
13104 0 : return MATCH_ERROR;
13105 : }
13106 :
13107 : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
13108 :
13109 : The parameter b is name of a middle-end built-in.
13110 : FLAGS is optional and must be one of:
13111 : - (inbranch)
13112 : - (notinbranch)
13113 :
13114 : IF('target') is optional and TARGET is a name of a multilib ABI.
13115 :
13116 : When we come here, we have already matched the !GCC$ builtin string. */
13117 :
13118 : match
13119 3527961 : gfc_match_gcc_builtin (void)
13120 : {
13121 3527961 : char builtin[GFC_MAX_SYMBOL_LEN + 1];
13122 3527961 : char target[GFC_MAX_SYMBOL_LEN + 1];
13123 :
13124 3527961 : if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
13125 : return MATCH_ERROR;
13126 :
13127 3527961 : gfc_simd_clause clause = SIMD_NONE;
13128 3527961 : if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
13129 : clause = SIMD_NOTINBRANCH;
13130 21 : else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
13131 15 : clause = SIMD_INBRANCH;
13132 :
13133 3527961 : if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
13134 : {
13135 3527931 : if (strcmp (target, "fastmath") == 0)
13136 : {
13137 0 : if (!fast_math_flags_set_p (&global_options))
13138 : return MATCH_YES;
13139 : }
13140 : else
13141 : {
13142 3527931 : const char *abi = targetm.get_multilib_abi_name ();
13143 3527931 : if (abi == NULL || strcmp (abi, target) != 0)
13144 : return MATCH_YES;
13145 : }
13146 : }
13147 :
13148 1741910 : if (gfc_vectorized_builtins == NULL)
13149 32263 : gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
13150 :
13151 1741910 : char *r = XNEWVEC (char, strlen (builtin) + 32);
13152 1741910 : sprintf (r, "__builtin_%s", builtin);
13153 :
13154 1741910 : bool existed;
13155 1741910 : int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
13156 1741910 : value |= clause;
13157 1741910 : if (existed)
13158 23 : free (r);
13159 :
13160 : return MATCH_YES;
13161 : }
13162 :
13163 : /* Match an !GCC$ IVDEP statement.
13164 : When we come here, we have already matched the !GCC$ IVDEP string. */
13165 :
13166 : match
13167 3 : gfc_match_gcc_ivdep (void)
13168 : {
13169 3 : if (gfc_match_eos () == MATCH_YES)
13170 : {
13171 3 : directive_ivdep = true;
13172 3 : return MATCH_YES;
13173 : }
13174 :
13175 0 : gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
13176 0 : return MATCH_ERROR;
13177 : }
13178 :
13179 : /* Match an !GCC$ VECTOR statement.
13180 : When we come here, we have already matched the !GCC$ VECTOR string. */
13181 :
13182 : match
13183 3 : gfc_match_gcc_vector (void)
13184 : {
13185 3 : if (gfc_match_eos () == MATCH_YES)
13186 : {
13187 3 : directive_vector = true;
13188 3 : directive_novector = false;
13189 3 : return MATCH_YES;
13190 : }
13191 :
13192 0 : gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
13193 0 : return MATCH_ERROR;
13194 : }
13195 :
13196 : /* Match an !GCC$ NOVECTOR statement.
13197 : When we come here, we have already matched the !GCC$ NOVECTOR string. */
13198 :
13199 : match
13200 3 : gfc_match_gcc_novector (void)
13201 : {
13202 3 : if (gfc_match_eos () == MATCH_YES)
13203 : {
13204 3 : directive_novector = true;
13205 3 : directive_vector = false;
13206 3 : return MATCH_YES;
13207 : }
13208 :
13209 0 : gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
13210 0 : return MATCH_ERROR;
13211 : }
|