]> gcc.gnu.org Git - gcc.git/blame_incremental - gcc/fortran/decl.c
Fortran: validate shape of arrays in constructors against declarations
[gcc.git] / gcc / fortran / decl.c
... / ...
CommitLineData
1/* Declaration statement matcher
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
40static 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
46static 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
53static gfc_typespec current_ts;
54
55static symbol_attribute current_attr;
56static gfc_array_spec *current_as;
57static int colon_seen;
58static int attr_seen;
59
60/* The current binding label (if any). */
61static 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. */
64static 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. */
67static int has_name_equals = 0;
68
69/* Initializer of the previous enumerator. */
70
71static 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
77typedef struct enumerator_history
78{
79 gfc_symbol *sym;
80 gfc_expr *initializer;
81 struct enumerator_history *next;
82}
83enumerator_history;
84
85/* Header of enum history chain. */
86
87static enumerator_history *enum_history = NULL;
88
89/* Pointer of enum history node containing largest initializer. */
90
91static enumerator_history *max_enum = NULL;
92
93/* gfc_new_block points to the symbol of a newly matched block. */
94
95gfc_symbol *gfc_new_block;
96
97bool gfc_matching_function;
98
99/* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */
100int directive_unroll = -1;
101
102/* Set upon parsing supported !GCC$ pragmas for use in the next loop. */
103bool directive_ivdep = false;
104bool directive_vector = false;
105bool directive_novector = false;
106
107/* Map of middle-end built-ins that should be vectorized. */
108hash_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. */
112static 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. */
116static gfc_actual_arglist *decl_type_param_list;
117static gfc_actual_arglist *type_param_spec_list;
118
119/********************* DATA statement subroutines *********************/
120
121static bool in_match_data = false;
122
123bool
124gfc_in_match_data (void)
125{
126 return in_match_data;
127}
128
129static void
130set_in_match_data (bool set_value)
131{
132 in_match_data = set_value;
133}
134
135/* Free a gfc_data_variable structure and everything beneath it. */
136
137static void
138free_variable (gfc_data_variable *p)
139{
140 gfc_data_variable *q;
141
142 for (; p; p = q)
143 {
144 q = p->next;
145 gfc_free_expr (p->expr);
146 gfc_free_iterator (&p->iter, 0);
147 free_variable (p->list);
148 free (p);
149 }
150}
151
152
153/* Free a gfc_data_value structure and everything beneath it. */
154
155static void
156free_value (gfc_data_value *p)
157{
158 gfc_data_value *q;
159
160 for (; p; p = q)
161 {
162 q = p->next;
163 mpz_clear (p->repeat);
164 gfc_free_expr (p->expr);
165 free (p);
166 }
167}
168
169
170/* Free a list of gfc_data structures. */
171
172void
173gfc_free_data (gfc_data *p)
174{
175 gfc_data *q;
176
177 for (; p; p = q)
178 {
179 q = p->next;
180 free_variable (p->var);
181 free_value (p->value);
182 free (p);
183 }
184}
185
186
187/* Free all data in a namespace. */
188
189static void
190gfc_free_data_all (gfc_namespace *ns)
191{
192 gfc_data *d;
193
194 for (;ns->data;)
195 {
196 d = ns->data->next;
197 free (ns->data);
198 ns->data = d;
199 }
200}
201
202/* Reject data parsed since the last restore point was marked. */
203
204void
205gfc_reject_data (gfc_namespace *ns)
206{
207 gfc_data *d;
208
209 while (ns->data && ns->data != ns->old_data)
210 {
211 d = ns->data->next;
212 free (ns->data);
213 ns->data = d;
214 }
215}
216
217static match var_element (gfc_data_variable *);
218
219/* Match a list of variables terminated by an iterator and a right
220 parenthesis. */
221
222static match
223var_list (gfc_data_variable *parent)
224{
225 gfc_data_variable *tail, var;
226 match m;
227
228 m = var_element (&var);
229 if (m == MATCH_ERROR)
230 return MATCH_ERROR;
231 if (m == MATCH_NO)
232 goto syntax;
233
234 tail = gfc_get_data_variable ();
235 *tail = var;
236
237 parent->list = tail;
238
239 for (;;)
240 {
241 if (gfc_match_char (',') != MATCH_YES)
242 goto syntax;
243
244 m = gfc_match_iterator (&parent->iter, 1);
245 if (m == MATCH_YES)
246 break;
247 if (m == MATCH_ERROR)
248 return MATCH_ERROR;
249
250 m = var_element (&var);
251 if (m == MATCH_ERROR)
252 return MATCH_ERROR;
253 if (m == MATCH_NO)
254 goto syntax;
255
256 tail->next = gfc_get_data_variable ();
257 tail = tail->next;
258
259 *tail = var;
260 }
261
262 if (gfc_match_char (')') != MATCH_YES)
263 goto syntax;
264 return MATCH_YES;
265
266syntax:
267 gfc_syntax_error (ST_DATA);
268 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
275static match
276var_element (gfc_data_variable *new_var)
277{
278 match m;
279 gfc_symbol *sym;
280
281 memset (new_var, 0, sizeof (gfc_data_variable));
282
283 if (gfc_match_char ('(') == MATCH_YES)
284 return var_list (new_var);
285
286 m = gfc_match_variable (&new_var->expr, 0);
287 if (m != MATCH_YES)
288 return m;
289
290 if (new_var->expr->expr_type == EXPR_CONSTANT
291 && new_var->expr->symtree == NULL)
292 {
293 gfc_error ("Inquiry parameter cannot appear in a "
294 "data-stmt-object-list at %C");
295 return MATCH_ERROR;
296 }
297
298 sym = new_var->expr->symtree->n.sym;
299
300 /* Symbol should already have an associated type. */
301 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
302 return MATCH_ERROR;
303
304 if (!sym->attr.function && gfc_current_ns->parent
305 && gfc_current_ns->parent == sym->ns)
306 {
307 gfc_error ("Host associated variable %qs may not be in the DATA "
308 "statement at %C", sym->name);
309 return MATCH_ERROR;
310 }
311
312 if (gfc_current_state () != COMP_BLOCK_DATA
313 && sym->attr.in_common
314 && !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 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
328static match
329top_var_list (gfc_data *d)
330{
331 gfc_data_variable var, *tail, *new_var;
332 match m;
333
334 tail = NULL;
335
336 for (;;)
337 {
338 m = var_element (&var);
339 if (m == MATCH_NO)
340 goto syntax;
341 if (m == MATCH_ERROR)
342 return MATCH_ERROR;
343
344 new_var = gfc_get_data_variable ();
345 *new_var = var;
346 if (new_var->expr)
347 new_var->expr->where = gfc_current_locus;
348
349 if (tail == NULL)
350 d->var = new_var;
351 else
352 tail->next = new_var;
353
354 tail = new_var;
355
356 if (gfc_match_char ('/') == MATCH_YES)
357 break;
358 if (gfc_match_char (',') != MATCH_YES)
359 goto syntax;
360 }
361
362 return MATCH_YES;
363
364syntax:
365 gfc_syntax_error (ST_DATA);
366 gfc_free_data_all (gfc_current_ns);
367 return MATCH_ERROR;
368}
369
370
371static match
372match_data_constant (gfc_expr **result)
373{
374 char name[GFC_MAX_SYMBOL_LEN + 1];
375 gfc_symbol *sym, *dt_sym = NULL;
376 gfc_expr *expr;
377 match m;
378 locus old_loc;
379
380 m = gfc_match_literal_constant (&expr, 1);
381 if (m == MATCH_YES)
382 {
383 *result = expr;
384 return MATCH_YES;
385 }
386
387 if (m == MATCH_ERROR)
388 return MATCH_ERROR;
389
390 m = gfc_match_null (result);
391 if (m != MATCH_NO)
392 return m;
393
394 old_loc = gfc_current_locus;
395
396 /* Should this be a structure component, try to match it
397 before matching a name. */
398 m = gfc_match_rvalue (result);
399 if (m == MATCH_ERROR)
400 return m;
401
402 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
403 {
404 if (!gfc_simplify_expr (*result, 0))
405 m = MATCH_ERROR;
406 return m;
407 }
408 else if (m == MATCH_YES)
409 {
410 /* If a parameter inquiry ends up here, symtree is NULL but **result
411 contains the right constant expression. Check here. */
412 if ((*result)->symtree == NULL
413 && (*result)->expr_type == EXPR_CONSTANT
414 && ((*result)->ts.type == BT_INTEGER
415 || (*result)->ts.type == BT_REAL))
416 return m;
417
418 /* F2018:R845 data-stmt-constant is initial-data-target.
419 A data-stmt-constant shall be ... initial-data-target if and
420 only if the corresponding data-stmt-object has the POINTER
421 attribute. ... If data-stmt-constant is initial-data-target
422 the corresponding data statement object shall be
423 data-pointer-initialization compatible (7.5.4.6) with the initial
424 data target; the data statement object is initially associated
425 with the target. */
426 if ((*result)->symtree->n.sym->attr.save
427 && (*result)->symtree->n.sym->attr.target)
428 return m;
429 gfc_free_expr (*result);
430 }
431
432 gfc_current_locus = old_loc;
433
434 m = gfc_match_name (name);
435 if (m != MATCH_YES)
436 return m;
437
438 if (gfc_find_symbol (name, NULL, 1, &sym))
439 return MATCH_ERROR;
440
441 if (sym && sym->attr.generic)
442 dt_sym = gfc_find_dt_in_generic (sym);
443
444 if (sym == NULL
445 || (sym->attr.flavor != FL_PARAMETER
446 && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor))))
447 {
448 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
449 name);
450 *result = NULL;
451 return MATCH_ERROR;
452 }
453 else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
454 return gfc_match_structure_constructor (dt_sym, result);
455
456 /* Check to see if the value is an initialization array expression. */
457 if (sym->value->expr_type == EXPR_ARRAY)
458 {
459 gfc_current_locus = old_loc;
460
461 m = gfc_match_init_expr (result);
462 if (m == MATCH_ERROR)
463 return m;
464
465 if (m == MATCH_YES)
466 {
467 if (!gfc_simplify_expr (*result, 0))
468 m = MATCH_ERROR;
469
470 if ((*result)->expr_type == EXPR_CONSTANT)
471 return m;
472 else
473 {
474 gfc_error ("Invalid initializer %s in Data statement at %C", name);
475 return MATCH_ERROR;
476 }
477 }
478 }
479
480 *result = gfc_copy_expr (sym->value);
481 return MATCH_YES;
482}
483
484
485/* Match a list of values in a DATA statement. The leading '/' has
486 already been seen at this point. */
487
488static match
489top_val_list (gfc_data *data)
490{
491 gfc_data_value *new_val, *tail;
492 gfc_expr *expr;
493 match m;
494
495 tail = NULL;
496
497 for (;;)
498 {
499 m = match_data_constant (&expr);
500 if (m == MATCH_NO)
501 goto syntax;
502 if (m == MATCH_ERROR)
503 return MATCH_ERROR;
504
505 new_val = gfc_get_data_value ();
506 mpz_init (new_val->repeat);
507
508 if (tail == NULL)
509 data->value = new_val;
510 else
511 tail->next = new_val;
512
513 tail = new_val;
514
515 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
516 {
517 tail->expr = expr;
518 mpz_set_ui (tail->repeat, 1);
519 }
520 else
521 {
522 mpz_set (tail->repeat, expr->value.integer);
523 gfc_free_expr (expr);
524
525 m = match_data_constant (&tail->expr);
526 if (m == MATCH_NO)
527 goto syntax;
528 if (m == MATCH_ERROR)
529 return MATCH_ERROR;
530 }
531
532 if (gfc_match_char ('/') == MATCH_YES)
533 break;
534 if (gfc_match_char (',') == MATCH_NO)
535 goto syntax;
536 }
537
538 return MATCH_YES;
539
540syntax:
541 gfc_syntax_error (ST_DATA);
542 gfc_free_data_all (gfc_current_ns);
543 return MATCH_ERROR;
544}
545
546
547/* Matches an old style initialization. */
548
549static match
550match_old_style_init (const char *name)
551{
552 match m;
553 gfc_symtree *st;
554 gfc_symbol *sym;
555 gfc_data *newdata, *nd;
556
557 /* Set up data structure to hold initializers. */
558 gfc_find_sym_tree (name, NULL, 0, &st);
559 sym = st->n.sym;
560
561 newdata = gfc_get_data ();
562 newdata->var = gfc_get_data_variable ();
563 newdata->var->expr = gfc_get_variable_expr (st);
564 newdata->var->expr->where = sym->declared_at;
565 newdata->where = gfc_current_locus;
566
567 /* Match initial value list. This also eats the terminal '/'. */
568 m = top_val_list (newdata);
569 if (m != MATCH_YES)
570 {
571 free (newdata);
572 return m;
573 }
574
575 /* Check that a BOZ did not creep into an old-style initialization. */
576 for (nd = newdata; nd; nd = nd->next)
577 {
578 if (nd->value->expr->ts.type == BT_BOZ
579 && gfc_invalid_boz (G_("BOZ at %L cannot appear in an old-style "
580 "initialization"), &nd->value->expr->where))
581 return MATCH_ERROR;
582
583 if (nd->var->expr->ts.type != BT_INTEGER
584 && nd->var->expr->ts.type != BT_REAL
585 && nd->value->expr->ts.type == BT_BOZ)
586 {
587 gfc_error (G_("BOZ literal constant near %L cannot be assigned to "
588 "a %qs variable in an old-style initialization"),
589 &nd->value->expr->where,
590 gfc_typename (&nd->value->expr->ts));
591 return MATCH_ERROR;
592 }
593 }
594
595 if (gfc_pure (NULL))
596 {
597 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
598 free (newdata);
599 return MATCH_ERROR;
600 }
601 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
602
603 /* Mark the variable as having appeared in a data statement. */
604 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
605 {
606 free (newdata);
607 return MATCH_ERROR;
608 }
609
610 /* Chain in namespace list of DATA initializers. */
611 newdata->next = gfc_current_ns->data;
612 gfc_current_ns->data = newdata;
613
614 return m;
615}
616
617
618/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
619 we are matching a DATA statement and are therefore issuing an error
620 if we encounter something unexpected, if not, we're trying to match
621 an old-style initialization expression of the form INTEGER I /2/. */
622
623match
624gfc_match_data (void)
625{
626 gfc_data *new_data;
627 gfc_expr *e;
628 gfc_ref *ref;
629 match m;
630 char c;
631
632 /* DATA has been matched. In free form source code, the next character
633 needs to be whitespace or '(' from an implied do-loop. Check that
634 here. */
635 c = gfc_peek_ascii_char ();
636 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '(')
637 return MATCH_NO;
638
639 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
640 if ((gfc_current_state () == COMP_FUNCTION
641 || gfc_current_state () == COMP_SUBROUTINE)
642 && gfc_state_stack->previous->state == COMP_INTERFACE)
643 {
644 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
645 return MATCH_ERROR;
646 }
647
648 set_in_match_data (true);
649
650 for (;;)
651 {
652 new_data = gfc_get_data ();
653 new_data->where = gfc_current_locus;
654
655 m = top_var_list (new_data);
656 if (m != MATCH_YES)
657 goto cleanup;
658
659 if (new_data->var->iter.var
660 && new_data->var->iter.var->ts.type == BT_INTEGER
661 && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1
662 && new_data->var->list
663 && new_data->var->list->expr
664 && new_data->var->list->expr->ts.type == BT_CHARACTER
665 && new_data->var->list->expr->ref
666 && new_data->var->list->expr->ref->type == REF_SUBSTRING)
667 {
668 gfc_error ("Invalid substring in data-implied-do at %L in DATA "
669 "statement", &new_data->var->list->expr->where);
670 goto cleanup;
671 }
672
673 /* Check for an entity with an allocatable component, which is not
674 allowed. */
675 e = new_data->var->expr;
676 if (e)
677 {
678 bool invalid;
679
680 invalid = false;
681 for (ref = e->ref; ref; ref = ref->next)
682 if ((ref->type == REF_COMPONENT
683 && ref->u.c.component->attr.allocatable)
684 || (ref->type == REF_ARRAY
685 && e->symtree->n.sym->attr.pointer != 1
686 && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED))
687 invalid = true;
688
689 if (invalid)
690 {
691 gfc_error ("Allocatable component or deferred-shaped array "
692 "near %C in DATA statement");
693 goto cleanup;
694 }
695
696 /* F2008:C567 (R536) A data-i-do-object or a variable that appears
697 as a data-stmt-object shall not be an object designator in which
698 a pointer appears other than as the entire rightmost part-ref. */
699 if (!e->ref && e->ts.type == BT_DERIVED
700 && e->symtree->n.sym->attr.pointer)
701 goto partref;
702
703 ref = e->ref;
704 if (e->symtree->n.sym->ts.type == BT_DERIVED
705 && e->symtree->n.sym->attr.pointer
706 && ref->type == REF_COMPONENT)
707 goto partref;
708
709 for (; ref; ref = ref->next)
710 if (ref->type == REF_COMPONENT
711 && ref->u.c.component->attr.pointer
712 && ref->next)
713 goto partref;
714 }
715
716 m = top_val_list (new_data);
717 if (m != MATCH_YES)
718 goto cleanup;
719
720 new_data->next = gfc_current_ns->data;
721 gfc_current_ns->data = new_data;
722
723 /* A BOZ literal constant cannot appear in a structure constructor.
724 Check for that here for a data statement value. */
725 if (new_data->value->expr->ts.type == BT_DERIVED
726 && new_data->value->expr->value.constructor)
727 {
728 gfc_constructor *c;
729 c = gfc_constructor_first (new_data->value->expr->value.constructor);
730 for (; c; c = gfc_constructor_next (c))
731 if (c->expr && c->expr->ts.type == BT_BOZ)
732 {
733 gfc_error ("BOZ literal constant at %L cannot appear in a "
734 "structure constructor", &c->expr->where);
735 return MATCH_ERROR;
736 }
737 }
738
739 if (gfc_match_eos () == MATCH_YES)
740 break;
741
742 gfc_match_char (','); /* Optional comma */
743 }
744
745 set_in_match_data (false);
746
747 if (gfc_pure (NULL))
748 {
749 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
750 return MATCH_ERROR;
751 }
752 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
753
754 return MATCH_YES;
755
756partref:
757
758 gfc_error ("part-ref with pointer attribute near %L is not "
759 "rightmost part-ref of data-stmt-object",
760 &e->where);
761
762cleanup:
763 set_in_match_data (false);
764 gfc_free_data (new_data);
765 return MATCH_ERROR;
766}
767
768
769/************************ Declaration statements *********************/
770
771
772/* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
773 list). The difference here is the expression is a list of constants
774 and is surrounded by '/'.
775 The typespec ts must match the typespec of the variable which the
776 clist is initializing.
777 The arrayspec tells whether this should match a list of constants
778 corresponding to array elements or a scalar (as == NULL). */
779
780static match
781match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as)
782{
783 gfc_constructor_base array_head = NULL;
784 gfc_expr *expr = NULL;
785 match m = MATCH_ERROR;
786 locus where;
787 mpz_t repeat, cons_size, as_size;
788 bool scalar;
789 int cmp;
790
791 gcc_assert (ts);
792
793 /* We have already matched '/' - now look for a constant list, as with
794 top_val_list from decl.c, but append the result to an array. */
795 if (gfc_match ("/") == MATCH_YES)
796 {
797 gfc_error ("Empty old style initializer list at %C");
798 return MATCH_ERROR;
799 }
800
801 where = gfc_current_locus;
802 scalar = !as || !as->rank;
803
804 if (!scalar && !spec_size (as, &as_size))
805 {
806 gfc_error ("Array in initializer list at %L must have an explicit shape",
807 as->type == AS_EXPLICIT ? &as->upper[0]->where : &where);
808 /* Nothing to cleanup yet. */
809 return MATCH_ERROR;
810 }
811
812 mpz_init_set_ui (repeat, 0);
813
814 for (;;)
815 {
816 m = match_data_constant (&expr);
817 if (m != MATCH_YES)
818 expr = NULL; /* match_data_constant may set expr to garbage */
819 if (m == MATCH_NO)
820 goto syntax;
821 if (m == MATCH_ERROR)
822 goto cleanup;
823
824 /* Found r in repeat spec r*c; look for the constant to repeat. */
825 if ( gfc_match_char ('*') == MATCH_YES)
826 {
827 if (scalar)
828 {
829 gfc_error ("Repeat spec invalid in scalar initializer at %C");
830 goto cleanup;
831 }
832 if (expr->ts.type != BT_INTEGER)
833 {
834 gfc_error ("Repeat spec must be an integer at %C");
835 goto cleanup;
836 }
837 mpz_set (repeat, expr->value.integer);
838 gfc_free_expr (expr);
839 expr = NULL;
840
841 m = match_data_constant (&expr);
842 if (m == MATCH_NO)
843 {
844 m = MATCH_ERROR;
845 gfc_error ("Expected data constant after repeat spec at %C");
846 }
847 if (m != MATCH_YES)
848 goto cleanup;
849 }
850 /* No repeat spec, we matched the data constant itself. */
851 else
852 mpz_set_ui (repeat, 1);
853
854 if (!scalar)
855 {
856 /* Add the constant initializer as many times as repeated. */
857 for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1))
858 {
859 /* Make sure types of elements match */
860 if(ts && !gfc_compare_types (&expr->ts, ts)
861 && !gfc_convert_type (expr, ts, 1))
862 goto cleanup;
863
864 gfc_constructor_append_expr (&array_head,
865 gfc_copy_expr (expr), &gfc_current_locus);
866 }
867
868 gfc_free_expr (expr);
869 expr = NULL;
870 }
871
872 /* For scalar initializers quit after one element. */
873 else
874 {
875 if(gfc_match_char ('/') != MATCH_YES)
876 {
877 gfc_error ("End of scalar initializer expected at %C");
878 goto cleanup;
879 }
880 break;
881 }
882
883 if (gfc_match_char ('/') == MATCH_YES)
884 break;
885 if (gfc_match_char (',') == MATCH_NO)
886 goto syntax;
887 }
888
889 /* If we break early from here out, we encountered an error. */
890 m = MATCH_ERROR;
891
892 /* Set up expr as an array constructor. */
893 if (!scalar)
894 {
895 expr = gfc_get_array_expr (ts->type, ts->kind, &where);
896 expr->ts = *ts;
897 expr->value.constructor = array_head;
898
899 /* Validate sizes. We built expr ourselves, so cons_size will be
900 constant (we fail above for non-constant expressions).
901 We still need to verify that the sizes match. */
902 gcc_assert (gfc_array_size (expr, &cons_size));
903 cmp = mpz_cmp (cons_size, as_size);
904 if (cmp < 0)
905 gfc_error ("Not enough elements in array initializer at %C");
906 else if (cmp > 0)
907 gfc_error ("Too many elements in array initializer at %C");
908 mpz_clear (cons_size);
909 if (cmp)
910 goto cleanup;
911
912 /* Set the rank/shape to match the LHS as auto-reshape is implied. */
913 expr->rank = as->rank;
914 expr->shape = gfc_get_shape (as->rank);
915 for (int i = 0; i < as->rank; ++i)
916 spec_dimen_size (as, i, &expr->shape[i]);
917 }
918
919 /* Make sure scalar types match. */
920 else if (!gfc_compare_types (&expr->ts, ts)
921 && !gfc_convert_type (expr, ts, 1))
922 goto cleanup;
923
924 if (expr->ts.u.cl)
925 expr->ts.u.cl->length_from_typespec = 1;
926
927 *result = expr;
928 m = MATCH_YES;
929 goto done;
930
931syntax:
932 m = MATCH_ERROR;
933 gfc_error ("Syntax error in old style initializer list at %C");
934
935cleanup:
936 if (expr)
937 expr->value.constructor = NULL;
938 gfc_free_expr (expr);
939 gfc_constructor_free (array_head);
940
941done:
942 mpz_clear (repeat);
943 if (!scalar)
944 mpz_clear (as_size);
945 return m;
946}
947
948
949/* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
950
951static bool
952merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
953{
954 if ((from->type == AS_ASSUMED_RANK && to->corank)
955 || (to->type == AS_ASSUMED_RANK && from->corank))
956 {
957 gfc_error ("The assumed-rank array at %C shall not have a codimension");
958 return false;
959 }
960
961 if (to->rank == 0 && from->rank > 0)
962 {
963 to->rank = from->rank;
964 to->type = from->type;
965 to->cray_pointee = from->cray_pointee;
966 to->cp_was_assumed = from->cp_was_assumed;
967
968 for (int i = to->corank - 1; i >= 0; i--)
969 {
970 /* Do not exceed the limits on lower[] and upper[]. gfortran
971 cleans up elsewhere. */
972 int j = from->rank + i;
973 if (j >= GFC_MAX_DIMENSIONS)
974 break;
975
976 to->lower[j] = to->lower[i];
977 to->upper[j] = to->upper[i];
978 }
979 for (int i = 0; i < from->rank; i++)
980 {
981 if (copy)
982 {
983 to->lower[i] = gfc_copy_expr (from->lower[i]);
984 to->upper[i] = gfc_copy_expr (from->upper[i]);
985 }
986 else
987 {
988 to->lower[i] = from->lower[i];
989 to->upper[i] = from->upper[i];
990 }
991 }
992 }
993 else if (to->corank == 0 && from->corank > 0)
994 {
995 to->corank = from->corank;
996 to->cotype = from->cotype;
997
998 for (int i = 0; i < from->corank; i++)
999 {
1000 /* Do not exceed the limits on lower[] and upper[]. gfortran
1001 cleans up elsewhere. */
1002 int k = from->rank + i;
1003 int j = to->rank + i;
1004 if (j >= GFC_MAX_DIMENSIONS)
1005 break;
1006
1007 if (copy)
1008 {
1009 to->lower[j] = gfc_copy_expr (from->lower[k]);
1010 to->upper[j] = gfc_copy_expr (from->upper[k]);
1011 }
1012 else
1013 {
1014 to->lower[j] = from->lower[k];
1015 to->upper[j] = from->upper[k];
1016 }
1017 }
1018 }
1019
1020 if (to->rank + to->corank > GFC_MAX_DIMENSIONS)
1021 {
1022 gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum "
1023 "allowed dimensions of %d",
1024 to->rank, to->corank, GFC_MAX_DIMENSIONS);
1025 to->corank = GFC_MAX_DIMENSIONS - to->rank;
1026 return false;
1027 }
1028 return true;
1029}
1030
1031
1032/* Match an intent specification. Since this can only happen after an
1033 INTENT word, a legal intent-spec must follow. */
1034
1035static sym_intent
1036match_intent_spec (void)
1037{
1038
1039 if (gfc_match (" ( in out )") == MATCH_YES)
1040 return INTENT_INOUT;
1041 if (gfc_match (" ( in )") == MATCH_YES)
1042 return INTENT_IN;
1043 if (gfc_match (" ( out )") == MATCH_YES)
1044 return INTENT_OUT;
1045
1046 gfc_error ("Bad INTENT specification at %C");
1047 return INTENT_UNKNOWN;
1048}
1049
1050
1051/* Matches a character length specification, which is either a
1052 specification expression, '*', or ':'. */
1053
1054static match
1055char_len_param_value (gfc_expr **expr, bool *deferred)
1056{
1057 match m;
1058
1059 *expr = NULL;
1060 *deferred = false;
1061
1062 if (gfc_match_char ('*') == MATCH_YES)
1063 return MATCH_YES;
1064
1065 if (gfc_match_char (':') == MATCH_YES)
1066 {
1067 if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C"))
1068 return MATCH_ERROR;
1069
1070 *deferred = true;
1071
1072 return MATCH_YES;
1073 }
1074
1075 m = gfc_match_expr (expr);
1076
1077 if (m == MATCH_NO || m == MATCH_ERROR)
1078 return m;
1079
1080 if (!gfc_expr_check_typed (*expr, gfc_current_ns, false))
1081 return MATCH_ERROR;
1082
1083 /* If gfortran gets an EXPR_OP, try to simplifiy it. This catches things
1084 like CHARACTER(([1])). */
1085 if ((*expr)->expr_type == EXPR_OP)
1086 gfc_simplify_expr (*expr, 1);
1087
1088 if ((*expr)->expr_type == EXPR_FUNCTION)
1089 {
1090 if ((*expr)->ts.type == BT_INTEGER
1091 || ((*expr)->ts.type == BT_UNKNOWN
1092 && strcmp((*expr)->symtree->name, "null") != 0))
1093 return MATCH_YES;
1094
1095 goto syntax;
1096 }
1097 else if ((*expr)->expr_type == EXPR_CONSTANT)
1098 {
1099 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
1100 processor dependent and its value is greater than or equal to zero.
1101 F2008, 4.4.3.2: If the character length parameter value evaluates
1102 to a negative value, the length of character entities declared
1103 is zero. */
1104
1105 if ((*expr)->ts.type == BT_INTEGER)
1106 {
1107 if (mpz_cmp_si ((*expr)->value.integer, 0) < 0)
1108 mpz_set_si ((*expr)->value.integer, 0);
1109 }
1110 else
1111 goto syntax;
1112 }
1113 else if ((*expr)->expr_type == EXPR_ARRAY)
1114 goto syntax;
1115 else if ((*expr)->expr_type == EXPR_VARIABLE)
1116 {
1117 bool t;
1118 gfc_expr *e;
1119
1120 e = gfc_copy_expr (*expr);
1121
1122 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
1123 which causes an ICE if gfc_reduce_init_expr() is called. */
1124 if (e->ref && e->ref->type == REF_ARRAY
1125 && e->ref->u.ar.type == AR_UNKNOWN
1126 && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE)
1127 goto syntax;
1128
1129 t = gfc_reduce_init_expr (e);
1130
1131 if (!t && e->ts.type == BT_UNKNOWN
1132 && e->symtree->n.sym->attr.untyped == 1
1133 && (flag_implicit_none
1134 || e->symtree->n.sym->ns->seen_implicit_none == 1
1135 || e->symtree->n.sym->ns->parent->seen_implicit_none == 1))
1136 {
1137 gfc_free_expr (e);
1138 goto syntax;
1139 }
1140
1141 if ((e->ref && e->ref->type == REF_ARRAY
1142 && e->ref->u.ar.type != AR_ELEMENT)
1143 || (!e->ref && e->expr_type == EXPR_ARRAY))
1144 {
1145 gfc_free_expr (e);
1146 goto syntax;
1147 }
1148
1149 gfc_free_expr (e);
1150 }
1151
1152 if (gfc_seen_div0)
1153 m = MATCH_ERROR;
1154
1155 return m;
1156
1157syntax:
1158 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where);
1159 return MATCH_ERROR;
1160}
1161
1162
1163/* A character length is a '*' followed by a literal integer or a
1164 char_len_param_value in parenthesis. */
1165
1166static match
1167match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
1168{
1169 int length;
1170 match m;
1171
1172 *deferred = false;
1173 m = gfc_match_char ('*');
1174 if (m != MATCH_YES)
1175 return m;
1176
1177 m = gfc_match_small_literal_int (&length, NULL);
1178 if (m == MATCH_ERROR)
1179 return m;
1180
1181 if (m == MATCH_YES)
1182 {
1183 if (obsolescent_check
1184 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
1185 return MATCH_ERROR;
1186 *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length);
1187 return m;
1188 }
1189
1190 if (gfc_match_char ('(') == MATCH_NO)
1191 goto syntax;
1192
1193 m = char_len_param_value (expr, deferred);
1194 if (m != MATCH_YES && gfc_matching_function)
1195 {
1196 gfc_undo_symbols ();
1197 m = MATCH_YES;
1198 }
1199
1200 if (m == MATCH_ERROR)
1201 return m;
1202 if (m == MATCH_NO)
1203 goto syntax;
1204
1205 if (gfc_match_char (')') == MATCH_NO)
1206 {
1207 gfc_free_expr (*expr);
1208 *expr = NULL;
1209 goto syntax;
1210 }
1211
1212 return MATCH_YES;
1213
1214syntax:
1215 gfc_error ("Syntax error in character length specification at %C");
1216 return MATCH_ERROR;
1217}
1218
1219
1220/* Special subroutine for finding a symbol. Check if the name is found
1221 in the current name space. If not, and we're compiling a function or
1222 subroutine and the parent compilation unit is an interface, then check
1223 to see if the name we've been given is the name of the interface
1224 (located in another namespace). */
1225
1226static int
1227find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
1228{
1229 gfc_state_data *s;
1230 gfc_symtree *st;
1231 int i;
1232
1233 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
1234 if (i == 0)
1235 {
1236 *result = st ? st->n.sym : NULL;
1237 goto end;
1238 }
1239
1240 if (gfc_current_state () != COMP_SUBROUTINE
1241 && gfc_current_state () != COMP_FUNCTION)
1242 goto end;
1243
1244 s = gfc_state_stack->previous;
1245 if (s == NULL)
1246 goto end;
1247
1248 if (s->state != COMP_INTERFACE)
1249 goto end;
1250 if (s->sym == NULL)
1251 goto end; /* Nameless interface. */
1252
1253 if (strcmp (name, s->sym->name) == 0)
1254 {
1255 *result = s->sym;
1256 return 0;
1257 }
1258
1259end:
1260 return i;
1261}
1262
1263
1264/* Special subroutine for getting a symbol node associated with a
1265 procedure name, used in SUBROUTINE and FUNCTION statements. The
1266 symbol is created in the parent using with symtree node in the
1267 child unit pointing to the symbol. If the current namespace has no
1268 parent, then the symbol is just created in the current unit. */
1269
1270static int
1271get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
1272{
1273 gfc_symtree *st;
1274 gfc_symbol *sym;
1275 int rc = 0;
1276
1277 /* Module functions have to be left in their own namespace because
1278 they have potentially (almost certainly!) already been referenced.
1279 In this sense, they are rather like external functions. This is
1280 fixed up in resolve.c(resolve_entries), where the symbol name-
1281 space is set to point to the master function, so that the fake
1282 result mechanism can work. */
1283 if (module_fcn_entry)
1284 {
1285 /* Present if entry is declared to be a module procedure. */
1286 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
1287
1288 if (*result == NULL)
1289 rc = gfc_get_symbol (name, NULL, result);
1290 else if (!gfc_get_symbol (name, NULL, &sym) && sym
1291 && (*result)->ts.type == BT_UNKNOWN
1292 && sym->attr.flavor == FL_UNKNOWN)
1293 /* Pick up the typespec for the entry, if declared in the function
1294 body. Note that this symbol is FL_UNKNOWN because it will
1295 only have appeared in a type declaration. The local symtree
1296 is set to point to the module symbol and a unique symtree
1297 to the local version. This latter ensures a correct clearing
1298 of the symbols. */
1299 {
1300 /* If the ENTRY proceeds its specification, we need to ensure
1301 that this does not raise a "has no IMPLICIT type" error. */
1302 if (sym->ts.type == BT_UNKNOWN)
1303 sym->attr.untyped = 1;
1304
1305 (*result)->ts = sym->ts;
1306
1307 /* Put the symbol in the procedure namespace so that, should
1308 the ENTRY precede its specification, the specification
1309 can be applied. */
1310 (*result)->ns = gfc_current_ns;
1311
1312 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
1313 st->n.sym = *result;
1314 st = gfc_get_unique_symtree (gfc_current_ns);
1315 sym->refs++;
1316 st->n.sym = sym;
1317 }
1318 }
1319 else
1320 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
1321
1322 if (rc)
1323 return rc;
1324
1325 sym = *result;
1326 if (sym->attr.proc == PROC_ST_FUNCTION)
1327 return rc;
1328
1329 if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY)
1330 {
1331 /* Create a partially populated interface symbol to carry the
1332 characteristics of the procedure and the result. */
1333 sym->tlink = gfc_new_symbol (name, sym->ns);
1334 gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus);
1335 gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL);
1336 if (sym->attr.dimension)
1337 sym->tlink->as = gfc_copy_array_spec (sym->as);
1338
1339 /* Ideally, at this point, a copy would be made of the formal
1340 arguments and their namespace. However, this does not appear
1341 to be necessary, albeit at the expense of not being able to
1342 use gfc_compare_interfaces directly. */
1343
1344 if (sym->result && sym->result != sym)
1345 {
1346 sym->tlink->result = sym->result;
1347 sym->result = NULL;
1348 }
1349 else if (sym->result)
1350 {
1351 sym->tlink->result = sym->tlink;
1352 }
1353 }
1354 else if (sym && !sym->gfc_new
1355 && gfc_current_state () != COMP_INTERFACE)
1356 {
1357 /* Trap another encompassed procedure with the same name. All
1358 these conditions are necessary to avoid picking up an entry
1359 whose name clashes with that of the encompassing procedure;
1360 this is handled using gsymbols to register unique, globally
1361 accessible names. */
1362 if (sym->attr.flavor != 0
1363 && sym->attr.proc != 0
1364 && (sym->attr.subroutine || sym->attr.function || sym->attr.entry)
1365 && sym->attr.if_source != IFSRC_UNKNOWN)
1366 {
1367 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1368 name, &sym->declared_at);
1369 return true;
1370 }
1371 if (sym->attr.flavor != 0
1372 && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN)
1373 {
1374 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1375 name, &sym->declared_at);
1376 return true;
1377 }
1378
1379 if (sym->attr.external && sym->attr.procedure
1380 && gfc_current_state () == COMP_CONTAINS)
1381 {
1382 gfc_error_now ("Contained procedure %qs at %C clashes with "
1383 "procedure defined at %L",
1384 name, &sym->declared_at);
1385 return true;
1386 }
1387
1388 /* Trap a procedure with a name the same as interface in the
1389 encompassing scope. */
1390 if (sym->attr.generic != 0
1391 && (sym->attr.subroutine || sym->attr.function)
1392 && !sym->attr.mod_proc)
1393 {
1394 gfc_error_now ("Name %qs at %C is already defined"
1395 " as a generic interface at %L",
1396 name, &sym->declared_at);
1397 return true;
1398 }
1399
1400 /* Trap declarations of attributes in encompassing scope. The
1401 signature for this is that ts.kind is nonzero for no-CLASS
1402 entity. For a CLASS entity, ts.kind is zero. */
1403 if ((sym->ts.kind != 0 || sym->ts.type == BT_CLASS)
1404 && !sym->attr.implicit_type
1405 && sym->attr.proc == 0
1406 && gfc_current_ns->parent != NULL
1407 && sym->attr.access == 0
1408 && !module_fcn_entry)
1409 {
1410 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1411 "from a previous declaration", name);
1412 return true;
1413 }
1414 }
1415
1416 /* C1246 (R1225) MODULE shall appear only in the function-stmt or
1417 subroutine-stmt of a module subprogram or of a nonabstract interface
1418 body that is declared in the scoping unit of a module or submodule. */
1419 if (sym->attr.external
1420 && (sym->attr.subroutine || sym->attr.function)
1421 && sym->attr.if_source == IFSRC_IFBODY
1422 && !current_attr.module_procedure
1423 && sym->attr.proc == PROC_MODULE
1424 && gfc_state_stack->state == COMP_CONTAINS)
1425 {
1426 gfc_error_now ("Procedure %qs defined in interface body at %L "
1427 "clashes with internal procedure defined at %C",
1428 name, &sym->declared_at);
1429 return true;
1430 }
1431
1432 if (sym && !sym->gfc_new
1433 && sym->attr.flavor != FL_UNKNOWN
1434 && sym->attr.referenced == 0 && sym->attr.subroutine == 1
1435 && gfc_state_stack->state == COMP_CONTAINS
1436 && gfc_state_stack->previous->state == COMP_SUBROUTINE)
1437 {
1438 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1439 name, &sym->declared_at);
1440 return true;
1441 }
1442
1443 if (gfc_current_ns->parent == NULL || *result == NULL)
1444 return rc;
1445
1446 /* Module function entries will already have a symtree in
1447 the current namespace but will need one at module level. */
1448 if (module_fcn_entry)
1449 {
1450 /* Present if entry is declared to be a module procedure. */
1451 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
1452 if (st == NULL)
1453 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
1454 }
1455 else
1456 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
1457
1458 st->n.sym = sym;
1459 sym->refs++;
1460
1461 /* See if the procedure should be a module procedure. */
1462
1463 if (((sym->ns->proc_name != NULL
1464 && sym->ns->proc_name->attr.flavor == FL_MODULE
1465 && sym->attr.proc != PROC_MODULE)
1466 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
1467 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
1468 rc = 2;
1469
1470 return rc;
1471}
1472
1473
1474/* Verify that the given symbol representing a parameter is C
1475 interoperable, by checking to see if it was marked as such after
1476 its declaration. If the given symbol is not interoperable, a
1477 warning is reported, thus removing the need to return the status to
1478 the calling function. The standard does not require the user use
1479 one of the iso_c_binding named constants to declare an
1480 interoperable parameter, but we can't be sure if the param is C
1481 interop or not if the user doesn't. For example, integer(4) may be
1482 legal Fortran, but doesn't have meaning in C. It may interop with
1483 a number of the C types, which causes a problem because the
1484 compiler can't know which one. This code is almost certainly not
1485 portable, and the user will get what they deserve if the C type
1486 across platforms isn't always interoperable with integer(4). If
1487 the user had used something like integer(c_int) or integer(c_long),
1488 the compiler could have automatically handled the varying sizes
1489 across platforms. */
1490
1491bool
1492gfc_verify_c_interop_param (gfc_symbol *sym)
1493{
1494 int is_c_interop = 0;
1495 bool retval = true;
1496
1497 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1498 Don't repeat the checks here. */
1499 if (sym->attr.implicit_type)
1500 return true;
1501
1502 /* For subroutines or functions that are passed to a BIND(C) procedure,
1503 they're interoperable if they're BIND(C) and their params are all
1504 interoperable. */
1505 if (sym->attr.flavor == FL_PROCEDURE)
1506 {
1507 if (sym->attr.is_bind_c == 0)
1508 {
1509 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1510 "attribute to be C interoperable", sym->name,
1511 &(sym->declared_at));
1512 return false;
1513 }
1514 else
1515 {
1516 if (sym->attr.is_c_interop == 1)
1517 /* We've already checked this procedure; don't check it again. */
1518 return true;
1519 else
1520 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1521 sym->common_block);
1522 }
1523 }
1524
1525 /* See if we've stored a reference to a procedure that owns sym. */
1526 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1527 {
1528 if (sym->ns->proc_name->attr.is_bind_c == 1)
1529 {
1530 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1531
1532 if (is_c_interop != 1)
1533 {
1534 /* Make personalized messages to give better feedback. */
1535 if (sym->ts.type == BT_DERIVED)
1536 gfc_error ("Variable %qs at %L is a dummy argument to the "
1537 "BIND(C) procedure %qs but is not C interoperable "
1538 "because derived type %qs is not C interoperable",
1539 sym->name, &(sym->declared_at),
1540 sym->ns->proc_name->name,
1541 sym->ts.u.derived->name);
1542 else if (sym->ts.type == BT_CLASS)
1543 gfc_error ("Variable %qs at %L is a dummy argument to the "
1544 "BIND(C) procedure %qs but is not C interoperable "
1545 "because it is polymorphic",
1546 sym->name, &(sym->declared_at),
1547 sym->ns->proc_name->name);
1548 else if (warn_c_binding_type)
1549 gfc_warning (OPT_Wc_binding_type,
1550 "Variable %qs at %L is a dummy argument of the "
1551 "BIND(C) procedure %qs but may not be C "
1552 "interoperable",
1553 sym->name, &(sym->declared_at),
1554 sym->ns->proc_name->name);
1555 }
1556
1557 /* Character strings are only C interoperable if they have a
1558 length of 1. */
1559 if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension)
1560 {
1561 gfc_charlen *cl = sym->ts.u.cl;
1562 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1563 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1564 {
1565 gfc_error ("Character argument %qs at %L "
1566 "must be length 1 because "
1567 "procedure %qs is BIND(C)",
1568 sym->name, &sym->declared_at,
1569 sym->ns->proc_name->name);
1570 retval = false;
1571 }
1572 }
1573
1574 /* We have to make sure that any param to a bind(c) routine does
1575 not have the allocatable, pointer, or optional attributes,
1576 according to J3/04-007, section 5.1. */
1577 if (sym->attr.allocatable == 1
1578 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1579 "ALLOCATABLE attribute in procedure %qs "
1580 "with BIND(C)", sym->name,
1581 &(sym->declared_at),
1582 sym->ns->proc_name->name))
1583 retval = false;
1584
1585 if (sym->attr.pointer == 1
1586 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with "
1587 "POINTER attribute in procedure %qs "
1588 "with BIND(C)", sym->name,
1589 &(sym->declared_at),
1590 sym->ns->proc_name->name))
1591 retval = false;
1592
1593 if (sym->attr.optional == 1 && sym->attr.value)
1594 {
1595 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1596 "and the VALUE attribute because procedure %qs "
1597 "is BIND(C)", sym->name, &(sym->declared_at),
1598 sym->ns->proc_name->name);
1599 retval = false;
1600 }
1601 else if (sym->attr.optional == 1
1602 && !gfc_notify_std (GFC_STD_F2018, "Variable %qs "
1603 "at %L with OPTIONAL attribute in "
1604 "procedure %qs which is BIND(C)",
1605 sym->name, &(sym->declared_at),
1606 sym->ns->proc_name->name))
1607 retval = false;
1608
1609 /* Make sure that if it has the dimension attribute, that it is
1610 either assumed size or explicit shape. Deferred shape is already
1611 covered by the pointer/allocatable attribute. */
1612 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1613 && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs "
1614 "at %L as dummy argument to the BIND(C) "
1615 "procedure %qs at %L", sym->name,
1616 &(sym->declared_at),
1617 sym->ns->proc_name->name,
1618 &(sym->ns->proc_name->declared_at)))
1619 retval = false;
1620 }
1621 }
1622
1623 return retval;
1624}
1625
1626
1627
1628/* Function called by variable_decl() that adds a name to the symbol table. */
1629
1630static bool
1631build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1632 gfc_array_spec **as, locus *var_locus)
1633{
1634 symbol_attribute attr;
1635 gfc_symbol *sym;
1636 int upper;
1637 gfc_symtree *st;
1638
1639 /* Symbols in a submodule are host associated from the parent module or
1640 submodules. Therefore, they can be overridden by declarations in the
1641 submodule scope. Deal with this by attaching the existing symbol to
1642 a new symtree and recycling the old symtree with a new symbol... */
1643 st = gfc_find_symtree (gfc_current_ns->sym_root, name);
1644 if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE
1645 && st->n.sym != NULL
1646 && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule)
1647 {
1648 gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns);
1649 s->n.sym = st->n.sym;
1650 sym = gfc_new_symbol (name, gfc_current_ns);
1651
1652
1653 st->n.sym = sym;
1654 sym->refs++;
1655 gfc_set_sym_referenced (sym);
1656 }
1657 /* ...Otherwise generate a new symtree and new symbol. */
1658 else if (gfc_get_symbol (name, NULL, &sym))
1659 return false;
1660
1661 /* Check if the name has already been defined as a type. The
1662 first letter of the symtree will be in upper case then. Of
1663 course, this is only necessary if the upper case letter is
1664 actually different. */
1665
1666 upper = TOUPPER(name[0]);
1667 if (upper != name[0])
1668 {
1669 char u_name[GFC_MAX_SYMBOL_LEN + 1];
1670 gfc_symtree *st;
1671
1672 gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN);
1673 strcpy (u_name, name);
1674 u_name[0] = upper;
1675
1676 st = gfc_find_symtree (gfc_current_ns->sym_root, u_name);
1677
1678 /* STRUCTURE types can alias symbol names */
1679 if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT)
1680 {
1681 gfc_error ("Symbol %qs at %C also declared as a type at %L", name,
1682 &st->n.sym->declared_at);
1683 return false;
1684 }
1685 }
1686
1687 /* Start updating the symbol table. Add basic type attribute if present. */
1688 if (current_ts.type != BT_UNKNOWN
1689 && (sym->attr.implicit_type == 0
1690 || !gfc_compare_types (&sym->ts, &current_ts))
1691 && !gfc_add_type (sym, &current_ts, var_locus))
1692 return false;
1693
1694 if (sym->ts.type == BT_CHARACTER)
1695 {
1696 sym->ts.u.cl = cl;
1697 sym->ts.deferred = cl_deferred;
1698 }
1699
1700 /* Add dimension attribute if present. */
1701 if (!gfc_set_array_spec (sym, *as, var_locus))
1702 return false;
1703 *as = NULL;
1704
1705 /* Add attribute to symbol. The copy is so that we can reset the
1706 dimension attribute. */
1707 attr = current_attr;
1708 attr.dimension = 0;
1709 attr.codimension = 0;
1710
1711 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1712 return false;
1713
1714 /* Finish any work that may need to be done for the binding label,
1715 if it's a bind(c). The bind(c) attr is found before the symbol
1716 is made, and before the symbol name (for data decls), so the
1717 current_ts is holding the binding label, or nothing if the
1718 name= attr wasn't given. Therefore, test here if we're dealing
1719 with a bind(c) and make sure the binding label is set correctly. */
1720 if (sym->attr.is_bind_c == 1)
1721 {
1722 if (!sym->binding_label)
1723 {
1724 /* Set the binding label and verify that if a NAME= was specified
1725 then only one identifier was in the entity-decl-list. */
1726 if (!set_binding_label (&sym->binding_label, sym->name,
1727 num_idents_on_line))
1728 return false;
1729 }
1730 }
1731
1732 /* See if we know we're in a common block, and if it's a bind(c)
1733 common then we need to make sure we're an interoperable type. */
1734 if (sym->attr.in_common == 1)
1735 {
1736 /* Test the common block object. */
1737 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1738 && sym->ts.is_c_interop != 1)
1739 {
1740 gfc_error_now ("Variable %qs in common block %qs at %C "
1741 "must be declared with a C interoperable "
1742 "kind since common block %qs is BIND(C)",
1743 sym->name, sym->common_block->name,
1744 sym->common_block->name);
1745 gfc_clear_error ();
1746 }
1747 }
1748
1749 sym->attr.implied_index = 0;
1750
1751 /* Use the parameter expressions for a parameterized derived type. */
1752 if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
1753 && sym->ts.u.derived->attr.pdt_type && type_param_spec_list)
1754 sym->param_list = gfc_copy_actual_arglist (type_param_spec_list);
1755
1756 if (sym->ts.type == BT_CLASS)
1757 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1758
1759 return true;
1760}
1761
1762
1763/* Set character constant to the given length. The constant will be padded or
1764 truncated. If we're inside an array constructor without a typespec, we
1765 additionally check that all elements have the same length; check_len -1
1766 means no checking. */
1767
1768void
1769gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr,
1770 gfc_charlen_t check_len)
1771{
1772 gfc_char_t *s;
1773 gfc_charlen_t slen;
1774
1775 if (expr->ts.type != BT_CHARACTER)
1776 return;
1777
1778 if (expr->expr_type != EXPR_CONSTANT)
1779 {
1780 gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where);
1781 return;
1782 }
1783
1784 slen = expr->value.character.length;
1785 if (len != slen)
1786 {
1787 s = gfc_get_wide_string (len + 1);
1788 memcpy (s, expr->value.character.string,
1789 MIN (len, slen) * sizeof (gfc_char_t));
1790 if (len > slen)
1791 gfc_wide_memset (&s[slen], ' ', len - slen);
1792
1793 if (warn_character_truncation && slen > len)
1794 gfc_warning_now (OPT_Wcharacter_truncation,
1795 "CHARACTER expression at %L is being truncated "
1796 "(%ld/%ld)", &expr->where,
1797 (long) slen, (long) len);
1798
1799 /* Apply the standard by 'hand' otherwise it gets cleared for
1800 initializers. */
1801 if (check_len != -1 && slen != check_len
1802 && !(gfc_option.allow_std & GFC_STD_GNU))
1803 gfc_error_now ("The CHARACTER elements of the array constructor "
1804 "at %L must have the same length (%ld/%ld)",
1805 &expr->where, (long) slen,
1806 (long) check_len);
1807
1808 s[len] = '\0';
1809 free (expr->value.character.string);
1810 expr->value.character.string = s;
1811 expr->value.character.length = len;
1812 /* If explicit representation was given, clear it
1813 as it is no longer needed after padding. */
1814 if (expr->representation.length)
1815 {
1816 expr->representation.length = 0;
1817 free (expr->representation.string);
1818 expr->representation.string = NULL;
1819 }
1820 }
1821}
1822
1823
1824/* Function to create and update the enumerator history
1825 using the information passed as arguments.
1826 Pointer "max_enum" is also updated, to point to
1827 enum history node containing largest initializer.
1828
1829 SYM points to the symbol node of enumerator.
1830 INIT points to its enumerator value. */
1831
1832static void
1833create_enum_history (gfc_symbol *sym, gfc_expr *init)
1834{
1835 enumerator_history *new_enum_history;
1836 gcc_assert (sym != NULL && init != NULL);
1837
1838 new_enum_history = XCNEW (enumerator_history);
1839
1840 new_enum_history->sym = sym;
1841 new_enum_history->initializer = init;
1842 new_enum_history->next = NULL;
1843
1844 if (enum_history == NULL)
1845 {
1846 enum_history = new_enum_history;
1847 max_enum = enum_history;
1848 }
1849 else
1850 {
1851 new_enum_history->next = enum_history;
1852 enum_history = new_enum_history;
1853
1854 if (mpz_cmp (max_enum->initializer->value.integer,
1855 new_enum_history->initializer->value.integer) < 0)
1856 max_enum = new_enum_history;
1857 }
1858}
1859
1860
1861/* Function to free enum kind history. */
1862
1863void
1864gfc_free_enum_history (void)
1865{
1866 enumerator_history *current = enum_history;
1867 enumerator_history *next;
1868
1869 while (current != NULL)
1870 {
1871 next = current->next;
1872 free (current);
1873 current = next;
1874 }
1875 max_enum = NULL;
1876 enum_history = NULL;
1877}
1878
1879
1880/* Function called by variable_decl() that adds an initialization
1881 expression to a symbol. */
1882
1883static bool
1884add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1885{
1886 symbol_attribute attr;
1887 gfc_symbol *sym;
1888 gfc_expr *init;
1889
1890 init = *initp;
1891 if (find_special (name, &sym, false))
1892 return false;
1893
1894 attr = sym->attr;
1895
1896 /* If this symbol is confirming an implicit parameter type,
1897 then an initialization expression is not allowed. */
1898 if (attr.flavor == FL_PARAMETER && sym->value != NULL)
1899 {
1900 if (*initp != NULL)
1901 {
1902 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1903 sym->name);
1904 return false;
1905 }
1906 else
1907 return true;
1908 }
1909
1910 if (init == NULL)
1911 {
1912 /* An initializer is required for PARAMETER declarations. */
1913 if (attr.flavor == FL_PARAMETER)
1914 {
1915 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1916 return false;
1917 }
1918 }
1919 else
1920 {
1921 /* If a variable appears in a DATA block, it cannot have an
1922 initializer. */
1923 if (sym->attr.data)
1924 {
1925 gfc_error ("Variable %qs at %C with an initializer already "
1926 "appears in a DATA statement", sym->name);
1927 return false;
1928 }
1929
1930 /* Check if the assignment can happen. This has to be put off
1931 until later for derived type variables and procedure pointers. */
1932 if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type)
1933 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1934 && !sym->attr.proc_pointer
1935 && !gfc_check_assign_symbol (sym, NULL, init))
1936 return false;
1937
1938 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1939 && init->ts.type == BT_CHARACTER)
1940 {
1941 /* Update symbol character length according initializer. */
1942 if (!gfc_check_assign_symbol (sym, NULL, init))
1943 return false;
1944
1945 if (sym->ts.u.cl->length == NULL)
1946 {
1947 gfc_charlen_t clen;
1948 /* If there are multiple CHARACTER variables declared on the
1949 same line, we don't want them to share the same length. */
1950 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1951
1952 if (sym->attr.flavor == FL_PARAMETER)
1953 {
1954 if (init->expr_type == EXPR_CONSTANT)
1955 {
1956 clen = init->value.character.length;
1957 sym->ts.u.cl->length
1958 = gfc_get_int_expr (gfc_charlen_int_kind,
1959 NULL, clen);
1960 }
1961 else if (init->expr_type == EXPR_ARRAY)
1962 {
1963 if (init->ts.u.cl && init->ts.u.cl->length)
1964 {
1965 const gfc_expr *length = init->ts.u.cl->length;
1966 if (length->expr_type != EXPR_CONSTANT)
1967 {
1968 gfc_error ("Cannot initialize parameter array "
1969 "at %L "
1970 "with variable length elements",
1971 &sym->declared_at);
1972 return false;
1973 }
1974 clen = mpz_get_si (length->value.integer);
1975 }
1976 else if (init->value.constructor)
1977 {
1978 gfc_constructor *c;
1979 c = gfc_constructor_first (init->value.constructor);
1980 clen = c->expr->value.character.length;
1981 }
1982 else
1983 gcc_unreachable ();
1984 sym->ts.u.cl->length
1985 = gfc_get_int_expr (gfc_charlen_int_kind,
1986 NULL, clen);
1987 }
1988 else if (init->ts.u.cl && init->ts.u.cl->length)
1989 sym->ts.u.cl->length =
1990 gfc_copy_expr (init->ts.u.cl->length);
1991 }
1992 }
1993 /* Update initializer character length according symbol. */
1994 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1995 {
1996 if (!gfc_specification_expr (sym->ts.u.cl->length))
1997 return false;
1998
1999 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind,
2000 false);
2001 /* resolve_charlen will complain later on if the length
2002 is too large. Just skeep the initialization in that case. */
2003 if (mpz_cmp (sym->ts.u.cl->length->value.integer,
2004 gfc_integer_kinds[k].huge) <= 0)
2005 {
2006 HOST_WIDE_INT len
2007 = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer);
2008
2009 if (init->expr_type == EXPR_CONSTANT)
2010 gfc_set_constant_character_len (len, init, -1);
2011 else if (init->expr_type == EXPR_ARRAY)
2012 {
2013 gfc_constructor *c;
2014
2015 /* Build a new charlen to prevent simplification from
2016 deleting the length before it is resolved. */
2017 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2018 init->ts.u.cl->length
2019 = gfc_copy_expr (sym->ts.u.cl->length);
2020
2021 for (c = gfc_constructor_first (init->value.constructor);
2022 c; c = gfc_constructor_next (c))
2023 gfc_set_constant_character_len (len, c->expr, -1);
2024 }
2025 }
2026 }
2027 }
2028
2029 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension && sym->as
2030 && sym->as->rank && init->rank && init->rank != sym->as->rank)
2031 {
2032 gfc_error ("Rank mismatch of array at %L and its initializer "
2033 "(%d/%d)", &sym->declared_at, sym->as->rank, init->rank);
2034 return false;
2035 }
2036
2037 /* If sym is implied-shape, set its upper bounds from init. */
2038 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2039 && sym->as->type == AS_IMPLIED_SHAPE)
2040 {
2041 int dim;
2042
2043 if (init->rank == 0)
2044 {
2045 gfc_error ("Cannot initialize implied-shape array at %L"
2046 " with scalar", &sym->declared_at);
2047 return false;
2048 }
2049
2050 /* The shape may be NULL for EXPR_ARRAY, set it. */
2051 if (init->shape == NULL)
2052 {
2053 gcc_assert (init->expr_type == EXPR_ARRAY);
2054 init->shape = gfc_get_shape (1);
2055 if (!gfc_array_size (init, &init->shape[0]))
2056 gfc_internal_error ("gfc_array_size failed");
2057 }
2058
2059 for (dim = 0; dim < sym->as->rank; ++dim)
2060 {
2061 int k;
2062 gfc_expr *e, *lower;
2063
2064 lower = sym->as->lower[dim];
2065
2066 /* If the lower bound is an array element from another
2067 parameterized array, then it is marked with EXPR_VARIABLE and
2068 is an initialization expression. Try to reduce it. */
2069 if (lower->expr_type == EXPR_VARIABLE)
2070 gfc_reduce_init_expr (lower);
2071
2072 if (lower->expr_type == EXPR_CONSTANT)
2073 {
2074 /* All dimensions must be without upper bound. */
2075 gcc_assert (!sym->as->upper[dim]);
2076
2077 k = lower->ts.kind;
2078 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
2079 mpz_add (e->value.integer, lower->value.integer,
2080 init->shape[dim]);
2081 mpz_sub_ui (e->value.integer, e->value.integer, 1);
2082 sym->as->upper[dim] = e;
2083 }
2084 else
2085 {
2086 gfc_error ("Non-constant lower bound in implied-shape"
2087 " declaration at %L", &lower->where);
2088 return false;
2089 }
2090 }
2091
2092 sym->as->type = AS_EXPLICIT;
2093 }
2094
2095 /* Ensure that explicit bounds are simplified. */
2096 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
2097 && sym->as->type == AS_EXPLICIT)
2098 {
2099 for (int dim = 0; dim < sym->as->rank; ++dim)
2100 {
2101 gfc_expr *e;
2102
2103 e = sym->as->lower[dim];
2104 if (e->expr_type != EXPR_CONSTANT)
2105 gfc_reduce_init_expr (e);
2106
2107 e = sym->as->upper[dim];
2108 if (e->expr_type != EXPR_CONSTANT)
2109 gfc_reduce_init_expr (e);
2110 }
2111 }
2112
2113 /* Need to check if the expression we initialized this
2114 to was one of the iso_c_binding named constants. If so,
2115 and we're a parameter (constant), let it be iso_c.
2116 For example:
2117 integer(c_int), parameter :: my_int = c_int
2118 integer(my_int) :: my_int_2
2119 If we mark my_int as iso_c (since we can see it's value
2120 is equal to one of the named constants), then my_int_2
2121 will be considered C interoperable. */
2122 if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type))
2123 {
2124 sym->ts.is_iso_c |= init->ts.is_iso_c;
2125 sym->ts.is_c_interop |= init->ts.is_c_interop;
2126 /* attr bits needed for module files. */
2127 sym->attr.is_iso_c |= init->ts.is_iso_c;
2128 sym->attr.is_c_interop |= init->ts.is_c_interop;
2129 if (init->ts.is_iso_c)
2130 sym->ts.f90_type = init->ts.f90_type;
2131 }
2132
2133 /* Add initializer. Make sure we keep the ranks sane. */
2134 if (sym->attr.dimension && init->rank == 0)
2135 {
2136 mpz_t size;
2137 gfc_expr *array;
2138 int n;
2139 if (sym->attr.flavor == FL_PARAMETER
2140 && gfc_is_constant_expr (init)
2141 && (init->expr_type == EXPR_CONSTANT
2142 || init->expr_type == EXPR_STRUCTURE)
2143 && spec_size (sym->as, &size)
2144 && mpz_cmp_si (size, 0) > 0)
2145 {
2146 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
2147 &init->where);
2148 if (init->ts.type == BT_DERIVED)
2149 array->ts.u.derived = init->ts.u.derived;
2150 for (n = 0; n < (int)mpz_get_si (size); n++)
2151 gfc_constructor_append_expr (&array->value.constructor,
2152 n == 0
2153 ? init
2154 : gfc_copy_expr (init),
2155 &init->where);
2156
2157 array->shape = gfc_get_shape (sym->as->rank);
2158 for (n = 0; n < sym->as->rank; n++)
2159 spec_dimen_size (sym->as, n, &array->shape[n]);
2160
2161 init = array;
2162 mpz_clear (size);
2163 }
2164 init->rank = sym->as->rank;
2165 }
2166
2167 sym->value = init;
2168 if (sym->attr.save == SAVE_NONE)
2169 sym->attr.save = SAVE_IMPLICIT;
2170 *initp = NULL;
2171 }
2172
2173 return true;
2174}
2175
2176
2177/* Function called by variable_decl() that adds a name to a structure
2178 being built. */
2179
2180static bool
2181build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
2182 gfc_array_spec **as)
2183{
2184 gfc_state_data *s;
2185 gfc_component *c;
2186
2187 /* F03:C438/C439. If the current symbol is of the same derived type that we're
2188 constructing, it must have the pointer attribute. */
2189 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
2190 && current_ts.u.derived == gfc_current_block ()
2191 && current_attr.pointer == 0)
2192 {
2193 if (current_attr.allocatable
2194 && !gfc_notify_std(GFC_STD_F2008, "Component at %C "
2195 "must have the POINTER attribute"))
2196 {
2197 return false;
2198 }
2199 else if (current_attr.allocatable == 0)
2200 {
2201 gfc_error ("Component at %C must have the POINTER attribute");
2202 return false;
2203 }
2204 }
2205
2206 /* F03:C437. */
2207 if (current_ts.type == BT_CLASS
2208 && !(current_attr.pointer || current_attr.allocatable))
2209 {
2210 gfc_error ("Component %qs with CLASS at %C must be allocatable "
2211 "or pointer", name);
2212 return false;
2213 }
2214
2215 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
2216 {
2217 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
2218 {
2219 gfc_error ("Array component of structure at %C must have explicit "
2220 "or deferred shape");
2221 return false;
2222 }
2223 }
2224
2225 /* If we are in a nested union/map definition, gfc_add_component will not
2226 properly find repeated components because:
2227 (i) gfc_add_component does a flat search, where components of unions
2228 and maps are implicity chained so nested components may conflict.
2229 (ii) Unions and maps are not linked as components of their parent
2230 structures until after they are parsed.
2231 For (i) we use gfc_find_component which searches recursively, and for (ii)
2232 we search each block directly from the parse stack until we find the top
2233 level structure. */
2234
2235 s = gfc_state_stack;
2236 if (s->state == COMP_UNION || s->state == COMP_MAP)
2237 {
2238 while (s->state == COMP_UNION || gfc_comp_struct (s->state))
2239 {
2240 c = gfc_find_component (s->sym, name, true, true, NULL);
2241 if (c != NULL)
2242 {
2243 gfc_error_now ("Component %qs at %C already declared at %L",
2244 name, &c->loc);
2245 return false;
2246 }
2247 /* Break after we've searched the entire chain. */
2248 if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE)
2249 break;
2250 s = s->previous;
2251 }
2252 }
2253
2254 if (!gfc_add_component (gfc_current_block(), name, &c))
2255 return false;
2256
2257 c->ts = current_ts;
2258 if (c->ts.type == BT_CHARACTER)
2259 c->ts.u.cl = cl;
2260
2261 if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED
2262 && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER)
2263 && saved_kind_expr != NULL)
2264 c->kind_expr = gfc_copy_expr (saved_kind_expr);
2265
2266 c->attr = current_attr;
2267
2268 c->initializer = *init;
2269 *init = NULL;
2270
2271 c->as = *as;
2272 if (c->as != NULL)
2273 {
2274 if (c->as->corank)
2275 c->attr.codimension = 1;
2276 if (c->as->rank)
2277 c->attr.dimension = 1;
2278 }
2279 *as = NULL;
2280
2281 gfc_apply_init (&c->ts, &c->attr, c->initializer);
2282
2283 /* Check array components. */
2284 if (!c->attr.dimension)
2285 goto scalar;
2286
2287 if (c->attr.pointer)
2288 {
2289 if (c->as->type != AS_DEFERRED)
2290 {
2291 gfc_error ("Pointer array component of structure at %C must have a "
2292 "deferred shape");
2293 return false;
2294 }
2295 }
2296 else if (c->attr.allocatable)
2297 {
2298 if (c->as->type != AS_DEFERRED)
2299 {
2300 gfc_error ("Allocatable component of structure at %C must have a "
2301 "deferred shape");
2302 return false;
2303 }
2304 }
2305 else
2306 {
2307 if (c->as->type != AS_EXPLICIT)
2308 {
2309 gfc_error ("Array component of structure at %C must have an "
2310 "explicit shape");
2311 return false;
2312 }
2313 }
2314
2315scalar:
2316 if (c->ts.type == BT_CLASS)
2317 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
2318
2319 if (c->attr.pdt_kind || c->attr.pdt_len)
2320 {
2321 gfc_symbol *sym;
2322 gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived,
2323 0, &sym);
2324 if (sym == NULL)
2325 {
2326 gfc_error ("Type parameter %qs at %C has no corresponding entry "
2327 "in the type parameter name list at %L",
2328 c->name, &gfc_current_block ()->declared_at);
2329 return false;
2330 }
2331 sym->ts = c->ts;
2332 sym->attr.pdt_kind = c->attr.pdt_kind;
2333 sym->attr.pdt_len = c->attr.pdt_len;
2334 if (c->initializer)
2335 sym->value = gfc_copy_expr (c->initializer);
2336 sym->attr.flavor = FL_VARIABLE;
2337 }
2338
2339 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2340 && c->ts.u.derived && c->ts.u.derived->attr.pdt_template
2341 && decl_type_param_list)
2342 c->param_list = gfc_copy_actual_arglist (decl_type_param_list);
2343
2344 return true;
2345}
2346
2347
2348/* Match a 'NULL()', and possibly take care of some side effects. */
2349
2350match
2351gfc_match_null (gfc_expr **result)
2352{
2353 gfc_symbol *sym;
2354 match m, m2 = MATCH_NO;
2355
2356 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
2357 return MATCH_ERROR;
2358
2359 if (m == MATCH_NO)
2360 {
2361 locus old_loc;
2362 char name[GFC_MAX_SYMBOL_LEN + 1];
2363
2364 if ((m2 = gfc_match (" null (")) != MATCH_YES)
2365 return m2;
2366
2367 old_loc = gfc_current_locus;
2368 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
2369 return MATCH_ERROR;
2370 if (m2 != MATCH_YES
2371 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
2372 return MATCH_ERROR;
2373 if (m2 == MATCH_NO)
2374 {
2375 gfc_current_locus = old_loc;
2376 return MATCH_NO;
2377 }
2378 }
2379
2380 /* The NULL symbol now has to be/become an intrinsic function. */
2381 if (gfc_get_symbol ("null", NULL, &sym))
2382 {
2383 gfc_error ("NULL() initialization at %C is ambiguous");
2384 return MATCH_ERROR;
2385 }
2386
2387 gfc_intrinsic_symbol (sym);
2388
2389 if (sym->attr.proc != PROC_INTRINSIC
2390 && !(sym->attr.use_assoc && sym->attr.intrinsic)
2391 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
2392 || !gfc_add_function (&sym->attr, sym->name, NULL)))
2393 return MATCH_ERROR;
2394
2395 *result = gfc_get_null_expr (&gfc_current_locus);
2396
2397 /* Invalid per F2008, C512. */
2398 if (m2 == MATCH_YES)
2399 {
2400 gfc_error ("NULL() initialization at %C may not have MOLD");
2401 return MATCH_ERROR;
2402 }
2403
2404 return MATCH_YES;
2405}
2406
2407
2408/* Match the initialization expr for a data pointer or procedure pointer. */
2409
2410static match
2411match_pointer_init (gfc_expr **init, int procptr)
2412{
2413 match m;
2414
2415 if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state))
2416 {
2417 gfc_error ("Initialization of pointer at %C is not allowed in "
2418 "a PURE procedure");
2419 return MATCH_ERROR;
2420 }
2421 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2422
2423 /* Match NULL() initialization. */
2424 m = gfc_match_null (init);
2425 if (m != MATCH_NO)
2426 return m;
2427
2428 /* Match non-NULL initialization. */
2429 gfc_matching_ptr_assignment = !procptr;
2430 gfc_matching_procptr_assignment = procptr;
2431 m = gfc_match_rvalue (init);
2432 gfc_matching_ptr_assignment = 0;
2433 gfc_matching_procptr_assignment = 0;
2434 if (m == MATCH_ERROR)
2435 return MATCH_ERROR;
2436 else if (m == MATCH_NO)
2437 {
2438 gfc_error ("Error in pointer initialization at %C");
2439 return MATCH_ERROR;
2440 }
2441
2442 if (!procptr && !gfc_resolve_expr (*init))
2443 return MATCH_ERROR;
2444
2445 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
2446 "initialization at %C"))
2447 return MATCH_ERROR;
2448
2449 return MATCH_YES;
2450}
2451
2452
2453static bool
2454check_function_name (char *name)
2455{
2456 /* In functions that have a RESULT variable defined, the function name always
2457 refers to function calls. Therefore, the name is not allowed to appear in
2458 specification statements. When checking this, be careful about
2459 'hidden' procedure pointer results ('ppr@'). */
2460
2461 if (gfc_current_state () == COMP_FUNCTION)
2462 {
2463 gfc_symbol *block = gfc_current_block ();
2464 if (block && block->result && block->result != block
2465 && strcmp (block->result->name, "ppr@") != 0
2466 && strcmp (block->name, name) == 0)
2467 {
2468 gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C "
2469 "from appearing in a specification statement",
2470 block->result->name, &block->result->declared_at, name);
2471 return false;
2472 }
2473 }
2474
2475 return true;
2476}
2477
2478
2479/* Match a variable name with an optional initializer. When this
2480 subroutine is called, a variable is expected to be parsed next.
2481 Depending on what is happening at the moment, updates either the
2482 symbol table or the current interface. */
2483
2484static match
2485variable_decl (int elem)
2486{
2487 char name[GFC_MAX_SYMBOL_LEN + 1];
2488 static unsigned int fill_id = 0;
2489 gfc_expr *initializer, *char_len;
2490 gfc_array_spec *as;
2491 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
2492 gfc_charlen *cl;
2493 bool cl_deferred;
2494 locus var_locus;
2495 match m;
2496 bool t;
2497 gfc_symbol *sym;
2498 char c;
2499
2500 initializer = NULL;
2501 as = NULL;
2502 cp_as = NULL;
2503
2504 /* When we get here, we've just matched a list of attributes and
2505 maybe a type and a double colon. The next thing we expect to see
2506 is the name of the symbol. */
2507
2508 /* If we are parsing a structure with legacy support, we allow the symbol
2509 name to be '%FILL' which gives it an anonymous (inaccessible) name. */
2510 m = MATCH_NO;
2511 gfc_gobble_whitespace ();
2512 c = gfc_peek_ascii_char ();
2513 if (c == '%')
2514 {
2515 gfc_next_ascii_char (); /* Burn % character. */
2516 m = gfc_match ("fill");
2517 if (m == MATCH_YES)
2518 {
2519 if (gfc_current_state () != COMP_STRUCTURE)
2520 {
2521 if (flag_dec_structure)
2522 gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL");
2523 else
2524 gfc_error ("%qs at %C is a DEC extension, enable with "
2525 "%<-fdec-structure%>", "%FILL");
2526 m = MATCH_ERROR;
2527 goto cleanup;
2528 }
2529
2530 if (attr_seen)
2531 {
2532 gfc_error ("%qs entity cannot have attributes at %C", "%FILL");
2533 m = MATCH_ERROR;
2534 goto cleanup;
2535 }
2536
2537 /* %FILL components are given invalid fortran names. */
2538 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++);
2539 }
2540 else
2541 {
2542 gfc_error ("Invalid character %qc in variable name at %C", c);
2543 return MATCH_ERROR;
2544 }
2545 }
2546 else
2547 {
2548 m = gfc_match_name (name);
2549 if (m != MATCH_YES)
2550 goto cleanup;
2551 }
2552
2553 var_locus = gfc_current_locus;
2554
2555 /* Now we could see the optional array spec. or character length. */
2556 m = gfc_match_array_spec (&as, true, true);
2557 if (m == MATCH_ERROR)
2558 goto cleanup;
2559
2560 if (m == MATCH_NO)
2561 as = gfc_copy_array_spec (current_as);
2562 else if (current_as
2563 && !merge_array_spec (current_as, as, true))
2564 {
2565 m = MATCH_ERROR;
2566 goto cleanup;
2567 }
2568
2569 if (flag_cray_pointer)
2570 cp_as = gfc_copy_array_spec (as);
2571
2572 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2573 determine (and check) whether it can be implied-shape. If it
2574 was parsed as assumed-size, change it because PARAMETERs cannot
2575 be assumed-size.
2576
2577 An explicit-shape-array cannot appear under several conditions.
2578 That check is done here as well. */
2579 if (as)
2580 {
2581 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
2582 {
2583 m = MATCH_ERROR;
2584 gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape",
2585 name, &var_locus);
2586 goto cleanup;
2587 }
2588
2589 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
2590 && current_attr.flavor == FL_PARAMETER)
2591 as->type = AS_IMPLIED_SHAPE;
2592
2593 if (as->type == AS_IMPLIED_SHAPE
2594 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
2595 &var_locus))
2596 {
2597 m = MATCH_ERROR;
2598 goto cleanup;
2599 }
2600
2601 gfc_seen_div0 = false;
2602
2603 /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not
2604 constant expressions shall appear only in a subprogram, derived
2605 type definition, BLOCK construct, or interface body. */
2606 if (as->type == AS_EXPLICIT
2607 && gfc_current_state () != COMP_BLOCK
2608 && gfc_current_state () != COMP_DERIVED
2609 && gfc_current_state () != COMP_FUNCTION
2610 && gfc_current_state () != COMP_INTERFACE
2611 && gfc_current_state () != COMP_SUBROUTINE)
2612 {
2613 gfc_expr *e;
2614 bool not_constant = false;
2615
2616 for (int i = 0; i < as->rank; i++)
2617 {
2618 e = gfc_copy_expr (as->lower[i]);
2619 if (!gfc_resolve_expr (e) && gfc_seen_div0)
2620 {
2621 m = MATCH_ERROR;
2622 goto cleanup;
2623 }
2624
2625 gfc_simplify_expr (e, 0);
2626 if (e && (e->expr_type != EXPR_CONSTANT))
2627 {
2628 not_constant = true;
2629 break;
2630 }
2631 gfc_free_expr (e);
2632
2633 e = gfc_copy_expr (as->upper[i]);
2634 if (!gfc_resolve_expr (e) && gfc_seen_div0)
2635 {
2636 m = MATCH_ERROR;
2637 goto cleanup;
2638 }
2639
2640 gfc_simplify_expr (e, 0);
2641 if (e && (e->expr_type != EXPR_CONSTANT))
2642 {
2643 not_constant = true;
2644 break;
2645 }
2646 gfc_free_expr (e);
2647 }
2648
2649 if (not_constant && e->ts.type != BT_INTEGER)
2650 {
2651 gfc_error ("Explicit array shape at %C must be constant of "
2652 "INTEGER type and not %s type",
2653 gfc_basic_typename (e->ts.type));
2654 m = MATCH_ERROR;
2655 goto cleanup;
2656 }
2657 if (not_constant)
2658 {
2659 gfc_error ("Explicit shaped array with nonconstant bounds at %C");
2660 m = MATCH_ERROR;
2661 goto cleanup;
2662 }
2663 }
2664 if (as->type == AS_EXPLICIT)
2665 {
2666 for (int i = 0; i < as->rank; i++)
2667 {
2668 gfc_expr *e, *n;
2669 e = as->lower[i];
2670 if (e->expr_type != EXPR_CONSTANT)
2671 {
2672 n = gfc_copy_expr (e);
2673 if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2674 {
2675 m = MATCH_ERROR;
2676 goto cleanup;
2677 }
2678
2679 if (n->expr_type == EXPR_CONSTANT)
2680 gfc_replace_expr (e, n);
2681 else
2682 gfc_free_expr (n);
2683 }
2684 e = as->upper[i];
2685 if (e->expr_type != EXPR_CONSTANT)
2686 {
2687 n = gfc_copy_expr (e);
2688 if (!gfc_simplify_expr (n, 1) && gfc_seen_div0)
2689 {
2690 m = MATCH_ERROR;
2691 goto cleanup;
2692 }
2693
2694 if (n->expr_type == EXPR_CONSTANT)
2695 gfc_replace_expr (e, n);
2696 else
2697 gfc_free_expr (n);
2698 }
2699 }
2700 }
2701 }
2702
2703 char_len = NULL;
2704 cl = NULL;
2705 cl_deferred = false;
2706
2707 if (current_ts.type == BT_CHARACTER)
2708 {
2709 switch (match_char_length (&char_len, &cl_deferred, false))
2710 {
2711 case MATCH_YES:
2712 cl = gfc_new_charlen (gfc_current_ns, NULL);
2713
2714 cl->length = char_len;
2715 break;
2716
2717 /* Non-constant lengths need to be copied after the first
2718 element. Also copy assumed lengths. */
2719 case MATCH_NO:
2720 if (elem > 1
2721 && (current_ts.u.cl->length == NULL
2722 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
2723 {
2724 cl = gfc_new_charlen (gfc_current_ns, NULL);
2725 cl->length = gfc_copy_expr (current_ts.u.cl->length);
2726 }
2727 else
2728 cl = current_ts.u.cl;
2729
2730 cl_deferred = current_ts.deferred;
2731
2732 break;
2733
2734 case MATCH_ERROR:
2735 goto cleanup;
2736 }
2737 }
2738
2739 /* The dummy arguments and result of the abreviated form of MODULE
2740 PROCEDUREs, used in SUBMODULES should not be redefined. */
2741 if (gfc_current_ns->proc_name
2742 && gfc_current_ns->proc_name->abr_modproc_decl)
2743 {
2744 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
2745 if (sym != NULL && (sym->attr.dummy || sym->attr.result))
2746 {
2747 m = MATCH_ERROR;
2748 gfc_error ("%qs at %C is a redefinition of the declaration "
2749 "in the corresponding interface for MODULE "
2750 "PROCEDURE %qs", sym->name,
2751 gfc_current_ns->proc_name->name);
2752 goto cleanup;
2753 }
2754 }
2755
2756 /* %FILL components may not have initializers. */
2757 if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES)
2758 {
2759 gfc_error ("%qs entity cannot have an initializer at %C", "%FILL");
2760 m = MATCH_ERROR;
2761 goto cleanup;
2762 }
2763
2764 /* If this symbol has already shown up in a Cray Pointer declaration,
2765 and this is not a component declaration,
2766 then we want to set the type & bail out. */
2767 if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ()))
2768 {
2769 gfc_find_symbol (name, gfc_current_ns, 0, &sym);
2770 if (sym != NULL && sym->attr.cray_pointee)
2771 {
2772 m = MATCH_YES;
2773 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
2774 {
2775 m = MATCH_ERROR;
2776 goto cleanup;
2777 }
2778
2779 /* Check to see if we have an array specification. */
2780 if (cp_as != NULL)
2781 {
2782 if (sym->as != NULL)
2783 {
2784 gfc_error ("Duplicate array spec for Cray pointee at %C");
2785 gfc_free_array_spec (cp_as);
2786 m = MATCH_ERROR;
2787 goto cleanup;
2788 }
2789 else
2790 {
2791 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
2792 gfc_internal_error ("Cannot set pointee array spec.");
2793
2794 /* Fix the array spec. */
2795 m = gfc_mod_pointee_as (sym->as);
2796 if (m == MATCH_ERROR)
2797 goto cleanup;
2798 }
2799 }
2800 goto cleanup;
2801 }
2802 else
2803 {
2804 gfc_free_array_spec (cp_as);
2805 }
2806 }
2807
2808 /* Procedure pointer as function result. */
2809 if (gfc_current_state () == COMP_FUNCTION
2810 && strcmp ("ppr@", gfc_current_block ()->name) == 0
2811 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
2812 strcpy (name, "ppr@");
2813
2814 if (gfc_current_state () == COMP_FUNCTION
2815 && strcmp (name, gfc_current_block ()->name) == 0
2816 && gfc_current_block ()->result
2817 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
2818 strcpy (name, "ppr@");
2819
2820 /* OK, we've successfully matched the declaration. Now put the
2821 symbol in the current namespace, because it might be used in the
2822 optional initialization expression for this symbol, e.g. this is
2823 perfectly legal:
2824
2825 integer, parameter :: i = huge(i)
2826
2827 This is only true for parameters or variables of a basic type.
2828 For components of derived types, it is not true, so we don't
2829 create a symbol for those yet. If we fail to create the symbol,
2830 bail out. */
2831 if (!gfc_comp_struct (gfc_current_state ())
2832 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
2833 {
2834 m = MATCH_ERROR;
2835 goto cleanup;
2836 }
2837
2838 if (!check_function_name (name))
2839 {
2840 m = MATCH_ERROR;
2841 goto cleanup;
2842 }
2843
2844 /* We allow old-style initializations of the form
2845 integer i /2/, j(4) /3*3, 1/
2846 (if no colon has been seen). These are different from data
2847 statements in that initializers are only allowed to apply to the
2848 variable immediately preceding, i.e.
2849 integer i, j /1, 2/
2850 is not allowed. Therefore we have to do some work manually, that
2851 could otherwise be left to the matchers for DATA statements. */
2852
2853 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2854 {
2855 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2856 "initialization at %C"))
2857 return MATCH_ERROR;
2858
2859 /* Allow old style initializations for components of STRUCTUREs and MAPs
2860 but not components of derived types. */
2861 else if (gfc_current_state () == COMP_DERIVED)
2862 {
2863 gfc_error ("Invalid old style initialization for derived type "
2864 "component at %C");
2865 m = MATCH_ERROR;
2866 goto cleanup;
2867 }
2868
2869 /* For structure components, read the initializer as a special
2870 expression and let the rest of this function apply the initializer
2871 as usual. */
2872 else if (gfc_comp_struct (gfc_current_state ()))
2873 {
2874 m = match_clist_expr (&initializer, &current_ts, as);
2875 if (m == MATCH_NO)
2876 gfc_error ("Syntax error in old style initialization of %s at %C",
2877 name);
2878 if (m != MATCH_YES)
2879 goto cleanup;
2880 }
2881
2882 /* Otherwise we treat the old style initialization just like a
2883 DATA declaration for the current variable. */
2884 else
2885 return match_old_style_init (name);
2886 }
2887
2888 /* The double colon must be present in order to have initializers.
2889 Otherwise the statement is ambiguous with an assignment statement. */
2890 if (colon_seen)
2891 {
2892 if (gfc_match (" =>") == MATCH_YES)
2893 {
2894 if (!current_attr.pointer)
2895 {
2896 gfc_error ("Initialization at %C isn't for a pointer variable");
2897 m = MATCH_ERROR;
2898 goto cleanup;
2899 }
2900
2901 m = match_pointer_init (&initializer, 0);
2902 if (m != MATCH_YES)
2903 goto cleanup;
2904
2905 /* The target of a pointer initialization must have the SAVE
2906 attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope
2907 is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */
2908 if (initializer->expr_type == EXPR_VARIABLE
2909 && initializer->symtree->n.sym->attr.save == SAVE_NONE
2910 && (gfc_current_state () == COMP_PROGRAM
2911 || gfc_current_state () == COMP_MODULE
2912 || gfc_current_state () == COMP_SUBMODULE))
2913 initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
2914 }
2915 else if (gfc_match_char ('=') == MATCH_YES)
2916 {
2917 if (current_attr.pointer)
2918 {
2919 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2920 "not %<=%>");
2921 m = MATCH_ERROR;
2922 goto cleanup;
2923 }
2924
2925 m = gfc_match_init_expr (&initializer);
2926 if (m == MATCH_NO)
2927 {
2928 gfc_error ("Expected an initialization expression at %C");
2929 m = MATCH_ERROR;
2930 }
2931
2932 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2933 && !gfc_comp_struct (gfc_state_stack->state))
2934 {
2935 gfc_error ("Initialization of variable at %C is not allowed in "
2936 "a PURE procedure");
2937 m = MATCH_ERROR;
2938 }
2939
2940 if (current_attr.flavor != FL_PARAMETER
2941 && !gfc_comp_struct (gfc_state_stack->state))
2942 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2943
2944 if (m != MATCH_YES)
2945 goto cleanup;
2946 }
2947 }
2948
2949 if (initializer != NULL && current_attr.allocatable
2950 && gfc_comp_struct (gfc_current_state ()))
2951 {
2952 gfc_error ("Initialization of allocatable component at %C is not "
2953 "allowed");
2954 m = MATCH_ERROR;
2955 goto cleanup;
2956 }
2957
2958 if (gfc_current_state () == COMP_DERIVED
2959 && initializer && initializer->ts.type == BT_HOLLERITH)
2960 {
2961 gfc_error ("Initialization of structure component with a HOLLERITH "
2962 "constant at %L is not allowed", &initializer->where);
2963 m = MATCH_ERROR;
2964 goto cleanup;
2965 }
2966
2967 if (gfc_current_state () == COMP_DERIVED
2968 && gfc_current_block ()->attr.pdt_template)
2969 {
2970 gfc_symbol *param;
2971 gfc_find_symbol (name, gfc_current_block ()->f2k_derived,
2972 0, &param);
2973 if (!param && (current_attr.pdt_kind || current_attr.pdt_len))
2974 {
2975 gfc_error ("The component with KIND or LEN attribute at %C does not "
2976 "not appear in the type parameter list at %L",
2977 &gfc_current_block ()->declared_at);
2978 m = MATCH_ERROR;
2979 goto cleanup;
2980 }
2981 else if (param && !(current_attr.pdt_kind || current_attr.pdt_len))
2982 {
2983 gfc_error ("The component at %C that appears in the type parameter "
2984 "list at %L has neither the KIND nor LEN attribute",
2985 &gfc_current_block ()->declared_at);
2986 m = MATCH_ERROR;
2987 goto cleanup;
2988 }
2989 else if (as && (current_attr.pdt_kind || current_attr.pdt_len))
2990 {
2991 gfc_error ("The component at %C which is a type parameter must be "
2992 "a scalar");
2993 m = MATCH_ERROR;
2994 goto cleanup;
2995 }
2996 else if (param && initializer)
2997 {
2998 if (initializer->ts.type == BT_BOZ)
2999 {
3000 gfc_error ("BOZ literal constant at %L cannot appear as an "
3001 "initializer", &initializer->where);
3002 m = MATCH_ERROR;
3003 goto cleanup;
3004 }
3005 param->value = gfc_copy_expr (initializer);
3006 }
3007 }
3008
3009 /* Before adding a possible initilizer, do a simple check for compatibility
3010 of lhs and rhs types. Assigning a REAL value to a derived type is not a
3011 good thing. */
3012 if (current_ts.type == BT_DERIVED && initializer
3013 && (gfc_numeric_ts (&initializer->ts)
3014 || initializer->ts.type == BT_LOGICAL
3015 || initializer->ts.type == BT_CHARACTER))
3016 {
3017 gfc_error ("Incompatible initialization between a derived type "
3018 "entity and an entity with %qs type at %C",
3019 gfc_typename (initializer));
3020 m = MATCH_ERROR;
3021 goto cleanup;
3022 }
3023
3024
3025 /* Add the initializer. Note that it is fine if initializer is
3026 NULL here, because we sometimes also need to check if a
3027 declaration *must* have an initialization expression. */
3028 if (!gfc_comp_struct (gfc_current_state ()))
3029 t = add_init_expr_to_sym (name, &initializer, &var_locus);
3030 else
3031 {
3032 if (current_ts.type == BT_DERIVED
3033 && !current_attr.pointer && !initializer)
3034 initializer = gfc_default_initializer (&current_ts);
3035 t = build_struct (name, cl, &initializer, &as);
3036
3037 /* If we match a nested structure definition we expect to see the
3038 * body even if the variable declarations blow up, so we need to keep
3039 * the structure declaration around. */
3040 if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT)
3041 gfc_commit_symbol (gfc_new_block);
3042 }
3043
3044 m = (t) ? MATCH_YES : MATCH_ERROR;
3045
3046cleanup:
3047 /* Free stuff up and return. */
3048 gfc_seen_div0 = false;
3049 gfc_free_expr (initializer);
3050 gfc_free_array_spec (as);
3051
3052 return m;
3053}
3054
3055
3056/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
3057 This assumes that the byte size is equal to the kind number for
3058 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
3059
3060match
3061gfc_match_old_kind_spec (gfc_typespec *ts)
3062{
3063 match m;
3064 int original_kind;
3065
3066 if (gfc_match_char ('*') != MATCH_YES)
3067 return MATCH_NO;
3068
3069 m = gfc_match_small_literal_int (&ts->kind, NULL);
3070 if (m != MATCH_YES)
3071 return MATCH_ERROR;
3072
3073 original_kind = ts->kind;
3074
3075 /* Massage the kind numbers for complex types. */
3076 if (ts->type == BT_COMPLEX)
3077 {
3078 if (ts->kind % 2)
3079 {
3080 gfc_error ("Old-style type declaration %s*%d not supported at %C",
3081 gfc_basic_typename (ts->type), original_kind);
3082 return MATCH_ERROR;
3083 }
3084 ts->kind /= 2;
3085
3086 }
3087
3088 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3089 ts->kind = 8;
3090
3091 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3092 {
3093 if (ts->kind == 4)
3094 {
3095 if (flag_real4_kind == 8)
3096 ts->kind = 8;
3097 if (flag_real4_kind == 10)
3098 ts->kind = 10;
3099 if (flag_real4_kind == 16)
3100 ts->kind = 16;
3101 }
3102 else if (ts->kind == 8)
3103 {
3104 if (flag_real8_kind == 4)
3105 ts->kind = 4;
3106 if (flag_real8_kind == 10)
3107 ts->kind = 10;
3108 if (flag_real8_kind == 16)
3109 ts->kind = 16;
3110 }
3111 }
3112
3113 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3114 {
3115 gfc_error ("Old-style type declaration %s*%d not supported at %C",
3116 gfc_basic_typename (ts->type), original_kind);
3117 return MATCH_ERROR;
3118 }
3119
3120 if (!gfc_notify_std (GFC_STD_GNU,
3121 "Nonstandard type declaration %s*%d at %C",
3122 gfc_basic_typename(ts->type), original_kind))
3123 return MATCH_ERROR;
3124
3125 return MATCH_YES;
3126}
3127
3128
3129/* Match a kind specification. Since kinds are generally optional, we
3130 usually return MATCH_NO if something goes wrong. If a "kind="
3131 string is found, then we know we have an error. */
3132
3133match
3134gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
3135{
3136 locus where, loc;
3137 gfc_expr *e;
3138 match m, n;
3139 char c;
3140
3141 m = MATCH_NO;
3142 n = MATCH_YES;
3143 e = NULL;
3144 saved_kind_expr = NULL;
3145
3146 where = loc = gfc_current_locus;
3147
3148 if (kind_expr_only)
3149 goto kind_expr;
3150
3151 if (gfc_match_char ('(') == MATCH_NO)
3152 return MATCH_NO;
3153
3154 /* Also gobbles optional text. */
3155 if (gfc_match (" kind = ") == MATCH_YES)
3156 m = MATCH_ERROR;
3157
3158 loc = gfc_current_locus;
3159
3160kind_expr:
3161
3162 n = gfc_match_init_expr (&e);
3163
3164 if (gfc_derived_parameter_expr (e))
3165 {
3166 ts->kind = 0;
3167 saved_kind_expr = gfc_copy_expr (e);
3168 goto close_brackets;
3169 }
3170
3171 if (n != MATCH_YES)
3172 {
3173 if (gfc_matching_function)
3174 {
3175 /* The function kind expression might include use associated or
3176 imported parameters and try again after the specification
3177 expressions..... */
3178 if (gfc_match_char (')') != MATCH_YES)
3179 {
3180 gfc_error ("Missing right parenthesis at %C");
3181 m = MATCH_ERROR;
3182 goto no_match;
3183 }
3184
3185 gfc_free_expr (e);
3186 gfc_undo_symbols ();
3187 return MATCH_YES;
3188 }
3189 else
3190 {
3191 /* ....or else, the match is real. */
3192 if (n == MATCH_NO)
3193 gfc_error ("Expected initialization expression at %C");
3194 if (n != MATCH_YES)
3195 return MATCH_ERROR;
3196 }
3197 }
3198
3199 if (e->rank != 0)
3200 {
3201 gfc_error ("Expected scalar initialization expression at %C");
3202 m = MATCH_ERROR;
3203 goto no_match;
3204 }
3205
3206 if (gfc_extract_int (e, &ts->kind, 1))
3207 {
3208 m = MATCH_ERROR;
3209 goto no_match;
3210 }
3211
3212 /* Before throwing away the expression, let's see if we had a
3213 C interoperable kind (and store the fact). */
3214 if (e->ts.is_c_interop == 1)
3215 {
3216 /* Mark this as C interoperable if being declared with one
3217 of the named constants from iso_c_binding. */
3218 ts->is_c_interop = e->ts.is_iso_c;
3219 ts->f90_type = e->ts.f90_type;
3220 if (e->symtree)
3221 ts->interop_kind = e->symtree->n.sym;
3222 }
3223
3224 gfc_free_expr (e);
3225 e = NULL;
3226
3227 /* Ignore errors to this point, if we've gotten here. This means
3228 we ignore the m=MATCH_ERROR from above. */
3229 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
3230 {
3231 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
3232 gfc_basic_typename (ts->type));
3233 gfc_current_locus = where;
3234 return MATCH_ERROR;
3235 }
3236
3237 /* Warn if, e.g., c_int is used for a REAL variable, but not
3238 if, e.g., c_double is used for COMPLEX as the standard
3239 explicitly says that the kind type parameter for complex and real
3240 variable is the same, i.e. c_float == c_float_complex. */
3241 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
3242 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
3243 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
3244 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
3245 "is %s", gfc_basic_typename (ts->f90_type), &where,
3246 gfc_basic_typename (ts->type));
3247
3248close_brackets:
3249
3250 gfc_gobble_whitespace ();
3251 if ((c = gfc_next_ascii_char ()) != ')'
3252 && (ts->type != BT_CHARACTER || c != ','))
3253 {
3254 if (ts->type == BT_CHARACTER)
3255 gfc_error ("Missing right parenthesis or comma at %C");
3256 else
3257 gfc_error ("Missing right parenthesis at %C");
3258 m = MATCH_ERROR;
3259 }
3260 else
3261 /* All tests passed. */
3262 m = MATCH_YES;
3263
3264 if(m == MATCH_ERROR)
3265 gfc_current_locus = where;
3266
3267 if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8)
3268 ts->kind = 8;
3269
3270 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
3271 {
3272 if (ts->kind == 4)
3273 {
3274 if (flag_real4_kind == 8)
3275 ts->kind = 8;
3276 if (flag_real4_kind == 10)
3277 ts->kind = 10;
3278 if (flag_real4_kind == 16)
3279 ts->kind = 16;
3280 }
3281 else if (ts->kind == 8)
3282 {
3283 if (flag_real8_kind == 4)
3284 ts->kind = 4;
3285 if (flag_real8_kind == 10)
3286 ts->kind = 10;
3287 if (flag_real8_kind == 16)
3288 ts->kind = 16;
3289 }
3290 }
3291
3292 /* Return what we know from the test(s). */
3293 return m;
3294
3295no_match:
3296 gfc_free_expr (e);
3297 gfc_current_locus = where;
3298 return m;
3299}
3300
3301
3302static match
3303match_char_kind (int * kind, int * is_iso_c)
3304{
3305 locus where;
3306 gfc_expr *e;
3307 match m, n;
3308 bool fail;
3309
3310 m = MATCH_NO;
3311 e = NULL;
3312 where = gfc_current_locus;
3313
3314 n = gfc_match_init_expr (&e);
3315
3316 if (n != MATCH_YES && gfc_matching_function)
3317 {
3318 /* The expression might include use-associated or imported
3319 parameters and try again after the specification
3320 expressions. */
3321 gfc_free_expr (e);
3322 gfc_undo_symbols ();
3323 return MATCH_YES;
3324 }
3325
3326 if (n == MATCH_NO)
3327 gfc_error ("Expected initialization expression at %C");
3328 if (n != MATCH_YES)
3329 return MATCH_ERROR;
3330
3331 if (e->rank != 0)
3332 {
3333 gfc_error ("Expected scalar initialization expression at %C");
3334 m = MATCH_ERROR;
3335 goto no_match;
3336 }
3337
3338 if (gfc_derived_parameter_expr (e))
3339 {
3340 saved_kind_expr = e;
3341 *kind = 0;
3342 return MATCH_YES;
3343 }
3344
3345 fail = gfc_extract_int (e, kind, 1);
3346 *is_iso_c = e->ts.is_iso_c;
3347 if (fail)
3348 {
3349 m = MATCH_ERROR;
3350 goto no_match;
3351 }
3352
3353 gfc_free_expr (e);
3354
3355 /* Ignore errors to this point, if we've gotten here. This means
3356 we ignore the m=MATCH_ERROR from above. */
3357 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
3358 {
3359 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
3360 m = MATCH_ERROR;
3361 }
3362 else
3363 /* All tests passed. */
3364 m = MATCH_YES;
3365
3366 if (m == MATCH_ERROR)
3367 gfc_current_locus = where;
3368
3369 /* Return what we know from the test(s). */
3370 return m;
3371
3372no_match:
3373 gfc_free_expr (e);
3374 gfc_current_locus = where;
3375 return m;
3376}
3377
3378
3379/* Match the various kind/length specifications in a CHARACTER
3380 declaration. We don't return MATCH_NO. */
3381
3382match
3383gfc_match_char_spec (gfc_typespec *ts)
3384{
3385 int kind, seen_length, is_iso_c;
3386 gfc_charlen *cl;
3387 gfc_expr *len;
3388 match m;
3389 bool deferred;
3390
3391 len = NULL;
3392 seen_length = 0;
3393 kind = 0;
3394 is_iso_c = 0;
3395 deferred = false;
3396
3397 /* Try the old-style specification first. */
3398 old_char_selector = 0;
3399
3400 m = match_char_length (&len, &deferred, true);
3401 if (m != MATCH_NO)
3402 {
3403 if (m == MATCH_YES)
3404 old_char_selector = 1;
3405 seen_length = 1;
3406 goto done;
3407 }
3408
3409 m = gfc_match_char ('(');
3410 if (m != MATCH_YES)
3411 {
3412 m = MATCH_YES; /* Character without length is a single char. */
3413 goto done;
3414 }
3415
3416 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
3417 if (gfc_match (" kind =") == MATCH_YES)
3418 {
3419 m = match_char_kind (&kind, &is_iso_c);
3420
3421 if (m == MATCH_ERROR)
3422 goto done;
3423 if (m == MATCH_NO)
3424 goto syntax;
3425
3426 if (gfc_match (" , len =") == MATCH_NO)
3427 goto rparen;
3428
3429 m = char_len_param_value (&len, &deferred);
3430 if (m == MATCH_NO)
3431 goto syntax;
3432 if (m == MATCH_ERROR)
3433 goto done;
3434 seen_length = 1;
3435
3436 goto rparen;
3437 }
3438
3439 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
3440 if (gfc_match (" len =") == MATCH_YES)
3441 {
3442 m = char_len_param_value (&len, &deferred);
3443 if (m == MATCH_NO)
3444 goto syntax;
3445 if (m == MATCH_ERROR)
3446 goto done;
3447 seen_length = 1;
3448
3449 if (gfc_match_char (')') == MATCH_YES)
3450 goto done;
3451
3452 if (gfc_match (" , kind =") != MATCH_YES)
3453 goto syntax;
3454
3455 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
3456 goto done;
3457
3458 goto rparen;
3459 }
3460
3461 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
3462 m = char_len_param_value (&len, &deferred);
3463 if (m == MATCH_NO)
3464 goto syntax;
3465 if (m == MATCH_ERROR)
3466 goto done;
3467 seen_length = 1;
3468
3469 m = gfc_match_char (')');
3470 if (m == MATCH_YES)
3471 goto done;
3472
3473 if (gfc_match_char (',') != MATCH_YES)
3474 goto syntax;
3475
3476 gfc_match (" kind ="); /* Gobble optional text. */
3477
3478 m = match_char_kind (&kind, &is_iso_c);
3479 if (m == MATCH_ERROR)
3480 goto done;
3481 if (m == MATCH_NO)
3482 goto syntax;
3483
3484rparen:
3485 /* Require a right-paren at this point. */
3486 m = gfc_match_char (')');
3487 if (m == MATCH_YES)
3488 goto done;
3489
3490syntax:
3491 gfc_error ("Syntax error in CHARACTER declaration at %C");
3492 m = MATCH_ERROR;
3493 gfc_free_expr (len);
3494 return m;
3495
3496done:
3497 /* Deal with character functions after USE and IMPORT statements. */
3498 if (gfc_matching_function)
3499 {
3500 gfc_free_expr (len);
3501 gfc_undo_symbols ();
3502 return MATCH_YES;
3503 }
3504
3505 if (m != MATCH_YES)
3506 {
3507 gfc_free_expr (len);
3508 return m;
3509 }
3510
3511 /* Do some final massaging of the length values. */
3512 cl = gfc_new_charlen (gfc_current_ns, NULL);
3513
3514 if (seen_length == 0)
3515 cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
3516 else
3517 {
3518 /* If gfortran ends up here, then len may be reducible to a constant.
3519 Try to do that here. If it does not reduce, simply assign len to
3520 charlen. A complication occurs with user-defined generic functions,
3521 which are not resolved. Use a private namespace to deal with
3522 generic functions. */
3523
3524 if (len && len->expr_type != EXPR_CONSTANT)
3525 {
3526 gfc_namespace *old_ns;
3527 gfc_expr *e;
3528
3529 old_ns = gfc_current_ns;
3530 gfc_current_ns = gfc_get_namespace (NULL, 0);
3531
3532 e = gfc_copy_expr (len);
3533 gfc_reduce_init_expr (e);
3534 if (e->expr_type == EXPR_CONSTANT)
3535 {
3536 gfc_replace_expr (len, e);
3537 if (mpz_cmp_si (len->value.integer, 0) < 0)
3538 mpz_set_ui (len->value.integer, 0);
3539 }
3540 else
3541 gfc_free_expr (e);
3542
3543 gfc_free_namespace (gfc_current_ns);
3544 gfc_current_ns = old_ns;
3545 }
3546
3547 cl->length = len;
3548 }
3549
3550 ts->u.cl = cl;
3551 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
3552 ts->deferred = deferred;
3553
3554 /* We have to know if it was a C interoperable kind so we can
3555 do accurate type checking of bind(c) procs, etc. */
3556 if (kind != 0)
3557 /* Mark this as C interoperable if being declared with one
3558 of the named constants from iso_c_binding. */
3559 ts->is_c_interop = is_iso_c;
3560 else if (len != NULL)
3561 /* Here, we might have parsed something such as: character(c_char)
3562 In this case, the parsing code above grabs the c_char when
3563 looking for the length (line 1690, roughly). it's the last
3564 testcase for parsing the kind params of a character variable.
3565 However, it's not actually the length. this seems like it
3566 could be an error.
3567 To see if the user used a C interop kind, test the expr
3568 of the so called length, and see if it's C interoperable. */
3569 ts->is_c_interop = len->ts.is_iso_c;
3570
3571 return MATCH_YES;
3572}
3573
3574
3575/* Matches a RECORD declaration. */
3576
3577static match
3578match_record_decl (char *name)
3579{
3580 locus old_loc;
3581 old_loc = gfc_current_locus;
3582 match m;
3583
3584 m = gfc_match (" record /");
3585 if (m == MATCH_YES)
3586 {
3587 if (!flag_dec_structure)
3588 {
3589 gfc_current_locus = old_loc;
3590 gfc_error ("RECORD at %C is an extension, enable it with "
3591 "%<-fdec-structure%>");
3592 return MATCH_ERROR;
3593 }
3594 m = gfc_match (" %n/", name);
3595 if (m == MATCH_YES)
3596 return MATCH_YES;
3597 }
3598
3599 gfc_current_locus = old_loc;
3600 if (flag_dec_structure
3601 && (gfc_match (" record% ") == MATCH_YES
3602 || gfc_match (" record%t") == MATCH_YES))
3603 gfc_error ("Structure name expected after RECORD at %C");
3604 if (m == MATCH_NO)
3605 return MATCH_NO;
3606
3607 return MATCH_ERROR;
3608}
3609
3610
3611/* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source
3612 of expressions to substitute into the possibly parameterized expression
3613 'e'. Using a list is inefficient but should not be too bad since the
3614 number of type parameters is not likely to be large. */
3615static bool
3616insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3617 int* f)
3618{
3619 gfc_actual_arglist *param;
3620 gfc_expr *copy;
3621
3622 if (e->expr_type != EXPR_VARIABLE)
3623 return false;
3624
3625 gcc_assert (e->symtree);
3626 if (e->symtree->n.sym->attr.pdt_kind
3627 || (*f != 0 && e->symtree->n.sym->attr.pdt_len))
3628 {
3629 for (param = type_param_spec_list; param; param = param->next)
3630 if (strcmp (e->symtree->n.sym->name, param->name) == 0)
3631 break;
3632
3633 if (param)
3634 {
3635 copy = gfc_copy_expr (param->expr);
3636 *e = *copy;
3637 free (copy);
3638 }
3639 }
3640
3641 return false;
3642}
3643
3644
3645bool
3646gfc_insert_kind_parameter_exprs (gfc_expr *e)
3647{
3648 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0);
3649}
3650
3651
3652bool
3653gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list)
3654{
3655 gfc_actual_arglist *old_param_spec_list = type_param_spec_list;
3656 type_param_spec_list = param_list;
3657 return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1);
3658 type_param_spec_list = NULL;
3659 type_param_spec_list = old_param_spec_list;
3660}
3661
3662/* Determines the instance of a parameterized derived type to be used by
3663 matching determining the values of the kind parameters and using them
3664 in the name of the instance. If the instance exists, it is used, otherwise
3665 a new derived type is created. */
3666match
3667gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
3668 gfc_actual_arglist **ext_param_list)
3669{
3670 /* The PDT template symbol. */
3671 gfc_symbol *pdt = *sym;
3672 /* The symbol for the parameter in the template f2k_namespace. */
3673 gfc_symbol *param;
3674 /* The hoped for instance of the PDT. */
3675 gfc_symbol *instance;
3676 /* The list of parameters appearing in the PDT declaration. */
3677 gfc_formal_arglist *type_param_name_list;
3678 /* Used to store the parameter specification list during recursive calls. */
3679 gfc_actual_arglist *old_param_spec_list;
3680 /* Pointers to the parameter specification being used. */
3681 gfc_actual_arglist *actual_param;
3682 gfc_actual_arglist *tail = NULL;
3683 /* Used to build up the name of the PDT instance. The prefix uses 4
3684 characters and each KIND parameter 2 more. Allow 8 of the latter. */
3685 char name[GFC_MAX_SYMBOL_LEN + 21];
3686
3687 bool name_seen = (param_list == NULL);
3688 bool assumed_seen = false;
3689 bool deferred_seen = false;
3690 bool spec_error = false;
3691 int kind_value, i;
3692 gfc_expr *kind_expr;
3693 gfc_component *c1, *c2;
3694 match m;
3695
3696 type_param_spec_list = NULL;
3697
3698 type_param_name_list = pdt->formal;
3699 actual_param = param_list;
3700 sprintf (name, "Pdt%s", pdt->name);
3701
3702 /* Run through the parameter name list and pick up the actual
3703 parameter values or use the default values in the PDT declaration. */
3704 for (; type_param_name_list;
3705 type_param_name_list = type_param_name_list->next)
3706 {
3707 if (actual_param && actual_param->spec_type != SPEC_EXPLICIT)
3708 {
3709 if (actual_param->spec_type == SPEC_ASSUMED)
3710 spec_error = deferred_seen;
3711 else
3712 spec_error = assumed_seen;
3713
3714 if (spec_error)
3715 {
3716 gfc_error ("The type parameter spec list at %C cannot contain "
3717 "both ASSUMED and DEFERRED parameters");
3718 goto error_return;
3719 }
3720 }
3721
3722 if (actual_param && actual_param->name)
3723 name_seen = true;
3724 param = type_param_name_list->sym;
3725
3726 if (!param || !param->name)
3727 continue;
3728
3729 c1 = gfc_find_component (pdt, param->name, false, true, NULL);
3730 /* An error should already have been thrown in resolve.c
3731 (resolve_fl_derived0). */
3732 if (!pdt->attr.use_assoc && !c1)
3733 goto error_return;
3734
3735 kind_expr = NULL;
3736 if (!name_seen)
3737 {
3738 if (!actual_param && !(c1 && c1->initializer))
3739 {
3740 gfc_error ("The type parameter spec list at %C does not contain "
3741 "enough parameter expressions");
3742 goto error_return;
3743 }
3744 else if (!actual_param && c1 && c1->initializer)
3745 kind_expr = gfc_copy_expr (c1->initializer);
3746 else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3747 kind_expr = gfc_copy_expr (actual_param->expr);
3748 }
3749 else
3750 {
3751 actual_param = param_list;
3752 for (;actual_param; actual_param = actual_param->next)
3753 if (actual_param->name
3754 && strcmp (actual_param->name, param->name) == 0)
3755 break;
3756 if (actual_param && actual_param->spec_type == SPEC_EXPLICIT)
3757 kind_expr = gfc_copy_expr (actual_param->expr);
3758 else
3759 {
3760 if (c1->initializer)
3761 kind_expr = gfc_copy_expr (c1->initializer);
3762 else if (!(actual_param && param->attr.pdt_len))
3763 {
3764 gfc_error ("The derived parameter %qs at %C does not "
3765 "have a default value", param->name);
3766 goto error_return;
3767 }
3768 }
3769 }
3770
3771 /* Store the current parameter expressions in a temporary actual
3772 arglist 'list' so that they can be substituted in the corresponding
3773 expressions in the PDT instance. */
3774 if (type_param_spec_list == NULL)
3775 {
3776 type_param_spec_list = gfc_get_actual_arglist ();
3777 tail = type_param_spec_list;
3778 }
3779 else
3780 {
3781 tail->next = gfc_get_actual_arglist ();
3782 tail = tail->next;
3783 }
3784 tail->name = param->name;
3785
3786 if (kind_expr)
3787 {
3788 /* Try simplification even for LEN expressions. */
3789 bool ok;
3790 gfc_resolve_expr (kind_expr);
3791 ok = gfc_simplify_expr (kind_expr, 1);
3792 /* Variable expressions seem to default to BT_PROCEDURE.
3793 TODO find out why this is and fix it. */
3794 if (kind_expr->ts.type != BT_INTEGER
3795 && kind_expr->ts.type != BT_PROCEDURE)
3796 {
3797 gfc_error ("The parameter expression at %C must be of "
3798 "INTEGER type and not %s type",
3799 gfc_basic_typename (kind_expr->ts.type));
3800 goto error_return;
3801 }
3802 if (kind_expr->ts.type == BT_INTEGER && !ok)
3803 {
3804 gfc_error ("The parameter expression at %C does not "
3805 "simplify to an INTEGER constant");
3806 goto error_return;
3807 }
3808
3809 tail->expr = gfc_copy_expr (kind_expr);
3810 }
3811
3812 if (actual_param)
3813 tail->spec_type = actual_param->spec_type;
3814
3815 if (!param->attr.pdt_kind)
3816 {
3817 if (!name_seen && actual_param)
3818 actual_param = actual_param->next;
3819 if (kind_expr)
3820 {
3821 gfc_free_expr (kind_expr);
3822 kind_expr = NULL;
3823 }
3824 continue;
3825 }
3826
3827 if (actual_param
3828 && (actual_param->spec_type == SPEC_ASSUMED
3829 || actual_param->spec_type == SPEC_DEFERRED))
3830 {
3831 gfc_error ("The KIND parameter %qs at %C cannot either be "
3832 "ASSUMED or DEFERRED", param->name);
3833 goto error_return;
3834 }
3835
3836 if (!kind_expr || !gfc_is_constant_expr (kind_expr))
3837 {
3838 gfc_error ("The value for the KIND parameter %qs at %C does not "
3839 "reduce to a constant expression", param->name);
3840 goto error_return;
3841 }
3842
3843 gfc_extract_int (kind_expr, &kind_value);
3844 sprintf (name + strlen (name), "_%d", kind_value);
3845
3846 if (!name_seen && actual_param)
3847 actual_param = actual_param->next;
3848 gfc_free_expr (kind_expr);
3849 }
3850
3851 if (!name_seen && actual_param)
3852 {
3853 gfc_error ("The type parameter spec list at %C contains too many "
3854 "parameter expressions");
3855 goto error_return;
3856 }
3857
3858 /* Now we search for the PDT instance 'name'. If it doesn't exist, we
3859 build it, using 'pdt' as a template. */
3860 if (gfc_get_symbol (name, pdt->ns, &instance))
3861 {
3862 gfc_error ("Parameterized derived type at %C is ambiguous");
3863 goto error_return;
3864 }
3865
3866 m = MATCH_YES;
3867
3868 if (instance->attr.flavor == FL_DERIVED
3869 && instance->attr.pdt_type)
3870 {
3871 instance->refs++;
3872 if (ext_param_list)
3873 *ext_param_list = type_param_spec_list;
3874 *sym = instance;
3875 gfc_commit_symbols ();
3876 return m;
3877 }
3878
3879 /* Start building the new instance of the parameterized type. */
3880 gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at);
3881 instance->attr.pdt_template = 0;
3882 instance->attr.pdt_type = 1;
3883 instance->declared_at = gfc_current_locus;
3884
3885 /* Add the components, replacing the parameters in all expressions
3886 with the expressions for their values in 'type_param_spec_list'. */
3887 c1 = pdt->components;
3888 tail = type_param_spec_list;
3889 for (; c1; c1 = c1->next)
3890 {
3891 gfc_add_component (instance, c1->name, &c2);
3892
3893 c2->ts = c1->ts;
3894 c2->attr = c1->attr;
3895
3896 /* The order of declaration of the type_specs might not be the
3897 same as that of the components. */
3898 if (c1->attr.pdt_kind || c1->attr.pdt_len)
3899 {
3900 for (tail = type_param_spec_list; tail; tail = tail->next)
3901 if (strcmp (c1->name, tail->name) == 0)
3902 break;
3903 }
3904
3905 /* Deal with type extension by recursively calling this function
3906 to obtain the instance of the extended type. */
3907 if (gfc_current_state () != COMP_DERIVED
3908 && c1 == pdt->components
3909 && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
3910 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template
3911 && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
3912 {
3913 gfc_formal_arglist *f;
3914
3915 old_param_spec_list = type_param_spec_list;
3916
3917 /* Obtain a spec list appropriate to the extended type..*/
3918 actual_param = gfc_copy_actual_arglist (type_param_spec_list);
3919 type_param_spec_list = actual_param;
3920 for (f = c1->ts.u.derived->formal; f && f->next; f = f->next)
3921 actual_param = actual_param->next;
3922 if (actual_param)
3923 {
3924 gfc_free_actual_arglist (actual_param->next);
3925 actual_param->next = NULL;
3926 }
3927
3928 /* Now obtain the PDT instance for the extended type. */
3929 c2->param_list = type_param_spec_list;
3930 m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived,
3931 NULL);
3932 type_param_spec_list = old_param_spec_list;
3933
3934 c2->ts.u.derived->refs++;
3935 gfc_set_sym_referenced (c2->ts.u.derived);
3936
3937 /* Set extension level. */
3938 if (c2->ts.u.derived->attr.extension == 255)
3939 {
3940 /* Since the extension field is 8 bit wide, we can only have
3941 up to 255 extension levels. */
3942 gfc_error ("Maximum extension level reached with type %qs at %L",
3943 c2->ts.u.derived->name,
3944 &c2->ts.u.derived->declared_at);
3945 goto error_return;
3946 }
3947 instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
3948
3949 continue;
3950 }
3951
3952 /* Set the component kind using the parameterized expression. */
3953 if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER)
3954 && c1->kind_expr != NULL)
3955 {
3956 gfc_expr *e = gfc_copy_expr (c1->kind_expr);
3957 gfc_insert_kind_parameter_exprs (e);
3958 gfc_simplify_expr (e, 1);
3959 gfc_extract_int (e, &c2->ts.kind);
3960 gfc_free_expr (e);
3961 if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0)
3962 {
3963 gfc_error ("Kind %d not supported for type %s at %C",
3964 c2->ts.kind, gfc_basic_typename (c2->ts.type));
3965 goto error_return;
3966 }
3967 }
3968
3969 /* Similarly, set the string length if parameterized. */
3970 if (c1->ts.type == BT_CHARACTER
3971 && c1->ts.u.cl->length
3972 && gfc_derived_parameter_expr (c1->ts.u.cl->length))
3973 {
3974 gfc_expr *e;
3975 e = gfc_copy_expr (c1->ts.u.cl->length);
3976 gfc_insert_kind_parameter_exprs (e);
3977 gfc_simplify_expr (e, 1);
3978 c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3979 c2->ts.u.cl->length = e;
3980 c2->attr.pdt_string = 1;
3981 }
3982
3983 /* Set up either the KIND/LEN initializer, if constant,
3984 or the parameterized expression. Use the template
3985 initializer if one is not already set in this instance. */
3986 if (c2->attr.pdt_kind || c2->attr.pdt_len)
3987 {
3988 if (tail && tail->expr && gfc_is_constant_expr (tail->expr))
3989 c2->initializer = gfc_copy_expr (tail->expr);
3990 else if (tail && tail->expr)
3991 {
3992 c2->param_list = gfc_get_actual_arglist ();
3993 c2->param_list->name = tail->name;
3994 c2->param_list->expr = gfc_copy_expr (tail->expr);
3995 c2->param_list->next = NULL;
3996 }
3997
3998 if (!c2->initializer && c1->initializer)
3999 c2->initializer = gfc_copy_expr (c1->initializer);
4000 }
4001
4002 /* Copy the array spec. */
4003 c2->as = gfc_copy_array_spec (c1->as);
4004 if (c1->ts.type == BT_CLASS)
4005 CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as);
4006
4007 /* Determine if an array spec is parameterized. If so, substitute
4008 in the parameter expressions for the bounds and set the pdt_array
4009 attribute. Notice that this attribute must be unconditionally set
4010 if this is an array of parameterized character length. */
4011 if (c1->as && c1->as->type == AS_EXPLICIT)
4012 {
4013 bool pdt_array = false;
4014
4015 /* Are the bounds of the array parameterized? */
4016 for (i = 0; i < c1->as->rank; i++)
4017 {
4018 if (gfc_derived_parameter_expr (c1->as->lower[i]))
4019 pdt_array = true;
4020 if (gfc_derived_parameter_expr (c1->as->upper[i]))
4021 pdt_array = true;
4022 }
4023
4024 /* If they are, free the expressions for the bounds and
4025 replace them with the template expressions with substitute
4026 values. */
4027 for (i = 0; pdt_array && i < c1->as->rank; i++)
4028 {
4029 gfc_expr *e;
4030 e = gfc_copy_expr (c1->as->lower[i]);
4031 gfc_insert_kind_parameter_exprs (e);
4032 gfc_simplify_expr (e, 1);
4033 gfc_free_expr (c2->as->lower[i]);
4034 c2->as->lower[i] = e;
4035 e = gfc_copy_expr (c1->as->upper[i]);
4036 gfc_insert_kind_parameter_exprs (e);
4037 gfc_simplify_expr (e, 1);
4038 gfc_free_expr (c2->as->upper[i]);
4039 c2->as->upper[i] = e;
4040 }
4041 c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string;
4042 if (c1->initializer)
4043 {
4044 c2->initializer = gfc_copy_expr (c1->initializer);
4045 gfc_insert_kind_parameter_exprs (c2->initializer);
4046 gfc_simplify_expr (c2->initializer, 1);
4047 }
4048 }
4049
4050 /* Recurse into this function for PDT components. */
4051 if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS)
4052 && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template)
4053 {
4054 gfc_actual_arglist *params;
4055 /* The component in the template has a list of specification
4056 expressions derived from its declaration. */
4057 params = gfc_copy_actual_arglist (c1->param_list);
4058 actual_param = params;
4059 /* Substitute the template parameters with the expressions
4060 from the specification list. */
4061 for (;actual_param; actual_param = actual_param->next)
4062 gfc_insert_parameter_exprs (actual_param->expr,
4063 type_param_spec_list);
4064
4065 /* Now obtain the PDT instance for the component. */
4066 old_param_spec_list = type_param_spec_list;
4067 m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL);
4068 type_param_spec_list = old_param_spec_list;
4069
4070 c2->param_list = params;
4071 if (!(c2->attr.pointer || c2->attr.allocatable))
4072 c2->initializer = gfc_default_initializer (&c2->ts);
4073
4074 if (c2->attr.allocatable)
4075 instance->attr.alloc_comp = 1;
4076 }
4077 }
4078
4079 gfc_commit_symbol (instance);
4080 if (ext_param_list)
4081 *ext_param_list = type_param_spec_list;
4082 *sym = instance;
4083 return m;
4084
4085error_return:
4086 gfc_free_actual_arglist (type_param_spec_list);
4087 return MATCH_ERROR;
4088}
4089
4090
4091/* Match a legacy nonstandard BYTE type-spec. */
4092
4093static match
4094match_byte_typespec (gfc_typespec *ts)
4095{
4096 if (gfc_match (" byte") == MATCH_YES)
4097 {
4098 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
4099 return MATCH_ERROR;
4100
4101 if (gfc_current_form == FORM_FREE)
4102 {
4103 char c = gfc_peek_ascii_char ();
4104 if (!gfc_is_whitespace (c) && c != ',')
4105 return MATCH_NO;
4106 }
4107
4108 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
4109 {
4110 gfc_error ("BYTE type used at %C "
4111 "is not available on the target machine");
4112 return MATCH_ERROR;
4113 }
4114
4115 ts->type = BT_INTEGER;
4116 ts->kind = 1;
4117 return MATCH_YES;
4118 }
4119 return MATCH_NO;
4120}
4121
4122
4123/* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
4124 structure to the matched specification. This is necessary for FUNCTION and
4125 IMPLICIT statements.
4126
4127 If implicit_flag is nonzero, then we don't check for the optional
4128 kind specification. Not doing so is needed for matching an IMPLICIT
4129 statement correctly. */
4130
4131match
4132gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
4133{
4134 /* Provide sufficient space to hold "pdtsymbol". */
4135 char *name = XALLOCAVEC (char, GFC_MAX_SYMBOL_LEN + 1);
4136 gfc_symbol *sym, *dt_sym;
4137 match m;
4138 char c;
4139 bool seen_deferred_kind, matched_type;
4140 const char *dt_name;
4141
4142 decl_type_param_list = NULL;
4143
4144 /* A belt and braces check that the typespec is correctly being treated
4145 as a deferred characteristic association. */
4146 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
4147 && (gfc_current_block ()->result->ts.kind == -1)
4148 && (ts->kind == -1);
4149 gfc_clear_ts (ts);
4150 if (seen_deferred_kind)
4151 ts->kind = -1;
4152
4153 /* Clear the current binding label, in case one is given. */
4154 curr_binding_label = NULL;
4155
4156 /* Match BYTE type-spec. */
4157 m = match_byte_typespec (ts);
4158 if (m != MATCH_NO)
4159 return m;
4160
4161 m = gfc_match (" type (");
4162 matched_type = (m == MATCH_YES);
4163 if (matched_type)
4164 {
4165 gfc_gobble_whitespace ();
4166 if (gfc_peek_ascii_char () == '*')
4167 {
4168 if ((m = gfc_match ("* ) ")) != MATCH_YES)
4169 return m;
4170 if (gfc_comp_struct (gfc_current_state ()))
4171 {
4172 gfc_error ("Assumed type at %C is not allowed for components");
4173 return MATCH_ERROR;
4174 }
4175 if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C"))
4176 return MATCH_ERROR;
4177 ts->type = BT_ASSUMED;
4178 return MATCH_YES;
4179 }
4180
4181 m = gfc_match ("%n", name);
4182 matched_type = (m == MATCH_YES);
4183 }
4184
4185 if ((matched_type && strcmp ("integer", name) == 0)
4186 || (!matched_type && gfc_match (" integer") == MATCH_YES))
4187 {
4188 ts->type = BT_INTEGER;
4189 ts->kind = gfc_default_integer_kind;
4190 goto get_kind;
4191 }
4192
4193 if ((matched_type && strcmp ("character", name) == 0)
4194 || (!matched_type && gfc_match (" character") == MATCH_YES))
4195 {
4196 if (matched_type
4197 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4198 "intrinsic-type-spec at %C"))
4199 return MATCH_ERROR;
4200
4201 ts->type = BT_CHARACTER;
4202 if (implicit_flag == 0)
4203 m = gfc_match_char_spec (ts);
4204 else
4205 m = MATCH_YES;
4206
4207 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
4208 {
4209 gfc_error ("Malformed type-spec at %C");
4210 return MATCH_ERROR;
4211 }
4212
4213 return m;
4214 }
4215
4216 if ((matched_type && strcmp ("real", name) == 0)
4217 || (!matched_type && gfc_match (" real") == MATCH_YES))
4218 {
4219 ts->type = BT_REAL;
4220 ts->kind = gfc_default_real_kind;
4221 goto get_kind;
4222 }
4223
4224 if ((matched_type
4225 && (strcmp ("doubleprecision", name) == 0
4226 || (strcmp ("double", name) == 0
4227 && gfc_match (" precision") == MATCH_YES)))
4228 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
4229 {
4230 if (matched_type
4231 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4232 "intrinsic-type-spec at %C"))
4233 return MATCH_ERROR;
4234
4235 if (matched_type && gfc_match_char (')') != MATCH_YES)
4236 {
4237 gfc_error ("Malformed type-spec at %C");
4238 return MATCH_ERROR;
4239 }
4240
4241 ts->type = BT_REAL;
4242 ts->kind = gfc_default_double_kind;
4243 return MATCH_YES;
4244 }
4245
4246 if ((matched_type && strcmp ("complex", name) == 0)
4247 || (!matched_type && gfc_match (" complex") == MATCH_YES))
4248 {
4249 ts->type = BT_COMPLEX;
4250 ts->kind = gfc_default_complex_kind;
4251 goto get_kind;
4252 }
4253
4254 if ((matched_type
4255 && (strcmp ("doublecomplex", name) == 0
4256 || (strcmp ("double", name) == 0
4257 && gfc_match (" complex") == MATCH_YES)))
4258 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
4259 {
4260 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
4261 return MATCH_ERROR;
4262
4263 if (matched_type
4264 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4265 "intrinsic-type-spec at %C"))
4266 return MATCH_ERROR;
4267
4268 if (matched_type && gfc_match_char (')') != MATCH_YES)
4269 {
4270 gfc_error ("Malformed type-spec at %C");
4271 return MATCH_ERROR;
4272 }
4273
4274 ts->type = BT_COMPLEX;
4275 ts->kind = gfc_default_double_kind;
4276 return MATCH_YES;
4277 }
4278
4279 if ((matched_type && strcmp ("logical", name) == 0)
4280 || (!matched_type && gfc_match (" logical") == MATCH_YES))
4281 {
4282 ts->type = BT_LOGICAL;
4283 ts->kind = gfc_default_logical_kind;
4284 goto get_kind;
4285 }
4286
4287 if (matched_type)
4288 {
4289 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4290 if (m == MATCH_ERROR)
4291 return m;
4292
4293 gfc_gobble_whitespace ();
4294 if (gfc_peek_ascii_char () != ')')
4295 {
4296 gfc_error ("Malformed type-spec at %C");
4297 return MATCH_ERROR;
4298 }
4299 m = gfc_match_char (')'); /* Burn closing ')'. */
4300 }
4301
4302 if (m != MATCH_YES)
4303 m = match_record_decl (name);
4304
4305 if (matched_type || m == MATCH_YES)
4306 {
4307 ts->type = BT_DERIVED;
4308 /* We accept record/s/ or type(s) where s is a structure, but we
4309 * don't need all the extra derived-type stuff for structures. */
4310 if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym))
4311 {
4312 gfc_error ("Type name %qs at %C is ambiguous", name);
4313 return MATCH_ERROR;
4314 }
4315
4316 if (sym && sym->attr.flavor == FL_DERIVED
4317 && sym->attr.pdt_template
4318 && gfc_current_state () != COMP_DERIVED)
4319 {
4320 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4321 if (m != MATCH_YES)
4322 return m;
4323 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4324 ts->u.derived = sym;
4325 const char* lower = gfc_dt_lower_string (sym->name);
4326 size_t len = strlen (lower);
4327 /* Reallocate with sufficient size. */
4328 if (len > GFC_MAX_SYMBOL_LEN)
4329 name = XALLOCAVEC (char, len + 1);
4330 memcpy (name, lower, len);
4331 name[len] = '\0';
4332 }
4333
4334 if (sym && sym->attr.flavor == FL_STRUCT)
4335 {
4336 ts->u.derived = sym;
4337 return MATCH_YES;
4338 }
4339 /* Actually a derived type. */
4340 }
4341
4342 else
4343 {
4344 /* Match nested STRUCTURE declarations; only valid within another
4345 structure declaration. */
4346 if (flag_dec_structure
4347 && (gfc_current_state () == COMP_STRUCTURE
4348 || gfc_current_state () == COMP_MAP))
4349 {
4350 m = gfc_match (" structure");
4351 if (m == MATCH_YES)
4352 {
4353 m = gfc_match_structure_decl ();
4354 if (m == MATCH_YES)
4355 {
4356 /* gfc_new_block is updated by match_structure_decl. */
4357 ts->type = BT_DERIVED;
4358 ts->u.derived = gfc_new_block;
4359 return MATCH_YES;
4360 }
4361 }
4362 if (m == MATCH_ERROR)
4363 return MATCH_ERROR;
4364 }
4365
4366 /* Match CLASS declarations. */
4367 m = gfc_match (" class ( * )");
4368 if (m == MATCH_ERROR)
4369 return MATCH_ERROR;
4370 else if (m == MATCH_YES)
4371 {
4372 gfc_symbol *upe;
4373 gfc_symtree *st;
4374 ts->type = BT_CLASS;
4375 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
4376 if (upe == NULL)
4377 {
4378 upe = gfc_new_symbol ("STAR", gfc_current_ns);
4379 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
4380 st->n.sym = upe;
4381 gfc_set_sym_referenced (upe);
4382 upe->refs++;
4383 upe->ts.type = BT_VOID;
4384 upe->attr.unlimited_polymorphic = 1;
4385 /* This is essential to force the construction of
4386 unlimited polymorphic component class containers. */
4387 upe->attr.zero_comp = 1;
4388 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
4389 &gfc_current_locus))
4390 return MATCH_ERROR;
4391 }
4392 else
4393 {
4394 st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR");
4395 st->n.sym = upe;
4396 upe->refs++;
4397 }
4398 ts->u.derived = upe;
4399 return m;
4400 }
4401
4402 m = gfc_match (" class (");
4403
4404 if (m == MATCH_YES)
4405 m = gfc_match ("%n", name);
4406 else
4407 return m;
4408
4409 if (m != MATCH_YES)
4410 return m;
4411 ts->type = BT_CLASS;
4412
4413 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
4414 return MATCH_ERROR;
4415
4416 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
4417 if (m == MATCH_ERROR)
4418 return m;
4419
4420 m = gfc_match_char (')');
4421 if (m != MATCH_YES)
4422 return m;
4423 }
4424
4425 /* Defer association of the derived type until the end of the
4426 specification block. However, if the derived type can be
4427 found, add it to the typespec. */
4428 if (gfc_matching_function)
4429 {
4430 ts->u.derived = NULL;
4431 if (gfc_current_state () != COMP_INTERFACE
4432 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
4433 {
4434 sym = gfc_find_dt_in_generic (sym);
4435 ts->u.derived = sym;
4436 }
4437 return MATCH_YES;
4438 }
4439
4440 /* Search for the name but allow the components to be defined later. If
4441 type = -1, this typespec has been seen in a function declaration but
4442 the type could not be accessed at that point. The actual derived type is
4443 stored in a symtree with the first letter of the name capitalized; the
4444 symtree with the all lower-case name contains the associated
4445 generic function. */
4446 dt_name = gfc_dt_upper_string (name);
4447 sym = NULL;
4448 dt_sym = NULL;
4449 if (ts->kind != -1)
4450 {
4451 gfc_get_ha_symbol (name, &sym);
4452 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
4453 {
4454 gfc_error ("Type name %qs at %C is ambiguous", name);
4455 return MATCH_ERROR;
4456 }
4457 if (sym->generic && !dt_sym)
4458 dt_sym = gfc_find_dt_in_generic (sym);
4459
4460 /* Host associated PDTs can get confused with their constructors
4461 because they ar instantiated in the template's namespace. */
4462 if (!dt_sym)
4463 {
4464 if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4465 {
4466 gfc_error ("Type name %qs at %C is ambiguous", name);
4467 return MATCH_ERROR;
4468 }
4469 if (dt_sym && !dt_sym->attr.pdt_type)
4470 dt_sym = NULL;
4471 }
4472 }
4473 else if (ts->kind == -1)
4474 {
4475 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
4476 || gfc_current_ns->has_import_set;
4477 gfc_find_symbol (name, NULL, iface, &sym);
4478 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
4479 {
4480 gfc_error ("Type name %qs at %C is ambiguous", name);
4481 return MATCH_ERROR;
4482 }
4483 if (sym && sym->generic && !dt_sym)
4484 dt_sym = gfc_find_dt_in_generic (sym);
4485
4486 ts->kind = 0;
4487 if (sym == NULL)
4488 return MATCH_NO;
4489 }
4490
4491 if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT
4492 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
4493 || sym->attr.subroutine)
4494 {
4495 gfc_error ("Type name %qs at %C conflicts with previously declared "
4496 "entity at %L, which has the same name", name,
4497 &sym->declared_at);
4498 return MATCH_ERROR;
4499 }
4500
4501 if (sym && sym->attr.flavor == FL_DERIVED
4502 && sym->attr.pdt_template
4503 && gfc_current_state () != COMP_DERIVED)
4504 {
4505 m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL);
4506 if (m != MATCH_YES)
4507 return m;
4508 gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type);
4509 ts->u.derived = sym;
4510 strcpy (name, gfc_dt_lower_string (sym->name));
4511 }
4512
4513 gfc_save_symbol_data (sym);
4514 gfc_set_sym_referenced (sym);
4515 if (!sym->attr.generic
4516 && !gfc_add_generic (&sym->attr, sym->name, NULL))
4517 return MATCH_ERROR;
4518
4519 if (!sym->attr.function
4520 && !gfc_add_function (&sym->attr, sym->name, NULL))
4521 return MATCH_ERROR;
4522
4523 if (dt_sym && dt_sym->attr.flavor == FL_DERIVED
4524 && dt_sym->attr.pdt_template
4525 && gfc_current_state () != COMP_DERIVED)
4526 {
4527 m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL);
4528 if (m != MATCH_YES)
4529 return m;
4530 gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type);
4531 }
4532
4533 if (!dt_sym)
4534 {
4535 gfc_interface *intr, *head;
4536
4537 /* Use upper case to save the actual derived-type symbol. */
4538 gfc_get_symbol (dt_name, NULL, &dt_sym);
4539 dt_sym->name = gfc_get_string ("%s", sym->name);
4540 head = sym->generic;
4541 intr = gfc_get_interface ();
4542 intr->sym = dt_sym;
4543 intr->where = gfc_current_locus;
4544 intr->next = head;
4545 sym->generic = intr;
4546 sym->attr.if_source = IFSRC_DECL;
4547 }
4548 else
4549 gfc_save_symbol_data (dt_sym);
4550
4551 gfc_set_sym_referenced (dt_sym);
4552
4553 if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT
4554 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
4555 return MATCH_ERROR;
4556
4557 ts->u.derived = dt_sym;
4558
4559 return MATCH_YES;
4560
4561get_kind:
4562 if (matched_type
4563 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
4564 "intrinsic-type-spec at %C"))
4565 return MATCH_ERROR;
4566
4567 /* For all types except double, derived and character, look for an
4568 optional kind specifier. MATCH_NO is actually OK at this point. */
4569 if (implicit_flag == 1)
4570 {
4571 if (matched_type && gfc_match_char (')') != MATCH_YES)
4572 return MATCH_ERROR;
4573
4574 return MATCH_YES;
4575 }
4576
4577 if (gfc_current_form == FORM_FREE)
4578 {
4579 c = gfc_peek_ascii_char ();
4580 if (!gfc_is_whitespace (c) && c != '*' && c != '('
4581 && c != ':' && c != ',')
4582 {
4583 if (matched_type && c == ')')
4584 {
4585 gfc_next_ascii_char ();
4586 return MATCH_YES;
4587 }
4588 gfc_error ("Malformed type-spec at %C");
4589 return MATCH_NO;
4590 }
4591 }
4592
4593 m = gfc_match_kind_spec (ts, false);
4594 if (m == MATCH_NO && ts->type != BT_CHARACTER)
4595 {
4596 m = gfc_match_old_kind_spec (ts);
4597 if (gfc_validate_kind (ts->type, ts->kind, true) == -1)
4598 return MATCH_ERROR;
4599 }
4600
4601 if (matched_type && gfc_match_char (')') != MATCH_YES)
4602 {
4603 gfc_error ("Malformed type-spec at %C");
4604 return MATCH_ERROR;
4605 }
4606
4607 /* Defer association of the KIND expression of function results
4608 until after USE and IMPORT statements. */
4609 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
4610 || gfc_matching_function)
4611 return MATCH_YES;
4612
4613 if (m == MATCH_NO)
4614 m = MATCH_YES; /* No kind specifier found. */
4615
4616 return m;
4617}
4618
4619
4620/* Match an IMPLICIT NONE statement. Actually, this statement is
4621 already matched in parse.c, or we would not end up here in the
4622 first place. So the only thing we need to check, is if there is
4623 trailing garbage. If not, the match is successful. */
4624
4625match
4626gfc_match_implicit_none (void)
4627{
4628 char c;
4629 match m;
4630 char name[GFC_MAX_SYMBOL_LEN + 1];
4631 bool type = false;
4632 bool external = false;
4633 locus cur_loc = gfc_current_locus;
4634
4635 if (gfc_current_ns->seen_implicit_none
4636 || gfc_current_ns->has_implicit_none_export)
4637 {
4638 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
4639 return MATCH_ERROR;
4640 }
4641
4642 gfc_gobble_whitespace ();
4643 c = gfc_peek_ascii_char ();
4644 if (c == '(')
4645 {
4646 (void) gfc_next_ascii_char ();
4647 if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C"))
4648 return MATCH_ERROR;
4649
4650 gfc_gobble_whitespace ();
4651 if (gfc_peek_ascii_char () == ')')
4652 {
4653 (void) gfc_next_ascii_char ();
4654 type = true;
4655 }
4656 else
4657 for(;;)
4658 {
4659 m = gfc_match (" %n", name);
4660 if (m != MATCH_YES)
4661 return MATCH_ERROR;
4662
4663 if (strcmp (name, "type") == 0)
4664 type = true;
4665 else if (strcmp (name, "external") == 0)
4666 external = true;
4667 else
4668 return MATCH_ERROR;
4669
4670 gfc_gobble_whitespace ();
4671 c = gfc_next_ascii_char ();
4672 if (c == ',')
4673 continue;
4674 if (c == ')')
4675 break;
4676 return MATCH_ERROR;
4677 }
4678 }
4679 else
4680 type = true;
4681
4682 if (gfc_match_eos () != MATCH_YES)
4683 return MATCH_ERROR;
4684
4685 gfc_set_implicit_none (type, external, &cur_loc);
4686
4687 return MATCH_YES;
4688}
4689
4690
4691/* Match the letter range(s) of an IMPLICIT statement. */
4692
4693static match
4694match_implicit_range (void)
4695{
4696 char c, c1, c2;
4697 int inner;
4698 locus cur_loc;
4699
4700 cur_loc = gfc_current_locus;
4701
4702 gfc_gobble_whitespace ();
4703 c = gfc_next_ascii_char ();
4704 if (c != '(')
4705 {
4706 gfc_error ("Missing character range in IMPLICIT at %C");
4707 goto bad;
4708 }
4709
4710 inner = 1;
4711 while (inner)
4712 {
4713 gfc_gobble_whitespace ();
4714 c1 = gfc_next_ascii_char ();
4715 if (!ISALPHA (c1))
4716 goto bad;
4717
4718 gfc_gobble_whitespace ();
4719 c = gfc_next_ascii_char ();
4720
4721 switch (c)
4722 {
4723 case ')':
4724 inner = 0; /* Fall through. */
4725
4726 case ',':
4727 c2 = c1;
4728 break;
4729
4730 case '-':
4731 gfc_gobble_whitespace ();
4732 c2 = gfc_next_ascii_char ();
4733 if (!ISALPHA (c2))
4734 goto bad;
4735
4736 gfc_gobble_whitespace ();
4737 c = gfc_next_ascii_char ();
4738
4739 if ((c != ',') && (c != ')'))
4740 goto bad;
4741 if (c == ')')
4742 inner = 0;
4743
4744 break;
4745
4746 default:
4747 goto bad;
4748 }
4749
4750 if (c1 > c2)
4751 {
4752 gfc_error ("Letters must be in alphabetic order in "
4753 "IMPLICIT statement at %C");
4754 goto bad;
4755 }
4756
4757 /* See if we can add the newly matched range to the pending
4758 implicits from this IMPLICIT statement. We do not check for
4759 conflicts with whatever earlier IMPLICIT statements may have
4760 set. This is done when we've successfully finished matching
4761 the current one. */
4762 if (!gfc_add_new_implicit_range (c1, c2))
4763 goto bad;
4764 }
4765
4766 return MATCH_YES;
4767
4768bad:
4769 gfc_syntax_error (ST_IMPLICIT);
4770
4771 gfc_current_locus = cur_loc;
4772 return MATCH_ERROR;
4773}
4774
4775
4776/* Match an IMPLICIT statement, storing the types for
4777 gfc_set_implicit() if the statement is accepted by the parser.
4778 There is a strange looking, but legal syntactic construction
4779 possible. It looks like:
4780
4781 IMPLICIT INTEGER (a-b) (c-d)
4782
4783 This is legal if "a-b" is a constant expression that happens to
4784 equal one of the legal kinds for integers. The real problem
4785 happens with an implicit specification that looks like:
4786
4787 IMPLICIT INTEGER (a-b)
4788
4789 In this case, a typespec matcher that is "greedy" (as most of the
4790 matchers are) gobbles the character range as a kindspec, leaving
4791 nothing left. We therefore have to go a bit more slowly in the
4792 matching process by inhibiting the kindspec checking during
4793 typespec matching and checking for a kind later. */
4794
4795match
4796gfc_match_implicit (void)
4797{
4798 gfc_typespec ts;
4799 locus cur_loc;
4800 char c;
4801 match m;
4802
4803 if (gfc_current_ns->seen_implicit_none)
4804 {
4805 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
4806 "statement");
4807 return MATCH_ERROR;
4808 }
4809
4810 gfc_clear_ts (&ts);
4811
4812 /* We don't allow empty implicit statements. */
4813 if (gfc_match_eos () == MATCH_YES)
4814 {
4815 gfc_error ("Empty IMPLICIT statement at %C");
4816 return MATCH_ERROR;
4817 }
4818
4819 do
4820 {
4821 /* First cleanup. */
4822 gfc_clear_new_implicit ();
4823
4824 /* A basic type is mandatory here. */
4825 m = gfc_match_decl_type_spec (&ts, 1);
4826 if (m == MATCH_ERROR)
4827 goto error;
4828 if (m == MATCH_NO)
4829 goto syntax;
4830
4831 cur_loc = gfc_current_locus;
4832 m = match_implicit_range ();
4833
4834 if (m == MATCH_YES)
4835 {
4836 /* We may have <TYPE> (<RANGE>). */
4837 gfc_gobble_whitespace ();
4838 c = gfc_peek_ascii_char ();
4839 if (c == ',' || c == '\n' || c == ';' || c == '!')
4840 {
4841 /* Check for CHARACTER with no length parameter. */
4842 if (ts.type == BT_CHARACTER && !ts.u.cl)
4843 {
4844 ts.kind = gfc_default_character_kind;
4845 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4846 ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
4847 NULL, 1);
4848 }
4849
4850 /* Record the Successful match. */
4851 if (!gfc_merge_new_implicit (&ts))
4852 return MATCH_ERROR;
4853 if (c == ',')
4854 c = gfc_next_ascii_char ();
4855 else if (gfc_match_eos () == MATCH_ERROR)
4856 goto error;
4857 continue;
4858 }
4859
4860 gfc_current_locus = cur_loc;
4861 }
4862
4863 /* Discard the (incorrectly) matched range. */
4864 gfc_clear_new_implicit ();
4865
4866 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
4867 if (ts.type == BT_CHARACTER)
4868 m = gfc_match_char_spec (&ts);
4869 else if (gfc_numeric_ts(&ts) || ts.type == BT_LOGICAL)
4870 {
4871 m = gfc_match_kind_spec (&ts, false);
4872 if (m == MATCH_NO)
4873 {
4874 m = gfc_match_old_kind_spec (&ts);
4875 if (m == MATCH_ERROR)
4876 goto error;
4877 if (m == MATCH_NO)
4878 goto syntax;
4879 }
4880 }
4881 if (m == MATCH_ERROR)
4882 goto error;
4883
4884 m = match_implicit_range ();
4885 if (m == MATCH_ERROR)
4886 goto error;
4887 if (m == MATCH_NO)
4888 goto syntax;
4889
4890 gfc_gobble_whitespace ();
4891 c = gfc_next_ascii_char ();
4892 if (c != ',' && gfc_match_eos () != MATCH_YES)
4893 goto syntax;
4894
4895 if (!gfc_merge_new_implicit (&ts))
4896 return MATCH_ERROR;
4897 }
4898 while (c == ',');
4899
4900 return MATCH_YES;
4901
4902syntax:
4903 gfc_syntax_error (ST_IMPLICIT);
4904
4905error:
4906 return MATCH_ERROR;
4907}
4908
4909
4910match
4911gfc_match_import (void)
4912{
4913 char name[GFC_MAX_SYMBOL_LEN + 1];
4914 match m;
4915 gfc_symbol *sym;
4916 gfc_symtree *st;
4917
4918 if (gfc_current_ns->proc_name == NULL
4919 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
4920 {
4921 gfc_error ("IMPORT statement at %C only permitted in "
4922 "an INTERFACE body");
4923 return MATCH_ERROR;
4924 }
4925
4926 if (gfc_current_ns->proc_name->attr.module_procedure)
4927 {
4928 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
4929 "in a module procedure interface body");
4930 return MATCH_ERROR;
4931 }
4932
4933 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
4934 return MATCH_ERROR;
4935
4936 if (gfc_match_eos () == MATCH_YES)
4937 {
4938 /* All host variables should be imported. */
4939 gfc_current_ns->has_import_set = 1;
4940 return MATCH_YES;
4941 }
4942
4943 if (gfc_match (" ::") == MATCH_YES)
4944 {
4945 if (gfc_match_eos () == MATCH_YES)
4946 {
4947 gfc_error ("Expecting list of named entities at %C");
4948 return MATCH_ERROR;
4949 }
4950 }
4951
4952 for(;;)
4953 {
4954 sym = NULL;
4955 m = gfc_match (" %n", name);
4956 switch (m)
4957 {
4958 case MATCH_YES:
4959 if (gfc_current_ns->parent != NULL
4960 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
4961 {
4962 gfc_error ("Type name %qs at %C is ambiguous", name);
4963 return MATCH_ERROR;
4964 }
4965 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
4966 && gfc_find_symbol (name,
4967 gfc_current_ns->proc_name->ns->parent,
4968 1, &sym))
4969 {
4970 gfc_error ("Type name %qs at %C is ambiguous", name);
4971 return MATCH_ERROR;
4972 }
4973
4974 if (sym == NULL)
4975 {
4976 gfc_error ("Cannot IMPORT %qs from host scoping unit "
4977 "at %C - does not exist.", name);
4978 return MATCH_ERROR;
4979 }
4980
4981 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
4982 {
4983 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
4984 "at %C", name);
4985 goto next_item;
4986 }
4987
4988 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4989 st->n.sym = sym;
4990 sym->refs++;
4991 sym->attr.imported = 1;
4992
4993 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
4994 {
4995 /* The actual derived type is stored in a symtree with the first
4996 letter of the name capitalized; the symtree with the all
4997 lower-case name contains the associated generic function. */
4998 st = gfc_new_symtree (&gfc_current_ns->sym_root,
4999 gfc_dt_upper_string (name));
5000 st->n.sym = sym;
5001 sym->refs++;
5002 sym->attr.imported = 1;
5003 }
5004
5005 goto next_item;
5006
5007 case MATCH_NO:
5008 break;
5009
5010 case MATCH_ERROR:
5011 return MATCH_ERROR;
5012 }
5013
5014 next_item:
5015 if (gfc_match_eos () == MATCH_YES)
5016 break;
5017 if (gfc_match_char (',') != MATCH_YES)
5018 goto syntax;
5019 }
5020
5021 return MATCH_YES;
5022
5023syntax:
5024 gfc_error ("Syntax error in IMPORT statement at %C");
5025 return MATCH_ERROR;
5026}
5027
5028
5029/* A minimal implementation of gfc_match without whitespace, escape
5030 characters or variable arguments. Returns true if the next
5031 characters match the TARGET template exactly. */
5032
5033static bool
5034match_string_p (const char *target)
5035{
5036 const char *p;
5037
5038 for (p = target; *p; p++)
5039 if ((char) gfc_next_ascii_char () != *p)
5040 return false;
5041 return true;
5042}
5043
5044/* Matches an attribute specification including array specs. If
5045 successful, leaves the variables current_attr and current_as
5046 holding the specification. Also sets the colon_seen variable for
5047 later use by matchers associated with initializations.
5048
5049 This subroutine is a little tricky in the sense that we don't know
5050 if we really have an attr-spec until we hit the double colon.
5051 Until that time, we can only return MATCH_NO. This forces us to
5052 check for duplicate specification at this level. */
5053
5054static match
5055match_attr_spec (void)
5056{
5057 /* Modifiers that can exist in a type statement. */
5058 enum
5059 { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN,
5060 DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT,
5061 DECL_DIMENSION, DECL_EXTERNAL,
5062 DECL_INTRINSIC, DECL_OPTIONAL,
5063 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
5064 DECL_STATIC, DECL_AUTOMATIC,
5065 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
5066 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
5067 DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */
5068 };
5069
5070/* GFC_DECL_END is the sentinel, index starts at 0. */
5071#define NUM_DECL GFC_DECL_END
5072
5073 /* Make sure that values from sym_intent are safe to be used here. */
5074 gcc_assert (INTENT_IN > 0);
5075
5076 locus start, seen_at[NUM_DECL];
5077 int seen[NUM_DECL];
5078 unsigned int d;
5079 const char *attr;
5080 match m;
5081 bool t;
5082
5083 gfc_clear_attr (&current_attr);
5084 start = gfc_current_locus;
5085
5086 current_as = NULL;
5087 colon_seen = 0;
5088 attr_seen = 0;
5089
5090 /* See if we get all of the keywords up to the final double colon. */
5091 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5092 seen[d] = 0;
5093
5094 for (;;)
5095 {
5096 char ch;
5097
5098 d = DECL_NONE;
5099 gfc_gobble_whitespace ();
5100
5101 ch = gfc_next_ascii_char ();
5102 if (ch == ':')
5103 {
5104 /* This is the successful exit condition for the loop. */
5105 if (gfc_next_ascii_char () == ':')
5106 break;
5107 }
5108 else if (ch == ',')
5109 {
5110 gfc_gobble_whitespace ();
5111 switch (gfc_peek_ascii_char ())
5112 {
5113 case 'a':
5114 gfc_next_ascii_char ();
5115 switch (gfc_next_ascii_char ())
5116 {
5117 case 'l':
5118 if (match_string_p ("locatable"))
5119 {
5120 /* Matched "allocatable". */
5121 d = DECL_ALLOCATABLE;
5122 }
5123 break;
5124
5125 case 's':
5126 if (match_string_p ("ynchronous"))
5127 {
5128 /* Matched "asynchronous". */
5129 d = DECL_ASYNCHRONOUS;
5130 }
5131 break;
5132
5133 case 'u':
5134 if (match_string_p ("tomatic"))
5135 {
5136 /* Matched "automatic". */
5137 d = DECL_AUTOMATIC;
5138 }
5139 break;
5140 }
5141 break;
5142
5143 case 'b':
5144 /* Try and match the bind(c). */
5145 m = gfc_match_bind_c (NULL, true);
5146 if (m == MATCH_YES)
5147 d = DECL_IS_BIND_C;
5148 else if (m == MATCH_ERROR)
5149 goto cleanup;
5150 break;
5151
5152 case 'c':
5153 gfc_next_ascii_char ();
5154 if ('o' != gfc_next_ascii_char ())
5155 break;
5156 switch (gfc_next_ascii_char ())
5157 {
5158 case 'd':
5159 if (match_string_p ("imension"))
5160 {
5161 d = DECL_CODIMENSION;
5162 break;
5163 }
5164 /* FALLTHRU */
5165 case 'n':
5166 if (match_string_p ("tiguous"))
5167 {
5168 d = DECL_CONTIGUOUS;
5169 break;
5170 }
5171 }
5172 break;
5173
5174 case 'd':
5175 if (match_string_p ("dimension"))
5176 d = DECL_DIMENSION;
5177 break;
5178
5179 case 'e':
5180 if (match_string_p ("external"))
5181 d = DECL_EXTERNAL;
5182 break;
5183
5184 case 'i':
5185 if (match_string_p ("int"))
5186 {
5187 ch = gfc_next_ascii_char ();
5188 if (ch == 'e')
5189 {
5190 if (match_string_p ("nt"))
5191 {
5192 /* Matched "intent". */
5193 d = match_intent_spec ();
5194 if (d == INTENT_UNKNOWN)
5195 {
5196 m = MATCH_ERROR;
5197 goto cleanup;
5198 }
5199 }
5200 }
5201 else if (ch == 'r')
5202 {
5203 if (match_string_p ("insic"))
5204 {
5205 /* Matched "intrinsic". */
5206 d = DECL_INTRINSIC;
5207 }
5208 }
5209 }
5210 break;
5211
5212 case 'k':
5213 if (match_string_p ("kind"))
5214 d = DECL_KIND;
5215 break;
5216
5217 case 'l':
5218 if (match_string_p ("len"))
5219 d = DECL_LEN;
5220 break;
5221
5222 case 'o':
5223 if (match_string_p ("optional"))
5224 d = DECL_OPTIONAL;
5225 break;
5226
5227 case 'p':
5228 gfc_next_ascii_char ();
5229 switch (gfc_next_ascii_char ())
5230 {
5231 case 'a':
5232 if (match_string_p ("rameter"))
5233 {
5234 /* Matched "parameter". */
5235 d = DECL_PARAMETER;
5236 }
5237 break;
5238
5239 case 'o':
5240 if (match_string_p ("inter"))
5241 {
5242 /* Matched "pointer". */
5243 d = DECL_POINTER;
5244 }
5245 break;
5246
5247 case 'r':
5248 ch = gfc_next_ascii_char ();
5249 if (ch == 'i')
5250 {
5251 if (match_string_p ("vate"))
5252 {
5253 /* Matched "private". */
5254 d = DECL_PRIVATE;
5255 }
5256 }
5257 else if (ch == 'o')
5258 {
5259 if (match_string_p ("tected"))
5260 {
5261 /* Matched "protected". */
5262 d = DECL_PROTECTED;
5263 }
5264 }
5265 break;
5266
5267 case 'u':
5268 if (match_string_p ("blic"))
5269 {
5270 /* Matched "public". */
5271 d = DECL_PUBLIC;
5272 }
5273 break;
5274 }
5275 break;
5276
5277 case 's':
5278 gfc_next_ascii_char ();
5279 switch (gfc_next_ascii_char ())
5280 {
5281 case 'a':
5282 if (match_string_p ("ve"))
5283 {
5284 /* Matched "save". */
5285 d = DECL_SAVE;
5286 }
5287 break;
5288
5289 case 't':
5290 if (match_string_p ("atic"))
5291 {
5292 /* Matched "static". */
5293 d = DECL_STATIC;
5294 }
5295 break;
5296 }
5297 break;
5298
5299 case 't':
5300 if (match_string_p ("target"))
5301 d = DECL_TARGET;
5302 break;
5303
5304 case 'v':
5305 gfc_next_ascii_char ();
5306 ch = gfc_next_ascii_char ();
5307 if (ch == 'a')
5308 {
5309 if (match_string_p ("lue"))
5310 {
5311 /* Matched "value". */
5312 d = DECL_VALUE;
5313 }
5314 }
5315 else if (ch == 'o')
5316 {
5317 if (match_string_p ("latile"))
5318 {
5319 /* Matched "volatile". */
5320 d = DECL_VOLATILE;
5321 }
5322 }
5323 break;
5324 }
5325 }
5326
5327 /* No double colon and no recognizable decl_type, so assume that
5328 we've been looking at something else the whole time. */
5329 if (d == DECL_NONE)
5330 {
5331 m = MATCH_NO;
5332 goto cleanup;
5333 }
5334
5335 /* Check to make sure any parens are paired up correctly. */
5336 if (gfc_match_parens () == MATCH_ERROR)
5337 {
5338 m = MATCH_ERROR;
5339 goto cleanup;
5340 }
5341
5342 seen[d]++;
5343 seen_at[d] = gfc_current_locus;
5344
5345 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
5346 {
5347 gfc_array_spec *as = NULL;
5348
5349 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
5350 d == DECL_CODIMENSION);
5351
5352 if (current_as == NULL)
5353 current_as = as;
5354 else if (m == MATCH_YES)
5355 {
5356 if (!merge_array_spec (as, current_as, false))
5357 m = MATCH_ERROR;
5358 free (as);
5359 }
5360
5361 if (m == MATCH_NO)
5362 {
5363 if (d == DECL_CODIMENSION)
5364 gfc_error ("Missing codimension specification at %C");
5365 else
5366 gfc_error ("Missing dimension specification at %C");
5367 m = MATCH_ERROR;
5368 }
5369
5370 if (m == MATCH_ERROR)
5371 goto cleanup;
5372 }
5373 }
5374
5375 /* Since we've seen a double colon, we have to be looking at an
5376 attr-spec. This means that we can now issue errors. */
5377 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5378 if (seen[d] > 1)
5379 {
5380 switch (d)
5381 {
5382 case DECL_ALLOCATABLE:
5383 attr = "ALLOCATABLE";
5384 break;
5385 case DECL_ASYNCHRONOUS:
5386 attr = "ASYNCHRONOUS";
5387 break;
5388 case DECL_CODIMENSION:
5389 attr = "CODIMENSION";
5390 break;
5391 case DECL_CONTIGUOUS:
5392 attr = "CONTIGUOUS";
5393 break;
5394 case DECL_DIMENSION:
5395 attr = "DIMENSION";
5396 break;
5397 case DECL_EXTERNAL:
5398 attr = "EXTERNAL";
5399 break;
5400 case DECL_IN:
5401 attr = "INTENT (IN)";
5402 break;
5403 case DECL_OUT:
5404 attr = "INTENT (OUT)";
5405 break;
5406 case DECL_INOUT:
5407 attr = "INTENT (IN OUT)";
5408 break;
5409 case DECL_INTRINSIC:
5410 attr = "INTRINSIC";
5411 break;
5412 case DECL_OPTIONAL:
5413 attr = "OPTIONAL";
5414 break;
5415 case DECL_KIND:
5416 attr = "KIND";
5417 break;
5418 case DECL_LEN:
5419 attr = "LEN";
5420 break;
5421 case DECL_PARAMETER:
5422 attr = "PARAMETER";
5423 break;
5424 case DECL_POINTER:
5425 attr = "POINTER";
5426 break;
5427 case DECL_PROTECTED:
5428 attr = "PROTECTED";
5429 break;
5430 case DECL_PRIVATE:
5431 attr = "PRIVATE";
5432 break;
5433 case DECL_PUBLIC:
5434 attr = "PUBLIC";
5435 break;
5436 case DECL_SAVE:
5437 attr = "SAVE";
5438 break;
5439 case DECL_STATIC:
5440 attr = "STATIC";
5441 break;
5442 case DECL_AUTOMATIC:
5443 attr = "AUTOMATIC";
5444 break;
5445 case DECL_TARGET:
5446 attr = "TARGET";
5447 break;
5448 case DECL_IS_BIND_C:
5449 attr = "IS_BIND_C";
5450 break;
5451 case DECL_VALUE:
5452 attr = "VALUE";
5453 break;
5454 case DECL_VOLATILE:
5455 attr = "VOLATILE";
5456 break;
5457 default:
5458 attr = NULL; /* This shouldn't happen. */
5459 }
5460
5461 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
5462 m = MATCH_ERROR;
5463 goto cleanup;
5464 }
5465
5466 /* Now that we've dealt with duplicate attributes, add the attributes
5467 to the current attribute. */
5468 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
5469 {
5470 if (seen[d] == 0)
5471 continue;
5472 else
5473 attr_seen = 1;
5474
5475 if ((d == DECL_STATIC || d == DECL_AUTOMATIC)
5476 && !flag_dec_static)
5477 {
5478 gfc_error ("%s at %L is a DEC extension, enable with "
5479 "%<-fdec-static%>",
5480 d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]);
5481 m = MATCH_ERROR;
5482 goto cleanup;
5483 }
5484 /* Allow SAVE with STATIC, but don't complain. */
5485 if (d == DECL_STATIC && seen[DECL_SAVE])
5486 continue;
5487
5488 if (gfc_comp_struct (gfc_current_state ())
5489 && d != DECL_DIMENSION && d != DECL_CODIMENSION
5490 && d != DECL_POINTER && d != DECL_PRIVATE
5491 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
5492 {
5493 bool is_derived = gfc_current_state () == COMP_DERIVED;
5494 if (d == DECL_ALLOCATABLE)
5495 {
5496 if (!gfc_notify_std (GFC_STD_F2003, is_derived
5497 ? G_("ALLOCATABLE attribute at %C in a "
5498 "TYPE definition")
5499 : G_("ALLOCATABLE attribute at %C in a "
5500 "STRUCTURE definition")))
5501 {
5502 m = MATCH_ERROR;
5503 goto cleanup;
5504 }
5505 }
5506 else if (d == DECL_KIND)
5507 {
5508 if (!gfc_notify_std (GFC_STD_F2003, is_derived
5509 ? G_("KIND attribute at %C in a "
5510 "TYPE definition")
5511 : G_("KIND attribute at %C in a "
5512 "STRUCTURE definition")))
5513 {
5514 m = MATCH_ERROR;
5515 goto cleanup;
5516 }
5517 if (current_ts.type != BT_INTEGER)
5518 {
5519 gfc_error ("Component with KIND attribute at %C must be "
5520 "INTEGER");
5521 m = MATCH_ERROR;
5522 goto cleanup;
5523 }
5524 }
5525 else if (d == DECL_LEN)
5526 {
5527 if (!gfc_notify_std (GFC_STD_F2003, is_derived
5528 ? G_("LEN attribute at %C in a "
5529 "TYPE definition")
5530 : G_("LEN attribute at %C in a "
5531 "STRUCTURE definition")))
5532 {
5533 m = MATCH_ERROR;
5534 goto cleanup;
5535 }
5536 if (current_ts.type != BT_INTEGER)
5537 {
5538 gfc_error ("Component with LEN attribute at %C must be "
5539 "INTEGER");
5540 m = MATCH_ERROR;
5541 goto cleanup;
5542 }
5543 }
5544 else
5545 {
5546 gfc_error (is_derived ? G_("Attribute at %L is not allowed in a "
5547 "TYPE definition")
5548 : G_("Attribute at %L is not allowed in a "
5549 "STRUCTURE definition"), &seen_at[d]);
5550 m = MATCH_ERROR;
5551 goto cleanup;
5552 }
5553 }
5554
5555 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
5556 && gfc_current_state () != COMP_MODULE)
5557 {
5558 if (d == DECL_PRIVATE)
5559 attr = "PRIVATE";
5560 else
5561 attr = "PUBLIC";
5562 if (gfc_current_state () == COMP_DERIVED
5563 && gfc_state_stack->previous
5564 && gfc_state_stack->previous->state == COMP_MODULE)
5565 {
5566 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
5567 "at %L in a TYPE definition", attr,
5568 &seen_at[d]))
5569 {
5570 m = MATCH_ERROR;
5571 goto cleanup;
5572 }
5573 }
5574 else
5575 {
5576 gfc_error ("%s attribute at %L is not allowed outside of the "
5577 "specification part of a module", attr, &seen_at[d]);
5578 m = MATCH_ERROR;
5579 goto cleanup;
5580 }
5581 }
5582
5583 if (gfc_current_state () != COMP_DERIVED
5584 && (d == DECL_KIND || d == DECL_LEN))
5585 {
5586 gfc_error ("Attribute at %L is not allowed outside a TYPE "
5587 "definition", &seen_at[d]);
5588 m = MATCH_ERROR;
5589 goto cleanup;
5590 }
5591
5592 switch (d)
5593 {
5594 case DECL_ALLOCATABLE:
5595 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
5596 break;
5597
5598 case DECL_ASYNCHRONOUS:
5599 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
5600 t = false;
5601 else
5602 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
5603 break;
5604
5605 case DECL_CODIMENSION:
5606 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
5607 break;
5608
5609 case DECL_CONTIGUOUS:
5610 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
5611 t = false;
5612 else
5613 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
5614 break;
5615
5616 case DECL_DIMENSION:
5617 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
5618 break;
5619
5620 case DECL_EXTERNAL:
5621 t = gfc_add_external (&current_attr, &seen_at[d]);
5622 break;
5623
5624 case DECL_IN:
5625 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
5626 break;
5627
5628 case DECL_OUT:
5629 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
5630 break;
5631
5632 case DECL_INOUT:
5633 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
5634 break;
5635
5636 case DECL_INTRINSIC:
5637 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
5638 break;
5639
5640 case DECL_OPTIONAL:
5641 t = gfc_add_optional (&current_attr, &seen_at[d]);
5642 break;
5643
5644 case DECL_KIND:
5645 t = gfc_add_kind (&current_attr, &seen_at[d]);
5646 break;
5647
5648 case DECL_LEN:
5649 t = gfc_add_len (&current_attr, &seen_at[d]);
5650 break;
5651
5652 case DECL_PARAMETER:
5653 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
5654 break;
5655
5656 case DECL_POINTER:
5657 t = gfc_add_pointer (&current_attr, &seen_at[d]);
5658 break;
5659
5660 case DECL_PROTECTED:
5661 if (gfc_current_state () != COMP_MODULE
5662 || (gfc_current_ns->proc_name
5663 && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
5664 {
5665 gfc_error ("PROTECTED at %C only allowed in specification "
5666 "part of a module");
5667 t = false;
5668 break;
5669 }
5670
5671 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
5672 t = false;
5673 else
5674 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
5675 break;
5676
5677 case DECL_PRIVATE:
5678 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
5679 &seen_at[d]);
5680 break;
5681
5682 case DECL_PUBLIC:
5683 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
5684 &seen_at[d]);
5685 break;
5686
5687 case DECL_STATIC:
5688 case DECL_SAVE:
5689 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
5690 break;
5691
5692 case DECL_AUTOMATIC:
5693 t = gfc_add_automatic (&current_attr, NULL, &seen_at[d]);
5694 break;
5695
5696 case DECL_TARGET:
5697 t = gfc_add_target (&current_attr, &seen_at[d]);
5698 break;
5699
5700 case DECL_IS_BIND_C:
5701 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
5702 break;
5703
5704 case DECL_VALUE:
5705 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
5706 t = false;
5707 else
5708 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
5709 break;
5710
5711 case DECL_VOLATILE:
5712 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
5713 t = false;
5714 else
5715 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
5716 break;
5717
5718 default:
5719 gfc_internal_error ("match_attr_spec(): Bad attribute");
5720 }
5721
5722 if (!t)
5723 {
5724 m = MATCH_ERROR;
5725 goto cleanup;
5726 }
5727 }
5728
5729 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
5730 if ((gfc_current_state () == COMP_MODULE
5731 || gfc_current_state () == COMP_SUBMODULE)
5732 && !current_attr.save
5733 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5734 current_attr.save = SAVE_IMPLICIT;
5735
5736 colon_seen = 1;
5737 return MATCH_YES;
5738
5739cleanup:
5740 gfc_current_locus = start;
5741 gfc_free_array_spec (current_as);
5742 current_as = NULL;
5743 attr_seen = 0;
5744 return m;
5745}
5746
5747
5748/* Set the binding label, dest_label, either with the binding label
5749 stored in the given gfc_typespec, ts, or if none was provided, it
5750 will be the symbol name in all lower case, as required by the draft
5751 (J3/04-007, section 15.4.1). If a binding label was given and
5752 there is more than one argument (num_idents), it is an error. */
5753
5754static bool
5755set_binding_label (const char **dest_label, const char *sym_name,
5756 int num_idents)
5757{
5758 if (num_idents > 1 && has_name_equals)
5759 {
5760 gfc_error ("Multiple identifiers provided with "
5761 "single NAME= specifier at %C");
5762 return false;
5763 }
5764
5765 if (curr_binding_label)
5766 /* Binding label given; store in temp holder till have sym. */
5767 *dest_label = curr_binding_label;
5768 else
5769 {
5770 /* No binding label given, and the NAME= specifier did not exist,
5771 which means there was no NAME="". */
5772 if (sym_name != NULL && has_name_equals == 0)
5773 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
5774 }
5775
5776 return true;
5777}
5778
5779
5780/* Set the status of the given common block as being BIND(C) or not,
5781 depending on the given parameter, is_bind_c. */
5782
5783void
5784set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
5785{
5786 com_block->is_bind_c = is_bind_c;
5787 return;
5788}
5789
5790
5791/* Verify that the given gfc_typespec is for a C interoperable type. */
5792
5793bool
5794gfc_verify_c_interop (gfc_typespec *ts)
5795{
5796 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
5797 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
5798 ? true : false;
5799 else if (ts->type == BT_CLASS)
5800 return false;
5801 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
5802 return false;
5803
5804 return true;
5805}
5806
5807
5808/* Verify that the variables of a given common block, which has been
5809 defined with the attribute specifier bind(c), to be of a C
5810 interoperable type. Errors will be reported here, if
5811 encountered. */
5812
5813bool
5814verify_com_block_vars_c_interop (gfc_common_head *com_block)
5815{
5816 gfc_symbol *curr_sym = NULL;
5817 bool retval = true;
5818
5819 curr_sym = com_block->head;
5820
5821 /* Make sure we have at least one symbol. */
5822 if (curr_sym == NULL)
5823 return retval;
5824
5825 /* Here we know we have a symbol, so we'll execute this loop
5826 at least once. */
5827 do
5828 {
5829 /* The second to last param, 1, says this is in a common block. */
5830 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
5831 curr_sym = curr_sym->common_next;
5832 } while (curr_sym != NULL);
5833
5834 return retval;
5835}
5836
5837
5838/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
5839 an appropriate error message is reported. */
5840
5841bool
5842verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
5843 int is_in_common, gfc_common_head *com_block)
5844{
5845 bool bind_c_function = false;
5846 bool retval = true;
5847
5848 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
5849 bind_c_function = true;
5850
5851 if (tmp_sym->attr.function && tmp_sym->result != NULL)
5852 {
5853 tmp_sym = tmp_sym->result;
5854 /* Make sure it wasn't an implicitly typed result. */
5855 if (tmp_sym->attr.implicit_type && warn_c_binding_type)
5856 {
5857 gfc_warning (OPT_Wc_binding_type,
5858 "Implicitly declared BIND(C) function %qs at "
5859 "%L may not be C interoperable", tmp_sym->name,
5860 &tmp_sym->declared_at);
5861 tmp_sym->ts.f90_type = tmp_sym->ts.type;
5862 /* Mark it as C interoperable to prevent duplicate warnings. */
5863 tmp_sym->ts.is_c_interop = 1;
5864 tmp_sym->attr.is_c_interop = 1;
5865 }
5866 }
5867
5868 /* Here, we know we have the bind(c) attribute, so if we have
5869 enough type info, then verify that it's a C interop kind.
5870 The info could be in the symbol already, or possibly still in
5871 the given ts (current_ts), so look in both. */
5872 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
5873 {
5874 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
5875 {
5876 /* See if we're dealing with a sym in a common block or not. */
5877 if (is_in_common == 1 && warn_c_binding_type)
5878 {
5879 gfc_warning (OPT_Wc_binding_type,
5880 "Variable %qs in common block %qs at %L "
5881 "may not be a C interoperable "
5882 "kind though common block %qs is BIND(C)",
5883 tmp_sym->name, com_block->name,
5884 &(tmp_sym->declared_at), com_block->name);
5885 }
5886 else
5887 {
5888 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
5889 gfc_error ("Type declaration %qs at %L is not C "
5890 "interoperable but it is BIND(C)",
5891 tmp_sym->name, &(tmp_sym->declared_at));
5892 else if (warn_c_binding_type)
5893 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
5894 "may not be a C interoperable "
5895 "kind but it is BIND(C)",
5896 tmp_sym->name, &(tmp_sym->declared_at));
5897 }
5898 }
5899
5900 /* Variables declared w/in a common block can't be bind(c)
5901 since there's no way for C to see these variables, so there's
5902 semantically no reason for the attribute. */
5903 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
5904 {
5905 gfc_error ("Variable %qs in common block %qs at "
5906 "%L cannot be declared with BIND(C) "
5907 "since it is not a global",
5908 tmp_sym->name, com_block->name,
5909 &(tmp_sym->declared_at));
5910 retval = false;
5911 }
5912
5913 /* Scalar variables that are bind(c) cannot have the pointer
5914 or allocatable attributes. */
5915 if (tmp_sym->attr.is_bind_c == 1)
5916 {
5917 if (tmp_sym->attr.pointer == 1)
5918 {
5919 gfc_error ("Variable %qs at %L cannot have both the "
5920 "POINTER and BIND(C) attributes",
5921 tmp_sym->name, &(tmp_sym->declared_at));
5922 retval = false;
5923 }
5924
5925 if (tmp_sym->attr.allocatable == 1)
5926 {
5927 gfc_error ("Variable %qs at %L cannot have both the "
5928 "ALLOCATABLE and BIND(C) attributes",
5929 tmp_sym->name, &(tmp_sym->declared_at));
5930 retval = false;
5931 }
5932
5933 }
5934
5935 /* If it is a BIND(C) function, make sure the return value is a
5936 scalar value. The previous tests in this function made sure
5937 the type is interoperable. */
5938 if (bind_c_function && tmp_sym->as != NULL)
5939 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
5940 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
5941
5942 /* BIND(C) functions cannot return a character string. */
5943 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
5944 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
5945 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5946 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
5947 gfc_error ("Return type of BIND(C) function %qs of character "
5948 "type at %L must have length 1", tmp_sym->name,
5949 &(tmp_sym->declared_at));
5950 }
5951
5952 /* See if the symbol has been marked as private. If it has, make sure
5953 there is no binding label and warn the user if there is one. */
5954 if (tmp_sym->attr.access == ACCESS_PRIVATE
5955 && tmp_sym->binding_label)
5956 /* Use gfc_warning_now because we won't say that the symbol fails
5957 just because of this. */
5958 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
5959 "given the binding label %qs", tmp_sym->name,
5960 &(tmp_sym->declared_at), tmp_sym->binding_label);
5961
5962 return retval;
5963}
5964
5965
5966/* Set the appropriate fields for a symbol that's been declared as
5967 BIND(C) (the is_bind_c flag and the binding label), and verify that
5968 the type is C interoperable. Errors are reported by the functions
5969 used to set/test these fields. */
5970
5971bool
5972set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
5973{
5974 bool retval = true;
5975
5976 /* TODO: Do we need to make sure the vars aren't marked private? */
5977
5978 /* Set the is_bind_c bit in symbol_attribute. */
5979 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
5980
5981 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
5982 return false;
5983
5984 return retval;
5985}
5986
5987
5988/* Set the fields marking the given common block as BIND(C), including
5989 a binding label, and report any errors encountered. */
5990
5991bool
5992set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
5993{
5994 bool retval = true;
5995
5996 /* destLabel, common name, typespec (which may have binding label). */
5997 if (!set_binding_label (&com_block->binding_label, com_block->name,
5998 num_idents))
5999 return false;
6000
6001 /* Set the given common block (com_block) to being bind(c) (1). */
6002 set_com_block_bind_c (com_block, 1);
6003
6004 return retval;
6005}
6006
6007
6008/* Retrieve the list of one or more identifiers that the given bind(c)
6009 attribute applies to. */
6010
6011bool
6012get_bind_c_idents (void)
6013{
6014 char name[GFC_MAX_SYMBOL_LEN + 1];
6015 int num_idents = 0;
6016 gfc_symbol *tmp_sym = NULL;
6017 match found_id;
6018 gfc_common_head *com_block = NULL;
6019
6020 if (gfc_match_name (name) == MATCH_YES)
6021 {
6022 found_id = MATCH_YES;
6023 gfc_get_ha_symbol (name, &tmp_sym);
6024 }
6025 else if (gfc_match_common_name (name) == MATCH_YES)
6026 {
6027 found_id = MATCH_YES;
6028 com_block = gfc_get_common (name, 0);
6029 }
6030 else
6031 {
6032 gfc_error ("Need either entity or common block name for "
6033 "attribute specification statement at %C");
6034 return false;
6035 }
6036
6037 /* Save the current identifier and look for more. */
6038 do
6039 {
6040 /* Increment the number of identifiers found for this spec stmt. */
6041 num_idents++;
6042
6043 /* Make sure we have a sym or com block, and verify that it can
6044 be bind(c). Set the appropriate field(s) and look for more
6045 identifiers. */
6046 if (tmp_sym != NULL || com_block != NULL)
6047 {
6048 if (tmp_sym != NULL)
6049 {
6050 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
6051 return false;
6052 }
6053 else
6054 {
6055 if (!set_verify_bind_c_com_block (com_block, num_idents))
6056 return false;
6057 }
6058
6059 /* Look to see if we have another identifier. */
6060 tmp_sym = NULL;
6061 if (gfc_match_eos () == MATCH_YES)
6062 found_id = MATCH_NO;
6063 else if (gfc_match_char (',') != MATCH_YES)
6064 found_id = MATCH_NO;
6065 else if (gfc_match_name (name) == MATCH_YES)
6066 {
6067 found_id = MATCH_YES;
6068 gfc_get_ha_symbol (name, &tmp_sym);
6069 }
6070 else if (gfc_match_common_name (name) == MATCH_YES)
6071 {
6072 found_id = MATCH_YES;
6073 com_block = gfc_get_common (name, 0);
6074 }
6075 else
6076 {
6077 gfc_error ("Missing entity or common block name for "
6078 "attribute specification statement at %C");
6079 return false;
6080 }
6081 }
6082 else
6083 {
6084 gfc_internal_error ("Missing symbol");
6085 }
6086 } while (found_id == MATCH_YES);
6087
6088 /* if we get here we were successful */
6089 return true;
6090}
6091
6092
6093/* Try and match a BIND(C) attribute specification statement. */
6094
6095match
6096gfc_match_bind_c_stmt (void)
6097{
6098 match found_match = MATCH_NO;
6099 gfc_typespec *ts;
6100
6101 ts = &current_ts;
6102
6103 /* This may not be necessary. */
6104 gfc_clear_ts (ts);
6105 /* Clear the temporary binding label holder. */
6106 curr_binding_label = NULL;
6107
6108 /* Look for the bind(c). */
6109 found_match = gfc_match_bind_c (NULL, true);
6110
6111 if (found_match == MATCH_YES)
6112 {
6113 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
6114 return MATCH_ERROR;
6115
6116 /* Look for the :: now, but it is not required. */
6117 gfc_match (" :: ");
6118
6119 /* Get the identifier(s) that needs to be updated. This may need to
6120 change to hand the flag(s) for the attr specified so all identifiers
6121 found can have all appropriate parts updated (assuming that the same
6122 spec stmt can have multiple attrs, such as both bind(c) and
6123 allocatable...). */
6124 if (!get_bind_c_idents ())
6125 /* Error message should have printed already. */
6126 return MATCH_ERROR;
6127 }
6128
6129 return found_match;
6130}
6131
6132
6133/* Match a data declaration statement. */
6134
6135match
6136gfc_match_data_decl (void)
6137{
6138 gfc_symbol *sym;
6139 match m;
6140 int elem;
6141
6142 type_param_spec_list = NULL;
6143 decl_type_param_list = NULL;
6144
6145 num_idents_on_line = 0;
6146
6147 m = gfc_match_decl_type_spec (&current_ts, 0);
6148 if (m != MATCH_YES)
6149 return m;
6150
6151 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6152 && !gfc_comp_struct (gfc_current_state ()))
6153 {
6154 sym = gfc_use_derived (current_ts.u.derived);
6155
6156 if (sym == NULL)
6157 {
6158 m = MATCH_ERROR;
6159 goto cleanup;
6160 }
6161
6162 current_ts.u.derived = sym;
6163 }
6164
6165 m = match_attr_spec ();
6166 if (m == MATCH_ERROR)
6167 {
6168 m = MATCH_NO;
6169 goto cleanup;
6170 }
6171
6172 if (current_ts.type == BT_CLASS
6173 && current_ts.u.derived->attr.unlimited_polymorphic)
6174 goto ok;
6175
6176 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
6177 && current_ts.u.derived->components == NULL
6178 && !current_ts.u.derived->attr.zero_comp)
6179 {
6180
6181 if (current_attr.pointer && gfc_comp_struct (gfc_current_state ()))
6182 goto ok;
6183
6184 if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED)
6185 goto ok;
6186
6187 gfc_find_symbol (current_ts.u.derived->name,
6188 current_ts.u.derived->ns, 1, &sym);
6189
6190 /* Any symbol that we find had better be a type definition
6191 which has its components defined, or be a structure definition
6192 actively being parsed. */
6193 if (sym != NULL && gfc_fl_struct (sym->attr.flavor)
6194 && (current_ts.u.derived->components != NULL
6195 || current_ts.u.derived->attr.zero_comp
6196 || current_ts.u.derived == gfc_new_block))
6197 goto ok;
6198
6199 gfc_error ("Derived type at %C has not been previously defined "
6200 "and so cannot appear in a derived type definition");
6201 m = MATCH_ERROR;
6202 goto cleanup;
6203 }
6204
6205ok:
6206 /* If we have an old-style character declaration, and no new-style
6207 attribute specifications, then there a comma is optional between
6208 the type specification and the variable list. */
6209 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
6210 gfc_match_char (',');
6211
6212 /* Give the types/attributes to symbols that follow. Give the element
6213 a number so that repeat character length expressions can be copied. */
6214 elem = 1;
6215 for (;;)
6216 {
6217 num_idents_on_line++;
6218 m = variable_decl (elem++);
6219 if (m == MATCH_ERROR)
6220 goto cleanup;
6221 if (m == MATCH_NO)
6222 break;
6223
6224 if (gfc_match_eos () == MATCH_YES)
6225 goto cleanup;
6226 if (gfc_match_char (',') != MATCH_YES)
6227 break;
6228 }
6229
6230 if (!gfc_error_flag_test ())
6231 {
6232 /* An anonymous structure declaration is unambiguous; if we matched one
6233 according to gfc_match_structure_decl, we need to return MATCH_YES
6234 here to avoid confusing the remaining matchers, even if there was an
6235 error during variable_decl. We must flush any such errors. Note this
6236 causes the parser to gracefully continue parsing the remaining input
6237 as a structure body, which likely follows. */
6238 if (current_ts.type == BT_DERIVED && current_ts.u.derived
6239 && gfc_fl_struct (current_ts.u.derived->attr.flavor))
6240 {
6241 gfc_error_now ("Syntax error in anonymous structure declaration"
6242 " at %C");
6243 /* Skip the bad variable_decl and line up for the start of the
6244 structure body. */
6245 gfc_error_recovery ();
6246 m = MATCH_YES;
6247 goto cleanup;
6248 }
6249
6250 gfc_error ("Syntax error in data declaration at %C");
6251 }
6252
6253 m = MATCH_ERROR;
6254
6255 gfc_free_data_all (gfc_current_ns);
6256
6257cleanup:
6258 if (saved_kind_expr)
6259 gfc_free_expr (saved_kind_expr);
6260 if (type_param_spec_list)
6261 gfc_free_actual_arglist (type_param_spec_list);
6262 if (decl_type_param_list)
6263 gfc_free_actual_arglist (decl_type_param_list);
6264 saved_kind_expr = NULL;
6265 gfc_free_array_spec (current_as);
6266 current_as = NULL;
6267 return m;
6268}
6269
6270static bool
6271in_module_or_interface(void)
6272{
6273 if (gfc_current_state () == COMP_MODULE
6274 || gfc_current_state () == COMP_SUBMODULE
6275 || gfc_current_state () == COMP_INTERFACE)
6276 return true;
6277
6278 if (gfc_state_stack->state == COMP_CONTAINS
6279 || gfc_state_stack->state == COMP_FUNCTION
6280 || gfc_state_stack->state == COMP_SUBROUTINE)
6281 {
6282 gfc_state_data *p;
6283 for (p = gfc_state_stack->previous; p ; p = p->previous)
6284 {
6285 if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE
6286 || p->state == COMP_INTERFACE)
6287 return true;
6288 }
6289 }
6290 return false;
6291}
6292
6293/* Match a prefix associated with a function or subroutine
6294 declaration. If the typespec pointer is nonnull, then a typespec
6295 can be matched. Note that if nothing matches, MATCH_YES is
6296 returned (the null string was matched). */
6297
6298match
6299gfc_match_prefix (gfc_typespec *ts)
6300{
6301 bool seen_type;
6302 bool seen_impure;
6303 bool found_prefix;
6304
6305 gfc_clear_attr (&current_attr);
6306 seen_type = false;
6307 seen_impure = false;
6308
6309 gcc_assert (!gfc_matching_prefix);
6310 gfc_matching_prefix = true;
6311
6312 do
6313 {
6314 found_prefix = false;
6315
6316 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
6317 corresponding attribute seems natural and distinguishes these
6318 procedures from procedure types of PROC_MODULE, which these are
6319 as well. */
6320 if (gfc_match ("module% ") == MATCH_YES)
6321 {
6322 if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
6323 goto error;
6324
6325 if (!in_module_or_interface ())
6326 {
6327 gfc_error ("MODULE prefix at %C found outside of a module, "
6328 "submodule, or interface");
6329 goto error;
6330 }
6331
6332 current_attr.module_procedure = 1;
6333 found_prefix = true;
6334 }
6335
6336 if (!seen_type && ts != NULL)
6337 {
6338 match m;
6339 m = gfc_match_decl_type_spec (ts, 0);
6340 if (m == MATCH_ERROR)
6341 goto error;
6342 if (m == MATCH_YES && gfc_match_space () == MATCH_YES)
6343 {
6344 seen_type = true;
6345 found_prefix = true;
6346 }
6347 }
6348
6349 if (gfc_match ("elemental% ") == MATCH_YES)
6350 {
6351 if (!gfc_add_elemental (&current_attr, NULL))
6352 goto error;
6353
6354 found_prefix = true;
6355 }
6356
6357 if (gfc_match ("pure% ") == MATCH_YES)
6358 {
6359 if (!gfc_add_pure (&current_attr, NULL))
6360 goto error;
6361
6362 found_prefix = true;
6363 }
6364
6365 if (gfc_match ("recursive% ") == MATCH_YES)
6366 {
6367 if (!gfc_add_recursive (&current_attr, NULL))
6368 goto error;
6369
6370 found_prefix = true;
6371 }
6372
6373 /* IMPURE is a somewhat special case, as it needs not set an actual
6374 attribute but rather only prevents ELEMENTAL routines from being
6375 automatically PURE. */
6376 if (gfc_match ("impure% ") == MATCH_YES)
6377 {
6378 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
6379 goto error;
6380
6381 seen_impure = true;
6382 found_prefix = true;
6383 }
6384 }
6385 while (found_prefix);
6386
6387 /* IMPURE and PURE must not both appear, of course. */
6388 if (seen_impure && current_attr.pure)
6389 {
6390 gfc_error ("PURE and IMPURE must not appear both at %C");
6391 goto error;
6392 }
6393
6394 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
6395 if (!seen_impure && current_attr.elemental && !current_attr.pure)
6396 {
6397 if (!gfc_add_pure (&current_attr, NULL))
6398 goto error;
6399 }
6400
6401 /* At this point, the next item is not a prefix. */
6402 gcc_assert (gfc_matching_prefix);
6403
6404 gfc_matching_prefix = false;
6405 return MATCH_YES;
6406
6407error:
6408 gcc_assert (gfc_matching_prefix);
6409 gfc_matching_prefix = false;
6410 return MATCH_ERROR;
6411}
6412
6413
6414/* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
6415
6416static bool
6417copy_prefix (symbol_attribute *dest, locus *where)
6418{
6419 if (dest->module_procedure)
6420 {
6421 if (current_attr.elemental)
6422 dest->elemental = 1;
6423
6424 if (current_attr.pure)
6425 dest->pure = 1;
6426
6427 if (current_attr.recursive)
6428 dest->recursive = 1;
6429
6430 /* Module procedures are unusual in that the 'dest' is copied from
6431 the interface declaration. However, this is an oportunity to
6432 check that the submodule declaration is compliant with the
6433 interface. */
6434 if (dest->elemental && !current_attr.elemental)
6435 {
6436 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
6437 "missing at %L", where);
6438 return false;
6439 }
6440
6441 if (dest->pure && !current_attr.pure)
6442 {
6443 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
6444 "missing at %L", where);
6445 return false;
6446 }
6447
6448 if (dest->recursive && !current_attr.recursive)
6449 {
6450 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
6451 "missing at %L", where);
6452 return false;
6453 }
6454
6455 return true;
6456 }
6457
6458 if (current_attr.elemental && !gfc_add_elemental (dest, where))
6459 return false;
6460
6461 if (current_attr.pure && !gfc_add_pure (dest, where))
6462 return false;
6463
6464 if (current_attr.recursive && !gfc_add_recursive (dest, where))
6465 return false;
6466
6467 return true;
6468}
6469
6470
6471/* Match a formal argument list or, if typeparam is true, a
6472 type_param_name_list. */
6473
6474match
6475gfc_match_formal_arglist (gfc_symbol *progname, int st_flag,
6476 int null_flag, bool typeparam)
6477{
6478 gfc_formal_arglist *head, *tail, *p, *q;
6479 char name[GFC_MAX_SYMBOL_LEN + 1];
6480 gfc_symbol *sym;
6481 match m;
6482 gfc_formal_arglist *formal = NULL;
6483
6484 head = tail = NULL;
6485
6486 /* Keep the interface formal argument list and null it so that the
6487 matching for the new declaration can be done. The numbers and
6488 names of the arguments are checked here. The interface formal
6489 arguments are retained in formal_arglist and the characteristics
6490 are compared in resolve.c(resolve_fl_procedure). See the remark
6491 in get_proc_name about the eventual need to copy the formal_arglist
6492 and populate the formal namespace of the interface symbol. */
6493 if (progname->attr.module_procedure
6494 && progname->attr.host_assoc)
6495 {
6496 formal = progname->formal;
6497 progname->formal = NULL;
6498 }
6499
6500 if (gfc_match_char ('(') != MATCH_YES)
6501 {
6502 if (null_flag)
6503 goto ok;
6504 return MATCH_NO;
6505 }
6506
6507 if (gfc_match_char (')') == MATCH_YES)
6508 {
6509 if (typeparam)
6510 {
6511 gfc_error_now ("A type parameter list is required at %C");
6512 m = MATCH_ERROR;
6513 goto cleanup;
6514 }
6515 else
6516 goto ok;
6517 }
6518
6519 for (;;)
6520 {
6521 if (gfc_match_char ('*') == MATCH_YES)
6522 {
6523 sym = NULL;
6524 if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS,
6525 "Alternate-return argument at %C"))
6526 {
6527 m = MATCH_ERROR;
6528 goto cleanup;
6529 }
6530 else if (typeparam)
6531 gfc_error_now ("A parameter name is required at %C");
6532 }
6533 else
6534 {
6535 m = gfc_match_name (name);
6536 if (m != MATCH_YES)
6537 {
6538 if(typeparam)
6539 gfc_error_now ("A parameter name is required at %C");
6540 goto cleanup;
6541 }
6542
6543 if (!typeparam && gfc_get_symbol (name, NULL, &sym))
6544 goto cleanup;
6545 else if (typeparam
6546 && gfc_get_symbol (name, progname->f2k_derived, &sym))
6547 goto cleanup;
6548 }
6549
6550 p = gfc_get_formal_arglist ();
6551
6552 if (head == NULL)
6553 head = tail = p;
6554 else
6555 {
6556 tail->next = p;
6557 tail = p;
6558 }
6559
6560 tail->sym = sym;
6561
6562 /* We don't add the VARIABLE flavor because the name could be a
6563 dummy procedure. We don't apply these attributes to formal
6564 arguments of statement functions. */
6565 if (sym != NULL && !st_flag
6566 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
6567 || !gfc_missing_attr (&sym->attr, NULL)))
6568 {
6569 m = MATCH_ERROR;
6570 goto cleanup;
6571 }
6572
6573 /* The name of a program unit can be in a different namespace,
6574 so check for it explicitly. After the statement is accepted,
6575 the name is checked for especially in gfc_get_symbol(). */
6576 if (gfc_new_block != NULL && sym != NULL && !typeparam
6577 && strcmp (sym->name, gfc_new_block->name) == 0)
6578 {
6579 gfc_error ("Name %qs at %C is the name of the procedure",
6580 sym->name);
6581 m = MATCH_ERROR;
6582 goto cleanup;
6583 }
6584
6585 if (gfc_match_char (')') == MATCH_YES)
6586 goto ok;
6587
6588 m = gfc_match_char (',');
6589 if (m != MATCH_YES)
6590 {
6591 if (typeparam)
6592 gfc_error_now ("Expected parameter list in type declaration "
6593 "at %C");
6594 else
6595 gfc_error ("Unexpected junk in formal argument list at %C");
6596 goto cleanup;
6597 }
6598 }
6599
6600ok:
6601 /* Check for duplicate symbols in the formal argument list. */
6602 if (head != NULL)
6603 {
6604 for (p = head; p->next; p = p->next)
6605 {
6606 if (p->sym == NULL)
6607 continue;
6608
6609 for (q = p->next; q; q = q->next)
6610 if (p->sym == q->sym)
6611 {
6612 if (typeparam)
6613 gfc_error_now ("Duplicate name %qs in parameter "
6614 "list at %C", p->sym->name);
6615 else
6616 gfc_error ("Duplicate symbol %qs in formal argument "
6617 "list at %C", p->sym->name);
6618
6619 m = MATCH_ERROR;
6620 goto cleanup;
6621 }
6622 }
6623 }
6624
6625 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
6626 {
6627 m = MATCH_ERROR;
6628 goto cleanup;
6629 }
6630
6631 /* gfc_error_now used in following and return with MATCH_YES because
6632 doing otherwise results in a cascade of extraneous errors and in
6633 some cases an ICE in symbol.c(gfc_release_symbol). */
6634 if (progname->attr.module_procedure && progname->attr.host_assoc)
6635 {
6636 bool arg_count_mismatch = false;
6637
6638 if (!formal && head)
6639 arg_count_mismatch = true;
6640
6641 /* Abbreviated module procedure declaration is not meant to have any
6642 formal arguments! */
6643 if (!progname->abr_modproc_decl && formal && !head)
6644 arg_count_mismatch = true;
6645
6646 for (p = formal, q = head; p && q; p = p->next, q = q->next)
6647 {
6648 if ((p->next != NULL && q->next == NULL)
6649 || (p->next == NULL && q->next != NULL))
6650 arg_count_mismatch = true;
6651 else if ((p->sym == NULL && q->sym == NULL)
6652 || strcmp (p->sym->name, q->sym->name) == 0)
6653 continue;
6654 else
6655 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
6656 "argument names (%s/%s) at %C",
6657 p->sym->name, q->sym->name);
6658 }
6659
6660 if (arg_count_mismatch)
6661 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
6662 "formal arguments at %C");
6663 }
6664
6665 return MATCH_YES;
6666
6667cleanup:
6668 gfc_free_formal_arglist (head);
6669 return m;
6670}
6671
6672
6673/* Match a RESULT specification following a function declaration or
6674 ENTRY statement. Also matches the end-of-statement. */
6675
6676static match
6677match_result (gfc_symbol *function, gfc_symbol **result)
6678{
6679 char name[GFC_MAX_SYMBOL_LEN + 1];
6680 gfc_symbol *r;
6681 match m;
6682
6683 if (gfc_match (" result (") != MATCH_YES)
6684 return MATCH_NO;
6685
6686 m = gfc_match_name (name);
6687 if (m != MATCH_YES)
6688 return m;
6689
6690 /* Get the right paren, and that's it because there could be the
6691 bind(c) attribute after the result clause. */
6692 if (gfc_match_char (')') != MATCH_YES)
6693 {
6694 /* TODO: should report the missing right paren here. */
6695 return MATCH_ERROR;
6696 }
6697
6698 if (strcmp (function->name, name) == 0)
6699 {
6700 gfc_error ("RESULT variable at %C must be different than function name");
6701 return MATCH_ERROR;
6702 }
6703
6704 if (gfc_get_symbol (name, NULL, &r))
6705 return MATCH_ERROR;
6706
6707 if (!gfc_add_result (&r->attr, r->name, NULL))
6708 return MATCH_ERROR;
6709
6710 *result = r;
6711
6712 return MATCH_YES;
6713}
6714
6715
6716/* Match a function suffix, which could be a combination of a result
6717 clause and BIND(C), either one, or neither. The draft does not
6718 require them to come in a specific order. */
6719
6720match
6721gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
6722{
6723 match is_bind_c; /* Found bind(c). */
6724 match is_result; /* Found result clause. */
6725 match found_match; /* Status of whether we've found a good match. */
6726 char peek_char; /* Character we're going to peek at. */
6727 bool allow_binding_name;
6728
6729 /* Initialize to having found nothing. */
6730 found_match = MATCH_NO;
6731 is_bind_c = MATCH_NO;
6732 is_result = MATCH_NO;
6733
6734 /* Get the next char to narrow between result and bind(c). */
6735 gfc_gobble_whitespace ();
6736 peek_char = gfc_peek_ascii_char ();
6737
6738 /* C binding names are not allowed for internal procedures. */
6739 if (gfc_current_state () == COMP_CONTAINS
6740 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6741 allow_binding_name = false;
6742 else
6743 allow_binding_name = true;
6744
6745 switch (peek_char)
6746 {
6747 case 'r':
6748 /* Look for result clause. */
6749 is_result = match_result (sym, result);
6750 if (is_result == MATCH_YES)
6751 {
6752 /* Now see if there is a bind(c) after it. */
6753 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6754 /* We've found the result clause and possibly bind(c). */
6755 found_match = MATCH_YES;
6756 }
6757 else
6758 /* This should only be MATCH_ERROR. */
6759 found_match = is_result;
6760 break;
6761 case 'b':
6762 /* Look for bind(c) first. */
6763 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
6764 if (is_bind_c == MATCH_YES)
6765 {
6766 /* Now see if a result clause followed it. */
6767 is_result = match_result (sym, result);
6768 found_match = MATCH_YES;
6769 }
6770 else
6771 {
6772 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
6773 found_match = MATCH_ERROR;
6774 }
6775 break;
6776 default:
6777 gfc_error ("Unexpected junk after function declaration at %C");
6778 found_match = MATCH_ERROR;
6779 break;
6780 }
6781
6782 if (is_bind_c == MATCH_YES)
6783 {
6784 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
6785 if (gfc_current_state () == COMP_CONTAINS
6786 && sym->ns->proc_name->attr.flavor != FL_MODULE
6787 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6788 "at %L may not be specified for an internal "
6789 "procedure", &gfc_current_locus))
6790 return MATCH_ERROR;
6791
6792 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
6793 return MATCH_ERROR;
6794 }
6795
6796 return found_match;
6797}
6798
6799
6800/* Procedure pointer return value without RESULT statement:
6801 Add "hidden" result variable named "ppr@". */
6802
6803static bool
6804add_hidden_procptr_result (gfc_symbol *sym)
6805{
6806 bool case1,case2;
6807
6808 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
6809 return false;
6810
6811 /* First usage case: PROCEDURE and EXTERNAL statements. */
6812 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
6813 && strcmp (gfc_current_block ()->name, sym->name) == 0
6814 && sym->attr.external;
6815 /* Second usage case: INTERFACE statements. */
6816 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
6817 && gfc_state_stack->previous->state == COMP_FUNCTION
6818 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
6819
6820 if (case1 || case2)
6821 {
6822 gfc_symtree *stree;
6823 if (case1)
6824 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
6825 else
6826 {
6827 gfc_symtree *st2;
6828 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
6829 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
6830 st2->n.sym = stree->n.sym;
6831 stree->n.sym->refs++;
6832 }
6833 sym->result = stree->n.sym;
6834
6835 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
6836 sym->result->attr.pointer = sym->attr.pointer;
6837 sym->result->attr.external = sym->attr.external;
6838 sym->result->attr.referenced = sym->attr.referenced;
6839 sym->result->ts = sym->ts;
6840 sym->attr.proc_pointer = 0;
6841 sym->attr.pointer = 0;
6842 sym->attr.external = 0;
6843 if (sym->result->attr.external && sym->result->attr.pointer)
6844 {
6845 sym->result->attr.pointer = 0;
6846 sym->result->attr.proc_pointer = 1;
6847 }
6848
6849 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
6850 }
6851 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
6852 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
6853 && sym->result && sym->result != sym && sym->result->attr.external
6854 && sym == gfc_current_ns->proc_name
6855 && sym == sym->result->ns->proc_name
6856 && strcmp ("ppr@", sym->result->name) == 0)
6857 {
6858 sym->result->attr.proc_pointer = 1;
6859 sym->attr.pointer = 0;
6860 return true;
6861 }
6862 else
6863 return false;
6864}
6865
6866
6867/* Match the interface for a PROCEDURE declaration,
6868 including brackets (R1212). */
6869
6870static match
6871match_procedure_interface (gfc_symbol **proc_if)
6872{
6873 match m;
6874 gfc_symtree *st;
6875 locus old_loc, entry_loc;
6876 gfc_namespace *old_ns = gfc_current_ns;
6877 char name[GFC_MAX_SYMBOL_LEN + 1];
6878
6879 old_loc = entry_loc = gfc_current_locus;
6880 gfc_clear_ts (&current_ts);
6881
6882 if (gfc_match (" (") != MATCH_YES)
6883 {
6884 gfc_current_locus = entry_loc;
6885 return MATCH_NO;
6886 }
6887
6888 /* Get the type spec. for the procedure interface. */
6889 old_loc = gfc_current_locus;
6890 m = gfc_match_decl_type_spec (&current_ts, 0);
6891 gfc_gobble_whitespace ();
6892 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
6893 goto got_ts;
6894
6895 if (m == MATCH_ERROR)
6896 return m;
6897
6898 /* Procedure interface is itself a procedure. */
6899 gfc_current_locus = old_loc;
6900 m = gfc_match_name (name);
6901
6902 /* First look to see if it is already accessible in the current
6903 namespace because it is use associated or contained. */
6904 st = NULL;
6905 if (gfc_find_sym_tree (name, NULL, 0, &st))
6906 return MATCH_ERROR;
6907
6908 /* If it is still not found, then try the parent namespace, if it
6909 exists and create the symbol there if it is still not found. */
6910 if (gfc_current_ns->parent)
6911 gfc_current_ns = gfc_current_ns->parent;
6912 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
6913 return MATCH_ERROR;
6914
6915 gfc_current_ns = old_ns;
6916 *proc_if = st->n.sym;
6917
6918 if (*proc_if)
6919 {
6920 (*proc_if)->refs++;
6921 /* Resolve interface if possible. That way, attr.procedure is only set
6922 if it is declared by a later procedure-declaration-stmt, which is
6923 invalid per F08:C1216 (cf. resolve_procedure_interface). */
6924 while ((*proc_if)->ts.interface
6925 && *proc_if != (*proc_if)->ts.interface)
6926 *proc_if = (*proc_if)->ts.interface;
6927
6928 if ((*proc_if)->attr.flavor == FL_UNKNOWN
6929 && (*proc_if)->ts.type == BT_UNKNOWN
6930 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
6931 (*proc_if)->name, NULL))
6932 return MATCH_ERROR;
6933 }
6934
6935got_ts:
6936 if (gfc_match (" )") != MATCH_YES)
6937 {
6938 gfc_current_locus = entry_loc;
6939 return MATCH_NO;
6940 }
6941
6942 return MATCH_YES;
6943}
6944
6945
6946/* Match a PROCEDURE declaration (R1211). */
6947
6948static match
6949match_procedure_decl (void)
6950{
6951 match m;
6952 gfc_symbol *sym, *proc_if = NULL;
6953 int num;
6954 gfc_expr *initializer = NULL;
6955
6956 /* Parse interface (with brackets). */
6957 m = match_procedure_interface (&proc_if);
6958 if (m != MATCH_YES)
6959 return m;
6960
6961 /* Parse attributes (with colons). */
6962 m = match_attr_spec();
6963 if (m == MATCH_ERROR)
6964 return MATCH_ERROR;
6965
6966 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
6967 {
6968 current_attr.is_bind_c = 1;
6969 has_name_equals = 0;
6970 curr_binding_label = NULL;
6971 }
6972
6973 /* Get procedure symbols. */
6974 for(num=1;;num++)
6975 {
6976 m = gfc_match_symbol (&sym, 0);
6977 if (m == MATCH_NO)
6978 goto syntax;
6979 else if (m == MATCH_ERROR)
6980 return m;
6981
6982 /* Add current_attr to the symbol attributes. */
6983 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
6984 return MATCH_ERROR;
6985
6986 if (sym->attr.is_bind_c)
6987 {
6988 /* Check for C1218. */
6989 if (!proc_if || !proc_if->attr.is_bind_c)
6990 {
6991 gfc_error ("BIND(C) attribute at %C requires "
6992 "an interface with BIND(C)");
6993 return MATCH_ERROR;
6994 }
6995 /* Check for C1217. */
6996 if (has_name_equals && sym->attr.pointer)
6997 {
6998 gfc_error ("BIND(C) procedure with NAME may not have "
6999 "POINTER attribute at %C");
7000 return MATCH_ERROR;
7001 }
7002 if (has_name_equals && sym->attr.dummy)
7003 {
7004 gfc_error ("Dummy procedure at %C may not have "
7005 "BIND(C) attribute with NAME");
7006 return MATCH_ERROR;
7007 }
7008 /* Set binding label for BIND(C). */
7009 if (!set_binding_label (&sym->binding_label, sym->name, num))
7010 return MATCH_ERROR;
7011 }
7012
7013 if (!gfc_add_external (&sym->attr, NULL))
7014 return MATCH_ERROR;
7015
7016 if (add_hidden_procptr_result (sym))
7017 sym = sym->result;
7018
7019 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
7020 return MATCH_ERROR;
7021
7022 /* Set interface. */
7023 if (proc_if != NULL)
7024 {
7025 if (sym->ts.type != BT_UNKNOWN)
7026 {
7027 gfc_error ("Procedure %qs at %L already has basic type of %s",
7028 sym->name, &gfc_current_locus,
7029 gfc_basic_typename (sym->ts.type));
7030 return MATCH_ERROR;
7031 }
7032 sym->ts.interface = proc_if;
7033 sym->attr.untyped = 1;
7034 sym->attr.if_source = IFSRC_IFBODY;
7035 }
7036 else if (current_ts.type != BT_UNKNOWN)
7037 {
7038 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
7039 return MATCH_ERROR;
7040 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7041 sym->ts.interface->ts = current_ts;
7042 sym->ts.interface->attr.flavor = FL_PROCEDURE;
7043 sym->ts.interface->attr.function = 1;
7044 sym->attr.function = 1;
7045 sym->attr.if_source = IFSRC_UNKNOWN;
7046 }
7047
7048 if (gfc_match (" =>") == MATCH_YES)
7049 {
7050 if (!current_attr.pointer)
7051 {
7052 gfc_error ("Initialization at %C isn't for a pointer variable");
7053 m = MATCH_ERROR;
7054 goto cleanup;
7055 }
7056
7057 m = match_pointer_init (&initializer, 1);
7058 if (m != MATCH_YES)
7059 goto cleanup;
7060
7061 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
7062 goto cleanup;
7063
7064 }
7065
7066 if (gfc_match_eos () == MATCH_YES)
7067 return MATCH_YES;
7068 if (gfc_match_char (',') != MATCH_YES)
7069 goto syntax;
7070 }
7071
7072syntax:
7073 gfc_error ("Syntax error in PROCEDURE statement at %C");
7074 return MATCH_ERROR;
7075
7076cleanup:
7077 /* Free stuff up and return. */
7078 gfc_free_expr (initializer);
7079 return m;
7080}
7081
7082
7083static match
7084match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
7085
7086
7087/* Match a procedure pointer component declaration (R445). */
7088
7089static match
7090match_ppc_decl (void)
7091{
7092 match m;
7093 gfc_symbol *proc_if = NULL;
7094 gfc_typespec ts;
7095 int num;
7096 gfc_component *c;
7097 gfc_expr *initializer = NULL;
7098 gfc_typebound_proc* tb;
7099 char name[GFC_MAX_SYMBOL_LEN + 1];
7100
7101 /* Parse interface (with brackets). */
7102 m = match_procedure_interface (&proc_if);
7103 if (m != MATCH_YES)
7104 goto syntax;
7105
7106 /* Parse attributes. */
7107 tb = XCNEW (gfc_typebound_proc);
7108 tb->where = gfc_current_locus;
7109 m = match_binding_attributes (tb, false, true);
7110 if (m == MATCH_ERROR)
7111 return m;
7112
7113 gfc_clear_attr (&current_attr);
7114 current_attr.procedure = 1;
7115 current_attr.proc_pointer = 1;
7116 current_attr.access = tb->access;
7117 current_attr.flavor = FL_PROCEDURE;
7118
7119 /* Match the colons (required). */
7120 if (gfc_match (" ::") != MATCH_YES)
7121 {
7122 gfc_error ("Expected %<::%> after binding-attributes at %C");
7123 return MATCH_ERROR;
7124 }
7125
7126 /* Check for C450. */
7127 if (!tb->nopass && proc_if == NULL)
7128 {
7129 gfc_error("NOPASS or explicit interface required at %C");
7130 return MATCH_ERROR;
7131 }
7132
7133 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
7134 return MATCH_ERROR;
7135
7136 /* Match PPC names. */
7137 ts = current_ts;
7138 for(num=1;;num++)
7139 {
7140 m = gfc_match_name (name);
7141 if (m == MATCH_NO)
7142 goto syntax;
7143 else if (m == MATCH_ERROR)
7144 return m;
7145
7146 if (!gfc_add_component (gfc_current_block(), name, &c))
7147 return MATCH_ERROR;
7148
7149 /* Add current_attr to the symbol attributes. */
7150 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
7151 return MATCH_ERROR;
7152
7153 if (!gfc_add_external (&c->attr, NULL))
7154 return MATCH_ERROR;
7155
7156 if (!gfc_add_proc (&c->attr, name, NULL))
7157 return MATCH_ERROR;
7158
7159 if (num == 1)
7160 c->tb = tb;
7161 else
7162 {
7163 c->tb = XCNEW (gfc_typebound_proc);
7164 c->tb->where = gfc_current_locus;
7165 *c->tb = *tb;
7166 }
7167
7168 /* Set interface. */
7169 if (proc_if != NULL)
7170 {
7171 c->ts.interface = proc_if;
7172 c->attr.untyped = 1;
7173 c->attr.if_source = IFSRC_IFBODY;
7174 }
7175 else if (ts.type != BT_UNKNOWN)
7176 {
7177 c->ts = ts;
7178 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
7179 c->ts.interface->result = c->ts.interface;
7180 c->ts.interface->ts = ts;
7181 c->ts.interface->attr.flavor = FL_PROCEDURE;
7182 c->ts.interface->attr.function = 1;
7183 c->attr.function = 1;
7184 c->attr.if_source = IFSRC_UNKNOWN;
7185 }
7186
7187 if (gfc_match (" =>") == MATCH_YES)
7188 {
7189 m = match_pointer_init (&initializer, 1);
7190 if (m != MATCH_YES)
7191 {
7192 gfc_free_expr (initializer);
7193 return m;
7194 }
7195 c->initializer = initializer;
7196 }
7197
7198 if (gfc_match_eos () == MATCH_YES)
7199 return MATCH_YES;
7200 if (gfc_match_char (',') != MATCH_YES)
7201 goto syntax;
7202 }
7203
7204syntax:
7205 gfc_error ("Syntax error in procedure pointer component at %C");
7206 return MATCH_ERROR;
7207}
7208
7209
7210/* Match a PROCEDURE declaration inside an interface (R1206). */
7211
7212static match
7213match_procedure_in_interface (void)
7214{
7215 match m;
7216 gfc_symbol *sym;
7217 char name[GFC_MAX_SYMBOL_LEN + 1];
7218 locus old_locus;
7219
7220 if (current_interface.type == INTERFACE_NAMELESS
7221 || current_interface.type == INTERFACE_ABSTRACT)
7222 {
7223 gfc_error ("PROCEDURE at %C must be in a generic interface");
7224 return MATCH_ERROR;
7225 }
7226
7227 /* Check if the F2008 optional double colon appears. */
7228 gfc_gobble_whitespace ();
7229 old_locus = gfc_current_locus;
7230 if (gfc_match ("::") == MATCH_YES)
7231 {
7232 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7233 "MODULE PROCEDURE statement at %L", &old_locus))
7234 return MATCH_ERROR;
7235 }
7236 else
7237 gfc_current_locus = old_locus;
7238
7239 for(;;)
7240 {
7241 m = gfc_match_name (name);
7242 if (m == MATCH_NO)
7243 goto syntax;
7244 else if (m == MATCH_ERROR)
7245 return m;
7246 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
7247 return MATCH_ERROR;
7248
7249 if (!gfc_add_interface (sym))
7250 return MATCH_ERROR;
7251
7252 if (gfc_match_eos () == MATCH_YES)
7253 break;
7254 if (gfc_match_char (',') != MATCH_YES)
7255 goto syntax;
7256 }
7257
7258 return MATCH_YES;
7259
7260syntax:
7261 gfc_error ("Syntax error in PROCEDURE statement at %C");
7262 return MATCH_ERROR;
7263}
7264
7265
7266/* General matcher for PROCEDURE declarations. */
7267
7268static match match_procedure_in_type (void);
7269
7270match
7271gfc_match_procedure (void)
7272{
7273 match m;
7274
7275 switch (gfc_current_state ())
7276 {
7277 case COMP_NONE:
7278 case COMP_PROGRAM:
7279 case COMP_MODULE:
7280 case COMP_SUBMODULE:
7281 case COMP_SUBROUTINE:
7282 case COMP_FUNCTION:
7283 case COMP_BLOCK:
7284 m = match_procedure_decl ();
7285 break;
7286 case COMP_INTERFACE:
7287 m = match_procedure_in_interface ();
7288 break;
7289 case COMP_DERIVED:
7290 m = match_ppc_decl ();
7291 break;
7292 case COMP_DERIVED_CONTAINS:
7293 m = match_procedure_in_type ();
7294 break;
7295 default:
7296 return MATCH_NO;
7297 }
7298
7299 if (m != MATCH_YES)
7300 return m;
7301
7302 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
7303 return MATCH_ERROR;
7304
7305 return m;
7306}
7307
7308
7309/* Warn if a matched procedure has the same name as an intrinsic; this is
7310 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
7311 parser-state-stack to find out whether we're in a module. */
7312
7313static void
7314do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
7315{
7316 bool in_module;
7317
7318 in_module = (gfc_state_stack->previous
7319 && (gfc_state_stack->previous->state == COMP_MODULE
7320 || gfc_state_stack->previous->state == COMP_SUBMODULE));
7321
7322 gfc_warn_intrinsic_shadow (sym, in_module, func);
7323}
7324
7325
7326/* Match a function declaration. */
7327
7328match
7329gfc_match_function_decl (void)
7330{
7331 char name[GFC_MAX_SYMBOL_LEN + 1];
7332 gfc_symbol *sym, *result;
7333 locus old_loc;
7334 match m;
7335 match suffix_match;
7336 match found_match; /* Status returned by match func. */
7337
7338 if (gfc_current_state () != COMP_NONE
7339 && gfc_current_state () != COMP_INTERFACE
7340 && gfc_current_state () != COMP_CONTAINS)
7341 return MATCH_NO;
7342
7343 gfc_clear_ts (&current_ts);
7344
7345 old_loc = gfc_current_locus;
7346
7347 m = gfc_match_prefix (&current_ts);
7348 if (m != MATCH_YES)
7349 {
7350 gfc_current_locus = old_loc;
7351 return m;
7352 }
7353
7354 if (gfc_match ("function% %n", name) != MATCH_YES)
7355 {
7356 gfc_current_locus = old_loc;
7357 return MATCH_NO;
7358 }
7359
7360 if (get_proc_name (name, &sym, false))
7361 return MATCH_ERROR;
7362
7363 if (add_hidden_procptr_result (sym))
7364 sym = sym->result;
7365
7366 if (current_attr.module_procedure)
7367 sym->attr.module_procedure = 1;
7368
7369 gfc_new_block = sym;
7370
7371 m = gfc_match_formal_arglist (sym, 0, 0);
7372 if (m == MATCH_NO)
7373 {
7374 gfc_error ("Expected formal argument list in function "
7375 "definition at %C");
7376 m = MATCH_ERROR;
7377 goto cleanup;
7378 }
7379 else if (m == MATCH_ERROR)
7380 goto cleanup;
7381
7382 result = NULL;
7383
7384 /* According to the draft, the bind(c) and result clause can
7385 come in either order after the formal_arg_list (i.e., either
7386 can be first, both can exist together or by themselves or neither
7387 one). Therefore, the match_result can't match the end of the
7388 string, and check for the bind(c) or result clause in either order. */
7389 found_match = gfc_match_eos ();
7390
7391 /* Make sure that it isn't already declared as BIND(C). If it is, it
7392 must have been marked BIND(C) with a BIND(C) attribute and that is
7393 not allowed for procedures. */
7394 if (sym->attr.is_bind_c == 1)
7395 {
7396 sym->attr.is_bind_c = 0;
7397
7398 if (gfc_state_stack->previous
7399 && gfc_state_stack->previous->state != COMP_SUBMODULE)
7400 {
7401 locus loc;
7402 loc = sym->old_symbol != NULL
7403 ? sym->old_symbol->declared_at : gfc_current_locus;
7404 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7405 "variables or common blocks", &loc);
7406 }
7407 }
7408
7409 if (found_match != MATCH_YES)
7410 {
7411 /* If we haven't found the end-of-statement, look for a suffix. */
7412 suffix_match = gfc_match_suffix (sym, &result);
7413 if (suffix_match == MATCH_YES)
7414 /* Need to get the eos now. */
7415 found_match = gfc_match_eos ();
7416 else
7417 found_match = suffix_match;
7418 }
7419
7420 /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7421 subprogram and a binding label is specified, it shall be the
7422 same as the binding label specified in the corresponding module
7423 procedure interface body. */
7424 if (sym->attr.is_bind_c && sym->attr.module_procedure && sym->old_symbol
7425 && strcmp (sym->name, sym->old_symbol->name) == 0
7426 && sym->binding_label && sym->old_symbol->binding_label
7427 && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7428 {
7429 const char *null = "NULL", *s1, *s2;
7430 s1 = sym->binding_label;
7431 if (!s1) s1 = null;
7432 s2 = sym->old_symbol->binding_label;
7433 if (!s2) s2 = null;
7434 gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7435 sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
7436 return MATCH_ERROR;
7437 }
7438
7439 if(found_match != MATCH_YES)
7440 m = MATCH_ERROR;
7441 else
7442 {
7443 /* Make changes to the symbol. */
7444 m = MATCH_ERROR;
7445
7446 if (!gfc_add_function (&sym->attr, sym->name, NULL))
7447 goto cleanup;
7448
7449 if (!gfc_missing_attr (&sym->attr, NULL))
7450 goto cleanup;
7451
7452 if (!copy_prefix (&sym->attr, &sym->declared_at))
7453 {
7454 if(!sym->attr.module_procedure)
7455 goto cleanup;
7456 else
7457 gfc_error_check ();
7458 }
7459
7460 /* Delay matching the function characteristics until after the
7461 specification block by signalling kind=-1. */
7462 sym->declared_at = old_loc;
7463 if (current_ts.type != BT_UNKNOWN)
7464 current_ts.kind = -1;
7465 else
7466 current_ts.kind = 0;
7467
7468 if (result == NULL)
7469 {
7470 if (current_ts.type != BT_UNKNOWN
7471 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
7472 goto cleanup;
7473 sym->result = sym;
7474 }
7475 else
7476 {
7477 if (current_ts.type != BT_UNKNOWN
7478 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
7479 goto cleanup;
7480 sym->result = result;
7481 }
7482
7483 /* Warn if this procedure has the same name as an intrinsic. */
7484 do_warn_intrinsic_shadow (sym, true);
7485
7486 return MATCH_YES;
7487 }
7488
7489cleanup:
7490 gfc_current_locus = old_loc;
7491 return m;
7492}
7493
7494
7495/* This is mostly a copy of parse.c(add_global_procedure) but modified to
7496 pass the name of the entry, rather than the gfc_current_block name, and
7497 to return false upon finding an existing global entry. */
7498
7499static bool
7500add_global_entry (const char *name, const char *binding_label, bool sub,
7501 locus *where)
7502{
7503 gfc_gsymbol *s;
7504 enum gfc_symbol_type type;
7505
7506 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7507
7508 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7509 name is a global identifier. */
7510 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
7511 {
7512 s = gfc_get_gsymbol (name, false);
7513
7514 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7515 {
7516 gfc_global_used (s, where);
7517 return false;
7518 }
7519 else
7520 {
7521 s->type = type;
7522 s->sym_name = name;
7523 s->where = *where;
7524 s->defined = 1;
7525 s->ns = gfc_current_ns;
7526 }
7527 }
7528
7529 /* Don't add the symbol multiple times. */
7530 if (binding_label
7531 && (!gfc_notification_std (GFC_STD_F2008)
7532 || strcmp (name, binding_label) != 0))
7533 {
7534 s = gfc_get_gsymbol (binding_label, true);
7535
7536 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
7537 {
7538 gfc_global_used (s, where);
7539 return false;
7540 }
7541 else
7542 {
7543 s->type = type;
7544 s->sym_name = name;
7545 s->binding_label = binding_label;
7546 s->where = *where;
7547 s->defined = 1;
7548 s->ns = gfc_current_ns;
7549 }
7550 }
7551
7552 return true;
7553}
7554
7555
7556/* Match an ENTRY statement. */
7557
7558match
7559gfc_match_entry (void)
7560{
7561 gfc_symbol *proc;
7562 gfc_symbol *result;
7563 gfc_symbol *entry;
7564 char name[GFC_MAX_SYMBOL_LEN + 1];
7565 gfc_compile_state state;
7566 match m;
7567 gfc_entry_list *el;
7568 locus old_loc;
7569 bool module_procedure;
7570 char peek_char;
7571 match is_bind_c;
7572
7573 m = gfc_match_name (name);
7574 if (m != MATCH_YES)
7575 return m;
7576
7577 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
7578 return MATCH_ERROR;
7579
7580 state = gfc_current_state ();
7581 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
7582 {
7583 switch (state)
7584 {
7585 case COMP_PROGRAM:
7586 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
7587 break;
7588 case COMP_MODULE:
7589 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
7590 break;
7591 case COMP_SUBMODULE:
7592 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
7593 break;
7594 case COMP_BLOCK_DATA:
7595 gfc_error ("ENTRY statement at %C cannot appear within "
7596 "a BLOCK DATA");
7597 break;
7598 case COMP_INTERFACE:
7599 gfc_error ("ENTRY statement at %C cannot appear within "
7600 "an INTERFACE");
7601 break;
7602 case COMP_STRUCTURE:
7603 gfc_error ("ENTRY statement at %C cannot appear within "
7604 "a STRUCTURE block");
7605 break;
7606 case COMP_DERIVED:
7607 gfc_error ("ENTRY statement at %C cannot appear within "
7608 "a DERIVED TYPE block");
7609 break;
7610 case COMP_IF:
7611 gfc_error ("ENTRY statement at %C cannot appear within "
7612 "an IF-THEN block");
7613 break;
7614 case COMP_DO:
7615 case COMP_DO_CONCURRENT:
7616 gfc_error ("ENTRY statement at %C cannot appear within "
7617 "a DO block");
7618 break;
7619 case COMP_SELECT:
7620 gfc_error ("ENTRY statement at %C cannot appear within "
7621 "a SELECT block");
7622 break;
7623 case COMP_FORALL:
7624 gfc_error ("ENTRY statement at %C cannot appear within "
7625 "a FORALL block");
7626 break;
7627 case COMP_WHERE:
7628 gfc_error ("ENTRY statement at %C cannot appear within "
7629 "a WHERE block");
7630 break;
7631 case COMP_CONTAINS:
7632 gfc_error ("ENTRY statement at %C cannot appear within "
7633 "a contained subprogram");
7634 break;
7635 default:
7636 gfc_error ("Unexpected ENTRY statement at %C");
7637 }
7638 return MATCH_ERROR;
7639 }
7640
7641 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
7642 && gfc_state_stack->previous->state == COMP_INTERFACE)
7643 {
7644 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
7645 return MATCH_ERROR;
7646 }
7647
7648 module_procedure = gfc_current_ns->parent != NULL
7649 && gfc_current_ns->parent->proc_name
7650 && gfc_current_ns->parent->proc_name->attr.flavor
7651 == FL_MODULE;
7652
7653 if (gfc_current_ns->parent != NULL
7654 && gfc_current_ns->parent->proc_name
7655 && !module_procedure)
7656 {
7657 gfc_error("ENTRY statement at %C cannot appear in a "
7658 "contained procedure");
7659 return MATCH_ERROR;
7660 }
7661
7662 /* Module function entries need special care in get_proc_name
7663 because previous references within the function will have
7664 created symbols attached to the current namespace. */
7665 if (get_proc_name (name, &entry,
7666 gfc_current_ns->parent != NULL
7667 && module_procedure))
7668 return MATCH_ERROR;
7669
7670 proc = gfc_current_block ();
7671
7672 /* Make sure that it isn't already declared as BIND(C). If it is, it
7673 must have been marked BIND(C) with a BIND(C) attribute and that is
7674 not allowed for procedures. */
7675 if (entry->attr.is_bind_c == 1)
7676 {
7677 locus loc;
7678
7679 entry->attr.is_bind_c = 0;
7680
7681 loc = entry->old_symbol != NULL
7682 ? entry->old_symbol->declared_at : gfc_current_locus;
7683 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7684 "variables or common blocks", &loc);
7685 }
7686
7687 /* Check what next non-whitespace character is so we can tell if there
7688 is the required parens if we have a BIND(C). */
7689 old_loc = gfc_current_locus;
7690 gfc_gobble_whitespace ();
7691 peek_char = gfc_peek_ascii_char ();
7692
7693 if (state == COMP_SUBROUTINE)
7694 {
7695 m = gfc_match_formal_arglist (entry, 0, 1);
7696 if (m != MATCH_YES)
7697 return MATCH_ERROR;
7698
7699 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
7700 never be an internal procedure. */
7701 is_bind_c = gfc_match_bind_c (entry, true);
7702 if (is_bind_c == MATCH_ERROR)
7703 return MATCH_ERROR;
7704 if (is_bind_c == MATCH_YES)
7705 {
7706 if (peek_char != '(')
7707 {
7708 gfc_error ("Missing required parentheses before BIND(C) at %C");
7709 return MATCH_ERROR;
7710 }
7711
7712 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
7713 &(entry->declared_at), 1))
7714 return MATCH_ERROR;
7715
7716 }
7717
7718 if (!gfc_current_ns->parent
7719 && !add_global_entry (name, entry->binding_label, true,
7720 &old_loc))
7721 return MATCH_ERROR;
7722
7723 /* An entry in a subroutine. */
7724 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7725 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
7726 return MATCH_ERROR;
7727 }
7728 else
7729 {
7730 /* An entry in a function.
7731 We need to take special care because writing
7732 ENTRY f()
7733 as
7734 ENTRY f
7735 is allowed, whereas
7736 ENTRY f() RESULT (r)
7737 can't be written as
7738 ENTRY f RESULT (r). */
7739 if (gfc_match_eos () == MATCH_YES)
7740 {
7741 gfc_current_locus = old_loc;
7742 /* Match the empty argument list, and add the interface to
7743 the symbol. */
7744 m = gfc_match_formal_arglist (entry, 0, 1);
7745 }
7746 else
7747 m = gfc_match_formal_arglist (entry, 0, 0);
7748
7749 if (m != MATCH_YES)
7750 return MATCH_ERROR;
7751
7752 result = NULL;
7753
7754 if (gfc_match_eos () == MATCH_YES)
7755 {
7756 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7757 || !gfc_add_function (&entry->attr, entry->name, NULL))
7758 return MATCH_ERROR;
7759
7760 entry->result = entry;
7761 }
7762 else
7763 {
7764 m = gfc_match_suffix (entry, &result);
7765 if (m == MATCH_NO)
7766 gfc_syntax_error (ST_ENTRY);
7767 if (m != MATCH_YES)
7768 return MATCH_ERROR;
7769
7770 if (result)
7771 {
7772 if (!gfc_add_result (&result->attr, result->name, NULL)
7773 || !gfc_add_entry (&entry->attr, result->name, NULL)
7774 || !gfc_add_function (&entry->attr, result->name, NULL))
7775 return MATCH_ERROR;
7776 entry->result = result;
7777 }
7778 else
7779 {
7780 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
7781 || !gfc_add_function (&entry->attr, entry->name, NULL))
7782 return MATCH_ERROR;
7783 entry->result = entry;
7784 }
7785 }
7786
7787 if (!gfc_current_ns->parent
7788 && !add_global_entry (name, entry->binding_label, false,
7789 &old_loc))
7790 return MATCH_ERROR;
7791 }
7792
7793 if (gfc_match_eos () != MATCH_YES)
7794 {
7795 gfc_syntax_error (ST_ENTRY);
7796 return MATCH_ERROR;
7797 }
7798
7799 /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */
7800 if (proc->attr.elemental && entry->attr.is_bind_c)
7801 {
7802 gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an "
7803 "elemental procedure", &entry->declared_at);
7804 return MATCH_ERROR;
7805 }
7806
7807 entry->attr.recursive = proc->attr.recursive;
7808 entry->attr.elemental = proc->attr.elemental;
7809 entry->attr.pure = proc->attr.pure;
7810
7811 el = gfc_get_entry_list ();
7812 el->sym = entry;
7813 el->next = gfc_current_ns->entries;
7814 gfc_current_ns->entries = el;
7815 if (el->next)
7816 el->id = el->next->id + 1;
7817 else
7818 el->id = 1;
7819
7820 new_st.op = EXEC_ENTRY;
7821 new_st.ext.entry = el;
7822
7823 return MATCH_YES;
7824}
7825
7826
7827/* Match a subroutine statement, including optional prefixes. */
7828
7829match
7830gfc_match_subroutine (void)
7831{
7832 char name[GFC_MAX_SYMBOL_LEN + 1];
7833 gfc_symbol *sym;
7834 match m;
7835 match is_bind_c;
7836 char peek_char;
7837 bool allow_binding_name;
7838 locus loc;
7839
7840 if (gfc_current_state () != COMP_NONE
7841 && gfc_current_state () != COMP_INTERFACE
7842 && gfc_current_state () != COMP_CONTAINS)
7843 return MATCH_NO;
7844
7845 m = gfc_match_prefix (NULL);
7846 if (m != MATCH_YES)
7847 return m;
7848
7849 m = gfc_match ("subroutine% %n", name);
7850 if (m != MATCH_YES)
7851 return m;
7852
7853 if (get_proc_name (name, &sym, false))
7854 return MATCH_ERROR;
7855
7856 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7857 the symbol existed before. */
7858 sym->declared_at = gfc_current_locus;
7859
7860 if (current_attr.module_procedure)
7861 sym->attr.module_procedure = 1;
7862
7863 if (add_hidden_procptr_result (sym))
7864 sym = sym->result;
7865
7866 gfc_new_block = sym;
7867
7868 /* Check what next non-whitespace character is so we can tell if there
7869 is the required parens if we have a BIND(C). */
7870 gfc_gobble_whitespace ();
7871 peek_char = gfc_peek_ascii_char ();
7872
7873 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
7874 return MATCH_ERROR;
7875
7876 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
7877 return MATCH_ERROR;
7878
7879 /* Make sure that it isn't already declared as BIND(C). If it is, it
7880 must have been marked BIND(C) with a BIND(C) attribute and that is
7881 not allowed for procedures. */
7882 if (sym->attr.is_bind_c == 1)
7883 {
7884 sym->attr.is_bind_c = 0;
7885
7886 if (gfc_state_stack->previous
7887 && gfc_state_stack->previous->state != COMP_SUBMODULE)
7888 {
7889 locus loc;
7890 loc = sym->old_symbol != NULL
7891 ? sym->old_symbol->declared_at : gfc_current_locus;
7892 gfc_error_now ("BIND(C) attribute at %L can only be used for "
7893 "variables or common blocks", &loc);
7894 }
7895 }
7896
7897 /* C binding names are not allowed for internal procedures. */
7898 if (gfc_current_state () == COMP_CONTAINS
7899 && sym->ns->proc_name->attr.flavor != FL_MODULE)
7900 allow_binding_name = false;
7901 else
7902 allow_binding_name = true;
7903
7904 /* Here, we are just checking if it has the bind(c) attribute, and if
7905 so, then we need to make sure it's all correct. If it doesn't,
7906 we still need to continue matching the rest of the subroutine line. */
7907 gfc_gobble_whitespace ();
7908 loc = gfc_current_locus;
7909 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
7910 if (is_bind_c == MATCH_ERROR)
7911 {
7912 /* There was an attempt at the bind(c), but it was wrong. An
7913 error message should have been printed w/in the gfc_match_bind_c
7914 so here we'll just return the MATCH_ERROR. */
7915 return MATCH_ERROR;
7916 }
7917
7918 if (is_bind_c == MATCH_YES)
7919 {
7920 gfc_formal_arglist *arg;
7921
7922 /* The following is allowed in the Fortran 2008 draft. */
7923 if (gfc_current_state () == COMP_CONTAINS
7924 && sym->ns->proc_name->attr.flavor != FL_MODULE
7925 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
7926 "at %L may not be specified for an internal "
7927 "procedure", &gfc_current_locus))
7928 return MATCH_ERROR;
7929
7930 if (peek_char != '(')
7931 {
7932 gfc_error ("Missing required parentheses before BIND(C) at %C");
7933 return MATCH_ERROR;
7934 }
7935
7936 /* F2018 C1550 (R1526) If MODULE appears in the prefix of a module
7937 subprogram and a binding label is specified, it shall be the
7938 same as the binding label specified in the corresponding module
7939 procedure interface body. */
7940 if (sym->attr.module_procedure && sym->old_symbol
7941 && strcmp (sym->name, sym->old_symbol->name) == 0
7942 && sym->binding_label && sym->old_symbol->binding_label
7943 && strcmp (sym->binding_label, sym->old_symbol->binding_label) != 0)
7944 {
7945 const char *null = "NULL", *s1, *s2;
7946 s1 = sym->binding_label;
7947 if (!s1) s1 = null;
7948 s2 = sym->old_symbol->binding_label;
7949 if (!s2) s2 = null;
7950 gfc_error ("Mismatch in BIND(C) names (%qs/%qs) at %C", s1, s2);
7951 sym->refs++; /* Needed to avoid an ICE in gfc_release_symbol */
7952 return MATCH_ERROR;
7953 }
7954
7955 /* Scan the dummy arguments for an alternate return. */
7956 for (arg = sym->formal; arg; arg = arg->next)
7957 if (!arg->sym)
7958 {
7959 gfc_error ("Alternate return dummy argument cannot appear in a "
7960 "SUBROUTINE with the BIND(C) attribute at %L", &loc);
7961 return MATCH_ERROR;
7962 }
7963
7964 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1))
7965 return MATCH_ERROR;
7966 }
7967
7968 if (gfc_match_eos () != MATCH_YES)
7969 {
7970 gfc_syntax_error (ST_SUBROUTINE);
7971 return MATCH_ERROR;
7972 }
7973
7974 if (!copy_prefix (&sym->attr, &sym->declared_at))
7975 {
7976 if(!sym->attr.module_procedure)
7977 return MATCH_ERROR;
7978 else
7979 gfc_error_check ();
7980 }
7981
7982 /* Warn if it has the same name as an intrinsic. */
7983 do_warn_intrinsic_shadow (sym, false);
7984
7985 return MATCH_YES;
7986}
7987
7988
7989/* Check that the NAME identifier in a BIND attribute or statement
7990 is conform to C identifier rules. */
7991
7992match
7993check_bind_name_identifier (char **name)
7994{
7995 char *n = *name, *p;
7996
7997 /* Remove leading spaces. */
7998 while (*n == ' ')
7999 n++;
8000
8001 /* On an empty string, free memory and set name to NULL. */
8002 if (*n == '\0')
8003 {
8004 free (*name);
8005 *name = NULL;
8006 return MATCH_YES;
8007 }
8008
8009 /* Remove trailing spaces. */
8010 p = n + strlen(n) - 1;
8011 while (*p == ' ')
8012 *(p--) = '\0';
8013
8014 /* Insert the identifier into the symbol table. */
8015 p = xstrdup (n);
8016 free (*name);
8017 *name = p;
8018
8019 /* Now check that identifier is valid under C rules. */
8020 if (ISDIGIT (*p))
8021 {
8022 gfc_error ("Invalid C identifier in NAME= specifier at %C");
8023 return MATCH_ERROR;
8024 }
8025
8026 for (; *p; p++)
8027 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
8028 {
8029 gfc_error ("Invalid C identifier in NAME= specifier at %C");
8030 return MATCH_ERROR;
8031 }
8032
8033 return MATCH_YES;
8034}
8035
8036
8037/* Match a BIND(C) specifier, with the optional 'name=' specifier if
8038 given, and set the binding label in either the given symbol (if not
8039 NULL), or in the current_ts. The symbol may be NULL because we may
8040 encounter the BIND(C) before the declaration itself. Return
8041 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
8042 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
8043 or MATCH_YES if the specifier was correct and the binding label and
8044 bind(c) fields were set correctly for the given symbol or the
8045 current_ts. If allow_binding_name is false, no binding name may be
8046 given. */
8047
8048match
8049gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
8050{
8051 char *binding_label = NULL;
8052 gfc_expr *e = NULL;
8053
8054 /* Initialize the flag that specifies whether we encountered a NAME=
8055 specifier or not. */
8056 has_name_equals = 0;
8057
8058 /* This much we have to be able to match, in this order, if
8059 there is a bind(c) label. */
8060 if (gfc_match (" bind ( c ") != MATCH_YES)
8061 return MATCH_NO;
8062
8063 /* Now see if there is a binding label, or if we've reached the
8064 end of the bind(c) attribute without one. */
8065 if (gfc_match_char (',') == MATCH_YES)
8066 {
8067 if (gfc_match (" name = ") != MATCH_YES)
8068 {
8069 gfc_error ("Syntax error in NAME= specifier for binding label "
8070 "at %C");
8071 /* should give an error message here */
8072 return MATCH_ERROR;
8073 }
8074
8075 has_name_equals = 1;
8076
8077 if (gfc_match_init_expr (&e) != MATCH_YES)
8078 {
8079 gfc_free_expr (e);
8080 return MATCH_ERROR;
8081 }
8082
8083 if (!gfc_simplify_expr(e, 0))
8084 {
8085 gfc_error ("NAME= specifier at %C should be a constant expression");
8086 gfc_free_expr (e);
8087 return MATCH_ERROR;
8088 }
8089
8090 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
8091 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
8092 {
8093 gfc_error ("NAME= specifier at %C should be a scalar of "
8094 "default character kind");
8095 gfc_free_expr(e);
8096 return MATCH_ERROR;
8097 }
8098
8099 // Get a C string from the Fortran string constant
8100 binding_label = gfc_widechar_to_char (e->value.character.string,
8101 e->value.character.length);
8102 gfc_free_expr(e);
8103
8104 // Check that it is valid (old gfc_match_name_C)
8105 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
8106 return MATCH_ERROR;
8107 }
8108
8109 /* Get the required right paren. */
8110 if (gfc_match_char (')') != MATCH_YES)
8111 {
8112 gfc_error ("Missing closing paren for binding label at %C");
8113 return MATCH_ERROR;
8114 }
8115
8116 if (has_name_equals && !allow_binding_name)
8117 {
8118 gfc_error ("No binding name is allowed in BIND(C) at %C");
8119 return MATCH_ERROR;
8120 }
8121
8122 if (has_name_equals && sym != NULL && sym->attr.dummy)
8123 {
8124 gfc_error ("For dummy procedure %s, no binding name is "
8125 "allowed in BIND(C) at %C", sym->name);
8126 return MATCH_ERROR;
8127 }
8128
8129
8130 /* Save the binding label to the symbol. If sym is null, we're
8131 probably matching the typespec attributes of a declaration and
8132 haven't gotten the name yet, and therefore, no symbol yet. */
8133 if (binding_label)
8134 {
8135 if (sym != NULL)
8136 sym->binding_label = binding_label;
8137 else
8138 curr_binding_label = binding_label;
8139 }
8140 else if (allow_binding_name)
8141 {
8142 /* No binding label, but if symbol isn't null, we
8143 can set the label for it here.
8144 If name="" or allow_binding_name is false, no C binding name is
8145 created. */
8146 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
8147 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
8148 }
8149
8150 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
8151 && current_interface.type == INTERFACE_ABSTRACT)
8152 {
8153 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
8154 return MATCH_ERROR;
8155 }
8156
8157 return MATCH_YES;
8158}
8159
8160
8161/* Return nonzero if we're currently compiling a contained procedure. */
8162
8163static int
8164contained_procedure (void)
8165{
8166 gfc_state_data *s = gfc_state_stack;
8167
8168 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
8169 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
8170 return 1;
8171
8172 return 0;
8173}
8174
8175/* Set the kind of each enumerator. The kind is selected such that it is
8176 interoperable with the corresponding C enumeration type, making
8177 sure that -fshort-enums is honored. */
8178
8179static void
8180set_enum_kind(void)
8181{
8182 enumerator_history *current_history = NULL;
8183 int kind;
8184 int i;
8185
8186 if (max_enum == NULL || enum_history == NULL)
8187 return;
8188
8189 if (!flag_short_enums)
8190 return;
8191
8192 i = 0;
8193 do
8194 {
8195 kind = gfc_integer_kinds[i++].kind;
8196 }
8197 while (kind < gfc_c_int_kind
8198 && gfc_check_integer_range (max_enum->initializer->value.integer,
8199 kind) != ARITH_OK);
8200
8201 current_history = enum_history;
8202 while (current_history != NULL)
8203 {
8204 current_history->sym->ts.kind = kind;
8205 current_history = current_history->next;
8206 }
8207}
8208
8209
8210/* Match any of the various end-block statements. Returns the type of
8211 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
8212 and END BLOCK statements cannot be replaced by a single END statement. */
8213
8214match
8215gfc_match_end (gfc_statement *st)
8216{
8217 char name[GFC_MAX_SYMBOL_LEN + 1];
8218 gfc_compile_state state;
8219 locus old_loc;
8220 const char *block_name;
8221 const char *target;
8222 int eos_ok;
8223 match m;
8224 gfc_namespace *parent_ns, *ns, *prev_ns;
8225 gfc_namespace **nsp;
8226 bool abreviated_modproc_decl = false;
8227 bool got_matching_end = false;
8228
8229 old_loc = gfc_current_locus;
8230 if (gfc_match ("end") != MATCH_YES)
8231 return MATCH_NO;
8232
8233 state = gfc_current_state ();
8234 block_name = gfc_current_block () == NULL
8235 ? NULL : gfc_current_block ()->name;
8236
8237 switch (state)
8238 {
8239 case COMP_ASSOCIATE:
8240 case COMP_BLOCK:
8241 if (gfc_str_startswith (block_name, "block@"))
8242 block_name = NULL;
8243 break;
8244
8245 case COMP_CONTAINS:
8246 case COMP_DERIVED_CONTAINS:
8247 state = gfc_state_stack->previous->state;
8248 block_name = gfc_state_stack->previous->sym == NULL
8249 ? NULL : gfc_state_stack->previous->sym->name;
8250 abreviated_modproc_decl = gfc_state_stack->previous->sym
8251 && gfc_state_stack->previous->sym->abr_modproc_decl;
8252 break;
8253
8254 default:
8255 break;
8256 }
8257
8258 if (!abreviated_modproc_decl)
8259 abreviated_modproc_decl = gfc_current_block ()
8260 && gfc_current_block ()->abr_modproc_decl;
8261
8262 switch (state)
8263 {
8264 case COMP_NONE:
8265 case COMP_PROGRAM:
8266 *st = ST_END_PROGRAM;
8267 target = " program";
8268 eos_ok = 1;
8269 break;
8270
8271 case COMP_SUBROUTINE:
8272 *st = ST_END_SUBROUTINE;
8273 if (!abreviated_modproc_decl)
8274 target = " subroutine";
8275 else
8276 target = " procedure";
8277 eos_ok = !contained_procedure ();
8278 break;
8279
8280 case COMP_FUNCTION:
8281 *st = ST_END_FUNCTION;
8282 if (!abreviated_modproc_decl)
8283 target = " function";
8284 else
8285 target = " procedure";
8286 eos_ok = !contained_procedure ();
8287 break;
8288
8289 case COMP_BLOCK_DATA:
8290 *st = ST_END_BLOCK_DATA;
8291 target = " block data";
8292 eos_ok = 1;
8293 break;
8294
8295 case COMP_MODULE:
8296 *st = ST_END_MODULE;
8297 target = " module";
8298 eos_ok = 1;
8299 break;
8300
8301 case COMP_SUBMODULE:
8302 *st = ST_END_SUBMODULE;
8303 target = " submodule";
8304 eos_ok = 1;
8305 break;
8306
8307 case COMP_INTERFACE:
8308 *st = ST_END_INTERFACE;
8309 target = " interface";
8310 eos_ok = 0;
8311 break;
8312
8313 case COMP_MAP:
8314 *st = ST_END_MAP;
8315 target = " map";
8316 eos_ok = 0;
8317 break;
8318
8319 case COMP_UNION:
8320 *st = ST_END_UNION;
8321 target = " union";
8322 eos_ok = 0;
8323 break;
8324
8325 case COMP_STRUCTURE:
8326 *st = ST_END_STRUCTURE;
8327 target = " structure";
8328 eos_ok = 0;
8329 break;
8330
8331 case COMP_DERIVED:
8332 case COMP_DERIVED_CONTAINS:
8333 *st = ST_END_TYPE;
8334 target = " type";
8335 eos_ok = 0;
8336 break;
8337
8338 case COMP_ASSOCIATE:
8339 *st = ST_END_ASSOCIATE;
8340 target = " associate";
8341 eos_ok = 0;
8342 break;
8343
8344 case COMP_BLOCK:
8345 *st = ST_END_BLOCK;
8346 target = " block";
8347 eos_ok = 0;
8348 break;
8349
8350 case COMP_IF:
8351 *st = ST_ENDIF;
8352 target = " if";
8353 eos_ok = 0;
8354 break;
8355
8356 case COMP_DO:
8357 case COMP_DO_CONCURRENT:
8358 *st = ST_ENDDO;
8359 target = " do";
8360 eos_ok = 0;
8361 break;
8362
8363 case COMP_CRITICAL:
8364 *st = ST_END_CRITICAL;
8365 target = " critical";
8366 eos_ok = 0;
8367 break;
8368
8369 case COMP_SELECT:
8370 case COMP_SELECT_TYPE:
8371 case COMP_SELECT_RANK:
8372 *st = ST_END_SELECT;
8373 target = " select";
8374 eos_ok = 0;
8375 break;
8376
8377 case COMP_FORALL:
8378 *st = ST_END_FORALL;
8379 target = " forall";
8380 eos_ok = 0;
8381 break;
8382
8383 case COMP_WHERE:
8384 *st = ST_END_WHERE;
8385 target = " where";
8386 eos_ok = 0;
8387 break;
8388
8389 case COMP_ENUM:
8390 *st = ST_END_ENUM;
8391 target = " enum";
8392 eos_ok = 0;
8393 last_initializer = NULL;
8394 set_enum_kind ();
8395 gfc_free_enum_history ();
8396 break;
8397
8398 default:
8399 gfc_error ("Unexpected END statement at %C");
8400 goto cleanup;
8401 }
8402
8403 old_loc = gfc_current_locus;
8404 if (gfc_match_eos () == MATCH_YES)
8405 {
8406 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
8407 {
8408 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
8409 "instead of %s statement at %L",
8410 abreviated_modproc_decl ? "END PROCEDURE"
8411 : gfc_ascii_statement(*st), &old_loc))
8412 goto cleanup;
8413 }
8414 else if (!eos_ok)
8415 {
8416 /* We would have required END [something]. */
8417 gfc_error ("%s statement expected at %L",
8418 gfc_ascii_statement (*st), &old_loc);
8419 goto cleanup;
8420 }
8421
8422 return MATCH_YES;
8423 }
8424
8425 /* Verify that we've got the sort of end-block that we're expecting. */
8426 if (gfc_match (target) != MATCH_YES)
8427 {
8428 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
8429 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
8430 goto cleanup;
8431 }
8432 else
8433 got_matching_end = true;
8434
8435 old_loc = gfc_current_locus;
8436 /* If we're at the end, make sure a block name wasn't required. */
8437 if (gfc_match_eos () == MATCH_YES)
8438 {
8439
8440 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
8441 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
8442 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
8443 return MATCH_YES;
8444
8445 if (!block_name)
8446 return MATCH_YES;
8447
8448 gfc_error ("Expected block name of %qs in %s statement at %L",
8449 block_name, gfc_ascii_statement (*st), &old_loc);
8450
8451 return MATCH_ERROR;
8452 }
8453
8454 /* END INTERFACE has a special handler for its several possible endings. */
8455 if (*st == ST_END_INTERFACE)
8456 return gfc_match_end_interface ();
8457
8458 /* We haven't hit the end of statement, so what is left must be an
8459 end-name. */
8460 m = gfc_match_space ();
8461 if (m == MATCH_YES)
8462 m = gfc_match_name (name);
8463
8464 if (m == MATCH_NO)
8465 gfc_error ("Expected terminating name at %C");
8466 if (m != MATCH_YES)
8467 goto cleanup;
8468
8469 if (block_name == NULL)
8470 goto syntax;
8471
8472 /* We have to pick out the declared submodule name from the composite
8473 required by F2008:11.2.3 para 2, which ends in the declared name. */
8474 if (state == COMP_SUBMODULE)
8475 block_name = strchr (block_name, '.') + 1;
8476
8477 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
8478 {
8479 gfc_error ("Expected label %qs for %s statement at %C", block_name,
8480 gfc_ascii_statement (*st));
8481 goto cleanup;
8482 }
8483 /* Procedure pointer as function result. */
8484 else if (strcmp (block_name, "ppr@") == 0
8485 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
8486 {
8487 gfc_error ("Expected label %qs for %s statement at %C",
8488 gfc_current_block ()->ns->proc_name->name,
8489 gfc_ascii_statement (*st));
8490 goto cleanup;
8491 }
8492
8493 if (gfc_match_eos () == MATCH_YES)
8494 return MATCH_YES;
8495
8496syntax:
8497 gfc_syntax_error (*st);
8498
8499cleanup:
8500 gfc_current_locus = old_loc;
8501
8502 /* If we are missing an END BLOCK, we created a half-ready namespace.
8503 Remove it from the parent namespace's sibling list. */
8504
8505 while (state == COMP_BLOCK && !got_matching_end)
8506 {
8507 parent_ns = gfc_current_ns->parent;
8508
8509 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
8510
8511 prev_ns = NULL;
8512 ns = *nsp;
8513 while (ns)
8514 {
8515 if (ns == gfc_current_ns)
8516 {
8517 if (prev_ns == NULL)
8518 *nsp = NULL;
8519 else
8520 prev_ns->sibling = ns->sibling;
8521 }
8522 prev_ns = ns;
8523 ns = ns->sibling;
8524 }
8525
8526 gfc_free_namespace (gfc_current_ns);
8527 gfc_current_ns = parent_ns;
8528 gfc_state_stack = gfc_state_stack->previous;
8529 state = gfc_current_state ();
8530 }
8531
8532 return MATCH_ERROR;
8533}
8534
8535
8536
8537/***************** Attribute declaration statements ****************/
8538
8539/* Set the attribute of a single variable. */
8540
8541static match
8542attr_decl1 (void)
8543{
8544 char name[GFC_MAX_SYMBOL_LEN + 1];
8545 gfc_array_spec *as;
8546
8547 /* Workaround -Wmaybe-uninitialized false positive during
8548 profiledbootstrap by initializing them. */
8549 gfc_symbol *sym = NULL;
8550 locus var_locus;
8551 match m;
8552
8553 as = NULL;
8554
8555 m = gfc_match_name (name);
8556 if (m != MATCH_YES)
8557 goto cleanup;
8558
8559 if (find_special (name, &sym, false))
8560 return MATCH_ERROR;
8561
8562 if (!check_function_name (name))
8563 {
8564 m = MATCH_ERROR;
8565 goto cleanup;
8566 }
8567
8568 var_locus = gfc_current_locus;
8569
8570 /* Deal with possible array specification for certain attributes. */
8571 if (current_attr.dimension
8572 || current_attr.codimension
8573 || current_attr.allocatable
8574 || current_attr.pointer
8575 || current_attr.target)
8576 {
8577 m = gfc_match_array_spec (&as, !current_attr.codimension,
8578 !current_attr.dimension
8579 && !current_attr.pointer
8580 && !current_attr.target);
8581 if (m == MATCH_ERROR)
8582 goto cleanup;
8583
8584 if (current_attr.dimension && m == MATCH_NO)
8585 {
8586 gfc_error ("Missing array specification at %L in DIMENSION "
8587 "statement", &var_locus);
8588 m = MATCH_ERROR;
8589 goto cleanup;
8590 }
8591
8592 if (current_attr.dimension && sym->value)
8593 {
8594 gfc_error ("Dimensions specified for %s at %L after its "
8595 "initialization", sym->name, &var_locus);
8596 m = MATCH_ERROR;
8597 goto cleanup;
8598 }
8599
8600 if (current_attr.codimension && m == MATCH_NO)
8601 {
8602 gfc_error ("Missing array specification at %L in CODIMENSION "
8603 "statement", &var_locus);
8604 m = MATCH_ERROR;
8605 goto cleanup;
8606 }
8607
8608 if ((current_attr.allocatable || current_attr.pointer)
8609 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
8610 {
8611 gfc_error ("Array specification must be deferred at %L", &var_locus);
8612 m = MATCH_ERROR;
8613 goto cleanup;
8614 }
8615 }
8616
8617 /* Update symbol table. DIMENSION attribute is set in
8618 gfc_set_array_spec(). For CLASS variables, this must be applied
8619 to the first component, or '_data' field. */
8620 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
8621 {
8622 /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr. Check
8623 for duplicate attribute here. */
8624 if (CLASS_DATA(sym)->attr.dimension == 1 && as)
8625 {
8626 gfc_error ("Duplicate DIMENSION attribute at %C");
8627 m = MATCH_ERROR;
8628 goto cleanup;
8629 }
8630
8631 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
8632 {
8633 m = MATCH_ERROR;
8634 goto cleanup;
8635 }
8636 }
8637 else
8638 {
8639 if (current_attr.dimension == 0 && current_attr.codimension == 0
8640 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
8641 {
8642 m = MATCH_ERROR;
8643 goto cleanup;
8644 }
8645 }
8646
8647 if (sym->ts.type == BT_CLASS
8648 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
8649 {
8650 m = MATCH_ERROR;
8651 goto cleanup;
8652 }
8653
8654 if (!gfc_set_array_spec (sym, as, &var_locus))
8655 {
8656 m = MATCH_ERROR;
8657 goto cleanup;
8658 }
8659
8660 if (sym->attr.cray_pointee && sym->as != NULL)
8661 {
8662 /* Fix the array spec. */
8663 m = gfc_mod_pointee_as (sym->as);
8664 if (m == MATCH_ERROR)
8665 goto cleanup;
8666 }
8667
8668 if (!gfc_add_attribute (&sym->attr, &var_locus))
8669 {
8670 m = MATCH_ERROR;
8671 goto cleanup;
8672 }
8673
8674 if ((current_attr.external || current_attr.intrinsic)
8675 && sym->attr.flavor != FL_PROCEDURE
8676 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
8677 {
8678 m = MATCH_ERROR;
8679 goto cleanup;
8680 }
8681
8682 add_hidden_procptr_result (sym);
8683
8684 return MATCH_YES;
8685
8686cleanup:
8687 gfc_free_array_spec (as);
8688 return m;
8689}
8690
8691
8692/* Generic attribute declaration subroutine. Used for attributes that
8693 just have a list of names. */
8694
8695static match
8696attr_decl (void)
8697{
8698 match m;
8699
8700 /* Gobble the optional double colon, by simply ignoring the result
8701 of gfc_match(). */
8702 gfc_match (" ::");
8703
8704 for (;;)
8705 {
8706 m = attr_decl1 ();
8707 if (m != MATCH_YES)
8708 break;
8709
8710 if (gfc_match_eos () == MATCH_YES)
8711 {
8712 m = MATCH_YES;
8713 break;
8714 }
8715
8716 if (gfc_match_char (',') != MATCH_YES)
8717 {
8718 gfc_error ("Unexpected character in variable list at %C");
8719 m = MATCH_ERROR;
8720 break;
8721 }
8722 }
8723
8724 return m;
8725}
8726
8727
8728/* This routine matches Cray Pointer declarations of the form:
8729 pointer ( <pointer>, <pointee> )
8730 or
8731 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
8732 The pointer, if already declared, should be an integer. Otherwise, we
8733 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
8734 be either a scalar, or an array declaration. No space is allocated for
8735 the pointee. For the statement
8736 pointer (ipt, ar(10))
8737 any subsequent uses of ar will be translated (in C-notation) as
8738 ar(i) => ((<type> *) ipt)(i)
8739 After gimplification, pointee variable will disappear in the code. */
8740
8741static match
8742cray_pointer_decl (void)
8743{
8744 match m;
8745 gfc_array_spec *as = NULL;
8746 gfc_symbol *cptr; /* Pointer symbol. */
8747 gfc_symbol *cpte; /* Pointee symbol. */
8748 locus var_locus;
8749 bool done = false;
8750
8751 while (!done)
8752 {
8753 if (gfc_match_char ('(') != MATCH_YES)
8754 {
8755 gfc_error ("Expected %<(%> at %C");
8756 return MATCH_ERROR;
8757 }
8758
8759 /* Match pointer. */
8760 var_locus = gfc_current_locus;
8761 gfc_clear_attr (&current_attr);
8762 gfc_add_cray_pointer (&current_attr, &var_locus);
8763 current_ts.type = BT_INTEGER;
8764 current_ts.kind = gfc_index_integer_kind;
8765
8766 m = gfc_match_symbol (&cptr, 0);
8767 if (m != MATCH_YES)
8768 {
8769 gfc_error ("Expected variable name at %C");
8770 return m;
8771 }
8772
8773 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
8774 return MATCH_ERROR;
8775
8776 gfc_set_sym_referenced (cptr);
8777
8778 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
8779 {
8780 cptr->ts.type = BT_INTEGER;
8781 cptr->ts.kind = gfc_index_integer_kind;
8782 }
8783 else if (cptr->ts.type != BT_INTEGER)
8784 {
8785 gfc_error ("Cray pointer at %C must be an integer");
8786 return MATCH_ERROR;
8787 }
8788 else if (cptr->ts.kind < gfc_index_integer_kind)
8789 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
8790 " memory addresses require %d bytes",
8791 cptr->ts.kind, gfc_index_integer_kind);
8792
8793 if (gfc_match_char (',') != MATCH_YES)
8794 {
8795 gfc_error ("Expected \",\" at %C");
8796 return MATCH_ERROR;
8797 }
8798
8799 /* Match Pointee. */
8800 var_locus = gfc_current_locus;
8801 gfc_clear_attr (&current_attr);
8802 gfc_add_cray_pointee (&current_attr, &var_locus);
8803 current_ts.type = BT_UNKNOWN;
8804 current_ts.kind = 0;
8805
8806 m = gfc_match_symbol (&cpte, 0);
8807 if (m != MATCH_YES)
8808 {
8809 gfc_error ("Expected variable name at %C");
8810 return m;
8811 }
8812
8813 /* Check for an optional array spec. */
8814 m = gfc_match_array_spec (&as, true, false);
8815 if (m == MATCH_ERROR)
8816 {
8817 gfc_free_array_spec (as);
8818 return m;
8819 }
8820 else if (m == MATCH_NO)
8821 {
8822 gfc_free_array_spec (as);
8823 as = NULL;
8824 }
8825
8826 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
8827 return MATCH_ERROR;
8828
8829 gfc_set_sym_referenced (cpte);
8830
8831 if (cpte->as == NULL)
8832 {
8833 if (!gfc_set_array_spec (cpte, as, &var_locus))
8834 gfc_internal_error ("Cannot set Cray pointee array spec.");
8835 }
8836 else if (as != NULL)
8837 {
8838 gfc_error ("Duplicate array spec for Cray pointee at %C");
8839 gfc_free_array_spec (as);
8840 return MATCH_ERROR;
8841 }
8842
8843 as = NULL;
8844
8845 if (cpte->as != NULL)
8846 {
8847 /* Fix array spec. */
8848 m = gfc_mod_pointee_as (cpte->as);
8849 if (m == MATCH_ERROR)
8850 return m;
8851 }
8852
8853 /* Point the Pointee at the Pointer. */
8854 cpte->cp_pointer = cptr;
8855
8856 if (gfc_match_char (')') != MATCH_YES)
8857 {
8858 gfc_error ("Expected \")\" at %C");
8859 return MATCH_ERROR;
8860 }
8861 m = gfc_match_char (',');
8862 if (m != MATCH_YES)
8863 done = true; /* Stop searching for more declarations. */
8864
8865 }
8866
8867 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
8868 || gfc_match_eos () != MATCH_YES)
8869 {
8870 gfc_error ("Expected %<,%> or end of statement at %C");
8871 return MATCH_ERROR;
8872 }
8873 return MATCH_YES;
8874}
8875
8876
8877match
8878gfc_match_external (void)
8879{
8880
8881 gfc_clear_attr (&current_attr);
8882 current_attr.external = 1;
8883
8884 return attr_decl ();
8885}
8886
8887
8888match
8889gfc_match_intent (void)
8890{
8891 sym_intent intent;
8892
8893 /* This is not allowed within a BLOCK construct! */
8894 if (gfc_current_state () == COMP_BLOCK)
8895 {
8896 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
8897 return MATCH_ERROR;
8898 }
8899
8900 intent = match_intent_spec ();
8901 if (intent == INTENT_UNKNOWN)
8902 return MATCH_ERROR;
8903
8904 gfc_clear_attr (&current_attr);
8905 current_attr.intent = intent;
8906
8907 return attr_decl ();
8908}
8909
8910
8911match
8912gfc_match_intrinsic (void)
8913{
8914
8915 gfc_clear_attr (&current_attr);
8916 current_attr.intrinsic = 1;
8917
8918 return attr_decl ();
8919}
8920
8921
8922match
8923gfc_match_optional (void)
8924{
8925 /* This is not allowed within a BLOCK construct! */
8926 if (gfc_current_state () == COMP_BLOCK)
8927 {
8928 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
8929 return MATCH_ERROR;
8930 }
8931
8932 gfc_clear_attr (&current_attr);
8933 current_attr.optional = 1;
8934
8935 return attr_decl ();
8936}
8937
8938
8939match
8940gfc_match_pointer (void)
8941{
8942 gfc_gobble_whitespace ();
8943 if (gfc_peek_ascii_char () == '(')
8944 {
8945 if (!flag_cray_pointer)
8946 {
8947 gfc_error ("Cray pointer declaration at %C requires "
8948 "%<-fcray-pointer%> flag");
8949 return MATCH_ERROR;
8950 }
8951 return cray_pointer_decl ();
8952 }
8953 else
8954 {
8955 gfc_clear_attr (&current_attr);
8956 current_attr.pointer = 1;
8957
8958 return attr_decl ();
8959 }
8960}
8961
8962
8963match
8964gfc_match_allocatable (void)
8965{
8966 gfc_clear_attr (&current_attr);
8967 current_attr.allocatable = 1;
8968
8969 return attr_decl ();
8970}
8971
8972
8973match
8974gfc_match_codimension (void)
8975{
8976 gfc_clear_attr (&current_attr);
8977 current_attr.codimension = 1;
8978
8979 return attr_decl ();
8980}
8981
8982
8983match
8984gfc_match_contiguous (void)
8985{
8986 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
8987 return MATCH_ERROR;
8988
8989 gfc_clear_attr (&current_attr);
8990 current_attr.contiguous = 1;
8991
8992 return attr_decl ();
8993}
8994
8995
8996match
8997gfc_match_dimension (void)
8998{
8999 gfc_clear_attr (&current_attr);
9000 current_attr.dimension = 1;
9001
9002 return attr_decl ();
9003}
9004
9005
9006match
9007gfc_match_target (void)
9008{
9009 gfc_clear_attr (&current_attr);
9010 current_attr.target = 1;
9011
9012 return attr_decl ();
9013}
9014
9015
9016/* Match the list of entities being specified in a PUBLIC or PRIVATE
9017 statement. */
9018
9019static match
9020access_attr_decl (gfc_statement st)
9021{
9022 char name[GFC_MAX_SYMBOL_LEN + 1];
9023 interface_type type;
9024 gfc_user_op *uop;
9025 gfc_symbol *sym, *dt_sym;
9026 gfc_intrinsic_op op;
9027 match m;
9028 gfc_access access = (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
9029
9030 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9031 goto done;
9032
9033 for (;;)
9034 {
9035 m = gfc_match_generic_spec (&type, name, &op);
9036 if (m == MATCH_NO)
9037 goto syntax;
9038 if (m == MATCH_ERROR)
9039 goto done;
9040
9041 switch (type)
9042 {
9043 case INTERFACE_NAMELESS:
9044 case INTERFACE_ABSTRACT:
9045 goto syntax;
9046
9047 case INTERFACE_GENERIC:
9048 case INTERFACE_DTIO:
9049
9050 if (gfc_get_symbol (name, NULL, &sym))
9051 goto done;
9052
9053 if (type == INTERFACE_DTIO
9054 && gfc_current_ns->proc_name
9055 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE
9056 && sym->attr.flavor == FL_UNKNOWN)
9057 sym->attr.flavor = FL_PROCEDURE;
9058
9059 if (!gfc_add_access (&sym->attr, access, sym->name, NULL))
9060 goto done;
9061
9062 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
9063 && !gfc_add_access (&dt_sym->attr, access, sym->name, NULL))
9064 goto done;
9065
9066 break;
9067
9068 case INTERFACE_INTRINSIC_OP:
9069 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
9070 {
9071 gfc_intrinsic_op other_op;
9072
9073 gfc_current_ns->operator_access[op] = access;
9074
9075 /* Handle the case if there is another op with the same
9076 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
9077 other_op = gfc_equivalent_op (op);
9078
9079 if (other_op != INTRINSIC_NONE)
9080 gfc_current_ns->operator_access[other_op] = access;
9081 }
9082 else
9083 {
9084 gfc_error ("Access specification of the %s operator at %C has "
9085 "already been specified", gfc_op2string (op));
9086 goto done;
9087 }
9088
9089 break;
9090
9091 case INTERFACE_USER_OP:
9092 uop = gfc_get_uop (name);
9093
9094 if (uop->access == ACCESS_UNKNOWN)
9095 {
9096 uop->access = access;
9097 }
9098 else
9099 {
9100 gfc_error ("Access specification of the .%s. operator at %C "
9101 "has already been specified", uop->name);
9102 goto done;
9103 }
9104
9105 break;
9106 }
9107
9108 if (gfc_match_char (',') == MATCH_NO)
9109 break;
9110 }
9111
9112 if (gfc_match_eos () != MATCH_YES)
9113 goto syntax;
9114 return MATCH_YES;
9115
9116syntax:
9117 gfc_syntax_error (st);
9118
9119done:
9120 return MATCH_ERROR;
9121}
9122
9123
9124match
9125gfc_match_protected (void)
9126{
9127 gfc_symbol *sym;
9128 match m;
9129 char c;
9130
9131 /* PROTECTED has already been seen, but must be followed by whitespace
9132 or ::. */
9133 c = gfc_peek_ascii_char ();
9134 if (!gfc_is_whitespace (c) && c != ':')
9135 return MATCH_NO;
9136
9137 if (!gfc_current_ns->proc_name
9138 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
9139 {
9140 gfc_error ("PROTECTED at %C only allowed in specification "
9141 "part of a module");
9142 return MATCH_ERROR;
9143
9144 }
9145
9146 gfc_match (" ::");
9147
9148 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
9149 return MATCH_ERROR;
9150
9151 /* PROTECTED has an entity-list. */
9152 if (gfc_match_eos () == MATCH_YES)
9153 goto syntax;
9154
9155 for(;;)
9156 {
9157 m = gfc_match_symbol (&sym, 0);
9158 switch (m)
9159 {
9160 case MATCH_YES:
9161 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
9162 return MATCH_ERROR;
9163 goto next_item;
9164
9165 case MATCH_NO:
9166 break;
9167
9168 case MATCH_ERROR:
9169 return MATCH_ERROR;
9170 }
9171
9172 next_item:
9173 if (gfc_match_eos () == MATCH_YES)
9174 break;
9175 if (gfc_match_char (',') != MATCH_YES)
9176 goto syntax;
9177 }
9178
9179 return MATCH_YES;
9180
9181syntax:
9182 gfc_error ("Syntax error in PROTECTED statement at %C");
9183 return MATCH_ERROR;
9184}
9185
9186
9187/* The PRIVATE statement is a bit weird in that it can be an attribute
9188 declaration, but also works as a standalone statement inside of a
9189 type declaration or a module. */
9190
9191match
9192gfc_match_private (gfc_statement *st)
9193{
9194 gfc_state_data *prev;
9195
9196 if (gfc_match ("private") != MATCH_YES)
9197 return MATCH_NO;
9198
9199 /* Try matching PRIVATE without an access-list. */
9200 if (gfc_match_eos () == MATCH_YES)
9201 {
9202 prev = gfc_state_stack->previous;
9203 if (gfc_current_state () != COMP_MODULE
9204 && !(gfc_current_state () == COMP_DERIVED
9205 && prev && prev->state == COMP_MODULE)
9206 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9207 && prev->previous && prev->previous->state == COMP_MODULE))
9208 {
9209 gfc_error ("PRIVATE statement at %C is only allowed in the "
9210 "specification part of a module");
9211 return MATCH_ERROR;
9212 }
9213
9214 *st = ST_PRIVATE;
9215 return MATCH_YES;
9216 }
9217
9218 /* At this point in free-form source code, PRIVATE must be followed
9219 by whitespace or ::. */
9220 if (gfc_current_form == FORM_FREE)
9221 {
9222 char c = gfc_peek_ascii_char ();
9223 if (!gfc_is_whitespace (c) && c != ':')
9224 return MATCH_NO;
9225 }
9226
9227 prev = gfc_state_stack->previous;
9228 if (gfc_current_state () != COMP_MODULE
9229 && !(gfc_current_state () == COMP_DERIVED
9230 && prev && prev->state == COMP_MODULE)
9231 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
9232 && prev->previous && prev->previous->state == COMP_MODULE))
9233 {
9234 gfc_error ("PRIVATE statement at %C is only allowed in the "
9235 "specification part of a module");
9236 return MATCH_ERROR;
9237 }
9238
9239 *st = ST_ATTR_DECL;
9240 return access_attr_decl (ST_PRIVATE);
9241}
9242
9243
9244match
9245gfc_match_public (gfc_statement *st)
9246{
9247 if (gfc_match ("public") != MATCH_YES)
9248 return MATCH_NO;
9249
9250 /* Try matching PUBLIC without an access-list. */
9251 if (gfc_match_eos () == MATCH_YES)
9252 {
9253 if (gfc_current_state () != COMP_MODULE)
9254 {
9255 gfc_error ("PUBLIC statement at %C is only allowed in the "
9256 "specification part of a module");
9257 return MATCH_ERROR;
9258 }
9259
9260 *st = ST_PUBLIC;
9261 return MATCH_YES;
9262 }
9263
9264 /* At this point in free-form source code, PUBLIC must be followed
9265 by whitespace or ::. */
9266 if (gfc_current_form == FORM_FREE)
9267 {
9268 char c = gfc_peek_ascii_char ();
9269 if (!gfc_is_whitespace (c) && c != ':')
9270 return MATCH_NO;
9271 }
9272
9273 if (gfc_current_state () != COMP_MODULE)
9274 {
9275 gfc_error ("PUBLIC statement at %C is only allowed in the "
9276 "specification part of a module");
9277 return MATCH_ERROR;
9278 }
9279
9280 *st = ST_ATTR_DECL;
9281 return access_attr_decl (ST_PUBLIC);
9282}
9283
9284
9285/* Workhorse for gfc_match_parameter. */
9286
9287static match
9288do_parm (void)
9289{
9290 gfc_symbol *sym;
9291 gfc_expr *init;
9292 match m;
9293 bool t;
9294
9295 m = gfc_match_symbol (&sym, 0);
9296 if (m == MATCH_NO)
9297 gfc_error ("Expected variable name at %C in PARAMETER statement");
9298
9299 if (m != MATCH_YES)
9300 return m;
9301
9302 if (gfc_match_char ('=') == MATCH_NO)
9303 {
9304 gfc_error ("Expected = sign in PARAMETER statement at %C");
9305 return MATCH_ERROR;
9306 }
9307
9308 m = gfc_match_init_expr (&init);
9309 if (m == MATCH_NO)
9310 gfc_error ("Expected expression at %C in PARAMETER statement");
9311 if (m != MATCH_YES)
9312 return m;
9313
9314 if (sym->ts.type == BT_UNKNOWN
9315 && !gfc_set_default_type (sym, 1, NULL))
9316 {
9317 m = MATCH_ERROR;
9318 goto cleanup;
9319 }
9320
9321 if (!gfc_check_assign_symbol (sym, NULL, init)
9322 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
9323 {
9324 m = MATCH_ERROR;
9325 goto cleanup;
9326 }
9327
9328 if (sym->value)
9329 {
9330 gfc_error ("Initializing already initialized variable at %C");
9331 m = MATCH_ERROR;
9332 goto cleanup;
9333 }
9334
9335 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
9336 return (t) ? MATCH_YES : MATCH_ERROR;
9337
9338cleanup:
9339 gfc_free_expr (init);
9340 return m;
9341}
9342
9343
9344/* Match a parameter statement, with the weird syntax that these have. */
9345
9346match
9347gfc_match_parameter (void)
9348{
9349 const char *term = " )%t";
9350 match m;
9351
9352 if (gfc_match_char ('(') == MATCH_NO)
9353 {
9354 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
9355 if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C"))
9356 return MATCH_NO;
9357 term = " %t";
9358 }
9359
9360 for (;;)
9361 {
9362 m = do_parm ();
9363 if (m != MATCH_YES)
9364 break;
9365
9366 if (gfc_match (term) == MATCH_YES)
9367 break;
9368
9369 if (gfc_match_char (',') != MATCH_YES)
9370 {
9371 gfc_error ("Unexpected characters in PARAMETER statement at %C");
9372 m = MATCH_ERROR;
9373 break;
9374 }
9375 }
9376
9377 return m;
9378}
9379
9380
9381match
9382gfc_match_automatic (void)
9383{
9384 gfc_symbol *sym;
9385 match m;
9386 bool seen_symbol = false;
9387
9388 if (!flag_dec_static)
9389 {
9390 gfc_error ("%s at %C is a DEC extension, enable with "
9391 "%<-fdec-static%>",
9392 "AUTOMATIC"
9393 );
9394 return MATCH_ERROR;
9395 }
9396
9397 gfc_match (" ::");
9398
9399 for (;;)
9400 {
9401 m = gfc_match_symbol (&sym, 0);
9402 switch (m)
9403 {
9404 case MATCH_NO:
9405 break;
9406
9407 case MATCH_ERROR:
9408 return MATCH_ERROR;
9409
9410 case MATCH_YES:
9411 if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus))
9412 return MATCH_ERROR;
9413 seen_symbol = true;
9414 break;
9415 }
9416
9417 if (gfc_match_eos () == MATCH_YES)
9418 break;
9419 if (gfc_match_char (',') != MATCH_YES)
9420 goto syntax;
9421 }
9422
9423 if (!seen_symbol)
9424 {
9425 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
9426 return MATCH_ERROR;
9427 }
9428
9429 return MATCH_YES;
9430
9431syntax:
9432 gfc_error ("Syntax error in AUTOMATIC statement at %C");
9433 return MATCH_ERROR;
9434}
9435
9436
9437match
9438gfc_match_static (void)
9439{
9440 gfc_symbol *sym;
9441 match m;
9442 bool seen_symbol = false;
9443
9444 if (!flag_dec_static)
9445 {
9446 gfc_error ("%s at %C is a DEC extension, enable with "
9447 "%<-fdec-static%>",
9448 "STATIC");
9449 return MATCH_ERROR;
9450 }
9451
9452 gfc_match (" ::");
9453
9454 for (;;)
9455 {
9456 m = gfc_match_symbol (&sym, 0);
9457 switch (m)
9458 {
9459 case MATCH_NO:
9460 break;
9461
9462 case MATCH_ERROR:
9463 return MATCH_ERROR;
9464
9465 case MATCH_YES:
9466 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9467 &gfc_current_locus))
9468 return MATCH_ERROR;
9469 seen_symbol = true;
9470 break;
9471 }
9472
9473 if (gfc_match_eos () == MATCH_YES)
9474 break;
9475 if (gfc_match_char (',') != MATCH_YES)
9476 goto syntax;
9477 }
9478
9479 if (!seen_symbol)
9480 {
9481 gfc_error ("Expected entity-list in STATIC statement at %C");
9482 return MATCH_ERROR;
9483 }
9484
9485 return MATCH_YES;
9486
9487syntax:
9488 gfc_error ("Syntax error in STATIC statement at %C");
9489 return MATCH_ERROR;
9490}
9491
9492
9493/* Save statements have a special syntax. */
9494
9495match
9496gfc_match_save (void)
9497{
9498 char n[GFC_MAX_SYMBOL_LEN+1];
9499 gfc_common_head *c;
9500 gfc_symbol *sym;
9501 match m;
9502
9503 if (gfc_match_eos () == MATCH_YES)
9504 {
9505 if (gfc_current_ns->seen_save)
9506 {
9507 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
9508 "follows previous SAVE statement"))
9509 return MATCH_ERROR;
9510 }
9511
9512 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
9513 return MATCH_YES;
9514 }
9515
9516 if (gfc_current_ns->save_all)
9517 {
9518 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
9519 "blanket SAVE statement"))
9520 return MATCH_ERROR;
9521 }
9522
9523 gfc_match (" ::");
9524
9525 for (;;)
9526 {
9527 m = gfc_match_symbol (&sym, 0);
9528 switch (m)
9529 {
9530 case MATCH_YES:
9531 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
9532 &gfc_current_locus))
9533 return MATCH_ERROR;
9534 goto next_item;
9535
9536 case MATCH_NO:
9537 break;
9538
9539 case MATCH_ERROR:
9540 return MATCH_ERROR;
9541 }
9542
9543 m = gfc_match (" / %n /", &n);
9544 if (m == MATCH_ERROR)
9545 return MATCH_ERROR;
9546 if (m == MATCH_NO)
9547 goto syntax;
9548
9549 c = gfc_get_common (n, 0);
9550 c->saved = 1;
9551
9552 gfc_current_ns->seen_save = 1;
9553
9554 next_item:
9555 if (gfc_match_eos () == MATCH_YES)
9556 break;
9557 if (gfc_match_char (',') != MATCH_YES)
9558 goto syntax;
9559 }
9560
9561 return MATCH_YES;
9562
9563syntax:
9564 if (gfc_current_ns->seen_save)
9565 {
9566 gfc_error ("Syntax error in SAVE statement at %C");
9567 return MATCH_ERROR;
9568 }
9569 else
9570 return MATCH_NO;
9571}
9572
9573
9574match
9575gfc_match_value (void)
9576{
9577 gfc_symbol *sym;
9578 match m;
9579
9580 /* This is not allowed within a BLOCK construct! */
9581 if (gfc_current_state () == COMP_BLOCK)
9582 {
9583 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
9584 return MATCH_ERROR;
9585 }
9586
9587 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
9588 return MATCH_ERROR;
9589
9590 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9591 {
9592 return MATCH_ERROR;
9593 }
9594
9595 if (gfc_match_eos () == MATCH_YES)
9596 goto syntax;
9597
9598 for(;;)
9599 {
9600 m = gfc_match_symbol (&sym, 0);
9601 switch (m)
9602 {
9603 case MATCH_YES:
9604 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
9605 return MATCH_ERROR;
9606 goto next_item;
9607
9608 case MATCH_NO:
9609 break;
9610
9611 case MATCH_ERROR:
9612 return MATCH_ERROR;
9613 }
9614
9615 next_item:
9616 if (gfc_match_eos () == MATCH_YES)
9617 break;
9618 if (gfc_match_char (',') != MATCH_YES)
9619 goto syntax;
9620 }
9621
9622 return MATCH_YES;
9623
9624syntax:
9625 gfc_error ("Syntax error in VALUE statement at %C");
9626 return MATCH_ERROR;
9627}
9628
9629
9630match
9631gfc_match_volatile (void)
9632{
9633 gfc_symbol *sym;
9634 char *name;
9635 match m;
9636
9637 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
9638 return MATCH_ERROR;
9639
9640 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9641 {
9642 return MATCH_ERROR;
9643 }
9644
9645 if (gfc_match_eos () == MATCH_YES)
9646 goto syntax;
9647
9648 for(;;)
9649 {
9650 /* VOLATILE is special because it can be added to host-associated
9651 symbols locally. Except for coarrays. */
9652 m = gfc_match_symbol (&sym, 1);
9653 switch (m)
9654 {
9655 case MATCH_YES:
9656 name = XCNEWVAR (char, strlen (sym->name) + 1);
9657 strcpy (name, sym->name);
9658 if (!check_function_name (name))
9659 return MATCH_ERROR;
9660 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
9661 for variable in a BLOCK which is defined outside of the BLOCK. */
9662 if (sym->ns != gfc_current_ns && sym->attr.codimension)
9663 {
9664 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
9665 "%C, which is use-/host-associated", sym->name);
9666 return MATCH_ERROR;
9667 }
9668 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
9669 return MATCH_ERROR;
9670 goto next_item;
9671
9672 case MATCH_NO:
9673 break;
9674
9675 case MATCH_ERROR:
9676 return MATCH_ERROR;
9677 }
9678
9679 next_item:
9680 if (gfc_match_eos () == MATCH_YES)
9681 break;
9682 if (gfc_match_char (',') != MATCH_YES)
9683 goto syntax;
9684 }
9685
9686 return MATCH_YES;
9687
9688syntax:
9689 gfc_error ("Syntax error in VOLATILE statement at %C");
9690 return MATCH_ERROR;
9691}
9692
9693
9694match
9695gfc_match_asynchronous (void)
9696{
9697 gfc_symbol *sym;
9698 char *name;
9699 match m;
9700
9701 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
9702 return MATCH_ERROR;
9703
9704 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
9705 {
9706 return MATCH_ERROR;
9707 }
9708
9709 if (gfc_match_eos () == MATCH_YES)
9710 goto syntax;
9711
9712 for(;;)
9713 {
9714 /* ASYNCHRONOUS is special because it can be added to host-associated
9715 symbols locally. */
9716 m = gfc_match_symbol (&sym, 1);
9717 switch (m)
9718 {
9719 case MATCH_YES:
9720 name = XCNEWVAR (char, strlen (sym->name) + 1);
9721 strcpy (name, sym->name);
9722 if (!check_function_name (name))
9723 return MATCH_ERROR;
9724 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
9725 return MATCH_ERROR;
9726 goto next_item;
9727
9728 case MATCH_NO:
9729 break;
9730
9731 case MATCH_ERROR:
9732 return MATCH_ERROR;
9733 }
9734
9735 next_item:
9736 if (gfc_match_eos () == MATCH_YES)
9737 break;
9738 if (gfc_match_char (',') != MATCH_YES)
9739 goto syntax;
9740 }
9741
9742 return MATCH_YES;
9743
9744syntax:
9745 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
9746 return MATCH_ERROR;
9747}
9748
9749
9750/* Match a module procedure statement in a submodule. */
9751
9752match
9753gfc_match_submod_proc (void)
9754{
9755 char name[GFC_MAX_SYMBOL_LEN + 1];
9756 gfc_symbol *sym, *fsym;
9757 match m;
9758 gfc_formal_arglist *formal, *head, *tail;
9759
9760 if (gfc_current_state () != COMP_CONTAINS
9761 || !(gfc_state_stack->previous
9762 && (gfc_state_stack->previous->state == COMP_SUBMODULE
9763 || gfc_state_stack->previous->state == COMP_MODULE)))
9764 return MATCH_NO;
9765
9766 m = gfc_match (" module% procedure% %n", name);
9767 if (m != MATCH_YES)
9768 return m;
9769
9770 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
9771 "at %C"))
9772 return MATCH_ERROR;
9773
9774 if (get_proc_name (name, &sym, false))
9775 return MATCH_ERROR;
9776
9777 /* Make sure that the result field is appropriately filled. */
9778 if (sym->tlink && sym->tlink->attr.function)
9779 {
9780 if (sym->tlink->result && sym->tlink->result != sym->tlink)
9781 {
9782 sym->result = sym->tlink->result;
9783 if (!sym->result->attr.use_assoc)
9784 {
9785 gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
9786 sym->result->name);
9787 st->n.sym = sym->result;
9788 sym->result->refs++;
9789 }
9790 }
9791 else
9792 sym->result = sym;
9793 }
9794
9795 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
9796 the symbol existed before. */
9797 sym->declared_at = gfc_current_locus;
9798
9799 if (!sym->attr.module_procedure)
9800 return MATCH_ERROR;
9801
9802 /* Signal match_end to expect "end procedure". */
9803 sym->abr_modproc_decl = 1;
9804
9805 /* Change from IFSRC_IFBODY coming from the interface declaration. */
9806 sym->attr.if_source = IFSRC_DECL;
9807
9808 gfc_new_block = sym;
9809
9810 /* Make a new formal arglist with the symbols in the procedure
9811 namespace. */
9812 head = tail = NULL;
9813 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
9814 {
9815 if (formal == sym->formal)
9816 head = tail = gfc_get_formal_arglist ();
9817 else
9818 {
9819 tail->next = gfc_get_formal_arglist ();
9820 tail = tail->next;
9821 }
9822
9823 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
9824 goto cleanup;
9825
9826 tail->sym = fsym;
9827 gfc_set_sym_referenced (fsym);
9828 }
9829
9830 /* The dummy symbols get cleaned up, when the formal_namespace of the
9831 interface declaration is cleared. This allows us to add the
9832 explicit interface as is done for other type of procedure. */
9833 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
9834 &gfc_current_locus))
9835 return MATCH_ERROR;
9836
9837 if (gfc_match_eos () != MATCH_YES)
9838 {
9839 /* Unset st->n.sym. Note: in reject_statement (), the symbol changes are
9840 undone, such that the st->n.sym->formal points to the original symbol;
9841 if now this namespace is finalized, the formal namespace is freed,
9842 but it might be still needed in the parent namespace. */
9843 gfc_symtree *st = gfc_find_symtree (gfc_current_ns->sym_root, sym->name);
9844 st->n.sym = NULL;
9845 gfc_free_symbol (sym->tlink);
9846 sym->tlink = NULL;
9847 sym->refs--;
9848 gfc_syntax_error (ST_MODULE_PROC);
9849 return MATCH_ERROR;
9850 }
9851
9852 return MATCH_YES;
9853
9854cleanup:
9855 gfc_free_formal_arglist (head);
9856 return MATCH_ERROR;
9857}
9858
9859
9860/* Match a module procedure statement. Note that we have to modify
9861 symbols in the parent's namespace because the current one was there
9862 to receive symbols that are in an interface's formal argument list. */
9863
9864match
9865gfc_match_modproc (void)
9866{
9867 char name[GFC_MAX_SYMBOL_LEN + 1];
9868 gfc_symbol *sym;
9869 match m;
9870 locus old_locus;
9871 gfc_namespace *module_ns;
9872 gfc_interface *old_interface_head, *interface;
9873
9874 if ((gfc_state_stack->state != COMP_INTERFACE
9875 && gfc_state_stack->state != COMP_CONTAINS)
9876 || gfc_state_stack->previous == NULL
9877 || current_interface.type == INTERFACE_NAMELESS
9878 || current_interface.type == INTERFACE_ABSTRACT)
9879 {
9880 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
9881 "interface");
9882 return MATCH_ERROR;
9883 }
9884
9885 module_ns = gfc_current_ns->parent;
9886 for (; module_ns; module_ns = module_ns->parent)
9887 if (module_ns->proc_name->attr.flavor == FL_MODULE
9888 || module_ns->proc_name->attr.flavor == FL_PROGRAM
9889 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
9890 && !module_ns->proc_name->attr.contained))
9891 break;
9892
9893 if (module_ns == NULL)
9894 return MATCH_ERROR;
9895
9896 /* Store the current state of the interface. We will need it if we
9897 end up with a syntax error and need to recover. */
9898 old_interface_head = gfc_current_interface_head ();
9899
9900 /* Check if the F2008 optional double colon appears. */
9901 gfc_gobble_whitespace ();
9902 old_locus = gfc_current_locus;
9903 if (gfc_match ("::") == MATCH_YES)
9904 {
9905 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
9906 "MODULE PROCEDURE statement at %L", &old_locus))
9907 return MATCH_ERROR;
9908 }
9909 else
9910 gfc_current_locus = old_locus;
9911
9912 for (;;)
9913 {
9914 bool last = false;
9915 old_locus = gfc_current_locus;
9916
9917 m = gfc_match_name (name);
9918 if (m == MATCH_NO)
9919 goto syntax;
9920 if (m != MATCH_YES)
9921 return MATCH_ERROR;
9922
9923 /* Check for syntax error before starting to add symbols to the
9924 current namespace. */
9925 if (gfc_match_eos () == MATCH_YES)
9926 last = true;
9927
9928 if (!last && gfc_match_char (',') != MATCH_YES)
9929 goto syntax;
9930
9931 /* Now we're sure the syntax is valid, we process this item
9932 further. */
9933 if (gfc_get_symbol (name, module_ns, &sym))
9934 return MATCH_ERROR;
9935
9936 if (sym->attr.intrinsic)
9937 {
9938 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
9939 "PROCEDURE", &old_locus);
9940 return MATCH_ERROR;
9941 }
9942
9943 if (sym->attr.proc != PROC_MODULE
9944 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
9945 return MATCH_ERROR;
9946
9947 if (!gfc_add_interface (sym))
9948 return MATCH_ERROR;
9949
9950 sym->attr.mod_proc = 1;
9951 sym->declared_at = old_locus;
9952
9953 if (last)
9954 break;
9955 }
9956
9957 return MATCH_YES;
9958
9959syntax:
9960 /* Restore the previous state of the interface. */
9961 interface = gfc_current_interface_head ();
9962 gfc_set_current_interface_head (old_interface_head);
9963
9964 /* Free the new interfaces. */
9965 while (interface != old_interface_head)
9966 {
9967 gfc_interface *i = interface->next;
9968 free (interface);
9969 interface = i;
9970 }
9971
9972 /* And issue a syntax error. */
9973 gfc_syntax_error (ST_MODULE_PROC);
9974 return MATCH_ERROR;
9975}
9976
9977
9978/* Check a derived type that is being extended. */
9979
9980static gfc_symbol*
9981check_extended_derived_type (char *name)
9982{
9983 gfc_symbol *extended;
9984
9985 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
9986 {
9987 gfc_error ("Ambiguous symbol in TYPE definition at %C");
9988 return NULL;
9989 }
9990
9991 extended = gfc_find_dt_in_generic (extended);
9992
9993 /* F08:C428. */
9994 if (!extended)
9995 {
9996 gfc_error ("Symbol %qs at %C has not been previously defined", name);
9997 return NULL;
9998 }
9999
10000 if (extended->attr.flavor != FL_DERIVED)
10001 {
10002 gfc_error ("%qs in EXTENDS expression at %C is not a "
10003 "derived type", name);
10004 return NULL;
10005 }
10006
10007 if (extended->attr.is_bind_c)
10008 {
10009 gfc_error ("%qs cannot be extended at %C because it "
10010 "is BIND(C)", extended->name);
10011 return NULL;
10012 }
10013
10014 if (extended->attr.sequence)
10015 {
10016 gfc_error ("%qs cannot be extended at %C because it "
10017 "is a SEQUENCE type", extended->name);
10018 return NULL;
10019 }
10020
10021 return extended;
10022}
10023
10024
10025/* Match the optional attribute specifiers for a type declaration.
10026 Return MATCH_ERROR if an error is encountered in one of the handled
10027 attributes (public, private, bind(c)), MATCH_NO if what's found is
10028 not a handled attribute, and MATCH_YES otherwise. TODO: More error
10029 checking on attribute conflicts needs to be done. */
10030
10031match
10032gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
10033{
10034 /* See if the derived type is marked as private. */
10035 if (gfc_match (" , private") == MATCH_YES)
10036 {
10037 if (gfc_current_state () != COMP_MODULE)
10038 {
10039 gfc_error ("Derived type at %C can only be PRIVATE in the "
10040 "specification part of a module");
10041 return MATCH_ERROR;
10042 }
10043
10044 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
10045 return MATCH_ERROR;
10046 }
10047 else if (gfc_match (" , public") == MATCH_YES)
10048 {
10049 if (gfc_current_state () != COMP_MODULE)
10050 {
10051 gfc_error ("Derived type at %C can only be PUBLIC in the "
10052 "specification part of a module");
10053 return MATCH_ERROR;
10054 }
10055
10056 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
10057 return MATCH_ERROR;
10058 }
10059 else if (gfc_match (" , bind ( c )") == MATCH_YES)
10060 {
10061 /* If the type is defined to be bind(c) it then needs to make
10062 sure that all fields are interoperable. This will
10063 need to be a semantic check on the finished derived type.
10064 See 15.2.3 (lines 9-12) of F2003 draft. */
10065 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
10066 return MATCH_ERROR;
10067
10068 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
10069 }
10070 else if (gfc_match (" , abstract") == MATCH_YES)
10071 {
10072 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
10073 return MATCH_ERROR;
10074
10075 if (!gfc_add_abstract (attr, &gfc_current_locus))
10076 return MATCH_ERROR;
10077 }
10078 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
10079 {
10080 if (!gfc_add_extension (attr, &gfc_current_locus))
10081 return MATCH_ERROR;
10082 }
10083 else
10084 return MATCH_NO;
10085
10086 /* If we get here, something matched. */
10087 return MATCH_YES;
10088}
10089
10090
10091/* Common function for type declaration blocks similar to derived types, such
10092 as STRUCTURES and MAPs. Unlike derived types, a structure type
10093 does NOT have a generic symbol matching the name given by the user.
10094 STRUCTUREs can share names with variables and PARAMETERs so we must allow
10095 for the creation of an independent symbol.
10096 Other parameters are a message to prefix errors with, the name of the new
10097 type to be created, and the flavor to add to the resulting symbol. */
10098
10099static bool
10100get_struct_decl (const char *name, sym_flavor fl, locus *decl,
10101 gfc_symbol **result)
10102{
10103 gfc_symbol *sym;
10104 locus where;
10105
10106 gcc_assert (name[0] == (char) TOUPPER (name[0]));
10107
10108 if (decl)
10109 where = *decl;
10110 else
10111 where = gfc_current_locus;
10112
10113 if (gfc_get_symbol (name, NULL, &sym))
10114 return false;
10115
10116 if (!sym)
10117 {
10118 gfc_internal_error ("Failed to create structure type '%s' at %C", name);
10119 return false;
10120 }
10121
10122 if (sym->components != NULL || sym->attr.zero_comp)
10123 {
10124 gfc_error ("Type definition of %qs at %C was already defined at %L",
10125 sym->name, &sym->declared_at);
10126 return false;
10127 }
10128
10129 sym->declared_at = where;
10130
10131 if (sym->attr.flavor != fl
10132 && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL))
10133 return false;
10134
10135 if (!sym->hash_value)
10136 /* Set the hash for the compound name for this type. */
10137 sym->hash_value = gfc_hash_value (sym);
10138
10139 /* Normally the type is expected to have been completely parsed by the time
10140 a field declaration with this type is seen. For unions, maps, and nested
10141 structure declarations, we need to indicate that it is okay that we
10142 haven't seen any components yet. This will be updated after the structure
10143 is fully parsed. */
10144 sym->attr.zero_comp = 0;
10145
10146 /* Structures always act like derived-types with the SEQUENCE attribute */
10147 gfc_add_sequence (&sym->attr, sym->name, NULL);
10148
10149 if (result) *result = sym;
10150
10151 return true;
10152}
10153
10154
10155/* Match the opening of a MAP block. Like a struct within a union in C;
10156 behaves identical to STRUCTURE blocks. */
10157
10158match
10159gfc_match_map (void)
10160{
10161 /* Counter used to give unique internal names to map structures. */
10162 static unsigned int gfc_map_id = 0;
10163 char name[GFC_MAX_SYMBOL_LEN + 1];
10164 gfc_symbol *sym;
10165 locus old_loc;
10166
10167 old_loc = gfc_current_locus;
10168
10169 if (gfc_match_eos () != MATCH_YES)
10170 {
10171 gfc_error ("Junk after MAP statement at %C");
10172 gfc_current_locus = old_loc;
10173 return MATCH_ERROR;
10174 }
10175
10176 /* Map blocks are anonymous so we make up unique names for the symbol table
10177 which are invalid Fortran identifiers. */
10178 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++);
10179
10180 if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym))
10181 return MATCH_ERROR;
10182
10183 gfc_new_block = sym;
10184
10185 return MATCH_YES;
10186}
10187
10188
10189/* Match the opening of a UNION block. */
10190
10191match
10192gfc_match_union (void)
10193{
10194 /* Counter used to give unique internal names to union types. */
10195 static unsigned int gfc_union_id = 0;
10196 char name[GFC_MAX_SYMBOL_LEN + 1];
10197 gfc_symbol *sym;
10198 locus old_loc;
10199
10200 old_loc = gfc_current_locus;
10201
10202 if (gfc_match_eos () != MATCH_YES)
10203 {
10204 gfc_error ("Junk after UNION statement at %C");
10205 gfc_current_locus = old_loc;
10206 return MATCH_ERROR;
10207 }
10208
10209 /* Unions are anonymous so we make up unique names for the symbol table
10210 which are invalid Fortran identifiers. */
10211 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++);
10212
10213 if (!get_struct_decl (name, FL_UNION, &old_loc, &sym))
10214 return MATCH_ERROR;
10215
10216 gfc_new_block = sym;
10217
10218 return MATCH_YES;
10219}
10220
10221
10222/* Match the beginning of a STRUCTURE declaration. This is similar to
10223 matching the beginning of a derived type declaration with a few
10224 twists. The resulting type symbol has no access control or other
10225 interesting attributes. */
10226
10227match
10228gfc_match_structure_decl (void)
10229{
10230 /* Counter used to give unique internal names to anonymous structures. */
10231 static unsigned int gfc_structure_id = 0;
10232 char name[GFC_MAX_SYMBOL_LEN + 1];
10233 gfc_symbol *sym;
10234 match m;
10235 locus where;
10236
10237 if (!flag_dec_structure)
10238 {
10239 gfc_error ("%s at %C is a DEC extension, enable with "
10240 "%<-fdec-structure%>",
10241 "STRUCTURE");
10242 return MATCH_ERROR;
10243 }
10244
10245 name[0] = '\0';
10246
10247 m = gfc_match (" /%n/", name);
10248 if (m != MATCH_YES)
10249 {
10250 /* Non-nested structure declarations require a structure name. */
10251 if (!gfc_comp_struct (gfc_current_state ()))
10252 {
10253 gfc_error ("Structure name expected in non-nested structure "
10254 "declaration at %C");
10255 return MATCH_ERROR;
10256 }
10257 /* This is an anonymous structure; make up a unique name for it
10258 (upper-case letters never make it to symbol names from the source).
10259 The important thing is initializing the type variable
10260 and setting gfc_new_symbol, which is immediately used by
10261 parse_structure () and variable_decl () to add components of
10262 this type. */
10263 snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++);
10264 }
10265
10266 where = gfc_current_locus;
10267 /* No field list allowed after non-nested structure declaration. */
10268 if (!gfc_comp_struct (gfc_current_state ())
10269 && gfc_match_eos () != MATCH_YES)
10270 {
10271 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
10272 return MATCH_ERROR;
10273 }
10274
10275 /* Make sure the name is not the name of an intrinsic type. */
10276 if (gfc_is_intrinsic_typename (name))
10277 {
10278 gfc_error ("Structure name %qs at %C cannot be the same as an"
10279 " intrinsic type", name);
10280 return MATCH_ERROR;
10281 }
10282
10283 /* Store the actual type symbol for the structure with an upper-case first
10284 letter (an invalid Fortran identifier). */
10285
10286 if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym))
10287 return MATCH_ERROR;
10288
10289 gfc_new_block = sym;
10290 return MATCH_YES;
10291}
10292
10293
10294/* This function does some work to determine which matcher should be used to
10295 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
10296 * as an alias for PRINT from derived type declarations, TYPE IS statements,
10297 * and [parameterized] derived type declarations. */
10298
10299match
10300gfc_match_type (gfc_statement *st)
10301{
10302 char name[GFC_MAX_SYMBOL_LEN + 1];
10303 match m;
10304 locus old_loc;
10305
10306 /* Requires -fdec. */
10307 if (!flag_dec)
10308 return MATCH_NO;
10309
10310 m = gfc_match ("type");
10311 if (m != MATCH_YES)
10312 return m;
10313 /* If we already have an error in the buffer, it is probably from failing to
10314 * match a derived type data declaration. Let it happen. */
10315 else if (gfc_error_flag_test ())
10316 return MATCH_NO;
10317
10318 old_loc = gfc_current_locus;
10319 *st = ST_NONE;
10320
10321 /* If we see an attribute list before anything else it's definitely a derived
10322 * type declaration. */
10323 if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES)
10324 goto derived;
10325
10326 /* By now "TYPE" has already been matched. If we do not see a name, this may
10327 * be something like "TYPE *" or "TYPE <fmt>". */
10328 m = gfc_match_name (name);
10329 if (m != MATCH_YES)
10330 {
10331 /* Let print match if it can, otherwise throw an error from
10332 * gfc_match_derived_decl. */
10333 gfc_current_locus = old_loc;
10334 if (gfc_match_print () == MATCH_YES)
10335 {
10336 *st = ST_WRITE;
10337 return MATCH_YES;
10338 }
10339 goto derived;
10340 }
10341
10342 /* Check for EOS. */
10343 if (gfc_match_eos () == MATCH_YES)
10344 {
10345 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
10346 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
10347 * Otherwise if gfc_match_derived_decl fails it's probably an existing
10348 * symbol which can be printed. */
10349 gfc_current_locus = old_loc;
10350 m = gfc_match_derived_decl ();
10351 if (gfc_is_intrinsic_typename (name) || m == MATCH_YES)
10352 {
10353 *st = ST_DERIVED_DECL;
10354 return m;
10355 }
10356 }
10357 else
10358 {
10359 /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration
10360 like <type name(parameter)>. */
10361 gfc_gobble_whitespace ();
10362 bool paren = gfc_peek_ascii_char () == '(';
10363 if (paren)
10364 {
10365 if (strcmp ("is", name) == 0)
10366 goto typeis;
10367 else
10368 goto derived;
10369 }
10370 }
10371
10372 /* Treat TYPE... like PRINT... */
10373 gfc_current_locus = old_loc;
10374 *st = ST_WRITE;
10375 return gfc_match_print ();
10376
10377derived:
10378 gfc_current_locus = old_loc;
10379 *st = ST_DERIVED_DECL;
10380 return gfc_match_derived_decl ();
10381
10382typeis:
10383 gfc_current_locus = old_loc;
10384 *st = ST_TYPE_IS;
10385 return gfc_match_type_is ();
10386}
10387
10388
10389/* Match the beginning of a derived type declaration. If a type name
10390 was the result of a function, then it is possible to have a symbol
10391 already to be known as a derived type yet have no components. */
10392
10393match
10394gfc_match_derived_decl (void)
10395{
10396 char name[GFC_MAX_SYMBOL_LEN + 1];
10397 char parent[GFC_MAX_SYMBOL_LEN + 1];
10398 symbol_attribute attr;
10399 gfc_symbol *sym, *gensym;
10400 gfc_symbol *extended;
10401 match m;
10402 match is_type_attr_spec = MATCH_NO;
10403 bool seen_attr = false;
10404 gfc_interface *intr = NULL, *head;
10405 bool parameterized_type = false;
10406 bool seen_colons = false;
10407
10408 if (gfc_comp_struct (gfc_current_state ()))
10409 return MATCH_NO;
10410
10411 name[0] = '\0';
10412 parent[0] = '\0';
10413 gfc_clear_attr (&attr);
10414 extended = NULL;
10415
10416 do
10417 {
10418 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
10419 if (is_type_attr_spec == MATCH_ERROR)
10420 return MATCH_ERROR;
10421 if (is_type_attr_spec == MATCH_YES)
10422 seen_attr = true;
10423 } while (is_type_attr_spec == MATCH_YES);
10424
10425 /* Deal with derived type extensions. The extension attribute has
10426 been added to 'attr' but now the parent type must be found and
10427 checked. */
10428 if (parent[0])
10429 extended = check_extended_derived_type (parent);
10430
10431 if (parent[0] && !extended)
10432 return MATCH_ERROR;
10433
10434 m = gfc_match (" ::");
10435 if (m == MATCH_YES)
10436 {
10437 seen_colons = true;
10438 }
10439 else if (seen_attr)
10440 {
10441 gfc_error ("Expected :: in TYPE definition at %C");
10442 return MATCH_ERROR;
10443 }
10444
10445 /* In free source form, need to check for TYPE XXX as oppose to TYPEXXX.
10446 But, we need to simply return for TYPE(. */
10447 if (m == MATCH_NO && gfc_current_form == FORM_FREE)
10448 {
10449 char c = gfc_peek_ascii_char ();
10450 if (c == '(')
10451 return m;
10452 if (!gfc_is_whitespace (c))
10453 {
10454 gfc_error ("Mangled derived type definition at %C");
10455 return MATCH_NO;
10456 }
10457 }
10458
10459 m = gfc_match (" %n ", name);
10460 if (m != MATCH_YES)
10461 return m;
10462
10463 /* Make sure that we don't identify TYPE IS (...) as a parameterized
10464 derived type named 'is'.
10465 TODO Expand the check, when 'name' = "is" by matching " (tname) "
10466 and checking if this is a(n intrinsic) typename. This picks up
10467 misplaced TYPE IS statements such as in select_type_1.f03. */
10468 if (gfc_peek_ascii_char () == '(')
10469 {
10470 if (gfc_current_state () == COMP_SELECT_TYPE
10471 || (!seen_colons && !strcmp (name, "is")))
10472 return MATCH_NO;
10473 parameterized_type = true;
10474 }
10475
10476 m = gfc_match_eos ();
10477 if (m != MATCH_YES && !parameterized_type)
10478 return m;
10479
10480 /* Make sure the name is not the name of an intrinsic type. */
10481 if (gfc_is_intrinsic_typename (name))
10482 {
10483 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
10484 "type", name);
10485 return MATCH_ERROR;
10486 }
10487
10488 if (gfc_get_symbol (name, NULL, &gensym))
10489 return MATCH_ERROR;
10490
10491 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
10492 {
10493 if (gensym->ts.u.derived)
10494 gfc_error ("Derived type name %qs at %C already has a basic type "
10495 "of %s", gensym->name, gfc_typename (&gensym->ts));
10496 else
10497 gfc_error ("Derived type name %qs at %C already has a basic type",
10498 gensym->name);
10499 return MATCH_ERROR;
10500 }
10501
10502 if (!gensym->attr.generic
10503 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
10504 return MATCH_ERROR;
10505
10506 if (!gensym->attr.function
10507 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
10508 return MATCH_ERROR;
10509
10510 if (gensym->attr.dummy)
10511 {
10512 gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C",
10513 name, &gensym->declared_at);
10514 return MATCH_ERROR;
10515 }
10516
10517 sym = gfc_find_dt_in_generic (gensym);
10518
10519 if (sym && (sym->components != NULL || sym->attr.zero_comp))
10520 {
10521 gfc_error ("Derived type definition of %qs at %C has already been "
10522 "defined", sym->name);
10523 return MATCH_ERROR;
10524 }
10525
10526 if (!sym)
10527 {
10528 /* Use upper case to save the actual derived-type symbol. */
10529 gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym);
10530 sym->name = gfc_get_string ("%s", gensym->name);
10531 head = gensym->generic;
10532 intr = gfc_get_interface ();
10533 intr->sym = sym;
10534 intr->where = gfc_current_locus;
10535 intr->sym->declared_at = gfc_current_locus;
10536 intr->next = head;
10537 gensym->generic = intr;
10538 gensym->attr.if_source = IFSRC_DECL;
10539 }
10540
10541 /* The symbol may already have the derived attribute without the
10542 components. The ways this can happen is via a function
10543 definition, an INTRINSIC statement or a subtype in another
10544 derived type that is a pointer. The first part of the AND clause
10545 is true if the symbol is not the return value of a function. */
10546 if (sym->attr.flavor != FL_DERIVED
10547 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
10548 return MATCH_ERROR;
10549
10550 if (attr.access != ACCESS_UNKNOWN
10551 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
10552 return MATCH_ERROR;
10553 else if (sym->attr.access == ACCESS_UNKNOWN
10554 && gensym->attr.access != ACCESS_UNKNOWN
10555 && !gfc_add_access (&sym->attr, gensym->attr.access,
10556 sym->name, NULL))
10557 return MATCH_ERROR;
10558
10559 if (sym->attr.access != ACCESS_UNKNOWN
10560 && gensym->attr.access == ACCESS_UNKNOWN)
10561 gensym->attr.access = sym->attr.access;
10562
10563 /* See if the derived type was labeled as bind(c). */
10564 if (attr.is_bind_c != 0)
10565 sym->attr.is_bind_c = attr.is_bind_c;
10566
10567 /* Construct the f2k_derived namespace if it is not yet there. */
10568 if (!sym->f2k_derived)
10569 sym->f2k_derived = gfc_get_namespace (NULL, 0);
10570
10571 if (parameterized_type)
10572 {
10573 /* Ignore error or mismatches by going to the end of the statement
10574 in order to avoid the component declarations causing problems. */
10575 m = gfc_match_formal_arglist (sym, 0, 0, true);
10576 if (m != MATCH_YES)
10577 gfc_error_recovery ();
10578 else
10579 sym->attr.pdt_template = 1;
10580 m = gfc_match_eos ();
10581 if (m != MATCH_YES)
10582 {
10583 gfc_error_recovery ();
10584 gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C");
10585 }
10586 }
10587
10588 if (extended && !sym->components)
10589 {
10590 gfc_component *p;
10591 gfc_formal_arglist *f, *g, *h;
10592
10593 /* Add the extended derived type as the first component. */
10594 gfc_add_component (sym, parent, &p);
10595 extended->refs++;
10596 gfc_set_sym_referenced (extended);
10597
10598 p->ts.type = BT_DERIVED;
10599 p->ts.u.derived = extended;
10600 p->initializer = gfc_default_initializer (&p->ts);
10601
10602 /* Set extension level. */
10603 if (extended->attr.extension == 255)
10604 {
10605 /* Since the extension field is 8 bit wide, we can only have
10606 up to 255 extension levels. */
10607 gfc_error ("Maximum extension level reached with type %qs at %L",
10608 extended->name, &extended->declared_at);
10609 return MATCH_ERROR;
10610 }
10611 sym->attr.extension = extended->attr.extension + 1;
10612
10613 /* Provide the links between the extended type and its extension. */
10614 if (!extended->f2k_derived)
10615 extended->f2k_derived = gfc_get_namespace (NULL, 0);
10616
10617 /* Copy the extended type-param-name-list from the extended type,
10618 append those of the extension and add the whole lot to the
10619 extension. */
10620 if (extended->attr.pdt_template)
10621 {
10622 g = h = NULL;
10623 sym->attr.pdt_template = 1;
10624 for (f = extended->formal; f; f = f->next)
10625 {
10626 if (f == extended->formal)
10627 {
10628 g = gfc_get_formal_arglist ();
10629 h = g;
10630 }
10631 else
10632 {
10633 g->next = gfc_get_formal_arglist ();
10634 g = g->next;
10635 }
10636 g->sym = f->sym;
10637 }
10638 g->next = sym->formal;
10639 sym->formal = h;
10640 }
10641 }
10642
10643 if (!sym->hash_value)
10644 /* Set the hash for the compound name for this type. */
10645 sym->hash_value = gfc_hash_value (sym);
10646
10647 /* Take over the ABSTRACT attribute. */
10648 sym->attr.abstract = attr.abstract;
10649
10650 gfc_new_block = sym;
10651
10652 return MATCH_YES;
10653}
10654
10655
10656/* Cray Pointees can be declared as:
10657 pointer (ipt, a (n,m,...,*)) */
10658
10659match
10660gfc_mod_pointee_as (gfc_array_spec *as)
10661{
10662 as->cray_pointee = true; /* This will be useful to know later. */
10663 if (as->type == AS_ASSUMED_SIZE)
10664 as->cp_was_assumed = true;
10665 else if (as->type == AS_ASSUMED_SHAPE)
10666 {
10667 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
10668 return MATCH_ERROR;
10669 }
10670 return MATCH_YES;
10671}
10672
10673
10674/* Match the enum definition statement, here we are trying to match
10675 the first line of enum definition statement.
10676 Returns MATCH_YES if match is found. */
10677
10678match
10679gfc_match_enum (void)
10680{
10681 match m;
10682
10683 m = gfc_match_eos ();
10684 if (m != MATCH_YES)
10685 return m;
10686
10687 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
10688 return MATCH_ERROR;
10689
10690 return MATCH_YES;
10691}
10692
10693
10694/* Returns an initializer whose value is one higher than the value of the
10695 LAST_INITIALIZER argument. If the argument is NULL, the
10696 initializers value will be set to zero. The initializer's kind
10697 will be set to gfc_c_int_kind.
10698
10699 If -fshort-enums is given, the appropriate kind will be selected
10700 later after all enumerators have been parsed. A warning is issued
10701 here if an initializer exceeds gfc_c_int_kind. */
10702
10703static gfc_expr *
10704enum_initializer (gfc_expr *last_initializer, locus where)
10705{
10706 gfc_expr *result;
10707 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
10708
10709 mpz_init (result->value.integer);
10710
10711 if (last_initializer != NULL)
10712 {
10713 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
10714 result->where = last_initializer->where;
10715
10716 if (gfc_check_integer_range (result->value.integer,
10717 gfc_c_int_kind) != ARITH_OK)
10718 {
10719 gfc_error ("Enumerator exceeds the C integer type at %C");
10720 return NULL;
10721 }
10722 }
10723 else
10724 {
10725 /* Control comes here, if it's the very first enumerator and no
10726 initializer has been given. It will be initialized to zero. */
10727 mpz_set_si (result->value.integer, 0);
10728 }
10729
10730 return result;
10731}
10732
10733
10734/* Match a variable name with an optional initializer. When this
10735 subroutine is called, a variable is expected to be parsed next.
10736 Depending on what is happening at the moment, updates either the
10737 symbol table or the current interface. */
10738
10739static match
10740enumerator_decl (void)
10741{
10742 char name[GFC_MAX_SYMBOL_LEN + 1];
10743 gfc_expr *initializer;
10744 gfc_array_spec *as = NULL;
10745 gfc_symbol *sym;
10746 locus var_locus;
10747 match m;
10748 bool t;
10749 locus old_locus;
10750
10751 initializer = NULL;
10752 old_locus = gfc_current_locus;
10753
10754 /* When we get here, we've just matched a list of attributes and
10755 maybe a type and a double colon. The next thing we expect to see
10756 is the name of the symbol. */
10757 m = gfc_match_name (name);
10758 if (m != MATCH_YES)
10759 goto cleanup;
10760
10761 var_locus = gfc_current_locus;
10762
10763 /* OK, we've successfully matched the declaration. Now put the
10764 symbol in the current namespace. If we fail to create the symbol,
10765 bail out. */
10766 if (!build_sym (name, NULL, false, &as, &var_locus))
10767 {
10768 m = MATCH_ERROR;
10769 goto cleanup;
10770 }
10771
10772 /* The double colon must be present in order to have initializers.
10773 Otherwise the statement is ambiguous with an assignment statement. */
10774 if (colon_seen)
10775 {
10776 if (gfc_match_char ('=') == MATCH_YES)
10777 {
10778 m = gfc_match_init_expr (&initializer);
10779 if (m == MATCH_NO)
10780 {
10781 gfc_error ("Expected an initialization expression at %C");
10782 m = MATCH_ERROR;
10783 }
10784
10785 if (m != MATCH_YES)
10786 goto cleanup;
10787 }
10788 }
10789
10790 /* If we do not have an initializer, the initialization value of the
10791 previous enumerator (stored in last_initializer) is incremented
10792 by 1 and is used to initialize the current enumerator. */
10793 if (initializer == NULL)
10794 initializer = enum_initializer (last_initializer, old_locus);
10795
10796 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
10797 {
10798 gfc_error ("ENUMERATOR %L not initialized with integer expression",
10799 &var_locus);
10800 m = MATCH_ERROR;
10801 goto cleanup;
10802 }
10803
10804 /* Store this current initializer, for the next enumerator variable
10805 to be parsed. add_init_expr_to_sym() zeros initializer, so we
10806 use last_initializer below. */
10807 last_initializer = initializer;
10808 t = add_init_expr_to_sym (name, &initializer, &var_locus);
10809
10810 /* Maintain enumerator history. */
10811 gfc_find_symbol (name, NULL, 0, &sym);
10812 create_enum_history (sym, last_initializer);
10813
10814 return (t) ? MATCH_YES : MATCH_ERROR;
10815
10816cleanup:
10817 /* Free stuff up and return. */
10818 gfc_free_expr (initializer);
10819
10820 return m;
10821}
10822
10823
10824/* Match the enumerator definition statement. */
10825
10826match
10827gfc_match_enumerator_def (void)
10828{
10829 match m;
10830 bool t;
10831
10832 gfc_clear_ts (&current_ts);
10833
10834 m = gfc_match (" enumerator");
10835 if (m != MATCH_YES)
10836 return m;
10837
10838 m = gfc_match (" :: ");
10839 if (m == MATCH_ERROR)
10840 return m;
10841
10842 colon_seen = (m == MATCH_YES);
10843
10844 if (gfc_current_state () != COMP_ENUM)
10845 {
10846 gfc_error ("ENUM definition statement expected before %C");
10847 gfc_free_enum_history ();
10848 return MATCH_ERROR;
10849 }
10850
10851 (&current_ts)->type = BT_INTEGER;
10852 (&current_ts)->kind = gfc_c_int_kind;
10853
10854 gfc_clear_attr (&current_attr);
10855 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
10856 if (!t)
10857 {
10858 m = MATCH_ERROR;
10859 goto cleanup;
10860 }
10861
10862 for (;;)
10863 {
10864 m = enumerator_decl ();
10865 if (m == MATCH_ERROR)
10866 {
10867 gfc_free_enum_history ();
10868 goto cleanup;
10869 }
10870 if (m == MATCH_NO)
10871 break;
10872
10873 if (gfc_match_eos () == MATCH_YES)
10874 goto cleanup;
10875 if (gfc_match_char (',') != MATCH_YES)
10876 break;
10877 }
10878
10879 if (gfc_current_state () == COMP_ENUM)
10880 {
10881 gfc_free_enum_history ();
10882 gfc_error ("Syntax error in ENUMERATOR definition at %C");
10883 m = MATCH_ERROR;
10884 }
10885
10886cleanup:
10887 gfc_free_array_spec (current_as);
10888 current_as = NULL;
10889 return m;
10890
10891}
10892
10893
10894/* Match binding attributes. */
10895
10896static match
10897match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
10898{
10899 bool found_passing = false;
10900 bool seen_ptr = false;
10901 match m = MATCH_YES;
10902
10903 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
10904 this case the defaults are in there. */
10905 ba->access = ACCESS_UNKNOWN;
10906 ba->pass_arg = NULL;
10907 ba->pass_arg_num = 0;
10908 ba->nopass = 0;
10909 ba->non_overridable = 0;
10910 ba->deferred = 0;
10911 ba->ppc = ppc;
10912
10913 /* If we find a comma, we believe there are binding attributes. */
10914 m = gfc_match_char (',');
10915 if (m == MATCH_NO)
10916 goto done;
10917
10918 do
10919 {
10920 /* Access specifier. */
10921
10922 m = gfc_match (" public");
10923 if (m == MATCH_ERROR)
10924 goto error;
10925 if (m == MATCH_YES)
10926 {
10927 if (ba->access != ACCESS_UNKNOWN)
10928 {
10929 gfc_error ("Duplicate access-specifier at %C");
10930 goto error;
10931 }
10932
10933 ba->access = ACCESS_PUBLIC;
10934 continue;
10935 }
10936
10937 m = gfc_match (" private");
10938 if (m == MATCH_ERROR)
10939 goto error;
10940 if (m == MATCH_YES)
10941 {
10942 if (ba->access != ACCESS_UNKNOWN)
10943 {
10944 gfc_error ("Duplicate access-specifier at %C");
10945 goto error;
10946 }
10947
10948 ba->access = ACCESS_PRIVATE;
10949 continue;
10950 }
10951
10952 /* If inside GENERIC, the following is not allowed. */
10953 if (!generic)
10954 {
10955
10956 /* NOPASS flag. */
10957 m = gfc_match (" nopass");
10958 if (m == MATCH_ERROR)
10959 goto error;
10960 if (m == MATCH_YES)
10961 {
10962 if (found_passing)
10963 {
10964 gfc_error ("Binding attributes already specify passing,"
10965 " illegal NOPASS at %C");
10966 goto error;
10967 }
10968
10969 found_passing = true;
10970 ba->nopass = 1;
10971 continue;
10972 }
10973
10974 /* PASS possibly including argument. */
10975 m = gfc_match (" pass");
10976 if (m == MATCH_ERROR)
10977 goto error;
10978 if (m == MATCH_YES)
10979 {
10980 char arg[GFC_MAX_SYMBOL_LEN + 1];
10981
10982 if (found_passing)
10983 {
10984 gfc_error ("Binding attributes already specify passing,"
10985 " illegal PASS at %C");
10986 goto error;
10987 }
10988
10989 m = gfc_match (" ( %n )", arg);
10990 if (m == MATCH_ERROR)
10991 goto error;
10992 if (m == MATCH_YES)
10993 ba->pass_arg = gfc_get_string ("%s", arg);
10994 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
10995
10996 found_passing = true;
10997 ba->nopass = 0;
10998 continue;
10999 }
11000
11001 if (ppc)
11002 {
11003 /* POINTER flag. */
11004 m = gfc_match (" pointer");
11005 if (m == MATCH_ERROR)
11006 goto error;
11007 if (m == MATCH_YES)
11008 {
11009 if (seen_ptr)
11010 {
11011 gfc_error ("Duplicate POINTER attribute at %C");
11012 goto error;
11013 }
11014
11015 seen_ptr = true;
11016 continue;
11017 }
11018 }
11019 else
11020 {
11021 /* NON_OVERRIDABLE flag. */
11022 m = gfc_match (" non_overridable");
11023 if (m == MATCH_ERROR)
11024 goto error;
11025 if (m == MATCH_YES)
11026 {
11027 if (ba->non_overridable)
11028 {
11029 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
11030 goto error;
11031 }
11032
11033 ba->non_overridable = 1;
11034 continue;
11035 }
11036
11037 /* DEFERRED flag. */
11038 m = gfc_match (" deferred");
11039 if (m == MATCH_ERROR)
11040 goto error;
11041 if (m == MATCH_YES)
11042 {
11043 if (ba->deferred)
11044 {
11045 gfc_error ("Duplicate DEFERRED at %C");
11046 goto error;
11047 }
11048
11049 ba->deferred = 1;
11050 continue;
11051 }
11052 }
11053
11054 }
11055
11056 /* Nothing matching found. */
11057 if (generic)
11058 gfc_error ("Expected access-specifier at %C");
11059 else
11060 gfc_error ("Expected binding attribute at %C");
11061 goto error;
11062 }
11063 while (gfc_match_char (',') == MATCH_YES);
11064
11065 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
11066 if (ba->non_overridable && ba->deferred)
11067 {
11068 gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C");
11069 goto error;
11070 }
11071
11072 m = MATCH_YES;
11073
11074done:
11075 if (ba->access == ACCESS_UNKNOWN)
11076 ba->access = ppc ? gfc_current_block()->component_access
11077 : gfc_typebound_default_access;
11078
11079 if (ppc && !seen_ptr)
11080 {
11081 gfc_error ("POINTER attribute is required for procedure pointer component"
11082 " at %C");
11083 goto error;
11084 }
11085
11086 return m;
11087
11088error:
11089 return MATCH_ERROR;
11090}
11091
11092
11093/* Match a PROCEDURE specific binding inside a derived type. */
11094
11095static match
11096match_procedure_in_type (void)
11097{
11098 char name[GFC_MAX_SYMBOL_LEN + 1];
11099 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
11100 char* target = NULL, *ifc = NULL;
11101 gfc_typebound_proc tb;
11102 bool seen_colons;
11103 bool seen_attrs;
11104 match m;
11105 gfc_symtree* stree;
11106 gfc_namespace* ns;
11107 gfc_symbol* block;
11108 int num;
11109
11110 /* Check current state. */
11111 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
11112 block = gfc_state_stack->previous->sym;
11113 gcc_assert (block);
11114
11115 /* Try to match PROCEDURE(interface). */
11116 if (gfc_match (" (") == MATCH_YES)
11117 {
11118 m = gfc_match_name (target_buf);
11119 if (m == MATCH_ERROR)
11120 return m;
11121 if (m != MATCH_YES)
11122 {
11123 gfc_error ("Interface-name expected after %<(%> at %C");
11124 return MATCH_ERROR;
11125 }
11126
11127 if (gfc_match (" )") != MATCH_YES)
11128 {
11129 gfc_error ("%<)%> expected at %C");
11130 return MATCH_ERROR;
11131 }
11132
11133 ifc = target_buf;
11134 }
11135
11136 /* Construct the data structure. */
11137 memset (&tb, 0, sizeof (tb));
11138 tb.where = gfc_current_locus;
11139
11140 /* Match binding attributes. */
11141 m = match_binding_attributes (&tb, false, false);
11142 if (m == MATCH_ERROR)
11143 return m;
11144 seen_attrs = (m == MATCH_YES);
11145
11146 /* Check that attribute DEFERRED is given if an interface is specified. */
11147 if (tb.deferred && !ifc)
11148 {
11149 gfc_error ("Interface must be specified for DEFERRED binding at %C");
11150 return MATCH_ERROR;
11151 }
11152 if (ifc && !tb.deferred)
11153 {
11154 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
11155 return MATCH_ERROR;
11156 }
11157
11158 /* Match the colons. */
11159 m = gfc_match (" ::");
11160 if (m == MATCH_ERROR)
11161 return m;
11162 seen_colons = (m == MATCH_YES);
11163 if (seen_attrs && !seen_colons)
11164 {
11165 gfc_error ("Expected %<::%> after binding-attributes at %C");
11166 return MATCH_ERROR;
11167 }
11168
11169 /* Match the binding names. */
11170 for(num=1;;num++)
11171 {
11172 m = gfc_match_name (name);
11173 if (m == MATCH_ERROR)
11174 return m;
11175 if (m == MATCH_NO)
11176 {
11177 gfc_error ("Expected binding name at %C");
11178 return MATCH_ERROR;
11179 }
11180
11181 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
11182 return MATCH_ERROR;
11183
11184 /* Try to match the '=> target', if it's there. */
11185 target = ifc;
11186 m = gfc_match (" =>");
11187 if (m == MATCH_ERROR)
11188 return m;
11189 if (m == MATCH_YES)
11190 {
11191 if (tb.deferred)
11192 {
11193 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
11194 return MATCH_ERROR;
11195 }
11196
11197 if (!seen_colons)
11198 {
11199 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
11200 " at %C");
11201 return MATCH_ERROR;
11202 }
11203
11204 m = gfc_match_name (target_buf);
11205 if (m == MATCH_ERROR)
11206 return m;
11207 if (m == MATCH_NO)
11208 {
11209 gfc_error ("Expected binding target after %<=>%> at %C");
11210 return MATCH_ERROR;
11211 }
11212 target = target_buf;
11213 }
11214
11215 /* If no target was found, it has the same name as the binding. */
11216 if (!target)
11217 target = name;
11218
11219 /* Get the namespace to insert the symbols into. */
11220 ns = block->f2k_derived;
11221 gcc_assert (ns);
11222
11223 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
11224 if (tb.deferred && !block->attr.abstract)
11225 {
11226 gfc_error ("Type %qs containing DEFERRED binding at %C "
11227 "is not ABSTRACT", block->name);
11228 return MATCH_ERROR;
11229 }
11230
11231 /* See if we already have a binding with this name in the symtree which
11232 would be an error. If a GENERIC already targeted this binding, it may
11233 be already there but then typebound is still NULL. */
11234 stree = gfc_find_symtree (ns->tb_sym_root, name);
11235 if (stree && stree->n.tb)
11236 {
11237 gfc_error ("There is already a procedure with binding name %qs for "
11238 "the derived type %qs at %C", name, block->name);
11239 return MATCH_ERROR;
11240 }
11241
11242 /* Insert it and set attributes. */
11243
11244 if (!stree)
11245 {
11246 stree = gfc_new_symtree (&ns->tb_sym_root, name);
11247 gcc_assert (stree);
11248 }
11249 stree->n.tb = gfc_get_typebound_proc (&tb);
11250
11251 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
11252 false))
11253 return MATCH_ERROR;
11254 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
11255 gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE,
11256 target, &stree->n.tb->u.specific->n.sym->declared_at);
11257
11258 if (gfc_match_eos () == MATCH_YES)
11259 return MATCH_YES;
11260 if (gfc_match_char (',') != MATCH_YES)
11261 goto syntax;
11262 }
11263
11264syntax:
11265 gfc_error ("Syntax error in PROCEDURE statement at %C");
11266 return MATCH_ERROR;
11267}
11268
11269
11270/* Match a GENERIC procedure binding inside a derived type. */
11271
11272match
11273gfc_match_generic (void)
11274{
11275 char name[GFC_MAX_SYMBOL_LEN + 1];
11276 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
11277 gfc_symbol* block;
11278 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
11279 gfc_typebound_proc* tb;
11280 gfc_namespace* ns;
11281 interface_type op_type;
11282 gfc_intrinsic_op op;
11283 match m;
11284
11285 /* Check current state. */
11286 if (gfc_current_state () == COMP_DERIVED)
11287 {
11288 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
11289 return MATCH_ERROR;
11290 }
11291 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
11292 return MATCH_NO;
11293 block = gfc_state_stack->previous->sym;
11294 ns = block->f2k_derived;
11295 gcc_assert (block && ns);
11296
11297 memset (&tbattr, 0, sizeof (tbattr));
11298 tbattr.where = gfc_current_locus;
11299
11300 /* See if we get an access-specifier. */
11301 m = match_binding_attributes (&tbattr, true, false);
11302 if (m == MATCH_ERROR)
11303 goto error;
11304
11305 /* Now the colons, those are required. */
11306 if (gfc_match (" ::") != MATCH_YES)
11307 {
11308 gfc_error ("Expected %<::%> at %C");
11309 goto error;
11310 }
11311
11312 /* Match the binding name; depending on type (operator / generic) format
11313 it for future error messages into bind_name. */
11314
11315 m = gfc_match_generic_spec (&op_type, name, &op);
11316 if (m == MATCH_ERROR)
11317 return MATCH_ERROR;
11318 if (m == MATCH_NO)
11319 {
11320 gfc_error ("Expected generic name or operator descriptor at %C");
11321 goto error;
11322 }
11323
11324 switch (op_type)
11325 {
11326 case INTERFACE_GENERIC:
11327 case INTERFACE_DTIO:
11328 snprintf (bind_name, sizeof (bind_name), "%s", name);
11329 break;
11330
11331 case INTERFACE_USER_OP:
11332 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
11333 break;
11334
11335 case INTERFACE_INTRINSIC_OP:
11336 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
11337 gfc_op2string (op));
11338 break;
11339
11340 case INTERFACE_NAMELESS:
11341 gfc_error ("Malformed GENERIC statement at %C");
11342 goto error;
11343 break;
11344
11345 default:
11346 gcc_unreachable ();
11347 }
11348
11349 /* Match the required =>. */
11350 if (gfc_match (" =>") != MATCH_YES)
11351 {
11352 gfc_error ("Expected %<=>%> at %C");
11353 goto error;
11354 }
11355
11356 /* Try to find existing GENERIC binding with this name / for this operator;
11357 if there is something, check that it is another GENERIC and then extend
11358 it rather than building a new node. Otherwise, create it and put it
11359 at the right position. */
11360
11361 switch (op_type)
11362 {
11363 case INTERFACE_DTIO:
11364 case INTERFACE_USER_OP:
11365 case INTERFACE_GENERIC:
11366 {
11367 const bool is_op = (op_type == INTERFACE_USER_OP);
11368 gfc_symtree* st;
11369
11370 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
11371 tb = st ? st->n.tb : NULL;
11372 break;
11373 }
11374
11375 case INTERFACE_INTRINSIC_OP:
11376 tb = ns->tb_op[op];
11377 break;
11378
11379 default:
11380 gcc_unreachable ();
11381 }
11382
11383 if (tb)
11384 {
11385 if (!tb->is_generic)
11386 {
11387 gcc_assert (op_type == INTERFACE_GENERIC);
11388 gfc_error ("There's already a non-generic procedure with binding name"
11389 " %qs for the derived type %qs at %C",
11390 bind_name, block->name);
11391 goto error;
11392 }
11393
11394 if (tb->access != tbattr.access)
11395 {
11396 gfc_error ("Binding at %C must have the same access as already"
11397 " defined binding %qs", bind_name);
11398 goto error;
11399 }
11400 }
11401 else
11402 {
11403 tb = gfc_get_typebound_proc (NULL);
11404 tb->where = gfc_current_locus;
11405 tb->access = tbattr.access;
11406 tb->is_generic = 1;
11407 tb->u.generic = NULL;
11408
11409 switch (op_type)
11410 {
11411 case INTERFACE_DTIO:
11412 case INTERFACE_GENERIC:
11413 case INTERFACE_USER_OP:
11414 {
11415 const bool is_op = (op_type == INTERFACE_USER_OP);
11416 gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root :
11417 &ns->tb_sym_root, name);
11418 gcc_assert (st);
11419 st->n.tb = tb;
11420
11421 break;
11422 }
11423
11424 case INTERFACE_INTRINSIC_OP:
11425 ns->tb_op[op] = tb;
11426 break;
11427
11428 default:
11429 gcc_unreachable ();
11430 }
11431 }
11432
11433 /* Now, match all following names as specific targets. */
11434 do
11435 {
11436 gfc_symtree* target_st;
11437 gfc_tbp_generic* target;
11438
11439 m = gfc_match_name (name);
11440 if (m == MATCH_ERROR)
11441 goto error;
11442 if (m == MATCH_NO)
11443 {
11444 gfc_error ("Expected specific binding name at %C");
11445 goto error;
11446 }
11447
11448 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
11449
11450 /* See if this is a duplicate specification. */
11451 for (target = tb->u.generic; target; target = target->next)
11452 if (target_st == target->specific_st)
11453 {
11454 gfc_error ("%qs already defined as specific binding for the"
11455 " generic %qs at %C", name, bind_name);
11456 goto error;
11457 }
11458
11459 target = gfc_get_tbp_generic ();
11460 target->specific_st = target_st;
11461 target->specific = NULL;
11462 target->next = tb->u.generic;
11463 target->is_operator = ((op_type == INTERFACE_USER_OP)
11464 || (op_type == INTERFACE_INTRINSIC_OP));
11465 tb->u.generic = target;
11466 }
11467 while (gfc_match (" ,") == MATCH_YES);
11468
11469 /* Here should be the end. */
11470 if (gfc_match_eos () != MATCH_YES)
11471 {
11472 gfc_error ("Junk after GENERIC binding at %C");
11473 goto error;
11474 }
11475
11476 return MATCH_YES;
11477
11478error:
11479 return MATCH_ERROR;
11480}
11481
11482
11483/* Match a FINAL declaration inside a derived type. */
11484
11485match
11486gfc_match_final_decl (void)
11487{
11488 char name[GFC_MAX_SYMBOL_LEN + 1];
11489 gfc_symbol* sym;
11490 match m;
11491 gfc_namespace* module_ns;
11492 bool first, last;
11493 gfc_symbol* block;
11494
11495 if (gfc_current_form == FORM_FREE)
11496 {
11497 char c = gfc_peek_ascii_char ();
11498 if (!gfc_is_whitespace (c) && c != ':')
11499 return MATCH_NO;
11500 }
11501
11502 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
11503 {
11504 if (gfc_current_form == FORM_FIXED)
11505 return MATCH_NO;
11506
11507 gfc_error ("FINAL declaration at %C must be inside a derived type "
11508 "CONTAINS section");
11509 return MATCH_ERROR;
11510 }
11511
11512 block = gfc_state_stack->previous->sym;
11513 gcc_assert (block);
11514
11515 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
11516 || gfc_state_stack->previous->previous->state != COMP_MODULE)
11517 {
11518 gfc_error ("Derived type declaration with FINAL at %C must be in the"
11519 " specification part of a MODULE");
11520 return MATCH_ERROR;
11521 }
11522
11523 module_ns = gfc_current_ns;
11524 gcc_assert (module_ns);
11525 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
11526
11527 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
11528 if (gfc_match (" ::") == MATCH_ERROR)
11529 return MATCH_ERROR;
11530
11531 /* Match the sequence of procedure names. */
11532 first = true;
11533 last = false;
11534 do
11535 {
11536 gfc_finalizer* f;
11537
11538 if (first && gfc_match_eos () == MATCH_YES)
11539 {
11540 gfc_error ("Empty FINAL at %C");
11541 return MATCH_ERROR;
11542 }
11543
11544 m = gfc_match_name (name);
11545 if (m == MATCH_NO)
11546 {
11547 gfc_error ("Expected module procedure name at %C");
11548 return MATCH_ERROR;
11549 }
11550 else if (m != MATCH_YES)
11551 return MATCH_ERROR;
11552
11553 if (gfc_match_eos () == MATCH_YES)
11554 last = true;
11555 if (!last && gfc_match_char (',') != MATCH_YES)
11556 {
11557 gfc_error ("Expected %<,%> at %C");
11558 return MATCH_ERROR;
11559 }
11560
11561 if (gfc_get_symbol (name, module_ns, &sym))
11562 {
11563 gfc_error ("Unknown procedure name %qs at %C", name);
11564 return MATCH_ERROR;
11565 }
11566
11567 /* Mark the symbol as module procedure. */
11568 if (sym->attr.proc != PROC_MODULE
11569 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
11570 return MATCH_ERROR;
11571
11572 /* Check if we already have this symbol in the list, this is an error. */
11573 for (f = block->f2k_derived->finalizers; f; f = f->next)
11574 if (f->proc_sym == sym)
11575 {
11576 gfc_error ("%qs at %C is already defined as FINAL procedure",
11577 name);
11578 return MATCH_ERROR;
11579 }
11580
11581 /* Add this symbol to the list of finalizers. */
11582 gcc_assert (block->f2k_derived);
11583 sym->refs++;
11584 f = XCNEW (gfc_finalizer);
11585 f->proc_sym = sym;
11586 f->proc_tree = NULL;
11587 f->where = gfc_current_locus;
11588 f->next = block->f2k_derived->finalizers;
11589 block->f2k_derived->finalizers = f;
11590
11591 first = false;
11592 }
11593 while (!last);
11594
11595 return MATCH_YES;
11596}
11597
11598
11599const ext_attr_t ext_attr_list[] = {
11600 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
11601 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
11602 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
11603 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
11604 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
11605 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
11606 { "deprecated", EXT_ATTR_DEPRECATED, NULL },
11607 { NULL, EXT_ATTR_LAST, NULL }
11608};
11609
11610/* Match a !GCC$ ATTRIBUTES statement of the form:
11611 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
11612 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
11613
11614 TODO: We should support all GCC attributes using the same syntax for
11615 the attribute list, i.e. the list in C
11616 __attributes(( attribute-list ))
11617 matches then
11618 !GCC$ ATTRIBUTES attribute-list ::
11619 Cf. c-parser.c's c_parser_attributes; the data can then directly be
11620 saved into a TREE.
11621
11622 As there is absolutely no risk of confusion, we should never return
11623 MATCH_NO. */
11624match
11625gfc_match_gcc_attributes (void)
11626{
11627 symbol_attribute attr;
11628 char name[GFC_MAX_SYMBOL_LEN + 1];
11629 unsigned id;
11630 gfc_symbol *sym;
11631 match m;
11632
11633 gfc_clear_attr (&attr);
11634 for(;;)
11635 {
11636 char ch;
11637
11638 if (gfc_match_name (name) != MATCH_YES)
11639 return MATCH_ERROR;
11640
11641 for (id = 0; id < EXT_ATTR_LAST; id++)
11642 if (strcmp (name, ext_attr_list[id].name) == 0)
11643 break;
11644
11645 if (id == EXT_ATTR_LAST)
11646 {
11647 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
11648 return MATCH_ERROR;
11649 }
11650
11651 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
11652 return MATCH_ERROR;
11653
11654 gfc_gobble_whitespace ();
11655 ch = gfc_next_ascii_char ();
11656 if (ch == ':')
11657 {
11658 /* This is the successful exit condition for the loop. */
11659 if (gfc_next_ascii_char () == ':')
11660 break;
11661 }
11662
11663 if (ch == ',')
11664 continue;
11665
11666 goto syntax;
11667 }
11668
11669 if (gfc_match_eos () == MATCH_YES)
11670 goto syntax;
11671
11672 for(;;)
11673 {
11674 m = gfc_match_name (name);
11675 if (m != MATCH_YES)
11676 return m;
11677
11678 if (find_special (name, &sym, true))
11679 return MATCH_ERROR;
11680
11681 sym->attr.ext_attr |= attr.ext_attr;
11682
11683 if (gfc_match_eos () == MATCH_YES)
11684 break;
11685
11686 if (gfc_match_char (',') != MATCH_YES)
11687 goto syntax;
11688 }
11689
11690 return MATCH_YES;
11691
11692syntax:
11693 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
11694 return MATCH_ERROR;
11695}
11696
11697
11698/* Match a !GCC$ UNROLL statement of the form:
11699 !GCC$ UNROLL n
11700
11701 The parameter n is the number of times we are supposed to unroll.
11702
11703 When we come here, we have already matched the !GCC$ UNROLL string. */
11704match
11705gfc_match_gcc_unroll (void)
11706{
11707 int value;
11708
11709 if (gfc_match_small_int (&value) == MATCH_YES)
11710 {
11711 if (value < 0 || value > USHRT_MAX)
11712 {
11713 gfc_error ("%<GCC unroll%> directive requires a"
11714 " non-negative integral constant"
11715 " less than or equal to %u at %C",
11716 USHRT_MAX
11717 );
11718 return MATCH_ERROR;
11719 }
11720 if (gfc_match_eos () == MATCH_YES)
11721 {
11722 directive_unroll = value == 0 ? 1 : value;
11723 return MATCH_YES;
11724 }
11725 }
11726
11727 gfc_error ("Syntax error in !GCC$ UNROLL directive at %C");
11728 return MATCH_ERROR;
11729}
11730
11731/* Match a !GCC$ builtin (b) attributes simd flags if('target') form:
11732
11733 The parameter b is name of a middle-end built-in.
11734 FLAGS is optional and must be one of:
11735 - (inbranch)
11736 - (notinbranch)
11737
11738 IF('target') is optional and TARGET is a name of a multilib ABI.
11739
11740 When we come here, we have already matched the !GCC$ builtin string. */
11741
11742match
11743gfc_match_gcc_builtin (void)
11744{
11745 char builtin[GFC_MAX_SYMBOL_LEN + 1];
11746 char target[GFC_MAX_SYMBOL_LEN + 1];
11747
11748 if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES)
11749 return MATCH_ERROR;
11750
11751 gfc_simd_clause clause = SIMD_NONE;
11752 if (gfc_match (" ( notinbranch ) ") == MATCH_YES)
11753 clause = SIMD_NOTINBRANCH;
11754 else if (gfc_match (" ( inbranch ) ") == MATCH_YES)
11755 clause = SIMD_INBRANCH;
11756
11757 if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
11758 {
11759 const char *abi = targetm.get_multilib_abi_name ();
11760 if (abi == NULL || strcmp (abi, target) != 0)
11761 return MATCH_YES;
11762 }
11763
11764 if (gfc_vectorized_builtins == NULL)
11765 gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> ();
11766
11767 char *r = XNEWVEC (char, strlen (builtin) + 32);
11768 sprintf (r, "__builtin_%s", builtin);
11769
11770 bool existed;
11771 int &value = gfc_vectorized_builtins->get_or_insert (r, &existed);
11772 value |= clause;
11773 if (existed)
11774 free (r);
11775
11776 return MATCH_YES;
11777}
11778
11779/* Match an !GCC$ IVDEP statement.
11780 When we come here, we have already matched the !GCC$ IVDEP string. */
11781
11782match
11783gfc_match_gcc_ivdep (void)
11784{
11785 if (gfc_match_eos () == MATCH_YES)
11786 {
11787 directive_ivdep = true;
11788 return MATCH_YES;
11789 }
11790
11791 gfc_error ("Syntax error in !GCC$ IVDEP directive at %C");
11792 return MATCH_ERROR;
11793}
11794
11795/* Match an !GCC$ VECTOR statement.
11796 When we come here, we have already matched the !GCC$ VECTOR string. */
11797
11798match
11799gfc_match_gcc_vector (void)
11800{
11801 if (gfc_match_eos () == MATCH_YES)
11802 {
11803 directive_vector = true;
11804 directive_novector = false;
11805 return MATCH_YES;
11806 }
11807
11808 gfc_error ("Syntax error in !GCC$ VECTOR directive at %C");
11809 return MATCH_ERROR;
11810}
11811
11812/* Match an !GCC$ NOVECTOR statement.
11813 When we come here, we have already matched the !GCC$ NOVECTOR string. */
11814
11815match
11816gfc_match_gcc_novector (void)
11817{
11818 if (gfc_match_eos () == MATCH_YES)
11819 {
11820 directive_novector = true;
11821 directive_vector = false;
11822 return MATCH_YES;
11823 }
11824
11825 gfc_error ("Syntax error in !GCC$ NOVECTOR directive at %C");
11826 return MATCH_ERROR;
11827}
This page took 0.106771 seconds and 5 git commands to generate.