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");
513 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
515 /* Mark the variable as having appeared in a data statement. */
516 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
522 /* Chain in namespace list of DATA initializers. */
523 newdata
->next
= gfc_current_ns
->data
;
524 gfc_current_ns
->data
= newdata
;
530 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
531 we are matching a DATA statement and are therefore issuing an error
532 if we encounter something unexpected, if not, we're trying to match
533 an old-style initialization expression of the form INTEGER I /2/. */
536 gfc_match_data (void)
541 set_in_match_data (true);
545 new_data
= gfc_get_data ();
546 new_data
->where
= gfc_current_locus
;
548 m
= top_var_list (new_data
);
552 m
= top_val_list (new_data
);
556 new_data
->next
= gfc_current_ns
->data
;
557 gfc_current_ns
->data
= new_data
;
559 if (gfc_match_eos () == MATCH_YES
)
562 gfc_match_char (','); /* Optional comma */
565 set_in_match_data (false);
569 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
572 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
577 set_in_match_data (false);
578 gfc_free_data (new_data
);
583 /************************ Declaration statements *********************/
586 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
589 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
593 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
594 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
596 gfc_error ("The assumed-rank array at %C shall not have a codimension");
600 if (to
->rank
== 0 && from
->rank
> 0)
602 to
->rank
= from
->rank
;
603 to
->type
= from
->type
;
604 to
->cray_pointee
= from
->cray_pointee
;
605 to
->cp_was_assumed
= from
->cp_was_assumed
;
607 for (i
= 0; i
< to
->corank
; i
++)
609 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
610 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
612 for (i
= 0; i
< from
->rank
; i
++)
616 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
617 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
621 to
->lower
[i
] = from
->lower
[i
];
622 to
->upper
[i
] = from
->upper
[i
];
626 else if (to
->corank
== 0 && from
->corank
> 0)
628 to
->corank
= from
->corank
;
629 to
->cotype
= from
->cotype
;
631 for (i
= 0; i
< from
->corank
; i
++)
635 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
636 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
640 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
641 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
650 /* Match an intent specification. Since this can only happen after an
651 INTENT word, a legal intent-spec must follow. */
654 match_intent_spec (void)
657 if (gfc_match (" ( in out )") == MATCH_YES
)
659 if (gfc_match (" ( in )") == MATCH_YES
)
661 if (gfc_match (" ( out )") == MATCH_YES
)
664 gfc_error ("Bad INTENT specification at %C");
665 return INTENT_UNKNOWN
;
669 /* Matches a character length specification, which is either a
670 specification expression, '*', or ':'. */
673 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
680 if (gfc_match_char ('*') == MATCH_YES
)
683 if (gfc_match_char (':') == MATCH_YES
)
685 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
694 m
= gfc_match_expr (expr
);
697 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
700 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
702 if ((*expr
)->value
.function
.actual
703 && (*expr
)->value
.function
.actual
->expr
->symtree
)
706 e
= (*expr
)->value
.function
.actual
->expr
;
707 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
708 && e
->expr_type
== EXPR_VARIABLE
)
710 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
712 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
713 && e
->symtree
->n
.sym
->ts
.u
.cl
714 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
722 gfc_error ("Conflict in attributes of function argument at %C");
727 /* A character length is a '*' followed by a literal integer or a
728 char_len_param_value in parenthesis. */
731 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
737 m
= gfc_match_char ('*');
741 m
= gfc_match_small_literal_int (&length
, NULL
);
742 if (m
== MATCH_ERROR
)
747 if (obsolescent_check
748 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
750 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
754 if (gfc_match_char ('(') == MATCH_NO
)
757 m
= char_len_param_value (expr
, deferred
);
758 if (m
!= MATCH_YES
&& gfc_matching_function
)
764 if (m
== MATCH_ERROR
)
769 if (gfc_match_char (')') == MATCH_NO
)
771 gfc_free_expr (*expr
);
779 gfc_error ("Syntax error in character length specification at %C");
784 /* Special subroutine for finding a symbol. Check if the name is found
785 in the current name space. If not, and we're compiling a function or
786 subroutine and the parent compilation unit is an interface, then check
787 to see if the name we've been given is the name of the interface
788 (located in another namespace). */
791 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
797 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
800 *result
= st
? st
->n
.sym
: NULL
;
804 if (gfc_current_state () != COMP_SUBROUTINE
805 && gfc_current_state () != COMP_FUNCTION
)
808 s
= gfc_state_stack
->previous
;
812 if (s
->state
!= COMP_INTERFACE
)
815 goto end
; /* Nameless interface. */
817 if (strcmp (name
, s
->sym
->name
) == 0)
828 /* Special subroutine for getting a symbol node associated with a
829 procedure name, used in SUBROUTINE and FUNCTION statements. The
830 symbol is created in the parent using with symtree node in the
831 child unit pointing to the symbol. If the current namespace has no
832 parent, then the symbol is just created in the current unit. */
835 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
841 /* Module functions have to be left in their own namespace because
842 they have potentially (almost certainly!) already been referenced.
843 In this sense, they are rather like external functions. This is
844 fixed up in resolve.c(resolve_entries), where the symbol name-
845 space is set to point to the master function, so that the fake
846 result mechanism can work. */
847 if (module_fcn_entry
)
849 /* Present if entry is declared to be a module procedure. */
850 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
853 rc
= gfc_get_symbol (name
, NULL
, result
);
854 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
855 && (*result
)->ts
.type
== BT_UNKNOWN
856 && sym
->attr
.flavor
== FL_UNKNOWN
)
857 /* Pick up the typespec for the entry, if declared in the function
858 body. Note that this symbol is FL_UNKNOWN because it will
859 only have appeared in a type declaration. The local symtree
860 is set to point to the module symbol and a unique symtree
861 to the local version. This latter ensures a correct clearing
864 /* If the ENTRY proceeds its specification, we need to ensure
865 that this does not raise a "has no IMPLICIT type" error. */
866 if (sym
->ts
.type
== BT_UNKNOWN
)
867 sym
->attr
.untyped
= 1;
869 (*result
)->ts
= sym
->ts
;
871 /* Put the symbol in the procedure namespace so that, should
872 the ENTRY precede its specification, the specification
874 (*result
)->ns
= gfc_current_ns
;
876 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
878 st
= gfc_get_unique_symtree (gfc_current_ns
);
883 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
890 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
892 /* Trap another encompassed procedure with the same name. All
893 these conditions are necessary to avoid picking up an entry
894 whose name clashes with that of the encompassing procedure;
895 this is handled using gsymbols to register unique,globally
897 if (sym
->attr
.flavor
!= 0
898 && sym
->attr
.proc
!= 0
899 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
900 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
901 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
902 name
, &sym
->declared_at
);
904 /* Trap a procedure with a name the same as interface in the
905 encompassing scope. */
906 if (sym
->attr
.generic
!= 0
907 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
908 && !sym
->attr
.mod_proc
)
909 gfc_error_now ("Name '%s' at %C is already defined"
910 " as a generic interface at %L",
911 name
, &sym
->declared_at
);
913 /* Trap declarations of attributes in encompassing scope. The
914 signature for this is that ts.kind is set. Legitimate
915 references only set ts.type. */
916 if (sym
->ts
.kind
!= 0
917 && !sym
->attr
.implicit_type
918 && sym
->attr
.proc
== 0
919 && gfc_current_ns
->parent
!= NULL
920 && sym
->attr
.access
== 0
921 && !module_fcn_entry
)
922 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
923 "and must not have attributes declared at %L",
924 name
, &sym
->declared_at
);
927 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
930 /* Module function entries will already have a symtree in
931 the current namespace but will need one at module level. */
932 if (module_fcn_entry
)
934 /* Present if entry is declared to be a module procedure. */
935 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
937 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
940 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
945 /* See if the procedure should be a module procedure. */
947 if (((sym
->ns
->proc_name
!= NULL
948 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
949 && sym
->attr
.proc
!= PROC_MODULE
)
950 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
951 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
958 /* Verify that the given symbol representing a parameter is C
959 interoperable, by checking to see if it was marked as such after
960 its declaration. If the given symbol is not interoperable, a
961 warning is reported, thus removing the need to return the status to
962 the calling function. The standard does not require the user use
963 one of the iso_c_binding named constants to declare an
964 interoperable parameter, but we can't be sure if the param is C
965 interop or not if the user doesn't. For example, integer(4) may be
966 legal Fortran, but doesn't have meaning in C. It may interop with
967 a number of the C types, which causes a problem because the
968 compiler can't know which one. This code is almost certainly not
969 portable, and the user will get what they deserve if the C type
970 across platforms isn't always interoperable with integer(4). If
971 the user had used something like integer(c_int) or integer(c_long),
972 the compiler could have automatically handled the varying sizes
976 gfc_verify_c_interop_param (gfc_symbol
*sym
)
978 int is_c_interop
= 0;
981 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
982 Don't repeat the checks here. */
983 if (sym
->attr
.implicit_type
)
986 /* For subroutines or functions that are passed to a BIND(C) procedure,
987 they're interoperable if they're BIND(C) and their params are all
989 if (sym
->attr
.flavor
== FL_PROCEDURE
)
991 if (sym
->attr
.is_bind_c
== 0)
993 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
994 "attribute to be C interoperable", sym
->name
,
995 &(sym
->declared_at
));
1001 if (sym
->attr
.is_c_interop
== 1)
1002 /* We've already checked this procedure; don't check it again. */
1005 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1010 /* See if we've stored a reference to a procedure that owns sym. */
1011 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1013 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1015 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1017 if (is_c_interop
!= 1)
1019 /* Make personalized messages to give better feedback. */
1020 if (sym
->ts
.type
== BT_DERIVED
)
1021 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1022 "BIND(C) procedure '%s' but is not C interoperable "
1023 "because derived type '%s' is not C interoperable",
1024 sym
->name
, &(sym
->declared_at
),
1025 sym
->ns
->proc_name
->name
,
1026 sym
->ts
.u
.derived
->name
);
1027 else if (sym
->ts
.type
== BT_CLASS
)
1028 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1029 "BIND(C) procedure '%s' but is not C interoperable "
1030 "because it is polymorphic",
1031 sym
->name
, &(sym
->declared_at
),
1032 sym
->ns
->proc_name
->name
);
1033 else if (gfc_option
.warn_c_binding_type
)
1034 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1035 "BIND(C) procedure '%s' but may not be C "
1037 sym
->name
, &(sym
->declared_at
),
1038 sym
->ns
->proc_name
->name
);
1041 /* Character strings are only C interoperable if they have a
1043 if (sym
->ts
.type
== BT_CHARACTER
)
1045 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1046 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1047 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1049 gfc_error ("Character argument '%s' at %L "
1050 "must be length 1 because "
1051 "procedure '%s' is BIND(C)",
1052 sym
->name
, &sym
->declared_at
,
1053 sym
->ns
->proc_name
->name
);
1058 /* We have to make sure that any param to a bind(c) routine does
1059 not have the allocatable, pointer, or optional attributes,
1060 according to J3/04-007, section 5.1. */
1061 if (sym
->attr
.allocatable
== 1
1062 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1063 "ALLOCATABLE attribute in procedure '%s' "
1064 "with BIND(C)", sym
->name
,
1065 &(sym
->declared_at
),
1066 sym
->ns
->proc_name
->name
))
1069 if (sym
->attr
.pointer
== 1
1070 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1071 "POINTER attribute in procedure '%s' "
1072 "with BIND(C)", sym
->name
,
1073 &(sym
->declared_at
),
1074 sym
->ns
->proc_name
->name
))
1077 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1079 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1080 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1081 " supported", sym
->name
, &(sym
->declared_at
),
1082 sym
->ns
->proc_name
->name
);
1086 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1088 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1089 "and the VALUE attribute because procedure '%s' "
1090 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1091 sym
->ns
->proc_name
->name
);
1094 else if (sym
->attr
.optional
== 1
1095 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' "
1096 "at %L with OPTIONAL attribute in "
1097 "procedure '%s' which is BIND(C)",
1098 sym
->name
, &(sym
->declared_at
),
1099 sym
->ns
->proc_name
->name
))
1102 /* Make sure that if it has the dimension attribute, that it is
1103 either assumed size or explicit shape. Deferred shape is already
1104 covered by the pointer/allocatable attribute. */
1105 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1106 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array '%s' "
1107 "at %L as dummy argument to the BIND(C) "
1108 "procedure '%s' at %L", sym
->name
,
1109 &(sym
->declared_at
),
1110 sym
->ns
->proc_name
->name
,
1111 &(sym
->ns
->proc_name
->declared_at
)))
1121 /* Function called by variable_decl() that adds a name to the symbol table. */
1124 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1125 gfc_array_spec
**as
, locus
*var_locus
)
1127 symbol_attribute attr
;
1130 if (gfc_get_symbol (name
, NULL
, &sym
))
1133 /* Start updating the symbol table. Add basic type attribute if present. */
1134 if (current_ts
.type
!= BT_UNKNOWN
1135 && (sym
->attr
.implicit_type
== 0
1136 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1137 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1140 if (sym
->ts
.type
== BT_CHARACTER
)
1143 sym
->ts
.deferred
= cl_deferred
;
1146 /* Add dimension attribute if present. */
1147 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1151 /* Add attribute to symbol. The copy is so that we can reset the
1152 dimension attribute. */
1153 attr
= current_attr
;
1155 attr
.codimension
= 0;
1157 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1160 /* Finish any work that may need to be done for the binding label,
1161 if it's a bind(c). The bind(c) attr is found before the symbol
1162 is made, and before the symbol name (for data decls), so the
1163 current_ts is holding the binding label, or nothing if the
1164 name= attr wasn't given. Therefore, test here if we're dealing
1165 with a bind(c) and make sure the binding label is set correctly. */
1166 if (sym
->attr
.is_bind_c
== 1)
1168 if (!sym
->binding_label
)
1170 /* Set the binding label and verify that if a NAME= was specified
1171 then only one identifier was in the entity-decl-list. */
1172 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1173 num_idents_on_line
))
1178 /* See if we know we're in a common block, and if it's a bind(c)
1179 common then we need to make sure we're an interoperable type. */
1180 if (sym
->attr
.in_common
== 1)
1182 /* Test the common block object. */
1183 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1184 && sym
->ts
.is_c_interop
!= 1)
1186 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1187 "must be declared with a C interoperable "
1188 "kind since common block '%s' is BIND(C)",
1189 sym
->name
, sym
->common_block
->name
,
1190 sym
->common_block
->name
);
1195 sym
->attr
.implied_index
= 0;
1197 if (sym
->ts
.type
== BT_CLASS
)
1198 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1204 /* Set character constant to the given length. The constant will be padded or
1205 truncated. If we're inside an array constructor without a typespec, we
1206 additionally check that all elements have the same length; check_len -1
1207 means no checking. */
1210 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1215 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1216 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1218 slen
= expr
->value
.character
.length
;
1221 s
= gfc_get_wide_string (len
+ 1);
1222 memcpy (s
, expr
->value
.character
.string
,
1223 MIN (len
, slen
) * sizeof (gfc_char_t
));
1225 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1227 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1228 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1229 "(%d/%d)", &expr
->where
, slen
, len
);
1231 /* Apply the standard by 'hand' otherwise it gets cleared for
1233 if (check_len
!= -1 && slen
!= check_len
1234 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1235 gfc_error_now ("The CHARACTER elements of the array constructor "
1236 "at %L must have the same length (%d/%d)",
1237 &expr
->where
, slen
, check_len
);
1240 free (expr
->value
.character
.string
);
1241 expr
->value
.character
.string
= s
;
1242 expr
->value
.character
.length
= len
;
1247 /* Function to create and update the enumerator history
1248 using the information passed as arguments.
1249 Pointer "max_enum" is also updated, to point to
1250 enum history node containing largest initializer.
1252 SYM points to the symbol node of enumerator.
1253 INIT points to its enumerator value. */
1256 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1258 enumerator_history
*new_enum_history
;
1259 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1261 new_enum_history
= XCNEW (enumerator_history
);
1263 new_enum_history
->sym
= sym
;
1264 new_enum_history
->initializer
= init
;
1265 new_enum_history
->next
= NULL
;
1267 if (enum_history
== NULL
)
1269 enum_history
= new_enum_history
;
1270 max_enum
= enum_history
;
1274 new_enum_history
->next
= enum_history
;
1275 enum_history
= new_enum_history
;
1277 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1278 new_enum_history
->initializer
->value
.integer
) < 0)
1279 max_enum
= new_enum_history
;
1284 /* Function to free enum kind history. */
1287 gfc_free_enum_history (void)
1289 enumerator_history
*current
= enum_history
;
1290 enumerator_history
*next
;
1292 while (current
!= NULL
)
1294 next
= current
->next
;
1299 enum_history
= NULL
;
1303 /* Function called by variable_decl() that adds an initialization
1304 expression to a symbol. */
1307 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1309 symbol_attribute attr
;
1314 if (find_special (name
, &sym
, false))
1319 /* If this symbol is confirming an implicit parameter type,
1320 then an initialization expression is not allowed. */
1321 if (attr
.flavor
== FL_PARAMETER
1322 && sym
->value
!= NULL
1325 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1332 /* An initializer is required for PARAMETER declarations. */
1333 if (attr
.flavor
== FL_PARAMETER
)
1335 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1341 /* If a variable appears in a DATA block, it cannot have an
1345 gfc_error ("Variable '%s' at %C with an initializer already "
1346 "appears in a DATA statement", sym
->name
);
1350 /* Check if the assignment can happen. This has to be put off
1351 until later for derived type variables and procedure pointers. */
1352 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1353 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1354 && !sym
->attr
.proc_pointer
1355 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1358 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1359 && init
->ts
.type
== BT_CHARACTER
)
1361 /* Update symbol character length according initializer. */
1362 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1365 if (sym
->ts
.u
.cl
->length
== NULL
)
1368 /* If there are multiple CHARACTER variables declared on the
1369 same line, we don't want them to share the same length. */
1370 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1372 if (sym
->attr
.flavor
== FL_PARAMETER
)
1374 if (init
->expr_type
== EXPR_CONSTANT
)
1376 clen
= init
->value
.character
.length
;
1377 sym
->ts
.u
.cl
->length
1378 = gfc_get_int_expr (gfc_default_integer_kind
,
1381 else if (init
->expr_type
== EXPR_ARRAY
)
1384 c
= gfc_constructor_first (init
->value
.constructor
);
1385 clen
= c
->expr
->value
.character
.length
;
1386 sym
->ts
.u
.cl
->length
1387 = gfc_get_int_expr (gfc_default_integer_kind
,
1390 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1391 sym
->ts
.u
.cl
->length
=
1392 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1395 /* Update initializer character length according symbol. */
1396 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1398 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1400 if (init
->expr_type
== EXPR_CONSTANT
)
1401 gfc_set_constant_character_len (len
, init
, -1);
1402 else if (init
->expr_type
== EXPR_ARRAY
)
1406 /* Build a new charlen to prevent simplification from
1407 deleting the length before it is resolved. */
1408 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1409 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1411 for (c
= gfc_constructor_first (init
->value
.constructor
);
1412 c
; c
= gfc_constructor_next (c
))
1413 gfc_set_constant_character_len (len
, c
->expr
, -1);
1418 /* If sym is implied-shape, set its upper bounds from init. */
1419 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1420 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1424 if (init
->rank
== 0)
1426 gfc_error ("Can't initialize implied-shape array at %L"
1427 " with scalar", &sym
->declared_at
);
1430 gcc_assert (sym
->as
->rank
== init
->rank
);
1432 /* Shape should be present, we get an initialization expression. */
1433 gcc_assert (init
->shape
);
1435 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1441 lower
= sym
->as
->lower
[dim
];
1442 if (lower
->expr_type
!= EXPR_CONSTANT
)
1444 gfc_error ("Non-constant lower bound in implied-shape"
1445 " declaration at %L", &lower
->where
);
1449 /* All dimensions must be without upper bound. */
1450 gcc_assert (!sym
->as
->upper
[dim
]);
1453 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1454 mpz_add (e
->value
.integer
,
1455 lower
->value
.integer
, init
->shape
[dim
]);
1456 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1457 sym
->as
->upper
[dim
] = e
;
1460 sym
->as
->type
= AS_EXPLICIT
;
1463 /* Need to check if the expression we initialized this
1464 to was one of the iso_c_binding named constants. If so,
1465 and we're a parameter (constant), let it be iso_c.
1467 integer(c_int), parameter :: my_int = c_int
1468 integer(my_int) :: my_int_2
1469 If we mark my_int as iso_c (since we can see it's value
1470 is equal to one of the named constants), then my_int_2
1471 will be considered C interoperable. */
1472 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1474 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1475 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1476 /* attr bits needed for module files. */
1477 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1478 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1479 if (init
->ts
.is_iso_c
)
1480 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1483 /* Add initializer. Make sure we keep the ranks sane. */
1484 if (sym
->attr
.dimension
&& init
->rank
== 0)
1489 if (sym
->attr
.flavor
== FL_PARAMETER
1490 && init
->expr_type
== EXPR_CONSTANT
1491 && spec_size (sym
->as
, &size
)
1492 && mpz_cmp_si (size
, 0) > 0)
1494 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1496 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1497 gfc_constructor_append_expr (&array
->value
.constructor
,
1500 : gfc_copy_expr (init
),
1503 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1504 for (n
= 0; n
< sym
->as
->rank
; n
++)
1505 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1510 init
->rank
= sym
->as
->rank
;
1514 if (sym
->attr
.save
== SAVE_NONE
)
1515 sym
->attr
.save
= SAVE_IMPLICIT
;
1523 /* Function called by variable_decl() that adds a name to a structure
1527 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1528 gfc_array_spec
**as
)
1533 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1534 constructing, it must have the pointer attribute. */
1535 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1536 && current_ts
.u
.derived
== gfc_current_block ()
1537 && current_attr
.pointer
== 0)
1539 gfc_error ("Component at %C must have the POINTER attribute");
1543 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1545 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1547 gfc_error ("Array component of structure at %C must have explicit "
1548 "or deferred shape");
1553 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1557 if (c
->ts
.type
== BT_CHARACTER
)
1559 c
->attr
= current_attr
;
1561 c
->initializer
= *init
;
1568 c
->attr
.codimension
= 1;
1570 c
->attr
.dimension
= 1;
1574 /* Should this ever get more complicated, combine with similar section
1575 in add_init_expr_to_sym into a separate function. */
1576 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1578 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1582 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1583 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1584 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1586 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1588 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1589 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1590 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1591 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1593 gfc_constructor
*ctor
;
1594 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1599 bool has_ts
= (c
->initializer
->ts
.u
.cl
1600 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1602 /* Remember the length of the first element for checking
1603 that all elements *in the constructor* have the same
1604 length. This need not be the length of the LHS! */
1605 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1606 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1607 first_len
= ctor
->expr
->value
.character
.length
;
1609 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1610 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1612 gfc_set_constant_character_len (len
, ctor
->expr
,
1613 has_ts
? -1 : first_len
);
1614 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1620 /* Check array components. */
1621 if (!c
->attr
.dimension
)
1624 if (c
->attr
.pointer
)
1626 if (c
->as
->type
!= AS_DEFERRED
)
1628 gfc_error ("Pointer array component of structure at %C must have a "
1633 else if (c
->attr
.allocatable
)
1635 if (c
->as
->type
!= AS_DEFERRED
)
1637 gfc_error ("Allocatable component of structure at %C must have a "
1644 if (c
->as
->type
!= AS_EXPLICIT
)
1646 gfc_error ("Array component of structure at %C must have an "
1653 if (c
->ts
.type
== BT_CLASS
)
1655 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1665 /* Match a 'NULL()', and possibly take care of some side effects. */
1668 gfc_match_null (gfc_expr
**result
)
1671 match m
, m2
= MATCH_NO
;
1673 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1679 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1681 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1684 old_loc
= gfc_current_locus
;
1685 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1688 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1692 gfc_current_locus
= old_loc
;
1697 /* The NULL symbol now has to be/become an intrinsic function. */
1698 if (gfc_get_symbol ("null", NULL
, &sym
))
1700 gfc_error ("NULL() initialization at %C is ambiguous");
1704 gfc_intrinsic_symbol (sym
);
1706 if (sym
->attr
.proc
!= PROC_INTRINSIC
1707 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1708 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1709 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1712 *result
= gfc_get_null_expr (&gfc_current_locus
);
1714 /* Invalid per F2008, C512. */
1715 if (m2
== MATCH_YES
)
1717 gfc_error ("NULL() initialization at %C may not have MOLD");
1725 /* Match the initialization expr for a data pointer or procedure pointer. */
1728 match_pointer_init (gfc_expr
**init
, int procptr
)
1732 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1734 gfc_error ("Initialization of pointer at %C is not allowed in "
1735 "a PURE procedure");
1738 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
1740 /* Match NULL() initialization. */
1741 m
= gfc_match_null (init
);
1745 /* Match non-NULL initialization. */
1746 gfc_matching_ptr_assignment
= !procptr
;
1747 gfc_matching_procptr_assignment
= procptr
;
1748 m
= gfc_match_rvalue (init
);
1749 gfc_matching_ptr_assignment
= 0;
1750 gfc_matching_procptr_assignment
= 0;
1751 if (m
== MATCH_ERROR
)
1753 else if (m
== MATCH_NO
)
1755 gfc_error ("Error in pointer initialization at %C");
1760 gfc_resolve_expr (*init
);
1762 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1763 "initialization at %C"))
1771 check_function_name (char *name
)
1773 /* In functions that have a RESULT variable defined, the function name always
1774 refers to function calls. Therefore, the name is not allowed to appear in
1775 specification statements. When checking this, be careful about
1776 'hidden' procedure pointer results ('ppr@'). */
1778 if (gfc_current_state () == COMP_FUNCTION
)
1780 gfc_symbol
*block
= gfc_current_block ();
1781 if (block
&& block
->result
&& block
->result
!= block
1782 && strcmp (block
->result
->name
, "ppr@") != 0
1783 && strcmp (block
->name
, name
) == 0)
1785 gfc_error ("Function name '%s' not allowed at %C", name
);
1794 /* Match a variable name with an optional initializer. When this
1795 subroutine is called, a variable is expected to be parsed next.
1796 Depending on what is happening at the moment, updates either the
1797 symbol table or the current interface. */
1800 variable_decl (int elem
)
1802 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1803 gfc_expr
*initializer
, *char_len
;
1805 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1817 /* When we get here, we've just matched a list of attributes and
1818 maybe a type and a double colon. The next thing we expect to see
1819 is the name of the symbol. */
1820 m
= gfc_match_name (name
);
1824 var_locus
= gfc_current_locus
;
1826 /* Now we could see the optional array spec. or character length. */
1827 m
= gfc_match_array_spec (&as
, true, true);
1828 if (m
== MATCH_ERROR
)
1832 as
= gfc_copy_array_spec (current_as
);
1834 && !merge_array_spec (current_as
, as
, true))
1840 if (gfc_option
.flag_cray_pointer
)
1841 cp_as
= gfc_copy_array_spec (as
);
1843 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1844 determine (and check) whether it can be implied-shape. If it
1845 was parsed as assumed-size, change it because PARAMETERs can not
1849 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1852 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1857 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1858 && current_attr
.flavor
== FL_PARAMETER
)
1859 as
->type
= AS_IMPLIED_SHAPE
;
1861 if (as
->type
== AS_IMPLIED_SHAPE
1862 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1872 cl_deferred
= false;
1874 if (current_ts
.type
== BT_CHARACTER
)
1876 switch (match_char_length (&char_len
, &cl_deferred
, false))
1879 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1881 cl
->length
= char_len
;
1884 /* Non-constant lengths need to be copied after the first
1885 element. Also copy assumed lengths. */
1888 && (current_ts
.u
.cl
->length
== NULL
1889 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1891 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1892 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1895 cl
= current_ts
.u
.cl
;
1897 cl_deferred
= current_ts
.deferred
;
1906 /* If this symbol has already shown up in a Cray Pointer declaration,
1907 then we want to set the type & bail out. */
1908 if (gfc_option
.flag_cray_pointer
)
1910 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1911 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1913 sym
->ts
.type
= current_ts
.type
;
1914 sym
->ts
.kind
= current_ts
.kind
;
1916 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1917 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1918 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1921 /* Check to see if we have an array specification. */
1924 if (sym
->as
!= NULL
)
1926 gfc_error ("Duplicate array spec for Cray pointee at %C");
1927 gfc_free_array_spec (cp_as
);
1933 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1934 gfc_internal_error ("Couldn't set pointee array spec.");
1936 /* Fix the array spec. */
1937 m
= gfc_mod_pointee_as (sym
->as
);
1938 if (m
== MATCH_ERROR
)
1946 gfc_free_array_spec (cp_as
);
1950 /* Procedure pointer as function result. */
1951 if (gfc_current_state () == COMP_FUNCTION
1952 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1953 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1954 strcpy (name
, "ppr@");
1956 if (gfc_current_state () == COMP_FUNCTION
1957 && strcmp (name
, gfc_current_block ()->name
) == 0
1958 && gfc_current_block ()->result
1959 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1960 strcpy (name
, "ppr@");
1962 /* OK, we've successfully matched the declaration. Now put the
1963 symbol in the current namespace, because it might be used in the
1964 optional initialization expression for this symbol, e.g. this is
1967 integer, parameter :: i = huge(i)
1969 This is only true for parameters or variables of a basic type.
1970 For components of derived types, it is not true, so we don't
1971 create a symbol for those yet. If we fail to create the symbol,
1973 if (gfc_current_state () != COMP_DERIVED
1974 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
1980 if (!check_function_name (name
))
1986 /* We allow old-style initializations of the form
1987 integer i /2/, j(4) /3*3, 1/
1988 (if no colon has been seen). These are different from data
1989 statements in that initializers are only allowed to apply to the
1990 variable immediately preceding, i.e.
1992 is not allowed. Therefore we have to do some work manually, that
1993 could otherwise be left to the matchers for DATA statements. */
1995 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1997 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
1998 "initialization at %C"))
2000 else if (gfc_current_state () == COMP_DERIVED
)
2002 gfc_error ("Invalid old style initialization for derived type "
2008 return match_old_style_init (name
);
2011 /* The double colon must be present in order to have initializers.
2012 Otherwise the statement is ambiguous with an assignment statement. */
2015 if (gfc_match (" =>") == MATCH_YES
)
2017 if (!current_attr
.pointer
)
2019 gfc_error ("Initialization at %C isn't for a pointer variable");
2024 m
= match_pointer_init (&initializer
, 0);
2028 else if (gfc_match_char ('=') == MATCH_YES
)
2030 if (current_attr
.pointer
)
2032 gfc_error ("Pointer initialization at %C requires '=>', "
2038 m
= gfc_match_init_expr (&initializer
);
2041 gfc_error ("Expected an initialization expression at %C");
2045 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2046 && gfc_state_stack
->state
!= COMP_DERIVED
)
2048 gfc_error ("Initialization of variable at %C is not allowed in "
2049 "a PURE procedure");
2053 if (current_attr
.flavor
!= FL_PARAMETER
2054 && gfc_state_stack
->state
!= COMP_DERIVED
)
2055 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2062 if (initializer
!= NULL
&& current_attr
.allocatable
2063 && gfc_current_state () == COMP_DERIVED
)
2065 gfc_error ("Initialization of allocatable component at %C is not "
2071 /* Add the initializer. Note that it is fine if initializer is
2072 NULL here, because we sometimes also need to check if a
2073 declaration *must* have an initialization expression. */
2074 if (gfc_current_state () != COMP_DERIVED
)
2075 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2078 if (current_ts
.type
== BT_DERIVED
2079 && !current_attr
.pointer
&& !initializer
)
2080 initializer
= gfc_default_initializer (¤t_ts
);
2081 t
= build_struct (name
, cl
, &initializer
, &as
);
2084 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2087 /* Free stuff up and return. */
2088 gfc_free_expr (initializer
);
2089 gfc_free_array_spec (as
);
2095 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2096 This assumes that the byte size is equal to the kind number for
2097 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2100 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2105 if (gfc_match_char ('*') != MATCH_YES
)
2108 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2112 original_kind
= ts
->kind
;
2114 /* Massage the kind numbers for complex types. */
2115 if (ts
->type
== BT_COMPLEX
)
2119 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2120 gfc_basic_typename (ts
->type
), original_kind
);
2127 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2130 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2134 if (gfc_option
.flag_real4_kind
== 8)
2136 if (gfc_option
.flag_real4_kind
== 10)
2138 if (gfc_option
.flag_real4_kind
== 16)
2144 if (gfc_option
.flag_real8_kind
== 4)
2146 if (gfc_option
.flag_real8_kind
== 10)
2148 if (gfc_option
.flag_real8_kind
== 16)
2153 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2155 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2156 gfc_basic_typename (ts
->type
), original_kind
);
2160 if (!gfc_notify_std (GFC_STD_GNU
,
2161 "Nonstandard type declaration %s*%d at %C",
2162 gfc_basic_typename(ts
->type
), original_kind
))
2169 /* Match a kind specification. Since kinds are generally optional, we
2170 usually return MATCH_NO if something goes wrong. If a "kind="
2171 string is found, then we know we have an error. */
2174 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2186 where
= loc
= gfc_current_locus
;
2191 if (gfc_match_char ('(') == MATCH_NO
)
2194 /* Also gobbles optional text. */
2195 if (gfc_match (" kind = ") == MATCH_YES
)
2198 loc
= gfc_current_locus
;
2201 n
= gfc_match_init_expr (&e
);
2205 if (gfc_matching_function
)
2207 /* The function kind expression might include use associated or
2208 imported parameters and try again after the specification
2210 if (gfc_match_char (')') != MATCH_YES
)
2212 gfc_error ("Missing right parenthesis at %C");
2218 gfc_undo_symbols ();
2223 /* ....or else, the match is real. */
2225 gfc_error ("Expected initialization expression at %C");
2233 gfc_error ("Expected scalar initialization expression at %C");
2238 msg
= gfc_extract_int (e
, &ts
->kind
);
2247 /* Before throwing away the expression, let's see if we had a
2248 C interoperable kind (and store the fact). */
2249 if (e
->ts
.is_c_interop
== 1)
2251 /* Mark this as C interoperable if being declared with one
2252 of the named constants from iso_c_binding. */
2253 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2254 ts
->f90_type
= e
->ts
.f90_type
;
2260 /* Ignore errors to this point, if we've gotten here. This means
2261 we ignore the m=MATCH_ERROR from above. */
2262 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2264 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2265 gfc_basic_typename (ts
->type
));
2266 gfc_current_locus
= where
;
2270 /* Warn if, e.g., c_int is used for a REAL variable, but not
2271 if, e.g., c_double is used for COMPLEX as the standard
2272 explicitly says that the kind type parameter for complex and real
2273 variable is the same, i.e. c_float == c_float_complex. */
2274 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2275 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2276 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2277 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2278 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2279 gfc_basic_typename (ts
->type
));
2281 gfc_gobble_whitespace ();
2282 if ((c
= gfc_next_ascii_char ()) != ')'
2283 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2285 if (ts
->type
== BT_CHARACTER
)
2286 gfc_error ("Missing right parenthesis or comma at %C");
2288 gfc_error ("Missing right parenthesis at %C");
2292 /* All tests passed. */
2295 if(m
== MATCH_ERROR
)
2296 gfc_current_locus
= where
;
2298 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2301 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2305 if (gfc_option
.flag_real4_kind
== 8)
2307 if (gfc_option
.flag_real4_kind
== 10)
2309 if (gfc_option
.flag_real4_kind
== 16)
2315 if (gfc_option
.flag_real8_kind
== 4)
2317 if (gfc_option
.flag_real8_kind
== 10)
2319 if (gfc_option
.flag_real8_kind
== 16)
2324 /* Return what we know from the test(s). */
2329 gfc_current_locus
= where
;
2335 match_char_kind (int * kind
, int * is_iso_c
)
2344 where
= gfc_current_locus
;
2346 n
= gfc_match_init_expr (&e
);
2348 if (n
!= MATCH_YES
&& gfc_matching_function
)
2350 /* The expression might include use-associated or imported
2351 parameters and try again after the specification
2354 gfc_undo_symbols ();
2359 gfc_error ("Expected initialization expression at %C");
2365 gfc_error ("Expected scalar initialization expression at %C");
2370 msg
= gfc_extract_int (e
, kind
);
2371 *is_iso_c
= e
->ts
.is_iso_c
;
2381 /* Ignore errors to this point, if we've gotten here. This means
2382 we ignore the m=MATCH_ERROR from above. */
2383 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2385 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2389 /* All tests passed. */
2392 if (m
== MATCH_ERROR
)
2393 gfc_current_locus
= where
;
2395 /* Return what we know from the test(s). */
2400 gfc_current_locus
= where
;
2405 /* Match the various kind/length specifications in a CHARACTER
2406 declaration. We don't return MATCH_NO. */
2409 gfc_match_char_spec (gfc_typespec
*ts
)
2411 int kind
, seen_length
, is_iso_c
;
2423 /* Try the old-style specification first. */
2424 old_char_selector
= 0;
2426 m
= match_char_length (&len
, &deferred
, true);
2430 old_char_selector
= 1;
2435 m
= gfc_match_char ('(');
2438 m
= MATCH_YES
; /* Character without length is a single char. */
2442 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2443 if (gfc_match (" kind =") == MATCH_YES
)
2445 m
= match_char_kind (&kind
, &is_iso_c
);
2447 if (m
== MATCH_ERROR
)
2452 if (gfc_match (" , len =") == MATCH_NO
)
2455 m
= char_len_param_value (&len
, &deferred
);
2458 if (m
== MATCH_ERROR
)
2465 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2466 if (gfc_match (" len =") == MATCH_YES
)
2468 m
= char_len_param_value (&len
, &deferred
);
2471 if (m
== MATCH_ERROR
)
2475 if (gfc_match_char (')') == MATCH_YES
)
2478 if (gfc_match (" , kind =") != MATCH_YES
)
2481 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2487 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2488 m
= char_len_param_value (&len
, &deferred
);
2491 if (m
== MATCH_ERROR
)
2495 m
= gfc_match_char (')');
2499 if (gfc_match_char (',') != MATCH_YES
)
2502 gfc_match (" kind ="); /* Gobble optional text. */
2504 m
= match_char_kind (&kind
, &is_iso_c
);
2505 if (m
== MATCH_ERROR
)
2511 /* Require a right-paren at this point. */
2512 m
= gfc_match_char (')');
2517 gfc_error ("Syntax error in CHARACTER declaration at %C");
2519 gfc_free_expr (len
);
2523 /* Deal with character functions after USE and IMPORT statements. */
2524 if (gfc_matching_function
)
2526 gfc_free_expr (len
);
2527 gfc_undo_symbols ();
2533 gfc_free_expr (len
);
2537 /* Do some final massaging of the length values. */
2538 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2540 if (seen_length
== 0)
2541 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2546 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2547 ts
->deferred
= deferred
;
2549 /* We have to know if it was a C interoperable kind so we can
2550 do accurate type checking of bind(c) procs, etc. */
2552 /* Mark this as C interoperable if being declared with one
2553 of the named constants from iso_c_binding. */
2554 ts
->is_c_interop
= is_iso_c
;
2555 else if (len
!= NULL
)
2556 /* Here, we might have parsed something such as: character(c_char)
2557 In this case, the parsing code above grabs the c_char when
2558 looking for the length (line 1690, roughly). it's the last
2559 testcase for parsing the kind params of a character variable.
2560 However, it's not actually the length. this seems like it
2562 To see if the user used a C interop kind, test the expr
2563 of the so called length, and see if it's C interoperable. */
2564 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2570 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2571 structure to the matched specification. This is necessary for FUNCTION and
2572 IMPLICIT statements.
2574 If implicit_flag is nonzero, then we don't check for the optional
2575 kind specification. Not doing so is needed for matching an IMPLICIT
2576 statement correctly. */
2579 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2581 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2582 gfc_symbol
*sym
, *dt_sym
;
2585 bool seen_deferred_kind
, matched_type
;
2586 const char *dt_name
;
2588 /* A belt and braces check that the typespec is correctly being treated
2589 as a deferred characteristic association. */
2590 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2591 && (gfc_current_block ()->result
->ts
.kind
== -1)
2592 && (ts
->kind
== -1);
2594 if (seen_deferred_kind
)
2597 /* Clear the current binding label, in case one is given. */
2598 curr_binding_label
= NULL
;
2600 if (gfc_match (" byte") == MATCH_YES
)
2602 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2605 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2607 gfc_error ("BYTE type used at %C "
2608 "is not available on the target machine");
2612 ts
->type
= BT_INTEGER
;
2618 m
= gfc_match (" type (");
2619 matched_type
= (m
== MATCH_YES
);
2622 gfc_gobble_whitespace ();
2623 if (gfc_peek_ascii_char () == '*')
2625 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2627 if (gfc_current_state () == COMP_DERIVED
)
2629 gfc_error ("Assumed type at %C is not allowed for components");
2632 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2635 ts
->type
= BT_ASSUMED
;
2639 m
= gfc_match ("%n", name
);
2640 matched_type
= (m
== MATCH_YES
);
2643 if ((matched_type
&& strcmp ("integer", name
) == 0)
2644 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2646 ts
->type
= BT_INTEGER
;
2647 ts
->kind
= gfc_default_integer_kind
;
2651 if ((matched_type
&& strcmp ("character", name
) == 0)
2652 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2655 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2656 "intrinsic-type-spec at %C"))
2659 ts
->type
= BT_CHARACTER
;
2660 if (implicit_flag
== 0)
2661 m
= gfc_match_char_spec (ts
);
2665 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2671 if ((matched_type
&& strcmp ("real", name
) == 0)
2672 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2675 ts
->kind
= gfc_default_real_kind
;
2680 && (strcmp ("doubleprecision", name
) == 0
2681 || (strcmp ("double", name
) == 0
2682 && gfc_match (" precision") == MATCH_YES
)))
2683 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2686 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2687 "intrinsic-type-spec at %C"))
2689 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2693 ts
->kind
= gfc_default_double_kind
;
2697 if ((matched_type
&& strcmp ("complex", name
) == 0)
2698 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2700 ts
->type
= BT_COMPLEX
;
2701 ts
->kind
= gfc_default_complex_kind
;
2706 && (strcmp ("doublecomplex", name
) == 0
2707 || (strcmp ("double", name
) == 0
2708 && gfc_match (" complex") == MATCH_YES
)))
2709 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2711 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2715 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2716 "intrinsic-type-spec at %C"))
2719 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2722 ts
->type
= BT_COMPLEX
;
2723 ts
->kind
= gfc_default_double_kind
;
2727 if ((matched_type
&& strcmp ("logical", name
) == 0)
2728 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2730 ts
->type
= BT_LOGICAL
;
2731 ts
->kind
= gfc_default_logical_kind
;
2736 m
= gfc_match_char (')');
2739 ts
->type
= BT_DERIVED
;
2742 /* Match CLASS declarations. */
2743 m
= gfc_match (" class ( * )");
2744 if (m
== MATCH_ERROR
)
2746 else if (m
== MATCH_YES
)
2750 ts
->type
= BT_CLASS
;
2751 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2754 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2755 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2757 gfc_set_sym_referenced (upe
);
2759 upe
->ts
.type
= BT_VOID
;
2760 upe
->attr
.unlimited_polymorphic
= 1;
2761 /* This is essential to force the construction of
2762 unlimited polymorphic component class containers. */
2763 upe
->attr
.zero_comp
= 1;
2764 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2765 &gfc_current_locus
))
2770 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2772 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2776 ts
->u
.derived
= upe
;
2780 m
= gfc_match (" class ( %n )", name
);
2783 ts
->type
= BT_CLASS
;
2785 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2789 /* Defer association of the derived type until the end of the
2790 specification block. However, if the derived type can be
2791 found, add it to the typespec. */
2792 if (gfc_matching_function
)
2794 ts
->u
.derived
= NULL
;
2795 if (gfc_current_state () != COMP_INTERFACE
2796 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2798 sym
= gfc_find_dt_in_generic (sym
);
2799 ts
->u
.derived
= sym
;
2804 /* Search for the name but allow the components to be defined later. If
2805 type = -1, this typespec has been seen in a function declaration but
2806 the type could not be accessed at that point. The actual derived type is
2807 stored in a symtree with the first letter of the name capitalized; the
2808 symtree with the all lower-case name contains the associated
2809 generic function. */
2810 dt_name
= gfc_get_string ("%c%s",
2811 (char) TOUPPER ((unsigned char) name
[0]),
2812 (const char*)&name
[1]);
2817 gfc_get_ha_symbol (name
, &sym
);
2818 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2820 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2823 if (sym
->generic
&& !dt_sym
)
2824 dt_sym
= gfc_find_dt_in_generic (sym
);
2826 else if (ts
->kind
== -1)
2828 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2829 || gfc_current_ns
->has_import_set
;
2830 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2831 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2833 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2836 if (sym
&& sym
->generic
&& !dt_sym
)
2837 dt_sym
= gfc_find_dt_in_generic (sym
);
2844 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2845 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2846 || sym
->attr
.subroutine
)
2848 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2849 "entity at %L, which has the same name", name
,
2854 gfc_set_sym_referenced (sym
);
2855 if (!sym
->attr
.generic
2856 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2859 if (!sym
->attr
.function
2860 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2865 gfc_interface
*intr
, *head
;
2867 /* Use upper case to save the actual derived-type symbol. */
2868 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2869 dt_sym
->name
= gfc_get_string (sym
->name
);
2870 head
= sym
->generic
;
2871 intr
= gfc_get_interface ();
2873 intr
->where
= gfc_current_locus
;
2875 sym
->generic
= intr
;
2876 sym
->attr
.if_source
= IFSRC_DECL
;
2879 gfc_set_sym_referenced (dt_sym
);
2881 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2882 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2885 ts
->u
.derived
= dt_sym
;
2891 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2892 "intrinsic-type-spec at %C"))
2895 /* For all types except double, derived and character, look for an
2896 optional kind specifier. MATCH_NO is actually OK at this point. */
2897 if (implicit_flag
== 1)
2899 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2905 if (gfc_current_form
== FORM_FREE
)
2907 c
= gfc_peek_ascii_char ();
2908 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2909 && c
!= ':' && c
!= ',')
2911 if (matched_type
&& c
== ')')
2913 gfc_next_ascii_char ();
2920 m
= gfc_match_kind_spec (ts
, false);
2921 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2922 m
= gfc_match_old_kind_spec (ts
);
2924 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2927 /* Defer association of the KIND expression of function results
2928 until after USE and IMPORT statements. */
2929 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2930 || gfc_matching_function
)
2934 m
= MATCH_YES
; /* No kind specifier found. */
2940 /* Match an IMPLICIT NONE statement. Actually, this statement is
2941 already matched in parse.c, or we would not end up here in the
2942 first place. So the only thing we need to check, is if there is
2943 trailing garbage. If not, the match is successful. */
2946 gfc_match_implicit_none (void)
2948 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2952 /* Match the letter range(s) of an IMPLICIT statement. */
2955 match_implicit_range (void)
2961 cur_loc
= gfc_current_locus
;
2963 gfc_gobble_whitespace ();
2964 c
= gfc_next_ascii_char ();
2967 gfc_error ("Missing character range in IMPLICIT at %C");
2974 gfc_gobble_whitespace ();
2975 c1
= gfc_next_ascii_char ();
2979 gfc_gobble_whitespace ();
2980 c
= gfc_next_ascii_char ();
2985 inner
= 0; /* Fall through. */
2992 gfc_gobble_whitespace ();
2993 c2
= gfc_next_ascii_char ();
2997 gfc_gobble_whitespace ();
2998 c
= gfc_next_ascii_char ();
3000 if ((c
!= ',') && (c
!= ')'))
3013 gfc_error ("Letters must be in alphabetic order in "
3014 "IMPLICIT statement at %C");
3018 /* See if we can add the newly matched range to the pending
3019 implicits from this IMPLICIT statement. We do not check for
3020 conflicts with whatever earlier IMPLICIT statements may have
3021 set. This is done when we've successfully finished matching
3023 if (!gfc_add_new_implicit_range (c1
, c2
))
3030 gfc_syntax_error (ST_IMPLICIT
);
3032 gfc_current_locus
= cur_loc
;
3037 /* Match an IMPLICIT statement, storing the types for
3038 gfc_set_implicit() if the statement is accepted by the parser.
3039 There is a strange looking, but legal syntactic construction
3040 possible. It looks like:
3042 IMPLICIT INTEGER (a-b) (c-d)
3044 This is legal if "a-b" is a constant expression that happens to
3045 equal one of the legal kinds for integers. The real problem
3046 happens with an implicit specification that looks like:
3048 IMPLICIT INTEGER (a-b)
3050 In this case, a typespec matcher that is "greedy" (as most of the
3051 matchers are) gobbles the character range as a kindspec, leaving
3052 nothing left. We therefore have to go a bit more slowly in the
3053 matching process by inhibiting the kindspec checking during
3054 typespec matching and checking for a kind later. */
3057 gfc_match_implicit (void)
3066 /* We don't allow empty implicit statements. */
3067 if (gfc_match_eos () == MATCH_YES
)
3069 gfc_error ("Empty IMPLICIT statement at %C");
3075 /* First cleanup. */
3076 gfc_clear_new_implicit ();
3078 /* A basic type is mandatory here. */
3079 m
= gfc_match_decl_type_spec (&ts
, 1);
3080 if (m
== MATCH_ERROR
)
3085 cur_loc
= gfc_current_locus
;
3086 m
= match_implicit_range ();
3090 /* We may have <TYPE> (<RANGE>). */
3091 gfc_gobble_whitespace ();
3092 c
= gfc_next_ascii_char ();
3093 if ((c
== '\n') || (c
== ','))
3095 /* Check for CHARACTER with no length parameter. */
3096 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3098 ts
.kind
= gfc_default_character_kind
;
3099 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3100 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3104 /* Record the Successful match. */
3105 if (!gfc_merge_new_implicit (&ts
))
3110 gfc_current_locus
= cur_loc
;
3113 /* Discard the (incorrectly) matched range. */
3114 gfc_clear_new_implicit ();
3116 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3117 if (ts
.type
== BT_CHARACTER
)
3118 m
= gfc_match_char_spec (&ts
);
3121 m
= gfc_match_kind_spec (&ts
, false);
3124 m
= gfc_match_old_kind_spec (&ts
);
3125 if (m
== MATCH_ERROR
)
3131 if (m
== MATCH_ERROR
)
3134 m
= match_implicit_range ();
3135 if (m
== MATCH_ERROR
)
3140 gfc_gobble_whitespace ();
3141 c
= gfc_next_ascii_char ();
3142 if ((c
!= '\n') && (c
!= ','))
3145 if (!gfc_merge_new_implicit (&ts
))
3153 gfc_syntax_error (ST_IMPLICIT
);
3161 gfc_match_import (void)
3163 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3168 if (gfc_current_ns
->proc_name
== NULL
3169 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3171 gfc_error ("IMPORT statement at %C only permitted in "
3172 "an INTERFACE body");
3176 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3179 if (gfc_match_eos () == MATCH_YES
)
3181 /* All host variables should be imported. */
3182 gfc_current_ns
->has_import_set
= 1;
3186 if (gfc_match (" ::") == MATCH_YES
)
3188 if (gfc_match_eos () == MATCH_YES
)
3190 gfc_error ("Expecting list of named entities at %C");
3198 m
= gfc_match (" %n", name
);
3202 if (gfc_current_ns
->parent
!= NULL
3203 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3205 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3208 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3209 && gfc_find_symbol (name
,
3210 gfc_current_ns
->proc_name
->ns
->parent
,
3213 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3219 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3220 "at %C - does not exist.", name
);
3224 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3226 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3231 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3234 sym
->attr
.imported
= 1;
3236 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3238 /* The actual derived type is stored in a symtree with the first
3239 letter of the name capitalized; the symtree with the all
3240 lower-case name contains the associated generic function. */
3241 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3242 gfc_get_string ("%c%s",
3243 (char) TOUPPER ((unsigned char) name
[0]),
3247 sym
->attr
.imported
= 1;
3260 if (gfc_match_eos () == MATCH_YES
)
3262 if (gfc_match_char (',') != MATCH_YES
)
3269 gfc_error ("Syntax error in IMPORT statement at %C");
3274 /* A minimal implementation of gfc_match without whitespace, escape
3275 characters or variable arguments. Returns true if the next
3276 characters match the TARGET template exactly. */
3279 match_string_p (const char *target
)
3283 for (p
= target
; *p
; p
++)
3284 if ((char) gfc_next_ascii_char () != *p
)
3289 /* Matches an attribute specification including array specs. If
3290 successful, leaves the variables current_attr and current_as
3291 holding the specification. Also sets the colon_seen variable for
3292 later use by matchers associated with initializations.
3294 This subroutine is a little tricky in the sense that we don't know
3295 if we really have an attr-spec until we hit the double colon.
3296 Until that time, we can only return MATCH_NO. This forces us to
3297 check for duplicate specification at this level. */
3300 match_attr_spec (void)
3302 /* Modifiers that can exist in a type statement. */
3304 { GFC_DECL_BEGIN
= 0,
3305 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3306 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3307 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3308 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3309 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3310 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3313 /* GFC_DECL_END is the sentinel, index starts at 0. */
3314 #define NUM_DECL GFC_DECL_END
3316 locus start
, seen_at
[NUM_DECL
];
3323 gfc_clear_attr (¤t_attr
);
3324 start
= gfc_current_locus
;
3329 /* See if we get all of the keywords up to the final double colon. */
3330 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3338 gfc_gobble_whitespace ();
3340 ch
= gfc_next_ascii_char ();
3343 /* This is the successful exit condition for the loop. */
3344 if (gfc_next_ascii_char () == ':')
3349 gfc_gobble_whitespace ();
3350 switch (gfc_peek_ascii_char ())
3353 gfc_next_ascii_char ();
3354 switch (gfc_next_ascii_char ())
3357 if (match_string_p ("locatable"))
3359 /* Matched "allocatable". */
3360 d
= DECL_ALLOCATABLE
;
3365 if (match_string_p ("ynchronous"))
3367 /* Matched "asynchronous". */
3368 d
= DECL_ASYNCHRONOUS
;
3375 /* Try and match the bind(c). */
3376 m
= gfc_match_bind_c (NULL
, true);
3379 else if (m
== MATCH_ERROR
)
3384 gfc_next_ascii_char ();
3385 if ('o' != gfc_next_ascii_char ())
3387 switch (gfc_next_ascii_char ())
3390 if (match_string_p ("imension"))
3392 d
= DECL_CODIMENSION
;
3396 if (match_string_p ("tiguous"))
3398 d
= DECL_CONTIGUOUS
;
3405 if (match_string_p ("dimension"))
3410 if (match_string_p ("external"))
3415 if (match_string_p ("int"))
3417 ch
= gfc_next_ascii_char ();
3420 if (match_string_p ("nt"))
3422 /* Matched "intent". */
3423 /* TODO: Call match_intent_spec from here. */
3424 if (gfc_match (" ( in out )") == MATCH_YES
)
3426 else if (gfc_match (" ( in )") == MATCH_YES
)
3428 else if (gfc_match (" ( out )") == MATCH_YES
)
3434 if (match_string_p ("insic"))
3436 /* Matched "intrinsic". */
3444 if (match_string_p ("optional"))
3449 gfc_next_ascii_char ();
3450 switch (gfc_next_ascii_char ())
3453 if (match_string_p ("rameter"))
3455 /* Matched "parameter". */
3461 if (match_string_p ("inter"))
3463 /* Matched "pointer". */
3469 ch
= gfc_next_ascii_char ();
3472 if (match_string_p ("vate"))
3474 /* Matched "private". */
3480 if (match_string_p ("tected"))
3482 /* Matched "protected". */
3489 if (match_string_p ("blic"))
3491 /* Matched "public". */
3499 if (match_string_p ("save"))
3504 if (match_string_p ("target"))
3509 gfc_next_ascii_char ();
3510 ch
= gfc_next_ascii_char ();
3513 if (match_string_p ("lue"))
3515 /* Matched "value". */
3521 if (match_string_p ("latile"))
3523 /* Matched "volatile". */
3531 /* No double colon and no recognizable decl_type, so assume that
3532 we've been looking at something else the whole time. */
3539 /* Check to make sure any parens are paired up correctly. */
3540 if (gfc_match_parens () == MATCH_ERROR
)
3547 seen_at
[d
] = gfc_current_locus
;
3549 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3551 gfc_array_spec
*as
= NULL
;
3553 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3554 d
== DECL_CODIMENSION
);
3556 if (current_as
== NULL
)
3558 else if (m
== MATCH_YES
)
3560 if (!merge_array_spec (as
, current_as
, false))
3567 if (d
== DECL_CODIMENSION
)
3568 gfc_error ("Missing codimension specification at %C");
3570 gfc_error ("Missing dimension specification at %C");
3574 if (m
== MATCH_ERROR
)
3579 /* Since we've seen a double colon, we have to be looking at an
3580 attr-spec. This means that we can now issue errors. */
3581 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3586 case DECL_ALLOCATABLE
:
3587 attr
= "ALLOCATABLE";
3589 case DECL_ASYNCHRONOUS
:
3590 attr
= "ASYNCHRONOUS";
3592 case DECL_CODIMENSION
:
3593 attr
= "CODIMENSION";
3595 case DECL_CONTIGUOUS
:
3596 attr
= "CONTIGUOUS";
3598 case DECL_DIMENSION
:
3605 attr
= "INTENT (IN)";
3608 attr
= "INTENT (OUT)";
3611 attr
= "INTENT (IN OUT)";
3613 case DECL_INTRINSIC
:
3619 case DECL_PARAMETER
:
3625 case DECL_PROTECTED
:
3640 case DECL_IS_BIND_C
:
3650 attr
= NULL
; /* This shouldn't happen. */
3653 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3658 /* Now that we've dealt with duplicate attributes, add the attributes
3659 to the current attribute. */
3660 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3665 if (gfc_current_state () == COMP_DERIVED
3666 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3667 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3668 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3670 if (d
== DECL_ALLOCATABLE
)
3672 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3673 "attribute at %C in a TYPE definition"))
3681 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3688 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3689 && gfc_current_state () != COMP_MODULE
)
3691 if (d
== DECL_PRIVATE
)
3695 if (gfc_current_state () == COMP_DERIVED
3696 && gfc_state_stack
->previous
3697 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3699 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3700 "at %L in a TYPE definition", attr
,
3709 gfc_error ("%s attribute at %L is not allowed outside of the "
3710 "specification part of a module", attr
, &seen_at
[d
]);
3718 case DECL_ALLOCATABLE
:
3719 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3722 case DECL_ASYNCHRONOUS
:
3723 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3726 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3729 case DECL_CODIMENSION
:
3730 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3733 case DECL_CONTIGUOUS
:
3734 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3737 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3740 case DECL_DIMENSION
:
3741 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3745 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3749 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3753 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3757 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3760 case DECL_INTRINSIC
:
3761 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3765 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3768 case DECL_PARAMETER
:
3769 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3773 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3776 case DECL_PROTECTED
:
3777 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3779 gfc_error ("PROTECTED at %C only allowed in specification "
3780 "part of a module");
3785 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3788 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3792 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3797 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3802 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3806 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3809 case DECL_IS_BIND_C
:
3810 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3814 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3817 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3821 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3824 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3828 gfc_internal_error ("match_attr_spec(): Bad attribute");
3838 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3839 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3840 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3841 current_attr
.save
= SAVE_IMPLICIT
;
3847 gfc_current_locus
= start
;
3848 gfc_free_array_spec (current_as
);
3854 /* Set the binding label, dest_label, either with the binding label
3855 stored in the given gfc_typespec, ts, or if none was provided, it
3856 will be the symbol name in all lower case, as required by the draft
3857 (J3/04-007, section 15.4.1). If a binding label was given and
3858 there is more than one argument (num_idents), it is an error. */
3861 set_binding_label (const char **dest_label
, const char *sym_name
,
3864 if (num_idents
> 1 && has_name_equals
)
3866 gfc_error ("Multiple identifiers provided with "
3867 "single NAME= specifier at %C");
3871 if (curr_binding_label
)
3872 /* Binding label given; store in temp holder till have sym. */
3873 *dest_label
= curr_binding_label
;
3876 /* No binding label given, and the NAME= specifier did not exist,
3877 which means there was no NAME="". */
3878 if (sym_name
!= NULL
&& has_name_equals
== 0)
3879 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3886 /* Set the status of the given common block as being BIND(C) or not,
3887 depending on the given parameter, is_bind_c. */
3890 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3892 com_block
->is_bind_c
= is_bind_c
;
3897 /* Verify that the given gfc_typespec is for a C interoperable type. */
3900 gfc_verify_c_interop (gfc_typespec
*ts
)
3902 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3903 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3905 else if (ts
->type
== BT_CLASS
)
3907 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
3914 /* Verify that the variables of a given common block, which has been
3915 defined with the attribute specifier bind(c), to be of a C
3916 interoperable type. Errors will be reported here, if
3920 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3922 gfc_symbol
*curr_sym
= NULL
;
3925 curr_sym
= com_block
->head
;
3927 /* Make sure we have at least one symbol. */
3928 if (curr_sym
== NULL
)
3931 /* Here we know we have a symbol, so we'll execute this loop
3935 /* The second to last param, 1, says this is in a common block. */
3936 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3937 curr_sym
= curr_sym
->common_next
;
3938 } while (curr_sym
!= NULL
);
3944 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3945 an appropriate error message is reported. */
3948 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3949 int is_in_common
, gfc_common_head
*com_block
)
3951 bool bind_c_function
= false;
3954 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3955 bind_c_function
= true;
3957 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3959 tmp_sym
= tmp_sym
->result
;
3960 /* Make sure it wasn't an implicitly typed result. */
3961 if (tmp_sym
->attr
.implicit_type
&& gfc_option
.warn_c_binding_type
)
3963 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3964 "%L may not be C interoperable", tmp_sym
->name
,
3965 &tmp_sym
->declared_at
);
3966 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3967 /* Mark it as C interoperable to prevent duplicate warnings. */
3968 tmp_sym
->ts
.is_c_interop
= 1;
3969 tmp_sym
->attr
.is_c_interop
= 1;
3973 /* Here, we know we have the bind(c) attribute, so if we have
3974 enough type info, then verify that it's a C interop kind.
3975 The info could be in the symbol already, or possibly still in
3976 the given ts (current_ts), so look in both. */
3977 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3979 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
3981 /* See if we're dealing with a sym in a common block or not. */
3982 if (is_in_common
== 1 && gfc_option
.warn_c_binding_type
)
3984 gfc_warning ("Variable '%s' in common block '%s' at %L "
3985 "may not be a C interoperable "
3986 "kind though common block '%s' is BIND(C)",
3987 tmp_sym
->name
, com_block
->name
,
3988 &(tmp_sym
->declared_at
), com_block
->name
);
3992 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3993 gfc_error ("Type declaration '%s' at %L is not C "
3994 "interoperable but it is BIND(C)",
3995 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3996 else if (gfc_option
.warn_c_binding_type
)
3997 gfc_warning ("Variable '%s' at %L "
3998 "may not be a C interoperable "
3999 "kind but it is bind(c)",
4000 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4004 /* Variables declared w/in a common block can't be bind(c)
4005 since there's no way for C to see these variables, so there's
4006 semantically no reason for the attribute. */
4007 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4009 gfc_error ("Variable '%s' in common block '%s' at "
4010 "%L cannot be declared with BIND(C) "
4011 "since it is not a global",
4012 tmp_sym
->name
, com_block
->name
,
4013 &(tmp_sym
->declared_at
));
4017 /* Scalar variables that are bind(c) can not have the pointer
4018 or allocatable attributes. */
4019 if (tmp_sym
->attr
.is_bind_c
== 1)
4021 if (tmp_sym
->attr
.pointer
== 1)
4023 gfc_error ("Variable '%s' at %L cannot have both the "
4024 "POINTER and BIND(C) attributes",
4025 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4029 if (tmp_sym
->attr
.allocatable
== 1)
4031 gfc_error ("Variable '%s' at %L cannot have both the "
4032 "ALLOCATABLE and BIND(C) attributes",
4033 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4039 /* If it is a BIND(C) function, make sure the return value is a
4040 scalar value. The previous tests in this function made sure
4041 the type is interoperable. */
4042 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4043 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4044 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4046 /* BIND(C) functions can not return a character string. */
4047 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4048 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4049 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4050 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4051 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4052 "be a character string", tmp_sym
->name
,
4053 &(tmp_sym
->declared_at
));
4056 /* See if the symbol has been marked as private. If it has, make sure
4057 there is no binding label and warn the user if there is one. */
4058 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4059 && tmp_sym
->binding_label
)
4060 /* Use gfc_warning_now because we won't say that the symbol fails
4061 just because of this. */
4062 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4063 "given the binding label '%s'", tmp_sym
->name
,
4064 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4070 /* Set the appropriate fields for a symbol that's been declared as
4071 BIND(C) (the is_bind_c flag and the binding label), and verify that
4072 the type is C interoperable. Errors are reported by the functions
4073 used to set/test these fields. */
4076 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4080 /* TODO: Do we need to make sure the vars aren't marked private? */
4082 /* Set the is_bind_c bit in symbol_attribute. */
4083 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4085 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4092 /* Set the fields marking the given common block as BIND(C), including
4093 a binding label, and report any errors encountered. */
4096 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4100 /* destLabel, common name, typespec (which may have binding label). */
4101 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4105 /* Set the given common block (com_block) to being bind(c) (1). */
4106 set_com_block_bind_c (com_block
, 1);
4112 /* Retrieve the list of one or more identifiers that the given bind(c)
4113 attribute applies to. */
4116 get_bind_c_idents (void)
4118 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4120 gfc_symbol
*tmp_sym
= NULL
;
4122 gfc_common_head
*com_block
= NULL
;
4124 if (gfc_match_name (name
) == MATCH_YES
)
4126 found_id
= MATCH_YES
;
4127 gfc_get_ha_symbol (name
, &tmp_sym
);
4129 else if (match_common_name (name
) == MATCH_YES
)
4131 found_id
= MATCH_YES
;
4132 com_block
= gfc_get_common (name
, 0);
4136 gfc_error ("Need either entity or common block name for "
4137 "attribute specification statement at %C");
4141 /* Save the current identifier and look for more. */
4144 /* Increment the number of identifiers found for this spec stmt. */
4147 /* Make sure we have a sym or com block, and verify that it can
4148 be bind(c). Set the appropriate field(s) and look for more
4150 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4152 if (tmp_sym
!= NULL
)
4154 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4159 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4163 /* Look to see if we have another identifier. */
4165 if (gfc_match_eos () == MATCH_YES
)
4166 found_id
= MATCH_NO
;
4167 else if (gfc_match_char (',') != MATCH_YES
)
4168 found_id
= MATCH_NO
;
4169 else if (gfc_match_name (name
) == MATCH_YES
)
4171 found_id
= MATCH_YES
;
4172 gfc_get_ha_symbol (name
, &tmp_sym
);
4174 else if (match_common_name (name
) == MATCH_YES
)
4176 found_id
= MATCH_YES
;
4177 com_block
= gfc_get_common (name
, 0);
4181 gfc_error ("Missing entity or common block name for "
4182 "attribute specification statement at %C");
4188 gfc_internal_error ("Missing symbol");
4190 } while (found_id
== MATCH_YES
);
4192 /* if we get here we were successful */
4197 /* Try and match a BIND(C) attribute specification statement. */
4200 gfc_match_bind_c_stmt (void)
4202 match found_match
= MATCH_NO
;
4207 /* This may not be necessary. */
4209 /* Clear the temporary binding label holder. */
4210 curr_binding_label
= NULL
;
4212 /* Look for the bind(c). */
4213 found_match
= gfc_match_bind_c (NULL
, true);
4215 if (found_match
== MATCH_YES
)
4217 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4220 /* Look for the :: now, but it is not required. */
4223 /* Get the identifier(s) that needs to be updated. This may need to
4224 change to hand the flag(s) for the attr specified so all identifiers
4225 found can have all appropriate parts updated (assuming that the same
4226 spec stmt can have multiple attrs, such as both bind(c) and
4228 if (!get_bind_c_idents ())
4229 /* Error message should have printed already. */
4237 /* Match a data declaration statement. */
4240 gfc_match_data_decl (void)
4246 num_idents_on_line
= 0;
4248 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4252 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4253 && gfc_current_state () != COMP_DERIVED
)
4255 sym
= gfc_use_derived (current_ts
.u
.derived
);
4263 current_ts
.u
.derived
= sym
;
4266 m
= match_attr_spec ();
4267 if (m
== MATCH_ERROR
)
4273 if (current_ts
.type
== BT_CLASS
4274 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4277 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4278 && current_ts
.u
.derived
->components
== NULL
4279 && !current_ts
.u
.derived
->attr
.zero_comp
)
4282 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4285 gfc_find_symbol (current_ts
.u
.derived
->name
,
4286 current_ts
.u
.derived
->ns
, 1, &sym
);
4288 /* Any symbol that we find had better be a type definition
4289 which has its components defined. */
4290 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4291 && (current_ts
.u
.derived
->components
!= NULL
4292 || current_ts
.u
.derived
->attr
.zero_comp
))
4295 gfc_error ("Derived type at %C has not been previously defined "
4296 "and so cannot appear in a derived type definition");
4302 /* If we have an old-style character declaration, and no new-style
4303 attribute specifications, then there a comma is optional between
4304 the type specification and the variable list. */
4305 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4306 gfc_match_char (',');
4308 /* Give the types/attributes to symbols that follow. Give the element
4309 a number so that repeat character length expressions can be copied. */
4313 num_idents_on_line
++;
4314 m
= variable_decl (elem
++);
4315 if (m
== MATCH_ERROR
)
4320 if (gfc_match_eos () == MATCH_YES
)
4322 if (gfc_match_char (',') != MATCH_YES
)
4326 if (gfc_error_flag_test () == 0)
4327 gfc_error ("Syntax error in data declaration at %C");
4330 gfc_free_data_all (gfc_current_ns
);
4333 gfc_free_array_spec (current_as
);
4339 /* Match a prefix associated with a function or subroutine
4340 declaration. If the typespec pointer is nonnull, then a typespec
4341 can be matched. Note that if nothing matches, MATCH_YES is
4342 returned (the null string was matched). */
4345 gfc_match_prefix (gfc_typespec
*ts
)
4351 gfc_clear_attr (¤t_attr
);
4353 seen_impure
= false;
4355 gcc_assert (!gfc_matching_prefix
);
4356 gfc_matching_prefix
= true;
4360 found_prefix
= false;
4362 if (!seen_type
&& ts
!= NULL
4363 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4364 && gfc_match_space () == MATCH_YES
)
4368 found_prefix
= true;
4371 if (gfc_match ("elemental% ") == MATCH_YES
)
4373 if (!gfc_add_elemental (¤t_attr
, NULL
))
4376 found_prefix
= true;
4379 if (gfc_match ("pure% ") == MATCH_YES
)
4381 if (!gfc_add_pure (¤t_attr
, NULL
))
4384 found_prefix
= true;
4387 if (gfc_match ("recursive% ") == MATCH_YES
)
4389 if (!gfc_add_recursive (¤t_attr
, NULL
))
4392 found_prefix
= true;
4395 /* IMPURE is a somewhat special case, as it needs not set an actual
4396 attribute but rather only prevents ELEMENTAL routines from being
4397 automatically PURE. */
4398 if (gfc_match ("impure% ") == MATCH_YES
)
4400 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4404 found_prefix
= true;
4407 while (found_prefix
);
4409 /* IMPURE and PURE must not both appear, of course. */
4410 if (seen_impure
&& current_attr
.pure
)
4412 gfc_error ("PURE and IMPURE must not appear both at %C");
4416 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4417 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4419 if (!gfc_add_pure (¤t_attr
, NULL
))
4423 /* At this point, the next item is not a prefix. */
4424 gcc_assert (gfc_matching_prefix
);
4425 gfc_matching_prefix
= false;
4429 gcc_assert (gfc_matching_prefix
);
4430 gfc_matching_prefix
= false;
4435 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4438 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4440 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4443 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4446 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4453 /* Match a formal argument list. */
4456 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4458 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4459 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4465 if (gfc_match_char ('(') != MATCH_YES
)
4472 if (gfc_match_char (')') == MATCH_YES
)
4477 if (gfc_match_char ('*') == MATCH_YES
)
4480 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4489 m
= gfc_match_name (name
);
4493 if (gfc_get_symbol (name
, NULL
, &sym
))
4497 p
= gfc_get_formal_arglist ();
4509 /* We don't add the VARIABLE flavor because the name could be a
4510 dummy procedure. We don't apply these attributes to formal
4511 arguments of statement functions. */
4512 if (sym
!= NULL
&& !st_flag
4513 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4514 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4520 /* The name of a program unit can be in a different namespace,
4521 so check for it explicitly. After the statement is accepted,
4522 the name is checked for especially in gfc_get_symbol(). */
4523 if (gfc_new_block
!= NULL
&& sym
!= NULL
4524 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4526 gfc_error ("Name '%s' at %C is the name of the procedure",
4532 if (gfc_match_char (')') == MATCH_YES
)
4535 m
= gfc_match_char (',');
4538 gfc_error ("Unexpected junk in formal argument list at %C");
4544 /* Check for duplicate symbols in the formal argument list. */
4547 for (p
= head
; p
->next
; p
= p
->next
)
4552 for (q
= p
->next
; q
; q
= q
->next
)
4553 if (p
->sym
== q
->sym
)
4555 gfc_error ("Duplicate symbol '%s' in formal argument list "
4556 "at %C", p
->sym
->name
);
4564 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4573 gfc_free_formal_arglist (head
);
4578 /* Match a RESULT specification following a function declaration or
4579 ENTRY statement. Also matches the end-of-statement. */
4582 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4584 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4588 if (gfc_match (" result (") != MATCH_YES
)
4591 m
= gfc_match_name (name
);
4595 /* Get the right paren, and that's it because there could be the
4596 bind(c) attribute after the result clause. */
4597 if (gfc_match_char (')') != MATCH_YES
)
4599 /* TODO: should report the missing right paren here. */
4603 if (strcmp (function
->name
, name
) == 0)
4605 gfc_error ("RESULT variable at %C must be different than function name");
4609 if (gfc_get_symbol (name
, NULL
, &r
))
4612 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4621 /* Match a function suffix, which could be a combination of a result
4622 clause and BIND(C), either one, or neither. The draft does not
4623 require them to come in a specific order. */
4626 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4628 match is_bind_c
; /* Found bind(c). */
4629 match is_result
; /* Found result clause. */
4630 match found_match
; /* Status of whether we've found a good match. */
4631 char peek_char
; /* Character we're going to peek at. */
4632 bool allow_binding_name
;
4634 /* Initialize to having found nothing. */
4635 found_match
= MATCH_NO
;
4636 is_bind_c
= MATCH_NO
;
4637 is_result
= MATCH_NO
;
4639 /* Get the next char to narrow between result and bind(c). */
4640 gfc_gobble_whitespace ();
4641 peek_char
= gfc_peek_ascii_char ();
4643 /* C binding names are not allowed for internal procedures. */
4644 if (gfc_current_state () == COMP_CONTAINS
4645 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4646 allow_binding_name
= false;
4648 allow_binding_name
= true;
4653 /* Look for result clause. */
4654 is_result
= match_result (sym
, result
);
4655 if (is_result
== MATCH_YES
)
4657 /* Now see if there is a bind(c) after it. */
4658 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4659 /* We've found the result clause and possibly bind(c). */
4660 found_match
= MATCH_YES
;
4663 /* This should only be MATCH_ERROR. */
4664 found_match
= is_result
;
4667 /* Look for bind(c) first. */
4668 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4669 if (is_bind_c
== MATCH_YES
)
4671 /* Now see if a result clause followed it. */
4672 is_result
= match_result (sym
, result
);
4673 found_match
= MATCH_YES
;
4677 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4678 found_match
= MATCH_ERROR
;
4682 gfc_error ("Unexpected junk after function declaration at %C");
4683 found_match
= MATCH_ERROR
;
4687 if (is_bind_c
== MATCH_YES
)
4689 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4690 if (gfc_current_state () == COMP_CONTAINS
4691 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4692 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4693 "at %L may not be specified for an internal "
4694 "procedure", &gfc_current_locus
))
4697 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4705 /* Procedure pointer return value without RESULT statement:
4706 Add "hidden" result variable named "ppr@". */
4709 add_hidden_procptr_result (gfc_symbol
*sym
)
4713 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4716 /* First usage case: PROCEDURE and EXTERNAL statements. */
4717 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4718 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4719 && sym
->attr
.external
;
4720 /* Second usage case: INTERFACE statements. */
4721 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4722 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4723 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4729 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4733 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4734 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4735 st2
->n
.sym
= stree
->n
.sym
;
4737 sym
->result
= stree
->n
.sym
;
4739 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4740 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4741 sym
->result
->attr
.external
= sym
->attr
.external
;
4742 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4743 sym
->result
->ts
= sym
->ts
;
4744 sym
->attr
.proc_pointer
= 0;
4745 sym
->attr
.pointer
= 0;
4746 sym
->attr
.external
= 0;
4747 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4749 sym
->result
->attr
.pointer
= 0;
4750 sym
->result
->attr
.proc_pointer
= 1;
4753 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4755 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4756 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4757 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4758 && sym
== gfc_current_ns
->proc_name
4759 && sym
== sym
->result
->ns
->proc_name
4760 && strcmp ("ppr@", sym
->result
->name
) == 0)
4762 sym
->result
->attr
.proc_pointer
= 1;
4763 sym
->attr
.pointer
= 0;
4771 /* Match the interface for a PROCEDURE declaration,
4772 including brackets (R1212). */
4775 match_procedure_interface (gfc_symbol
**proc_if
)
4779 locus old_loc
, entry_loc
;
4780 gfc_namespace
*old_ns
= gfc_current_ns
;
4781 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4783 old_loc
= entry_loc
= gfc_current_locus
;
4784 gfc_clear_ts (¤t_ts
);
4786 if (gfc_match (" (") != MATCH_YES
)
4788 gfc_current_locus
= entry_loc
;
4792 /* Get the type spec. for the procedure interface. */
4793 old_loc
= gfc_current_locus
;
4794 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4795 gfc_gobble_whitespace ();
4796 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4799 if (m
== MATCH_ERROR
)
4802 /* Procedure interface is itself a procedure. */
4803 gfc_current_locus
= old_loc
;
4804 m
= gfc_match_name (name
);
4806 /* First look to see if it is already accessible in the current
4807 namespace because it is use associated or contained. */
4809 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4812 /* If it is still not found, then try the parent namespace, if it
4813 exists and create the symbol there if it is still not found. */
4814 if (gfc_current_ns
->parent
)
4815 gfc_current_ns
= gfc_current_ns
->parent
;
4816 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4819 gfc_current_ns
= old_ns
;
4820 *proc_if
= st
->n
.sym
;
4825 /* Resolve interface if possible. That way, attr.procedure is only set
4826 if it is declared by a later procedure-declaration-stmt, which is
4827 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4828 while ((*proc_if
)->ts
.interface
)
4829 *proc_if
= (*proc_if
)->ts
.interface
;
4831 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4832 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4833 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4834 (*proc_if
)->name
, NULL
))
4839 if (gfc_match (" )") != MATCH_YES
)
4841 gfc_current_locus
= entry_loc
;
4849 /* Match a PROCEDURE declaration (R1211). */
4852 match_procedure_decl (void)
4855 gfc_symbol
*sym
, *proc_if
= NULL
;
4857 gfc_expr
*initializer
= NULL
;
4859 /* Parse interface (with brackets). */
4860 m
= match_procedure_interface (&proc_if
);
4864 /* Parse attributes (with colons). */
4865 m
= match_attr_spec();
4866 if (m
== MATCH_ERROR
)
4869 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4871 current_attr
.is_bind_c
= 1;
4872 has_name_equals
= 0;
4873 curr_binding_label
= NULL
;
4876 /* Get procedure symbols. */
4879 m
= gfc_match_symbol (&sym
, 0);
4882 else if (m
== MATCH_ERROR
)
4885 /* Add current_attr to the symbol attributes. */
4886 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4889 if (sym
->attr
.is_bind_c
)
4891 /* Check for C1218. */
4892 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4894 gfc_error ("BIND(C) attribute at %C requires "
4895 "an interface with BIND(C)");
4898 /* Check for C1217. */
4899 if (has_name_equals
&& sym
->attr
.pointer
)
4901 gfc_error ("BIND(C) procedure with NAME may not have "
4902 "POINTER attribute at %C");
4905 if (has_name_equals
&& sym
->attr
.dummy
)
4907 gfc_error ("Dummy procedure at %C may not have "
4908 "BIND(C) attribute with NAME");
4911 /* Set binding label for BIND(C). */
4912 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
4916 if (!gfc_add_external (&sym
->attr
, NULL
))
4919 if (add_hidden_procptr_result (sym
))
4922 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
4925 /* Set interface. */
4926 if (proc_if
!= NULL
)
4928 if (sym
->ts
.type
!= BT_UNKNOWN
)
4930 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4931 sym
->name
, &gfc_current_locus
,
4932 gfc_basic_typename (sym
->ts
.type
));
4935 sym
->ts
.interface
= proc_if
;
4936 sym
->attr
.untyped
= 1;
4937 sym
->attr
.if_source
= IFSRC_IFBODY
;
4939 else if (current_ts
.type
!= BT_UNKNOWN
)
4941 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
4943 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4944 sym
->ts
.interface
->ts
= current_ts
;
4945 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4946 sym
->ts
.interface
->attr
.function
= 1;
4947 sym
->attr
.function
= 1;
4948 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4951 if (gfc_match (" =>") == MATCH_YES
)
4953 if (!current_attr
.pointer
)
4955 gfc_error ("Initialization at %C isn't for a pointer variable");
4960 m
= match_pointer_init (&initializer
, 1);
4964 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
4969 if (gfc_match_eos () == MATCH_YES
)
4971 if (gfc_match_char (',') != MATCH_YES
)
4976 gfc_error ("Syntax error in PROCEDURE statement at %C");
4980 /* Free stuff up and return. */
4981 gfc_free_expr (initializer
);
4987 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4990 /* Match a procedure pointer component declaration (R445). */
4993 match_ppc_decl (void)
4996 gfc_symbol
*proc_if
= NULL
;
5000 gfc_expr
*initializer
= NULL
;
5001 gfc_typebound_proc
* tb
;
5002 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5004 /* Parse interface (with brackets). */
5005 m
= match_procedure_interface (&proc_if
);
5009 /* Parse attributes. */
5010 tb
= XCNEW (gfc_typebound_proc
);
5011 tb
->where
= gfc_current_locus
;
5012 m
= match_binding_attributes (tb
, false, true);
5013 if (m
== MATCH_ERROR
)
5016 gfc_clear_attr (¤t_attr
);
5017 current_attr
.procedure
= 1;
5018 current_attr
.proc_pointer
= 1;
5019 current_attr
.access
= tb
->access
;
5020 current_attr
.flavor
= FL_PROCEDURE
;
5022 /* Match the colons (required). */
5023 if (gfc_match (" ::") != MATCH_YES
)
5025 gfc_error ("Expected '::' after binding-attributes at %C");
5029 /* Check for C450. */
5030 if (!tb
->nopass
&& proc_if
== NULL
)
5032 gfc_error("NOPASS or explicit interface required at %C");
5036 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5039 /* Match PPC names. */
5043 m
= gfc_match_name (name
);
5046 else if (m
== MATCH_ERROR
)
5049 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5052 /* Add current_attr to the symbol attributes. */
5053 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5056 if (!gfc_add_external (&c
->attr
, NULL
))
5059 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5066 c
->tb
= XCNEW (gfc_typebound_proc
);
5067 c
->tb
->where
= gfc_current_locus
;
5071 /* Set interface. */
5072 if (proc_if
!= NULL
)
5074 c
->ts
.interface
= proc_if
;
5075 c
->attr
.untyped
= 1;
5076 c
->attr
.if_source
= IFSRC_IFBODY
;
5078 else if (ts
.type
!= BT_UNKNOWN
)
5081 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5082 c
->ts
.interface
->result
= c
->ts
.interface
;
5083 c
->ts
.interface
->ts
= ts
;
5084 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5085 c
->ts
.interface
->attr
.function
= 1;
5086 c
->attr
.function
= 1;
5087 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5090 if (gfc_match (" =>") == MATCH_YES
)
5092 m
= match_pointer_init (&initializer
, 1);
5095 gfc_free_expr (initializer
);
5098 c
->initializer
= initializer
;
5101 if (gfc_match_eos () == MATCH_YES
)
5103 if (gfc_match_char (',') != MATCH_YES
)
5108 gfc_error ("Syntax error in procedure pointer component at %C");
5113 /* Match a PROCEDURE declaration inside an interface (R1206). */
5116 match_procedure_in_interface (void)
5120 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5123 if (current_interface
.type
== INTERFACE_NAMELESS
5124 || current_interface
.type
== INTERFACE_ABSTRACT
)
5126 gfc_error ("PROCEDURE at %C must be in a generic interface");
5130 /* Check if the F2008 optional double colon appears. */
5131 gfc_gobble_whitespace ();
5132 old_locus
= gfc_current_locus
;
5133 if (gfc_match ("::") == MATCH_YES
)
5135 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5136 "MODULE PROCEDURE statement at %L", &old_locus
))
5140 gfc_current_locus
= old_locus
;
5144 m
= gfc_match_name (name
);
5147 else if (m
== MATCH_ERROR
)
5149 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5152 if (!gfc_add_interface (sym
))
5155 if (gfc_match_eos () == MATCH_YES
)
5157 if (gfc_match_char (',') != MATCH_YES
)
5164 gfc_error ("Syntax error in PROCEDURE statement at %C");
5169 /* General matcher for PROCEDURE declarations. */
5171 static match
match_procedure_in_type (void);
5174 gfc_match_procedure (void)
5178 switch (gfc_current_state ())
5183 case COMP_SUBROUTINE
:
5186 m
= match_procedure_decl ();
5188 case COMP_INTERFACE
:
5189 m
= match_procedure_in_interface ();
5192 m
= match_ppc_decl ();
5194 case COMP_DERIVED_CONTAINS
:
5195 m
= match_procedure_in_type ();
5204 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5211 /* Warn if a matched procedure has the same name as an intrinsic; this is
5212 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5213 parser-state-stack to find out whether we're in a module. */
5216 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5220 in_module
= (gfc_state_stack
->previous
5221 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5223 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5227 /* Match a function declaration. */
5230 gfc_match_function_decl (void)
5232 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5233 gfc_symbol
*sym
, *result
;
5237 match found_match
; /* Status returned by match func. */
5239 if (gfc_current_state () != COMP_NONE
5240 && gfc_current_state () != COMP_INTERFACE
5241 && gfc_current_state () != COMP_CONTAINS
)
5244 gfc_clear_ts (¤t_ts
);
5246 old_loc
= gfc_current_locus
;
5248 m
= gfc_match_prefix (¤t_ts
);
5251 gfc_current_locus
= old_loc
;
5255 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5257 gfc_current_locus
= old_loc
;
5260 if (get_proc_name (name
, &sym
, false))
5263 if (add_hidden_procptr_result (sym
))
5266 gfc_new_block
= sym
;
5268 m
= gfc_match_formal_arglist (sym
, 0, 0);
5271 gfc_error ("Expected formal argument list in function "
5272 "definition at %C");
5276 else if (m
== MATCH_ERROR
)
5281 /* According to the draft, the bind(c) and result clause can
5282 come in either order after the formal_arg_list (i.e., either
5283 can be first, both can exist together or by themselves or neither
5284 one). Therefore, the match_result can't match the end of the
5285 string, and check for the bind(c) or result clause in either order. */
5286 found_match
= gfc_match_eos ();
5288 /* Make sure that it isn't already declared as BIND(C). If it is, it
5289 must have been marked BIND(C) with a BIND(C) attribute and that is
5290 not allowed for procedures. */
5291 if (sym
->attr
.is_bind_c
== 1)
5293 sym
->attr
.is_bind_c
= 0;
5294 if (sym
->old_symbol
!= NULL
)
5295 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5296 "variables or common blocks",
5297 &(sym
->old_symbol
->declared_at
));
5299 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5300 "variables or common blocks", &gfc_current_locus
);
5303 if (found_match
!= MATCH_YES
)
5305 /* If we haven't found the end-of-statement, look for a suffix. */
5306 suffix_match
= gfc_match_suffix (sym
, &result
);
5307 if (suffix_match
== MATCH_YES
)
5308 /* Need to get the eos now. */
5309 found_match
= gfc_match_eos ();
5311 found_match
= suffix_match
;
5314 if(found_match
!= MATCH_YES
)
5318 /* Make changes to the symbol. */
5321 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5324 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5325 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5328 /* Delay matching the function characteristics until after the
5329 specification block by signalling kind=-1. */
5330 sym
->declared_at
= old_loc
;
5331 if (current_ts
.type
!= BT_UNKNOWN
)
5332 current_ts
.kind
= -1;
5334 current_ts
.kind
= 0;
5338 if (current_ts
.type
!= BT_UNKNOWN
5339 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5345 if (current_ts
.type
!= BT_UNKNOWN
5346 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5348 sym
->result
= result
;
5351 /* Warn if this procedure has the same name as an intrinsic. */
5352 warn_intrinsic_shadow (sym
, true);
5358 gfc_current_locus
= old_loc
;
5363 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5364 pass the name of the entry, rather than the gfc_current_block name, and
5365 to return false upon finding an existing global entry. */
5368 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5372 enum gfc_symbol_type type
;
5374 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5376 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5377 name is a global identifier. */
5378 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5380 s
= gfc_get_gsymbol (name
);
5382 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5384 gfc_global_used (s
, where
);
5393 s
->ns
= gfc_current_ns
;
5397 /* Don't add the symbol multiple times. */
5399 && (!gfc_notification_std (GFC_STD_F2008
)
5400 || strcmp (name
, binding_label
) != 0))
5402 s
= gfc_get_gsymbol (binding_label
);
5404 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5406 gfc_global_used (s
, where
);
5413 s
->binding_label
= binding_label
;
5416 s
->ns
= gfc_current_ns
;
5424 /* Match an ENTRY statement. */
5427 gfc_match_entry (void)
5432 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5433 gfc_compile_state state
;
5437 bool module_procedure
;
5441 m
= gfc_match_name (name
);
5445 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5448 state
= gfc_current_state ();
5449 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5454 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5457 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5459 case COMP_BLOCK_DATA
:
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5463 case COMP_INTERFACE
:
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5468 gfc_error ("ENTRY statement at %C cannot appear within "
5469 "a DERIVED TYPE block");
5472 gfc_error ("ENTRY statement at %C cannot appear within "
5473 "an IF-THEN block");
5476 case COMP_DO_CONCURRENT
:
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 "
5489 gfc_error ("ENTRY statement at %C cannot appear within "
5493 gfc_error ("ENTRY statement at %C cannot appear within "
5494 "a contained subprogram");
5497 gfc_internal_error ("gfc_match_entry(): Bad state");
5502 module_procedure
= gfc_current_ns
->parent
!= NULL
5503 && gfc_current_ns
->parent
->proc_name
5504 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5507 if (gfc_current_ns
->parent
!= NULL
5508 && gfc_current_ns
->parent
->proc_name
5509 && !module_procedure
)
5511 gfc_error("ENTRY statement at %C cannot appear in a "
5512 "contained procedure");
5516 /* Module function entries need special care in get_proc_name
5517 because previous references within the function will have
5518 created symbols attached to the current namespace. */
5519 if (get_proc_name (name
, &entry
,
5520 gfc_current_ns
->parent
!= NULL
5521 && module_procedure
))
5524 proc
= gfc_current_block ();
5526 /* Make sure that it isn't already declared as BIND(C). If it is, it
5527 must have been marked BIND(C) with a BIND(C) attribute and that is
5528 not allowed for procedures. */
5529 if (entry
->attr
.is_bind_c
== 1)
5531 entry
->attr
.is_bind_c
= 0;
5532 if (entry
->old_symbol
!= NULL
)
5533 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5534 "variables or common blocks",
5535 &(entry
->old_symbol
->declared_at
));
5537 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5538 "variables or common blocks", &gfc_current_locus
);
5541 /* Check what next non-whitespace character is so we can tell if there
5542 is the required parens if we have a BIND(C). */
5543 old_loc
= gfc_current_locus
;
5544 gfc_gobble_whitespace ();
5545 peek_char
= gfc_peek_ascii_char ();
5547 if (state
== COMP_SUBROUTINE
)
5549 m
= gfc_match_formal_arglist (entry
, 0, 1);
5553 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5554 never be an internal procedure. */
5555 is_bind_c
= gfc_match_bind_c (entry
, true);
5556 if (is_bind_c
== MATCH_ERROR
)
5558 if (is_bind_c
== MATCH_YES
)
5560 if (peek_char
!= '(')
5562 gfc_error ("Missing required parentheses before BIND(C) at %C");
5565 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5566 &(entry
->declared_at
), 1))
5570 if (!gfc_current_ns
->parent
5571 && !add_global_entry (name
, entry
->binding_label
, true,
5575 /* An entry in a subroutine. */
5576 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5577 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5582 /* An entry in a function.
5583 We need to take special care because writing
5588 ENTRY f() RESULT (r)
5590 ENTRY f RESULT (r). */
5591 if (gfc_match_eos () == MATCH_YES
)
5593 gfc_current_locus
= old_loc
;
5594 /* Match the empty argument list, and add the interface to
5596 m
= gfc_match_formal_arglist (entry
, 0, 1);
5599 m
= gfc_match_formal_arglist (entry
, 0, 0);
5606 if (gfc_match_eos () == MATCH_YES
)
5608 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5609 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5612 entry
->result
= entry
;
5616 m
= gfc_match_suffix (entry
, &result
);
5618 gfc_syntax_error (ST_ENTRY
);
5624 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5625 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5626 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5628 entry
->result
= result
;
5632 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5633 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5635 entry
->result
= entry
;
5639 if (!gfc_current_ns
->parent
5640 && !add_global_entry (name
, entry
->binding_label
, false,
5645 if (gfc_match_eos () != MATCH_YES
)
5647 gfc_syntax_error (ST_ENTRY
);
5651 entry
->attr
.recursive
= proc
->attr
.recursive
;
5652 entry
->attr
.elemental
= proc
->attr
.elemental
;
5653 entry
->attr
.pure
= proc
->attr
.pure
;
5655 el
= gfc_get_entry_list ();
5657 el
->next
= gfc_current_ns
->entries
;
5658 gfc_current_ns
->entries
= el
;
5660 el
->id
= el
->next
->id
+ 1;
5664 new_st
.op
= EXEC_ENTRY
;
5665 new_st
.ext
.entry
= el
;
5671 /* Match a subroutine statement, including optional prefixes. */
5674 gfc_match_subroutine (void)
5676 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5681 bool allow_binding_name
;
5683 if (gfc_current_state () != COMP_NONE
5684 && gfc_current_state () != COMP_INTERFACE
5685 && gfc_current_state () != COMP_CONTAINS
)
5688 m
= gfc_match_prefix (NULL
);
5692 m
= gfc_match ("subroutine% %n", name
);
5696 if (get_proc_name (name
, &sym
, false))
5699 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5700 the symbol existed before. */
5701 sym
->declared_at
= gfc_current_locus
;
5703 if (add_hidden_procptr_result (sym
))
5706 gfc_new_block
= sym
;
5708 /* Check what next non-whitespace character is so we can tell if there
5709 is the required parens if we have a BIND(C). */
5710 gfc_gobble_whitespace ();
5711 peek_char
= gfc_peek_ascii_char ();
5713 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5716 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5719 /* Make sure that it isn't already declared as BIND(C). If it is, it
5720 must have been marked BIND(C) with a BIND(C) attribute and that is
5721 not allowed for procedures. */
5722 if (sym
->attr
.is_bind_c
== 1)
5724 sym
->attr
.is_bind_c
= 0;
5725 if (sym
->old_symbol
!= NULL
)
5726 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5727 "variables or common blocks",
5728 &(sym
->old_symbol
->declared_at
));
5730 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5731 "variables or common blocks", &gfc_current_locus
);
5734 /* C binding names are not allowed for internal procedures. */
5735 if (gfc_current_state () == COMP_CONTAINS
5736 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5737 allow_binding_name
= false;
5739 allow_binding_name
= true;
5741 /* Here, we are just checking if it has the bind(c) attribute, and if
5742 so, then we need to make sure it's all correct. If it doesn't,
5743 we still need to continue matching the rest of the subroutine line. */
5744 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5745 if (is_bind_c
== MATCH_ERROR
)
5747 /* There was an attempt at the bind(c), but it was wrong. An
5748 error message should have been printed w/in the gfc_match_bind_c
5749 so here we'll just return the MATCH_ERROR. */
5753 if (is_bind_c
== MATCH_YES
)
5755 /* The following is allowed in the Fortran 2008 draft. */
5756 if (gfc_current_state () == COMP_CONTAINS
5757 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5758 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5759 "at %L may not be specified for an internal "
5760 "procedure", &gfc_current_locus
))
5763 if (peek_char
!= '(')
5765 gfc_error ("Missing required parentheses before BIND(C) at %C");
5768 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5769 &(sym
->declared_at
), 1))
5773 if (gfc_match_eos () != MATCH_YES
)
5775 gfc_syntax_error (ST_SUBROUTINE
);
5779 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5782 /* Warn if it has the same name as an intrinsic. */
5783 warn_intrinsic_shadow (sym
, false);
5789 /* Check that the NAME identifier in a BIND attribute or statement
5790 is conform to C identifier rules. */
5793 check_bind_name_identifier (char **name
)
5795 char *n
= *name
, *p
;
5797 /* Remove leading spaces. */
5801 /* On an empty string, free memory and set name to NULL. */
5809 /* Remove trailing spaces. */
5810 p
= n
+ strlen(n
) - 1;
5814 /* Insert the identifier into the symbol table. */
5819 /* Now check that identifier is valid under C rules. */
5822 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5827 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
5829 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5837 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5838 given, and set the binding label in either the given symbol (if not
5839 NULL), or in the current_ts. The symbol may be NULL because we may
5840 encounter the BIND(C) before the declaration itself. Return
5841 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5842 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5843 or MATCH_YES if the specifier was correct and the binding label and
5844 bind(c) fields were set correctly for the given symbol or the
5845 current_ts. If allow_binding_name is false, no binding name may be
5849 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5851 char *binding_label
= NULL
;
5854 /* Initialize the flag that specifies whether we encountered a NAME=
5855 specifier or not. */
5856 has_name_equals
= 0;
5858 /* This much we have to be able to match, in this order, if
5859 there is a bind(c) label. */
5860 if (gfc_match (" bind ( c ") != MATCH_YES
)
5863 /* Now see if there is a binding label, or if we've reached the
5864 end of the bind(c) attribute without one. */
5865 if (gfc_match_char (',') == MATCH_YES
)
5867 if (gfc_match (" name = ") != MATCH_YES
)
5869 gfc_error ("Syntax error in NAME= specifier for binding label "
5871 /* should give an error message here */
5875 has_name_equals
= 1;
5877 if (gfc_match_init_expr (&e
) != MATCH_YES
)
5883 if (!gfc_simplify_expr(e
, 0))
5885 gfc_error ("NAME= specifier at %C should be a constant expression");
5890 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
5891 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
5893 gfc_error ("NAME= specifier at %C should be a scalar of "
5894 "default character kind");
5899 // Get a C string from the Fortran string constant
5900 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
5901 e
->value
.character
.length
);
5904 // Check that it is valid (old gfc_match_name_C)
5905 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
5909 /* Get the required right paren. */
5910 if (gfc_match_char (')') != MATCH_YES
)
5912 gfc_error ("Missing closing paren for binding label at %C");
5916 if (has_name_equals
&& !allow_binding_name
)
5918 gfc_error ("No binding name is allowed in BIND(C) at %C");
5922 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5924 gfc_error ("For dummy procedure %s, no binding name is "
5925 "allowed in BIND(C) at %C", sym
->name
);
5930 /* Save the binding label to the symbol. If sym is null, we're
5931 probably matching the typespec attributes of a declaration and
5932 haven't gotten the name yet, and therefore, no symbol yet. */
5936 sym
->binding_label
= binding_label
;
5938 curr_binding_label
= binding_label
;
5940 else if (allow_binding_name
)
5942 /* No binding label, but if symbol isn't null, we
5943 can set the label for it here.
5944 If name="" or allow_binding_name is false, no C binding name is
5946 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5947 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
5950 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5951 && current_interface
.type
== INTERFACE_ABSTRACT
)
5953 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5961 /* Return nonzero if we're currently compiling a contained procedure. */
5964 contained_procedure (void)
5966 gfc_state_data
*s
= gfc_state_stack
;
5968 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5969 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5975 /* Set the kind of each enumerator. The kind is selected such that it is
5976 interoperable with the corresponding C enumeration type, making
5977 sure that -fshort-enums is honored. */
5982 enumerator_history
*current_history
= NULL
;
5986 if (max_enum
== NULL
|| enum_history
== NULL
)
5989 if (!flag_short_enums
)
5995 kind
= gfc_integer_kinds
[i
++].kind
;
5997 while (kind
< gfc_c_int_kind
5998 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6001 current_history
= enum_history
;
6002 while (current_history
!= NULL
)
6004 current_history
->sym
->ts
.kind
= kind
;
6005 current_history
= current_history
->next
;
6010 /* Match any of the various end-block statements. Returns the type of
6011 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6012 and END BLOCK statements cannot be replaced by a single END statement. */
6015 gfc_match_end (gfc_statement
*st
)
6017 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6018 gfc_compile_state state
;
6020 const char *block_name
;
6024 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6025 gfc_namespace
**nsp
;
6027 old_loc
= gfc_current_locus
;
6028 if (gfc_match ("end") != MATCH_YES
)
6031 state
= gfc_current_state ();
6032 block_name
= gfc_current_block () == NULL
6033 ? NULL
: gfc_current_block ()->name
;
6037 case COMP_ASSOCIATE
:
6039 if (!strncmp (block_name
, "block@", strlen("block@")))
6044 case COMP_DERIVED_CONTAINS
:
6045 state
= gfc_state_stack
->previous
->state
;
6046 block_name
= gfc_state_stack
->previous
->sym
== NULL
6047 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6058 *st
= ST_END_PROGRAM
;
6059 target
= " program";
6063 case COMP_SUBROUTINE
:
6064 *st
= ST_END_SUBROUTINE
;
6065 target
= " subroutine";
6066 eos_ok
= !contained_procedure ();
6070 *st
= ST_END_FUNCTION
;
6071 target
= " function";
6072 eos_ok
= !contained_procedure ();
6075 case COMP_BLOCK_DATA
:
6076 *st
= ST_END_BLOCK_DATA
;
6077 target
= " block data";
6082 *st
= ST_END_MODULE
;
6087 case COMP_INTERFACE
:
6088 *st
= ST_END_INTERFACE
;
6089 target
= " interface";
6094 case COMP_DERIVED_CONTAINS
:
6100 case COMP_ASSOCIATE
:
6101 *st
= ST_END_ASSOCIATE
;
6102 target
= " associate";
6119 case COMP_DO_CONCURRENT
:
6126 *st
= ST_END_CRITICAL
;
6127 target
= " critical";
6132 case COMP_SELECT_TYPE
:
6133 *st
= ST_END_SELECT
;
6139 *st
= ST_END_FORALL
;
6154 last_initializer
= NULL
;
6156 gfc_free_enum_history ();
6160 gfc_error ("Unexpected END statement at %C");
6164 old_loc
= gfc_current_locus
;
6165 if (gfc_match_eos () == MATCH_YES
)
6167 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6169 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6170 "instead of %s statement at %L",
6171 gfc_ascii_statement(*st
), &old_loc
))
6176 /* We would have required END [something]. */
6177 gfc_error ("%s statement expected at %L",
6178 gfc_ascii_statement (*st
), &old_loc
);
6185 /* Verify that we've got the sort of end-block that we're expecting. */
6186 if (gfc_match (target
) != MATCH_YES
)
6188 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6193 old_loc
= gfc_current_locus
;
6194 /* If we're at the end, make sure a block name wasn't required. */
6195 if (gfc_match_eos () == MATCH_YES
)
6198 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6199 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6200 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6206 gfc_error ("Expected block name of '%s' in %s statement at %L",
6207 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6212 /* END INTERFACE has a special handler for its several possible endings. */
6213 if (*st
== ST_END_INTERFACE
)
6214 return gfc_match_end_interface ();
6216 /* We haven't hit the end of statement, so what is left must be an
6218 m
= gfc_match_space ();
6220 m
= gfc_match_name (name
);
6223 gfc_error ("Expected terminating name at %C");
6227 if (block_name
== NULL
)
6230 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6232 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
6233 gfc_ascii_statement (*st
));
6236 /* Procedure pointer as function result. */
6237 else if (strcmp (block_name
, "ppr@") == 0
6238 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6240 gfc_error ("Expected label '%s' for %s statement at %C",
6241 gfc_current_block ()->ns
->proc_name
->name
,
6242 gfc_ascii_statement (*st
));
6246 if (gfc_match_eos () == MATCH_YES
)
6250 gfc_syntax_error (*st
);
6253 gfc_current_locus
= old_loc
;
6255 /* If we are missing an END BLOCK, we created a half-ready namespace.
6256 Remove it from the parent namespace's sibling list. */
6258 if (state
== COMP_BLOCK
)
6260 parent_ns
= gfc_current_ns
->parent
;
6262 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6268 if (ns
== gfc_current_ns
)
6270 if (prev_ns
== NULL
)
6273 prev_ns
->sibling
= ns
->sibling
;
6279 gfc_free_namespace (gfc_current_ns
);
6280 gfc_current_ns
= parent_ns
;
6288 /***************** Attribute declaration statements ****************/
6290 /* Set the attribute of a single variable. */
6295 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6303 m
= gfc_match_name (name
);
6307 if (find_special (name
, &sym
, false))
6310 if (!check_function_name (name
))
6316 var_locus
= gfc_current_locus
;
6318 /* Deal with possible array specification for certain attributes. */
6319 if (current_attr
.dimension
6320 || current_attr
.codimension
6321 || current_attr
.allocatable
6322 || current_attr
.pointer
6323 || current_attr
.target
)
6325 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6326 !current_attr
.dimension
6327 && !current_attr
.pointer
6328 && !current_attr
.target
);
6329 if (m
== MATCH_ERROR
)
6332 if (current_attr
.dimension
&& m
== MATCH_NO
)
6334 gfc_error ("Missing array specification at %L in DIMENSION "
6335 "statement", &var_locus
);
6340 if (current_attr
.dimension
&& sym
->value
)
6342 gfc_error ("Dimensions specified for %s at %L after its "
6343 "initialisation", sym
->name
, &var_locus
);
6348 if (current_attr
.codimension
&& m
== MATCH_NO
)
6350 gfc_error ("Missing array specification at %L in CODIMENSION "
6351 "statement", &var_locus
);
6356 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6357 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6359 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6365 /* Update symbol table. DIMENSION attribute is set in
6366 gfc_set_array_spec(). For CLASS variables, this must be applied
6367 to the first component, or '_data' field. */
6368 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6370 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6378 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6379 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6386 if (sym
->ts
.type
== BT_CLASS
6387 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6393 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6399 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6401 /* Fix the array spec. */
6402 m
= gfc_mod_pointee_as (sym
->as
);
6403 if (m
== MATCH_ERROR
)
6407 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6413 if ((current_attr
.external
|| current_attr
.intrinsic
)
6414 && sym
->attr
.flavor
!= FL_PROCEDURE
6415 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6421 add_hidden_procptr_result (sym
);
6426 gfc_free_array_spec (as
);
6431 /* Generic attribute declaration subroutine. Used for attributes that
6432 just have a list of names. */
6439 /* Gobble the optional double colon, by simply ignoring the result
6449 if (gfc_match_eos () == MATCH_YES
)
6455 if (gfc_match_char (',') != MATCH_YES
)
6457 gfc_error ("Unexpected character in variable list at %C");
6467 /* This routine matches Cray Pointer declarations of the form:
6468 pointer ( <pointer>, <pointee> )
6470 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6471 The pointer, if already declared, should be an integer. Otherwise, we
6472 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6473 be either a scalar, or an array declaration. No space is allocated for
6474 the pointee. For the statement
6475 pointer (ipt, ar(10))
6476 any subsequent uses of ar will be translated (in C-notation) as
6477 ar(i) => ((<type> *) ipt)(i)
6478 After gimplification, pointee variable will disappear in the code. */
6481 cray_pointer_decl (void)
6484 gfc_array_spec
*as
= NULL
;
6485 gfc_symbol
*cptr
; /* Pointer symbol. */
6486 gfc_symbol
*cpte
; /* Pointee symbol. */
6492 if (gfc_match_char ('(') != MATCH_YES
)
6494 gfc_error ("Expected '(' at %C");
6498 /* Match pointer. */
6499 var_locus
= gfc_current_locus
;
6500 gfc_clear_attr (¤t_attr
);
6501 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6502 current_ts
.type
= BT_INTEGER
;
6503 current_ts
.kind
= gfc_index_integer_kind
;
6505 m
= gfc_match_symbol (&cptr
, 0);
6508 gfc_error ("Expected variable name at %C");
6512 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6515 gfc_set_sym_referenced (cptr
);
6517 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6519 cptr
->ts
.type
= BT_INTEGER
;
6520 cptr
->ts
.kind
= gfc_index_integer_kind
;
6522 else if (cptr
->ts
.type
!= BT_INTEGER
)
6524 gfc_error ("Cray pointer at %C must be an integer");
6527 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6528 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6529 " memory addresses require %d bytes",
6530 cptr
->ts
.kind
, gfc_index_integer_kind
);
6532 if (gfc_match_char (',') != MATCH_YES
)
6534 gfc_error ("Expected \",\" at %C");
6538 /* Match Pointee. */
6539 var_locus
= gfc_current_locus
;
6540 gfc_clear_attr (¤t_attr
);
6541 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6542 current_ts
.type
= BT_UNKNOWN
;
6543 current_ts
.kind
= 0;
6545 m
= gfc_match_symbol (&cpte
, 0);
6548 gfc_error ("Expected variable name at %C");
6552 /* Check for an optional array spec. */
6553 m
= gfc_match_array_spec (&as
, true, false);
6554 if (m
== MATCH_ERROR
)
6556 gfc_free_array_spec (as
);
6559 else if (m
== MATCH_NO
)
6561 gfc_free_array_spec (as
);
6565 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6568 gfc_set_sym_referenced (cpte
);
6570 if (cpte
->as
== NULL
)
6572 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6573 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6575 else if (as
!= NULL
)
6577 gfc_error ("Duplicate array spec for Cray pointee at %C");
6578 gfc_free_array_spec (as
);
6584 if (cpte
->as
!= NULL
)
6586 /* Fix array spec. */
6587 m
= gfc_mod_pointee_as (cpte
->as
);
6588 if (m
== MATCH_ERROR
)
6592 /* Point the Pointee at the Pointer. */
6593 cpte
->cp_pointer
= cptr
;
6595 if (gfc_match_char (')') != MATCH_YES
)
6597 gfc_error ("Expected \")\" at %C");
6600 m
= gfc_match_char (',');
6602 done
= true; /* Stop searching for more declarations. */
6606 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6607 || gfc_match_eos () != MATCH_YES
)
6609 gfc_error ("Expected \",\" or end of statement at %C");
6617 gfc_match_external (void)
6620 gfc_clear_attr (¤t_attr
);
6621 current_attr
.external
= 1;
6623 return attr_decl ();
6628 gfc_match_intent (void)
6632 /* This is not allowed within a BLOCK construct! */
6633 if (gfc_current_state () == COMP_BLOCK
)
6635 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6639 intent
= match_intent_spec ();
6640 if (intent
== INTENT_UNKNOWN
)
6643 gfc_clear_attr (¤t_attr
);
6644 current_attr
.intent
= intent
;
6646 return attr_decl ();
6651 gfc_match_intrinsic (void)
6654 gfc_clear_attr (¤t_attr
);
6655 current_attr
.intrinsic
= 1;
6657 return attr_decl ();
6662 gfc_match_optional (void)
6664 /* This is not allowed within a BLOCK construct! */
6665 if (gfc_current_state () == COMP_BLOCK
)
6667 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6671 gfc_clear_attr (¤t_attr
);
6672 current_attr
.optional
= 1;
6674 return attr_decl ();
6679 gfc_match_pointer (void)
6681 gfc_gobble_whitespace ();
6682 if (gfc_peek_ascii_char () == '(')
6684 if (!gfc_option
.flag_cray_pointer
)
6686 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6690 return cray_pointer_decl ();
6694 gfc_clear_attr (¤t_attr
);
6695 current_attr
.pointer
= 1;
6697 return attr_decl ();
6703 gfc_match_allocatable (void)
6705 gfc_clear_attr (¤t_attr
);
6706 current_attr
.allocatable
= 1;
6708 return attr_decl ();
6713 gfc_match_codimension (void)
6715 gfc_clear_attr (¤t_attr
);
6716 current_attr
.codimension
= 1;
6718 return attr_decl ();
6723 gfc_match_contiguous (void)
6725 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6728 gfc_clear_attr (¤t_attr
);
6729 current_attr
.contiguous
= 1;
6731 return attr_decl ();
6736 gfc_match_dimension (void)
6738 gfc_clear_attr (¤t_attr
);
6739 current_attr
.dimension
= 1;
6741 return attr_decl ();
6746 gfc_match_target (void)
6748 gfc_clear_attr (¤t_attr
);
6749 current_attr
.target
= 1;
6751 return attr_decl ();
6755 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6759 access_attr_decl (gfc_statement st
)
6761 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6762 interface_type type
;
6764 gfc_symbol
*sym
, *dt_sym
;
6765 gfc_intrinsic_op op
;
6768 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6773 m
= gfc_match_generic_spec (&type
, name
, &op
);
6776 if (m
== MATCH_ERROR
)
6781 case INTERFACE_NAMELESS
:
6782 case INTERFACE_ABSTRACT
:
6785 case INTERFACE_GENERIC
:
6786 if (gfc_get_symbol (name
, NULL
, &sym
))
6789 if (!gfc_add_access (&sym
->attr
,
6791 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6795 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6796 && !gfc_add_access (&dt_sym
->attr
,
6798 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6804 case INTERFACE_INTRINSIC_OP
:
6805 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6807 gfc_intrinsic_op other_op
;
6809 gfc_current_ns
->operator_access
[op
] =
6810 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6812 /* Handle the case if there is another op with the same
6813 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6814 other_op
= gfc_equivalent_op (op
);
6816 if (other_op
!= INTRINSIC_NONE
)
6817 gfc_current_ns
->operator_access
[other_op
] =
6818 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6823 gfc_error ("Access specification of the %s operator at %C has "
6824 "already been specified", gfc_op2string (op
));
6830 case INTERFACE_USER_OP
:
6831 uop
= gfc_get_uop (name
);
6833 if (uop
->access
== ACCESS_UNKNOWN
)
6835 uop
->access
= (st
== ST_PUBLIC
)
6836 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6840 gfc_error ("Access specification of the .%s. operator at %C "
6841 "has already been specified", sym
->name
);
6848 if (gfc_match_char (',') == MATCH_NO
)
6852 if (gfc_match_eos () != MATCH_YES
)
6857 gfc_syntax_error (st
);
6865 gfc_match_protected (void)
6870 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6872 gfc_error ("PROTECTED at %C only allowed in specification "
6873 "part of a module");
6878 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6881 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6886 if (gfc_match_eos () == MATCH_YES
)
6891 m
= gfc_match_symbol (&sym
, 0);
6895 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
6907 if (gfc_match_eos () == MATCH_YES
)
6909 if (gfc_match_char (',') != MATCH_YES
)
6916 gfc_error ("Syntax error in PROTECTED statement at %C");
6921 /* The PRIVATE statement is a bit weird in that it can be an attribute
6922 declaration, but also works as a standalone statement inside of a
6923 type declaration or a module. */
6926 gfc_match_private (gfc_statement
*st
)
6929 if (gfc_match ("private") != MATCH_YES
)
6932 if (gfc_current_state () != COMP_MODULE
6933 && !(gfc_current_state () == COMP_DERIVED
6934 && gfc_state_stack
->previous
6935 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6936 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6937 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6938 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6940 gfc_error ("PRIVATE statement at %C is only allowed in the "
6941 "specification part of a module");
6945 if (gfc_current_state () == COMP_DERIVED
)
6947 if (gfc_match_eos () == MATCH_YES
)
6953 gfc_syntax_error (ST_PRIVATE
);
6957 if (gfc_match_eos () == MATCH_YES
)
6964 return access_attr_decl (ST_PRIVATE
);
6969 gfc_match_public (gfc_statement
*st
)
6972 if (gfc_match ("public") != MATCH_YES
)
6975 if (gfc_current_state () != COMP_MODULE
)
6977 gfc_error ("PUBLIC statement at %C is only allowed in the "
6978 "specification part of a module");
6982 if (gfc_match_eos () == MATCH_YES
)
6989 return access_attr_decl (ST_PUBLIC
);
6993 /* Workhorse for gfc_match_parameter. */
7003 m
= gfc_match_symbol (&sym
, 0);
7005 gfc_error ("Expected variable name at %C in PARAMETER statement");
7010 if (gfc_match_char ('=') == MATCH_NO
)
7012 gfc_error ("Expected = sign in PARAMETER statement at %C");
7016 m
= gfc_match_init_expr (&init
);
7018 gfc_error ("Expected expression at %C in PARAMETER statement");
7022 if (sym
->ts
.type
== BT_UNKNOWN
7023 && !gfc_set_default_type (sym
, 1, NULL
))
7029 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7030 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7038 gfc_error ("Initializing already initialized variable at %C");
7043 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7044 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7047 gfc_free_expr (init
);
7052 /* Match a parameter statement, with the weird syntax that these have. */
7055 gfc_match_parameter (void)
7059 if (gfc_match_char ('(') == MATCH_NO
)
7068 if (gfc_match (" )%t") == MATCH_YES
)
7071 if (gfc_match_char (',') != MATCH_YES
)
7073 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7083 /* Save statements have a special syntax. */
7086 gfc_match_save (void)
7088 char n
[GFC_MAX_SYMBOL_LEN
+1];
7093 if (gfc_match_eos () == MATCH_YES
)
7095 if (gfc_current_ns
->seen_save
)
7097 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7098 "follows previous SAVE statement"))
7102 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7106 if (gfc_current_ns
->save_all
)
7108 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7109 "blanket SAVE statement"))
7117 m
= gfc_match_symbol (&sym
, 0);
7121 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7122 &gfc_current_locus
))
7133 m
= gfc_match (" / %n /", &n
);
7134 if (m
== MATCH_ERROR
)
7139 c
= gfc_get_common (n
, 0);
7142 gfc_current_ns
->seen_save
= 1;
7145 if (gfc_match_eos () == MATCH_YES
)
7147 if (gfc_match_char (',') != MATCH_YES
)
7154 gfc_error ("Syntax error in SAVE statement at %C");
7160 gfc_match_value (void)
7165 /* This is not allowed within a BLOCK construct! */
7166 if (gfc_current_state () == COMP_BLOCK
)
7168 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7172 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7175 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7180 if (gfc_match_eos () == MATCH_YES
)
7185 m
= gfc_match_symbol (&sym
, 0);
7189 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7201 if (gfc_match_eos () == MATCH_YES
)
7203 if (gfc_match_char (',') != MATCH_YES
)
7210 gfc_error ("Syntax error in VALUE statement at %C");
7216 gfc_match_volatile (void)
7221 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7224 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7229 if (gfc_match_eos () == MATCH_YES
)
7234 /* VOLATILE is special because it can be added to host-associated
7235 symbols locally. Except for coarrays. */
7236 m
= gfc_match_symbol (&sym
, 1);
7240 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7241 for variable in a BLOCK which is defined outside of the BLOCK. */
7242 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7244 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7245 "%C, which is use-/host-associated", sym
->name
);
7248 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7260 if (gfc_match_eos () == MATCH_YES
)
7262 if (gfc_match_char (',') != MATCH_YES
)
7269 gfc_error ("Syntax error in VOLATILE statement at %C");
7275 gfc_match_asynchronous (void)
7280 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7283 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7288 if (gfc_match_eos () == MATCH_YES
)
7293 /* ASYNCHRONOUS is special because it can be added to host-associated
7295 m
= gfc_match_symbol (&sym
, 1);
7299 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7311 if (gfc_match_eos () == MATCH_YES
)
7313 if (gfc_match_char (',') != MATCH_YES
)
7320 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7325 /* Match a module procedure statement. Note that we have to modify
7326 symbols in the parent's namespace because the current one was there
7327 to receive symbols that are in an interface's formal argument list. */
7330 gfc_match_modproc (void)
7332 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7336 gfc_namespace
*module_ns
;
7337 gfc_interface
*old_interface_head
, *interface
;
7339 if (gfc_state_stack
->state
!= COMP_INTERFACE
7340 || gfc_state_stack
->previous
== NULL
7341 || current_interface
.type
== INTERFACE_NAMELESS
7342 || current_interface
.type
== INTERFACE_ABSTRACT
)
7344 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7349 module_ns
= gfc_current_ns
->parent
;
7350 for (; module_ns
; module_ns
= module_ns
->parent
)
7351 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7352 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7353 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7354 && !module_ns
->proc_name
->attr
.contained
))
7357 if (module_ns
== NULL
)
7360 /* Store the current state of the interface. We will need it if we
7361 end up with a syntax error and need to recover. */
7362 old_interface_head
= gfc_current_interface_head ();
7364 /* Check if the F2008 optional double colon appears. */
7365 gfc_gobble_whitespace ();
7366 old_locus
= gfc_current_locus
;
7367 if (gfc_match ("::") == MATCH_YES
)
7369 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7370 "MODULE PROCEDURE statement at %L", &old_locus
))
7374 gfc_current_locus
= old_locus
;
7379 old_locus
= gfc_current_locus
;
7381 m
= gfc_match_name (name
);
7387 /* Check for syntax error before starting to add symbols to the
7388 current namespace. */
7389 if (gfc_match_eos () == MATCH_YES
)
7392 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7395 /* Now we're sure the syntax is valid, we process this item
7397 if (gfc_get_symbol (name
, module_ns
, &sym
))
7400 if (sym
->attr
.intrinsic
)
7402 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7403 "PROCEDURE", &old_locus
);
7407 if (sym
->attr
.proc
!= PROC_MODULE
7408 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7411 if (!gfc_add_interface (sym
))
7414 sym
->attr
.mod_proc
= 1;
7415 sym
->declared_at
= old_locus
;
7424 /* Restore the previous state of the interface. */
7425 interface
= gfc_current_interface_head ();
7426 gfc_set_current_interface_head (old_interface_head
);
7428 /* Free the new interfaces. */
7429 while (interface
!= old_interface_head
)
7431 gfc_interface
*i
= interface
->next
;
7436 /* And issue a syntax error. */
7437 gfc_syntax_error (ST_MODULE_PROC
);
7442 /* Check a derived type that is being extended. */
7445 check_extended_derived_type (char *name
)
7447 gfc_symbol
*extended
;
7449 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7451 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7455 extended
= gfc_find_dt_in_generic (extended
);
7460 gfc_error ("Symbol '%s' at %C has not been previously defined", name
);
7464 if (extended
->attr
.flavor
!= FL_DERIVED
)
7466 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7467 "derived type", name
);
7471 if (extended
->attr
.is_bind_c
)
7473 gfc_error ("'%s' cannot be extended at %C because it "
7474 "is BIND(C)", extended
->name
);
7478 if (extended
->attr
.sequence
)
7480 gfc_error ("'%s' cannot be extended at %C because it "
7481 "is a SEQUENCE type", extended
->name
);
7489 /* Match the optional attribute specifiers for a type declaration.
7490 Return MATCH_ERROR if an error is encountered in one of the handled
7491 attributes (public, private, bind(c)), MATCH_NO if what's found is
7492 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7493 checking on attribute conflicts needs to be done. */
7496 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7498 /* See if the derived type is marked as private. */
7499 if (gfc_match (" , private") == MATCH_YES
)
7501 if (gfc_current_state () != COMP_MODULE
)
7503 gfc_error ("Derived type at %C can only be PRIVATE in the "
7504 "specification part of a module");
7508 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7511 else if (gfc_match (" , public") == MATCH_YES
)
7513 if (gfc_current_state () != COMP_MODULE
)
7515 gfc_error ("Derived type at %C can only be PUBLIC in the "
7516 "specification part of a module");
7520 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7523 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7525 /* If the type is defined to be bind(c) it then needs to make
7526 sure that all fields are interoperable. This will
7527 need to be a semantic check on the finished derived type.
7528 See 15.2.3 (lines 9-12) of F2003 draft. */
7529 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7532 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7534 else if (gfc_match (" , abstract") == MATCH_YES
)
7536 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7539 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7542 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7544 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7550 /* If we get here, something matched. */
7555 /* Match the beginning of a derived type declaration. If a type name
7556 was the result of a function, then it is possible to have a symbol
7557 already to be known as a derived type yet have no components. */
7560 gfc_match_derived_decl (void)
7562 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7563 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7564 symbol_attribute attr
;
7565 gfc_symbol
*sym
, *gensym
;
7566 gfc_symbol
*extended
;
7568 match is_type_attr_spec
= MATCH_NO
;
7569 bool seen_attr
= false;
7570 gfc_interface
*intr
= NULL
, *head
;
7572 if (gfc_current_state () == COMP_DERIVED
)
7577 gfc_clear_attr (&attr
);
7582 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7583 if (is_type_attr_spec
== MATCH_ERROR
)
7585 if (is_type_attr_spec
== MATCH_YES
)
7587 } while (is_type_attr_spec
== MATCH_YES
);
7589 /* Deal with derived type extensions. The extension attribute has
7590 been added to 'attr' but now the parent type must be found and
7593 extended
= check_extended_derived_type (parent
);
7595 if (parent
[0] && !extended
)
7598 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7600 gfc_error ("Expected :: in TYPE definition at %C");
7604 m
= gfc_match (" %n%t", name
);
7608 /* Make sure the name is not the name of an intrinsic type. */
7609 if (gfc_is_intrinsic_typename (name
))
7611 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7616 if (gfc_get_symbol (name
, NULL
, &gensym
))
7619 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7621 gfc_error ("Derived type name '%s' at %C already has a basic type "
7622 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7626 if (!gensym
->attr
.generic
7627 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7630 if (!gensym
->attr
.function
7631 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7634 sym
= gfc_find_dt_in_generic (gensym
);
7636 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7638 gfc_error ("Derived type definition of '%s' at %C has already been "
7639 "defined", sym
->name
);
7645 /* Use upper case to save the actual derived-type symbol. */
7646 gfc_get_symbol (gfc_get_string ("%c%s",
7647 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7648 &gensym
->name
[1]), NULL
, &sym
);
7649 sym
->name
= gfc_get_string (gensym
->name
);
7650 head
= gensym
->generic
;
7651 intr
= gfc_get_interface ();
7653 intr
->where
= gfc_current_locus
;
7654 intr
->sym
->declared_at
= gfc_current_locus
;
7656 gensym
->generic
= intr
;
7657 gensym
->attr
.if_source
= IFSRC_DECL
;
7660 /* The symbol may already have the derived attribute without the
7661 components. The ways this can happen is via a function
7662 definition, an INTRINSIC statement or a subtype in another
7663 derived type that is a pointer. The first part of the AND clause
7664 is true if the symbol is not the return value of a function. */
7665 if (sym
->attr
.flavor
!= FL_DERIVED
7666 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7669 if (attr
.access
!= ACCESS_UNKNOWN
7670 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7672 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7673 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7674 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7678 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7679 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7680 gensym
->attr
.access
= sym
->attr
.access
;
7682 /* See if the derived type was labeled as bind(c). */
7683 if (attr
.is_bind_c
!= 0)
7684 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7686 /* Construct the f2k_derived namespace if it is not yet there. */
7687 if (!sym
->f2k_derived
)
7688 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7690 if (extended
&& !sym
->components
)
7695 /* Add the extended derived type as the first component. */
7696 gfc_add_component (sym
, parent
, &p
);
7698 gfc_set_sym_referenced (extended
);
7700 p
->ts
.type
= BT_DERIVED
;
7701 p
->ts
.u
.derived
= extended
;
7702 p
->initializer
= gfc_default_initializer (&p
->ts
);
7704 /* Set extension level. */
7705 if (extended
->attr
.extension
== 255)
7707 /* Since the extension field is 8 bit wide, we can only have
7708 up to 255 extension levels. */
7709 gfc_error ("Maximum extension level reached with type '%s' at %L",
7710 extended
->name
, &extended
->declared_at
);
7713 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7715 /* Provide the links between the extended type and its extension. */
7716 if (!extended
->f2k_derived
)
7717 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7718 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7722 if (!sym
->hash_value
)
7723 /* Set the hash for the compound name for this type. */
7724 sym
->hash_value
= gfc_hash_value (sym
);
7726 /* Take over the ABSTRACT attribute. */
7727 sym
->attr
.abstract
= attr
.abstract
;
7729 gfc_new_block
= sym
;
7735 /* Cray Pointees can be declared as:
7736 pointer (ipt, a (n,m,...,*)) */
7739 gfc_mod_pointee_as (gfc_array_spec
*as
)
7741 as
->cray_pointee
= true; /* This will be useful to know later. */
7742 if (as
->type
== AS_ASSUMED_SIZE
)
7743 as
->cp_was_assumed
= true;
7744 else if (as
->type
== AS_ASSUMED_SHAPE
)
7746 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7753 /* Match the enum definition statement, here we are trying to match
7754 the first line of enum definition statement.
7755 Returns MATCH_YES if match is found. */
7758 gfc_match_enum (void)
7762 m
= gfc_match_eos ();
7766 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7773 /* Returns an initializer whose value is one higher than the value of the
7774 LAST_INITIALIZER argument. If the argument is NULL, the
7775 initializers value will be set to zero. The initializer's kind
7776 will be set to gfc_c_int_kind.
7778 If -fshort-enums is given, the appropriate kind will be selected
7779 later after all enumerators have been parsed. A warning is issued
7780 here if an initializer exceeds gfc_c_int_kind. */
7783 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7786 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7788 mpz_init (result
->value
.integer
);
7790 if (last_initializer
!= NULL
)
7792 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7793 result
->where
= last_initializer
->where
;
7795 if (gfc_check_integer_range (result
->value
.integer
,
7796 gfc_c_int_kind
) != ARITH_OK
)
7798 gfc_error ("Enumerator exceeds the C integer type at %C");
7804 /* Control comes here, if it's the very first enumerator and no
7805 initializer has been given. It will be initialized to zero. */
7806 mpz_set_si (result
->value
.integer
, 0);
7813 /* Match a variable name with an optional initializer. When this
7814 subroutine is called, a variable is expected to be parsed next.
7815 Depending on what is happening at the moment, updates either the
7816 symbol table or the current interface. */
7819 enumerator_decl (void)
7821 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7822 gfc_expr
*initializer
;
7823 gfc_array_spec
*as
= NULL
;
7831 old_locus
= gfc_current_locus
;
7833 /* When we get here, we've just matched a list of attributes and
7834 maybe a type and a double colon. The next thing we expect to see
7835 is the name of the symbol. */
7836 m
= gfc_match_name (name
);
7840 var_locus
= gfc_current_locus
;
7842 /* OK, we've successfully matched the declaration. Now put the
7843 symbol in the current namespace. If we fail to create the symbol,
7845 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7851 /* The double colon must be present in order to have initializers.
7852 Otherwise the statement is ambiguous with an assignment statement. */
7855 if (gfc_match_char ('=') == MATCH_YES
)
7857 m
= gfc_match_init_expr (&initializer
);
7860 gfc_error ("Expected an initialization expression at %C");
7869 /* If we do not have an initializer, the initialization value of the
7870 previous enumerator (stored in last_initializer) is incremented
7871 by 1 and is used to initialize the current enumerator. */
7872 if (initializer
== NULL
)
7873 initializer
= enum_initializer (last_initializer
, old_locus
);
7875 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7877 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7883 /* Store this current initializer, for the next enumerator variable
7884 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7885 use last_initializer below. */
7886 last_initializer
= initializer
;
7887 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7889 /* Maintain enumerator history. */
7890 gfc_find_symbol (name
, NULL
, 0, &sym
);
7891 create_enum_history (sym
, last_initializer
);
7893 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7896 /* Free stuff up and return. */
7897 gfc_free_expr (initializer
);
7903 /* Match the enumerator definition statement. */
7906 gfc_match_enumerator_def (void)
7911 gfc_clear_ts (¤t_ts
);
7913 m
= gfc_match (" enumerator");
7917 m
= gfc_match (" :: ");
7918 if (m
== MATCH_ERROR
)
7921 colon_seen
= (m
== MATCH_YES
);
7923 if (gfc_current_state () != COMP_ENUM
)
7925 gfc_error ("ENUM definition statement expected before %C");
7926 gfc_free_enum_history ();
7930 (¤t_ts
)->type
= BT_INTEGER
;
7931 (¤t_ts
)->kind
= gfc_c_int_kind
;
7933 gfc_clear_attr (¤t_attr
);
7934 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7943 m
= enumerator_decl ();
7944 if (m
== MATCH_ERROR
)
7946 gfc_free_enum_history ();
7952 if (gfc_match_eos () == MATCH_YES
)
7954 if (gfc_match_char (',') != MATCH_YES
)
7958 if (gfc_current_state () == COMP_ENUM
)
7960 gfc_free_enum_history ();
7961 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7966 gfc_free_array_spec (current_as
);
7973 /* Match binding attributes. */
7976 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7978 bool found_passing
= false;
7979 bool seen_ptr
= false;
7980 match m
= MATCH_YES
;
7982 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7983 this case the defaults are in there. */
7984 ba
->access
= ACCESS_UNKNOWN
;
7985 ba
->pass_arg
= NULL
;
7986 ba
->pass_arg_num
= 0;
7988 ba
->non_overridable
= 0;
7992 /* If we find a comma, we believe there are binding attributes. */
7993 m
= gfc_match_char (',');
7999 /* Access specifier. */
8001 m
= gfc_match (" public");
8002 if (m
== MATCH_ERROR
)
8006 if (ba
->access
!= ACCESS_UNKNOWN
)
8008 gfc_error ("Duplicate access-specifier at %C");
8012 ba
->access
= ACCESS_PUBLIC
;
8016 m
= gfc_match (" private");
8017 if (m
== MATCH_ERROR
)
8021 if (ba
->access
!= ACCESS_UNKNOWN
)
8023 gfc_error ("Duplicate access-specifier at %C");
8027 ba
->access
= ACCESS_PRIVATE
;
8031 /* If inside GENERIC, the following is not allowed. */
8036 m
= gfc_match (" nopass");
8037 if (m
== MATCH_ERROR
)
8043 gfc_error ("Binding attributes already specify passing,"
8044 " illegal NOPASS at %C");
8048 found_passing
= true;
8053 /* PASS possibly including argument. */
8054 m
= gfc_match (" pass");
8055 if (m
== MATCH_ERROR
)
8059 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8063 gfc_error ("Binding attributes already specify passing,"
8064 " illegal PASS at %C");
8068 m
= gfc_match (" ( %n )", arg
);
8069 if (m
== MATCH_ERROR
)
8072 ba
->pass_arg
= gfc_get_string (arg
);
8073 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8075 found_passing
= true;
8083 m
= gfc_match (" pointer");
8084 if (m
== MATCH_ERROR
)
8090 gfc_error ("Duplicate POINTER attribute at %C");
8100 /* NON_OVERRIDABLE flag. */
8101 m
= gfc_match (" non_overridable");
8102 if (m
== MATCH_ERROR
)
8106 if (ba
->non_overridable
)
8108 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8112 ba
->non_overridable
= 1;
8116 /* DEFERRED flag. */
8117 m
= gfc_match (" deferred");
8118 if (m
== MATCH_ERROR
)
8124 gfc_error ("Duplicate DEFERRED at %C");
8135 /* Nothing matching found. */
8137 gfc_error ("Expected access-specifier at %C");
8139 gfc_error ("Expected binding attribute at %C");
8142 while (gfc_match_char (',') == MATCH_YES
);
8144 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8145 if (ba
->non_overridable
&& ba
->deferred
)
8147 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8154 if (ba
->access
== ACCESS_UNKNOWN
)
8155 ba
->access
= gfc_typebound_default_access
;
8157 if (ppc
&& !seen_ptr
)
8159 gfc_error ("POINTER attribute is required for procedure pointer component"
8171 /* Match a PROCEDURE specific binding inside a derived type. */
8174 match_procedure_in_type (void)
8176 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8177 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8178 char* target
= NULL
, *ifc
= NULL
;
8179 gfc_typebound_proc tb
;
8188 /* Check current state. */
8189 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8190 block
= gfc_state_stack
->previous
->sym
;
8193 /* Try to match PROCEDURE(interface). */
8194 if (gfc_match (" (") == MATCH_YES
)
8196 m
= gfc_match_name (target_buf
);
8197 if (m
== MATCH_ERROR
)
8201 gfc_error ("Interface-name expected after '(' at %C");
8205 if (gfc_match (" )") != MATCH_YES
)
8207 gfc_error ("')' expected at %C");
8214 /* Construct the data structure. */
8215 memset (&tb
, 0, sizeof (tb
));
8216 tb
.where
= gfc_current_locus
;
8218 /* Match binding attributes. */
8219 m
= match_binding_attributes (&tb
, false, false);
8220 if (m
== MATCH_ERROR
)
8222 seen_attrs
= (m
== MATCH_YES
);
8224 /* Check that attribute DEFERRED is given if an interface is specified. */
8225 if (tb
.deferred
&& !ifc
)
8227 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8230 if (ifc
&& !tb
.deferred
)
8232 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8236 /* Match the colons. */
8237 m
= gfc_match (" ::");
8238 if (m
== MATCH_ERROR
)
8240 seen_colons
= (m
== MATCH_YES
);
8241 if (seen_attrs
&& !seen_colons
)
8243 gfc_error ("Expected '::' after binding-attributes at %C");
8247 /* Match the binding names. */
8250 m
= gfc_match_name (name
);
8251 if (m
== MATCH_ERROR
)
8255 gfc_error ("Expected binding name at %C");
8259 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8262 /* Try to match the '=> target', if it's there. */
8264 m
= gfc_match (" =>");
8265 if (m
== MATCH_ERROR
)
8271 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8277 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8282 m
= gfc_match_name (target_buf
);
8283 if (m
== MATCH_ERROR
)
8287 gfc_error ("Expected binding target after '=>' at %C");
8290 target
= target_buf
;
8293 /* If no target was found, it has the same name as the binding. */
8297 /* Get the namespace to insert the symbols into. */
8298 ns
= block
->f2k_derived
;
8301 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8302 if (tb
.deferred
&& !block
->attr
.abstract
)
8304 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8305 "is not ABSTRACT", block
->name
);
8309 /* See if we already have a binding with this name in the symtree which
8310 would be an error. If a GENERIC already targeted this binding, it may
8311 be already there but then typebound is still NULL. */
8312 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8313 if (stree
&& stree
->n
.tb
)
8315 gfc_error ("There is already a procedure with binding name '%s' for "
8316 "the derived type '%s' at %C", name
, block
->name
);
8320 /* Insert it and set attributes. */
8324 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8327 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8329 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8332 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8334 if (gfc_match_eos () == MATCH_YES
)
8336 if (gfc_match_char (',') != MATCH_YES
)
8341 gfc_error ("Syntax error in PROCEDURE statement at %C");
8346 /* Match a GENERIC procedure binding inside a derived type. */
8349 gfc_match_generic (void)
8351 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8352 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8354 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8355 gfc_typebound_proc
* tb
;
8357 interface_type op_type
;
8358 gfc_intrinsic_op op
;
8361 /* Check current state. */
8362 if (gfc_current_state () == COMP_DERIVED
)
8364 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8367 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8369 block
= gfc_state_stack
->previous
->sym
;
8370 ns
= block
->f2k_derived
;
8371 gcc_assert (block
&& ns
);
8373 memset (&tbattr
, 0, sizeof (tbattr
));
8374 tbattr
.where
= gfc_current_locus
;
8376 /* See if we get an access-specifier. */
8377 m
= match_binding_attributes (&tbattr
, true, false);
8378 if (m
== MATCH_ERROR
)
8381 /* Now the colons, those are required. */
8382 if (gfc_match (" ::") != MATCH_YES
)
8384 gfc_error ("Expected '::' at %C");
8388 /* Match the binding name; depending on type (operator / generic) format
8389 it for future error messages into bind_name. */
8391 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8392 if (m
== MATCH_ERROR
)
8396 gfc_error ("Expected generic name or operator descriptor at %C");
8402 case INTERFACE_GENERIC
:
8403 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8406 case INTERFACE_USER_OP
:
8407 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8410 case INTERFACE_INTRINSIC_OP
:
8411 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8412 gfc_op2string (op
));
8419 /* Match the required =>. */
8420 if (gfc_match (" =>") != MATCH_YES
)
8422 gfc_error ("Expected '=>' at %C");
8426 /* Try to find existing GENERIC binding with this name / for this operator;
8427 if there is something, check that it is another GENERIC and then extend
8428 it rather than building a new node. Otherwise, create it and put it
8429 at the right position. */
8433 case INTERFACE_USER_OP
:
8434 case INTERFACE_GENERIC
:
8436 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8439 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8451 case INTERFACE_INTRINSIC_OP
:
8461 if (!tb
->is_generic
)
8463 gcc_assert (op_type
== INTERFACE_GENERIC
);
8464 gfc_error ("There's already a non-generic procedure with binding name"
8465 " '%s' for the derived type '%s' at %C",
8466 bind_name
, block
->name
);
8470 if (tb
->access
!= tbattr
.access
)
8472 gfc_error ("Binding at %C must have the same access as already"
8473 " defined binding '%s'", bind_name
);
8479 tb
= gfc_get_typebound_proc (NULL
);
8480 tb
->where
= gfc_current_locus
;
8481 tb
->access
= tbattr
.access
;
8483 tb
->u
.generic
= NULL
;
8487 case INTERFACE_GENERIC
:
8488 case INTERFACE_USER_OP
:
8490 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8493 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8501 case INTERFACE_INTRINSIC_OP
:
8510 /* Now, match all following names as specific targets. */
8513 gfc_symtree
* target_st
;
8514 gfc_tbp_generic
* target
;
8516 m
= gfc_match_name (name
);
8517 if (m
== MATCH_ERROR
)
8521 gfc_error ("Expected specific binding name at %C");
8525 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8527 /* See if this is a duplicate specification. */
8528 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8529 if (target_st
== target
->specific_st
)
8531 gfc_error ("'%s' already defined as specific binding for the"
8532 " generic '%s' at %C", name
, bind_name
);
8536 target
= gfc_get_tbp_generic ();
8537 target
->specific_st
= target_st
;
8538 target
->specific
= NULL
;
8539 target
->next
= tb
->u
.generic
;
8540 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8541 || (op_type
== INTERFACE_INTRINSIC_OP
));
8542 tb
->u
.generic
= target
;
8544 while (gfc_match (" ,") == MATCH_YES
);
8546 /* Here should be the end. */
8547 if (gfc_match_eos () != MATCH_YES
)
8549 gfc_error ("Junk after GENERIC binding at %C");
8560 /* Match a FINAL declaration inside a derived type. */
8563 gfc_match_final_decl (void)
8565 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8568 gfc_namespace
* module_ns
;
8572 if (gfc_current_form
== FORM_FREE
)
8574 char c
= gfc_peek_ascii_char ();
8575 if (!gfc_is_whitespace (c
) && c
!= ':')
8579 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8581 if (gfc_current_form
== FORM_FIXED
)
8584 gfc_error ("FINAL declaration at %C must be inside a derived type "
8585 "CONTAINS section");
8589 block
= gfc_state_stack
->previous
->sym
;
8592 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8593 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8595 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8596 " specification part of a MODULE");
8600 module_ns
= gfc_current_ns
;
8601 gcc_assert (module_ns
);
8602 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8604 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8605 if (gfc_match (" ::") == MATCH_ERROR
)
8608 /* Match the sequence of procedure names. */
8615 if (first
&& gfc_match_eos () == MATCH_YES
)
8617 gfc_error ("Empty FINAL at %C");
8621 m
= gfc_match_name (name
);
8624 gfc_error ("Expected module procedure name at %C");
8627 else if (m
!= MATCH_YES
)
8630 if (gfc_match_eos () == MATCH_YES
)
8632 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8634 gfc_error ("Expected ',' at %C");
8638 if (gfc_get_symbol (name
, module_ns
, &sym
))
8640 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8644 /* Mark the symbol as module procedure. */
8645 if (sym
->attr
.proc
!= PROC_MODULE
8646 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8649 /* Check if we already have this symbol in the list, this is an error. */
8650 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8651 if (f
->proc_sym
== sym
)
8653 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8658 /* Add this symbol to the list of finalizers. */
8659 gcc_assert (block
->f2k_derived
);
8661 f
= XCNEW (gfc_finalizer
);
8663 f
->proc_tree
= NULL
;
8664 f
->where
= gfc_current_locus
;
8665 f
->next
= block
->f2k_derived
->finalizers
;
8666 block
->f2k_derived
->finalizers
= f
;
8676 const ext_attr_t ext_attr_list
[] = {
8677 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8678 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8679 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8680 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8681 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8682 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8683 { NULL
, EXT_ATTR_LAST
, NULL
}
8686 /* Match a !GCC$ ATTRIBUTES statement of the form:
8687 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8688 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8690 TODO: We should support all GCC attributes using the same syntax for
8691 the attribute list, i.e. the list in C
8692 __attributes(( attribute-list ))
8694 !GCC$ ATTRIBUTES attribute-list ::
8695 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8698 As there is absolutely no risk of confusion, we should never return
8701 gfc_match_gcc_attributes (void)
8703 symbol_attribute attr
;
8704 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8709 gfc_clear_attr (&attr
);
8714 if (gfc_match_name (name
) != MATCH_YES
)
8717 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8718 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8721 if (id
== EXT_ATTR_LAST
)
8723 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8727 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8730 gfc_gobble_whitespace ();
8731 ch
= gfc_next_ascii_char ();
8734 /* This is the successful exit condition for the loop. */
8735 if (gfc_next_ascii_char () == ':')
8745 if (gfc_match_eos () == MATCH_YES
)
8750 m
= gfc_match_name (name
);
8754 if (find_special (name
, &sym
, true))
8757 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8759 if (gfc_match_eos () == MATCH_YES
)
8762 if (gfc_match_char (',') != MATCH_YES
)
8769 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");