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"))
2001 return match_old_style_init (name
);
2004 /* The double colon must be present in order to have initializers.
2005 Otherwise the statement is ambiguous with an assignment statement. */
2008 if (gfc_match (" =>") == MATCH_YES
)
2010 if (!current_attr
.pointer
)
2012 gfc_error ("Initialization at %C isn't for a pointer variable");
2017 m
= match_pointer_init (&initializer
, 0);
2021 else if (gfc_match_char ('=') == MATCH_YES
)
2023 if (current_attr
.pointer
)
2025 gfc_error ("Pointer initialization at %C requires '=>', "
2031 m
= gfc_match_init_expr (&initializer
);
2034 gfc_error ("Expected an initialization expression at %C");
2038 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2039 && gfc_state_stack
->state
!= COMP_DERIVED
)
2041 gfc_error ("Initialization of variable at %C is not allowed in "
2042 "a PURE procedure");
2046 if (current_attr
.flavor
!= FL_PARAMETER
2047 && gfc_state_stack
->state
!= COMP_DERIVED
)
2048 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2055 if (initializer
!= NULL
&& current_attr
.allocatable
2056 && gfc_current_state () == COMP_DERIVED
)
2058 gfc_error ("Initialization of allocatable component at %C is not "
2064 /* Add the initializer. Note that it is fine if initializer is
2065 NULL here, because we sometimes also need to check if a
2066 declaration *must* have an initialization expression. */
2067 if (gfc_current_state () != COMP_DERIVED
)
2068 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2071 if (current_ts
.type
== BT_DERIVED
2072 && !current_attr
.pointer
&& !initializer
)
2073 initializer
= gfc_default_initializer (¤t_ts
);
2074 t
= build_struct (name
, cl
, &initializer
, &as
);
2077 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2080 /* Free stuff up and return. */
2081 gfc_free_expr (initializer
);
2082 gfc_free_array_spec (as
);
2088 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2089 This assumes that the byte size is equal to the kind number for
2090 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2093 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2098 if (gfc_match_char ('*') != MATCH_YES
)
2101 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2105 original_kind
= ts
->kind
;
2107 /* Massage the kind numbers for complex types. */
2108 if (ts
->type
== BT_COMPLEX
)
2112 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2113 gfc_basic_typename (ts
->type
), original_kind
);
2120 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2123 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2127 if (gfc_option
.flag_real4_kind
== 8)
2129 if (gfc_option
.flag_real4_kind
== 10)
2131 if (gfc_option
.flag_real4_kind
== 16)
2137 if (gfc_option
.flag_real8_kind
== 4)
2139 if (gfc_option
.flag_real8_kind
== 10)
2141 if (gfc_option
.flag_real8_kind
== 16)
2146 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2148 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2149 gfc_basic_typename (ts
->type
), original_kind
);
2153 if (!gfc_notify_std (GFC_STD_GNU
,
2154 "Nonstandard type declaration %s*%d at %C",
2155 gfc_basic_typename(ts
->type
), original_kind
))
2162 /* Match a kind specification. Since kinds are generally optional, we
2163 usually return MATCH_NO if something goes wrong. If a "kind="
2164 string is found, then we know we have an error. */
2167 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2179 where
= loc
= gfc_current_locus
;
2184 if (gfc_match_char ('(') == MATCH_NO
)
2187 /* Also gobbles optional text. */
2188 if (gfc_match (" kind = ") == MATCH_YES
)
2191 loc
= gfc_current_locus
;
2194 n
= gfc_match_init_expr (&e
);
2198 if (gfc_matching_function
)
2200 /* The function kind expression might include use associated or
2201 imported parameters and try again after the specification
2203 if (gfc_match_char (')') != MATCH_YES
)
2205 gfc_error ("Missing right parenthesis at %C");
2211 gfc_undo_symbols ();
2216 /* ....or else, the match is real. */
2218 gfc_error ("Expected initialization expression at %C");
2226 gfc_error ("Expected scalar initialization expression at %C");
2231 msg
= gfc_extract_int (e
, &ts
->kind
);
2240 /* Before throwing away the expression, let's see if we had a
2241 C interoperable kind (and store the fact). */
2242 if (e
->ts
.is_c_interop
== 1)
2244 /* Mark this as C interoperable if being declared with one
2245 of the named constants from iso_c_binding. */
2246 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2247 ts
->f90_type
= e
->ts
.f90_type
;
2253 /* Ignore errors to this point, if we've gotten here. This means
2254 we ignore the m=MATCH_ERROR from above. */
2255 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2257 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2258 gfc_basic_typename (ts
->type
));
2259 gfc_current_locus
= where
;
2263 /* Warn if, e.g., c_int is used for a REAL variable, but not
2264 if, e.g., c_double is used for COMPLEX as the standard
2265 explicitly says that the kind type parameter for complex and real
2266 variable is the same, i.e. c_float == c_float_complex. */
2267 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2268 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2269 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2270 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2271 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2272 gfc_basic_typename (ts
->type
));
2274 gfc_gobble_whitespace ();
2275 if ((c
= gfc_next_ascii_char ()) != ')'
2276 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2278 if (ts
->type
== BT_CHARACTER
)
2279 gfc_error ("Missing right parenthesis or comma at %C");
2281 gfc_error ("Missing right parenthesis at %C");
2285 /* All tests passed. */
2288 if(m
== MATCH_ERROR
)
2289 gfc_current_locus
= where
;
2291 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2294 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2298 if (gfc_option
.flag_real4_kind
== 8)
2300 if (gfc_option
.flag_real4_kind
== 10)
2302 if (gfc_option
.flag_real4_kind
== 16)
2308 if (gfc_option
.flag_real8_kind
== 4)
2310 if (gfc_option
.flag_real8_kind
== 10)
2312 if (gfc_option
.flag_real8_kind
== 16)
2317 /* Return what we know from the test(s). */
2322 gfc_current_locus
= where
;
2328 match_char_kind (int * kind
, int * is_iso_c
)
2337 where
= gfc_current_locus
;
2339 n
= gfc_match_init_expr (&e
);
2341 if (n
!= MATCH_YES
&& gfc_matching_function
)
2343 /* The expression might include use-associated or imported
2344 parameters and try again after the specification
2347 gfc_undo_symbols ();
2352 gfc_error ("Expected initialization expression at %C");
2358 gfc_error ("Expected scalar initialization expression at %C");
2363 msg
= gfc_extract_int (e
, kind
);
2364 *is_iso_c
= e
->ts
.is_iso_c
;
2374 /* Ignore errors to this point, if we've gotten here. This means
2375 we ignore the m=MATCH_ERROR from above. */
2376 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2378 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2382 /* All tests passed. */
2385 if (m
== MATCH_ERROR
)
2386 gfc_current_locus
= where
;
2388 /* Return what we know from the test(s). */
2393 gfc_current_locus
= where
;
2398 /* Match the various kind/length specifications in a CHARACTER
2399 declaration. We don't return MATCH_NO. */
2402 gfc_match_char_spec (gfc_typespec
*ts
)
2404 int kind
, seen_length
, is_iso_c
;
2416 /* Try the old-style specification first. */
2417 old_char_selector
= 0;
2419 m
= match_char_length (&len
, &deferred
, true);
2423 old_char_selector
= 1;
2428 m
= gfc_match_char ('(');
2431 m
= MATCH_YES
; /* Character without length is a single char. */
2435 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2436 if (gfc_match (" kind =") == MATCH_YES
)
2438 m
= match_char_kind (&kind
, &is_iso_c
);
2440 if (m
== MATCH_ERROR
)
2445 if (gfc_match (" , len =") == MATCH_NO
)
2448 m
= char_len_param_value (&len
, &deferred
);
2451 if (m
== MATCH_ERROR
)
2458 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2459 if (gfc_match (" len =") == MATCH_YES
)
2461 m
= char_len_param_value (&len
, &deferred
);
2464 if (m
== MATCH_ERROR
)
2468 if (gfc_match_char (')') == MATCH_YES
)
2471 if (gfc_match (" , kind =") != MATCH_YES
)
2474 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2480 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2481 m
= char_len_param_value (&len
, &deferred
);
2484 if (m
== MATCH_ERROR
)
2488 m
= gfc_match_char (')');
2492 if (gfc_match_char (',') != MATCH_YES
)
2495 gfc_match (" kind ="); /* Gobble optional text. */
2497 m
= match_char_kind (&kind
, &is_iso_c
);
2498 if (m
== MATCH_ERROR
)
2504 /* Require a right-paren at this point. */
2505 m
= gfc_match_char (')');
2510 gfc_error ("Syntax error in CHARACTER declaration at %C");
2512 gfc_free_expr (len
);
2516 /* Deal with character functions after USE and IMPORT statements. */
2517 if (gfc_matching_function
)
2519 gfc_free_expr (len
);
2520 gfc_undo_symbols ();
2526 gfc_free_expr (len
);
2530 /* Do some final massaging of the length values. */
2531 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2533 if (seen_length
== 0)
2534 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2539 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2540 ts
->deferred
= deferred
;
2542 /* We have to know if it was a C interoperable kind so we can
2543 do accurate type checking of bind(c) procs, etc. */
2545 /* Mark this as C interoperable if being declared with one
2546 of the named constants from iso_c_binding. */
2547 ts
->is_c_interop
= is_iso_c
;
2548 else if (len
!= NULL
)
2549 /* Here, we might have parsed something such as: character(c_char)
2550 In this case, the parsing code above grabs the c_char when
2551 looking for the length (line 1690, roughly). it's the last
2552 testcase for parsing the kind params of a character variable.
2553 However, it's not actually the length. this seems like it
2555 To see if the user used a C interop kind, test the expr
2556 of the so called length, and see if it's C interoperable. */
2557 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2563 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2564 structure to the matched specification. This is necessary for FUNCTION and
2565 IMPLICIT statements.
2567 If implicit_flag is nonzero, then we don't check for the optional
2568 kind specification. Not doing so is needed for matching an IMPLICIT
2569 statement correctly. */
2572 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2574 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2575 gfc_symbol
*sym
, *dt_sym
;
2578 bool seen_deferred_kind
, matched_type
;
2579 const char *dt_name
;
2581 /* A belt and braces check that the typespec is correctly being treated
2582 as a deferred characteristic association. */
2583 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2584 && (gfc_current_block ()->result
->ts
.kind
== -1)
2585 && (ts
->kind
== -1);
2587 if (seen_deferred_kind
)
2590 /* Clear the current binding label, in case one is given. */
2591 curr_binding_label
= NULL
;
2593 if (gfc_match (" byte") == MATCH_YES
)
2595 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2598 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2600 gfc_error ("BYTE type used at %C "
2601 "is not available on the target machine");
2605 ts
->type
= BT_INTEGER
;
2611 m
= gfc_match (" type (");
2612 matched_type
= (m
== MATCH_YES
);
2615 gfc_gobble_whitespace ();
2616 if (gfc_peek_ascii_char () == '*')
2618 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2620 if (gfc_current_state () == COMP_DERIVED
)
2622 gfc_error ("Assumed type at %C is not allowed for components");
2625 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2628 ts
->type
= BT_ASSUMED
;
2632 m
= gfc_match ("%n", name
);
2633 matched_type
= (m
== MATCH_YES
);
2636 if ((matched_type
&& strcmp ("integer", name
) == 0)
2637 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2639 ts
->type
= BT_INTEGER
;
2640 ts
->kind
= gfc_default_integer_kind
;
2644 if ((matched_type
&& strcmp ("character", name
) == 0)
2645 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2648 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2649 "intrinsic-type-spec at %C"))
2652 ts
->type
= BT_CHARACTER
;
2653 if (implicit_flag
== 0)
2654 m
= gfc_match_char_spec (ts
);
2658 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2664 if ((matched_type
&& strcmp ("real", name
) == 0)
2665 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2668 ts
->kind
= gfc_default_real_kind
;
2673 && (strcmp ("doubleprecision", name
) == 0
2674 || (strcmp ("double", name
) == 0
2675 && gfc_match (" precision") == MATCH_YES
)))
2676 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2679 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2680 "intrinsic-type-spec at %C"))
2682 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2686 ts
->kind
= gfc_default_double_kind
;
2690 if ((matched_type
&& strcmp ("complex", name
) == 0)
2691 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2693 ts
->type
= BT_COMPLEX
;
2694 ts
->kind
= gfc_default_complex_kind
;
2699 && (strcmp ("doublecomplex", name
) == 0
2700 || (strcmp ("double", name
) == 0
2701 && gfc_match (" complex") == MATCH_YES
)))
2702 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2704 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2708 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2709 "intrinsic-type-spec at %C"))
2712 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2715 ts
->type
= BT_COMPLEX
;
2716 ts
->kind
= gfc_default_double_kind
;
2720 if ((matched_type
&& strcmp ("logical", name
) == 0)
2721 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2723 ts
->type
= BT_LOGICAL
;
2724 ts
->kind
= gfc_default_logical_kind
;
2729 m
= gfc_match_char (')');
2732 ts
->type
= BT_DERIVED
;
2735 /* Match CLASS declarations. */
2736 m
= gfc_match (" class ( * )");
2737 if (m
== MATCH_ERROR
)
2739 else if (m
== MATCH_YES
)
2743 ts
->type
= BT_CLASS
;
2744 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2747 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2748 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2750 gfc_set_sym_referenced (upe
);
2752 upe
->ts
.type
= BT_VOID
;
2753 upe
->attr
.unlimited_polymorphic
= 1;
2754 /* This is essential to force the construction of
2755 unlimited polymorphic component class containers. */
2756 upe
->attr
.zero_comp
= 1;
2757 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2758 &gfc_current_locus
))
2763 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2765 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2769 ts
->u
.derived
= upe
;
2773 m
= gfc_match (" class ( %n )", name
);
2776 ts
->type
= BT_CLASS
;
2778 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2782 /* Defer association of the derived type until the end of the
2783 specification block. However, if the derived type can be
2784 found, add it to the typespec. */
2785 if (gfc_matching_function
)
2787 ts
->u
.derived
= NULL
;
2788 if (gfc_current_state () != COMP_INTERFACE
2789 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2791 sym
= gfc_find_dt_in_generic (sym
);
2792 ts
->u
.derived
= sym
;
2797 /* Search for the name but allow the components to be defined later. If
2798 type = -1, this typespec has been seen in a function declaration but
2799 the type could not be accessed at that point. The actual derived type is
2800 stored in a symtree with the first letter of the name capitalized; the
2801 symtree with the all lower-case name contains the associated
2802 generic function. */
2803 dt_name
= gfc_get_string ("%c%s",
2804 (char) TOUPPER ((unsigned char) name
[0]),
2805 (const char*)&name
[1]);
2810 gfc_get_ha_symbol (name
, &sym
);
2811 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2813 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2816 if (sym
->generic
&& !dt_sym
)
2817 dt_sym
= gfc_find_dt_in_generic (sym
);
2819 else if (ts
->kind
== -1)
2821 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2822 || gfc_current_ns
->has_import_set
;
2823 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2824 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2826 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2829 if (sym
&& sym
->generic
&& !dt_sym
)
2830 dt_sym
= gfc_find_dt_in_generic (sym
);
2837 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2838 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2839 || sym
->attr
.subroutine
)
2841 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2842 "entity at %L, which has the same name", name
,
2847 gfc_set_sym_referenced (sym
);
2848 if (!sym
->attr
.generic
2849 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2852 if (!sym
->attr
.function
2853 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2858 gfc_interface
*intr
, *head
;
2860 /* Use upper case to save the actual derived-type symbol. */
2861 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2862 dt_sym
->name
= gfc_get_string (sym
->name
);
2863 head
= sym
->generic
;
2864 intr
= gfc_get_interface ();
2866 intr
->where
= gfc_current_locus
;
2868 sym
->generic
= intr
;
2869 sym
->attr
.if_source
= IFSRC_DECL
;
2872 gfc_set_sym_referenced (dt_sym
);
2874 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2875 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2878 ts
->u
.derived
= dt_sym
;
2884 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2885 "intrinsic-type-spec at %C"))
2888 /* For all types except double, derived and character, look for an
2889 optional kind specifier. MATCH_NO is actually OK at this point. */
2890 if (implicit_flag
== 1)
2892 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2898 if (gfc_current_form
== FORM_FREE
)
2900 c
= gfc_peek_ascii_char ();
2901 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2902 && c
!= ':' && c
!= ',')
2904 if (matched_type
&& c
== ')')
2906 gfc_next_ascii_char ();
2913 m
= gfc_match_kind_spec (ts
, false);
2914 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2915 m
= gfc_match_old_kind_spec (ts
);
2917 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2920 /* Defer association of the KIND expression of function results
2921 until after USE and IMPORT statements. */
2922 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2923 || gfc_matching_function
)
2927 m
= MATCH_YES
; /* No kind specifier found. */
2933 /* Match an IMPLICIT NONE statement. Actually, this statement is
2934 already matched in parse.c, or we would not end up here in the
2935 first place. So the only thing we need to check, is if there is
2936 trailing garbage. If not, the match is successful. */
2939 gfc_match_implicit_none (void)
2941 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2945 /* Match the letter range(s) of an IMPLICIT statement. */
2948 match_implicit_range (void)
2954 cur_loc
= gfc_current_locus
;
2956 gfc_gobble_whitespace ();
2957 c
= gfc_next_ascii_char ();
2960 gfc_error ("Missing character range in IMPLICIT at %C");
2967 gfc_gobble_whitespace ();
2968 c1
= gfc_next_ascii_char ();
2972 gfc_gobble_whitespace ();
2973 c
= gfc_next_ascii_char ();
2978 inner
= 0; /* Fall through. */
2985 gfc_gobble_whitespace ();
2986 c2
= gfc_next_ascii_char ();
2990 gfc_gobble_whitespace ();
2991 c
= gfc_next_ascii_char ();
2993 if ((c
!= ',') && (c
!= ')'))
3006 gfc_error ("Letters must be in alphabetic order in "
3007 "IMPLICIT statement at %C");
3011 /* See if we can add the newly matched range to the pending
3012 implicits from this IMPLICIT statement. We do not check for
3013 conflicts with whatever earlier IMPLICIT statements may have
3014 set. This is done when we've successfully finished matching
3016 if (!gfc_add_new_implicit_range (c1
, c2
))
3023 gfc_syntax_error (ST_IMPLICIT
);
3025 gfc_current_locus
= cur_loc
;
3030 /* Match an IMPLICIT statement, storing the types for
3031 gfc_set_implicit() if the statement is accepted by the parser.
3032 There is a strange looking, but legal syntactic construction
3033 possible. It looks like:
3035 IMPLICIT INTEGER (a-b) (c-d)
3037 This is legal if "a-b" is a constant expression that happens to
3038 equal one of the legal kinds for integers. The real problem
3039 happens with an implicit specification that looks like:
3041 IMPLICIT INTEGER (a-b)
3043 In this case, a typespec matcher that is "greedy" (as most of the
3044 matchers are) gobbles the character range as a kindspec, leaving
3045 nothing left. We therefore have to go a bit more slowly in the
3046 matching process by inhibiting the kindspec checking during
3047 typespec matching and checking for a kind later. */
3050 gfc_match_implicit (void)
3059 /* We don't allow empty implicit statements. */
3060 if (gfc_match_eos () == MATCH_YES
)
3062 gfc_error ("Empty IMPLICIT statement at %C");
3068 /* First cleanup. */
3069 gfc_clear_new_implicit ();
3071 /* A basic type is mandatory here. */
3072 m
= gfc_match_decl_type_spec (&ts
, 1);
3073 if (m
== MATCH_ERROR
)
3078 cur_loc
= gfc_current_locus
;
3079 m
= match_implicit_range ();
3083 /* We may have <TYPE> (<RANGE>). */
3084 gfc_gobble_whitespace ();
3085 c
= gfc_next_ascii_char ();
3086 if ((c
== '\n') || (c
== ','))
3088 /* Check for CHARACTER with no length parameter. */
3089 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3091 ts
.kind
= gfc_default_character_kind
;
3092 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3093 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3097 /* Record the Successful match. */
3098 if (!gfc_merge_new_implicit (&ts
))
3103 gfc_current_locus
= cur_loc
;
3106 /* Discard the (incorrectly) matched range. */
3107 gfc_clear_new_implicit ();
3109 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3110 if (ts
.type
== BT_CHARACTER
)
3111 m
= gfc_match_char_spec (&ts
);
3114 m
= gfc_match_kind_spec (&ts
, false);
3117 m
= gfc_match_old_kind_spec (&ts
);
3118 if (m
== MATCH_ERROR
)
3124 if (m
== MATCH_ERROR
)
3127 m
= match_implicit_range ();
3128 if (m
== MATCH_ERROR
)
3133 gfc_gobble_whitespace ();
3134 c
= gfc_next_ascii_char ();
3135 if ((c
!= '\n') && (c
!= ','))
3138 if (!gfc_merge_new_implicit (&ts
))
3146 gfc_syntax_error (ST_IMPLICIT
);
3154 gfc_match_import (void)
3156 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3161 if (gfc_current_ns
->proc_name
== NULL
3162 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3164 gfc_error ("IMPORT statement at %C only permitted in "
3165 "an INTERFACE body");
3169 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3172 if (gfc_match_eos () == MATCH_YES
)
3174 /* All host variables should be imported. */
3175 gfc_current_ns
->has_import_set
= 1;
3179 if (gfc_match (" ::") == MATCH_YES
)
3181 if (gfc_match_eos () == MATCH_YES
)
3183 gfc_error ("Expecting list of named entities at %C");
3191 m
= gfc_match (" %n", name
);
3195 if (gfc_current_ns
->parent
!= NULL
3196 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3198 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3201 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3202 && gfc_find_symbol (name
,
3203 gfc_current_ns
->proc_name
->ns
->parent
,
3206 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3212 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3213 "at %C - does not exist.", name
);
3217 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3219 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3224 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3227 sym
->attr
.imported
= 1;
3229 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3231 /* The actual derived type is stored in a symtree with the first
3232 letter of the name capitalized; the symtree with the all
3233 lower-case name contains the associated generic function. */
3234 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3235 gfc_get_string ("%c%s",
3236 (char) TOUPPER ((unsigned char) name
[0]),
3240 sym
->attr
.imported
= 1;
3253 if (gfc_match_eos () == MATCH_YES
)
3255 if (gfc_match_char (',') != MATCH_YES
)
3262 gfc_error ("Syntax error in IMPORT statement at %C");
3267 /* A minimal implementation of gfc_match without whitespace, escape
3268 characters or variable arguments. Returns true if the next
3269 characters match the TARGET template exactly. */
3272 match_string_p (const char *target
)
3276 for (p
= target
; *p
; p
++)
3277 if ((char) gfc_next_ascii_char () != *p
)
3282 /* Matches an attribute specification including array specs. If
3283 successful, leaves the variables current_attr and current_as
3284 holding the specification. Also sets the colon_seen variable for
3285 later use by matchers associated with initializations.
3287 This subroutine is a little tricky in the sense that we don't know
3288 if we really have an attr-spec until we hit the double colon.
3289 Until that time, we can only return MATCH_NO. This forces us to
3290 check for duplicate specification at this level. */
3293 match_attr_spec (void)
3295 /* Modifiers that can exist in a type statement. */
3297 { GFC_DECL_BEGIN
= 0,
3298 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3299 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3300 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3301 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3302 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3303 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3306 /* GFC_DECL_END is the sentinel, index starts at 0. */
3307 #define NUM_DECL GFC_DECL_END
3309 locus start
, seen_at
[NUM_DECL
];
3316 gfc_clear_attr (¤t_attr
);
3317 start
= gfc_current_locus
;
3322 /* See if we get all of the keywords up to the final double colon. */
3323 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3331 gfc_gobble_whitespace ();
3333 ch
= gfc_next_ascii_char ();
3336 /* This is the successful exit condition for the loop. */
3337 if (gfc_next_ascii_char () == ':')
3342 gfc_gobble_whitespace ();
3343 switch (gfc_peek_ascii_char ())
3346 gfc_next_ascii_char ();
3347 switch (gfc_next_ascii_char ())
3350 if (match_string_p ("locatable"))
3352 /* Matched "allocatable". */
3353 d
= DECL_ALLOCATABLE
;
3358 if (match_string_p ("ynchronous"))
3360 /* Matched "asynchronous". */
3361 d
= DECL_ASYNCHRONOUS
;
3368 /* Try and match the bind(c). */
3369 m
= gfc_match_bind_c (NULL
, true);
3372 else if (m
== MATCH_ERROR
)
3377 gfc_next_ascii_char ();
3378 if ('o' != gfc_next_ascii_char ())
3380 switch (gfc_next_ascii_char ())
3383 if (match_string_p ("imension"))
3385 d
= DECL_CODIMENSION
;
3389 if (match_string_p ("tiguous"))
3391 d
= DECL_CONTIGUOUS
;
3398 if (match_string_p ("dimension"))
3403 if (match_string_p ("external"))
3408 if (match_string_p ("int"))
3410 ch
= gfc_next_ascii_char ();
3413 if (match_string_p ("nt"))
3415 /* Matched "intent". */
3416 /* TODO: Call match_intent_spec from here. */
3417 if (gfc_match (" ( in out )") == MATCH_YES
)
3419 else if (gfc_match (" ( in )") == MATCH_YES
)
3421 else if (gfc_match (" ( out )") == MATCH_YES
)
3427 if (match_string_p ("insic"))
3429 /* Matched "intrinsic". */
3437 if (match_string_p ("optional"))
3442 gfc_next_ascii_char ();
3443 switch (gfc_next_ascii_char ())
3446 if (match_string_p ("rameter"))
3448 /* Matched "parameter". */
3454 if (match_string_p ("inter"))
3456 /* Matched "pointer". */
3462 ch
= gfc_next_ascii_char ();
3465 if (match_string_p ("vate"))
3467 /* Matched "private". */
3473 if (match_string_p ("tected"))
3475 /* Matched "protected". */
3482 if (match_string_p ("blic"))
3484 /* Matched "public". */
3492 if (match_string_p ("save"))
3497 if (match_string_p ("target"))
3502 gfc_next_ascii_char ();
3503 ch
= gfc_next_ascii_char ();
3506 if (match_string_p ("lue"))
3508 /* Matched "value". */
3514 if (match_string_p ("latile"))
3516 /* Matched "volatile". */
3524 /* No double colon and no recognizable decl_type, so assume that
3525 we've been looking at something else the whole time. */
3532 /* Check to make sure any parens are paired up correctly. */
3533 if (gfc_match_parens () == MATCH_ERROR
)
3540 seen_at
[d
] = gfc_current_locus
;
3542 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3544 gfc_array_spec
*as
= NULL
;
3546 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3547 d
== DECL_CODIMENSION
);
3549 if (current_as
== NULL
)
3551 else if (m
== MATCH_YES
)
3553 if (!merge_array_spec (as
, current_as
, false))
3560 if (d
== DECL_CODIMENSION
)
3561 gfc_error ("Missing codimension specification at %C");
3563 gfc_error ("Missing dimension specification at %C");
3567 if (m
== MATCH_ERROR
)
3572 /* Since we've seen a double colon, we have to be looking at an
3573 attr-spec. This means that we can now issue errors. */
3574 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3579 case DECL_ALLOCATABLE
:
3580 attr
= "ALLOCATABLE";
3582 case DECL_ASYNCHRONOUS
:
3583 attr
= "ASYNCHRONOUS";
3585 case DECL_CODIMENSION
:
3586 attr
= "CODIMENSION";
3588 case DECL_CONTIGUOUS
:
3589 attr
= "CONTIGUOUS";
3591 case DECL_DIMENSION
:
3598 attr
= "INTENT (IN)";
3601 attr
= "INTENT (OUT)";
3604 attr
= "INTENT (IN OUT)";
3606 case DECL_INTRINSIC
:
3612 case DECL_PARAMETER
:
3618 case DECL_PROTECTED
:
3633 case DECL_IS_BIND_C
:
3643 attr
= NULL
; /* This shouldn't happen. */
3646 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3651 /* Now that we've dealt with duplicate attributes, add the attributes
3652 to the current attribute. */
3653 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3658 if (gfc_current_state () == COMP_DERIVED
3659 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3660 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3661 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3663 if (d
== DECL_ALLOCATABLE
)
3665 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3666 "attribute at %C in a TYPE definition"))
3674 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3681 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3682 && gfc_current_state () != COMP_MODULE
)
3684 if (d
== DECL_PRIVATE
)
3688 if (gfc_current_state () == COMP_DERIVED
3689 && gfc_state_stack
->previous
3690 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3692 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3693 "at %L in a TYPE definition", attr
,
3702 gfc_error ("%s attribute at %L is not allowed outside of the "
3703 "specification part of a module", attr
, &seen_at
[d
]);
3711 case DECL_ALLOCATABLE
:
3712 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3715 case DECL_ASYNCHRONOUS
:
3716 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3719 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3722 case DECL_CODIMENSION
:
3723 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3726 case DECL_CONTIGUOUS
:
3727 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3730 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3733 case DECL_DIMENSION
:
3734 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3738 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3742 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3746 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3750 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3753 case DECL_INTRINSIC
:
3754 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3758 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3761 case DECL_PARAMETER
:
3762 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3766 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3769 case DECL_PROTECTED
:
3770 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3772 gfc_error ("PROTECTED at %C only allowed in specification "
3773 "part of a module");
3778 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3781 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3785 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3790 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3795 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3799 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3802 case DECL_IS_BIND_C
:
3803 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3807 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3810 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3814 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3817 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3821 gfc_internal_error ("match_attr_spec(): Bad attribute");
3831 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3832 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3833 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3834 current_attr
.save
= SAVE_IMPLICIT
;
3840 gfc_current_locus
= start
;
3841 gfc_free_array_spec (current_as
);
3847 /* Set the binding label, dest_label, either with the binding label
3848 stored in the given gfc_typespec, ts, or if none was provided, it
3849 will be the symbol name in all lower case, as required by the draft
3850 (J3/04-007, section 15.4.1). If a binding label was given and
3851 there is more than one argument (num_idents), it is an error. */
3854 set_binding_label (const char **dest_label
, const char *sym_name
,
3857 if (num_idents
> 1 && has_name_equals
)
3859 gfc_error ("Multiple identifiers provided with "
3860 "single NAME= specifier at %C");
3864 if (curr_binding_label
)
3865 /* Binding label given; store in temp holder till have sym. */
3866 *dest_label
= curr_binding_label
;
3869 /* No binding label given, and the NAME= specifier did not exist,
3870 which means there was no NAME="". */
3871 if (sym_name
!= NULL
&& has_name_equals
== 0)
3872 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3879 /* Set the status of the given common block as being BIND(C) or not,
3880 depending on the given parameter, is_bind_c. */
3883 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3885 com_block
->is_bind_c
= is_bind_c
;
3890 /* Verify that the given gfc_typespec is for a C interoperable type. */
3893 gfc_verify_c_interop (gfc_typespec
*ts
)
3895 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3896 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3898 else if (ts
->type
== BT_CLASS
)
3900 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
3907 /* Verify that the variables of a given common block, which has been
3908 defined with the attribute specifier bind(c), to be of a C
3909 interoperable type. Errors will be reported here, if
3913 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3915 gfc_symbol
*curr_sym
= NULL
;
3918 curr_sym
= com_block
->head
;
3920 /* Make sure we have at least one symbol. */
3921 if (curr_sym
== NULL
)
3924 /* Here we know we have a symbol, so we'll execute this loop
3928 /* The second to last param, 1, says this is in a common block. */
3929 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3930 curr_sym
= curr_sym
->common_next
;
3931 } while (curr_sym
!= NULL
);
3937 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3938 an appropriate error message is reported. */
3941 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3942 int is_in_common
, gfc_common_head
*com_block
)
3944 bool bind_c_function
= false;
3947 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3948 bind_c_function
= true;
3950 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3952 tmp_sym
= tmp_sym
->result
;
3953 /* Make sure it wasn't an implicitly typed result. */
3954 if (tmp_sym
->attr
.implicit_type
&& gfc_option
.warn_c_binding_type
)
3956 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3957 "%L may not be C interoperable", tmp_sym
->name
,
3958 &tmp_sym
->declared_at
);
3959 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3960 /* Mark it as C interoperable to prevent duplicate warnings. */
3961 tmp_sym
->ts
.is_c_interop
= 1;
3962 tmp_sym
->attr
.is_c_interop
= 1;
3966 /* Here, we know we have the bind(c) attribute, so if we have
3967 enough type info, then verify that it's a C interop kind.
3968 The info could be in the symbol already, or possibly still in
3969 the given ts (current_ts), so look in both. */
3970 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3972 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
3974 /* See if we're dealing with a sym in a common block or not. */
3975 if (is_in_common
== 1 && gfc_option
.warn_c_binding_type
)
3977 gfc_warning ("Variable '%s' in common block '%s' at %L "
3978 "may not be a C interoperable "
3979 "kind though common block '%s' is BIND(C)",
3980 tmp_sym
->name
, com_block
->name
,
3981 &(tmp_sym
->declared_at
), com_block
->name
);
3985 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3986 gfc_error ("Type declaration '%s' at %L is not C "
3987 "interoperable but it is BIND(C)",
3988 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3989 else if (gfc_option
.warn_c_binding_type
)
3990 gfc_warning ("Variable '%s' at %L "
3991 "may not be a C interoperable "
3992 "kind but it is bind(c)",
3993 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3997 /* Variables declared w/in a common block can't be bind(c)
3998 since there's no way for C to see these variables, so there's
3999 semantically no reason for the attribute. */
4000 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4002 gfc_error ("Variable '%s' in common block '%s' at "
4003 "%L cannot be declared with BIND(C) "
4004 "since it is not a global",
4005 tmp_sym
->name
, com_block
->name
,
4006 &(tmp_sym
->declared_at
));
4010 /* Scalar variables that are bind(c) can not have the pointer
4011 or allocatable attributes. */
4012 if (tmp_sym
->attr
.is_bind_c
== 1)
4014 if (tmp_sym
->attr
.pointer
== 1)
4016 gfc_error ("Variable '%s' at %L cannot have both the "
4017 "POINTER and BIND(C) attributes",
4018 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4022 if (tmp_sym
->attr
.allocatable
== 1)
4024 gfc_error ("Variable '%s' at %L cannot have both the "
4025 "ALLOCATABLE and BIND(C) attributes",
4026 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4032 /* If it is a BIND(C) function, make sure the return value is a
4033 scalar value. The previous tests in this function made sure
4034 the type is interoperable. */
4035 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4036 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4037 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4039 /* BIND(C) functions can not return a character string. */
4040 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4041 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4042 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4043 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4044 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4045 "be a character string", tmp_sym
->name
,
4046 &(tmp_sym
->declared_at
));
4049 /* See if the symbol has been marked as private. If it has, make sure
4050 there is no binding label and warn the user if there is one. */
4051 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4052 && tmp_sym
->binding_label
)
4053 /* Use gfc_warning_now because we won't say that the symbol fails
4054 just because of this. */
4055 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4056 "given the binding label '%s'", tmp_sym
->name
,
4057 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4063 /* Set the appropriate fields for a symbol that's been declared as
4064 BIND(C) (the is_bind_c flag and the binding label), and verify that
4065 the type is C interoperable. Errors are reported by the functions
4066 used to set/test these fields. */
4069 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4073 /* TODO: Do we need to make sure the vars aren't marked private? */
4075 /* Set the is_bind_c bit in symbol_attribute. */
4076 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4078 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4085 /* Set the fields marking the given common block as BIND(C), including
4086 a binding label, and report any errors encountered. */
4089 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4093 /* destLabel, common name, typespec (which may have binding label). */
4094 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4098 /* Set the given common block (com_block) to being bind(c) (1). */
4099 set_com_block_bind_c (com_block
, 1);
4105 /* Retrieve the list of one or more identifiers that the given bind(c)
4106 attribute applies to. */
4109 get_bind_c_idents (void)
4111 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4113 gfc_symbol
*tmp_sym
= NULL
;
4115 gfc_common_head
*com_block
= NULL
;
4117 if (gfc_match_name (name
) == MATCH_YES
)
4119 found_id
= MATCH_YES
;
4120 gfc_get_ha_symbol (name
, &tmp_sym
);
4122 else if (match_common_name (name
) == MATCH_YES
)
4124 found_id
= MATCH_YES
;
4125 com_block
= gfc_get_common (name
, 0);
4129 gfc_error ("Need either entity or common block name for "
4130 "attribute specification statement at %C");
4134 /* Save the current identifier and look for more. */
4137 /* Increment the number of identifiers found for this spec stmt. */
4140 /* Make sure we have a sym or com block, and verify that it can
4141 be bind(c). Set the appropriate field(s) and look for more
4143 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4145 if (tmp_sym
!= NULL
)
4147 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4152 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4156 /* Look to see if we have another identifier. */
4158 if (gfc_match_eos () == MATCH_YES
)
4159 found_id
= MATCH_NO
;
4160 else if (gfc_match_char (',') != MATCH_YES
)
4161 found_id
= MATCH_NO
;
4162 else if (gfc_match_name (name
) == MATCH_YES
)
4164 found_id
= MATCH_YES
;
4165 gfc_get_ha_symbol (name
, &tmp_sym
);
4167 else if (match_common_name (name
) == MATCH_YES
)
4169 found_id
= MATCH_YES
;
4170 com_block
= gfc_get_common (name
, 0);
4174 gfc_error ("Missing entity or common block name for "
4175 "attribute specification statement at %C");
4181 gfc_internal_error ("Missing symbol");
4183 } while (found_id
== MATCH_YES
);
4185 /* if we get here we were successful */
4190 /* Try and match a BIND(C) attribute specification statement. */
4193 gfc_match_bind_c_stmt (void)
4195 match found_match
= MATCH_NO
;
4200 /* This may not be necessary. */
4202 /* Clear the temporary binding label holder. */
4203 curr_binding_label
= NULL
;
4205 /* Look for the bind(c). */
4206 found_match
= gfc_match_bind_c (NULL
, true);
4208 if (found_match
== MATCH_YES
)
4210 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4213 /* Look for the :: now, but it is not required. */
4216 /* Get the identifier(s) that needs to be updated. This may need to
4217 change to hand the flag(s) for the attr specified so all identifiers
4218 found can have all appropriate parts updated (assuming that the same
4219 spec stmt can have multiple attrs, such as both bind(c) and
4221 if (!get_bind_c_idents ())
4222 /* Error message should have printed already. */
4230 /* Match a data declaration statement. */
4233 gfc_match_data_decl (void)
4239 num_idents_on_line
= 0;
4241 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4245 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4246 && gfc_current_state () != COMP_DERIVED
)
4248 sym
= gfc_use_derived (current_ts
.u
.derived
);
4256 current_ts
.u
.derived
= sym
;
4259 m
= match_attr_spec ();
4260 if (m
== MATCH_ERROR
)
4266 if (current_ts
.type
== BT_CLASS
4267 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4270 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4271 && current_ts
.u
.derived
->components
== NULL
4272 && !current_ts
.u
.derived
->attr
.zero_comp
)
4275 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4278 gfc_find_symbol (current_ts
.u
.derived
->name
,
4279 current_ts
.u
.derived
->ns
, 1, &sym
);
4281 /* Any symbol that we find had better be a type definition
4282 which has its components defined. */
4283 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4284 && (current_ts
.u
.derived
->components
!= NULL
4285 || current_ts
.u
.derived
->attr
.zero_comp
))
4288 gfc_error ("Derived type at %C has not been previously defined "
4289 "and so cannot appear in a derived type definition");
4295 /* If we have an old-style character declaration, and no new-style
4296 attribute specifications, then there a comma is optional between
4297 the type specification and the variable list. */
4298 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4299 gfc_match_char (',');
4301 /* Give the types/attributes to symbols that follow. Give the element
4302 a number so that repeat character length expressions can be copied. */
4306 num_idents_on_line
++;
4307 m
= variable_decl (elem
++);
4308 if (m
== MATCH_ERROR
)
4313 if (gfc_match_eos () == MATCH_YES
)
4315 if (gfc_match_char (',') != MATCH_YES
)
4319 if (gfc_error_flag_test () == 0)
4320 gfc_error ("Syntax error in data declaration at %C");
4323 gfc_free_data_all (gfc_current_ns
);
4326 gfc_free_array_spec (current_as
);
4332 /* Match a prefix associated with a function or subroutine
4333 declaration. If the typespec pointer is nonnull, then a typespec
4334 can be matched. Note that if nothing matches, MATCH_YES is
4335 returned (the null string was matched). */
4338 gfc_match_prefix (gfc_typespec
*ts
)
4344 gfc_clear_attr (¤t_attr
);
4346 seen_impure
= false;
4348 gcc_assert (!gfc_matching_prefix
);
4349 gfc_matching_prefix
= true;
4353 found_prefix
= false;
4355 if (!seen_type
&& ts
!= NULL
4356 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4357 && gfc_match_space () == MATCH_YES
)
4361 found_prefix
= true;
4364 if (gfc_match ("elemental% ") == MATCH_YES
)
4366 if (!gfc_add_elemental (¤t_attr
, NULL
))
4369 found_prefix
= true;
4372 if (gfc_match ("pure% ") == MATCH_YES
)
4374 if (!gfc_add_pure (¤t_attr
, NULL
))
4377 found_prefix
= true;
4380 if (gfc_match ("recursive% ") == MATCH_YES
)
4382 if (!gfc_add_recursive (¤t_attr
, NULL
))
4385 found_prefix
= true;
4388 /* IMPURE is a somewhat special case, as it needs not set an actual
4389 attribute but rather only prevents ELEMENTAL routines from being
4390 automatically PURE. */
4391 if (gfc_match ("impure% ") == MATCH_YES
)
4393 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4397 found_prefix
= true;
4400 while (found_prefix
);
4402 /* IMPURE and PURE must not both appear, of course. */
4403 if (seen_impure
&& current_attr
.pure
)
4405 gfc_error ("PURE and IMPURE must not appear both at %C");
4409 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4410 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4412 if (!gfc_add_pure (¤t_attr
, NULL
))
4416 /* At this point, the next item is not a prefix. */
4417 gcc_assert (gfc_matching_prefix
);
4418 gfc_matching_prefix
= false;
4422 gcc_assert (gfc_matching_prefix
);
4423 gfc_matching_prefix
= false;
4428 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4431 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4433 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4436 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4439 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4446 /* Match a formal argument list. */
4449 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4451 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4452 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4458 if (gfc_match_char ('(') != MATCH_YES
)
4465 if (gfc_match_char (')') == MATCH_YES
)
4470 if (gfc_match_char ('*') == MATCH_YES
)
4473 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4482 m
= gfc_match_name (name
);
4486 if (gfc_get_symbol (name
, NULL
, &sym
))
4490 p
= gfc_get_formal_arglist ();
4502 /* We don't add the VARIABLE flavor because the name could be a
4503 dummy procedure. We don't apply these attributes to formal
4504 arguments of statement functions. */
4505 if (sym
!= NULL
&& !st_flag
4506 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4507 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4513 /* The name of a program unit can be in a different namespace,
4514 so check for it explicitly. After the statement is accepted,
4515 the name is checked for especially in gfc_get_symbol(). */
4516 if (gfc_new_block
!= NULL
&& sym
!= NULL
4517 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4519 gfc_error ("Name '%s' at %C is the name of the procedure",
4525 if (gfc_match_char (')') == MATCH_YES
)
4528 m
= gfc_match_char (',');
4531 gfc_error ("Unexpected junk in formal argument list at %C");
4537 /* Check for duplicate symbols in the formal argument list. */
4540 for (p
= head
; p
->next
; p
= p
->next
)
4545 for (q
= p
->next
; q
; q
= q
->next
)
4546 if (p
->sym
== q
->sym
)
4548 gfc_error ("Duplicate symbol '%s' in formal argument list "
4549 "at %C", p
->sym
->name
);
4557 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4566 gfc_free_formal_arglist (head
);
4571 /* Match a RESULT specification following a function declaration or
4572 ENTRY statement. Also matches the end-of-statement. */
4575 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4577 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4581 if (gfc_match (" result (") != MATCH_YES
)
4584 m
= gfc_match_name (name
);
4588 /* Get the right paren, and that's it because there could be the
4589 bind(c) attribute after the result clause. */
4590 if (gfc_match_char (')') != MATCH_YES
)
4592 /* TODO: should report the missing right paren here. */
4596 if (strcmp (function
->name
, name
) == 0)
4598 gfc_error ("RESULT variable at %C must be different than function name");
4602 if (gfc_get_symbol (name
, NULL
, &r
))
4605 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4614 /* Match a function suffix, which could be a combination of a result
4615 clause and BIND(C), either one, or neither. The draft does not
4616 require them to come in a specific order. */
4619 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4621 match is_bind_c
; /* Found bind(c). */
4622 match is_result
; /* Found result clause. */
4623 match found_match
; /* Status of whether we've found a good match. */
4624 char peek_char
; /* Character we're going to peek at. */
4625 bool allow_binding_name
;
4627 /* Initialize to having found nothing. */
4628 found_match
= MATCH_NO
;
4629 is_bind_c
= MATCH_NO
;
4630 is_result
= MATCH_NO
;
4632 /* Get the next char to narrow between result and bind(c). */
4633 gfc_gobble_whitespace ();
4634 peek_char
= gfc_peek_ascii_char ();
4636 /* C binding names are not allowed for internal procedures. */
4637 if (gfc_current_state () == COMP_CONTAINS
4638 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4639 allow_binding_name
= false;
4641 allow_binding_name
= true;
4646 /* Look for result clause. */
4647 is_result
= match_result (sym
, result
);
4648 if (is_result
== MATCH_YES
)
4650 /* Now see if there is a bind(c) after it. */
4651 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4652 /* We've found the result clause and possibly bind(c). */
4653 found_match
= MATCH_YES
;
4656 /* This should only be MATCH_ERROR. */
4657 found_match
= is_result
;
4660 /* Look for bind(c) first. */
4661 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4662 if (is_bind_c
== MATCH_YES
)
4664 /* Now see if a result clause followed it. */
4665 is_result
= match_result (sym
, result
);
4666 found_match
= MATCH_YES
;
4670 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4671 found_match
= MATCH_ERROR
;
4675 gfc_error ("Unexpected junk after function declaration at %C");
4676 found_match
= MATCH_ERROR
;
4680 if (is_bind_c
== MATCH_YES
)
4682 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4683 if (gfc_current_state () == COMP_CONTAINS
4684 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4685 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4686 "at %L may not be specified for an internal "
4687 "procedure", &gfc_current_locus
))
4690 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4698 /* Procedure pointer return value without RESULT statement:
4699 Add "hidden" result variable named "ppr@". */
4702 add_hidden_procptr_result (gfc_symbol
*sym
)
4706 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4709 /* First usage case: PROCEDURE and EXTERNAL statements. */
4710 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4711 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4712 && sym
->attr
.external
;
4713 /* Second usage case: INTERFACE statements. */
4714 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4715 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4716 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4722 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4726 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4727 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4728 st2
->n
.sym
= stree
->n
.sym
;
4730 sym
->result
= stree
->n
.sym
;
4732 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4733 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4734 sym
->result
->attr
.external
= sym
->attr
.external
;
4735 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4736 sym
->result
->ts
= sym
->ts
;
4737 sym
->attr
.proc_pointer
= 0;
4738 sym
->attr
.pointer
= 0;
4739 sym
->attr
.external
= 0;
4740 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4742 sym
->result
->attr
.pointer
= 0;
4743 sym
->result
->attr
.proc_pointer
= 1;
4746 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4748 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4749 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4750 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4751 && sym
== gfc_current_ns
->proc_name
4752 && sym
== sym
->result
->ns
->proc_name
4753 && strcmp ("ppr@", sym
->result
->name
) == 0)
4755 sym
->result
->attr
.proc_pointer
= 1;
4756 sym
->attr
.pointer
= 0;
4764 /* Match the interface for a PROCEDURE declaration,
4765 including brackets (R1212). */
4768 match_procedure_interface (gfc_symbol
**proc_if
)
4772 locus old_loc
, entry_loc
;
4773 gfc_namespace
*old_ns
= gfc_current_ns
;
4774 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4776 old_loc
= entry_loc
= gfc_current_locus
;
4777 gfc_clear_ts (¤t_ts
);
4779 if (gfc_match (" (") != MATCH_YES
)
4781 gfc_current_locus
= entry_loc
;
4785 /* Get the type spec. for the procedure interface. */
4786 old_loc
= gfc_current_locus
;
4787 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4788 gfc_gobble_whitespace ();
4789 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4792 if (m
== MATCH_ERROR
)
4795 /* Procedure interface is itself a procedure. */
4796 gfc_current_locus
= old_loc
;
4797 m
= gfc_match_name (name
);
4799 /* First look to see if it is already accessible in the current
4800 namespace because it is use associated or contained. */
4802 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4805 /* If it is still not found, then try the parent namespace, if it
4806 exists and create the symbol there if it is still not found. */
4807 if (gfc_current_ns
->parent
)
4808 gfc_current_ns
= gfc_current_ns
->parent
;
4809 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4812 gfc_current_ns
= old_ns
;
4813 *proc_if
= st
->n
.sym
;
4818 /* Resolve interface if possible. That way, attr.procedure is only set
4819 if it is declared by a later procedure-declaration-stmt, which is
4820 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4821 while ((*proc_if
)->ts
.interface
)
4822 *proc_if
= (*proc_if
)->ts
.interface
;
4824 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4825 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4826 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4827 (*proc_if
)->name
, NULL
))
4832 if (gfc_match (" )") != MATCH_YES
)
4834 gfc_current_locus
= entry_loc
;
4842 /* Match a PROCEDURE declaration (R1211). */
4845 match_procedure_decl (void)
4848 gfc_symbol
*sym
, *proc_if
= NULL
;
4850 gfc_expr
*initializer
= NULL
;
4852 /* Parse interface (with brackets). */
4853 m
= match_procedure_interface (&proc_if
);
4857 /* Parse attributes (with colons). */
4858 m
= match_attr_spec();
4859 if (m
== MATCH_ERROR
)
4862 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4864 current_attr
.is_bind_c
= 1;
4865 has_name_equals
= 0;
4866 curr_binding_label
= NULL
;
4869 /* Get procedure symbols. */
4872 m
= gfc_match_symbol (&sym
, 0);
4875 else if (m
== MATCH_ERROR
)
4878 /* Add current_attr to the symbol attributes. */
4879 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4882 if (sym
->attr
.is_bind_c
)
4884 /* Check for C1218. */
4885 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4887 gfc_error ("BIND(C) attribute at %C requires "
4888 "an interface with BIND(C)");
4891 /* Check for C1217. */
4892 if (has_name_equals
&& sym
->attr
.pointer
)
4894 gfc_error ("BIND(C) procedure with NAME may not have "
4895 "POINTER attribute at %C");
4898 if (has_name_equals
&& sym
->attr
.dummy
)
4900 gfc_error ("Dummy procedure at %C may not have "
4901 "BIND(C) attribute with NAME");
4904 /* Set binding label for BIND(C). */
4905 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
4909 if (!gfc_add_external (&sym
->attr
, NULL
))
4912 if (add_hidden_procptr_result (sym
))
4915 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
4918 /* Set interface. */
4919 if (proc_if
!= NULL
)
4921 if (sym
->ts
.type
!= BT_UNKNOWN
)
4923 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4924 sym
->name
, &gfc_current_locus
,
4925 gfc_basic_typename (sym
->ts
.type
));
4928 sym
->ts
.interface
= proc_if
;
4929 sym
->attr
.untyped
= 1;
4930 sym
->attr
.if_source
= IFSRC_IFBODY
;
4932 else if (current_ts
.type
!= BT_UNKNOWN
)
4934 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
4936 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4937 sym
->ts
.interface
->ts
= current_ts
;
4938 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4939 sym
->ts
.interface
->attr
.function
= 1;
4940 sym
->attr
.function
= 1;
4941 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4944 if (gfc_match (" =>") == MATCH_YES
)
4946 if (!current_attr
.pointer
)
4948 gfc_error ("Initialization at %C isn't for a pointer variable");
4953 m
= match_pointer_init (&initializer
, 1);
4957 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
4962 if (gfc_match_eos () == MATCH_YES
)
4964 if (gfc_match_char (',') != MATCH_YES
)
4969 gfc_error ("Syntax error in PROCEDURE statement at %C");
4973 /* Free stuff up and return. */
4974 gfc_free_expr (initializer
);
4980 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4983 /* Match a procedure pointer component declaration (R445). */
4986 match_ppc_decl (void)
4989 gfc_symbol
*proc_if
= NULL
;
4993 gfc_expr
*initializer
= NULL
;
4994 gfc_typebound_proc
* tb
;
4995 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4997 /* Parse interface (with brackets). */
4998 m
= match_procedure_interface (&proc_if
);
5002 /* Parse attributes. */
5003 tb
= XCNEW (gfc_typebound_proc
);
5004 tb
->where
= gfc_current_locus
;
5005 m
= match_binding_attributes (tb
, false, true);
5006 if (m
== MATCH_ERROR
)
5009 gfc_clear_attr (¤t_attr
);
5010 current_attr
.procedure
= 1;
5011 current_attr
.proc_pointer
= 1;
5012 current_attr
.access
= tb
->access
;
5013 current_attr
.flavor
= FL_PROCEDURE
;
5015 /* Match the colons (required). */
5016 if (gfc_match (" ::") != MATCH_YES
)
5018 gfc_error ("Expected '::' after binding-attributes at %C");
5022 /* Check for C450. */
5023 if (!tb
->nopass
&& proc_if
== NULL
)
5025 gfc_error("NOPASS or explicit interface required at %C");
5029 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5032 /* Match PPC names. */
5036 m
= gfc_match_name (name
);
5039 else if (m
== MATCH_ERROR
)
5042 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5045 /* Add current_attr to the symbol attributes. */
5046 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5049 if (!gfc_add_external (&c
->attr
, NULL
))
5052 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5059 c
->tb
= XCNEW (gfc_typebound_proc
);
5060 c
->tb
->where
= gfc_current_locus
;
5064 /* Set interface. */
5065 if (proc_if
!= NULL
)
5067 c
->ts
.interface
= proc_if
;
5068 c
->attr
.untyped
= 1;
5069 c
->attr
.if_source
= IFSRC_IFBODY
;
5071 else if (ts
.type
!= BT_UNKNOWN
)
5074 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5075 c
->ts
.interface
->result
= c
->ts
.interface
;
5076 c
->ts
.interface
->ts
= ts
;
5077 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5078 c
->ts
.interface
->attr
.function
= 1;
5079 c
->attr
.function
= 1;
5080 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5083 if (gfc_match (" =>") == MATCH_YES
)
5085 m
= match_pointer_init (&initializer
, 1);
5088 gfc_free_expr (initializer
);
5091 c
->initializer
= initializer
;
5094 if (gfc_match_eos () == MATCH_YES
)
5096 if (gfc_match_char (',') != MATCH_YES
)
5101 gfc_error ("Syntax error in procedure pointer component at %C");
5106 /* Match a PROCEDURE declaration inside an interface (R1206). */
5109 match_procedure_in_interface (void)
5113 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5116 if (current_interface
.type
== INTERFACE_NAMELESS
5117 || current_interface
.type
== INTERFACE_ABSTRACT
)
5119 gfc_error ("PROCEDURE at %C must be in a generic interface");
5123 /* Check if the F2008 optional double colon appears. */
5124 gfc_gobble_whitespace ();
5125 old_locus
= gfc_current_locus
;
5126 if (gfc_match ("::") == MATCH_YES
)
5128 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5129 "MODULE PROCEDURE statement at %L", &old_locus
))
5133 gfc_current_locus
= old_locus
;
5137 m
= gfc_match_name (name
);
5140 else if (m
== MATCH_ERROR
)
5142 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5145 if (!gfc_add_interface (sym
))
5148 if (gfc_match_eos () == MATCH_YES
)
5150 if (gfc_match_char (',') != MATCH_YES
)
5157 gfc_error ("Syntax error in PROCEDURE statement at %C");
5162 /* General matcher for PROCEDURE declarations. */
5164 static match
match_procedure_in_type (void);
5167 gfc_match_procedure (void)
5171 switch (gfc_current_state ())
5176 case COMP_SUBROUTINE
:
5179 m
= match_procedure_decl ();
5181 case COMP_INTERFACE
:
5182 m
= match_procedure_in_interface ();
5185 m
= match_ppc_decl ();
5187 case COMP_DERIVED_CONTAINS
:
5188 m
= match_procedure_in_type ();
5197 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5204 /* Warn if a matched procedure has the same name as an intrinsic; this is
5205 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5206 parser-state-stack to find out whether we're in a module. */
5209 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5213 in_module
= (gfc_state_stack
->previous
5214 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5216 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5220 /* Match a function declaration. */
5223 gfc_match_function_decl (void)
5225 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5226 gfc_symbol
*sym
, *result
;
5230 match found_match
; /* Status returned by match func. */
5232 if (gfc_current_state () != COMP_NONE
5233 && gfc_current_state () != COMP_INTERFACE
5234 && gfc_current_state () != COMP_CONTAINS
)
5237 gfc_clear_ts (¤t_ts
);
5239 old_loc
= gfc_current_locus
;
5241 m
= gfc_match_prefix (¤t_ts
);
5244 gfc_current_locus
= old_loc
;
5248 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5250 gfc_current_locus
= old_loc
;
5253 if (get_proc_name (name
, &sym
, false))
5256 if (add_hidden_procptr_result (sym
))
5259 gfc_new_block
= sym
;
5261 m
= gfc_match_formal_arglist (sym
, 0, 0);
5264 gfc_error ("Expected formal argument list in function "
5265 "definition at %C");
5269 else if (m
== MATCH_ERROR
)
5274 /* According to the draft, the bind(c) and result clause can
5275 come in either order after the formal_arg_list (i.e., either
5276 can be first, both can exist together or by themselves or neither
5277 one). Therefore, the match_result can't match the end of the
5278 string, and check for the bind(c) or result clause in either order. */
5279 found_match
= gfc_match_eos ();
5281 /* Make sure that it isn't already declared as BIND(C). If it is, it
5282 must have been marked BIND(C) with a BIND(C) attribute and that is
5283 not allowed for procedures. */
5284 if (sym
->attr
.is_bind_c
== 1)
5286 sym
->attr
.is_bind_c
= 0;
5287 if (sym
->old_symbol
!= NULL
)
5288 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5289 "variables or common blocks",
5290 &(sym
->old_symbol
->declared_at
));
5292 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5293 "variables or common blocks", &gfc_current_locus
);
5296 if (found_match
!= MATCH_YES
)
5298 /* If we haven't found the end-of-statement, look for a suffix. */
5299 suffix_match
= gfc_match_suffix (sym
, &result
);
5300 if (suffix_match
== MATCH_YES
)
5301 /* Need to get the eos now. */
5302 found_match
= gfc_match_eos ();
5304 found_match
= suffix_match
;
5307 if(found_match
!= MATCH_YES
)
5311 /* Make changes to the symbol. */
5314 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5317 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5318 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5321 /* Delay matching the function characteristics until after the
5322 specification block by signalling kind=-1. */
5323 sym
->declared_at
= old_loc
;
5324 if (current_ts
.type
!= BT_UNKNOWN
)
5325 current_ts
.kind
= -1;
5327 current_ts
.kind
= 0;
5331 if (current_ts
.type
!= BT_UNKNOWN
5332 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5338 if (current_ts
.type
!= BT_UNKNOWN
5339 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5341 sym
->result
= result
;
5344 /* Warn if this procedure has the same name as an intrinsic. */
5345 warn_intrinsic_shadow (sym
, true);
5351 gfc_current_locus
= old_loc
;
5356 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5357 pass the name of the entry, rather than the gfc_current_block name, and
5358 to return false upon finding an existing global entry. */
5361 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5365 enum gfc_symbol_type type
;
5367 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5369 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5370 name is a global identifier. */
5371 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5373 s
= gfc_get_gsymbol (name
);
5375 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5377 gfc_global_used (s
, where
);
5386 s
->ns
= gfc_current_ns
;
5390 /* Don't add the symbol multiple times. */
5392 && (!gfc_notification_std (GFC_STD_F2008
)
5393 || strcmp (name
, binding_label
) != 0))
5395 s
= gfc_get_gsymbol (binding_label
);
5397 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5399 gfc_global_used (s
, where
);
5406 s
->binding_label
= binding_label
;
5409 s
->ns
= gfc_current_ns
;
5417 /* Match an ENTRY statement. */
5420 gfc_match_entry (void)
5425 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5426 gfc_compile_state state
;
5430 bool module_procedure
;
5434 m
= gfc_match_name (name
);
5438 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5441 state
= gfc_current_state ();
5442 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5447 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5450 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5452 case COMP_BLOCK_DATA
:
5453 gfc_error ("ENTRY statement at %C cannot appear within "
5456 case COMP_INTERFACE
:
5457 gfc_error ("ENTRY statement at %C cannot appear within "
5461 gfc_error ("ENTRY statement at %C cannot appear within "
5462 "a DERIVED TYPE block");
5465 gfc_error ("ENTRY statement at %C cannot appear within "
5466 "an IF-THEN block");
5469 case COMP_DO_CONCURRENT
:
5470 gfc_error ("ENTRY statement at %C cannot appear within "
5474 gfc_error ("ENTRY statement at %C cannot appear within "
5478 gfc_error ("ENTRY statement at %C cannot appear within "
5482 gfc_error ("ENTRY statement at %C cannot appear within "
5486 gfc_error ("ENTRY statement at %C cannot appear within "
5487 "a contained subprogram");
5490 gfc_internal_error ("gfc_match_entry(): Bad state");
5495 module_procedure
= gfc_current_ns
->parent
!= NULL
5496 && gfc_current_ns
->parent
->proc_name
5497 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5500 if (gfc_current_ns
->parent
!= NULL
5501 && gfc_current_ns
->parent
->proc_name
5502 && !module_procedure
)
5504 gfc_error("ENTRY statement at %C cannot appear in a "
5505 "contained procedure");
5509 /* Module function entries need special care in get_proc_name
5510 because previous references within the function will have
5511 created symbols attached to the current namespace. */
5512 if (get_proc_name (name
, &entry
,
5513 gfc_current_ns
->parent
!= NULL
5514 && module_procedure
))
5517 proc
= gfc_current_block ();
5519 /* Make sure that it isn't already declared as BIND(C). If it is, it
5520 must have been marked BIND(C) with a BIND(C) attribute and that is
5521 not allowed for procedures. */
5522 if (entry
->attr
.is_bind_c
== 1)
5524 entry
->attr
.is_bind_c
= 0;
5525 if (entry
->old_symbol
!= NULL
)
5526 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5527 "variables or common blocks",
5528 &(entry
->old_symbol
->declared_at
));
5530 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5531 "variables or common blocks", &gfc_current_locus
);
5534 /* Check what next non-whitespace character is so we can tell if there
5535 is the required parens if we have a BIND(C). */
5536 old_loc
= gfc_current_locus
;
5537 gfc_gobble_whitespace ();
5538 peek_char
= gfc_peek_ascii_char ();
5540 if (state
== COMP_SUBROUTINE
)
5542 m
= gfc_match_formal_arglist (entry
, 0, 1);
5546 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5547 never be an internal procedure. */
5548 is_bind_c
= gfc_match_bind_c (entry
, true);
5549 if (is_bind_c
== MATCH_ERROR
)
5551 if (is_bind_c
== MATCH_YES
)
5553 if (peek_char
!= '(')
5555 gfc_error ("Missing required parentheses before BIND(C) at %C");
5558 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5559 &(entry
->declared_at
), 1))
5563 if (!gfc_current_ns
->parent
5564 && !add_global_entry (name
, entry
->binding_label
, true,
5568 /* An entry in a subroutine. */
5569 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5570 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5575 /* An entry in a function.
5576 We need to take special care because writing
5581 ENTRY f() RESULT (r)
5583 ENTRY f RESULT (r). */
5584 if (gfc_match_eos () == MATCH_YES
)
5586 gfc_current_locus
= old_loc
;
5587 /* Match the empty argument list, and add the interface to
5589 m
= gfc_match_formal_arglist (entry
, 0, 1);
5592 m
= gfc_match_formal_arglist (entry
, 0, 0);
5599 if (gfc_match_eos () == MATCH_YES
)
5601 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5602 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5605 entry
->result
= entry
;
5609 m
= gfc_match_suffix (entry
, &result
);
5611 gfc_syntax_error (ST_ENTRY
);
5617 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5618 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5619 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5621 entry
->result
= result
;
5625 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5626 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5628 entry
->result
= entry
;
5632 if (!gfc_current_ns
->parent
5633 && !add_global_entry (name
, entry
->binding_label
, false,
5638 if (gfc_match_eos () != MATCH_YES
)
5640 gfc_syntax_error (ST_ENTRY
);
5644 entry
->attr
.recursive
= proc
->attr
.recursive
;
5645 entry
->attr
.elemental
= proc
->attr
.elemental
;
5646 entry
->attr
.pure
= proc
->attr
.pure
;
5648 el
= gfc_get_entry_list ();
5650 el
->next
= gfc_current_ns
->entries
;
5651 gfc_current_ns
->entries
= el
;
5653 el
->id
= el
->next
->id
+ 1;
5657 new_st
.op
= EXEC_ENTRY
;
5658 new_st
.ext
.entry
= el
;
5664 /* Match a subroutine statement, including optional prefixes. */
5667 gfc_match_subroutine (void)
5669 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5674 bool allow_binding_name
;
5676 if (gfc_current_state () != COMP_NONE
5677 && gfc_current_state () != COMP_INTERFACE
5678 && gfc_current_state () != COMP_CONTAINS
)
5681 m
= gfc_match_prefix (NULL
);
5685 m
= gfc_match ("subroutine% %n", name
);
5689 if (get_proc_name (name
, &sym
, false))
5692 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5693 the symbol existed before. */
5694 sym
->declared_at
= gfc_current_locus
;
5696 if (add_hidden_procptr_result (sym
))
5699 gfc_new_block
= sym
;
5701 /* Check what next non-whitespace character is so we can tell if there
5702 is the required parens if we have a BIND(C). */
5703 gfc_gobble_whitespace ();
5704 peek_char
= gfc_peek_ascii_char ();
5706 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5709 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5712 /* Make sure that it isn't already declared as BIND(C). If it is, it
5713 must have been marked BIND(C) with a BIND(C) attribute and that is
5714 not allowed for procedures. */
5715 if (sym
->attr
.is_bind_c
== 1)
5717 sym
->attr
.is_bind_c
= 0;
5718 if (sym
->old_symbol
!= NULL
)
5719 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5720 "variables or common blocks",
5721 &(sym
->old_symbol
->declared_at
));
5723 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5724 "variables or common blocks", &gfc_current_locus
);
5727 /* C binding names are not allowed for internal procedures. */
5728 if (gfc_current_state () == COMP_CONTAINS
5729 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5730 allow_binding_name
= false;
5732 allow_binding_name
= true;
5734 /* Here, we are just checking if it has the bind(c) attribute, and if
5735 so, then we need to make sure it's all correct. If it doesn't,
5736 we still need to continue matching the rest of the subroutine line. */
5737 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5738 if (is_bind_c
== MATCH_ERROR
)
5740 /* There was an attempt at the bind(c), but it was wrong. An
5741 error message should have been printed w/in the gfc_match_bind_c
5742 so here we'll just return the MATCH_ERROR. */
5746 if (is_bind_c
== MATCH_YES
)
5748 /* The following is allowed in the Fortran 2008 draft. */
5749 if (gfc_current_state () == COMP_CONTAINS
5750 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5751 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5752 "at %L may not be specified for an internal "
5753 "procedure", &gfc_current_locus
))
5756 if (peek_char
!= '(')
5758 gfc_error ("Missing required parentheses before BIND(C) at %C");
5761 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5762 &(sym
->declared_at
), 1))
5766 if (gfc_match_eos () != MATCH_YES
)
5768 gfc_syntax_error (ST_SUBROUTINE
);
5772 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5775 /* Warn if it has the same name as an intrinsic. */
5776 warn_intrinsic_shadow (sym
, false);
5782 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5783 given, and set the binding label in either the given symbol (if not
5784 NULL), or in the current_ts. The symbol may be NULL because we may
5785 encounter the BIND(C) before the declaration itself. Return
5786 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5787 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5788 or MATCH_YES if the specifier was correct and the binding label and
5789 bind(c) fields were set correctly for the given symbol or the
5790 current_ts. If allow_binding_name is false, no binding name may be
5794 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5796 /* binding label, if exists */
5797 const char* binding_label
= NULL
;
5801 /* Initialize the flag that specifies whether we encountered a NAME=
5802 specifier or not. */
5803 has_name_equals
= 0;
5805 /* This much we have to be able to match, in this order, if
5806 there is a bind(c) label. */
5807 if (gfc_match (" bind ( c ") != MATCH_YES
)
5810 /* Now see if there is a binding label, or if we've reached the
5811 end of the bind(c) attribute without one. */
5812 if (gfc_match_char (',') == MATCH_YES
)
5814 if (gfc_match (" name = ") != MATCH_YES
)
5816 gfc_error ("Syntax error in NAME= specifier for binding label "
5818 /* should give an error message here */
5822 has_name_equals
= 1;
5824 /* Get the opening quote. */
5825 double_quote
= MATCH_YES
;
5826 single_quote
= MATCH_YES
;
5827 double_quote
= gfc_match_char ('"');
5828 if (double_quote
!= MATCH_YES
)
5829 single_quote
= gfc_match_char ('\'');
5830 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5832 gfc_error ("Syntax error in NAME= specifier for binding label "
5837 /* Grab the binding label, using functions that will not lower
5838 case the names automatically. */
5839 if (gfc_match_name_C (&binding_label
) != MATCH_YES
)
5842 /* Get the closing quotation. */
5843 if (double_quote
== MATCH_YES
)
5845 if (gfc_match_char ('"') != MATCH_YES
)
5847 gfc_error ("Missing closing quote '\"' for binding label at %C");
5848 /* User started string with '"' so looked to match it. */
5854 if (gfc_match_char ('\'') != MATCH_YES
)
5856 gfc_error ("Missing closing quote '\'' for binding label at %C");
5857 /* User started string with "'" char. */
5863 /* Get the required right paren. */
5864 if (gfc_match_char (')') != MATCH_YES
)
5866 gfc_error ("Missing closing paren for binding label at %C");
5870 if (has_name_equals
&& !allow_binding_name
)
5872 gfc_error ("No binding name is allowed in BIND(C) at %C");
5876 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5878 gfc_error ("For dummy procedure %s, no binding name is "
5879 "allowed in BIND(C) at %C", sym
->name
);
5884 /* Save the binding label to the symbol. If sym is null, we're
5885 probably matching the typespec attributes of a declaration and
5886 haven't gotten the name yet, and therefore, no symbol yet. */
5890 sym
->binding_label
= binding_label
;
5892 curr_binding_label
= binding_label
;
5894 else if (allow_binding_name
)
5896 /* No binding label, but if symbol isn't null, we
5897 can set the label for it here.
5898 If name="" or allow_binding_name is false, no C binding name is
5900 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5901 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
5904 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5905 && current_interface
.type
== INTERFACE_ABSTRACT
)
5907 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5915 /* Return nonzero if we're currently compiling a contained procedure. */
5918 contained_procedure (void)
5920 gfc_state_data
*s
= gfc_state_stack
;
5922 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5923 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5929 /* Set the kind of each enumerator. The kind is selected such that it is
5930 interoperable with the corresponding C enumeration type, making
5931 sure that -fshort-enums is honored. */
5936 enumerator_history
*current_history
= NULL
;
5940 if (max_enum
== NULL
|| enum_history
== NULL
)
5943 if (!flag_short_enums
)
5949 kind
= gfc_integer_kinds
[i
++].kind
;
5951 while (kind
< gfc_c_int_kind
5952 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5955 current_history
= enum_history
;
5956 while (current_history
!= NULL
)
5958 current_history
->sym
->ts
.kind
= kind
;
5959 current_history
= current_history
->next
;
5964 /* Match any of the various end-block statements. Returns the type of
5965 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5966 and END BLOCK statements cannot be replaced by a single END statement. */
5969 gfc_match_end (gfc_statement
*st
)
5971 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5972 gfc_compile_state state
;
5974 const char *block_name
;
5978 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
5979 gfc_namespace
**nsp
;
5981 old_loc
= gfc_current_locus
;
5982 if (gfc_match ("end") != MATCH_YES
)
5985 state
= gfc_current_state ();
5986 block_name
= gfc_current_block () == NULL
5987 ? NULL
: gfc_current_block ()->name
;
5991 case COMP_ASSOCIATE
:
5993 if (!strncmp (block_name
, "block@", strlen("block@")))
5998 case COMP_DERIVED_CONTAINS
:
5999 state
= gfc_state_stack
->previous
->state
;
6000 block_name
= gfc_state_stack
->previous
->sym
== NULL
6001 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6012 *st
= ST_END_PROGRAM
;
6013 target
= " program";
6017 case COMP_SUBROUTINE
:
6018 *st
= ST_END_SUBROUTINE
;
6019 target
= " subroutine";
6020 eos_ok
= !contained_procedure ();
6024 *st
= ST_END_FUNCTION
;
6025 target
= " function";
6026 eos_ok
= !contained_procedure ();
6029 case COMP_BLOCK_DATA
:
6030 *st
= ST_END_BLOCK_DATA
;
6031 target
= " block data";
6036 *st
= ST_END_MODULE
;
6041 case COMP_INTERFACE
:
6042 *st
= ST_END_INTERFACE
;
6043 target
= " interface";
6048 case COMP_DERIVED_CONTAINS
:
6054 case COMP_ASSOCIATE
:
6055 *st
= ST_END_ASSOCIATE
;
6056 target
= " associate";
6073 case COMP_DO_CONCURRENT
:
6080 *st
= ST_END_CRITICAL
;
6081 target
= " critical";
6086 case COMP_SELECT_TYPE
:
6087 *st
= ST_END_SELECT
;
6093 *st
= ST_END_FORALL
;
6108 last_initializer
= NULL
;
6110 gfc_free_enum_history ();
6114 gfc_error ("Unexpected END statement at %C");
6118 old_loc
= gfc_current_locus
;
6119 if (gfc_match_eos () == MATCH_YES
)
6121 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6123 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6124 "instead of %s statement at %L",
6125 gfc_ascii_statement(*st
), &old_loc
))
6130 /* We would have required END [something]. */
6131 gfc_error ("%s statement expected at %L",
6132 gfc_ascii_statement (*st
), &old_loc
);
6139 /* Verify that we've got the sort of end-block that we're expecting. */
6140 if (gfc_match (target
) != MATCH_YES
)
6142 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6147 old_loc
= gfc_current_locus
;
6148 /* If we're at the end, make sure a block name wasn't required. */
6149 if (gfc_match_eos () == MATCH_YES
)
6152 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6153 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6154 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6160 gfc_error ("Expected block name of '%s' in %s statement at %L",
6161 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6166 /* END INTERFACE has a special handler for its several possible endings. */
6167 if (*st
== ST_END_INTERFACE
)
6168 return gfc_match_end_interface ();
6170 /* We haven't hit the end of statement, so what is left must be an
6172 m
= gfc_match_space ();
6174 m
= gfc_match_name (name
);
6177 gfc_error ("Expected terminating name at %C");
6181 if (block_name
== NULL
)
6184 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6186 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
6187 gfc_ascii_statement (*st
));
6190 /* Procedure pointer as function result. */
6191 else if (strcmp (block_name
, "ppr@") == 0
6192 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6194 gfc_error ("Expected label '%s' for %s statement at %C",
6195 gfc_current_block ()->ns
->proc_name
->name
,
6196 gfc_ascii_statement (*st
));
6200 if (gfc_match_eos () == MATCH_YES
)
6204 gfc_syntax_error (*st
);
6207 gfc_current_locus
= old_loc
;
6209 /* If we are missing an END BLOCK, we created a half-ready namespace.
6210 Remove it from the parent namespace's sibling list. */
6212 if (state
== COMP_BLOCK
)
6214 parent_ns
= gfc_current_ns
->parent
;
6216 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6222 if (ns
== gfc_current_ns
)
6224 if (prev_ns
== NULL
)
6227 prev_ns
->sibling
= ns
->sibling
;
6233 gfc_free_namespace (gfc_current_ns
);
6234 gfc_current_ns
= parent_ns
;
6242 /***************** Attribute declaration statements ****************/
6244 /* Set the attribute of a single variable. */
6249 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6257 m
= gfc_match_name (name
);
6261 if (find_special (name
, &sym
, false))
6264 if (!check_function_name (name
))
6270 var_locus
= gfc_current_locus
;
6272 /* Deal with possible array specification for certain attributes. */
6273 if (current_attr
.dimension
6274 || current_attr
.codimension
6275 || current_attr
.allocatable
6276 || current_attr
.pointer
6277 || current_attr
.target
)
6279 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6280 !current_attr
.dimension
6281 && !current_attr
.pointer
6282 && !current_attr
.target
);
6283 if (m
== MATCH_ERROR
)
6286 if (current_attr
.dimension
&& m
== MATCH_NO
)
6288 gfc_error ("Missing array specification at %L in DIMENSION "
6289 "statement", &var_locus
);
6294 if (current_attr
.dimension
&& sym
->value
)
6296 gfc_error ("Dimensions specified for %s at %L after its "
6297 "initialisation", sym
->name
, &var_locus
);
6302 if (current_attr
.codimension
&& m
== MATCH_NO
)
6304 gfc_error ("Missing array specification at %L in CODIMENSION "
6305 "statement", &var_locus
);
6310 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6311 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6313 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6319 /* Update symbol table. DIMENSION attribute is set in
6320 gfc_set_array_spec(). For CLASS variables, this must be applied
6321 to the first component, or '_data' field. */
6322 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6324 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6332 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6333 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6340 if (sym
->ts
.type
== BT_CLASS
6341 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6347 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6353 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6355 /* Fix the array spec. */
6356 m
= gfc_mod_pointee_as (sym
->as
);
6357 if (m
== MATCH_ERROR
)
6361 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6367 if ((current_attr
.external
|| current_attr
.intrinsic
)
6368 && sym
->attr
.flavor
!= FL_PROCEDURE
6369 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6375 add_hidden_procptr_result (sym
);
6380 gfc_free_array_spec (as
);
6385 /* Generic attribute declaration subroutine. Used for attributes that
6386 just have a list of names. */
6393 /* Gobble the optional double colon, by simply ignoring the result
6403 if (gfc_match_eos () == MATCH_YES
)
6409 if (gfc_match_char (',') != MATCH_YES
)
6411 gfc_error ("Unexpected character in variable list at %C");
6421 /* This routine matches Cray Pointer declarations of the form:
6422 pointer ( <pointer>, <pointee> )
6424 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6425 The pointer, if already declared, should be an integer. Otherwise, we
6426 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6427 be either a scalar, or an array declaration. No space is allocated for
6428 the pointee. For the statement
6429 pointer (ipt, ar(10))
6430 any subsequent uses of ar will be translated (in C-notation) as
6431 ar(i) => ((<type> *) ipt)(i)
6432 After gimplification, pointee variable will disappear in the code. */
6435 cray_pointer_decl (void)
6438 gfc_array_spec
*as
= NULL
;
6439 gfc_symbol
*cptr
; /* Pointer symbol. */
6440 gfc_symbol
*cpte
; /* Pointee symbol. */
6446 if (gfc_match_char ('(') != MATCH_YES
)
6448 gfc_error ("Expected '(' at %C");
6452 /* Match pointer. */
6453 var_locus
= gfc_current_locus
;
6454 gfc_clear_attr (¤t_attr
);
6455 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6456 current_ts
.type
= BT_INTEGER
;
6457 current_ts
.kind
= gfc_index_integer_kind
;
6459 m
= gfc_match_symbol (&cptr
, 0);
6462 gfc_error ("Expected variable name at %C");
6466 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6469 gfc_set_sym_referenced (cptr
);
6471 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6473 cptr
->ts
.type
= BT_INTEGER
;
6474 cptr
->ts
.kind
= gfc_index_integer_kind
;
6476 else if (cptr
->ts
.type
!= BT_INTEGER
)
6478 gfc_error ("Cray pointer at %C must be an integer");
6481 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6482 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6483 " memory addresses require %d bytes",
6484 cptr
->ts
.kind
, gfc_index_integer_kind
);
6486 if (gfc_match_char (',') != MATCH_YES
)
6488 gfc_error ("Expected \",\" at %C");
6492 /* Match Pointee. */
6493 var_locus
= gfc_current_locus
;
6494 gfc_clear_attr (¤t_attr
);
6495 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6496 current_ts
.type
= BT_UNKNOWN
;
6497 current_ts
.kind
= 0;
6499 m
= gfc_match_symbol (&cpte
, 0);
6502 gfc_error ("Expected variable name at %C");
6506 /* Check for an optional array spec. */
6507 m
= gfc_match_array_spec (&as
, true, false);
6508 if (m
== MATCH_ERROR
)
6510 gfc_free_array_spec (as
);
6513 else if (m
== MATCH_NO
)
6515 gfc_free_array_spec (as
);
6519 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6522 gfc_set_sym_referenced (cpte
);
6524 if (cpte
->as
== NULL
)
6526 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6527 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6529 else if (as
!= NULL
)
6531 gfc_error ("Duplicate array spec for Cray pointee at %C");
6532 gfc_free_array_spec (as
);
6538 if (cpte
->as
!= NULL
)
6540 /* Fix array spec. */
6541 m
= gfc_mod_pointee_as (cpte
->as
);
6542 if (m
== MATCH_ERROR
)
6546 /* Point the Pointee at the Pointer. */
6547 cpte
->cp_pointer
= cptr
;
6549 if (gfc_match_char (')') != MATCH_YES
)
6551 gfc_error ("Expected \")\" at %C");
6554 m
= gfc_match_char (',');
6556 done
= true; /* Stop searching for more declarations. */
6560 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6561 || gfc_match_eos () != MATCH_YES
)
6563 gfc_error ("Expected \",\" or end of statement at %C");
6571 gfc_match_external (void)
6574 gfc_clear_attr (¤t_attr
);
6575 current_attr
.external
= 1;
6577 return attr_decl ();
6582 gfc_match_intent (void)
6586 /* This is not allowed within a BLOCK construct! */
6587 if (gfc_current_state () == COMP_BLOCK
)
6589 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6593 intent
= match_intent_spec ();
6594 if (intent
== INTENT_UNKNOWN
)
6597 gfc_clear_attr (¤t_attr
);
6598 current_attr
.intent
= intent
;
6600 return attr_decl ();
6605 gfc_match_intrinsic (void)
6608 gfc_clear_attr (¤t_attr
);
6609 current_attr
.intrinsic
= 1;
6611 return attr_decl ();
6616 gfc_match_optional (void)
6618 /* This is not allowed within a BLOCK construct! */
6619 if (gfc_current_state () == COMP_BLOCK
)
6621 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6625 gfc_clear_attr (¤t_attr
);
6626 current_attr
.optional
= 1;
6628 return attr_decl ();
6633 gfc_match_pointer (void)
6635 gfc_gobble_whitespace ();
6636 if (gfc_peek_ascii_char () == '(')
6638 if (!gfc_option
.flag_cray_pointer
)
6640 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6644 return cray_pointer_decl ();
6648 gfc_clear_attr (¤t_attr
);
6649 current_attr
.pointer
= 1;
6651 return attr_decl ();
6657 gfc_match_allocatable (void)
6659 gfc_clear_attr (¤t_attr
);
6660 current_attr
.allocatable
= 1;
6662 return attr_decl ();
6667 gfc_match_codimension (void)
6669 gfc_clear_attr (¤t_attr
);
6670 current_attr
.codimension
= 1;
6672 return attr_decl ();
6677 gfc_match_contiguous (void)
6679 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6682 gfc_clear_attr (¤t_attr
);
6683 current_attr
.contiguous
= 1;
6685 return attr_decl ();
6690 gfc_match_dimension (void)
6692 gfc_clear_attr (¤t_attr
);
6693 current_attr
.dimension
= 1;
6695 return attr_decl ();
6700 gfc_match_target (void)
6702 gfc_clear_attr (¤t_attr
);
6703 current_attr
.target
= 1;
6705 return attr_decl ();
6709 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6713 access_attr_decl (gfc_statement st
)
6715 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6716 interface_type type
;
6718 gfc_symbol
*sym
, *dt_sym
;
6719 gfc_intrinsic_op op
;
6722 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6727 m
= gfc_match_generic_spec (&type
, name
, &op
);
6730 if (m
== MATCH_ERROR
)
6735 case INTERFACE_NAMELESS
:
6736 case INTERFACE_ABSTRACT
:
6739 case INTERFACE_GENERIC
:
6740 if (gfc_get_symbol (name
, NULL
, &sym
))
6743 if (!gfc_add_access (&sym
->attr
,
6745 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6749 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6750 && !gfc_add_access (&dt_sym
->attr
,
6752 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6758 case INTERFACE_INTRINSIC_OP
:
6759 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6761 gfc_intrinsic_op other_op
;
6763 gfc_current_ns
->operator_access
[op
] =
6764 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6766 /* Handle the case if there is another op with the same
6767 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6768 other_op
= gfc_equivalent_op (op
);
6770 if (other_op
!= INTRINSIC_NONE
)
6771 gfc_current_ns
->operator_access
[other_op
] =
6772 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6777 gfc_error ("Access specification of the %s operator at %C has "
6778 "already been specified", gfc_op2string (op
));
6784 case INTERFACE_USER_OP
:
6785 uop
= gfc_get_uop (name
);
6787 if (uop
->access
== ACCESS_UNKNOWN
)
6789 uop
->access
= (st
== ST_PUBLIC
)
6790 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6794 gfc_error ("Access specification of the .%s. operator at %C "
6795 "has already been specified", sym
->name
);
6802 if (gfc_match_char (',') == MATCH_NO
)
6806 if (gfc_match_eos () != MATCH_YES
)
6811 gfc_syntax_error (st
);
6819 gfc_match_protected (void)
6824 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6826 gfc_error ("PROTECTED at %C only allowed in specification "
6827 "part of a module");
6832 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6835 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6840 if (gfc_match_eos () == MATCH_YES
)
6845 m
= gfc_match_symbol (&sym
, 0);
6849 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
6861 if (gfc_match_eos () == MATCH_YES
)
6863 if (gfc_match_char (',') != MATCH_YES
)
6870 gfc_error ("Syntax error in PROTECTED statement at %C");
6875 /* The PRIVATE statement is a bit weird in that it can be an attribute
6876 declaration, but also works as a standalone statement inside of a
6877 type declaration or a module. */
6880 gfc_match_private (gfc_statement
*st
)
6883 if (gfc_match ("private") != MATCH_YES
)
6886 if (gfc_current_state () != COMP_MODULE
6887 && !(gfc_current_state () == COMP_DERIVED
6888 && gfc_state_stack
->previous
6889 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6890 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6891 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6892 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6894 gfc_error ("PRIVATE statement at %C is only allowed in the "
6895 "specification part of a module");
6899 if (gfc_current_state () == COMP_DERIVED
)
6901 if (gfc_match_eos () == MATCH_YES
)
6907 gfc_syntax_error (ST_PRIVATE
);
6911 if (gfc_match_eos () == MATCH_YES
)
6918 return access_attr_decl (ST_PRIVATE
);
6923 gfc_match_public (gfc_statement
*st
)
6926 if (gfc_match ("public") != MATCH_YES
)
6929 if (gfc_current_state () != COMP_MODULE
)
6931 gfc_error ("PUBLIC statement at %C is only allowed in the "
6932 "specification part of a module");
6936 if (gfc_match_eos () == MATCH_YES
)
6943 return access_attr_decl (ST_PUBLIC
);
6947 /* Workhorse for gfc_match_parameter. */
6957 m
= gfc_match_symbol (&sym
, 0);
6959 gfc_error ("Expected variable name at %C in PARAMETER statement");
6964 if (gfc_match_char ('=') == MATCH_NO
)
6966 gfc_error ("Expected = sign in PARAMETER statement at %C");
6970 m
= gfc_match_init_expr (&init
);
6972 gfc_error ("Expected expression at %C in PARAMETER statement");
6976 if (sym
->ts
.type
== BT_UNKNOWN
6977 && !gfc_set_default_type (sym
, 1, NULL
))
6983 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
6984 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
6992 gfc_error ("Initializing already initialized variable at %C");
6997 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6998 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7001 gfc_free_expr (init
);
7006 /* Match a parameter statement, with the weird syntax that these have. */
7009 gfc_match_parameter (void)
7013 if (gfc_match_char ('(') == MATCH_NO
)
7022 if (gfc_match (" )%t") == MATCH_YES
)
7025 if (gfc_match_char (',') != MATCH_YES
)
7027 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7037 /* Save statements have a special syntax. */
7040 gfc_match_save (void)
7042 char n
[GFC_MAX_SYMBOL_LEN
+1];
7047 if (gfc_match_eos () == MATCH_YES
)
7049 if (gfc_current_ns
->seen_save
)
7051 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7052 "follows previous SAVE statement"))
7056 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7060 if (gfc_current_ns
->save_all
)
7062 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7063 "blanket SAVE statement"))
7071 m
= gfc_match_symbol (&sym
, 0);
7075 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7076 &gfc_current_locus
))
7087 m
= gfc_match (" / %n /", &n
);
7088 if (m
== MATCH_ERROR
)
7093 c
= gfc_get_common (n
, 0);
7096 gfc_current_ns
->seen_save
= 1;
7099 if (gfc_match_eos () == MATCH_YES
)
7101 if (gfc_match_char (',') != MATCH_YES
)
7108 gfc_error ("Syntax error in SAVE statement at %C");
7114 gfc_match_value (void)
7119 /* This is not allowed within a BLOCK construct! */
7120 if (gfc_current_state () == COMP_BLOCK
)
7122 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7126 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7129 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7134 if (gfc_match_eos () == MATCH_YES
)
7139 m
= gfc_match_symbol (&sym
, 0);
7143 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7155 if (gfc_match_eos () == MATCH_YES
)
7157 if (gfc_match_char (',') != MATCH_YES
)
7164 gfc_error ("Syntax error in VALUE statement at %C");
7170 gfc_match_volatile (void)
7175 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7178 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7183 if (gfc_match_eos () == MATCH_YES
)
7188 /* VOLATILE is special because it can be added to host-associated
7189 symbols locally. Except for coarrays. */
7190 m
= gfc_match_symbol (&sym
, 1);
7194 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7195 for variable in a BLOCK which is defined outside of the BLOCK. */
7196 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7198 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7199 "%C, which is use-/host-associated", sym
->name
);
7202 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7214 if (gfc_match_eos () == MATCH_YES
)
7216 if (gfc_match_char (',') != MATCH_YES
)
7223 gfc_error ("Syntax error in VOLATILE statement at %C");
7229 gfc_match_asynchronous (void)
7234 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7237 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7242 if (gfc_match_eos () == MATCH_YES
)
7247 /* ASYNCHRONOUS is special because it can be added to host-associated
7249 m
= gfc_match_symbol (&sym
, 1);
7253 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7265 if (gfc_match_eos () == MATCH_YES
)
7267 if (gfc_match_char (',') != MATCH_YES
)
7274 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7279 /* Match a module procedure statement. Note that we have to modify
7280 symbols in the parent's namespace because the current one was there
7281 to receive symbols that are in an interface's formal argument list. */
7284 gfc_match_modproc (void)
7286 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7290 gfc_namespace
*module_ns
;
7291 gfc_interface
*old_interface_head
, *interface
;
7293 if (gfc_state_stack
->state
!= COMP_INTERFACE
7294 || gfc_state_stack
->previous
== NULL
7295 || current_interface
.type
== INTERFACE_NAMELESS
7296 || current_interface
.type
== INTERFACE_ABSTRACT
)
7298 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7303 module_ns
= gfc_current_ns
->parent
;
7304 for (; module_ns
; module_ns
= module_ns
->parent
)
7305 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7306 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7307 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7308 && !module_ns
->proc_name
->attr
.contained
))
7311 if (module_ns
== NULL
)
7314 /* Store the current state of the interface. We will need it if we
7315 end up with a syntax error and need to recover. */
7316 old_interface_head
= gfc_current_interface_head ();
7318 /* Check if the F2008 optional double colon appears. */
7319 gfc_gobble_whitespace ();
7320 old_locus
= gfc_current_locus
;
7321 if (gfc_match ("::") == MATCH_YES
)
7323 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7324 "MODULE PROCEDURE statement at %L", &old_locus
))
7328 gfc_current_locus
= old_locus
;
7333 old_locus
= gfc_current_locus
;
7335 m
= gfc_match_name (name
);
7341 /* Check for syntax error before starting to add symbols to the
7342 current namespace. */
7343 if (gfc_match_eos () == MATCH_YES
)
7346 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7349 /* Now we're sure the syntax is valid, we process this item
7351 if (gfc_get_symbol (name
, module_ns
, &sym
))
7354 if (sym
->attr
.intrinsic
)
7356 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7357 "PROCEDURE", &old_locus
);
7361 if (sym
->attr
.proc
!= PROC_MODULE
7362 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7365 if (!gfc_add_interface (sym
))
7368 sym
->attr
.mod_proc
= 1;
7369 sym
->declared_at
= old_locus
;
7378 /* Restore the previous state of the interface. */
7379 interface
= gfc_current_interface_head ();
7380 gfc_set_current_interface_head (old_interface_head
);
7382 /* Free the new interfaces. */
7383 while (interface
!= old_interface_head
)
7385 gfc_interface
*i
= interface
->next
;
7390 /* And issue a syntax error. */
7391 gfc_syntax_error (ST_MODULE_PROC
);
7396 /* Check a derived type that is being extended. */
7399 check_extended_derived_type (char *name
)
7401 gfc_symbol
*extended
;
7403 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7405 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7409 extended
= gfc_find_dt_in_generic (extended
);
7414 gfc_error ("Symbol '%s' at %C has not been previously defined", name
);
7418 if (extended
->attr
.flavor
!= FL_DERIVED
)
7420 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7421 "derived type", name
);
7425 if (extended
->attr
.is_bind_c
)
7427 gfc_error ("'%s' cannot be extended at %C because it "
7428 "is BIND(C)", extended
->name
);
7432 if (extended
->attr
.sequence
)
7434 gfc_error ("'%s' cannot be extended at %C because it "
7435 "is a SEQUENCE type", extended
->name
);
7443 /* Match the optional attribute specifiers for a type declaration.
7444 Return MATCH_ERROR if an error is encountered in one of the handled
7445 attributes (public, private, bind(c)), MATCH_NO if what's found is
7446 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7447 checking on attribute conflicts needs to be done. */
7450 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7452 /* See if the derived type is marked as private. */
7453 if (gfc_match (" , private") == MATCH_YES
)
7455 if (gfc_current_state () != COMP_MODULE
)
7457 gfc_error ("Derived type at %C can only be PRIVATE in the "
7458 "specification part of a module");
7462 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7465 else if (gfc_match (" , public") == MATCH_YES
)
7467 if (gfc_current_state () != COMP_MODULE
)
7469 gfc_error ("Derived type at %C can only be PUBLIC in the "
7470 "specification part of a module");
7474 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7477 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7479 /* If the type is defined to be bind(c) it then needs to make
7480 sure that all fields are interoperable. This will
7481 need to be a semantic check on the finished derived type.
7482 See 15.2.3 (lines 9-12) of F2003 draft. */
7483 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7486 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7488 else if (gfc_match (" , abstract") == MATCH_YES
)
7490 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7493 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7496 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7498 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7504 /* If we get here, something matched. */
7509 /* Match the beginning of a derived type declaration. If a type name
7510 was the result of a function, then it is possible to have a symbol
7511 already to be known as a derived type yet have no components. */
7514 gfc_match_derived_decl (void)
7516 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7517 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7518 symbol_attribute attr
;
7519 gfc_symbol
*sym
, *gensym
;
7520 gfc_symbol
*extended
;
7522 match is_type_attr_spec
= MATCH_NO
;
7523 bool seen_attr
= false;
7524 gfc_interface
*intr
= NULL
, *head
;
7526 if (gfc_current_state () == COMP_DERIVED
)
7531 gfc_clear_attr (&attr
);
7536 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7537 if (is_type_attr_spec
== MATCH_ERROR
)
7539 if (is_type_attr_spec
== MATCH_YES
)
7541 } while (is_type_attr_spec
== MATCH_YES
);
7543 /* Deal with derived type extensions. The extension attribute has
7544 been added to 'attr' but now the parent type must be found and
7547 extended
= check_extended_derived_type (parent
);
7549 if (parent
[0] && !extended
)
7552 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7554 gfc_error ("Expected :: in TYPE definition at %C");
7558 m
= gfc_match (" %n%t", name
);
7562 /* Make sure the name is not the name of an intrinsic type. */
7563 if (gfc_is_intrinsic_typename (name
))
7565 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7570 if (gfc_get_symbol (name
, NULL
, &gensym
))
7573 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7575 gfc_error ("Derived type name '%s' at %C already has a basic type "
7576 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7580 if (!gensym
->attr
.generic
7581 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7584 if (!gensym
->attr
.function
7585 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7588 sym
= gfc_find_dt_in_generic (gensym
);
7590 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7592 gfc_error ("Derived type definition of '%s' at %C has already been "
7593 "defined", sym
->name
);
7599 /* Use upper case to save the actual derived-type symbol. */
7600 gfc_get_symbol (gfc_get_string ("%c%s",
7601 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7602 &gensym
->name
[1]), NULL
, &sym
);
7603 sym
->name
= gfc_get_string (gensym
->name
);
7604 head
= gensym
->generic
;
7605 intr
= gfc_get_interface ();
7607 intr
->where
= gfc_current_locus
;
7608 intr
->sym
->declared_at
= gfc_current_locus
;
7610 gensym
->generic
= intr
;
7611 gensym
->attr
.if_source
= IFSRC_DECL
;
7614 /* The symbol may already have the derived attribute without the
7615 components. The ways this can happen is via a function
7616 definition, an INTRINSIC statement or a subtype in another
7617 derived type that is a pointer. The first part of the AND clause
7618 is true if the symbol is not the return value of a function. */
7619 if (sym
->attr
.flavor
!= FL_DERIVED
7620 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7623 if (attr
.access
!= ACCESS_UNKNOWN
7624 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7626 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7627 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7628 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7632 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7633 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7634 gensym
->attr
.access
= sym
->attr
.access
;
7636 /* See if the derived type was labeled as bind(c). */
7637 if (attr
.is_bind_c
!= 0)
7638 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7640 /* Construct the f2k_derived namespace if it is not yet there. */
7641 if (!sym
->f2k_derived
)
7642 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7644 if (extended
&& !sym
->components
)
7649 /* Add the extended derived type as the first component. */
7650 gfc_add_component (sym
, parent
, &p
);
7652 gfc_set_sym_referenced (extended
);
7654 p
->ts
.type
= BT_DERIVED
;
7655 p
->ts
.u
.derived
= extended
;
7656 p
->initializer
= gfc_default_initializer (&p
->ts
);
7658 /* Set extension level. */
7659 if (extended
->attr
.extension
== 255)
7661 /* Since the extension field is 8 bit wide, we can only have
7662 up to 255 extension levels. */
7663 gfc_error ("Maximum extension level reached with type '%s' at %L",
7664 extended
->name
, &extended
->declared_at
);
7667 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7669 /* Provide the links between the extended type and its extension. */
7670 if (!extended
->f2k_derived
)
7671 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7672 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7676 if (!sym
->hash_value
)
7677 /* Set the hash for the compound name for this type. */
7678 sym
->hash_value
= gfc_hash_value (sym
);
7680 /* Take over the ABSTRACT attribute. */
7681 sym
->attr
.abstract
= attr
.abstract
;
7683 gfc_new_block
= sym
;
7689 /* Cray Pointees can be declared as:
7690 pointer (ipt, a (n,m,...,*)) */
7693 gfc_mod_pointee_as (gfc_array_spec
*as
)
7695 as
->cray_pointee
= true; /* This will be useful to know later. */
7696 if (as
->type
== AS_ASSUMED_SIZE
)
7697 as
->cp_was_assumed
= true;
7698 else if (as
->type
== AS_ASSUMED_SHAPE
)
7700 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7707 /* Match the enum definition statement, here we are trying to match
7708 the first line of enum definition statement.
7709 Returns MATCH_YES if match is found. */
7712 gfc_match_enum (void)
7716 m
= gfc_match_eos ();
7720 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7727 /* Returns an initializer whose value is one higher than the value of the
7728 LAST_INITIALIZER argument. If the argument is NULL, the
7729 initializers value will be set to zero. The initializer's kind
7730 will be set to gfc_c_int_kind.
7732 If -fshort-enums is given, the appropriate kind will be selected
7733 later after all enumerators have been parsed. A warning is issued
7734 here if an initializer exceeds gfc_c_int_kind. */
7737 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7740 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7742 mpz_init (result
->value
.integer
);
7744 if (last_initializer
!= NULL
)
7746 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7747 result
->where
= last_initializer
->where
;
7749 if (gfc_check_integer_range (result
->value
.integer
,
7750 gfc_c_int_kind
) != ARITH_OK
)
7752 gfc_error ("Enumerator exceeds the C integer type at %C");
7758 /* Control comes here, if it's the very first enumerator and no
7759 initializer has been given. It will be initialized to zero. */
7760 mpz_set_si (result
->value
.integer
, 0);
7767 /* Match a variable name with an optional initializer. When this
7768 subroutine is called, a variable is expected to be parsed next.
7769 Depending on what is happening at the moment, updates either the
7770 symbol table or the current interface. */
7773 enumerator_decl (void)
7775 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7776 gfc_expr
*initializer
;
7777 gfc_array_spec
*as
= NULL
;
7785 old_locus
= gfc_current_locus
;
7787 /* When we get here, we've just matched a list of attributes and
7788 maybe a type and a double colon. The next thing we expect to see
7789 is the name of the symbol. */
7790 m
= gfc_match_name (name
);
7794 var_locus
= gfc_current_locus
;
7796 /* OK, we've successfully matched the declaration. Now put the
7797 symbol in the current namespace. If we fail to create the symbol,
7799 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7805 /* The double colon must be present in order to have initializers.
7806 Otherwise the statement is ambiguous with an assignment statement. */
7809 if (gfc_match_char ('=') == MATCH_YES
)
7811 m
= gfc_match_init_expr (&initializer
);
7814 gfc_error ("Expected an initialization expression at %C");
7823 /* If we do not have an initializer, the initialization value of the
7824 previous enumerator (stored in last_initializer) is incremented
7825 by 1 and is used to initialize the current enumerator. */
7826 if (initializer
== NULL
)
7827 initializer
= enum_initializer (last_initializer
, old_locus
);
7829 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7831 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7837 /* Store this current initializer, for the next enumerator variable
7838 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7839 use last_initializer below. */
7840 last_initializer
= initializer
;
7841 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7843 /* Maintain enumerator history. */
7844 gfc_find_symbol (name
, NULL
, 0, &sym
);
7845 create_enum_history (sym
, last_initializer
);
7847 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7850 /* Free stuff up and return. */
7851 gfc_free_expr (initializer
);
7857 /* Match the enumerator definition statement. */
7860 gfc_match_enumerator_def (void)
7865 gfc_clear_ts (¤t_ts
);
7867 m
= gfc_match (" enumerator");
7871 m
= gfc_match (" :: ");
7872 if (m
== MATCH_ERROR
)
7875 colon_seen
= (m
== MATCH_YES
);
7877 if (gfc_current_state () != COMP_ENUM
)
7879 gfc_error ("ENUM definition statement expected before %C");
7880 gfc_free_enum_history ();
7884 (¤t_ts
)->type
= BT_INTEGER
;
7885 (¤t_ts
)->kind
= gfc_c_int_kind
;
7887 gfc_clear_attr (¤t_attr
);
7888 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7897 m
= enumerator_decl ();
7898 if (m
== MATCH_ERROR
)
7900 gfc_free_enum_history ();
7906 if (gfc_match_eos () == MATCH_YES
)
7908 if (gfc_match_char (',') != MATCH_YES
)
7912 if (gfc_current_state () == COMP_ENUM
)
7914 gfc_free_enum_history ();
7915 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7920 gfc_free_array_spec (current_as
);
7927 /* Match binding attributes. */
7930 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7932 bool found_passing
= false;
7933 bool seen_ptr
= false;
7934 match m
= MATCH_YES
;
7936 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7937 this case the defaults are in there. */
7938 ba
->access
= ACCESS_UNKNOWN
;
7939 ba
->pass_arg
= NULL
;
7940 ba
->pass_arg_num
= 0;
7942 ba
->non_overridable
= 0;
7946 /* If we find a comma, we believe there are binding attributes. */
7947 m
= gfc_match_char (',');
7953 /* Access specifier. */
7955 m
= gfc_match (" public");
7956 if (m
== MATCH_ERROR
)
7960 if (ba
->access
!= ACCESS_UNKNOWN
)
7962 gfc_error ("Duplicate access-specifier at %C");
7966 ba
->access
= ACCESS_PUBLIC
;
7970 m
= gfc_match (" private");
7971 if (m
== MATCH_ERROR
)
7975 if (ba
->access
!= ACCESS_UNKNOWN
)
7977 gfc_error ("Duplicate access-specifier at %C");
7981 ba
->access
= ACCESS_PRIVATE
;
7985 /* If inside GENERIC, the following is not allowed. */
7990 m
= gfc_match (" nopass");
7991 if (m
== MATCH_ERROR
)
7997 gfc_error ("Binding attributes already specify passing,"
7998 " illegal NOPASS at %C");
8002 found_passing
= true;
8007 /* PASS possibly including argument. */
8008 m
= gfc_match (" pass");
8009 if (m
== MATCH_ERROR
)
8013 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8017 gfc_error ("Binding attributes already specify passing,"
8018 " illegal PASS at %C");
8022 m
= gfc_match (" ( %n )", arg
);
8023 if (m
== MATCH_ERROR
)
8026 ba
->pass_arg
= gfc_get_string (arg
);
8027 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8029 found_passing
= true;
8037 m
= gfc_match (" pointer");
8038 if (m
== MATCH_ERROR
)
8044 gfc_error ("Duplicate POINTER attribute at %C");
8054 /* NON_OVERRIDABLE flag. */
8055 m
= gfc_match (" non_overridable");
8056 if (m
== MATCH_ERROR
)
8060 if (ba
->non_overridable
)
8062 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8066 ba
->non_overridable
= 1;
8070 /* DEFERRED flag. */
8071 m
= gfc_match (" deferred");
8072 if (m
== MATCH_ERROR
)
8078 gfc_error ("Duplicate DEFERRED at %C");
8089 /* Nothing matching found. */
8091 gfc_error ("Expected access-specifier at %C");
8093 gfc_error ("Expected binding attribute at %C");
8096 while (gfc_match_char (',') == MATCH_YES
);
8098 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8099 if (ba
->non_overridable
&& ba
->deferred
)
8101 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8108 if (ba
->access
== ACCESS_UNKNOWN
)
8109 ba
->access
= gfc_typebound_default_access
;
8111 if (ppc
&& !seen_ptr
)
8113 gfc_error ("POINTER attribute is required for procedure pointer component"
8125 /* Match a PROCEDURE specific binding inside a derived type. */
8128 match_procedure_in_type (void)
8130 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8131 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8132 char* target
= NULL
, *ifc
= NULL
;
8133 gfc_typebound_proc tb
;
8142 /* Check current state. */
8143 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8144 block
= gfc_state_stack
->previous
->sym
;
8147 /* Try to match PROCEDURE(interface). */
8148 if (gfc_match (" (") == MATCH_YES
)
8150 m
= gfc_match_name (target_buf
);
8151 if (m
== MATCH_ERROR
)
8155 gfc_error ("Interface-name expected after '(' at %C");
8159 if (gfc_match (" )") != MATCH_YES
)
8161 gfc_error ("')' expected at %C");
8168 /* Construct the data structure. */
8169 memset (&tb
, 0, sizeof (tb
));
8170 tb
.where
= gfc_current_locus
;
8172 /* Match binding attributes. */
8173 m
= match_binding_attributes (&tb
, false, false);
8174 if (m
== MATCH_ERROR
)
8176 seen_attrs
= (m
== MATCH_YES
);
8178 /* Check that attribute DEFERRED is given if an interface is specified. */
8179 if (tb
.deferred
&& !ifc
)
8181 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8184 if (ifc
&& !tb
.deferred
)
8186 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8190 /* Match the colons. */
8191 m
= gfc_match (" ::");
8192 if (m
== MATCH_ERROR
)
8194 seen_colons
= (m
== MATCH_YES
);
8195 if (seen_attrs
&& !seen_colons
)
8197 gfc_error ("Expected '::' after binding-attributes at %C");
8201 /* Match the binding names. */
8204 m
= gfc_match_name (name
);
8205 if (m
== MATCH_ERROR
)
8209 gfc_error ("Expected binding name at %C");
8213 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8216 /* Try to match the '=> target', if it's there. */
8218 m
= gfc_match (" =>");
8219 if (m
== MATCH_ERROR
)
8225 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8231 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8236 m
= gfc_match_name (target_buf
);
8237 if (m
== MATCH_ERROR
)
8241 gfc_error ("Expected binding target after '=>' at %C");
8244 target
= target_buf
;
8247 /* If no target was found, it has the same name as the binding. */
8251 /* Get the namespace to insert the symbols into. */
8252 ns
= block
->f2k_derived
;
8255 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8256 if (tb
.deferred
&& !block
->attr
.abstract
)
8258 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8259 "is not ABSTRACT", block
->name
);
8263 /* See if we already have a binding with this name in the symtree which
8264 would be an error. If a GENERIC already targeted this binding, it may
8265 be already there but then typebound is still NULL. */
8266 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8267 if (stree
&& stree
->n
.tb
)
8269 gfc_error ("There is already a procedure with binding name '%s' for "
8270 "the derived type '%s' at %C", name
, block
->name
);
8274 /* Insert it and set attributes. */
8278 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8281 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8283 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8286 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8288 if (gfc_match_eos () == MATCH_YES
)
8290 if (gfc_match_char (',') != MATCH_YES
)
8295 gfc_error ("Syntax error in PROCEDURE statement at %C");
8300 /* Match a GENERIC procedure binding inside a derived type. */
8303 gfc_match_generic (void)
8305 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8306 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8308 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8309 gfc_typebound_proc
* tb
;
8311 interface_type op_type
;
8312 gfc_intrinsic_op op
;
8315 /* Check current state. */
8316 if (gfc_current_state () == COMP_DERIVED
)
8318 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8321 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8323 block
= gfc_state_stack
->previous
->sym
;
8324 ns
= block
->f2k_derived
;
8325 gcc_assert (block
&& ns
);
8327 memset (&tbattr
, 0, sizeof (tbattr
));
8328 tbattr
.where
= gfc_current_locus
;
8330 /* See if we get an access-specifier. */
8331 m
= match_binding_attributes (&tbattr
, true, false);
8332 if (m
== MATCH_ERROR
)
8335 /* Now the colons, those are required. */
8336 if (gfc_match (" ::") != MATCH_YES
)
8338 gfc_error ("Expected '::' at %C");
8342 /* Match the binding name; depending on type (operator / generic) format
8343 it for future error messages into bind_name. */
8345 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8346 if (m
== MATCH_ERROR
)
8350 gfc_error ("Expected generic name or operator descriptor at %C");
8356 case INTERFACE_GENERIC
:
8357 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8360 case INTERFACE_USER_OP
:
8361 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8364 case INTERFACE_INTRINSIC_OP
:
8365 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8366 gfc_op2string (op
));
8373 /* Match the required =>. */
8374 if (gfc_match (" =>") != MATCH_YES
)
8376 gfc_error ("Expected '=>' at %C");
8380 /* Try to find existing GENERIC binding with this name / for this operator;
8381 if there is something, check that it is another GENERIC and then extend
8382 it rather than building a new node. Otherwise, create it and put it
8383 at the right position. */
8387 case INTERFACE_USER_OP
:
8388 case INTERFACE_GENERIC
:
8390 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8393 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8405 case INTERFACE_INTRINSIC_OP
:
8415 if (!tb
->is_generic
)
8417 gcc_assert (op_type
== INTERFACE_GENERIC
);
8418 gfc_error ("There's already a non-generic procedure with binding name"
8419 " '%s' for the derived type '%s' at %C",
8420 bind_name
, block
->name
);
8424 if (tb
->access
!= tbattr
.access
)
8426 gfc_error ("Binding at %C must have the same access as already"
8427 " defined binding '%s'", bind_name
);
8433 tb
= gfc_get_typebound_proc (NULL
);
8434 tb
->where
= gfc_current_locus
;
8435 tb
->access
= tbattr
.access
;
8437 tb
->u
.generic
= NULL
;
8441 case INTERFACE_GENERIC
:
8442 case INTERFACE_USER_OP
:
8444 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8447 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8455 case INTERFACE_INTRINSIC_OP
:
8464 /* Now, match all following names as specific targets. */
8467 gfc_symtree
* target_st
;
8468 gfc_tbp_generic
* target
;
8470 m
= gfc_match_name (name
);
8471 if (m
== MATCH_ERROR
)
8475 gfc_error ("Expected specific binding name at %C");
8479 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8481 /* See if this is a duplicate specification. */
8482 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8483 if (target_st
== target
->specific_st
)
8485 gfc_error ("'%s' already defined as specific binding for the"
8486 " generic '%s' at %C", name
, bind_name
);
8490 target
= gfc_get_tbp_generic ();
8491 target
->specific_st
= target_st
;
8492 target
->specific
= NULL
;
8493 target
->next
= tb
->u
.generic
;
8494 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8495 || (op_type
== INTERFACE_INTRINSIC_OP
));
8496 tb
->u
.generic
= target
;
8498 while (gfc_match (" ,") == MATCH_YES
);
8500 /* Here should be the end. */
8501 if (gfc_match_eos () != MATCH_YES
)
8503 gfc_error ("Junk after GENERIC binding at %C");
8514 /* Match a FINAL declaration inside a derived type. */
8517 gfc_match_final_decl (void)
8519 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8522 gfc_namespace
* module_ns
;
8526 if (gfc_current_form
== FORM_FREE
)
8528 char c
= gfc_peek_ascii_char ();
8529 if (!gfc_is_whitespace (c
) && c
!= ':')
8533 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8535 if (gfc_current_form
== FORM_FIXED
)
8538 gfc_error ("FINAL declaration at %C must be inside a derived type "
8539 "CONTAINS section");
8543 block
= gfc_state_stack
->previous
->sym
;
8546 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8547 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8549 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8550 " specification part of a MODULE");
8554 module_ns
= gfc_current_ns
;
8555 gcc_assert (module_ns
);
8556 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8558 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8559 if (gfc_match (" ::") == MATCH_ERROR
)
8562 /* Match the sequence of procedure names. */
8569 if (first
&& gfc_match_eos () == MATCH_YES
)
8571 gfc_error ("Empty FINAL at %C");
8575 m
= gfc_match_name (name
);
8578 gfc_error ("Expected module procedure name at %C");
8581 else if (m
!= MATCH_YES
)
8584 if (gfc_match_eos () == MATCH_YES
)
8586 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8588 gfc_error ("Expected ',' at %C");
8592 if (gfc_get_symbol (name
, module_ns
, &sym
))
8594 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8598 /* Mark the symbol as module procedure. */
8599 if (sym
->attr
.proc
!= PROC_MODULE
8600 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8603 /* Check if we already have this symbol in the list, this is an error. */
8604 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8605 if (f
->proc_sym
== sym
)
8607 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8612 /* Add this symbol to the list of finalizers. */
8613 gcc_assert (block
->f2k_derived
);
8615 f
= XCNEW (gfc_finalizer
);
8617 f
->proc_tree
= NULL
;
8618 f
->where
= gfc_current_locus
;
8619 f
->next
= block
->f2k_derived
->finalizers
;
8620 block
->f2k_derived
->finalizers
= f
;
8630 const ext_attr_t ext_attr_list
[] = {
8631 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8632 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8633 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8634 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8635 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8636 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8637 { NULL
, EXT_ATTR_LAST
, NULL
}
8640 /* Match a !GCC$ ATTRIBUTES statement of the form:
8641 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8642 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8644 TODO: We should support all GCC attributes using the same syntax for
8645 the attribute list, i.e. the list in C
8646 __attributes(( attribute-list ))
8648 !GCC$ ATTRIBUTES attribute-list ::
8649 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8652 As there is absolutely no risk of confusion, we should never return
8655 gfc_match_gcc_attributes (void)
8657 symbol_attribute attr
;
8658 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8663 gfc_clear_attr (&attr
);
8668 if (gfc_match_name (name
) != MATCH_YES
)
8671 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8672 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8675 if (id
== EXT_ATTR_LAST
)
8677 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8681 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8684 gfc_gobble_whitespace ();
8685 ch
= gfc_next_ascii_char ();
8688 /* This is the successful exit condition for the loop. */
8689 if (gfc_next_ascii_char () == ':')
8699 if (gfc_match_eos () == MATCH_YES
)
8704 m
= gfc_match_name (name
);
8708 if (find_special (name
, &sym
, true))
8711 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8713 if (gfc_match_eos () == MATCH_YES
)
8716 if (gfc_match_char (',') != MATCH_YES
)
8723 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");