1 /* Declaration statement matcher
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stringpool.h"
30 #include "constructor.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector
;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts
;
54 static symbol_attribute current_attr
;
55 static gfc_array_spec
*current_as
;
56 static int colon_seen
;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label
;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line
;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals
= 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr
*last_initializer
;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
78 gfc_expr
*initializer
;
79 struct enumerator_history
*next
;
83 /* Header of enum history chain. */
85 static enumerator_history
*enum_history
= NULL
;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history
*max_enum
= NULL
;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol
*gfc_new_block
;
95 bool gfc_matching_function
;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data
= false;
103 gfc_in_match_data (void)
105 return in_match_data
;
109 set_in_match_data (bool set_value
)
111 in_match_data
= set_value
;
114 /* Free a gfc_data_variable structure and everything beneath it. */
117 free_variable (gfc_data_variable
*p
)
119 gfc_data_variable
*q
;
124 gfc_free_expr (p
->expr
);
125 gfc_free_iterator (&p
->iter
, 0);
126 free_variable (p
->list
);
132 /* Free a gfc_data_value structure and everything beneath it. */
135 free_value (gfc_data_value
*p
)
142 mpz_clear (p
->repeat
);
143 gfc_free_expr (p
->expr
);
149 /* Free a list of gfc_data structures. */
152 gfc_free_data (gfc_data
*p
)
159 free_variable (p
->var
);
160 free_value (p
->value
);
166 /* Free all data in a namespace. */
169 gfc_free_data_all (gfc_namespace
*ns
)
181 /* Reject data parsed since the last restore point was marked. */
184 gfc_reject_data (gfc_namespace
*ns
)
188 while (ns
->data
&& ns
->data
!= ns
->old_data
)
196 static match
var_element (gfc_data_variable
*);
198 /* Match a list of variables terminated by an iterator and a right
202 var_list (gfc_data_variable
*parent
)
204 gfc_data_variable
*tail
, var
;
207 m
= var_element (&var
);
208 if (m
== MATCH_ERROR
)
213 tail
= gfc_get_data_variable ();
220 if (gfc_match_char (',') != MATCH_YES
)
223 m
= gfc_match_iterator (&parent
->iter
, 1);
226 if (m
== MATCH_ERROR
)
229 m
= var_element (&var
);
230 if (m
== MATCH_ERROR
)
235 tail
->next
= gfc_get_data_variable ();
241 if (gfc_match_char (')') != MATCH_YES
)
246 gfc_syntax_error (ST_DATA
);
251 /* Match a single element in a data variable list, which can be a
252 variable-iterator list. */
255 var_element (gfc_data_variable
*new_var
)
260 memset (new_var
, 0, sizeof (gfc_data_variable
));
262 if (gfc_match_char ('(') == MATCH_YES
)
263 return var_list (new_var
);
265 m
= gfc_match_variable (&new_var
->expr
, 0);
269 sym
= new_var
->expr
->symtree
->n
.sym
;
271 /* Symbol should already have an associated type. */
272 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
275 if (!sym
->attr
.function
&& gfc_current_ns
->parent
276 && gfc_current_ns
->parent
== sym
->ns
)
278 gfc_error ("Host associated variable %qs may not be in the DATA "
279 "statement at %C", sym
->name
);
283 if (gfc_current_state () != COMP_BLOCK_DATA
284 && sym
->attr
.in_common
285 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
286 "common block variable %qs in DATA statement at %C",
290 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
297 /* Match the top-level list of data variables. */
300 top_var_list (gfc_data
*d
)
302 gfc_data_variable var
, *tail
, *new_var
;
309 m
= var_element (&var
);
312 if (m
== MATCH_ERROR
)
315 new_var
= gfc_get_data_variable ();
321 tail
->next
= new_var
;
325 if (gfc_match_char ('/') == MATCH_YES
)
327 if (gfc_match_char (',') != MATCH_YES
)
334 gfc_syntax_error (ST_DATA
);
335 gfc_free_data_all (gfc_current_ns
);
341 match_data_constant (gfc_expr
**result
)
343 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
344 gfc_symbol
*sym
, *dt_sym
= NULL
;
349 m
= gfc_match_literal_constant (&expr
, 1);
356 if (m
== MATCH_ERROR
)
359 m
= gfc_match_null (result
);
363 old_loc
= gfc_current_locus
;
365 /* Should this be a structure component, try to match it
366 before matching a name. */
367 m
= gfc_match_rvalue (result
);
368 if (m
== MATCH_ERROR
)
371 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
373 if (!gfc_simplify_expr (*result
, 0))
377 else if (m
== MATCH_YES
)
378 gfc_free_expr (*result
);
380 gfc_current_locus
= old_loc
;
382 m
= gfc_match_name (name
);
386 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
389 if (sym
&& sym
->attr
.generic
)
390 dt_sym
= gfc_find_dt_in_generic (sym
);
393 || (sym
->attr
.flavor
!= FL_PARAMETER
394 && (!dt_sym
|| !gfc_fl_struct (dt_sym
->attr
.flavor
))))
396 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
401 else if (dt_sym
&& gfc_fl_struct (dt_sym
->attr
.flavor
))
402 return gfc_match_structure_constructor (dt_sym
, result
);
404 /* Check to see if the value is an initialization array expression. */
405 if (sym
->value
->expr_type
== EXPR_ARRAY
)
407 gfc_current_locus
= old_loc
;
409 m
= gfc_match_init_expr (result
);
410 if (m
== MATCH_ERROR
)
415 if (!gfc_simplify_expr (*result
, 0))
418 if ((*result
)->expr_type
== EXPR_CONSTANT
)
422 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
428 *result
= gfc_copy_expr (sym
->value
);
433 /* Match a list of values in a DATA statement. The leading '/' has
434 already been seen at this point. */
437 top_val_list (gfc_data
*data
)
439 gfc_data_value
*new_val
, *tail
;
447 m
= match_data_constant (&expr
);
450 if (m
== MATCH_ERROR
)
453 new_val
= gfc_get_data_value ();
454 mpz_init (new_val
->repeat
);
457 data
->value
= new_val
;
459 tail
->next
= new_val
;
463 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
466 mpz_set_ui (tail
->repeat
, 1);
470 mpz_set (tail
->repeat
, expr
->value
.integer
);
471 gfc_free_expr (expr
);
473 m
= match_data_constant (&tail
->expr
);
476 if (m
== MATCH_ERROR
)
480 if (gfc_match_char ('/') == MATCH_YES
)
482 if (gfc_match_char (',') == MATCH_NO
)
489 gfc_syntax_error (ST_DATA
);
490 gfc_free_data_all (gfc_current_ns
);
495 /* Matches an old style initialization. */
498 match_old_style_init (const char *name
)
505 /* Set up data structure to hold initializers. */
506 gfc_find_sym_tree (name
, NULL
, 0, &st
);
509 newdata
= gfc_get_data ();
510 newdata
->var
= gfc_get_data_variable ();
511 newdata
->var
->expr
= gfc_get_variable_expr (st
);
512 newdata
->where
= gfc_current_locus
;
514 /* Match initial value list. This also eats the terminal '/'. */
515 m
= top_val_list (newdata
);
524 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
528 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
530 /* Mark the variable as having appeared in a data statement. */
531 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
537 /* Chain in namespace list of DATA initializers. */
538 newdata
->next
= gfc_current_ns
->data
;
539 gfc_current_ns
->data
= newdata
;
545 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
546 we are matching a DATA statement and are therefore issuing an error
547 if we encounter something unexpected, if not, we're trying to match
548 an old-style initialization expression of the form INTEGER I /2/. */
551 gfc_match_data (void)
556 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
557 if ((gfc_current_state () == COMP_FUNCTION
558 || gfc_current_state () == COMP_SUBROUTINE
)
559 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
561 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
565 set_in_match_data (true);
569 new_data
= gfc_get_data ();
570 new_data
->where
= gfc_current_locus
;
572 m
= top_var_list (new_data
);
576 m
= top_val_list (new_data
);
580 new_data
->next
= gfc_current_ns
->data
;
581 gfc_current_ns
->data
= new_data
;
583 if (gfc_match_eos () == MATCH_YES
)
586 gfc_match_char (','); /* Optional comma */
589 set_in_match_data (false);
593 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
596 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
601 set_in_match_data (false);
602 gfc_free_data (new_data
);
607 /************************ Declaration statements *********************/
610 /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization
611 list). The difference here is the expression is a list of constants
612 and is surrounded by '/'.
613 The typespec ts must match the typespec of the variable which the
614 clist is initializing.
615 The arrayspec tells whether this should match a list of constants
616 corresponding to array elements or a scalar (as == NULL). */
619 match_clist_expr (gfc_expr
**result
, gfc_typespec
*ts
, gfc_array_spec
*as
)
621 gfc_constructor_base array_head
= NULL
;
622 gfc_expr
*expr
= NULL
;
631 mpz_init_set_ui (repeat
, 0);
633 scalar
= !as
|| !as
->rank
;
635 /* We have already matched '/' - now look for a constant list, as with
636 top_val_list from decl.c, but append the result to an array. */
637 if (gfc_match ("/") == MATCH_YES
)
639 gfc_error ("Empty old style initializer list at %C");
643 where
= gfc_current_locus
;
646 m
= match_data_constant (&expr
);
648 expr
= NULL
; /* match_data_constant may set expr to garbage */
651 if (m
== MATCH_ERROR
)
654 /* Found r in repeat spec r*c; look for the constant to repeat. */
655 if ( gfc_match_char ('*') == MATCH_YES
)
659 gfc_error ("Repeat spec invalid in scalar initializer at %C");
662 if (expr
->ts
.type
!= BT_INTEGER
)
664 gfc_error ("Repeat spec must be an integer at %C");
667 mpz_set (repeat
, expr
->value
.integer
);
668 gfc_free_expr (expr
);
671 m
= match_data_constant (&expr
);
673 gfc_error ("Expected data constant after repeat spec at %C");
677 /* No repeat spec, we matched the data constant itself. */
679 mpz_set_ui (repeat
, 1);
683 /* Add the constant initializer as many times as repeated. */
684 for (; mpz_cmp_ui (repeat
, 0) > 0; mpz_sub_ui (repeat
, repeat
, 1))
686 /* Make sure types of elements match */
687 if(ts
&& !gfc_compare_types (&expr
->ts
, ts
)
688 && !gfc_convert_type (expr
, ts
, 1))
691 gfc_constructor_append_expr (&array_head
,
692 gfc_copy_expr (expr
), &gfc_current_locus
);
695 gfc_free_expr (expr
);
699 /* For scalar initializers quit after one element. */
702 if(gfc_match_char ('/') != MATCH_YES
)
704 gfc_error ("End of scalar initializer expected at %C");
710 if (gfc_match_char ('/') == MATCH_YES
)
712 if (gfc_match_char (',') == MATCH_NO
)
716 /* Set up expr as an array constructor. */
719 expr
= gfc_get_array_expr (ts
->type
, ts
->kind
, &where
);
721 expr
->value
.constructor
= array_head
;
723 expr
->rank
= as
->rank
;
724 expr
->shape
= gfc_get_shape (expr
->rank
);
726 /* Validate sizes. */
727 gcc_assert (gfc_array_size (expr
, &size
));
728 gcc_assert (spec_size (as
, &repeat
));
729 cmp
= mpz_cmp (size
, repeat
);
731 gfc_error ("Not enough elements in array initializer at %C");
733 gfc_error ("Too many elements in array initializer at %C");
738 /* Make sure scalar types match. */
739 else if (!gfc_compare_types (&expr
->ts
, ts
)
740 && !gfc_convert_type (expr
, ts
, 1))
744 expr
->ts
.u
.cl
->length_from_typespec
= 1;
752 gfc_error ("Syntax error in old style initializer list at %C");
756 expr
->value
.constructor
= NULL
;
757 gfc_free_expr (expr
);
758 gfc_constructor_free (array_head
);
765 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
768 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
772 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
773 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
775 gfc_error ("The assumed-rank array at %C shall not have a codimension");
779 if (to
->rank
== 0 && from
->rank
> 0)
781 to
->rank
= from
->rank
;
782 to
->type
= from
->type
;
783 to
->cray_pointee
= from
->cray_pointee
;
784 to
->cp_was_assumed
= from
->cp_was_assumed
;
786 for (i
= 0; i
< to
->corank
; i
++)
788 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
789 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
791 for (i
= 0; i
< from
->rank
; i
++)
795 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
796 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
800 to
->lower
[i
] = from
->lower
[i
];
801 to
->upper
[i
] = from
->upper
[i
];
805 else if (to
->corank
== 0 && from
->corank
> 0)
807 to
->corank
= from
->corank
;
808 to
->cotype
= from
->cotype
;
810 for (i
= 0; i
< from
->corank
; i
++)
814 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
815 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
819 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
820 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
829 /* Match an intent specification. Since this can only happen after an
830 INTENT word, a legal intent-spec must follow. */
833 match_intent_spec (void)
836 if (gfc_match (" ( in out )") == MATCH_YES
)
838 if (gfc_match (" ( in )") == MATCH_YES
)
840 if (gfc_match (" ( out )") == MATCH_YES
)
843 gfc_error ("Bad INTENT specification at %C");
844 return INTENT_UNKNOWN
;
848 /* Matches a character length specification, which is either a
849 specification expression, '*', or ':'. */
852 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
859 if (gfc_match_char ('*') == MATCH_YES
)
862 if (gfc_match_char (':') == MATCH_YES
)
864 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type parameter at %C"))
872 m
= gfc_match_expr (expr
);
874 if (m
== MATCH_NO
|| m
== MATCH_ERROR
)
877 if (!gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
880 if ((*expr
)->expr_type
== EXPR_FUNCTION
)
882 if ((*expr
)->ts
.type
== BT_INTEGER
883 || ((*expr
)->ts
.type
== BT_UNKNOWN
884 && strcmp((*expr
)->symtree
->name
, "null") != 0))
889 else if ((*expr
)->expr_type
== EXPR_CONSTANT
)
891 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
892 processor dependent and its value is greater than or equal to zero.
893 F2008, 4.4.3.2: If the character length parameter value evaluates
894 to a negative value, the length of character entities declared
897 if ((*expr
)->ts
.type
== BT_INTEGER
)
899 if (mpz_cmp_si ((*expr
)->value
.integer
, 0) < 0)
900 mpz_set_si ((*expr
)->value
.integer
, 0);
905 else if ((*expr
)->expr_type
== EXPR_ARRAY
)
907 else if ((*expr
)->expr_type
== EXPR_VARIABLE
)
912 e
= gfc_copy_expr (*expr
);
914 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
915 which causes an ICE if gfc_reduce_init_expr() is called. */
916 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
917 && e
->ref
->u
.ar
.type
== AR_UNKNOWN
918 && e
->ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
)
921 t
= gfc_reduce_init_expr (e
);
923 if (!t
&& e
->ts
.type
== BT_UNKNOWN
924 && e
->symtree
->n
.sym
->attr
.untyped
== 1
925 && (flag_implicit_none
926 || e
->symtree
->n
.sym
->ns
->seen_implicit_none
== 1
927 || e
->symtree
->n
.sym
->ns
->parent
->seen_implicit_none
== 1))
933 if ((e
->ref
&& e
->ref
->type
== REF_ARRAY
934 && e
->ref
->u
.ar
.type
!= AR_ELEMENT
)
935 || (!e
->ref
&& e
->expr_type
== EXPR_ARRAY
))
947 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr
)->where
);
952 /* A character length is a '*' followed by a literal integer or a
953 char_len_param_value in parenthesis. */
956 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
962 m
= gfc_match_char ('*');
966 m
= gfc_match_small_literal_int (&length
, NULL
);
967 if (m
== MATCH_ERROR
)
972 if (obsolescent_check
973 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
975 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
979 if (gfc_match_char ('(') == MATCH_NO
)
982 m
= char_len_param_value (expr
, deferred
);
983 if (m
!= MATCH_YES
&& gfc_matching_function
)
989 if (m
== MATCH_ERROR
)
994 if (gfc_match_char (')') == MATCH_NO
)
996 gfc_free_expr (*expr
);
1004 gfc_error ("Syntax error in character length specification at %C");
1009 /* Special subroutine for finding a symbol. Check if the name is found
1010 in the current name space. If not, and we're compiling a function or
1011 subroutine and the parent compilation unit is an interface, then check
1012 to see if the name we've been given is the name of the interface
1013 (located in another namespace). */
1016 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
1022 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
1025 *result
= st
? st
->n
.sym
: NULL
;
1029 if (gfc_current_state () != COMP_SUBROUTINE
1030 && gfc_current_state () != COMP_FUNCTION
)
1033 s
= gfc_state_stack
->previous
;
1037 if (s
->state
!= COMP_INTERFACE
)
1040 goto end
; /* Nameless interface. */
1042 if (strcmp (name
, s
->sym
->name
) == 0)
1053 /* Special subroutine for getting a symbol node associated with a
1054 procedure name, used in SUBROUTINE and FUNCTION statements. The
1055 symbol is created in the parent using with symtree node in the
1056 child unit pointing to the symbol. If the current namespace has no
1057 parent, then the symbol is just created in the current unit. */
1060 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
1066 /* Module functions have to be left in their own namespace because
1067 they have potentially (almost certainly!) already been referenced.
1068 In this sense, they are rather like external functions. This is
1069 fixed up in resolve.c(resolve_entries), where the symbol name-
1070 space is set to point to the master function, so that the fake
1071 result mechanism can work. */
1072 if (module_fcn_entry
)
1074 /* Present if entry is declared to be a module procedure. */
1075 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
1077 if (*result
== NULL
)
1078 rc
= gfc_get_symbol (name
, NULL
, result
);
1079 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
1080 && (*result
)->ts
.type
== BT_UNKNOWN
1081 && sym
->attr
.flavor
== FL_UNKNOWN
)
1082 /* Pick up the typespec for the entry, if declared in the function
1083 body. Note that this symbol is FL_UNKNOWN because it will
1084 only have appeared in a type declaration. The local symtree
1085 is set to point to the module symbol and a unique symtree
1086 to the local version. This latter ensures a correct clearing
1089 /* If the ENTRY proceeds its specification, we need to ensure
1090 that this does not raise a "has no IMPLICIT type" error. */
1091 if (sym
->ts
.type
== BT_UNKNOWN
)
1092 sym
->attr
.untyped
= 1;
1094 (*result
)->ts
= sym
->ts
;
1096 /* Put the symbol in the procedure namespace so that, should
1097 the ENTRY precede its specification, the specification
1099 (*result
)->ns
= gfc_current_ns
;
1101 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
1102 st
->n
.sym
= *result
;
1103 st
= gfc_get_unique_symtree (gfc_current_ns
);
1109 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
1115 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
1118 if (sym
->attr
.module_procedure
1119 && sym
->attr
.if_source
== IFSRC_IFBODY
)
1121 /* Create a partially populated interface symbol to carry the
1122 characteristics of the procedure and the result. */
1123 sym
->tlink
= gfc_new_symbol (name
, sym
->ns
);
1124 gfc_add_type (sym
->tlink
, &(sym
->ts
),
1125 &gfc_current_locus
);
1126 gfc_copy_attr (&sym
->tlink
->attr
, &sym
->attr
, NULL
);
1127 if (sym
->attr
.dimension
)
1128 sym
->tlink
->as
= gfc_copy_array_spec (sym
->as
);
1130 /* Ideally, at this point, a copy would be made of the formal
1131 arguments and their namespace. However, this does not appear
1132 to be necessary, albeit at the expense of not being able to
1133 use gfc_compare_interfaces directly. */
1135 if (sym
->result
&& sym
->result
!= sym
)
1137 sym
->tlink
->result
= sym
->result
;
1140 else if (sym
->result
)
1142 sym
->tlink
->result
= sym
->tlink
;
1145 else if (sym
&& !sym
->gfc_new
1146 && gfc_current_state () != COMP_INTERFACE
)
1148 /* Trap another encompassed procedure with the same name. All
1149 these conditions are necessary to avoid picking up an entry
1150 whose name clashes with that of the encompassing procedure;
1151 this is handled using gsymbols to register unique, globally
1152 accessible names. */
1153 if (sym
->attr
.flavor
!= 0
1154 && sym
->attr
.proc
!= 0
1155 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
1156 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
1157 gfc_error_now ("Procedure %qs at %C is already defined at %L",
1158 name
, &sym
->declared_at
);
1160 /* Trap a procedure with a name the same as interface in the
1161 encompassing scope. */
1162 if (sym
->attr
.generic
!= 0
1163 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
1164 && !sym
->attr
.mod_proc
)
1165 gfc_error_now ("Name %qs at %C is already defined"
1166 " as a generic interface at %L",
1167 name
, &sym
->declared_at
);
1169 /* Trap declarations of attributes in encompassing scope. The
1170 signature for this is that ts.kind is set. Legitimate
1171 references only set ts.type. */
1172 if (sym
->ts
.kind
!= 0
1173 && !sym
->attr
.implicit_type
1174 && sym
->attr
.proc
== 0
1175 && gfc_current_ns
->parent
!= NULL
1176 && sym
->attr
.access
== 0
1177 && !module_fcn_entry
)
1178 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1179 "and must not have attributes declared at %L",
1180 name
, &sym
->declared_at
);
1183 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
1186 /* Module function entries will already have a symtree in
1187 the current namespace but will need one at module level. */
1188 if (module_fcn_entry
)
1190 /* Present if entry is declared to be a module procedure. */
1191 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
1193 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
1196 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
1201 /* See if the procedure should be a module procedure. */
1203 if (((sym
->ns
->proc_name
!= NULL
1204 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1205 && sym
->attr
.proc
!= PROC_MODULE
)
1206 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
1207 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
1214 /* Verify that the given symbol representing a parameter is C
1215 interoperable, by checking to see if it was marked as such after
1216 its declaration. If the given symbol is not interoperable, a
1217 warning is reported, thus removing the need to return the status to
1218 the calling function. The standard does not require the user use
1219 one of the iso_c_binding named constants to declare an
1220 interoperable parameter, but we can't be sure if the param is C
1221 interop or not if the user doesn't. For example, integer(4) may be
1222 legal Fortran, but doesn't have meaning in C. It may interop with
1223 a number of the C types, which causes a problem because the
1224 compiler can't know which one. This code is almost certainly not
1225 portable, and the user will get what they deserve if the C type
1226 across platforms isn't always interoperable with integer(4). If
1227 the user had used something like integer(c_int) or integer(c_long),
1228 the compiler could have automatically handled the varying sizes
1229 across platforms. */
1232 gfc_verify_c_interop_param (gfc_symbol
*sym
)
1234 int is_c_interop
= 0;
1237 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1238 Don't repeat the checks here. */
1239 if (sym
->attr
.implicit_type
)
1242 /* For subroutines or functions that are passed to a BIND(C) procedure,
1243 they're interoperable if they're BIND(C) and their params are all
1245 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1247 if (sym
->attr
.is_bind_c
== 0)
1249 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1250 "attribute to be C interoperable", sym
->name
,
1251 &(sym
->declared_at
));
1256 if (sym
->attr
.is_c_interop
== 1)
1257 /* We've already checked this procedure; don't check it again. */
1260 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1265 /* See if we've stored a reference to a procedure that owns sym. */
1266 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1268 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1270 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1272 if (is_c_interop
!= 1)
1274 /* Make personalized messages to give better feedback. */
1275 if (sym
->ts
.type
== BT_DERIVED
)
1276 gfc_error ("Variable %qs at %L is a dummy argument to the "
1277 "BIND(C) procedure %qs but is not C interoperable "
1278 "because derived type %qs is not C interoperable",
1279 sym
->name
, &(sym
->declared_at
),
1280 sym
->ns
->proc_name
->name
,
1281 sym
->ts
.u
.derived
->name
);
1282 else if (sym
->ts
.type
== BT_CLASS
)
1283 gfc_error ("Variable %qs at %L is a dummy argument to the "
1284 "BIND(C) procedure %qs but is not C interoperable "
1285 "because it is polymorphic",
1286 sym
->name
, &(sym
->declared_at
),
1287 sym
->ns
->proc_name
->name
);
1288 else if (warn_c_binding_type
)
1289 gfc_warning (OPT_Wc_binding_type
,
1290 "Variable %qs at %L is a dummy argument of the "
1291 "BIND(C) procedure %qs but may not be C "
1293 sym
->name
, &(sym
->declared_at
),
1294 sym
->ns
->proc_name
->name
);
1297 /* Character strings are only C interoperable if they have a
1299 if (sym
->ts
.type
== BT_CHARACTER
)
1301 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1302 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1303 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1305 gfc_error ("Character argument %qs at %L "
1306 "must be length 1 because "
1307 "procedure %qs is BIND(C)",
1308 sym
->name
, &sym
->declared_at
,
1309 sym
->ns
->proc_name
->name
);
1314 /* We have to make sure that any param to a bind(c) routine does
1315 not have the allocatable, pointer, or optional attributes,
1316 according to J3/04-007, section 5.1. */
1317 if (sym
->attr
.allocatable
== 1
1318 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1319 "ALLOCATABLE attribute in procedure %qs "
1320 "with BIND(C)", sym
->name
,
1321 &(sym
->declared_at
),
1322 sym
->ns
->proc_name
->name
))
1325 if (sym
->attr
.pointer
== 1
1326 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1327 "POINTER attribute in procedure %qs "
1328 "with BIND(C)", sym
->name
,
1329 &(sym
->declared_at
),
1330 sym
->ns
->proc_name
->name
))
1333 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1335 gfc_error ("Scalar variable %qs at %L with POINTER or "
1336 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1337 " supported", sym
->name
, &(sym
->declared_at
),
1338 sym
->ns
->proc_name
->name
);
1342 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1344 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1345 "and the VALUE attribute because procedure %qs "
1346 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1347 sym
->ns
->proc_name
->name
);
1350 else if (sym
->attr
.optional
== 1
1351 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs "
1352 "at %L with OPTIONAL attribute in "
1353 "procedure %qs which is BIND(C)",
1354 sym
->name
, &(sym
->declared_at
),
1355 sym
->ns
->proc_name
->name
))
1358 /* Make sure that if it has the dimension attribute, that it is
1359 either assumed size or explicit shape. Deferred shape is already
1360 covered by the pointer/allocatable attribute. */
1361 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1362 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array %qs "
1363 "at %L as dummy argument to the BIND(C) "
1364 "procedure %qs at %L", sym
->name
,
1365 &(sym
->declared_at
),
1366 sym
->ns
->proc_name
->name
,
1367 &(sym
->ns
->proc_name
->declared_at
)))
1377 /* Function called by variable_decl() that adds a name to the symbol table. */
1380 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1381 gfc_array_spec
**as
, locus
*var_locus
)
1383 symbol_attribute attr
;
1387 if (gfc_get_symbol (name
, NULL
, &sym
))
1390 /* Check if the name has already been defined as a type. The
1391 first letter of the symtree will be in upper case then. Of
1392 course, this is only necessary if the upper case letter is
1393 actually different. */
1395 upper
= TOUPPER(name
[0]);
1396 if (upper
!= name
[0])
1398 char u_name
[GFC_MAX_SYMBOL_LEN
+ 1];
1402 nlen
= strlen(name
);
1403 gcc_assert (nlen
<= GFC_MAX_SYMBOL_LEN
);
1404 strncpy (u_name
, name
, nlen
+ 1);
1407 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, u_name
);
1409 /* STRUCTURE types can alias symbol names */
1410 if (st
!= 0 && st
->n
.sym
->attr
.flavor
!= FL_STRUCT
)
1412 gfc_error ("Symbol %qs at %C also declared as a type at %L", name
,
1413 &st
->n
.sym
->declared_at
);
1418 /* Start updating the symbol table. Add basic type attribute if present. */
1419 if (current_ts
.type
!= BT_UNKNOWN
1420 && (sym
->attr
.implicit_type
== 0
1421 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1422 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1425 if (sym
->ts
.type
== BT_CHARACTER
)
1428 sym
->ts
.deferred
= cl_deferred
;
1431 /* Add dimension attribute if present. */
1432 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1436 /* Add attribute to symbol. The copy is so that we can reset the
1437 dimension attribute. */
1438 attr
= current_attr
;
1440 attr
.codimension
= 0;
1442 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1445 /* Finish any work that may need to be done for the binding label,
1446 if it's a bind(c). The bind(c) attr is found before the symbol
1447 is made, and before the symbol name (for data decls), so the
1448 current_ts is holding the binding label, or nothing if the
1449 name= attr wasn't given. Therefore, test here if we're dealing
1450 with a bind(c) and make sure the binding label is set correctly. */
1451 if (sym
->attr
.is_bind_c
== 1)
1453 if (!sym
->binding_label
)
1455 /* Set the binding label and verify that if a NAME= was specified
1456 then only one identifier was in the entity-decl-list. */
1457 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1458 num_idents_on_line
))
1463 /* See if we know we're in a common block, and if it's a bind(c)
1464 common then we need to make sure we're an interoperable type. */
1465 if (sym
->attr
.in_common
== 1)
1467 /* Test the common block object. */
1468 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1469 && sym
->ts
.is_c_interop
!= 1)
1471 gfc_error_now ("Variable %qs in common block %qs at %C "
1472 "must be declared with a C interoperable "
1473 "kind since common block %qs is BIND(C)",
1474 sym
->name
, sym
->common_block
->name
,
1475 sym
->common_block
->name
);
1480 sym
->attr
.implied_index
= 0;
1482 if (sym
->ts
.type
== BT_CLASS
)
1483 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1489 /* Set character constant to the given length. The constant will be padded or
1490 truncated. If we're inside an array constructor without a typespec, we
1491 additionally check that all elements have the same length; check_len -1
1492 means no checking. */
1495 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1500 if (expr
->ts
.type
!= BT_CHARACTER
)
1503 if (expr
->expr_type
!= EXPR_CONSTANT
)
1505 gfc_error_now ("CHARACTER length must be a constant at %L", &expr
->where
);
1509 slen
= expr
->value
.character
.length
;
1512 s
= gfc_get_wide_string (len
+ 1);
1513 memcpy (s
, expr
->value
.character
.string
,
1514 MIN (len
, slen
) * sizeof (gfc_char_t
));
1516 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1518 if (warn_character_truncation
&& slen
> len
)
1519 gfc_warning_now (OPT_Wcharacter_truncation
,
1520 "CHARACTER expression at %L is being truncated "
1521 "(%d/%d)", &expr
->where
, slen
, len
);
1523 /* Apply the standard by 'hand' otherwise it gets cleared for
1525 if (check_len
!= -1 && slen
!= check_len
1526 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1527 gfc_error_now ("The CHARACTER elements of the array constructor "
1528 "at %L must have the same length (%d/%d)",
1529 &expr
->where
, slen
, check_len
);
1532 free (expr
->value
.character
.string
);
1533 expr
->value
.character
.string
= s
;
1534 expr
->value
.character
.length
= len
;
1539 /* Function to create and update the enumerator history
1540 using the information passed as arguments.
1541 Pointer "max_enum" is also updated, to point to
1542 enum history node containing largest initializer.
1544 SYM points to the symbol node of enumerator.
1545 INIT points to its enumerator value. */
1548 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1550 enumerator_history
*new_enum_history
;
1551 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1553 new_enum_history
= XCNEW (enumerator_history
);
1555 new_enum_history
->sym
= sym
;
1556 new_enum_history
->initializer
= init
;
1557 new_enum_history
->next
= NULL
;
1559 if (enum_history
== NULL
)
1561 enum_history
= new_enum_history
;
1562 max_enum
= enum_history
;
1566 new_enum_history
->next
= enum_history
;
1567 enum_history
= new_enum_history
;
1569 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1570 new_enum_history
->initializer
->value
.integer
) < 0)
1571 max_enum
= new_enum_history
;
1576 /* Function to free enum kind history. */
1579 gfc_free_enum_history (void)
1581 enumerator_history
*current
= enum_history
;
1582 enumerator_history
*next
;
1584 while (current
!= NULL
)
1586 next
= current
->next
;
1591 enum_history
= NULL
;
1595 /* Function called by variable_decl() that adds an initialization
1596 expression to a symbol. */
1599 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1601 symbol_attribute attr
;
1606 if (find_special (name
, &sym
, false))
1611 /* If this symbol is confirming an implicit parameter type,
1612 then an initialization expression is not allowed. */
1613 if (attr
.flavor
== FL_PARAMETER
1614 && sym
->value
!= NULL
1617 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1624 /* An initializer is required for PARAMETER declarations. */
1625 if (attr
.flavor
== FL_PARAMETER
)
1627 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1633 /* If a variable appears in a DATA block, it cannot have an
1637 gfc_error ("Variable %qs at %C with an initializer already "
1638 "appears in a DATA statement", sym
->name
);
1642 /* Check if the assignment can happen. This has to be put off
1643 until later for derived type variables and procedure pointers. */
1644 if (!gfc_bt_struct (sym
->ts
.type
) && !gfc_bt_struct (init
->ts
.type
)
1645 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1646 && !sym
->attr
.proc_pointer
1647 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1650 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1651 && init
->ts
.type
== BT_CHARACTER
)
1653 /* Update symbol character length according initializer. */
1654 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1657 if (sym
->ts
.u
.cl
->length
== NULL
)
1660 /* If there are multiple CHARACTER variables declared on the
1661 same line, we don't want them to share the same length. */
1662 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1664 if (sym
->attr
.flavor
== FL_PARAMETER
)
1666 if (init
->expr_type
== EXPR_CONSTANT
)
1668 clen
= init
->value
.character
.length
;
1669 sym
->ts
.u
.cl
->length
1670 = gfc_get_int_expr (gfc_default_integer_kind
,
1673 else if (init
->expr_type
== EXPR_ARRAY
)
1677 const gfc_expr
*length
= init
->ts
.u
.cl
->length
;
1678 if (length
->expr_type
!= EXPR_CONSTANT
)
1680 gfc_error ("Cannot initialize parameter array "
1682 "with variable length elements",
1686 clen
= mpz_get_si (length
->value
.integer
);
1688 else if (init
->value
.constructor
)
1691 c
= gfc_constructor_first (init
->value
.constructor
);
1692 clen
= c
->expr
->value
.character
.length
;
1696 sym
->ts
.u
.cl
->length
1697 = gfc_get_int_expr (gfc_default_integer_kind
,
1700 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1701 sym
->ts
.u
.cl
->length
=
1702 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1705 /* Update initializer character length according symbol. */
1706 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1710 if (!gfc_specification_expr (sym
->ts
.u
.cl
->length
))
1713 len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1715 if (init
->expr_type
== EXPR_CONSTANT
)
1716 gfc_set_constant_character_len (len
, init
, -1);
1717 else if (init
->expr_type
== EXPR_ARRAY
)
1721 /* Build a new charlen to prevent simplification from
1722 deleting the length before it is resolved. */
1723 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1724 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1726 for (c
= gfc_constructor_first (init
->value
.constructor
);
1727 c
; c
= gfc_constructor_next (c
))
1728 gfc_set_constant_character_len (len
, c
->expr
, -1);
1733 /* If sym is implied-shape, set its upper bounds from init. */
1734 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1735 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1739 if (init
->rank
== 0)
1741 gfc_error ("Can't initialize implied-shape array at %L"
1742 " with scalar", &sym
->declared_at
);
1746 /* Shape should be present, we get an initialization expression. */
1747 gcc_assert (init
->shape
);
1749 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1752 gfc_expr
*e
, *lower
;
1754 lower
= sym
->as
->lower
[dim
];
1756 /* If the lower bound is an array element from another
1757 parameterized array, then it is marked with EXPR_VARIABLE and
1758 is an initialization expression. Try to reduce it. */
1759 if (lower
->expr_type
== EXPR_VARIABLE
)
1760 gfc_reduce_init_expr (lower
);
1762 if (lower
->expr_type
== EXPR_CONSTANT
)
1764 /* All dimensions must be without upper bound. */
1765 gcc_assert (!sym
->as
->upper
[dim
]);
1768 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1769 mpz_add (e
->value
.integer
, lower
->value
.integer
,
1771 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1772 sym
->as
->upper
[dim
] = e
;
1776 gfc_error ("Non-constant lower bound in implied-shape"
1777 " declaration at %L", &lower
->where
);
1782 sym
->as
->type
= AS_EXPLICIT
;
1785 /* Need to check if the expression we initialized this
1786 to was one of the iso_c_binding named constants. If so,
1787 and we're a parameter (constant), let it be iso_c.
1789 integer(c_int), parameter :: my_int = c_int
1790 integer(my_int) :: my_int_2
1791 If we mark my_int as iso_c (since we can see it's value
1792 is equal to one of the named constants), then my_int_2
1793 will be considered C interoperable. */
1794 if (sym
->ts
.type
!= BT_CHARACTER
&& !gfc_bt_struct (sym
->ts
.type
))
1796 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1797 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1798 /* attr bits needed for module files. */
1799 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1800 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1801 if (init
->ts
.is_iso_c
)
1802 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1805 /* Add initializer. Make sure we keep the ranks sane. */
1806 if (sym
->attr
.dimension
&& init
->rank
== 0)
1811 if (sym
->attr
.flavor
== FL_PARAMETER
1812 && init
->expr_type
== EXPR_CONSTANT
1813 && spec_size (sym
->as
, &size
)
1814 && mpz_cmp_si (size
, 0) > 0)
1816 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1818 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1819 gfc_constructor_append_expr (&array
->value
.constructor
,
1822 : gfc_copy_expr (init
),
1825 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1826 for (n
= 0; n
< sym
->as
->rank
; n
++)
1827 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1832 init
->rank
= sym
->as
->rank
;
1836 if (sym
->attr
.save
== SAVE_NONE
)
1837 sym
->attr
.save
= SAVE_IMPLICIT
;
1845 /* Function called by variable_decl() that adds a name to a structure
1849 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1850 gfc_array_spec
**as
)
1855 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1856 constructing, it must have the pointer attribute. */
1857 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1858 && current_ts
.u
.derived
== gfc_current_block ()
1859 && current_attr
.pointer
== 0)
1861 if (current_attr
.allocatable
1862 && !gfc_notify_std(GFC_STD_F2008
, "Component at %C "
1863 "must have the POINTER attribute"))
1867 else if (current_attr
.allocatable
== 0)
1869 gfc_error ("Component at %C must have the POINTER attribute");
1875 if (current_ts
.type
== BT_CLASS
1876 && !(current_attr
.pointer
|| current_attr
.allocatable
))
1878 gfc_error ("Component %qs with CLASS at %C must be allocatable "
1879 "or pointer", name
);
1883 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1885 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1887 gfc_error ("Array component of structure at %C must have explicit "
1888 "or deferred shape");
1893 /* If we are in a nested union/map definition, gfc_add_component will not
1894 properly find repeated components because:
1895 (i) gfc_add_component does a flat search, where components of unions
1896 and maps are implicity chained so nested components may conflict.
1897 (ii) Unions and maps are not linked as components of their parent
1898 structures until after they are parsed.
1899 For (i) we use gfc_find_component which searches recursively, and for (ii)
1900 we search each block directly from the parse stack until we find the top
1903 s
= gfc_state_stack
;
1904 if (s
->state
== COMP_UNION
|| s
->state
== COMP_MAP
)
1906 while (s
->state
== COMP_UNION
|| gfc_comp_struct (s
->state
))
1908 c
= gfc_find_component (s
->sym
, name
, true, true, NULL
);
1911 gfc_error_now ("Component '%s' at %C already declared at %L",
1915 /* Break after we've searched the entire chain. */
1916 if (s
->state
== COMP_DERIVED
|| s
->state
== COMP_STRUCTURE
)
1922 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1926 if (c
->ts
.type
== BT_CHARACTER
)
1928 c
->attr
= current_attr
;
1930 c
->initializer
= *init
;
1937 c
->attr
.codimension
= 1;
1939 c
->attr
.dimension
= 1;
1943 gfc_apply_init (&c
->ts
, &c
->attr
, c
->initializer
);
1945 /* Check array components. */
1946 if (!c
->attr
.dimension
)
1949 if (c
->attr
.pointer
)
1951 if (c
->as
->type
!= AS_DEFERRED
)
1953 gfc_error ("Pointer array component of structure at %C must have a "
1958 else if (c
->attr
.allocatable
)
1960 if (c
->as
->type
!= AS_DEFERRED
)
1962 gfc_error ("Allocatable component of structure at %C must have a "
1969 if (c
->as
->type
!= AS_EXPLICIT
)
1971 gfc_error ("Array component of structure at %C must have an "
1978 if (c
->ts
.type
== BT_CLASS
)
1979 return gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1985 /* Match a 'NULL()', and possibly take care of some side effects. */
1988 gfc_match_null (gfc_expr
**result
)
1991 match m
, m2
= MATCH_NO
;
1993 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1999 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2001 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
2004 old_loc
= gfc_current_locus
;
2005 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
2008 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
2012 gfc_current_locus
= old_loc
;
2017 /* The NULL symbol now has to be/become an intrinsic function. */
2018 if (gfc_get_symbol ("null", NULL
, &sym
))
2020 gfc_error ("NULL() initialization at %C is ambiguous");
2024 gfc_intrinsic_symbol (sym
);
2026 if (sym
->attr
.proc
!= PROC_INTRINSIC
2027 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
2028 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
2029 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
2032 *result
= gfc_get_null_expr (&gfc_current_locus
);
2034 /* Invalid per F2008, C512. */
2035 if (m2
== MATCH_YES
)
2037 gfc_error ("NULL() initialization at %C may not have MOLD");
2045 /* Match the initialization expr for a data pointer or procedure pointer. */
2048 match_pointer_init (gfc_expr
**init
, int procptr
)
2052 if (gfc_pure (NULL
) && !gfc_comp_struct (gfc_state_stack
->state
))
2054 gfc_error ("Initialization of pointer at %C is not allowed in "
2055 "a PURE procedure");
2058 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2060 /* Match NULL() initialization. */
2061 m
= gfc_match_null (init
);
2065 /* Match non-NULL initialization. */
2066 gfc_matching_ptr_assignment
= !procptr
;
2067 gfc_matching_procptr_assignment
= procptr
;
2068 m
= gfc_match_rvalue (init
);
2069 gfc_matching_ptr_assignment
= 0;
2070 gfc_matching_procptr_assignment
= 0;
2071 if (m
== MATCH_ERROR
)
2073 else if (m
== MATCH_NO
)
2075 gfc_error ("Error in pointer initialization at %C");
2079 if (!procptr
&& !gfc_resolve_expr (*init
))
2082 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
2083 "initialization at %C"))
2091 check_function_name (char *name
)
2093 /* In functions that have a RESULT variable defined, the function name always
2094 refers to function calls. Therefore, the name is not allowed to appear in
2095 specification statements. When checking this, be careful about
2096 'hidden' procedure pointer results ('ppr@'). */
2098 if (gfc_current_state () == COMP_FUNCTION
)
2100 gfc_symbol
*block
= gfc_current_block ();
2101 if (block
&& block
->result
&& block
->result
!= block
2102 && strcmp (block
->result
->name
, "ppr@") != 0
2103 && strcmp (block
->name
, name
) == 0)
2105 gfc_error ("Function name %qs not allowed at %C", name
);
2114 /* Match a variable name with an optional initializer. When this
2115 subroutine is called, a variable is expected to be parsed next.
2116 Depending on what is happening at the moment, updates either the
2117 symbol table or the current interface. */
2120 variable_decl (int elem
)
2122 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2123 gfc_expr
*initializer
, *char_len
;
2125 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
2137 /* When we get here, we've just matched a list of attributes and
2138 maybe a type and a double colon. The next thing we expect to see
2139 is the name of the symbol. */
2140 m
= gfc_match_name (name
);
2144 var_locus
= gfc_current_locus
;
2146 /* Now we could see the optional array spec. or character length. */
2147 m
= gfc_match_array_spec (&as
, true, true);
2148 if (m
== MATCH_ERROR
)
2152 as
= gfc_copy_array_spec (current_as
);
2154 && !merge_array_spec (current_as
, as
, true))
2160 if (flag_cray_pointer
)
2161 cp_as
= gfc_copy_array_spec (as
);
2163 /* At this point, we know for sure if the symbol is PARAMETER and can thus
2164 determine (and check) whether it can be implied-shape. If it
2165 was parsed as assumed-size, change it because PARAMETERs can not
2169 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
2172 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
2177 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
2178 && current_attr
.flavor
== FL_PARAMETER
)
2179 as
->type
= AS_IMPLIED_SHAPE
;
2181 if (as
->type
== AS_IMPLIED_SHAPE
2182 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
2192 cl_deferred
= false;
2194 if (current_ts
.type
== BT_CHARACTER
)
2196 switch (match_char_length (&char_len
, &cl_deferred
, false))
2199 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2201 cl
->length
= char_len
;
2204 /* Non-constant lengths need to be copied after the first
2205 element. Also copy assumed lengths. */
2208 && (current_ts
.u
.cl
->length
== NULL
2209 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
2211 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2212 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
2215 cl
= current_ts
.u
.cl
;
2217 cl_deferred
= current_ts
.deferred
;
2226 /* The dummy arguments and result of the abreviated form of MODULE
2227 PROCEDUREs, used in SUBMODULES should not be redefined. */
2228 if (gfc_current_ns
->proc_name
2229 && gfc_current_ns
->proc_name
->abr_modproc_decl
)
2231 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
2232 if (sym
!= NULL
&& (sym
->attr
.dummy
|| sym
->attr
.result
))
2235 gfc_error ("%qs at %C is a redefinition of the declaration "
2236 "in the corresponding interface for MODULE "
2237 "PROCEDURE %qs", sym
->name
,
2238 gfc_current_ns
->proc_name
->name
);
2243 /* If this symbol has already shown up in a Cray Pointer declaration,
2244 and this is not a component declaration,
2245 then we want to set the type & bail out. */
2246 if (flag_cray_pointer
&& !gfc_comp_struct (gfc_current_state ()))
2248 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
2249 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
2251 sym
->ts
.type
= current_ts
.type
;
2252 sym
->ts
.kind
= current_ts
.kind
;
2254 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
2255 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
2256 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
2259 /* Check to see if we have an array specification. */
2262 if (sym
->as
!= NULL
)
2264 gfc_error ("Duplicate array spec for Cray pointee at %C");
2265 gfc_free_array_spec (cp_as
);
2271 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
2272 gfc_internal_error ("Couldn't set pointee array spec.");
2274 /* Fix the array spec. */
2275 m
= gfc_mod_pointee_as (sym
->as
);
2276 if (m
== MATCH_ERROR
)
2284 gfc_free_array_spec (cp_as
);
2288 /* Procedure pointer as function result. */
2289 if (gfc_current_state () == COMP_FUNCTION
2290 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
2291 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
2292 strcpy (name
, "ppr@");
2294 if (gfc_current_state () == COMP_FUNCTION
2295 && strcmp (name
, gfc_current_block ()->name
) == 0
2296 && gfc_current_block ()->result
2297 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
2298 strcpy (name
, "ppr@");
2300 /* OK, we've successfully matched the declaration. Now put the
2301 symbol in the current namespace, because it might be used in the
2302 optional initialization expression for this symbol, e.g. this is
2305 integer, parameter :: i = huge(i)
2307 This is only true for parameters or variables of a basic type.
2308 For components of derived types, it is not true, so we don't
2309 create a symbol for those yet. If we fail to create the symbol,
2311 if (!gfc_comp_struct (gfc_current_state ())
2312 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
2318 if (!check_function_name (name
))
2324 /* We allow old-style initializations of the form
2325 integer i /2/, j(4) /3*3, 1/
2326 (if no colon has been seen). These are different from data
2327 statements in that initializers are only allowed to apply to the
2328 variable immediately preceding, i.e.
2330 is not allowed. Therefore we have to do some work manually, that
2331 could otherwise be left to the matchers for DATA statements. */
2333 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2335 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2336 "initialization at %C"))
2339 /* Allow old style initializations for components of STRUCTUREs and MAPs
2340 but not components of derived types. */
2341 else if (gfc_current_state () == COMP_DERIVED
)
2343 gfc_error ("Invalid old style initialization for derived type "
2349 /* For structure components, read the initializer as a special
2350 expression and let the rest of this function apply the initializer
2352 else if (gfc_comp_struct (gfc_current_state ()))
2354 m
= match_clist_expr (&initializer
, ¤t_ts
, as
);
2356 gfc_error ("Syntax error in old style initialization of %s at %C",
2362 /* Otherwise we treat the old style initialization just like a
2363 DATA declaration for the current variable. */
2365 return match_old_style_init (name
);
2368 /* The double colon must be present in order to have initializers.
2369 Otherwise the statement is ambiguous with an assignment statement. */
2372 if (gfc_match (" =>") == MATCH_YES
)
2374 if (!current_attr
.pointer
)
2376 gfc_error ("Initialization at %C isn't for a pointer variable");
2381 m
= match_pointer_init (&initializer
, 0);
2385 else if (gfc_match_char ('=') == MATCH_YES
)
2387 if (current_attr
.pointer
)
2389 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2395 m
= gfc_match_init_expr (&initializer
);
2398 gfc_error ("Expected an initialization expression at %C");
2402 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2403 && !gfc_comp_struct (gfc_state_stack
->state
))
2405 gfc_error ("Initialization of variable at %C is not allowed in "
2406 "a PURE procedure");
2410 if (current_attr
.flavor
!= FL_PARAMETER
2411 && !gfc_comp_struct (gfc_state_stack
->state
))
2412 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2419 if (initializer
!= NULL
&& current_attr
.allocatable
2420 && gfc_comp_struct (gfc_current_state ()))
2422 gfc_error ("Initialization of allocatable component at %C is not "
2428 /* Add the initializer. Note that it is fine if initializer is
2429 NULL here, because we sometimes also need to check if a
2430 declaration *must* have an initialization expression. */
2431 if (!gfc_comp_struct (gfc_current_state ()))
2432 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2435 if (current_ts
.type
== BT_DERIVED
2436 && !current_attr
.pointer
&& !initializer
)
2437 initializer
= gfc_default_initializer (¤t_ts
);
2438 t
= build_struct (name
, cl
, &initializer
, &as
);
2440 /* If we match a nested structure definition we expect to see the
2441 * body even if the variable declarations blow up, so we need to keep
2442 * the structure declaration around. */
2443 if (gfc_new_block
&& gfc_new_block
->attr
.flavor
== FL_STRUCT
)
2444 gfc_commit_symbol (gfc_new_block
);
2447 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2450 /* Free stuff up and return. */
2451 gfc_free_expr (initializer
);
2452 gfc_free_array_spec (as
);
2458 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2459 This assumes that the byte size is equal to the kind number for
2460 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2463 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2468 if (gfc_match_char ('*') != MATCH_YES
)
2471 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2475 original_kind
= ts
->kind
;
2477 /* Massage the kind numbers for complex types. */
2478 if (ts
->type
== BT_COMPLEX
)
2482 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2483 gfc_basic_typename (ts
->type
), original_kind
);
2490 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2493 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2497 if (flag_real4_kind
== 8)
2499 if (flag_real4_kind
== 10)
2501 if (flag_real4_kind
== 16)
2507 if (flag_real8_kind
== 4)
2509 if (flag_real8_kind
== 10)
2511 if (flag_real8_kind
== 16)
2516 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2518 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2519 gfc_basic_typename (ts
->type
), original_kind
);
2523 if (!gfc_notify_std (GFC_STD_GNU
,
2524 "Nonstandard type declaration %s*%d at %C",
2525 gfc_basic_typename(ts
->type
), original_kind
))
2532 /* Match a kind specification. Since kinds are generally optional, we
2533 usually return MATCH_NO if something goes wrong. If a "kind="
2534 string is found, then we know we have an error. */
2537 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2548 where
= loc
= gfc_current_locus
;
2553 if (gfc_match_char ('(') == MATCH_NO
)
2556 /* Also gobbles optional text. */
2557 if (gfc_match (" kind = ") == MATCH_YES
)
2560 loc
= gfc_current_locus
;
2563 n
= gfc_match_init_expr (&e
);
2567 if (gfc_matching_function
)
2569 /* The function kind expression might include use associated or
2570 imported parameters and try again after the specification
2572 if (gfc_match_char (')') != MATCH_YES
)
2574 gfc_error ("Missing right parenthesis at %C");
2580 gfc_undo_symbols ();
2585 /* ....or else, the match is real. */
2587 gfc_error ("Expected initialization expression at %C");
2595 gfc_error ("Expected scalar initialization expression at %C");
2600 if (gfc_extract_int (e
, &ts
->kind
, 1))
2606 /* Before throwing away the expression, let's see if we had a
2607 C interoperable kind (and store the fact). */
2608 if (e
->ts
.is_c_interop
== 1)
2610 /* Mark this as C interoperable if being declared with one
2611 of the named constants from iso_c_binding. */
2612 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2613 ts
->f90_type
= e
->ts
.f90_type
;
2619 /* Ignore errors to this point, if we've gotten here. This means
2620 we ignore the m=MATCH_ERROR from above. */
2621 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2623 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2624 gfc_basic_typename (ts
->type
));
2625 gfc_current_locus
= where
;
2629 /* Warn if, e.g., c_int is used for a REAL variable, but not
2630 if, e.g., c_double is used for COMPLEX as the standard
2631 explicitly says that the kind type parameter for complex and real
2632 variable is the same, i.e. c_float == c_float_complex. */
2633 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2634 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2635 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2636 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2637 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2638 gfc_basic_typename (ts
->type
));
2640 gfc_gobble_whitespace ();
2641 if ((c
= gfc_next_ascii_char ()) != ')'
2642 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2644 if (ts
->type
== BT_CHARACTER
)
2645 gfc_error ("Missing right parenthesis or comma at %C");
2647 gfc_error ("Missing right parenthesis at %C");
2651 /* All tests passed. */
2654 if(m
== MATCH_ERROR
)
2655 gfc_current_locus
= where
;
2657 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2660 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2664 if (flag_real4_kind
== 8)
2666 if (flag_real4_kind
== 10)
2668 if (flag_real4_kind
== 16)
2674 if (flag_real8_kind
== 4)
2676 if (flag_real8_kind
== 10)
2678 if (flag_real8_kind
== 16)
2683 /* Return what we know from the test(s). */
2688 gfc_current_locus
= where
;
2694 match_char_kind (int * kind
, int * is_iso_c
)
2703 where
= gfc_current_locus
;
2705 n
= gfc_match_init_expr (&e
);
2707 if (n
!= MATCH_YES
&& gfc_matching_function
)
2709 /* The expression might include use-associated or imported
2710 parameters and try again after the specification
2713 gfc_undo_symbols ();
2718 gfc_error ("Expected initialization expression at %C");
2724 gfc_error ("Expected scalar initialization expression at %C");
2729 fail
= gfc_extract_int (e
, kind
, 1);
2730 *is_iso_c
= e
->ts
.is_iso_c
;
2739 /* Ignore errors to this point, if we've gotten here. This means
2740 we ignore the m=MATCH_ERROR from above. */
2741 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2743 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2747 /* All tests passed. */
2750 if (m
== MATCH_ERROR
)
2751 gfc_current_locus
= where
;
2753 /* Return what we know from the test(s). */
2758 gfc_current_locus
= where
;
2763 /* Match the various kind/length specifications in a CHARACTER
2764 declaration. We don't return MATCH_NO. */
2767 gfc_match_char_spec (gfc_typespec
*ts
)
2769 int kind
, seen_length
, is_iso_c
;
2781 /* Try the old-style specification first. */
2782 old_char_selector
= 0;
2784 m
= match_char_length (&len
, &deferred
, true);
2788 old_char_selector
= 1;
2793 m
= gfc_match_char ('(');
2796 m
= MATCH_YES
; /* Character without length is a single char. */
2800 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2801 if (gfc_match (" kind =") == MATCH_YES
)
2803 m
= match_char_kind (&kind
, &is_iso_c
);
2805 if (m
== MATCH_ERROR
)
2810 if (gfc_match (" , len =") == MATCH_NO
)
2813 m
= char_len_param_value (&len
, &deferred
);
2816 if (m
== MATCH_ERROR
)
2823 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2824 if (gfc_match (" len =") == MATCH_YES
)
2826 m
= char_len_param_value (&len
, &deferred
);
2829 if (m
== MATCH_ERROR
)
2833 if (gfc_match_char (')') == MATCH_YES
)
2836 if (gfc_match (" , kind =") != MATCH_YES
)
2839 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2845 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2846 m
= char_len_param_value (&len
, &deferred
);
2849 if (m
== MATCH_ERROR
)
2853 m
= gfc_match_char (')');
2857 if (gfc_match_char (',') != MATCH_YES
)
2860 gfc_match (" kind ="); /* Gobble optional text. */
2862 m
= match_char_kind (&kind
, &is_iso_c
);
2863 if (m
== MATCH_ERROR
)
2869 /* Require a right-paren at this point. */
2870 m
= gfc_match_char (')');
2875 gfc_error ("Syntax error in CHARACTER declaration at %C");
2877 gfc_free_expr (len
);
2881 /* Deal with character functions after USE and IMPORT statements. */
2882 if (gfc_matching_function
)
2884 gfc_free_expr (len
);
2885 gfc_undo_symbols ();
2891 gfc_free_expr (len
);
2895 /* Do some final massaging of the length values. */
2896 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2898 if (seen_length
== 0)
2899 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2904 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2905 ts
->deferred
= deferred
;
2907 /* We have to know if it was a C interoperable kind so we can
2908 do accurate type checking of bind(c) procs, etc. */
2910 /* Mark this as C interoperable if being declared with one
2911 of the named constants from iso_c_binding. */
2912 ts
->is_c_interop
= is_iso_c
;
2913 else if (len
!= NULL
)
2914 /* Here, we might have parsed something such as: character(c_char)
2915 In this case, the parsing code above grabs the c_char when
2916 looking for the length (line 1690, roughly). it's the last
2917 testcase for parsing the kind params of a character variable.
2918 However, it's not actually the length. this seems like it
2920 To see if the user used a C interop kind, test the expr
2921 of the so called length, and see if it's C interoperable. */
2922 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2928 /* Matches a RECORD declaration. */
2931 match_record_decl (char *name
)
2934 old_loc
= gfc_current_locus
;
2937 m
= gfc_match (" record /");
2940 if (!flag_dec_structure
)
2942 gfc_current_locus
= old_loc
;
2943 gfc_error ("RECORD at %C is an extension, enable it with "
2947 m
= gfc_match (" %n/", name
);
2952 gfc_current_locus
= old_loc
;
2953 if (flag_dec_structure
2954 && (gfc_match (" record% ") == MATCH_YES
2955 || gfc_match (" record%t") == MATCH_YES
))
2956 gfc_error ("Structure name expected after RECORD at %C");
2963 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2964 structure to the matched specification. This is necessary for FUNCTION and
2965 IMPLICIT statements.
2967 If implicit_flag is nonzero, then we don't check for the optional
2968 kind specification. Not doing so is needed for matching an IMPLICIT
2969 statement correctly. */
2972 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2974 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2975 gfc_symbol
*sym
, *dt_sym
;
2978 bool seen_deferred_kind
, matched_type
;
2979 const char *dt_name
;
2981 /* A belt and braces check that the typespec is correctly being treated
2982 as a deferred characteristic association. */
2983 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2984 && (gfc_current_block ()->result
->ts
.kind
== -1)
2985 && (ts
->kind
== -1);
2987 if (seen_deferred_kind
)
2990 /* Clear the current binding label, in case one is given. */
2991 curr_binding_label
= NULL
;
2993 if (gfc_match (" byte") == MATCH_YES
)
2995 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2998 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
3000 gfc_error ("BYTE type used at %C "
3001 "is not available on the target machine");
3005 ts
->type
= BT_INTEGER
;
3011 m
= gfc_match (" type (");
3012 matched_type
= (m
== MATCH_YES
);
3015 gfc_gobble_whitespace ();
3016 if (gfc_peek_ascii_char () == '*')
3018 if ((m
= gfc_match ("*)")) != MATCH_YES
)
3020 if (gfc_comp_struct (gfc_current_state ()))
3022 gfc_error ("Assumed type at %C is not allowed for components");
3025 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
3028 ts
->type
= BT_ASSUMED
;
3032 m
= gfc_match ("%n", name
);
3033 matched_type
= (m
== MATCH_YES
);
3036 if ((matched_type
&& strcmp ("integer", name
) == 0)
3037 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
3039 ts
->type
= BT_INTEGER
;
3040 ts
->kind
= gfc_default_integer_kind
;
3044 if ((matched_type
&& strcmp ("character", name
) == 0)
3045 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
3048 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
3049 "intrinsic-type-spec at %C"))
3052 ts
->type
= BT_CHARACTER
;
3053 if (implicit_flag
== 0)
3054 m
= gfc_match_char_spec (ts
);
3058 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
3064 if ((matched_type
&& strcmp ("real", name
) == 0)
3065 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
3068 ts
->kind
= gfc_default_real_kind
;
3073 && (strcmp ("doubleprecision", name
) == 0
3074 || (strcmp ("double", name
) == 0
3075 && gfc_match (" precision") == MATCH_YES
)))
3076 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
3079 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
3080 "intrinsic-type-spec at %C"))
3082 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3086 ts
->kind
= gfc_default_double_kind
;
3090 if ((matched_type
&& strcmp ("complex", name
) == 0)
3091 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
3093 ts
->type
= BT_COMPLEX
;
3094 ts
->kind
= gfc_default_complex_kind
;
3099 && (strcmp ("doublecomplex", name
) == 0
3100 || (strcmp ("double", name
) == 0
3101 && gfc_match (" complex") == MATCH_YES
)))
3102 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
3104 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
3108 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
3109 "intrinsic-type-spec at %C"))
3112 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3115 ts
->type
= BT_COMPLEX
;
3116 ts
->kind
= gfc_default_double_kind
;
3120 if ((matched_type
&& strcmp ("logical", name
) == 0)
3121 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
3123 ts
->type
= BT_LOGICAL
;
3124 ts
->kind
= gfc_default_logical_kind
;
3129 m
= gfc_match_char (')');
3132 m
= match_record_decl (name
);
3134 if (matched_type
|| m
== MATCH_YES
)
3136 ts
->type
= BT_DERIVED
;
3137 /* We accept record/s/ or type(s) where s is a structure, but we
3138 * don't need all the extra derived-type stuff for structures. */
3139 if (gfc_find_symbol (gfc_dt_upper_string (name
), NULL
, 1, &sym
))
3141 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3144 if (sym
&& sym
->attr
.flavor
== FL_STRUCT
)
3146 ts
->u
.derived
= sym
;
3149 /* Actually a derived type. */
3154 /* Match nested STRUCTURE declarations; only valid within another
3155 structure declaration. */
3156 if (flag_dec_structure
3157 && (gfc_current_state () == COMP_STRUCTURE
3158 || gfc_current_state () == COMP_MAP
))
3160 m
= gfc_match (" structure");
3163 m
= gfc_match_structure_decl ();
3166 /* gfc_new_block is updated by match_structure_decl. */
3167 ts
->type
= BT_DERIVED
;
3168 ts
->u
.derived
= gfc_new_block
;
3172 if (m
== MATCH_ERROR
)
3176 /* Match CLASS declarations. */
3177 m
= gfc_match (" class ( * )");
3178 if (m
== MATCH_ERROR
)
3180 else if (m
== MATCH_YES
)
3184 ts
->type
= BT_CLASS
;
3185 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
3188 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
3189 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
3191 gfc_set_sym_referenced (upe
);
3193 upe
->ts
.type
= BT_VOID
;
3194 upe
->attr
.unlimited_polymorphic
= 1;
3195 /* This is essential to force the construction of
3196 unlimited polymorphic component class containers. */
3197 upe
->attr
.zero_comp
= 1;
3198 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
3199 &gfc_current_locus
))
3204 st
= gfc_get_tbp_symtree (&gfc_current_ns
->sym_root
, "STAR");
3208 ts
->u
.derived
= upe
;
3212 m
= gfc_match (" class ( %n )", name
);
3215 ts
->type
= BT_CLASS
;
3217 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
3221 /* Defer association of the derived type until the end of the
3222 specification block. However, if the derived type can be
3223 found, add it to the typespec. */
3224 if (gfc_matching_function
)
3226 ts
->u
.derived
= NULL
;
3227 if (gfc_current_state () != COMP_INTERFACE
3228 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
3230 sym
= gfc_find_dt_in_generic (sym
);
3231 ts
->u
.derived
= sym
;
3236 /* Search for the name but allow the components to be defined later. If
3237 type = -1, this typespec has been seen in a function declaration but
3238 the type could not be accessed at that point. The actual derived type is
3239 stored in a symtree with the first letter of the name capitalized; the
3240 symtree with the all lower-case name contains the associated
3241 generic function. */
3242 dt_name
= gfc_dt_upper_string (name
);
3247 gfc_get_ha_symbol (name
, &sym
);
3248 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
3250 gfc_error ("Type name %qs at %C is ambiguous", name
);
3253 if (sym
->generic
&& !dt_sym
)
3254 dt_sym
= gfc_find_dt_in_generic (sym
);
3256 else if (ts
->kind
== -1)
3258 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
3259 || gfc_current_ns
->has_import_set
;
3260 gfc_find_symbol (name
, NULL
, iface
, &sym
);
3261 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
3263 gfc_error ("Type name %qs at %C is ambiguous", name
);
3266 if (sym
&& sym
->generic
&& !dt_sym
)
3267 dt_sym
= gfc_find_dt_in_generic (sym
);
3274 if ((sym
->attr
.flavor
!= FL_UNKNOWN
&& sym
->attr
.flavor
!= FL_STRUCT
3275 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
3276 || sym
->attr
.subroutine
)
3278 gfc_error ("Type name %qs at %C conflicts with previously declared "
3279 "entity at %L, which has the same name", name
,
3284 gfc_save_symbol_data (sym
);
3285 gfc_set_sym_referenced (sym
);
3286 if (!sym
->attr
.generic
3287 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
3290 if (!sym
->attr
.function
3291 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
3296 gfc_interface
*intr
, *head
;
3298 /* Use upper case to save the actual derived-type symbol. */
3299 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
3300 dt_sym
->name
= gfc_get_string ("%s", sym
->name
);
3301 head
= sym
->generic
;
3302 intr
= gfc_get_interface ();
3304 intr
->where
= gfc_current_locus
;
3306 sym
->generic
= intr
;
3307 sym
->attr
.if_source
= IFSRC_DECL
;
3310 gfc_save_symbol_data (dt_sym
);
3312 gfc_set_sym_referenced (dt_sym
);
3314 if (dt_sym
->attr
.flavor
!= FL_DERIVED
&& dt_sym
->attr
.flavor
!= FL_STRUCT
3315 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
3318 ts
->u
.derived
= dt_sym
;
3324 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
3325 "intrinsic-type-spec at %C"))
3328 /* For all types except double, derived and character, look for an
3329 optional kind specifier. MATCH_NO is actually OK at this point. */
3330 if (implicit_flag
== 1)
3332 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3338 if (gfc_current_form
== FORM_FREE
)
3340 c
= gfc_peek_ascii_char ();
3341 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
3342 && c
!= ':' && c
!= ',')
3344 if (matched_type
&& c
== ')')
3346 gfc_next_ascii_char ();
3353 m
= gfc_match_kind_spec (ts
, false);
3354 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
3356 m
= gfc_match_old_kind_spec (ts
);
3357 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) == -1)
3361 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3364 /* Defer association of the KIND expression of function results
3365 until after USE and IMPORT statements. */
3366 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
3367 || gfc_matching_function
)
3371 m
= MATCH_YES
; /* No kind specifier found. */
3377 /* Match an IMPLICIT NONE statement. Actually, this statement is
3378 already matched in parse.c, or we would not end up here in the
3379 first place. So the only thing we need to check, is if there is
3380 trailing garbage. If not, the match is successful. */
3383 gfc_match_implicit_none (void)
3387 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3389 bool external
= false;
3390 locus cur_loc
= gfc_current_locus
;
3392 if (gfc_current_ns
->seen_implicit_none
3393 || gfc_current_ns
->has_implicit_none_export
)
3395 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3399 gfc_gobble_whitespace ();
3400 c
= gfc_peek_ascii_char ();
3403 (void) gfc_next_ascii_char ();
3404 if (!gfc_notify_std (GFC_STD_F2015
, "IMPORT NONE with spec list at %C"))
3407 gfc_gobble_whitespace ();
3408 if (gfc_peek_ascii_char () == ')')
3410 (void) gfc_next_ascii_char ();
3416 m
= gfc_match (" %n", name
);
3420 if (strcmp (name
, "type") == 0)
3422 else if (strcmp (name
, "external") == 0)
3427 gfc_gobble_whitespace ();
3428 c
= gfc_next_ascii_char ();
3439 if (gfc_match_eos () != MATCH_YES
)
3442 gfc_set_implicit_none (type
, external
, &cur_loc
);
3448 /* Match the letter range(s) of an IMPLICIT statement. */
3451 match_implicit_range (void)
3457 cur_loc
= gfc_current_locus
;
3459 gfc_gobble_whitespace ();
3460 c
= gfc_next_ascii_char ();
3463 gfc_error ("Missing character range in IMPLICIT at %C");
3470 gfc_gobble_whitespace ();
3471 c1
= gfc_next_ascii_char ();
3475 gfc_gobble_whitespace ();
3476 c
= gfc_next_ascii_char ();
3481 inner
= 0; /* Fall through. */
3488 gfc_gobble_whitespace ();
3489 c2
= gfc_next_ascii_char ();
3493 gfc_gobble_whitespace ();
3494 c
= gfc_next_ascii_char ();
3496 if ((c
!= ',') && (c
!= ')'))
3509 gfc_error ("Letters must be in alphabetic order in "
3510 "IMPLICIT statement at %C");
3514 /* See if we can add the newly matched range to the pending
3515 implicits from this IMPLICIT statement. We do not check for
3516 conflicts with whatever earlier IMPLICIT statements may have
3517 set. This is done when we've successfully finished matching
3519 if (!gfc_add_new_implicit_range (c1
, c2
))
3526 gfc_syntax_error (ST_IMPLICIT
);
3528 gfc_current_locus
= cur_loc
;
3533 /* Match an IMPLICIT statement, storing the types for
3534 gfc_set_implicit() if the statement is accepted by the parser.
3535 There is a strange looking, but legal syntactic construction
3536 possible. It looks like:
3538 IMPLICIT INTEGER (a-b) (c-d)
3540 This is legal if "a-b" is a constant expression that happens to
3541 equal one of the legal kinds for integers. The real problem
3542 happens with an implicit specification that looks like:
3544 IMPLICIT INTEGER (a-b)
3546 In this case, a typespec matcher that is "greedy" (as most of the
3547 matchers are) gobbles the character range as a kindspec, leaving
3548 nothing left. We therefore have to go a bit more slowly in the
3549 matching process by inhibiting the kindspec checking during
3550 typespec matching and checking for a kind later. */
3553 gfc_match_implicit (void)
3560 if (gfc_current_ns
->seen_implicit_none
)
3562 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3569 /* We don't allow empty implicit statements. */
3570 if (gfc_match_eos () == MATCH_YES
)
3572 gfc_error ("Empty IMPLICIT statement at %C");
3578 /* First cleanup. */
3579 gfc_clear_new_implicit ();
3581 /* A basic type is mandatory here. */
3582 m
= gfc_match_decl_type_spec (&ts
, 1);
3583 if (m
== MATCH_ERROR
)
3588 cur_loc
= gfc_current_locus
;
3589 m
= match_implicit_range ();
3593 /* We may have <TYPE> (<RANGE>). */
3594 gfc_gobble_whitespace ();
3595 c
= gfc_peek_ascii_char ();
3596 if (c
== ',' || c
== '\n' || c
== ';' || c
== '!')
3598 /* Check for CHARACTER with no length parameter. */
3599 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3601 ts
.kind
= gfc_default_character_kind
;
3602 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3603 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3607 /* Record the Successful match. */
3608 if (!gfc_merge_new_implicit (&ts
))
3611 c
= gfc_next_ascii_char ();
3612 else if (gfc_match_eos () == MATCH_ERROR
)
3617 gfc_current_locus
= cur_loc
;
3620 /* Discard the (incorrectly) matched range. */
3621 gfc_clear_new_implicit ();
3623 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3624 if (ts
.type
== BT_CHARACTER
)
3625 m
= gfc_match_char_spec (&ts
);
3628 m
= gfc_match_kind_spec (&ts
, false);
3631 m
= gfc_match_old_kind_spec (&ts
);
3632 if (m
== MATCH_ERROR
)
3638 if (m
== MATCH_ERROR
)
3641 m
= match_implicit_range ();
3642 if (m
== MATCH_ERROR
)
3647 gfc_gobble_whitespace ();
3648 c
= gfc_next_ascii_char ();
3649 if (c
!= ',' && gfc_match_eos () != MATCH_YES
)
3652 if (!gfc_merge_new_implicit (&ts
))
3660 gfc_syntax_error (ST_IMPLICIT
);
3668 gfc_match_import (void)
3670 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3675 if (gfc_current_ns
->proc_name
== NULL
3676 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3678 gfc_error ("IMPORT statement at %C only permitted in "
3679 "an INTERFACE body");
3683 if (gfc_current_ns
->proc_name
->attr
.module_procedure
)
3685 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3686 "in a module procedure interface body");
3690 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3693 if (gfc_match_eos () == MATCH_YES
)
3695 /* All host variables should be imported. */
3696 gfc_current_ns
->has_import_set
= 1;
3700 if (gfc_match (" ::") == MATCH_YES
)
3702 if (gfc_match_eos () == MATCH_YES
)
3704 gfc_error ("Expecting list of named entities at %C");
3712 m
= gfc_match (" %n", name
);
3716 if (gfc_current_ns
->parent
!= NULL
3717 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3719 gfc_error ("Type name %qs at %C is ambiguous", name
);
3722 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3723 && gfc_find_symbol (name
,
3724 gfc_current_ns
->proc_name
->ns
->parent
,
3727 gfc_error ("Type name %qs at %C is ambiguous", name
);
3733 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3734 "at %C - does not exist.", name
);
3738 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3740 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3745 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3748 sym
->attr
.imported
= 1;
3750 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3752 /* The actual derived type is stored in a symtree with the first
3753 letter of the name capitalized; the symtree with the all
3754 lower-case name contains the associated generic function. */
3755 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3756 gfc_dt_upper_string (name
));
3759 sym
->attr
.imported
= 1;
3772 if (gfc_match_eos () == MATCH_YES
)
3774 if (gfc_match_char (',') != MATCH_YES
)
3781 gfc_error ("Syntax error in IMPORT statement at %C");
3786 /* A minimal implementation of gfc_match without whitespace, escape
3787 characters or variable arguments. Returns true if the next
3788 characters match the TARGET template exactly. */
3791 match_string_p (const char *target
)
3795 for (p
= target
; *p
; p
++)
3796 if ((char) gfc_next_ascii_char () != *p
)
3801 /* Matches an attribute specification including array specs. If
3802 successful, leaves the variables current_attr and current_as
3803 holding the specification. Also sets the colon_seen variable for
3804 later use by matchers associated with initializations.
3806 This subroutine is a little tricky in the sense that we don't know
3807 if we really have an attr-spec until we hit the double colon.
3808 Until that time, we can only return MATCH_NO. This forces us to
3809 check for duplicate specification at this level. */
3812 match_attr_spec (void)
3814 /* Modifiers that can exist in a type statement. */
3816 { GFC_DECL_BEGIN
= 0,
3817 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3818 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3819 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3820 DECL_STATIC
, DECL_AUTOMATIC
,
3821 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3822 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3823 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3826 /* GFC_DECL_END is the sentinel, index starts at 0. */
3827 #define NUM_DECL GFC_DECL_END
3829 locus start
, seen_at
[NUM_DECL
];
3836 gfc_clear_attr (¤t_attr
);
3837 start
= gfc_current_locus
;
3842 /* See if we get all of the keywords up to the final double colon. */
3843 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3851 gfc_gobble_whitespace ();
3853 ch
= gfc_next_ascii_char ();
3856 /* This is the successful exit condition for the loop. */
3857 if (gfc_next_ascii_char () == ':')
3862 gfc_gobble_whitespace ();
3863 switch (gfc_peek_ascii_char ())
3866 gfc_next_ascii_char ();
3867 switch (gfc_next_ascii_char ())
3870 if (match_string_p ("locatable"))
3872 /* Matched "allocatable". */
3873 d
= DECL_ALLOCATABLE
;
3878 if (match_string_p ("ynchronous"))
3880 /* Matched "asynchronous". */
3881 d
= DECL_ASYNCHRONOUS
;
3886 if (match_string_p ("tomatic"))
3888 /* Matched "automatic". */
3896 /* Try and match the bind(c). */
3897 m
= gfc_match_bind_c (NULL
, true);
3900 else if (m
== MATCH_ERROR
)
3905 gfc_next_ascii_char ();
3906 if ('o' != gfc_next_ascii_char ())
3908 switch (gfc_next_ascii_char ())
3911 if (match_string_p ("imension"))
3913 d
= DECL_CODIMENSION
;
3918 if (match_string_p ("tiguous"))
3920 d
= DECL_CONTIGUOUS
;
3927 if (match_string_p ("dimension"))
3932 if (match_string_p ("external"))
3937 if (match_string_p ("int"))
3939 ch
= gfc_next_ascii_char ();
3942 if (match_string_p ("nt"))
3944 /* Matched "intent". */
3945 /* TODO: Call match_intent_spec from here. */
3946 if (gfc_match (" ( in out )") == MATCH_YES
)
3948 else if (gfc_match (" ( in )") == MATCH_YES
)
3950 else if (gfc_match (" ( out )") == MATCH_YES
)
3956 if (match_string_p ("insic"))
3958 /* Matched "intrinsic". */
3966 if (match_string_p ("optional"))
3971 gfc_next_ascii_char ();
3972 switch (gfc_next_ascii_char ())
3975 if (match_string_p ("rameter"))
3977 /* Matched "parameter". */
3983 if (match_string_p ("inter"))
3985 /* Matched "pointer". */
3991 ch
= gfc_next_ascii_char ();
3994 if (match_string_p ("vate"))
3996 /* Matched "private". */
4002 if (match_string_p ("tected"))
4004 /* Matched "protected". */
4011 if (match_string_p ("blic"))
4013 /* Matched "public". */
4021 gfc_next_ascii_char ();
4022 switch (gfc_next_ascii_char ())
4025 if (match_string_p ("ve"))
4027 /* Matched "save". */
4033 if (match_string_p ("atic"))
4035 /* Matched "static". */
4043 if (match_string_p ("target"))
4048 gfc_next_ascii_char ();
4049 ch
= gfc_next_ascii_char ();
4052 if (match_string_p ("lue"))
4054 /* Matched "value". */
4060 if (match_string_p ("latile"))
4062 /* Matched "volatile". */
4070 /* No double colon and no recognizable decl_type, so assume that
4071 we've been looking at something else the whole time. */
4078 /* Check to make sure any parens are paired up correctly. */
4079 if (gfc_match_parens () == MATCH_ERROR
)
4086 seen_at
[d
] = gfc_current_locus
;
4088 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
4090 gfc_array_spec
*as
= NULL
;
4092 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
4093 d
== DECL_CODIMENSION
);
4095 if (current_as
== NULL
)
4097 else if (m
== MATCH_YES
)
4099 if (!merge_array_spec (as
, current_as
, false))
4106 if (d
== DECL_CODIMENSION
)
4107 gfc_error ("Missing codimension specification at %C");
4109 gfc_error ("Missing dimension specification at %C");
4113 if (m
== MATCH_ERROR
)
4118 /* Since we've seen a double colon, we have to be looking at an
4119 attr-spec. This means that we can now issue errors. */
4120 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
4125 case DECL_ALLOCATABLE
:
4126 attr
= "ALLOCATABLE";
4128 case DECL_ASYNCHRONOUS
:
4129 attr
= "ASYNCHRONOUS";
4131 case DECL_CODIMENSION
:
4132 attr
= "CODIMENSION";
4134 case DECL_CONTIGUOUS
:
4135 attr
= "CONTIGUOUS";
4137 case DECL_DIMENSION
:
4144 attr
= "INTENT (IN)";
4147 attr
= "INTENT (OUT)";
4150 attr
= "INTENT (IN OUT)";
4152 case DECL_INTRINSIC
:
4158 case DECL_PARAMETER
:
4164 case DECL_PROTECTED
:
4179 case DECL_AUTOMATIC
:
4185 case DECL_IS_BIND_C
:
4195 attr
= NULL
; /* This shouldn't happen. */
4198 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
4203 /* Now that we've dealt with duplicate attributes, add the attributes
4204 to the current attribute. */
4205 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
4210 if ((d
== DECL_STATIC
|| d
== DECL_AUTOMATIC
)
4211 && !flag_dec_static
)
4213 gfc_error ("%s at %L is a DEC extension, enable with -fdec-static",
4214 d
== DECL_STATIC
? "STATIC" : "AUTOMATIC", &seen_at
[d
]);
4218 /* Allow SAVE with STATIC, but don't complain. */
4219 if (d
== DECL_STATIC
&& seen
[DECL_SAVE
])
4222 if (gfc_current_state () == COMP_DERIVED
4223 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
4224 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
4225 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
4227 if (d
== DECL_ALLOCATABLE
)
4229 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
4230 "attribute at %C in a TYPE definition"))
4238 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
4245 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
4246 && gfc_current_state () != COMP_MODULE
)
4248 if (d
== DECL_PRIVATE
)
4252 if (gfc_current_state () == COMP_DERIVED
4253 && gfc_state_stack
->previous
4254 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
4256 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
4257 "at %L in a TYPE definition", attr
,
4266 gfc_error ("%s attribute at %L is not allowed outside of the "
4267 "specification part of a module", attr
, &seen_at
[d
]);
4275 case DECL_ALLOCATABLE
:
4276 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
4279 case DECL_ASYNCHRONOUS
:
4280 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
4283 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
4286 case DECL_CODIMENSION
:
4287 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
4290 case DECL_CONTIGUOUS
:
4291 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
4294 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
4297 case DECL_DIMENSION
:
4298 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
4302 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
4306 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
4310 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
4314 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
4317 case DECL_INTRINSIC
:
4318 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
4322 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
4325 case DECL_PARAMETER
:
4326 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
4330 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
4333 case DECL_PROTECTED
:
4334 if (gfc_current_state () != COMP_MODULE
4335 || (gfc_current_ns
->proc_name
4336 && gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
))
4338 gfc_error ("PROTECTED at %C only allowed in specification "
4339 "part of a module");
4344 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
4347 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
4351 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
4356 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
4362 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
4365 case DECL_AUTOMATIC
:
4366 t
= gfc_add_automatic (¤t_attr
, NULL
, &seen_at
[d
]);
4370 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
4373 case DECL_IS_BIND_C
:
4374 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
4378 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
4381 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
4385 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
4388 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
4392 gfc_internal_error ("match_attr_spec(): Bad attribute");
4402 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4403 if ((gfc_current_state () == COMP_MODULE
4404 || gfc_current_state () == COMP_SUBMODULE
)
4405 && !current_attr
.save
4406 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
4407 current_attr
.save
= SAVE_IMPLICIT
;
4413 gfc_current_locus
= start
;
4414 gfc_free_array_spec (current_as
);
4420 /* Set the binding label, dest_label, either with the binding label
4421 stored in the given gfc_typespec, ts, or if none was provided, it
4422 will be the symbol name in all lower case, as required by the draft
4423 (J3/04-007, section 15.4.1). If a binding label was given and
4424 there is more than one argument (num_idents), it is an error. */
4427 set_binding_label (const char **dest_label
, const char *sym_name
,
4430 if (num_idents
> 1 && has_name_equals
)
4432 gfc_error ("Multiple identifiers provided with "
4433 "single NAME= specifier at %C");
4437 if (curr_binding_label
)
4438 /* Binding label given; store in temp holder till have sym. */
4439 *dest_label
= curr_binding_label
;
4442 /* No binding label given, and the NAME= specifier did not exist,
4443 which means there was no NAME="". */
4444 if (sym_name
!= NULL
&& has_name_equals
== 0)
4445 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
4452 /* Set the status of the given common block as being BIND(C) or not,
4453 depending on the given parameter, is_bind_c. */
4456 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
4458 com_block
->is_bind_c
= is_bind_c
;
4463 /* Verify that the given gfc_typespec is for a C interoperable type. */
4466 gfc_verify_c_interop (gfc_typespec
*ts
)
4468 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
4469 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
4471 else if (ts
->type
== BT_CLASS
)
4473 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
4480 /* Verify that the variables of a given common block, which has been
4481 defined with the attribute specifier bind(c), to be of a C
4482 interoperable type. Errors will be reported here, if
4486 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
4488 gfc_symbol
*curr_sym
= NULL
;
4491 curr_sym
= com_block
->head
;
4493 /* Make sure we have at least one symbol. */
4494 if (curr_sym
== NULL
)
4497 /* Here we know we have a symbol, so we'll execute this loop
4501 /* The second to last param, 1, says this is in a common block. */
4502 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
4503 curr_sym
= curr_sym
->common_next
;
4504 } while (curr_sym
!= NULL
);
4510 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4511 an appropriate error message is reported. */
4514 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
4515 int is_in_common
, gfc_common_head
*com_block
)
4517 bool bind_c_function
= false;
4520 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
4521 bind_c_function
= true;
4523 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
4525 tmp_sym
= tmp_sym
->result
;
4526 /* Make sure it wasn't an implicitly typed result. */
4527 if (tmp_sym
->attr
.implicit_type
&& warn_c_binding_type
)
4529 gfc_warning (OPT_Wc_binding_type
,
4530 "Implicitly declared BIND(C) function %qs at "
4531 "%L may not be C interoperable", tmp_sym
->name
,
4532 &tmp_sym
->declared_at
);
4533 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
4534 /* Mark it as C interoperable to prevent duplicate warnings. */
4535 tmp_sym
->ts
.is_c_interop
= 1;
4536 tmp_sym
->attr
.is_c_interop
= 1;
4540 /* Here, we know we have the bind(c) attribute, so if we have
4541 enough type info, then verify that it's a C interop kind.
4542 The info could be in the symbol already, or possibly still in
4543 the given ts (current_ts), so look in both. */
4544 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
4546 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
4548 /* See if we're dealing with a sym in a common block or not. */
4549 if (is_in_common
== 1 && warn_c_binding_type
)
4551 gfc_warning (OPT_Wc_binding_type
,
4552 "Variable %qs in common block %qs at %L "
4553 "may not be a C interoperable "
4554 "kind though common block %qs is BIND(C)",
4555 tmp_sym
->name
, com_block
->name
,
4556 &(tmp_sym
->declared_at
), com_block
->name
);
4560 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
4561 gfc_error ("Type declaration %qs at %L is not C "
4562 "interoperable but it is BIND(C)",
4563 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4564 else if (warn_c_binding_type
)
4565 gfc_warning (OPT_Wc_binding_type
, "Variable %qs at %L "
4566 "may not be a C interoperable "
4567 "kind but it is BIND(C)",
4568 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4572 /* Variables declared w/in a common block can't be bind(c)
4573 since there's no way for C to see these variables, so there's
4574 semantically no reason for the attribute. */
4575 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4577 gfc_error ("Variable %qs in common block %qs at "
4578 "%L cannot be declared with BIND(C) "
4579 "since it is not a global",
4580 tmp_sym
->name
, com_block
->name
,
4581 &(tmp_sym
->declared_at
));
4585 /* Scalar variables that are bind(c) can not have the pointer
4586 or allocatable attributes. */
4587 if (tmp_sym
->attr
.is_bind_c
== 1)
4589 if (tmp_sym
->attr
.pointer
== 1)
4591 gfc_error ("Variable %qs at %L cannot have both the "
4592 "POINTER and BIND(C) attributes",
4593 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4597 if (tmp_sym
->attr
.allocatable
== 1)
4599 gfc_error ("Variable %qs at %L cannot have both the "
4600 "ALLOCATABLE and BIND(C) attributes",
4601 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4607 /* If it is a BIND(C) function, make sure the return value is a
4608 scalar value. The previous tests in this function made sure
4609 the type is interoperable. */
4610 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4611 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4612 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4614 /* BIND(C) functions can not return a character string. */
4615 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4616 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4617 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4618 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4619 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4620 "be a character string", tmp_sym
->name
,
4621 &(tmp_sym
->declared_at
));
4624 /* See if the symbol has been marked as private. If it has, make sure
4625 there is no binding label and warn the user if there is one. */
4626 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4627 && tmp_sym
->binding_label
)
4628 /* Use gfc_warning_now because we won't say that the symbol fails
4629 just because of this. */
4630 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4631 "given the binding label %qs", tmp_sym
->name
,
4632 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4638 /* Set the appropriate fields for a symbol that's been declared as
4639 BIND(C) (the is_bind_c flag and the binding label), and verify that
4640 the type is C interoperable. Errors are reported by the functions
4641 used to set/test these fields. */
4644 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4648 /* TODO: Do we need to make sure the vars aren't marked private? */
4650 /* Set the is_bind_c bit in symbol_attribute. */
4651 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4653 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4660 /* Set the fields marking the given common block as BIND(C), including
4661 a binding label, and report any errors encountered. */
4664 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4668 /* destLabel, common name, typespec (which may have binding label). */
4669 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4673 /* Set the given common block (com_block) to being bind(c) (1). */
4674 set_com_block_bind_c (com_block
, 1);
4680 /* Retrieve the list of one or more identifiers that the given bind(c)
4681 attribute applies to. */
4684 get_bind_c_idents (void)
4686 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4688 gfc_symbol
*tmp_sym
= NULL
;
4690 gfc_common_head
*com_block
= NULL
;
4692 if (gfc_match_name (name
) == MATCH_YES
)
4694 found_id
= MATCH_YES
;
4695 gfc_get_ha_symbol (name
, &tmp_sym
);
4697 else if (match_common_name (name
) == MATCH_YES
)
4699 found_id
= MATCH_YES
;
4700 com_block
= gfc_get_common (name
, 0);
4704 gfc_error ("Need either entity or common block name for "
4705 "attribute specification statement at %C");
4709 /* Save the current identifier and look for more. */
4712 /* Increment the number of identifiers found for this spec stmt. */
4715 /* Make sure we have a sym or com block, and verify that it can
4716 be bind(c). Set the appropriate field(s) and look for more
4718 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4720 if (tmp_sym
!= NULL
)
4722 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4727 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4731 /* Look to see if we have another identifier. */
4733 if (gfc_match_eos () == MATCH_YES
)
4734 found_id
= MATCH_NO
;
4735 else if (gfc_match_char (',') != MATCH_YES
)
4736 found_id
= MATCH_NO
;
4737 else if (gfc_match_name (name
) == MATCH_YES
)
4739 found_id
= MATCH_YES
;
4740 gfc_get_ha_symbol (name
, &tmp_sym
);
4742 else if (match_common_name (name
) == MATCH_YES
)
4744 found_id
= MATCH_YES
;
4745 com_block
= gfc_get_common (name
, 0);
4749 gfc_error ("Missing entity or common block name for "
4750 "attribute specification statement at %C");
4756 gfc_internal_error ("Missing symbol");
4758 } while (found_id
== MATCH_YES
);
4760 /* if we get here we were successful */
4765 /* Try and match a BIND(C) attribute specification statement. */
4768 gfc_match_bind_c_stmt (void)
4770 match found_match
= MATCH_NO
;
4775 /* This may not be necessary. */
4777 /* Clear the temporary binding label holder. */
4778 curr_binding_label
= NULL
;
4780 /* Look for the bind(c). */
4781 found_match
= gfc_match_bind_c (NULL
, true);
4783 if (found_match
== MATCH_YES
)
4785 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4788 /* Look for the :: now, but it is not required. */
4791 /* Get the identifier(s) that needs to be updated. This may need to
4792 change to hand the flag(s) for the attr specified so all identifiers
4793 found can have all appropriate parts updated (assuming that the same
4794 spec stmt can have multiple attrs, such as both bind(c) and
4796 if (!get_bind_c_idents ())
4797 /* Error message should have printed already. */
4805 /* Match a data declaration statement. */
4808 gfc_match_data_decl (void)
4814 num_idents_on_line
= 0;
4816 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4820 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4821 && !gfc_comp_struct (gfc_current_state ()))
4823 sym
= gfc_use_derived (current_ts
.u
.derived
);
4831 current_ts
.u
.derived
= sym
;
4834 m
= match_attr_spec ();
4835 if (m
== MATCH_ERROR
)
4841 if (current_ts
.type
== BT_CLASS
4842 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4845 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4846 && current_ts
.u
.derived
->components
== NULL
4847 && !current_ts
.u
.derived
->attr
.zero_comp
)
4850 if (current_attr
.pointer
&& gfc_comp_struct (gfc_current_state ()))
4853 if (current_attr
.allocatable
&& gfc_current_state () == COMP_DERIVED
4854 && current_ts
.u
.derived
== gfc_current_block ())
4857 gfc_find_symbol (current_ts
.u
.derived
->name
,
4858 current_ts
.u
.derived
->ns
, 1, &sym
);
4860 /* Any symbol that we find had better be a type definition
4861 which has its components defined, or be a structure definition
4862 actively being parsed. */
4863 if (sym
!= NULL
&& gfc_fl_struct (sym
->attr
.flavor
)
4864 && (current_ts
.u
.derived
->components
!= NULL
4865 || current_ts
.u
.derived
->attr
.zero_comp
4866 || current_ts
.u
.derived
== gfc_new_block
))
4869 gfc_error ("Derived type at %C has not been previously defined "
4870 "and so cannot appear in a derived type definition");
4876 /* If we have an old-style character declaration, and no new-style
4877 attribute specifications, then there a comma is optional between
4878 the type specification and the variable list. */
4879 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4880 gfc_match_char (',');
4882 /* Give the types/attributes to symbols that follow. Give the element
4883 a number so that repeat character length expressions can be copied. */
4887 num_idents_on_line
++;
4888 m
= variable_decl (elem
++);
4889 if (m
== MATCH_ERROR
)
4894 if (gfc_match_eos () == MATCH_YES
)
4896 if (gfc_match_char (',') != MATCH_YES
)
4900 if (!gfc_error_flag_test ())
4902 /* An anonymous structure declaration is unambiguous; if we matched one
4903 according to gfc_match_structure_decl, we need to return MATCH_YES
4904 here to avoid confusing the remaining matchers, even if there was an
4905 error during variable_decl. We must flush any such errors. Note this
4906 causes the parser to gracefully continue parsing the remaining input
4907 as a structure body, which likely follows. */
4908 if (current_ts
.type
== BT_DERIVED
&& current_ts
.u
.derived
4909 && gfc_fl_struct (current_ts
.u
.derived
->attr
.flavor
))
4911 gfc_error_now ("Syntax error in anonymous structure declaration"
4913 /* Skip the bad variable_decl and line up for the start of the
4915 gfc_error_recovery ();
4920 gfc_error ("Syntax error in data declaration at %C");
4925 gfc_free_data_all (gfc_current_ns
);
4928 gfc_free_array_spec (current_as
);
4934 /* Match a prefix associated with a function or subroutine
4935 declaration. If the typespec pointer is nonnull, then a typespec
4936 can be matched. Note that if nothing matches, MATCH_YES is
4937 returned (the null string was matched). */
4940 gfc_match_prefix (gfc_typespec
*ts
)
4946 gfc_clear_attr (¤t_attr
);
4948 seen_impure
= false;
4950 gcc_assert (!gfc_matching_prefix
);
4951 gfc_matching_prefix
= true;
4955 found_prefix
= false;
4957 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
4958 corresponding attribute seems natural and distinguishes these
4959 procedures from procedure types of PROC_MODULE, which these are
4961 if (gfc_match ("module% ") == MATCH_YES
)
4963 if (!gfc_notify_std (GFC_STD_F2008
, "MODULE prefix at %C"))
4966 current_attr
.module_procedure
= 1;
4967 found_prefix
= true;
4970 if (!seen_type
&& ts
!= NULL
4971 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4972 && gfc_match_space () == MATCH_YES
)
4976 found_prefix
= true;
4979 if (gfc_match ("elemental% ") == MATCH_YES
)
4981 if (!gfc_add_elemental (¤t_attr
, NULL
))
4984 found_prefix
= true;
4987 if (gfc_match ("pure% ") == MATCH_YES
)
4989 if (!gfc_add_pure (¤t_attr
, NULL
))
4992 found_prefix
= true;
4995 if (gfc_match ("recursive% ") == MATCH_YES
)
4997 if (!gfc_add_recursive (¤t_attr
, NULL
))
5000 found_prefix
= true;
5003 /* IMPURE is a somewhat special case, as it needs not set an actual
5004 attribute but rather only prevents ELEMENTAL routines from being
5005 automatically PURE. */
5006 if (gfc_match ("impure% ") == MATCH_YES
)
5008 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
5012 found_prefix
= true;
5015 while (found_prefix
);
5017 /* IMPURE and PURE must not both appear, of course. */
5018 if (seen_impure
&& current_attr
.pure
)
5020 gfc_error ("PURE and IMPURE must not appear both at %C");
5024 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
5025 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
5027 if (!gfc_add_pure (¤t_attr
, NULL
))
5031 /* At this point, the next item is not a prefix. */
5032 gcc_assert (gfc_matching_prefix
);
5034 gfc_matching_prefix
= false;
5038 gcc_assert (gfc_matching_prefix
);
5039 gfc_matching_prefix
= false;
5044 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
5047 copy_prefix (symbol_attribute
*dest
, locus
*where
)
5049 if (dest
->module_procedure
)
5051 if (current_attr
.elemental
)
5052 dest
->elemental
= 1;
5054 if (current_attr
.pure
)
5057 if (current_attr
.recursive
)
5058 dest
->recursive
= 1;
5060 /* Module procedures are unusual in that the 'dest' is copied from
5061 the interface declaration. However, this is an oportunity to
5062 check that the submodule declaration is compliant with the
5064 if (dest
->elemental
&& !current_attr
.elemental
)
5066 gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is "
5067 "missing at %L", where
);
5071 if (dest
->pure
&& !current_attr
.pure
)
5073 gfc_error ("PURE prefix in MODULE PROCEDURE interface is "
5074 "missing at %L", where
);
5078 if (dest
->recursive
&& !current_attr
.recursive
)
5080 gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is "
5081 "missing at %L", where
);
5088 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
5091 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
5094 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
5101 /* Match a formal argument list. */
5104 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
5106 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
5107 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5110 gfc_formal_arglist
*formal
= NULL
;
5114 /* Keep the interface formal argument list and null it so that the
5115 matching for the new declaration can be done. The numbers and
5116 names of the arguments are checked here. The interface formal
5117 arguments are retained in formal_arglist and the characteristics
5118 are compared in resolve.c(resolve_fl_procedure). See the remark
5119 in get_proc_name about the eventual need to copy the formal_arglist
5120 and populate the formal namespace of the interface symbol. */
5121 if (progname
->attr
.module_procedure
5122 && progname
->attr
.host_assoc
)
5124 formal
= progname
->formal
;
5125 progname
->formal
= NULL
;
5128 if (gfc_match_char ('(') != MATCH_YES
)
5135 if (gfc_match_char (')') == MATCH_YES
)
5140 if (gfc_match_char ('*') == MATCH_YES
)
5143 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
5152 m
= gfc_match_name (name
);
5156 if (gfc_get_symbol (name
, NULL
, &sym
))
5160 p
= gfc_get_formal_arglist ();
5172 /* We don't add the VARIABLE flavor because the name could be a
5173 dummy procedure. We don't apply these attributes to formal
5174 arguments of statement functions. */
5175 if (sym
!= NULL
&& !st_flag
5176 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
5177 || !gfc_missing_attr (&sym
->attr
, NULL
)))
5183 /* The name of a program unit can be in a different namespace,
5184 so check for it explicitly. After the statement is accepted,
5185 the name is checked for especially in gfc_get_symbol(). */
5186 if (gfc_new_block
!= NULL
&& sym
!= NULL
5187 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
5189 gfc_error ("Name %qs at %C is the name of the procedure",
5195 if (gfc_match_char (')') == MATCH_YES
)
5198 m
= gfc_match_char (',');
5201 gfc_error ("Unexpected junk in formal argument list at %C");
5207 /* Check for duplicate symbols in the formal argument list. */
5210 for (p
= head
; p
->next
; p
= p
->next
)
5215 for (q
= p
->next
; q
; q
= q
->next
)
5216 if (p
->sym
== q
->sym
)
5218 gfc_error ("Duplicate symbol %qs in formal argument list "
5219 "at %C", p
->sym
->name
);
5227 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
5233 /* gfc_error_now used in following and return with MATCH_YES because
5234 doing otherwise results in a cascade of extraneous errors and in
5235 some cases an ICE in symbol.c(gfc_release_symbol). */
5236 if (progname
->attr
.module_procedure
&& progname
->attr
.host_assoc
)
5238 bool arg_count_mismatch
= false;
5240 if (!formal
&& head
)
5241 arg_count_mismatch
= true;
5243 /* Abbreviated module procedure declaration is not meant to have any
5244 formal arguments! */
5245 if (!progname
->abr_modproc_decl
&& formal
&& !head
)
5246 arg_count_mismatch
= true;
5248 for (p
= formal
, q
= head
; p
&& q
; p
= p
->next
, q
= q
->next
)
5250 if ((p
->next
!= NULL
&& q
->next
== NULL
)
5251 || (p
->next
== NULL
&& q
->next
!= NULL
))
5252 arg_count_mismatch
= true;
5253 else if ((p
->sym
== NULL
&& q
->sym
== NULL
)
5254 || strcmp (p
->sym
->name
, q
->sym
->name
) == 0)
5257 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
5258 "argument names (%s/%s) at %C",
5259 p
->sym
->name
, q
->sym
->name
);
5262 if (arg_count_mismatch
)
5263 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
5264 "formal arguments at %C");
5270 gfc_free_formal_arglist (head
);
5275 /* Match a RESULT specification following a function declaration or
5276 ENTRY statement. Also matches the end-of-statement. */
5279 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
5281 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5285 if (gfc_match (" result (") != MATCH_YES
)
5288 m
= gfc_match_name (name
);
5292 /* Get the right paren, and that's it because there could be the
5293 bind(c) attribute after the result clause. */
5294 if (gfc_match_char (')') != MATCH_YES
)
5296 /* TODO: should report the missing right paren here. */
5300 if (strcmp (function
->name
, name
) == 0)
5302 gfc_error ("RESULT variable at %C must be different than function name");
5306 if (gfc_get_symbol (name
, NULL
, &r
))
5309 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
5318 /* Match a function suffix, which could be a combination of a result
5319 clause and BIND(C), either one, or neither. The draft does not
5320 require them to come in a specific order. */
5323 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
5325 match is_bind_c
; /* Found bind(c). */
5326 match is_result
; /* Found result clause. */
5327 match found_match
; /* Status of whether we've found a good match. */
5328 char peek_char
; /* Character we're going to peek at. */
5329 bool allow_binding_name
;
5331 /* Initialize to having found nothing. */
5332 found_match
= MATCH_NO
;
5333 is_bind_c
= MATCH_NO
;
5334 is_result
= MATCH_NO
;
5336 /* Get the next char to narrow between result and bind(c). */
5337 gfc_gobble_whitespace ();
5338 peek_char
= gfc_peek_ascii_char ();
5340 /* C binding names are not allowed for internal procedures. */
5341 if (gfc_current_state () == COMP_CONTAINS
5342 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5343 allow_binding_name
= false;
5345 allow_binding_name
= true;
5350 /* Look for result clause. */
5351 is_result
= match_result (sym
, result
);
5352 if (is_result
== MATCH_YES
)
5354 /* Now see if there is a bind(c) after it. */
5355 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5356 /* We've found the result clause and possibly bind(c). */
5357 found_match
= MATCH_YES
;
5360 /* This should only be MATCH_ERROR. */
5361 found_match
= is_result
;
5364 /* Look for bind(c) first. */
5365 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5366 if (is_bind_c
== MATCH_YES
)
5368 /* Now see if a result clause followed it. */
5369 is_result
= match_result (sym
, result
);
5370 found_match
= MATCH_YES
;
5374 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
5375 found_match
= MATCH_ERROR
;
5379 gfc_error ("Unexpected junk after function declaration at %C");
5380 found_match
= MATCH_ERROR
;
5384 if (is_bind_c
== MATCH_YES
)
5386 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
5387 if (gfc_current_state () == COMP_CONTAINS
5388 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5389 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5390 "at %L may not be specified for an internal "
5391 "procedure", &gfc_current_locus
))
5394 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
5402 /* Procedure pointer return value without RESULT statement:
5403 Add "hidden" result variable named "ppr@". */
5406 add_hidden_procptr_result (gfc_symbol
*sym
)
5410 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
5413 /* First usage case: PROCEDURE and EXTERNAL statements. */
5414 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
5415 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
5416 && sym
->attr
.external
;
5417 /* Second usage case: INTERFACE statements. */
5418 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
5419 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
5420 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
5426 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
5430 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
5431 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
5432 st2
->n
.sym
= stree
->n
.sym
;
5433 stree
->n
.sym
->refs
++;
5435 sym
->result
= stree
->n
.sym
;
5437 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
5438 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
5439 sym
->result
->attr
.external
= sym
->attr
.external
;
5440 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
5441 sym
->result
->ts
= sym
->ts
;
5442 sym
->attr
.proc_pointer
= 0;
5443 sym
->attr
.pointer
= 0;
5444 sym
->attr
.external
= 0;
5445 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
5447 sym
->result
->attr
.pointer
= 0;
5448 sym
->result
->attr
.proc_pointer
= 1;
5451 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
5453 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5454 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
5455 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
5456 && sym
== gfc_current_ns
->proc_name
5457 && sym
== sym
->result
->ns
->proc_name
5458 && strcmp ("ppr@", sym
->result
->name
) == 0)
5460 sym
->result
->attr
.proc_pointer
= 1;
5461 sym
->attr
.pointer
= 0;
5469 /* Match the interface for a PROCEDURE declaration,
5470 including brackets (R1212). */
5473 match_procedure_interface (gfc_symbol
**proc_if
)
5477 locus old_loc
, entry_loc
;
5478 gfc_namespace
*old_ns
= gfc_current_ns
;
5479 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5481 old_loc
= entry_loc
= gfc_current_locus
;
5482 gfc_clear_ts (¤t_ts
);
5484 if (gfc_match (" (") != MATCH_YES
)
5486 gfc_current_locus
= entry_loc
;
5490 /* Get the type spec. for the procedure interface. */
5491 old_loc
= gfc_current_locus
;
5492 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
5493 gfc_gobble_whitespace ();
5494 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
5497 if (m
== MATCH_ERROR
)
5500 /* Procedure interface is itself a procedure. */
5501 gfc_current_locus
= old_loc
;
5502 m
= gfc_match_name (name
);
5504 /* First look to see if it is already accessible in the current
5505 namespace because it is use associated or contained. */
5507 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
5510 /* If it is still not found, then try the parent namespace, if it
5511 exists and create the symbol there if it is still not found. */
5512 if (gfc_current_ns
->parent
)
5513 gfc_current_ns
= gfc_current_ns
->parent
;
5514 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
5517 gfc_current_ns
= old_ns
;
5518 *proc_if
= st
->n
.sym
;
5523 /* Resolve interface if possible. That way, attr.procedure is only set
5524 if it is declared by a later procedure-declaration-stmt, which is
5525 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5526 while ((*proc_if
)->ts
.interface
5527 && *proc_if
!= (*proc_if
)->ts
.interface
)
5528 *proc_if
= (*proc_if
)->ts
.interface
;
5530 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
5531 && (*proc_if
)->ts
.type
== BT_UNKNOWN
5532 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
5533 (*proc_if
)->name
, NULL
))
5538 if (gfc_match (" )") != MATCH_YES
)
5540 gfc_current_locus
= entry_loc
;
5548 /* Match a PROCEDURE declaration (R1211). */
5551 match_procedure_decl (void)
5554 gfc_symbol
*sym
, *proc_if
= NULL
;
5556 gfc_expr
*initializer
= NULL
;
5558 /* Parse interface (with brackets). */
5559 m
= match_procedure_interface (&proc_if
);
5563 /* Parse attributes (with colons). */
5564 m
= match_attr_spec();
5565 if (m
== MATCH_ERROR
)
5568 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
5570 current_attr
.is_bind_c
= 1;
5571 has_name_equals
= 0;
5572 curr_binding_label
= NULL
;
5575 /* Get procedure symbols. */
5578 m
= gfc_match_symbol (&sym
, 0);
5581 else if (m
== MATCH_ERROR
)
5584 /* Add current_attr to the symbol attributes. */
5585 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
5588 if (sym
->attr
.is_bind_c
)
5590 /* Check for C1218. */
5591 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
5593 gfc_error ("BIND(C) attribute at %C requires "
5594 "an interface with BIND(C)");
5597 /* Check for C1217. */
5598 if (has_name_equals
&& sym
->attr
.pointer
)
5600 gfc_error ("BIND(C) procedure with NAME may not have "
5601 "POINTER attribute at %C");
5604 if (has_name_equals
&& sym
->attr
.dummy
)
5606 gfc_error ("Dummy procedure at %C may not have "
5607 "BIND(C) attribute with NAME");
5610 /* Set binding label for BIND(C). */
5611 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
5615 if (!gfc_add_external (&sym
->attr
, NULL
))
5618 if (add_hidden_procptr_result (sym
))
5621 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
5624 /* Set interface. */
5625 if (proc_if
!= NULL
)
5627 if (sym
->ts
.type
!= BT_UNKNOWN
)
5629 gfc_error ("Procedure %qs at %L already has basic type of %s",
5630 sym
->name
, &gfc_current_locus
,
5631 gfc_basic_typename (sym
->ts
.type
));
5634 sym
->ts
.interface
= proc_if
;
5635 sym
->attr
.untyped
= 1;
5636 sym
->attr
.if_source
= IFSRC_IFBODY
;
5638 else if (current_ts
.type
!= BT_UNKNOWN
)
5640 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5642 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5643 sym
->ts
.interface
->ts
= current_ts
;
5644 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5645 sym
->ts
.interface
->attr
.function
= 1;
5646 sym
->attr
.function
= 1;
5647 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
5650 if (gfc_match (" =>") == MATCH_YES
)
5652 if (!current_attr
.pointer
)
5654 gfc_error ("Initialization at %C isn't for a pointer variable");
5659 m
= match_pointer_init (&initializer
, 1);
5663 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
5668 if (gfc_match_eos () == MATCH_YES
)
5670 if (gfc_match_char (',') != MATCH_YES
)
5675 gfc_error ("Syntax error in PROCEDURE statement at %C");
5679 /* Free stuff up and return. */
5680 gfc_free_expr (initializer
);
5686 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
5689 /* Match a procedure pointer component declaration (R445). */
5692 match_ppc_decl (void)
5695 gfc_symbol
*proc_if
= NULL
;
5699 gfc_expr
*initializer
= NULL
;
5700 gfc_typebound_proc
* tb
;
5701 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5703 /* Parse interface (with brackets). */
5704 m
= match_procedure_interface (&proc_if
);
5708 /* Parse attributes. */
5709 tb
= XCNEW (gfc_typebound_proc
);
5710 tb
->where
= gfc_current_locus
;
5711 m
= match_binding_attributes (tb
, false, true);
5712 if (m
== MATCH_ERROR
)
5715 gfc_clear_attr (¤t_attr
);
5716 current_attr
.procedure
= 1;
5717 current_attr
.proc_pointer
= 1;
5718 current_attr
.access
= tb
->access
;
5719 current_attr
.flavor
= FL_PROCEDURE
;
5721 /* Match the colons (required). */
5722 if (gfc_match (" ::") != MATCH_YES
)
5724 gfc_error ("Expected %<::%> after binding-attributes at %C");
5728 /* Check for C450. */
5729 if (!tb
->nopass
&& proc_if
== NULL
)
5731 gfc_error("NOPASS or explicit interface required at %C");
5735 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5738 /* Match PPC names. */
5742 m
= gfc_match_name (name
);
5745 else if (m
== MATCH_ERROR
)
5748 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5751 /* Add current_attr to the symbol attributes. */
5752 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5755 if (!gfc_add_external (&c
->attr
, NULL
))
5758 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5765 c
->tb
= XCNEW (gfc_typebound_proc
);
5766 c
->tb
->where
= gfc_current_locus
;
5770 /* Set interface. */
5771 if (proc_if
!= NULL
)
5773 c
->ts
.interface
= proc_if
;
5774 c
->attr
.untyped
= 1;
5775 c
->attr
.if_source
= IFSRC_IFBODY
;
5777 else if (ts
.type
!= BT_UNKNOWN
)
5780 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5781 c
->ts
.interface
->result
= c
->ts
.interface
;
5782 c
->ts
.interface
->ts
= ts
;
5783 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5784 c
->ts
.interface
->attr
.function
= 1;
5785 c
->attr
.function
= 1;
5786 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5789 if (gfc_match (" =>") == MATCH_YES
)
5791 m
= match_pointer_init (&initializer
, 1);
5794 gfc_free_expr (initializer
);
5797 c
->initializer
= initializer
;
5800 if (gfc_match_eos () == MATCH_YES
)
5802 if (gfc_match_char (',') != MATCH_YES
)
5807 gfc_error ("Syntax error in procedure pointer component at %C");
5812 /* Match a PROCEDURE declaration inside an interface (R1206). */
5815 match_procedure_in_interface (void)
5819 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5822 if (current_interface
.type
== INTERFACE_NAMELESS
5823 || current_interface
.type
== INTERFACE_ABSTRACT
)
5825 gfc_error ("PROCEDURE at %C must be in a generic interface");
5829 /* Check if the F2008 optional double colon appears. */
5830 gfc_gobble_whitespace ();
5831 old_locus
= gfc_current_locus
;
5832 if (gfc_match ("::") == MATCH_YES
)
5834 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5835 "MODULE PROCEDURE statement at %L", &old_locus
))
5839 gfc_current_locus
= old_locus
;
5843 m
= gfc_match_name (name
);
5846 else if (m
== MATCH_ERROR
)
5848 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5851 if (!gfc_add_interface (sym
))
5854 if (gfc_match_eos () == MATCH_YES
)
5856 if (gfc_match_char (',') != MATCH_YES
)
5863 gfc_error ("Syntax error in PROCEDURE statement at %C");
5868 /* General matcher for PROCEDURE declarations. */
5870 static match
match_procedure_in_type (void);
5873 gfc_match_procedure (void)
5877 switch (gfc_current_state ())
5882 case COMP_SUBMODULE
:
5883 case COMP_SUBROUTINE
:
5886 m
= match_procedure_decl ();
5888 case COMP_INTERFACE
:
5889 m
= match_procedure_in_interface ();
5892 m
= match_ppc_decl ();
5894 case COMP_DERIVED_CONTAINS
:
5895 m
= match_procedure_in_type ();
5904 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5911 /* Warn if a matched procedure has the same name as an intrinsic; this is
5912 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5913 parser-state-stack to find out whether we're in a module. */
5916 do_warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5920 in_module
= (gfc_state_stack
->previous
5921 && (gfc_state_stack
->previous
->state
== COMP_MODULE
5922 || gfc_state_stack
->previous
->state
== COMP_SUBMODULE
));
5924 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5928 /* Match a function declaration. */
5931 gfc_match_function_decl (void)
5933 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5934 gfc_symbol
*sym
, *result
;
5938 match found_match
; /* Status returned by match func. */
5940 if (gfc_current_state () != COMP_NONE
5941 && gfc_current_state () != COMP_INTERFACE
5942 && gfc_current_state () != COMP_CONTAINS
)
5945 gfc_clear_ts (¤t_ts
);
5947 old_loc
= gfc_current_locus
;
5949 m
= gfc_match_prefix (¤t_ts
);
5952 gfc_current_locus
= old_loc
;
5956 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5958 gfc_current_locus
= old_loc
;
5962 if (get_proc_name (name
, &sym
, false))
5965 if (add_hidden_procptr_result (sym
))
5968 if (current_attr
.module_procedure
)
5969 sym
->attr
.module_procedure
= 1;
5971 gfc_new_block
= sym
;
5973 m
= gfc_match_formal_arglist (sym
, 0, 0);
5976 gfc_error ("Expected formal argument list in function "
5977 "definition at %C");
5981 else if (m
== MATCH_ERROR
)
5986 /* According to the draft, the bind(c) and result clause can
5987 come in either order after the formal_arg_list (i.e., either
5988 can be first, both can exist together or by themselves or neither
5989 one). Therefore, the match_result can't match the end of the
5990 string, and check for the bind(c) or result clause in either order. */
5991 found_match
= gfc_match_eos ();
5993 /* Make sure that it isn't already declared as BIND(C). If it is, it
5994 must have been marked BIND(C) with a BIND(C) attribute and that is
5995 not allowed for procedures. */
5996 if (sym
->attr
.is_bind_c
== 1)
5998 sym
->attr
.is_bind_c
= 0;
5999 if (sym
->old_symbol
!= NULL
)
6000 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6001 "variables or common blocks",
6002 &(sym
->old_symbol
->declared_at
));
6004 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6005 "variables or common blocks", &gfc_current_locus
);
6008 if (found_match
!= MATCH_YES
)
6010 /* If we haven't found the end-of-statement, look for a suffix. */
6011 suffix_match
= gfc_match_suffix (sym
, &result
);
6012 if (suffix_match
== MATCH_YES
)
6013 /* Need to get the eos now. */
6014 found_match
= gfc_match_eos ();
6016 found_match
= suffix_match
;
6019 if(found_match
!= MATCH_YES
)
6023 /* Make changes to the symbol. */
6026 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
6029 if (!gfc_missing_attr (&sym
->attr
, NULL
))
6032 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
6034 if(!sym
->attr
.module_procedure
)
6040 /* Delay matching the function characteristics until after the
6041 specification block by signalling kind=-1. */
6042 sym
->declared_at
= old_loc
;
6043 if (current_ts
.type
!= BT_UNKNOWN
)
6044 current_ts
.kind
= -1;
6046 current_ts
.kind
= 0;
6050 if (current_ts
.type
!= BT_UNKNOWN
6051 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
6057 if (current_ts
.type
!= BT_UNKNOWN
6058 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
6060 sym
->result
= result
;
6063 /* Warn if this procedure has the same name as an intrinsic. */
6064 do_warn_intrinsic_shadow (sym
, true);
6070 gfc_current_locus
= old_loc
;
6075 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
6076 pass the name of the entry, rather than the gfc_current_block name, and
6077 to return false upon finding an existing global entry. */
6080 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
6084 enum gfc_symbol_type type
;
6086 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
6088 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
6089 name is a global identifier. */
6090 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
6092 s
= gfc_get_gsymbol (name
);
6094 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
6096 gfc_global_used (s
, where
);
6105 s
->ns
= gfc_current_ns
;
6109 /* Don't add the symbol multiple times. */
6111 && (!gfc_notification_std (GFC_STD_F2008
)
6112 || strcmp (name
, binding_label
) != 0))
6114 s
= gfc_get_gsymbol (binding_label
);
6116 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
6118 gfc_global_used (s
, where
);
6125 s
->binding_label
= binding_label
;
6128 s
->ns
= gfc_current_ns
;
6136 /* Match an ENTRY statement. */
6139 gfc_match_entry (void)
6144 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6145 gfc_compile_state state
;
6149 bool module_procedure
;
6153 m
= gfc_match_name (name
);
6157 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
6160 state
= gfc_current_state ();
6161 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
6166 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
6169 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
6171 case COMP_SUBMODULE
:
6172 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
6174 case COMP_BLOCK_DATA
:
6175 gfc_error ("ENTRY statement at %C cannot appear within "
6178 case COMP_INTERFACE
:
6179 gfc_error ("ENTRY statement at %C cannot appear within "
6182 case COMP_STRUCTURE
:
6183 gfc_error ("ENTRY statement at %C cannot appear within "
6184 "a STRUCTURE block");
6187 gfc_error ("ENTRY statement at %C cannot appear within "
6188 "a DERIVED TYPE block");
6191 gfc_error ("ENTRY statement at %C cannot appear within "
6192 "an IF-THEN block");
6195 case COMP_DO_CONCURRENT
:
6196 gfc_error ("ENTRY statement at %C cannot appear within "
6200 gfc_error ("ENTRY statement at %C cannot appear within "
6204 gfc_error ("ENTRY statement at %C cannot appear within "
6208 gfc_error ("ENTRY statement at %C cannot appear within "
6212 gfc_error ("ENTRY statement at %C cannot appear within "
6213 "a contained subprogram");
6216 gfc_error ("Unexpected ENTRY statement at %C");
6221 if ((state
== COMP_SUBROUTINE
|| state
== COMP_FUNCTION
)
6222 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
6224 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
6228 module_procedure
= gfc_current_ns
->parent
!= NULL
6229 && gfc_current_ns
->parent
->proc_name
6230 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
6233 if (gfc_current_ns
->parent
!= NULL
6234 && gfc_current_ns
->parent
->proc_name
6235 && !module_procedure
)
6237 gfc_error("ENTRY statement at %C cannot appear in a "
6238 "contained procedure");
6242 /* Module function entries need special care in get_proc_name
6243 because previous references within the function will have
6244 created symbols attached to the current namespace. */
6245 if (get_proc_name (name
, &entry
,
6246 gfc_current_ns
->parent
!= NULL
6247 && module_procedure
))
6250 proc
= gfc_current_block ();
6252 /* Make sure that it isn't already declared as BIND(C). If it is, it
6253 must have been marked BIND(C) with a BIND(C) attribute and that is
6254 not allowed for procedures. */
6255 if (entry
->attr
.is_bind_c
== 1)
6257 entry
->attr
.is_bind_c
= 0;
6258 if (entry
->old_symbol
!= NULL
)
6259 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6260 "variables or common blocks",
6261 &(entry
->old_symbol
->declared_at
));
6263 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6264 "variables or common blocks", &gfc_current_locus
);
6267 /* Check what next non-whitespace character is so we can tell if there
6268 is the required parens if we have a BIND(C). */
6269 old_loc
= gfc_current_locus
;
6270 gfc_gobble_whitespace ();
6271 peek_char
= gfc_peek_ascii_char ();
6273 if (state
== COMP_SUBROUTINE
)
6275 m
= gfc_match_formal_arglist (entry
, 0, 1);
6279 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
6280 never be an internal procedure. */
6281 is_bind_c
= gfc_match_bind_c (entry
, true);
6282 if (is_bind_c
== MATCH_ERROR
)
6284 if (is_bind_c
== MATCH_YES
)
6286 if (peek_char
!= '(')
6288 gfc_error ("Missing required parentheses before BIND(C) at %C");
6291 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
6292 &(entry
->declared_at
), 1))
6296 if (!gfc_current_ns
->parent
6297 && !add_global_entry (name
, entry
->binding_label
, true,
6301 /* An entry in a subroutine. */
6302 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
6303 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
6308 /* An entry in a function.
6309 We need to take special care because writing
6314 ENTRY f() RESULT (r)
6316 ENTRY f RESULT (r). */
6317 if (gfc_match_eos () == MATCH_YES
)
6319 gfc_current_locus
= old_loc
;
6320 /* Match the empty argument list, and add the interface to
6322 m
= gfc_match_formal_arglist (entry
, 0, 1);
6325 m
= gfc_match_formal_arglist (entry
, 0, 0);
6332 if (gfc_match_eos () == MATCH_YES
)
6334 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
6335 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
6338 entry
->result
= entry
;
6342 m
= gfc_match_suffix (entry
, &result
);
6344 gfc_syntax_error (ST_ENTRY
);
6350 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
6351 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
6352 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
6354 entry
->result
= result
;
6358 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
6359 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
6361 entry
->result
= entry
;
6365 if (!gfc_current_ns
->parent
6366 && !add_global_entry (name
, entry
->binding_label
, false,
6371 if (gfc_match_eos () != MATCH_YES
)
6373 gfc_syntax_error (ST_ENTRY
);
6377 entry
->attr
.recursive
= proc
->attr
.recursive
;
6378 entry
->attr
.elemental
= proc
->attr
.elemental
;
6379 entry
->attr
.pure
= proc
->attr
.pure
;
6381 el
= gfc_get_entry_list ();
6383 el
->next
= gfc_current_ns
->entries
;
6384 gfc_current_ns
->entries
= el
;
6386 el
->id
= el
->next
->id
+ 1;
6390 new_st
.op
= EXEC_ENTRY
;
6391 new_st
.ext
.entry
= el
;
6397 /* Match a subroutine statement, including optional prefixes. */
6400 gfc_match_subroutine (void)
6402 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6407 bool allow_binding_name
;
6409 if (gfc_current_state () != COMP_NONE
6410 && gfc_current_state () != COMP_INTERFACE
6411 && gfc_current_state () != COMP_CONTAINS
)
6414 m
= gfc_match_prefix (NULL
);
6418 m
= gfc_match ("subroutine% %n", name
);
6422 if (get_proc_name (name
, &sym
, false))
6425 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6426 the symbol existed before. */
6427 sym
->declared_at
= gfc_current_locus
;
6429 if (current_attr
.module_procedure
)
6430 sym
->attr
.module_procedure
= 1;
6432 if (add_hidden_procptr_result (sym
))
6435 gfc_new_block
= sym
;
6437 /* Check what next non-whitespace character is so we can tell if there
6438 is the required parens if we have a BIND(C). */
6439 gfc_gobble_whitespace ();
6440 peek_char
= gfc_peek_ascii_char ();
6442 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
6445 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
6448 /* Make sure that it isn't already declared as BIND(C). If it is, it
6449 must have been marked BIND(C) with a BIND(C) attribute and that is
6450 not allowed for procedures. */
6451 if (sym
->attr
.is_bind_c
== 1)
6453 sym
->attr
.is_bind_c
= 0;
6454 if (sym
->old_symbol
!= NULL
)
6455 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6456 "variables or common blocks",
6457 &(sym
->old_symbol
->declared_at
));
6459 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6460 "variables or common blocks", &gfc_current_locus
);
6463 /* C binding names are not allowed for internal procedures. */
6464 if (gfc_current_state () == COMP_CONTAINS
6465 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6466 allow_binding_name
= false;
6468 allow_binding_name
= true;
6470 /* Here, we are just checking if it has the bind(c) attribute, and if
6471 so, then we need to make sure it's all correct. If it doesn't,
6472 we still need to continue matching the rest of the subroutine line. */
6473 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
6474 if (is_bind_c
== MATCH_ERROR
)
6476 /* There was an attempt at the bind(c), but it was wrong. An
6477 error message should have been printed w/in the gfc_match_bind_c
6478 so here we'll just return the MATCH_ERROR. */
6482 if (is_bind_c
== MATCH_YES
)
6484 /* The following is allowed in the Fortran 2008 draft. */
6485 if (gfc_current_state () == COMP_CONTAINS
6486 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
6487 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
6488 "at %L may not be specified for an internal "
6489 "procedure", &gfc_current_locus
))
6492 if (peek_char
!= '(')
6494 gfc_error ("Missing required parentheses before BIND(C) at %C");
6497 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
6498 &(sym
->declared_at
), 1))
6502 if (gfc_match_eos () != MATCH_YES
)
6504 gfc_syntax_error (ST_SUBROUTINE
);
6508 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
6510 if(!sym
->attr
.module_procedure
)
6516 /* Warn if it has the same name as an intrinsic. */
6517 do_warn_intrinsic_shadow (sym
, false);
6523 /* Check that the NAME identifier in a BIND attribute or statement
6524 is conform to C identifier rules. */
6527 check_bind_name_identifier (char **name
)
6529 char *n
= *name
, *p
;
6531 /* Remove leading spaces. */
6535 /* On an empty string, free memory and set name to NULL. */
6543 /* Remove trailing spaces. */
6544 p
= n
+ strlen(n
) - 1;
6548 /* Insert the identifier into the symbol table. */
6553 /* Now check that identifier is valid under C rules. */
6556 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6561 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
6563 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6571 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6572 given, and set the binding label in either the given symbol (if not
6573 NULL), or in the current_ts. The symbol may be NULL because we may
6574 encounter the BIND(C) before the declaration itself. Return
6575 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6576 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6577 or MATCH_YES if the specifier was correct and the binding label and
6578 bind(c) fields were set correctly for the given symbol or the
6579 current_ts. If allow_binding_name is false, no binding name may be
6583 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
6585 char *binding_label
= NULL
;
6588 /* Initialize the flag that specifies whether we encountered a NAME=
6589 specifier or not. */
6590 has_name_equals
= 0;
6592 /* This much we have to be able to match, in this order, if
6593 there is a bind(c) label. */
6594 if (gfc_match (" bind ( c ") != MATCH_YES
)
6597 /* Now see if there is a binding label, or if we've reached the
6598 end of the bind(c) attribute without one. */
6599 if (gfc_match_char (',') == MATCH_YES
)
6601 if (gfc_match (" name = ") != MATCH_YES
)
6603 gfc_error ("Syntax error in NAME= specifier for binding label "
6605 /* should give an error message here */
6609 has_name_equals
= 1;
6611 if (gfc_match_init_expr (&e
) != MATCH_YES
)
6617 if (!gfc_simplify_expr(e
, 0))
6619 gfc_error ("NAME= specifier at %C should be a constant expression");
6624 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
6625 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
6627 gfc_error ("NAME= specifier at %C should be a scalar of "
6628 "default character kind");
6633 // Get a C string from the Fortran string constant
6634 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
6635 e
->value
.character
.length
);
6638 // Check that it is valid (old gfc_match_name_C)
6639 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
6643 /* Get the required right paren. */
6644 if (gfc_match_char (')') != MATCH_YES
)
6646 gfc_error ("Missing closing paren for binding label at %C");
6650 if (has_name_equals
&& !allow_binding_name
)
6652 gfc_error ("No binding name is allowed in BIND(C) at %C");
6656 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
6658 gfc_error ("For dummy procedure %s, no binding name is "
6659 "allowed in BIND(C) at %C", sym
->name
);
6664 /* Save the binding label to the symbol. If sym is null, we're
6665 probably matching the typespec attributes of a declaration and
6666 haven't gotten the name yet, and therefore, no symbol yet. */
6670 sym
->binding_label
= binding_label
;
6672 curr_binding_label
= binding_label
;
6674 else if (allow_binding_name
)
6676 /* No binding label, but if symbol isn't null, we
6677 can set the label for it here.
6678 If name="" or allow_binding_name is false, no C binding name is
6680 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
6681 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
6684 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
6685 && current_interface
.type
== INTERFACE_ABSTRACT
)
6687 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6695 /* Return nonzero if we're currently compiling a contained procedure. */
6698 contained_procedure (void)
6700 gfc_state_data
*s
= gfc_state_stack
;
6702 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
6703 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
6709 /* Set the kind of each enumerator. The kind is selected such that it is
6710 interoperable with the corresponding C enumeration type, making
6711 sure that -fshort-enums is honored. */
6716 enumerator_history
*current_history
= NULL
;
6720 if (max_enum
== NULL
|| enum_history
== NULL
)
6723 if (!flag_short_enums
)
6729 kind
= gfc_integer_kinds
[i
++].kind
;
6731 while (kind
< gfc_c_int_kind
6732 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6735 current_history
= enum_history
;
6736 while (current_history
!= NULL
)
6738 current_history
->sym
->ts
.kind
= kind
;
6739 current_history
= current_history
->next
;
6744 /* Match any of the various end-block statements. Returns the type of
6745 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6746 and END BLOCK statements cannot be replaced by a single END statement. */
6749 gfc_match_end (gfc_statement
*st
)
6751 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6752 gfc_compile_state state
;
6754 const char *block_name
;
6758 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6759 gfc_namespace
**nsp
;
6760 bool abreviated_modproc_decl
= false;
6761 bool got_matching_end
= false;
6763 old_loc
= gfc_current_locus
;
6764 if (gfc_match ("end") != MATCH_YES
)
6767 state
= gfc_current_state ();
6768 block_name
= gfc_current_block () == NULL
6769 ? NULL
: gfc_current_block ()->name
;
6773 case COMP_ASSOCIATE
:
6775 if (!strncmp (block_name
, "block@", strlen("block@")))
6780 case COMP_DERIVED_CONTAINS
:
6781 state
= gfc_state_stack
->previous
->state
;
6782 block_name
= gfc_state_stack
->previous
->sym
== NULL
6783 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6784 abreviated_modproc_decl
= gfc_state_stack
->previous
->sym
6785 && gfc_state_stack
->previous
->sym
->abr_modproc_decl
;
6792 if (!abreviated_modproc_decl
)
6793 abreviated_modproc_decl
= gfc_current_block ()
6794 && gfc_current_block ()->abr_modproc_decl
;
6800 *st
= ST_END_PROGRAM
;
6801 target
= " program";
6805 case COMP_SUBROUTINE
:
6806 *st
= ST_END_SUBROUTINE
;
6807 if (!abreviated_modproc_decl
)
6808 target
= " subroutine";
6810 target
= " procedure";
6811 eos_ok
= !contained_procedure ();
6815 *st
= ST_END_FUNCTION
;
6816 if (!abreviated_modproc_decl
)
6817 target
= " function";
6819 target
= " procedure";
6820 eos_ok
= !contained_procedure ();
6823 case COMP_BLOCK_DATA
:
6824 *st
= ST_END_BLOCK_DATA
;
6825 target
= " block data";
6830 *st
= ST_END_MODULE
;
6835 case COMP_SUBMODULE
:
6836 *st
= ST_END_SUBMODULE
;
6837 target
= " submodule";
6841 case COMP_INTERFACE
:
6842 *st
= ST_END_INTERFACE
;
6843 target
= " interface";
6859 case COMP_STRUCTURE
:
6860 *st
= ST_END_STRUCTURE
;
6861 target
= " structure";
6866 case COMP_DERIVED_CONTAINS
:
6872 case COMP_ASSOCIATE
:
6873 *st
= ST_END_ASSOCIATE
;
6874 target
= " associate";
6891 case COMP_DO_CONCURRENT
:
6898 *st
= ST_END_CRITICAL
;
6899 target
= " critical";
6904 case COMP_SELECT_TYPE
:
6905 *st
= ST_END_SELECT
;
6911 *st
= ST_END_FORALL
;
6926 last_initializer
= NULL
;
6928 gfc_free_enum_history ();
6932 gfc_error ("Unexpected END statement at %C");
6936 old_loc
= gfc_current_locus
;
6937 if (gfc_match_eos () == MATCH_YES
)
6939 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6941 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6942 "instead of %s statement at %L",
6943 abreviated_modproc_decl
? "END PROCEDURE"
6944 : gfc_ascii_statement(*st
), &old_loc
))
6949 /* We would have required END [something]. */
6950 gfc_error ("%s statement expected at %L",
6951 gfc_ascii_statement (*st
), &old_loc
);
6958 /* Verify that we've got the sort of end-block that we're expecting. */
6959 if (gfc_match (target
) != MATCH_YES
)
6961 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6962 ? "END PROCEDURE" : gfc_ascii_statement(*st
), &old_loc
);
6966 got_matching_end
= true;
6968 old_loc
= gfc_current_locus
;
6969 /* If we're at the end, make sure a block name wasn't required. */
6970 if (gfc_match_eos () == MATCH_YES
)
6973 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6974 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6975 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6981 gfc_error ("Expected block name of %qs in %s statement at %L",
6982 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6987 /* END INTERFACE has a special handler for its several possible endings. */
6988 if (*st
== ST_END_INTERFACE
)
6989 return gfc_match_end_interface ();
6991 /* We haven't hit the end of statement, so what is left must be an
6993 m
= gfc_match_space ();
6995 m
= gfc_match_name (name
);
6998 gfc_error ("Expected terminating name at %C");
7002 if (block_name
== NULL
)
7005 /* We have to pick out the declared submodule name from the composite
7006 required by F2008:11.2.3 para 2, which ends in the declared name. */
7007 if (state
== COMP_SUBMODULE
)
7008 block_name
= strchr (block_name
, '.') + 1;
7010 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
7012 gfc_error ("Expected label %qs for %s statement at %C", block_name
,
7013 gfc_ascii_statement (*st
));
7016 /* Procedure pointer as function result. */
7017 else if (strcmp (block_name
, "ppr@") == 0
7018 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
7020 gfc_error ("Expected label %qs for %s statement at %C",
7021 gfc_current_block ()->ns
->proc_name
->name
,
7022 gfc_ascii_statement (*st
));
7026 if (gfc_match_eos () == MATCH_YES
)
7030 gfc_syntax_error (*st
);
7033 gfc_current_locus
= old_loc
;
7035 /* If we are missing an END BLOCK, we created a half-ready namespace.
7036 Remove it from the parent namespace's sibling list. */
7038 while (state
== COMP_BLOCK
&& !got_matching_end
)
7040 parent_ns
= gfc_current_ns
->parent
;
7042 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
7048 if (ns
== gfc_current_ns
)
7050 if (prev_ns
== NULL
)
7053 prev_ns
->sibling
= ns
->sibling
;
7059 gfc_free_namespace (gfc_current_ns
);
7060 gfc_current_ns
= parent_ns
;
7061 gfc_state_stack
= gfc_state_stack
->previous
;
7062 state
= gfc_current_state ();
7070 /***************** Attribute declaration statements ****************/
7072 /* Set the attribute of a single variable. */
7077 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7080 /* Workaround -Wmaybe-uninitialized false positive during
7081 profiledbootstrap by initializing them. */
7082 gfc_symbol
*sym
= NULL
;
7088 m
= gfc_match_name (name
);
7092 if (find_special (name
, &sym
, false))
7095 if (!check_function_name (name
))
7101 var_locus
= gfc_current_locus
;
7103 /* Deal with possible array specification for certain attributes. */
7104 if (current_attr
.dimension
7105 || current_attr
.codimension
7106 || current_attr
.allocatable
7107 || current_attr
.pointer
7108 || current_attr
.target
)
7110 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
7111 !current_attr
.dimension
7112 && !current_attr
.pointer
7113 && !current_attr
.target
);
7114 if (m
== MATCH_ERROR
)
7117 if (current_attr
.dimension
&& m
== MATCH_NO
)
7119 gfc_error ("Missing array specification at %L in DIMENSION "
7120 "statement", &var_locus
);
7125 if (current_attr
.dimension
&& sym
->value
)
7127 gfc_error ("Dimensions specified for %s at %L after its "
7128 "initialization", sym
->name
, &var_locus
);
7133 if (current_attr
.codimension
&& m
== MATCH_NO
)
7135 gfc_error ("Missing array specification at %L in CODIMENSION "
7136 "statement", &var_locus
);
7141 if ((current_attr
.allocatable
|| current_attr
.pointer
)
7142 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
7144 gfc_error ("Array specification must be deferred at %L", &var_locus
);
7150 /* Update symbol table. DIMENSION attribute is set in
7151 gfc_set_array_spec(). For CLASS variables, this must be applied
7152 to the first component, or '_data' field. */
7153 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
7155 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
7163 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
7164 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
7171 if (sym
->ts
.type
== BT_CLASS
7172 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
7178 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
7184 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
7186 /* Fix the array spec. */
7187 m
= gfc_mod_pointee_as (sym
->as
);
7188 if (m
== MATCH_ERROR
)
7192 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
7198 if ((current_attr
.external
|| current_attr
.intrinsic
)
7199 && sym
->attr
.flavor
!= FL_PROCEDURE
7200 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
7206 add_hidden_procptr_result (sym
);
7211 gfc_free_array_spec (as
);
7216 /* Generic attribute declaration subroutine. Used for attributes that
7217 just have a list of names. */
7224 /* Gobble the optional double colon, by simply ignoring the result
7234 if (gfc_match_eos () == MATCH_YES
)
7240 if (gfc_match_char (',') != MATCH_YES
)
7242 gfc_error ("Unexpected character in variable list at %C");
7252 /* This routine matches Cray Pointer declarations of the form:
7253 pointer ( <pointer>, <pointee> )
7255 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
7256 The pointer, if already declared, should be an integer. Otherwise, we
7257 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
7258 be either a scalar, or an array declaration. No space is allocated for
7259 the pointee. For the statement
7260 pointer (ipt, ar(10))
7261 any subsequent uses of ar will be translated (in C-notation) as
7262 ar(i) => ((<type> *) ipt)(i)
7263 After gimplification, pointee variable will disappear in the code. */
7266 cray_pointer_decl (void)
7269 gfc_array_spec
*as
= NULL
;
7270 gfc_symbol
*cptr
; /* Pointer symbol. */
7271 gfc_symbol
*cpte
; /* Pointee symbol. */
7277 if (gfc_match_char ('(') != MATCH_YES
)
7279 gfc_error ("Expected %<(%> at %C");
7283 /* Match pointer. */
7284 var_locus
= gfc_current_locus
;
7285 gfc_clear_attr (¤t_attr
);
7286 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
7287 current_ts
.type
= BT_INTEGER
;
7288 current_ts
.kind
= gfc_index_integer_kind
;
7290 m
= gfc_match_symbol (&cptr
, 0);
7293 gfc_error ("Expected variable name at %C");
7297 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
7300 gfc_set_sym_referenced (cptr
);
7302 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
7304 cptr
->ts
.type
= BT_INTEGER
;
7305 cptr
->ts
.kind
= gfc_index_integer_kind
;
7307 else if (cptr
->ts
.type
!= BT_INTEGER
)
7309 gfc_error ("Cray pointer at %C must be an integer");
7312 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
7313 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
7314 " memory addresses require %d bytes",
7315 cptr
->ts
.kind
, gfc_index_integer_kind
);
7317 if (gfc_match_char (',') != MATCH_YES
)
7319 gfc_error ("Expected \",\" at %C");
7323 /* Match Pointee. */
7324 var_locus
= gfc_current_locus
;
7325 gfc_clear_attr (¤t_attr
);
7326 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
7327 current_ts
.type
= BT_UNKNOWN
;
7328 current_ts
.kind
= 0;
7330 m
= gfc_match_symbol (&cpte
, 0);
7333 gfc_error ("Expected variable name at %C");
7337 /* Check for an optional array spec. */
7338 m
= gfc_match_array_spec (&as
, true, false);
7339 if (m
== MATCH_ERROR
)
7341 gfc_free_array_spec (as
);
7344 else if (m
== MATCH_NO
)
7346 gfc_free_array_spec (as
);
7350 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
7353 gfc_set_sym_referenced (cpte
);
7355 if (cpte
->as
== NULL
)
7357 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
7358 gfc_internal_error ("Couldn't set Cray pointee array spec.");
7360 else if (as
!= NULL
)
7362 gfc_error ("Duplicate array spec for Cray pointee at %C");
7363 gfc_free_array_spec (as
);
7369 if (cpte
->as
!= NULL
)
7371 /* Fix array spec. */
7372 m
= gfc_mod_pointee_as (cpte
->as
);
7373 if (m
== MATCH_ERROR
)
7377 /* Point the Pointee at the Pointer. */
7378 cpte
->cp_pointer
= cptr
;
7380 if (gfc_match_char (')') != MATCH_YES
)
7382 gfc_error ("Expected \")\" at %C");
7385 m
= gfc_match_char (',');
7387 done
= true; /* Stop searching for more declarations. */
7391 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
7392 || gfc_match_eos () != MATCH_YES
)
7394 gfc_error ("Expected %<,%> or end of statement at %C");
7402 gfc_match_external (void)
7405 gfc_clear_attr (¤t_attr
);
7406 current_attr
.external
= 1;
7408 return attr_decl ();
7413 gfc_match_intent (void)
7417 /* This is not allowed within a BLOCK construct! */
7418 if (gfc_current_state () == COMP_BLOCK
)
7420 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
7424 intent
= match_intent_spec ();
7425 if (intent
== INTENT_UNKNOWN
)
7428 gfc_clear_attr (¤t_attr
);
7429 current_attr
.intent
= intent
;
7431 return attr_decl ();
7436 gfc_match_intrinsic (void)
7439 gfc_clear_attr (¤t_attr
);
7440 current_attr
.intrinsic
= 1;
7442 return attr_decl ();
7447 gfc_match_optional (void)
7449 /* This is not allowed within a BLOCK construct! */
7450 if (gfc_current_state () == COMP_BLOCK
)
7452 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
7456 gfc_clear_attr (¤t_attr
);
7457 current_attr
.optional
= 1;
7459 return attr_decl ();
7464 gfc_match_pointer (void)
7466 gfc_gobble_whitespace ();
7467 if (gfc_peek_ascii_char () == '(')
7469 if (!flag_cray_pointer
)
7471 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7475 return cray_pointer_decl ();
7479 gfc_clear_attr (¤t_attr
);
7480 current_attr
.pointer
= 1;
7482 return attr_decl ();
7488 gfc_match_allocatable (void)
7490 gfc_clear_attr (¤t_attr
);
7491 current_attr
.allocatable
= 1;
7493 return attr_decl ();
7498 gfc_match_codimension (void)
7500 gfc_clear_attr (¤t_attr
);
7501 current_attr
.codimension
= 1;
7503 return attr_decl ();
7508 gfc_match_contiguous (void)
7510 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
7513 gfc_clear_attr (¤t_attr
);
7514 current_attr
.contiguous
= 1;
7516 return attr_decl ();
7521 gfc_match_dimension (void)
7523 gfc_clear_attr (¤t_attr
);
7524 current_attr
.dimension
= 1;
7526 return attr_decl ();
7531 gfc_match_target (void)
7533 gfc_clear_attr (¤t_attr
);
7534 current_attr
.target
= 1;
7536 return attr_decl ();
7540 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7544 access_attr_decl (gfc_statement st
)
7546 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7547 interface_type type
;
7549 gfc_symbol
*sym
, *dt_sym
;
7550 gfc_intrinsic_op op
;
7553 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7558 m
= gfc_match_generic_spec (&type
, name
, &op
);
7561 if (m
== MATCH_ERROR
)
7566 case INTERFACE_NAMELESS
:
7567 case INTERFACE_ABSTRACT
:
7570 case INTERFACE_GENERIC
:
7571 case INTERFACE_DTIO
:
7573 if (type
== INTERFACE_DTIO
7574 && gfc_current_ns
->proc_name
7575 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
7577 gfc_find_symbol (name
, gfc_current_ns
, 0, &sym
);
7580 gfc_error ("The GENERIC DTIO INTERFACE at %C is not "
7581 "present in the MODULE '%s'",
7582 gfc_current_ns
->proc_name
->name
);
7587 if (gfc_get_symbol (name
, NULL
, &sym
))
7590 if (!gfc_add_access (&sym
->attr
,
7592 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
7596 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
7597 && !gfc_add_access (&dt_sym
->attr
,
7599 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
7605 case INTERFACE_INTRINSIC_OP
:
7606 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
7608 gfc_intrinsic_op other_op
;
7610 gfc_current_ns
->operator_access
[op
] =
7611 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7613 /* Handle the case if there is another op with the same
7614 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7615 other_op
= gfc_equivalent_op (op
);
7617 if (other_op
!= INTRINSIC_NONE
)
7618 gfc_current_ns
->operator_access
[other_op
] =
7619 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7624 gfc_error ("Access specification of the %s operator at %C has "
7625 "already been specified", gfc_op2string (op
));
7631 case INTERFACE_USER_OP
:
7632 uop
= gfc_get_uop (name
);
7634 if (uop
->access
== ACCESS_UNKNOWN
)
7636 uop
->access
= (st
== ST_PUBLIC
)
7637 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7641 gfc_error ("Access specification of the .%s. operator at %C "
7642 "has already been specified", sym
->name
);
7649 if (gfc_match_char (',') == MATCH_NO
)
7653 if (gfc_match_eos () != MATCH_YES
)
7658 gfc_syntax_error (st
);
7666 gfc_match_protected (void)
7671 if (!gfc_current_ns
->proc_name
7672 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
7674 gfc_error ("PROTECTED at %C only allowed in specification "
7675 "part of a module");
7680 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
7683 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7688 if (gfc_match_eos () == MATCH_YES
)
7693 m
= gfc_match_symbol (&sym
, 0);
7697 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7709 if (gfc_match_eos () == MATCH_YES
)
7711 if (gfc_match_char (',') != MATCH_YES
)
7718 gfc_error ("Syntax error in PROTECTED statement at %C");
7723 /* The PRIVATE statement is a bit weird in that it can be an attribute
7724 declaration, but also works as a standalone statement inside of a
7725 type declaration or a module. */
7728 gfc_match_private (gfc_statement
*st
)
7731 if (gfc_match ("private") != MATCH_YES
)
7734 if (gfc_current_state () != COMP_MODULE
7735 && !(gfc_current_state () == COMP_DERIVED
7736 && gfc_state_stack
->previous
7737 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
7738 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7739 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
7740 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
7742 gfc_error ("PRIVATE statement at %C is only allowed in the "
7743 "specification part of a module");
7747 if (gfc_current_state () == COMP_DERIVED
)
7749 if (gfc_match_eos () == MATCH_YES
)
7755 gfc_syntax_error (ST_PRIVATE
);
7759 if (gfc_match_eos () == MATCH_YES
)
7766 return access_attr_decl (ST_PRIVATE
);
7771 gfc_match_public (gfc_statement
*st
)
7774 if (gfc_match ("public") != MATCH_YES
)
7777 if (gfc_current_state () != COMP_MODULE
)
7779 gfc_error ("PUBLIC statement at %C is only allowed in the "
7780 "specification part of a module");
7784 if (gfc_match_eos () == MATCH_YES
)
7791 return access_attr_decl (ST_PUBLIC
);
7795 /* Workhorse for gfc_match_parameter. */
7805 m
= gfc_match_symbol (&sym
, 0);
7807 gfc_error ("Expected variable name at %C in PARAMETER statement");
7812 if (gfc_match_char ('=') == MATCH_NO
)
7814 gfc_error ("Expected = sign in PARAMETER statement at %C");
7818 m
= gfc_match_init_expr (&init
);
7820 gfc_error ("Expected expression at %C in PARAMETER statement");
7824 if (sym
->ts
.type
== BT_UNKNOWN
7825 && !gfc_set_default_type (sym
, 1, NULL
))
7831 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7832 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7840 gfc_error ("Initializing already initialized variable at %C");
7845 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7846 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7849 gfc_free_expr (init
);
7854 /* Match a parameter statement, with the weird syntax that these have. */
7857 gfc_match_parameter (void)
7859 const char *term
= " )%t";
7862 if (gfc_match_char ('(') == MATCH_NO
)
7864 /* With legacy PARAMETER statements, don't expect a terminating ')'. */
7865 if (!gfc_notify_std (GFC_STD_LEGACY
, "PARAMETER without '()' at %C"))
7876 if (gfc_match (term
) == MATCH_YES
)
7879 if (gfc_match_char (',') != MATCH_YES
)
7881 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7892 gfc_match_automatic (void)
7896 bool seen_symbol
= false;
7898 if (!flag_dec_static
)
7900 gfc_error ("AUTOMATIC at %C is a DEC extension, enable with "
7909 m
= gfc_match_symbol (&sym
, 0);
7919 if (!gfc_add_automatic (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7925 if (gfc_match_eos () == MATCH_YES
)
7927 if (gfc_match_char (',') != MATCH_YES
)
7933 gfc_error ("Expected entity-list in AUTOMATIC statement at %C");
7940 gfc_error ("Syntax error in AUTOMATIC statement at %C");
7946 gfc_match_static (void)
7950 bool seen_symbol
= false;
7952 if (!flag_dec_static
)
7954 gfc_error ("STATIC at %C is a DEC extension, enable with -fdec-static");
7962 m
= gfc_match_symbol (&sym
, 0);
7972 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7973 &gfc_current_locus
))
7979 if (gfc_match_eos () == MATCH_YES
)
7981 if (gfc_match_char (',') != MATCH_YES
)
7987 gfc_error ("Expected entity-list in STATIC statement at %C");
7994 gfc_error ("Syntax error in STATIC statement at %C");
7999 /* Save statements have a special syntax. */
8002 gfc_match_save (void)
8004 char n
[GFC_MAX_SYMBOL_LEN
+1];
8009 if (gfc_match_eos () == MATCH_YES
)
8011 if (gfc_current_ns
->seen_save
)
8013 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
8014 "follows previous SAVE statement"))
8018 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
8022 if (gfc_current_ns
->save_all
)
8024 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
8025 "blanket SAVE statement"))
8033 m
= gfc_match_symbol (&sym
, 0);
8037 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
8038 &gfc_current_locus
))
8049 m
= gfc_match (" / %n /", &n
);
8050 if (m
== MATCH_ERROR
)
8055 c
= gfc_get_common (n
, 0);
8058 gfc_current_ns
->seen_save
= 1;
8061 if (gfc_match_eos () == MATCH_YES
)
8063 if (gfc_match_char (',') != MATCH_YES
)
8070 gfc_error ("Syntax error in SAVE statement at %C");
8076 gfc_match_value (void)
8081 /* This is not allowed within a BLOCK construct! */
8082 if (gfc_current_state () == COMP_BLOCK
)
8084 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
8088 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
8091 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
8096 if (gfc_match_eos () == MATCH_YES
)
8101 m
= gfc_match_symbol (&sym
, 0);
8105 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
8117 if (gfc_match_eos () == MATCH_YES
)
8119 if (gfc_match_char (',') != MATCH_YES
)
8126 gfc_error ("Syntax error in VALUE statement at %C");
8132 gfc_match_volatile (void)
8137 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
8140 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
8145 if (gfc_match_eos () == MATCH_YES
)
8150 /* VOLATILE is special because it can be added to host-associated
8151 symbols locally. Except for coarrays. */
8152 m
= gfc_match_symbol (&sym
, 1);
8156 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
8157 for variable in a BLOCK which is defined outside of the BLOCK. */
8158 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
8160 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
8161 "%C, which is use-/host-associated", sym
->name
);
8164 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
8176 if (gfc_match_eos () == MATCH_YES
)
8178 if (gfc_match_char (',') != MATCH_YES
)
8185 gfc_error ("Syntax error in VOLATILE statement at %C");
8191 gfc_match_asynchronous (void)
8196 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
8199 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
8204 if (gfc_match_eos () == MATCH_YES
)
8209 /* ASYNCHRONOUS is special because it can be added to host-associated
8211 m
= gfc_match_symbol (&sym
, 1);
8215 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
8227 if (gfc_match_eos () == MATCH_YES
)
8229 if (gfc_match_char (',') != MATCH_YES
)
8236 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
8241 /* Match a module procedure statement in a submodule. */
8244 gfc_match_submod_proc (void)
8246 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8247 gfc_symbol
*sym
, *fsym
;
8249 gfc_formal_arglist
*formal
, *head
, *tail
;
8251 if (gfc_current_state () != COMP_CONTAINS
8252 || !(gfc_state_stack
->previous
8253 && (gfc_state_stack
->previous
->state
== COMP_SUBMODULE
8254 || gfc_state_stack
->previous
->state
== COMP_MODULE
)))
8257 m
= gfc_match (" module% procedure% %n", name
);
8261 if (!gfc_notify_std (GFC_STD_F2008
, "MODULE PROCEDURE declaration "
8265 if (get_proc_name (name
, &sym
, false))
8268 /* Make sure that the result field is appropriately filled, even though
8269 the result symbol will be replaced later on. */
8270 if (sym
->tlink
&& sym
->tlink
->attr
.function
)
8272 if (sym
->tlink
->result
8273 && sym
->tlink
->result
!= sym
->tlink
)
8274 sym
->result
= sym
->tlink
->result
;
8279 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
8280 the symbol existed before. */
8281 sym
->declared_at
= gfc_current_locus
;
8283 if (!sym
->attr
.module_procedure
)
8286 /* Signal match_end to expect "end procedure". */
8287 sym
->abr_modproc_decl
= 1;
8289 /* Change from IFSRC_IFBODY coming from the interface declaration. */
8290 sym
->attr
.if_source
= IFSRC_DECL
;
8292 gfc_new_block
= sym
;
8294 /* Make a new formal arglist with the symbols in the procedure
8297 for (formal
= sym
->formal
; formal
&& formal
->sym
; formal
= formal
->next
)
8299 if (formal
== sym
->formal
)
8300 head
= tail
= gfc_get_formal_arglist ();
8303 tail
->next
= gfc_get_formal_arglist ();
8307 if (gfc_copy_dummy_sym (&fsym
, formal
->sym
, 0))
8311 gfc_set_sym_referenced (fsym
);
8314 /* The dummy symbols get cleaned up, when the formal_namespace of the
8315 interface declaration is cleared. This allows us to add the
8316 explicit interface as is done for other type of procedure. */
8317 if (!gfc_add_explicit_interface (sym
, IFSRC_DECL
, head
,
8318 &gfc_current_locus
))
8321 if (gfc_match_eos () != MATCH_YES
)
8323 gfc_syntax_error (ST_MODULE_PROC
);
8330 gfc_free_formal_arglist (head
);
8335 /* Match a module procedure statement. Note that we have to modify
8336 symbols in the parent's namespace because the current one was there
8337 to receive symbols that are in an interface's formal argument list. */
8340 gfc_match_modproc (void)
8342 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8346 gfc_namespace
*module_ns
;
8347 gfc_interface
*old_interface_head
, *interface
;
8349 if (gfc_state_stack
->state
!= COMP_INTERFACE
8350 || gfc_state_stack
->previous
== NULL
8351 || current_interface
.type
== INTERFACE_NAMELESS
8352 || current_interface
.type
== INTERFACE_ABSTRACT
)
8354 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
8359 module_ns
= gfc_current_ns
->parent
;
8360 for (; module_ns
; module_ns
= module_ns
->parent
)
8361 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
8362 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
8363 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
8364 && !module_ns
->proc_name
->attr
.contained
))
8367 if (module_ns
== NULL
)
8370 /* Store the current state of the interface. We will need it if we
8371 end up with a syntax error and need to recover. */
8372 old_interface_head
= gfc_current_interface_head ();
8374 /* Check if the F2008 optional double colon appears. */
8375 gfc_gobble_whitespace ();
8376 old_locus
= gfc_current_locus
;
8377 if (gfc_match ("::") == MATCH_YES
)
8379 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
8380 "MODULE PROCEDURE statement at %L", &old_locus
))
8384 gfc_current_locus
= old_locus
;
8389 old_locus
= gfc_current_locus
;
8391 m
= gfc_match_name (name
);
8397 /* Check for syntax error before starting to add symbols to the
8398 current namespace. */
8399 if (gfc_match_eos () == MATCH_YES
)
8402 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8405 /* Now we're sure the syntax is valid, we process this item
8407 if (gfc_get_symbol (name
, module_ns
, &sym
))
8410 if (sym
->attr
.intrinsic
)
8412 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
8413 "PROCEDURE", &old_locus
);
8417 if (sym
->attr
.proc
!= PROC_MODULE
8418 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8421 if (!gfc_add_interface (sym
))
8424 sym
->attr
.mod_proc
= 1;
8425 sym
->declared_at
= old_locus
;
8434 /* Restore the previous state of the interface. */
8435 interface
= gfc_current_interface_head ();
8436 gfc_set_current_interface_head (old_interface_head
);
8438 /* Free the new interfaces. */
8439 while (interface
!= old_interface_head
)
8441 gfc_interface
*i
= interface
->next
;
8446 /* And issue a syntax error. */
8447 gfc_syntax_error (ST_MODULE_PROC
);
8452 /* Check a derived type that is being extended. */
8455 check_extended_derived_type (char *name
)
8457 gfc_symbol
*extended
;
8459 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
8461 gfc_error ("Ambiguous symbol in TYPE definition at %C");
8465 extended
= gfc_find_dt_in_generic (extended
);
8470 gfc_error ("Symbol %qs at %C has not been previously defined", name
);
8474 if (extended
->attr
.flavor
!= FL_DERIVED
)
8476 gfc_error ("%qs in EXTENDS expression at %C is not a "
8477 "derived type", name
);
8481 if (extended
->attr
.is_bind_c
)
8483 gfc_error ("%qs cannot be extended at %C because it "
8484 "is BIND(C)", extended
->name
);
8488 if (extended
->attr
.sequence
)
8490 gfc_error ("%qs cannot be extended at %C because it "
8491 "is a SEQUENCE type", extended
->name
);
8499 /* Match the optional attribute specifiers for a type declaration.
8500 Return MATCH_ERROR if an error is encountered in one of the handled
8501 attributes (public, private, bind(c)), MATCH_NO if what's found is
8502 not a handled attribute, and MATCH_YES otherwise. TODO: More error
8503 checking on attribute conflicts needs to be done. */
8506 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
8508 /* See if the derived type is marked as private. */
8509 if (gfc_match (" , private") == MATCH_YES
)
8511 if (gfc_current_state () != COMP_MODULE
)
8513 gfc_error ("Derived type at %C can only be PRIVATE in the "
8514 "specification part of a module");
8518 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
8521 else if (gfc_match (" , public") == MATCH_YES
)
8523 if (gfc_current_state () != COMP_MODULE
)
8525 gfc_error ("Derived type at %C can only be PUBLIC in the "
8526 "specification part of a module");
8530 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
8533 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
8535 /* If the type is defined to be bind(c) it then needs to make
8536 sure that all fields are interoperable. This will
8537 need to be a semantic check on the finished derived type.
8538 See 15.2.3 (lines 9-12) of F2003 draft. */
8539 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
8542 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
8544 else if (gfc_match (" , abstract") == MATCH_YES
)
8546 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
8549 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
8552 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
8554 if (!gfc_add_extension (attr
, &gfc_current_locus
))
8560 /* If we get here, something matched. */
8565 /* Common function for type declaration blocks similar to derived types, such
8566 as STRUCTURES and MAPs. Unlike derived types, a structure type
8567 does NOT have a generic symbol matching the name given by the user.
8568 STRUCTUREs can share names with variables and PARAMETERs so we must allow
8569 for the creation of an independent symbol.
8570 Other parameters are a message to prefix errors with, the name of the new
8571 type to be created, and the flavor to add to the resulting symbol. */
8574 get_struct_decl (const char *name
, sym_flavor fl
, locus
*decl
,
8575 gfc_symbol
**result
)
8580 gcc_assert (name
[0] == (char) TOUPPER (name
[0]));
8585 where
= gfc_current_locus
;
8587 if (gfc_get_symbol (name
, NULL
, &sym
))
8592 gfc_internal_error ("Failed to create structure type '%s' at %C", name
);
8596 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
8598 gfc_error ("Type definition of '%s' at %C was already defined at %L",
8599 sym
->name
, &sym
->declared_at
);
8603 sym
->declared_at
= where
;
8605 if (sym
->attr
.flavor
!= fl
8606 && !gfc_add_flavor (&sym
->attr
, fl
, sym
->name
, NULL
))
8609 if (!sym
->hash_value
)
8610 /* Set the hash for the compound name for this type. */
8611 sym
->hash_value
= gfc_hash_value (sym
);
8613 /* Normally the type is expected to have been completely parsed by the time
8614 a field declaration with this type is seen. For unions, maps, and nested
8615 structure declarations, we need to indicate that it is okay that we
8616 haven't seen any components yet. This will be updated after the structure
8618 sym
->attr
.zero_comp
= 0;
8620 /* Structures always act like derived-types with the SEQUENCE attribute */
8621 gfc_add_sequence (&sym
->attr
, sym
->name
, NULL
);
8623 if (result
) *result
= sym
;
8629 /* Match the opening of a MAP block. Like a struct within a union in C;
8630 behaves identical to STRUCTURE blocks. */
8633 gfc_match_map (void)
8635 /* Counter used to give unique internal names to map structures. */
8636 static unsigned int gfc_map_id
= 0;
8637 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8641 old_loc
= gfc_current_locus
;
8643 if (gfc_match_eos () != MATCH_YES
)
8645 gfc_error ("Junk after MAP statement at %C");
8646 gfc_current_locus
= old_loc
;
8650 /* Map blocks are anonymous so we make up unique names for the symbol table
8651 which are invalid Fortran identifiers. */
8652 snprintf (name
, GFC_MAX_SYMBOL_LEN
+ 1, "MM$%u", gfc_map_id
++);
8654 if (!get_struct_decl (name
, FL_STRUCT
, &old_loc
, &sym
))
8657 gfc_new_block
= sym
;
8663 /* Match the opening of a UNION block. */
8666 gfc_match_union (void)
8668 /* Counter used to give unique internal names to union types. */
8669 static unsigned int gfc_union_id
= 0;
8670 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8674 old_loc
= gfc_current_locus
;
8676 if (gfc_match_eos () != MATCH_YES
)
8678 gfc_error ("Junk after UNION statement at %C");
8679 gfc_current_locus
= old_loc
;
8683 /* Unions are anonymous so we make up unique names for the symbol table
8684 which are invalid Fortran identifiers. */
8685 snprintf (name
, GFC_MAX_SYMBOL_LEN
+ 1, "UU$%u", gfc_union_id
++);
8687 if (!get_struct_decl (name
, FL_UNION
, &old_loc
, &sym
))
8690 gfc_new_block
= sym
;
8696 /* Match the beginning of a STRUCTURE declaration. This is similar to
8697 matching the beginning of a derived type declaration with a few
8698 twists. The resulting type symbol has no access control or other
8699 interesting attributes. */
8702 gfc_match_structure_decl (void)
8704 /* Counter used to give unique internal names to anonymous structures. */
8705 static unsigned int gfc_structure_id
= 0;
8706 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8711 if (!flag_dec_structure
)
8713 gfc_error ("STRUCTURE at %C is a DEC extension, enable with "
8720 m
= gfc_match (" /%n/", name
);
8723 /* Non-nested structure declarations require a structure name. */
8724 if (!gfc_comp_struct (gfc_current_state ()))
8726 gfc_error ("Structure name expected in non-nested structure "
8727 "declaration at %C");
8730 /* This is an anonymous structure; make up a unique name for it
8731 (upper-case letters never make it to symbol names from the source).
8732 The important thing is initializing the type variable
8733 and setting gfc_new_symbol, which is immediately used by
8734 parse_structure () and variable_decl () to add components of
8736 snprintf (name
, GFC_MAX_SYMBOL_LEN
+ 1, "SS$%u", gfc_structure_id
++);
8739 where
= gfc_current_locus
;
8740 /* No field list allowed after non-nested structure declaration. */
8741 if (!gfc_comp_struct (gfc_current_state ())
8742 && gfc_match_eos () != MATCH_YES
)
8744 gfc_error ("Junk after non-nested STRUCTURE statement at %C");
8748 /* Make sure the name is not the name of an intrinsic type. */
8749 if (gfc_is_intrinsic_typename (name
))
8751 gfc_error ("Structure name '%s' at %C cannot be the same as an"
8752 " intrinsic type", name
);
8756 /* Store the actual type symbol for the structure with an upper-case first
8757 letter (an invalid Fortran identifier). */
8759 if (!get_struct_decl (gfc_dt_upper_string (name
), FL_STRUCT
, &where
, &sym
))
8762 gfc_new_block
= sym
;
8767 /* This function does some work to determine which matcher should be used to
8768 * match a statement beginning with "TYPE". This is used to disambiguate TYPE
8769 * as an alias for PRINT from derived type declarations, TYPE IS statements,
8770 * and derived type data declarations. */
8773 gfc_match_type (gfc_statement
*st
)
8775 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8779 /* Requires -fdec. */
8783 m
= gfc_match ("type");
8786 /* If we already have an error in the buffer, it is probably from failing to
8787 * match a derived type data declaration. Let it happen. */
8788 else if (gfc_error_flag_test ())
8791 old_loc
= gfc_current_locus
;
8794 /* If we see an attribute list before anything else it's definitely a derived
8795 * type declaration. */
8796 if (gfc_match (" ,") == MATCH_YES
|| gfc_match (" ::") == MATCH_YES
)
8798 gfc_current_locus
= old_loc
;
8799 *st
= ST_DERIVED_DECL
;
8800 return gfc_match_derived_decl ();
8803 /* By now "TYPE" has already been matched. If we do not see a name, this may
8804 * be something like "TYPE *" or "TYPE <fmt>". */
8805 m
= gfc_match_name (name
);
8808 /* Let print match if it can, otherwise throw an error from
8809 * gfc_match_derived_decl. */
8810 gfc_current_locus
= old_loc
;
8811 if (gfc_match_print () == MATCH_YES
)
8816 gfc_current_locus
= old_loc
;
8817 *st
= ST_DERIVED_DECL
;
8818 return gfc_match_derived_decl ();
8821 /* A derived type declaration requires an EOS. Without it, assume print. */
8822 m
= gfc_match_eos ();
8825 /* Check manually for TYPE IS (... - this is invalid print syntax. */
8826 if (strncmp ("is", name
, 3) == 0
8827 && gfc_match (" (", name
) == MATCH_YES
)
8829 gfc_current_locus
= old_loc
;
8830 gcc_assert (gfc_match (" is") == MATCH_YES
);
8832 return gfc_match_type_is ();
8834 gfc_current_locus
= old_loc
;
8836 return gfc_match_print ();
8840 /* By now we have "TYPE <name> <EOS>". Check first if the name is an
8841 * intrinsic typename - if so let gfc_match_derived_decl dump an error.
8842 * Otherwise if gfc_match_derived_decl fails it's probably an existing
8843 * symbol which can be printed. */
8844 gfc_current_locus
= old_loc
;
8845 m
= gfc_match_derived_decl ();
8846 if (gfc_is_intrinsic_typename (name
) || m
== MATCH_YES
)
8848 *st
= ST_DERIVED_DECL
;
8851 gfc_current_locus
= old_loc
;
8853 return gfc_match_print ();
8860 /* Match the beginning of a derived type declaration. If a type name
8861 was the result of a function, then it is possible to have a symbol
8862 already to be known as a derived type yet have no components. */
8865 gfc_match_derived_decl (void)
8867 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8868 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
8869 symbol_attribute attr
;
8870 gfc_symbol
*sym
, *gensym
;
8871 gfc_symbol
*extended
;
8873 match is_type_attr_spec
= MATCH_NO
;
8874 bool seen_attr
= false;
8875 gfc_interface
*intr
= NULL
, *head
;
8877 if (gfc_comp_struct (gfc_current_state ()))
8882 gfc_clear_attr (&attr
);
8887 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
8888 if (is_type_attr_spec
== MATCH_ERROR
)
8890 if (is_type_attr_spec
== MATCH_YES
)
8892 } while (is_type_attr_spec
== MATCH_YES
);
8894 /* Deal with derived type extensions. The extension attribute has
8895 been added to 'attr' but now the parent type must be found and
8898 extended
= check_extended_derived_type (parent
);
8900 if (parent
[0] && !extended
)
8903 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
8905 gfc_error ("Expected :: in TYPE definition at %C");
8909 m
= gfc_match (" %n%t", name
);
8913 /* Make sure the name is not the name of an intrinsic type. */
8914 if (gfc_is_intrinsic_typename (name
))
8916 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8921 if (gfc_get_symbol (name
, NULL
, &gensym
))
8924 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
8926 gfc_error ("Derived type name %qs at %C already has a basic type "
8927 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
8931 if (!gensym
->attr
.generic
8932 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
8935 if (!gensym
->attr
.function
8936 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
8939 sym
= gfc_find_dt_in_generic (gensym
);
8941 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
8943 gfc_error ("Derived type definition of %qs at %C has already been "
8944 "defined", sym
->name
);
8950 /* Use upper case to save the actual derived-type symbol. */
8951 gfc_get_symbol (gfc_dt_upper_string (gensym
->name
), NULL
, &sym
);
8952 sym
->name
= gfc_get_string ("%s", gensym
->name
);
8953 head
= gensym
->generic
;
8954 intr
= gfc_get_interface ();
8956 intr
->where
= gfc_current_locus
;
8957 intr
->sym
->declared_at
= gfc_current_locus
;
8959 gensym
->generic
= intr
;
8960 gensym
->attr
.if_source
= IFSRC_DECL
;
8963 /* The symbol may already have the derived attribute without the
8964 components. The ways this can happen is via a function
8965 definition, an INTRINSIC statement or a subtype in another
8966 derived type that is a pointer. The first part of the AND clause
8967 is true if the symbol is not the return value of a function. */
8968 if (sym
->attr
.flavor
!= FL_DERIVED
8969 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
8972 if (attr
.access
!= ACCESS_UNKNOWN
8973 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
8975 else if (sym
->attr
.access
== ACCESS_UNKNOWN
8976 && gensym
->attr
.access
!= ACCESS_UNKNOWN
8977 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
8981 if (sym
->attr
.access
!= ACCESS_UNKNOWN
8982 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
8983 gensym
->attr
.access
= sym
->attr
.access
;
8985 /* See if the derived type was labeled as bind(c). */
8986 if (attr
.is_bind_c
!= 0)
8987 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
8989 /* Construct the f2k_derived namespace if it is not yet there. */
8990 if (!sym
->f2k_derived
)
8991 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
8993 if (extended
&& !sym
->components
)
8997 /* Add the extended derived type as the first component. */
8998 gfc_add_component (sym
, parent
, &p
);
9000 gfc_set_sym_referenced (extended
);
9002 p
->ts
.type
= BT_DERIVED
;
9003 p
->ts
.u
.derived
= extended
;
9004 p
->initializer
= gfc_default_initializer (&p
->ts
);
9006 /* Set extension level. */
9007 if (extended
->attr
.extension
== 255)
9009 /* Since the extension field is 8 bit wide, we can only have
9010 up to 255 extension levels. */
9011 gfc_error ("Maximum extension level reached with type %qs at %L",
9012 extended
->name
, &extended
->declared_at
);
9015 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
9017 /* Provide the links between the extended type and its extension. */
9018 if (!extended
->f2k_derived
)
9019 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
9022 if (!sym
->hash_value
)
9023 /* Set the hash for the compound name for this type. */
9024 sym
->hash_value
= gfc_hash_value (sym
);
9026 /* Take over the ABSTRACT attribute. */
9027 sym
->attr
.abstract
= attr
.abstract
;
9029 gfc_new_block
= sym
;
9035 /* Cray Pointees can be declared as:
9036 pointer (ipt, a (n,m,...,*)) */
9039 gfc_mod_pointee_as (gfc_array_spec
*as
)
9041 as
->cray_pointee
= true; /* This will be useful to know later. */
9042 if (as
->type
== AS_ASSUMED_SIZE
)
9043 as
->cp_was_assumed
= true;
9044 else if (as
->type
== AS_ASSUMED_SHAPE
)
9046 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
9053 /* Match the enum definition statement, here we are trying to match
9054 the first line of enum definition statement.
9055 Returns MATCH_YES if match is found. */
9058 gfc_match_enum (void)
9062 m
= gfc_match_eos ();
9066 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
9073 /* Returns an initializer whose value is one higher than the value of the
9074 LAST_INITIALIZER argument. If the argument is NULL, the
9075 initializers value will be set to zero. The initializer's kind
9076 will be set to gfc_c_int_kind.
9078 If -fshort-enums is given, the appropriate kind will be selected
9079 later after all enumerators have been parsed. A warning is issued
9080 here if an initializer exceeds gfc_c_int_kind. */
9083 enum_initializer (gfc_expr
*last_initializer
, locus where
)
9086 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
9088 mpz_init (result
->value
.integer
);
9090 if (last_initializer
!= NULL
)
9092 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
9093 result
->where
= last_initializer
->where
;
9095 if (gfc_check_integer_range (result
->value
.integer
,
9096 gfc_c_int_kind
) != ARITH_OK
)
9098 gfc_error ("Enumerator exceeds the C integer type at %C");
9104 /* Control comes here, if it's the very first enumerator and no
9105 initializer has been given. It will be initialized to zero. */
9106 mpz_set_si (result
->value
.integer
, 0);
9113 /* Match a variable name with an optional initializer. When this
9114 subroutine is called, a variable is expected to be parsed next.
9115 Depending on what is happening at the moment, updates either the
9116 symbol table or the current interface. */
9119 enumerator_decl (void)
9121 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9122 gfc_expr
*initializer
;
9123 gfc_array_spec
*as
= NULL
;
9131 old_locus
= gfc_current_locus
;
9133 /* When we get here, we've just matched a list of attributes and
9134 maybe a type and a double colon. The next thing we expect to see
9135 is the name of the symbol. */
9136 m
= gfc_match_name (name
);
9140 var_locus
= gfc_current_locus
;
9142 /* OK, we've successfully matched the declaration. Now put the
9143 symbol in the current namespace. If we fail to create the symbol,
9145 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
9151 /* The double colon must be present in order to have initializers.
9152 Otherwise the statement is ambiguous with an assignment statement. */
9155 if (gfc_match_char ('=') == MATCH_YES
)
9157 m
= gfc_match_init_expr (&initializer
);
9160 gfc_error ("Expected an initialization expression at %C");
9169 /* If we do not have an initializer, the initialization value of the
9170 previous enumerator (stored in last_initializer) is incremented
9171 by 1 and is used to initialize the current enumerator. */
9172 if (initializer
== NULL
)
9173 initializer
= enum_initializer (last_initializer
, old_locus
);
9175 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
9177 gfc_error ("ENUMERATOR %L not initialized with integer expression",
9183 /* Store this current initializer, for the next enumerator variable
9184 to be parsed. add_init_expr_to_sym() zeros initializer, so we
9185 use last_initializer below. */
9186 last_initializer
= initializer
;
9187 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
9189 /* Maintain enumerator history. */
9190 gfc_find_symbol (name
, NULL
, 0, &sym
);
9191 create_enum_history (sym
, last_initializer
);
9193 return (t
) ? MATCH_YES
: MATCH_ERROR
;
9196 /* Free stuff up and return. */
9197 gfc_free_expr (initializer
);
9203 /* Match the enumerator definition statement. */
9206 gfc_match_enumerator_def (void)
9211 gfc_clear_ts (¤t_ts
);
9213 m
= gfc_match (" enumerator");
9217 m
= gfc_match (" :: ");
9218 if (m
== MATCH_ERROR
)
9221 colon_seen
= (m
== MATCH_YES
);
9223 if (gfc_current_state () != COMP_ENUM
)
9225 gfc_error ("ENUM definition statement expected before %C");
9226 gfc_free_enum_history ();
9230 (¤t_ts
)->type
= BT_INTEGER
;
9231 (¤t_ts
)->kind
= gfc_c_int_kind
;
9233 gfc_clear_attr (¤t_attr
);
9234 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
9243 m
= enumerator_decl ();
9244 if (m
== MATCH_ERROR
)
9246 gfc_free_enum_history ();
9252 if (gfc_match_eos () == MATCH_YES
)
9254 if (gfc_match_char (',') != MATCH_YES
)
9258 if (gfc_current_state () == COMP_ENUM
)
9260 gfc_free_enum_history ();
9261 gfc_error ("Syntax error in ENUMERATOR definition at %C");
9266 gfc_free_array_spec (current_as
);
9273 /* Match binding attributes. */
9276 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
9278 bool found_passing
= false;
9279 bool seen_ptr
= false;
9280 match m
= MATCH_YES
;
9282 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
9283 this case the defaults are in there. */
9284 ba
->access
= ACCESS_UNKNOWN
;
9285 ba
->pass_arg
= NULL
;
9286 ba
->pass_arg_num
= 0;
9288 ba
->non_overridable
= 0;
9292 /* If we find a comma, we believe there are binding attributes. */
9293 m
= gfc_match_char (',');
9299 /* Access specifier. */
9301 m
= gfc_match (" public");
9302 if (m
== MATCH_ERROR
)
9306 if (ba
->access
!= ACCESS_UNKNOWN
)
9308 gfc_error ("Duplicate access-specifier at %C");
9312 ba
->access
= ACCESS_PUBLIC
;
9316 m
= gfc_match (" private");
9317 if (m
== MATCH_ERROR
)
9321 if (ba
->access
!= ACCESS_UNKNOWN
)
9323 gfc_error ("Duplicate access-specifier at %C");
9327 ba
->access
= ACCESS_PRIVATE
;
9331 /* If inside GENERIC, the following is not allowed. */
9336 m
= gfc_match (" nopass");
9337 if (m
== MATCH_ERROR
)
9343 gfc_error ("Binding attributes already specify passing,"
9344 " illegal NOPASS at %C");
9348 found_passing
= true;
9353 /* PASS possibly including argument. */
9354 m
= gfc_match (" pass");
9355 if (m
== MATCH_ERROR
)
9359 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
9363 gfc_error ("Binding attributes already specify passing,"
9364 " illegal PASS at %C");
9368 m
= gfc_match (" ( %n )", arg
);
9369 if (m
== MATCH_ERROR
)
9372 ba
->pass_arg
= gfc_get_string ("%s", arg
);
9373 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
9375 found_passing
= true;
9383 m
= gfc_match (" pointer");
9384 if (m
== MATCH_ERROR
)
9390 gfc_error ("Duplicate POINTER attribute at %C");
9400 /* NON_OVERRIDABLE flag. */
9401 m
= gfc_match (" non_overridable");
9402 if (m
== MATCH_ERROR
)
9406 if (ba
->non_overridable
)
9408 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
9412 ba
->non_overridable
= 1;
9416 /* DEFERRED flag. */
9417 m
= gfc_match (" deferred");
9418 if (m
== MATCH_ERROR
)
9424 gfc_error ("Duplicate DEFERRED at %C");
9435 /* Nothing matching found. */
9437 gfc_error ("Expected access-specifier at %C");
9439 gfc_error ("Expected binding attribute at %C");
9442 while (gfc_match_char (',') == MATCH_YES
);
9444 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
9445 if (ba
->non_overridable
&& ba
->deferred
)
9447 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
9454 if (ba
->access
== ACCESS_UNKNOWN
)
9455 ba
->access
= gfc_typebound_default_access
;
9457 if (ppc
&& !seen_ptr
)
9459 gfc_error ("POINTER attribute is required for procedure pointer component"
9471 /* Match a PROCEDURE specific binding inside a derived type. */
9474 match_procedure_in_type (void)
9476 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9477 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
9478 char* target
= NULL
, *ifc
= NULL
;
9479 gfc_typebound_proc tb
;
9488 /* Check current state. */
9489 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
9490 block
= gfc_state_stack
->previous
->sym
;
9493 /* Try to match PROCEDURE(interface). */
9494 if (gfc_match (" (") == MATCH_YES
)
9496 m
= gfc_match_name (target_buf
);
9497 if (m
== MATCH_ERROR
)
9501 gfc_error ("Interface-name expected after %<(%> at %C");
9505 if (gfc_match (" )") != MATCH_YES
)
9507 gfc_error ("%<)%> expected at %C");
9514 /* Construct the data structure. */
9515 memset (&tb
, 0, sizeof (tb
));
9516 tb
.where
= gfc_current_locus
;
9518 /* Match binding attributes. */
9519 m
= match_binding_attributes (&tb
, false, false);
9520 if (m
== MATCH_ERROR
)
9522 seen_attrs
= (m
== MATCH_YES
);
9524 /* Check that attribute DEFERRED is given if an interface is specified. */
9525 if (tb
.deferred
&& !ifc
)
9527 gfc_error ("Interface must be specified for DEFERRED binding at %C");
9530 if (ifc
&& !tb
.deferred
)
9532 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
9536 /* Match the colons. */
9537 m
= gfc_match (" ::");
9538 if (m
== MATCH_ERROR
)
9540 seen_colons
= (m
== MATCH_YES
);
9541 if (seen_attrs
&& !seen_colons
)
9543 gfc_error ("Expected %<::%> after binding-attributes at %C");
9547 /* Match the binding names. */
9550 m
= gfc_match_name (name
);
9551 if (m
== MATCH_ERROR
)
9555 gfc_error ("Expected binding name at %C");
9559 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
9562 /* Try to match the '=> target', if it's there. */
9564 m
= gfc_match (" =>");
9565 if (m
== MATCH_ERROR
)
9571 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
9577 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
9582 m
= gfc_match_name (target_buf
);
9583 if (m
== MATCH_ERROR
)
9587 gfc_error ("Expected binding target after %<=>%> at %C");
9590 target
= target_buf
;
9593 /* If no target was found, it has the same name as the binding. */
9597 /* Get the namespace to insert the symbols into. */
9598 ns
= block
->f2k_derived
;
9601 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
9602 if (tb
.deferred
&& !block
->attr
.abstract
)
9604 gfc_error ("Type %qs containing DEFERRED binding at %C "
9605 "is not ABSTRACT", block
->name
);
9609 /* See if we already have a binding with this name in the symtree which
9610 would be an error. If a GENERIC already targeted this binding, it may
9611 be already there but then typebound is still NULL. */
9612 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
9613 if (stree
&& stree
->n
.tb
)
9615 gfc_error ("There is already a procedure with binding name %qs for "
9616 "the derived type %qs at %C", name
, block
->name
);
9620 /* Insert it and set attributes. */
9624 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
9627 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
9629 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
9632 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
9633 gfc_add_flavor(&stree
->n
.tb
->u
.specific
->n
.sym
->attr
, FL_PROCEDURE
,
9634 target
, &stree
->n
.tb
->u
.specific
->n
.sym
->declared_at
);
9636 if (gfc_match_eos () == MATCH_YES
)
9638 if (gfc_match_char (',') != MATCH_YES
)
9643 gfc_error ("Syntax error in PROCEDURE statement at %C");
9648 /* Match a GENERIC procedure binding inside a derived type. */
9651 gfc_match_generic (void)
9653 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9654 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
9656 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
9657 gfc_typebound_proc
* tb
;
9659 interface_type op_type
;
9660 gfc_intrinsic_op op
;
9663 /* Check current state. */
9664 if (gfc_current_state () == COMP_DERIVED
)
9666 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
9669 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
9671 block
= gfc_state_stack
->previous
->sym
;
9672 ns
= block
->f2k_derived
;
9673 gcc_assert (block
&& ns
);
9675 memset (&tbattr
, 0, sizeof (tbattr
));
9676 tbattr
.where
= gfc_current_locus
;
9678 /* See if we get an access-specifier. */
9679 m
= match_binding_attributes (&tbattr
, true, false);
9680 if (m
== MATCH_ERROR
)
9683 /* Now the colons, those are required. */
9684 if (gfc_match (" ::") != MATCH_YES
)
9686 gfc_error ("Expected %<::%> at %C");
9690 /* Match the binding name; depending on type (operator / generic) format
9691 it for future error messages into bind_name. */
9693 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
9694 if (m
== MATCH_ERROR
)
9698 gfc_error ("Expected generic name or operator descriptor at %C");
9704 case INTERFACE_GENERIC
:
9705 case INTERFACE_DTIO
:
9706 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
9709 case INTERFACE_USER_OP
:
9710 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
9713 case INTERFACE_INTRINSIC_OP
:
9714 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
9715 gfc_op2string (op
));
9718 case INTERFACE_NAMELESS
:
9719 gfc_error ("Malformed GENERIC statement at %C");
9727 /* Match the required =>. */
9728 if (gfc_match (" =>") != MATCH_YES
)
9730 gfc_error ("Expected %<=>%> at %C");
9734 /* Try to find existing GENERIC binding with this name / for this operator;
9735 if there is something, check that it is another GENERIC and then extend
9736 it rather than building a new node. Otherwise, create it and put it
9737 at the right position. */
9741 case INTERFACE_DTIO
:
9742 case INTERFACE_USER_OP
:
9743 case INTERFACE_GENERIC
:
9745 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
9748 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
9749 tb
= st
? st
->n
.tb
: NULL
;
9753 case INTERFACE_INTRINSIC_OP
:
9763 if (!tb
->is_generic
)
9765 gcc_assert (op_type
== INTERFACE_GENERIC
);
9766 gfc_error ("There's already a non-generic procedure with binding name"
9767 " %qs for the derived type %qs at %C",
9768 bind_name
, block
->name
);
9772 if (tb
->access
!= tbattr
.access
)
9774 gfc_error ("Binding at %C must have the same access as already"
9775 " defined binding %qs", bind_name
);
9781 tb
= gfc_get_typebound_proc (NULL
);
9782 tb
->where
= gfc_current_locus
;
9783 tb
->access
= tbattr
.access
;
9785 tb
->u
.generic
= NULL
;
9789 case INTERFACE_DTIO
:
9790 case INTERFACE_GENERIC
:
9791 case INTERFACE_USER_OP
:
9793 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
9794 gfc_symtree
* st
= gfc_get_tbp_symtree (is_op
? &ns
->tb_uop_root
:
9795 &ns
->tb_sym_root
, name
);
9802 case INTERFACE_INTRINSIC_OP
:
9811 /* Now, match all following names as specific targets. */
9814 gfc_symtree
* target_st
;
9815 gfc_tbp_generic
* target
;
9817 m
= gfc_match_name (name
);
9818 if (m
== MATCH_ERROR
)
9822 gfc_error ("Expected specific binding name at %C");
9826 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
9828 /* See if this is a duplicate specification. */
9829 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
9830 if (target_st
== target
->specific_st
)
9832 gfc_error ("%qs already defined as specific binding for the"
9833 " generic %qs at %C", name
, bind_name
);
9837 target
= gfc_get_tbp_generic ();
9838 target
->specific_st
= target_st
;
9839 target
->specific
= NULL
;
9840 target
->next
= tb
->u
.generic
;
9841 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
9842 || (op_type
== INTERFACE_INTRINSIC_OP
));
9843 tb
->u
.generic
= target
;
9845 while (gfc_match (" ,") == MATCH_YES
);
9847 /* Here should be the end. */
9848 if (gfc_match_eos () != MATCH_YES
)
9850 gfc_error ("Junk after GENERIC binding at %C");
9861 /* Match a FINAL declaration inside a derived type. */
9864 gfc_match_final_decl (void)
9866 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9869 gfc_namespace
* module_ns
;
9873 if (gfc_current_form
== FORM_FREE
)
9875 char c
= gfc_peek_ascii_char ();
9876 if (!gfc_is_whitespace (c
) && c
!= ':')
9880 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
9882 if (gfc_current_form
== FORM_FIXED
)
9885 gfc_error ("FINAL declaration at %C must be inside a derived type "
9886 "CONTAINS section");
9890 block
= gfc_state_stack
->previous
->sym
;
9893 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
9894 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
9896 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9897 " specification part of a MODULE");
9901 module_ns
= gfc_current_ns
;
9902 gcc_assert (module_ns
);
9903 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
9905 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9906 if (gfc_match (" ::") == MATCH_ERROR
)
9909 /* Match the sequence of procedure names. */
9916 if (first
&& gfc_match_eos () == MATCH_YES
)
9918 gfc_error ("Empty FINAL at %C");
9922 m
= gfc_match_name (name
);
9925 gfc_error ("Expected module procedure name at %C");
9928 else if (m
!= MATCH_YES
)
9931 if (gfc_match_eos () == MATCH_YES
)
9933 if (!last
&& gfc_match_char (',') != MATCH_YES
)
9935 gfc_error ("Expected %<,%> at %C");
9939 if (gfc_get_symbol (name
, module_ns
, &sym
))
9941 gfc_error ("Unknown procedure name %qs at %C", name
);
9945 /* Mark the symbol as module procedure. */
9946 if (sym
->attr
.proc
!= PROC_MODULE
9947 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
9950 /* Check if we already have this symbol in the list, this is an error. */
9951 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
9952 if (f
->proc_sym
== sym
)
9954 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9959 /* Add this symbol to the list of finalizers. */
9960 gcc_assert (block
->f2k_derived
);
9962 f
= XCNEW (gfc_finalizer
);
9964 f
->proc_tree
= NULL
;
9965 f
->where
= gfc_current_locus
;
9966 f
->next
= block
->f2k_derived
->finalizers
;
9967 block
->f2k_derived
->finalizers
= f
;
9977 const ext_attr_t ext_attr_list
[] = {
9978 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
9979 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
9980 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
9981 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
9982 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
9983 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
9984 { NULL
, EXT_ATTR_LAST
, NULL
}
9987 /* Match a !GCC$ ATTRIBUTES statement of the form:
9988 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9989 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9991 TODO: We should support all GCC attributes using the same syntax for
9992 the attribute list, i.e. the list in C
9993 __attributes(( attribute-list ))
9995 !GCC$ ATTRIBUTES attribute-list ::
9996 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9999 As there is absolutely no risk of confusion, we should never return
10002 gfc_match_gcc_attributes (void)
10004 symbol_attribute attr
;
10005 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
10010 gfc_clear_attr (&attr
);
10015 if (gfc_match_name (name
) != MATCH_YES
)
10016 return MATCH_ERROR
;
10018 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
10019 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
10022 if (id
== EXT_ATTR_LAST
)
10024 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
10025 return MATCH_ERROR
;
10028 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
10029 return MATCH_ERROR
;
10031 gfc_gobble_whitespace ();
10032 ch
= gfc_next_ascii_char ();
10035 /* This is the successful exit condition for the loop. */
10036 if (gfc_next_ascii_char () == ':')
10046 if (gfc_match_eos () == MATCH_YES
)
10051 m
= gfc_match_name (name
);
10052 if (m
!= MATCH_YES
)
10055 if (find_special (name
, &sym
, true))
10056 return MATCH_ERROR
;
10058 sym
->attr
.ext_attr
|= attr
.ext_attr
;
10060 if (gfc_match_eos () == MATCH_YES
)
10063 if (gfc_match_char (',') != MATCH_YES
)
10070 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
10071 return MATCH_ERROR
;