1 /* Declaration statement matcher
2 Copyright (C) 2002-2014 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"
28 #include "constructor.h"
30 #include "stringpool.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
)
182 static match
var_element (gfc_data_variable
*);
184 /* Match a list of variables terminated by an iterator and a right
188 var_list (gfc_data_variable
*parent
)
190 gfc_data_variable
*tail
, var
;
193 m
= var_element (&var
);
194 if (m
== MATCH_ERROR
)
199 tail
= gfc_get_data_variable ();
206 if (gfc_match_char (',') != MATCH_YES
)
209 m
= gfc_match_iterator (&parent
->iter
, 1);
212 if (m
== MATCH_ERROR
)
215 m
= var_element (&var
);
216 if (m
== MATCH_ERROR
)
221 tail
->next
= gfc_get_data_variable ();
227 if (gfc_match_char (')') != MATCH_YES
)
232 gfc_syntax_error (ST_DATA
);
237 /* Match a single element in a data variable list, which can be a
238 variable-iterator list. */
241 var_element (gfc_data_variable
*new_var
)
246 memset (new_var
, 0, sizeof (gfc_data_variable
));
248 if (gfc_match_char ('(') == MATCH_YES
)
249 return var_list (new_var
);
251 m
= gfc_match_variable (&new_var
->expr
, 0);
255 sym
= new_var
->expr
->symtree
->n
.sym
;
257 /* Symbol should already have an associated type. */
258 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
261 if (!sym
->attr
.function
&& gfc_current_ns
->parent
262 && gfc_current_ns
->parent
== sym
->ns
)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym
->name
);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym
->attr
.in_common
271 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
272 "common block variable '%s' in DATA statement at %C",
276 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data
*d
)
288 gfc_data_variable var
, *tail
, *new_var
;
295 m
= var_element (&var
);
298 if (m
== MATCH_ERROR
)
301 new_var
= gfc_get_data_variable ();
307 tail
->next
= new_var
;
311 if (gfc_match_char ('/') == MATCH_YES
)
313 if (gfc_match_char (',') != MATCH_YES
)
320 gfc_syntax_error (ST_DATA
);
321 gfc_free_data_all (gfc_current_ns
);
327 match_data_constant (gfc_expr
**result
)
329 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
330 gfc_symbol
*sym
, *dt_sym
= NULL
;
335 m
= gfc_match_literal_constant (&expr
, 1);
342 if (m
== MATCH_ERROR
)
345 m
= gfc_match_null (result
);
349 old_loc
= gfc_current_locus
;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m
= gfc_match_rvalue (result
);
354 if (m
== MATCH_ERROR
)
357 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
359 if (!gfc_simplify_expr (*result
, 0))
363 else if (m
== MATCH_YES
)
364 gfc_free_expr (*result
);
366 gfc_current_locus
= old_loc
;
368 m
= gfc_match_name (name
);
372 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
375 if (sym
&& sym
->attr
.generic
)
376 dt_sym
= gfc_find_dt_in_generic (sym
);
379 || (sym
->attr
.flavor
!= FL_PARAMETER
380 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
382 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
386 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
387 return gfc_match_structure_constructor (dt_sym
, result
);
389 /* Check to see if the value is an initialization array expression. */
390 if (sym
->value
->expr_type
== EXPR_ARRAY
)
392 gfc_current_locus
= old_loc
;
394 m
= gfc_match_init_expr (result
);
395 if (m
== MATCH_ERROR
)
400 if (!gfc_simplify_expr (*result
, 0))
403 if ((*result
)->expr_type
== EXPR_CONSTANT
)
407 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
413 *result
= gfc_copy_expr (sym
->value
);
418 /* Match a list of values in a DATA statement. The leading '/' has
419 already been seen at this point. */
422 top_val_list (gfc_data
*data
)
424 gfc_data_value
*new_val
, *tail
;
432 m
= match_data_constant (&expr
);
435 if (m
== MATCH_ERROR
)
438 new_val
= gfc_get_data_value ();
439 mpz_init (new_val
->repeat
);
442 data
->value
= new_val
;
444 tail
->next
= new_val
;
448 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
451 mpz_set_ui (tail
->repeat
, 1);
455 mpz_set (tail
->repeat
, expr
->value
.integer
);
456 gfc_free_expr (expr
);
458 m
= match_data_constant (&tail
->expr
);
461 if (m
== MATCH_ERROR
)
465 if (gfc_match_char ('/') == MATCH_YES
)
467 if (gfc_match_char (',') == MATCH_NO
)
474 gfc_syntax_error (ST_DATA
);
475 gfc_free_data_all (gfc_current_ns
);
480 /* Matches an old style initialization. */
483 match_old_style_init (const char *name
)
490 /* Set up data structure to hold initializers. */
491 gfc_find_sym_tree (name
, NULL
, 0, &st
);
494 newdata
= gfc_get_data ();
495 newdata
->var
= gfc_get_data_variable ();
496 newdata
->var
->expr
= gfc_get_variable_expr (st
);
497 newdata
->where
= gfc_current_locus
;
499 /* Match initial value list. This also eats the terminal '/'. */
500 m
= top_val_list (newdata
);
509 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
514 if (gfc_implicit_pure (NULL
))
515 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
517 /* Mark the variable as having appeared in a data statement. */
518 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
524 /* Chain in namespace list of DATA initializers. */
525 newdata
->next
= gfc_current_ns
->data
;
526 gfc_current_ns
->data
= newdata
;
532 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
533 we are matching a DATA statement and are therefore issuing an error
534 if we encounter something unexpected, if not, we're trying to match
535 an old-style initialization expression of the form INTEGER I /2/. */
538 gfc_match_data (void)
543 set_in_match_data (true);
547 new_data
= gfc_get_data ();
548 new_data
->where
= gfc_current_locus
;
550 m
= top_var_list (new_data
);
554 m
= top_val_list (new_data
);
558 new_data
->next
= gfc_current_ns
->data
;
559 gfc_current_ns
->data
= new_data
;
561 if (gfc_match_eos () == MATCH_YES
)
564 gfc_match_char (','); /* Optional comma */
567 set_in_match_data (false);
571 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
575 if (gfc_implicit_pure (NULL
))
576 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
581 set_in_match_data (false);
582 gfc_free_data (new_data
);
587 /************************ Declaration statements *********************/
590 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
593 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
597 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
598 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
600 gfc_error ("The assumed-rank array at %C shall not have a codimension");
604 if (to
->rank
== 0 && from
->rank
> 0)
606 to
->rank
= from
->rank
;
607 to
->type
= from
->type
;
608 to
->cray_pointee
= from
->cray_pointee
;
609 to
->cp_was_assumed
= from
->cp_was_assumed
;
611 for (i
= 0; i
< to
->corank
; i
++)
613 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
614 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
616 for (i
= 0; i
< from
->rank
; i
++)
620 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
621 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
625 to
->lower
[i
] = from
->lower
[i
];
626 to
->upper
[i
] = from
->upper
[i
];
630 else if (to
->corank
== 0 && from
->corank
> 0)
632 to
->corank
= from
->corank
;
633 to
->cotype
= from
->cotype
;
635 for (i
= 0; i
< from
->corank
; i
++)
639 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
640 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
644 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
645 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
654 /* Match an intent specification. Since this can only happen after an
655 INTENT word, a legal intent-spec must follow. */
658 match_intent_spec (void)
661 if (gfc_match (" ( in out )") == MATCH_YES
)
663 if (gfc_match (" ( in )") == MATCH_YES
)
665 if (gfc_match (" ( out )") == MATCH_YES
)
668 gfc_error ("Bad INTENT specification at %C");
669 return INTENT_UNKNOWN
;
673 /* Matches a character length specification, which is either a
674 specification expression, '*', or ':'. */
677 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
684 if (gfc_match_char ('*') == MATCH_YES
)
687 if (gfc_match_char (':') == MATCH_YES
)
689 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
698 m
= gfc_match_expr (expr
);
701 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
704 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
706 if ((*expr
)->value
.function
.actual
707 && (*expr
)->value
.function
.actual
->expr
->symtree
)
710 e
= (*expr
)->value
.function
.actual
->expr
;
711 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
712 && e
->expr_type
== EXPR_VARIABLE
)
714 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
716 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
717 && e
->symtree
->n
.sym
->ts
.u
.cl
718 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
726 gfc_error ("Conflict in attributes of function argument at %C");
731 /* A character length is a '*' followed by a literal integer or a
732 char_len_param_value in parenthesis. */
735 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
741 m
= gfc_match_char ('*');
745 m
= gfc_match_small_literal_int (&length
, NULL
);
746 if (m
== MATCH_ERROR
)
751 if (obsolescent_check
752 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
754 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
758 if (gfc_match_char ('(') == MATCH_NO
)
761 m
= char_len_param_value (expr
, deferred
);
762 if (m
!= MATCH_YES
&& gfc_matching_function
)
768 if (m
== MATCH_ERROR
)
773 if (gfc_match_char (')') == MATCH_NO
)
775 gfc_free_expr (*expr
);
783 gfc_error ("Syntax error in character length specification at %C");
788 /* Special subroutine for finding a symbol. Check if the name is found
789 in the current name space. If not, and we're compiling a function or
790 subroutine and the parent compilation unit is an interface, then check
791 to see if the name we've been given is the name of the interface
792 (located in another namespace). */
795 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
801 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
804 *result
= st
? st
->n
.sym
: NULL
;
808 if (gfc_current_state () != COMP_SUBROUTINE
809 && gfc_current_state () != COMP_FUNCTION
)
812 s
= gfc_state_stack
->previous
;
816 if (s
->state
!= COMP_INTERFACE
)
819 goto end
; /* Nameless interface. */
821 if (strcmp (name
, s
->sym
->name
) == 0)
832 /* Special subroutine for getting a symbol node associated with a
833 procedure name, used in SUBROUTINE and FUNCTION statements. The
834 symbol is created in the parent using with symtree node in the
835 child unit pointing to the symbol. If the current namespace has no
836 parent, then the symbol is just created in the current unit. */
839 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
845 /* Module functions have to be left in their own namespace because
846 they have potentially (almost certainly!) already been referenced.
847 In this sense, they are rather like external functions. This is
848 fixed up in resolve.c(resolve_entries), where the symbol name-
849 space is set to point to the master function, so that the fake
850 result mechanism can work. */
851 if (module_fcn_entry
)
853 /* Present if entry is declared to be a module procedure. */
854 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
857 rc
= gfc_get_symbol (name
, NULL
, result
);
858 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
859 && (*result
)->ts
.type
== BT_UNKNOWN
860 && sym
->attr
.flavor
== FL_UNKNOWN
)
861 /* Pick up the typespec for the entry, if declared in the function
862 body. Note that this symbol is FL_UNKNOWN because it will
863 only have appeared in a type declaration. The local symtree
864 is set to point to the module symbol and a unique symtree
865 to the local version. This latter ensures a correct clearing
868 /* If the ENTRY proceeds its specification, we need to ensure
869 that this does not raise a "has no IMPLICIT type" error. */
870 if (sym
->ts
.type
== BT_UNKNOWN
)
871 sym
->attr
.untyped
= 1;
873 (*result
)->ts
= sym
->ts
;
875 /* Put the symbol in the procedure namespace so that, should
876 the ENTRY precede its specification, the specification
878 (*result
)->ns
= gfc_current_ns
;
880 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
882 st
= gfc_get_unique_symtree (gfc_current_ns
);
887 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
894 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
896 /* Trap another encompassed procedure with the same name. All
897 these conditions are necessary to avoid picking up an entry
898 whose name clashes with that of the encompassing procedure;
899 this is handled using gsymbols to register unique,globally
901 if (sym
->attr
.flavor
!= 0
902 && sym
->attr
.proc
!= 0
903 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
904 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
905 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
906 name
, &sym
->declared_at
);
908 /* Trap a procedure with a name the same as interface in the
909 encompassing scope. */
910 if (sym
->attr
.generic
!= 0
911 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
912 && !sym
->attr
.mod_proc
)
913 gfc_error_now ("Name '%s' at %C is already defined"
914 " as a generic interface at %L",
915 name
, &sym
->declared_at
);
917 /* Trap declarations of attributes in encompassing scope. The
918 signature for this is that ts.kind is set. Legitimate
919 references only set ts.type. */
920 if (sym
->ts
.kind
!= 0
921 && !sym
->attr
.implicit_type
922 && sym
->attr
.proc
== 0
923 && gfc_current_ns
->parent
!= NULL
924 && sym
->attr
.access
== 0
925 && !module_fcn_entry
)
926 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
927 "and must not have attributes declared at %L",
928 name
, &sym
->declared_at
);
931 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
934 /* Module function entries will already have a symtree in
935 the current namespace but will need one at module level. */
936 if (module_fcn_entry
)
938 /* Present if entry is declared to be a module procedure. */
939 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
941 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
944 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
949 /* See if the procedure should be a module procedure. */
951 if (((sym
->ns
->proc_name
!= NULL
952 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
953 && sym
->attr
.proc
!= PROC_MODULE
)
954 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
955 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
962 /* Verify that the given symbol representing a parameter is C
963 interoperable, by checking to see if it was marked as such after
964 its declaration. If the given symbol is not interoperable, a
965 warning is reported, thus removing the need to return the status to
966 the calling function. The standard does not require the user use
967 one of the iso_c_binding named constants to declare an
968 interoperable parameter, but we can't be sure if the param is C
969 interop or not if the user doesn't. For example, integer(4) may be
970 legal Fortran, but doesn't have meaning in C. It may interop with
971 a number of the C types, which causes a problem because the
972 compiler can't know which one. This code is almost certainly not
973 portable, and the user will get what they deserve if the C type
974 across platforms isn't always interoperable with integer(4). If
975 the user had used something like integer(c_int) or integer(c_long),
976 the compiler could have automatically handled the varying sizes
980 gfc_verify_c_interop_param (gfc_symbol
*sym
)
982 int is_c_interop
= 0;
985 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
986 Don't repeat the checks here. */
987 if (sym
->attr
.implicit_type
)
990 /* For subroutines or functions that are passed to a BIND(C) procedure,
991 they're interoperable if they're BIND(C) and their params are all
993 if (sym
->attr
.flavor
== FL_PROCEDURE
)
995 if (sym
->attr
.is_bind_c
== 0)
997 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
998 "attribute to be C interoperable", sym
->name
,
999 &(sym
->declared_at
));
1005 if (sym
->attr
.is_c_interop
== 1)
1006 /* We've already checked this procedure; don't check it again. */
1009 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1014 /* See if we've stored a reference to a procedure that owns sym. */
1015 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1017 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1019 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1021 if (is_c_interop
!= 1)
1023 /* Make personalized messages to give better feedback. */
1024 if (sym
->ts
.type
== BT_DERIVED
)
1025 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1026 "BIND(C) procedure '%s' but is not C interoperable "
1027 "because derived type '%s' is not C interoperable",
1028 sym
->name
, &(sym
->declared_at
),
1029 sym
->ns
->proc_name
->name
,
1030 sym
->ts
.u
.derived
->name
);
1031 else if (sym
->ts
.type
== BT_CLASS
)
1032 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1033 "BIND(C) procedure '%s' but is not C interoperable "
1034 "because it is polymorphic",
1035 sym
->name
, &(sym
->declared_at
),
1036 sym
->ns
->proc_name
->name
);
1037 else if (gfc_option
.warn_c_binding_type
)
1038 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1039 "BIND(C) procedure '%s' but may not be C "
1041 sym
->name
, &(sym
->declared_at
),
1042 sym
->ns
->proc_name
->name
);
1045 /* Character strings are only C interoperable if they have a
1047 if (sym
->ts
.type
== BT_CHARACTER
)
1049 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1050 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1051 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1053 gfc_error ("Character argument '%s' at %L "
1054 "must be length 1 because "
1055 "procedure '%s' is BIND(C)",
1056 sym
->name
, &sym
->declared_at
,
1057 sym
->ns
->proc_name
->name
);
1062 /* We have to make sure that any param to a bind(c) routine does
1063 not have the allocatable, pointer, or optional attributes,
1064 according to J3/04-007, section 5.1. */
1065 if (sym
->attr
.allocatable
== 1
1066 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1067 "ALLOCATABLE attribute in procedure '%s' "
1068 "with BIND(C)", sym
->name
,
1069 &(sym
->declared_at
),
1070 sym
->ns
->proc_name
->name
))
1073 if (sym
->attr
.pointer
== 1
1074 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1075 "POINTER attribute in procedure '%s' "
1076 "with BIND(C)", sym
->name
,
1077 &(sym
->declared_at
),
1078 sym
->ns
->proc_name
->name
))
1081 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1083 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1084 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1085 " supported", sym
->name
, &(sym
->declared_at
),
1086 sym
->ns
->proc_name
->name
);
1090 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1092 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1093 "and the VALUE attribute because procedure '%s' "
1094 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1095 sym
->ns
->proc_name
->name
);
1098 else if (sym
->attr
.optional
== 1
1099 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' "
1100 "at %L with OPTIONAL attribute in "
1101 "procedure '%s' which is BIND(C)",
1102 sym
->name
, &(sym
->declared_at
),
1103 sym
->ns
->proc_name
->name
))
1106 /* Make sure that if it has the dimension attribute, that it is
1107 either assumed size or explicit shape. Deferred shape is already
1108 covered by the pointer/allocatable attribute. */
1109 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1110 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array '%s' "
1111 "at %L as dummy argument to the BIND(C) "
1112 "procedure '%s' at %L", sym
->name
,
1113 &(sym
->declared_at
),
1114 sym
->ns
->proc_name
->name
,
1115 &(sym
->ns
->proc_name
->declared_at
)))
1125 /* Function called by variable_decl() that adds a name to the symbol table. */
1128 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1129 gfc_array_spec
**as
, locus
*var_locus
)
1131 symbol_attribute attr
;
1134 if (gfc_get_symbol (name
, NULL
, &sym
))
1137 /* Start updating the symbol table. Add basic type attribute if present. */
1138 if (current_ts
.type
!= BT_UNKNOWN
1139 && (sym
->attr
.implicit_type
== 0
1140 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1141 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1144 if (sym
->ts
.type
== BT_CHARACTER
)
1147 sym
->ts
.deferred
= cl_deferred
;
1150 /* Add dimension attribute if present. */
1151 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1155 /* Add attribute to symbol. The copy is so that we can reset the
1156 dimension attribute. */
1157 attr
= current_attr
;
1159 attr
.codimension
= 0;
1161 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1164 /* Finish any work that may need to be done for the binding label,
1165 if it's a bind(c). The bind(c) attr is found before the symbol
1166 is made, and before the symbol name (for data decls), so the
1167 current_ts is holding the binding label, or nothing if the
1168 name= attr wasn't given. Therefore, test here if we're dealing
1169 with a bind(c) and make sure the binding label is set correctly. */
1170 if (sym
->attr
.is_bind_c
== 1)
1172 if (!sym
->binding_label
)
1174 /* Set the binding label and verify that if a NAME= was specified
1175 then only one identifier was in the entity-decl-list. */
1176 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1177 num_idents_on_line
))
1182 /* See if we know we're in a common block, and if it's a bind(c)
1183 common then we need to make sure we're an interoperable type. */
1184 if (sym
->attr
.in_common
== 1)
1186 /* Test the common block object. */
1187 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1188 && sym
->ts
.is_c_interop
!= 1)
1190 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1191 "must be declared with a C interoperable "
1192 "kind since common block '%s' is BIND(C)",
1193 sym
->name
, sym
->common_block
->name
,
1194 sym
->common_block
->name
);
1199 sym
->attr
.implied_index
= 0;
1201 if (sym
->ts
.type
== BT_CLASS
)
1202 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1208 /* Set character constant to the given length. The constant will be padded or
1209 truncated. If we're inside an array constructor without a typespec, we
1210 additionally check that all elements have the same length; check_len -1
1211 means no checking. */
1214 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1219 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1220 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1222 slen
= expr
->value
.character
.length
;
1225 s
= gfc_get_wide_string (len
+ 1);
1226 memcpy (s
, expr
->value
.character
.string
,
1227 MIN (len
, slen
) * sizeof (gfc_char_t
));
1229 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1231 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1232 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1233 "(%d/%d)", &expr
->where
, slen
, len
);
1235 /* Apply the standard by 'hand' otherwise it gets cleared for
1237 if (check_len
!= -1 && slen
!= check_len
1238 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1239 gfc_error_now ("The CHARACTER elements of the array constructor "
1240 "at %L must have the same length (%d/%d)",
1241 &expr
->where
, slen
, check_len
);
1244 free (expr
->value
.character
.string
);
1245 expr
->value
.character
.string
= s
;
1246 expr
->value
.character
.length
= len
;
1251 /* Function to create and update the enumerator history
1252 using the information passed as arguments.
1253 Pointer "max_enum" is also updated, to point to
1254 enum history node containing largest initializer.
1256 SYM points to the symbol node of enumerator.
1257 INIT points to its enumerator value. */
1260 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1262 enumerator_history
*new_enum_history
;
1263 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1265 new_enum_history
= XCNEW (enumerator_history
);
1267 new_enum_history
->sym
= sym
;
1268 new_enum_history
->initializer
= init
;
1269 new_enum_history
->next
= NULL
;
1271 if (enum_history
== NULL
)
1273 enum_history
= new_enum_history
;
1274 max_enum
= enum_history
;
1278 new_enum_history
->next
= enum_history
;
1279 enum_history
= new_enum_history
;
1281 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1282 new_enum_history
->initializer
->value
.integer
) < 0)
1283 max_enum
= new_enum_history
;
1288 /* Function to free enum kind history. */
1291 gfc_free_enum_history (void)
1293 enumerator_history
*current
= enum_history
;
1294 enumerator_history
*next
;
1296 while (current
!= NULL
)
1298 next
= current
->next
;
1303 enum_history
= NULL
;
1307 /* Function called by variable_decl() that adds an initialization
1308 expression to a symbol. */
1311 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1313 symbol_attribute attr
;
1318 if (find_special (name
, &sym
, false))
1323 /* If this symbol is confirming an implicit parameter type,
1324 then an initialization expression is not allowed. */
1325 if (attr
.flavor
== FL_PARAMETER
1326 && sym
->value
!= NULL
1329 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1336 /* An initializer is required for PARAMETER declarations. */
1337 if (attr
.flavor
== FL_PARAMETER
)
1339 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1345 /* If a variable appears in a DATA block, it cannot have an
1349 gfc_error ("Variable '%s' at %C with an initializer already "
1350 "appears in a DATA statement", sym
->name
);
1354 /* Check if the assignment can happen. This has to be put off
1355 until later for derived type variables and procedure pointers. */
1356 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1357 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1358 && !sym
->attr
.proc_pointer
1359 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1362 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1363 && init
->ts
.type
== BT_CHARACTER
)
1365 /* Update symbol character length according initializer. */
1366 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1369 if (sym
->ts
.u
.cl
->length
== NULL
)
1372 /* If there are multiple CHARACTER variables declared on the
1373 same line, we don't want them to share the same length. */
1374 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1376 if (sym
->attr
.flavor
== FL_PARAMETER
)
1378 if (init
->expr_type
== EXPR_CONSTANT
)
1380 clen
= init
->value
.character
.length
;
1381 sym
->ts
.u
.cl
->length
1382 = gfc_get_int_expr (gfc_default_integer_kind
,
1385 else if (init
->expr_type
== EXPR_ARRAY
)
1388 c
= gfc_constructor_first (init
->value
.constructor
);
1389 clen
= c
->expr
->value
.character
.length
;
1390 sym
->ts
.u
.cl
->length
1391 = gfc_get_int_expr (gfc_default_integer_kind
,
1394 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1395 sym
->ts
.u
.cl
->length
=
1396 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1399 /* Update initializer character length according symbol. */
1400 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1402 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1404 if (init
->expr_type
== EXPR_CONSTANT
)
1405 gfc_set_constant_character_len (len
, init
, -1);
1406 else if (init
->expr_type
== EXPR_ARRAY
)
1410 /* Build a new charlen to prevent simplification from
1411 deleting the length before it is resolved. */
1412 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1413 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1415 for (c
= gfc_constructor_first (init
->value
.constructor
);
1416 c
; c
= gfc_constructor_next (c
))
1417 gfc_set_constant_character_len (len
, c
->expr
, -1);
1422 /* If sym is implied-shape, set its upper bounds from init. */
1423 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1424 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1428 if (init
->rank
== 0)
1430 gfc_error ("Can't initialize implied-shape array at %L"
1431 " with scalar", &sym
->declared_at
);
1434 gcc_assert (sym
->as
->rank
== init
->rank
);
1436 /* Shape should be present, we get an initialization expression. */
1437 gcc_assert (init
->shape
);
1439 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1445 lower
= sym
->as
->lower
[dim
];
1446 if (lower
->expr_type
!= EXPR_CONSTANT
)
1448 gfc_error ("Non-constant lower bound in implied-shape"
1449 " declaration at %L", &lower
->where
);
1453 /* All dimensions must be without upper bound. */
1454 gcc_assert (!sym
->as
->upper
[dim
]);
1457 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1458 mpz_add (e
->value
.integer
,
1459 lower
->value
.integer
, init
->shape
[dim
]);
1460 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1461 sym
->as
->upper
[dim
] = e
;
1464 sym
->as
->type
= AS_EXPLICIT
;
1467 /* Need to check if the expression we initialized this
1468 to was one of the iso_c_binding named constants. If so,
1469 and we're a parameter (constant), let it be iso_c.
1471 integer(c_int), parameter :: my_int = c_int
1472 integer(my_int) :: my_int_2
1473 If we mark my_int as iso_c (since we can see it's value
1474 is equal to one of the named constants), then my_int_2
1475 will be considered C interoperable. */
1476 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1478 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1479 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1480 /* attr bits needed for module files. */
1481 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1482 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1483 if (init
->ts
.is_iso_c
)
1484 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1487 /* Add initializer. Make sure we keep the ranks sane. */
1488 if (sym
->attr
.dimension
&& init
->rank
== 0)
1493 if (sym
->attr
.flavor
== FL_PARAMETER
1494 && init
->expr_type
== EXPR_CONSTANT
1495 && spec_size (sym
->as
, &size
)
1496 && mpz_cmp_si (size
, 0) > 0)
1498 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1500 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1501 gfc_constructor_append_expr (&array
->value
.constructor
,
1504 : gfc_copy_expr (init
),
1507 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1508 for (n
= 0; n
< sym
->as
->rank
; n
++)
1509 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1514 init
->rank
= sym
->as
->rank
;
1518 if (sym
->attr
.save
== SAVE_NONE
)
1519 sym
->attr
.save
= SAVE_IMPLICIT
;
1527 /* Function called by variable_decl() that adds a name to a structure
1531 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1532 gfc_array_spec
**as
)
1537 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1538 constructing, it must have the pointer attribute. */
1539 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1540 && current_ts
.u
.derived
== gfc_current_block ()
1541 && current_attr
.pointer
== 0)
1543 gfc_error ("Component at %C must have the POINTER attribute");
1547 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1549 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1551 gfc_error ("Array component of structure at %C must have explicit "
1552 "or deferred shape");
1557 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1561 if (c
->ts
.type
== BT_CHARACTER
)
1563 c
->attr
= current_attr
;
1565 c
->initializer
= *init
;
1572 c
->attr
.codimension
= 1;
1574 c
->attr
.dimension
= 1;
1578 /* Should this ever get more complicated, combine with similar section
1579 in add_init_expr_to_sym into a separate function. */
1580 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1582 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1586 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1587 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1588 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1590 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1592 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1593 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1594 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1595 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1597 gfc_constructor
*ctor
;
1598 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1603 bool has_ts
= (c
->initializer
->ts
.u
.cl
1604 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1606 /* Remember the length of the first element for checking
1607 that all elements *in the constructor* have the same
1608 length. This need not be the length of the LHS! */
1609 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1610 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1611 first_len
= ctor
->expr
->value
.character
.length
;
1613 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1614 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1616 gfc_set_constant_character_len (len
, ctor
->expr
,
1617 has_ts
? -1 : first_len
);
1618 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1624 /* Check array components. */
1625 if (!c
->attr
.dimension
)
1628 if (c
->attr
.pointer
)
1630 if (c
->as
->type
!= AS_DEFERRED
)
1632 gfc_error ("Pointer array component of structure at %C must have a "
1637 else if (c
->attr
.allocatable
)
1639 if (c
->as
->type
!= AS_DEFERRED
)
1641 gfc_error ("Allocatable component of structure at %C must have a "
1648 if (c
->as
->type
!= AS_EXPLICIT
)
1650 gfc_error ("Array component of structure at %C must have an "
1657 if (c
->ts
.type
== BT_CLASS
)
1659 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1669 /* Match a 'NULL()', and possibly take care of some side effects. */
1672 gfc_match_null (gfc_expr
**result
)
1675 match m
, m2
= MATCH_NO
;
1677 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1683 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1685 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1688 old_loc
= gfc_current_locus
;
1689 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1692 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1696 gfc_current_locus
= old_loc
;
1701 /* The NULL symbol now has to be/become an intrinsic function. */
1702 if (gfc_get_symbol ("null", NULL
, &sym
))
1704 gfc_error ("NULL() initialization at %C is ambiguous");
1708 gfc_intrinsic_symbol (sym
);
1710 if (sym
->attr
.proc
!= PROC_INTRINSIC
1711 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1712 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1713 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1716 *result
= gfc_get_null_expr (&gfc_current_locus
);
1718 /* Invalid per F2008, C512. */
1719 if (m2
== MATCH_YES
)
1721 gfc_error ("NULL() initialization at %C may not have MOLD");
1729 /* Match the initialization expr for a data pointer or procedure pointer. */
1732 match_pointer_init (gfc_expr
**init
, int procptr
)
1736 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1738 gfc_error ("Initialization of pointer at %C is not allowed in "
1739 "a PURE procedure");
1743 /* Match NULL() initialization. */
1744 m
= gfc_match_null (init
);
1748 /* Match non-NULL initialization. */
1749 gfc_matching_ptr_assignment
= !procptr
;
1750 gfc_matching_procptr_assignment
= procptr
;
1751 m
= gfc_match_rvalue (init
);
1752 gfc_matching_ptr_assignment
= 0;
1753 gfc_matching_procptr_assignment
= 0;
1754 if (m
== MATCH_ERROR
)
1756 else if (m
== MATCH_NO
)
1758 gfc_error ("Error in pointer initialization at %C");
1763 gfc_resolve_expr (*init
);
1765 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1766 "initialization at %C"))
1774 check_function_name (char *name
)
1776 /* In functions that have a RESULT variable defined, the function name always
1777 refers to function calls. Therefore, the name is not allowed to appear in
1778 specification statements. When checking this, be careful about
1779 'hidden' procedure pointer results ('ppr@'). */
1781 if (gfc_current_state () == COMP_FUNCTION
)
1783 gfc_symbol
*block
= gfc_current_block ();
1784 if (block
&& block
->result
&& block
->result
!= block
1785 && strcmp (block
->result
->name
, "ppr@") != 0
1786 && strcmp (block
->name
, name
) == 0)
1788 gfc_error ("Function name '%s' not allowed at %C", name
);
1797 /* Match a variable name with an optional initializer. When this
1798 subroutine is called, a variable is expected to be parsed next.
1799 Depending on what is happening at the moment, updates either the
1800 symbol table or the current interface. */
1803 variable_decl (int elem
)
1805 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1806 gfc_expr
*initializer
, *char_len
;
1808 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1820 /* When we get here, we've just matched a list of attributes and
1821 maybe a type and a double colon. The next thing we expect to see
1822 is the name of the symbol. */
1823 m
= gfc_match_name (name
);
1827 var_locus
= gfc_current_locus
;
1829 /* Now we could see the optional array spec. or character length. */
1830 m
= gfc_match_array_spec (&as
, true, true);
1831 if (m
== MATCH_ERROR
)
1835 as
= gfc_copy_array_spec (current_as
);
1837 && !merge_array_spec (current_as
, as
, true))
1843 if (gfc_option
.flag_cray_pointer
)
1844 cp_as
= gfc_copy_array_spec (as
);
1846 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1847 determine (and check) whether it can be implied-shape. If it
1848 was parsed as assumed-size, change it because PARAMETERs can not
1852 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1855 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1860 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1861 && current_attr
.flavor
== FL_PARAMETER
)
1862 as
->type
= AS_IMPLIED_SHAPE
;
1864 if (as
->type
== AS_IMPLIED_SHAPE
1865 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1875 cl_deferred
= false;
1877 if (current_ts
.type
== BT_CHARACTER
)
1879 switch (match_char_length (&char_len
, &cl_deferred
, false))
1882 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1884 cl
->length
= char_len
;
1887 /* Non-constant lengths need to be copied after the first
1888 element. Also copy assumed lengths. */
1891 && (current_ts
.u
.cl
->length
== NULL
1892 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1894 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1895 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1898 cl
= current_ts
.u
.cl
;
1900 cl_deferred
= current_ts
.deferred
;
1909 /* If this symbol has already shown up in a Cray Pointer declaration,
1910 then we want to set the type & bail out. */
1911 if (gfc_option
.flag_cray_pointer
)
1913 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1914 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1916 sym
->ts
.type
= current_ts
.type
;
1917 sym
->ts
.kind
= current_ts
.kind
;
1919 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1920 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1921 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1924 /* Check to see if we have an array specification. */
1927 if (sym
->as
!= NULL
)
1929 gfc_error ("Duplicate array spec for Cray pointee at %C");
1930 gfc_free_array_spec (cp_as
);
1936 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1937 gfc_internal_error ("Couldn't set pointee array spec.");
1939 /* Fix the array spec. */
1940 m
= gfc_mod_pointee_as (sym
->as
);
1941 if (m
== MATCH_ERROR
)
1949 gfc_free_array_spec (cp_as
);
1953 /* Procedure pointer as function result. */
1954 if (gfc_current_state () == COMP_FUNCTION
1955 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1956 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1957 strcpy (name
, "ppr@");
1959 if (gfc_current_state () == COMP_FUNCTION
1960 && strcmp (name
, gfc_current_block ()->name
) == 0
1961 && gfc_current_block ()->result
1962 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1963 strcpy (name
, "ppr@");
1965 /* OK, we've successfully matched the declaration. Now put the
1966 symbol in the current namespace, because it might be used in the
1967 optional initialization expression for this symbol, e.g. this is
1970 integer, parameter :: i = huge(i)
1972 This is only true for parameters or variables of a basic type.
1973 For components of derived types, it is not true, so we don't
1974 create a symbol for those yet. If we fail to create the symbol,
1976 if (gfc_current_state () != COMP_DERIVED
1977 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
1983 if (!check_function_name (name
))
1989 /* We allow old-style initializations of the form
1990 integer i /2/, j(4) /3*3, 1/
1991 (if no colon has been seen). These are different from data
1992 statements in that initializers are only allowed to apply to the
1993 variable immediately preceding, i.e.
1995 is not allowed. Therefore we have to do some work manually, that
1996 could otherwise be left to the matchers for DATA statements. */
1998 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2000 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2001 "initialization at %C"))
2004 return match_old_style_init (name
);
2007 /* The double colon must be present in order to have initializers.
2008 Otherwise the statement is ambiguous with an assignment statement. */
2011 if (gfc_match (" =>") == MATCH_YES
)
2013 if (!current_attr
.pointer
)
2015 gfc_error ("Initialization at %C isn't for a pointer variable");
2020 m
= match_pointer_init (&initializer
, 0);
2024 else if (gfc_match_char ('=') == MATCH_YES
)
2026 if (current_attr
.pointer
)
2028 gfc_error ("Pointer initialization at %C requires '=>', "
2034 m
= gfc_match_init_expr (&initializer
);
2037 gfc_error ("Expected an initialization expression at %C");
2041 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2042 && gfc_state_stack
->state
!= COMP_DERIVED
)
2044 gfc_error ("Initialization of variable at %C is not allowed in "
2045 "a PURE procedure");
2054 if (initializer
!= NULL
&& current_attr
.allocatable
2055 && gfc_current_state () == COMP_DERIVED
)
2057 gfc_error ("Initialization of allocatable component at %C is not "
2063 /* Add the initializer. Note that it is fine if initializer is
2064 NULL here, because we sometimes also need to check if a
2065 declaration *must* have an initialization expression. */
2066 if (gfc_current_state () != COMP_DERIVED
)
2067 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2070 if (current_ts
.type
== BT_DERIVED
2071 && !current_attr
.pointer
&& !initializer
)
2072 initializer
= gfc_default_initializer (¤t_ts
);
2073 t
= build_struct (name
, cl
, &initializer
, &as
);
2076 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2079 /* Free stuff up and return. */
2080 gfc_free_expr (initializer
);
2081 gfc_free_array_spec (as
);
2087 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2088 This assumes that the byte size is equal to the kind number for
2089 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2092 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2097 if (gfc_match_char ('*') != MATCH_YES
)
2100 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2104 original_kind
= ts
->kind
;
2106 /* Massage the kind numbers for complex types. */
2107 if (ts
->type
== BT_COMPLEX
)
2111 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2112 gfc_basic_typename (ts
->type
), original_kind
);
2119 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2122 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2126 if (gfc_option
.flag_real4_kind
== 8)
2128 if (gfc_option
.flag_real4_kind
== 10)
2130 if (gfc_option
.flag_real4_kind
== 16)
2136 if (gfc_option
.flag_real8_kind
== 4)
2138 if (gfc_option
.flag_real8_kind
== 10)
2140 if (gfc_option
.flag_real8_kind
== 16)
2145 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2147 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2148 gfc_basic_typename (ts
->type
), original_kind
);
2152 if (!gfc_notify_std (GFC_STD_GNU
,
2153 "Nonstandard type declaration %s*%d at %C",
2154 gfc_basic_typename(ts
->type
), original_kind
))
2161 /* Match a kind specification. Since kinds are generally optional, we
2162 usually return MATCH_NO if something goes wrong. If a "kind="
2163 string is found, then we know we have an error. */
2166 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2178 where
= loc
= gfc_current_locus
;
2183 if (gfc_match_char ('(') == MATCH_NO
)
2186 /* Also gobbles optional text. */
2187 if (gfc_match (" kind = ") == MATCH_YES
)
2190 loc
= gfc_current_locus
;
2193 n
= gfc_match_init_expr (&e
);
2197 if (gfc_matching_function
)
2199 /* The function kind expression might include use associated or
2200 imported parameters and try again after the specification
2202 if (gfc_match_char (')') != MATCH_YES
)
2204 gfc_error ("Missing right parenthesis at %C");
2210 gfc_undo_symbols ();
2215 /* ....or else, the match is real. */
2217 gfc_error ("Expected initialization expression at %C");
2225 gfc_error ("Expected scalar initialization expression at %C");
2230 msg
= gfc_extract_int (e
, &ts
->kind
);
2239 /* Before throwing away the expression, let's see if we had a
2240 C interoperable kind (and store the fact). */
2241 if (e
->ts
.is_c_interop
== 1)
2243 /* Mark this as C interoperable if being declared with one
2244 of the named constants from iso_c_binding. */
2245 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2246 ts
->f90_type
= e
->ts
.f90_type
;
2252 /* Ignore errors to this point, if we've gotten here. This means
2253 we ignore the m=MATCH_ERROR from above. */
2254 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2256 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2257 gfc_basic_typename (ts
->type
));
2258 gfc_current_locus
= where
;
2262 /* Warn if, e.g., c_int is used for a REAL variable, but not
2263 if, e.g., c_double is used for COMPLEX as the standard
2264 explicitly says that the kind type parameter for complex and real
2265 variable is the same, i.e. c_float == c_float_complex. */
2266 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2267 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2268 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2269 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2270 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2271 gfc_basic_typename (ts
->type
));
2273 gfc_gobble_whitespace ();
2274 if ((c
= gfc_next_ascii_char ()) != ')'
2275 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2277 if (ts
->type
== BT_CHARACTER
)
2278 gfc_error ("Missing right parenthesis or comma at %C");
2280 gfc_error ("Missing right parenthesis at %C");
2284 /* All tests passed. */
2287 if(m
== MATCH_ERROR
)
2288 gfc_current_locus
= where
;
2290 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2293 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2297 if (gfc_option
.flag_real4_kind
== 8)
2299 if (gfc_option
.flag_real4_kind
== 10)
2301 if (gfc_option
.flag_real4_kind
== 16)
2307 if (gfc_option
.flag_real8_kind
== 4)
2309 if (gfc_option
.flag_real8_kind
== 10)
2311 if (gfc_option
.flag_real8_kind
== 16)
2316 /* Return what we know from the test(s). */
2321 gfc_current_locus
= where
;
2327 match_char_kind (int * kind
, int * is_iso_c
)
2336 where
= gfc_current_locus
;
2338 n
= gfc_match_init_expr (&e
);
2340 if (n
!= MATCH_YES
&& gfc_matching_function
)
2342 /* The expression might include use-associated or imported
2343 parameters and try again after the specification
2346 gfc_undo_symbols ();
2351 gfc_error ("Expected initialization expression at %C");
2357 gfc_error ("Expected scalar initialization expression at %C");
2362 msg
= gfc_extract_int (e
, kind
);
2363 *is_iso_c
= e
->ts
.is_iso_c
;
2373 /* Ignore errors to this point, if we've gotten here. This means
2374 we ignore the m=MATCH_ERROR from above. */
2375 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2377 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2381 /* All tests passed. */
2384 if (m
== MATCH_ERROR
)
2385 gfc_current_locus
= where
;
2387 /* Return what we know from the test(s). */
2392 gfc_current_locus
= where
;
2397 /* Match the various kind/length specifications in a CHARACTER
2398 declaration. We don't return MATCH_NO. */
2401 gfc_match_char_spec (gfc_typespec
*ts
)
2403 int kind
, seen_length
, is_iso_c
;
2415 /* Try the old-style specification first. */
2416 old_char_selector
= 0;
2418 m
= match_char_length (&len
, &deferred
, true);
2422 old_char_selector
= 1;
2427 m
= gfc_match_char ('(');
2430 m
= MATCH_YES
; /* Character without length is a single char. */
2434 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2435 if (gfc_match (" kind =") == MATCH_YES
)
2437 m
= match_char_kind (&kind
, &is_iso_c
);
2439 if (m
== MATCH_ERROR
)
2444 if (gfc_match (" , len =") == MATCH_NO
)
2447 m
= char_len_param_value (&len
, &deferred
);
2450 if (m
== MATCH_ERROR
)
2457 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2458 if (gfc_match (" len =") == MATCH_YES
)
2460 m
= char_len_param_value (&len
, &deferred
);
2463 if (m
== MATCH_ERROR
)
2467 if (gfc_match_char (')') == MATCH_YES
)
2470 if (gfc_match (" , kind =") != MATCH_YES
)
2473 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2479 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2480 m
= char_len_param_value (&len
, &deferred
);
2483 if (m
== MATCH_ERROR
)
2487 m
= gfc_match_char (')');
2491 if (gfc_match_char (',') != MATCH_YES
)
2494 gfc_match (" kind ="); /* Gobble optional text. */
2496 m
= match_char_kind (&kind
, &is_iso_c
);
2497 if (m
== MATCH_ERROR
)
2503 /* Require a right-paren at this point. */
2504 m
= gfc_match_char (')');
2509 gfc_error ("Syntax error in CHARACTER declaration at %C");
2511 gfc_free_expr (len
);
2515 /* Deal with character functions after USE and IMPORT statements. */
2516 if (gfc_matching_function
)
2518 gfc_free_expr (len
);
2519 gfc_undo_symbols ();
2525 gfc_free_expr (len
);
2529 /* Do some final massaging of the length values. */
2530 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2532 if (seen_length
== 0)
2533 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2538 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2539 ts
->deferred
= deferred
;
2541 /* We have to know if it was a C interoperable kind so we can
2542 do accurate type checking of bind(c) procs, etc. */
2544 /* Mark this as C interoperable if being declared with one
2545 of the named constants from iso_c_binding. */
2546 ts
->is_c_interop
= is_iso_c
;
2547 else if (len
!= NULL
)
2548 /* Here, we might have parsed something such as: character(c_char)
2549 In this case, the parsing code above grabs the c_char when
2550 looking for the length (line 1690, roughly). it's the last
2551 testcase for parsing the kind params of a character variable.
2552 However, it's not actually the length. this seems like it
2554 To see if the user used a C interop kind, test the expr
2555 of the so called length, and see if it's C interoperable. */
2556 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2562 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2563 structure to the matched specification. This is necessary for FUNCTION and
2564 IMPLICIT statements.
2566 If implicit_flag is nonzero, then we don't check for the optional
2567 kind specification. Not doing so is needed for matching an IMPLICIT
2568 statement correctly. */
2571 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2573 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2574 gfc_symbol
*sym
, *dt_sym
;
2577 bool seen_deferred_kind
, matched_type
;
2578 const char *dt_name
;
2580 /* A belt and braces check that the typespec is correctly being treated
2581 as a deferred characteristic association. */
2582 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2583 && (gfc_current_block ()->result
->ts
.kind
== -1)
2584 && (ts
->kind
== -1);
2586 if (seen_deferred_kind
)
2589 /* Clear the current binding label, in case one is given. */
2590 curr_binding_label
= NULL
;
2592 if (gfc_match (" byte") == MATCH_YES
)
2594 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2597 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2599 gfc_error ("BYTE type used at %C "
2600 "is not available on the target machine");
2604 ts
->type
= BT_INTEGER
;
2610 m
= gfc_match (" type (");
2611 matched_type
= (m
== MATCH_YES
);
2614 gfc_gobble_whitespace ();
2615 if (gfc_peek_ascii_char () == '*')
2617 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2619 if (gfc_current_state () == COMP_DERIVED
)
2621 gfc_error ("Assumed type at %C is not allowed for components");
2624 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2627 ts
->type
= BT_ASSUMED
;
2631 m
= gfc_match ("%n", name
);
2632 matched_type
= (m
== MATCH_YES
);
2635 if ((matched_type
&& strcmp ("integer", name
) == 0)
2636 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2638 ts
->type
= BT_INTEGER
;
2639 ts
->kind
= gfc_default_integer_kind
;
2643 if ((matched_type
&& strcmp ("character", name
) == 0)
2644 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2647 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2648 "intrinsic-type-spec at %C"))
2651 ts
->type
= BT_CHARACTER
;
2652 if (implicit_flag
== 0)
2653 m
= gfc_match_char_spec (ts
);
2657 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2663 if ((matched_type
&& strcmp ("real", name
) == 0)
2664 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2667 ts
->kind
= gfc_default_real_kind
;
2672 && (strcmp ("doubleprecision", name
) == 0
2673 || (strcmp ("double", name
) == 0
2674 && gfc_match (" precision") == MATCH_YES
)))
2675 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2678 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2679 "intrinsic-type-spec at %C"))
2681 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2685 ts
->kind
= gfc_default_double_kind
;
2689 if ((matched_type
&& strcmp ("complex", name
) == 0)
2690 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2692 ts
->type
= BT_COMPLEX
;
2693 ts
->kind
= gfc_default_complex_kind
;
2698 && (strcmp ("doublecomplex", name
) == 0
2699 || (strcmp ("double", name
) == 0
2700 && gfc_match (" complex") == MATCH_YES
)))
2701 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2703 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2707 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2708 "intrinsic-type-spec at %C"))
2711 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2714 ts
->type
= BT_COMPLEX
;
2715 ts
->kind
= gfc_default_double_kind
;
2719 if ((matched_type
&& strcmp ("logical", name
) == 0)
2720 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2722 ts
->type
= BT_LOGICAL
;
2723 ts
->kind
= gfc_default_logical_kind
;
2728 m
= gfc_match_char (')');
2731 ts
->type
= BT_DERIVED
;
2734 /* Match CLASS declarations. */
2735 m
= gfc_match (" class ( * )");
2736 if (m
== MATCH_ERROR
)
2738 else if (m
== MATCH_YES
)
2742 ts
->type
= BT_CLASS
;
2743 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2746 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2747 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2749 gfc_set_sym_referenced (upe
);
2751 upe
->ts
.type
= BT_VOID
;
2752 upe
->attr
.unlimited_polymorphic
= 1;
2753 /* This is essential to force the construction of
2754 unlimited polymorphic component class containers. */
2755 upe
->attr
.zero_comp
= 1;
2756 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2757 &gfc_current_locus
))
2762 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2764 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2768 ts
->u
.derived
= upe
;
2772 m
= gfc_match (" class ( %n )", name
);
2775 ts
->type
= BT_CLASS
;
2777 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2781 /* Defer association of the derived type until the end of the
2782 specification block. However, if the derived type can be
2783 found, add it to the typespec. */
2784 if (gfc_matching_function
)
2786 ts
->u
.derived
= NULL
;
2787 if (gfc_current_state () != COMP_INTERFACE
2788 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2790 sym
= gfc_find_dt_in_generic (sym
);
2791 ts
->u
.derived
= sym
;
2796 /* Search for the name but allow the components to be defined later. If
2797 type = -1, this typespec has been seen in a function declaration but
2798 the type could not be accessed at that point. The actual derived type is
2799 stored in a symtree with the first letter of the name capitalized; the
2800 symtree with the all lower-case name contains the associated
2801 generic function. */
2802 dt_name
= gfc_get_string ("%c%s",
2803 (char) TOUPPER ((unsigned char) name
[0]),
2804 (const char*)&name
[1]);
2809 gfc_get_ha_symbol (name
, &sym
);
2810 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2812 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2815 if (sym
->generic
&& !dt_sym
)
2816 dt_sym
= gfc_find_dt_in_generic (sym
);
2818 else if (ts
->kind
== -1)
2820 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2821 || gfc_current_ns
->has_import_set
;
2822 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2823 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2825 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2828 if (sym
&& sym
->generic
&& !dt_sym
)
2829 dt_sym
= gfc_find_dt_in_generic (sym
);
2836 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2837 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2838 || sym
->attr
.subroutine
)
2840 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2841 "entity at %L, which has the same name", name
,
2846 gfc_set_sym_referenced (sym
);
2847 if (!sym
->attr
.generic
2848 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2851 if (!sym
->attr
.function
2852 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2857 gfc_interface
*intr
, *head
;
2859 /* Use upper case to save the actual derived-type symbol. */
2860 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2861 dt_sym
->name
= gfc_get_string (sym
->name
);
2862 head
= sym
->generic
;
2863 intr
= gfc_get_interface ();
2865 intr
->where
= gfc_current_locus
;
2867 sym
->generic
= intr
;
2868 sym
->attr
.if_source
= IFSRC_DECL
;
2871 gfc_set_sym_referenced (dt_sym
);
2873 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2874 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2877 ts
->u
.derived
= dt_sym
;
2883 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2884 "intrinsic-type-spec at %C"))
2887 /* For all types except double, derived and character, look for an
2888 optional kind specifier. MATCH_NO is actually OK at this point. */
2889 if (implicit_flag
== 1)
2891 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2897 if (gfc_current_form
== FORM_FREE
)
2899 c
= gfc_peek_ascii_char ();
2900 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2901 && c
!= ':' && c
!= ',')
2903 if (matched_type
&& c
== ')')
2905 gfc_next_ascii_char ();
2912 m
= gfc_match_kind_spec (ts
, false);
2913 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2914 m
= gfc_match_old_kind_spec (ts
);
2916 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2919 /* Defer association of the KIND expression of function results
2920 until after USE and IMPORT statements. */
2921 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2922 || gfc_matching_function
)
2926 m
= MATCH_YES
; /* No kind specifier found. */
2932 /* Match an IMPLICIT NONE statement. Actually, this statement is
2933 already matched in parse.c, or we would not end up here in the
2934 first place. So the only thing we need to check, is if there is
2935 trailing garbage. If not, the match is successful. */
2938 gfc_match_implicit_none (void)
2940 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2944 /* Match the letter range(s) of an IMPLICIT statement. */
2947 match_implicit_range (void)
2953 cur_loc
= gfc_current_locus
;
2955 gfc_gobble_whitespace ();
2956 c
= gfc_next_ascii_char ();
2959 gfc_error ("Missing character range in IMPLICIT at %C");
2966 gfc_gobble_whitespace ();
2967 c1
= gfc_next_ascii_char ();
2971 gfc_gobble_whitespace ();
2972 c
= gfc_next_ascii_char ();
2977 inner
= 0; /* Fall through. */
2984 gfc_gobble_whitespace ();
2985 c2
= gfc_next_ascii_char ();
2989 gfc_gobble_whitespace ();
2990 c
= gfc_next_ascii_char ();
2992 if ((c
!= ',') && (c
!= ')'))
3005 gfc_error ("Letters must be in alphabetic order in "
3006 "IMPLICIT statement at %C");
3010 /* See if we can add the newly matched range to the pending
3011 implicits from this IMPLICIT statement. We do not check for
3012 conflicts with whatever earlier IMPLICIT statements may have
3013 set. This is done when we've successfully finished matching
3015 if (!gfc_add_new_implicit_range (c1
, c2
))
3022 gfc_syntax_error (ST_IMPLICIT
);
3024 gfc_current_locus
= cur_loc
;
3029 /* Match an IMPLICIT statement, storing the types for
3030 gfc_set_implicit() if the statement is accepted by the parser.
3031 There is a strange looking, but legal syntactic construction
3032 possible. It looks like:
3034 IMPLICIT INTEGER (a-b) (c-d)
3036 This is legal if "a-b" is a constant expression that happens to
3037 equal one of the legal kinds for integers. The real problem
3038 happens with an implicit specification that looks like:
3040 IMPLICIT INTEGER (a-b)
3042 In this case, a typespec matcher that is "greedy" (as most of the
3043 matchers are) gobbles the character range as a kindspec, leaving
3044 nothing left. We therefore have to go a bit more slowly in the
3045 matching process by inhibiting the kindspec checking during
3046 typespec matching and checking for a kind later. */
3049 gfc_match_implicit (void)
3058 /* We don't allow empty implicit statements. */
3059 if (gfc_match_eos () == MATCH_YES
)
3061 gfc_error ("Empty IMPLICIT statement at %C");
3067 /* First cleanup. */
3068 gfc_clear_new_implicit ();
3070 /* A basic type is mandatory here. */
3071 m
= gfc_match_decl_type_spec (&ts
, 1);
3072 if (m
== MATCH_ERROR
)
3077 cur_loc
= gfc_current_locus
;
3078 m
= match_implicit_range ();
3082 /* We may have <TYPE> (<RANGE>). */
3083 gfc_gobble_whitespace ();
3084 c
= gfc_next_ascii_char ();
3085 if ((c
== '\n') || (c
== ','))
3087 /* Check for CHARACTER with no length parameter. */
3088 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3090 ts
.kind
= gfc_default_character_kind
;
3091 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3092 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3096 /* Record the Successful match. */
3097 if (!gfc_merge_new_implicit (&ts
))
3102 gfc_current_locus
= cur_loc
;
3105 /* Discard the (incorrectly) matched range. */
3106 gfc_clear_new_implicit ();
3108 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3109 if (ts
.type
== BT_CHARACTER
)
3110 m
= gfc_match_char_spec (&ts
);
3113 m
= gfc_match_kind_spec (&ts
, false);
3116 m
= gfc_match_old_kind_spec (&ts
);
3117 if (m
== MATCH_ERROR
)
3123 if (m
== MATCH_ERROR
)
3126 m
= match_implicit_range ();
3127 if (m
== MATCH_ERROR
)
3132 gfc_gobble_whitespace ();
3133 c
= gfc_next_ascii_char ();
3134 if ((c
!= '\n') && (c
!= ','))
3137 if (!gfc_merge_new_implicit (&ts
))
3145 gfc_syntax_error (ST_IMPLICIT
);
3153 gfc_match_import (void)
3155 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3160 if (gfc_current_ns
->proc_name
== NULL
3161 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3163 gfc_error ("IMPORT statement at %C only permitted in "
3164 "an INTERFACE body");
3168 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3171 if (gfc_match_eos () == MATCH_YES
)
3173 /* All host variables should be imported. */
3174 gfc_current_ns
->has_import_set
= 1;
3178 if (gfc_match (" ::") == MATCH_YES
)
3180 if (gfc_match_eos () == MATCH_YES
)
3182 gfc_error ("Expecting list of named entities at %C");
3190 m
= gfc_match (" %n", name
);
3194 if (gfc_current_ns
->parent
!= NULL
3195 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3197 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3200 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3201 && gfc_find_symbol (name
,
3202 gfc_current_ns
->proc_name
->ns
->parent
,
3205 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3211 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3212 "at %C - does not exist.", name
);
3216 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3218 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3223 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3226 sym
->attr
.imported
= 1;
3228 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3230 /* The actual derived type is stored in a symtree with the first
3231 letter of the name capitalized; the symtree with the all
3232 lower-case name contains the associated generic function. */
3233 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3234 gfc_get_string ("%c%s",
3235 (char) TOUPPER ((unsigned char) name
[0]),
3239 sym
->attr
.imported
= 1;
3252 if (gfc_match_eos () == MATCH_YES
)
3254 if (gfc_match_char (',') != MATCH_YES
)
3261 gfc_error ("Syntax error in IMPORT statement at %C");
3266 /* A minimal implementation of gfc_match without whitespace, escape
3267 characters or variable arguments. Returns true if the next
3268 characters match the TARGET template exactly. */
3271 match_string_p (const char *target
)
3275 for (p
= target
; *p
; p
++)
3276 if ((char) gfc_next_ascii_char () != *p
)
3281 /* Matches an attribute specification including array specs. If
3282 successful, leaves the variables current_attr and current_as
3283 holding the specification. Also sets the colon_seen variable for
3284 later use by matchers associated with initializations.
3286 This subroutine is a little tricky in the sense that we don't know
3287 if we really have an attr-spec until we hit the double colon.
3288 Until that time, we can only return MATCH_NO. This forces us to
3289 check for duplicate specification at this level. */
3292 match_attr_spec (void)
3294 /* Modifiers that can exist in a type statement. */
3296 { GFC_DECL_BEGIN
= 0,
3297 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3298 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3299 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3300 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3301 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3302 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3305 /* GFC_DECL_END is the sentinel, index starts at 0. */
3306 #define NUM_DECL GFC_DECL_END
3308 locus start
, seen_at
[NUM_DECL
];
3315 gfc_clear_attr (¤t_attr
);
3316 start
= gfc_current_locus
;
3321 /* See if we get all of the keywords up to the final double colon. */
3322 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3330 gfc_gobble_whitespace ();
3332 ch
= gfc_next_ascii_char ();
3335 /* This is the successful exit condition for the loop. */
3336 if (gfc_next_ascii_char () == ':')
3341 gfc_gobble_whitespace ();
3342 switch (gfc_peek_ascii_char ())
3345 gfc_next_ascii_char ();
3346 switch (gfc_next_ascii_char ())
3349 if (match_string_p ("locatable"))
3351 /* Matched "allocatable". */
3352 d
= DECL_ALLOCATABLE
;
3357 if (match_string_p ("ynchronous"))
3359 /* Matched "asynchronous". */
3360 d
= DECL_ASYNCHRONOUS
;
3367 /* Try and match the bind(c). */
3368 m
= gfc_match_bind_c (NULL
, true);
3371 else if (m
== MATCH_ERROR
)
3376 gfc_next_ascii_char ();
3377 if ('o' != gfc_next_ascii_char ())
3379 switch (gfc_next_ascii_char ())
3382 if (match_string_p ("imension"))
3384 d
= DECL_CODIMENSION
;
3388 if (match_string_p ("tiguous"))
3390 d
= DECL_CONTIGUOUS
;
3397 if (match_string_p ("dimension"))
3402 if (match_string_p ("external"))
3407 if (match_string_p ("int"))
3409 ch
= gfc_next_ascii_char ();
3412 if (match_string_p ("nt"))
3414 /* Matched "intent". */
3415 /* TODO: Call match_intent_spec from here. */
3416 if (gfc_match (" ( in out )") == MATCH_YES
)
3418 else if (gfc_match (" ( in )") == MATCH_YES
)
3420 else if (gfc_match (" ( out )") == MATCH_YES
)
3426 if (match_string_p ("insic"))
3428 /* Matched "intrinsic". */
3436 if (match_string_p ("optional"))
3441 gfc_next_ascii_char ();
3442 switch (gfc_next_ascii_char ())
3445 if (match_string_p ("rameter"))
3447 /* Matched "parameter". */
3453 if (match_string_p ("inter"))
3455 /* Matched "pointer". */
3461 ch
= gfc_next_ascii_char ();
3464 if (match_string_p ("vate"))
3466 /* Matched "private". */
3472 if (match_string_p ("tected"))
3474 /* Matched "protected". */
3481 if (match_string_p ("blic"))
3483 /* Matched "public". */
3491 if (match_string_p ("save"))
3496 if (match_string_p ("target"))
3501 gfc_next_ascii_char ();
3502 ch
= gfc_next_ascii_char ();
3505 if (match_string_p ("lue"))
3507 /* Matched "value". */
3513 if (match_string_p ("latile"))
3515 /* Matched "volatile". */
3523 /* No double colon and no recognizable decl_type, so assume that
3524 we've been looking at something else the whole time. */
3531 /* Check to make sure any parens are paired up correctly. */
3532 if (gfc_match_parens () == MATCH_ERROR
)
3539 seen_at
[d
] = gfc_current_locus
;
3541 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3543 gfc_array_spec
*as
= NULL
;
3545 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3546 d
== DECL_CODIMENSION
);
3548 if (current_as
== NULL
)
3550 else if (m
== MATCH_YES
)
3552 if (!merge_array_spec (as
, current_as
, false))
3559 if (d
== DECL_CODIMENSION
)
3560 gfc_error ("Missing codimension specification at %C");
3562 gfc_error ("Missing dimension specification at %C");
3566 if (m
== MATCH_ERROR
)
3571 /* Since we've seen a double colon, we have to be looking at an
3572 attr-spec. This means that we can now issue errors. */
3573 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3578 case DECL_ALLOCATABLE
:
3579 attr
= "ALLOCATABLE";
3581 case DECL_ASYNCHRONOUS
:
3582 attr
= "ASYNCHRONOUS";
3584 case DECL_CODIMENSION
:
3585 attr
= "CODIMENSION";
3587 case DECL_CONTIGUOUS
:
3588 attr
= "CONTIGUOUS";
3590 case DECL_DIMENSION
:
3597 attr
= "INTENT (IN)";
3600 attr
= "INTENT (OUT)";
3603 attr
= "INTENT (IN OUT)";
3605 case DECL_INTRINSIC
:
3611 case DECL_PARAMETER
:
3617 case DECL_PROTECTED
:
3632 case DECL_IS_BIND_C
:
3642 attr
= NULL
; /* This shouldn't happen. */
3645 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3650 /* Now that we've dealt with duplicate attributes, add the attributes
3651 to the current attribute. */
3652 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3657 if (gfc_current_state () == COMP_DERIVED
3658 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3659 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3660 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3662 if (d
== DECL_ALLOCATABLE
)
3664 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3665 "attribute at %C in a TYPE definition"))
3673 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3680 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3681 && gfc_current_state () != COMP_MODULE
)
3683 if (d
== DECL_PRIVATE
)
3687 if (gfc_current_state () == COMP_DERIVED
3688 && gfc_state_stack
->previous
3689 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3691 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3692 "at %L in a TYPE definition", attr
,
3701 gfc_error ("%s attribute at %L is not allowed outside of the "
3702 "specification part of a module", attr
, &seen_at
[d
]);
3710 case DECL_ALLOCATABLE
:
3711 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3714 case DECL_ASYNCHRONOUS
:
3715 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3718 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3721 case DECL_CODIMENSION
:
3722 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3725 case DECL_CONTIGUOUS
:
3726 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3729 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3732 case DECL_DIMENSION
:
3733 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3737 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3741 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3745 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3749 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3752 case DECL_INTRINSIC
:
3753 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3757 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3760 case DECL_PARAMETER
:
3761 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3765 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3768 case DECL_PROTECTED
:
3769 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3771 gfc_error ("PROTECTED at %C only allowed in specification "
3772 "part of a module");
3777 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3780 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3784 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3789 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3794 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3798 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3801 case DECL_IS_BIND_C
:
3802 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3806 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3809 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3813 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3816 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3820 gfc_internal_error ("match_attr_spec(): Bad attribute");
3830 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3831 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3832 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3833 current_attr
.save
= SAVE_IMPLICIT
;
3839 gfc_current_locus
= start
;
3840 gfc_free_array_spec (current_as
);
3846 /* Set the binding label, dest_label, either with the binding label
3847 stored in the given gfc_typespec, ts, or if none was provided, it
3848 will be the symbol name in all lower case, as required by the draft
3849 (J3/04-007, section 15.4.1). If a binding label was given and
3850 there is more than one argument (num_idents), it is an error. */
3853 set_binding_label (const char **dest_label
, const char *sym_name
,
3856 if (num_idents
> 1 && has_name_equals
)
3858 gfc_error ("Multiple identifiers provided with "
3859 "single NAME= specifier at %C");
3863 if (curr_binding_label
)
3864 /* Binding label given; store in temp holder till have sym. */
3865 *dest_label
= curr_binding_label
;
3868 /* No binding label given, and the NAME= specifier did not exist,
3869 which means there was no NAME="". */
3870 if (sym_name
!= NULL
&& has_name_equals
== 0)
3871 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3878 /* Set the status of the given common block as being BIND(C) or not,
3879 depending on the given parameter, is_bind_c. */
3882 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3884 com_block
->is_bind_c
= is_bind_c
;
3889 /* Verify that the given gfc_typespec is for a C interoperable type. */
3892 gfc_verify_c_interop (gfc_typespec
*ts
)
3894 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3895 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3897 else if (ts
->type
== BT_CLASS
)
3899 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
3906 /* Verify that the variables of a given common block, which has been
3907 defined with the attribute specifier bind(c), to be of a C
3908 interoperable type. Errors will be reported here, if
3912 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3914 gfc_symbol
*curr_sym
= NULL
;
3917 curr_sym
= com_block
->head
;
3919 /* Make sure we have at least one symbol. */
3920 if (curr_sym
== NULL
)
3923 /* Here we know we have a symbol, so we'll execute this loop
3927 /* The second to last param, 1, says this is in a common block. */
3928 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3929 curr_sym
= curr_sym
->common_next
;
3930 } while (curr_sym
!= NULL
);
3936 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3937 an appropriate error message is reported. */
3940 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3941 int is_in_common
, gfc_common_head
*com_block
)
3943 bool bind_c_function
= false;
3946 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3947 bind_c_function
= true;
3949 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3951 tmp_sym
= tmp_sym
->result
;
3952 /* Make sure it wasn't an implicitly typed result. */
3953 if (tmp_sym
->attr
.implicit_type
&& gfc_option
.warn_c_binding_type
)
3955 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3956 "%L may not be C interoperable", tmp_sym
->name
,
3957 &tmp_sym
->declared_at
);
3958 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3959 /* Mark it as C interoperable to prevent duplicate warnings. */
3960 tmp_sym
->ts
.is_c_interop
= 1;
3961 tmp_sym
->attr
.is_c_interop
= 1;
3965 /* Here, we know we have the bind(c) attribute, so if we have
3966 enough type info, then verify that it's a C interop kind.
3967 The info could be in the symbol already, or possibly still in
3968 the given ts (current_ts), so look in both. */
3969 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3971 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
3973 /* See if we're dealing with a sym in a common block or not. */
3974 if (is_in_common
== 1 && gfc_option
.warn_c_binding_type
)
3976 gfc_warning ("Variable '%s' in common block '%s' at %L "
3977 "may not be a C interoperable "
3978 "kind though common block '%s' is BIND(C)",
3979 tmp_sym
->name
, com_block
->name
,
3980 &(tmp_sym
->declared_at
), com_block
->name
);
3984 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3985 gfc_error ("Type declaration '%s' at %L is not C "
3986 "interoperable but it is BIND(C)",
3987 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3988 else if (gfc_option
.warn_c_binding_type
)
3989 gfc_warning ("Variable '%s' at %L "
3990 "may not be a C interoperable "
3991 "kind but it is bind(c)",
3992 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3996 /* Variables declared w/in a common block can't be bind(c)
3997 since there's no way for C to see these variables, so there's
3998 semantically no reason for the attribute. */
3999 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4001 gfc_error ("Variable '%s' in common block '%s' at "
4002 "%L cannot be declared with BIND(C) "
4003 "since it is not a global",
4004 tmp_sym
->name
, com_block
->name
,
4005 &(tmp_sym
->declared_at
));
4009 /* Scalar variables that are bind(c) can not have the pointer
4010 or allocatable attributes. */
4011 if (tmp_sym
->attr
.is_bind_c
== 1)
4013 if (tmp_sym
->attr
.pointer
== 1)
4015 gfc_error ("Variable '%s' at %L cannot have both the "
4016 "POINTER and BIND(C) attributes",
4017 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4021 if (tmp_sym
->attr
.allocatable
== 1)
4023 gfc_error ("Variable '%s' at %L cannot have both the "
4024 "ALLOCATABLE and BIND(C) attributes",
4025 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4031 /* If it is a BIND(C) function, make sure the return value is a
4032 scalar value. The previous tests in this function made sure
4033 the type is interoperable. */
4034 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4035 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4036 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4038 /* BIND(C) functions can not return a character string. */
4039 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4040 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4041 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4042 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4043 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4044 "be a character string", tmp_sym
->name
,
4045 &(tmp_sym
->declared_at
));
4048 /* See if the symbol has been marked as private. If it has, make sure
4049 there is no binding label and warn the user if there is one. */
4050 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4051 && tmp_sym
->binding_label
)
4052 /* Use gfc_warning_now because we won't say that the symbol fails
4053 just because of this. */
4054 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4055 "given the binding label '%s'", tmp_sym
->name
,
4056 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4062 /* Set the appropriate fields for a symbol that's been declared as
4063 BIND(C) (the is_bind_c flag and the binding label), and verify that
4064 the type is C interoperable. Errors are reported by the functions
4065 used to set/test these fields. */
4068 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4072 /* TODO: Do we need to make sure the vars aren't marked private? */
4074 /* Set the is_bind_c bit in symbol_attribute. */
4075 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4077 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4084 /* Set the fields marking the given common block as BIND(C), including
4085 a binding label, and report any errors encountered. */
4088 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4092 /* destLabel, common name, typespec (which may have binding label). */
4093 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4097 /* Set the given common block (com_block) to being bind(c) (1). */
4098 set_com_block_bind_c (com_block
, 1);
4104 /* Retrieve the list of one or more identifiers that the given bind(c)
4105 attribute applies to. */
4108 get_bind_c_idents (void)
4110 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4112 gfc_symbol
*tmp_sym
= NULL
;
4114 gfc_common_head
*com_block
= NULL
;
4116 if (gfc_match_name (name
) == MATCH_YES
)
4118 found_id
= MATCH_YES
;
4119 gfc_get_ha_symbol (name
, &tmp_sym
);
4121 else if (match_common_name (name
) == MATCH_YES
)
4123 found_id
= MATCH_YES
;
4124 com_block
= gfc_get_common (name
, 0);
4128 gfc_error ("Need either entity or common block name for "
4129 "attribute specification statement at %C");
4133 /* Save the current identifier and look for more. */
4136 /* Increment the number of identifiers found for this spec stmt. */
4139 /* Make sure we have a sym or com block, and verify that it can
4140 be bind(c). Set the appropriate field(s) and look for more
4142 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4144 if (tmp_sym
!= NULL
)
4146 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4151 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4155 /* Look to see if we have another identifier. */
4157 if (gfc_match_eos () == MATCH_YES
)
4158 found_id
= MATCH_NO
;
4159 else if (gfc_match_char (',') != MATCH_YES
)
4160 found_id
= MATCH_NO
;
4161 else if (gfc_match_name (name
) == MATCH_YES
)
4163 found_id
= MATCH_YES
;
4164 gfc_get_ha_symbol (name
, &tmp_sym
);
4166 else if (match_common_name (name
) == MATCH_YES
)
4168 found_id
= MATCH_YES
;
4169 com_block
= gfc_get_common (name
, 0);
4173 gfc_error ("Missing entity or common block name for "
4174 "attribute specification statement at %C");
4180 gfc_internal_error ("Missing symbol");
4182 } while (found_id
== MATCH_YES
);
4184 /* if we get here we were successful */
4189 /* Try and match a BIND(C) attribute specification statement. */
4192 gfc_match_bind_c_stmt (void)
4194 match found_match
= MATCH_NO
;
4199 /* This may not be necessary. */
4201 /* Clear the temporary binding label holder. */
4202 curr_binding_label
= NULL
;
4204 /* Look for the bind(c). */
4205 found_match
= gfc_match_bind_c (NULL
, true);
4207 if (found_match
== MATCH_YES
)
4209 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4212 /* Look for the :: now, but it is not required. */
4215 /* Get the identifier(s) that needs to be updated. This may need to
4216 change to hand the flag(s) for the attr specified so all identifiers
4217 found can have all appropriate parts updated (assuming that the same
4218 spec stmt can have multiple attrs, such as both bind(c) and
4220 if (!get_bind_c_idents ())
4221 /* Error message should have printed already. */
4229 /* Match a data declaration statement. */
4232 gfc_match_data_decl (void)
4238 num_idents_on_line
= 0;
4240 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4244 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4245 && gfc_current_state () != COMP_DERIVED
)
4247 sym
= gfc_use_derived (current_ts
.u
.derived
);
4255 current_ts
.u
.derived
= sym
;
4258 m
= match_attr_spec ();
4259 if (m
== MATCH_ERROR
)
4265 if (current_ts
.type
== BT_CLASS
4266 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4269 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4270 && current_ts
.u
.derived
->components
== NULL
4271 && !current_ts
.u
.derived
->attr
.zero_comp
)
4274 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4277 gfc_find_symbol (current_ts
.u
.derived
->name
,
4278 current_ts
.u
.derived
->ns
, 1, &sym
);
4280 /* Any symbol that we find had better be a type definition
4281 which has its components defined. */
4282 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4283 && (current_ts
.u
.derived
->components
!= NULL
4284 || current_ts
.u
.derived
->attr
.zero_comp
))
4287 gfc_error ("Derived type at %C has not been previously defined "
4288 "and so cannot appear in a derived type definition");
4294 /* If we have an old-style character declaration, and no new-style
4295 attribute specifications, then there a comma is optional between
4296 the type specification and the variable list. */
4297 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4298 gfc_match_char (',');
4300 /* Give the types/attributes to symbols that follow. Give the element
4301 a number so that repeat character length expressions can be copied. */
4305 num_idents_on_line
++;
4306 m
= variable_decl (elem
++);
4307 if (m
== MATCH_ERROR
)
4312 if (gfc_match_eos () == MATCH_YES
)
4314 if (gfc_match_char (',') != MATCH_YES
)
4318 if (gfc_error_flag_test () == 0)
4319 gfc_error ("Syntax error in data declaration at %C");
4322 gfc_free_data_all (gfc_current_ns
);
4325 gfc_free_array_spec (current_as
);
4331 /* Match a prefix associated with a function or subroutine
4332 declaration. If the typespec pointer is nonnull, then a typespec
4333 can be matched. Note that if nothing matches, MATCH_YES is
4334 returned (the null string was matched). */
4337 gfc_match_prefix (gfc_typespec
*ts
)
4343 gfc_clear_attr (¤t_attr
);
4345 seen_impure
= false;
4347 gcc_assert (!gfc_matching_prefix
);
4348 gfc_matching_prefix
= true;
4352 found_prefix
= false;
4354 if (!seen_type
&& ts
!= NULL
4355 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4356 && gfc_match_space () == MATCH_YES
)
4360 found_prefix
= true;
4363 if (gfc_match ("elemental% ") == MATCH_YES
)
4365 if (!gfc_add_elemental (¤t_attr
, NULL
))
4368 found_prefix
= true;
4371 if (gfc_match ("pure% ") == MATCH_YES
)
4373 if (!gfc_add_pure (¤t_attr
, NULL
))
4376 found_prefix
= true;
4379 if (gfc_match ("recursive% ") == MATCH_YES
)
4381 if (!gfc_add_recursive (¤t_attr
, NULL
))
4384 found_prefix
= true;
4387 /* IMPURE is a somewhat special case, as it needs not set an actual
4388 attribute but rather only prevents ELEMENTAL routines from being
4389 automatically PURE. */
4390 if (gfc_match ("impure% ") == MATCH_YES
)
4392 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4396 found_prefix
= true;
4399 while (found_prefix
);
4401 /* IMPURE and PURE must not both appear, of course. */
4402 if (seen_impure
&& current_attr
.pure
)
4404 gfc_error ("PURE and IMPURE must not appear both at %C");
4408 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4409 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4411 if (!gfc_add_pure (¤t_attr
, NULL
))
4415 /* At this point, the next item is not a prefix. */
4416 gcc_assert (gfc_matching_prefix
);
4417 gfc_matching_prefix
= false;
4421 gcc_assert (gfc_matching_prefix
);
4422 gfc_matching_prefix
= false;
4427 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4430 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4432 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4435 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4438 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4445 /* Match a formal argument list. */
4448 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4450 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4451 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4457 if (gfc_match_char ('(') != MATCH_YES
)
4464 if (gfc_match_char (')') == MATCH_YES
)
4469 if (gfc_match_char ('*') == MATCH_YES
)
4472 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4481 m
= gfc_match_name (name
);
4485 if (gfc_get_symbol (name
, NULL
, &sym
))
4489 p
= gfc_get_formal_arglist ();
4501 /* We don't add the VARIABLE flavor because the name could be a
4502 dummy procedure. We don't apply these attributes to formal
4503 arguments of statement functions. */
4504 if (sym
!= NULL
&& !st_flag
4505 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4506 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4512 /* The name of a program unit can be in a different namespace,
4513 so check for it explicitly. After the statement is accepted,
4514 the name is checked for especially in gfc_get_symbol(). */
4515 if (gfc_new_block
!= NULL
&& sym
!= NULL
4516 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4518 gfc_error ("Name '%s' at %C is the name of the procedure",
4524 if (gfc_match_char (')') == MATCH_YES
)
4527 m
= gfc_match_char (',');
4530 gfc_error ("Unexpected junk in formal argument list at %C");
4536 /* Check for duplicate symbols in the formal argument list. */
4539 for (p
= head
; p
->next
; p
= p
->next
)
4544 for (q
= p
->next
; q
; q
= q
->next
)
4545 if (p
->sym
== q
->sym
)
4547 gfc_error ("Duplicate symbol '%s' in formal argument list "
4548 "at %C", p
->sym
->name
);
4556 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4565 gfc_free_formal_arglist (head
);
4570 /* Match a RESULT specification following a function declaration or
4571 ENTRY statement. Also matches the end-of-statement. */
4574 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4576 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4580 if (gfc_match (" result (") != MATCH_YES
)
4583 m
= gfc_match_name (name
);
4587 /* Get the right paren, and that's it because there could be the
4588 bind(c) attribute after the result clause. */
4589 if (gfc_match_char (')') != MATCH_YES
)
4591 /* TODO: should report the missing right paren here. */
4595 if (strcmp (function
->name
, name
) == 0)
4597 gfc_error ("RESULT variable at %C must be different than function name");
4601 if (gfc_get_symbol (name
, NULL
, &r
))
4604 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4613 /* Match a function suffix, which could be a combination of a result
4614 clause and BIND(C), either one, or neither. The draft does not
4615 require them to come in a specific order. */
4618 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4620 match is_bind_c
; /* Found bind(c). */
4621 match is_result
; /* Found result clause. */
4622 match found_match
; /* Status of whether we've found a good match. */
4623 char peek_char
; /* Character we're going to peek at. */
4624 bool allow_binding_name
;
4626 /* Initialize to having found nothing. */
4627 found_match
= MATCH_NO
;
4628 is_bind_c
= MATCH_NO
;
4629 is_result
= MATCH_NO
;
4631 /* Get the next char to narrow between result and bind(c). */
4632 gfc_gobble_whitespace ();
4633 peek_char
= gfc_peek_ascii_char ();
4635 /* C binding names are not allowed for internal procedures. */
4636 if (gfc_current_state () == COMP_CONTAINS
4637 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4638 allow_binding_name
= false;
4640 allow_binding_name
= true;
4645 /* Look for result clause. */
4646 is_result
= match_result (sym
, result
);
4647 if (is_result
== MATCH_YES
)
4649 /* Now see if there is a bind(c) after it. */
4650 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4651 /* We've found the result clause and possibly bind(c). */
4652 found_match
= MATCH_YES
;
4655 /* This should only be MATCH_ERROR. */
4656 found_match
= is_result
;
4659 /* Look for bind(c) first. */
4660 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4661 if (is_bind_c
== MATCH_YES
)
4663 /* Now see if a result clause followed it. */
4664 is_result
= match_result (sym
, result
);
4665 found_match
= MATCH_YES
;
4669 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4670 found_match
= MATCH_ERROR
;
4674 gfc_error ("Unexpected junk after function declaration at %C");
4675 found_match
= MATCH_ERROR
;
4679 if (is_bind_c
== MATCH_YES
)
4681 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4682 if (gfc_current_state () == COMP_CONTAINS
4683 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4684 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4685 "at %L may not be specified for an internal "
4686 "procedure", &gfc_current_locus
))
4689 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4697 /* Procedure pointer return value without RESULT statement:
4698 Add "hidden" result variable named "ppr@". */
4701 add_hidden_procptr_result (gfc_symbol
*sym
)
4705 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4708 /* First usage case: PROCEDURE and EXTERNAL statements. */
4709 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4710 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4711 && sym
->attr
.external
;
4712 /* Second usage case: INTERFACE statements. */
4713 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4714 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4715 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4721 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4725 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4726 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4727 st2
->n
.sym
= stree
->n
.sym
;
4729 sym
->result
= stree
->n
.sym
;
4731 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4732 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4733 sym
->result
->attr
.external
= sym
->attr
.external
;
4734 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4735 sym
->result
->ts
= sym
->ts
;
4736 sym
->attr
.proc_pointer
= 0;
4737 sym
->attr
.pointer
= 0;
4738 sym
->attr
.external
= 0;
4739 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4741 sym
->result
->attr
.pointer
= 0;
4742 sym
->result
->attr
.proc_pointer
= 1;
4745 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4747 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4748 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4749 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4750 && sym
== gfc_current_ns
->proc_name
4751 && sym
== sym
->result
->ns
->proc_name
4752 && strcmp ("ppr@", sym
->result
->name
) == 0)
4754 sym
->result
->attr
.proc_pointer
= 1;
4755 sym
->attr
.pointer
= 0;
4763 /* Match the interface for a PROCEDURE declaration,
4764 including brackets (R1212). */
4767 match_procedure_interface (gfc_symbol
**proc_if
)
4771 locus old_loc
, entry_loc
;
4772 gfc_namespace
*old_ns
= gfc_current_ns
;
4773 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4775 old_loc
= entry_loc
= gfc_current_locus
;
4776 gfc_clear_ts (¤t_ts
);
4778 if (gfc_match (" (") != MATCH_YES
)
4780 gfc_current_locus
= entry_loc
;
4784 /* Get the type spec. for the procedure interface. */
4785 old_loc
= gfc_current_locus
;
4786 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4787 gfc_gobble_whitespace ();
4788 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4791 if (m
== MATCH_ERROR
)
4794 /* Procedure interface is itself a procedure. */
4795 gfc_current_locus
= old_loc
;
4796 m
= gfc_match_name (name
);
4798 /* First look to see if it is already accessible in the current
4799 namespace because it is use associated or contained. */
4801 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4804 /* If it is still not found, then try the parent namespace, if it
4805 exists and create the symbol there if it is still not found. */
4806 if (gfc_current_ns
->parent
)
4807 gfc_current_ns
= gfc_current_ns
->parent
;
4808 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4811 gfc_current_ns
= old_ns
;
4812 *proc_if
= st
->n
.sym
;
4817 /* Resolve interface if possible. That way, attr.procedure is only set
4818 if it is declared by a later procedure-declaration-stmt, which is
4819 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4820 while ((*proc_if
)->ts
.interface
)
4821 *proc_if
= (*proc_if
)->ts
.interface
;
4823 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4824 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4825 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4826 (*proc_if
)->name
, NULL
))
4831 if (gfc_match (" )") != MATCH_YES
)
4833 gfc_current_locus
= entry_loc
;
4841 /* Match a PROCEDURE declaration (R1211). */
4844 match_procedure_decl (void)
4847 gfc_symbol
*sym
, *proc_if
= NULL
;
4849 gfc_expr
*initializer
= NULL
;
4851 /* Parse interface (with brackets). */
4852 m
= match_procedure_interface (&proc_if
);
4856 /* Parse attributes (with colons). */
4857 m
= match_attr_spec();
4858 if (m
== MATCH_ERROR
)
4861 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4863 current_attr
.is_bind_c
= 1;
4864 has_name_equals
= 0;
4865 curr_binding_label
= NULL
;
4868 /* Get procedure symbols. */
4871 m
= gfc_match_symbol (&sym
, 0);
4874 else if (m
== MATCH_ERROR
)
4877 /* Add current_attr to the symbol attributes. */
4878 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4881 if (sym
->attr
.is_bind_c
)
4883 /* Check for C1218. */
4884 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4886 gfc_error ("BIND(C) attribute at %C requires "
4887 "an interface with BIND(C)");
4890 /* Check for C1217. */
4891 if (has_name_equals
&& sym
->attr
.pointer
)
4893 gfc_error ("BIND(C) procedure with NAME may not have "
4894 "POINTER attribute at %C");
4897 if (has_name_equals
&& sym
->attr
.dummy
)
4899 gfc_error ("Dummy procedure at %C may not have "
4900 "BIND(C) attribute with NAME");
4903 /* Set binding label for BIND(C). */
4904 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
4908 if (!gfc_add_external (&sym
->attr
, NULL
))
4911 if (add_hidden_procptr_result (sym
))
4914 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
4917 /* Set interface. */
4918 if (proc_if
!= NULL
)
4920 if (sym
->ts
.type
!= BT_UNKNOWN
)
4922 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4923 sym
->name
, &gfc_current_locus
,
4924 gfc_basic_typename (sym
->ts
.type
));
4927 sym
->ts
.interface
= proc_if
;
4928 sym
->attr
.untyped
= 1;
4929 sym
->attr
.if_source
= IFSRC_IFBODY
;
4931 else if (current_ts
.type
!= BT_UNKNOWN
)
4933 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
4935 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4936 sym
->ts
.interface
->ts
= current_ts
;
4937 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4938 sym
->ts
.interface
->attr
.function
= 1;
4939 sym
->attr
.function
= 1;
4940 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4943 if (gfc_match (" =>") == MATCH_YES
)
4945 if (!current_attr
.pointer
)
4947 gfc_error ("Initialization at %C isn't for a pointer variable");
4952 m
= match_pointer_init (&initializer
, 1);
4956 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
4961 if (gfc_match_eos () == MATCH_YES
)
4963 if (gfc_match_char (',') != MATCH_YES
)
4968 gfc_error ("Syntax error in PROCEDURE statement at %C");
4972 /* Free stuff up and return. */
4973 gfc_free_expr (initializer
);
4979 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4982 /* Match a procedure pointer component declaration (R445). */
4985 match_ppc_decl (void)
4988 gfc_symbol
*proc_if
= NULL
;
4992 gfc_expr
*initializer
= NULL
;
4993 gfc_typebound_proc
* tb
;
4994 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4996 /* Parse interface (with brackets). */
4997 m
= match_procedure_interface (&proc_if
);
5001 /* Parse attributes. */
5002 tb
= XCNEW (gfc_typebound_proc
);
5003 tb
->where
= gfc_current_locus
;
5004 m
= match_binding_attributes (tb
, false, true);
5005 if (m
== MATCH_ERROR
)
5008 gfc_clear_attr (¤t_attr
);
5009 current_attr
.procedure
= 1;
5010 current_attr
.proc_pointer
= 1;
5011 current_attr
.access
= tb
->access
;
5012 current_attr
.flavor
= FL_PROCEDURE
;
5014 /* Match the colons (required). */
5015 if (gfc_match (" ::") != MATCH_YES
)
5017 gfc_error ("Expected '::' after binding-attributes at %C");
5021 /* Check for C450. */
5022 if (!tb
->nopass
&& proc_if
== NULL
)
5024 gfc_error("NOPASS or explicit interface required at %C");
5028 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5031 /* Match PPC names. */
5035 m
= gfc_match_name (name
);
5038 else if (m
== MATCH_ERROR
)
5041 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5044 /* Add current_attr to the symbol attributes. */
5045 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5048 if (!gfc_add_external (&c
->attr
, NULL
))
5051 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5058 c
->tb
= XCNEW (gfc_typebound_proc
);
5059 c
->tb
->where
= gfc_current_locus
;
5063 /* Set interface. */
5064 if (proc_if
!= NULL
)
5066 c
->ts
.interface
= proc_if
;
5067 c
->attr
.untyped
= 1;
5068 c
->attr
.if_source
= IFSRC_IFBODY
;
5070 else if (ts
.type
!= BT_UNKNOWN
)
5073 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5074 c
->ts
.interface
->result
= c
->ts
.interface
;
5075 c
->ts
.interface
->ts
= ts
;
5076 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5077 c
->ts
.interface
->attr
.function
= 1;
5078 c
->attr
.function
= 1;
5079 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5082 if (gfc_match (" =>") == MATCH_YES
)
5084 m
= match_pointer_init (&initializer
, 1);
5087 gfc_free_expr (initializer
);
5090 c
->initializer
= initializer
;
5093 if (gfc_match_eos () == MATCH_YES
)
5095 if (gfc_match_char (',') != MATCH_YES
)
5100 gfc_error ("Syntax error in procedure pointer component at %C");
5105 /* Match a PROCEDURE declaration inside an interface (R1206). */
5108 match_procedure_in_interface (void)
5112 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5115 if (current_interface
.type
== INTERFACE_NAMELESS
5116 || current_interface
.type
== INTERFACE_ABSTRACT
)
5118 gfc_error ("PROCEDURE at %C must be in a generic interface");
5122 /* Check if the F2008 optional double colon appears. */
5123 gfc_gobble_whitespace ();
5124 old_locus
= gfc_current_locus
;
5125 if (gfc_match ("::") == MATCH_YES
)
5127 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5128 "MODULE PROCEDURE statement at %L", &old_locus
))
5132 gfc_current_locus
= old_locus
;
5136 m
= gfc_match_name (name
);
5139 else if (m
== MATCH_ERROR
)
5141 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5144 if (!gfc_add_interface (sym
))
5147 if (gfc_match_eos () == MATCH_YES
)
5149 if (gfc_match_char (',') != MATCH_YES
)
5156 gfc_error ("Syntax error in PROCEDURE statement at %C");
5161 /* General matcher for PROCEDURE declarations. */
5163 static match
match_procedure_in_type (void);
5166 gfc_match_procedure (void)
5170 switch (gfc_current_state ())
5175 case COMP_SUBROUTINE
:
5178 m
= match_procedure_decl ();
5180 case COMP_INTERFACE
:
5181 m
= match_procedure_in_interface ();
5184 m
= match_ppc_decl ();
5186 case COMP_DERIVED_CONTAINS
:
5187 m
= match_procedure_in_type ();
5196 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5203 /* Warn if a matched procedure has the same name as an intrinsic; this is
5204 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5205 parser-state-stack to find out whether we're in a module. */
5208 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5212 in_module
= (gfc_state_stack
->previous
5213 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5215 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5219 /* Match a function declaration. */
5222 gfc_match_function_decl (void)
5224 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5225 gfc_symbol
*sym
, *result
;
5229 match found_match
; /* Status returned by match func. */
5231 if (gfc_current_state () != COMP_NONE
5232 && gfc_current_state () != COMP_INTERFACE
5233 && gfc_current_state () != COMP_CONTAINS
)
5236 gfc_clear_ts (¤t_ts
);
5238 old_loc
= gfc_current_locus
;
5240 m
= gfc_match_prefix (¤t_ts
);
5243 gfc_current_locus
= old_loc
;
5247 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5249 gfc_current_locus
= old_loc
;
5252 if (get_proc_name (name
, &sym
, false))
5255 if (add_hidden_procptr_result (sym
))
5258 gfc_new_block
= sym
;
5260 m
= gfc_match_formal_arglist (sym
, 0, 0);
5263 gfc_error ("Expected formal argument list in function "
5264 "definition at %C");
5268 else if (m
== MATCH_ERROR
)
5273 /* According to the draft, the bind(c) and result clause can
5274 come in either order after the formal_arg_list (i.e., either
5275 can be first, both can exist together or by themselves or neither
5276 one). Therefore, the match_result can't match the end of the
5277 string, and check for the bind(c) or result clause in either order. */
5278 found_match
= gfc_match_eos ();
5280 /* Make sure that it isn't already declared as BIND(C). If it is, it
5281 must have been marked BIND(C) with a BIND(C) attribute and that is
5282 not allowed for procedures. */
5283 if (sym
->attr
.is_bind_c
== 1)
5285 sym
->attr
.is_bind_c
= 0;
5286 if (sym
->old_symbol
!= NULL
)
5287 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5288 "variables or common blocks",
5289 &(sym
->old_symbol
->declared_at
));
5291 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5292 "variables or common blocks", &gfc_current_locus
);
5295 if (found_match
!= MATCH_YES
)
5297 /* If we haven't found the end-of-statement, look for a suffix. */
5298 suffix_match
= gfc_match_suffix (sym
, &result
);
5299 if (suffix_match
== MATCH_YES
)
5300 /* Need to get the eos now. */
5301 found_match
= gfc_match_eos ();
5303 found_match
= suffix_match
;
5306 if(found_match
!= MATCH_YES
)
5310 /* Make changes to the symbol. */
5313 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5316 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5317 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5320 /* Delay matching the function characteristics until after the
5321 specification block by signalling kind=-1. */
5322 sym
->declared_at
= old_loc
;
5323 if (current_ts
.type
!= BT_UNKNOWN
)
5324 current_ts
.kind
= -1;
5326 current_ts
.kind
= 0;
5330 if (current_ts
.type
!= BT_UNKNOWN
5331 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5337 if (current_ts
.type
!= BT_UNKNOWN
5338 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5340 sym
->result
= result
;
5343 /* Warn if this procedure has the same name as an intrinsic. */
5344 warn_intrinsic_shadow (sym
, true);
5350 gfc_current_locus
= old_loc
;
5355 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5356 pass the name of the entry, rather than the gfc_current_block name, and
5357 to return false upon finding an existing global entry. */
5360 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5364 enum gfc_symbol_type type
;
5366 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5368 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5369 name is a global identifier. */
5370 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5372 s
= gfc_get_gsymbol (name
);
5374 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5376 gfc_global_used (s
, where
);
5385 s
->ns
= gfc_current_ns
;
5389 /* Don't add the symbol multiple times. */
5391 && (!gfc_notification_std (GFC_STD_F2008
)
5392 || strcmp (name
, binding_label
) != 0))
5394 s
= gfc_get_gsymbol (binding_label
);
5396 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5398 gfc_global_used (s
, where
);
5405 s
->binding_label
= binding_label
;
5408 s
->ns
= gfc_current_ns
;
5416 /* Match an ENTRY statement. */
5419 gfc_match_entry (void)
5424 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5425 gfc_compile_state state
;
5429 bool module_procedure
;
5433 m
= gfc_match_name (name
);
5437 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5440 state
= gfc_current_state ();
5441 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5446 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5449 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5451 case COMP_BLOCK_DATA
:
5452 gfc_error ("ENTRY statement at %C cannot appear within "
5455 case COMP_INTERFACE
:
5456 gfc_error ("ENTRY statement at %C cannot appear within "
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5461 "a DERIVED TYPE block");
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5465 "an IF-THEN block");
5468 case COMP_DO_CONCURRENT
:
5469 gfc_error ("ENTRY statement at %C cannot appear within "
5473 gfc_error ("ENTRY statement at %C cannot appear within "
5477 gfc_error ("ENTRY statement at %C cannot appear within "
5481 gfc_error ("ENTRY statement at %C cannot appear within "
5485 gfc_error ("ENTRY statement at %C cannot appear within "
5486 "a contained subprogram");
5489 gfc_internal_error ("gfc_match_entry(): Bad state");
5494 module_procedure
= gfc_current_ns
->parent
!= NULL
5495 && gfc_current_ns
->parent
->proc_name
5496 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5499 if (gfc_current_ns
->parent
!= NULL
5500 && gfc_current_ns
->parent
->proc_name
5501 && !module_procedure
)
5503 gfc_error("ENTRY statement at %C cannot appear in a "
5504 "contained procedure");
5508 /* Module function entries need special care in get_proc_name
5509 because previous references within the function will have
5510 created symbols attached to the current namespace. */
5511 if (get_proc_name (name
, &entry
,
5512 gfc_current_ns
->parent
!= NULL
5513 && module_procedure
))
5516 proc
= gfc_current_block ();
5518 /* Make sure that it isn't already declared as BIND(C). If it is, it
5519 must have been marked BIND(C) with a BIND(C) attribute and that is
5520 not allowed for procedures. */
5521 if (entry
->attr
.is_bind_c
== 1)
5523 entry
->attr
.is_bind_c
= 0;
5524 if (entry
->old_symbol
!= NULL
)
5525 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5526 "variables or common blocks",
5527 &(entry
->old_symbol
->declared_at
));
5529 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5530 "variables or common blocks", &gfc_current_locus
);
5533 /* Check what next non-whitespace character is so we can tell if there
5534 is the required parens if we have a BIND(C). */
5535 old_loc
= gfc_current_locus
;
5536 gfc_gobble_whitespace ();
5537 peek_char
= gfc_peek_ascii_char ();
5539 if (state
== COMP_SUBROUTINE
)
5541 m
= gfc_match_formal_arglist (entry
, 0, 1);
5545 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5546 never be an internal procedure. */
5547 is_bind_c
= gfc_match_bind_c (entry
, true);
5548 if (is_bind_c
== MATCH_ERROR
)
5550 if (is_bind_c
== MATCH_YES
)
5552 if (peek_char
!= '(')
5554 gfc_error ("Missing required parentheses before BIND(C) at %C");
5557 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5558 &(entry
->declared_at
), 1))
5562 if (!gfc_current_ns
->parent
5563 && !add_global_entry (name
, entry
->binding_label
, true,
5567 /* An entry in a subroutine. */
5568 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5569 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5574 /* An entry in a function.
5575 We need to take special care because writing
5580 ENTRY f() RESULT (r)
5582 ENTRY f RESULT (r). */
5583 if (gfc_match_eos () == MATCH_YES
)
5585 gfc_current_locus
= old_loc
;
5586 /* Match the empty argument list, and add the interface to
5588 m
= gfc_match_formal_arglist (entry
, 0, 1);
5591 m
= gfc_match_formal_arglist (entry
, 0, 0);
5598 if (gfc_match_eos () == MATCH_YES
)
5600 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5601 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5604 entry
->result
= entry
;
5608 m
= gfc_match_suffix (entry
, &result
);
5610 gfc_syntax_error (ST_ENTRY
);
5616 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5617 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5618 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5620 entry
->result
= result
;
5624 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5625 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5627 entry
->result
= entry
;
5631 if (!gfc_current_ns
->parent
5632 && !add_global_entry (name
, entry
->binding_label
, false,
5637 if (gfc_match_eos () != MATCH_YES
)
5639 gfc_syntax_error (ST_ENTRY
);
5643 entry
->attr
.recursive
= proc
->attr
.recursive
;
5644 entry
->attr
.elemental
= proc
->attr
.elemental
;
5645 entry
->attr
.pure
= proc
->attr
.pure
;
5647 el
= gfc_get_entry_list ();
5649 el
->next
= gfc_current_ns
->entries
;
5650 gfc_current_ns
->entries
= el
;
5652 el
->id
= el
->next
->id
+ 1;
5656 new_st
.op
= EXEC_ENTRY
;
5657 new_st
.ext
.entry
= el
;
5663 /* Match a subroutine statement, including optional prefixes. */
5666 gfc_match_subroutine (void)
5668 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5673 bool allow_binding_name
;
5675 if (gfc_current_state () != COMP_NONE
5676 && gfc_current_state () != COMP_INTERFACE
5677 && gfc_current_state () != COMP_CONTAINS
)
5680 m
= gfc_match_prefix (NULL
);
5684 m
= gfc_match ("subroutine% %n", name
);
5688 if (get_proc_name (name
, &sym
, false))
5691 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5692 the symbol existed before. */
5693 sym
->declared_at
= gfc_current_locus
;
5695 if (add_hidden_procptr_result (sym
))
5698 gfc_new_block
= sym
;
5700 /* Check what next non-whitespace character is so we can tell if there
5701 is the required parens if we have a BIND(C). */
5702 gfc_gobble_whitespace ();
5703 peek_char
= gfc_peek_ascii_char ();
5705 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5708 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5711 /* Make sure that it isn't already declared as BIND(C). If it is, it
5712 must have been marked BIND(C) with a BIND(C) attribute and that is
5713 not allowed for procedures. */
5714 if (sym
->attr
.is_bind_c
== 1)
5716 sym
->attr
.is_bind_c
= 0;
5717 if (sym
->old_symbol
!= NULL
)
5718 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5719 "variables or common blocks",
5720 &(sym
->old_symbol
->declared_at
));
5722 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5723 "variables or common blocks", &gfc_current_locus
);
5726 /* C binding names are not allowed for internal procedures. */
5727 if (gfc_current_state () == COMP_CONTAINS
5728 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5729 allow_binding_name
= false;
5731 allow_binding_name
= true;
5733 /* Here, we are just checking if it has the bind(c) attribute, and if
5734 so, then we need to make sure it's all correct. If it doesn't,
5735 we still need to continue matching the rest of the subroutine line. */
5736 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5737 if (is_bind_c
== MATCH_ERROR
)
5739 /* There was an attempt at the bind(c), but it was wrong. An
5740 error message should have been printed w/in the gfc_match_bind_c
5741 so here we'll just return the MATCH_ERROR. */
5745 if (is_bind_c
== MATCH_YES
)
5747 /* The following is allowed in the Fortran 2008 draft. */
5748 if (gfc_current_state () == COMP_CONTAINS
5749 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5750 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5751 "at %L may not be specified for an internal "
5752 "procedure", &gfc_current_locus
))
5755 if (peek_char
!= '(')
5757 gfc_error ("Missing required parentheses before BIND(C) at %C");
5760 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5761 &(sym
->declared_at
), 1))
5765 if (gfc_match_eos () != MATCH_YES
)
5767 gfc_syntax_error (ST_SUBROUTINE
);
5771 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5774 /* Warn if it has the same name as an intrinsic. */
5775 warn_intrinsic_shadow (sym
, false);
5781 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5782 given, and set the binding label in either the given symbol (if not
5783 NULL), or in the current_ts. The symbol may be NULL because we may
5784 encounter the BIND(C) before the declaration itself. Return
5785 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5786 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5787 or MATCH_YES if the specifier was correct and the binding label and
5788 bind(c) fields were set correctly for the given symbol or the
5789 current_ts. If allow_binding_name is false, no binding name may be
5793 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5795 /* binding label, if exists */
5796 const char* binding_label
= NULL
;
5800 /* Initialize the flag that specifies whether we encountered a NAME=
5801 specifier or not. */
5802 has_name_equals
= 0;
5804 /* This much we have to be able to match, in this order, if
5805 there is a bind(c) label. */
5806 if (gfc_match (" bind ( c ") != MATCH_YES
)
5809 /* Now see if there is a binding label, or if we've reached the
5810 end of the bind(c) attribute without one. */
5811 if (gfc_match_char (',') == MATCH_YES
)
5813 if (gfc_match (" name = ") != MATCH_YES
)
5815 gfc_error ("Syntax error in NAME= specifier for binding label "
5817 /* should give an error message here */
5821 has_name_equals
= 1;
5823 /* Get the opening quote. */
5824 double_quote
= MATCH_YES
;
5825 single_quote
= MATCH_YES
;
5826 double_quote
= gfc_match_char ('"');
5827 if (double_quote
!= MATCH_YES
)
5828 single_quote
= gfc_match_char ('\'');
5829 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5831 gfc_error ("Syntax error in NAME= specifier for binding label "
5836 /* Grab the binding label, using functions that will not lower
5837 case the names automatically. */
5838 if (gfc_match_name_C (&binding_label
) != MATCH_YES
)
5841 /* Get the closing quotation. */
5842 if (double_quote
== MATCH_YES
)
5844 if (gfc_match_char ('"') != MATCH_YES
)
5846 gfc_error ("Missing closing quote '\"' for binding label at %C");
5847 /* User started string with '"' so looked to match it. */
5853 if (gfc_match_char ('\'') != MATCH_YES
)
5855 gfc_error ("Missing closing quote '\'' for binding label at %C");
5856 /* User started string with "'" char. */
5862 /* Get the required right paren. */
5863 if (gfc_match_char (')') != MATCH_YES
)
5865 gfc_error ("Missing closing paren for binding label at %C");
5869 if (has_name_equals
&& !allow_binding_name
)
5871 gfc_error ("No binding name is allowed in BIND(C) at %C");
5875 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5877 gfc_error ("For dummy procedure %s, no binding name is "
5878 "allowed in BIND(C) at %C", sym
->name
);
5883 /* Save the binding label to the symbol. If sym is null, we're
5884 probably matching the typespec attributes of a declaration and
5885 haven't gotten the name yet, and therefore, no symbol yet. */
5889 sym
->binding_label
= binding_label
;
5891 curr_binding_label
= binding_label
;
5893 else if (allow_binding_name
)
5895 /* No binding label, but if symbol isn't null, we
5896 can set the label for it here.
5897 If name="" or allow_binding_name is false, no C binding name is
5899 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5900 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
5903 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5904 && current_interface
.type
== INTERFACE_ABSTRACT
)
5906 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5914 /* Return nonzero if we're currently compiling a contained procedure. */
5917 contained_procedure (void)
5919 gfc_state_data
*s
= gfc_state_stack
;
5921 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5922 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5928 /* Set the kind of each enumerator. The kind is selected such that it is
5929 interoperable with the corresponding C enumeration type, making
5930 sure that -fshort-enums is honored. */
5935 enumerator_history
*current_history
= NULL
;
5939 if (max_enum
== NULL
|| enum_history
== NULL
)
5942 if (!flag_short_enums
)
5948 kind
= gfc_integer_kinds
[i
++].kind
;
5950 while (kind
< gfc_c_int_kind
5951 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5954 current_history
= enum_history
;
5955 while (current_history
!= NULL
)
5957 current_history
->sym
->ts
.kind
= kind
;
5958 current_history
= current_history
->next
;
5963 /* Match any of the various end-block statements. Returns the type of
5964 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5965 and END BLOCK statements cannot be replaced by a single END statement. */
5968 gfc_match_end (gfc_statement
*st
)
5970 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5971 gfc_compile_state state
;
5973 const char *block_name
;
5977 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
5978 gfc_namespace
**nsp
;
5980 old_loc
= gfc_current_locus
;
5981 if (gfc_match ("end") != MATCH_YES
)
5984 state
= gfc_current_state ();
5985 block_name
= gfc_current_block () == NULL
5986 ? NULL
: gfc_current_block ()->name
;
5990 case COMP_ASSOCIATE
:
5992 if (!strncmp (block_name
, "block@", strlen("block@")))
5997 case COMP_DERIVED_CONTAINS
:
5998 state
= gfc_state_stack
->previous
->state
;
5999 block_name
= gfc_state_stack
->previous
->sym
== NULL
6000 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6011 *st
= ST_END_PROGRAM
;
6012 target
= " program";
6016 case COMP_SUBROUTINE
:
6017 *st
= ST_END_SUBROUTINE
;
6018 target
= " subroutine";
6019 eos_ok
= !contained_procedure ();
6023 *st
= ST_END_FUNCTION
;
6024 target
= " function";
6025 eos_ok
= !contained_procedure ();
6028 case COMP_BLOCK_DATA
:
6029 *st
= ST_END_BLOCK_DATA
;
6030 target
= " block data";
6035 *st
= ST_END_MODULE
;
6040 case COMP_INTERFACE
:
6041 *st
= ST_END_INTERFACE
;
6042 target
= " interface";
6047 case COMP_DERIVED_CONTAINS
:
6053 case COMP_ASSOCIATE
:
6054 *st
= ST_END_ASSOCIATE
;
6055 target
= " associate";
6072 case COMP_DO_CONCURRENT
:
6079 *st
= ST_END_CRITICAL
;
6080 target
= " critical";
6085 case COMP_SELECT_TYPE
:
6086 *st
= ST_END_SELECT
;
6092 *st
= ST_END_FORALL
;
6107 last_initializer
= NULL
;
6109 gfc_free_enum_history ();
6113 gfc_error ("Unexpected END statement at %C");
6117 old_loc
= gfc_current_locus
;
6118 if (gfc_match_eos () == MATCH_YES
)
6120 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6122 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6123 "instead of %s statement at %L",
6124 gfc_ascii_statement(*st
), &old_loc
))
6129 /* We would have required END [something]. */
6130 gfc_error ("%s statement expected at %L",
6131 gfc_ascii_statement (*st
), &old_loc
);
6138 /* Verify that we've got the sort of end-block that we're expecting. */
6139 if (gfc_match (target
) != MATCH_YES
)
6141 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6146 old_loc
= gfc_current_locus
;
6147 /* If we're at the end, make sure a block name wasn't required. */
6148 if (gfc_match_eos () == MATCH_YES
)
6151 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6152 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6153 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6159 gfc_error ("Expected block name of '%s' in %s statement at %L",
6160 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6165 /* END INTERFACE has a special handler for its several possible endings. */
6166 if (*st
== ST_END_INTERFACE
)
6167 return gfc_match_end_interface ();
6169 /* We haven't hit the end of statement, so what is left must be an
6171 m
= gfc_match_space ();
6173 m
= gfc_match_name (name
);
6176 gfc_error ("Expected terminating name at %C");
6180 if (block_name
== NULL
)
6183 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6185 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
6186 gfc_ascii_statement (*st
));
6189 /* Procedure pointer as function result. */
6190 else if (strcmp (block_name
, "ppr@") == 0
6191 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6193 gfc_error ("Expected label '%s' for %s statement at %C",
6194 gfc_current_block ()->ns
->proc_name
->name
,
6195 gfc_ascii_statement (*st
));
6199 if (gfc_match_eos () == MATCH_YES
)
6203 gfc_syntax_error (*st
);
6206 gfc_current_locus
= old_loc
;
6208 /* If we are missing an END BLOCK, we created a half-ready namespace.
6209 Remove it from the parent namespace's sibling list. */
6211 if (state
== COMP_BLOCK
)
6213 parent_ns
= gfc_current_ns
->parent
;
6215 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6221 if (ns
== gfc_current_ns
)
6223 if (prev_ns
== NULL
)
6226 prev_ns
->sibling
= ns
->sibling
;
6232 gfc_free_namespace (gfc_current_ns
);
6233 gfc_current_ns
= parent_ns
;
6241 /***************** Attribute declaration statements ****************/
6243 /* Set the attribute of a single variable. */
6248 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6256 m
= gfc_match_name (name
);
6260 if (find_special (name
, &sym
, false))
6263 if (!check_function_name (name
))
6269 var_locus
= gfc_current_locus
;
6271 /* Deal with possible array specification for certain attributes. */
6272 if (current_attr
.dimension
6273 || current_attr
.codimension
6274 || current_attr
.allocatable
6275 || current_attr
.pointer
6276 || current_attr
.target
)
6278 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6279 !current_attr
.dimension
6280 && !current_attr
.pointer
6281 && !current_attr
.target
);
6282 if (m
== MATCH_ERROR
)
6285 if (current_attr
.dimension
&& m
== MATCH_NO
)
6287 gfc_error ("Missing array specification at %L in DIMENSION "
6288 "statement", &var_locus
);
6293 if (current_attr
.dimension
&& sym
->value
)
6295 gfc_error ("Dimensions specified for %s at %L after its "
6296 "initialisation", sym
->name
, &var_locus
);
6301 if (current_attr
.codimension
&& m
== MATCH_NO
)
6303 gfc_error ("Missing array specification at %L in CODIMENSION "
6304 "statement", &var_locus
);
6309 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6310 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6312 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6318 /* Update symbol table. DIMENSION attribute is set in
6319 gfc_set_array_spec(). For CLASS variables, this must be applied
6320 to the first component, or '_data' field. */
6321 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6323 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6331 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6332 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6339 if (sym
->ts
.type
== BT_CLASS
6340 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6346 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6352 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6354 /* Fix the array spec. */
6355 m
= gfc_mod_pointee_as (sym
->as
);
6356 if (m
== MATCH_ERROR
)
6360 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6366 if ((current_attr
.external
|| current_attr
.intrinsic
)
6367 && sym
->attr
.flavor
!= FL_PROCEDURE
6368 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6374 add_hidden_procptr_result (sym
);
6379 gfc_free_array_spec (as
);
6384 /* Generic attribute declaration subroutine. Used for attributes that
6385 just have a list of names. */
6392 /* Gobble the optional double colon, by simply ignoring the result
6402 if (gfc_match_eos () == MATCH_YES
)
6408 if (gfc_match_char (',') != MATCH_YES
)
6410 gfc_error ("Unexpected character in variable list at %C");
6420 /* This routine matches Cray Pointer declarations of the form:
6421 pointer ( <pointer>, <pointee> )
6423 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6424 The pointer, if already declared, should be an integer. Otherwise, we
6425 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6426 be either a scalar, or an array declaration. No space is allocated for
6427 the pointee. For the statement
6428 pointer (ipt, ar(10))
6429 any subsequent uses of ar will be translated (in C-notation) as
6430 ar(i) => ((<type> *) ipt)(i)
6431 After gimplification, pointee variable will disappear in the code. */
6434 cray_pointer_decl (void)
6437 gfc_array_spec
*as
= NULL
;
6438 gfc_symbol
*cptr
; /* Pointer symbol. */
6439 gfc_symbol
*cpte
; /* Pointee symbol. */
6445 if (gfc_match_char ('(') != MATCH_YES
)
6447 gfc_error ("Expected '(' at %C");
6451 /* Match pointer. */
6452 var_locus
= gfc_current_locus
;
6453 gfc_clear_attr (¤t_attr
);
6454 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6455 current_ts
.type
= BT_INTEGER
;
6456 current_ts
.kind
= gfc_index_integer_kind
;
6458 m
= gfc_match_symbol (&cptr
, 0);
6461 gfc_error ("Expected variable name at %C");
6465 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6468 gfc_set_sym_referenced (cptr
);
6470 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6472 cptr
->ts
.type
= BT_INTEGER
;
6473 cptr
->ts
.kind
= gfc_index_integer_kind
;
6475 else if (cptr
->ts
.type
!= BT_INTEGER
)
6477 gfc_error ("Cray pointer at %C must be an integer");
6480 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6481 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6482 " memory addresses require %d bytes",
6483 cptr
->ts
.kind
, gfc_index_integer_kind
);
6485 if (gfc_match_char (',') != MATCH_YES
)
6487 gfc_error ("Expected \",\" at %C");
6491 /* Match Pointee. */
6492 var_locus
= gfc_current_locus
;
6493 gfc_clear_attr (¤t_attr
);
6494 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6495 current_ts
.type
= BT_UNKNOWN
;
6496 current_ts
.kind
= 0;
6498 m
= gfc_match_symbol (&cpte
, 0);
6501 gfc_error ("Expected variable name at %C");
6505 /* Check for an optional array spec. */
6506 m
= gfc_match_array_spec (&as
, true, false);
6507 if (m
== MATCH_ERROR
)
6509 gfc_free_array_spec (as
);
6512 else if (m
== MATCH_NO
)
6514 gfc_free_array_spec (as
);
6518 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6521 gfc_set_sym_referenced (cpte
);
6523 if (cpte
->as
== NULL
)
6525 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6526 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6528 else if (as
!= NULL
)
6530 gfc_error ("Duplicate array spec for Cray pointee at %C");
6531 gfc_free_array_spec (as
);
6537 if (cpte
->as
!= NULL
)
6539 /* Fix array spec. */
6540 m
= gfc_mod_pointee_as (cpte
->as
);
6541 if (m
== MATCH_ERROR
)
6545 /* Point the Pointee at the Pointer. */
6546 cpte
->cp_pointer
= cptr
;
6548 if (gfc_match_char (')') != MATCH_YES
)
6550 gfc_error ("Expected \")\" at %C");
6553 m
= gfc_match_char (',');
6555 done
= true; /* Stop searching for more declarations. */
6559 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6560 || gfc_match_eos () != MATCH_YES
)
6562 gfc_error ("Expected \",\" or end of statement at %C");
6570 gfc_match_external (void)
6573 gfc_clear_attr (¤t_attr
);
6574 current_attr
.external
= 1;
6576 return attr_decl ();
6581 gfc_match_intent (void)
6585 /* This is not allowed within a BLOCK construct! */
6586 if (gfc_current_state () == COMP_BLOCK
)
6588 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6592 intent
= match_intent_spec ();
6593 if (intent
== INTENT_UNKNOWN
)
6596 gfc_clear_attr (¤t_attr
);
6597 current_attr
.intent
= intent
;
6599 return attr_decl ();
6604 gfc_match_intrinsic (void)
6607 gfc_clear_attr (¤t_attr
);
6608 current_attr
.intrinsic
= 1;
6610 return attr_decl ();
6615 gfc_match_optional (void)
6617 /* This is not allowed within a BLOCK construct! */
6618 if (gfc_current_state () == COMP_BLOCK
)
6620 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6624 gfc_clear_attr (¤t_attr
);
6625 current_attr
.optional
= 1;
6627 return attr_decl ();
6632 gfc_match_pointer (void)
6634 gfc_gobble_whitespace ();
6635 if (gfc_peek_ascii_char () == '(')
6637 if (!gfc_option
.flag_cray_pointer
)
6639 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6643 return cray_pointer_decl ();
6647 gfc_clear_attr (¤t_attr
);
6648 current_attr
.pointer
= 1;
6650 return attr_decl ();
6656 gfc_match_allocatable (void)
6658 gfc_clear_attr (¤t_attr
);
6659 current_attr
.allocatable
= 1;
6661 return attr_decl ();
6666 gfc_match_codimension (void)
6668 gfc_clear_attr (¤t_attr
);
6669 current_attr
.codimension
= 1;
6671 return attr_decl ();
6676 gfc_match_contiguous (void)
6678 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6681 gfc_clear_attr (¤t_attr
);
6682 current_attr
.contiguous
= 1;
6684 return attr_decl ();
6689 gfc_match_dimension (void)
6691 gfc_clear_attr (¤t_attr
);
6692 current_attr
.dimension
= 1;
6694 return attr_decl ();
6699 gfc_match_target (void)
6701 gfc_clear_attr (¤t_attr
);
6702 current_attr
.target
= 1;
6704 return attr_decl ();
6708 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6712 access_attr_decl (gfc_statement st
)
6714 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6715 interface_type type
;
6717 gfc_symbol
*sym
, *dt_sym
;
6718 gfc_intrinsic_op op
;
6721 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6726 m
= gfc_match_generic_spec (&type
, name
, &op
);
6729 if (m
== MATCH_ERROR
)
6734 case INTERFACE_NAMELESS
:
6735 case INTERFACE_ABSTRACT
:
6738 case INTERFACE_GENERIC
:
6739 if (gfc_get_symbol (name
, NULL
, &sym
))
6742 if (!gfc_add_access (&sym
->attr
,
6744 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6748 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6749 && !gfc_add_access (&dt_sym
->attr
,
6751 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6757 case INTERFACE_INTRINSIC_OP
:
6758 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6760 gfc_intrinsic_op other_op
;
6762 gfc_current_ns
->operator_access
[op
] =
6763 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6765 /* Handle the case if there is another op with the same
6766 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6767 other_op
= gfc_equivalent_op (op
);
6769 if (other_op
!= INTRINSIC_NONE
)
6770 gfc_current_ns
->operator_access
[other_op
] =
6771 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6776 gfc_error ("Access specification of the %s operator at %C has "
6777 "already been specified", gfc_op2string (op
));
6783 case INTERFACE_USER_OP
:
6784 uop
= gfc_get_uop (name
);
6786 if (uop
->access
== ACCESS_UNKNOWN
)
6788 uop
->access
= (st
== ST_PUBLIC
)
6789 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6793 gfc_error ("Access specification of the .%s. operator at %C "
6794 "has already been specified", sym
->name
);
6801 if (gfc_match_char (',') == MATCH_NO
)
6805 if (gfc_match_eos () != MATCH_YES
)
6810 gfc_syntax_error (st
);
6818 gfc_match_protected (void)
6823 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6825 gfc_error ("PROTECTED at %C only allowed in specification "
6826 "part of a module");
6831 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6834 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6839 if (gfc_match_eos () == MATCH_YES
)
6844 m
= gfc_match_symbol (&sym
, 0);
6848 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
6860 if (gfc_match_eos () == MATCH_YES
)
6862 if (gfc_match_char (',') != MATCH_YES
)
6869 gfc_error ("Syntax error in PROTECTED statement at %C");
6874 /* The PRIVATE statement is a bit weird in that it can be an attribute
6875 declaration, but also works as a standalone statement inside of a
6876 type declaration or a module. */
6879 gfc_match_private (gfc_statement
*st
)
6882 if (gfc_match ("private") != MATCH_YES
)
6885 if (gfc_current_state () != COMP_MODULE
6886 && !(gfc_current_state () == COMP_DERIVED
6887 && gfc_state_stack
->previous
6888 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6889 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6890 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6891 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6893 gfc_error ("PRIVATE statement at %C is only allowed in the "
6894 "specification part of a module");
6898 if (gfc_current_state () == COMP_DERIVED
)
6900 if (gfc_match_eos () == MATCH_YES
)
6906 gfc_syntax_error (ST_PRIVATE
);
6910 if (gfc_match_eos () == MATCH_YES
)
6917 return access_attr_decl (ST_PRIVATE
);
6922 gfc_match_public (gfc_statement
*st
)
6925 if (gfc_match ("public") != MATCH_YES
)
6928 if (gfc_current_state () != COMP_MODULE
)
6930 gfc_error ("PUBLIC statement at %C is only allowed in the "
6931 "specification part of a module");
6935 if (gfc_match_eos () == MATCH_YES
)
6942 return access_attr_decl (ST_PUBLIC
);
6946 /* Workhorse for gfc_match_parameter. */
6956 m
= gfc_match_symbol (&sym
, 0);
6958 gfc_error ("Expected variable name at %C in PARAMETER statement");
6963 if (gfc_match_char ('=') == MATCH_NO
)
6965 gfc_error ("Expected = sign in PARAMETER statement at %C");
6969 m
= gfc_match_init_expr (&init
);
6971 gfc_error ("Expected expression at %C in PARAMETER statement");
6975 if (sym
->ts
.type
== BT_UNKNOWN
6976 && !gfc_set_default_type (sym
, 1, NULL
))
6982 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
6983 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
6991 gfc_error ("Initializing already initialized variable at %C");
6996 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6997 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7000 gfc_free_expr (init
);
7005 /* Match a parameter statement, with the weird syntax that these have. */
7008 gfc_match_parameter (void)
7012 if (gfc_match_char ('(') == MATCH_NO
)
7021 if (gfc_match (" )%t") == MATCH_YES
)
7024 if (gfc_match_char (',') != MATCH_YES
)
7026 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7036 /* Save statements have a special syntax. */
7039 gfc_match_save (void)
7041 char n
[GFC_MAX_SYMBOL_LEN
+1];
7046 if (gfc_match_eos () == MATCH_YES
)
7048 if (gfc_current_ns
->seen_save
)
7050 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7051 "follows previous SAVE statement"))
7055 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7059 if (gfc_current_ns
->save_all
)
7061 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7062 "blanket SAVE statement"))
7070 m
= gfc_match_symbol (&sym
, 0);
7074 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7075 &gfc_current_locus
))
7086 m
= gfc_match (" / %n /", &n
);
7087 if (m
== MATCH_ERROR
)
7092 c
= gfc_get_common (n
, 0);
7095 gfc_current_ns
->seen_save
= 1;
7098 if (gfc_match_eos () == MATCH_YES
)
7100 if (gfc_match_char (',') != MATCH_YES
)
7107 gfc_error ("Syntax error in SAVE statement at %C");
7113 gfc_match_value (void)
7118 /* This is not allowed within a BLOCK construct! */
7119 if (gfc_current_state () == COMP_BLOCK
)
7121 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7125 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7128 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7133 if (gfc_match_eos () == MATCH_YES
)
7138 m
= gfc_match_symbol (&sym
, 0);
7142 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7154 if (gfc_match_eos () == MATCH_YES
)
7156 if (gfc_match_char (',') != MATCH_YES
)
7163 gfc_error ("Syntax error in VALUE statement at %C");
7169 gfc_match_volatile (void)
7174 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7177 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7182 if (gfc_match_eos () == MATCH_YES
)
7187 /* VOLATILE is special because it can be added to host-associated
7188 symbols locally. Except for coarrays. */
7189 m
= gfc_match_symbol (&sym
, 1);
7193 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7194 for variable in a BLOCK which is defined outside of the BLOCK. */
7195 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7197 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7198 "%C, which is use-/host-associated", sym
->name
);
7201 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7213 if (gfc_match_eos () == MATCH_YES
)
7215 if (gfc_match_char (',') != MATCH_YES
)
7222 gfc_error ("Syntax error in VOLATILE statement at %C");
7228 gfc_match_asynchronous (void)
7233 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7236 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7241 if (gfc_match_eos () == MATCH_YES
)
7246 /* ASYNCHRONOUS is special because it can be added to host-associated
7248 m
= gfc_match_symbol (&sym
, 1);
7252 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7264 if (gfc_match_eos () == MATCH_YES
)
7266 if (gfc_match_char (',') != MATCH_YES
)
7273 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7278 /* Match a module procedure statement. Note that we have to modify
7279 symbols in the parent's namespace because the current one was there
7280 to receive symbols that are in an interface's formal argument list. */
7283 gfc_match_modproc (void)
7285 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7289 gfc_namespace
*module_ns
;
7290 gfc_interface
*old_interface_head
, *interface
;
7292 if (gfc_state_stack
->state
!= COMP_INTERFACE
7293 || gfc_state_stack
->previous
== NULL
7294 || current_interface
.type
== INTERFACE_NAMELESS
7295 || current_interface
.type
== INTERFACE_ABSTRACT
)
7297 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7302 module_ns
= gfc_current_ns
->parent
;
7303 for (; module_ns
; module_ns
= module_ns
->parent
)
7304 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7305 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7306 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7307 && !module_ns
->proc_name
->attr
.contained
))
7310 if (module_ns
== NULL
)
7313 /* Store the current state of the interface. We will need it if we
7314 end up with a syntax error and need to recover. */
7315 old_interface_head
= gfc_current_interface_head ();
7317 /* Check if the F2008 optional double colon appears. */
7318 gfc_gobble_whitespace ();
7319 old_locus
= gfc_current_locus
;
7320 if (gfc_match ("::") == MATCH_YES
)
7322 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7323 "MODULE PROCEDURE statement at %L", &old_locus
))
7327 gfc_current_locus
= old_locus
;
7332 old_locus
= gfc_current_locus
;
7334 m
= gfc_match_name (name
);
7340 /* Check for syntax error before starting to add symbols to the
7341 current namespace. */
7342 if (gfc_match_eos () == MATCH_YES
)
7345 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7348 /* Now we're sure the syntax is valid, we process this item
7350 if (gfc_get_symbol (name
, module_ns
, &sym
))
7353 if (sym
->attr
.intrinsic
)
7355 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7356 "PROCEDURE", &old_locus
);
7360 if (sym
->attr
.proc
!= PROC_MODULE
7361 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7364 if (!gfc_add_interface (sym
))
7367 sym
->attr
.mod_proc
= 1;
7368 sym
->declared_at
= old_locus
;
7377 /* Restore the previous state of the interface. */
7378 interface
= gfc_current_interface_head ();
7379 gfc_set_current_interface_head (old_interface_head
);
7381 /* Free the new interfaces. */
7382 while (interface
!= old_interface_head
)
7384 gfc_interface
*i
= interface
->next
;
7389 /* And issue a syntax error. */
7390 gfc_syntax_error (ST_MODULE_PROC
);
7395 /* Check a derived type that is being extended. */
7398 check_extended_derived_type (char *name
)
7400 gfc_symbol
*extended
;
7402 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7404 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7408 extended
= gfc_find_dt_in_generic (extended
);
7413 gfc_error ("Symbol '%s' at %C has not been previously defined", name
);
7417 if (extended
->attr
.flavor
!= FL_DERIVED
)
7419 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7420 "derived type", name
);
7424 if (extended
->attr
.is_bind_c
)
7426 gfc_error ("'%s' cannot be extended at %C because it "
7427 "is BIND(C)", extended
->name
);
7431 if (extended
->attr
.sequence
)
7433 gfc_error ("'%s' cannot be extended at %C because it "
7434 "is a SEQUENCE type", extended
->name
);
7442 /* Match the optional attribute specifiers for a type declaration.
7443 Return MATCH_ERROR if an error is encountered in one of the handled
7444 attributes (public, private, bind(c)), MATCH_NO if what's found is
7445 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7446 checking on attribute conflicts needs to be done. */
7449 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7451 /* See if the derived type is marked as private. */
7452 if (gfc_match (" , private") == MATCH_YES
)
7454 if (gfc_current_state () != COMP_MODULE
)
7456 gfc_error ("Derived type at %C can only be PRIVATE in the "
7457 "specification part of a module");
7461 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7464 else if (gfc_match (" , public") == MATCH_YES
)
7466 if (gfc_current_state () != COMP_MODULE
)
7468 gfc_error ("Derived type at %C can only be PUBLIC in the "
7469 "specification part of a module");
7473 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7476 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7478 /* If the type is defined to be bind(c) it then needs to make
7479 sure that all fields are interoperable. This will
7480 need to be a semantic check on the finished derived type.
7481 See 15.2.3 (lines 9-12) of F2003 draft. */
7482 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7485 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7487 else if (gfc_match (" , abstract") == MATCH_YES
)
7489 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7492 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7495 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7497 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7503 /* If we get here, something matched. */
7508 /* Match the beginning of a derived type declaration. If a type name
7509 was the result of a function, then it is possible to have a symbol
7510 already to be known as a derived type yet have no components. */
7513 gfc_match_derived_decl (void)
7515 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7516 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7517 symbol_attribute attr
;
7518 gfc_symbol
*sym
, *gensym
;
7519 gfc_symbol
*extended
;
7521 match is_type_attr_spec
= MATCH_NO
;
7522 bool seen_attr
= false;
7523 gfc_interface
*intr
= NULL
, *head
;
7525 if (gfc_current_state () == COMP_DERIVED
)
7530 gfc_clear_attr (&attr
);
7535 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7536 if (is_type_attr_spec
== MATCH_ERROR
)
7538 if (is_type_attr_spec
== MATCH_YES
)
7540 } while (is_type_attr_spec
== MATCH_YES
);
7542 /* Deal with derived type extensions. The extension attribute has
7543 been added to 'attr' but now the parent type must be found and
7546 extended
= check_extended_derived_type (parent
);
7548 if (parent
[0] && !extended
)
7551 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7553 gfc_error ("Expected :: in TYPE definition at %C");
7557 m
= gfc_match (" %n%t", name
);
7561 /* Make sure the name is not the name of an intrinsic type. */
7562 if (gfc_is_intrinsic_typename (name
))
7564 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7569 if (gfc_get_symbol (name
, NULL
, &gensym
))
7572 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7574 gfc_error ("Derived type name '%s' at %C already has a basic type "
7575 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7579 if (!gensym
->attr
.generic
7580 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7583 if (!gensym
->attr
.function
7584 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7587 sym
= gfc_find_dt_in_generic (gensym
);
7589 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7591 gfc_error ("Derived type definition of '%s' at %C has already been "
7592 "defined", sym
->name
);
7598 /* Use upper case to save the actual derived-type symbol. */
7599 gfc_get_symbol (gfc_get_string ("%c%s",
7600 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7601 &gensym
->name
[1]), NULL
, &sym
);
7602 sym
->name
= gfc_get_string (gensym
->name
);
7603 head
= gensym
->generic
;
7604 intr
= gfc_get_interface ();
7606 intr
->where
= gfc_current_locus
;
7607 intr
->sym
->declared_at
= gfc_current_locus
;
7609 gensym
->generic
= intr
;
7610 gensym
->attr
.if_source
= IFSRC_DECL
;
7613 /* The symbol may already have the derived attribute without the
7614 components. The ways this can happen is via a function
7615 definition, an INTRINSIC statement or a subtype in another
7616 derived type that is a pointer. The first part of the AND clause
7617 is true if the symbol is not the return value of a function. */
7618 if (sym
->attr
.flavor
!= FL_DERIVED
7619 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7622 if (attr
.access
!= ACCESS_UNKNOWN
7623 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7625 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7626 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7627 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7631 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7632 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7633 gensym
->attr
.access
= sym
->attr
.access
;
7635 /* See if the derived type was labeled as bind(c). */
7636 if (attr
.is_bind_c
!= 0)
7637 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7639 /* Construct the f2k_derived namespace if it is not yet there. */
7640 if (!sym
->f2k_derived
)
7641 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7643 if (extended
&& !sym
->components
)
7648 /* Add the extended derived type as the first component. */
7649 gfc_add_component (sym
, parent
, &p
);
7651 gfc_set_sym_referenced (extended
);
7653 p
->ts
.type
= BT_DERIVED
;
7654 p
->ts
.u
.derived
= extended
;
7655 p
->initializer
= gfc_default_initializer (&p
->ts
);
7657 /* Set extension level. */
7658 if (extended
->attr
.extension
== 255)
7660 /* Since the extension field is 8 bit wide, we can only have
7661 up to 255 extension levels. */
7662 gfc_error ("Maximum extension level reached with type '%s' at %L",
7663 extended
->name
, &extended
->declared_at
);
7666 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7668 /* Provide the links between the extended type and its extension. */
7669 if (!extended
->f2k_derived
)
7670 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7671 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7675 if (!sym
->hash_value
)
7676 /* Set the hash for the compound name for this type. */
7677 sym
->hash_value
= gfc_hash_value (sym
);
7679 /* Take over the ABSTRACT attribute. */
7680 sym
->attr
.abstract
= attr
.abstract
;
7682 gfc_new_block
= sym
;
7688 /* Cray Pointees can be declared as:
7689 pointer (ipt, a (n,m,...,*)) */
7692 gfc_mod_pointee_as (gfc_array_spec
*as
)
7694 as
->cray_pointee
= true; /* This will be useful to know later. */
7695 if (as
->type
== AS_ASSUMED_SIZE
)
7696 as
->cp_was_assumed
= true;
7697 else if (as
->type
== AS_ASSUMED_SHAPE
)
7699 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7706 /* Match the enum definition statement, here we are trying to match
7707 the first line of enum definition statement.
7708 Returns MATCH_YES if match is found. */
7711 gfc_match_enum (void)
7715 m
= gfc_match_eos ();
7719 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7726 /* Returns an initializer whose value is one higher than the value of the
7727 LAST_INITIALIZER argument. If the argument is NULL, the
7728 initializers value will be set to zero. The initializer's kind
7729 will be set to gfc_c_int_kind.
7731 If -fshort-enums is given, the appropriate kind will be selected
7732 later after all enumerators have been parsed. A warning is issued
7733 here if an initializer exceeds gfc_c_int_kind. */
7736 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7739 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7741 mpz_init (result
->value
.integer
);
7743 if (last_initializer
!= NULL
)
7745 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7746 result
->where
= last_initializer
->where
;
7748 if (gfc_check_integer_range (result
->value
.integer
,
7749 gfc_c_int_kind
) != ARITH_OK
)
7751 gfc_error ("Enumerator exceeds the C integer type at %C");
7757 /* Control comes here, if it's the very first enumerator and no
7758 initializer has been given. It will be initialized to zero. */
7759 mpz_set_si (result
->value
.integer
, 0);
7766 /* Match a variable name with an optional initializer. When this
7767 subroutine is called, a variable is expected to be parsed next.
7768 Depending on what is happening at the moment, updates either the
7769 symbol table or the current interface. */
7772 enumerator_decl (void)
7774 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7775 gfc_expr
*initializer
;
7776 gfc_array_spec
*as
= NULL
;
7784 old_locus
= gfc_current_locus
;
7786 /* When we get here, we've just matched a list of attributes and
7787 maybe a type and a double colon. The next thing we expect to see
7788 is the name of the symbol. */
7789 m
= gfc_match_name (name
);
7793 var_locus
= gfc_current_locus
;
7795 /* OK, we've successfully matched the declaration. Now put the
7796 symbol in the current namespace. If we fail to create the symbol,
7798 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7804 /* The double colon must be present in order to have initializers.
7805 Otherwise the statement is ambiguous with an assignment statement. */
7808 if (gfc_match_char ('=') == MATCH_YES
)
7810 m
= gfc_match_init_expr (&initializer
);
7813 gfc_error ("Expected an initialization expression at %C");
7822 /* If we do not have an initializer, the initialization value of the
7823 previous enumerator (stored in last_initializer) is incremented
7824 by 1 and is used to initialize the current enumerator. */
7825 if (initializer
== NULL
)
7826 initializer
= enum_initializer (last_initializer
, old_locus
);
7828 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7830 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7836 /* Store this current initializer, for the next enumerator variable
7837 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7838 use last_initializer below. */
7839 last_initializer
= initializer
;
7840 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7842 /* Maintain enumerator history. */
7843 gfc_find_symbol (name
, NULL
, 0, &sym
);
7844 create_enum_history (sym
, last_initializer
);
7846 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7849 /* Free stuff up and return. */
7850 gfc_free_expr (initializer
);
7856 /* Match the enumerator definition statement. */
7859 gfc_match_enumerator_def (void)
7864 gfc_clear_ts (¤t_ts
);
7866 m
= gfc_match (" enumerator");
7870 m
= gfc_match (" :: ");
7871 if (m
== MATCH_ERROR
)
7874 colon_seen
= (m
== MATCH_YES
);
7876 if (gfc_current_state () != COMP_ENUM
)
7878 gfc_error ("ENUM definition statement expected before %C");
7879 gfc_free_enum_history ();
7883 (¤t_ts
)->type
= BT_INTEGER
;
7884 (¤t_ts
)->kind
= gfc_c_int_kind
;
7886 gfc_clear_attr (¤t_attr
);
7887 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7896 m
= enumerator_decl ();
7897 if (m
== MATCH_ERROR
)
7899 gfc_free_enum_history ();
7905 if (gfc_match_eos () == MATCH_YES
)
7907 if (gfc_match_char (',') != MATCH_YES
)
7911 if (gfc_current_state () == COMP_ENUM
)
7913 gfc_free_enum_history ();
7914 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7919 gfc_free_array_spec (current_as
);
7926 /* Match binding attributes. */
7929 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7931 bool found_passing
= false;
7932 bool seen_ptr
= false;
7933 match m
= MATCH_YES
;
7935 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7936 this case the defaults are in there. */
7937 ba
->access
= ACCESS_UNKNOWN
;
7938 ba
->pass_arg
= NULL
;
7939 ba
->pass_arg_num
= 0;
7941 ba
->non_overridable
= 0;
7945 /* If we find a comma, we believe there are binding attributes. */
7946 m
= gfc_match_char (',');
7952 /* Access specifier. */
7954 m
= gfc_match (" public");
7955 if (m
== MATCH_ERROR
)
7959 if (ba
->access
!= ACCESS_UNKNOWN
)
7961 gfc_error ("Duplicate access-specifier at %C");
7965 ba
->access
= ACCESS_PUBLIC
;
7969 m
= gfc_match (" private");
7970 if (m
== MATCH_ERROR
)
7974 if (ba
->access
!= ACCESS_UNKNOWN
)
7976 gfc_error ("Duplicate access-specifier at %C");
7980 ba
->access
= ACCESS_PRIVATE
;
7984 /* If inside GENERIC, the following is not allowed. */
7989 m
= gfc_match (" nopass");
7990 if (m
== MATCH_ERROR
)
7996 gfc_error ("Binding attributes already specify passing,"
7997 " illegal NOPASS at %C");
8001 found_passing
= true;
8006 /* PASS possibly including argument. */
8007 m
= gfc_match (" pass");
8008 if (m
== MATCH_ERROR
)
8012 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8016 gfc_error ("Binding attributes already specify passing,"
8017 " illegal PASS at %C");
8021 m
= gfc_match (" ( %n )", arg
);
8022 if (m
== MATCH_ERROR
)
8025 ba
->pass_arg
= gfc_get_string (arg
);
8026 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8028 found_passing
= true;
8036 m
= gfc_match (" pointer");
8037 if (m
== MATCH_ERROR
)
8043 gfc_error ("Duplicate POINTER attribute at %C");
8053 /* NON_OVERRIDABLE flag. */
8054 m
= gfc_match (" non_overridable");
8055 if (m
== MATCH_ERROR
)
8059 if (ba
->non_overridable
)
8061 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8065 ba
->non_overridable
= 1;
8069 /* DEFERRED flag. */
8070 m
= gfc_match (" deferred");
8071 if (m
== MATCH_ERROR
)
8077 gfc_error ("Duplicate DEFERRED at %C");
8088 /* Nothing matching found. */
8090 gfc_error ("Expected access-specifier at %C");
8092 gfc_error ("Expected binding attribute at %C");
8095 while (gfc_match_char (',') == MATCH_YES
);
8097 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8098 if (ba
->non_overridable
&& ba
->deferred
)
8100 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8107 if (ba
->access
== ACCESS_UNKNOWN
)
8108 ba
->access
= gfc_typebound_default_access
;
8110 if (ppc
&& !seen_ptr
)
8112 gfc_error ("POINTER attribute is required for procedure pointer component"
8124 /* Match a PROCEDURE specific binding inside a derived type. */
8127 match_procedure_in_type (void)
8129 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8130 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8131 char* target
= NULL
, *ifc
= NULL
;
8132 gfc_typebound_proc tb
;
8141 /* Check current state. */
8142 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8143 block
= gfc_state_stack
->previous
->sym
;
8146 /* Try to match PROCEDURE(interface). */
8147 if (gfc_match (" (") == MATCH_YES
)
8149 m
= gfc_match_name (target_buf
);
8150 if (m
== MATCH_ERROR
)
8154 gfc_error ("Interface-name expected after '(' at %C");
8158 if (gfc_match (" )") != MATCH_YES
)
8160 gfc_error ("')' expected at %C");
8167 /* Construct the data structure. */
8168 memset (&tb
, 0, sizeof (tb
));
8169 tb
.where
= gfc_current_locus
;
8171 /* Match binding attributes. */
8172 m
= match_binding_attributes (&tb
, false, false);
8173 if (m
== MATCH_ERROR
)
8175 seen_attrs
= (m
== MATCH_YES
);
8177 /* Check that attribute DEFERRED is given if an interface is specified. */
8178 if (tb
.deferred
&& !ifc
)
8180 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8183 if (ifc
&& !tb
.deferred
)
8185 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8189 /* Match the colons. */
8190 m
= gfc_match (" ::");
8191 if (m
== MATCH_ERROR
)
8193 seen_colons
= (m
== MATCH_YES
);
8194 if (seen_attrs
&& !seen_colons
)
8196 gfc_error ("Expected '::' after binding-attributes at %C");
8200 /* Match the binding names. */
8203 m
= gfc_match_name (name
);
8204 if (m
== MATCH_ERROR
)
8208 gfc_error ("Expected binding name at %C");
8212 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8215 /* Try to match the '=> target', if it's there. */
8217 m
= gfc_match (" =>");
8218 if (m
== MATCH_ERROR
)
8224 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8230 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8235 m
= gfc_match_name (target_buf
);
8236 if (m
== MATCH_ERROR
)
8240 gfc_error ("Expected binding target after '=>' at %C");
8243 target
= target_buf
;
8246 /* If no target was found, it has the same name as the binding. */
8250 /* Get the namespace to insert the symbols into. */
8251 ns
= block
->f2k_derived
;
8254 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8255 if (tb
.deferred
&& !block
->attr
.abstract
)
8257 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8258 "is not ABSTRACT", block
->name
);
8262 /* See if we already have a binding with this name in the symtree which
8263 would be an error. If a GENERIC already targeted this binding, it may
8264 be already there but then typebound is still NULL. */
8265 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8266 if (stree
&& stree
->n
.tb
)
8268 gfc_error ("There is already a procedure with binding name '%s' for "
8269 "the derived type '%s' at %C", name
, block
->name
);
8273 /* Insert it and set attributes. */
8277 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8280 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8282 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8285 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8287 if (gfc_match_eos () == MATCH_YES
)
8289 if (gfc_match_char (',') != MATCH_YES
)
8294 gfc_error ("Syntax error in PROCEDURE statement at %C");
8299 /* Match a GENERIC procedure binding inside a derived type. */
8302 gfc_match_generic (void)
8304 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8305 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8307 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8308 gfc_typebound_proc
* tb
;
8310 interface_type op_type
;
8311 gfc_intrinsic_op op
;
8314 /* Check current state. */
8315 if (gfc_current_state () == COMP_DERIVED
)
8317 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8320 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8322 block
= gfc_state_stack
->previous
->sym
;
8323 ns
= block
->f2k_derived
;
8324 gcc_assert (block
&& ns
);
8326 memset (&tbattr
, 0, sizeof (tbattr
));
8327 tbattr
.where
= gfc_current_locus
;
8329 /* See if we get an access-specifier. */
8330 m
= match_binding_attributes (&tbattr
, true, false);
8331 if (m
== MATCH_ERROR
)
8334 /* Now the colons, those are required. */
8335 if (gfc_match (" ::") != MATCH_YES
)
8337 gfc_error ("Expected '::' at %C");
8341 /* Match the binding name; depending on type (operator / generic) format
8342 it for future error messages into bind_name. */
8344 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8345 if (m
== MATCH_ERROR
)
8349 gfc_error ("Expected generic name or operator descriptor at %C");
8355 case INTERFACE_GENERIC
:
8356 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8359 case INTERFACE_USER_OP
:
8360 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8363 case INTERFACE_INTRINSIC_OP
:
8364 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8365 gfc_op2string (op
));
8372 /* Match the required =>. */
8373 if (gfc_match (" =>") != MATCH_YES
)
8375 gfc_error ("Expected '=>' at %C");
8379 /* Try to find existing GENERIC binding with this name / for this operator;
8380 if there is something, check that it is another GENERIC and then extend
8381 it rather than building a new node. Otherwise, create it and put it
8382 at the right position. */
8386 case INTERFACE_USER_OP
:
8387 case INTERFACE_GENERIC
:
8389 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8392 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8404 case INTERFACE_INTRINSIC_OP
:
8414 if (!tb
->is_generic
)
8416 gcc_assert (op_type
== INTERFACE_GENERIC
);
8417 gfc_error ("There's already a non-generic procedure with binding name"
8418 " '%s' for the derived type '%s' at %C",
8419 bind_name
, block
->name
);
8423 if (tb
->access
!= tbattr
.access
)
8425 gfc_error ("Binding at %C must have the same access as already"
8426 " defined binding '%s'", bind_name
);
8432 tb
= gfc_get_typebound_proc (NULL
);
8433 tb
->where
= gfc_current_locus
;
8434 tb
->access
= tbattr
.access
;
8436 tb
->u
.generic
= NULL
;
8440 case INTERFACE_GENERIC
:
8441 case INTERFACE_USER_OP
:
8443 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8446 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8454 case INTERFACE_INTRINSIC_OP
:
8463 /* Now, match all following names as specific targets. */
8466 gfc_symtree
* target_st
;
8467 gfc_tbp_generic
* target
;
8469 m
= gfc_match_name (name
);
8470 if (m
== MATCH_ERROR
)
8474 gfc_error ("Expected specific binding name at %C");
8478 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8480 /* See if this is a duplicate specification. */
8481 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8482 if (target_st
== target
->specific_st
)
8484 gfc_error ("'%s' already defined as specific binding for the"
8485 " generic '%s' at %C", name
, bind_name
);
8489 target
= gfc_get_tbp_generic ();
8490 target
->specific_st
= target_st
;
8491 target
->specific
= NULL
;
8492 target
->next
= tb
->u
.generic
;
8493 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8494 || (op_type
== INTERFACE_INTRINSIC_OP
));
8495 tb
->u
.generic
= target
;
8497 while (gfc_match (" ,") == MATCH_YES
);
8499 /* Here should be the end. */
8500 if (gfc_match_eos () != MATCH_YES
)
8502 gfc_error ("Junk after GENERIC binding at %C");
8513 /* Match a FINAL declaration inside a derived type. */
8516 gfc_match_final_decl (void)
8518 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8521 gfc_namespace
* module_ns
;
8525 if (gfc_current_form
== FORM_FREE
)
8527 char c
= gfc_peek_ascii_char ();
8528 if (!gfc_is_whitespace (c
) && c
!= ':')
8532 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8534 if (gfc_current_form
== FORM_FIXED
)
8537 gfc_error ("FINAL declaration at %C must be inside a derived type "
8538 "CONTAINS section");
8542 block
= gfc_state_stack
->previous
->sym
;
8545 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8546 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8548 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8549 " specification part of a MODULE");
8553 module_ns
= gfc_current_ns
;
8554 gcc_assert (module_ns
);
8555 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8557 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8558 if (gfc_match (" ::") == MATCH_ERROR
)
8561 /* Match the sequence of procedure names. */
8568 if (first
&& gfc_match_eos () == MATCH_YES
)
8570 gfc_error ("Empty FINAL at %C");
8574 m
= gfc_match_name (name
);
8577 gfc_error ("Expected module procedure name at %C");
8580 else if (m
!= MATCH_YES
)
8583 if (gfc_match_eos () == MATCH_YES
)
8585 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8587 gfc_error ("Expected ',' at %C");
8591 if (gfc_get_symbol (name
, module_ns
, &sym
))
8593 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8597 /* Mark the symbol as module procedure. */
8598 if (sym
->attr
.proc
!= PROC_MODULE
8599 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8602 /* Check if we already have this symbol in the list, this is an error. */
8603 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8604 if (f
->proc_sym
== sym
)
8606 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8611 /* Add this symbol to the list of finalizers. */
8612 gcc_assert (block
->f2k_derived
);
8614 f
= XCNEW (gfc_finalizer
);
8616 f
->proc_tree
= NULL
;
8617 f
->where
= gfc_current_locus
;
8618 f
->next
= block
->f2k_derived
->finalizers
;
8619 block
->f2k_derived
->finalizers
= f
;
8629 const ext_attr_t ext_attr_list
[] = {
8630 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8631 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8632 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8633 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8634 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8635 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8636 { NULL
, EXT_ATTR_LAST
, NULL
}
8639 /* Match a !GCC$ ATTRIBUTES statement of the form:
8640 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8641 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8643 TODO: We should support all GCC attributes using the same syntax for
8644 the attribute list, i.e. the list in C
8645 __attributes(( attribute-list ))
8647 !GCC$ ATTRIBUTES attribute-list ::
8648 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8651 As there is absolutely no risk of confusion, we should never return
8654 gfc_match_gcc_attributes (void)
8656 symbol_attribute attr
;
8657 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8662 gfc_clear_attr (&attr
);
8667 if (gfc_match_name (name
) != MATCH_YES
)
8670 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8671 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8674 if (id
== EXT_ATTR_LAST
)
8676 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8680 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8683 gfc_gobble_whitespace ();
8684 ch
= gfc_next_ascii_char ();
8687 /* This is the successful exit condition for the loop. */
8688 if (gfc_next_ascii_char () == ':')
8698 if (gfc_match_eos () == MATCH_YES
)
8703 m
= gfc_match_name (name
);
8707 if (find_special (name
, &sym
, true))
8710 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8712 if (gfc_match_eos () == MATCH_YES
)
8715 if (gfc_match_char (',') != MATCH_YES
)
8722 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");