Branch data Line data Source code
1 : : /* Declaration statement matcher
2 : : Copyright (C) 2002-2025 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 : :
33 : : /* Macros to access allocate memory for gfc_data_variable,
34 : : gfc_data_value and gfc_data. */
35 : : #define gfc_get_data_variable() XCNEW (gfc_data_variable)
36 : : #define gfc_get_data_value() XCNEW (gfc_data_value)
37 : : #define gfc_get_data() XCNEW (gfc_data)
38 : :
39 : :
40 : : static bool set_binding_label (const char **, const char *, int);
41 : :
42 : :
43 : : /* This flag is set if an old-style length selector is matched
44 : : during a type-declaration statement. */
45 : :
46 : : static int old_char_selector;
47 : :
48 : : /* When variables acquire types and attributes from a declaration
49 : : statement, they get them from the following static variables. The
50 : : first part of a declaration sets these variables and the second
51 : : part copies these into symbol structures. */
52 : :
53 : : static gfc_typespec current_ts;
54 : :
55 : : static symbol_attribute current_attr;
56 : : static gfc_array_spec *current_as;
57 : : static int colon_seen;
58 : : static int attr_seen;
59 : :
60 : : /* The current binding label (if any). */
61 : : static const char* curr_binding_label;
62 : : /* Need to know how many identifiers are on the current data declaration
63 : : line in case we're given the BIND(C) attribute with a NAME= specifier. */
64 : : static int num_idents_on_line;
65 : : /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
66 : : can supply a name if the curr_binding_label is nil and NAME= was not. */
67 : : static int has_name_equals = 0;
68 : :
69 : : /* Initializer of the previous enumerator. */
70 : :
71 : : static gfc_expr *last_initializer;
72 : :
73 : : /* History of all the enumerators is maintained, so that
74 : : kind values of all the enumerators could be updated depending
75 : : upon the maximum initialized value. */
76 : :
77 : : typedef struct enumerator_history
78 : : {
79 : : gfc_symbol *sym;
80 : : gfc_expr *initializer;
81 : : struct enumerator_history *next;
82 : : }
83 : : enumerator_history;
84 : :
85 : : /* Header of enum history chain. */
86 : :
87 : : static enumerator_history *enum_history = NULL;
88 : :
89 : : /* Pointer of enum history node containing largest initializer. */
90 : :
91 : : static enumerator_history *max_enum = NULL;
92 : :
93 : : /* gfc_new_block points to the symbol of a newly matched block. */
94 : :
95 : : gfc_symbol *gfc_new_block;
96 : :
97 : : bool gfc_matching_function;
98 : :
99 : : /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
100 : : int directive_unroll = -1;
101 : :
102 : : /* Set upon parsing supported !GCC$ pragmas for use in the next loop. */
103 : : bool directive_ivdep = false;
104 : : bool directive_vector = false;
105 : : bool directive_novector = false;
106 : :
107 : : /* Map of middle-end built-ins that should be vectorized. */
108 : : hash_map<nofree_string_hash, int> *gfc_vectorized_builtins;
109 : :
110 : : /* If a kind expression of a component of a parameterized derived type is
111 : : parameterized, temporarily store the expression here. */
112 : : static gfc_expr *saved_kind_expr = NULL;
113 : :
114 : : /* Used to store the parameter list arising in a PDT declaration and
115 : : in the typespec of a PDT variable or component. */
116 : : static gfc_actual_arglist *decl_type_param_list;
117 : : static gfc_actual_arglist *type_param_spec_list;
118 : :
119 : : /********************* DATA statement subroutines *********************/
120 : :
121 : : static bool in_match_data = false;
122 : :
123 : : bool
124 : 8175 : gfc_in_match_data (void)
125 : : {
126 : 8175 : return in_match_data;
127 : : }
128 : :
129 : : static void
130 : 4802 : set_in_match_data (bool set_value)
131 : : {
132 : 4802 : in_match_data = set_value;
133 : 2401 : }
134 : :
135 : : /* Free a gfc_data_variable structure and everything beneath it. */
136 : :
137 : : static void
138 : 5624 : free_variable (gfc_data_variable *p)
139 : : {
140 : 5624 : gfc_data_variable *q;
141 : :
142 : 8693 : for (; p; p = q)
143 : : {
144 : 3069 : q = p->next;
145 : 3069 : gfc_free_expr (p->expr);
146 : 3069 : gfc_free_iterator (&p->iter, 0);
147 : 3069 : free_variable (p->list);
148 : 3069 : free (p);
149 : : }
150 : 5624 : }
151 : :
152 : :
153 : : /* Free a gfc_data_value structure and everything beneath it. */
154 : :
155 : : static void
156 : 2555 : free_value (gfc_data_value *p)
157 : : {
158 : 2555 : gfc_data_value *q;
159 : :
160 : 10846 : for (; p; p = q)
161 : : {
162 : 8291 : q = p->next;
163 : 8291 : mpz_clear (p->repeat);
164 : 8291 : gfc_free_expr (p->expr);
165 : 8291 : free (p);
166 : : }
167 : 2555 : }
168 : :
169 : :
170 : : /* Free a list of gfc_data structures. */
171 : :
172 : : void
173 : 507187 : gfc_free_data (gfc_data *p)
174 : : {
175 : 507187 : gfc_data *q;
176 : :
177 : 509742 : for (; p; p = q)
178 : : {
179 : 2555 : q = p->next;
180 : 2555 : free_variable (p->var);
181 : 2555 : free_value (p->value);
182 : 2555 : free (p);
183 : : }
184 : 507187 : }
185 : :
186 : :
187 : : /* Free all data in a namespace. */
188 : :
189 : : static void
190 : 38 : gfc_free_data_all (gfc_namespace *ns)
191 : : {
192 : 38 : gfc_data *d;
193 : :
194 : 44 : for (;ns->data;)
195 : : {
196 : 6 : d = ns->data->next;
197 : 6 : free (ns->data);
198 : 6 : ns->data = d;
199 : : }
200 : 38 : }
201 : :
202 : : /* Reject data parsed since the last restore point was marked. */
203 : :
204 : : void
205 : 8790627 : gfc_reject_data (gfc_namespace *ns)
206 : : {
207 : 8790627 : gfc_data *d;
208 : :
209 : 8790629 : while (ns->data && ns->data != ns->old_data)
210 : : {
211 : 2 : d = ns->data->next;
212 : 2 : free (ns->data);
213 : 2 : ns->data = d;
214 : : }
215 : 8790627 : }
216 : :
217 : : static match var_element (gfc_data_variable *);
218 : :
219 : : /* Match a list of variables terminated by an iterator and a right
220 : : parenthesis. */
221 : :
222 : : static match
223 : 153 : var_list (gfc_data_variable *parent)
224 : : {
225 : 153 : gfc_data_variable *tail, var;
226 : 153 : match m;
227 : :
228 : 153 : m = var_element (&var);
229 : 153 : if (m == MATCH_ERROR)
230 : : return MATCH_ERROR;
231 : 153 : if (m == MATCH_NO)
232 : 0 : goto syntax;
233 : :
234 : 153 : tail = gfc_get_data_variable ();
235 : 153 : *tail = var;
236 : :
237 : 153 : parent->list = tail;
238 : :
239 : 155 : for (;;)
240 : : {
241 : 154 : if (gfc_match_char (',') != MATCH_YES)
242 : 0 : goto syntax;
243 : :
244 : 154 : m = gfc_match_iterator (&parent->iter, 1);
245 : 154 : if (m == MATCH_YES)
246 : : break;
247 : 1 : if (m == MATCH_ERROR)
248 : : return MATCH_ERROR;
249 : :
250 : 1 : m = var_element (&var);
251 : 1 : if (m == MATCH_ERROR)
252 : : return MATCH_ERROR;
253 : 1 : if (m == MATCH_NO)
254 : 0 : goto syntax;
255 : :
256 : 1 : tail->next = gfc_get_data_variable ();
257 : 1 : tail = tail->next;
258 : :
259 : 1 : *tail = var;
260 : : }
261 : :
262 : 153 : if (gfc_match_char (')') != MATCH_YES)
263 : 0 : goto syntax;
264 : : return MATCH_YES;
265 : :
266 : 0 : syntax:
267 : 0 : gfc_syntax_error (ST_DATA);
268 : 0 : return MATCH_ERROR;
269 : : }
270 : :
271 : :
272 : : /* Match a single element in a data variable list, which can be a
273 : : variable-iterator list. */
274 : :
275 : : static match
276 : 3027 : var_element (gfc_data_variable *new_var)
277 : : {
278 : 3027 : match m;
279 : 3027 : gfc_symbol *sym;
280 : :
281 : 3027 : memset (new_var, 0, sizeof (gfc_data_variable));
282 : :
283 : 3027 : if (gfc_match_char ('(') == MATCH_YES)
284 : 153 : return var_list (new_var);
285 : :
286 : 2874 : m = gfc_match_variable (&new_var->expr, 0);
287 : 2874 : if (m != MATCH_YES)
288 : : return m;
289 : :
290 : 2870 : if (new_var->expr->expr_type == EXPR_CONSTANT
291 : 2 : && new_var->expr->symtree == NULL)
292 : : {
293 : 2 : gfc_error ("Inquiry parameter cannot appear in a "
294 : : "data-stmt-object-list at %C");
295 : 2 : return MATCH_ERROR;
296 : : }
297 : :
298 : 2868 : sym = new_var->expr->symtree->n.sym;
299 : :
300 : : /* Symbol should already have an associated type. */
301 : 2868 : if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
302 : : return MATCH_ERROR;
303 : :
304 : 2867 : if (!sym->attr.function && gfc_current_ns->parent
305 : 148 : && gfc_current_ns->parent == sym->ns)
306 : : {
307 : 1 : gfc_error ("Host associated variable %qs may not be in the DATA "
308 : : "statement at %C", sym->name);
309 : 1 : return MATCH_ERROR;
310 : : }
311 : :
312 : 2866 : if (gfc_current_state () != COMP_BLOCK_DATA
313 : 2729 : && sym->attr.in_common
314 : 2895 : && !gfc_notify_std (GFC_STD_GNU, "initialization of "
315 : : "common block variable %qs in DATA statement at %C",
316 : : sym->name))
317 : : return MATCH_ERROR;
318 : :
319 : 2864 : if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
320 : : return MATCH_ERROR;
321 : :
322 : : return MATCH_YES;
323 : : }
324 : :
325 : :
326 : : /* Match the top-level list of data variables. */
327 : :
328 : : static match
329 : 2498 : top_var_list (gfc_data *d)
330 : : {
331 : 2498 : gfc_data_variable var, *tail, *new_var;
332 : 2498 : match m;
333 : :
334 : 2498 : tail = NULL;
335 : :
336 : 2873 : for (;;)
337 : : {
338 : 2873 : m = var_element (&var);
339 : 2873 : if (m == MATCH_NO)
340 : 0 : goto syntax;
341 : 2873 : if (m == MATCH_ERROR)
342 : : return MATCH_ERROR;
343 : :
344 : 2858 : new_var = gfc_get_data_variable ();
345 : 2858 : *new_var = var;
346 : 2858 : if (new_var->expr)
347 : 2733 : new_var->expr->where = gfc_current_locus;
348 : :
349 : 2858 : if (tail == NULL)
350 : 2483 : d->var = new_var;
351 : : else
352 : 375 : tail->next = new_var;
353 : :
354 : 2858 : tail = new_var;
355 : :
356 : 2858 : if (gfc_match_char ('/') == MATCH_YES)
357 : : break;
358 : 378 : if (gfc_match_char (',') != MATCH_YES)
359 : 3 : goto syntax;
360 : : }
361 : :
362 : : return MATCH_YES;
363 : :
364 : 3 : syntax:
365 : 3 : gfc_syntax_error (ST_DATA);
366 : 3 : gfc_free_data_all (gfc_current_ns);
367 : 3 : return MATCH_ERROR;
368 : : }
369 : :
370 : :
371 : : static match
372 : 8692 : match_data_constant (gfc_expr **result)
373 : : {
374 : 8692 : char name[GFC_MAX_SYMBOL_LEN + 1];
375 : 8692 : gfc_symbol *sym, *dt_sym = NULL;
376 : 8692 : gfc_expr *expr;
377 : 8692 : match m;
378 : 8692 : locus old_loc;
379 : 8692 : gfc_symtree *symtree;
380 : :
381 : 8692 : m = gfc_match_literal_constant (&expr, 1);
382 : 8692 : if (m == MATCH_YES)
383 : : {
384 : 8347 : *result = expr;
385 : 8347 : return MATCH_YES;
386 : : }
387 : :
388 : 345 : if (m == MATCH_ERROR)
389 : : return MATCH_ERROR;
390 : :
391 : 337 : m = gfc_match_null (result);
392 : 337 : if (m != MATCH_NO)
393 : : return m;
394 : :
395 : 329 : old_loc = gfc_current_locus;
396 : :
397 : : /* Should this be a structure component, try to match it
398 : : before matching a name. */
399 : 329 : m = gfc_match_rvalue (result);
400 : 329 : if (m == MATCH_ERROR)
401 : : return m;
402 : :
403 : 329 : if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
404 : : {
405 : 4 : if (!gfc_simplify_expr (*result, 0))
406 : 0 : m = MATCH_ERROR;
407 : 4 : return m;
408 : : }
409 : 319 : else if (m == MATCH_YES)
410 : : {
411 : : /* If a parameter inquiry ends up here, symtree is NULL but **result
412 : : contains the right constant expression. Check here. */
413 : 319 : if ((*result)->symtree == NULL
414 : 37 : && (*result)->expr_type == EXPR_CONSTANT
415 : 37 : && ((*result)->ts.type == BT_INTEGER
416 : 1 : || (*result)->ts.type == BT_REAL))
417 : : return m;
418 : :
419 : : /* F2018:R845 data-stmt-constant is initial-data-target.
420 : : A data-stmt-constant shall be ... initial-data-target if and
421 : : only if the corresponding data-stmt-object has the POINTER
422 : : attribute. ... If data-stmt-constant is initial-data-target
423 : : the corresponding data statement object shall be
424 : : data-pointer-initialization compatible (7.5.4.6) with the initial
425 : : data target; the data statement object is initially associated
426 : : with the target. */
427 : 283 : if ((*result)->symtree
428 : 282 : && (*result)->symtree->n.sym->attr.save
429 : 218 : && (*result)->symtree->n.sym->attr.target)
430 : : return m;
431 : 250 : gfc_free_expr (*result);
432 : : }
433 : :
434 : 256 : gfc_current_locus = old_loc;
435 : :
436 : 256 : m = gfc_match_name (name);
437 : 256 : if (m != MATCH_YES)
438 : : return m;
439 : :
440 : 250 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
441 : : return MATCH_ERROR;
442 : :
443 : 250 : sym = symtree->n.sym;
444 : :
445 : 250 : if (sym && sym->attr.generic)
446 : 60 : dt_sym = gfc_find_dt_in_generic (sym);
447 : :
448 : 60 : if (sym == NULL
449 : 250 : || (sym->attr.flavor != FL_PARAMETER
450 : 65 : && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
451 : : {
452 : 5 : gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
453 : : name);
454 : 5 : *result = NULL;
455 : 5 : return MATCH_ERROR;
456 : : }
457 : 245 : else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
458 : 60 : return gfc_match_structure_constructor (dt_sym, symtree, result);
459 : :
460 : : /* Check to see if the value is an initialization array expression. */
461 : 185 : if (sym->value->expr_type == EXPR_ARRAY)
462 : : {
463 : 67 : gfc_current_locus = old_loc;
464 : :
465 : 67 : m = gfc_match_init_expr (result);
466 : 67 : if (m == MATCH_ERROR)
467 : : return m;
468 : :
469 : 66 : if (m == MATCH_YES)
470 : : {
471 : 66 : if (!gfc_simplify_expr (*result, 0))
472 : 0 : m = MATCH_ERROR;
473 : :
474 : 66 : if ((*result)->expr_type == EXPR_CONSTANT)
475 : : return m;
476 : : else
477 : : {
478 : 2 : gfc_error ("Invalid initializer %s in Data statement at %C", name);
479 : 2 : return MATCH_ERROR;
480 : : }
481 : : }
482 : : }
483 : :
484 : 118 : *result = gfc_copy_expr (sym->value);
485 : 118 : return MATCH_YES;
486 : : }
487 : :
488 : :
489 : : /* Match a list of values in a DATA statement. The leading '/' has
490 : : already been seen at this point. */
491 : :
492 : : static match
493 : 2541 : top_val_list (gfc_data *data)
494 : : {
495 : 2541 : gfc_data_value *new_val, *tail;
496 : 2541 : gfc_expr *expr;
497 : 2541 : match m;
498 : :
499 : 2541 : tail = NULL;
500 : :
501 : 8328 : for (;;)
502 : : {
503 : 8328 : m = match_data_constant (&expr);
504 : 8328 : if (m == MATCH_NO)
505 : 3 : goto syntax;
506 : 8325 : if (m == MATCH_ERROR)
507 : : return MATCH_ERROR;
508 : :
509 : 8303 : new_val = gfc_get_data_value ();
510 : 8303 : mpz_init (new_val->repeat);
511 : :
512 : 8303 : if (tail == NULL)
513 : 2516 : data->value = new_val;
514 : : else
515 : 5787 : tail->next = new_val;
516 : :
517 : 8303 : tail = new_val;
518 : :
519 : 8303 : if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
520 : : {
521 : 8098 : tail->expr = expr;
522 : 8098 : mpz_set_ui (tail->repeat, 1);
523 : : }
524 : : else
525 : : {
526 : 205 : mpz_set (tail->repeat, expr->value.integer);
527 : 205 : gfc_free_expr (expr);
528 : :
529 : 205 : m = match_data_constant (&tail->expr);
530 : 205 : if (m == MATCH_NO)
531 : 0 : goto syntax;
532 : 205 : if (m == MATCH_ERROR)
533 : : return MATCH_ERROR;
534 : : }
535 : :
536 : 8299 : if (gfc_match_char ('/') == MATCH_YES)
537 : : break;
538 : 5788 : if (gfc_match_char (',') == MATCH_NO)
539 : 1 : goto syntax;
540 : : }
541 : :
542 : : return MATCH_YES;
543 : :
544 : 4 : syntax:
545 : 4 : gfc_syntax_error (ST_DATA);
546 : 4 : gfc_free_data_all (gfc_current_ns);
547 : 4 : return MATCH_ERROR;
548 : : }
549 : :
550 : :
551 : : /* Matches an old style initialization. */
552 : :
553 : : static match
554 : 70 : match_old_style_init (const char *name)
555 : : {
556 : 70 : match m;
557 : 70 : gfc_symtree *st;
558 : 70 : gfc_symbol *sym;
559 : 70 : gfc_data *newdata, *nd;
560 : :
561 : : /* Set up data structure to hold initializers. */
562 : 70 : gfc_find_sym_tree (name, NULL, 0, &st);
563 : 70 : sym = st->n.sym;
564 : :
565 : 70 : newdata = gfc_get_data ();
566 : 70 : newdata->var = gfc_get_data_variable ();
567 : 70 : newdata->var->expr = gfc_get_variable_expr (st);
568 : 70 : newdata->var->expr->where = sym->declared_at;
569 : 70 : newdata->where = gfc_current_locus;
570 : :
571 : : /* Match initial value list. This also eats the terminal '/'. */
572 : 70 : m = top_val_list (newdata);
573 : 70 : if (m != MATCH_YES)
574 : : {
575 : 1 : free (newdata);
576 : 1 : return m;
577 : : }
578 : :
579 : : /* Check that a BOZ did not creep into an old-style initialization. */
580 : 137 : for (nd = newdata; nd; nd = nd->next)
581 : : {
582 : 69 : if (nd->value->expr->ts.type == BT_BOZ
583 : 69 : && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
584 : : "initialization"), &nd->value->expr->where))
585 : : return MATCH_ERROR;
586 : :
587 : 68 : if (nd->var->expr->ts.type != BT_INTEGER
588 : 27 : && nd->var->expr->ts.type != BT_REAL
589 : 21 : && nd->value->expr->ts.type == BT_BOZ)
590 : : {
591 : 0 : gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
592 : : "a %qs variable in an old-style initialization"),
593 : 0 : &nd->value->expr->where,
594 : : gfc_typename (&nd->value->expr->ts));
595 : 0 : return MATCH_ERROR;
596 : : }
597 : : }
598 : :
599 : 68 : if (gfc_pure (NULL))
600 : : {
601 : 1 : gfc_error ("Initialization at %C is not allowed in a PURE procedure");
602 : 1 : free (newdata);
603 : 1 : return MATCH_ERROR;
604 : : }
605 : 67 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
606 : :
607 : : /* Mark the variable as having appeared in a data statement. */
608 : 67 : if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
609 : : {
610 : 2 : free (newdata);
611 : 2 : return MATCH_ERROR;
612 : : }
613 : :
614 : : /* Chain in namespace list of DATA initializers. */
615 : 65 : newdata->next = gfc_current_ns->data;
616 : 65 : gfc_current_ns->data = newdata;
617 : :
618 : 65 : return m;
619 : : }
620 : :
621 : :
622 : : /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
623 : : we are matching a DATA statement and are therefore issuing an error
624 : : if we encounter something unexpected, if not, we're trying to match
625 : : an old-style initialization expression of the form INTEGER I /2/. */
626 : :
627 : : match
628 : 2403 : gfc_match_data (void)
629 : : {
630 : 2403 : gfc_data *new_data;
631 : 2403 : gfc_expr *e;
632 : 2403 : gfc_ref *ref;
633 : 2403 : match m;
634 : 2403 : char c;
635 : :
636 : : /* DATA has been matched. In free form source code, the next character
637 : : needs to be whitespace or '(' from an implied do-loop. Check that
638 : : here. */
639 : 2403 : c = gfc_peek_ascii_char ();
640 : 2403 : if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
641 : : return MATCH_NO;
642 : :
643 : : /* Before parsing the rest of a DATA statement, check F2008:c1206. */
644 : 2402 : if ((gfc_current_state () == COMP_FUNCTION
645 : 2402 : || gfc_current_state () == COMP_SUBROUTINE)
646 : 1153 : && gfc_state_stack->previous->state == COMP_INTERFACE)
647 : : {
648 : 1 : gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
649 : 1 : return MATCH_ERROR;
650 : : }
651 : :
652 : 2401 : set_in_match_data (true);
653 : :
654 : 2595 : for (;;)
655 : : {
656 : 2498 : new_data = gfc_get_data ();
657 : 2498 : new_data->where = gfc_current_locus;
658 : :
659 : 2498 : m = top_var_list (new_data);
660 : 2498 : if (m != MATCH_YES)
661 : 18 : goto cleanup;
662 : :
663 : 2480 : if (new_data->var->iter.var
664 : 116 : && new_data->var->iter.var->ts.type == BT_INTEGER
665 : 73 : && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
666 : 67 : && new_data->var->list
667 : 67 : && new_data->var->list->expr
668 : 54 : && new_data->var->list->expr->ts.type == BT_CHARACTER
669 : 3 : && new_data->var->list->expr->ref
670 : 3 : && new_data->var->list->expr->ref->type == REF_SUBSTRING)
671 : : {
672 : 1 : gfc_error ("Invalid substring in data-implied-do at %L in DATA "
673 : : "statement", &new_data->var->list->expr->where);
674 : 1 : goto cleanup;
675 : : }
676 : :
677 : : /* Check for an entity with an allocatable component, which is not
678 : : allowed. */
679 : 2479 : e = new_data->var->expr;
680 : 2479 : if (e)
681 : : {
682 : 2364 : bool invalid;
683 : :
684 : 2364 : invalid = false;
685 : 3586 : for (ref = e->ref; ref; ref = ref->next)
686 : 1222 : if ((ref->type == REF_COMPONENT
687 : 140 : && ref->u.c.component->attr.allocatable)
688 : 1220 : || (ref->type == REF_ARRAY
689 : 1032 : && e->symtree->n.sym->attr.pointer != 1
690 : 1029 : && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
691 : 1222 : invalid = true;
692 : :
693 : 2364 : if (invalid)
694 : : {
695 : 2 : gfc_error ("Allocatable component or deferred-shaped array "
696 : : "near %C in DATA statement");
697 : 2 : goto cleanup;
698 : : }
699 : :
700 : : /* F2008:C567 (R536) A data-i-do-object or a variable that appears
701 : : as a data-stmt-object shall not be an object designator in which
702 : : a pointer appears other than as the entire rightmost part-ref. */
703 : 2362 : if (!e->ref && e->ts.type == BT_DERIVED
704 : 43 : && e->symtree->n.sym->attr.pointer)
705 : 4 : goto partref;
706 : :
707 : 2358 : ref = e->ref;
708 : 2358 : if (e->symtree->n.sym->ts.type == BT_DERIVED
709 : 125 : && e->symtree->n.sym->attr.pointer
710 : 1 : && ref->type == REF_COMPONENT)
711 : 1 : goto partref;
712 : :
713 : 3571 : for (; ref; ref = ref->next)
714 : 1215 : if (ref->type == REF_COMPONENT
715 : 135 : && ref->u.c.component->attr.pointer
716 : 27 : && ref->next)
717 : 1 : goto partref;
718 : : }
719 : :
720 : 2471 : m = top_val_list (new_data);
721 : 2471 : if (m != MATCH_YES)
722 : 29 : goto cleanup;
723 : :
724 : 2442 : new_data->next = gfc_current_ns->data;
725 : 2442 : gfc_current_ns->data = new_data;
726 : :
727 : : /* A BOZ literal constant cannot appear in a structure constructor.
728 : : Check for that here for a data statement value. */
729 : 2442 : if (new_data->value->expr->ts.type == BT_DERIVED
730 : 37 : && new_data->value->expr->value.constructor)
731 : : {
732 : 35 : gfc_constructor *c;
733 : 35 : c = gfc_constructor_first (new_data->value->expr->value.constructor);
734 : 106 : for (; c; c = gfc_constructor_next (c))
735 : 36 : if (c->expr && c->expr->ts.type == BT_BOZ)
736 : : {
737 : 0 : gfc_error ("BOZ literal constant at %L cannot appear in a "
738 : : "structure constructor", &c->expr->where);
739 : 0 : return MATCH_ERROR;
740 : : }
741 : : }
742 : :
743 : 2442 : if (gfc_match_eos () == MATCH_YES)
744 : : break;
745 : :
746 : 97 : gfc_match_char (','); /* Optional comma */
747 : 97 : }
748 : :
749 : 2345 : set_in_match_data (false);
750 : :
751 : 2345 : if (gfc_pure (NULL))
752 : : {
753 : 0 : gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
754 : 0 : return MATCH_ERROR;
755 : : }
756 : 2345 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
757 : :
758 : 2345 : return MATCH_YES;
759 : :
760 : 6 : partref:
761 : :
762 : 6 : gfc_error ("part-ref with pointer attribute near %L is not "
763 : : "rightmost part-ref of data-stmt-object",
764 : : &e->where);
765 : :
766 : 56 : cleanup:
767 : 56 : set_in_match_data (false);
768 : 56 : gfc_free_data (new_data);
769 : 56 : return MATCH_ERROR;
770 : : }
771 : :
772 : :
773 : : /************************ Declaration statements *********************/
774 : :
775 : :
776 : : /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
777 : : list). The difference here is the expression is a list of constants
778 : : and is surrounded by '/'.
779 : : The typespec ts must match the typespec of the variable which the
780 : : clist is initializing.
781 : : The arrayspec tells whether this should match a list of constants
782 : : corresponding to array elements or a scalar (as == NULL). */
783 : :
784 : : static match
785 : 74 : match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
786 : : {
787 : 74 : gfc_constructor_base array_head = NULL;
788 : 74 : gfc_expr *expr = NULL;
789 : 74 : match m = MATCH_ERROR;
790 : 74 : locus where;
791 : 74 : mpz_t repeat, cons_size, as_size;
792 : 74 : bool scalar;
793 : 74 : int cmp;
794 : :
795 : 74 : gcc_assert (ts);
796 : :
797 : : /* We have already matched '/' - now look for a constant list, as with
798 : : top_val_list from decl.cc, but append the result to an array. */
799 : 74 : if (gfc_match ("/") == MATCH_YES)
800 : : {
801 : 1 : gfc_error ("Empty old style initializer list at %C");
802 : 1 : return MATCH_ERROR;
803 : : }
804 : :
805 : 73 : where = gfc_current_locus;
806 : 73 : scalar = !as || !as->rank;
807 : :
808 : 42 : if (!scalar && !spec_size (as, &as_size))
809 : : {
810 : 2 : gfc_error ("Array in initializer list at %L must have an explicit shape",
811 : 1 : as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
812 : : /* Nothing to cleanup yet. */
813 : 1 : return MATCH_ERROR;
814 : : }
815 : :
816 : 72 : mpz_init_set_ui (repeat, 0);
817 : :
818 : 143 : for (;;)
819 : : {
820 : 143 : m = match_data_constant (&expr);
821 : 143 : if (m != MATCH_YES)
822 : 3 : expr = NULL; /* match_data_constant may set expr to garbage */
823 : 3 : if (m == MATCH_NO)
824 : 2 : goto syntax;
825 : 141 : if (m == MATCH_ERROR)
826 : 1 : goto cleanup;
827 : :
828 : : /* Found r in repeat spec r*c; look for the constant to repeat. */
829 : 140 : if ( gfc_match_char ('*') == MATCH_YES)
830 : : {
831 : 18 : if (scalar)
832 : : {
833 : 1 : gfc_error ("Repeat spec invalid in scalar initializer at %C");
834 : 1 : goto cleanup;
835 : : }
836 : 17 : if (expr->ts.type != BT_INTEGER)
837 : : {
838 : 1 : gfc_error ("Repeat spec must be an integer at %C");
839 : 1 : goto cleanup;
840 : : }
841 : 16 : mpz_set (repeat, expr->value.integer);
842 : 16 : gfc_free_expr (expr);
843 : 16 : expr = NULL;
844 : :
845 : 16 : m = match_data_constant (&expr);
846 : 16 : if (m == MATCH_NO)
847 : : {
848 : 1 : m = MATCH_ERROR;
849 : 1 : gfc_error ("Expected data constant after repeat spec at %C");
850 : : }
851 : 16 : if (m != MATCH_YES)
852 : 1 : goto cleanup;
853 : : }
854 : : /* No repeat spec, we matched the data constant itself. */
855 : : else
856 : 122 : mpz_set_ui (repeat, 1);
857 : :
858 : 137 : if (!scalar)
859 : : {
860 : : /* Add the constant initializer as many times as repeated. */
861 : 251 : for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
862 : : {
863 : : /* Make sure types of elements match */
864 : 144 : if(ts && !gfc_compare_types (&expr->ts, ts)
865 : 12 : && !gfc_convert_type (expr, ts, 1))
866 : 0 : goto cleanup;
867 : :
868 : 144 : gfc_constructor_append_expr (&array_head,
869 : : gfc_copy_expr (expr), &gfc_current_locus);
870 : : }
871 : :
872 : 107 : gfc_free_expr (expr);
873 : 107 : expr = NULL;
874 : : }
875 : :
876 : : /* For scalar initializers quit after one element. */
877 : : else
878 : : {
879 : 30 : if(gfc_match_char ('/') != MATCH_YES)
880 : : {
881 : 1 : gfc_error ("End of scalar initializer expected at %C");
882 : 1 : goto cleanup;
883 : : }
884 : : break;
885 : : }
886 : :
887 : 107 : if (gfc_match_char ('/') == MATCH_YES)
888 : : break;
889 : 72 : if (gfc_match_char (',') == MATCH_NO)
890 : 1 : goto syntax;
891 : : }
892 : :
893 : : /* If we break early from here out, we encountered an error. */
894 : 64 : m = MATCH_ERROR;
895 : :
896 : : /* Set up expr as an array constructor. */
897 : 64 : if (!scalar)
898 : : {
899 : 35 : expr = gfc_get_array_expr (ts->type, ts->kind, &where);
900 : 35 : expr->ts = *ts;
901 : 35 : expr->value.constructor = array_head;
902 : :
903 : : /* Validate sizes. We built expr ourselves, so cons_size will be
904 : : constant (we fail above for non-constant expressions).
905 : : We still need to verify that the sizes match. */
906 : 35 : gcc_assert (gfc_array_size (expr, &cons_size));
907 : 35 : cmp = mpz_cmp (cons_size, as_size);
908 : 35 : if (cmp < 0)
909 : 2 : gfc_error ("Not enough elements in array initializer at %C");
910 : 33 : else if (cmp > 0)
911 : 3 : gfc_error ("Too many elements in array initializer at %C");
912 : 35 : mpz_clear (cons_size);
913 : 35 : if (cmp)
914 : 5 : goto cleanup;
915 : :
916 : : /* Set the rank/shape to match the LHS as auto-reshape is implied. */
917 : 30 : expr->rank = as->rank;
918 : 30 : expr->corank = as->corank;
919 : 30 : expr->shape = gfc_get_shape (as->rank);
920 : 66 : for (int i = 0; i < as->rank; ++i)
921 : 36 : spec_dimen_size (as, i, &expr->shape[i]);
922 : : }
923 : :
924 : : /* Make sure scalar types match. */
925 : 29 : else if (!gfc_compare_types (&expr->ts, ts)
926 : 29 : && !gfc_convert_type (expr, ts, 1))
927 : 2 : goto cleanup;
928 : :
929 : 57 : if (expr->ts.u.cl)
930 : 1 : expr->ts.u.cl->length_from_typespec = 1;
931 : :
932 : 57 : *result = expr;
933 : 57 : m = MATCH_YES;
934 : 57 : goto done;
935 : :
936 : 3 : syntax:
937 : 3 : m = MATCH_ERROR;
938 : 3 : gfc_error ("Syntax error in old style initializer list at %C");
939 : :
940 : 15 : cleanup:
941 : 15 : if (expr)
942 : 10 : expr->value.constructor = NULL;
943 : 15 : gfc_free_expr (expr);
944 : 15 : gfc_constructor_free (array_head);
945 : :
946 : 72 : done:
947 : 72 : mpz_clear (repeat);
948 : 72 : if (!scalar)
949 : 41 : mpz_clear (as_size);
950 : : return m;
951 : : }
952 : :
953 : :
954 : : /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
955 : :
956 : : static bool
957 : 88 : merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
958 : : {
959 : 88 : if ((from->type == AS_ASSUMED_RANK && to->corank)
960 : 86 : || (to->type == AS_ASSUMED_RANK && from->corank))
961 : : {
962 : 5 : gfc_error ("The assumed-rank array at %C shall not have a codimension");
963 : 5 : return false;
964 : : }
965 : :
966 : 83 : if (to->rank == 0 && from->rank > 0)
967 : : {
968 : 36 : to->rank = from->rank;
969 : 36 : to->type = from->type;
970 : 36 : to->cray_pointee = from->cray_pointee;
971 : 36 : to->cp_was_assumed = from->cp_was_assumed;
972 : :
973 : 110 : for (int i = to->corank - 1; i >= 0; i--)
974 : : {
975 : : /* Do not exceed the limits on lower[] and upper[]. gfortran
976 : : cleans up elsewhere. */
977 : 74 : int j = from->rank + i;
978 : 74 : if (j >= GFC_MAX_DIMENSIONS)
979 : : break;
980 : :
981 : 74 : to->lower[j] = to->lower[i];
982 : 74 : to->upper[j] = to->upper[i];
983 : : }
984 : 85 : for (int i = 0; i < from->rank; i++)
985 : : {
986 : 49 : if (copy)
987 : : {
988 : 33 : to->lower[i] = gfc_copy_expr (from->lower[i]);
989 : 33 : to->upper[i] = gfc_copy_expr (from->upper[i]);
990 : : }
991 : : else
992 : : {
993 : 16 : to->lower[i] = from->lower[i];
994 : 16 : to->upper[i] = from->upper[i];
995 : : }
996 : : }
997 : : }
998 : 47 : else if (to->corank == 0 && from->corank > 0)
999 : : {
1000 : 22 : to->corank = from->corank;
1001 : 22 : to->cotype = from->cotype;
1002 : :
1003 : 75 : for (int i = 0; i < from->corank; i++)
1004 : : {
1005 : : /* Do not exceed the limits on lower[] and upper[]. gfortran
1006 : : cleans up elsewhere. */
1007 : 54 : int k = from->rank + i;
1008 : 54 : int j = to->rank + i;
1009 : 54 : if (j >= GFC_MAX_DIMENSIONS)
1010 : : break;
1011 : :
1012 : 53 : if (copy)
1013 : : {
1014 : 24 : to->lower[j] = gfc_copy_expr (from->lower[k]);
1015 : 24 : to->upper[j] = gfc_copy_expr (from->upper[k]);
1016 : : }
1017 : : else
1018 : : {
1019 : 29 : to->lower[j] = from->lower[k];
1020 : 29 : to->upper[j] = from->upper[k];
1021 : : }
1022 : : }
1023 : : }
1024 : :
1025 : 83 : if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
1026 : : {
1027 : 1 : gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1028 : : "allowed dimensions of %d",
1029 : : to->rank, to->corank, GFC_MAX_DIMENSIONS);
1030 : 1 : to->corank = GFC_MAX_DIMENSIONS - to->rank;
1031 : 1 : return false;
1032 : : }
1033 : : return true;
1034 : : }
1035 : :
1036 : :
1037 : : /* Match an intent specification. Since this can only happen after an
1038 : : INTENT word, a legal intent-spec must follow. */
1039 : :
1040 : : static sym_intent
1041 : 26469 : match_intent_spec (void)
1042 : : {
1043 : :
1044 : 26469 : if (gfc_match (" ( in out )") == MATCH_YES)
1045 : : return INTENT_INOUT;
1046 : 23526 : if (gfc_match (" ( in )") == MATCH_YES)
1047 : : return INTENT_IN;
1048 : 3546 : if (gfc_match (" ( out )") == MATCH_YES)
1049 : : return INTENT_OUT;
1050 : :
1051 : 2 : gfc_error ("Bad INTENT specification at %C");
1052 : 2 : return INTENT_UNKNOWN;
1053 : : }
1054 : :
1055 : :
1056 : : /* Matches a character length specification, which is either a
1057 : : specification expression, '*', or ':'. */
1058 : :
1059 : : static match
1060 : 26163 : char_len_param_value (gfc_expr **expr, bool *deferred)
1061 : : {
1062 : 26163 : match m;
1063 : 26163 : gfc_expr *p;
1064 : :
1065 : 26163 : *expr = NULL;
1066 : 26163 : *deferred = false;
1067 : :
1068 : 26163 : if (gfc_match_char ('*') == MATCH_YES)
1069 : : return MATCH_YES;
1070 : :
1071 : 19726 : if (gfc_match_char (':') == MATCH_YES)
1072 : : {
1073 : 3246 : if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1074 : : return MATCH_ERROR;
1075 : :
1076 : 3244 : *deferred = true;
1077 : :
1078 : 3244 : return MATCH_YES;
1079 : : }
1080 : :
1081 : 16480 : m = gfc_match_expr (expr);
1082 : :
1083 : 16480 : if (m == MATCH_NO || m == MATCH_ERROR)
1084 : : return m;
1085 : :
1086 : 16475 : if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1087 : : return MATCH_ERROR;
1088 : :
1089 : : /* Try to simplify the expression to catch things like CHARACTER(([1])). */
1090 : 16469 : p = gfc_copy_expr (*expr);
1091 : 16469 : if (gfc_is_constant_expr (p) && gfc_simplify_expr (p, 1))
1092 : 13944 : gfc_replace_expr (*expr, p);
1093 : : else
1094 : 2525 : gfc_free_expr (p);
1095 : :
1096 : 16469 : if ((*expr)->expr_type == EXPR_FUNCTION)
1097 : : {
1098 : 1014 : if ((*expr)->ts.type == BT_INTEGER
1099 : 1013 : || ((*expr)->ts.type == BT_UNKNOWN
1100 : 1013 : && strcmp((*expr)->symtree->name, "null") != 0))
1101 : : return MATCH_YES;
1102 : :
1103 : 2 : goto syntax;
1104 : : }
1105 : 15455 : else if ((*expr)->expr_type == EXPR_CONSTANT)
1106 : : {
1107 : : /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1108 : : processor dependent and its value is greater than or equal to zero.
1109 : : F2008, 4.4.3.2: If the character length parameter value evaluates
1110 : : to a negative value, the length of character entities declared
1111 : : is zero. */
1112 : :
1113 : 13876 : if ((*expr)->ts.type == BT_INTEGER)
1114 : : {
1115 : 13858 : if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1116 : 4 : mpz_set_si ((*expr)->value.integer, 0);
1117 : : }
1118 : : else
1119 : 18 : goto syntax;
1120 : : }
1121 : 1579 : else if ((*expr)->expr_type == EXPR_ARRAY)
1122 : 8 : goto syntax;
1123 : 1571 : else if ((*expr)->expr_type == EXPR_VARIABLE)
1124 : : {
1125 : 1173 : bool t;
1126 : 1173 : gfc_expr *e;
1127 : :
1128 : 1173 : e = gfc_copy_expr (*expr);
1129 : :
1130 : : /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1131 : : which causes an ICE if gfc_reduce_init_expr() is called. */
1132 : 1173 : if (e->ref && e->ref->type == REF_ARRAY
1133 : 8 : && e->ref->u.ar.type == AR_UNKNOWN
1134 : 7 : && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1135 : 2 : goto syntax;
1136 : :
1137 : 1171 : t = gfc_reduce_init_expr (e);
1138 : :
1139 : 1171 : if (!t && e->ts.type == BT_UNKNOWN
1140 : 7 : && e->symtree->n.sym->attr.untyped == 1
1141 : 7 : && (flag_implicit_none
1142 : 5 : || e->symtree->n.sym->ns->seen_implicit_none == 1
1143 : 1 : || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1144 : : {
1145 : 7 : gfc_free_expr (e);
1146 : 7 : goto syntax;
1147 : : }
1148 : :
1149 : 1164 : if ((e->ref && e->ref->type == REF_ARRAY
1150 : 4 : && e->ref->u.ar.type != AR_ELEMENT)
1151 : 1163 : || (!e->ref && e->expr_type == EXPR_ARRAY))
1152 : : {
1153 : 2 : gfc_free_expr (e);
1154 : 2 : goto syntax;
1155 : : }
1156 : :
1157 : 1162 : gfc_free_expr (e);
1158 : : }
1159 : :
1160 : 15418 : if (gfc_seen_div0)
1161 : 52 : m = MATCH_ERROR;
1162 : :
1163 : : return m;
1164 : :
1165 : 39 : syntax:
1166 : 39 : gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1167 : 39 : return MATCH_ERROR;
1168 : : }
1169 : :
1170 : :
1171 : : /* A character length is a '*' followed by a literal integer or a
1172 : : char_len_param_value in parenthesis. */
1173 : :
1174 : : static match
1175 : 60502 : match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1176 : : {
1177 : 60502 : int length;
1178 : 60502 : match m;
1179 : :
1180 : 60502 : *deferred = false;
1181 : 60502 : m = gfc_match_char ('*');
1182 : 60502 : if (m != MATCH_YES)
1183 : : return m;
1184 : :
1185 : 2653 : m = gfc_match_small_literal_int (&length, NULL);
1186 : 2653 : if (m == MATCH_ERROR)
1187 : : return m;
1188 : :
1189 : 2653 : if (m == MATCH_YES)
1190 : : {
1191 : 2137 : if (obsolescent_check
1192 : 2137 : && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1193 : : return MATCH_ERROR;
1194 : 2137 : *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1195 : 2137 : return m;
1196 : : }
1197 : :
1198 : 516 : if (gfc_match_char ('(') == MATCH_NO)
1199 : 0 : goto syntax;
1200 : :
1201 : 516 : m = char_len_param_value (expr, deferred);
1202 : 516 : if (m != MATCH_YES && gfc_matching_function)
1203 : : {
1204 : 0 : gfc_undo_symbols ();
1205 : 0 : m = MATCH_YES;
1206 : : }
1207 : :
1208 : 1 : if (m == MATCH_ERROR)
1209 : : return m;
1210 : 515 : if (m == MATCH_NO)
1211 : 0 : goto syntax;
1212 : :
1213 : 515 : if (gfc_match_char (')') == MATCH_NO)
1214 : : {
1215 : 0 : gfc_free_expr (*expr);
1216 : 0 : *expr = NULL;
1217 : 0 : goto syntax;
1218 : : }
1219 : :
1220 : : return MATCH_YES;
1221 : :
1222 : 0 : syntax:
1223 : 0 : gfc_error ("Syntax error in character length specification at %C");
1224 : 0 : return MATCH_ERROR;
1225 : : }
1226 : :
1227 : :
1228 : : /* Special subroutine for finding a symbol. Check if the name is found
1229 : : in the current name space. If not, and we're compiling a function or
1230 : : subroutine and the parent compilation unit is an interface, then check
1231 : : to see if the name we've been given is the name of the interface
1232 : : (located in another namespace). */
1233 : :
1234 : : static int
1235 : 275176 : find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1236 : : {
1237 : 275176 : gfc_state_data *s;
1238 : 275176 : gfc_symtree *st;
1239 : 275176 : int i;
1240 : :
1241 : 275176 : i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1242 : 275176 : if (i == 0)
1243 : : {
1244 : 275176 : *result = st ? st->n.sym : NULL;
1245 : 275176 : goto end;
1246 : : }
1247 : :
1248 : 0 : if (gfc_current_state () != COMP_SUBROUTINE
1249 : 0 : && gfc_current_state () != COMP_FUNCTION)
1250 : 0 : goto end;
1251 : :
1252 : 0 : s = gfc_state_stack->previous;
1253 : 0 : if (s == NULL)
1254 : 0 : goto end;
1255 : :
1256 : 0 : if (s->state != COMP_INTERFACE)
1257 : 0 : goto end;
1258 : 0 : if (s->sym == NULL)
1259 : 0 : goto end; /* Nameless interface. */
1260 : :
1261 : 0 : if (strcmp (name, s->sym->name) == 0)
1262 : : {
1263 : 0 : *result = s->sym;
1264 : 0 : return 0;
1265 : : }
1266 : :
1267 : 0 : end:
1268 : : return i;
1269 : : }
1270 : :
1271 : :
1272 : : /* Special subroutine for getting a symbol node associated with a
1273 : : procedure name, used in SUBROUTINE and FUNCTION statements. The
1274 : : symbol is created in the parent using with symtree node in the
1275 : : child unit pointing to the symbol. If the current namespace has no
1276 : : parent, then the symbol is just created in the current unit. */
1277 : :
1278 : : static int
1279 : 61815 : get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1280 : : {
1281 : 61815 : gfc_symtree *st;
1282 : 61815 : gfc_symbol *sym;
1283 : 61815 : int rc = 0;
1284 : :
1285 : : /* Module functions have to be left in their own namespace because
1286 : : they have potentially (almost certainly!) already been referenced.
1287 : : In this sense, they are rather like external functions. This is
1288 : : fixed up in resolve.cc(resolve_entries), where the symbol name-
1289 : : space is set to point to the master function, so that the fake
1290 : : result mechanism can work. */
1291 : 61815 : if (module_fcn_entry)
1292 : : {
1293 : : /* Present if entry is declared to be a module procedure. */
1294 : 259 : rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1295 : :
1296 : 259 : if (*result == NULL)
1297 : 216 : rc = gfc_get_symbol (name, NULL, result);
1298 : 86 : else if (!gfc_get_symbol (name, NULL, &sym) && sym
1299 : 43 : && (*result)->ts.type == BT_UNKNOWN
1300 : 86 : && sym->attr.flavor == FL_UNKNOWN)
1301 : : /* Pick up the typespec for the entry, if declared in the function
1302 : : body. Note that this symbol is FL_UNKNOWN because it will
1303 : : only have appeared in a type declaration. The local symtree
1304 : : is set to point to the module symbol and a unique symtree
1305 : : to the local version. This latter ensures a correct clearing
1306 : : of the symbols. */
1307 : : {
1308 : : /* If the ENTRY proceeds its specification, we need to ensure
1309 : : that this does not raise a "has no IMPLICIT type" error. */
1310 : 43 : if (sym->ts.type == BT_UNKNOWN)
1311 : 23 : sym->attr.untyped = 1;
1312 : :
1313 : 43 : (*result)->ts = sym->ts;
1314 : :
1315 : : /* Put the symbol in the procedure namespace so that, should
1316 : : the ENTRY precede its specification, the specification
1317 : : can be applied. */
1318 : 43 : (*result)->ns = gfc_current_ns;
1319 : :
1320 : 43 : gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1321 : 43 : st->n.sym = *result;
1322 : 43 : st = gfc_get_unique_symtree (gfc_current_ns);
1323 : 43 : sym->refs++;
1324 : 43 : st->n.sym = sym;
1325 : : }
1326 : : }
1327 : : else
1328 : 61556 : rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1329 : :
1330 : 61815 : if (rc)
1331 : : return rc;
1332 : :
1333 : 61814 : sym = *result;
1334 : 61814 : if (sym->attr.proc == PROC_ST_FUNCTION)
1335 : : return rc;
1336 : :
1337 : 61814 : if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1338 : : {
1339 : : /* Create a partially populated interface symbol to carry the
1340 : : characteristics of the procedure and the result. */
1341 : 432 : sym->tlink = gfc_new_symbol (name, sym->ns);
1342 : 432 : gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1343 : 432 : gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1344 : 432 : if (sym->attr.dimension)
1345 : 17 : sym->tlink->as = gfc_copy_array_spec (sym->as);
1346 : :
1347 : : /* Ideally, at this point, a copy would be made of the formal
1348 : : arguments and their namespace. However, this does not appear
1349 : : to be necessary, albeit at the expense of not being able to
1350 : : use gfc_compare_interfaces directly. */
1351 : :
1352 : 432 : if (sym->result && sym->result != sym)
1353 : : {
1354 : 103 : sym->tlink->result = sym->result;
1355 : 103 : sym->result = NULL;
1356 : : }
1357 : 329 : else if (sym->result)
1358 : : {
1359 : 83 : sym->tlink->result = sym->tlink;
1360 : : }
1361 : : }
1362 : 61382 : else if (sym && !sym->gfc_new
1363 : 23434 : && gfc_current_state () != COMP_INTERFACE)
1364 : : {
1365 : : /* Trap another encompassed procedure with the same name. All
1366 : : these conditions are necessary to avoid picking up an entry
1367 : : whose name clashes with that of the encompassing procedure;
1368 : : this is handled using gsymbols to register unique, globally
1369 : : accessible names. */
1370 : 22440 : if (sym->attr.flavor != 0
1371 : 20419 : && sym->attr.proc != 0
1372 : 2143 : && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1373 : 7 : && sym->attr.if_source != IFSRC_UNKNOWN)
1374 : : {
1375 : 7 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1376 : : name, &sym->declared_at);
1377 : 7 : return true;
1378 : : }
1379 : 22433 : if (sym->attr.flavor != 0
1380 : 20412 : && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1381 : : {
1382 : 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1383 : : name, &sym->declared_at);
1384 : 1 : return true;
1385 : : }
1386 : :
1387 : 22432 : if (sym->attr.external && sym->attr.procedure
1388 : 2 : && gfc_current_state () == COMP_CONTAINS)
1389 : : {
1390 : 1 : gfc_error_now ("Contained procedure %qs at %C clashes with "
1391 : : "procedure defined at %L",
1392 : : name, &sym->declared_at);
1393 : 1 : return true;
1394 : : }
1395 : :
1396 : : /* Trap a procedure with a name the same as interface in the
1397 : : encompassing scope. */
1398 : 22431 : if (sym->attr.generic != 0
1399 : 60 : && (sym->attr.subroutine || sym->attr.function)
1400 : 1 : && !sym->attr.mod_proc)
1401 : : {
1402 : 1 : gfc_error_now ("Name %qs at %C is already defined"
1403 : : " as a generic interface at %L",
1404 : : name, &sym->declared_at);
1405 : 1 : return true;
1406 : : }
1407 : :
1408 : : /* Trap declarations of attributes in encompassing scope. The
1409 : : signature for this is that ts.kind is nonzero for no-CLASS
1410 : : entity. For a CLASS entity, ts.kind is zero. */
1411 : 22430 : if ((sym->ts.kind != 0
1412 : 22089 : || sym->ts.type == BT_CLASS
1413 : 22088 : || sym->ts.type == BT_DERIVED)
1414 : 365 : && !sym->attr.implicit_type
1415 : 364 : && sym->attr.proc == 0
1416 : 346 : && gfc_current_ns->parent != NULL
1417 : 137 : && sym->attr.access == 0
1418 : 135 : && !module_fcn_entry)
1419 : : {
1420 : 5 : gfc_error_now ("Procedure %qs at %C has an explicit interface "
1421 : : "from a previous declaration", name);
1422 : 5 : return true;
1423 : : }
1424 : : }
1425 : :
1426 : : /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1427 : : subroutine-stmt of a module subprogram or of a nonabstract interface
1428 : : body that is declared in the scoping unit of a module or submodule. */
1429 : 61799 : if (sym->attr.external
1430 : 92 : && (sym->attr.subroutine || sym->attr.function)
1431 : 91 : && sym->attr.if_source == IFSRC_IFBODY
1432 : 91 : && !current_attr.module_procedure
1433 : 3 : && sym->attr.proc == PROC_MODULE
1434 : 3 : && gfc_state_stack->state == COMP_CONTAINS)
1435 : : {
1436 : 1 : gfc_error_now ("Procedure %qs defined in interface body at %L "
1437 : : "clashes with internal procedure defined at %C",
1438 : : name, &sym->declared_at);
1439 : 1 : return true;
1440 : : }
1441 : :
1442 : 61798 : if (sym && !sym->gfc_new
1443 : 23850 : && sym->attr.flavor != FL_UNKNOWN
1444 : 21452 : && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1445 : 215 : && gfc_state_stack->state == COMP_CONTAINS
1446 : 210 : && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1447 : : {
1448 : 1 : gfc_error_now ("Procedure %qs at %C is already defined at %L",
1449 : : name, &sym->declared_at);
1450 : 1 : return true;
1451 : : }
1452 : :
1453 : 61797 : if (gfc_current_ns->parent == NULL || *result == NULL)
1454 : : return rc;
1455 : :
1456 : : /* Module function entries will already have a symtree in
1457 : : the current namespace but will need one at module level. */
1458 : 49927 : if (module_fcn_entry)
1459 : : {
1460 : : /* Present if entry is declared to be a module procedure. */
1461 : 257 : rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1462 : 257 : if (st == NULL)
1463 : 216 : st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1464 : : }
1465 : : else
1466 : 49670 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1467 : :
1468 : 49927 : st->n.sym = sym;
1469 : 49927 : sym->refs++;
1470 : :
1471 : : /* See if the procedure should be a module procedure. */
1472 : :
1473 : 49927 : if (((sym->ns->proc_name != NULL
1474 : 49927 : && sym->ns->proc_name->attr.flavor == FL_MODULE
1475 : 20262 : && sym->attr.proc != PROC_MODULE)
1476 : 49927 : || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1477 : 67644 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1478 : : rc = 2;
1479 : :
1480 : : return rc;
1481 : : }
1482 : :
1483 : :
1484 : : /* Verify that the given symbol representing a parameter is C
1485 : : interoperable, by checking to see if it was marked as such after
1486 : : its declaration. If the given symbol is not interoperable, a
1487 : : warning is reported, thus removing the need to return the status to
1488 : : the calling function. The standard does not require the user use
1489 : : one of the iso_c_binding named constants to declare an
1490 : : interoperable parameter, but we can't be sure if the param is C
1491 : : interop or not if the user doesn't. For example, integer(4) may be
1492 : : legal Fortran, but doesn't have meaning in C. It may interop with
1493 : : a number of the C types, which causes a problem because the
1494 : : compiler can't know which one. This code is almost certainly not
1495 : : portable, and the user will get what they deserve if the C type
1496 : : across platforms isn't always interoperable with integer(4). If
1497 : : the user had used something like integer(c_int) or integer(c_long),
1498 : : the compiler could have automatically handled the varying sizes
1499 : : across platforms. */
1500 : :
1501 : : bool
1502 : 16361 : gfc_verify_c_interop_param (gfc_symbol *sym)
1503 : : {
1504 : 16361 : int is_c_interop = 0;
1505 : 16361 : bool retval = true;
1506 : :
1507 : : /* We check implicitly typed variables in symbol.cc:gfc_set_default_type().
1508 : : Don't repeat the checks here. */
1509 : 16361 : if (sym->attr.implicit_type)
1510 : : return true;
1511 : :
1512 : : /* For subroutines or functions that are passed to a BIND(C) procedure,
1513 : : they're interoperable if they're BIND(C) and their params are all
1514 : : interoperable. */
1515 : 16361 : if (sym->attr.flavor == FL_PROCEDURE)
1516 : : {
1517 : 4 : if (sym->attr.is_bind_c == 0)
1518 : : {
1519 : 0 : gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1520 : : "attribute to be C interoperable", sym->name,
1521 : : &(sym->declared_at));
1522 : 0 : return false;
1523 : : }
1524 : : else
1525 : : {
1526 : 4 : if (sym->attr.is_c_interop == 1)
1527 : : /* We've already checked this procedure; don't check it again. */
1528 : : return true;
1529 : : else
1530 : 4 : return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1531 : 4 : sym->common_block);
1532 : : }
1533 : : }
1534 : :
1535 : : /* See if we've stored a reference to a procedure that owns sym. */
1536 : 16357 : if (sym->ns != NULL && sym->ns->proc_name != NULL)
1537 : : {
1538 : 16357 : if (sym->ns->proc_name->attr.is_bind_c == 1)
1539 : : {
1540 : 16318 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;
1541 : 16318 : bool f2018_added = false;
1542 : :
1543 : 16318 : is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1544 : :
1545 : : /* F2018:18.3.6 has the following text:
1546 : : "(5) any dummy argument without the VALUE attribute corresponds to
1547 : : a formal parameter of the prototype that is of a pointer type, and
1548 : : either
1549 : : • the dummy argument is interoperable with an entity of the
1550 : : referenced type (ISO/IEC 9899:2011, 6.2.5, 7.19, and 7.20.1) of
1551 : : the formal parameter (this is equivalent to the F2008 text),
1552 : : • the dummy argument is a nonallocatable nonpointer variable of
1553 : : type CHARACTER with assumed character length and the formal
1554 : : parameter is a pointer to CFI_cdesc_t,
1555 : : • the dummy argument is allocatable, assumed-shape, assumed-rank,
1556 : : or a pointer without the CONTIGUOUS attribute, and the formal
1557 : : parameter is a pointer to CFI_cdesc_t, or
1558 : : • the dummy argument is assumed-type and not allocatable,
1559 : : assumed-shape, assumed-rank, or a pointer, and the formal
1560 : : parameter is a pointer to void," */
1561 : 3720 : if (is_c_interop == 0 && !sym->attr.value && f2018_allowed)
1562 : : {
1563 : 2354 : bool as_ar = (sym->as
1564 : 2354 : && (sym->as->type == AS_ASSUMED_SHAPE
1565 : 2109 : || sym->as->type == AS_ASSUMED_RANK));
1566 : 4708 : bool cond1 = (sym->ts.type == BT_CHARACTER
1567 : 1564 : && !(sym->ts.u.cl && sym->ts.u.cl->length)
1568 : 904 : && !sym->attr.allocatable
1569 : 3240 : && !sym->attr.pointer);
1570 : 4708 : bool cond2 = (sym->attr.allocatable
1571 : 2257 : || as_ar
1572 : 3370 : || (IS_POINTER (sym) && !sym->attr.contiguous));
1573 : 4708 : bool cond3 = (sym->ts.type == BT_ASSUMED
1574 : 0 : && !sym->attr.allocatable
1575 : 0 : && !sym->attr.pointer
1576 : 2354 : && !as_ar);
1577 : 2354 : f2018_added = cond1 || cond2 || cond3;
1578 : : }
1579 : :
1580 : 16318 : if (is_c_interop != 1 && !f2018_added)
1581 : : {
1582 : : /* Make personalized messages to give better feedback. */
1583 : 1828 : if (sym->ts.type == BT_DERIVED)
1584 : 1 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1585 : : "BIND(C) procedure %qs but is not C interoperable "
1586 : : "because derived type %qs is not C interoperable",
1587 : : sym->name, &(sym->declared_at),
1588 : 1 : sym->ns->proc_name->name,
1589 : 1 : sym->ts.u.derived->name);
1590 : 1827 : else if (sym->ts.type == BT_CLASS)
1591 : 6 : gfc_error ("Variable %qs at %L is a dummy argument to the "
1592 : : "BIND(C) procedure %qs but is not C interoperable "
1593 : : "because it is polymorphic",
1594 : : sym->name, &(sym->declared_at),
1595 : 6 : sym->ns->proc_name->name);
1596 : 1821 : else if (warn_c_binding_type)
1597 : 39 : gfc_warning (OPT_Wc_binding_type,
1598 : : "Variable %qs at %L is a dummy argument of the "
1599 : : "BIND(C) procedure %qs but may not be C "
1600 : : "interoperable",
1601 : : sym->name, &(sym->declared_at),
1602 : 39 : sym->ns->proc_name->name);
1603 : : }
1604 : :
1605 : : /* Per F2018, 18.3.6 (5), pointer + contiguous is not permitted. */
1606 : 16318 : if (sym->attr.pointer && sym->attr.contiguous)
1607 : 2 : gfc_error ("Dummy argument %qs at %L may not be a pointer with "
1608 : : "CONTIGUOUS attribute as procedure %qs is BIND(C)",
1609 : 2 : sym->name, &sym->declared_at, sym->ns->proc_name->name);
1610 : :
1611 : : /* Per F2018, C1557, pointer/allocatable dummies to a bind(c)
1612 : : procedure that are default-initialized are not permitted. */
1613 : 15680 : if ((sym->attr.pointer || sym->attr.allocatable)
1614 : 1037 : && sym->ts.type == BT_DERIVED
1615 : 16696 : && gfc_has_default_initializer (sym->ts.u.derived))
1616 : : {
1617 : 8 : gfc_error ("Default-initialized dummy argument %qs with %s "
1618 : : "attribute at %L is not permitted in BIND(C) "
1619 : : "procedure %qs", sym->name,
1620 : 4 : (sym->attr.pointer ? "POINTER" : "ALLOCATABLE"),
1621 : 4 : &sym->declared_at, sym->ns->proc_name->name);
1622 : 4 : retval = false;
1623 : : }
1624 : :
1625 : : /* Character strings are only C interoperable if they have a
1626 : : length of 1. However, as an argument they are also interoperable
1627 : : when passed as descriptor (which requires len=: or len=*). */
1628 : 16318 : if (sym->ts.type == BT_CHARACTER)
1629 : : {
1630 : 2338 : gfc_charlen *cl = sym->ts.u.cl;
1631 : :
1632 : 2338 : if (sym->attr.allocatable || sym->attr.pointer)
1633 : : {
1634 : : /* F2018, 18.3.6 (6). */
1635 : 193 : if (!sym->ts.deferred)
1636 : : {
1637 : 64 : if (sym->attr.allocatable)
1638 : 32 : gfc_error ("Allocatable character dummy argument %qs "
1639 : : "at %L must have deferred length as "
1640 : : "procedure %qs is BIND(C)", sym->name,
1641 : 32 : &sym->declared_at, sym->ns->proc_name->name);
1642 : : else
1643 : 32 : gfc_error ("Pointer character dummy argument %qs at %L "
1644 : : "must have deferred length as procedure %qs "
1645 : : "is BIND(C)", sym->name, &sym->declared_at,
1646 : 32 : sym->ns->proc_name->name);
1647 : : retval = false;
1648 : : }
1649 : 129 : else if (!gfc_notify_std (GFC_STD_F2018,
1650 : : "Deferred-length character dummy "
1651 : : "argument %qs at %L of procedure "
1652 : : "%qs with BIND(C) attribute",
1653 : : sym->name, &sym->declared_at,
1654 : 129 : sym->ns->proc_name->name))
1655 : 102 : retval = false;
1656 : : }
1657 : 2145 : else if (sym->attr.value
1658 : 354 : && (!cl || !cl->length
1659 : 354 : || cl->length->expr_type != EXPR_CONSTANT
1660 : 354 : || mpz_cmp_si (cl->length->value.integer, 1) != 0))
1661 : : {
1662 : 1 : gfc_error ("Character dummy argument %qs at %L must be "
1663 : : "of length 1 as it has the VALUE attribute",
1664 : : sym->name, &sym->declared_at);
1665 : 1 : retval = false;
1666 : : }
1667 : 2144 : else if (!cl || !cl->length)
1668 : : {
1669 : : /* Assumed length; F2018, 18.3.6 (5)(2).
1670 : : Uses the CFI array descriptor - also for scalars and
1671 : : explicit-size/assumed-size arrays. */
1672 : 957 : if (!gfc_notify_std (GFC_STD_F2018,
1673 : : "Assumed-length character dummy argument "
1674 : : "%qs at %L of procedure %qs with BIND(C) "
1675 : : "attribute", sym->name, &sym->declared_at,
1676 : 957 : sym->ns->proc_name->name))
1677 : 102 : retval = false;
1678 : : }
1679 : 1187 : else if (cl->length->expr_type != EXPR_CONSTANT
1680 : 873 : || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1681 : : {
1682 : : /* F2018, 18.3.6, (5), item 4. */
1683 : 653 : if (!sym->attr.dimension
1684 : 645 : || sym->as->type == AS_ASSUMED_SIZE
1685 : 639 : || sym->as->type == AS_EXPLICIT)
1686 : : {
1687 : 20 : gfc_error ("Character dummy argument %qs at %L must be "
1688 : : "of constant length of one or assumed length, "
1689 : : "unless it has assumed shape or assumed rank, "
1690 : : "as procedure %qs has the BIND(C) attribute",
1691 : : sym->name, &sym->declared_at,
1692 : 20 : sym->ns->proc_name->name);
1693 : 20 : retval = false;
1694 : : }
1695 : : /* else: valid only since F2018 - and an assumed-shape/rank
1696 : : array; however, gfc_notify_std is already called when
1697 : : those array types are used. Thus, silently accept F200x. */
1698 : : }
1699 : : }
1700 : :
1701 : : /* We have to make sure that any param to a bind(c) routine does
1702 : : not have the allocatable, pointer, or optional attributes,
1703 : : according to J3/04-007, section 5.1. */
1704 : 16318 : if (sym->attr.allocatable == 1
1705 : 16717 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1706 : : "ALLOCATABLE attribute in procedure %qs "
1707 : : "with BIND(C)", sym->name,
1708 : : &(sym->declared_at),
1709 : 399 : sym->ns->proc_name->name))
1710 : : retval = false;
1711 : :
1712 : 16318 : if (sym->attr.pointer == 1
1713 : 16956 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1714 : : "POINTER attribute in procedure %qs "
1715 : : "with BIND(C)", sym->name,
1716 : : &(sym->declared_at),
1717 : 638 : sym->ns->proc_name->name))
1718 : : retval = false;
1719 : :
1720 : 16318 : if (sym->attr.optional == 1 && sym->attr.value)
1721 : : {
1722 : 9 : gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1723 : : "and the VALUE attribute because procedure %qs "
1724 : : "is BIND(C)", sym->name, &(sym->declared_at),
1725 : 9 : sym->ns->proc_name->name);
1726 : 9 : retval = false;
1727 : : }
1728 : 16309 : else if (sym->attr.optional == 1
1729 : 17253 : && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1730 : : "at %L with OPTIONAL attribute in "
1731 : : "procedure %qs which is BIND(C)",
1732 : : sym->name, &(sym->declared_at),
1733 : 944 : sym->ns->proc_name->name))
1734 : : retval = false;
1735 : :
1736 : : /* Make sure that if it has the dimension attribute, that it is
1737 : : either assumed size or explicit shape. Deferred shape is already
1738 : : covered by the pointer/allocatable attribute. */
1739 : 5399 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1740 : 17648 : && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1741 : : "at %L as dummy argument to the BIND(C) "
1742 : : "procedure %qs at %L", sym->name,
1743 : : &(sym->declared_at),
1744 : : sym->ns->proc_name->name,
1745 : 1330 : &(sym->ns->proc_name->declared_at)))
1746 : : retval = false;
1747 : : }
1748 : : }
1749 : :
1750 : : return retval;
1751 : : }
1752 : :
1753 : :
1754 : :
1755 : : /* Function called by variable_decl() that adds a name to the symbol table. */
1756 : :
1757 : : static bool
1758 : 254536 : build_sym (const char *name, int elem, gfc_charlen *cl, bool cl_deferred,
1759 : : gfc_array_spec **as, locus *var_locus)
1760 : : {
1761 : 254536 : symbol_attribute attr;
1762 : 254536 : gfc_symbol *sym;
1763 : 254536 : int upper;
1764 : 254536 : gfc_symtree *st, *host_st = NULL;
1765 : :
1766 : : /* Symbols in a submodule are host associated from the parent module or
1767 : : submodules. Therefore, they can be overridden by declarations in the
1768 : : submodule scope. Deal with this by attaching the existing symbol to
1769 : : a new symtree and recycling the old symtree with a new symbol... */
1770 : 254536 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1771 : 254536 : if (((st && st->import_only) || (gfc_current_ns->import_state == IMPORT_ALL))
1772 : 3 : && gfc_current_ns->parent)
1773 : 3 : host_st = gfc_find_symtree (gfc_current_ns->parent->sym_root, name);
1774 : :
1775 : 254536 : if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1776 : 12 : && st->n.sym != NULL
1777 : 12 : && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1778 : : {
1779 : 12 : gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1780 : 12 : s->n.sym = st->n.sym;
1781 : 12 : sym = gfc_new_symbol (name, gfc_current_ns, var_locus);
1782 : :
1783 : 12 : st->n.sym = sym;
1784 : 12 : sym->refs++;
1785 : 12 : gfc_set_sym_referenced (sym);
1786 : 12 : }
1787 : : /* ...Check that F2018 IMPORT, ONLY and IMPORT, ALL statements, within the
1788 : : current scope are not violated by local redeclarations. Note that there is
1789 : : no need to guard for std >= F2018 because import_only and IMPORT_ALL are
1790 : : only set for these standards. */
1791 : 254524 : else if (host_st && host_st->n.sym
1792 : 2 : && host_st->n.sym != gfc_current_ns->proc_name
1793 : 2 : && !(st && st->n.sym
1794 : 1 : && (st->n.sym->attr.dummy || st->n.sym->attr.result)))
1795 : : {
1796 : 2 : gfc_error ("F2018: C8102 %s at %L is already imported by an %s "
1797 : : "statement and must not be re-declared", name, var_locus,
1798 : 1 : (st && st->import_only) ? "IMPORT, ONLY" : "IMPORT, ALL");
1799 : 2 : return false;
1800 : : }
1801 : : /* ...Otherwise generate a new symtree and new symbol. */
1802 : 254522 : else if (gfc_get_symbol (name, NULL, &sym, var_locus))
1803 : : return false;
1804 : :
1805 : : /* Check if the name has already been defined as a type. The
1806 : : first letter of the symtree will be in upper case then. Of
1807 : : course, this is only necessary if the upper case letter is
1808 : : actually different. */
1809 : :
1810 : 254534 : upper = TOUPPER(name[0]);
1811 : 254534 : if (upper != name[0])
1812 : : {
1813 : 253896 : char u_name[GFC_MAX_SYMBOL_LEN + 1];
1814 : 253896 : gfc_symtree *st;
1815 : :
1816 : 253896 : gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1817 : 253896 : strcpy (u_name, name);
1818 : 253896 : u_name[0] = upper;
1819 : :
1820 : 253896 : st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1821 : :
1822 : : /* STRUCTURE types can alias symbol names */
1823 : 253896 : if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1824 : : {
1825 : 1 : gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1826 : : &st->n.sym->declared_at);
1827 : 1 : return false;
1828 : : }
1829 : : }
1830 : :
1831 : : /* Start updating the symbol table. Add basic type attribute if present. */
1832 : 254533 : if (current_ts.type != BT_UNKNOWN
1833 : 254533 : && (sym->attr.implicit_type == 0
1834 : 185 : || !gfc_compare_types (&sym->ts, ¤t_ts))
1835 : 508885 : && !gfc_add_type (sym, ¤t_ts, var_locus))
1836 : : return false;
1837 : :
1838 : 254507 : if (sym->ts.type == BT_CHARACTER)
1839 : : {
1840 : 28323 : if (elem > 1)
1841 : 4045 : sym->ts.u.cl = gfc_new_charlen (sym->ns, cl);
1842 : : else
1843 : 24278 : sym->ts.u.cl = cl;
1844 : 28323 : sym->ts.deferred = cl_deferred;
1845 : : }
1846 : :
1847 : : /* Add dimension attribute if present. */
1848 : 254507 : if (!gfc_set_array_spec (sym, *as, var_locus))
1849 : : return false;
1850 : 254505 : *as = NULL;
1851 : :
1852 : : /* Add attribute to symbol. The copy is so that we can reset the
1853 : : dimension attribute. */
1854 : 254505 : attr = current_attr;
1855 : 254505 : attr.dimension = 0;
1856 : 254505 : attr.codimension = 0;
1857 : :
1858 : 254505 : if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1859 : : return false;
1860 : :
1861 : : /* Finish any work that may need to be done for the binding label,
1862 : : if it's a bind(c). The bind(c) attr is found before the symbol
1863 : : is made, and before the symbol name (for data decls), so the
1864 : : current_ts is holding the binding label, or nothing if the
1865 : : name= attr wasn't given. Therefore, test here if we're dealing
1866 : : with a bind(c) and make sure the binding label is set correctly. */
1867 : 254491 : if (sym->attr.is_bind_c == 1)
1868 : : {
1869 : 1300 : if (!sym->binding_label)
1870 : : {
1871 : : /* Set the binding label and verify that if a NAME= was specified
1872 : : then only one identifier was in the entity-decl-list. */
1873 : 136 : if (!set_binding_label (&sym->binding_label, sym->name,
1874 : : num_idents_on_line))
1875 : : return false;
1876 : : }
1877 : : }
1878 : :
1879 : : /* See if we know we're in a common block, and if it's a bind(c)
1880 : : common then we need to make sure we're an interoperable type. */
1881 : 254489 : if (sym->attr.in_common == 1)
1882 : : {
1883 : : /* Test the common block object. */
1884 : 613 : if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1885 : 6 : && sym->ts.is_c_interop != 1)
1886 : : {
1887 : 0 : gfc_error_now ("Variable %qs in common block %qs at %C "
1888 : : "must be declared with a C interoperable "
1889 : : "kind since common block %qs is BIND(C)",
1890 : : sym->name, sym->common_block->name,
1891 : 0 : sym->common_block->name);
1892 : 0 : gfc_clear_error ();
1893 : : }
1894 : : }
1895 : :
1896 : 254489 : sym->attr.implied_index = 0;
1897 : :
1898 : : /* Use the parameter expressions for a parameterized derived type. */
1899 : 254489 : if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1900 : 35373 : && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1901 : 762 : sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1902 : :
1903 : 254489 : if (sym->ts.type == BT_CLASS)
1904 : 10655 : return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1905 : :
1906 : : return true;
1907 : : }
1908 : :
1909 : :
1910 : : /* Set character constant to the given length. The constant will be padded or
1911 : : truncated. If we're inside an array constructor without a typespec, we
1912 : : additionally check that all elements have the same length; check_len -1
1913 : : means no checking. */
1914 : :
1915 : : void
1916 : 13198 : gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1917 : : gfc_charlen_t check_len)
1918 : : {
1919 : 13198 : gfc_char_t *s;
1920 : 13198 : gfc_charlen_t slen;
1921 : :
1922 : 13198 : if (expr->ts.type != BT_CHARACTER)
1923 : : return;
1924 : :
1925 : 13197 : if (expr->expr_type != EXPR_CONSTANT)
1926 : : {
1927 : 1 : gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1928 : 1 : return;
1929 : : }
1930 : :
1931 : 13196 : slen = expr->value.character.length;
1932 : 13196 : if (len != slen)
1933 : : {
1934 : 1589 : s = gfc_get_wide_string (len + 1);
1935 : 1589 : memcpy (s, expr->value.character.string,
1936 : 1589 : MIN (len, slen) * sizeof (gfc_char_t));
1937 : 1589 : if (len > slen)
1938 : 1418 : gfc_wide_memset (&s[slen], ' ', len - slen);
1939 : :
1940 : 1589 : if (warn_character_truncation && slen > len)
1941 : 1 : gfc_warning_now (OPT_Wcharacter_truncation,
1942 : : "CHARACTER expression at %L is being truncated "
1943 : : "(%ld/%ld)", &expr->where,
1944 : : (long) slen, (long) len);
1945 : :
1946 : : /* Apply the standard by 'hand' otherwise it gets cleared for
1947 : : initializers. */
1948 : 1589 : if (check_len != -1 && slen != check_len)
1949 : : {
1950 : 3 : if (!(gfc_option.allow_std & GFC_STD_GNU))
1951 : 0 : gfc_error_now ("The CHARACTER elements of the array constructor "
1952 : : "at %L must have the same length (%ld/%ld)",
1953 : : &expr->where, (long) slen,
1954 : : (long) check_len);
1955 : : else
1956 : 3 : gfc_notify_std (GFC_STD_LEGACY,
1957 : : "The CHARACTER elements of the array constructor "
1958 : : "at %L must have the same length (%ld/%ld)",
1959 : : &expr->where, (long) slen,
1960 : : (long) check_len);
1961 : : }
1962 : :
1963 : 1589 : s[len] = '\0';
1964 : 1589 : free (expr->value.character.string);
1965 : 1589 : expr->value.character.string = s;
1966 : 1589 : expr->value.character.length = len;
1967 : : /* If explicit representation was given, clear it
1968 : : as it is no longer needed after padding. */
1969 : 1589 : if (expr->representation.length)
1970 : : {
1971 : 45 : expr->representation.length = 0;
1972 : 45 : free (expr->representation.string);
1973 : 45 : expr->representation.string = NULL;
1974 : : }
1975 : : }
1976 : : }
1977 : :
1978 : :
1979 : : /* Function to create and update the enumerator history
1980 : : using the information passed as arguments.
1981 : : Pointer "max_enum" is also updated, to point to
1982 : : enum history node containing largest initializer.
1983 : :
1984 : : SYM points to the symbol node of enumerator.
1985 : : INIT points to its enumerator value. */
1986 : :
1987 : : static void
1988 : 543 : create_enum_history (gfc_symbol *sym, gfc_expr *init)
1989 : : {
1990 : 543 : enumerator_history *new_enum_history;
1991 : 543 : gcc_assert (sym != NULL && init != NULL);
1992 : :
1993 : 543 : new_enum_history = XCNEW (enumerator_history);
1994 : :
1995 : 543 : new_enum_history->sym = sym;
1996 : 543 : new_enum_history->initializer = init;
1997 : 543 : new_enum_history->next = NULL;
1998 : :
1999 : 543 : if (enum_history == NULL)
2000 : : {
2001 : 160 : enum_history = new_enum_history;
2002 : 160 : max_enum = enum_history;
2003 : : }
2004 : : else
2005 : : {
2006 : 383 : new_enum_history->next = enum_history;
2007 : 383 : enum_history = new_enum_history;
2008 : :
2009 : 383 : if (mpz_cmp (max_enum->initializer->value.integer,
2010 : 383 : new_enum_history->initializer->value.integer) < 0)
2011 : 381 : max_enum = new_enum_history;
2012 : : }
2013 : 543 : }
2014 : :
2015 : :
2016 : : /* Function to free enum kind history. */
2017 : :
2018 : : void
2019 : 175 : gfc_free_enum_history (void)
2020 : : {
2021 : 175 : enumerator_history *current = enum_history;
2022 : 175 : enumerator_history *next;
2023 : :
2024 : 718 : while (current != NULL)
2025 : : {
2026 : 543 : next = current->next;
2027 : 543 : free (current);
2028 : 543 : current = next;
2029 : : }
2030 : 175 : max_enum = NULL;
2031 : 175 : enum_history = NULL;
2032 : 175 : }
2033 : :
2034 : :
2035 : : /* Function to fix initializer character length if the length of the
2036 : : symbol or component is constant. */
2037 : :
2038 : : static bool
2039 : 2679 : fix_initializer_charlen (gfc_typespec *ts, gfc_expr *init)
2040 : : {
2041 : 2679 : if (!gfc_specification_expr (ts->u.cl->length))
2042 : : return false;
2043 : :
2044 : 2679 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
2045 : :
2046 : : /* resolve_charlen will complain later on if the length
2047 : : is too large. Just skip the initialization in that case. */
2048 : 2679 : if (mpz_cmp (ts->u.cl->length->value.integer,
2049 : 2679 : gfc_integer_kinds[k].huge) <= 0)
2050 : : {
2051 : 2678 : HOST_WIDE_INT len
2052 : 2678 : = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
2053 : :
2054 : 2678 : if (init->expr_type == EXPR_CONSTANT)
2055 : 1944 : gfc_set_constant_character_len (len, init, -1);
2056 : 734 : else if (init->expr_type == EXPR_ARRAY)
2057 : : {
2058 : 733 : gfc_constructor *cons;
2059 : :
2060 : : /* Build a new charlen to prevent simplification from
2061 : : deleting the length before it is resolved. */
2062 : 733 : init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2063 : 733 : init->ts.u.cl->length = gfc_copy_expr (ts->u.cl->length);
2064 : 733 : cons = gfc_constructor_first (init->value.constructor);
2065 : 4971 : for (; cons; cons = gfc_constructor_next (cons))
2066 : 3505 : gfc_set_constant_character_len (len, cons->expr, -1);
2067 : : }
2068 : : }
2069 : :
2070 : : return true;
2071 : : }
2072 : :
2073 : :
2074 : : /* Function called by variable_decl() that adds an initialization
2075 : : expression to a symbol. */
2076 : :
2077 : : static bool
2078 : 261974 : add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
2079 : : {
2080 : 261974 : symbol_attribute attr;
2081 : 261974 : gfc_symbol *sym;
2082 : 261974 : gfc_expr *init;
2083 : :
2084 : 261974 : init = *initp;
2085 : 261974 : if (find_special (name, &sym, false))
2086 : : return false;
2087 : :
2088 : 261974 : attr = sym->attr;
2089 : :
2090 : : /* If this symbol is confirming an implicit parameter type,
2091 : : then an initialization expression is not allowed. */
2092 : 261974 : if (attr.flavor == FL_PARAMETER && sym->value != NULL)
2093 : : {
2094 : 1 : if (*initp != NULL)
2095 : : {
2096 : 0 : gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
2097 : : sym->name);
2098 : 0 : return false;
2099 : : }
2100 : : else
2101 : : return true;
2102 : : }
2103 : :
2104 : 261973 : if (init == NULL)
2105 : : {
2106 : : /* An initializer is required for PARAMETER declarations. */
2107 : 230383 : if (attr.flavor == FL_PARAMETER)
2108 : : {
2109 : 1 : gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
2110 : 1 : return false;
2111 : : }
2112 : : }
2113 : : else
2114 : : {
2115 : : /* If a variable appears in a DATA block, it cannot have an
2116 : : initializer. */
2117 : 31590 : if (sym->attr.data)
2118 : : {
2119 : 0 : gfc_error ("Variable %qs at %C with an initializer already "
2120 : : "appears in a DATA statement", sym->name);
2121 : 0 : return false;
2122 : : }
2123 : :
2124 : : /* Check if the assignment can happen. This has to be put off
2125 : : until later for derived type variables and procedure pointers. */
2126 : 30474 : if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
2127 : 30451 : && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
2128 : 30401 : && !sym->attr.proc_pointer
2129 : 61905 : && !gfc_check_assign_symbol (sym, NULL, init))
2130 : : return false;
2131 : :
2132 : 31559 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
2133 : 3359 : && init->ts.type == BT_CHARACTER)
2134 : : {
2135 : : /* Update symbol character length according initializer. */
2136 : 3195 : if (!gfc_check_assign_symbol (sym, NULL, init))
2137 : : return false;
2138 : :
2139 : 3195 : if (sym->ts.u.cl->length == NULL)
2140 : : {
2141 : 832 : gfc_charlen_t clen;
2142 : : /* If there are multiple CHARACTER variables declared on the
2143 : : same line, we don't want them to share the same length. */
2144 : 832 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2145 : :
2146 : 832 : if (sym->attr.flavor == FL_PARAMETER)
2147 : : {
2148 : 823 : if (init->expr_type == EXPR_CONSTANT)
2149 : : {
2150 : 540 : clen = init->value.character.length;
2151 : 540 : sym->ts.u.cl->length
2152 : 540 : = gfc_get_int_expr (gfc_charlen_int_kind,
2153 : : NULL, clen);
2154 : : }
2155 : 283 : else if (init->expr_type == EXPR_ARRAY)
2156 : : {
2157 : 283 : if (init->ts.u.cl && init->ts.u.cl->length)
2158 : : {
2159 : 271 : const gfc_expr *length = init->ts.u.cl->length;
2160 : 271 : if (length->expr_type != EXPR_CONSTANT)
2161 : : {
2162 : 1 : gfc_error ("Cannot initialize parameter array "
2163 : : "at %L "
2164 : : "with variable length elements",
2165 : : &sym->declared_at);
2166 : 1 : return false;
2167 : : }
2168 : 270 : clen = mpz_get_si (length->value.integer);
2169 : 270 : }
2170 : 12 : else if (init->value.constructor)
2171 : : {
2172 : 12 : gfc_constructor *c;
2173 : 12 : c = gfc_constructor_first (init->value.constructor);
2174 : 12 : clen = c->expr->value.character.length;
2175 : : }
2176 : : else
2177 : 0 : gcc_unreachable ();
2178 : 282 : sym->ts.u.cl->length
2179 : 282 : = gfc_get_int_expr (gfc_charlen_int_kind,
2180 : : NULL, clen);
2181 : : }
2182 : 0 : else if (init->ts.u.cl && init->ts.u.cl->length)
2183 : 0 : sym->ts.u.cl->length =
2184 : 0 : gfc_copy_expr (init->ts.u.cl->length);
2185 : : }
2186 : : }
2187 : : /* Update initializer character length according to symbol. */
2188 : 2363 : else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2189 : 2363 : && !fix_initializer_charlen (&sym->ts, init))
2190 : : return false;
2191 : : }
2192 : :
2193 : 31558 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
2194 : 3743 : && sym->as->rank && init->rank && init->rank != sym->as->rank)
2195 : : {
2196 : 3 : gfc_error ("Rank mismatch of array at %L and its initializer "
2197 : : "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
2198 : 3 : return false;
2199 : : }
2200 : :
2201 : : /* If sym is implied-shape, set its upper bounds from init. */
2202 : 31555 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2203 : 3740 : && sym->as->type == AS_IMPLIED_SHAPE)
2204 : : {
2205 : 1030 : int dim;
2206 : :
2207 : 1030 : if (init->rank == 0)
2208 : : {
2209 : 1 : gfc_error ("Cannot initialize implied-shape array at %L"
2210 : : " with scalar", &sym->declared_at);
2211 : 1 : return false;
2212 : : }
2213 : :
2214 : : /* The shape may be NULL for EXPR_ARRAY, set it. */
2215 : 1029 : if (init->shape == NULL)
2216 : : {
2217 : 5 : if (init->expr_type != EXPR_ARRAY)
2218 : : {
2219 : 2 : gfc_error ("Bad shape of initializer at %L", &init->where);
2220 : 2 : return false;
2221 : : }
2222 : :
2223 : 3 : init->shape = gfc_get_shape (1);
2224 : 3 : if (!gfc_array_size (init, &init->shape[0]))
2225 : : {
2226 : 1 : gfc_error ("Cannot determine shape of initializer at %L",
2227 : : &init->where);
2228 : 1 : free (init->shape);
2229 : 1 : init->shape = NULL;
2230 : 1 : return false;
2231 : : }
2232 : : }
2233 : :
2234 : 2153 : for (dim = 0; dim < sym->as->rank; ++dim)
2235 : : {
2236 : 1128 : int k;
2237 : 1128 : gfc_expr *e, *lower;
2238 : :
2239 : 1128 : lower = sym->as->lower[dim];
2240 : :
2241 : : /* If the lower bound is an array element from another
2242 : : parameterized array, then it is marked with EXPR_VARIABLE and
2243 : : is an initialization expression. Try to reduce it. */
2244 : 1128 : if (lower->expr_type == EXPR_VARIABLE)
2245 : 7 : gfc_reduce_init_expr (lower);
2246 : :
2247 : 1128 : if (lower->expr_type == EXPR_CONSTANT)
2248 : : {
2249 : : /* All dimensions must be without upper bound. */
2250 : 1127 : gcc_assert (!sym->as->upper[dim]);
2251 : :
2252 : 1127 : k = lower->ts.kind;
2253 : 1127 : e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2254 : 1127 : mpz_add (e->value.integer, lower->value.integer,
2255 : 1127 : init->shape[dim]);
2256 : 1127 : mpz_sub_ui (e->value.integer, e->value.integer, 1);
2257 : 1127 : sym->as->upper[dim] = e;
2258 : : }
2259 : : else
2260 : : {
2261 : 1 : gfc_error ("Non-constant lower bound in implied-shape"
2262 : : " declaration at %L", &lower->where);
2263 : 1 : return false;
2264 : : }
2265 : : }
2266 : :
2267 : 1025 : sym->as->type = AS_EXPLICIT;
2268 : : }
2269 : :
2270 : : /* Ensure that explicit bounds are simplified. */
2271 : 31550 : if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2272 : 3735 : && sym->as->type == AS_EXPLICIT)
2273 : : {
2274 : 8302 : for (int dim = 0; dim < sym->as->rank; ++dim)
2275 : : {
2276 : 4579 : gfc_expr *e;
2277 : :
2278 : 4579 : e = sym->as->lower[dim];
2279 : 4579 : if (e->expr_type != EXPR_CONSTANT)
2280 : 12 : gfc_reduce_init_expr (e);
2281 : :
2282 : 4579 : e = sym->as->upper[dim];
2283 : 4579 : if (e->expr_type != EXPR_CONSTANT)
2284 : 96 : gfc_reduce_init_expr (e);
2285 : : }
2286 : : }
2287 : :
2288 : : /* Need to check if the expression we initialized this
2289 : : to was one of the iso_c_binding named constants. If so,
2290 : : and we're a parameter (constant), let it be iso_c.
2291 : : For example:
2292 : : integer(c_int), parameter :: my_int = c_int
2293 : : integer(my_int) :: my_int_2
2294 : : If we mark my_int as iso_c (since we can see it's value
2295 : : is equal to one of the named constants), then my_int_2
2296 : : will be considered C interoperable. */
2297 : 31550 : if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2298 : : {
2299 : 27079 : sym->ts.is_iso_c |= init->ts.is_iso_c;
2300 : 27079 : sym->ts.is_c_interop |= init->ts.is_c_interop;
2301 : : /* attr bits needed for module files. */
2302 : 27079 : sym->attr.is_iso_c |= init->ts.is_iso_c;
2303 : 27079 : sym->attr.is_c_interop |= init->ts.is_c_interop;
2304 : 27079 : if (init->ts.is_iso_c)
2305 : 108 : sym->ts.f90_type = init->ts.f90_type;
2306 : : }
2307 : :
2308 : : /* Catch the case: type(t), parameter :: x = z'1'. */
2309 : 31550 : if (sym->ts.type == BT_DERIVED && init->ts.type == BT_BOZ)
2310 : : {
2311 : 1 : gfc_error ("Entity %qs at %L is incompatible with a BOZ "
2312 : : "literal constant", name, &sym->declared_at);
2313 : 1 : return false;
2314 : : }
2315 : :
2316 : : /* Add initializer. Make sure we keep the ranks sane. */
2317 : 31549 : if (sym->attr.dimension && init->rank == 0)
2318 : : {
2319 : 1234 : mpz_t size;
2320 : 1234 : gfc_expr *array;
2321 : 1234 : int n;
2322 : 1234 : if (sym->attr.flavor == FL_PARAMETER
2323 : 438 : && gfc_is_constant_expr (init)
2324 : 438 : && (init->expr_type == EXPR_CONSTANT
2325 : 31 : || init->expr_type == EXPR_STRUCTURE)
2326 : 1672 : && spec_size (sym->as, &size))
2327 : : {
2328 : 434 : array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2329 : : &init->where);
2330 : 434 : if (init->ts.type == BT_DERIVED)
2331 : 31 : array->ts.u.derived = init->ts.u.derived;
2332 : 67549 : for (n = 0; n < (int)mpz_get_si (size); n++)
2333 : 133937 : gfc_constructor_append_expr (&array->value.constructor,
2334 : : n == 0
2335 : : ? init
2336 : 66822 : : gfc_copy_expr (init),
2337 : : &init->where);
2338 : :
2339 : 434 : array->shape = gfc_get_shape (sym->as->rank);
2340 : 994 : for (n = 0; n < sym->as->rank; n++)
2341 : 560 : spec_dimen_size (sym->as, n, &array->shape[n]);
2342 : :
2343 : 434 : init = array;
2344 : 434 : mpz_clear (size);
2345 : : }
2346 : 1234 : init->rank = sym->as->rank;
2347 : 1234 : init->corank = sym->as->corank;
2348 : : }
2349 : :
2350 : 31549 : sym->value = init;
2351 : 31549 : if (sym->attr.save == SAVE_NONE)
2352 : 27206 : sym->attr.save = SAVE_IMPLICIT;
2353 : 31549 : *initp = NULL;
2354 : : }
2355 : :
2356 : : return true;
2357 : : }
2358 : :
2359 : :
2360 : : /* Function called by variable_decl() that adds a name to a structure
2361 : : being built. */
2362 : :
2363 : : static bool
2364 : 17060 : build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2365 : : gfc_array_spec **as)
2366 : : {
2367 : 17060 : gfc_state_data *s;
2368 : 17060 : gfc_component *c;
2369 : :
2370 : : /* F03:C438/C439. If the current symbol is of the same derived type that we're
2371 : : constructing, it must have the pointer attribute. */
2372 : 17060 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2373 : 3150 : && current_ts.u.derived == gfc_current_block ()
2374 : 260 : && current_attr.pointer == 0)
2375 : : {
2376 : 99 : if (current_attr.allocatable
2377 : 99 : && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2378 : : "must have the POINTER attribute"))
2379 : : {
2380 : : return false;
2381 : : }
2382 : 98 : else if (current_attr.allocatable == 0)
2383 : : {
2384 : 0 : gfc_error ("Component at %C must have the POINTER attribute");
2385 : 0 : return false;
2386 : : }
2387 : : }
2388 : :
2389 : : /* F03:C437. */
2390 : 17059 : if (current_ts.type == BT_CLASS
2391 : 806 : && !(current_attr.pointer || current_attr.allocatable))
2392 : : {
2393 : 5 : gfc_error ("Component %qs with CLASS at %C must be allocatable "
2394 : : "or pointer", name);
2395 : 5 : return false;
2396 : : }
2397 : :
2398 : 17054 : if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2399 : : {
2400 : 0 : if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2401 : : {
2402 : 0 : gfc_error ("Array component of structure at %C must have explicit "
2403 : : "or deferred shape");
2404 : 0 : return false;
2405 : : }
2406 : : }
2407 : :
2408 : : /* If we are in a nested union/map definition, gfc_add_component will not
2409 : : properly find repeated components because:
2410 : : (i) gfc_add_component does a flat search, where components of unions
2411 : : and maps are implicity chained so nested components may conflict.
2412 : : (ii) Unions and maps are not linked as components of their parent
2413 : : structures until after they are parsed.
2414 : : For (i) we use gfc_find_component which searches recursively, and for (ii)
2415 : : we search each block directly from the parse stack until we find the top
2416 : : level structure. */
2417 : :
2418 : 17054 : s = gfc_state_stack;
2419 : 17054 : if (s->state == COMP_UNION || s->state == COMP_MAP)
2420 : : {
2421 : 1434 : while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2422 : : {
2423 : 1434 : c = gfc_find_component (s->sym, name, true, true, NULL);
2424 : 1434 : if (c != NULL)
2425 : : {
2426 : 0 : gfc_error_now ("Component %qs at %C already declared at %L",
2427 : : name, &c->loc);
2428 : 0 : return false;
2429 : : }
2430 : : /* Break after we've searched the entire chain. */
2431 : 1434 : if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2432 : : break;
2433 : 1000 : s = s->previous;
2434 : : }
2435 : : }
2436 : :
2437 : 17054 : if (!gfc_add_component (gfc_current_block(), name, &c))
2438 : : return false;
2439 : :
2440 : 17048 : c->ts = current_ts;
2441 : 17048 : if (c->ts.type == BT_CHARACTER)
2442 : 1905 : c->ts.u.cl = cl;
2443 : :
2444 : 17048 : if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2445 : 13904 : && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2446 : 2053 : && saved_kind_expr != NULL)
2447 : 162 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
2448 : :
2449 : 17048 : c->attr = current_attr;
2450 : :
2451 : 17048 : c->initializer = *init;
2452 : 17048 : *init = NULL;
2453 : :
2454 : : /* Update initializer character length according to component. */
2455 : 1905 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length
2456 : 1512 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT
2457 : 1451 : && c->initializer && c->initializer->ts.type == BT_CHARACTER
2458 : 17367 : && !fix_initializer_charlen (&c->ts, c->initializer))
2459 : : return false;
2460 : :
2461 : 17048 : c->as = *as;
2462 : 17048 : if (c->as != NULL)
2463 : : {
2464 : 4284 : if (c->as->corank)
2465 : 97 : c->attr.codimension = 1;
2466 : 4284 : if (c->as->rank)
2467 : 4215 : c->attr.dimension = 1;
2468 : : }
2469 : 17048 : *as = NULL;
2470 : :
2471 : 17048 : gfc_apply_init (&c->ts, &c->attr, c->initializer);
2472 : :
2473 : : /* Check array components. */
2474 : 17048 : if (!c->attr.dimension)
2475 : 12833 : goto scalar;
2476 : :
2477 : 4215 : if (c->attr.pointer)
2478 : : {
2479 : 672 : if (c->as->type != AS_DEFERRED)
2480 : : {
2481 : 5 : gfc_error ("Pointer array component of structure at %C must have a "
2482 : : "deferred shape");
2483 : 5 : return false;
2484 : : }
2485 : : }
2486 : 3543 : else if (c->attr.allocatable)
2487 : : {
2488 : 2011 : const char *err = G_("Allocatable component of structure at %C must have "
2489 : : "a deferred shape");
2490 : 2011 : if (c->as->type != AS_DEFERRED)
2491 : : {
2492 : 14 : if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
2493 : : {
2494 : : /* Issue an immediate error and allow this component to pass for
2495 : : the sake of clean error recovery. Set the error flag for the
2496 : : containing derived type so that finalizers are not built. */
2497 : 4 : gfc_error_now (err);
2498 : 4 : s->sym->error = 1;
2499 : 4 : c->as->type = AS_DEFERRED;
2500 : : }
2501 : : else
2502 : : {
2503 : 10 : gfc_error (err);
2504 : 10 : return false;
2505 : : }
2506 : : }
2507 : : }
2508 : : else
2509 : : {
2510 : 1532 : if (c->as->type != AS_EXPLICIT)
2511 : : {
2512 : 7 : gfc_error ("Array component of structure at %C must have an "
2513 : : "explicit shape");
2514 : 7 : return false;
2515 : : }
2516 : : }
2517 : :
2518 : 1525 : scalar:
2519 : 17026 : if (c->ts.type == BT_CLASS)
2520 : 798 : return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2521 : :
2522 : 16228 : if (c->attr.pdt_kind || c->attr.pdt_len)
2523 : : {
2524 : 471 : gfc_symbol *sym;
2525 : 471 : gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2526 : : 0, &sym);
2527 : 471 : if (sym == NULL)
2528 : : {
2529 : 0 : gfc_error ("Type parameter %qs at %C has no corresponding entry "
2530 : : "in the type parameter name list at %L",
2531 : 0 : c->name, &gfc_current_block ()->declared_at);
2532 : 0 : return false;
2533 : : }
2534 : 471 : sym->ts = c->ts;
2535 : 471 : sym->attr.pdt_kind = c->attr.pdt_kind;
2536 : 471 : sym->attr.pdt_len = c->attr.pdt_len;
2537 : 471 : if (c->initializer)
2538 : 172 : sym->value = gfc_copy_expr (c->initializer);
2539 : 471 : sym->attr.flavor = FL_VARIABLE;
2540 : : }
2541 : :
2542 : 16228 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2543 : 2343 : && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2544 : 77 : && decl_type_param_list)
2545 : 77 : c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2546 : :
2547 : : return true;
2548 : : }
2549 : :
2550 : :
2551 : : /* Match a 'NULL()', and possibly take care of some side effects. */
2552 : :
2553 : : match
2554 : 1662 : gfc_match_null (gfc_expr **result)
2555 : : {
2556 : 1662 : gfc_symbol *sym;
2557 : 1662 : match m, m2 = MATCH_NO;
2558 : :
2559 : 1662 : if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2560 : : return MATCH_ERROR;
2561 : :
2562 : 1662 : if (m == MATCH_NO)
2563 : : {
2564 : 505 : locus old_loc;
2565 : 505 : char name[GFC_MAX_SYMBOL_LEN + 1];
2566 : :
2567 : 505 : if ((m2 = gfc_match (" null (")) != MATCH_YES)
2568 : 499 : return m2;
2569 : :
2570 : 6 : old_loc = gfc_current_locus;
2571 : 6 : if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2572 : : return MATCH_ERROR;
2573 : 6 : if (m2 != MATCH_YES
2574 : 6 : && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2575 : : return MATCH_ERROR;
2576 : 6 : if (m2 == MATCH_NO)
2577 : : {
2578 : 0 : gfc_current_locus = old_loc;
2579 : 0 : return MATCH_NO;
2580 : : }
2581 : : }
2582 : :
2583 : : /* The NULL symbol now has to be/become an intrinsic function. */
2584 : 1163 : if (gfc_get_symbol ("null", NULL, &sym))
2585 : : {
2586 : 0 : gfc_error ("NULL() initialization at %C is ambiguous");
2587 : 0 : return MATCH_ERROR;
2588 : : }
2589 : :
2590 : 1163 : gfc_intrinsic_symbol (sym);
2591 : :
2592 : 1163 : if (sym->attr.proc != PROC_INTRINSIC
2593 : 813 : && !(sym->attr.use_assoc && sym->attr.intrinsic)
2594 : 1975 : && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2595 : 812 : || !gfc_add_function (&sym->attr, sym->name, NULL)))
2596 : 0 : return MATCH_ERROR;
2597 : :
2598 : 1163 : *result = gfc_get_null_expr (&gfc_current_locus);
2599 : :
2600 : : /* Invalid per F2008, C512. */
2601 : 1163 : if (m2 == MATCH_YES)
2602 : : {
2603 : 6 : gfc_error ("NULL() initialization at %C may not have MOLD");
2604 : 6 : return MATCH_ERROR;
2605 : : }
2606 : :
2607 : : return MATCH_YES;
2608 : : }
2609 : :
2610 : :
2611 : : /* Match the initialization expr for a data pointer or procedure pointer. */
2612 : :
2613 : : static match
2614 : 1326 : match_pointer_init (gfc_expr **init, int procptr)
2615 : : {
2616 : 1326 : match m;
2617 : :
2618 : 1326 : if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2619 : : {
2620 : 1 : gfc_error ("Initialization of pointer at %C is not allowed in "
2621 : : "a PURE procedure");
2622 : 1 : return MATCH_ERROR;
2623 : : }
2624 : 1325 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2625 : :
2626 : : /* Match NULL() initialization. */
2627 : 1325 : m = gfc_match_null (init);
2628 : 1325 : if (m != MATCH_NO)
2629 : : return m;
2630 : :
2631 : : /* Match non-NULL initialization. */
2632 : 170 : gfc_matching_ptr_assignment = !procptr;
2633 : 170 : gfc_matching_procptr_assignment = procptr;
2634 : 170 : m = gfc_match_rvalue (init);
2635 : 170 : gfc_matching_ptr_assignment = 0;
2636 : 170 : gfc_matching_procptr_assignment = 0;
2637 : 170 : if (m == MATCH_ERROR)
2638 : : return MATCH_ERROR;
2639 : 169 : else if (m == MATCH_NO)
2640 : : {
2641 : 2 : gfc_error ("Error in pointer initialization at %C");
2642 : 2 : return MATCH_ERROR;
2643 : : }
2644 : :
2645 : 167 : if (!procptr && !gfc_resolve_expr (*init))
2646 : : return MATCH_ERROR;
2647 : :
2648 : 166 : if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2649 : : "initialization at %C"))
2650 : : return MATCH_ERROR;
2651 : :
2652 : : return MATCH_YES;
2653 : : }
2654 : :
2655 : :
2656 : : static bool
2657 : 281319 : check_function_name (char *name)
2658 : : {
2659 : : /* In functions that have a RESULT variable defined, the function name always
2660 : : refers to function calls. Therefore, the name is not allowed to appear in
2661 : : specification statements. When checking this, be careful about
2662 : : 'hidden' procedure pointer results ('ppr@'). */
2663 : :
2664 : 281319 : if (gfc_current_state () == COMP_FUNCTION)
2665 : : {
2666 : 45076 : gfc_symbol *block = gfc_current_block ();
2667 : 45076 : if (block && block->result && block->result != block
2668 : 14840 : && strcmp (block->result->name, "ppr@") != 0
2669 : 14781 : && strcmp (block->name, name) == 0)
2670 : : {
2671 : 9 : gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2672 : : "from appearing in a specification statement",
2673 : : block->result->name, &block->result->declared_at, name);
2674 : 9 : return false;
2675 : : }
2676 : : }
2677 : :
2678 : : return true;
2679 : : }
2680 : :
2681 : :
2682 : : /* Match a variable name with an optional initializer. When this
2683 : : subroutine is called, a variable is expected to be parsed next.
2684 : : Depending on what is happening at the moment, updates either the
2685 : : symbol table or the current interface. */
2686 : :
2687 : : static match
2688 : 271278 : variable_decl (int elem)
2689 : : {
2690 : 271278 : char name[GFC_MAX_SYMBOL_LEN + 1];
2691 : 271278 : static unsigned int fill_id = 0;
2692 : 271278 : gfc_expr *initializer, *char_len;
2693 : 271278 : gfc_array_spec *as;
2694 : 271278 : gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2695 : 271278 : gfc_charlen *cl;
2696 : 271278 : bool cl_deferred;
2697 : 271278 : locus var_locus;
2698 : 271278 : match m;
2699 : 271278 : bool t;
2700 : 271278 : gfc_symbol *sym;
2701 : 271278 : char c;
2702 : :
2703 : 271278 : initializer = NULL;
2704 : 271278 : as = NULL;
2705 : 271278 : cp_as = NULL;
2706 : :
2707 : : /* When we get here, we've just matched a list of attributes and
2708 : : maybe a type and a double colon. The next thing we expect to see
2709 : : is the name of the symbol. */
2710 : :
2711 : : /* If we are parsing a structure with legacy support, we allow the symbol
2712 : : name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2713 : 271278 : m = MATCH_NO;
2714 : 271278 : gfc_gobble_whitespace ();
2715 : 271278 : var_locus = gfc_current_locus;
2716 : 271278 : c = gfc_peek_ascii_char ();
2717 : 271278 : if (c == '%')
2718 : : {
2719 : 12 : gfc_next_ascii_char (); /* Burn % character. */
2720 : 12 : m = gfc_match ("fill");
2721 : 12 : if (m == MATCH_YES)
2722 : : {
2723 : 11 : if (gfc_current_state () != COMP_STRUCTURE)
2724 : : {
2725 : 2 : if (flag_dec_structure)
2726 : 1 : gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2727 : : else
2728 : 1 : gfc_error ("%qs at %C is a DEC extension, enable with "
2729 : : "%<-fdec-structure%>", "%FILL");
2730 : 2 : m = MATCH_ERROR;
2731 : 2 : goto cleanup;
2732 : : }
2733 : :
2734 : 9 : if (attr_seen)
2735 : : {
2736 : 1 : gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2737 : 1 : m = MATCH_ERROR;
2738 : 1 : goto cleanup;
2739 : : }
2740 : :
2741 : : /* %FILL components are given invalid fortran names. */
2742 : 8 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2743 : : }
2744 : : else
2745 : : {
2746 : 1 : gfc_error ("Invalid character %qc in variable name at %C", c);
2747 : 1 : return MATCH_ERROR;
2748 : : }
2749 : : }
2750 : : else
2751 : : {
2752 : 271266 : m = gfc_match_name (name);
2753 : 271265 : if (m != MATCH_YES)
2754 : 10 : goto cleanup;
2755 : : }
2756 : :
2757 : : /* Now we could see the optional array spec. or character length. */
2758 : 271263 : m = gfc_match_array_spec (&as, true, true);
2759 : 271262 : if (m == MATCH_ERROR)
2760 : 56 : goto cleanup;
2761 : :
2762 : 271206 : if (m == MATCH_NO)
2763 : 212044 : as = gfc_copy_array_spec (current_as);
2764 : 59162 : else if (current_as
2765 : 59162 : && !merge_array_spec (current_as, as, true))
2766 : : {
2767 : 4 : m = MATCH_ERROR;
2768 : 4 : goto cleanup;
2769 : : }
2770 : :
2771 : 271202 : var_locus = gfc_get_location_range (NULL, 0, &var_locus, 1,
2772 : : &gfc_current_locus);
2773 : 271202 : if (flag_cray_pointer)
2774 : 3063 : cp_as = gfc_copy_array_spec (as);
2775 : :
2776 : : /* At this point, we know for sure if the symbol is PARAMETER and can thus
2777 : : determine (and check) whether it can be implied-shape. If it
2778 : : was parsed as assumed-size, change it because PARAMETERs cannot
2779 : : be assumed-size.
2780 : :
2781 : : An explicit-shape-array cannot appear under several conditions.
2782 : : That check is done here as well. */
2783 : 271202 : if (as)
2784 : : {
2785 : 81560 : if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2786 : : {
2787 : 2 : m = MATCH_ERROR;
2788 : 2 : gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2789 : : name, &var_locus);
2790 : 2 : goto cleanup;
2791 : : }
2792 : :
2793 : 81558 : if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2794 : 6447 : && current_attr.flavor == FL_PARAMETER)
2795 : 982 : as->type = AS_IMPLIED_SHAPE;
2796 : :
2797 : 81558 : if (as->type == AS_IMPLIED_SHAPE
2798 : 81558 : && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2799 : : &var_locus))
2800 : : {
2801 : 1 : m = MATCH_ERROR;
2802 : 1 : goto cleanup;
2803 : : }
2804 : :
2805 : 81557 : gfc_seen_div0 = false;
2806 : :
2807 : : /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2808 : : constant expressions shall appear only in a subprogram, derived
2809 : : type definition, BLOCK construct, or interface body. */
2810 : 81557 : if (as->type == AS_EXPLICIT
2811 : 40976 : && gfc_current_state () != COMP_BLOCK
2812 : : && gfc_current_state () != COMP_DERIVED
2813 : : && gfc_current_state () != COMP_FUNCTION
2814 : : && gfc_current_state () != COMP_INTERFACE
2815 : : && gfc_current_state () != COMP_SUBROUTINE)
2816 : : {
2817 : : gfc_expr *e;
2818 : 48953 : bool not_constant = false;
2819 : :
2820 : 48953 : for (int i = 0; i < as->rank; i++)
2821 : : {
2822 : 27929 : e = gfc_copy_expr (as->lower[i]);
2823 : 27929 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2824 : : {
2825 : 0 : m = MATCH_ERROR;
2826 : 0 : goto cleanup;
2827 : : }
2828 : :
2829 : 27929 : gfc_simplify_expr (e, 0);
2830 : 27929 : if (e && (e->expr_type != EXPR_CONSTANT))
2831 : : {
2832 : : not_constant = true;
2833 : : break;
2834 : : }
2835 : 27929 : gfc_free_expr (e);
2836 : :
2837 : 27929 : e = gfc_copy_expr (as->upper[i]);
2838 : 27929 : if (!gfc_resolve_expr (e) && gfc_seen_div0)
2839 : : {
2840 : 4 : m = MATCH_ERROR;
2841 : 4 : goto cleanup;
2842 : : }
2843 : :
2844 : 27925 : gfc_simplify_expr (e, 0);
2845 : 27925 : if (e && (e->expr_type != EXPR_CONSTANT))
2846 : : {
2847 : : not_constant = true;
2848 : : break;
2849 : : }
2850 : 27912 : gfc_free_expr (e);
2851 : : }
2852 : :
2853 : 21037 : if (not_constant && e->ts.type != BT_INTEGER)
2854 : : {
2855 : 4 : gfc_error ("Explicit array shape at %C must be constant of "
2856 : : "INTEGER type and not %s type",
2857 : : gfc_basic_typename (e->ts.type));
2858 : 4 : m = MATCH_ERROR;
2859 : 4 : goto cleanup;
2860 : : }
2861 : 9 : if (not_constant)
2862 : : {
2863 : 9 : gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2864 : 9 : m = MATCH_ERROR;
2865 : 9 : goto cleanup;
2866 : : }
2867 : : }
2868 : 81540 : if (as->type == AS_EXPLICIT)
2869 : : {
2870 : 98343 : for (int i = 0; i < as->rank; i++)
2871 : : {
2872 : 57384 : gfc_expr *e, *n;
2873 : 57384 : e = as->lower[i];
2874 : 57384 : if (e->expr_type != EXPR_CONSTANT)
2875 : : {
2876 : 452 : n = gfc_copy_expr (e);
2877 : 452 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2878 : : {
2879 : 0 : m = MATCH_ERROR;
2880 : 0 : goto cleanup;
2881 : : }
2882 : :
2883 : 452 : if (n->expr_type == EXPR_CONSTANT)
2884 : 22 : gfc_replace_expr (e, n);
2885 : : else
2886 : 430 : gfc_free_expr (n);
2887 : : }
2888 : 57384 : e = as->upper[i];
2889 : 57384 : if (e->expr_type != EXPR_CONSTANT)
2890 : : {
2891 : 6540 : n = gfc_copy_expr (e);
2892 : 6540 : if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2893 : : {
2894 : 0 : m = MATCH_ERROR;
2895 : 0 : goto cleanup;
2896 : : }
2897 : :
2898 : 6540 : if (n->expr_type == EXPR_CONSTANT)
2899 : 45 : gfc_replace_expr (e, n);
2900 : : else
2901 : 6495 : gfc_free_expr (n);
2902 : : }
2903 : : /* For an explicit-shape spec with constant bounds, ensure
2904 : : that the effective upper bound is not lower than the
2905 : : respective lower bound minus one. Otherwise adjust it so
2906 : : that the extent is trivially derived to be zero. */
2907 : 57384 : if (as->lower[i]->expr_type == EXPR_CONSTANT
2908 : 56954 : && as->upper[i]->expr_type == EXPR_CONSTANT
2909 : 50883 : && as->lower[i]->ts.type == BT_INTEGER
2910 : 50883 : && as->upper[i]->ts.type == BT_INTEGER
2911 : 50878 : && mpz_cmp (as->upper[i]->value.integer,
2912 : 50878 : as->lower[i]->value.integer) < 0)
2913 : 1205 : mpz_sub_ui (as->upper[i]->value.integer,
2914 : : as->lower[i]->value.integer, 1);
2915 : : }
2916 : : }
2917 : : }
2918 : :
2919 : 271182 : char_len = NULL;
2920 : 271182 : cl = NULL;
2921 : 271182 : cl_deferred = false;
2922 : :
2923 : 271182 : if (current_ts.type == BT_CHARACTER)
2924 : : {
2925 : 30268 : switch (match_char_length (&char_len, &cl_deferred, false))
2926 : : {
2927 : 435 : case MATCH_YES:
2928 : 435 : cl = gfc_new_charlen (gfc_current_ns, NULL);
2929 : :
2930 : 435 : cl->length = char_len;
2931 : 435 : break;
2932 : :
2933 : : /* Non-constant lengths need to be copied after the first
2934 : : element. Also copy assumed lengths. */
2935 : 29832 : case MATCH_NO:
2936 : 29832 : if (elem > 1
2937 : 3814 : && (current_ts.u.cl->length == NULL
2938 : 2627 : || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2939 : : {
2940 : 1242 : cl = gfc_new_charlen (gfc_current_ns, NULL);
2941 : 1242 : cl->length = gfc_copy_expr (current_ts.u.cl->length);
2942 : : }
2943 : : else
2944 : 28590 : cl = current_ts.u.cl;
2945 : :
2946 : 29832 : cl_deferred = current_ts.deferred;
2947 : :
2948 : 29832 : break;
2949 : :
2950 : 1 : case MATCH_ERROR:
2951 : 1 : goto cleanup;
2952 : : }
2953 : : }
2954 : :
2955 : : /* The dummy arguments and result of the abbreviated form of MODULE
2956 : : PROCEDUREs, used in SUBMODULES should not be redefined. */
2957 : 271181 : if (gfc_current_ns->proc_name
2958 : 266706 : && gfc_current_ns->proc_name->abr_modproc_decl)
2959 : : {
2960 : 44 : gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2961 : 44 : if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2962 : : {
2963 : 2 : m = MATCH_ERROR;
2964 : 2 : gfc_error ("%qs at %L is a redefinition of the declaration "
2965 : : "in the corresponding interface for MODULE "
2966 : : "PROCEDURE %qs", sym->name, &var_locus,
2967 : 2 : gfc_current_ns->proc_name->name);
2968 : 2 : goto cleanup;
2969 : : }
2970 : : }
2971 : :
2972 : : /* %FILL components may not have initializers. */
2973 : 271179 : if (startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2974 : : {
2975 : 1 : gfc_error ("%qs entity cannot have an initializer at %L", "%FILL",
2976 : : &var_locus);
2977 : 1 : m = MATCH_ERROR;
2978 : 1 : goto cleanup;
2979 : : }
2980 : :
2981 : : /* If this symbol has already shown up in a Cray Pointer declaration,
2982 : : and this is not a component declaration,
2983 : : then we want to set the type & bail out. */
2984 : 271178 : if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2985 : : {
2986 : 2959 : gfc_find_symbol (name, gfc_current_ns, 0, &sym);
2987 : 2959 : if (sym != NULL && sym->attr.cray_pointee)
2988 : : {
2989 : 101 : m = MATCH_YES;
2990 : 101 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
2991 : : {
2992 : 1 : m = MATCH_ERROR;
2993 : 1 : goto cleanup;
2994 : : }
2995 : :
2996 : : /* Check to see if we have an array specification. */
2997 : 100 : if (cp_as != NULL)
2998 : : {
2999 : 49 : if (sym->as != NULL)
3000 : : {
3001 : 1 : gfc_error ("Duplicate array spec for Cray pointee at %L", &var_locus);
3002 : 1 : gfc_free_array_spec (cp_as);
3003 : 1 : m = MATCH_ERROR;
3004 : 1 : goto cleanup;
3005 : : }
3006 : : else
3007 : : {
3008 : 48 : if (!gfc_set_array_spec (sym, cp_as, &var_locus))
3009 : 0 : gfc_internal_error ("Cannot set pointee array spec.");
3010 : :
3011 : : /* Fix the array spec. */
3012 : 48 : m = gfc_mod_pointee_as (sym->as);
3013 : 48 : if (m == MATCH_ERROR)
3014 : 0 : goto cleanup;
3015 : : }
3016 : : }
3017 : 99 : goto cleanup;
3018 : : }
3019 : : else
3020 : : {
3021 : 2858 : gfc_free_array_spec (cp_as);
3022 : : }
3023 : : }
3024 : :
3025 : : /* Procedure pointer as function result. */
3026 : 271077 : if (gfc_current_state () == COMP_FUNCTION
3027 : 43716 : && strcmp ("ppr@", gfc_current_block ()->name) == 0
3028 : 25 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
3029 : 7 : strcpy (name, "ppr@");
3030 : :
3031 : 271077 : if (gfc_current_state () == COMP_FUNCTION
3032 : 43716 : && strcmp (name, gfc_current_block ()->name) == 0
3033 : 7476 : && gfc_current_block ()->result
3034 : 7476 : && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
3035 : 16 : strcpy (name, "ppr@");
3036 : :
3037 : : /* OK, we've successfully matched the declaration. Now put the
3038 : : symbol in the current namespace, because it might be used in the
3039 : : optional initialization expression for this symbol, e.g. this is
3040 : : perfectly legal:
3041 : :
3042 : : integer, parameter :: i = huge(i)
3043 : :
3044 : : This is only true for parameters or variables of a basic type.
3045 : : For components of derived types, it is not true, so we don't
3046 : : create a symbol for those yet. If we fail to create the symbol,
3047 : : bail out. */
3048 : 271077 : if (!gfc_comp_struct (gfc_current_state ())
3049 : 253988 : && !build_sym (name, elem, cl, cl_deferred, &as, &var_locus))
3050 : : {
3051 : 47 : m = MATCH_ERROR;
3052 : 47 : goto cleanup;
3053 : : }
3054 : :
3055 : 271030 : if (!check_function_name (name))
3056 : : {
3057 : 0 : m = MATCH_ERROR;
3058 : 0 : goto cleanup;
3059 : : }
3060 : :
3061 : : /* We allow old-style initializations of the form
3062 : : integer i /2/, j(4) /3*3, 1/
3063 : : (if no colon has been seen). These are different from data
3064 : : statements in that initializers are only allowed to apply to the
3065 : : variable immediately preceding, i.e.
3066 : : integer i, j /1, 2/
3067 : : is not allowed. Therefore we have to do some work manually, that
3068 : : could otherwise be left to the matchers for DATA statements. */
3069 : :
3070 : 271030 : if (!colon_seen && gfc_match (" /") == MATCH_YES)
3071 : : {
3072 : 146 : if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
3073 : : "initialization at %C"))
3074 : : return MATCH_ERROR;
3075 : :
3076 : : /* Allow old style initializations for components of STRUCTUREs and MAPs
3077 : : but not components of derived types. */
3078 : 146 : else if (gfc_current_state () == COMP_DERIVED)
3079 : : {
3080 : 2 : gfc_error ("Invalid old style initialization for derived type "
3081 : : "component at %C");
3082 : 2 : m = MATCH_ERROR;
3083 : 2 : goto cleanup;
3084 : : }
3085 : :
3086 : : /* For structure components, read the initializer as a special
3087 : : expression and let the rest of this function apply the initializer
3088 : : as usual. */
3089 : 144 : else if (gfc_comp_struct (gfc_current_state ()))
3090 : : {
3091 : 74 : m = match_clist_expr (&initializer, ¤t_ts, as);
3092 : 74 : if (m == MATCH_NO)
3093 : : gfc_error ("Syntax error in old style initialization of %s at %C",
3094 : : name);
3095 : 74 : if (m != MATCH_YES)
3096 : 14 : goto cleanup;
3097 : : }
3098 : :
3099 : : /* Otherwise we treat the old style initialization just like a
3100 : : DATA declaration for the current variable. */
3101 : : else
3102 : 70 : return match_old_style_init (name);
3103 : : }
3104 : :
3105 : : /* The double colon must be present in order to have initializers.
3106 : : Otherwise the statement is ambiguous with an assignment statement. */
3107 : 270944 : if (colon_seen)
3108 : : {
3109 : 226254 : if (gfc_match (" =>") == MATCH_YES)
3110 : : {
3111 : 1173 : if (!current_attr.pointer)
3112 : : {
3113 : 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
3114 : 0 : m = MATCH_ERROR;
3115 : 0 : goto cleanup;
3116 : : }
3117 : :
3118 : 1173 : m = match_pointer_init (&initializer, 0);
3119 : 1173 : if (m != MATCH_YES)
3120 : 10 : goto cleanup;
3121 : :
3122 : : /* The target of a pointer initialization must have the SAVE
3123 : : attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
3124 : : is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
3125 : 1163 : if (initializer->expr_type == EXPR_VARIABLE
3126 : 128 : && initializer->symtree->n.sym->attr.save == SAVE_NONE
3127 : 25 : && (gfc_current_state () == COMP_PROGRAM
3128 : : || gfc_current_state () == COMP_MODULE
3129 : 25 : || gfc_current_state () == COMP_SUBMODULE))
3130 : 11 : initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
3131 : : }
3132 : 225081 : else if (gfc_match_char ('=') == MATCH_YES)
3133 : : {
3134 : 25228 : if (current_attr.pointer)
3135 : : {
3136 : 0 : gfc_error ("Pointer initialization at %C requires %<=>%>, "
3137 : : "not %<=%>");
3138 : 0 : m = MATCH_ERROR;
3139 : 0 : goto cleanup;
3140 : : }
3141 : :
3142 : 25228 : if (gfc_comp_struct (gfc_current_state ())
3143 : 2333 : && gfc_current_block ()->attr.pdt_template)
3144 : : {
3145 : 183 : m = gfc_match_expr (&initializer);
3146 : 183 : if (initializer && initializer->ts.type == BT_UNKNOWN)
3147 : 59 : initializer->ts = current_ts;
3148 : : }
3149 : : else
3150 : 25045 : m = gfc_match_init_expr (&initializer);
3151 : :
3152 : 25228 : if (m == MATCH_NO)
3153 : : {
3154 : 0 : gfc_error ("Expected an initialization expression at %C");
3155 : 0 : m = MATCH_ERROR;
3156 : : }
3157 : :
3158 : 9595 : if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
3159 : 25230 : && !gfc_comp_struct (gfc_state_stack->state))
3160 : : {
3161 : 1 : gfc_error ("Initialization of variable at %C is not allowed in "
3162 : : "a PURE procedure");
3163 : 1 : m = MATCH_ERROR;
3164 : : }
3165 : :
3166 : 25228 : if (current_attr.flavor != FL_PARAMETER
3167 : 9595 : && !gfc_comp_struct (gfc_state_stack->state))
3168 : 7262 : gfc_unset_implicit_pure (gfc_current_ns->proc_name);
3169 : :
3170 : 25228 : if (m != MATCH_YES)
3171 : 155 : goto cleanup;
3172 : : }
3173 : : }
3174 : :
3175 : 270779 : if (initializer != NULL && current_attr.allocatable
3176 : 3 : && gfc_comp_struct (gfc_current_state ()))
3177 : : {
3178 : 2 : gfc_error ("Initialization of allocatable component at %C is not "
3179 : : "allowed");
3180 : 2 : m = MATCH_ERROR;
3181 : 2 : goto cleanup;
3182 : : }
3183 : :
3184 : 270777 : if (gfc_current_state () == COMP_DERIVED
3185 : 16047 : && initializer && initializer->ts.type == BT_HOLLERITH)
3186 : : {
3187 : 1 : gfc_error ("Initialization of structure component with a HOLLERITH "
3188 : : "constant at %L is not allowed", &initializer->where);
3189 : 1 : m = MATCH_ERROR;
3190 : 1 : goto cleanup;
3191 : : }
3192 : :
3193 : 270776 : if (gfc_current_state () == COMP_DERIVED
3194 : 16046 : && gfc_current_block ()->attr.pdt_template)
3195 : : {
3196 : 849 : gfc_symbol *param;
3197 : 849 : gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
3198 : : 0, ¶m);
3199 : 849 : if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
3200 : : {
3201 : 1 : gfc_error ("The component with KIND or LEN attribute at %C does not "
3202 : : "not appear in the type parameter list at %L",
3203 : 1 : &gfc_current_block ()->declared_at);
3204 : 1 : m = MATCH_ERROR;
3205 : 4 : goto cleanup;
3206 : : }
3207 : 848 : else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
3208 : : {
3209 : 1 : gfc_error ("The component at %C that appears in the type parameter "
3210 : : "list at %L has neither the KIND nor LEN attribute",
3211 : 1 : &gfc_current_block ()->declared_at);
3212 : 1 : m = MATCH_ERROR;
3213 : 1 : goto cleanup;
3214 : : }
3215 : 847 : else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
3216 : : {
3217 : 1 : gfc_error ("The component at %C which is a type parameter must be "
3218 : : "a scalar");
3219 : 1 : m = MATCH_ERROR;
3220 : 1 : goto cleanup;
3221 : : }
3222 : 846 : else if (param && initializer)
3223 : : {
3224 : 173 : if (initializer->ts.type == BT_BOZ)
3225 : : {
3226 : 1 : gfc_error ("BOZ literal constant at %L cannot appear as an "
3227 : : "initializer", &initializer->where);
3228 : 1 : m = MATCH_ERROR;
3229 : 1 : goto cleanup;
3230 : : }
3231 : 172 : param->value = gfc_copy_expr (initializer);
3232 : : }
3233 : : }
3234 : :
3235 : : /* Before adding a possible initializer, do a simple check for compatibility
3236 : : of lhs and rhs types. Assigning a REAL value to a derived type is not a
3237 : : good thing. */
3238 : 27043 : if (current_ts.type == BT_DERIVED && initializer
3239 : 272140 : && (gfc_numeric_ts (&initializer->ts)
3240 : 1366 : || initializer->ts.type == BT_LOGICAL
3241 : 1366 : || initializer->ts.type == BT_CHARACTER))
3242 : : {
3243 : 2 : gfc_error ("Incompatible initialization between a derived type "
3244 : : "entity and an entity with %qs type at %C",
3245 : : gfc_typename (initializer));
3246 : 2 : m = MATCH_ERROR;
3247 : 2 : goto cleanup;
3248 : : }
3249 : :
3250 : :
3251 : : /* Add the initializer. Note that it is fine if initializer is
3252 : : NULL here, because we sometimes also need to check if a
3253 : : declaration *must* have an initialization expression. */
3254 : 270770 : if (!gfc_comp_struct (gfc_current_state ()))
3255 : 253710 : t = add_init_expr_to_sym (name, &initializer, &var_locus);
3256 : : else
3257 : : {
3258 : 17060 : if (current_ts.type == BT_DERIVED
3259 : 2343 : && !current_attr.pointer && !initializer)
3260 : 1802 : initializer = gfc_default_initializer (¤t_ts);
3261 : 17060 : t = build_struct (name, cl, &initializer, &as);
3262 : :
3263 : : /* If we match a nested structure definition we expect to see the
3264 : : * body even if the variable declarations blow up, so we need to keep
3265 : : * the structure declaration around. */
3266 : 17060 : if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
3267 : 34 : gfc_commit_symbol (gfc_new_block);
3268 : : }
3269 : :
3270 : 270916 : m = (t) ? MATCH_YES : MATCH_ERROR;
3271 : :
3272 : 271205 : cleanup:
3273 : : /* Free stuff up and return. */
3274 : 271205 : gfc_seen_div0 = false;
3275 : 271205 : gfc_free_expr (initializer);
3276 : 271205 : gfc_free_array_spec (as);
3277 : :
3278 : 271205 : return m;
3279 : : }
3280 : :
3281 : :
3282 : : /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
3283 : : This assumes that the byte size is equal to the kind number for
3284 : : non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
3285 : :
3286 : : static match
3287 : 104776 : gfc_match_old_kind_spec (gfc_typespec *ts)
3288 : : {
3289 : 104776 : match m;
3290 : 104776 : int original_kind;
3291 : :
3292 : 104776 : if (gfc_match_char ('*') != MATCH_YES)
3293 : : return MATCH_NO;
3294 : :
3295 : 1138 : m = gfc_match_small_literal_int (&ts->kind, NULL);
3296 : 1138 : if (m != MATCH_YES)
3297 : : return MATCH_ERROR;
3298 : :
3299 : 1138 : original_kind = ts->kind;
3300 : :
3301 : : /* Massage the kind numbers for complex types. */
3302 : 1138 : if (ts->type == BT_COMPLEX)
3303 : : {
3304 : 79 : if (ts->kind % 2)
3305 : : {
3306 : 0 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3307 : : gfc_basic_typename (ts->type), original_kind);
3308 : 0 : return MATCH_ERROR;
3309 : : }
3310 : 79 : ts->kind /= 2;
3311 : :
3312 : : }
3313 : :
3314 : 1138 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3315 : 0 : ts->kind = 8;
3316 : :
3317 : 1138 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3318 : : {
3319 : 846 : if (ts->kind == 4)
3320 : : {
3321 : 212 : if (flag_real4_kind == 8)
3322 : 24 : ts->kind = 8;
3323 : 212 : if (flag_real4_kind == 10)
3324 : 24 : ts->kind = 10;
3325 : 212 : if (flag_real4_kind == 16)
3326 : 24 : ts->kind = 16;
3327 : : }
3328 : 634 : else if (ts->kind == 8)
3329 : : {
3330 : 629 : if (flag_real8_kind == 4)
3331 : 24 : ts->kind = 4;
3332 : 629 : if (flag_real8_kind == 10)
3333 : 24 : ts->kind = 10;
3334 : 629 : if (flag_real8_kind == 16)
3335 : 24 : ts->kind = 16;
3336 : : }
3337 : : }
3338 : :
3339 : 1138 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3340 : : {
3341 : 8 : gfc_error ("Old-style type declaration %s*%d not supported at %C",
3342 : : gfc_basic_typename (ts->type), original_kind);
3343 : 8 : return MATCH_ERROR;
3344 : : }
3345 : :
3346 : 1130 : if (!gfc_notify_std (GFC_STD_GNU,
3347 : : "Nonstandard type declaration %s*%d at %C",
3348 : : gfc_basic_typename(ts->type), original_kind))
3349 : : return MATCH_ERROR;
3350 : :
3351 : : return MATCH_YES;
3352 : : }
3353 : :
3354 : :
3355 : : /* Match a kind specification. Since kinds are generally optional, we
3356 : : usually return MATCH_NO if something goes wrong. If a "kind="
3357 : : string is found, then we know we have an error. */
3358 : :
3359 : : match
3360 : 153964 : gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3361 : : {
3362 : 153964 : locus where, loc;
3363 : 153964 : gfc_expr *e;
3364 : 153964 : match m, n;
3365 : 153964 : char c;
3366 : :
3367 : 153964 : m = MATCH_NO;
3368 : 153964 : n = MATCH_YES;
3369 : 153964 : e = NULL;
3370 : 153964 : saved_kind_expr = NULL;
3371 : :
3372 : 153964 : where = loc = gfc_current_locus;
3373 : :
3374 : 153964 : if (kind_expr_only)
3375 : 0 : goto kind_expr;
3376 : :
3377 : 153964 : if (gfc_match_char ('(') == MATCH_NO)
3378 : : return MATCH_NO;
3379 : :
3380 : : /* Also gobbles optional text. */
3381 : 47843 : if (gfc_match (" kind = ") == MATCH_YES)
3382 : 47843 : m = MATCH_ERROR;
3383 : :
3384 : 47843 : loc = gfc_current_locus;
3385 : :
3386 : 47843 : kind_expr:
3387 : :
3388 : 47843 : n = gfc_match_init_expr (&e);
3389 : :
3390 : 47843 : if (gfc_derived_parameter_expr (e))
3391 : : {
3392 : 130 : ts->kind = 0;
3393 : 130 : saved_kind_expr = gfc_copy_expr (e);
3394 : 130 : goto close_brackets;
3395 : : }
3396 : :
3397 : 47713 : if (n != MATCH_YES)
3398 : : {
3399 : 345 : if (gfc_matching_function)
3400 : : {
3401 : : /* The function kind expression might include use associated or
3402 : : imported parameters and try again after the specification
3403 : : expressions..... */
3404 : 317 : if (gfc_match_char (')') != MATCH_YES)
3405 : : {
3406 : 1 : gfc_error ("Missing right parenthesis at %C");
3407 : 1 : m = MATCH_ERROR;
3408 : 1 : goto no_match;
3409 : : }
3410 : :
3411 : 316 : gfc_free_expr (e);
3412 : 316 : gfc_undo_symbols ();
3413 : 316 : return MATCH_YES;
3414 : : }
3415 : : else
3416 : : {
3417 : : /* ....or else, the match is real. */
3418 : 28 : if (n == MATCH_NO)
3419 : 0 : gfc_error ("Expected initialization expression at %C");
3420 : 28 : if (n != MATCH_YES)
3421 : 28 : return MATCH_ERROR;
3422 : : }
3423 : : }
3424 : :
3425 : 47368 : if (e->rank != 0)
3426 : : {
3427 : 0 : gfc_error ("Expected scalar initialization expression at %C");
3428 : 0 : m = MATCH_ERROR;
3429 : 0 : goto no_match;
3430 : : }
3431 : :
3432 : 47368 : if (gfc_extract_int (e, &ts->kind, 1))
3433 : : {
3434 : 0 : m = MATCH_ERROR;
3435 : 0 : goto no_match;
3436 : : }
3437 : :
3438 : : /* Before throwing away the expression, let's see if we had a
3439 : : C interoperable kind (and store the fact). */
3440 : 47368 : if (e->ts.is_c_interop == 1)
3441 : : {
3442 : : /* Mark this as C interoperable if being declared with one
3443 : : of the named constants from iso_c_binding. */
3444 : 17588 : ts->is_c_interop = e->ts.is_iso_c;
3445 : 17588 : ts->f90_type = e->ts.f90_type;
3446 : 17588 : if (e->symtree)
3447 : 17587 : ts->interop_kind = e->symtree->n.sym;
3448 : : }
3449 : :
3450 : 47368 : gfc_free_expr (e);
3451 : 47368 : e = NULL;
3452 : :
3453 : : /* Ignore errors to this point, if we've gotten here. This means
3454 : : we ignore the m=MATCH_ERROR from above. */
3455 : 47368 : if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3456 : : {
3457 : 7 : gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3458 : : gfc_basic_typename (ts->type));
3459 : 7 : gfc_current_locus = where;
3460 : 7 : return MATCH_ERROR;
3461 : : }
3462 : :
3463 : : /* Warn if, e.g., c_int is used for a REAL variable, but not
3464 : : if, e.g., c_double is used for COMPLEX as the standard
3465 : : explicitly says that the kind type parameter for complex and real
3466 : : variable is the same, i.e. c_float == c_float_complex. */
3467 : 47361 : if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3468 : 17 : && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3469 : 1 : || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3470 : 13 : gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3471 : : "is %s", gfc_basic_typename (ts->f90_type), &where,
3472 : : gfc_basic_typename (ts->type));
3473 : :
3474 : 47348 : close_brackets:
3475 : :
3476 : 47491 : gfc_gobble_whitespace ();
3477 : 47491 : if ((c = gfc_next_ascii_char ()) != ')'
3478 : 47491 : && (ts->type != BT_CHARACTER || c != ','))
3479 : : {
3480 : 0 : if (ts->type == BT_CHARACTER)
3481 : 0 : gfc_error ("Missing right parenthesis or comma at %C");
3482 : : else
3483 : 0 : gfc_error ("Missing right parenthesis at %C");
3484 : 0 : m = MATCH_ERROR;
3485 : 0 : goto no_match;
3486 : : }
3487 : : else
3488 : : /* All tests passed. */
3489 : 47491 : m = MATCH_YES;
3490 : :
3491 : 47491 : if(m == MATCH_ERROR)
3492 : : gfc_current_locus = where;
3493 : :
3494 : 47491 : if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3495 : 0 : ts->kind = 8;
3496 : :
3497 : 47491 : if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3498 : : {
3499 : 13624 : if (ts->kind == 4)
3500 : : {
3501 : 4390 : if (flag_real4_kind == 8)
3502 : 54 : ts->kind = 8;
3503 : 4390 : if (flag_real4_kind == 10)
3504 : 54 : ts->kind = 10;
3505 : 4390 : if (flag_real4_kind == 16)
3506 : 54 : ts->kind = 16;
3507 : : }
3508 : 9234 : else if (ts->kind == 8)
3509 : : {
3510 : 6288 : if (flag_real8_kind == 4)
3511 : 48 : ts->kind = 4;
3512 : 6288 : if (flag_real8_kind == 10)
3513 : 48 : ts->kind = 10;
3514 : 6288 : if (flag_real8_kind == 16)
3515 : 48 : ts->kind = 16;
3516 : : }
3517 : : }
3518 : :
3519 : : /* Return what we know from the test(s). */
3520 : : return m;
3521 : :
3522 : 1 : no_match:
3523 : 1 : gfc_free_expr (e);
3524 : 1 : gfc_current_locus = where;
3525 : 1 : return m;
3526 : : }
3527 : :
3528 : :
3529 : : static match
3530 : 4669 : match_char_kind (int * kind, int * is_iso_c)
3531 : : {
3532 : 4669 : locus where;
3533 : 4669 : gfc_expr *e;
3534 : 4669 : match m, n;
3535 : 4669 : bool fail;
3536 : :
3537 : 4669 : m = MATCH_NO;
3538 : 4669 : e = NULL;
3539 : 4669 : where = gfc_current_locus;
3540 : :
3541 : 4669 : n = gfc_match_init_expr (&e);
3542 : :
3543 : 4669 : if (n != MATCH_YES && gfc_matching_function)
3544 : : {
3545 : : /* The expression might include use-associated or imported
3546 : : parameters and try again after the specification
3547 : : expressions. */
3548 : 7 : gfc_free_expr (e);
3549 : 7 : gfc_undo_symbols ();
3550 : 7 : return MATCH_YES;
3551 : : }
3552 : :
3553 : 7 : if (n == MATCH_NO)
3554 : 2 : gfc_error ("Expected initialization expression at %C");
3555 : 4662 : if (n != MATCH_YES)
3556 : : return MATCH_ERROR;
3557 : :
3558 : 4655 : 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 : 4655 : if (gfc_derived_parameter_expr (e))
3566 : : {
3567 : 14 : saved_kind_expr = e;
3568 : 14 : *kind = 0;
3569 : 14 : return MATCH_YES;
3570 : : }
3571 : :
3572 : 4641 : fail = gfc_extract_int (e, kind, 1);
3573 : 4641 : *is_iso_c = e->ts.is_iso_c;
3574 : 4641 : if (fail)
3575 : : {
3576 : 0 : m = MATCH_ERROR;
3577 : 0 : goto no_match;
3578 : : }
3579 : :
3580 : 4641 : gfc_free_expr (e);
3581 : :
3582 : : /* Ignore errors to this point, if we've gotten here. This means
3583 : : we ignore the m=MATCH_ERROR from above. */
3584 : 4641 : if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3585 : : {
3586 : 14 : gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3587 : 14 : m = MATCH_ERROR;
3588 : : }
3589 : : else
3590 : : /* All tests passed. */
3591 : : m = MATCH_YES;
3592 : :
3593 : 14 : if (m == MATCH_ERROR)
3594 : 14 : gfc_current_locus = where;
3595 : :
3596 : : /* Return what we know from the test(s). */
3597 : : return m;
3598 : :
3599 : 0 : no_match:
3600 : 0 : gfc_free_expr (e);
3601 : 0 : gfc_current_locus = where;
3602 : 0 : return m;
3603 : : }
3604 : :
3605 : :
3606 : : /* Match the various kind/length specifications in a CHARACTER
3607 : : declaration. We don't return MATCH_NO. */
3608 : :
3609 : : match
3610 : 30234 : gfc_match_char_spec (gfc_typespec *ts)
3611 : : {
3612 : 30234 : int kind, seen_length, is_iso_c;
3613 : 30234 : gfc_charlen *cl;
3614 : 30234 : gfc_expr *len;
3615 : 30234 : match m;
3616 : 30234 : bool deferred;
3617 : :
3618 : 30234 : len = NULL;
3619 : 30234 : seen_length = 0;
3620 : 30234 : kind = 0;
3621 : 30234 : is_iso_c = 0;
3622 : 30234 : deferred = false;
3623 : :
3624 : : /* Try the old-style specification first. */
3625 : 30234 : old_char_selector = 0;
3626 : :
3627 : 30234 : m = match_char_length (&len, &deferred, true);
3628 : 30234 : if (m != MATCH_NO)
3629 : : {
3630 : 2217 : if (m == MATCH_YES)
3631 : 2217 : old_char_selector = 1;
3632 : 2217 : seen_length = 1;
3633 : 2217 : goto done;
3634 : : }
3635 : :
3636 : 28017 : m = gfc_match_char ('(');
3637 : 28017 : if (m != MATCH_YES)
3638 : : {
3639 : 1838 : m = MATCH_YES; /* Character without length is a single char. */
3640 : 1838 : goto done;
3641 : : }
3642 : :
3643 : : /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3644 : 26179 : if (gfc_match (" kind =") == MATCH_YES)
3645 : : {
3646 : 3252 : m = match_char_kind (&kind, &is_iso_c);
3647 : :
3648 : 3252 : if (m == MATCH_ERROR)
3649 : 16 : goto done;
3650 : 3236 : if (m == MATCH_NO)
3651 : : goto syntax;
3652 : :
3653 : 3236 : if (gfc_match (" , len =") == MATCH_NO)
3654 : 516 : goto rparen;
3655 : :
3656 : 2720 : m = char_len_param_value (&len, &deferred);
3657 : 2720 : if (m == MATCH_NO)
3658 : 0 : goto syntax;
3659 : 2720 : if (m == MATCH_ERROR)
3660 : 2 : goto done;
3661 : 2718 : seen_length = 1;
3662 : :
3663 : 2718 : goto rparen;
3664 : : }
3665 : :
3666 : : /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3667 : 22927 : if (gfc_match (" len =") == MATCH_YES)
3668 : : {
3669 : 13713 : m = char_len_param_value (&len, &deferred);
3670 : 13713 : if (m == MATCH_NO)
3671 : 2 : goto syntax;
3672 : 13711 : if (m == MATCH_ERROR)
3673 : 8 : goto done;
3674 : 13703 : seen_length = 1;
3675 : :
3676 : 13703 : if (gfc_match_char (')') == MATCH_YES)
3677 : 12428 : goto done;
3678 : :
3679 : 1275 : if (gfc_match (" , kind =") != MATCH_YES)
3680 : 0 : goto syntax;
3681 : :
3682 : 1275 : if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3683 : 2 : goto done;
3684 : :
3685 : 1273 : goto rparen;
3686 : : }
3687 : :
3688 : : /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3689 : 9214 : m = char_len_param_value (&len, &deferred);
3690 : 9214 : if (m == MATCH_NO)
3691 : 0 : goto syntax;
3692 : 9214 : if (m == MATCH_ERROR)
3693 : 44 : goto done;
3694 : 9170 : seen_length = 1;
3695 : :
3696 : 9170 : m = gfc_match_char (')');
3697 : 9170 : if (m == MATCH_YES)
3698 : 9026 : goto done;
3699 : :
3700 : 144 : if (gfc_match_char (',') != MATCH_YES)
3701 : 2 : goto syntax;
3702 : :
3703 : 142 : gfc_match (" kind ="); /* Gobble optional text. */
3704 : :
3705 : 142 : m = match_char_kind (&kind, &is_iso_c);
3706 : 142 : if (m == MATCH_ERROR)
3707 : 3 : goto done;
3708 : : if (m == MATCH_NO)
3709 : : goto syntax;
3710 : :
3711 : 4646 : rparen:
3712 : : /* Require a right-paren at this point. */
3713 : 4646 : m = gfc_match_char (')');
3714 : 4646 : if (m == MATCH_YES)
3715 : 4646 : goto done;
3716 : :
3717 : 0 : syntax:
3718 : 4 : gfc_error ("Syntax error in CHARACTER declaration at %C");
3719 : 4 : m = MATCH_ERROR;
3720 : 4 : gfc_free_expr (len);
3721 : 4 : return m;
3722 : :
3723 : 30230 : done:
3724 : : /* Deal with character functions after USE and IMPORT statements. */
3725 : 30230 : if (gfc_matching_function)
3726 : : {
3727 : 1417 : gfc_free_expr (len);
3728 : 1417 : gfc_undo_symbols ();
3729 : 1417 : return MATCH_YES;
3730 : : }
3731 : :
3732 : 28813 : if (m != MATCH_YES)
3733 : : {
3734 : 65 : gfc_free_expr (len);
3735 : 65 : return m;
3736 : : }
3737 : :
3738 : : /* Do some final massaging of the length values. */
3739 : 28748 : cl = gfc_new_charlen (gfc_current_ns, NULL);
3740 : :
3741 : 28748 : if (seen_length == 0)
3742 : 2302 : cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3743 : : else
3744 : : {
3745 : : /* If gfortran ends up here, then len may be reducible to a constant.
3746 : : Try to do that here. If it does not reduce, simply assign len to
3747 : : charlen. A complication occurs with user-defined generic functions,
3748 : : which are not resolved. Use a private namespace to deal with
3749 : : generic functions. */
3750 : :
3751 : 26446 : if (len && len->expr_type != EXPR_CONSTANT)
3752 : : {
3753 : 2534 : gfc_namespace *old_ns;
3754 : 2534 : gfc_expr *e;
3755 : :
3756 : 2534 : old_ns = gfc_current_ns;
3757 : 2534 : gfc_current_ns = gfc_get_namespace (NULL, 0);
3758 : :
3759 : 2534 : e = gfc_copy_expr (len);
3760 : 2534 : gfc_push_suppress_errors ();
3761 : 2534 : gfc_reduce_init_expr (e);
3762 : 2534 : gfc_pop_suppress_errors ();
3763 : 2534 : if (e->expr_type == EXPR_CONSTANT)
3764 : : {
3765 : 294 : gfc_replace_expr (len, e);
3766 : 294 : if (mpz_cmp_si (len->value.integer, 0) < 0)
3767 : 7 : mpz_set_ui (len->value.integer, 0);
3768 : : }
3769 : : else
3770 : 2240 : gfc_free_expr (e);
3771 : :
3772 : 2534 : gfc_free_namespace (gfc_current_ns);
3773 : 2534 : gfc_current_ns = old_ns;
3774 : : }
3775 : :
3776 : 26446 : cl->length = len;
3777 : : }
3778 : :
3779 : 28748 : ts->u.cl = cl;
3780 : 28748 : ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3781 : 28748 : ts->deferred = deferred;
3782 : :
3783 : : /* We have to know if it was a C interoperable kind so we can
3784 : : do accurate type checking of bind(c) procs, etc. */
3785 : 28748 : if (kind != 0)
3786 : : /* Mark this as C interoperable if being declared with one
3787 : : of the named constants from iso_c_binding. */
3788 : 4552 : ts->is_c_interop = is_iso_c;
3789 : 24196 : else if (len != NULL)
3790 : : /* Here, we might have parsed something such as: character(c_char)
3791 : : In this case, the parsing code above grabs the c_char when
3792 : : looking for the length (line 1690, roughly). it's the last
3793 : : testcase for parsing the kind params of a character variable.
3794 : : However, it's not actually the length. this seems like it
3795 : : could be an error.
3796 : : To see if the user used a C interop kind, test the expr
3797 : : of the so called length, and see if it's C interoperable. */
3798 : 15262 : ts->is_c_interop = len->ts.is_iso_c;
3799 : :
3800 : : return MATCH_YES;
3801 : : }
3802 : :
3803 : :
3804 : : /* Matches a RECORD declaration. */
3805 : :
3806 : : static match
3807 : 931974 : match_record_decl (char *name)
3808 : : {
3809 : 931974 : locus old_loc;
3810 : 931974 : old_loc = gfc_current_locus;
3811 : 931974 : match m;
3812 : :
3813 : 931974 : m = gfc_match (" record /");
3814 : 931974 : if (m == MATCH_YES)
3815 : : {
3816 : 353 : if (!flag_dec_structure)
3817 : : {
3818 : 6 : gfc_current_locus = old_loc;
3819 : 6 : gfc_error ("RECORD at %C is an extension, enable it with "
3820 : : "%<-fdec-structure%>");
3821 : 6 : return MATCH_ERROR;
3822 : : }
3823 : 347 : m = gfc_match (" %n/", name);
3824 : 347 : if (m == MATCH_YES)
3825 : : return MATCH_YES;
3826 : : }
3827 : :
3828 : 931624 : gfc_current_locus = old_loc;
3829 : 931624 : if (flag_dec_structure
3830 : 931624 : && (gfc_match (" record% ") == MATCH_YES
3831 : 8026 : || gfc_match (" record%t") == MATCH_YES))
3832 : 6 : gfc_error ("Structure name expected after RECORD at %C");
3833 : 931624 : if (m == MATCH_NO)
3834 : : return MATCH_NO;
3835 : :
3836 : : return MATCH_ERROR;
3837 : : }
3838 : :
3839 : :
3840 : : /* In parsing a PDT, it is possible that one of the type parameters has the
3841 : : same name as a previously declared symbol that is not a type parameter.
3842 : : Intercept this now by looking for the symtree in f2k_derived. */
3843 : :
3844 : : static bool
3845 : 767 : correct_parm_expr (gfc_expr* e, gfc_symbol* pdt, int* f ATTRIBUTE_UNUSED)
3846 : : {
3847 : 767 : if (!e || (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION))
3848 : : return false;
3849 : :
3850 : 611 : if (!(e->symtree->n.sym->attr.pdt_len
3851 : 73 : || e->symtree->n.sym->attr.pdt_kind))
3852 : : {
3853 : 36 : gfc_symtree *st;
3854 : 36 : st = gfc_find_symtree (pdt->f2k_derived->sym_root,
3855 : : e->symtree->n.sym->name);
3856 : 36 : if (st && st->n.sym
3857 : 30 : && (st->n.sym->attr.pdt_len || st->n.sym->attr.pdt_kind))
3858 : : {
3859 : 30 : gfc_expr *new_expr;
3860 : 30 : gfc_set_sym_referenced (st->n.sym);
3861 : 30 : new_expr = gfc_get_expr ();
3862 : 30 : new_expr->ts = st->n.sym->ts;
3863 : 30 : new_expr->expr_type = EXPR_VARIABLE;
3864 : 30 : new_expr->symtree = st;
3865 : 30 : new_expr->where = e->where;
3866 : 30 : gfc_replace_expr (e, new_expr);
3867 : : }
3868 : : }
3869 : :
3870 : : return false;
3871 : : }
3872 : :
3873 : :
3874 : : void
3875 : 545 : gfc_correct_parm_expr (gfc_symbol *pdt, gfc_expr **bound)
3876 : : {
3877 : 545 : if (!*bound || (*bound)->expr_type == EXPR_CONSTANT)
3878 : : return;
3879 : 531 : gfc_traverse_expr (*bound, pdt, &correct_parm_expr, 0);
3880 : : }
3881 : :
3882 : : /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3883 : : of expressions to substitute into the possibly parameterized expression
3884 : : 'e'. Using a list is inefficient but should not be too bad since the
3885 : : number of type parameters is not likely to be large. */
3886 : : static bool
3887 : 2746 : insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3888 : : int* f)
3889 : : {
3890 : 2746 : gfc_actual_arglist *param;
3891 : 2746 : gfc_expr *copy;
3892 : :
3893 : 2746 : if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
3894 : : return false;
3895 : :
3896 : 1249 : gcc_assert (e->symtree);
3897 : 1249 : if (e->symtree->n.sym->attr.pdt_kind
3898 : 942 : || (*f != 0 && e->symtree->n.sym->attr.pdt_len)
3899 : 429 : || (e->expr_type == EXPR_FUNCTION && e->symtree->n.sym))
3900 : : {
3901 : 1257 : for (param = type_param_spec_list; param; param = param->next)
3902 : 1236 : if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3903 : : break;
3904 : :
3905 : 828 : if (param && param->expr)
3906 : : {
3907 : 799 : copy = gfc_copy_expr (param->expr);
3908 : 799 : *e = *copy;
3909 : 799 : free (copy);
3910 : : /* Catch variables declared without a value expression. */
3911 : 799 : if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_PROCEDURE)
3912 : 13 : e->ts = e->symtree->n.sym->ts;
3913 : : }
3914 : : }
3915 : :
3916 : : return false;
3917 : : }
3918 : :
3919 : :
3920 : : static bool
3921 : 891 : gfc_insert_kind_parameter_exprs (gfc_expr *e)
3922 : : {
3923 : 891 : return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3924 : : }
3925 : :
3926 : :
3927 : : bool
3928 : 1558 : gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3929 : : {
3930 : 1558 : gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3931 : 1558 : type_param_spec_list = param_list;
3932 : 1558 : bool res = gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3933 : 1558 : type_param_spec_list = old_param_spec_list;
3934 : 1558 : return res;
3935 : : }
3936 : :
3937 : : /* Determines the instance of a parameterized derived type to be used by
3938 : : matching determining the values of the kind parameters and using them
3939 : : in the name of the instance. If the instance exists, it is used, otherwise
3940 : : a new derived type is created. */
3941 : : match
3942 : 1700 : gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3943 : : gfc_actual_arglist **ext_param_list)
3944 : : {
3945 : : /* The PDT template symbol. */
3946 : 1700 : gfc_symbol *pdt = *sym;
3947 : : /* The symbol for the parameter in the template f2k_namespace. */
3948 : 1700 : gfc_symbol *param;
3949 : : /* The hoped for instance of the PDT. */
3950 : 1700 : gfc_symbol *instance = NULL;
3951 : : /* The list of parameters appearing in the PDT declaration. */
3952 : 1700 : gfc_formal_arglist *type_param_name_list;
3953 : : /* Used to store the parameter specification list during recursive calls. */
3954 : 1700 : gfc_actual_arglist *old_param_spec_list;
3955 : : /* Pointers to the parameter specification being used. */
3956 : 1700 : gfc_actual_arglist *actual_param;
3957 : 1700 : gfc_actual_arglist *tail = NULL;
3958 : : /* Used to build up the name of the PDT instance. The prefix uses 4
3959 : : characters and each KIND parameter 2 more. Allow 8 of the latter. */
3960 : 1700 : char name[GFC_MAX_SYMBOL_LEN + 21];
3961 : :
3962 : 1700 : bool name_seen = (param_list == NULL);
3963 : 1700 : bool assumed_seen = false;
3964 : 1700 : bool deferred_seen = false;
3965 : 1700 : bool spec_error = false;
3966 : 1700 : bool alloc_seen = false;
3967 : 1700 : bool ptr_seen = false;
3968 : 1700 : int kind_value, i;
3969 : 1700 : gfc_expr *kind_expr;
3970 : 1700 : gfc_component *c1, *c2;
3971 : 1700 : match m;
3972 : :
3973 : 1700 : type_param_spec_list = NULL;
3974 : :
3975 : 1700 : type_param_name_list = pdt->formal;
3976 : 1700 : actual_param = param_list;
3977 : 1700 : sprintf (name, "Pdt%s", pdt->name);
3978 : :
3979 : : /* Prevent a PDT component of the same type as the template from being
3980 : : converted into an instance. Doing this results in the component being
3981 : : lost. */
3982 : 1700 : if (gfc_current_state () == COMP_DERIVED
3983 : 81 : && !(gfc_state_stack->previous
3984 : 81 : && gfc_state_stack->previous->state == COMP_DERIVED)
3985 : 81 : && gfc_current_block ()->attr.pdt_template
3986 : 80 : && !strcmp (gfc_current_block ()->name, (*sym)->name))
3987 : : {
3988 : 19 : if (ext_param_list)
3989 : 19 : *ext_param_list = gfc_copy_actual_arglist (param_list);
3990 : 19 : return MATCH_YES;
3991 : : }
3992 : :
3993 : : /* Run through the parameter name list and pick up the actual
3994 : : parameter values or use the default values in the PDT declaration. */
3995 : 4144 : for (; type_param_name_list;
3996 : 2463 : type_param_name_list = type_param_name_list->next)
3997 : : {
3998 : 2531 : if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3999 : : {
4000 : 2311 : if (actual_param->spec_type == SPEC_ASSUMED)
4001 : : spec_error = deferred_seen;
4002 : : else
4003 : 2311 : spec_error = assumed_seen;
4004 : :
4005 : 2311 : if (spec_error)
4006 : : {
4007 : : gfc_error ("The type parameter spec list at %C cannot contain "
4008 : : "both ASSUMED and DEFERRED parameters");
4009 : : goto error_return;
4010 : : }
4011 : : }
4012 : :
4013 : 2311 : if (actual_param && actual_param->name)
4014 : 2531 : name_seen = true;
4015 : 2531 : param = type_param_name_list->sym;
4016 : :
4017 : 2531 : if (!param || !param->name)
4018 : 2 : continue;
4019 : :
4020 : 2529 : c1 = gfc_find_component (pdt, param->name, false, true, NULL);
4021 : : /* An error should already have been thrown in resolve.cc
4022 : : (resolve_fl_derived0). */
4023 : 2529 : if (!pdt->attr.use_assoc && !c1)
4024 : 8 : goto error_return;
4025 : :
4026 : 2521 : kind_expr = NULL;
4027 : 2521 : if (!name_seen)
4028 : : {
4029 : 1489 : if (!actual_param && !(c1 && c1->initializer))
4030 : : {
4031 : 2 : gfc_error ("The type parameter spec list at %C does not contain "
4032 : : "enough parameter expressions");
4033 : 2 : goto error_return;
4034 : : }
4035 : 1487 : else if (!actual_param && c1 && c1->initializer)
4036 : 5 : kind_expr = gfc_copy_expr (c1->initializer);
4037 : 1482 : else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4038 : 1327 : kind_expr = gfc_copy_expr (actual_param->expr);
4039 : : }
4040 : : else
4041 : : {
4042 : : actual_param = param_list;
4043 : 1424 : for (;actual_param; actual_param = actual_param->next)
4044 : 1203 : if (actual_param->name
4045 : 1199 : && strcmp (actual_param->name, param->name) == 0)
4046 : : break;
4047 : 1032 : if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
4048 : 661 : kind_expr = gfc_copy_expr (actual_param->expr);
4049 : : else
4050 : : {
4051 : 371 : if (c1->initializer)
4052 : 304 : kind_expr = gfc_copy_expr (c1->initializer);
4053 : 67 : else if (!(actual_param && param->attr.pdt_len))
4054 : : {
4055 : 20 : gfc_error ("The derived parameter %qs at %C does not "
4056 : : "have a default value", param->name);
4057 : 20 : goto error_return;
4058 : : }
4059 : : }
4060 : : }
4061 : :
4062 : 2297 : if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
4063 : 224 : && kind_expr->ts.type != BT_INTEGER
4064 : 84 : && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
4065 : : {
4066 : 10 : gfc_error ("The type parameter expression at %L must be of INTEGER "
4067 : : "type and not %s", &kind_expr->where,
4068 : : gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
4069 : 10 : goto error_return;
4070 : : }
4071 : :
4072 : : /* Store the current parameter expressions in a temporary actual
4073 : : arglist 'list' so that they can be substituted in the corresponding
4074 : : expressions in the PDT instance. */
4075 : 2489 : if (type_param_spec_list == NULL)
4076 : : {
4077 : 1646 : type_param_spec_list = gfc_get_actual_arglist ();
4078 : 1646 : tail = type_param_spec_list;
4079 : : }
4080 : : else
4081 : : {
4082 : 843 : tail->next = gfc_get_actual_arglist ();
4083 : 843 : tail = tail->next;
4084 : : }
4085 : 2489 : tail->name = param->name;
4086 : :
4087 : 2489 : if (kind_expr)
4088 : : {
4089 : : /* Try simplification even for LEN expressions. */
4090 : 2287 : bool ok;
4091 : 2287 : gfc_resolve_expr (kind_expr);
4092 : :
4093 : 2287 : if (c1->attr.pdt_kind
4094 : 1226 : && kind_expr->expr_type != EXPR_CONSTANT
4095 : 24 : && type_param_spec_list)
4096 : 24 : gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
4097 : :
4098 : 2287 : ok = gfc_simplify_expr (kind_expr, 1);
4099 : : /* Variable expressions default to BT_PROCEDURE in the absence of an
4100 : : initializer so allow for this. */
4101 : 2287 : if (kind_expr->ts.type != BT_INTEGER
4102 : 80 : && kind_expr->ts.type != BT_PROCEDURE)
4103 : : {
4104 : 6 : gfc_error ("The parameter expression at %C must be of "
4105 : : "INTEGER type and not %s type",
4106 : : gfc_basic_typename (kind_expr->ts.type));
4107 : 6 : goto error_return;
4108 : : }
4109 : 2281 : if (kind_expr->ts.type == BT_INTEGER && !ok)
4110 : : {
4111 : 4 : gfc_error ("The parameter expression at %C does not "
4112 : : "simplify to an INTEGER constant");
4113 : 4 : goto error_return;
4114 : : }
4115 : :
4116 : 2277 : tail->expr = gfc_copy_expr (kind_expr);
4117 : : }
4118 : :
4119 : 2479 : if (actual_param)
4120 : 2273 : tail->spec_type = actual_param->spec_type;
4121 : :
4122 : 2479 : if (!param->attr.pdt_kind)
4123 : : {
4124 : 1255 : if (!name_seen && actual_param)
4125 : 748 : actual_param = actual_param->next;
4126 : 1255 : if (kind_expr)
4127 : : {
4128 : 1055 : gfc_free_expr (kind_expr);
4129 : 1055 : kind_expr = NULL;
4130 : : }
4131 : 1255 : continue;
4132 : : }
4133 : :
4134 : 1224 : if (actual_param
4135 : 1051 : && (actual_param->spec_type == SPEC_ASSUMED
4136 : 1051 : || actual_param->spec_type == SPEC_DEFERRED))
4137 : : {
4138 : 2 : gfc_error ("The KIND parameter %qs at %C cannot either be "
4139 : : "ASSUMED or DEFERRED", param->name);
4140 : 2 : goto error_return;
4141 : : }
4142 : :
4143 : 1222 : if (!kind_expr || !gfc_is_constant_expr (kind_expr))
4144 : : {
4145 : 2 : gfc_error ("The value for the KIND parameter %qs at %C does not "
4146 : : "reduce to a constant expression", param->name);
4147 : 2 : goto error_return;
4148 : : }
4149 : :
4150 : 1220 : kind_value = 0;
4151 : : /* This can come about during the parsing of nested pdt_templates. An
4152 : : error arises because the KIND parameter expression has not been
4153 : : provided. Use the template instead of an incorrect instance. */
4154 : 1220 : if (gfc_extract_int (kind_expr, &kind_value))
4155 : : {
4156 : 14 : gfc_free_actual_arglist (type_param_spec_list);
4157 : 14 : return MATCH_YES;
4158 : : }
4159 : :
4160 : 1206 : sprintf (name + strlen (name), "_%d", kind_value);
4161 : :
4162 : 1206 : if (!name_seen && actual_param)
4163 : 696 : actual_param = actual_param->next;
4164 : 1206 : gfc_free_expr (kind_expr);
4165 : : }
4166 : :
4167 : 1613 : if (!name_seen && actual_param)
4168 : : {
4169 : 2 : gfc_error ("The type parameter spec list at %C contains too many "
4170 : : "parameter expressions");
4171 : 2 : goto error_return;
4172 : : }
4173 : :
4174 : : /* Now we search for the PDT instance 'name'. If it doesn't exist, we
4175 : : build it, using 'pdt' as a template. */
4176 : 1611 : if (gfc_get_symbol (name, pdt->ns, &instance))
4177 : : {
4178 : 0 : gfc_error ("Parameterized derived type at %C is ambiguous");
4179 : 0 : goto error_return;
4180 : : }
4181 : :
4182 : 1611 : m = MATCH_YES;
4183 : :
4184 : 1611 : if (instance->attr.flavor == FL_DERIVED
4185 : 1217 : && instance->attr.pdt_type)
4186 : : {
4187 : 1217 : instance->refs++;
4188 : 1217 : if (ext_param_list)
4189 : 593 : *ext_param_list = type_param_spec_list;
4190 : 1217 : *sym = instance;
4191 : 1217 : gfc_commit_symbols ();
4192 : 1217 : return m;
4193 : : }
4194 : :
4195 : : /* Start building the new instance of the parameterized type. */
4196 : 394 : gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
4197 : 394 : if (pdt->attr.use_assoc)
4198 : 33 : instance->module = pdt->module;
4199 : 394 : instance->attr.pdt_template = 0;
4200 : 394 : instance->attr.pdt_type = 1;
4201 : 394 : instance->declared_at = gfc_current_locus;
4202 : :
4203 : : /* Add the components, replacing the parameters in all expressions
4204 : : with the expressions for their values in 'type_param_spec_list'. */
4205 : 394 : c1 = pdt->components;
4206 : 394 : tail = type_param_spec_list;
4207 : 1530 : for (; c1; c1 = c1->next)
4208 : : {
4209 : 1138 : gfc_add_component (instance, c1->name, &c2);
4210 : :
4211 : 1138 : c2->ts = c1->ts;
4212 : 1138 : c2->attr = c1->attr;
4213 : 1138 : if (c1->tb)
4214 : : {
4215 : 6 : c2->tb = gfc_get_tbp ();
4216 : 6 : *c2->tb = *c1->tb;
4217 : : }
4218 : :
4219 : : /* The order of declaration of the type_specs might not be the
4220 : : same as that of the components. */
4221 : 1138 : if (c1->attr.pdt_kind || c1->attr.pdt_len)
4222 : : {
4223 : 912 : for (tail = type_param_spec_list; tail; tail = tail->next)
4224 : 912 : if (strcmp (c1->name, tail->name) == 0)
4225 : : break;
4226 : : }
4227 : :
4228 : : /* Deal with type extension by recursively calling this function
4229 : : to obtain the instance of the extended type. */
4230 : 1138 : if (gfc_current_state () != COMP_DERIVED
4231 : 1063 : && c1 == pdt->components
4232 : 363 : && c1->ts.type == BT_DERIVED
4233 : 30 : && c1->ts.u.derived
4234 : 1168 : && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
4235 : : {
4236 : 30 : if (c1->ts.u.derived->attr.pdt_template)
4237 : : {
4238 : 29 : gfc_formal_arglist *f;
4239 : :
4240 : 29 : old_param_spec_list = type_param_spec_list;
4241 : :
4242 : : /* Obtain a spec list appropriate to the extended type..*/
4243 : 29 : actual_param = gfc_copy_actual_arglist (type_param_spec_list);
4244 : 29 : type_param_spec_list = actual_param;
4245 : 61 : for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
4246 : 32 : actual_param = actual_param->next;
4247 : 29 : if (actual_param)
4248 : : {
4249 : 29 : gfc_free_actual_arglist (actual_param->next);
4250 : 29 : actual_param->next = NULL;
4251 : : }
4252 : :
4253 : : /* Now obtain the PDT instance for the extended type. */
4254 : 29 : c2->param_list = type_param_spec_list;
4255 : 29 : m = gfc_get_pdt_instance (type_param_spec_list,
4256 : : &c2->ts.u.derived,
4257 : : &c2->param_list);
4258 : 29 : type_param_spec_list = old_param_spec_list;
4259 : : }
4260 : : else
4261 : 1 : c2->ts = c1->ts;
4262 : :
4263 : 30 : c2->ts.u.derived->refs++;
4264 : 30 : gfc_set_sym_referenced (c2->ts.u.derived);
4265 : :
4266 : : /* Set extension level. */
4267 : 30 : if (c2->ts.u.derived->attr.extension == 255)
4268 : : {
4269 : : /* Since the extension field is 8 bit wide, we can only have
4270 : : up to 255 extension levels. */
4271 : 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
4272 : : c2->ts.u.derived->name,
4273 : : &c2->ts.u.derived->declared_at);
4274 : 0 : goto error_return;
4275 : : }
4276 : 30 : instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
4277 : :
4278 : 30 : continue;
4279 : 30 : }
4280 : :
4281 : : /* Addressing PR82943, this will fix the issue where a function or
4282 : : subroutine is declared as not a member of the PDT instance.
4283 : : The reason for this is because the PDT instance did not have access
4284 : : to its template's f2k_derived namespace in order to find the
4285 : : typebound procedures.
4286 : :
4287 : : The number of references to the PDT template's f2k_derived will
4288 : : ensure that f2k_derived is properly freed later on. */
4289 : :
4290 : 1108 : if (!instance->f2k_derived && pdt->f2k_derived)
4291 : : {
4292 : 387 : instance->f2k_derived = pdt->f2k_derived;
4293 : 387 : instance->f2k_derived->refs++;
4294 : : }
4295 : :
4296 : : /* Set the component kind using the parameterized expression. */
4297 : 1108 : if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
4298 : 376 : && c1->kind_expr != NULL)
4299 : : {
4300 : 250 : gfc_expr *e = gfc_copy_expr (c1->kind_expr);
4301 : 250 : gfc_insert_kind_parameter_exprs (e);
4302 : 250 : gfc_simplify_expr (e, 1);
4303 : 250 : gfc_extract_int (e, &c2->ts.kind);
4304 : 250 : gfc_free_expr (e);
4305 : 250 : if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
4306 : : {
4307 : 2 : gfc_error ("Kind %d not supported for type %s at %C",
4308 : : c2->ts.kind, gfc_basic_typename (c2->ts.type));
4309 : 2 : goto error_return;
4310 : : }
4311 : 248 : if (c2->attr.proc_pointer && c2->attr.function
4312 : 0 : && c1->ts.interface && c1->ts.interface->ts.kind == 0)
4313 : : {
4314 : 0 : c2->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4315 : 0 : c2->ts.interface->result = c2->ts.interface;
4316 : 0 : c2->ts.interface->ts = c2->ts;
4317 : 0 : c2->ts.interface->attr.flavor = FL_PROCEDURE;
4318 : 0 : c2->ts.interface->attr.function = 1;
4319 : 0 : c2->attr.function = 1;
4320 : 0 : c2->attr.if_source = IFSRC_UNKNOWN;
4321 : : }
4322 : : }
4323 : :
4324 : : /* Set up either the KIND/LEN initializer, if constant,
4325 : : or the parameterized expression. Use the template
4326 : : initializer if one is not already set in this instance. */
4327 : 1106 : if (c2->attr.pdt_kind || c2->attr.pdt_len)
4328 : : {
4329 : 598 : if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
4330 : 516 : c2->initializer = gfc_copy_expr (tail->expr);
4331 : 82 : else if (tail && tail->expr)
4332 : : {
4333 : 3 : c2->param_list = gfc_get_actual_arglist ();
4334 : 3 : c2->param_list->name = tail->name;
4335 : 3 : c2->param_list->expr = gfc_copy_expr (tail->expr);
4336 : 3 : c2->param_list->next = NULL;
4337 : : }
4338 : :
4339 : 598 : if (!c2->initializer && c1->initializer)
4340 : 16 : c2->initializer = gfc_copy_expr (c1->initializer);
4341 : :
4342 : 598 : if (c2->initializer)
4343 : 532 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4344 : : }
4345 : :
4346 : : /* Copy the array spec. */
4347 : 1106 : c2->as = gfc_copy_array_spec (c1->as);
4348 : 1106 : if (c1->ts.type == BT_CLASS)
4349 : 0 : CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
4350 : :
4351 : 1106 : if (c1->attr.allocatable)
4352 : 28 : alloc_seen = true;
4353 : :
4354 : 1106 : if (c1->attr.pointer)
4355 : 7 : ptr_seen = true;
4356 : :
4357 : : /* Determine if an array spec is parameterized. If so, substitute
4358 : : in the parameter expressions for the bounds and set the pdt_array
4359 : : attribute. Notice that this attribute must be unconditionally set
4360 : : if this is an array of parameterized character length. */
4361 : 1106 : if (c1->as && c1->as->type == AS_EXPLICIT)
4362 : : {
4363 : : bool pdt_array = false;
4364 : :
4365 : : /* Are the bounds of the array parameterized? */
4366 : 475 : for (i = 0; i < c1->as->rank; i++)
4367 : : {
4368 : 291 : if (gfc_derived_parameter_expr (c1->as->lower[i]))
4369 : 6 : pdt_array = true;
4370 : 291 : if (gfc_derived_parameter_expr (c1->as->upper[i]))
4371 : 277 : pdt_array = true;
4372 : : }
4373 : :
4374 : : /* If they are, free the expressions for the bounds and
4375 : : replace them with the template expressions with substitute
4376 : : values. */
4377 : 461 : for (i = 0; pdt_array && i < c1->as->rank; i++)
4378 : : {
4379 : 277 : gfc_expr *e;
4380 : 277 : e = gfc_copy_expr (c1->as->lower[i]);
4381 : 277 : gfc_insert_kind_parameter_exprs (e);
4382 : 277 : gfc_simplify_expr (e, 1);
4383 : 277 : gfc_free_expr (c2->as->lower[i]);
4384 : 277 : c2->as->lower[i] = e;
4385 : 277 : e = gfc_copy_expr (c1->as->upper[i]);
4386 : 277 : gfc_insert_kind_parameter_exprs (e);
4387 : 277 : gfc_simplify_expr (e, 1);
4388 : 277 : gfc_free_expr (c2->as->upper[i]);
4389 : 277 : c2->as->upper[i] = e;
4390 : : }
4391 : :
4392 : 184 : c2->attr.pdt_array = 1;
4393 : 184 : if (c1->initializer)
4394 : : {
4395 : 6 : c2->initializer = gfc_copy_expr (c1->initializer);
4396 : 6 : gfc_insert_kind_parameter_exprs (c2->initializer);
4397 : 6 : gfc_simplify_expr (c2->initializer, 1);
4398 : : }
4399 : : }
4400 : :
4401 : : /* Similarly, set the string length if parameterized. */
4402 : 1106 : if (c1->ts.type == BT_CHARACTER
4403 : 81 : && c1->ts.u.cl->length
4404 : 1187 : && gfc_derived_parameter_expr (c1->ts.u.cl->length))
4405 : : {
4406 : 81 : gfc_expr *e;
4407 : 81 : e = gfc_copy_expr (c1->ts.u.cl->length);
4408 : 81 : gfc_insert_kind_parameter_exprs (e);
4409 : 81 : gfc_simplify_expr (e, 1);
4410 : 81 : gfc_free_expr (c2->ts.u.cl->length);
4411 : 81 : c2->ts.u.cl->length = e;
4412 : 81 : c2->attr.pdt_string = 1;
4413 : : }
4414 : :
4415 : : /* Recurse into this function for PDT components. */
4416 : 1106 : if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
4417 : 70 : && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
4418 : : {
4419 : 70 : gfc_actual_arglist *params;
4420 : : /* The component in the template has a list of specification
4421 : : expressions derived from its declaration. */
4422 : 70 : params = gfc_copy_actual_arglist (c1->param_list);
4423 : 70 : actual_param = params;
4424 : : /* Substitute the template parameters with the expressions
4425 : : from the specification list. */
4426 : 223 : for (;actual_param; actual_param = actual_param->next)
4427 : : {
4428 : 83 : gfc_correct_parm_expr (pdt, &actual_param->expr);
4429 : 83 : gfc_insert_parameter_exprs (actual_param->expr,
4430 : : type_param_spec_list);
4431 : : }
4432 : :
4433 : : /* Now obtain the PDT instance for the component. */
4434 : 70 : old_param_spec_list = type_param_spec_list;
4435 : 140 : m = gfc_get_pdt_instance (params, &c2->ts.u.derived,
4436 : 70 : &c2->param_list);
4437 : 70 : type_param_spec_list = old_param_spec_list;
4438 : :
4439 : 70 : if (!(c2->attr.pointer || c2->attr.allocatable))
4440 : 43 : c2->initializer = gfc_default_initializer (&c2->ts);
4441 : :
4442 : 70 : if (c2->attr.allocatable)
4443 : 20 : instance->attr.alloc_comp = 1;
4444 : : }
4445 : 1036 : else if (!(c2->attr.pdt_kind || c2->attr.pdt_len || c2->attr.pdt_string
4446 : 357 : || c2->attr.pdt_array) && c1->initializer)
4447 : : {
4448 : 8 : c2->initializer = gfc_copy_expr (c1->initializer);
4449 : 8 : if (c2->initializer->ts.type == BT_UNKNOWN)
4450 : 0 : c2->initializer->ts = c2->ts;
4451 : 8 : gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list);
4452 : : /* The template initializers are parsed using gfc_match_expr rather
4453 : : than gfc_match_init_expr. Apply the missing reduction to the
4454 : : PDT instance initializers. */
4455 : 8 : if (!gfc_reduce_init_expr (c2->initializer))
4456 : : {
4457 : 0 : gfc_free_expr (c2->initializer);
4458 : 0 : goto error_return;
4459 : : }
4460 : 8 : gfc_simplify_expr (c2->initializer, 1);
4461 : : }
4462 : : }
4463 : :
4464 : 392 : if (alloc_seen)
4465 : 28 : instance->attr.alloc_comp = 1;
4466 : 392 : if (ptr_seen)
4467 : 7 : instance->attr.pointer_comp = 1;
4468 : :
4469 : :
4470 : 392 : gfc_commit_symbol (instance);
4471 : 392 : if (ext_param_list)
4472 : 258 : *ext_param_list = type_param_spec_list;
4473 : 392 : *sym = instance;
4474 : 392 : return m;
4475 : :
4476 : 58 : error_return:
4477 : 58 : gfc_free_actual_arglist (type_param_spec_list);
4478 : 58 : return MATCH_ERROR;
4479 : : }
4480 : :
4481 : :
4482 : : /* Match a legacy nonstandard BYTE type-spec. */
4483 : :
4484 : : static match
4485 : 1145833 : match_byte_typespec (gfc_typespec *ts)
4486 : : {
4487 : 1145833 : if (gfc_match (" byte") == MATCH_YES)
4488 : : {
4489 : 33 : if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4490 : : return MATCH_ERROR;
4491 : :
4492 : 31 : if (gfc_current_form == FORM_FREE)
4493 : : {
4494 : 19 : char c = gfc_peek_ascii_char ();
4495 : 19 : if (!gfc_is_whitespace (c) && c != ',')
4496 : : return MATCH_NO;
4497 : : }
4498 : :
4499 : 29 : if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4500 : : {
4501 : 0 : gfc_error ("BYTE type used at %C "
4502 : : "is not available on the target machine");
4503 : 0 : return MATCH_ERROR;
4504 : : }
4505 : :
4506 : 29 : ts->type = BT_INTEGER;
4507 : 29 : ts->kind = 1;
4508 : 29 : return MATCH_YES;
4509 : : }
4510 : : return MATCH_NO;
4511 : : }
4512 : :
4513 : :
4514 : : /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
4515 : : structure to the matched specification. This is necessary for FUNCTION and
4516 : : IMPLICIT statements.
4517 : :
4518 : : If implicit_flag is nonzero, then we don't check for the optional
4519 : : kind specification. Not doing so is needed for matching an IMPLICIT
4520 : : statement correctly. */
4521 : :
4522 : : match
4523 : 1145833 : gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4524 : : {
4525 : : /* Provide sufficient space to hold "pdtsymbol". */
4526 : 1145833 : char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
4527 : 1145833 : gfc_symbol *sym, *dt_sym;
4528 : 1145833 : match m;
4529 : 1145833 : char c;
4530 : 1145833 : bool seen_deferred_kind, matched_type;
4531 : 1145833 : const char *dt_name;
4532 : :
4533 : 1145833 : decl_type_param_list = NULL;
4534 : :
4535 : : /* A belt and braces check that the typespec is correctly being treated
4536 : : as a deferred characteristic association. */
4537 : 2291666 : seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4538 : 79661 : && (gfc_current_block ()->result->ts.kind == -1)
4539 : 1157396 : && (ts->kind == -1);
4540 : 1145833 : gfc_clear_ts (ts);
4541 : 1145833 : if (seen_deferred_kind)
4542 : 9389 : ts->kind = -1;
4543 : :
4544 : : /* Clear the current binding label, in case one is given. */
4545 : 1145833 : curr_binding_label = NULL;
4546 : :
4547 : : /* Match BYTE type-spec. */
4548 : 1145833 : m = match_byte_typespec (ts);
4549 : 1145833 : if (m != MATCH_NO)
4550 : : return m;
4551 : :
4552 : 1145802 : m = gfc_match (" type (");
4553 : 1145802 : matched_type = (m == MATCH_YES);
4554 : 1145802 : if (matched_type)
4555 : : {
4556 : 30123 : gfc_gobble_whitespace ();
4557 : 30123 : if (gfc_peek_ascii_char () == '*')
4558 : : {
4559 : 5617 : if ((m = gfc_match ("* ) ")) != MATCH_YES)
4560 : : return m;
4561 : 5617 : if (gfc_comp_struct (gfc_current_state ()))
4562 : : {
4563 : 2 : gfc_error ("Assumed type at %C is not allowed for components");
4564 : 2 : return MATCH_ERROR;
4565 : : }
4566 : 5615 : if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4567 : : return MATCH_ERROR;
4568 : 5613 : ts->type = BT_ASSUMED;
4569 : 5613 : return MATCH_YES;
4570 : : }
4571 : :
4572 : 24506 : m = gfc_match ("%n", name);
4573 : 24506 : matched_type = (m == MATCH_YES);
4574 : : }
4575 : :
4576 : 24506 : if ((matched_type && strcmp ("integer", name) == 0)
4577 : 1140185 : || (!matched_type && gfc_match (" integer") == MATCH_YES))
4578 : : {
4579 : 107090 : ts->type = BT_INTEGER;
4580 : 107090 : ts->kind = gfc_default_integer_kind;
4581 : 107090 : goto get_kind;
4582 : : }
4583 : :
4584 : 1033095 : if (flag_unsigned)
4585 : : {
4586 : 0 : if ((matched_type && strcmp ("unsigned", name) == 0)
4587 : 22489 : || (!matched_type && gfc_match (" unsigned") == MATCH_YES))
4588 : : {
4589 : 1036 : ts->type = BT_UNSIGNED;
4590 : 1036 : ts->kind = gfc_default_integer_kind;
4591 : 1036 : goto get_kind;
4592 : : }
4593 : : }
4594 : :
4595 : 24500 : if ((matched_type && strcmp ("character", name) == 0)
4596 : 1032059 : || (!matched_type && gfc_match (" character") == MATCH_YES))
4597 : : {
4598 : 28317 : if (matched_type
4599 : 28317 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4600 : : "intrinsic-type-spec at %C"))
4601 : : return MATCH_ERROR;
4602 : :
4603 : 28316 : ts->type = BT_CHARACTER;
4604 : 28316 : if (implicit_flag == 0)
4605 : 28210 : m = gfc_match_char_spec (ts);
4606 : : else
4607 : : m = MATCH_YES;
4608 : :
4609 : 28316 : if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4610 : : {
4611 : 1 : gfc_error ("Malformed type-spec at %C");
4612 : 1 : return MATCH_ERROR;
4613 : : }
4614 : :
4615 : 28315 : return m;
4616 : : }
4617 : :
4618 : 24496 : if ((matched_type && strcmp ("real", name) == 0)
4619 : 1003742 : || (!matched_type && gfc_match (" real") == MATCH_YES))
4620 : : {
4621 : 29246 : ts->type = BT_REAL;
4622 : 29246 : ts->kind = gfc_default_real_kind;
4623 : 29246 : goto get_kind;
4624 : : }
4625 : :
4626 : 974496 : if ((matched_type
4627 : 24493 : && (strcmp ("doubleprecision", name) == 0
4628 : 24492 : || (strcmp ("double", name) == 0
4629 : 5 : && gfc_match (" precision") == MATCH_YES)))
4630 : 974496 : || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4631 : : {
4632 : 2550 : if (matched_type
4633 : 2550 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4634 : : "intrinsic-type-spec at %C"))
4635 : : return MATCH_ERROR;
4636 : :
4637 : 2549 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4638 : : {
4639 : 2 : gfc_error ("Malformed type-spec at %C");
4640 : 2 : return MATCH_ERROR;
4641 : : }
4642 : :
4643 : 2547 : ts->type = BT_REAL;
4644 : 2547 : ts->kind = gfc_default_double_kind;
4645 : 2547 : return MATCH_YES;
4646 : : }
4647 : :
4648 : 24489 : if ((matched_type && strcmp ("complex", name) == 0)
4649 : 971946 : || (!matched_type && gfc_match (" complex") == MATCH_YES))
4650 : : {
4651 : 3939 : ts->type = BT_COMPLEX;
4652 : 3939 : ts->kind = gfc_default_complex_kind;
4653 : 3939 : goto get_kind;
4654 : : }
4655 : :
4656 : 968007 : if ((matched_type
4657 : 24489 : && (strcmp ("doublecomplex", name) == 0
4658 : 24488 : || (strcmp ("double", name) == 0
4659 : 2 : && gfc_match (" complex") == MATCH_YES)))
4660 : 968007 : || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4661 : : {
4662 : 204 : if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4663 : : return MATCH_ERROR;
4664 : :
4665 : 203 : if (matched_type
4666 : 203 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4667 : : "intrinsic-type-spec at %C"))
4668 : : return MATCH_ERROR;
4669 : :
4670 : 203 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4671 : : {
4672 : 2 : gfc_error ("Malformed type-spec at %C");
4673 : 2 : return MATCH_ERROR;
4674 : : }
4675 : :
4676 : 201 : ts->type = BT_COMPLEX;
4677 : 201 : ts->kind = gfc_default_double_kind;
4678 : 201 : return MATCH_YES;
4679 : : }
4680 : :
4681 : 24486 : if ((matched_type && strcmp ("logical", name) == 0)
4682 : 967803 : || (!matched_type && gfc_match (" logical") == MATCH_YES))
4683 : : {
4684 : 11346 : ts->type = BT_LOGICAL;
4685 : 11346 : ts->kind = gfc_default_logical_kind;
4686 : 11346 : goto get_kind;
4687 : : }
4688 : :
4689 : 956457 : if (matched_type)
4690 : : {
4691 : 24483 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4692 : 24483 : if (m == MATCH_ERROR)
4693 : : return m;
4694 : :
4695 : 24483 : gfc_gobble_whitespace ();
4696 : 24483 : if (gfc_peek_ascii_char () != ')')
4697 : : {
4698 : 1 : gfc_error ("Malformed type-spec at %C");
4699 : 1 : return MATCH_ERROR;
4700 : : }
4701 : 24482 : m = gfc_match_char (')'); /* Burn closing ')'. */
4702 : : }
4703 : :
4704 : 956456 : if (m != MATCH_YES)
4705 : 931974 : m = match_record_decl (name);
4706 : :
4707 : 956456 : if (matched_type || m == MATCH_YES)
4708 : : {
4709 : 24826 : ts->type = BT_DERIVED;
4710 : : /* We accept record/s/ or type(s) where s is a structure, but we
4711 : : * don't need all the extra derived-type stuff for structures. */
4712 : 24826 : if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4713 : : {
4714 : 1 : gfc_error ("Type name %qs at %C is ambiguous", name);
4715 : 1 : return MATCH_ERROR;
4716 : : }
4717 : :
4718 : 24825 : if (sym && sym->attr.flavor == FL_DERIVED
4719 : 24072 : && sym->attr.pdt_template
4720 : 674 : && gfc_current_state () != COMP_DERIVED)
4721 : : {
4722 : 599 : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4723 : 599 : if (m != MATCH_YES)
4724 : : return m;
4725 : 584 : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4726 : 584 : ts->u.derived = sym;
4727 : 584 : const char* lower = gfc_dt_lower_string (sym->name);
4728 : 584 : size_t len = strlen (lower);
4729 : : /* Reallocate with sufficient size. */
4730 : 584 : if (len > GFC_MAX_SYMBOL_LEN)
4731 : 2 : name = XALLOCAVEC (char, len + 1);
4732 : 584 : memcpy (name, lower, len);
4733 : 584 : name[len] = '\0';
4734 : : }
4735 : :
4736 : 24810 : if (sym && sym->attr.flavor == FL_STRUCT)
4737 : : {
4738 : 361 : ts->u.derived = sym;
4739 : 361 : return MATCH_YES;
4740 : : }
4741 : : /* Actually a derived type. */
4742 : : }
4743 : :
4744 : : else
4745 : : {
4746 : : /* Match nested STRUCTURE declarations; only valid within another
4747 : : structure declaration. */
4748 : 931630 : if (flag_dec_structure
4749 : 8032 : && (gfc_current_state () == COMP_STRUCTURE
4750 : 7570 : || gfc_current_state () == COMP_MAP))
4751 : : {
4752 : 732 : m = gfc_match (" structure");
4753 : 732 : if (m == MATCH_YES)
4754 : : {
4755 : 27 : m = gfc_match_structure_decl ();
4756 : 27 : if (m == MATCH_YES)
4757 : : {
4758 : : /* gfc_new_block is updated by match_structure_decl. */
4759 : 26 : ts->type = BT_DERIVED;
4760 : 26 : ts->u.derived = gfc_new_block;
4761 : 26 : return MATCH_YES;
4762 : : }
4763 : : }
4764 : 706 : if (m == MATCH_ERROR)
4765 : : return MATCH_ERROR;
4766 : : }
4767 : :
4768 : : /* Match CLASS declarations. */
4769 : 931603 : m = gfc_match (" class ( * )");
4770 : 931603 : if (m == MATCH_ERROR)
4771 : : return MATCH_ERROR;
4772 : 931603 : else if (m == MATCH_YES)
4773 : : {
4774 : 1872 : gfc_symbol *upe;
4775 : 1872 : gfc_symtree *st;
4776 : 1872 : ts->type = BT_CLASS;
4777 : 1872 : gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4778 : 1872 : if (upe == NULL)
4779 : : {
4780 : 1157 : upe = gfc_new_symbol ("STAR", gfc_current_ns);
4781 : 1157 : st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4782 : 1157 : st->n.sym = upe;
4783 : 1157 : gfc_set_sym_referenced (upe);
4784 : 1157 : upe->refs++;
4785 : 1157 : upe->ts.type = BT_VOID;
4786 : 1157 : upe->attr.unlimited_polymorphic = 1;
4787 : : /* This is essential to force the construction of
4788 : : unlimited polymorphic component class containers. */
4789 : 1157 : upe->attr.zero_comp = 1;
4790 : 1157 : if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4791 : : &gfc_current_locus))
4792 : : return MATCH_ERROR;
4793 : : }
4794 : : else
4795 : : {
4796 : 715 : st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4797 : 715 : st->n.sym = upe;
4798 : 715 : upe->refs++;
4799 : : }
4800 : 1872 : ts->u.derived = upe;
4801 : 1872 : return m;
4802 : : }
4803 : :
4804 : 929731 : m = gfc_match (" class (");
4805 : :
4806 : 929731 : if (m == MATCH_YES)
4807 : 8610 : m = gfc_match ("%n", name);
4808 : : else
4809 : : return m;
4810 : :
4811 : 8610 : if (m != MATCH_YES)
4812 : : return m;
4813 : 8610 : ts->type = BT_CLASS;
4814 : :
4815 : 8610 : if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4816 : : return MATCH_ERROR;
4817 : :
4818 : 8609 : m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4819 : 8609 : if (m == MATCH_ERROR)
4820 : : return m;
4821 : :
4822 : 8609 : m = gfc_match_char (')');
4823 : 8609 : if (m != MATCH_YES)
4824 : : return m;
4825 : : }
4826 : :
4827 : : /* Defer association of the derived type until the end of the
4828 : : specification block. However, if the derived type can be
4829 : : found, add it to the typespec. */
4830 : 33058 : if (gfc_matching_function)
4831 : : {
4832 : 1022 : ts->u.derived = NULL;
4833 : 1022 : if (gfc_current_state () != COMP_INTERFACE
4834 : 1022 : && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4835 : : {
4836 : 505 : sym = gfc_find_dt_in_generic (sym);
4837 : 505 : ts->u.derived = sym;
4838 : : }
4839 : 1022 : return MATCH_YES;
4840 : : }
4841 : :
4842 : : /* Search for the name but allow the components to be defined later. If
4843 : : type = -1, this typespec has been seen in a function declaration but
4844 : : the type could not be accessed at that point. The actual derived type is
4845 : : stored in a symtree with the first letter of the name capitalized; the
4846 : : symtree with the all lower-case name contains the associated
4847 : : generic function. */
4848 : 32036 : dt_name = gfc_dt_upper_string (name);
4849 : 32036 : sym = NULL;
4850 : 32036 : dt_sym = NULL;
4851 : 32036 : if (ts->kind != -1)
4852 : : {
4853 : 30853 : gfc_get_ha_symbol (name, &sym);
4854 : 30853 : if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4855 : : {
4856 : 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
4857 : 0 : return MATCH_ERROR;
4858 : : }
4859 : 30853 : if (sym->generic && !dt_sym)
4860 : 12732 : dt_sym = gfc_find_dt_in_generic (sym);
4861 : :
4862 : : /* Host associated PDTs can get confused with their constructors
4863 : : because they ar instantiated in the template's namespace. */
4864 : 30853 : if (!dt_sym)
4865 : : {
4866 : 701 : if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4867 : : {
4868 : 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
4869 : 0 : return MATCH_ERROR;
4870 : : }
4871 : 701 : if (dt_sym && !dt_sym->attr.pdt_type)
4872 : 0 : dt_sym = NULL;
4873 : : }
4874 : : }
4875 : 1183 : else if (ts->kind == -1)
4876 : : {
4877 : 2366 : int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4878 : 1183 : || gfc_current_ns->has_import_set;
4879 : 1183 : gfc_find_symbol (name, NULL, iface, &sym);
4880 : 1183 : if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4881 : : {
4882 : 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
4883 : 0 : return MATCH_ERROR;
4884 : : }
4885 : 1183 : if (sym && sym->generic && !dt_sym)
4886 : 0 : dt_sym = gfc_find_dt_in_generic (sym);
4887 : :
4888 : 1183 : ts->kind = 0;
4889 : 1183 : if (sym == NULL)
4890 : : return MATCH_NO;
4891 : : }
4892 : :
4893 : 32026 : if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4894 : 31499 : && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4895 : 32024 : || sym->attr.subroutine)
4896 : : {
4897 : 2 : gfc_error ("Type name %qs at %C conflicts with previously declared "
4898 : : "entity at %L, which has the same name", name,
4899 : : &sym->declared_at);
4900 : 2 : return MATCH_ERROR;
4901 : : }
4902 : :
4903 : 32024 : if (dt_sym && decl_type_param_list
4904 : 714 : && dt_sym->attr.flavor == FL_DERIVED
4905 : 714 : && !dt_sym->attr.pdt_type
4906 : 173 : && !dt_sym->attr.pdt_template)
4907 : : {
4908 : 1 : gfc_error ("Type %qs is not parameterized and so the type parameter spec "
4909 : : "list at %C may not appear", dt_sym->name);
4910 : 1 : return MATCH_ERROR;
4911 : : }
4912 : :
4913 : 32023 : if (sym && sym->attr.flavor == FL_DERIVED
4914 : : && sym->attr.pdt_template
4915 : : && gfc_current_state () != COMP_DERIVED)
4916 : : {
4917 : : m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4918 : : if (m != MATCH_YES)
4919 : : return m;
4920 : : gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4921 : : ts->u.derived = sym;
4922 : : strcpy (name, gfc_dt_lower_string (sym->name));
4923 : : }
4924 : :
4925 : 32023 : gfc_save_symbol_data (sym);
4926 : 32023 : gfc_set_sym_referenced (sym);
4927 : 32023 : if (!sym->attr.generic
4928 : 32023 : && !gfc_add_generic (&sym->attr, sym->name, NULL))
4929 : : return MATCH_ERROR;
4930 : :
4931 : 32023 : if (!sym->attr.function
4932 : 32023 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4933 : : return MATCH_ERROR;
4934 : :
4935 : 32023 : if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4936 : 31903 : && dt_sym->attr.pdt_template
4937 : 179 : && gfc_current_state () != COMP_DERIVED)
4938 : : {
4939 : 104 : m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4940 : 104 : if (m != MATCH_YES)
4941 : : return m;
4942 : 104 : gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4943 : : }
4944 : :
4945 : 32023 : if (!dt_sym)
4946 : : {
4947 : 120 : gfc_interface *intr, *head;
4948 : :
4949 : : /* Use upper case to save the actual derived-type symbol. */
4950 : 120 : gfc_get_symbol (dt_name, NULL, &dt_sym);
4951 : 120 : dt_sym->name = gfc_get_string ("%s", sym->name);
4952 : 120 : head = sym->generic;
4953 : 120 : intr = gfc_get_interface ();
4954 : 120 : intr->sym = dt_sym;
4955 : 120 : intr->where = gfc_current_locus;
4956 : 120 : intr->next = head;
4957 : 120 : sym->generic = intr;
4958 : 120 : sym->attr.if_source = IFSRC_DECL;
4959 : : }
4960 : : else
4961 : 31903 : gfc_save_symbol_data (dt_sym);
4962 : :
4963 : 32023 : gfc_set_sym_referenced (dt_sym);
4964 : :
4965 : 120 : if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4966 : 32143 : && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4967 : : return MATCH_ERROR;
4968 : :
4969 : 32023 : ts->u.derived = dt_sym;
4970 : :
4971 : 32023 : return MATCH_YES;
4972 : :
4973 : 152657 : get_kind:
4974 : 152657 : if (matched_type
4975 : 152657 : && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4976 : : "intrinsic-type-spec at %C"))
4977 : : return MATCH_ERROR;
4978 : :
4979 : : /* For all types except double, derived and character, look for an
4980 : : optional kind specifier. MATCH_NO is actually OK at this point. */
4981 : 152654 : if (implicit_flag == 1)
4982 : : {
4983 : 223 : if (matched_type && gfc_match_char (')') != MATCH_YES)
4984 : : return MATCH_ERROR;
4985 : :
4986 : 223 : return MATCH_YES;
4987 : : }
4988 : :
4989 : 152431 : if (gfc_current_form == FORM_FREE)
4990 : : {
4991 : 138680 : c = gfc_peek_ascii_char ();
4992 : 138680 : if (!gfc_is_whitespace (c) && c != '*' && c != '('
4993 : 68895 : && c != ':' && c != ',')
4994 : : {
4995 : 166 : if (matched_type && c == ')')
4996 : : {
4997 : 3 : gfc_next_ascii_char ();
4998 : 3 : return MATCH_YES;
4999 : : }
5000 : 163 : gfc_error ("Malformed type-spec at %C");
5001 : 163 : return MATCH_NO;
5002 : : }
5003 : : }
5004 : :
5005 : 152265 : m = gfc_match_kind_spec (ts, false);
5006 : 152265 : if (m == MATCH_ERROR)
5007 : : return MATCH_ERROR;
5008 : :
5009 : 152229 : if (m == MATCH_NO && ts->type != BT_CHARACTER)
5010 : : {
5011 : 104736 : m = gfc_match_old_kind_spec (ts);
5012 : 104736 : if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
5013 : : return MATCH_ERROR;
5014 : : }
5015 : :
5016 : 152221 : if (matched_type && gfc_match_char (')') != MATCH_YES)
5017 : : {
5018 : 0 : gfc_error ("Malformed type-spec at %C");
5019 : 0 : return MATCH_ERROR;
5020 : : }
5021 : :
5022 : : /* Defer association of the KIND expression of function results
5023 : : until after USE and IMPORT statements. */
5024 : 4434 : if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
5025 : 156628 : || gfc_matching_function)
5026 : 7038 : return MATCH_YES;
5027 : :
5028 : 145183 : if (m == MATCH_NO)
5029 : 147254 : m = MATCH_YES; /* No kind specifier found. */
5030 : :
5031 : : return m;
5032 : : }
5033 : :
5034 : :
5035 : : /* Match an IMPLICIT NONE statement. Actually, this statement is
5036 : : already matched in parse.cc, or we would not end up here in the
5037 : : first place. So the only thing we need to check, is if there is
5038 : : trailing garbage. If not, the match is successful. */
5039 : :
5040 : : match
5041 : 22979 : gfc_match_implicit_none (void)
5042 : : {
5043 : 22979 : char c;
5044 : 22979 : match m;
5045 : 22979 : char name[GFC_MAX_SYMBOL_LEN + 1];
5046 : 22979 : bool type = false;
5047 : 22979 : bool external = false;
5048 : 22979 : locus cur_loc = gfc_current_locus;
5049 : :
5050 : 22979 : if (gfc_current_ns->seen_implicit_none
5051 : 22977 : || gfc_current_ns->has_implicit_none_export)
5052 : : {
5053 : 4 : gfc_error ("Duplicate IMPLICIT NONE statement at %C");
5054 : 4 : return MATCH_ERROR;
5055 : : }
5056 : :
5057 : 22975 : gfc_gobble_whitespace ();
5058 : 22975 : c = gfc_peek_ascii_char ();
5059 : 22975 : if (c == '(')
5060 : : {
5061 : 1059 : (void) gfc_next_ascii_char ();
5062 : 1059 : if (!gfc_notify_std (GFC_STD_F2018, "IMPLICIT NONE with spec list at %C"))
5063 : : return MATCH_ERROR;
5064 : :
5065 : 1058 : gfc_gobble_whitespace ();
5066 : 1058 : if (gfc_peek_ascii_char () == ')')
5067 : : {
5068 : 1 : (void) gfc_next_ascii_char ();
5069 : 1 : type = true;
5070 : : }
5071 : : else
5072 : 3151 : for(;;)
5073 : : {
5074 : 2104 : m = gfc_match (" %n", name);
5075 : 2104 : if (m != MATCH_YES)
5076 : : return MATCH_ERROR;
5077 : :
5078 : 2104 : if (strcmp (name, "type") == 0)
5079 : : type = true;
5080 : 1057 : else if (strcmp (name, "external") == 0)
5081 : : external = true;
5082 : : else
5083 : : return MATCH_ERROR;
5084 : :
5085 : 2104 : gfc_gobble_whitespace ();
5086 : 2104 : c = gfc_next_ascii_char ();
5087 : 2104 : if (c == ',')
5088 : 1047 : continue;
5089 : 1057 : if (c == ')')
5090 : : break;
5091 : : return MATCH_ERROR;
5092 : : }
5093 : : }
5094 : : else
5095 : : type = true;
5096 : :
5097 : 22974 : if (gfc_match_eos () != MATCH_YES)
5098 : : return MATCH_ERROR;
5099 : :
5100 : 22974 : gfc_set_implicit_none (type, external, &cur_loc);
5101 : :
5102 : 22974 : return MATCH_YES;
5103 : : }
5104 : :
5105 : :
5106 : : /* Match the letter range(s) of an IMPLICIT statement. */
5107 : :
5108 : : static match
5109 : 600 : match_implicit_range (void)
5110 : : {
5111 : 600 : char c, c1, c2;
5112 : 600 : int inner;
5113 : 600 : locus cur_loc;
5114 : :
5115 : 600 : cur_loc = gfc_current_locus;
5116 : :
5117 : 600 : gfc_gobble_whitespace ();
5118 : 600 : c = gfc_next_ascii_char ();
5119 : 600 : if (c != '(')
5120 : : {
5121 : 59 : gfc_error ("Missing character range in IMPLICIT at %C");
5122 : 59 : goto bad;
5123 : : }
5124 : :
5125 : : inner = 1;
5126 : 1195 : while (inner)
5127 : : {
5128 : 722 : gfc_gobble_whitespace ();
5129 : 722 : c1 = gfc_next_ascii_char ();
5130 : 722 : if (!ISALPHA (c1))
5131 : 33 : goto bad;
5132 : :
5133 : 689 : gfc_gobble_whitespace ();
5134 : 689 : c = gfc_next_ascii_char ();
5135 : :
5136 : 689 : switch (c)
5137 : : {
5138 : 201 : case ')':
5139 : 201 : inner = 0; /* Fall through. */
5140 : :
5141 : : case ',':
5142 : : c2 = c1;
5143 : : break;
5144 : :
5145 : 439 : case '-':
5146 : 439 : gfc_gobble_whitespace ();
5147 : 439 : c2 = gfc_next_ascii_char ();
5148 : 439 : if (!ISALPHA (c2))
5149 : 0 : goto bad;
5150 : :
5151 : 439 : gfc_gobble_whitespace ();
5152 : 439 : c = gfc_next_ascii_char ();
5153 : :
5154 : 439 : if ((c != ',') && (c != ')'))
5155 : 0 : goto bad;
5156 : 439 : if (c == ')')
5157 : 272 : inner = 0;
5158 : :
5159 : : break;
5160 : :
5161 : 35 : default:
5162 : 35 : goto bad;
5163 : : }
5164 : :
5165 : 654 : if (c1 > c2)
5166 : : {
5167 : 0 : gfc_error ("Letters must be in alphabetic order in "
5168 : : "IMPLICIT statement at %C");
5169 : 0 : goto bad;
5170 : : }
5171 : :
5172 : : /* See if we can add the newly matched range to the pending
5173 : : implicits from this IMPLICIT statement. We do not check for
5174 : : conflicts with whatever earlier IMPLICIT statements may have
5175 : : set. This is done when we've successfully finished matching
5176 : : the current one. */
5177 : 654 : if (!gfc_add_new_implicit_range (c1, c2))
5178 : 0 : goto bad;
5179 : : }
5180 : :
5181 : : return MATCH_YES;
5182 : :
5183 : 127 : bad:
5184 : 127 : gfc_syntax_error (ST_IMPLICIT);
5185 : :
5186 : 127 : gfc_current_locus = cur_loc;
5187 : 127 : return MATCH_ERROR;
5188 : : }
5189 : :
5190 : :
5191 : : /* Match an IMPLICIT statement, storing the types for
5192 : : gfc_set_implicit() if the statement is accepted by the parser.
5193 : : There is a strange looking, but legal syntactic construction
5194 : : possible. It looks like:
5195 : :
5196 : : IMPLICIT INTEGER (a-b) (c-d)
5197 : :
5198 : : This is legal if "a-b" is a constant expression that happens to
5199 : : equal one of the legal kinds for integers. The real problem
5200 : : happens with an implicit specification that looks like:
5201 : :
5202 : : IMPLICIT INTEGER (a-b)
5203 : :
5204 : : In this case, a typespec matcher that is "greedy" (as most of the
5205 : : matchers are) gobbles the character range as a kindspec, leaving
5206 : : nothing left. We therefore have to go a bit more slowly in the
5207 : : matching process by inhibiting the kindspec checking during
5208 : : typespec matching and checking for a kind later. */
5209 : :
5210 : : match
5211 : 23405 : gfc_match_implicit (void)
5212 : : {
5213 : 23405 : gfc_typespec ts;
5214 : 23405 : locus cur_loc;
5215 : 23405 : char c;
5216 : 23405 : match m;
5217 : :
5218 : 23405 : if (gfc_current_ns->seen_implicit_none)
5219 : : {
5220 : 4 : gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
5221 : : "statement");
5222 : 4 : return MATCH_ERROR;
5223 : : }
5224 : :
5225 : 23401 : gfc_clear_ts (&ts);
5226 : :
5227 : : /* We don't allow empty implicit statements. */
5228 : 23401 : if (gfc_match_eos () == MATCH_YES)
5229 : : {
5230 : 0 : gfc_error ("Empty IMPLICIT statement at %C");
5231 : 0 : return MATCH_ERROR;
5232 : : }
5233 : :
5234 : 23430 : do
5235 : : {
5236 : : /* First cleanup. */
5237 : 23430 : gfc_clear_new_implicit ();
5238 : :
5239 : : /* A basic type is mandatory here. */
5240 : 23430 : m = gfc_match_decl_type_spec (&ts, 1);
5241 : 23430 : if (m == MATCH_ERROR)
5242 : 0 : goto error;
5243 : 23430 : if (m == MATCH_NO)
5244 : 22977 : goto syntax;
5245 : :
5246 : 453 : cur_loc = gfc_current_locus;
5247 : 453 : m = match_implicit_range ();
5248 : :
5249 : 453 : if (m == MATCH_YES)
5250 : : {
5251 : : /* We may have <TYPE> (<RANGE>). */
5252 : 326 : gfc_gobble_whitespace ();
5253 : 326 : c = gfc_peek_ascii_char ();
5254 : 326 : if (c == ',' || c == '\n' || c == ';' || c == '!')
5255 : : {
5256 : : /* Check for CHARACTER with no length parameter. */
5257 : 299 : if (ts.type == BT_CHARACTER && !ts.u.cl)
5258 : : {
5259 : 32 : ts.kind = gfc_default_character_kind;
5260 : 32 : ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5261 : 32 : ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
5262 : : NULL, 1);
5263 : : }
5264 : :
5265 : : /* Record the Successful match. */
5266 : 299 : if (!gfc_merge_new_implicit (&ts))
5267 : : return MATCH_ERROR;
5268 : 297 : if (c == ',')
5269 : 28 : c = gfc_next_ascii_char ();
5270 : 269 : else if (gfc_match_eos () == MATCH_ERROR)
5271 : 0 : goto error;
5272 : 297 : continue;
5273 : : }
5274 : :
5275 : 27 : gfc_current_locus = cur_loc;
5276 : : }
5277 : :
5278 : : /* Discard the (incorrectly) matched range. */
5279 : 154 : gfc_clear_new_implicit ();
5280 : :
5281 : : /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
5282 : 154 : if (ts.type == BT_CHARACTER)
5283 : 74 : m = gfc_match_char_spec (&ts);
5284 : 80 : else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
5285 : : {
5286 : 76 : m = gfc_match_kind_spec (&ts, false);
5287 : 76 : if (m == MATCH_NO)
5288 : : {
5289 : 40 : m = gfc_match_old_kind_spec (&ts);
5290 : 40 : if (m == MATCH_ERROR)
5291 : 0 : goto error;
5292 : 40 : if (m == MATCH_NO)
5293 : 0 : goto syntax;
5294 : : }
5295 : : }
5296 : 154 : if (m == MATCH_ERROR)
5297 : 7 : goto error;
5298 : :
5299 : 147 : m = match_implicit_range ();
5300 : 147 : if (m == MATCH_ERROR)
5301 : 0 : goto error;
5302 : 147 : if (m == MATCH_NO)
5303 : : goto syntax;
5304 : :
5305 : 147 : gfc_gobble_whitespace ();
5306 : 147 : c = gfc_next_ascii_char ();
5307 : 147 : if (c != ',' && gfc_match_eos () != MATCH_YES)
5308 : 0 : goto syntax;
5309 : :
5310 : 147 : if (!gfc_merge_new_implicit (&ts))
5311 : : return MATCH_ERROR;
5312 : : }
5313 : 444 : while (c == ',');
5314 : :
5315 : : return MATCH_YES;
5316 : :
5317 : 22977 : syntax:
5318 : 22977 : gfc_syntax_error (ST_IMPLICIT);
5319 : :
5320 : : error:
5321 : : return MATCH_ERROR;
5322 : : }
5323 : :
5324 : :
5325 : : /* Match the IMPORT statement. IMPORT was added to F2003 as
5326 : :
5327 : : R1209 import-stmt is IMPORT [[ :: ] import-name-list ]
5328 : :
5329 : : C1210 (R1209) The IMPORT statement is allowed only in an interface-body.
5330 : :
5331 : : C1211 (R1209) Each import-name shall be the name of an entity in the
5332 : : host scoping unit.
5333 : :
5334 : : under the description of an interface block. Under F2008, IMPORT was
5335 : : split out of the interface block description to 12.4.3.3 and C1210
5336 : : became
5337 : :
5338 : : C1210 (R1209) The IMPORT statement is allowed only in an interface-body
5339 : : that is not a module procedure interface body.
5340 : :
5341 : : Finally, F2018, section 8.8, has changed the IMPORT statement to
5342 : :
5343 : : R867 import-stmt is IMPORT [[ :: ] import-name-list ]
5344 : : or IMPORT, ONLY : import-name-list
5345 : : or IMPORT, NONE
5346 : : or IMPORT, ALL
5347 : :
5348 : : C896 (R867) An IMPORT statement shall not appear in the scoping unit of
5349 : : a main-program, external-subprogram, module, or block-data.
5350 : :
5351 : : C897 (R867) Each import-name shall be the name of an entity in the host
5352 : : scoping unit.
5353 : :
5354 : : C898 If any IMPORT statement in a scoping unit has an ONLY specifier,
5355 : : all IMPORT statements in that scoping unit shall have an ONLY
5356 : : specifier.
5357 : :
5358 : : C899 IMPORT, NONE shall not appear in the scoping unit of a submodule.
5359 : :
5360 : : C8100 If an IMPORT, NONE or IMPORT, ALL statement appears in a scoping
5361 : : unit, no other IMPORT statement shall appear in that scoping unit.
5362 : :
5363 : : C8101 Within an interface body, an entity that is accessed by host
5364 : : association shall be accessible by host or use association within
5365 : : the host scoping unit, or explicitly declared prior to the interface
5366 : : body.
5367 : :
5368 : : C8102 An entity whose name appears as an import-name or which is made
5369 : : accessible by an IMPORT, ALL statement shall not appear in any
5370 : : context described in 19.5.1.4 that would cause the host entity
5371 : : of that name to be inaccessible. */
5372 : :
5373 : : match
5374 : 3913 : gfc_match_import (void)
5375 : : {
5376 : 3913 : char name[GFC_MAX_SYMBOL_LEN + 1];
5377 : 3913 : match m;
5378 : 3913 : gfc_symbol *sym;
5379 : 3913 : gfc_symtree *st;
5380 : 3913 : bool f2018_allowed = gfc_option.allow_std & ~GFC_STD_OPT_F08;;
5381 : 3913 : importstate current_import_state = gfc_current_ns->import_state;
5382 : :
5383 : 3913 : if (!f2018_allowed
5384 : 13 : && (gfc_current_ns->proc_name == NULL
5385 : 12 : || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY))
5386 : : {
5387 : 3 : gfc_error ("IMPORT statement at %C only permitted in "
5388 : : "an INTERFACE body");
5389 : 3 : return MATCH_ERROR;
5390 : : }
5391 : : else if (f2018_allowed
5392 : 3900 : && (!gfc_current_ns->parent || gfc_current_ns->is_block_data))
5393 : 4 : goto C897;
5394 : :
5395 : 3896 : if (f2018_allowed
5396 : 3896 : && (current_import_state == IMPORT_ALL
5397 : 3896 : || current_import_state == IMPORT_NONE))
5398 : 2 : goto C8100;
5399 : :
5400 : 3904 : if (gfc_current_ns->proc_name
5401 : 3903 : && gfc_current_ns->proc_name->attr.module_procedure)
5402 : : {
5403 : 1 : gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
5404 : : "in a module procedure interface body");
5405 : 1 : return MATCH_ERROR;
5406 : : }
5407 : :
5408 : 3903 : if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
5409 : : return MATCH_ERROR;
5410 : :
5411 : 3899 : gfc_current_ns->import_state = IMPORT_NOT_SET;
5412 : 3899 : if (f2018_allowed)
5413 : : {
5414 : 3893 : if (gfc_match (" , none") == MATCH_YES)
5415 : : {
5416 : 8 : if (current_import_state == IMPORT_ONLY)
5417 : 0 : goto C898;
5418 : 8 : if (gfc_current_state () == COMP_SUBMODULE)
5419 : 0 : goto C899;
5420 : 8 : gfc_current_ns->import_state = IMPORT_NONE;
5421 : : }
5422 : 3885 : else if (gfc_match (" , only :") == MATCH_YES)
5423 : : {
5424 : 19 : if (current_import_state != IMPORT_NOT_SET
5425 : 19 : && current_import_state != IMPORT_ONLY)
5426 : 0 : goto C898;
5427 : 19 : gfc_current_ns->import_state = IMPORT_ONLY;
5428 : : }
5429 : 3866 : else if (gfc_match (" , all") == MATCH_YES)
5430 : : {
5431 : 1 : if (current_import_state == IMPORT_ONLY)
5432 : 0 : goto C898;
5433 : 1 : gfc_current_ns->import_state = IMPORT_ALL;
5434 : : }
5435 : :
5436 : 3893 : if (current_import_state != IMPORT_NOT_SET
5437 : 12 : && (gfc_current_ns->import_state == IMPORT_NONE
5438 : 12 : || gfc_current_ns->import_state == IMPORT_ALL))
5439 : 0 : goto C8100;
5440 : : }
5441 : :
5442 : : /* F2008 IMPORT<eos> is distinct from F2018 IMPORT, ALL. */
5443 : 3899 : if (gfc_match_eos () == MATCH_YES)
5444 : : {
5445 : : /* This is the F2008 variant. */
5446 : 225 : if (gfc_current_ns->import_state == IMPORT_NOT_SET)
5447 : : {
5448 : 216 : if (current_import_state == IMPORT_ONLY)
5449 : 0 : goto C898;
5450 : 216 : gfc_current_ns->import_state = IMPORT_F2008;
5451 : : }
5452 : :
5453 : : /* Host variables should be imported. */
5454 : 225 : if (gfc_current_ns->import_state != IMPORT_NONE)
5455 : 217 : gfc_current_ns->has_import_set = 1;
5456 : 225 : return MATCH_YES;
5457 : : }
5458 : :
5459 : 3674 : if (gfc_match (" ::") == MATCH_YES
5460 : 3674 : && gfc_current_ns->import_state != IMPORT_ONLY)
5461 : : {
5462 : 1164 : if (gfc_match_eos () == MATCH_YES)
5463 : 1 : goto expecting_list;
5464 : 1163 : gfc_current_ns->import_state = IMPORT_F2008;
5465 : : }
5466 : 2510 : else if (gfc_current_ns->import_state == IMPORT_ONLY)
5467 : : {
5468 : 19 : if (gfc_match_eos () == MATCH_YES)
5469 : 0 : goto expecting_list;
5470 : : }
5471 : :
5472 : 4357 : for(;;)
5473 : : {
5474 : 4357 : sym = NULL;
5475 : 4357 : m = gfc_match (" %n", name);
5476 : 4357 : switch (m)
5477 : : {
5478 : 4357 : case MATCH_YES:
5479 : 4357 : if (gfc_current_ns->parent != NULL
5480 : 4357 : && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
5481 : : {
5482 : 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5483 : 0 : return MATCH_ERROR;
5484 : : }
5485 : 4357 : else if (!sym
5486 : 5 : && gfc_current_ns->proc_name
5487 : 4 : && gfc_current_ns->proc_name->ns->parent
5488 : 4358 : && gfc_find_symbol (name,
5489 : : gfc_current_ns->proc_name->ns->parent,
5490 : : 1, &sym))
5491 : : {
5492 : 0 : gfc_error ("Type name %qs at %C is ambiguous", name);
5493 : 0 : return MATCH_ERROR;
5494 : : }
5495 : :
5496 : 4357 : if (sym == NULL)
5497 : : {
5498 : 5 : if (gfc_current_ns->proc_name
5499 : 4 : && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
5500 : : {
5501 : 1 : gfc_error ("Cannot IMPORT %qs from host scoping unit "
5502 : : "at %C - does not exist.", name);
5503 : 1 : return MATCH_ERROR;
5504 : : }
5505 : : else
5506 : : {
5507 : : /* This might be a procedure that has not yet been parsed. If
5508 : : so gfc_fixup_sibling_symbols will replace this symbol with
5509 : : that of the procedure. */
5510 : 4 : gfc_get_sym_tree (name, gfc_current_ns, &st, false,
5511 : : &gfc_current_locus);
5512 : 4 : st->n.sym->refs++;
5513 : 4 : st->n.sym->attr.imported = 1;
5514 : 4 : st->import_only = 1;
5515 : 4 : goto next_item;
5516 : : }
5517 : : }
5518 : :
5519 : 4352 : st = gfc_find_symtree (gfc_current_ns->sym_root, name);
5520 : 4352 : if (st && st->n.sym && st->n.sym->attr.imported)
5521 : : {
5522 : 6 : gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
5523 : : "at %C", name);
5524 : 6 : goto next_item;
5525 : : }
5526 : :
5527 : 4346 : st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
5528 : 4346 : st->n.sym = sym;
5529 : 4346 : sym->refs++;
5530 : 4346 : sym->attr.imported = 1;
5531 : 4346 : st->import_only = 1;
5532 : :
5533 : 4346 : if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
5534 : : {
5535 : : /* The actual derived type is stored in a symtree with the first
5536 : : letter of the name capitalized; the symtree with the all
5537 : : lower-case name contains the associated generic function. */
5538 : 601 : st = gfc_new_symtree (&gfc_current_ns->sym_root,
5539 : : gfc_dt_upper_string (name));
5540 : 601 : st->n.sym = sym;
5541 : 601 : sym->refs++;
5542 : 601 : sym->attr.imported = 1;
5543 : 601 : st->import_only = 1;
5544 : : }
5545 : :
5546 : 4346 : goto next_item;
5547 : :
5548 : : case MATCH_NO:
5549 : : break;
5550 : :
5551 : : case MATCH_ERROR:
5552 : : return MATCH_ERROR;
5553 : : }
5554 : :
5555 : 4356 : next_item:
5556 : 4356 : if (gfc_match_eos () == MATCH_YES)
5557 : : break;
5558 : 684 : if (gfc_match_char (',') != MATCH_YES)
5559 : 0 : goto syntax;
5560 : : }
5561 : :
5562 : : return MATCH_YES;
5563 : :
5564 : 0 : syntax:
5565 : 0 : gfc_error ("Syntax error in IMPORT statement at %C");
5566 : 0 : return MATCH_ERROR;
5567 : :
5568 : 4 : C897:
5569 : 4 : gfc_error ("F2018: C897 IMPORT statement at %C cannot appear in a main "
5570 : : "program, an external subprogram, a module or block data");
5571 : 4 : return MATCH_ERROR;
5572 : :
5573 : 0 : C898:
5574 : 0 : gfc_error ("F2018: C898 IMPORT statement at %C is not permitted because "
5575 : : "a scoping unit has an ONLY specifier, can only have IMPORT "
5576 : : "with an ONLY specifier");
5577 : 0 : return MATCH_ERROR;
5578 : :
5579 : 0 : C899:
5580 : 0 : gfc_error ("F2018: C899 IMPORT, NONE shall not appear in the scoping unit"
5581 : : " of a submodule as at %C");
5582 : 0 : return MATCH_ERROR;
5583 : :
5584 : 2 : C8100:
5585 : 4 : gfc_error ("F2018: C8100 IMPORT statement at %C is not permitted because "
5586 : : "%s has already been declared, which must be unique in the "
5587 : : "scoping unit",
5588 : 2 : gfc_current_ns->import_state == IMPORT_ALL ? "IMPORT, ALL" :
5589 : : "IMPORT, NONE");
5590 : 2 : return MATCH_ERROR;
5591 : :
5592 : 1 : expecting_list:
5593 : 1 : gfc_error ("Expecting list of named entities at %C");
5594 : 1 : return MATCH_ERROR;
5595 : : }
5596 : :
5597 : :
5598 : : /* A minimal implementation of gfc_match without whitespace, escape
5599 : : characters or variable arguments. Returns true if the next
5600 : : characters match the TARGET template exactly. */
5601 : :
5602 : : static bool
5603 : 140775 : match_string_p (const char *target)
5604 : : {
5605 : 140775 : const char *p;
5606 : :
5607 : 890374 : for (p = target; *p; p++)
5608 : 749600 : if ((char) gfc_next_ascii_char () != *p)
5609 : : return false;
5610 : : return true;
5611 : : }
5612 : :
5613 : : /* Matches an attribute specification including array specs. If
5614 : : successful, leaves the variables current_attr and current_as
5615 : : holding the specification. Also sets the colon_seen variable for
5616 : : later use by matchers associated with initializations.
5617 : :
5618 : : This subroutine is a little tricky in the sense that we don't know
5619 : : if we really have an attr-spec until we hit the double colon.
5620 : : Until that time, we can only return MATCH_NO. This forces us to
5621 : : check for duplicate specification at this level. */
5622 : :
5623 : : static match
5624 : 208778 : match_attr_spec (void)
5625 : : {
5626 : : /* Modifiers that can exist in a type statement. */
5627 : 208778 : enum
5628 : : { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
5629 : : DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
5630 : : DECL_DIMENSION, DECL_EXTERNAL,
5631 : : DECL_INTRINSIC, DECL_OPTIONAL,
5632 : : DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
5633 : : DECL_STATIC, DECL_AUTOMATIC,
5634 : : DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
5635 : : DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5636 : : DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
5637 : : };
5638 : :
5639 : : /* GFC_DECL_END is the sentinel, index starts at 0. */
5640 : : #define NUM_DECL GFC_DECL_END
5641 : :
5642 : : /* Make sure that values from sym_intent are safe to be used here. */
5643 : 208778 : gcc_assert (INTENT_IN > 0);
5644 : :
5645 : 208778 : locus start, seen_at[NUM_DECL];
5646 : 208778 : int seen[NUM_DECL];
5647 : 208778 : unsigned int d;
5648 : 208778 : const char *attr;
5649 : 208778 : match m;
5650 : 208778 : bool t;
5651 : :
5652 : 208778 : gfc_clear_attr (¤t_attr);
5653 : 208778 : start = gfc_current_locus;
5654 : :
5655 : 208778 : current_as = NULL;
5656 : 208778 : colon_seen = 0;
5657 : 208778 : attr_seen = 0;
5658 : :
5659 : : /* See if we get all of the keywords up to the final double colon. */
5660 : 5637006 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5661 : 5428228 : seen[d] = 0;
5662 : :
5663 : 323435 : for (;;)
5664 : : {
5665 : 323435 : char ch;
5666 : :
5667 : 323435 : d = DECL_NONE;
5668 : 323435 : gfc_gobble_whitespace ();
5669 : :
5670 : 323435 : ch = gfc_next_ascii_char ();
5671 : 323435 : if (ch == ':')
5672 : : {
5673 : : /* This is the successful exit condition for the loop. */
5674 : 176336 : if (gfc_next_ascii_char () == ':')
5675 : : break;
5676 : : }
5677 : 147099 : else if (ch == ',')
5678 : : {
5679 : 114669 : gfc_gobble_whitespace ();
5680 : 114669 : switch (gfc_peek_ascii_char ())
5681 : : {
5682 : 17594 : case 'a':
5683 : 17594 : gfc_next_ascii_char ();
5684 : 17594 : switch (gfc_next_ascii_char ())
5685 : : {
5686 : 17529 : case 'l':
5687 : 17529 : if (match_string_p ("locatable"))
5688 : : {
5689 : : /* Matched "allocatable". */
5690 : : d = DECL_ALLOCATABLE;
5691 : : }
5692 : : break;
5693 : :
5694 : 24 : case 's':
5695 : 24 : if (match_string_p ("ynchronous"))
5696 : : {
5697 : : /* Matched "asynchronous". */
5698 : : d = DECL_ASYNCHRONOUS;
5699 : : }
5700 : : break;
5701 : :
5702 : 41 : case 'u':
5703 : 41 : if (match_string_p ("tomatic"))
5704 : : {
5705 : : /* Matched "automatic". */
5706 : : d = DECL_AUTOMATIC;
5707 : : }
5708 : : break;
5709 : : }
5710 : : break;
5711 : :
5712 : 163 : case 'b':
5713 : : /* Try and match the bind(c). */
5714 : 163 : m = gfc_match_bind_c (NULL, true);
5715 : 163 : if (m == MATCH_YES)
5716 : : d = DECL_IS_BIND_C;
5717 : 0 : else if (m == MATCH_ERROR)
5718 : 0 : goto cleanup;
5719 : : break;
5720 : :
5721 : 2085 : case 'c':
5722 : 2085 : gfc_next_ascii_char ();
5723 : 2085 : if ('o' != gfc_next_ascii_char ())
5724 : : break;
5725 : 2084 : switch (gfc_next_ascii_char ())
5726 : : {
5727 : 46 : case 'd':
5728 : 46 : if (match_string_p ("imension"))
5729 : : {
5730 : : d = DECL_CODIMENSION;
5731 : : break;
5732 : : }
5733 : : /* FALLTHRU */
5734 : 2038 : case 'n':
5735 : 2038 : if (match_string_p ("tiguous"))
5736 : : {
5737 : : d = DECL_CONTIGUOUS;
5738 : : break;
5739 : : }
5740 : : }
5741 : : break;
5742 : :
5743 : 19470 : case 'd':
5744 : 19470 : if (match_string_p ("dimension"))
5745 : : d = DECL_DIMENSION;
5746 : : break;
5747 : :
5748 : 177 : case 'e':
5749 : 177 : if (match_string_p ("external"))
5750 : : d = DECL_EXTERNAL;
5751 : : break;
5752 : :
5753 : 26270 : case 'i':
5754 : 26270 : if (match_string_p ("int"))
5755 : : {
5756 : 26270 : ch = gfc_next_ascii_char ();
5757 : 26270 : if (ch == 'e')
5758 : : {
5759 : 26264 : if (match_string_p ("nt"))
5760 : : {
5761 : : /* Matched "intent". */
5762 : 26263 : d = match_intent_spec ();
5763 : 26263 : if (d == INTENT_UNKNOWN)
5764 : : {
5765 : 2 : m = MATCH_ERROR;
5766 : 2 : goto cleanup;
5767 : : }
5768 : : }
5769 : : }
5770 : 6 : else if (ch == 'r')
5771 : : {
5772 : 6 : if (match_string_p ("insic"))
5773 : : {
5774 : : /* Matched "intrinsic". */
5775 : : d = DECL_INTRINSIC;
5776 : : }
5777 : : }
5778 : : }
5779 : : break;
5780 : :
5781 : 220 : case 'k':
5782 : 220 : if (match_string_p ("kind"))
5783 : : d = DECL_KIND;
5784 : : break;
5785 : :
5786 : 256 : case 'l':
5787 : 256 : if (match_string_p ("len"))
5788 : : d = DECL_LEN;
5789 : : break;
5790 : :
5791 : 5040 : case 'o':
5792 : 5040 : if (match_string_p ("optional"))
5793 : : d = DECL_OPTIONAL;
5794 : : break;
5795 : :
5796 : 26495 : case 'p':
5797 : 26495 : gfc_next_ascii_char ();
5798 : 26495 : switch (gfc_next_ascii_char ())
5799 : : {
5800 : 13925 : case 'a':
5801 : 13925 : if (match_string_p ("rameter"))
5802 : : {
5803 : : /* Matched "parameter". */
5804 : : d = DECL_PARAMETER;
5805 : : }
5806 : : break;
5807 : :
5808 : 12051 : case 'o':
5809 : 12051 : if (match_string_p ("inter"))
5810 : : {
5811 : : /* Matched "pointer". */
5812 : : d = DECL_POINTER;
5813 : : }
5814 : : break;
5815 : :
5816 : 267 : case 'r':
5817 : 267 : ch = gfc_next_ascii_char ();
5818 : 267 : if (ch == 'i')
5819 : : {
5820 : 216 : if (match_string_p ("vate"))
5821 : : {
5822 : : /* Matched "private". */
5823 : : d = DECL_PRIVATE;
5824 : : }
5825 : : }
5826 : 51 : else if (ch == 'o')
5827 : : {
5828 : 51 : if (match_string_p ("tected"))
5829 : : {
5830 : : /* Matched "protected". */
5831 : : d = DECL_PROTECTED;
5832 : : }
5833 : : }
5834 : : break;
5835 : :
5836 : 252 : case 'u':
5837 : 252 : if (match_string_p ("blic"))
5838 : : {
5839 : : /* Matched "public". */
5840 : : d = DECL_PUBLIC;
5841 : : }
5842 : : break;
5843 : : }
5844 : : break;
5845 : :
5846 : 1155 : case 's':
5847 : 1155 : gfc_next_ascii_char ();
5848 : 1155 : switch (gfc_next_ascii_char ())
5849 : : {
5850 : 1142 : case 'a':
5851 : 1142 : if (match_string_p ("ve"))
5852 : : {
5853 : : /* Matched "save". */
5854 : : d = DECL_SAVE;
5855 : : }
5856 : : break;
5857 : :
5858 : 13 : case 't':
5859 : 13 : if (match_string_p ("atic"))
5860 : : {
5861 : : /* Matched "static". */
5862 : : d = DECL_STATIC;
5863 : : }
5864 : : break;
5865 : : }
5866 : : break;
5867 : :
5868 : 5238 : case 't':
5869 : 5238 : if (match_string_p ("target"))
5870 : : d = DECL_TARGET;
5871 : : break;
5872 : :
5873 : 10506 : case 'v':
5874 : 10506 : gfc_next_ascii_char ();
5875 : 10506 : ch = gfc_next_ascii_char ();
5876 : 10506 : if (ch == 'a')
5877 : : {
5878 : 10004 : if (match_string_p ("lue"))
5879 : : {
5880 : : /* Matched "value". */
5881 : : d = DECL_VALUE;
5882 : : }
5883 : : }
5884 : 502 : else if (ch == 'o')
5885 : : {
5886 : 502 : if (match_string_p ("latile"))
5887 : : {
5888 : : /* Matched "volatile". */
5889 : : d = DECL_VOLATILE;
5890 : : }
5891 : : }
5892 : : break;
5893 : : }
5894 : : }
5895 : :
5896 : : /* No double colon and no recognizable decl_type, so assume that
5897 : : we've been looking at something else the whole time. */
5898 : : if (d == DECL_NONE)
5899 : : {
5900 : 32433 : m = MATCH_NO;
5901 : 32433 : goto cleanup;
5902 : : }
5903 : :
5904 : : /* Check to make sure any parens are paired up correctly. */
5905 : 114665 : if (gfc_match_parens () == MATCH_ERROR)
5906 : : {
5907 : 1 : m = MATCH_ERROR;
5908 : 1 : goto cleanup;
5909 : : }
5910 : :
5911 : 114664 : seen[d]++;
5912 : 114664 : seen_at[d] = gfc_current_locus;
5913 : :
5914 : 114664 : if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5915 : : {
5916 : 19515 : gfc_array_spec *as = NULL;
5917 : :
5918 : 19515 : m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5919 : : d == DECL_CODIMENSION);
5920 : :
5921 : 19515 : if (current_as == NULL)
5922 : 19497 : current_as = as;
5923 : 18 : else if (m == MATCH_YES)
5924 : : {
5925 : 18 : if (!merge_array_spec (as, current_as, false))
5926 : 2 : m = MATCH_ERROR;
5927 : 18 : free (as);
5928 : : }
5929 : :
5930 : 19515 : if (m == MATCH_NO)
5931 : : {
5932 : 0 : if (d == DECL_CODIMENSION)
5933 : 0 : gfc_error ("Missing codimension specification at %C");
5934 : : else
5935 : 0 : gfc_error ("Missing dimension specification at %C");
5936 : : m = MATCH_ERROR;
5937 : : }
5938 : :
5939 : 19515 : if (m == MATCH_ERROR)
5940 : 7 : goto cleanup;
5941 : : }
5942 : : }
5943 : :
5944 : : /* Since we've seen a double colon, we have to be looking at an
5945 : : attr-spec. This means that we can now issue errors. */
5946 : 4761024 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5947 : 4584691 : if (seen[d] > 1)
5948 : : {
5949 : 2 : switch (d)
5950 : : {
5951 : : case DECL_ALLOCATABLE:
5952 : : attr = "ALLOCATABLE";
5953 : : break;
5954 : 0 : case DECL_ASYNCHRONOUS:
5955 : 0 : attr = "ASYNCHRONOUS";
5956 : 0 : break;
5957 : 0 : case DECL_CODIMENSION:
5958 : 0 : attr = "CODIMENSION";
5959 : 0 : break;
5960 : 0 : case DECL_CONTIGUOUS:
5961 : 0 : attr = "CONTIGUOUS";
5962 : 0 : break;
5963 : 0 : case DECL_DIMENSION:
5964 : 0 : attr = "DIMENSION";
5965 : 0 : break;
5966 : 0 : case DECL_EXTERNAL:
5967 : 0 : attr = "EXTERNAL";
5968 : 0 : break;
5969 : 0 : case DECL_IN:
5970 : 0 : attr = "INTENT (IN)";
5971 : 0 : break;
5972 : 0 : case DECL_OUT:
5973 : 0 : attr = "INTENT (OUT)";
5974 : 0 : break;
5975 : 0 : case DECL_INOUT:
5976 : 0 : attr = "INTENT (IN OUT)";
5977 : 0 : break;
5978 : 0 : case DECL_INTRINSIC:
5979 : 0 : attr = "INTRINSIC";
5980 : 0 : break;
5981 : 0 : case DECL_OPTIONAL:
5982 : 0 : attr = "OPTIONAL";
5983 : 0 : break;
5984 : 0 : case DECL_KIND:
5985 : 0 : attr = "KIND";
5986 : 0 : break;
5987 : 0 : case DECL_LEN:
5988 : 0 : attr = "LEN";
5989 : 0 : break;
5990 : 0 : case DECL_PARAMETER:
5991 : 0 : attr = "PARAMETER";
5992 : 0 : break;
5993 : 0 : case DECL_POINTER:
5994 : 0 : attr = "POINTER";
5995 : 0 : break;
5996 : 0 : case DECL_PROTECTED:
5997 : 0 : attr = "PROTECTED";
5998 : 0 : break;
5999 : 0 : case DECL_PRIVATE:
6000 : 0 : attr = "PRIVATE";
6001 : 0 : break;
6002 : 0 : case DECL_PUBLIC:
6003 : 0 : attr = "PUBLIC";
6004 : 0 : break;
6005 : 0 : case DECL_SAVE:
6006 : 0 : attr = "SAVE";
6007 : 0 : break;
6008 : 0 : case DECL_STATIC:
6009 : 0 : attr = "STATIC";
6010 : 0 : break;
6011 : 1 : case DECL_AUTOMATIC:
6012 : 1 : attr = "AUTOMATIC";
6013 : 1 : break;
6014 : 0 : case DECL_TARGET:
6015 : 0 : attr = "TARGET";
6016 : 0 : break;
6017 : 0 : case DECL_IS_BIND_C:
6018 : 0 : attr = "IS_BIND_C";
6019 : 0 : break;
6020 : 0 : case DECL_VALUE:
6021 : 0 : attr = "VALUE";
6022 : 0 : break;
6023 : 1 : case DECL_VOLATILE:
6024 : 1 : attr = "VOLATILE";
6025 : 1 : break;
6026 : 0 : default:
6027 : 0 : attr = NULL; /* This shouldn't happen. */
6028 : : }
6029 : :
6030 : 2 : gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
6031 : 2 : m = MATCH_ERROR;
6032 : 2 : goto cleanup;
6033 : : }
6034 : :
6035 : : /* Now that we've dealt with duplicate attributes, add the attributes
6036 : : to the current attribute. */
6037 : 4760204 : for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
6038 : : {
6039 : 4583944 : if (seen[d] == 0)
6040 : 4469296 : continue;
6041 : : else
6042 : 114648 : attr_seen = 1;
6043 : :
6044 : 114648 : if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
6045 : 52 : && !flag_dec_static)
6046 : : {
6047 : 3 : gfc_error ("%s at %L is a DEC extension, enable with "
6048 : : "%<-fdec-static%>",
6049 : : d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
6050 : 2 : m = MATCH_ERROR;
6051 : 2 : goto cleanup;
6052 : : }
6053 : : /* Allow SAVE with STATIC, but don't complain. */
6054 : 50 : if (d == DECL_STATIC && seen[DECL_SAVE])
6055 : 0 : continue;
6056 : :
6057 : 114646 : if (gfc_comp_struct (gfc_current_state ())
6058 : 6228 : && d != DECL_DIMENSION && d != DECL_CODIMENSION
6059 : 5286 : && d != DECL_POINTER && d != DECL_PRIVATE
6060 : 3677 : && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
6061 : : {
6062 : 3620 : bool is_derived = gfc_current_state () == COMP_DERIVED;
6063 : 3620 : if (d == DECL_ALLOCATABLE)
6064 : : {
6065 : 3131 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6066 : : ? G_("ALLOCATABLE attribute at %C in a "
6067 : : "TYPE definition")
6068 : : : G_("ALLOCATABLE attribute at %C in a "
6069 : : "STRUCTURE definition")))
6070 : : {
6071 : 2 : m = MATCH_ERROR;
6072 : 2 : goto cleanup;
6073 : : }
6074 : : }
6075 : 489 : else if (d == DECL_KIND)
6076 : : {
6077 : 218 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6078 : : ? G_("KIND attribute at %C in a "
6079 : : "TYPE definition")
6080 : : : G_("KIND attribute at %C in a "
6081 : : "STRUCTURE definition")))
6082 : : {
6083 : 1 : m = MATCH_ERROR;
6084 : 1 : goto cleanup;
6085 : : }
6086 : 217 : if (current_ts.type != BT_INTEGER)
6087 : : {
6088 : 2 : gfc_error ("Component with KIND attribute at %C must be "
6089 : : "INTEGER");
6090 : 2 : m = MATCH_ERROR;
6091 : 2 : goto cleanup;
6092 : : }
6093 : : }
6094 : 271 : else if (d == DECL_LEN)
6095 : : {
6096 : 255 : if (!gfc_notify_std (GFC_STD_F2003, is_derived
6097 : : ? G_("LEN attribute at %C in a "
6098 : : "TYPE definition")
6099 : : : G_("LEN attribute at %C in a "
6100 : : "STRUCTURE definition")))
6101 : : {
6102 : 0 : m = MATCH_ERROR;
6103 : 0 : goto cleanup;
6104 : : }
6105 : 255 : if (current_ts.type != BT_INTEGER)
6106 : : {
6107 : 1 : gfc_error ("Component with LEN attribute at %C must be "
6108 : : "INTEGER");
6109 : 1 : m = MATCH_ERROR;
6110 : 1 : goto cleanup;
6111 : : }
6112 : : }
6113 : : else
6114 : : {
6115 : 32 : gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
6116 : : "TYPE definition")
6117 : : : G_("Attribute at %L is not allowed in a "
6118 : : "STRUCTURE definition"), &seen_at[d]);
6119 : 16 : m = MATCH_ERROR;
6120 : 16 : goto cleanup;
6121 : : }
6122 : : }
6123 : :
6124 : 114624 : if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
6125 : 468 : && gfc_current_state () != COMP_MODULE)
6126 : : {
6127 : 147 : if (d == DECL_PRIVATE)
6128 : : attr = "PRIVATE";
6129 : : else
6130 : 43 : attr = "PUBLIC";
6131 : 147 : if (gfc_current_state () == COMP_DERIVED
6132 : 141 : && gfc_state_stack->previous
6133 : 141 : && gfc_state_stack->previous->state == COMP_MODULE)
6134 : : {
6135 : 138 : if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
6136 : : "at %L in a TYPE definition", attr,
6137 : : &seen_at[d]))
6138 : : {
6139 : 2 : m = MATCH_ERROR;
6140 : 2 : goto cleanup;
6141 : : }
6142 : : }
6143 : : else
6144 : : {
6145 : 9 : gfc_error ("%s attribute at %L is not allowed outside of the "
6146 : : "specification part of a module", attr, &seen_at[d]);
6147 : 9 : m = MATCH_ERROR;
6148 : 9 : goto cleanup;
6149 : : }
6150 : : }
6151 : :
6152 : 114613 : if (gfc_current_state () != COMP_DERIVED
6153 : 108416 : && (d == DECL_KIND || d == DECL_LEN))
6154 : : {
6155 : 3 : gfc_error ("Attribute at %L is not allowed outside a TYPE "
6156 : : "definition", &seen_at[d]);
6157 : 3 : m = MATCH_ERROR;
6158 : 3 : goto cleanup;
6159 : : }
6160 : :
6161 : 114610 : switch (d)
6162 : : {
6163 : 17527 : case DECL_ALLOCATABLE:
6164 : 17527 : t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
6165 : 17527 : break;
6166 : :
6167 : 23 : case DECL_ASYNCHRONOUS:
6168 : 23 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
6169 : : t = false;
6170 : : else
6171 : 23 : t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
6172 : : break;
6173 : :
6174 : 44 : case DECL_CODIMENSION:
6175 : 44 : t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
6176 : 44 : break;
6177 : :
6178 : 2038 : case DECL_CONTIGUOUS:
6179 : 2038 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
6180 : : t = false;
6181 : : else
6182 : 2037 : t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
6183 : : break;
6184 : :
6185 : 19462 : case DECL_DIMENSION:
6186 : 19462 : t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
6187 : 19462 : break;
6188 : :
6189 : 176 : case DECL_EXTERNAL:
6190 : 176 : t = gfc_add_external (¤t_attr, &seen_at[d]);
6191 : 176 : break;
6192 : :
6193 : 19808 : case DECL_IN:
6194 : 19808 : t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
6195 : 19808 : break;
6196 : :
6197 : 3540 : case DECL_OUT:
6198 : 3540 : t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
6199 : 3540 : break;
6200 : :
6201 : 2909 : case DECL_INOUT:
6202 : 2909 : t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
6203 : 2909 : break;
6204 : :
6205 : 5 : case DECL_INTRINSIC:
6206 : 5 : t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
6207 : 5 : break;
6208 : :
6209 : 5039 : case DECL_OPTIONAL:
6210 : 5039 : t = gfc_add_optional (¤t_attr, &seen_at[d]);
6211 : 5039 : break;
6212 : :
6213 : 215 : case DECL_KIND:
6214 : 215 : t = gfc_add_kind (¤t_attr, &seen_at[d]);
6215 : 215 : break;
6216 : :
6217 : 254 : case DECL_LEN:
6218 : 254 : t = gfc_add_len (¤t_attr, &seen_at[d]);
6219 : 254 : break;
6220 : :
6221 : 13924 : case DECL_PARAMETER:
6222 : 13924 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
6223 : 13924 : break;
6224 : :
6225 : 12050 : case DECL_POINTER:
6226 : 12050 : t = gfc_add_pointer (¤t_attr, &seen_at[d]);
6227 : 12050 : break;
6228 : :
6229 : 50 : case DECL_PROTECTED:
6230 : 50 : if (gfc_current_state () != COMP_MODULE
6231 : 48 : || (gfc_current_ns->proc_name
6232 : 48 : && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
6233 : : {
6234 : 2 : gfc_error ("PROTECTED at %C only allowed in specification "
6235 : : "part of a module");
6236 : 2 : t = false;
6237 : 2 : break;
6238 : : }
6239 : :
6240 : 48 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
6241 : : t = false;
6242 : : else
6243 : 44 : t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
6244 : : break;
6245 : :
6246 : 213 : case DECL_PRIVATE:
6247 : 213 : t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
6248 : : &seen_at[d]);
6249 : 213 : break;
6250 : :
6251 : 244 : case DECL_PUBLIC:
6252 : 244 : t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
6253 : : &seen_at[d]);
6254 : 244 : break;
6255 : :
6256 : 1152 : case DECL_STATIC:
6257 : 1152 : case DECL_SAVE:
6258 : 1152 : t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
6259 : 1152 : break;
6260 : :
6261 : 37 : case DECL_AUTOMATIC:
6262 : 37 : t = gfc_add_automatic (¤t_attr, NULL, &seen_at[d]);
6263 : 37 : break;
6264 : :
6265 : 5236 : case DECL_TARGET:
6266 : 5236 : t = gfc_add_target (¤t_attr, &seen_at[d]);
6267 : 5236 : break;
6268 : :
6269 : 162 : case DECL_IS_BIND_C:
6270 : 162 : t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
6271 : 162 : break;
6272 : :
6273 : 10003 : case DECL_VALUE:
6274 : 10003 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
6275 : : t = false;
6276 : : else
6277 : 10003 : t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
6278 : : break;
6279 : :
6280 : 499 : case DECL_VOLATILE:
6281 : 499 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
6282 : : t = false;
6283 : : else
6284 : 498 : t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
6285 : : break;
6286 : :
6287 : 0 : default:
6288 : 0 : gfc_internal_error ("match_attr_spec(): Bad attribute");
6289 : : }
6290 : :
6291 : 114604 : if (!t)
6292 : : {
6293 : 35 : m = MATCH_ERROR;
6294 : 35 : goto cleanup;
6295 : : }
6296 : : }
6297 : :
6298 : : /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
6299 : 176260 : if ((gfc_current_state () == COMP_MODULE
6300 : 176260 : || gfc_current_state () == COMP_SUBMODULE)
6301 : 5506 : && !current_attr.save
6302 : 5324 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
6303 : 5232 : current_attr.save = SAVE_IMPLICIT;
6304 : :
6305 : 176260 : colon_seen = 1;
6306 : 176260 : return MATCH_YES;
6307 : :
6308 : 32518 : cleanup:
6309 : 32518 : gfc_current_locus = start;
6310 : 32518 : gfc_free_array_spec (current_as);
6311 : 32518 : current_as = NULL;
6312 : 32518 : attr_seen = 0;
6313 : 32518 : return m;
6314 : : }
6315 : :
6316 : :
6317 : : /* Set the binding label, dest_label, either with the binding label
6318 : : stored in the given gfc_typespec, ts, or if none was provided, it
6319 : : will be the symbol name in all lower case, as required by the draft
6320 : : (J3/04-007, section 15.4.1). If a binding label was given and
6321 : : there is more than one argument (num_idents), it is an error. */
6322 : :
6323 : : static bool
6324 : 310 : set_binding_label (const char **dest_label, const char *sym_name,
6325 : : int num_idents)
6326 : : {
6327 : 310 : if (num_idents > 1 && has_name_equals)
6328 : : {
6329 : 4 : gfc_error ("Multiple identifiers provided with "
6330 : : "single NAME= specifier at %C");
6331 : 4 : return false;
6332 : : }
6333 : :
6334 : 306 : if (curr_binding_label)
6335 : : /* Binding label given; store in temp holder till have sym. */
6336 : 107 : *dest_label = curr_binding_label;
6337 : : else
6338 : : {
6339 : : /* No binding label given, and the NAME= specifier did not exist,
6340 : : which means there was no NAME="". */
6341 : 199 : if (sym_name != NULL && has_name_equals == 0)
6342 : 169 : *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
6343 : : }
6344 : :
6345 : : return true;
6346 : : }
6347 : :
6348 : :
6349 : : /* Set the status of the given common block as being BIND(C) or not,
6350 : : depending on the given parameter, is_bind_c. */
6351 : :
6352 : : static void
6353 : 76 : set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
6354 : : {
6355 : 76 : com_block->is_bind_c = is_bind_c;
6356 : 76 : return;
6357 : : }
6358 : :
6359 : :
6360 : : /* Verify that the given gfc_typespec is for a C interoperable type. */
6361 : :
6362 : : bool
6363 : 19896 : gfc_verify_c_interop (gfc_typespec *ts)
6364 : : {
6365 : 19896 : if (ts->type == BT_DERIVED && ts->u.derived != NULL)
6366 : 4276 : return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
6367 : 8509 : ? true : false;
6368 : 15636 : else if (ts->type == BT_CLASS)
6369 : : return false;
6370 : 15628 : else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
6371 : 3897 : return false;
6372 : :
6373 : : return true;
6374 : : }
6375 : :
6376 : :
6377 : : /* Verify that the variables of a given common block, which has been
6378 : : defined with the attribute specifier bind(c), to be of a C
6379 : : interoperable type. Errors will be reported here, if
6380 : : encountered. */
6381 : :
6382 : : bool
6383 : 1 : verify_com_block_vars_c_interop (gfc_common_head *com_block)
6384 : : {
6385 : 1 : gfc_symbol *curr_sym = NULL;
6386 : 1 : bool retval = true;
6387 : :
6388 : 1 : curr_sym = com_block->head;
6389 : :
6390 : : /* Make sure we have at least one symbol. */
6391 : 1 : if (curr_sym == NULL)
6392 : : return retval;
6393 : :
6394 : : /* Here we know we have a symbol, so we'll execute this loop
6395 : : at least once. */
6396 : 1 : do
6397 : : {
6398 : : /* The second to last param, 1, says this is in a common block. */
6399 : 1 : retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
6400 : 1 : curr_sym = curr_sym->common_next;
6401 : 1 : } while (curr_sym != NULL);
6402 : :
6403 : : return retval;
6404 : : }
6405 : :
6406 : :
6407 : : /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
6408 : : an appropriate error message is reported. */
6409 : :
6410 : : bool
6411 : 6745 : verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
6412 : : int is_in_common, gfc_common_head *com_block)
6413 : : {
6414 : 6745 : bool bind_c_function = false;
6415 : 6745 : bool retval = true;
6416 : :
6417 : 6745 : if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
6418 : 6745 : bind_c_function = true;
6419 : :
6420 : 6745 : if (tmp_sym->attr.function && tmp_sym->result != NULL)
6421 : : {
6422 : 2583 : tmp_sym = tmp_sym->result;
6423 : : /* Make sure it wasn't an implicitly typed result. */
6424 : 2583 : if (tmp_sym->attr.implicit_type && warn_c_binding_type)
6425 : : {
6426 : 1 : gfc_warning (OPT_Wc_binding_type,
6427 : : "Implicitly declared BIND(C) function %qs at "
6428 : : "%L may not be C interoperable", tmp_sym->name,
6429 : : &tmp_sym->declared_at);
6430 : 1 : tmp_sym->ts.f90_type = tmp_sym->ts.type;
6431 : : /* Mark it as C interoperable to prevent duplicate warnings. */
6432 : 1 : tmp_sym->ts.is_c_interop = 1;
6433 : 1 : tmp_sym->attr.is_c_interop = 1;
6434 : : }
6435 : : }
6436 : :
6437 : : /* Here, we know we have the bind(c) attribute, so if we have
6438 : : enough type info, then verify that it's a C interop kind.
6439 : : The info could be in the symbol already, or possibly still in
6440 : : the given ts (current_ts), so look in both. */
6441 : 6745 : if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
6442 : : {
6443 : 2741 : if (!gfc_verify_c_interop (&(tmp_sym->ts)))
6444 : : {
6445 : : /* See if we're dealing with a sym in a common block or not. */
6446 : 162 : if (is_in_common == 1 && warn_c_binding_type)
6447 : : {
6448 : 0 : gfc_warning (OPT_Wc_binding_type,
6449 : : "Variable %qs in common block %qs at %L "
6450 : : "may not be a C interoperable "
6451 : : "kind though common block %qs is BIND(C)",
6452 : : tmp_sym->name, com_block->name,
6453 : 0 : &(tmp_sym->declared_at), com_block->name);
6454 : : }
6455 : : else
6456 : : {
6457 : 162 : if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
6458 : 160 : || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
6459 : : {
6460 : 3 : gfc_error ("Type declaration %qs at %L is not C "
6461 : : "interoperable but it is BIND(C)",
6462 : : tmp_sym->name, &(tmp_sym->declared_at));
6463 : 3 : retval = false;
6464 : : }
6465 : 159 : else if (warn_c_binding_type)
6466 : 3 : gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
6467 : : "may not be a C interoperable "
6468 : : "kind but it is BIND(C)",
6469 : : tmp_sym->name, &(tmp_sym->declared_at));
6470 : : }
6471 : : }
6472 : :
6473 : : /* Variables declared w/in a common block can't be bind(c)
6474 : : since there's no way for C to see these variables, so there's
6475 : : semantically no reason for the attribute. */
6476 : 2741 : if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
6477 : : {
6478 : 1 : gfc_error ("Variable %qs in common block %qs at "
6479 : : "%L cannot be declared with BIND(C) "
6480 : : "since it is not a global",
6481 : 1 : tmp_sym->name, com_block->name,
6482 : : &(tmp_sym->declared_at));
6483 : 1 : retval = false;
6484 : : }
6485 : :
6486 : : /* Scalar variables that are bind(c) cannot have the pointer
6487 : : or allocatable attributes. */
6488 : 2741 : if (tmp_sym->attr.is_bind_c == 1)
6489 : : {
6490 : 2221 : if (tmp_sym->attr.pointer == 1)
6491 : : {
6492 : 1 : gfc_error ("Variable %qs at %L cannot have both the "
6493 : : "POINTER and BIND(C) attributes",
6494 : : tmp_sym->name, &(tmp_sym->declared_at));
6495 : 1 : retval = false;
6496 : : }
6497 : :
6498 : 2221 : if (tmp_sym->attr.allocatable == 1)
6499 : : {
6500 : 0 : gfc_error ("Variable %qs at %L cannot have both the "
6501 : : "ALLOCATABLE and BIND(C) attributes",
6502 : : tmp_sym->name, &(tmp_sym->declared_at));
6503 : 0 : retval = false;
6504 : : }
6505 : :
6506 : : }
6507 : :
6508 : : /* If it is a BIND(C) function, make sure the return value is a
6509 : : scalar value. The previous tests in this function made sure
6510 : : the type is interoperable. */
6511 : 2741 : if (bind_c_function && tmp_sym->as != NULL)
6512 : 2 : gfc_error ("Return type of BIND(C) function %qs at %L cannot "
6513 : : "be an array", tmp_sym->name, &(tmp_sym->declared_at));
6514 : :
6515 : : /* BIND(C) functions cannot return a character string. */
6516 : 2583 : if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
6517 : 68 : if (!gfc_length_one_character_type_p (&tmp_sym->ts))
6518 : 4 : gfc_error ("Return type of BIND(C) function %qs of character "
6519 : : "type at %L must have length 1", tmp_sym->name,
6520 : : &(tmp_sym->declared_at));
6521 : : }
6522 : :
6523 : : /* See if the symbol has been marked as private. If it has, warn if
6524 : : there is a binding label with default binding name. */
6525 : 6745 : if (tmp_sym->attr.access == ACCESS_PRIVATE
6526 : 11 : && tmp_sym->binding_label
6527 : 8 : && strcmp (tmp_sym->name, tmp_sym->binding_label) == 0
6528 : 5 : && (tmp_sym->attr.flavor == FL_VARIABLE
6529 : 4 : || tmp_sym->attr.if_source == IFSRC_DECL))
6530 : 4 : gfc_warning (OPT_Wsurprising,
6531 : : "Symbol %qs at %L is marked PRIVATE but is accessible "
6532 : : "via its default binding name %qs", tmp_sym->name,
6533 : : &(tmp_sym->declared_at), tmp_sym->binding_label);
6534 : :
6535 : 6745 : return retval;
6536 : : }
6537 : :
6538 : :
6539 : : /* Set the appropriate fields for a symbol that's been declared as
6540 : : BIND(C) (the is_bind_c flag and the binding label), and verify that
6541 : : the type is C interoperable. Errors are reported by the functions
6542 : : used to set/test these fields. */
6543 : :
6544 : : static bool
6545 : 47 : set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
6546 : : {
6547 : 47 : bool retval = true;
6548 : :
6549 : : /* TODO: Do we need to make sure the vars aren't marked private? */
6550 : :
6551 : : /* Set the is_bind_c bit in symbol_attribute. */
6552 : 47 : gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
6553 : :
6554 : 47 : if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
6555 : : return false;
6556 : :
6557 : : return retval;
6558 : : }
6559 : :
6560 : :
6561 : : /* Set the fields marking the given common block as BIND(C), including
6562 : : a binding label, and report any errors encountered. */
6563 : :
6564 : : static bool
6565 : 76 : set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
6566 : : {
6567 : 76 : bool retval = true;
6568 : :
6569 : : /* destLabel, common name, typespec (which may have binding label). */
6570 : 76 : if (!set_binding_label (&com_block->binding_label, com_block->name,
6571 : : num_idents))
6572 : : return false;
6573 : :
6574 : : /* Set the given common block (com_block) to being bind(c) (1). */
6575 : 76 : set_com_block_bind_c (com_block, 1);
6576 : :
6577 : 76 : return retval;
6578 : : }
6579 : :
6580 : :
6581 : : /* Retrieve the list of one or more identifiers that the given bind(c)
6582 : : attribute applies to. */
6583 : :
6584 : : static bool
6585 : 102 : get_bind_c_idents (void)
6586 : : {
6587 : 102 : char name[GFC_MAX_SYMBOL_LEN + 1];
6588 : 102 : int num_idents = 0;
6589 : 102 : gfc_symbol *tmp_sym = NULL;
6590 : 102 : match found_id;
6591 : 102 : gfc_common_head *com_block = NULL;
6592 : :
6593 : 102 : if (gfc_match_name (name) == MATCH_YES)
6594 : : {
6595 : 38 : found_id = MATCH_YES;
6596 : 38 : gfc_get_ha_symbol (name, &tmp_sym);
6597 : : }
6598 : 64 : else if (gfc_match_common_name (name) == MATCH_YES)
6599 : : {
6600 : 64 : found_id = MATCH_YES;
6601 : 64 : com_block = gfc_get_common (name, 0);
6602 : : }
6603 : : else
6604 : : {
6605 : 0 : gfc_error ("Need either entity or common block name for "
6606 : : "attribute specification statement at %C");
6607 : 0 : return false;
6608 : : }
6609 : :
6610 : : /* Save the current identifier and look for more. */
6611 : 123 : do
6612 : : {
6613 : : /* Increment the number of identifiers found for this spec stmt. */
6614 : 123 : num_idents++;
6615 : :
6616 : : /* Make sure we have a sym or com block, and verify that it can
6617 : : be bind(c). Set the appropriate field(s) and look for more
6618 : : identifiers. */
6619 : 123 : if (tmp_sym != NULL || com_block != NULL)
6620 : : {
6621 : 123 : if (tmp_sym != NULL)
6622 : : {
6623 : 47 : if (!set_verify_bind_c_sym (tmp_sym, num_idents))
6624 : : return false;
6625 : : }
6626 : : else
6627 : : {
6628 : 76 : if (!set_verify_bind_c_com_block (com_block, num_idents))
6629 : : return false;
6630 : : }
6631 : :
6632 : : /* Look to see if we have another identifier. */
6633 : 122 : tmp_sym = NULL;
6634 : 122 : if (gfc_match_eos () == MATCH_YES)
6635 : : found_id = MATCH_NO;
6636 : 21 : else if (gfc_match_char (',') != MATCH_YES)
6637 : : found_id = MATCH_NO;
6638 : 21 : else if (gfc_match_name (name) == MATCH_YES)
6639 : : {
6640 : 9 : found_id = MATCH_YES;
6641 : 9 : gfc_get_ha_symbol (name, &tmp_sym);
6642 : : }
6643 : 12 : else if (gfc_match_common_name (name) == MATCH_YES)
6644 : : {
6645 : 12 : found_id = MATCH_YES;
6646 : 12 : com_block = gfc_get_common (name, 0);
6647 : : }
6648 : : else
6649 : : {
6650 : 0 : gfc_error ("Missing entity or common block name for "
6651 : : "attribute specification statement at %C");
6652 : 0 : return false;
6653 : : }
6654 : : }
6655 : : else
6656 : : {
6657 : 0 : gfc_internal_error ("Missing symbol");
6658 : : }
6659 : 122 : } while (found_id == MATCH_YES);
6660 : :
6661 : : /* if we get here we were successful */
6662 : : return true;
6663 : : }
6664 : :
6665 : :
6666 : : /* Try and match a BIND(C) attribute specification statement. */
6667 : :
6668 : : match
6669 : 140 : gfc_match_bind_c_stmt (void)
6670 : : {
6671 : 140 : match found_match = MATCH_NO;
6672 : 140 : gfc_typespec *ts;
6673 : :
6674 : 140 : ts = ¤t_ts;
6675 : :
6676 : : /* This may not be necessary. */
6677 : 140 : gfc_clear_ts (ts);
6678 : : /* Clear the temporary binding label holder. */
6679 : 140 : curr_binding_label = NULL;
6680 : :
6681 : : /* Look for the bind(c). */
6682 : 140 : found_match = gfc_match_bind_c (NULL, true);
6683 : :
6684 : 140 : if (found_match == MATCH_YES)
6685 : : {
6686 : 103 : if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6687 : : return MATCH_ERROR;
6688 : :
6689 : : /* Look for the :: now, but it is not required. */
6690 : 102 : gfc_match (" :: ");
6691 : :
6692 : : /* Get the identifier(s) that needs to be updated. This may need to
6693 : : change to hand the flag(s) for the attr specified so all identifiers
6694 : : found can have all appropriate parts updated (assuming that the same
6695 : : spec stmt can have multiple attrs, such as both bind(c) and
6696 : : allocatable...). */
6697 : 102 : if (!get_bind_c_idents ())
6698 : : /* Error message should have printed already. */
6699 : : return MATCH_ERROR;
6700 : : }
6701 : :
6702 : : return found_match;
6703 : : }
6704 : :
6705 : :
6706 : : /* Match a data declaration statement. */
6707 : :
6708 : : match
6709 : 990535 : gfc_match_data_decl (void)
6710 : : {
6711 : 990535 : gfc_symbol *sym;
6712 : 990535 : match m;
6713 : 990535 : int elem;
6714 : :
6715 : 990535 : type_param_spec_list = NULL;
6716 : 990535 : decl_type_param_list = NULL;
6717 : :
6718 : 990535 : num_idents_on_line = 0;
6719 : :
6720 : 990535 : m = gfc_match_decl_type_spec (¤t_ts, 0);
6721 : 990535 : if (m != MATCH_YES)
6722 : : return m;
6723 : :
6724 : 207669 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6725 : 33220 : && !gfc_comp_struct (gfc_current_state ()))
6726 : : {
6727 : 30138 : sym = gfc_use_derived (current_ts.u.derived);
6728 : :
6729 : 30138 : if (sym == NULL)
6730 : : {
6731 : 15 : m = MATCH_ERROR;
6732 : 15 : goto cleanup;
6733 : : }
6734 : :
6735 : 30123 : current_ts.u.derived = sym;
6736 : : }
6737 : :
6738 : 207654 : m = match_attr_spec ();
6739 : 207654 : if (m == MATCH_ERROR)
6740 : : {
6741 : 84 : m = MATCH_NO;
6742 : 84 : goto cleanup;
6743 : : }
6744 : :
6745 : : /* F2018:C708. */
6746 : 207570 : if (current_ts.type == BT_CLASS && current_attr.flavor == FL_PARAMETER)
6747 : : {
6748 : 6 : gfc_error ("CLASS entity at %C cannot have the PARAMETER attribute");
6749 : 6 : m = MATCH_ERROR;
6750 : 6 : goto cleanup;
6751 : : }
6752 : :
6753 : 207564 : if (current_ts.type == BT_CLASS
6754 : 10408 : && current_ts.u.derived->attr.unlimited_polymorphic)
6755 : 1844 : goto ok;
6756 : :
6757 : 205720 : if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6758 : 31354 : && current_ts.u.derived->components == NULL
6759 : 2694 : && !current_ts.u.derived->attr.zero_comp)
6760 : : {
6761 : :
6762 : 202 : if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
6763 : 135 : goto ok;
6764 : :
6765 : 67 : if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
6766 : 40 : goto ok;
6767 : :
6768 : 27 : gfc_find_symbol (current_ts.u.derived->name,
6769 : 27 : current_ts.u.derived->ns, 1, &sym);
6770 : :
6771 : : /* Any symbol that we find had better be a type definition
6772 : : which has its components defined, or be a structure definition
6773 : : actively being parsed. */
6774 : 27 : if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
6775 : 26 : && (current_ts.u.derived->components != NULL
6776 : 26 : || current_ts.u.derived->attr.zero_comp
6777 : 26 : || current_ts.u.derived == gfc_new_block))
6778 : 26 : goto ok;
6779 : :
6780 : 1 : gfc_error ("Derived type at %C has not been previously defined "
6781 : : "and so cannot appear in a derived type definition");
6782 : 1 : m = MATCH_ERROR;
6783 : 1 : goto cleanup;
6784 : : }
6785 : :
6786 : 205518 : ok:
6787 : : /* If we have an old-style character declaration, and no new-style
6788 : : attribute specifications, then there a comma is optional between
6789 : : the type specification and the variable list. */
6790 : 207563 : if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6791 : 1407 : gfc_match_char (',');
6792 : :
6793 : : /* Give the types/attributes to symbols that follow. Give the element
6794 : : a number so that repeat character length expressions can be copied. */
6795 : : elem = 1;
6796 : 271278 : for (;;)
6797 : : {
6798 : 271278 : num_idents_on_line++;
6799 : 271278 : m = variable_decl (elem++);
6800 : 271276 : if (m == MATCH_ERROR)
6801 : 406 : goto cleanup;
6802 : 270870 : if (m == MATCH_NO)
6803 : : break;
6804 : :
6805 : 270859 : if (gfc_match_eos () == MATCH_YES)
6806 : 207123 : goto cleanup;
6807 : 63736 : if (gfc_match_char (',') != MATCH_YES)
6808 : : break;
6809 : : }
6810 : :
6811 : 32 : if (!gfc_error_flag_test ())
6812 : : {
6813 : : /* An anonymous structure declaration is unambiguous; if we matched one
6814 : : according to gfc_match_structure_decl, we need to return MATCH_YES
6815 : : here to avoid confusing the remaining matchers, even if there was an
6816 : : error during variable_decl. We must flush any such errors. Note this
6817 : : causes the parser to gracefully continue parsing the remaining input
6818 : : as a structure body, which likely follows. */
6819 : 8 : if (current_ts.type == BT_DERIVED && current_ts.u.derived
6820 : 1 : && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6821 : : {
6822 : 1 : gfc_error_now ("Syntax error in anonymous structure declaration"
6823 : : " at %C");
6824 : : /* Skip the bad variable_decl and line up for the start of the
6825 : : structure body. */
6826 : 1 : gfc_error_recovery ();
6827 : 1 : m = MATCH_YES;
6828 : 1 : goto cleanup;
6829 : : }
6830 : :
6831 : 7 : gfc_error ("Syntax error in data declaration at %C");
6832 : : }
6833 : :
6834 : 31 : m = MATCH_ERROR;
6835 : :
6836 : 31 : gfc_free_data_all (gfc_current_ns);
6837 : :
6838 : 207667 : cleanup:
6839 : 207667 : if (saved_kind_expr)
6840 : 144 : gfc_free_expr (saved_kind_expr);
6841 : 207667 : if (type_param_spec_list)
6842 : 686 : gfc_free_actual_arglist (type_param_spec_list);
6843 : 207667 : if (decl_type_param_list)
6844 : 713 : gfc_free_actual_arglist (decl_type_param_list);
6845 : 207667 : saved_kind_expr = NULL;
6846 : 207667 : gfc_free_array_spec (current_as);
6847 : 207667 : current_as = NULL;
6848 : 207667 : return m;
6849 : : }
6850 : :
6851 : : static bool
6852 : 23005 : in_module_or_interface(void)
6853 : : {
6854 : 23005 : if (gfc_current_state () == COMP_MODULE
6855 : 23005 : || gfc_current_state () == COMP_SUBMODULE
6856 : 23005 : || gfc_current_state () == COMP_INTERFACE)
6857 : : return true;
6858 : :
6859 : 19427 : if (gfc_state_stack->state == COMP_CONTAINS
6860 : 18642 : || gfc_state_stack->state == COMP_FUNCTION
6861 : 18546 : || gfc_state_stack->state == COMP_SUBROUTINE)
6862 : : {
6863 : 881 : gfc_state_data *p;
6864 : 924 : for (p = gfc_state_stack->previous; p ; p = p->previous)
6865 : : {
6866 : 920 : if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
6867 : 108 : || p->state == COMP_INTERFACE)
6868 : : return true;
6869 : : }
6870 : : }
6871 : : return false;
6872 : : }
6873 : :
6874 : : /* Match a prefix associated with a function or subroutine
6875 : : declaration. If the typespec pointer is nonnull, then a typespec
6876 : : can be matched. Note that if nothing matches, MATCH_YES is
6877 : : returned (the null string was matched). */
6878 : :
6879 : : match
6880 : 232846 : gfc_match_prefix (gfc_typespec *ts)
6881 : : {
6882 : 232846 : bool seen_type;
6883 : 232846 : bool seen_impure;
6884 : 232846 : bool found_prefix;
6885 : :
6886 : 232846 : gfc_clear_attr (¤t_attr);
6887 : 232846 : seen_type = false;
6888 : 232846 : seen_impure = false;
6889 : :
6890 : 232846 : gcc_assert (!gfc_matching_prefix);
6891 : 232846 : gfc_matching_prefix = true;
6892 : :
6893 : 242259 : do
6894 : : {
6895 : 261109 : found_prefix = false;
6896 : :
6897 : : /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6898 : : corresponding attribute seems natural and distinguishes these
6899 : : procedures from procedure types of PROC_MODULE, which these are
6900 : : as well. */
6901 : 261109 : if (gfc_match ("module% ") == MATCH_YES)
6902 : : {
6903 : 23280 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6904 : 275 : goto error;
6905 : :
6906 : 23005 : if (!in_module_or_interface ())
6907 : : {
6908 : 18550 : gfc_error ("MODULE prefix at %C found outside of a module, "
6909 : : "submodule, or interface");
6910 : 18550 : goto error;
6911 : : }
6912 : :
6913 : 4455 : current_attr.module_procedure = 1;
6914 : 4455 : found_prefix = true;
6915 : : }
6916 : :
6917 : 242284 : if (!seen_type && ts != NULL)
6918 : : {
6919 : 130320 : match m;
6920 : 130320 : m = gfc_match_decl_type_spec (ts, 0);
6921 : 130320 : if (m == MATCH_ERROR)
6922 : 15 : goto error;
6923 : 130305 : if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
6924 : : {
6925 : : seen_type = true;
6926 : : found_prefix = true;
6927 : : }
6928 : : }
6929 : :
6930 : 242269 : if (gfc_match ("elemental% ") == MATCH_YES)
6931 : : {
6932 : 5087 : if (!gfc_add_elemental (¤t_attr, NULL))
6933 : 2 : goto error;
6934 : :
6935 : : found_prefix = true;
6936 : : }
6937 : :
6938 : 242267 : if (gfc_match ("pure% ") == MATCH_YES)
6939 : : {
6940 : 2346 : if (!gfc_add_pure (¤t_attr, NULL))
6941 : 2 : goto error;
6942 : :
6943 : : found_prefix = true;
6944 : : }
6945 : :
6946 : 242265 : if (gfc_match ("recursive% ") == MATCH_YES)
6947 : : {
6948 : 463 : if (!gfc_add_recursive (¤t_attr, NULL))
6949 : 2 : goto error;
6950 : :
6951 : : found_prefix = true;
6952 : : }
6953 : :
6954 : : /* IMPURE is a somewhat special case, as it needs not set an actual
6955 : : attribute but rather only prevents ELEMENTAL routines from being
6956 : : automatically PURE. */
6957 : 242263 : if (gfc_match ("impure% ") == MATCH_YES)
6958 : : {
6959 : 615 : if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6960 : 4 : goto error;
6961 : :
6962 : : seen_impure = true;
6963 : : found_prefix = true;
6964 : : }
6965 : : }
6966 : : while (found_prefix);
6967 : :
6968 : : /* IMPURE and PURE must not both appear, of course. */
6969 : 213996 : if (seen_impure && current_attr.pure)
6970 : : {
6971 : 4 : gfc_error ("PURE and IMPURE must not appear both at %C");
6972 : 4 : goto error;
6973 : : }
6974 : :
6975 : : /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6976 : 213385 : if (!seen_impure && current_attr.elemental && !current_attr.pure)
6977 : : {
6978 : 4470 : if (!gfc_add_pure (¤t_attr, NULL))
6979 : 0 : goto error;
6980 : : }
6981 : :
6982 : : /* At this point, the next item is not a prefix. */
6983 : 213992 : gcc_assert (gfc_matching_prefix);
6984 : :
6985 : 213992 : gfc_matching_prefix = false;
6986 : 213992 : return MATCH_YES;
6987 : :
6988 : 18854 : error:
6989 : 18854 : gcc_assert (gfc_matching_prefix);
6990 : 18854 : gfc_matching_prefix = false;
6991 : 18854 : return MATCH_ERROR;
6992 : : }
6993 : :
6994 : :
6995 : : /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6996 : :
6997 : : static bool
6998 : 60750 : copy_prefix (symbol_attribute *dest, locus *where)
6999 : : {
7000 : 60750 : if (dest->module_procedure)
7001 : : {
7002 : 654 : if (current_attr.elemental)
7003 : 4 : dest->elemental = 1;
7004 : :
7005 : 654 : if (current_attr.pure)
7006 : 51 : dest->pure = 1;
7007 : :
7008 : 654 : if (current_attr.recursive)
7009 : 8 : dest->recursive = 1;
7010 : :
7011 : : /* Module procedures are unusual in that the 'dest' is copied from
7012 : : the interface declaration. However, this is an oportunity to
7013 : : check that the submodule declaration is compliant with the
7014 : : interface. */
7015 : 654 : if (dest->elemental && !current_attr.elemental)
7016 : : {
7017 : 1 : gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
7018 : : "missing at %L", where);
7019 : 1 : return false;
7020 : : }
7021 : :
7022 : 653 : if (dest->pure && !current_attr.pure)
7023 : : {
7024 : 1 : gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
7025 : : "missing at %L", where);
7026 : 1 : return false;
7027 : : }
7028 : :
7029 : 652 : if (dest->recursive && !current_attr.recursive)
7030 : : {
7031 : 1 : gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
7032 : : "missing at %L", where);
7033 : 1 : return false;
7034 : : }
7035 : :
7036 : : return true;
7037 : : }
7038 : :
7039 : 60096 : if (current_attr.elemental && !gfc_add_elemental (dest, where))
7040 : : return false;
7041 : :
7042 : 60094 : if (current_attr.pure && !gfc_add_pure (dest, where))
7043 : : return false;
7044 : :
7045 : 60094 : if (current_attr.recursive && !gfc_add_recursive (dest, where))
7046 : : return false;
7047 : :
7048 : : return true;
7049 : : }
7050 : :
7051 : :
7052 : : /* Match a formal argument list or, if typeparam is true, a
7053 : : type_param_name_list. */
7054 : :
7055 : : match
7056 : 467607 : gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
7057 : : int null_flag, bool typeparam)
7058 : : {
7059 : 467607 : gfc_formal_arglist *head, *tail, *p, *q;
7060 : 467607 : char name[GFC_MAX_SYMBOL_LEN + 1];
7061 : 467607 : gfc_symbol *sym;
7062 : 467607 : match m;
7063 : 467607 : gfc_formal_arglist *formal = NULL;
7064 : :
7065 : 467607 : head = tail = NULL;
7066 : :
7067 : : /* Keep the interface formal argument list and null it so that the
7068 : : matching for the new declaration can be done. The numbers and
7069 : : names of the arguments are checked here. The interface formal
7070 : : arguments are retained in formal_arglist and the characteristics
7071 : : are compared in resolve.cc(resolve_fl_procedure). See the remark
7072 : : in get_proc_name about the eventual need to copy the formal_arglist
7073 : : and populate the formal namespace of the interface symbol. */
7074 : 467607 : if (progname->attr.module_procedure
7075 : 658 : && progname->attr.host_assoc)
7076 : : {
7077 : 179 : formal = progname->formal;
7078 : 179 : progname->formal = NULL;
7079 : : }
7080 : :
7081 : 467607 : if (gfc_match_char ('(') != MATCH_YES)
7082 : : {
7083 : 276908 : if (null_flag)
7084 : 6280 : goto ok;
7085 : : return MATCH_NO;
7086 : : }
7087 : :
7088 : 190699 : if (gfc_match_char (')') == MATCH_YES)
7089 : : {
7090 : 10135 : if (typeparam)
7091 : : {
7092 : 1 : gfc_error_now ("A type parameter list is required at %C");
7093 : 1 : m = MATCH_ERROR;
7094 : 1 : goto cleanup;
7095 : : }
7096 : : else
7097 : 10134 : goto ok;
7098 : : }
7099 : :
7100 : 241446 : for (;;)
7101 : : {
7102 : 241446 : gfc_gobble_whitespace ();
7103 : 241446 : if (gfc_match_char ('*') == MATCH_YES)
7104 : : {
7105 : 10203 : sym = NULL;
7106 : 10203 : if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
7107 : : "Alternate-return argument at %C"))
7108 : : {
7109 : 1 : m = MATCH_ERROR;
7110 : 1 : goto cleanup;
7111 : : }
7112 : 10202 : else if (typeparam)
7113 : 2 : gfc_error_now ("A parameter name is required at %C");
7114 : : }
7115 : : else
7116 : : {
7117 : 231243 : locus loc = gfc_current_locus;
7118 : 231243 : m = gfc_match_name (name);
7119 : 231243 : if (m != MATCH_YES)
7120 : : {
7121 : 15714 : if(typeparam)
7122 : 1 : gfc_error_now ("A parameter name is required at %C");
7123 : 15730 : goto cleanup;
7124 : : }
7125 : 215529 : loc = gfc_get_location_range (NULL, 0, &loc, 1, &gfc_current_locus);
7126 : :
7127 : 215529 : if (!typeparam && gfc_get_symbol (name, NULL, &sym, &loc))
7128 : 16 : goto cleanup;
7129 : 215513 : else if (typeparam
7130 : 215513 : && gfc_get_symbol (name, progname->f2k_derived, &sym, &loc))
7131 : 0 : goto cleanup;
7132 : : }
7133 : :
7134 : 225715 : p = gfc_get_formal_arglist ();
7135 : :
7136 : 225715 : if (head == NULL)
7137 : : head = tail = p;
7138 : : else
7139 : : {
7140 : 60179 : tail->next = p;
7141 : 60179 : tail = p;
7142 : : }
7143 : :
7144 : 225715 : tail->sym = sym;
7145 : :
7146 : : /* We don't add the VARIABLE flavor because the name could be a
7147 : : dummy procedure. We don't apply these attributes to formal
7148 : : arguments of statement functions. */
7149 : 215513 : if (sym != NULL && !st_flag
7150 : 323524 : && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
7151 : 97809 : || !gfc_missing_attr (&sym->attr, NULL)))
7152 : : {
7153 : 0 : m = MATCH_ERROR;
7154 : 0 : goto cleanup;
7155 : : }
7156 : :
7157 : : /* The name of a program unit can be in a different namespace,
7158 : : so check for it explicitly. After the statement is accepted,
7159 : : the name is checked for especially in gfc_get_symbol(). */
7160 : 225715 : if (gfc_new_block != NULL && sym != NULL && !typeparam
7161 : 96683 : && strcmp (sym->name, gfc_new_block->name) == 0)
7162 : : {
7163 : 0 : gfc_error ("Name %qs at %C is the name of the procedure",
7164 : : sym->name);
7165 : 0 : m = MATCH_ERROR;
7166 : 0 : goto cleanup;
7167 : : }
7168 : :
7169 : 225715 : if (gfc_match_char (')') == MATCH_YES)
7170 : 118670 : goto ok;
7171 : :
7172 : 107045 : m = gfc_match_char (',');
7173 : 107045 : if (m != MATCH_YES)
7174 : : {
7175 : 46163 : if (typeparam)
7176 : 1 : gfc_error_now ("Expected parameter list in type declaration "
7177 : : "at %C");
7178 : : else
7179 : 46162 : gfc_error ("Unexpected junk in formal argument list at %C");
7180 : 46163 : goto cleanup;
7181 : : }
7182 : : }
7183 : :
7184 : 135084 : ok:
7185 : : /* Check for duplicate symbols in the formal argument list. */
7186 : 135084 : if (head != NULL)
7187 : : {
7188 : 177285 : for (p = head; p->next; p = p->next)
7189 : : {
7190 : 58663 : if (p->sym == NULL)
7191 : 327 : continue;
7192 : :
7193 : 233295 : for (q = p->next; q; q = q->next)
7194 : 175007 : if (p->sym == q->sym)
7195 : : {
7196 : 48 : if (typeparam)
7197 : 1 : gfc_error_now ("Duplicate name %qs in parameter "
7198 : : "list at %C", p->sym->name);
7199 : : else
7200 : 47 : gfc_error ("Duplicate symbol %qs in formal argument "
7201 : : "list at %C", p->sym->name);
7202 : :
7203 : 48 : m = MATCH_ERROR;
7204 : 48 : goto cleanup;
7205 : : }
7206 : : }
7207 : : }
7208 : :
7209 : 135036 : if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
7210 : : {
7211 : 0 : m = MATCH_ERROR;
7212 : 0 : goto cleanup;
7213 : : }
7214 : :
7215 : : /* gfc_error_now used in following and return with MATCH_YES because
7216 : : doing otherwise results in a cascade of extraneous errors and in
7217 : : some cases an ICE in symbol.cc(gfc_release_symbol). */
7218 : 135036 : if (progname->attr.module_procedure && progname->attr.host_assoc)
7219 : : {
7220 : 178 : bool arg_count_mismatch = false;
7221 : :
7222 : 178 : if (!formal && head)
7223 : : arg_count_mismatch = true;
7224 : :
7225 : : /* Abbreviated module procedure declaration is not meant to have any
7226 : : formal arguments! */
7227 : 178 : if (!progname->abr_modproc_decl && formal && !head)
7228 : 1 : arg_count_mismatch = true;
7229 : :
7230 : 348 : for (p = formal, q = head; p && q; p = p->next, q = q->next)
7231 : : {
7232 : 170 : if ((p->next != NULL && q->next == NULL)
7233 : 169 : || (p->next == NULL && q->next != NULL))
7234 : : arg_count_mismatch = true;
7235 : 168 : else if ((p->sym == NULL && q->sym == NULL)
7236 : 168 : || (p->sym && q->sym
7237 : 166 : && strcmp (p->sym->name, q->sym->name) == 0))
7238 : 164 : continue;
7239 : : else
7240 : : {
7241 : 4 : if (q->sym == NULL)
7242 : 1 : gfc_error_now ("MODULE PROCEDURE formal argument %qs "
7243 : : "conflicts with alternate return at %C",
7244 : : p->sym->name);
7245 : 3 : else if (p->sym == NULL)
7246 : 1 : gfc_error_now ("MODULE PROCEDURE formal argument is "
7247 : : "alternate return and conflicts with "
7248 : : "%qs in the separate declaration at %C",
7249 : : q->sym->name);
7250 : : else
7251 : 2 : gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
7252 : : "argument names (%s/%s) at %C",
7253 : : p->sym->name, q->sym->name);
7254 : : }
7255 : : }
7256 : :
7257 : 178 : if (arg_count_mismatch)
7258 : 4 : gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
7259 : : "formal arguments at %C");
7260 : : }
7261 : :
7262 : : return MATCH_YES;
7263 : :
7264 : 61943 : cleanup:
7265 : 61943 : gfc_free_formal_arglist (head);
7266 : 61943 : return m;
7267 : : }
7268 : :
7269 : :
7270 : : /* Match a RESULT specification following a function declaration or
7271 : : ENTRY statement. Also matches the end-of-statement. */
7272 : :
7273 : : static match
7274 : 7826 : match_result (gfc_symbol *function, gfc_symbol **result)
7275 : : {
7276 : 7826 : char name[GFC_MAX_SYMBOL_LEN + 1];
7277 : 7826 : gfc_symbol *r;
7278 : 7826 : match m;
7279 : :
7280 : 7826 : if (gfc_match (" result (") != MATCH_YES)
7281 : : return MATCH_NO;
7282 : :
7283 : 5783 : m = gfc_match_name (name);
7284 : 5783 : if (m != MATCH_YES)
7285 : : return m;
7286 : :
7287 : : /* Get the right paren, and that's it because there could be the
7288 : : bind(c) attribute after the result clause. */
7289 : 5783 : if (gfc_match_char (')') != MATCH_YES)
7290 : : {
7291 : : /* TODO: should report the missing right paren here. */
7292 : : return MATCH_ERROR;
7293 : : }
7294 : :
7295 : 5783 : if (strcmp (function->name, name) == 0)
7296 : : {
7297 : 1 : gfc_error ("RESULT variable at %C must be different than function name");
7298 : 1 : return MATCH_ERROR;
7299 : : }
7300 : :
7301 : 5782 : if (gfc_get_symbol (name, NULL, &r))
7302 : : return MATCH_ERROR;
7303 : :
7304 : 5782 : if (!gfc_add_result (&r->attr, r->name, NULL))
7305 : : return MATCH_ERROR;
7306 : :
7307 : 5782 : *result = r;
7308 : :
7309 : 5782 : return MATCH_YES;
7310 : : }
7311 : :
7312 : :
7313 : : /* Match a function suffix, which could be a combination of a result
7314 : : clause and BIND(C), either one, or neither. The draft does not
7315 : : require them to come in a specific order. */
7316 : :
7317 : : static match
7318 : 7830 : gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
7319 : : {
7320 : 7830 : match is_bind_c; /* Found bind(c). */
7321 : 7830 : match is_result; /* Found result clause. */
7322 : 7830 : match found_match; /* Status of whether we've found a good match. */
7323 : 7830 : char peek_char; /* Character we're going to peek at. */
7324 : 7830 : bool allow_binding_name;
7325 : :
7326 : : /* Initialize to having found nothing. */
7327 : 7830 : found_match = MATCH_NO;
7328 : 7830 : is_bind_c = MATCH_NO;
7329 : 7830 : is_result = MATCH_NO;
7330 : :
7331 : : /* Get the next char to narrow between result and bind(c). */
7332 : 7830 : gfc_gobble_whitespace ();
7333 : 7830 : peek_char = gfc_peek_ascii_char ();
7334 : :
7335 : : /* C binding names are not allowed for internal procedures. */
7336 : 7830 : if (gfc_current_state () == COMP_CONTAINS
7337 : 4560 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
7338 : : allow_binding_name = false;
7339 : : else
7340 : 6194 : allow_binding_name = true;
7341 : :
7342 : 7830 : switch (peek_char)
7343 : : {
7344 : 5412 : case 'r':
7345 : : /* Look for result clause. */
7346 : 5412 : is_result = match_result (sym, result);
7347 : 5412 : if (is_result == MATCH_YES)
7348 : : {
7349 : : /* Now see if there is a bind(c) after it. */
7350 : 5411 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7351 : : /* We've found the result clause and possibly bind(c). */
7352 : 5411 : found_match = MATCH_YES;
7353 : : }
7354 : : else
7355 : : /* This should only be MATCH_ERROR. */
7356 : : found_match = is_result;
7357 : : break;
7358 : 2418 : case 'b':
7359 : : /* Look for bind(c) first. */
7360 : 2418 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7361 : 2418 : if (is_bind_c == MATCH_YES)
7362 : : {
7363 : : /* Now see if a result clause followed it. */
7364 : 2414 : is_result = match_result (sym, result);
7365 : 2414 : found_match = MATCH_YES;
7366 : : }
7367 : : else
7368 : : {
7369 : : /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
7370 : : found_match = MATCH_ERROR;
7371 : : }
7372 : : break;
7373 : 0 : default:
7374 : 0 : gfc_error ("Unexpected junk after function declaration at %C");
7375 : 0 : found_match = MATCH_ERROR;
7376 : 0 : break;
7377 : : }
7378 : :
7379 : 7825 : if (is_bind_c == MATCH_YES)
7380 : : {
7381 : : /* Fortran 2008 draft allows BIND(C) for internal procedures. */
7382 : 2563 : if (gfc_current_state () == COMP_CONTAINS
7383 : 416 : && sym->ns->proc_name->attr.flavor != FL_MODULE
7384 : 2575 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7385 : : "at %L may not be specified for an internal "
7386 : : "procedure", &gfc_current_locus))
7387 : : return MATCH_ERROR;
7388 : :
7389 : 2560 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
7390 : : return MATCH_ERROR;
7391 : : }
7392 : :
7393 : : return found_match;
7394 : : }
7395 : :
7396 : :
7397 : : /* Procedure pointer return value without RESULT statement:
7398 : : Add "hidden" result variable named "ppr@". */
7399 : :
7400 : : static bool
7401 : 72161 : add_hidden_procptr_result (gfc_symbol *sym)
7402 : : {
7403 : 72161 : bool case1,case2;
7404 : :
7405 : 72161 : if (gfc_notification_std (GFC_STD_F2003) == ERROR)
7406 : : return false;
7407 : :
7408 : : /* First usage case: PROCEDURE and EXTERNAL statements. */
7409 : 1519 : case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
7410 : 1519 : && strcmp (gfc_current_block ()->name, sym->name) == 0
7411 : 72547 : && sym->attr.external;
7412 : : /* Second usage case: INTERFACE statements. */
7413 : 13929 : case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
7414 : 13929 : && gfc_state_stack->previous->state == COMP_FUNCTION
7415 : 72208 : && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
7416 : :
7417 : 71977 : if (case1 || case2)
7418 : : {
7419 : 124 : gfc_symtree *stree;
7420 : 124 : if (case1)
7421 : 94 : gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
7422 : : else
7423 : : {
7424 : 30 : gfc_symtree *st2;
7425 : 30 : gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
7426 : 30 : st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
7427 : 30 : st2->n.sym = stree->n.sym;
7428 : 30 : stree->n.sym->refs++;
7429 : : }
7430 : 124 : sym->result = stree->n.sym;
7431 : :
7432 : 124 : sym->result->attr.proc_pointer = sym->attr.proc_pointer;
7433 : 124 : sym->result->attr.pointer = sym->attr.pointer;
7434 : 124 : sym->result->attr.external = sym->attr.external;
7435 : 124 : sym->result->attr.referenced = sym->attr.referenced;
7436 : 124 : sym->result->ts = sym->ts;
7437 : 124 : sym->attr.proc_pointer = 0;
7438 : 124 : sym->attr.pointer = 0;
7439 : 124 : sym->attr.external = 0;
7440 : 124 : if (sym->result->attr.external && sym->result->attr.pointer)
7441 : : {
7442 : 4 : sym->result->attr.pointer = 0;
7443 : 4 : sym->result->attr.proc_pointer = 1;
7444 : : }
7445 : :
7446 : 124 : return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
7447 : : }
7448 : : /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
7449 : 71883 : else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
7450 : 399 : && sym->result && sym->result != sym && sym->result->attr.external
7451 : 28 : && sym == gfc_current_ns->proc_name
7452 : 28 : && sym == sym->result->ns->proc_name
7453 : 28 : && strcmp ("ppr@", sym->result->name) == 0)
7454 : : {
7455 : 28 : sym->result->attr.proc_pointer = 1;
7456 : 28 : sym->attr.pointer = 0;
7457 : 28 : return true;
7458 : : }
7459 : : else
7460 : : return false;
7461 : : }
7462 : :
7463 : :
7464 : : /* Match the interface for a PROCEDURE declaration,
7465 : : including brackets (R1212). */
7466 : :
7467 : : static match
7468 : 1549 : match_procedure_interface (gfc_symbol **proc_if)
7469 : : {
7470 : 1549 : match m;
7471 : 1549 : gfc_symtree *st;
7472 : 1549 : locus old_loc, entry_loc;
7473 : 1549 : gfc_namespace *old_ns = gfc_current_ns;
7474 : 1549 : char name[GFC_MAX_SYMBOL_LEN + 1];
7475 : :
7476 : 1549 : old_loc = entry_loc = gfc_current_locus;
7477 : 1549 : gfc_clear_ts (¤t_ts);
7478 : :
7479 : 1549 : if (gfc_match (" (") != MATCH_YES)
7480 : : {
7481 : 1 : gfc_current_locus = entry_loc;
7482 : 1 : return MATCH_NO;
7483 : : }
7484 : :
7485 : : /* Get the type spec. for the procedure interface. */
7486 : 1548 : old_loc = gfc_current_locus;
7487 : 1548 : m = gfc_match_decl_type_spec (¤t_ts, 0);
7488 : 1548 : gfc_gobble_whitespace ();
7489 : 1548 : if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
7490 : 390 : goto got_ts;
7491 : :
7492 : 1158 : if (m == MATCH_ERROR)
7493 : : return m;
7494 : :
7495 : : /* Procedure interface is itself a procedure. */
7496 : 1158 : gfc_current_locus = old_loc;
7497 : 1158 : m = gfc_match_name (name);
7498 : :
7499 : : /* First look to see if it is already accessible in the current
7500 : : namespace because it is use associated or contained. */
7501 : 1158 : st = NULL;
7502 : 1158 : if (gfc_find_sym_tree (name, NULL, 0, &st))
7503 : : return MATCH_ERROR;
7504 : :
7505 : : /* If it is still not found, then try the parent namespace, if it
7506 : : exists and create the symbol there if it is still not found. */
7507 : 1158 : if (gfc_current_ns->parent)
7508 : 385 : gfc_current_ns = gfc_current_ns->parent;
7509 : 1158 : if (st == NULL && gfc_get_ha_sym_tree (name, &st))
7510 : : return MATCH_ERROR;
7511 : :
7512 : 1158 : gfc_current_ns = old_ns;
7513 : 1158 : *proc_if = st->n.sym;
7514 : :
7515 : 1158 : if (*proc_if)
7516 : : {
7517 : 1158 : (*proc_if)->refs++;
7518 : : /* Resolve interface if possible. That way, attr.procedure is only set
7519 : : if it is declared by a later procedure-declaration-stmt, which is
7520 : : invalid per F08:C1216 (cf. resolve_procedure_interface). */
7521 : 1158 : while ((*proc_if)->ts.interface
7522 : 1165 : && *proc_if != (*proc_if)->ts.interface)
7523 : 7 : *proc_if = (*proc_if)->ts.interface;
7524 : :
7525 : 1158 : if ((*proc_if)->attr.flavor == FL_UNKNOWN
7526 : 387 : && (*proc_if)->ts.type == BT_UNKNOWN
7527 : 1545 : && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
7528 : : (*proc_if)->name, NULL))
7529 : : return MATCH_ERROR;
7530 : : }
7531 : :
7532 : 0 : got_ts:
7533 : 1548 : if (gfc_match (" )") != MATCH_YES)
7534 : : {
7535 : 0 : gfc_current_locus = entry_loc;
7536 : 0 : return MATCH_NO;
7537 : : }
7538 : :
7539 : : return MATCH_YES;
7540 : : }
7541 : :
7542 : :
7543 : : /* Match a PROCEDURE declaration (R1211). */
7544 : :
7545 : : static match
7546 : 1124 : match_procedure_decl (void)
7547 : : {
7548 : 1124 : match m;
7549 : 1124 : gfc_symbol *sym, *proc_if = NULL;
7550 : 1124 : int num;
7551 : 1124 : gfc_expr *initializer = NULL;
7552 : :
7553 : : /* Parse interface (with brackets). */
7554 : 1124 : m = match_procedure_interface (&proc_if);
7555 : 1124 : if (m != MATCH_YES)
7556 : : return m;
7557 : :
7558 : : /* Parse attributes (with colons). */
7559 : 1124 : m = match_attr_spec();
7560 : 1124 : if (m == MATCH_ERROR)
7561 : : return MATCH_ERROR;
7562 : :
7563 : 1123 : if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
7564 : : {
7565 : 17 : current_attr.is_bind_c = 1;
7566 : 17 : has_name_equals = 0;
7567 : 17 : curr_binding_label = NULL;
7568 : : }
7569 : :
7570 : : /* Get procedure symbols. */
7571 : 79 : for(num=1;;num++)
7572 : : {
7573 : 1202 : m = gfc_match_symbol (&sym, 0);
7574 : 1202 : if (m == MATCH_NO)
7575 : 1 : goto syntax;
7576 : 1201 : else if (m == MATCH_ERROR)
7577 : : return m;
7578 : :
7579 : : /* Add current_attr to the symbol attributes. */
7580 : 1201 : if (!gfc_copy_attr (&sym->attr, ¤t_attr, NULL))
7581 : : return MATCH_ERROR;
7582 : :
7583 : 1199 : if (sym->attr.is_bind_c)
7584 : : {
7585 : : /* Check for C1218. */
7586 : 54 : if (!proc_if || !proc_if->attr.is_bind_c)
7587 : : {
7588 : 1 : gfc_error ("BIND(C) attribute at %C requires "
7589 : : "an interface with BIND(C)");
7590 : 1 : return MATCH_ERROR;
7591 : : }
7592 : : /* Check for C1217. */
7593 : 53 : if (has_name_equals && sym->attr.pointer)
7594 : : {
7595 : 1 : gfc_error ("BIND(C) procedure with NAME may not have "
7596 : : "POINTER attribute at %C");
7597 : 1 : return MATCH_ERROR;
7598 : : }
7599 : 52 : if (has_name_equals && sym->attr.dummy)
7600 : : {
7601 : 1 : gfc_error ("Dummy procedure at %C may not have "
7602 : : "BIND(C) attribute with NAME");
7603 : 1 : return MATCH_ERROR;
7604 : : }
7605 : : /* Set binding label for BIND(C). */
7606 : 51 : if (!set_binding_label (&sym->binding_label, sym->name, num))
7607 : : return MATCH_ERROR;
7608 : : }
7609 : :
7610 : 1195 : if (!gfc_add_external (&sym->attr, NULL))
7611 : : return MATCH_ERROR;
7612 : :
7613 : 1191 : if (add_hidden_procptr_result (sym))
7614 : 67 : sym = sym->result;
7615 : :
7616 : 1191 : if (!gfc_add_proc (&sym->attr, sym->name, NULL))
7617 : : return MATCH_ERROR;
7618 : :
7619 : : /* Set interface. */
7620 : 1190 : if (proc_if != NULL)
7621 : : {
7622 : 851 : if (sym->ts.type != BT_UNKNOWN)
7623 : : {
7624 : 1 : gfc_error ("Procedure %qs at %L already has basic type of %s",
7625 : : sym->name, &gfc_current_locus,
7626 : : gfc_basic_typename (sym->ts.type));
7627 : 1 : return MATCH_ERROR;
7628 : : }
7629 : 850 : sym->ts.interface = proc_if;
7630 : 850 : sym->attr.untyped = 1;
7631 : 850 : sym->attr.if_source = IFSRC_IFBODY;
7632 : : }
7633 : 339 : else if (current_ts.type != BT_UNKNOWN)
7634 : : {
7635 : 199 : if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
7636 : : return MATCH_ERROR;
7637 : 198 : sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7638 : 198 : sym->ts.interface->ts = current_ts;
7639 : 198 : sym->ts.interface->attr.flavor = FL_PROCEDURE;
7640 : 198 : sym->ts.interface->attr.function = 1;
7641 : 198 : sym->attr.function = 1;
7642 : 198 : sym->attr.if_source = IFSRC_UNKNOWN;
7643 : : }
7644 : :
7645 : 1188 : if (gfc_match (" =>") == MATCH_YES)
7646 : : {
7647 : 87 : if (!current_attr.pointer)
7648 : : {
7649 : 0 : gfc_error ("Initialization at %C isn't for a pointer variable");
7650 : 0 : m = MATCH_ERROR;
7651 : 0 : goto cleanup;
7652 : : }
7653 : :
7654 : 87 : m = match_pointer_init (&initializer, 1);
7655 : 87 : if (m != MATCH_YES)
7656 : 1 : goto cleanup;
7657 : :
7658 : 86 : if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
7659 : 0 : goto cleanup;
7660 : :
7661 : : }
7662 : :
7663 : 1187 : if (gfc_match_eos () == MATCH_YES)
7664 : : return MATCH_YES;
7665 : 79 : if (gfc_match_char (',') != MATCH_YES)
7666 : 0 : goto syntax;
7667 : : }
7668 : :
7669 : 1 : syntax:
7670 : 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
7671 : 1 : return MATCH_ERROR;
7672 : :
7673 : 1 : cleanup:
7674 : : /* Free stuff up and return. */
7675 : 1 : gfc_free_expr (initializer);
7676 : 1 : return m;
7677 : : }
7678 : :
7679 : :
7680 : : static match
7681 : : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
7682 : :
7683 : :
7684 : : /* Match a procedure pointer component declaration (R445). */
7685 : :
7686 : : static match
7687 : 425 : match_ppc_decl (void)
7688 : : {
7689 : 425 : match m;
7690 : 425 : gfc_symbol *proc_if = NULL;
7691 : 425 : gfc_typespec ts;
7692 : 425 : int num;
7693 : 425 : gfc_component *c;
7694 : 425 : gfc_expr *initializer = NULL;
7695 : 425 : gfc_typebound_proc* tb;
7696 : 425 : char name[GFC_MAX_SYMBOL_LEN + 1];
7697 : :
7698 : : /* Parse interface (with brackets). */
7699 : 425 : m = match_procedure_interface (&proc_if);
7700 : 425 : if (m != MATCH_YES)
7701 : 1 : goto syntax;
7702 : :
7703 : : /* Parse attributes. */
7704 : 424 : tb = XCNEW (gfc_typebound_proc);
7705 : 424 : tb->where = gfc_current_locus;
7706 : 424 : m = match_binding_attributes (tb, false, true);
7707 : 424 : if (m == MATCH_ERROR)
7708 : : return m;
7709 : :
7710 : 421 : gfc_clear_attr (¤t_attr);
7711 : 421 : current_attr.procedure = 1;
7712 : 421 : current_attr.proc_pointer = 1;
7713 : 421 : current_attr.access = tb->access;
7714 : 421 : current_attr.flavor = FL_PROCEDURE;
7715 : :
7716 : : /* Match the colons (required). */
7717 : 421 : if (gfc_match (" ::") != MATCH_YES)
7718 : : {
7719 : 1 : gfc_error ("Expected %<::%> after binding-attributes at %C");
7720 : 1 : return MATCH_ERROR;
7721 : : }
7722 : :
7723 : : /* Check for C450. */
7724 : 420 : if (!tb->nopass && proc_if == NULL)
7725 : : {
7726 : 2 : gfc_error("NOPASS or explicit interface required at %C");
7727 : 2 : return MATCH_ERROR;
7728 : : }
7729 : :
7730 : 418 : if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
7731 : : return MATCH_ERROR;
7732 : :
7733 : : /* Match PPC names. */
7734 : 417 : ts = current_ts;
7735 : 417 : for(num=1;;num++)
7736 : : {
7737 : 418 : m = gfc_match_name (name);
7738 : 418 : if (m == MATCH_NO)
7739 : 0 : goto syntax;
7740 : 418 : else if (m == MATCH_ERROR)
7741 : : return m;
7742 : :
7743 : 418 : if (!gfc_add_component (gfc_current_block(), name, &c))
7744 : : return MATCH_ERROR;
7745 : :
7746 : : /* Add current_attr to the symbol attributes. */
7747 : 418 : if (!gfc_copy_attr (&c->attr, ¤t_attr, NULL))
7748 : : return MATCH_ERROR;
7749 : :
7750 : 418 : if (!gfc_add_external (&c->attr, NULL))
7751 : : return MATCH_ERROR;
7752 : :
7753 : 418 : if (!gfc_add_proc (&c->attr, name, NULL))
7754 : : return MATCH_ERROR;
7755 : :
7756 : 418 : if (num == 1)
7757 : 417 : c->tb = tb;
7758 : : else
7759 : : {
7760 : 1 : c->tb = XCNEW (gfc_typebound_proc);
7761 : 1 : c->tb->where = gfc_current_locus;
7762 : 1 : *c->tb = *tb;
7763 : : }
7764 : :
7765 : 418 : if (saved_kind_expr)
7766 : 0 : c->kind_expr = gfc_copy_expr (saved_kind_expr);
7767 : :
7768 : : /* Set interface. */
7769 : 418 : if (proc_if != NULL)
7770 : : {
7771 : 352 : c->ts.interface = proc_if;
7772 : 352 : c->attr.untyped = 1;
7773 : 352 : c->attr.if_source = IFSRC_IFBODY;
7774 : : }
7775 : 66 : else if (ts.type != BT_UNKNOWN)
7776 : : {
7777 : 29 : c->ts = ts;
7778 : 29 : c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7779 : 29 : c->ts.interface->result = c->ts.interface;
7780 : 29 : c->ts.interface->ts = ts;
7781 : 29 : c->ts.interface->attr.flavor = FL_PROCEDURE;
7782 : 29 : c->ts.interface->attr.function = 1;
7783 : 29 : c->attr.function = 1;
7784 : 29 : c->attr.if_source = IFSRC_UNKNOWN;
7785 : : }
7786 : :
7787 : 418 : if (gfc_match (" =>") == MATCH_YES)
7788 : : {
7789 : 66 : m = match_pointer_init (&initializer, 1);
7790 : 66 : if (m != MATCH_YES)
7791 : : {
7792 : 0 : gfc_free_expr (initializer);
7793 : 0 : return m;
7794 : : }
7795 : 66 : c->initializer = initializer;
7796 : : }
7797 : :
7798 : 418 : if (gfc_match_eos () == MATCH_YES)
7799 : : return MATCH_YES;
7800 : 1 : if (gfc_match_char (',') != MATCH_YES)
7801 : 0 : goto syntax;
7802 : : }
7803 : :
7804 : 1 : syntax:
7805 : 1 : gfc_error ("Syntax error in procedure pointer component at %C");
7806 : 1 : return MATCH_ERROR;
7807 : : }
7808 : :
7809 : :
7810 : : /* Match a PROCEDURE declaration inside an interface (R1206). */
7811 : :
7812 : : static match
7813 : 1561 : match_procedure_in_interface (void)
7814 : : {
7815 : 1561 : match m;
7816 : 1561 : gfc_symbol *sym;
7817 : 1561 : char name[GFC_MAX_SYMBOL_LEN + 1];
7818 : 1561 : locus old_locus;
7819 : :
7820 : 1561 : if (current_interface.type == INTERFACE_NAMELESS
7821 : 1561 : || current_interface.type == INTERFACE_ABSTRACT)
7822 : : {
7823 : 1 : gfc_error ("PROCEDURE at %C must be in a generic interface");
7824 : 1 : return MATCH_ERROR;
7825 : : }
7826 : :
7827 : : /* Check if the F2008 optional double colon appears. */
7828 : 1560 : gfc_gobble_whitespace ();
7829 : 1560 : old_locus = gfc_current_locus;
7830 : 1560 : if (gfc_match ("::") == MATCH_YES)
7831 : : {
7832 : 875 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7833 : : "MODULE PROCEDURE statement at %L", &old_locus))
7834 : : return MATCH_ERROR;
7835 : : }
7836 : : else
7837 : 685 : gfc_current_locus = old_locus;
7838 : :
7839 : 2214 : for(;;)
7840 : : {
7841 : 2214 : m = gfc_match_name (name);
7842 : 2214 : if (m == MATCH_NO)
7843 : 0 : goto syntax;
7844 : 2214 : else if (m == MATCH_ERROR)
7845 : : return m;
7846 : 2214 : if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7847 : : return MATCH_ERROR;
7848 : :
7849 : 2214 : if (!gfc_add_interface (sym))
7850 : : return MATCH_ERROR;
7851 : :
7852 : 2213 : if (gfc_match_eos () == MATCH_YES)
7853 : : break;
7854 : 655 : if (gfc_match_char (',') != MATCH_YES)
7855 : 0 : goto syntax;
7856 : : }
7857 : :
7858 : : return MATCH_YES;
7859 : :
7860 : 0 : syntax:
7861 : 0 : gfc_error ("Syntax error in PROCEDURE statement at %C");
7862 : 0 : return MATCH_ERROR;
7863 : : }
7864 : :
7865 : :
7866 : : /* General matcher for PROCEDURE declarations. */
7867 : :
7868 : : static match match_procedure_in_type (void);
7869 : :
7870 : : match
7871 : 6204 : gfc_match_procedure (void)
7872 : : {
7873 : 6204 : match m;
7874 : :
7875 : 6204 : switch (gfc_current_state ())
7876 : : {
7877 : 1124 : case COMP_NONE:
7878 : 1124 : case COMP_PROGRAM:
7879 : 1124 : case COMP_MODULE:
7880 : 1124 : case COMP_SUBMODULE:
7881 : 1124 : case COMP_SUBROUTINE:
7882 : 1124 : case COMP_FUNCTION:
7883 : 1124 : case COMP_BLOCK:
7884 : 1124 : m = match_procedure_decl ();
7885 : 1124 : break;
7886 : 1561 : case COMP_INTERFACE:
7887 : 1561 : m = match_procedure_in_interface ();
7888 : 1561 : break;
7889 : 425 : case COMP_DERIVED:
7890 : 425 : m = match_ppc_decl ();
7891 : 425 : break;
7892 : 3094 : case COMP_DERIVED_CONTAINS:
7893 : 3094 : m = match_procedure_in_type ();
7894 : 3094 : break;
7895 : : default:
7896 : : return MATCH_NO;
7897 : : }
7898 : :
7899 : 6204 : if (m != MATCH_YES)
7900 : : return m;
7901 : :
7902 : 6148 : if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7903 : 4 : return MATCH_ERROR;
7904 : :
7905 : : return m;
7906 : : }
7907 : :
7908 : :
7909 : : /* Warn if a matched procedure has the same name as an intrinsic; this is
7910 : : simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7911 : : parser-state-stack to find out whether we're in a module. */
7912 : :
7913 : : static void
7914 : 60747 : do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7915 : : {
7916 : 60747 : bool in_module;
7917 : :
7918 : 121494 : in_module = (gfc_state_stack->previous
7919 : 60747 : && (gfc_state_stack->previous->state == COMP_MODULE
7920 : 49390 : || gfc_state_stack->previous->state == COMP_SUBMODULE));
7921 : :
7922 : 60747 : gfc_warn_intrinsic_shadow (sym, in_module, func);
7923 : 60747 : }
7924 : :
7925 : :
7926 : : /* Match a function declaration. */
7927 : :
7928 : : match
7929 : 123929 : gfc_match_function_decl (void)
7930 : : {
7931 : 123929 : char name[GFC_MAX_SYMBOL_LEN + 1];
7932 : 123929 : gfc_symbol *sym, *result;
7933 : 123929 : locus old_loc;
7934 : 123929 : match m;
7935 : 123929 : match suffix_match;
7936 : 123929 : match found_match; /* Status returned by match func. */
7937 : :
7938 : 123929 : if (gfc_current_state () != COMP_NONE
7939 : 77733 : && gfc_current_state () != COMP_INTERFACE
7940 : 50330 : && gfc_current_state () != COMP_CONTAINS)
7941 : : return MATCH_NO;
7942 : :
7943 : 123929 : gfc_clear_ts (¤t_ts);
7944 : :
7945 : 123929 : old_loc = gfc_current_locus;
7946 : :
7947 : 123929 : m = gfc_match_prefix (¤t_ts);
7948 : 123929 : if (m != MATCH_YES)
7949 : : {
7950 : 9429 : gfc_current_locus = old_loc;
7951 : 9429 : return m;
7952 : : }
7953 : :
7954 : 114500 : if (gfc_match ("function% %n", name) != MATCH_YES)
7955 : : {
7956 : 95695 : gfc_current_locus = old_loc;
7957 : 95695 : return MATCH_NO;
7958 : : }
7959 : :
7960 : 18805 : if (get_proc_name (name, &sym, false))
7961 : : return MATCH_ERROR;
7962 : :
7963 : 18800 : if (add_hidden_procptr_result (sym))
7964 : 20 : sym = sym->result;
7965 : :
7966 : 18800 : if (current_attr.module_procedure)
7967 : 282 : sym->attr.module_procedure = 1;
7968 : :
7969 : 18800 : gfc_new_block = sym;
7970 : :
7971 : 18800 : m = gfc_match_formal_arglist (sym, 0, 0);
7972 : 18800 : if (m == MATCH_NO)
7973 : : {
7974 : 6 : gfc_error ("Expected formal argument list in function "
7975 : : "definition at %C");
7976 : 6 : m = MATCH_ERROR;
7977 : 6 : goto cleanup;
7978 : : }
7979 : 18794 : else if (m == MATCH_ERROR)
7980 : 0 : goto cleanup;
7981 : :
7982 : 18794 : result = NULL;
7983 : :
7984 : : /* According to the draft, the bind(c) and result clause can
7985 : : come in either order after the formal_arg_list (i.e., either
7986 : : can be first, both can exist together or by themselves or neither
7987 : : one). Therefore, the match_result can't match the end of the
7988 : : string, and check for the bind(c) or result clause in either order. */
7989 : 18794 : found_match = gfc_match_eos ();
7990 : :
7991 : : /* Make sure that it isn't already declared as BIND(C). If it is, it
7992 : : must have been marked BIND(C) with a BIND(C) attribute and that is
7993 : : not allowed for procedures. */
7994 : 18794 : if (sym->attr.is_bind_c == 1)
7995 : : {
7996 : 3 : sym->attr.is_bind_c = 0;
7997 : :
7998 : 3 : if (gfc_state_stack->previous
7999 : 3 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8000 : : {
8001 : 1 : locus loc;
8002 : 1 : loc = sym->old_symbol != NULL
8003 : 1 : ? sym->old_symbol->declared_at : gfc_current_locus;
8004 : 1 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8005 : : "variables or common blocks", &loc);
8006 : : }
8007 : : }
8008 : :
8009 : 18794 : if (found_match != MATCH_YES)
8010 : : {
8011 : : /* If we haven't found the end-of-statement, look for a suffix. */
8012 : 7599 : suffix_match = gfc_match_suffix (sym, &result);
8013 : 7599 : if (suffix_match == MATCH_YES)
8014 : : /* Need to get the eos now. */
8015 : 7591 : found_match = gfc_match_eos ();
8016 : : else
8017 : : found_match = suffix_match;
8018 : : }
8019 : :
8020 : : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8021 : : subprogram and a binding label is specified, it shall be the
8022 : : same as the binding label specified in the corresponding module
8023 : : procedure interface body. */
8024 : 18794 : if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
8025 : 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8026 : 3 : && sym->binding_label && sym->old_symbol->binding_label
8027 : 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8028 : : {
8029 : 1 : const char *null = "NULL", *s1, *s2;
8030 : 1 : s1 = sym->binding_label;
8031 : 1 : if (!s1) s1 = null;
8032 : 1 : s2 = sym->old_symbol->binding_label;
8033 : 1 : if (!s2) s2 = null;
8034 : 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8035 : 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8036 : 1 : return MATCH_ERROR;
8037 : : }
8038 : :
8039 : 18793 : if(found_match != MATCH_YES)
8040 : : m = MATCH_ERROR;
8041 : : else
8042 : : {
8043 : : /* Make changes to the symbol. */
8044 : 18785 : m = MATCH_ERROR;
8045 : :
8046 : 18785 : if (!gfc_add_function (&sym->attr, sym->name, NULL))
8047 : 0 : goto cleanup;
8048 : :
8049 : 18785 : if (!gfc_missing_attr (&sym->attr, NULL))
8050 : 0 : goto cleanup;
8051 : :
8052 : 18785 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8053 : : {
8054 : 1 : if(!sym->attr.module_procedure)
8055 : 1 : goto cleanup;
8056 : : else
8057 : 0 : gfc_error_check ();
8058 : : }
8059 : :
8060 : : /* Delay matching the function characteristics until after the
8061 : : specification block by signalling kind=-1. */
8062 : 18784 : sym->declared_at = old_loc;
8063 : 18784 : if (current_ts.type != BT_UNKNOWN)
8064 : 6695 : current_ts.kind = -1;
8065 : : else
8066 : 12089 : current_ts.kind = 0;
8067 : :
8068 : 18784 : if (result == NULL)
8069 : : {
8070 : 13214 : if (current_ts.type != BT_UNKNOWN
8071 : 13214 : && !gfc_add_type (sym, ¤t_ts, &gfc_current_locus))
8072 : 1 : goto cleanup;
8073 : 13213 : sym->result = sym;
8074 : : }
8075 : : else
8076 : : {
8077 : 5570 : if (current_ts.type != BT_UNKNOWN
8078 : 5570 : && !gfc_add_type (result, ¤t_ts, &gfc_current_locus))
8079 : 0 : goto cleanup;
8080 : 5570 : sym->result = result;
8081 : : }
8082 : :
8083 : : /* Warn if this procedure has the same name as an intrinsic. */
8084 : 18783 : do_warn_intrinsic_shadow (sym, true);
8085 : :
8086 : 18783 : return MATCH_YES;
8087 : : }
8088 : :
8089 : 16 : cleanup:
8090 : 16 : gfc_current_locus = old_loc;
8091 : 16 : return m;
8092 : : }
8093 : :
8094 : :
8095 : : /* This is mostly a copy of parse.cc(add_global_procedure) but modified to
8096 : : pass the name of the entry, rather than the gfc_current_block name, and
8097 : : to return false upon finding an existing global entry. */
8098 : :
8099 : : static bool
8100 : 504 : add_global_entry (const char *name, const char *binding_label, bool sub,
8101 : : locus *where)
8102 : : {
8103 : 504 : gfc_gsymbol *s;
8104 : 504 : enum gfc_symbol_type type;
8105 : :
8106 : 504 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
8107 : :
8108 : : /* Only in Fortran 2003: For procedures with a binding label also the Fortran
8109 : : name is a global identifier. */
8110 : 504 : if (!binding_label || gfc_notification_std (GFC_STD_F2008))
8111 : : {
8112 : 499 : s = gfc_get_gsymbol (name, false);
8113 : :
8114 : 499 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8115 : : {
8116 : 2 : gfc_global_used (s, where);
8117 : 2 : return false;
8118 : : }
8119 : : else
8120 : : {
8121 : 497 : s->type = type;
8122 : 497 : s->sym_name = name;
8123 : 497 : s->where = *where;
8124 : 497 : s->defined = 1;
8125 : 497 : s->ns = gfc_current_ns;
8126 : : }
8127 : : }
8128 : :
8129 : : /* Don't add the symbol multiple times. */
8130 : 502 : if (binding_label
8131 : 502 : && (!gfc_notification_std (GFC_STD_F2008)
8132 : 0 : || strcmp (name, binding_label) != 0))
8133 : : {
8134 : 5 : s = gfc_get_gsymbol (binding_label, true);
8135 : :
8136 : 5 : if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
8137 : : {
8138 : 1 : gfc_global_used (s, where);
8139 : 1 : return false;
8140 : : }
8141 : : else
8142 : : {
8143 : 4 : s->type = type;
8144 : 4 : s->sym_name = name;
8145 : 4 : s->binding_label = binding_label;
8146 : 4 : s->where = *where;
8147 : 4 : s->defined = 1;
8148 : 4 : s->ns = gfc_current_ns;
8149 : : }
8150 : : }
8151 : :
8152 : : return true;
8153 : : }
8154 : :
8155 : :
8156 : : /* Match an ENTRY statement. */
8157 : :
8158 : : match
8159 : 769 : gfc_match_entry (void)
8160 : : {
8161 : 769 : gfc_symbol *proc;
8162 : 769 : gfc_symbol *result;
8163 : 769 : gfc_symbol *entry;
8164 : 769 : char name[GFC_MAX_SYMBOL_LEN + 1];
8165 : 769 : gfc_compile_state state;
8166 : 769 : match m;
8167 : 769 : gfc_entry_list *el;
8168 : 769 : locus old_loc;
8169 : 769 : bool module_procedure;
8170 : 769 : char peek_char;
8171 : 769 : match is_bind_c;
8172 : :
8173 : 769 : m = gfc_match_name (name);
8174 : 769 : if (m != MATCH_YES)
8175 : : return m;
8176 : :
8177 : 769 : if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
8178 : : return MATCH_ERROR;
8179 : :
8180 : 769 : state = gfc_current_state ();
8181 : 769 : if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
8182 : : {
8183 : 3 : switch (state)
8184 : : {
8185 : 0 : case COMP_PROGRAM:
8186 : 0 : gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
8187 : 0 : break;
8188 : 0 : case COMP_MODULE:
8189 : 0 : gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
8190 : 0 : break;
8191 : 0 : case COMP_SUBMODULE:
8192 : 0 : gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
8193 : 0 : break;
8194 : 0 : case COMP_BLOCK_DATA:
8195 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8196 : : "a BLOCK DATA");
8197 : 0 : break;
8198 : 0 : case COMP_INTERFACE:
8199 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8200 : : "an INTERFACE");
8201 : 0 : break;
8202 : 1 : case COMP_STRUCTURE:
8203 : 1 : gfc_error ("ENTRY statement at %C cannot appear within "
8204 : : "a STRUCTURE block");
8205 : 1 : break;
8206 : 0 : case COMP_DERIVED:
8207 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8208 : : "a DERIVED TYPE block");
8209 : 0 : break;
8210 : 0 : case COMP_IF:
8211 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8212 : : "an IF-THEN block");
8213 : 0 : break;
8214 : 0 : case COMP_DO:
8215 : 0 : case COMP_DO_CONCURRENT:
8216 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8217 : : "a DO block");
8218 : 0 : break;
8219 : 0 : case COMP_SELECT:
8220 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8221 : : "a SELECT block");
8222 : 0 : break;
8223 : 0 : case COMP_FORALL:
8224 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8225 : : "a FORALL block");
8226 : 0 : break;
8227 : 0 : case COMP_WHERE:
8228 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8229 : : "a WHERE block");
8230 : 0 : break;
8231 : 0 : case COMP_CONTAINS:
8232 : 0 : gfc_error ("ENTRY statement at %C cannot appear within "
8233 : : "a contained subprogram");
8234 : 0 : break;
8235 : 2 : default:
8236 : 2 : gfc_error ("Unexpected ENTRY statement at %C");
8237 : : }
8238 : 3 : return MATCH_ERROR;
8239 : : }
8240 : :
8241 : 766 : if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
8242 : 766 : && gfc_state_stack->previous->state == COMP_INTERFACE)
8243 : : {
8244 : 1 : gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
8245 : 1 : return MATCH_ERROR;
8246 : : }
8247 : :
8248 : 1530 : module_procedure = gfc_current_ns->parent != NULL
8249 : 259 : && gfc_current_ns->parent->proc_name
8250 : 765 : && gfc_current_ns->parent->proc_name->attr.flavor
8251 : 259 : == FL_MODULE;
8252 : :
8253 : 765 : if (gfc_current_ns->parent != NULL
8254 : 259 : && gfc_current_ns->parent->proc_name
8255 : 259 : && !module_procedure)
8256 : : {
8257 : 0 : gfc_error("ENTRY statement at %C cannot appear in a "
8258 : : "contained procedure");
8259 : 0 : return MATCH_ERROR;
8260 : : }
8261 : :
8262 : : /* Module function entries need special care in get_proc_name
8263 : : because previous references within the function will have
8264 : : created symbols attached to the current namespace. */
8265 : 765 : if (get_proc_name (name, &entry,
8266 : : gfc_current_ns->parent != NULL
8267 : 765 : && module_procedure))
8268 : : return MATCH_ERROR;
8269 : :
8270 : 763 : proc = gfc_current_block ();
8271 : :
8272 : : /* Make sure that it isn't already declared as BIND(C). If it is, it
8273 : : must have been marked BIND(C) with a BIND(C) attribute and that is
8274 : : not allowed for procedures. */
8275 : 763 : if (entry->attr.is_bind_c == 1)
8276 : : {
8277 : 0 : locus loc;
8278 : :
8279 : 0 : entry->attr.is_bind_c = 0;
8280 : :
8281 : 0 : loc = entry->old_symbol != NULL
8282 : 0 : ? entry->old_symbol->declared_at : gfc_current_locus;
8283 : 0 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8284 : : "variables or common blocks", &loc);
8285 : : }
8286 : :
8287 : : /* Check what next non-whitespace character is so we can tell if there
8288 : : is the required parens if we have a BIND(C). */
8289 : 763 : old_loc = gfc_current_locus;
8290 : 763 : gfc_gobble_whitespace ();
8291 : 763 : peek_char = gfc_peek_ascii_char ();
8292 : :
8293 : 763 : if (state == COMP_SUBROUTINE)
8294 : : {
8295 : 134 : m = gfc_match_formal_arglist (entry, 0, 1);
8296 : 134 : if (m != MATCH_YES)
8297 : : return MATCH_ERROR;
8298 : :
8299 : : /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
8300 : : never be an internal procedure. */
8301 : 134 : is_bind_c = gfc_match_bind_c (entry, true);
8302 : 134 : if (is_bind_c == MATCH_ERROR)
8303 : : return MATCH_ERROR;
8304 : 134 : if (is_bind_c == MATCH_YES)
8305 : : {
8306 : 22 : if (peek_char != '(')
8307 : : {
8308 : 0 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8309 : 0 : return MATCH_ERROR;
8310 : : }
8311 : :
8312 : 22 : if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
8313 : 22 : &(entry->declared_at), 1))
8314 : : return MATCH_ERROR;
8315 : :
8316 : : }
8317 : :
8318 : 134 : if (!gfc_current_ns->parent
8319 : 134 : && !add_global_entry (name, entry->binding_label, true,
8320 : : &old_loc))
8321 : : return MATCH_ERROR;
8322 : :
8323 : : /* An entry in a subroutine. */
8324 : 131 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8325 : 131 : || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
8326 : 3 : return MATCH_ERROR;
8327 : : }
8328 : : else
8329 : : {
8330 : : /* An entry in a function.
8331 : : We need to take special care because writing
8332 : : ENTRY f()
8333 : : as
8334 : : ENTRY f
8335 : : is allowed, whereas
8336 : : ENTRY f() RESULT (r)
8337 : : can't be written as
8338 : : ENTRY f RESULT (r). */
8339 : 629 : if (gfc_match_eos () == MATCH_YES)
8340 : : {
8341 : 24 : gfc_current_locus = old_loc;
8342 : : /* Match the empty argument list, and add the interface to
8343 : : the symbol. */
8344 : 24 : m = gfc_match_formal_arglist (entry, 0, 1);
8345 : : }
8346 : : else
8347 : 605 : m = gfc_match_formal_arglist (entry, 0, 0);
8348 : :
8349 : 629 : if (m != MATCH_YES)
8350 : : return MATCH_ERROR;
8351 : :
8352 : 628 : result = NULL;
8353 : :
8354 : 628 : if (gfc_match_eos () == MATCH_YES)
8355 : : {
8356 : 397 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8357 : 397 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8358 : 2 : return MATCH_ERROR;
8359 : :
8360 : 395 : entry->result = entry;
8361 : : }
8362 : : else
8363 : : {
8364 : 231 : m = gfc_match_suffix (entry, &result);
8365 : 231 : if (m == MATCH_NO)
8366 : 0 : gfc_syntax_error (ST_ENTRY);
8367 : 231 : if (m != MATCH_YES)
8368 : : return MATCH_ERROR;
8369 : :
8370 : 231 : if (result)
8371 : : {
8372 : 212 : if (!gfc_add_result (&result->attr, result->name, NULL)
8373 : 212 : || !gfc_add_entry (&entry->attr, result->name, NULL)
8374 : 424 : || !gfc_add_function (&entry->attr, result->name, NULL))
8375 : 0 : return MATCH_ERROR;
8376 : 212 : entry->result = result;
8377 : : }
8378 : : else
8379 : : {
8380 : 19 : if (!gfc_add_entry (&entry->attr, entry->name, NULL)
8381 : 19 : || !gfc_add_function (&entry->attr, entry->name, NULL))
8382 : 0 : return MATCH_ERROR;
8383 : 19 : entry->result = entry;
8384 : : }
8385 : : }
8386 : :
8387 : 626 : if (!gfc_current_ns->parent
8388 : 626 : && !add_global_entry (name, entry->binding_label, false,
8389 : : &old_loc))
8390 : : return MATCH_ERROR;
8391 : : }
8392 : :
8393 : 754 : if (gfc_match_eos () != MATCH_YES)
8394 : : {
8395 : 0 : gfc_syntax_error (ST_ENTRY);
8396 : 0 : return MATCH_ERROR;
8397 : : }
8398 : :
8399 : : /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
8400 : 754 : if (proc->attr.elemental && entry->attr.is_bind_c)
8401 : : {
8402 : 2 : gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
8403 : : "elemental procedure", &entry->declared_at);
8404 : 2 : return MATCH_ERROR;
8405 : : }
8406 : :
8407 : 752 : entry->attr.recursive = proc->attr.recursive;
8408 : 752 : entry->attr.elemental = proc->attr.elemental;
8409 : 752 : entry->attr.pure = proc->attr.pure;
8410 : :
8411 : 752 : el = gfc_get_entry_list ();
8412 : 752 : el->sym = entry;
8413 : 752 : el->next = gfc_current_ns->entries;
8414 : 752 : gfc_current_ns->entries = el;
8415 : 752 : if (el->next)
8416 : 84 : el->id = el->next->id + 1;
8417 : : else
8418 : 668 : el->id = 1;
8419 : :
8420 : 752 : new_st.op = EXEC_ENTRY;
8421 : 752 : new_st.ext.entry = el;
8422 : :
8423 : 752 : return MATCH_YES;
8424 : : }
8425 : :
8426 : :
8427 : : /* Match a subroutine statement, including optional prefixes. */
8428 : :
8429 : : match
8430 : 781923 : gfc_match_subroutine (void)
8431 : : {
8432 : 781923 : char name[GFC_MAX_SYMBOL_LEN + 1];
8433 : 781923 : gfc_symbol *sym;
8434 : 781923 : match m;
8435 : 781923 : match is_bind_c;
8436 : 781923 : char peek_char;
8437 : 781923 : bool allow_binding_name;
8438 : 781923 : locus loc;
8439 : :
8440 : 781923 : if (gfc_current_state () != COMP_NONE
8441 : 741462 : && gfc_current_state () != COMP_INTERFACE
8442 : 719841 : && gfc_current_state () != COMP_CONTAINS)
8443 : : return MATCH_NO;
8444 : :
8445 : 102224 : m = gfc_match_prefix (NULL);
8446 : 102224 : if (m != MATCH_YES)
8447 : : return m;
8448 : :
8449 : 92805 : loc = gfc_current_locus;
8450 : 92805 : m = gfc_match ("subroutine% %n", name);
8451 : 92805 : if (m != MATCH_YES)
8452 : : return m;
8453 : :
8454 : 42001 : if (get_proc_name (name, &sym, false))
8455 : : return MATCH_ERROR;
8456 : :
8457 : : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8458 : : the symbol existed before. */
8459 : 41989 : sym->declared_at = gfc_get_location_range (NULL, 0, &loc, 1,
8460 : : &gfc_current_locus);
8461 : :
8462 : 41989 : if (current_attr.module_procedure)
8463 : 364 : sym->attr.module_procedure = 1;
8464 : :
8465 : 41989 : if (add_hidden_procptr_result (sym))
8466 : 9 : sym = sym->result;
8467 : :
8468 : 41989 : gfc_new_block = sym;
8469 : :
8470 : : /* Check what next non-whitespace character is so we can tell if there
8471 : : is the required parens if we have a BIND(C). */
8472 : 41989 : gfc_gobble_whitespace ();
8473 : 41989 : peek_char = gfc_peek_ascii_char ();
8474 : :
8475 : 41989 : if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
8476 : : return MATCH_ERROR;
8477 : :
8478 : 41986 : if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
8479 : : return MATCH_ERROR;
8480 : :
8481 : : /* Make sure that it isn't already declared as BIND(C). If it is, it
8482 : : must have been marked BIND(C) with a BIND(C) attribute and that is
8483 : : not allowed for procedures. */
8484 : 41986 : if (sym->attr.is_bind_c == 1)
8485 : : {
8486 : 4 : sym->attr.is_bind_c = 0;
8487 : :
8488 : 4 : if (gfc_state_stack->previous
8489 : 4 : && gfc_state_stack->previous->state != COMP_SUBMODULE)
8490 : : {
8491 : 2 : locus loc;
8492 : 2 : loc = sym->old_symbol != NULL
8493 : 2 : ? sym->old_symbol->declared_at : gfc_current_locus;
8494 : 2 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
8495 : : "variables or common blocks", &loc);
8496 : : }
8497 : : }
8498 : :
8499 : : /* C binding names are not allowed for internal procedures. */
8500 : 41986 : if (gfc_current_state () == COMP_CONTAINS
8501 : 25300 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
8502 : : allow_binding_name = false;
8503 : : else
8504 : 27445 : allow_binding_name = true;
8505 : :
8506 : : /* Here, we are just checking if it has the bind(c) attribute, and if
8507 : : so, then we need to make sure it's all correct. If it doesn't,
8508 : : we still need to continue matching the rest of the subroutine line. */
8509 : 41986 : gfc_gobble_whitespace ();
8510 : 41986 : loc = gfc_current_locus;
8511 : 41986 : is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
8512 : 41986 : if (is_bind_c == MATCH_ERROR)
8513 : : {
8514 : : /* There was an attempt at the bind(c), but it was wrong. An
8515 : : error message should have been printed w/in the gfc_match_bind_c
8516 : : so here we'll just return the MATCH_ERROR. */
8517 : : return MATCH_ERROR;
8518 : : }
8519 : :
8520 : 41973 : if (is_bind_c == MATCH_YES)
8521 : : {
8522 : 3967 : gfc_formal_arglist *arg;
8523 : :
8524 : : /* The following is allowed in the Fortran 2008 draft. */
8525 : 3967 : if (gfc_current_state () == COMP_CONTAINS
8526 : 1296 : && sym->ns->proc_name->attr.flavor != FL_MODULE
8527 : 4378 : && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
8528 : : "at %L may not be specified for an internal "
8529 : : "procedure", &gfc_current_locus))
8530 : : return MATCH_ERROR;
8531 : :
8532 : 3964 : if (peek_char != '(')
8533 : : {
8534 : 1 : gfc_error ("Missing required parentheses before BIND(C) at %C");
8535 : 1 : return MATCH_ERROR;
8536 : : }
8537 : :
8538 : : /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
8539 : : subprogram and a binding label is specified, it shall be the
8540 : : same as the binding label specified in the corresponding module
8541 : : procedure interface body. */
8542 : 3963 : if (sym->attr.module_procedure && sym->old_symbol
8543 : 3 : && strcmp (sym->name, sym->old_symbol->name) == 0
8544 : 3 : && sym->binding_label && sym->old_symbol->binding_label
8545 : 2 : && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
8546 : : {
8547 : 1 : const char *null = "NULL", *s1, *s2;
8548 : 1 : s1 = sym->binding_label;
8549 : 1 : if (!s1) s1 = null;
8550 : 1 : s2 = sym->old_symbol->binding_label;
8551 : 1 : if (!s2) s2 = null;
8552 : 1 : gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
8553 : 1 : sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
8554 : 1 : return MATCH_ERROR;
8555 : : }
8556 : :
8557 : : /* Scan the dummy arguments for an alternate return. */
8558 : 12239 : for (arg = sym->formal; arg; arg = arg->next)
8559 : 8278 : if (!arg->sym)
8560 : : {
8561 : 1 : gfc_error ("Alternate return dummy argument cannot appear in a "
8562 : : "SUBROUTINE with the BIND(C) attribute at %L", &loc);
8563 : 1 : return MATCH_ERROR;
8564 : : }
8565 : :
8566 : 3961 : if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
8567 : : return MATCH_ERROR;
8568 : : }
8569 : :
8570 : 41966 : if (gfc_match_eos () != MATCH_YES)
8571 : : {
8572 : 1 : gfc_syntax_error (ST_SUBROUTINE);
8573 : 1 : return MATCH_ERROR;
8574 : : }
8575 : :
8576 : 41965 : if (!copy_prefix (&sym->attr, &sym->declared_at))
8577 : : {
8578 : 4 : if(!sym->attr.module_procedure)
8579 : : return MATCH_ERROR;
8580 : : else
8581 : 3 : gfc_error_check ();
8582 : : }
8583 : :
8584 : : /* Warn if it has the same name as an intrinsic. */
8585 : 41964 : do_warn_intrinsic_shadow (sym, false);
8586 : :
8587 : 41964 : return MATCH_YES;
8588 : : }
8589 : :
8590 : :
8591 : : /* Check that the NAME identifier in a BIND attribute or statement
8592 : : is conform to C identifier rules. */
8593 : :
8594 : : match
8595 : 1162 : check_bind_name_identifier (char **name)
8596 : : {
8597 : 1162 : char *n = *name, *p;
8598 : :
8599 : : /* Remove leading spaces. */
8600 : 1188 : while (*n == ' ')
8601 : 26 : n++;
8602 : :
8603 : : /* On an empty string, free memory and set name to NULL. */
8604 : 1162 : if (*n == '\0')
8605 : : {
8606 : 42 : free (*name);
8607 : 42 : *name = NULL;
8608 : 42 : return MATCH_YES;
8609 : : }
8610 : :
8611 : : /* Remove trailing spaces. */
8612 : 1120 : p = n + strlen(n) - 1;
8613 : 1136 : while (*p == ' ')
8614 : 16 : *(p--) = '\0';
8615 : :
8616 : : /* Insert the identifier into the symbol table. */
8617 : 1120 : p = xstrdup (n);
8618 : 1120 : free (*name);
8619 : 1120 : *name = p;
8620 : :
8621 : : /* Now check that identifier is valid under C rules. */
8622 : 1120 : if (ISDIGIT (*p))
8623 : : {
8624 : 2 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
8625 : 2 : return MATCH_ERROR;
8626 : : }
8627 : :
8628 : 12355 : for (; *p; p++)
8629 : 11240 : if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
8630 : : {
8631 : 3 : gfc_error ("Invalid C identifier in NAME= specifier at %C");
8632 : 3 : return MATCH_ERROR;
8633 : : }
8634 : :
8635 : : return MATCH_YES;
8636 : : }
8637 : :
8638 : :
8639 : : /* Match a BIND(C) specifier, with the optional 'name=' specifier if
8640 : : given, and set the binding label in either the given symbol (if not
8641 : : NULL), or in the current_ts. The symbol may be NULL because we may
8642 : : encounter the BIND(C) before the declaration itself. Return
8643 : : MATCH_NO if what we're looking at isn't a BIND(C) specifier,
8644 : : MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
8645 : : or MATCH_YES if the specifier was correct and the binding label and
8646 : : bind(c) fields were set correctly for the given symbol or the
8647 : : current_ts. If allow_binding_name is false, no binding name may be
8648 : : given. */
8649 : :
8650 : : match
8651 : 50252 : gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
8652 : : {
8653 : 50252 : char *binding_label = NULL;
8654 : 50252 : gfc_expr *e = NULL;
8655 : :
8656 : : /* Initialize the flag that specifies whether we encountered a NAME=
8657 : : specifier or not. */
8658 : 50252 : has_name_equals = 0;
8659 : :
8660 : : /* This much we have to be able to match, in this order, if
8661 : : there is a bind(c) label. */
8662 : 50252 : if (gfc_match (" bind ( c ") != MATCH_YES)
8663 : : return MATCH_NO;
8664 : :
8665 : : /* Now see if there is a binding label, or if we've reached the
8666 : : end of the bind(c) attribute without one. */
8667 : 6840 : if (gfc_match_char (',') == MATCH_YES)
8668 : : {
8669 : 1169 : if (gfc_match (" name = ") != MATCH_YES)
8670 : : {
8671 : 1 : gfc_error ("Syntax error in NAME= specifier for binding label "
8672 : : "at %C");
8673 : : /* should give an error message here */
8674 : 1 : return MATCH_ERROR;
8675 : : }
8676 : :
8677 : 1168 : has_name_equals = 1;
8678 : :
8679 : 1168 : if (gfc_match_init_expr (&e) != MATCH_YES)
8680 : : {
8681 : 2 : gfc_free_expr (e);
8682 : 2 : return MATCH_ERROR;
8683 : : }
8684 : :
8685 : 1166 : if (!gfc_simplify_expr(e, 0))
8686 : : {
8687 : 0 : gfc_error ("NAME= specifier at %C should be a constant expression");
8688 : 0 : gfc_free_expr (e);
8689 : 0 : return MATCH_ERROR;
8690 : : }
8691 : :
8692 : 1166 : if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
8693 : 1163 : || e->ts.kind != gfc_default_character_kind || e->rank != 0)
8694 : : {
8695 : 4 : gfc_error ("NAME= specifier at %C should be a scalar of "
8696 : : "default character kind");
8697 : 4 : gfc_free_expr(e);
8698 : 4 : return MATCH_ERROR;
8699 : : }
8700 : :
8701 : : // Get a C string from the Fortran string constant
8702 : 2324 : binding_label = gfc_widechar_to_char (e->value.character.string,
8703 : 1162 : e->value.character.length);
8704 : 1162 : gfc_free_expr(e);
8705 : :
8706 : : // Check that it is valid (old gfc_match_name_C)
8707 : 1162 : if (check_bind_name_identifier (&binding_label) != MATCH_YES)
8708 : : return MATCH_ERROR;
8709 : : }
8710 : :
8711 : : /* Get the required right paren. */
8712 : 6828 : if (gfc_match_char (')') != MATCH_YES)
8713 : : {
8714 : 1 : gfc_error ("Missing closing paren for binding label at %C");
8715 : 1 : return MATCH_ERROR;
8716 : : }
8717 : :
8718 : 6827 : if (has_name_equals && !allow_binding_name)
8719 : : {
8720 : 6 : gfc_error ("No binding name is allowed in BIND(C) at %C");
8721 : 6 : return MATCH_ERROR;
8722 : : }
8723 : :
8724 : 6821 : if (has_name_equals && sym != NULL && sym->attr.dummy)
8725 : : {
8726 : 2 : gfc_error ("For dummy procedure %s, no binding name is "
8727 : : "allowed in BIND(C) at %C", sym->name);
8728 : 2 : return MATCH_ERROR;
8729 : : }
8730 : :
8731 : :
8732 : : /* Save the binding label to the symbol. If sym is null, we're
8733 : : probably matching the typespec attributes of a declaration and
8734 : : haven't gotten the name yet, and therefore, no symbol yet. */
8735 : 6819 : if (binding_label)
8736 : : {
8737 : 1108 : if (sym != NULL)
8738 : 999 : sym->binding_label = binding_label;
8739 : : else
8740 : 109 : curr_binding_label = binding_label;
8741 : : }
8742 : 5711 : else if (allow_binding_name)
8743 : : {
8744 : : /* No binding label, but if symbol isn't null, we
8745 : : can set the label for it here.
8746 : : If name="" or allow_binding_name is false, no C binding name is
8747 : : created. */
8748 : 5288 : if (sym != NULL && sym->name != NULL && has_name_equals == 0)
8749 : 5121 : sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
8750 : : }
8751 : :
8752 : 6819 : if (has_name_equals && gfc_current_state () == COMP_INTERFACE
8753 : 718 : && current_interface.type == INTERFACE_ABSTRACT)
8754 : : {
8755 : 1 : gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
8756 : 1 : return MATCH_ERROR;
8757 : : }
8758 : :
8759 : : return MATCH_YES;
8760 : : }
8761 : :
8762 : :
8763 : : /* Return nonzero if we're currently compiling a contained procedure. */
8764 : :
8765 : : static int
8766 : 61049 : contained_procedure (void)
8767 : : {
8768 : 61049 : gfc_state_data *s = gfc_state_stack;
8769 : :
8770 : 61049 : if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
8771 : 60173 : && s->previous != NULL && s->previous->state == COMP_CONTAINS)
8772 : 35395 : return 1;
8773 : :
8774 : : return 0;
8775 : : }
8776 : :
8777 : : /* Set the kind of each enumerator. The kind is selected such that it is
8778 : : interoperable with the corresponding C enumeration type, making
8779 : : sure that -fshort-enums is honored. */
8780 : :
8781 : : static void
8782 : 158 : set_enum_kind(void)
8783 : : {
8784 : 158 : enumerator_history *current_history = NULL;
8785 : 158 : int kind;
8786 : 158 : int i;
8787 : :
8788 : 158 : if (max_enum == NULL || enum_history == NULL)
8789 : : return;
8790 : :
8791 : 150 : if (!flag_short_enums)
8792 : : return;
8793 : :
8794 : : i = 0;
8795 : 48 : do
8796 : : {
8797 : 48 : kind = gfc_integer_kinds[i++].kind;
8798 : : }
8799 : 48 : while (kind < gfc_c_int_kind
8800 : 72 : && gfc_check_integer_range (max_enum->initializer->value.integer,
8801 : : kind) != ARITH_OK);
8802 : :
8803 : 24 : current_history = enum_history;
8804 : 96 : while (current_history != NULL)
8805 : : {
8806 : 72 : current_history->sym->ts.kind = kind;
8807 : 72 : current_history = current_history->next;
8808 : : }
8809 : : }
8810 : :
8811 : :
8812 : : /* Match any of the various end-block statements. Returns the type of
8813 : : END to the caller. The END INTERFACE, END IF, END DO, END SELECT
8814 : : and END BLOCK statements cannot be replaced by a single END statement. */
8815 : :
8816 : : match
8817 : 179112 : gfc_match_end (gfc_statement *st)
8818 : : {
8819 : 179112 : char name[GFC_MAX_SYMBOL_LEN + 1];
8820 : 179112 : gfc_compile_state state;
8821 : 179112 : locus old_loc;
8822 : 179112 : const char *block_name;
8823 : 179112 : const char *target;
8824 : 179112 : int eos_ok;
8825 : 179112 : match m;
8826 : 179112 : gfc_namespace *parent_ns, *ns, *prev_ns;
8827 : 179112 : gfc_namespace **nsp;
8828 : 179112 : bool abbreviated_modproc_decl = false;
8829 : 179112 : bool got_matching_end = false;
8830 : :
8831 : 179112 : old_loc = gfc_current_locus;
8832 : 179112 : if (gfc_match ("end") != MATCH_YES)
8833 : : return MATCH_NO;
8834 : :
8835 : 174117 : state = gfc_current_state ();
8836 : 94928 : block_name = gfc_current_block () == NULL
8837 : 174117 : ? NULL : gfc_current_block ()->name;
8838 : :
8839 : 174117 : switch (state)
8840 : : {
8841 : 2746 : case COMP_ASSOCIATE:
8842 : 2746 : case COMP_BLOCK:
8843 : 2746 : case COMP_CHANGE_TEAM:
8844 : 2746 : if (startswith (block_name, "block@"))
8845 : : block_name = NULL;
8846 : : break;
8847 : :
8848 : 16739 : case COMP_CONTAINS:
8849 : 16739 : case COMP_DERIVED_CONTAINS:
8850 : 16739 : case COMP_OMP_BEGIN_METADIRECTIVE:
8851 : 16739 : state = gfc_state_stack->previous->state;
8852 : 15232 : block_name = gfc_state_stack->previous->sym == NULL
8853 : 16739 : ? NULL : gfc_state_stack->previous->sym->name;
8854 : 16739 : abbreviated_modproc_decl = gfc_state_stack->previous->sym
8855 : 16739 : && gfc_state_stack->previous->sym->abr_modproc_decl;
8856 : : break;
8857 : :
8858 : : case COMP_OMP_METADIRECTIVE:
8859 : : {
8860 : : /* Metadirectives can be nested, so we need to drill down to the
8861 : : first state that is not COMP_OMP_METADIRECTIVE. */
8862 : : gfc_state_data *state_data = gfc_state_stack;
8863 : :
8864 : 85 : do
8865 : : {
8866 : 85 : state_data = state_data->previous;
8867 : 85 : state = state_data->state;
8868 : 77 : block_name = (state_data->sym == NULL
8869 : 85 : ? NULL : state_data->sym->name);
8870 : 170 : abbreviated_modproc_decl = (state_data->sym
8871 : 85 : && state_data->sym->abr_modproc_decl);
8872 : : }
8873 : 85 : while (state == COMP_OMP_METADIRECTIVE);
8874 : :
8875 : 83 : if (block_name && startswith (block_name, "block@"))
8876 : : block_name = NULL;
8877 : : }
8878 : : break;
8879 : :
8880 : : default:
8881 : : break;
8882 : : }
8883 : :
8884 : 83 : if (!abbreviated_modproc_decl)
8885 : 174116 : abbreviated_modproc_decl = gfc_current_block ()
8886 : 174116 : && gfc_current_block ()->abr_modproc_decl;
8887 : :
8888 : 174117 : switch (state)
8889 : : {
8890 : 27214 : case COMP_NONE:
8891 : 27214 : case COMP_PROGRAM:
8892 : 27214 : *st = ST_END_PROGRAM;
8893 : 27214 : target = " program";
8894 : 27214 : eos_ok = 1;
8895 : 27214 : break;
8896 : :
8897 : 42141 : case COMP_SUBROUTINE:
8898 : 42141 : *st = ST_END_SUBROUTINE;
8899 : 42141 : if (!abbreviated_modproc_decl)
8900 : : target = " subroutine";
8901 : : else
8902 : 134 : target = " procedure";
8903 : 42141 : eos_ok = !contained_procedure ();
8904 : 42141 : break;
8905 : :
8906 : 18908 : case COMP_FUNCTION:
8907 : 18908 : *st = ST_END_FUNCTION;
8908 : 18908 : if (!abbreviated_modproc_decl)
8909 : : target = " function";
8910 : : else
8911 : 108 : target = " procedure";
8912 : 18908 : eos_ok = !contained_procedure ();
8913 : 18908 : break;
8914 : :
8915 : 85 : case COMP_BLOCK_DATA:
8916 : 85 : *st = ST_END_BLOCK_DATA;
8917 : 85 : target = " block data";
8918 : 85 : eos_ok = 1;
8919 : 85 : break;
8920 : :
8921 : 9410 : case COMP_MODULE:
8922 : 9410 : *st = ST_END_MODULE;
8923 : 9410 : target = " module";
8924 : 9410 : eos_ok = 1;
8925 : 9410 : break;
8926 : :
8927 : 228 : case COMP_SUBMODULE:
8928 : 228 : *st = ST_END_SUBMODULE;
8929 : 228 : target = " submodule";
8930 : 228 : eos_ok = 1;
8931 : 228 : break;
8932 : :
8933 : 10444 : case COMP_INTERFACE:
8934 : 10444 : *st = ST_END_INTERFACE;
8935 : 10444 : target = " interface";
8936 : 10444 : eos_ok = 0;
8937 : 10444 : break;
8938 : :
8939 : 257 : case COMP_MAP:
8940 : 257 : *st = ST_END_MAP;
8941 : 257 : target = " map";
8942 : 257 : eos_ok = 0;
8943 : 257 : break;
8944 : :
8945 : 132 : case COMP_UNION:
8946 : 132 : *st = ST_END_UNION;
8947 : 132 : target = " union";
8948 : 132 : eos_ok = 0;
8949 : 132 : break;
8950 : :
8951 : 313 : case COMP_STRUCTURE:
8952 : 313 : *st = ST_END_STRUCTURE;
8953 : 313 : target = " structure";
8954 : 313 : eos_ok = 0;
8955 : 313 : break;
8956 : :
8957 : 12208 : case COMP_DERIVED:
8958 : 12208 : case COMP_DERIVED_CONTAINS:
8959 : 12208 : *st = ST_END_TYPE;
8960 : 12208 : target = " type";
8961 : 12208 : eos_ok = 0;
8962 : 12208 : break;
8963 : :
8964 : 1390 : case COMP_ASSOCIATE:
8965 : 1390 : *st = ST_END_ASSOCIATE;
8966 : 1390 : target = " associate";
8967 : 1390 : eos_ok = 0;
8968 : 1390 : break;
8969 : :
8970 : 1319 : case COMP_BLOCK:
8971 : 1319 : case COMP_OMP_STRICTLY_STRUCTURED_BLOCK:
8972 : 1319 : *st = ST_END_BLOCK;
8973 : 1319 : target = " block";
8974 : 1319 : eos_ok = 0;
8975 : 1319 : break;
8976 : :
8977 : 14348 : case COMP_IF:
8978 : 14348 : *st = ST_ENDIF;
8979 : 14348 : target = " if";
8980 : 14348 : eos_ok = 0;
8981 : 14348 : break;
8982 : :
8983 : 30076 : case COMP_DO:
8984 : 30076 : case COMP_DO_CONCURRENT:
8985 : 30076 : *st = ST_ENDDO;
8986 : 30076 : target = " do";
8987 : 30076 : eos_ok = 0;
8988 : 30076 : break;
8989 : :
8990 : 50 : case COMP_CRITICAL:
8991 : 50 : *st = ST_END_CRITICAL;
8992 : 50 : target = " critical";
8993 : 50 : eos_ok = 0;
8994 : 50 : break;
8995 : :
8996 : 4481 : case COMP_SELECT:
8997 : 4481 : case COMP_SELECT_TYPE:
8998 : 4481 : case COMP_SELECT_RANK:
8999 : 4481 : *st = ST_END_SELECT;
9000 : 4481 : target = " select";
9001 : 4481 : eos_ok = 0;
9002 : 4481 : break;
9003 : :
9004 : 506 : case COMP_FORALL:
9005 : 506 : *st = ST_END_FORALL;
9006 : 506 : target = " forall";
9007 : 506 : eos_ok = 0;
9008 : 506 : break;
9009 : :
9010 : 373 : case COMP_WHERE:
9011 : 373 : *st = ST_END_WHERE;
9012 : 373 : target = " where";
9013 : 373 : eos_ok = 0;
9014 : 373 : break;
9015 : :
9016 : 158 : case COMP_ENUM:
9017 : 158 : *st = ST_END_ENUM;
9018 : 158 : target = " enum";
9019 : 158 : eos_ok = 0;
9020 : 158 : last_initializer = NULL;
9021 : 158 : set_enum_kind ();
9022 : 158 : gfc_free_enum_history ();
9023 : 158 : break;
9024 : :
9025 : 0 : case COMP_OMP_BEGIN_METADIRECTIVE:
9026 : 0 : *st = ST_OMP_END_METADIRECTIVE;
9027 : 0 : target = " metadirective";
9028 : 0 : eos_ok = 0;
9029 : 0 : break;
9030 : :
9031 : 67 : case COMP_CHANGE_TEAM:
9032 : 67 : *st = ST_END_TEAM;
9033 : 67 : target = " team";
9034 : 67 : eos_ok = 0;
9035 : 67 : break;
9036 : :
9037 : 9 : default:
9038 : 9 : gfc_error ("Unexpected END statement at %C");
9039 : 9 : goto cleanup;
9040 : : }
9041 : :
9042 : 174108 : old_loc = gfc_current_locus;
9043 : 174108 : if (gfc_match_eos () == MATCH_YES)
9044 : : {
9045 : 20093 : if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
9046 : : {
9047 : 7865 : if (!gfc_notify_std (GFC_STD_F2008, "END statement "
9048 : : "instead of %s statement at %L",
9049 : : abbreviated_modproc_decl ? "END PROCEDURE"
9050 : 3920 : : gfc_ascii_statement(*st), &old_loc))
9051 : 4 : goto cleanup;
9052 : : }
9053 : 9 : else if (!eos_ok)
9054 : : {
9055 : : /* We would have required END [something]. */
9056 : 9 : gfc_error ("%s statement expected at %L",
9057 : : gfc_ascii_statement (*st), &old_loc);
9058 : 9 : goto cleanup;
9059 : : }
9060 : :
9061 : 20080 : return MATCH_YES;
9062 : : }
9063 : :
9064 : : /* Verify that we've got the sort of end-block that we're expecting. */
9065 : 154015 : if (gfc_match (target) != MATCH_YES)
9066 : : {
9067 : 327 : gfc_error ("Expecting %s statement at %L", abbreviated_modproc_decl
9068 : 163 : ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
9069 : 164 : goto cleanup;
9070 : : }
9071 : : else
9072 : 153851 : got_matching_end = true;
9073 : :
9074 : 153851 : if (*st == ST_END_TEAM && gfc_match_end_team () == MATCH_ERROR)
9075 : : /* Emit errors of stat and errmsg parsing now to finish the block and
9076 : : continue analysis of compilation unit. */
9077 : 2 : gfc_error_check ();
9078 : :
9079 : 153851 : old_loc = gfc_current_locus;
9080 : : /* If we're at the end, make sure a block name wasn't required. */
9081 : 153851 : if (gfc_match_eos () == MATCH_YES)
9082 : : {
9083 : 101733 : if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
9084 : : && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
9085 : : && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL
9086 : : && *st != ST_END_TEAM)
9087 : : return MATCH_YES;
9088 : :
9089 : 52117 : if (!block_name)
9090 : : return MATCH_YES;
9091 : :
9092 : 8 : gfc_error ("Expected block name of %qs in %s statement at %L",
9093 : : block_name, gfc_ascii_statement (*st), &old_loc);
9094 : :
9095 : 8 : return MATCH_ERROR;
9096 : : }
9097 : :
9098 : : /* END INTERFACE has a special handler for its several possible endings. */
9099 : 52118 : if (*st == ST_END_INTERFACE)
9100 : 592 : return gfc_match_end_interface ();
9101 : :
9102 : : /* We haven't hit the end of statement, so what is left must be an
9103 : : end-name. */
9104 : 51526 : m = gfc_match_space ();
9105 : 51526 : if (m == MATCH_YES)
9106 : 51526 : m = gfc_match_name (name);
9107 : :
9108 : 51526 : if (m == MATCH_NO)
9109 : 0 : gfc_error ("Expected terminating name at %C");
9110 : 51526 : if (m != MATCH_YES)
9111 : 0 : goto cleanup;
9112 : :
9113 : 51526 : if (block_name == NULL)
9114 : 15 : goto syntax;
9115 : :
9116 : : /* We have to pick out the declared submodule name from the composite
9117 : : required by F2008:11.2.3 para 2, which ends in the declared name. */
9118 : 51511 : if (state == COMP_SUBMODULE)
9119 : 114 : block_name = strchr (block_name, '.') + 1;
9120 : :
9121 : 51511 : if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
9122 : : {
9123 : 8 : gfc_error ("Expected label %qs for %s statement at %C", block_name,
9124 : : gfc_ascii_statement (*st));
9125 : 8 : goto cleanup;
9126 : : }
9127 : : /* Procedure pointer as function result. */
9128 : 51503 : else if (strcmp (block_name, "ppr@") == 0
9129 : 21 : && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
9130 : : {
9131 : 0 : gfc_error ("Expected label %qs for %s statement at %C",
9132 : 0 : gfc_current_block ()->ns->proc_name->name,
9133 : : gfc_ascii_statement (*st));
9134 : 0 : goto cleanup;
9135 : : }
9136 : :
9137 : 51503 : if (gfc_match_eos () == MATCH_YES)
9138 : : return MATCH_YES;
9139 : :
9140 : 0 : syntax:
9141 : 15 : gfc_syntax_error (*st);
9142 : :
9143 : 209 : cleanup:
9144 : 209 : gfc_current_locus = old_loc;
9145 : :
9146 : : /* If we are missing an END BLOCK, we created a half-ready namespace.
9147 : : Remove it from the parent namespace's sibling list. */
9148 : :
9149 : 209 : if (state == COMP_BLOCK && !got_matching_end)
9150 : : {
9151 : 7 : parent_ns = gfc_current_ns->parent;
9152 : :
9153 : 7 : nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
9154 : :
9155 : 7 : prev_ns = NULL;
9156 : 7 : ns = *nsp;
9157 : 14 : while (ns)
9158 : : {
9159 : 7 : if (ns == gfc_current_ns)
9160 : : {
9161 : 7 : if (prev_ns == NULL)
9162 : 7 : *nsp = NULL;
9163 : : else
9164 : 0 : prev_ns->sibling = ns->sibling;
9165 : : }
9166 : 7 : prev_ns = ns;
9167 : 7 : ns = ns->sibling;
9168 : : }
9169 : :
9170 : 7 : gfc_free_namespace (gfc_current_ns);
9171 : 7 : gfc_current_ns = parent_ns;
9172 : 7 : gfc_state_stack = gfc_state_stack->previous;
9173 : 7 : state = gfc_current_state ();
9174 : : }
9175 : :
9176 : : return MATCH_ERROR;
9177 : : }
9178 : :
9179 : :
9180 : :
9181 : : /***************** Attribute declaration statements ****************/
9182 : :
9183 : : /* Set the attribute of a single variable. */
9184 : :
9185 : : static match
9186 : 10231 : attr_decl1 (void)
9187 : : {
9188 : 10231 : char name[GFC_MAX_SYMBOL_LEN + 1];
9189 : 10231 : gfc_array_spec *as;
9190 : :
9191 : : /* Workaround -Wmaybe-uninitialized false positive during
9192 : : profiledbootstrap by initializing them. */
9193 : 10231 : gfc_symbol *sym = NULL;
9194 : 10231 : locus var_locus;
9195 : 10231 : match m;
9196 : :
9197 : 10231 : as = NULL;
9198 : :
9199 : 10231 : m = gfc_match_name (name);
9200 : 10231 : if (m != MATCH_YES)
9201 : 0 : goto cleanup;
9202 : :
9203 : 10231 : if (find_special (name, &sym, false))
9204 : : return MATCH_ERROR;
9205 : :
9206 : 10231 : if (!check_function_name (name))
9207 : : {
9208 : 7 : m = MATCH_ERROR;
9209 : 7 : goto cleanup;
9210 : : }
9211 : :
9212 : 10224 : var_locus = gfc_current_locus;
9213 : :
9214 : : /* Deal with possible array specification for certain attributes. */
9215 : 10224 : if (current_attr.dimension
9216 : 8650 : || current_attr.codimension
9217 : 8629 : || current_attr.allocatable
9218 : 8206 : || current_attr.pointer
9219 : 7495 : || current_attr.target)
9220 : : {
9221 : 2955 : m = gfc_match_array_spec (&as, !current_attr.codimension,
9222 : : !current_attr.dimension
9223 : 1381 : && !current_attr.pointer
9224 : 3625 : && !current_attr.target);
9225 : 2955 : if (m == MATCH_ERROR)
9226 : 2 : goto cleanup;
9227 : :
9228 : 2953 : if (current_attr.dimension && m == MATCH_NO)
9229 : : {
9230 : 0 : gfc_error ("Missing array specification at %L in DIMENSION "
9231 : : "statement", &var_locus);
9232 : 0 : m = MATCH_ERROR;
9233 : 0 : goto cleanup;
9234 : : }
9235 : :
9236 : 2953 : if (current_attr.dimension && sym->value)
9237 : : {
9238 : 1 : gfc_error ("Dimensions specified for %s at %L after its "
9239 : : "initialization", sym->name, &var_locus);
9240 : 1 : m = MATCH_ERROR;
9241 : 1 : goto cleanup;
9242 : : }
9243 : :
9244 : 2952 : if (current_attr.codimension && m == MATCH_NO)
9245 : : {
9246 : 0 : gfc_error ("Missing array specification at %L in CODIMENSION "
9247 : : "statement", &var_locus);
9248 : 0 : m = MATCH_ERROR;
9249 : 0 : goto cleanup;
9250 : : }
9251 : :
9252 : 2952 : if ((current_attr.allocatable || current_attr.pointer)
9253 : 1134 : && (m == MATCH_YES) && (as->type != AS_DEFERRED))
9254 : : {
9255 : 0 : gfc_error ("Array specification must be deferred at %L", &var_locus);
9256 : 0 : m = MATCH_ERROR;
9257 : 0 : goto cleanup;
9258 : : }
9259 : : }
9260 : :
9261 : 10221 : if (sym->ts.type == BT_CLASS
9262 : 200 : && sym->ts.u.derived
9263 : 200 : && sym->ts.u.derived->attr.is_class)
9264 : : {
9265 : 177 : sym->attr.pointer = CLASS_DATA(sym)->attr.class_pointer;
9266 : 177 : sym->attr.allocatable = CLASS_DATA(sym)->attr.allocatable;
9267 : 177 : sym->attr.dimension = CLASS_DATA(sym)->attr.dimension;
9268 : 177 : sym->attr.codimension = CLASS_DATA(sym)->attr.codimension;
9269 : 177 : if (CLASS_DATA (sym)->as)
9270 : 123 : sym->as = gfc_copy_array_spec (CLASS_DATA (sym)->as);
9271 : : }
9272 : 8649 : if (current_attr.dimension == 0 && current_attr.codimension == 0
9273 : 18850 : && !gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus))
9274 : : {
9275 : 22 : m = MATCH_ERROR;
9276 : 22 : goto cleanup;
9277 : : }
9278 : 10199 : if (!gfc_set_array_spec (sym, as, &var_locus))
9279 : : {
9280 : 18 : m = MATCH_ERROR;
9281 : 18 : goto cleanup;
9282 : : }
9283 : :
9284 : 10181 : if (sym->attr.cray_pointee && sym->as != NULL)
9285 : : {
9286 : : /* Fix the array spec. */
9287 : 2 : m = gfc_mod_pointee_as (sym->as);
9288 : 2 : if (m == MATCH_ERROR)
9289 : 0 : goto cleanup;
9290 : : }
9291 : :
9292 : 10181 : if (!gfc_add_attribute (&sym->attr, &var_locus))
9293 : : {
9294 : 0 : m = MATCH_ERROR;
9295 : 0 : goto cleanup;
9296 : : }
9297 : :
9298 : 5685 : if ((current_attr.external || current_attr.intrinsic)
9299 : 6112 : && sym->attr.flavor != FL_PROCEDURE
9300 : 16261 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
9301 : : {
9302 : 0 : m = MATCH_ERROR;
9303 : 0 : goto cleanup;
9304 : : }
9305 : :
9306 : 10181 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class
9307 : 169 : && !as && !current_attr.pointer && !current_attr.allocatable
9308 : 136 : && !current_attr.external)
9309 : : {
9310 : 136 : sym->attr.pointer = 0;
9311 : 136 : sym->attr.allocatable = 0;
9312 : 136 : sym->attr.dimension = 0;
9313 : 136 : sym->attr.codimension = 0;
9314 : 136 : gfc_free_array_spec (sym->as);
9315 : 136 : sym->as = NULL;
9316 : : }
9317 : 10045 : else if (sym->ts.type == BT_CLASS
9318 : 10045 : && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
9319 : : {
9320 : 0 : m = MATCH_ERROR;
9321 : 0 : goto cleanup;
9322 : : }
9323 : :
9324 : 10181 : add_hidden_procptr_result (sym);
9325 : :
9326 : 10181 : return MATCH_YES;
9327 : :
9328 : 50 : cleanup:
9329 : 50 : gfc_free_array_spec (as);
9330 : 50 : return m;
9331 : : }
9332 : :
9333 : :
9334 : : /* Generic attribute declaration subroutine. Used for attributes that
9335 : : just have a list of names. */
9336 : :
9337 : : static match
9338 : 6571 : attr_decl (void)
9339 : : {
9340 : 6571 : match m;
9341 : :
9342 : : /* Gobble the optional double colon, by simply ignoring the result
9343 : : of gfc_match(). */
9344 : 6571 : gfc_match (" ::");
9345 : :
9346 : 10231 : for (;;)
9347 : : {
9348 : 10231 : m = attr_decl1 ();
9349 : 10231 : if (m != MATCH_YES)
9350 : : break;
9351 : :
9352 : 10181 : if (gfc_match_eos () == MATCH_YES)
9353 : : {
9354 : : m = MATCH_YES;
9355 : : break;
9356 : : }
9357 : :
9358 : 3660 : if (gfc_match_char (',') != MATCH_YES)
9359 : : {
9360 : 0 : gfc_error ("Unexpected character in variable list at %C");
9361 : 0 : m = MATCH_ERROR;
9362 : 0 : break;
9363 : : }
9364 : : }
9365 : :
9366 : 6571 : return m;
9367 : : }
9368 : :
9369 : :
9370 : : /* This routine matches Cray Pointer declarations of the form:
9371 : : pointer ( <pointer>, <pointee> )
9372 : : or
9373 : : pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
9374 : : The pointer, if already declared, should be an integer. Otherwise, we
9375 : : set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
9376 : : be either a scalar, or an array declaration. No space is allocated for
9377 : : the pointee. For the statement
9378 : : pointer (ipt, ar(10))
9379 : : any subsequent uses of ar will be translated (in C-notation) as
9380 : : ar(i) => ((<type> *) ipt)(i)
9381 : : After gimplification, pointee variable will disappear in the code. */
9382 : :
9383 : : static match
9384 : 334 : cray_pointer_decl (void)
9385 : : {
9386 : 334 : match m;
9387 : 334 : gfc_array_spec *as = NULL;
9388 : 334 : gfc_symbol *cptr; /* Pointer symbol. */
9389 : 334 : gfc_symbol *cpte; /* Pointee symbol. */
9390 : 334 : locus var_locus;
9391 : 334 : bool done = false;
9392 : :
9393 : 334 : while (!done)
9394 : : {
9395 : 347 : if (gfc_match_char ('(') != MATCH_YES)
9396 : : {
9397 : 1 : gfc_error ("Expected %<(%> at %C");
9398 : 1 : return MATCH_ERROR;
9399 : : }
9400 : :
9401 : : /* Match pointer. */
9402 : 346 : var_locus = gfc_current_locus;
9403 : 346 : gfc_clear_attr (¤t_attr);
9404 : 346 : gfc_add_cray_pointer (¤t_attr, &var_locus);
9405 : 346 : current_ts.type = BT_INTEGER;
9406 : 346 : current_ts.kind = gfc_index_integer_kind;
9407 : :
9408 : 346 : m = gfc_match_symbol (&cptr, 0);
9409 : 346 : if (m != MATCH_YES)
9410 : : {
9411 : 2 : gfc_error ("Expected variable name at %C");
9412 : 2 : return m;
9413 : : }
9414 : :
9415 : 344 : if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
9416 : : return MATCH_ERROR;
9417 : :
9418 : 341 : gfc_set_sym_referenced (cptr);
9419 : :
9420 : 341 : if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
9421 : : {
9422 : 327 : cptr->ts.type = BT_INTEGER;
9423 : 327 : cptr->ts.kind = gfc_index_integer_kind;
9424 : : }
9425 : 14 : else if (cptr->ts.type != BT_INTEGER)
9426 : : {
9427 : 1 : gfc_error ("Cray pointer at %C must be an integer");
9428 : 1 : return MATCH_ERROR;
9429 : : }
9430 : 13 : else if (cptr->ts.kind < gfc_index_integer_kind)
9431 : 0 : gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
9432 : : " memory addresses require %d bytes",
9433 : : cptr->ts.kind, gfc_index_integer_kind);
9434 : :
9435 : 340 : if (gfc_match_char (',') != MATCH_YES)
9436 : : {
9437 : 2 : gfc_error ("Expected \",\" at %C");
9438 : 2 : return MATCH_ERROR;
9439 : : }
9440 : :
9441 : : /* Match Pointee. */
9442 : 338 : var_locus = gfc_current_locus;
9443 : 338 : gfc_clear_attr (¤t_attr);
9444 : 338 : gfc_add_cray_pointee (¤t_attr, &var_locus);
9445 : 338 : current_ts.type = BT_UNKNOWN;
9446 : 338 : current_ts.kind = 0;
9447 : :
9448 : 338 : m = gfc_match_symbol (&cpte, 0);
9449 : 338 : if (m != MATCH_YES)
9450 : : {
9451 : 2 : gfc_error ("Expected variable name at %C");
9452 : 2 : return m;
9453 : : }
9454 : :
9455 : : /* Check for an optional array spec. */
9456 : 336 : m = gfc_match_array_spec (&as, true, false);
9457 : 336 : if (m == MATCH_ERROR)
9458 : : {
9459 : 0 : gfc_free_array_spec (as);
9460 : 0 : return m;
9461 : : }
9462 : 336 : else if (m == MATCH_NO)
9463 : : {
9464 : 226 : gfc_free_array_spec (as);
9465 : 226 : as = NULL;
9466 : : }
9467 : :
9468 : 336 : if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
9469 : : return MATCH_ERROR;
9470 : :
9471 : 329 : gfc_set_sym_referenced (cpte);
9472 : :
9473 : 329 : if (cpte->as == NULL)
9474 : : {
9475 : 247 : if (!gfc_set_array_spec (cpte, as, &var_locus))
9476 : 0 : gfc_internal_error ("Cannot set Cray pointee array spec.");
9477 : : }
9478 : 82 : else if (as != NULL)
9479 : : {
9480 : 1 : gfc_error ("Duplicate array spec for Cray pointee at %C");
9481 : 1 : gfc_free_array_spec (as);
9482 : 1 : return MATCH_ERROR;
9483 : : }
9484 : :
9485 : 328 : as = NULL;
9486 : :
9487 : 328 : if (cpte->as != NULL)
9488 : : {
9489 : : /* Fix array spec. */
9490 : 190 : m = gfc_mod_pointee_as (cpte->as);
9491 : 190 : if (m == MATCH_ERROR)
9492 : : return m;
9493 : : }
9494 : :
9495 : : /* Point the Pointee at the Pointer. */
9496 : 328 : cpte->cp_pointer = cptr;
9497 : :
9498 : 328 : if (gfc_match_char (')') != MATCH_YES)
9499 : : {
9500 : 2 : gfc_error ("Expected \")\" at %C");
9501 : 2 : return MATCH_ERROR;
9502 : : }
9503 : 326 : m = gfc_match_char (',');
9504 : 326 : if (m != MATCH_YES)
9505 : 313 : done = true; /* Stop searching for more declarations. */
9506 : :
9507 : : }
9508 : :
9509 : 313 : if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
9510 : 313 : || gfc_match_eos () != MATCH_YES)
9511 : : {
9512 : 0 : gfc_error ("Expected %<,%> or end of statement at %C");
9513 : 0 : return MATCH_ERROR;
9514 : : }
9515 : : return MATCH_YES;
9516 : : }
9517 : :
9518 : :
9519 : : match
9520 : 3116 : gfc_match_external (void)
9521 : : {
9522 : :
9523 : 3116 : gfc_clear_attr (¤t_attr);
9524 : 3116 : current_attr.external = 1;
9525 : :
9526 : 3116 : return attr_decl ();
9527 : : }
9528 : :
9529 : :
9530 : : match
9531 : 208 : gfc_match_intent (void)
9532 : : {
9533 : 208 : sym_intent intent;
9534 : :
9535 : : /* This is not allowed within a BLOCK construct! */
9536 : 208 : if (gfc_current_state () == COMP_BLOCK)
9537 : : {
9538 : 2 : gfc_error ("INTENT is not allowed inside of BLOCK at %C");
9539 : 2 : return MATCH_ERROR;
9540 : : }
9541 : :
9542 : 206 : intent = match_intent_spec ();
9543 : 206 : if (intent == INTENT_UNKNOWN)
9544 : : return MATCH_ERROR;
9545 : :
9546 : 206 : gfc_clear_attr (¤t_attr);
9547 : 206 : current_attr.intent = intent;
9548 : :
9549 : 206 : return attr_decl ();
9550 : : }
9551 : :
9552 : :
9553 : : match
9554 : 1456 : gfc_match_intrinsic (void)
9555 : : {
9556 : :
9557 : 1456 : gfc_clear_attr (¤t_attr);
9558 : 1456 : current_attr.intrinsic = 1;
9559 : :
9560 : 1456 : return attr_decl ();
9561 : : }
9562 : :
9563 : :
9564 : : match
9565 : 220 : gfc_match_optional (void)
9566 : : {
9567 : : /* This is not allowed within a BLOCK construct! */
9568 : 220 : if (gfc_current_state () == COMP_BLOCK)
9569 : : {
9570 : 2 : gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
9571 : 2 : return MATCH_ERROR;
9572 : : }
9573 : :
9574 : 218 : gfc_clear_attr (¤t_attr);
9575 : 218 : current_attr.optional = 1;
9576 : :
9577 : 218 : return attr_decl ();
9578 : : }
9579 : :
9580 : :
9581 : : match
9582 : 903 : gfc_match_pointer (void)
9583 : : {
9584 : 903 : gfc_gobble_whitespace ();
9585 : 903 : if (gfc_peek_ascii_char () == '(')
9586 : : {
9587 : 335 : if (!flag_cray_pointer)
9588 : : {
9589 : 1 : gfc_error ("Cray pointer declaration at %C requires "
9590 : : "%<-fcray-pointer%> flag");
9591 : 1 : return MATCH_ERROR;
9592 : : }
9593 : 334 : return cray_pointer_decl ();
9594 : : }
9595 : : else
9596 : : {
9597 : 568 : gfc_clear_attr (¤t_attr);
9598 : 568 : current_attr.pointer = 1;
9599 : :
9600 : 568 : return attr_decl ();
9601 : : }
9602 : : }
9603 : :
9604 : :
9605 : : match
9606 : 161 : gfc_match_allocatable (void)
9607 : : {
9608 : 161 : gfc_clear_attr (¤t_attr);
9609 : 161 : current_attr.allocatable = 1;
9610 : :
9611 : 161 : return attr_decl ();
9612 : : }
9613 : :
9614 : :
9615 : : match
9616 : 22 : gfc_match_codimension (void)
9617 : : {
9618 : 22 : gfc_clear_attr (¤t_attr);
9619 : 22 : current_attr.codimension = 1;
9620 : :
9621 : 22 : return attr_decl ();
9622 : : }
9623 : :
9624 : :
9625 : : match
9626 : 80 : gfc_match_contiguous (void)
9627 : : {
9628 : 80 : if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
9629 : : return MATCH_ERROR;
9630 : :
9631 : 79 : gfc_clear_attr (¤t_attr);
9632 : 79 : current_attr.contiguous = 1;
9633 : :
9634 : 79 : return attr_decl ();
9635 : : }
9636 : :
9637 : :
9638 : : match
9639 : 646 : gfc_match_dimension (void)
9640 : : {
9641 : 646 : gfc_clear_attr (¤t_attr);
9642 : 646 : current_attr.dimension = 1;
9643 : :
9644 : 646 : return attr_decl ();
9645 : : }
9646 : :
9647 : :
9648 : : match
9649 : 99 : gfc_match_target (void)
9650 : : {
9651 : 99 : gfc_clear_attr (¤t_attr);
9652 : 99 : current_attr.target = 1;
9653 : :
9654 : 99 : return attr_decl ();
9655 : : }
9656 : :
9657 : :
9658 : : /* Match the list of entities being specified in a PUBLIC or PRIVATE
9659 : : statement. */
9660 : :
9661 : : static match
9662 : 1677 : access_attr_decl (gfc_statement st)
9663 : : {
9664 : 1677 : char name[GFC_MAX_SYMBOL_LEN + 1];
9665 : 1677 : interface_type type;
9666 : 1677 : gfc_user_op *uop;
9667 : 1677 : gfc_symbol *sym, *dt_sym;
9668 : 1677 : gfc_intrinsic_op op;
9669 : 1677 : match m;
9670 : 1677 : gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
9671 : :
9672 : 1677 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9673 : 0 : goto done;
9674 : :
9675 : 2793 : for (;;)
9676 : : {
9677 : 2793 : m = gfc_match_generic_spec (&type, name, &op);
9678 : 2793 : if (m == MATCH_NO)
9679 : 0 : goto syntax;
9680 : 2793 : if (m == MATCH_ERROR)
9681 : 0 : goto done;
9682 : :
9683 : 2793 : switch (type)
9684 : : {
9685 : 0 : case INTERFACE_NAMELESS:
9686 : 0 : case INTERFACE_ABSTRACT:
9687 : 0 : goto syntax;
9688 : :
9689 : 2719 : case INTERFACE_GENERIC:
9690 : 2719 : case INTERFACE_DTIO:
9691 : :
9692 : 2719 : if (gfc_get_symbol (name, NULL, &sym))
9693 : 0 : goto done;
9694 : :
9695 : 2719 : if (type == INTERFACE_DTIO
9696 : 20 : && gfc_current_ns->proc_name
9697 : 20 : && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
9698 : 20 : && sym->attr.flavor == FL_UNKNOWN)
9699 : 2 : sym->attr.flavor = FL_PROCEDURE;
9700 : :
9701 : 2719 : if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
9702 : 4 : goto done;
9703 : :
9704 : 317 : if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
9705 : 2765 : && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
9706 : 0 : goto done;
9707 : :
9708 : : break;
9709 : :
9710 : 70 : case INTERFACE_INTRINSIC_OP:
9711 : 70 : if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
9712 : : {
9713 : 70 : gfc_intrinsic_op other_op;
9714 : :
9715 : 70 : gfc_current_ns->operator_access[op] = access;
9716 : :
9717 : : /* Handle the case if there is another op with the same
9718 : : function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
9719 : 70 : other_op = gfc_equivalent_op (op);
9720 : :
9721 : 70 : if (other_op != INTRINSIC_NONE)
9722 : 21 : gfc_current_ns->operator_access[other_op] = access;
9723 : : }
9724 : : else
9725 : : {
9726 : 0 : gfc_error ("Access specification of the %s operator at %C has "
9727 : : "already been specified", gfc_op2string (op));
9728 : 0 : goto done;
9729 : : }
9730 : :
9731 : : break;
9732 : :
9733 : 4 : case INTERFACE_USER_OP:
9734 : 4 : uop = gfc_get_uop (name);
9735 : :
9736 : 4 : if (uop->access == ACCESS_UNKNOWN)
9737 : : {
9738 : 3 : uop->access = access;
9739 : : }
9740 : : else
9741 : : {
9742 : 1 : gfc_error ("Access specification of the .%s. operator at %C "
9743 : : "has already been specified", uop->name);
9744 : 1 : goto done;
9745 : : }
9746 : :
9747 : 3 : break;
9748 : : }
9749 : :
9750 : 2788 : if (gfc_match_char (',') == MATCH_NO)
9751 : : break;
9752 : : }
9753 : :
9754 : 1672 : if (gfc_match_eos () != MATCH_YES)
9755 : 0 : goto syntax;
9756 : : return MATCH_YES;
9757 : :
9758 : 0 : syntax:
9759 : 0 : gfc_syntax_error (st);
9760 : :
9761 : : done:
9762 : : return MATCH_ERROR;
9763 : : }
9764 : :
9765 : :
9766 : : match
9767 : 23 : gfc_match_protected (void)
9768 : : {
9769 : 23 : gfc_symbol *sym;
9770 : 23 : match m;
9771 : 23 : char c;
9772 : :
9773 : : /* PROTECTED has already been seen, but must be followed by whitespace
9774 : : or ::. */
9775 : 23 : c = gfc_peek_ascii_char ();
9776 : 23 : if (!gfc_is_whitespace (c) && c != ':')
9777 : : return MATCH_NO;
9778 : :
9779 : 22 : if (!gfc_current_ns->proc_name
9780 : 20 : || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
9781 : : {
9782 : 3 : gfc_error ("PROTECTED at %C only allowed in specification "
9783 : : "part of a module");
9784 : 3 : return MATCH_ERROR;
9785 : :
9786 : : }
9787 : :
9788 : 19 : gfc_match (" ::");
9789 : :
9790 : 19 : if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
9791 : : return MATCH_ERROR;
9792 : :
9793 : : /* PROTECTED has an entity-list. */
9794 : 18 : if (gfc_match_eos () == MATCH_YES)
9795 : 0 : goto syntax;
9796 : :
9797 : 26 : for(;;)
9798 : : {
9799 : 26 : m = gfc_match_symbol (&sym, 0);
9800 : 26 : switch (m)
9801 : : {
9802 : 26 : case MATCH_YES:
9803 : 26 : if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
9804 : : return MATCH_ERROR;
9805 : 25 : goto next_item;
9806 : :
9807 : : case MATCH_NO:
9808 : : break;
9809 : :
9810 : : case MATCH_ERROR:
9811 : : return MATCH_ERROR;
9812 : : }
9813 : :
9814 : 25 : next_item:
9815 : 25 : if (gfc_match_eos () == MATCH_YES)
9816 : : break;
9817 : 8 : if (gfc_match_char (',') != MATCH_YES)
9818 : 0 : goto syntax;
9819 : : }
9820 : :
9821 : : return MATCH_YES;
9822 : :
9823 : 0 : syntax:
9824 : 0 : gfc_error ("Syntax error in PROTECTED statement at %C");
9825 : 0 : return MATCH_ERROR;
9826 : : }
9827 : :
9828 : :
9829 : : /* The PRIVATE statement is a bit weird in that it can be an attribute
9830 : : declaration, but also works as a standalone statement inside of a
9831 : : type declaration or a module. */
9832 : :
9833 : : match
9834 : 28159 : gfc_match_private (gfc_statement *st)
9835 : : {
9836 : 28159 : gfc_state_data *prev;
9837 : :
9838 : 28159 : if (gfc_match ("private") != MATCH_YES)
9839 : : return MATCH_NO;
9840 : :
9841 : : /* Try matching PRIVATE without an access-list. */
9842 : 1545 : if (gfc_match_eos () == MATCH_YES)
9843 : : {
9844 : 1259 : prev = gfc_state_stack->previous;
9845 : 1259 : if (gfc_current_state () != COMP_MODULE
9846 : 360 : && !(gfc_current_state () == COMP_DERIVED
9847 : 327 : && prev && prev->state == COMP_MODULE)
9848 : 34 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9849 : 32 : && prev->previous && prev->previous->state == COMP_MODULE))
9850 : : {
9851 : 2 : gfc_error ("PRIVATE statement at %C is only allowed in the "
9852 : : "specification part of a module");
9853 : 2 : return MATCH_ERROR;
9854 : : }
9855 : :
9856 : 1257 : *st = ST_PRIVATE;
9857 : 1257 : return MATCH_YES;
9858 : : }
9859 : :
9860 : : /* At this point in free-form source code, PRIVATE must be followed
9861 : : by whitespace or ::. */
9862 : 286 : if (gfc_current_form == FORM_FREE)
9863 : : {
9864 : 284 : char c = gfc_peek_ascii_char ();
9865 : 284 : if (!gfc_is_whitespace (c) && c != ':')
9866 : : return MATCH_NO;
9867 : : }
9868 : :
9869 : 285 : prev = gfc_state_stack->previous;
9870 : 285 : if (gfc_current_state () != COMP_MODULE
9871 : 1 : && !(gfc_current_state () == COMP_DERIVED
9872 : 0 : && prev && prev->state == COMP_MODULE)
9873 : 1 : && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9874 : 0 : && prev->previous && prev->previous->state == COMP_MODULE))
9875 : : {
9876 : 1 : gfc_error ("PRIVATE statement at %C is only allowed in the "
9877 : : "specification part of a module");
9878 : 1 : return MATCH_ERROR;
9879 : : }
9880 : :
9881 : 284 : *st = ST_ATTR_DECL;
9882 : 284 : return access_attr_decl (ST_PRIVATE);
9883 : : }
9884 : :
9885 : :
9886 : : match
9887 : 1779 : gfc_match_public (gfc_statement *st)
9888 : : {
9889 : 1779 : if (gfc_match ("public") != MATCH_YES)
9890 : : return MATCH_NO;
9891 : :
9892 : : /* Try matching PUBLIC without an access-list. */
9893 : 1440 : if (gfc_match_eos () == MATCH_YES)
9894 : : {
9895 : 45 : if (gfc_current_state () != COMP_MODULE)
9896 : : {
9897 : 2 : gfc_error ("PUBLIC statement at %C is only allowed in the "
9898 : : "specification part of a module");
9899 : 2 : return MATCH_ERROR;
9900 : : }
9901 : :
9902 : 43 : *st = ST_PUBLIC;
9903 : 43 : return MATCH_YES;
9904 : : }
9905 : :
9906 : : /* At this point in free-form source code, PUBLIC must be followed
9907 : : by whitespace or ::. */
9908 : 1395 : if (gfc_current_form == FORM_FREE)
9909 : : {
9910 : 1393 : char c = gfc_peek_ascii_char ();
9911 : 1393 : if (!gfc_is_whitespace (c) && c != ':')
9912 : : return MATCH_NO;
9913 : : }
9914 : :
9915 : 1394 : if (gfc_current_state () != COMP_MODULE)
9916 : : {
9917 : 1 : gfc_error ("PUBLIC statement at %C is only allowed in the "
9918 : : "specification part of a module");
9919 : 1 : return MATCH_ERROR;
9920 : : }
9921 : :
9922 : 1393 : *st = ST_ATTR_DECL;
9923 : 1393 : return access_attr_decl (ST_PUBLIC);
9924 : : }
9925 : :
9926 : :
9927 : : /* Workhorse for gfc_match_parameter. */
9928 : :
9929 : : static match
9930 : 7639 : do_parm (void)
9931 : : {
9932 : 7639 : gfc_symbol *sym;
9933 : 7639 : gfc_expr *init;
9934 : 7639 : match m;
9935 : 7639 : bool t;
9936 : :
9937 : 7639 : m = gfc_match_symbol (&sym, 0);
9938 : 7639 : if (m == MATCH_NO)
9939 : 0 : gfc_error ("Expected variable name at %C in PARAMETER statement");
9940 : :
9941 : 7639 : if (m != MATCH_YES)
9942 : : return m;
9943 : :
9944 : 7639 : if (gfc_match_char ('=') == MATCH_NO)
9945 : : {
9946 : 0 : gfc_error ("Expected = sign in PARAMETER statement at %C");
9947 : 0 : return MATCH_ERROR;
9948 : : }
9949 : :
9950 : 7639 : m = gfc_match_init_expr (&init);
9951 : 7639 : if (m == MATCH_NO)
9952 : 0 : gfc_error ("Expected expression at %C in PARAMETER statement");
9953 : 7639 : if (m != MATCH_YES)
9954 : : return m;
9955 : :
9956 : 7638 : if (sym->ts.type == BT_UNKNOWN
9957 : 7638 : && !gfc_set_default_type (sym, 1, NULL))
9958 : : {
9959 : 1 : m = MATCH_ERROR;
9960 : 1 : goto cleanup;
9961 : : }
9962 : :
9963 : 7637 : if (!gfc_check_assign_symbol (sym, NULL, init)
9964 : 7637 : || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
9965 : : {
9966 : 1 : m = MATCH_ERROR;
9967 : 1 : goto cleanup;
9968 : : }
9969 : :
9970 : 7636 : if (sym->value)
9971 : : {
9972 : 1 : gfc_error ("Initializing already initialized variable at %C");
9973 : 1 : m = MATCH_ERROR;
9974 : 1 : goto cleanup;
9975 : : }
9976 : :
9977 : 7635 : t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
9978 : 7635 : return (t) ? MATCH_YES : MATCH_ERROR;
9979 : :
9980 : 3 : cleanup:
9981 : 3 : gfc_free_expr (init);
9982 : 3 : return m;
9983 : : }
9984 : :
9985 : :
9986 : : /* Match a parameter statement, with the weird syntax that these have. */
9987 : :
9988 : : match
9989 : 6929 : gfc_match_parameter (void)
9990 : : {
9991 : 6929 : const char *term = " )%t";
9992 : 6929 : match m;
9993 : :
9994 : 6929 : if (gfc_match_char ('(') == MATCH_NO)
9995 : : {
9996 : : /* With legacy PARAMETER statements, don't expect a terminating ')'. */
9997 : 28 : if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9998 : : return MATCH_NO;
9999 : 6928 : term = " %t";
10000 : : }
10001 : :
10002 : 7639 : for (;;)
10003 : : {
10004 : 7639 : m = do_parm ();
10005 : 7639 : if (m != MATCH_YES)
10006 : : break;
10007 : :
10008 : 7635 : if (gfc_match (term) == MATCH_YES)
10009 : : break;
10010 : :
10011 : 711 : if (gfc_match_char (',') != MATCH_YES)
10012 : : {
10013 : 0 : gfc_error ("Unexpected characters in PARAMETER statement at %C");
10014 : 0 : m = MATCH_ERROR;
10015 : 0 : break;
10016 : : }
10017 : : }
10018 : :
10019 : : return m;
10020 : : }
10021 : :
10022 : :
10023 : : match
10024 : 8 : gfc_match_automatic (void)
10025 : : {
10026 : 8 : gfc_symbol *sym;
10027 : 8 : match m;
10028 : 8 : bool seen_symbol = false;
10029 : :
10030 : 8 : if (!flag_dec_static)
10031 : : {
10032 : 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10033 : : "%<-fdec-static%>",
10034 : : "AUTOMATIC"
10035 : : );
10036 : 2 : return MATCH_ERROR;
10037 : : }
10038 : :
10039 : 6 : gfc_match (" ::");
10040 : :
10041 : 6 : for (;;)
10042 : : {
10043 : 6 : m = gfc_match_symbol (&sym, 0);
10044 : 6 : switch (m)
10045 : : {
10046 : : case MATCH_NO:
10047 : : break;
10048 : :
10049 : : case MATCH_ERROR:
10050 : : return MATCH_ERROR;
10051 : :
10052 : 4 : case MATCH_YES:
10053 : 4 : if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
10054 : : return MATCH_ERROR;
10055 : : seen_symbol = true;
10056 : : break;
10057 : : }
10058 : :
10059 : 4 : if (gfc_match_eos () == MATCH_YES)
10060 : : break;
10061 : 0 : if (gfc_match_char (',') != MATCH_YES)
10062 : 0 : goto syntax;
10063 : : }
10064 : :
10065 : 4 : if (!seen_symbol)
10066 : : {
10067 : 2 : gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
10068 : 2 : return MATCH_ERROR;
10069 : : }
10070 : :
10071 : : return MATCH_YES;
10072 : :
10073 : 0 : syntax:
10074 : 0 : gfc_error ("Syntax error in AUTOMATIC statement at %C");
10075 : 0 : return MATCH_ERROR;
10076 : : }
10077 : :
10078 : :
10079 : : match
10080 : 7 : gfc_match_static (void)
10081 : : {
10082 : 7 : gfc_symbol *sym;
10083 : 7 : match m;
10084 : 7 : bool seen_symbol = false;
10085 : :
10086 : 7 : if (!flag_dec_static)
10087 : : {
10088 : 2 : gfc_error ("%s at %C is a DEC extension, enable with "
10089 : : "%<-fdec-static%>",
10090 : : "STATIC");
10091 : 2 : return MATCH_ERROR;
10092 : : }
10093 : :
10094 : 5 : gfc_match (" ::");
10095 : :
10096 : 5 : for (;;)
10097 : : {
10098 : 5 : m = gfc_match_symbol (&sym, 0);
10099 : 5 : switch (m)
10100 : : {
10101 : : case MATCH_NO:
10102 : : break;
10103 : :
10104 : : case MATCH_ERROR:
10105 : : return MATCH_ERROR;
10106 : :
10107 : 3 : case MATCH_YES:
10108 : 3 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10109 : : &gfc_current_locus))
10110 : : return MATCH_ERROR;
10111 : : seen_symbol = true;
10112 : : break;
10113 : : }
10114 : :
10115 : 3 : if (gfc_match_eos () == MATCH_YES)
10116 : : break;
10117 : 0 : if (gfc_match_char (',') != MATCH_YES)
10118 : 0 : goto syntax;
10119 : : }
10120 : :
10121 : 3 : if (!seen_symbol)
10122 : : {
10123 : 2 : gfc_error ("Expected entity-list in STATIC statement at %C");
10124 : 2 : return MATCH_ERROR;
10125 : : }
10126 : :
10127 : : return MATCH_YES;
10128 : :
10129 : 0 : syntax:
10130 : 0 : gfc_error ("Syntax error in STATIC statement at %C");
10131 : 0 : return MATCH_ERROR;
10132 : : }
10133 : :
10134 : :
10135 : : /* Save statements have a special syntax. */
10136 : :
10137 : : match
10138 : 271 : gfc_match_save (void)
10139 : : {
10140 : 271 : char n[GFC_MAX_SYMBOL_LEN+1];
10141 : 271 : gfc_common_head *c;
10142 : 271 : gfc_symbol *sym;
10143 : 271 : match m;
10144 : :
10145 : 271 : if (gfc_match_eos () == MATCH_YES)
10146 : : {
10147 : 150 : if (gfc_current_ns->seen_save)
10148 : : {
10149 : 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
10150 : : "follows previous SAVE statement"))
10151 : : return MATCH_ERROR;
10152 : : }
10153 : :
10154 : 149 : gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
10155 : 149 : return MATCH_YES;
10156 : : }
10157 : :
10158 : 121 : if (gfc_current_ns->save_all)
10159 : : {
10160 : 7 : if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
10161 : : "blanket SAVE statement"))
10162 : : return MATCH_ERROR;
10163 : : }
10164 : :
10165 : 120 : gfc_match (" ::");
10166 : :
10167 : 180 : for (;;)
10168 : : {
10169 : 180 : m = gfc_match_symbol (&sym, 0);
10170 : 180 : switch (m)
10171 : : {
10172 : 178 : case MATCH_YES:
10173 : 178 : if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
10174 : : &gfc_current_locus))
10175 : : return MATCH_ERROR;
10176 : 176 : goto next_item;
10177 : :
10178 : : case MATCH_NO:
10179 : : break;
10180 : :
10181 : : case MATCH_ERROR:
10182 : : return MATCH_ERROR;
10183 : : }
10184 : :
10185 : 2 : m = gfc_match (" / %n /", &n);
10186 : 2 : if (m == MATCH_ERROR)
10187 : : return MATCH_ERROR;
10188 : 2 : if (m == MATCH_NO)
10189 : 0 : goto syntax;
10190 : :
10191 : : /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
10192 : : saved-entity-list that does not specify a common-block-name. */
10193 : 2 : if (gfc_current_state () == COMP_BLOCK)
10194 : : {
10195 : 1 : gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
10196 : : "in a BLOCK construct", n);
10197 : 1 : return MATCH_ERROR;
10198 : : }
10199 : :
10200 : 1 : c = gfc_get_common (n, 0);
10201 : 1 : c->saved = 1;
10202 : :
10203 : 1 : gfc_current_ns->seen_save = 1;
10204 : :
10205 : 177 : next_item:
10206 : 177 : if (gfc_match_eos () == MATCH_YES)
10207 : : break;
10208 : 60 : if (gfc_match_char (',') != MATCH_YES)
10209 : 0 : goto syntax;
10210 : : }
10211 : :
10212 : : return MATCH_YES;
10213 : :
10214 : 0 : syntax:
10215 : 0 : if (gfc_current_ns->seen_save)
10216 : : {
10217 : 0 : gfc_error ("Syntax error in SAVE statement at %C");
10218 : 0 : return MATCH_ERROR;
10219 : : }
10220 : : else
10221 : : return MATCH_NO;
10222 : : }
10223 : :
10224 : :
10225 : : match
10226 : 93 : gfc_match_value (void)
10227 : : {
10228 : 93 : gfc_symbol *sym;
10229 : 93 : match m;
10230 : :
10231 : : /* This is not allowed within a BLOCK construct! */
10232 : 93 : if (gfc_current_state () == COMP_BLOCK)
10233 : : {
10234 : 2 : gfc_error ("VALUE is not allowed inside of BLOCK at %C");
10235 : 2 : return MATCH_ERROR;
10236 : : }
10237 : :
10238 : 91 : if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
10239 : : return MATCH_ERROR;
10240 : :
10241 : 90 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10242 : : {
10243 : : return MATCH_ERROR;
10244 : : }
10245 : :
10246 : 90 : if (gfc_match_eos () == MATCH_YES)
10247 : 0 : goto syntax;
10248 : :
10249 : 116 : for(;;)
10250 : : {
10251 : 116 : m = gfc_match_symbol (&sym, 0);
10252 : 116 : switch (m)
10253 : : {
10254 : 116 : case MATCH_YES:
10255 : 116 : if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
10256 : : return MATCH_ERROR;
10257 : 109 : goto next_item;
10258 : :
10259 : : case MATCH_NO:
10260 : : break;
10261 : :
10262 : : case MATCH_ERROR:
10263 : : return MATCH_ERROR;
10264 : : }
10265 : :
10266 : 109 : next_item:
10267 : 109 : if (gfc_match_eos () == MATCH_YES)
10268 : : break;
10269 : 26 : if (gfc_match_char (',') != MATCH_YES)
10270 : 0 : goto syntax;
10271 : : }
10272 : :
10273 : : return MATCH_YES;
10274 : :
10275 : 0 : syntax:
10276 : 0 : gfc_error ("Syntax error in VALUE statement at %C");
10277 : 0 : return MATCH_ERROR;
10278 : : }
10279 : :
10280 : :
10281 : : match
10282 : 45 : gfc_match_volatile (void)
10283 : : {
10284 : 45 : gfc_symbol *sym;
10285 : 45 : char *name;
10286 : 45 : match m;
10287 : :
10288 : 45 : if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
10289 : : return MATCH_ERROR;
10290 : :
10291 : 44 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10292 : : {
10293 : : return MATCH_ERROR;
10294 : : }
10295 : :
10296 : 44 : if (gfc_match_eos () == MATCH_YES)
10297 : 1 : goto syntax;
10298 : :
10299 : 48 : for(;;)
10300 : : {
10301 : : /* VOLATILE is special because it can be added to host-associated
10302 : : symbols locally. Except for coarrays. */
10303 : 48 : m = gfc_match_symbol (&sym, 1);
10304 : 48 : switch (m)
10305 : : {
10306 : 48 : case MATCH_YES:
10307 : 48 : name = XCNEWVAR (char, strlen (sym->name) + 1);
10308 : 48 : strcpy (name, sym->name);
10309 : 48 : if (!check_function_name (name))
10310 : : return MATCH_ERROR;
10311 : : /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
10312 : : for variable in a BLOCK which is defined outside of the BLOCK. */
10313 : 47 : if (sym->ns != gfc_current_ns && sym->attr.codimension)
10314 : : {
10315 : 2 : gfc_error ("Specifying VOLATILE for coarray variable %qs at "
10316 : : "%C, which is use-/host-associated", sym->name);
10317 : 2 : return MATCH_ERROR;
10318 : : }
10319 : 45 : if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
10320 : : return MATCH_ERROR;
10321 : 42 : goto next_item;
10322 : :
10323 : : case MATCH_NO:
10324 : : break;
10325 : :
10326 : : case MATCH_ERROR:
10327 : : return MATCH_ERROR;
10328 : : }
10329 : :
10330 : 42 : next_item:
10331 : 42 : if (gfc_match_eos () == MATCH_YES)
10332 : : break;
10333 : 5 : if (gfc_match_char (',') != MATCH_YES)
10334 : 0 : goto syntax;
10335 : : }
10336 : :
10337 : : return MATCH_YES;
10338 : :
10339 : 1 : syntax:
10340 : 1 : gfc_error ("Syntax error in VOLATILE statement at %C");
10341 : 1 : return MATCH_ERROR;
10342 : : }
10343 : :
10344 : :
10345 : : match
10346 : 11 : gfc_match_asynchronous (void)
10347 : : {
10348 : 11 : gfc_symbol *sym;
10349 : 11 : char *name;
10350 : 11 : match m;
10351 : :
10352 : 11 : if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
10353 : : return MATCH_ERROR;
10354 : :
10355 : 10 : if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
10356 : : {
10357 : : return MATCH_ERROR;
10358 : : }
10359 : :
10360 : 10 : if (gfc_match_eos () == MATCH_YES)
10361 : 0 : goto syntax;
10362 : :
10363 : 10 : for(;;)
10364 : : {
10365 : : /* ASYNCHRONOUS is special because it can be added to host-associated
10366 : : symbols locally. */
10367 : 10 : m = gfc_match_symbol (&sym, 1);
10368 : 10 : switch (m)
10369 : : {
10370 : 10 : case MATCH_YES:
10371 : 10 : name = XCNEWVAR (char, strlen (sym->name) + 1);
10372 : 10 : strcpy (name, sym->name);
10373 : 10 : if (!check_function_name (name))
10374 : : return MATCH_ERROR;
10375 : 9 : if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
10376 : : return MATCH_ERROR;
10377 : 7 : goto next_item;
10378 : :
10379 : : case MATCH_NO:
10380 : : break;
10381 : :
10382 : : case MATCH_ERROR:
10383 : : return MATCH_ERROR;
10384 : : }
10385 : :
10386 : 7 : next_item:
10387 : 7 : if (gfc_match_eos () == MATCH_YES)
10388 : : break;
10389 : 0 : if (gfc_match_char (',') != MATCH_YES)
10390 : 0 : goto syntax;
10391 : : }
10392 : :
10393 : : return MATCH_YES;
10394 : :
10395 : 0 : syntax:
10396 : 0 : gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
10397 : 0 : return MATCH_ERROR;
10398 : : }
10399 : :
10400 : :
10401 : : /* Match a module procedure statement in a submodule. */
10402 : :
10403 : : match
10404 : 739959 : gfc_match_submod_proc (void)
10405 : : {
10406 : 739959 : char name[GFC_MAX_SYMBOL_LEN + 1];
10407 : 739959 : gfc_symbol *sym, *fsym;
10408 : 739959 : match m;
10409 : 739959 : gfc_formal_arglist *formal, *head, *tail;
10410 : :
10411 : 739959 : if (gfc_current_state () != COMP_CONTAINS
10412 : 14857 : || !(gfc_state_stack->previous
10413 : 14857 : && (gfc_state_stack->previous->state == COMP_SUBMODULE
10414 : 14857 : || gfc_state_stack->previous->state == COMP_MODULE)))
10415 : : return MATCH_NO;
10416 : :
10417 : 7409 : m = gfc_match (" module% procedure% %n", name);
10418 : 7409 : if (m != MATCH_YES)
10419 : : return m;
10420 : :
10421 : 244 : if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
10422 : : "at %C"))
10423 : : return MATCH_ERROR;
10424 : :
10425 : 244 : if (get_proc_name (name, &sym, false))
10426 : : return MATCH_ERROR;
10427 : :
10428 : : /* Make sure that the result field is appropriately filled. */
10429 : 244 : if (sym->tlink && sym->tlink->attr.function)
10430 : : {
10431 : 108 : if (sym->tlink->result && sym->tlink->result != sym->tlink)
10432 : : {
10433 : 65 : sym->result = sym->tlink->result;
10434 : 65 : if (!sym->result->attr.use_assoc)
10435 : : {
10436 : 20 : gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
10437 : : sym->result->name);
10438 : 20 : st->n.sym = sym->result;
10439 : 20 : sym->result->refs++;
10440 : : }
10441 : : }
10442 : : else
10443 : 43 : sym->result = sym;
10444 : : }
10445 : :
10446 : : /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
10447 : : the symbol existed before. */
10448 : 244 : sym->declared_at = gfc_current_locus;
10449 : :
10450 : 244 : if (!sym->attr.module_procedure)
10451 : : return MATCH_ERROR;
10452 : :
10453 : : /* Signal match_end to expect "end procedure". */
10454 : 242 : sym->abr_modproc_decl = 1;
10455 : :
10456 : : /* Change from IFSRC_IFBODY coming from the interface declaration. */
10457 : 242 : sym->attr.if_source = IFSRC_DECL;
10458 : :
10459 : 242 : gfc_new_block = sym;
10460 : :
10461 : : /* Make a new formal arglist with the symbols in the procedure
10462 : : namespace. */
10463 : 242 : head = tail = NULL;
10464 : 548 : for (formal = sym->formal; formal && formal->sym; formal = formal->next)
10465 : : {
10466 : 306 : if (formal == sym->formal)
10467 : 216 : head = tail = gfc_get_formal_arglist ();
10468 : : else
10469 : : {
10470 : 90 : tail->next = gfc_get_formal_arglist ();
10471 : 90 : tail = tail->next;
10472 : : }
10473 : :
10474 : 306 : if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
10475 : 0 : goto cleanup;
10476 : :
10477 : 306 : tail->sym = fsym;
10478 : 306 : gfc_set_sym_referenced (fsym);
10479 : : }
10480 : :
10481 : : /* The dummy symbols get cleaned up, when the formal_namespace of the
10482 : : interface declaration is cleared. This allows us to add the
10483 : : explicit interface as is done for other type of procedure. */
10484 : 242 : if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
10485 : : &gfc_current_locus))
10486 : : return MATCH_ERROR;
10487 : :
10488 : 242 : if (gfc_match_eos () != MATCH_YES)
10489 : : {
10490 : : /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
10491 : : undone, such that the st->n.sym->formal points to the original symbol;
10492 : : if now this namespace is finalized, the formal namespace is freed,
10493 : : but it might be still needed in the parent namespace. */
10494 : 1 : gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
10495 : 1 : st->n.sym = NULL;
10496 : 1 : gfc_free_symbol (sym->tlink);
10497 : 1 : sym->tlink = NULL;
10498 : 1 : sym->refs--;
10499 : 1 : gfc_syntax_error (ST_MODULE_PROC);
10500 : 1 : return MATCH_ERROR;
10501 : : }
10502 : :
10503 : : return MATCH_YES;
10504 : :
10505 : 0 : cleanup:
10506 : 0 : gfc_free_formal_arglist (head);
10507 : 0 : return MATCH_ERROR;
10508 : : }
10509 : :
10510 : :
10511 : : /* Match a module procedure statement. Note that we have to modify
10512 : : symbols in the parent's namespace because the current one was there
10513 : : to receive symbols that are in an interface's formal argument list. */
10514 : :
10515 : : match
10516 : 1455 : gfc_match_modproc (void)
10517 : : {
10518 : 1455 : char name[GFC_MAX_SYMBOL_LEN + 1];
10519 : 1455 : gfc_symbol *sym;
10520 : 1455 : match m;
10521 : 1455 : locus old_locus;
10522 : 1455 : gfc_namespace *module_ns;
10523 : 1455 : gfc_interface *old_interface_head, *interface;
10524 : :
10525 : 1455 : if (gfc_state_stack->previous == NULL
10526 : 1453 : || (gfc_state_stack->state != COMP_INTERFACE
10527 : 4 : && (gfc_state_stack->state != COMP_CONTAINS
10528 : 4 : || gfc_state_stack->previous->state != COMP_INTERFACE))
10529 : 1449 : || current_interface.type == INTERFACE_NAMELESS
10530 : 1449 : || current_interface.type == INTERFACE_ABSTRACT)
10531 : : {
10532 : 7 : gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
10533 : : "interface");
10534 : 7 : return MATCH_ERROR;
10535 : : }
10536 : :
10537 : 1448 : module_ns = gfc_current_ns->parent;
10538 : 1454 : for (; module_ns; module_ns = module_ns->parent)
10539 : 1454 : if (module_ns->proc_name->attr.flavor == FL_MODULE
10540 : 29 : || module_ns->proc_name->attr.flavor == FL_PROGRAM
10541 : 12 : || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
10542 : 12 : && !module_ns->proc_name->attr.contained))
10543 : : break;
10544 : :
10545 : 1448 : if (module_ns == NULL)
10546 : : return MATCH_ERROR;
10547 : :
10548 : : /* Store the current state of the interface. We will need it if we
10549 : : end up with a syntax error and need to recover. */
10550 : 1448 : old_interface_head = gfc_current_interface_head ();
10551 : :
10552 : : /* Check if the F2008 optional double colon appears. */
10553 : 1448 : gfc_gobble_whitespace ();
10554 : 1448 : old_locus = gfc_current_locus;
10555 : 1448 : if (gfc_match ("::") == MATCH_YES)
10556 : : {
10557 : 25 : if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
10558 : : "MODULE PROCEDURE statement at %L", &old_locus))
10559 : : return MATCH_ERROR;
10560 : : }
10561 : : else
10562 : 1423 : gfc_current_locus = old_locus;
10563 : :
10564 : 1799 : for (;;)
10565 : : {
10566 : 1799 : bool last = false;
10567 : 1799 : old_locus = gfc_current_locus;
10568 : :
10569 : 1799 : m = gfc_match_name (name);
10570 : 1799 : if (m == MATCH_NO)
10571 : 1 : goto syntax;
10572 : 1798 : if (m != MATCH_YES)
10573 : : return MATCH_ERROR;
10574 : :
10575 : : /* Check for syntax error before starting to add symbols to the
10576 : : current namespace. */
10577 : 1798 : if (gfc_match_eos () == MATCH_YES)
10578 : : last = true;
10579 : :
10580 : 356 : if (!last && gfc_match_char (',') != MATCH_YES)
10581 : 2 : goto syntax;
10582 : :
10583 : : /* Now we're sure the syntax is valid, we process this item
10584 : : further. */
10585 : 1796 : if (gfc_get_symbol (name, module_ns, &sym))
10586 : : return MATCH_ERROR;
10587 : :
10588 : 1796 : if (sym->attr.intrinsic)
10589 : : {
10590 : 1 : gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
10591 : : "PROCEDURE", &old_locus);
10592 : 1 : return MATCH_ERROR;
10593 : : }
10594 : :
10595 : 1795 : if (sym->attr.proc != PROC_MODULE
10596 : 1795 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
10597 : : return MATCH_ERROR;
10598 : :
10599 : 1792 : if (!gfc_add_interface (sym))
10600 : : return MATCH_ERROR;
10601 : :
10602 : 1789 : sym->attr.mod_proc = 1;
10603 : 1789 : sym->declared_at = old_locus;
10604 : :
10605 : 1789 : if (last)
10606 : : break;
10607 : : }
10608 : :
10609 : : return MATCH_YES;
10610 : :
10611 : 3 : syntax:
10612 : : /* Restore the previous state of the interface. */
10613 : 3 : interface = gfc_current_interface_head ();
10614 : 3 : gfc_set_current_interface_head (old_interface_head);
10615 : :
10616 : : /* Free the new interfaces. */
10617 : 10 : while (interface != old_interface_head)
10618 : : {
10619 : 4 : gfc_interface *i = interface->next;
10620 : 4 : free (interface);
10621 : 4 : interface = i;
10622 : : }
10623 : :
10624 : : /* And issue a syntax error. */
10625 : 3 : gfc_syntax_error (ST_MODULE_PROC);
10626 : 3 : return MATCH_ERROR;
10627 : : }
10628 : :
10629 : :
10630 : : /* Check a derived type that is being extended. */
10631 : :
10632 : : static gfc_symbol*
10633 : 1443 : check_extended_derived_type (char *name)
10634 : : {
10635 : 1443 : gfc_symbol *extended;
10636 : :
10637 : 1443 : if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
10638 : : {
10639 : 0 : gfc_error ("Ambiguous symbol in TYPE definition at %C");
10640 : 0 : return NULL;
10641 : : }
10642 : :
10643 : 1443 : extended = gfc_find_dt_in_generic (extended);
10644 : :
10645 : : /* F08:C428. */
10646 : 1443 : if (!extended)
10647 : : {
10648 : 2 : gfc_error ("Symbol %qs at %C has not been previously defined", name);
10649 : 2 : return NULL;
10650 : : }
10651 : :
10652 : 1441 : if (extended->attr.flavor != FL_DERIVED)
10653 : : {
10654 : 0 : gfc_error ("%qs in EXTENDS expression at %C is not a "
10655 : : "derived type", name);
10656 : 0 : return NULL;
10657 : : }
10658 : :
10659 : 1441 : if (extended->attr.is_bind_c)
10660 : : {
10661 : 1 : gfc_error ("%qs cannot be extended at %C because it "
10662 : : "is BIND(C)", extended->name);
10663 : 1 : return NULL;
10664 : : }
10665 : :
10666 : 1440 : if (extended->attr.sequence)
10667 : : {
10668 : 1 : gfc_error ("%qs cannot be extended at %C because it "
10669 : : "is a SEQUENCE type", extended->name);
10670 : 1 : return NULL;
10671 : : }
10672 : :
10673 : : return extended;
10674 : : }
10675 : :
10676 : :
10677 : : /* Match the optional attribute specifiers for a type declaration.
10678 : : Return MATCH_ERROR if an error is encountered in one of the handled
10679 : : attributes (public, private, bind(c)), MATCH_NO if what's found is
10680 : : not a handled attribute, and MATCH_YES otherwise. TODO: More error
10681 : : checking on attribute conflicts needs to be done. */
10682 : :
10683 : : static match
10684 : 18422 : gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
10685 : : {
10686 : : /* See if the derived type is marked as private. */
10687 : 18422 : if (gfc_match (" , private") == MATCH_YES)
10688 : : {
10689 : 15 : if (gfc_current_state () != COMP_MODULE)
10690 : : {
10691 : 1 : gfc_error ("Derived type at %C can only be PRIVATE in the "
10692 : : "specification part of a module");
10693 : 1 : return MATCH_ERROR;
10694 : : }
10695 : :
10696 : 14 : if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
10697 : : return MATCH_ERROR;
10698 : : }
10699 : 18407 : else if (gfc_match (" , public") == MATCH_YES)
10700 : : {
10701 : 545 : if (gfc_current_state () != COMP_MODULE)
10702 : : {
10703 : 0 : gfc_error ("Derived type at %C can only be PUBLIC in the "
10704 : : "specification part of a module");
10705 : 0 : return MATCH_ERROR;
10706 : : }
10707 : :
10708 : 545 : if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
10709 : : return MATCH_ERROR;
10710 : : }
10711 : 17862 : else if (gfc_match (" , bind ( c )") == MATCH_YES)
10712 : : {
10713 : : /* If the type is defined to be bind(c) it then needs to make
10714 : : sure that all fields are interoperable. This will
10715 : : need to be a semantic check on the finished derived type.
10716 : : See 15.2.3 (lines 9-12) of F2003 draft. */
10717 : 407 : if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
10718 : : return MATCH_ERROR;
10719 : :
10720 : : /* TODO: attr conflicts need to be checked, probably in symbol.cc. */
10721 : : }
10722 : 17455 : else if (gfc_match (" , abstract") == MATCH_YES)
10723 : : {
10724 : 320 : if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
10725 : : return MATCH_ERROR;
10726 : :
10727 : 319 : if (!gfc_add_abstract (attr, &gfc_current_locus))
10728 : : return MATCH_ERROR;
10729 : : }
10730 : 17135 : else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
10731 : : {
10732 : 1444 : if (!gfc_add_extension (attr, &gfc_current_locus))
10733 : : return MATCH_ERROR;
10734 : : }
10735 : : else
10736 : 15691 : return MATCH_NO;
10737 : :
10738 : : /* If we get here, something matched. */
10739 : : return MATCH_YES;
10740 : : }
10741 : :
10742 : :
10743 : : /* Common function for type declaration blocks similar to derived types, such
10744 : : as STRUCTURES and MAPs. Unlike derived types, a structure type
10745 : : does NOT have a generic symbol matching the name given by the user.
10746 : : STRUCTUREs can share names with variables and PARAMETERs so we must allow
10747 : : for the creation of an independent symbol.
10748 : : Other parameters are a message to prefix errors with, the name of the new
10749 : : type to be created, and the flavor to add to the resulting symbol. */
10750 : :
10751 : : static bool
10752 : 717 : get_struct_decl (const char *name, sym_flavor fl, locus *decl,
10753 : : gfc_symbol **result)
10754 : : {
10755 : 717 : gfc_symbol *sym;
10756 : 717 : locus where;
10757 : :
10758 : 717 : gcc_assert (name[0] == (char) TOUPPER (name[0]));
10759 : :
10760 : 717 : if (decl)
10761 : 717 : where = *decl;
10762 : : else
10763 : 0 : where = gfc_current_locus;
10764 : :
10765 : 717 : if (gfc_get_symbol (name, NULL, &sym))
10766 : : return false;
10767 : :
10768 : 717 : if (!sym)
10769 : : {
10770 : 0 : gfc_internal_error ("Failed to create structure type '%s' at %C", name);
10771 : : return false;
10772 : : }
10773 : :
10774 : 717 : if (sym->components != NULL || sym->attr.zero_comp)
10775 : : {
10776 : 3 : gfc_error ("Type definition of %qs at %C was already defined at %L",
10777 : : sym->name, &sym->declared_at);
10778 : 3 : return false;
10779 : : }
10780 : :
10781 : 714 : sym->declared_at = where;
10782 : :
10783 : 714 : if (sym->attr.flavor != fl
10784 : 714 : && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
10785 : : return false;
10786 : :
10787 : 714 : if (!sym->hash_value)
10788 : : /* Set the hash for the compound name for this type. */
10789 : 713 : sym->hash_value = gfc_hash_value (sym);
10790 : :
10791 : : /* Normally the type is expected to have been completely parsed by the time
10792 : : a field declaration with this type is seen. For unions, maps, and nested
10793 : : structure declarations, we need to indicate that it is okay that we
10794 : : haven't seen any components yet. This will be updated after the structure
10795 : : is fully parsed. */
10796 : 714 : sym->attr.zero_comp = 0;
10797 : :
10798 : : /* Structures always act like derived-types with the SEQUENCE attribute */
10799 : 714 : gfc_add_sequence (&sym->attr, sym->name, NULL);
10800 : :
10801 : 714 : if (result) *result = sym;
10802 : :
10803 : : return true;
10804 : : }
10805 : :
10806 : :
10807 : : /* Match the opening of a MAP block. Like a struct within a union in C;
10808 : : behaves identical to STRUCTURE blocks. */
10809 : :
10810 : : match
10811 : 259 : gfc_match_map (void)
10812 : : {
10813 : : /* Counter used to give unique internal names to map structures. */
10814 : 259 : static unsigned int gfc_map_id = 0;
10815 : 259 : char name[GFC_MAX_SYMBOL_LEN + 1];
10816 : 259 : gfc_symbol *sym;
10817 : 259 : locus old_loc;
10818 : :
10819 : 259 : old_loc = gfc_current_locus;
10820 : :
10821 : 259 : if (gfc_match_eos () != MATCH_YES)
10822 : : {
10823 : 1 : gfc_error ("Junk after MAP statement at %C");
10824 : 1 : gfc_current_locus = old_loc;
10825 : 1 : return MATCH_ERROR;
10826 : : }
10827 : :
10828 : : /* Map blocks are anonymous so we make up unique names for the symbol table
10829 : : which are invalid Fortran identifiers. */
10830 : 258 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
10831 : :
10832 : 258 : if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
10833 : : return MATCH_ERROR;
10834 : :
10835 : 258 : gfc_new_block = sym;
10836 : :
10837 : 258 : return MATCH_YES;
10838 : : }
10839 : :
10840 : :
10841 : : /* Match the opening of a UNION block. */
10842 : :
10843 : : match
10844 : 133 : gfc_match_union (void)
10845 : : {
10846 : : /* Counter used to give unique internal names to union types. */
10847 : 133 : static unsigned int gfc_union_id = 0;
10848 : 133 : char name[GFC_MAX_SYMBOL_LEN + 1];
10849 : 133 : gfc_symbol *sym;
10850 : 133 : locus old_loc;
10851 : :
10852 : 133 : old_loc = gfc_current_locus;
10853 : :
10854 : 133 : if (gfc_match_eos () != MATCH_YES)
10855 : : {
10856 : 1 : gfc_error ("Junk after UNION statement at %C");
10857 : 1 : gfc_current_locus = old_loc;
10858 : 1 : return MATCH_ERROR;
10859 : : }
10860 : :
10861 : : /* Unions are anonymous so we make up unique names for the symbol table
10862 : : which are invalid Fortran identifiers. */
10863 : 132 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
10864 : :
10865 : 132 : if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
10866 : : return MATCH_ERROR;
10867 : :
10868 : 132 : gfc_new_block = sym;
10869 : :
10870 : 132 : return MATCH_YES;
10871 : : }
10872 : :
10873 : :
10874 : : /* Match the beginning of a STRUCTURE declaration. This is similar to
10875 : : matching the beginning of a derived type declaration with a few
10876 : : twists. The resulting type symbol has no access control or other
10877 : : interesting attributes. */
10878 : :
10879 : : match
10880 : 336 : gfc_match_structure_decl (void)
10881 : : {
10882 : : /* Counter used to give unique internal names to anonymous structures. */
10883 : 336 : static unsigned int gfc_structure_id = 0;
10884 : 336 : char name[GFC_MAX_SYMBOL_LEN + 1];
10885 : 336 : gfc_symbol *sym;
10886 : 336 : match m;
10887 : 336 : locus where;
10888 : :
10889 : 336 : if (!flag_dec_structure)
10890 : : {
10891 : 3 : gfc_error ("%s at %C is a DEC extension, enable with "
10892 : : "%<-fdec-structure%>",
10893 : : "STRUCTURE");
10894 : 3 : return MATCH_ERROR;
10895 : : }
10896 : :
10897 : 333 : name[0] = '\0';
10898 : :
10899 : 333 : m = gfc_match (" /%n/", name);
10900 : 333 : if (m != MATCH_YES)
10901 : : {
10902 : : /* Non-nested structure declarations require a structure name. */
10903 : 24 : if (!gfc_comp_struct (gfc_current_state ()))
10904 : : {
10905 : 4 : gfc_error ("Structure name expected in non-nested structure "
10906 : : "declaration at %C");
10907 : 4 : return MATCH_ERROR;
10908 : : }
10909 : : /* This is an anonymous structure; make up a unique name for it
10910 : : (upper-case letters never make it to symbol names from the source).
10911 : : The important thing is initializing the type variable
10912 : : and setting gfc_new_symbol, which is immediately used by
10913 : : parse_structure () and variable_decl () to add components of
10914 : : this type. */
10915 : 20 : snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
10916 : : }
10917 : :
10918 : 329 : where = gfc_current_locus;
10919 : : /* No field list allowed after non-nested structure declaration. */
10920 : 329 : if (!gfc_comp_struct (gfc_current_state ())
10921 : 296 : && gfc_match_eos () != MATCH_YES)
10922 : : {
10923 : 1 : gfc_error ("Junk after non-nested STRUCTURE statement at %C");
10924 : 1 : return MATCH_ERROR;
10925 : : }
10926 : :
10927 : : /* Make sure the name is not the name of an intrinsic type. */
10928 : 328 : if (gfc_is_intrinsic_typename (name))
10929 : : {
10930 : 1 : gfc_error ("Structure name %qs at %C cannot be the same as an"
10931 : : " intrinsic type", name);
10932 : 1 : return MATCH_ERROR;
10933 : : }
10934 : :
10935 : : /* Store the actual type symbol for the structure with an upper-case first
10936 : : letter (an invalid Fortran identifier). */
10937 : :
10938 : 327 : if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
10939 : : return MATCH_ERROR;
10940 : :
10941 : 324 : gfc_new_block = sym;
10942 : 324 : return MATCH_YES;
10943 : : }
10944 : :
10945 : :
10946 : : /* This function does some work to determine which matcher should be used to
10947 : : * match a statement beginning with "TYPE". This is used to disambiguate TYPE
10948 : : * as an alias for PRINT from derived type declarations, TYPE IS statements,
10949 : : * and [parameterized] derived type declarations. */
10950 : :
10951 : : match
10952 : 510856 : gfc_match_type (gfc_statement *st)
10953 : : {
10954 : 510856 : char name[GFC_MAX_SYMBOL_LEN + 1];
10955 : 510856 : match m;
10956 : 510856 : locus old_loc;
10957 : :
10958 : : /* Requires -fdec. */
10959 : 510856 : if (!flag_dec)
10960 : : return MATCH_NO;
10961 : :
10962 : 2483 : m = gfc_match ("type");
10963 : 2483 : if (m != MATCH_YES)
10964 : : return m;
10965 : : /* If we already have an error in the buffer, it is probably from failing to
10966 : : * match a derived type data declaration. Let it happen. */
10967 : 20 : else if (gfc_error_flag_test ())
10968 : : return MATCH_NO;
10969 : :
10970 : 20 : old_loc = gfc_current_locus;
10971 : 20 : *st = ST_NONE;
10972 : :
10973 : : /* If we see an attribute list before anything else it's definitely a derived
10974 : : * type declaration. */
10975 : 20 : if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
10976 : 8 : goto derived;
10977 : :
10978 : : /* By now "TYPE" has already been matched. If we do not see a name, this may
10979 : : * be something like "TYPE *" or "TYPE <fmt>". */
10980 : 12 : m = gfc_match_name (name);
10981 : 12 : if (m != MATCH_YES)
10982 : : {
10983 : : /* Let print match if it can, otherwise throw an error from
10984 : : * gfc_match_derived_decl. */
10985 : 7 : gfc_current_locus = old_loc;
10986 : 7 : if (gfc_match_print () == MATCH_YES)
10987 : : {
10988 : 7 : *st = ST_WRITE;
10989 : 7 : return MATCH_YES;
10990 : : }
10991 : 0 : goto derived;
10992 : : }
10993 : :
10994 : : /* Check for EOS. */
10995 : 5 : if (gfc_match_eos () == MATCH_YES)
10996 : : {
10997 : : /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10998 : : * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10999 : : * Otherwise if gfc_match_derived_decl fails it's probably an existing
11000 : : * symbol which can be printed. */
11001 : 3 : gfc_current_locus = old_loc;
11002 : 3 : m = gfc_match_derived_decl ();
11003 : 3 : if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
11004 : : {
11005 : 2 : *st = ST_DERIVED_DECL;
11006 : 2 : return m;
11007 : : }
11008 : : }
11009 : : else
11010 : : {
11011 : : /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
11012 : : like <type name(parameter)>. */
11013 : 2 : gfc_gobble_whitespace ();
11014 : 2 : bool paren = gfc_peek_ascii_char () == '(';
11015 : 2 : if (paren)
11016 : : {
11017 : 1 : if (strcmp ("is", name) == 0)
11018 : 1 : goto typeis;
11019 : : else
11020 : 0 : goto derived;
11021 : : }
11022 : : }
11023 : :
11024 : : /* Treat TYPE... like PRINT... */
11025 : 2 : gfc_current_locus = old_loc;
11026 : 2 : *st = ST_WRITE;
11027 : 2 : return gfc_match_print ();
11028 : :
11029 : 8 : derived:
11030 : 8 : gfc_current_locus = old_loc;
11031 : 8 : *st = ST_DERIVED_DECL;
11032 : 8 : return gfc_match_derived_decl ();
11033 : :
11034 : 1 : typeis:
11035 : 1 : gfc_current_locus = old_loc;
11036 : 1 : *st = ST_TYPE_IS;
11037 : 1 : return gfc_match_type_is ();
11038 : : }
11039 : :
11040 : :
11041 : : /* Match the beginning of a derived type declaration. If a type name
11042 : : was the result of a function, then it is possible to have a symbol
11043 : : already to be known as a derived type yet have no components. */
11044 : :
11045 : : match
11046 : 15698 : gfc_match_derived_decl (void)
11047 : : {
11048 : 15698 : char name[GFC_MAX_SYMBOL_LEN + 1];
11049 : 15698 : char parent[GFC_MAX_SYMBOL_LEN + 1];
11050 : 15698 : symbol_attribute attr;
11051 : 15698 : gfc_symbol *sym, *gensym;
11052 : 15698 : gfc_symbol *extended;
11053 : 15698 : match m;
11054 : 15698 : match is_type_attr_spec = MATCH_NO;
11055 : 15698 : bool seen_attr = false;
11056 : 15698 : gfc_interface *intr = NULL, *head;
11057 : 15698 : bool parameterized_type = false;
11058 : 15698 : bool seen_colons = false;
11059 : :
11060 : 15698 : if (gfc_comp_struct (gfc_current_state ()))
11061 : : return MATCH_NO;
11062 : :
11063 : 15694 : name[0] = '\0';
11064 : 15694 : parent[0] = '\0';
11065 : 15694 : gfc_clear_attr (&attr);
11066 : 15694 : extended = NULL;
11067 : :
11068 : 18422 : do
11069 : : {
11070 : 18422 : is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
11071 : 18422 : if (is_type_attr_spec == MATCH_ERROR)
11072 : : return MATCH_ERROR;
11073 : 18419 : if (is_type_attr_spec == MATCH_YES)
11074 : 2728 : seen_attr = true;
11075 : 18419 : } while (is_type_attr_spec == MATCH_YES);
11076 : :
11077 : : /* Deal with derived type extensions. The extension attribute has
11078 : : been added to 'attr' but now the parent type must be found and
11079 : : checked. */
11080 : 15691 : if (parent[0])
11081 : 1443 : extended = check_extended_derived_type (parent);
11082 : :
11083 : 15691 : if (parent[0] && !extended)
11084 : : return MATCH_ERROR;
11085 : :
11086 : 15687 : m = gfc_match (" ::");
11087 : 15687 : if (m == MATCH_YES)
11088 : : {
11089 : : seen_colons = true;
11090 : : }
11091 : 9876 : else if (seen_attr)
11092 : : {
11093 : 5 : gfc_error ("Expected :: in TYPE definition at %C");
11094 : 5 : return MATCH_ERROR;
11095 : : }
11096 : :
11097 : : /* In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
11098 : : But, we need to simply return for TYPE(. */
11099 : 9871 : if (m == MATCH_NO && gfc_current_form == FORM_FREE)
11100 : : {
11101 : 9823 : char c = gfc_peek_ascii_char ();
11102 : 9823 : if (c == '(')
11103 : : return m;
11104 : 9759 : if (!gfc_is_whitespace (c))
11105 : : {
11106 : 4 : gfc_error ("Mangled derived type definition at %C");
11107 : 4 : return MATCH_NO;
11108 : : }
11109 : : }
11110 : :
11111 : 15614 : m = gfc_match (" %n ", name);
11112 : 15614 : if (m != MATCH_YES)
11113 : : return m;
11114 : :
11115 : : /* Make sure that we don't identify TYPE IS (...) as a parameterized
11116 : : derived type named 'is'.
11117 : : TODO Expand the check, when 'name' = "is" by matching " (tname) "
11118 : : and checking if this is a(n intrinsic) typename. This picks up
11119 : : misplaced TYPE IS statements such as in select_type_1.f03. */
11120 : 15603 : if (gfc_peek_ascii_char () == '(')
11121 : : {
11122 : 3691 : if (gfc_current_state () == COMP_SELECT_TYPE
11123 : 333 : || (!seen_colons && !strcmp (name, "is")))
11124 : : return MATCH_NO;
11125 : : parameterized_type = true;
11126 : : }
11127 : :
11128 : 12243 : m = gfc_match_eos ();
11129 : 12243 : if (m != MATCH_YES && !parameterized_type)
11130 : : return m;
11131 : :
11132 : : /* Make sure the name is not the name of an intrinsic type. */
11133 : 12240 : if (gfc_is_intrinsic_typename (name))
11134 : : {
11135 : 18 : gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
11136 : : "type", name);
11137 : 18 : return MATCH_ERROR;
11138 : : }
11139 : :
11140 : 12222 : if (gfc_get_symbol (name, NULL, &gensym))
11141 : : return MATCH_ERROR;
11142 : :
11143 : 12222 : if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
11144 : : {
11145 : 5 : if (gensym->ts.u.derived)
11146 : 0 : gfc_error ("Derived type name %qs at %C already has a basic type "
11147 : : "of %s", gensym->name, gfc_typename (&gensym->ts));
11148 : : else
11149 : 5 : gfc_error ("Derived type name %qs at %C already has a basic type",
11150 : : gensym->name);
11151 : 5 : return MATCH_ERROR;
11152 : : }
11153 : :
11154 : 12217 : if (!gensym->attr.generic
11155 : 12217 : && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
11156 : : return MATCH_ERROR;
11157 : :
11158 : 12213 : if (!gensym->attr.function
11159 : 12213 : && !gfc_add_function (&gensym->attr, gensym->name, NULL))
11160 : : return MATCH_ERROR;
11161 : :
11162 : 12212 : if (gensym->attr.dummy)
11163 : : {
11164 : 1 : gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
11165 : : name, &gensym->declared_at);
11166 : 1 : return MATCH_ERROR;
11167 : : }
11168 : :
11169 : 12211 : sym = gfc_find_dt_in_generic (gensym);
11170 : :
11171 : 12211 : if (sym && (sym->components != NULL || sym->attr.zero_comp))
11172 : : {
11173 : 1 : gfc_error ("Derived type definition of %qs at %C has already been "
11174 : : "defined", sym->name);
11175 : 1 : return MATCH_ERROR;
11176 : : }
11177 : :
11178 : 12210 : if (!sym)
11179 : : {
11180 : : /* Use upper case to save the actual derived-type symbol. */
11181 : 12125 : gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
11182 : 12125 : sym->name = gfc_get_string ("%s", gensym->name);
11183 : 12125 : head = gensym->generic;
11184 : 12125 : intr = gfc_get_interface ();
11185 : 12125 : intr->sym = sym;
11186 : 12125 : intr->where = gfc_current_locus;
11187 : 12125 : intr->sym->declared_at = gfc_current_locus;
11188 : 12125 : intr->next = head;
11189 : 12125 : gensym->generic = intr;
11190 : 12125 : gensym->attr.if_source = IFSRC_DECL;
11191 : : }
11192 : :
11193 : : /* The symbol may already have the derived attribute without the
11194 : : components. The ways this can happen is via a function
11195 : : definition, an INTRINSIC statement or a subtype in another
11196 : : derived type that is a pointer. The first part of the AND clause
11197 : : is true if the symbol is not the return value of a function. */
11198 : 12210 : if (sym->attr.flavor != FL_DERIVED
11199 : 12210 : && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
11200 : : return MATCH_ERROR;
11201 : :
11202 : 12210 : if (attr.access != ACCESS_UNKNOWN
11203 : 12210 : && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
11204 : : return MATCH_ERROR;
11205 : 12210 : else if (sym->attr.access == ACCESS_UNKNOWN
11206 : 11655 : && gensym->attr.access != ACCESS_UNKNOWN
11207 : 12524 : && !gfc_add_access (&sym->attr, gensym->attr.access,
11208 : : sym->name, NULL))
11209 : : return MATCH_ERROR;
11210 : :
11211 : 12210 : if (sym->attr.access != ACCESS_UNKNOWN
11212 : 869 : && gensym->attr.access == ACCESS_UNKNOWN)
11213 : 555 : gensym->attr.access = sym->attr.access;
11214 : :
11215 : : /* See if the derived type was labeled as bind(c). */
11216 : 12210 : if (attr.is_bind_c != 0)
11217 : 404 : sym->attr.is_bind_c = attr.is_bind_c;
11218 : :
11219 : : /* Construct the f2k_derived namespace if it is not yet there. */
11220 : 12210 : if (!sym->f2k_derived)
11221 : 12210 : sym->f2k_derived = gfc_get_namespace (NULL, 0);
11222 : :
11223 : 12210 : if (parameterized_type)
11224 : : {
11225 : : /* Ignore error or mismatches by going to the end of the statement
11226 : : in order to avoid the component declarations causing problems. */
11227 : 331 : m = gfc_match_formal_arglist (sym, 0, 0, true);
11228 : 331 : if (m != MATCH_YES)
11229 : 4 : gfc_error_recovery ();
11230 : : else
11231 : 327 : sym->attr.pdt_template = 1;
11232 : 331 : m = gfc_match_eos ();
11233 : 331 : if (m != MATCH_YES)
11234 : : {
11235 : 1 : gfc_error_recovery ();
11236 : 1 : gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
11237 : : }
11238 : : }
11239 : :
11240 : 12210 : if (extended && !sym->components)
11241 : : {
11242 : 1439 : gfc_component *p;
11243 : 1439 : gfc_formal_arglist *f, *g, *h;
11244 : :
11245 : : /* Add the extended derived type as the first component. */
11246 : 1439 : gfc_add_component (sym, parent, &p);
11247 : 1439 : extended->refs++;
11248 : 1439 : gfc_set_sym_referenced (extended);
11249 : :
11250 : 1439 : p->ts.type = BT_DERIVED;
11251 : 1439 : p->ts.u.derived = extended;
11252 : 1439 : p->initializer = gfc_default_initializer (&p->ts);
11253 : :
11254 : : /* Set extension level. */
11255 : 1439 : if (extended->attr.extension == 255)
11256 : : {
11257 : : /* Since the extension field is 8 bit wide, we can only have
11258 : : up to 255 extension levels. */
11259 : 0 : gfc_error ("Maximum extension level reached with type %qs at %L",
11260 : : extended->name, &extended->declared_at);
11261 : 0 : return MATCH_ERROR;
11262 : : }
11263 : 1439 : sym->attr.extension = extended->attr.extension + 1;
11264 : :
11265 : : /* Provide the links between the extended type and its extension. */
11266 : 1439 : if (!extended->f2k_derived)
11267 : 1 : extended->f2k_derived = gfc_get_namespace (NULL, 0);
11268 : :
11269 : : /* Copy the extended type-param-name-list from the extended type,
11270 : : append those of the extension and add the whole lot to the
11271 : : extension. */
11272 : 1439 : if (extended->attr.pdt_template)
11273 : : {
11274 : 28 : g = h = NULL;
11275 : 28 : sym->attr.pdt_template = 1;
11276 : 87 : for (f = extended->formal; f; f = f->next)
11277 : : {
11278 : 59 : if (f == extended->formal)
11279 : : {
11280 : 28 : g = gfc_get_formal_arglist ();
11281 : 28 : h = g;
11282 : : }
11283 : : else
11284 : : {
11285 : 31 : g->next = gfc_get_formal_arglist ();
11286 : 31 : g = g->next;
11287 : : }
11288 : 59 : g->sym = f->sym;
11289 : : }
11290 : 28 : g->next = sym->formal;
11291 : 28 : sym->formal = h;
11292 : : }
11293 : : }
11294 : :
11295 : 12210 : if (!sym->hash_value)
11296 : : /* Set the hash for the compound name for this type. */
11297 : 12210 : sym->hash_value = gfc_hash_value (sym);
11298 : :
11299 : : /* Take over the ABSTRACT attribute. */
11300 : 12210 : sym->attr.abstract = attr.abstract;
11301 : :
11302 : 12210 : gfc_new_block = sym;
11303 : :
11304 : 12210 : return MATCH_YES;
11305 : : }
11306 : :
11307 : :
11308 : : /* Cray Pointees can be declared as:
11309 : : pointer (ipt, a (n,m,...,*)) */
11310 : :
11311 : : match
11312 : 240 : gfc_mod_pointee_as (gfc_array_spec *as)
11313 : : {
11314 : 240 : as->cray_pointee = true; /* This will be useful to know later. */
11315 : 240 : if (as->type == AS_ASSUMED_SIZE)
11316 : 72 : as->cp_was_assumed = true;
11317 : 168 : else if (as->type == AS_ASSUMED_SHAPE)
11318 : : {
11319 : 0 : gfc_error ("Cray Pointee at %C cannot be assumed shape array");
11320 : 0 : return MATCH_ERROR;
11321 : : }
11322 : : return MATCH_YES;
11323 : : }
11324 : :
11325 : :
11326 : : /* Match the enum definition statement, here we are trying to match
11327 : : the first line of enum definition statement.
11328 : : Returns MATCH_YES if match is found. */
11329 : :
11330 : : match
11331 : 158 : gfc_match_enum (void)
11332 : : {
11333 : 158 : match m;
11334 : :
11335 : 158 : m = gfc_match_eos ();
11336 : 158 : if (m != MATCH_YES)
11337 : : return m;
11338 : :
11339 : 158 : if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
11340 : 0 : return MATCH_ERROR;
11341 : :
11342 : : return MATCH_YES;
11343 : : }
11344 : :
11345 : :
11346 : : /* Returns an initializer whose value is one higher than the value of the
11347 : : LAST_INITIALIZER argument. If the argument is NULL, the
11348 : : initializers value will be set to zero. The initializer's kind
11349 : : will be set to gfc_c_int_kind.
11350 : :
11351 : : If -fshort-enums is given, the appropriate kind will be selected
11352 : : later after all enumerators have been parsed. A warning is issued
11353 : : here if an initializer exceeds gfc_c_int_kind. */
11354 : :
11355 : : static gfc_expr *
11356 : 377 : enum_initializer (gfc_expr *last_initializer, locus where)
11357 : : {
11358 : 377 : gfc_expr *result;
11359 : 377 : result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
11360 : :
11361 : 377 : mpz_init (result->value.integer);
11362 : :
11363 : 377 : if (last_initializer != NULL)
11364 : : {
11365 : 266 : mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
11366 : 266 : result->where = last_initializer->where;
11367 : :
11368 : 266 : if (gfc_check_integer_range (result->value.integer,
11369 : : gfc_c_int_kind) != ARITH_OK)
11370 : : {
11371 : 0 : gfc_error ("Enumerator exceeds the C integer type at %C");
11372 : 0 : return NULL;
11373 : : }
11374 : : }
11375 : : else
11376 : : {
11377 : : /* Control comes here, if it's the very first enumerator and no
11378 : : initializer has been given. It will be initialized to zero. */
11379 : 111 : mpz_set_si (result->value.integer, 0);
11380 : : }
11381 : :
11382 : : return result;
11383 : : }
11384 : :
11385 : :
11386 : : /* Match a variable name with an optional initializer. When this
11387 : : subroutine is called, a variable is expected to be parsed next.
11388 : : Depending on what is happening at the moment, updates either the
11389 : : symbol table or the current interface. */
11390 : :
11391 : : static match
11392 : 549 : enumerator_decl (void)
11393 : : {
11394 : 549 : char name[GFC_MAX_SYMBOL_LEN + 1];
11395 : 549 : gfc_expr *initializer;
11396 : 549 : gfc_array_spec *as = NULL;
11397 : 549 : gfc_symbol *sym;
11398 : 549 : locus var_locus;
11399 : 549 : match m;
11400 : 549 : bool t;
11401 : 549 : locus old_locus;
11402 : :
11403 : 549 : initializer = NULL;
11404 : 549 : old_locus = gfc_current_locus;
11405 : :
11406 : : /* When we get here, we've just matched a list of attributes and
11407 : : maybe a type and a double colon. The next thing we expect to see
11408 : : is the name of the symbol. */
11409 : 549 : m = gfc_match_name (name);
11410 : 549 : if (m != MATCH_YES)
11411 : 1 : goto cleanup;
11412 : :
11413 : 548 : var_locus = gfc_current_locus;
11414 : :
11415 : : /* OK, we've successfully matched the declaration. Now put the
11416 : : symbol in the current namespace. If we fail to create the symbol,
11417 : : bail out. */
11418 : 548 : if (!build_sym (name, 1, NULL, false, &as, &var_locus))
11419 : : {
11420 : 1 : m = MATCH_ERROR;
11421 : 1 : goto cleanup;
11422 : : }
11423 : :
11424 : : /* The double colon must be present in order to have initializers.
11425 : : Otherwise the statement is ambiguous with an assignment statement. */
11426 : 547 : if (colon_seen)
11427 : : {
11428 : 471 : if (gfc_match_char ('=') == MATCH_YES)
11429 : : {
11430 : 170 : m = gfc_match_init_expr (&initializer);
11431 : 170 : if (m == MATCH_NO)
11432 : : {
11433 : 0 : gfc_error ("Expected an initialization expression at %C");
11434 : 0 : m = MATCH_ERROR;
11435 : : }
11436 : :
11437 : 170 : if (m != MATCH_YES)
11438 : 2 : goto cleanup;
11439 : : }
11440 : : }
11441 : :
11442 : : /* If we do not have an initializer, the initialization value of the
11443 : : previous enumerator (stored in last_initializer) is incremented
11444 : : by 1 and is used to initialize the current enumerator. */
11445 : 545 : if (initializer == NULL)
11446 : 377 : initializer = enum_initializer (last_initializer, old_locus);
11447 : :
11448 : 545 : if (initializer == NULL || initializer->ts.type != BT_INTEGER)
11449 : : {
11450 : 2 : gfc_error ("ENUMERATOR %L not initialized with integer expression",
11451 : : &var_locus);
11452 : 2 : m = MATCH_ERROR;
11453 : 2 : goto cleanup;
11454 : : }
11455 : :
11456 : : /* Store this current initializer, for the next enumerator variable
11457 : : to be parsed. add_init_expr_to_sym() zeros initializer, so we
11458 : : use last_initializer below. */
11459 : 543 : last_initializer = initializer;
11460 : 543 : t = add_init_expr_to_sym (name, &initializer, &var_locus);
11461 : :
11462 : : /* Maintain enumerator history. */
11463 : 543 : gfc_find_symbol (name, NULL, 0, &sym);
11464 : 543 : create_enum_history (sym, last_initializer);
11465 : :
11466 : 543 : return (t) ? MATCH_YES : MATCH_ERROR;
11467 : :
11468 : 6 : cleanup:
11469 : : /* Free stuff up and return. */
11470 : 6 : gfc_free_expr (initializer);
11471 : :
11472 : 6 : return m;
11473 : : }
11474 : :
11475 : :
11476 : : /* Match the enumerator definition statement. */
11477 : :
11478 : : match
11479 : 783409 : gfc_match_enumerator_def (void)
11480 : : {
11481 : 783409 : match m;
11482 : 783409 : bool t;
11483 : :
11484 : 783409 : gfc_clear_ts (¤t_ts);
11485 : :
11486 : 783409 : m = gfc_match (" enumerator");
11487 : 783409 : if (m != MATCH_YES)
11488 : : return m;
11489 : :
11490 : 269 : m = gfc_match (" :: ");
11491 : 269 : if (m == MATCH_ERROR)
11492 : : return m;
11493 : :
11494 : 269 : colon_seen = (m == MATCH_YES);
11495 : :
11496 : 269 : if (gfc_current_state () != COMP_ENUM)
11497 : : {
11498 : 4 : gfc_error ("ENUM definition statement expected before %C");
11499 : 4 : gfc_free_enum_history ();
11500 : 4 : return MATCH_ERROR;
11501 : : }
11502 : :
11503 : 265 : (¤t_ts)->type = BT_INTEGER;
11504 : 265 : (¤t_ts)->kind = gfc_c_int_kind;
11505 : :
11506 : 265 : gfc_clear_attr (¤t_attr);
11507 : 265 : t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
11508 : 265 : if (!t)
11509 : : {
11510 : 0 : m = MATCH_ERROR;
11511 : 0 : goto cleanup;
11512 : : }
11513 : :
11514 : 549 : for (;;)
11515 : : {
11516 : 549 : m = enumerator_decl ();
11517 : 549 : if (m == MATCH_ERROR)
11518 : : {
11519 : 6 : gfc_free_enum_history ();
11520 : 6 : goto cleanup;
11521 : : }
11522 : 543 : if (m == MATCH_NO)
11523 : : break;
11524 : :
11525 : 542 : if (gfc_match_eos () == MATCH_YES)
11526 : 256 : goto cleanup;
11527 : 286 : if (gfc_match_char (',') != MATCH_YES)
11528 : : break;
11529 : : }
11530 : :
11531 : 3 : if (gfc_current_state () == COMP_ENUM)
11532 : : {
11533 : 3 : gfc_free_enum_history ();
11534 : 3 : gfc_error ("Syntax error in ENUMERATOR definition at %C");
11535 : 3 : m = MATCH_ERROR;
11536 : : }
11537 : :
11538 : 0 : cleanup:
11539 : 265 : gfc_free_array_spec (current_as);
11540 : 265 : current_as = NULL;
11541 : 265 : return m;
11542 : :
11543 : : }
11544 : :
11545 : :
11546 : : /* Match binding attributes. */
11547 : :
11548 : : static match
11549 : 4499 : match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
11550 : : {
11551 : 4499 : bool found_passing = false;
11552 : 4499 : bool seen_ptr = false;
11553 : 4499 : match m = MATCH_YES;
11554 : :
11555 : : /* Initialize to defaults. Do so even before the MATCH_NO check so that in
11556 : : this case the defaults are in there. */
11557 : 4499 : ba->access = ACCESS_UNKNOWN;
11558 : 4499 : ba->pass_arg = NULL;
11559 : 4499 : ba->pass_arg_num = 0;
11560 : 4499 : ba->nopass = 0;
11561 : 4499 : ba->non_overridable = 0;
11562 : 4499 : ba->deferred = 0;
11563 : 4499 : ba->ppc = ppc;
11564 : :
11565 : : /* If we find a comma, we believe there are binding attributes. */
11566 : 4499 : m = gfc_match_char (',');
11567 : 4499 : if (m == MATCH_NO)
11568 : 2292 : goto done;
11569 : :
11570 : 2747 : do
11571 : : {
11572 : : /* Access specifier. */
11573 : :
11574 : 2747 : m = gfc_match (" public");
11575 : 2747 : if (m == MATCH_ERROR)
11576 : 0 : goto error;
11577 : 2747 : if (m == MATCH_YES)
11578 : : {
11579 : 250 : if (ba->access != ACCESS_UNKNOWN)
11580 : : {
11581 : 0 : gfc_error ("Duplicate access-specifier at %C");
11582 : 0 : goto error;
11583 : : }
11584 : :
11585 : 250 : ba->access = ACCESS_PUBLIC;
11586 : 250 : continue;
11587 : : }
11588 : :
11589 : 2497 : m = gfc_match (" private");
11590 : 2497 : if (m == MATCH_ERROR)
11591 : 0 : goto error;
11592 : 2497 : if (m == MATCH_YES)
11593 : : {
11594 : 163 : if (ba->access != ACCESS_UNKNOWN)
11595 : : {
11596 : 1 : gfc_error ("Duplicate access-specifier at %C");
11597 : 1 : goto error;
11598 : : }
11599 : :
11600 : 162 : ba->access = ACCESS_PRIVATE;
11601 : 162 : continue;
11602 : : }
11603 : :
11604 : : /* If inside GENERIC, the following is not allowed. */
11605 : 2334 : if (!generic)
11606 : : {
11607 : :
11608 : : /* NOPASS flag. */
11609 : 2333 : m = gfc_match (" nopass");
11610 : 2333 : if (m == MATCH_ERROR)
11611 : 0 : goto error;
11612 : 2333 : if (m == MATCH_YES)
11613 : : {
11614 : 700 : if (found_passing)
11615 : : {
11616 : 1 : gfc_error ("Binding attributes already specify passing,"
11617 : : " illegal NOPASS at %C");
11618 : 1 : goto error;
11619 : : }
11620 : :
11621 : 699 : found_passing = true;
11622 : 699 : ba->nopass = 1;
11623 : 699 : continue;
11624 : : }
11625 : :
11626 : : /* PASS possibly including argument. */
11627 : 1633 : m = gfc_match (" pass");
11628 : 1633 : if (m == MATCH_ERROR)
11629 : 0 : goto error;
11630 : 1633 : if (m == MATCH_YES)
11631 : : {
11632 : 890 : char arg[GFC_MAX_SYMBOL_LEN + 1];
11633 : :
11634 : 890 : if (found_passing)
11635 : : {
11636 : 2 : gfc_error ("Binding attributes already specify passing,"
11637 : : " illegal PASS at %C");
11638 : 2 : goto error;
11639 : : }
11640 : :
11641 : 888 : m = gfc_match (" ( %n )", arg);
11642 : 888 : if (m == MATCH_ERROR)
11643 : 0 : goto error;
11644 : 888 : if (m == MATCH_YES)
11645 : 480 : ba->pass_arg = gfc_get_string ("%s", arg);
11646 : 888 : gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
11647 : :
11648 : 888 : found_passing = true;
11649 : 888 : ba->nopass = 0;
11650 : 888 : continue;
11651 : 888 : }
11652 : :
11653 : 743 : if (ppc)
11654 : : {
11655 : : /* POINTER flag. */
11656 : 423 : m = gfc_match (" pointer");
11657 : 423 : if (m == MATCH_ERROR)
11658 : 0 : goto error;
11659 : 423 : if (m == MATCH_YES)
11660 : : {
11661 : 423 : if (seen_ptr)
11662 : : {
11663 : 1 : gfc_error ("Duplicate POINTER attribute at %C");
11664 : 1 : goto error;
11665 : : }
11666 : :
11667 : 422 : seen_ptr = true;
11668 : 422 : continue;
11669 : : }
11670 : : }
11671 : : else
11672 : : {
11673 : : /* NON_OVERRIDABLE flag. */
11674 : 320 : m = gfc_match (" non_overridable");
11675 : 320 : if (m == MATCH_ERROR)
11676 : 0 : goto error;
11677 : 320 : if (m == MATCH_YES)
11678 : : {
11679 : 62 : if (ba->non_overridable)
11680 : : {
11681 : 1 : gfc_error ("Duplicate NON_OVERRIDABLE at %C");
11682 : 1 : goto error;
11683 : : }
11684 : :
11685 : 61 : ba->non_overridable = 1;
11686 : 61 : continue;
11687 : : }
11688 : :
11689 : : /* DEFERRED flag. */
11690 : 258 : m = gfc_match (" deferred");
11691 : 258 : if (m == MATCH_ERROR)
11692 : 0 : goto error;
11693 : 258 : if (m == MATCH_YES)
11694 : : {
11695 : 258 : if (ba->deferred)
11696 : : {
11697 : 1 : gfc_error ("Duplicate DEFERRED at %C");
11698 : 1 : goto error;
11699 : : }
11700 : :
11701 : 257 : ba->deferred = 1;
11702 : 257 : continue;
11703 : : }
11704 : : }
11705 : :
11706 : : }
11707 : :
11708 : : /* Nothing matching found. */
11709 : 1 : if (generic)
11710 : 1 : gfc_error ("Expected access-specifier at %C");
11711 : : else
11712 : 0 : gfc_error ("Expected binding attribute at %C");
11713 : 1 : goto error;
11714 : : }
11715 : 2739 : while (gfc_match_char (',') == MATCH_YES);
11716 : :
11717 : : /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
11718 : 2199 : if (ba->non_overridable && ba->deferred)
11719 : : {
11720 : 1 : gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
11721 : 1 : goto error;
11722 : : }
11723 : :
11724 : : m = MATCH_YES;
11725 : :
11726 : 4490 : done:
11727 : 4490 : if (ba->access == ACCESS_UNKNOWN)
11728 : 4079 : ba->access = ppc ? gfc_current_block()->component_access
11729 : : : gfc_typebound_default_access;
11730 : :
11731 : 4490 : if (ppc && !seen_ptr)
11732 : : {
11733 : 2 : gfc_error ("POINTER attribute is required for procedure pointer component"
11734 : : " at %C");
11735 : 2 : goto error;
11736 : : }
11737 : :
11738 : : return m;
11739 : :
11740 : : error:
11741 : : return MATCH_ERROR;
11742 : : }
11743 : :
11744 : :
11745 : : /* Match a PROCEDURE specific binding inside a derived type. */
11746 : :
11747 : : static match
11748 : 3094 : match_procedure_in_type (void)
11749 : : {
11750 : 3094 : char name[GFC_MAX_SYMBOL_LEN + 1];
11751 : 3094 : char target_buf[GFC_MAX_SYMBOL_LEN + 1];
11752 : 3094 : char* target = NULL, *ifc = NULL;
11753 : 3094 : gfc_typebound_proc tb;
11754 : 3094 : bool seen_colons;
11755 : 3094 : bool seen_attrs;
11756 : 3094 : match m;
11757 : 3094 : gfc_symtree* stree;
11758 : 3094 : gfc_namespace* ns;
11759 : 3094 : gfc_symbol* block;
11760 : 3094 : int num;
11761 : :
11762 : : /* Check current state. */
11763 : 3094 : gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
11764 : 3094 : block = gfc_state_stack->previous->sym;
11765 : 3094 : gcc_assert (block);
11766 : :
11767 : : /* Try to match PROCEDURE(interface). */
11768 : 3094 : if (gfc_match (" (") == MATCH_YES)
11769 : : {
11770 : 259 : m = gfc_match_name (target_buf);
11771 : 259 : if (m == MATCH_ERROR)
11772 : : return m;
11773 : 259 : if (m != MATCH_YES)
11774 : : {
11775 : 1 : gfc_error ("Interface-name expected after %<(%> at %C");
11776 : 1 : return MATCH_ERROR;
11777 : : }
11778 : :
11779 : 258 : if (gfc_match (" )") != MATCH_YES)
11780 : : {
11781 : 1 : gfc_error ("%<)%> expected at %C");
11782 : 1 : return MATCH_ERROR;
11783 : : }
11784 : :
11785 : : ifc = target_buf;
11786 : : }
11787 : :
11788 : : /* Construct the data structure. */
11789 : 3092 : memset (&tb, 0, sizeof (tb));
11790 : 3092 : tb.where = gfc_current_locus;
11791 : :
11792 : : /* Match binding attributes. */
11793 : 3092 : m = match_binding_attributes (&tb, false, false);
11794 : 3092 : if (m == MATCH_ERROR)
11795 : : return m;
11796 : 3085 : seen_attrs = (m == MATCH_YES);
11797 : :
11798 : : /* Check that attribute DEFERRED is given if an interface is specified. */
11799 : 3085 : if (tb.deferred && !ifc)
11800 : : {
11801 : 1 : gfc_error ("Interface must be specified for DEFERRED binding at %C");
11802 : 1 : return MATCH_ERROR;
11803 : : }
11804 : 3084 : if (ifc && !tb.deferred)
11805 : : {
11806 : 1 : gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
11807 : 1 : return MATCH_ERROR;
11808 : : }
11809 : :
11810 : : /* Match the colons. */
11811 : 3083 : m = gfc_match (" ::");
11812 : 3083 : if (m == MATCH_ERROR)
11813 : : return m;
11814 : 3083 : seen_colons = (m == MATCH_YES);
11815 : 3083 : if (seen_attrs && !seen_colons)
11816 : : {
11817 : 4 : gfc_error ("Expected %<::%> after binding-attributes at %C");
11818 : 4 : return MATCH_ERROR;
11819 : : }
11820 : :
11821 : : /* Match the binding names. */
11822 : 19 : for(num=1;;num++)
11823 : : {
11824 : 3098 : m = gfc_match_name (name);
11825 : 3098 : if (m == MATCH_ERROR)
11826 : : return m;
11827 : 3098 : if (m == MATCH_NO)
11828 : : {
11829 : 5 : gfc_error ("Expected binding name at %C");
11830 : 5 : return MATCH_ERROR;
11831 : : }
11832 : :
11833 : 3093 : if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
11834 : : return MATCH_ERROR;
11835 : :
11836 : : /* Try to match the '=> target', if it's there. */
11837 : 3092 : target = ifc;
11838 : 3092 : m = gfc_match (" =>");
11839 : 3092 : if (m == MATCH_ERROR)
11840 : : return m;
11841 : 3092 : if (m == MATCH_YES)
11842 : : {
11843 : 1231 : if (tb.deferred)
11844 : : {
11845 : 1 : gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
11846 : 1 : return MATCH_ERROR;
11847 : : }
11848 : :
11849 : 1230 : if (!seen_colons)
11850 : : {
11851 : 1 : gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
11852 : : " at %C");
11853 : 1 : return MATCH_ERROR;
11854 : : }
11855 : :
11856 : 1229 : m = gfc_match_name (target_buf);
11857 : 1229 : if (m == MATCH_ERROR)
11858 : : return m;
11859 : 1229 : if (m == MATCH_NO)
11860 : : {
11861 : 2 : gfc_error ("Expected binding target after %<=>%> at %C");
11862 : 2 : return MATCH_ERROR;
11863 : : }
11864 : : target = target_buf;
11865 : : }
11866 : :
11867 : : /* If no target was found, it has the same name as the binding. */
11868 : 1861 : if (!target)
11869 : 1608 : target = name;
11870 : :
11871 : : /* Get the namespace to insert the symbols into. */
11872 : 3088 : ns = block->f2k_derived;
11873 : 3088 : gcc_assert (ns);
11874 : :
11875 : : /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
11876 : 3088 : if (tb.deferred && !block->attr.abstract)
11877 : : {
11878 : 1 : gfc_error ("Type %qs containing DEFERRED binding at %C "
11879 : : "is not ABSTRACT", block->name);
11880 : 1 : return MATCH_ERROR;
11881 : : }
11882 : :
11883 : : /* See if we already have a binding with this name in the symtree which
11884 : : would be an error. If a GENERIC already targeted this binding, it may
11885 : : be already there but then typebound is still NULL. */
11886 : 3087 : stree = gfc_find_symtree (ns->tb_sym_root, name);
11887 : 3087 : if (stree && stree->n.tb)
11888 : : {
11889 : 2 : gfc_error ("There is already a procedure with binding name %qs for "
11890 : : "the derived type %qs at %C", name, block->name);
11891 : 2 : return MATCH_ERROR;
11892 : : }
11893 : :
11894 : : /* Insert it and set attributes. */
11895 : :
11896 : 2997 : if (!stree)
11897 : : {
11898 : 2997 : stree = gfc_new_symtree (&ns->tb_sym_root, name);
11899 : 2997 : gcc_assert (stree);
11900 : : }
11901 : 3085 : stree->n.tb = gfc_get_typebound_proc (&tb);
11902 : :
11903 : 3085 : if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
11904 : : false))
11905 : : return MATCH_ERROR;
11906 : 3085 : gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
11907 : 3085 : gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
11908 : 3085 : target, &stree->n.tb->u.specific->n.sym->declared_at);
11909 : :
11910 : 3085 : if (gfc_match_eos () == MATCH_YES)
11911 : : return MATCH_YES;
11912 : 20 : if (gfc_match_char (',') != MATCH_YES)
11913 : 1 : goto syntax;
11914 : : }
11915 : :
11916 : 1 : syntax:
11917 : 1 : gfc_error ("Syntax error in PROCEDURE statement at %C");
11918 : 1 : return MATCH_ERROR;
11919 : : }
11920 : :
11921 : :
11922 : : /* Match a GENERIC statement.
11923 : : F2018 15.4.3.3 GENERIC statement
11924 : :
11925 : : A GENERIC statement specifies a generic identifier for one or more specific
11926 : : procedures, in the same way as a generic interface block that does not contain
11927 : : interface bodies.
11928 : :
11929 : : R1510 generic-stmt is:
11930 : : GENERIC [ , access-spec ] :: generic-spec => specific-procedure-list
11931 : :
11932 : : C1510 (R1510) A specific-procedure in a GENERIC statement shall not specify a
11933 : : procedure that was specified previously in any accessible interface with the
11934 : : same generic identifier.
11935 : :
11936 : : If access-spec appears, it specifies the accessibility (8.5.2) of generic-spec.
11937 : :
11938 : : For GENERIC statements outside of a derived type, use is made of the existing,
11939 : : typebound matching functions to obtain access-spec and generic-spec. After
11940 : : this the standard INTERFACE machinery is used. */
11941 : :
11942 : : static match
11943 : 100 : match_generic_stmt (void)
11944 : : {
11945 : 100 : char name[GFC_MAX_SYMBOL_LEN + 1];
11946 : : /* Allow space for OPERATOR(...). */
11947 : 100 : char generic_spec_name[GFC_MAX_SYMBOL_LEN + 16];
11948 : : /* Generics other than uops */
11949 : 100 : gfc_symbol* generic_spec = NULL;
11950 : : /* Generic uops */
11951 : 100 : gfc_user_op *generic_uop = NULL;
11952 : : /* For the matching calls */
11953 : 100 : gfc_typebound_proc tbattr;
11954 : 100 : gfc_namespace* ns = gfc_current_ns;
11955 : 100 : interface_type op_type;
11956 : 100 : gfc_intrinsic_op op;
11957 : 100 : match m;
11958 : 100 : gfc_symtree* st;
11959 : : /* The specific-procedure-list */
11960 : 100 : gfc_interface *generic = NULL;
11961 : : /* The head of the specific-procedure-list */
11962 : 100 : gfc_interface **generic_tail = NULL;
11963 : :
11964 : 100 : memset (&tbattr, 0, sizeof (tbattr));
11965 : 100 : tbattr.where = gfc_current_locus;
11966 : :
11967 : : /* See if we get an access-specifier. */
11968 : 100 : m = match_binding_attributes (&tbattr, true, false);
11969 : 100 : tbattr.where = gfc_current_locus;
11970 : 100 : if (m == MATCH_ERROR)
11971 : 0 : goto error;
11972 : :
11973 : : /* Now the colons, those are required. */
11974 : 100 : if (gfc_match (" ::") != MATCH_YES)
11975 : : {
11976 : 0 : gfc_error ("Expected %<::%> at %C");
11977 : 0 : goto error;
11978 : : }
11979 : :
11980 : : /* Match the generic-spec name; depending on type (operator / generic) format
11981 : : it for future error messages in 'generic_spec_name'. */
11982 : 100 : m = gfc_match_generic_spec (&op_type, name, &op);
11983 : 100 : if (m == MATCH_ERROR)
11984 : : return MATCH_ERROR;
11985 : 100 : if (m == MATCH_NO)
11986 : : {
11987 : 0 : gfc_error ("Expected generic name or operator descriptor at %C");
11988 : 0 : goto error;
11989 : : }
11990 : :
11991 : 100 : switch (op_type)
11992 : : {
11993 : 63 : case INTERFACE_GENERIC:
11994 : 63 : case INTERFACE_DTIO:
11995 : 63 : snprintf (generic_spec_name, sizeof (generic_spec_name), "%s", name);
11996 : 63 : break;
11997 : :
11998 : 22 : case INTERFACE_USER_OP:
11999 : 22 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(.%s.)", name);
12000 : 22 : break;
12001 : :
12002 : 13 : case INTERFACE_INTRINSIC_OP:
12003 : 13 : snprintf (generic_spec_name, sizeof (generic_spec_name), "OPERATOR(%s)",
12004 : : gfc_op2string (op));
12005 : 13 : break;
12006 : :
12007 : 2 : case INTERFACE_NAMELESS:
12008 : 2 : gfc_error ("Malformed GENERIC statement at %C");
12009 : 2 : goto error;
12010 : 0 : break;
12011 : :
12012 : 0 : default:
12013 : 0 : gcc_unreachable ();
12014 : : }
12015 : :
12016 : : /* Match the required =>. */
12017 : 98 : if (gfc_match (" =>") != MATCH_YES)
12018 : : {
12019 : 1 : gfc_error ("Expected %<=>%> at %C");
12020 : 1 : goto error;
12021 : : }
12022 : :
12023 : :
12024 : 97 : if (gfc_current_state () != COMP_MODULE && tbattr.access != ACCESS_UNKNOWN)
12025 : : {
12026 : 1 : gfc_error ("The access specification at %L not in a module",
12027 : : &tbattr.where);
12028 : 1 : goto error;
12029 : : }
12030 : :
12031 : : /* Try to find existing generic-spec with this name for this operator;
12032 : : if there is something, check that it is another generic-spec and then
12033 : : extend it rather than building a new symbol. Otherwise, create a new
12034 : : one with the right attributes. */
12035 : :
12036 : 96 : switch (op_type)
12037 : : {
12038 : 61 : case INTERFACE_DTIO:
12039 : 61 : case INTERFACE_GENERIC:
12040 : 61 : st = gfc_find_symtree (ns->sym_root, name);
12041 : 61 : generic_spec = st ? st->n.sym : NULL;
12042 : 61 : if (generic_spec)
12043 : : {
12044 : 25 : if (generic_spec->attr.flavor != FL_PROCEDURE
12045 : 11 : && generic_spec->attr.flavor != FL_UNKNOWN)
12046 : : {
12047 : 1 : gfc_error ("The generic-spec name %qs at %C clashes with the "
12048 : : "name of an entity declared at %L that is not a "
12049 : : "procedure", name, &generic_spec->declared_at);
12050 : 1 : goto error;
12051 : : }
12052 : :
12053 : 24 : if (op_type == INTERFACE_GENERIC && !generic_spec->attr.generic
12054 : 10 : && generic_spec->attr.flavor != FL_UNKNOWN)
12055 : : {
12056 : 0 : gfc_error ("There's already a non-generic procedure with "
12057 : : "name %qs at %C", generic_spec->name);
12058 : 0 : goto error;
12059 : : }
12060 : :
12061 : 24 : if (tbattr.access != ACCESS_UNKNOWN)
12062 : : {
12063 : 2 : if (generic_spec->attr.access != tbattr.access)
12064 : : {
12065 : 1 : gfc_error ("The access specification at %L conflicts with "
12066 : : "that already given to %qs", &tbattr.where,
12067 : : generic_spec->name);
12068 : 1 : goto error;
12069 : : }
12070 : : else
12071 : : {
12072 : 1 : gfc_error ("The access specification at %L repeats that "
12073 : : "already given to %qs", &tbattr.where,
12074 : : generic_spec->name);
12075 : 1 : goto error;
12076 : : }
12077 : : }
12078 : :
12079 : 22 : if (generic_spec->ts.type != BT_UNKNOWN)
12080 : : {
12081 : 1 : gfc_error ("The generic-spec in the generic statement at %C "
12082 : : "has a type from the declaration at %L",
12083 : : &generic_spec->declared_at);
12084 : 1 : goto error;
12085 : : }
12086 : : }
12087 : :
12088 : : /* Now create the generic_spec if it doesn't already exist and provide
12089 : : is with the appropriate attributes. */
12090 : 57 : if (!generic_spec || generic_spec->attr.flavor != FL_PROCEDURE)
12091 : : {
12092 : 45 : if (!generic_spec)
12093 : : {
12094 : 36 : gfc_get_symbol (name, ns, &generic_spec, &gfc_current_locus);
12095 : 36 : gfc_set_sym_referenced (generic_spec);
12096 : 36 : generic_spec->attr.access = tbattr.access;
12097 : : }
12098 : 9 : else if (generic_spec->attr.access == ACCESS_UNKNOWN)
12099 : 0 : generic_spec->attr.access = tbattr.access;
12100 : 45 : generic_spec->refs++;
12101 : 45 : generic_spec->attr.generic = 1;
12102 : 45 : generic_spec->attr.flavor = FL_PROCEDURE;
12103 : :
12104 : 45 : generic_spec->declared_at = gfc_current_locus;
12105 : : }
12106 : :
12107 : : /* Prepare to add the specific procedures. */
12108 : 57 : generic = generic_spec->generic;
12109 : 57 : generic_tail = &generic_spec->generic;
12110 : 57 : break;
12111 : :
12112 : 22 : case INTERFACE_USER_OP:
12113 : 22 : st = gfc_find_symtree (ns->uop_root, name);
12114 : 22 : generic_uop = st ? st->n.uop : NULL;
12115 : 2 : if (generic_uop)
12116 : : {
12117 : 2 : if (generic_uop->access != ACCESS_UNKNOWN
12118 : 2 : && tbattr.access != ACCESS_UNKNOWN)
12119 : : {
12120 : 2 : if (generic_uop->access != tbattr.access)
12121 : : {
12122 : 1 : gfc_error ("The user operator at %L must have the same "
12123 : : "access specification as already defined user "
12124 : : "operator %qs", &tbattr.where, generic_spec_name);
12125 : 1 : goto error;
12126 : : }
12127 : : else
12128 : : {
12129 : 1 : gfc_error ("The user operator at %L repeats the access "
12130 : : "specification of already defined user operator " "%qs", &tbattr.where, generic_spec_name);
12131 : 1 : goto error;
12132 : : }
12133 : : }
12134 : 0 : else if (generic_uop->access == ACCESS_UNKNOWN)
12135 : 0 : generic_uop->access = tbattr.access;
12136 : : }
12137 : : else
12138 : : {
12139 : 20 : generic_uop = gfc_get_uop (name);
12140 : 20 : generic_uop->access = tbattr.access;
12141 : : }
12142 : :
12143 : : /* Prepare to add the specific procedures. */
12144 : 20 : generic = generic_uop->op;
12145 : 20 : generic_tail = &generic_uop->op;
12146 : 20 : break;
12147 : :
12148 : 13 : case INTERFACE_INTRINSIC_OP:
12149 : 13 : generic = ns->op[op];
12150 : 13 : generic_tail = &ns->op[op];
12151 : 13 : break;
12152 : :
12153 : 0 : default:
12154 : 0 : gcc_unreachable ();
12155 : : }
12156 : :
12157 : : /* Now, match all following names in the specific-procedure-list. */
12158 : 154 : do
12159 : : {
12160 : 154 : m = gfc_match_name (name);
12161 : 154 : if (m == MATCH_ERROR)
12162 : 0 : goto error;
12163 : 154 : if (m == MATCH_NO)
12164 : : {
12165 : 0 : gfc_error ("Expected specific procedure name at %C");
12166 : 0 : goto error;
12167 : : }
12168 : :
12169 : 154 : if (op_type == INTERFACE_GENERIC
12170 : 95 : && !strcmp (generic_spec->name, name))
12171 : : {
12172 : 2 : gfc_error ("The name %qs of the specific procedure at %C conflicts "
12173 : : "with that of the generic-spec", name);
12174 : 2 : goto error;
12175 : : }
12176 : :
12177 : 152 : generic = *generic_tail;
12178 : 242 : for (; generic; generic = generic->next)
12179 : : {
12180 : 90 : if (!strcmp (generic->sym->name, name))
12181 : : {
12182 : 0 : gfc_error ("%qs already defined as a specific procedure for the"
12183 : : " generic %qs at %C", name, generic_spec->name);
12184 : 0 : goto error;
12185 : : }
12186 : : }
12187 : :
12188 : 152 : gfc_find_sym_tree (name, ns, 1, &st);
12189 : 152 : if (!st)
12190 : : {
12191 : : /* This might be a procedure that has not yet been parsed. If
12192 : : so gfc_fixup_sibling_symbols will replace this symbol with
12193 : : that of the procedure. */
12194 : 75 : gfc_get_sym_tree (name, ns, &st, false);
12195 : 75 : st->n.sym->refs++;
12196 : : }
12197 : :
12198 : 152 : generic = gfc_get_interface();
12199 : 152 : generic->next = *generic_tail;
12200 : 152 : *generic_tail = generic;
12201 : 152 : generic->where = gfc_current_locus;
12202 : 152 : generic->sym = st->n.sym;
12203 : : }
12204 : 152 : while (gfc_match (" ,") == MATCH_YES);
12205 : :
12206 : 88 : if (gfc_match_eos () != MATCH_YES)
12207 : : {
12208 : 0 : gfc_error ("Junk after GENERIC statement at %C");
12209 : 0 : goto error;
12210 : : }
12211 : :
12212 : 88 : gfc_commit_symbols ();
12213 : 88 : return MATCH_YES;
12214 : :
12215 : : error:
12216 : : return MATCH_ERROR;
12217 : : }
12218 : :
12219 : :
12220 : : /* Match a GENERIC procedure binding inside a derived type. */
12221 : :
12222 : : static match
12223 : 883 : match_typebound_generic (void)
12224 : : {
12225 : 883 : char name[GFC_MAX_SYMBOL_LEN + 1];
12226 : 883 : char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
12227 : 883 : gfc_symbol* block;
12228 : 883 : gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
12229 : 883 : gfc_typebound_proc* tb;
12230 : 883 : gfc_namespace* ns;
12231 : 883 : interface_type op_type;
12232 : 883 : gfc_intrinsic_op op;
12233 : 883 : match m;
12234 : :
12235 : : /* Check current state. */
12236 : 883 : if (gfc_current_state () == COMP_DERIVED)
12237 : : {
12238 : 0 : gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
12239 : 0 : return MATCH_ERROR;
12240 : : }
12241 : 883 : if (gfc_current_state () != COMP_DERIVED_CONTAINS)
12242 : : return MATCH_NO;
12243 : 883 : block = gfc_state_stack->previous->sym;
12244 : 883 : ns = block->f2k_derived;
12245 : 883 : gcc_assert (block && ns);
12246 : :
12247 : 883 : memset (&tbattr, 0, sizeof (tbattr));
12248 : 883 : tbattr.where = gfc_current_locus;
12249 : :
12250 : : /* See if we get an access-specifier. */
12251 : 883 : m = match_binding_attributes (&tbattr, true, false);
12252 : 883 : if (m == MATCH_ERROR)
12253 : 1 : goto error;
12254 : :
12255 : : /* Now the colons, those are required. */
12256 : 882 : if (gfc_match (" ::") != MATCH_YES)
12257 : : {
12258 : 0 : gfc_error ("Expected %<::%> at %C");
12259 : 0 : goto error;
12260 : : }
12261 : :
12262 : : /* Match the binding name; depending on type (operator / generic) format
12263 : : it for future error messages into bind_name. */
12264 : :
12265 : 882 : m = gfc_match_generic_spec (&op_type, name, &op);
12266 : 882 : if (m == MATCH_ERROR)
12267 : : return MATCH_ERROR;
12268 : 882 : if (m == MATCH_NO)
12269 : : {
12270 : 0 : gfc_error ("Expected generic name or operator descriptor at %C");
12271 : 0 : goto error;
12272 : : }
12273 : :
12274 : 882 : switch (op_type)
12275 : : {
12276 : 451 : case INTERFACE_GENERIC:
12277 : 451 : case INTERFACE_DTIO:
12278 : 451 : snprintf (bind_name, sizeof (bind_name), "%s", name);
12279 : 451 : break;
12280 : :
12281 : 27 : case INTERFACE_USER_OP:
12282 : 27 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
12283 : 27 : break;
12284 : :
12285 : 403 : case INTERFACE_INTRINSIC_OP:
12286 : 403 : snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
12287 : : gfc_op2string (op));
12288 : 403 : break;
12289 : :
12290 : 1 : case INTERFACE_NAMELESS:
12291 : 1 : gfc_error ("Malformed GENERIC statement at %C");
12292 : 1 : goto error;
12293 : 0 : break;
12294 : :
12295 : 0 : default:
12296 : 0 : gcc_unreachable ();
12297 : : }
12298 : :
12299 : : /* Match the required =>. */
12300 : 881 : if (gfc_match (" =>") != MATCH_YES)
12301 : : {
12302 : 0 : gfc_error ("Expected %<=>%> at %C");
12303 : 0 : goto error;
12304 : : }
12305 : :
12306 : : /* Try to find existing GENERIC binding with this name / for this operator;
12307 : : if there is something, check that it is another GENERIC and then extend
12308 : : it rather than building a new node. Otherwise, create it and put it
12309 : : at the right position. */
12310 : :
12311 : 881 : switch (op_type)
12312 : : {
12313 : 478 : case INTERFACE_DTIO:
12314 : 478 : case INTERFACE_USER_OP:
12315 : 478 : case INTERFACE_GENERIC:
12316 : 478 : {
12317 : 478 : const bool is_op = (op_type == INTERFACE_USER_OP);
12318 : 478 : gfc_symtree* st;
12319 : :
12320 : 478 : st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
12321 : 478 : tb = st ? st->n.tb : NULL;
12322 : : break;
12323 : : }
12324 : :
12325 : 403 : case INTERFACE_INTRINSIC_OP:
12326 : 403 : tb = ns->tb_op[op];
12327 : 403 : break;
12328 : :
12329 : 0 : default:
12330 : 0 : gcc_unreachable ();
12331 : : }
12332 : :
12333 : 414 : if (tb)
12334 : : {
12335 : 9 : if (!tb->is_generic)
12336 : : {
12337 : 1 : gcc_assert (op_type == INTERFACE_GENERIC);
12338 : 1 : gfc_error ("There's already a non-generic procedure with binding name"
12339 : : " %qs for the derived type %qs at %C",
12340 : : bind_name, block->name);
12341 : 1 : goto error;
12342 : : }
12343 : :
12344 : 8 : if (tb->access != tbattr.access)
12345 : : {
12346 : 2 : gfc_error ("Binding at %C must have the same access as already"
12347 : : " defined binding %qs", bind_name);
12348 : 2 : goto error;
12349 : : }
12350 : : }
12351 : : else
12352 : : {
12353 : 872 : tb = gfc_get_typebound_proc (NULL);
12354 : 872 : tb->where = gfc_current_locus;
12355 : 872 : tb->access = tbattr.access;
12356 : 872 : tb->is_generic = 1;
12357 : 872 : tb->u.generic = NULL;
12358 : :
12359 : 872 : switch (op_type)
12360 : : {
12361 : 469 : case INTERFACE_DTIO:
12362 : 469 : case INTERFACE_GENERIC:
12363 : 469 : case INTERFACE_USER_OP:
12364 : 469 : {
12365 : 469 : const bool is_op = (op_type == INTERFACE_USER_OP);
12366 : 469 : gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
12367 : : &ns->tb_sym_root, name);
12368 : 469 : gcc_assert (st);
12369 : 469 : st->n.tb = tb;
12370 : :
12371 : 469 : break;
12372 : : }
12373 : :
12374 : 403 : case INTERFACE_INTRINSIC_OP:
12375 : 403 : ns->tb_op[op] = tb;
12376 : 403 : break;
12377 : :
12378 : 0 : default:
12379 : 0 : gcc_unreachable ();
12380 : : }
12381 : : }
12382 : :
12383 : : /* Now, match all following names as specific targets. */
12384 : 1029 : do
12385 : : {
12386 : 1029 : gfc_symtree* target_st;
12387 : 1029 : gfc_tbp_generic* target;
12388 : :
12389 : 1029 : m = gfc_match_name (name);
12390 : 1029 : if (m == MATCH_ERROR)
12391 : 0 : goto error;
12392 : 1029 : if (m == MATCH_NO)
12393 : : {
12394 : 1 : gfc_error ("Expected specific binding name at %C");
12395 : 1 : goto error;
12396 : : }
12397 : :
12398 : 1028 : target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
12399 : :
12400 : : /* See if this is a duplicate specification. */
12401 : 1257 : for (target = tb->u.generic; target; target = target->next)
12402 : 230 : if (target_st == target->specific_st)
12403 : : {
12404 : 1 : gfc_error ("%qs already defined as specific binding for the"
12405 : : " generic %qs at %C", name, bind_name);
12406 : 1 : goto error;
12407 : : }
12408 : :
12409 : 1027 : target = gfc_get_tbp_generic ();
12410 : 1027 : target->specific_st = target_st;
12411 : 1027 : target->specific = NULL;
12412 : 1027 : target->next = tb->u.generic;
12413 : 1027 : target->is_operator = ((op_type == INTERFACE_USER_OP)
12414 : 1027 : || (op_type == INTERFACE_INTRINSIC_OP));
12415 : 1027 : tb->u.generic = target;
12416 : : }
12417 : 1027 : while (gfc_match (" ,") == MATCH_YES);
12418 : :
12419 : : /* Here should be the end. */
12420 : 876 : if (gfc_match_eos () != MATCH_YES)
12421 : : {
12422 : 1 : gfc_error ("Junk after GENERIC binding at %C");
12423 : 1 : goto error;
12424 : : }
12425 : :
12426 : : return MATCH_YES;
12427 : :
12428 : : error:
12429 : : return MATCH_ERROR;
12430 : : }
12431 : :
12432 : :
12433 : : match
12434 : 983 : gfc_match_generic ()
12435 : : {
12436 : 983 : if (gfc_option.allow_std & ~GFC_STD_OPT_F08
12437 : 981 : && gfc_current_state () != COMP_DERIVED_CONTAINS)
12438 : 100 : return match_generic_stmt ();
12439 : : else
12440 : 883 : return match_typebound_generic ();
12441 : : }
12442 : :
12443 : :
12444 : : /* Match a FINAL declaration inside a derived type. */
12445 : :
12446 : : match
12447 : 415 : gfc_match_final_decl (void)
12448 : : {
12449 : 415 : char name[GFC_MAX_SYMBOL_LEN + 1];
12450 : 415 : gfc_symbol* sym;
12451 : 415 : match m;
12452 : 415 : gfc_namespace* module_ns;
12453 : 415 : bool first, last;
12454 : 415 : gfc_symbol* block;
12455 : :
12456 : 415 : if (gfc_current_form == FORM_FREE)
12457 : : {
12458 : 415 : char c = gfc_peek_ascii_char ();
12459 : 415 : if (!gfc_is_whitespace (c) && c != ':')
12460 : : return MATCH_NO;
12461 : : }
12462 : :
12463 : 414 : if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
12464 : : {
12465 : 1 : if (gfc_current_form == FORM_FIXED)
12466 : : return MATCH_NO;
12467 : :
12468 : 1 : gfc_error ("FINAL declaration at %C must be inside a derived type "
12469 : : "CONTAINS section");
12470 : 1 : return MATCH_ERROR;
12471 : : }
12472 : :
12473 : 413 : block = gfc_state_stack->previous->sym;
12474 : 413 : gcc_assert (block);
12475 : :
12476 : 413 : if (gfc_state_stack->previous->previous
12477 : 413 : && gfc_state_stack->previous->previous->state != COMP_MODULE
12478 : 6 : && gfc_state_stack->previous->previous->state != COMP_SUBMODULE)
12479 : : {
12480 : 0 : gfc_error ("Derived type declaration with FINAL at %C must be in the"
12481 : : " specification part of a MODULE");
12482 : 0 : return MATCH_ERROR;
12483 : : }
12484 : :
12485 : 413 : module_ns = gfc_current_ns;
12486 : 413 : gcc_assert (module_ns);
12487 : 413 : gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
12488 : :
12489 : : /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
12490 : 413 : if (gfc_match (" ::") == MATCH_ERROR)
12491 : : return MATCH_ERROR;
12492 : :
12493 : : /* Match the sequence of procedure names. */
12494 : : first = true;
12495 : : last = false;
12496 : 487 : do
12497 : : {
12498 : 487 : gfc_finalizer* f;
12499 : :
12500 : 487 : if (first && gfc_match_eos () == MATCH_YES)
12501 : : {
12502 : 2 : gfc_error ("Empty FINAL at %C");
12503 : 2 : return MATCH_ERROR;
12504 : : }
12505 : :
12506 : 485 : m = gfc_match_name (name);
12507 : 485 : if (m == MATCH_NO)
12508 : : {
12509 : 1 : gfc_error ("Expected module procedure name at %C");
12510 : 1 : return MATCH_ERROR;
12511 : : }
12512 : 484 : else if (m != MATCH_YES)
12513 : : return MATCH_ERROR;
12514 : :
12515 : 484 : if (gfc_match_eos () == MATCH_YES)
12516 : : last = true;
12517 : 75 : if (!last && gfc_match_char (',') != MATCH_YES)
12518 : : {
12519 : 1 : gfc_error ("Expected %<,%> at %C");
12520 : 1 : return MATCH_ERROR;
12521 : : }
12522 : :
12523 : 483 : if (gfc_get_symbol (name, module_ns, &sym))
12524 : : {
12525 : 0 : gfc_error ("Unknown procedure name %qs at %C", name);
12526 : 0 : return MATCH_ERROR;
12527 : : }
12528 : :
12529 : : /* Mark the symbol as module procedure. */
12530 : 483 : if (sym->attr.proc != PROC_MODULE
12531 : 483 : && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
12532 : : return MATCH_ERROR;
12533 : :
12534 : : /* Check if we already have this symbol in the list, this is an error. */
12535 : 646 : for (f = block->f2k_derived->finalizers; f; f = f->next)
12536 : 164 : if (f->proc_sym == sym)
12537 : : {
12538 : 1 : gfc_error ("%qs at %C is already defined as FINAL procedure",
12539 : : name);
12540 : 1 : return MATCH_ERROR;
12541 : : }
12542 : :
12543 : : /* Add this symbol to the list of finalizers. */
12544 : 482 : gcc_assert (block->f2k_derived);
12545 : 482 : sym->refs++;
12546 : 482 : f = XCNEW (gfc_finalizer);
12547 : 482 : f->proc_sym = sym;
12548 : 482 : f->proc_tree = NULL;
12549 : 482 : f->where = gfc_current_locus;
12550 : 482 : f->next = block->f2k_derived->finalizers;
12551 : 482 : block->f2k_derived->finalizers = f;
12552 : :
12553 : 482 : first = false;
12554 : : }
12555 : 482 : while (!last);
12556 : :
12557 : : return MATCH_YES;
12558 : : }
12559 : :
12560 : :
12561 : : const ext_attr_t ext_attr_list[] = {
12562 : : { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
12563 : : { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
12564 : : { "cdecl", EXT_ATTR_CDECL, "cdecl" },
12565 : : { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
12566 : : { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
12567 : : { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
12568 : : { "deprecated", EXT_ATTR_DEPRECATED, NULL },
12569 : : { "noinline", EXT_ATTR_NOINLINE, NULL },
12570 : : { "noreturn", EXT_ATTR_NORETURN, NULL },
12571 : : { "weak", EXT_ATTR_WEAK, NULL },
12572 : : { NULL, EXT_ATTR_LAST, NULL }
12573 : : };
12574 : :
12575 : : /* Match a !GCC$ ATTRIBUTES statement of the form:
12576 : : !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
12577 : : When we come here, we have already matched the !GCC$ ATTRIBUTES string.
12578 : :
12579 : : TODO: We should support all GCC attributes using the same syntax for
12580 : : the attribute list, i.e. the list in C
12581 : : __attributes(( attribute-list ))
12582 : : matches then
12583 : : !GCC$ ATTRIBUTES attribute-list ::
12584 : : Cf. c-parser.cc's c_parser_attributes; the data can then directly be
12585 : : saved into a TREE.
12586 : :
12587 : : As there is absolutely no risk of confusion, we should never return
12588 : : MATCH_NO. */
12589 : : match
12590 : 2964 : gfc_match_gcc_attributes (void)
12591 : : {
12592 : 2964 : symbol_attribute attr;
12593 : 2964 : char name[GFC_MAX_SYMBOL_LEN + 1];
12594 : 2964 : unsigned id;
12595 : 2964 : gfc_symbol *sym;
12596 : 2964 : match m;
12597 : :
12598 : 2964 : gfc_clear_attr (&attr);
12599 : 2964 : for(;;)
12600 : : {
12601 : 2964 : char ch;
12602 : :
12603 : 2964 : if (gfc_match_name (name) != MATCH_YES)
12604 : : return MATCH_ERROR;
12605 : :
12606 : 17857 : for (id = 0; id < EXT_ATTR_LAST; id++)
12607 : 17857 : if (strcmp (name, ext_attr_list[id].name) == 0)
12608 : : break;
12609 : :
12610 : 2964 : if (id == EXT_ATTR_LAST)
12611 : : {
12612 : 0 : gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
12613 : 0 : return MATCH_ERROR;
12614 : : }
12615 : :
12616 : 2964 : if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
12617 : : return MATCH_ERROR;
12618 : :
12619 : 2964 : gfc_gobble_whitespace ();
12620 : 2964 : ch = gfc_next_ascii_char ();
12621 : 2964 : if (ch == ':')
12622 : : {
12623 : : /* This is the successful exit condition for the loop. */
12624 : 2964 : if (gfc_next_ascii_char () == ':')
12625 : : break;
12626 : : }
12627 : :
12628 : 0 : if (ch == ',')
12629 : 0 : continue;
12630 : :
12631 : 0 : goto syntax;
12632 : 0 : }
12633 : :
12634 : 2964 : if (gfc_match_eos () == MATCH_YES)
12635 : 0 : goto syntax;
12636 : :
12637 : 2971 : for(;;)
12638 : : {
12639 : 2971 : m = gfc_match_name (name);
12640 : 2971 : if (m != MATCH_YES)
12641 : : return m;
12642 : :
12643 : 2971 : if (find_special (name, &sym, true))
12644 : : return MATCH_ERROR;
12645 : :
12646 : 2971 : sym->attr.ext_attr |= attr.ext_attr;
12647 : :
12648 : 2971 : if (gfc_match_eos () == MATCH_YES)
12649 : : break;
12650 : :
12651 : 7 : if (gfc_match_char (',') != MATCH_YES)
12652 : 0 : goto syntax;
12653 : : }
12654 : :
12655 : : return MATCH_YES;
12656 : :
12657 : 0 : syntax:
12658 : 0 : gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
12659 : 0 : return MATCH_ERROR;
12660 : : }
12661 : :
12662 : :
12663 : : /* Match a !GCC$ UNROLL statement of the form:
12664 : : !GCC$ UNROLL n
12665 : :
12666 : : The parameter n is the number of times we are supposed to unroll.
12667 : :
12668 : : When we come here, we have already matched the !GCC$ UNROLL string. */
12669 : : match
12670 : 19 : gfc_match_gcc_unroll (void)
12671 : : {
12672 : 19 : int value;
12673 : :
12674 : : /* FIXME: use gfc_match_small_literal_int instead, delete small_int */
12675 : 19 : if (gfc_match_small_int (&value) == MATCH_YES)
12676 : : {
12677 : 19 : if (value < 0 || value > USHRT_MAX)
12678 : : {
12679 : 2 : gfc_error ("%<GCC unroll%> directive requires a"
12680 : : " non-negative integral constant"
12681 : : " less than or equal to %u at %C",
12682 : : USHRT_MAX
12683 : : );
12684 : 2 : return MATCH_ERROR;
12685 : : }
12686 : 17 : if (gfc_match_eos () == MATCH_YES)
12687 : : {
12688 : 17 : directive_unroll = value == 0 ? 1 : value;
12689 : 17 : return MATCH_YES;
12690 : : }
12691 : : }
12692 : :
12693 : 0 : gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
12694 : 0 : return MATCH_ERROR;
12695 : : }
12696 : :
12697 : : /* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
12698 : :
12699 : : The parameter b is name of a middle-end built-in.
12700 : : FLAGS is optional and must be one of:
12701 : : - (inbranch)
12702 : : - (notinbranch)
12703 : :
12704 : : IF('target') is optional and TARGET is a name of a multilib ABI.
12705 : :
12706 : : When we come here, we have already matched the !GCC$ builtin string. */
12707 : :
12708 : : match
12709 : 3339392 : gfc_match_gcc_builtin (void)
12710 : : {
12711 : 3339392 : char builtin[GFC_MAX_SYMBOL_LEN + 1];
12712 : 3339392 : char target[GFC_MAX_SYMBOL_LEN + 1];
12713 : :
12714 : 3339392 : if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
12715 : : return MATCH_ERROR;
12716 : :
12717 : 3339392 : gfc_simd_clause clause = SIMD_NONE;
12718 : 3339392 : if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
12719 : : clause = SIMD_NOTINBRANCH;
12720 : 21 : else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
12721 : 15 : clause = SIMD_INBRANCH;
12722 : :
12723 : 3339392 : if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
12724 : : {
12725 : 3339363 : const char *abi = targetm.get_multilib_abi_name ();
12726 : 3339363 : if (abi == NULL || strcmp (abi, target) != 0)
12727 : : return MATCH_YES;
12728 : : }
12729 : :
12730 : 1647733 : if (gfc_vectorized_builtins == NULL)
12731 : 30519 : gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
12732 : :
12733 : 1647733 : char *r = XNEWVEC (char, strlen (builtin) + 32);
12734 : 1647733 : sprintf (r, "__builtin_%s", builtin);
12735 : :
12736 : 1647733 : bool existed;
12737 : 1647733 : int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
12738 : 1647733 : value |= clause;
12739 : 1647733 : if (existed)
12740 : 22 : free (r);
12741 : :
12742 : : return MATCH_YES;
12743 : : }
12744 : :
12745 : : /* Match an !GCC$ IVDEP statement.
12746 : : When we come here, we have already matched the !GCC$ IVDEP string. */
12747 : :
12748 : : match
12749 : 3 : gfc_match_gcc_ivdep (void)
12750 : : {
12751 : 3 : if (gfc_match_eos () == MATCH_YES)
12752 : : {
12753 : 3 : directive_ivdep = true;
12754 : 3 : return MATCH_YES;
12755 : : }
12756 : :
12757 : 0 : gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
12758 : 0 : return MATCH_ERROR;
12759 : : }
12760 : :
12761 : : /* Match an !GCC$ VECTOR statement.
12762 : : When we come here, we have already matched the !GCC$ VECTOR string. */
12763 : :
12764 : : match
12765 : 3 : gfc_match_gcc_vector (void)
12766 : : {
12767 : 3 : if (gfc_match_eos () == MATCH_YES)
12768 : : {
12769 : 3 : directive_vector = true;
12770 : 3 : directive_novector = false;
12771 : 3 : return MATCH_YES;
12772 : : }
12773 : :
12774 : 0 : gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
12775 : 0 : return MATCH_ERROR;
12776 : : }
12777 : :
12778 : : /* Match an !GCC$ NOVECTOR statement.
12779 : : When we come here, we have already matched the !GCC$ NOVECTOR string. */
12780 : :
12781 : : match
12782 : 3 : gfc_match_gcc_novector (void)
12783 : : {
12784 : 3 : if (gfc_match_eos () == MATCH_YES)
12785 : : {
12786 : 3 : directive_novector = true;
12787 : 3 : directive_vector = false;
12788 : 3 : return MATCH_YES;
12789 : : }
12790 : :
12791 : 0 : gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
12792 : 0 : return MATCH_ERROR;
12793 : : }
|