1 /* Declaration statement matcher
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "constructor.h"
30 #include "stringpool.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector
;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts
;
54 static symbol_attribute current_attr
;
55 static gfc_array_spec
*current_as
;
56 static int colon_seen
;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label
;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line
;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals
= 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr
*last_initializer
;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
78 gfc_expr
*initializer
;
79 struct enumerator_history
*next
;
83 /* Header of enum history chain. */
85 static enumerator_history
*enum_history
= NULL
;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history
*max_enum
= NULL
;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol
*gfc_new_block
;
95 bool gfc_matching_function
;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data
= false;
103 gfc_in_match_data (void)
105 return in_match_data
;
109 set_in_match_data (bool set_value
)
111 in_match_data
= set_value
;
114 /* Free a gfc_data_variable structure and everything beneath it. */
117 free_variable (gfc_data_variable
*p
)
119 gfc_data_variable
*q
;
124 gfc_free_expr (p
->expr
);
125 gfc_free_iterator (&p
->iter
, 0);
126 free_variable (p
->list
);
132 /* Free a gfc_data_value structure and everything beneath it. */
135 free_value (gfc_data_value
*p
)
142 mpz_clear (p
->repeat
);
143 gfc_free_expr (p
->expr
);
149 /* Free a list of gfc_data structures. */
152 gfc_free_data (gfc_data
*p
)
159 free_variable (p
->var
);
160 free_value (p
->value
);
166 /* Free all data in a namespace. */
169 gfc_free_data_all (gfc_namespace
*ns
)
182 static match
var_element (gfc_data_variable
*);
184 /* Match a list of variables terminated by an iterator and a right
188 var_list (gfc_data_variable
*parent
)
190 gfc_data_variable
*tail
, var
;
193 m
= var_element (&var
);
194 if (m
== MATCH_ERROR
)
199 tail
= gfc_get_data_variable ();
206 if (gfc_match_char (',') != MATCH_YES
)
209 m
= gfc_match_iterator (&parent
->iter
, 1);
212 if (m
== MATCH_ERROR
)
215 m
= var_element (&var
);
216 if (m
== MATCH_ERROR
)
221 tail
->next
= gfc_get_data_variable ();
227 if (gfc_match_char (')') != MATCH_YES
)
232 gfc_syntax_error (ST_DATA
);
237 /* Match a single element in a data variable list, which can be a
238 variable-iterator list. */
241 var_element (gfc_data_variable
*new_var
)
246 memset (new_var
, 0, sizeof (gfc_data_variable
));
248 if (gfc_match_char ('(') == MATCH_YES
)
249 return var_list (new_var
);
251 m
= gfc_match_variable (&new_var
->expr
, 0);
255 sym
= new_var
->expr
->symtree
->n
.sym
;
257 /* Symbol should already have an associated type. */
258 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
261 if (!sym
->attr
.function
&& gfc_current_ns
->parent
262 && gfc_current_ns
->parent
== sym
->ns
)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym
->name
);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym
->attr
.in_common
271 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
272 "common block variable '%s' in DATA statement at %C",
276 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data
*d
)
288 gfc_data_variable var
, *tail
, *new_var
;
295 m
= var_element (&var
);
298 if (m
== MATCH_ERROR
)
301 new_var
= gfc_get_data_variable ();
307 tail
->next
= new_var
;
311 if (gfc_match_char ('/') == MATCH_YES
)
313 if (gfc_match_char (',') != MATCH_YES
)
320 gfc_syntax_error (ST_DATA
);
321 gfc_free_data_all (gfc_current_ns
);
327 match_data_constant (gfc_expr
**result
)
329 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
330 gfc_symbol
*sym
, *dt_sym
= NULL
;
335 m
= gfc_match_literal_constant (&expr
, 1);
342 if (m
== MATCH_ERROR
)
345 m
= gfc_match_null (result
);
349 old_loc
= gfc_current_locus
;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m
= gfc_match_rvalue (result
);
354 if (m
== MATCH_ERROR
)
357 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
359 if (!gfc_simplify_expr (*result
, 0))
363 else if (m
== MATCH_YES
)
364 gfc_free_expr (*result
);
366 gfc_current_locus
= old_loc
;
368 m
= gfc_match_name (name
);
372 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
375 if (sym
&& sym
->attr
.generic
)
376 dt_sym
= gfc_find_dt_in_generic (sym
);
379 || (sym
->attr
.flavor
!= FL_PARAMETER
380 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
382 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
386 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
387 return gfc_match_structure_constructor (dt_sym
, result
);
389 /* Check to see if the value is an initialization array expression. */
390 if (sym
->value
->expr_type
== EXPR_ARRAY
)
392 gfc_current_locus
= old_loc
;
394 m
= gfc_match_init_expr (result
);
395 if (m
== MATCH_ERROR
)
400 if (!gfc_simplify_expr (*result
, 0))
403 if ((*result
)->expr_type
== EXPR_CONSTANT
)
407 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
413 *result
= gfc_copy_expr (sym
->value
);
418 /* Match a list of values in a DATA statement. The leading '/' has
419 already been seen at this point. */
422 top_val_list (gfc_data
*data
)
424 gfc_data_value
*new_val
, *tail
;
432 m
= match_data_constant (&expr
);
435 if (m
== MATCH_ERROR
)
438 new_val
= gfc_get_data_value ();
439 mpz_init (new_val
->repeat
);
442 data
->value
= new_val
;
444 tail
->next
= new_val
;
448 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
451 mpz_set_ui (tail
->repeat
, 1);
455 mpz_set (tail
->repeat
, expr
->value
.integer
);
456 gfc_free_expr (expr
);
458 m
= match_data_constant (&tail
->expr
);
461 if (m
== MATCH_ERROR
)
465 if (gfc_match_char ('/') == MATCH_YES
)
467 if (gfc_match_char (',') == MATCH_NO
)
474 gfc_syntax_error (ST_DATA
);
475 gfc_free_data_all (gfc_current_ns
);
480 /* Matches an old style initialization. */
483 match_old_style_init (const char *name
)
490 /* Set up data structure to hold initializers. */
491 gfc_find_sym_tree (name
, NULL
, 0, &st
);
494 newdata
= gfc_get_data ();
495 newdata
->var
= gfc_get_data_variable ();
496 newdata
->var
->expr
= gfc_get_variable_expr (st
);
497 newdata
->where
= gfc_current_locus
;
499 /* Match initial value list. This also eats the terminal '/'. */
500 m
= top_val_list (newdata
);
509 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
514 if (gfc_implicit_pure (NULL
))
515 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
517 /* Mark the variable as having appeared in a data statement. */
518 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
524 /* Chain in namespace list of DATA initializers. */
525 newdata
->next
= gfc_current_ns
->data
;
526 gfc_current_ns
->data
= newdata
;
532 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
533 we are matching a DATA statement and are therefore issuing an error
534 if we encounter something unexpected, if not, we're trying to match
535 an old-style initialization expression of the form INTEGER I /2/. */
538 gfc_match_data (void)
543 set_in_match_data (true);
547 new_data
= gfc_get_data ();
548 new_data
->where
= gfc_current_locus
;
550 m
= top_var_list (new_data
);
554 m
= top_val_list (new_data
);
558 new_data
->next
= gfc_current_ns
->data
;
559 gfc_current_ns
->data
= new_data
;
561 if (gfc_match_eos () == MATCH_YES
)
564 gfc_match_char (','); /* Optional comma */
567 set_in_match_data (false);
571 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
575 if (gfc_implicit_pure (NULL
))
576 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
581 set_in_match_data (false);
582 gfc_free_data (new_data
);
587 /************************ Declaration statements *********************/
590 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
593 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
597 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
598 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
600 gfc_error ("The assumed-rank array at %C shall not have a codimension");
604 if (to
->rank
== 0 && from
->rank
> 0)
606 to
->rank
= from
->rank
;
607 to
->type
= from
->type
;
608 to
->cray_pointee
= from
->cray_pointee
;
609 to
->cp_was_assumed
= from
->cp_was_assumed
;
611 for (i
= 0; i
< to
->corank
; i
++)
613 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
614 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
616 for (i
= 0; i
< from
->rank
; i
++)
620 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
621 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
625 to
->lower
[i
] = from
->lower
[i
];
626 to
->upper
[i
] = from
->upper
[i
];
630 else if (to
->corank
== 0 && from
->corank
> 0)
632 to
->corank
= from
->corank
;
633 to
->cotype
= from
->cotype
;
635 for (i
= 0; i
< from
->corank
; i
++)
639 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
640 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
644 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
645 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
654 /* Match an intent specification. Since this can only happen after an
655 INTENT word, a legal intent-spec must follow. */
658 match_intent_spec (void)
661 if (gfc_match (" ( in out )") == MATCH_YES
)
663 if (gfc_match (" ( in )") == MATCH_YES
)
665 if (gfc_match (" ( out )") == MATCH_YES
)
668 gfc_error ("Bad INTENT specification at %C");
669 return INTENT_UNKNOWN
;
673 /* Matches a character length specification, which is either a
674 specification expression, '*', or ':'. */
677 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
684 if (gfc_match_char ('*') == MATCH_YES
)
687 if (gfc_match_char (':') == MATCH_YES
)
689 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
698 m
= gfc_match_expr (expr
);
701 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
704 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
706 if ((*expr
)->value
.function
.actual
707 && (*expr
)->value
.function
.actual
->expr
->symtree
)
710 e
= (*expr
)->value
.function
.actual
->expr
;
711 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
712 && e
->expr_type
== EXPR_VARIABLE
)
714 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
716 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
717 && e
->symtree
->n
.sym
->ts
.u
.cl
718 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
726 gfc_error ("Conflict in attributes of function argument at %C");
731 /* A character length is a '*' followed by a literal integer or a
732 char_len_param_value in parenthesis. */
735 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
741 m
= gfc_match_char ('*');
745 m
= gfc_match_small_literal_int (&length
, NULL
);
746 if (m
== MATCH_ERROR
)
751 if (obsolescent_check
752 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
754 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
758 if (gfc_match_char ('(') == MATCH_NO
)
761 m
= char_len_param_value (expr
, deferred
);
762 if (m
!= MATCH_YES
&& gfc_matching_function
)
768 if (m
== MATCH_ERROR
)
773 if (gfc_match_char (')') == MATCH_NO
)
775 gfc_free_expr (*expr
);
783 gfc_error ("Syntax error in character length specification at %C");
788 /* Special subroutine for finding a symbol. Check if the name is found
789 in the current name space. If not, and we're compiling a function or
790 subroutine and the parent compilation unit is an interface, then check
791 to see if the name we've been given is the name of the interface
792 (located in another namespace). */
795 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
801 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
804 *result
= st
? st
->n
.sym
: NULL
;
808 if (gfc_current_state () != COMP_SUBROUTINE
809 && gfc_current_state () != COMP_FUNCTION
)
812 s
= gfc_state_stack
->previous
;
816 if (s
->state
!= COMP_INTERFACE
)
819 goto end
; /* Nameless interface. */
821 if (strcmp (name
, s
->sym
->name
) == 0)
832 /* Special subroutine for getting a symbol node associated with a
833 procedure name, used in SUBROUTINE and FUNCTION statements. The
834 symbol is created in the parent using with symtree node in the
835 child unit pointing to the symbol. If the current namespace has no
836 parent, then the symbol is just created in the current unit. */
839 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
845 /* Module functions have to be left in their own namespace because
846 they have potentially (almost certainly!) already been referenced.
847 In this sense, they are rather like external functions. This is
848 fixed up in resolve.c(resolve_entries), where the symbol name-
849 space is set to point to the master function, so that the fake
850 result mechanism can work. */
851 if (module_fcn_entry
)
853 /* Present if entry is declared to be a module procedure. */
854 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
857 rc
= gfc_get_symbol (name
, NULL
, result
);
858 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
859 && (*result
)->ts
.type
== BT_UNKNOWN
860 && sym
->attr
.flavor
== FL_UNKNOWN
)
861 /* Pick up the typespec for the entry, if declared in the function
862 body. Note that this symbol is FL_UNKNOWN because it will
863 only have appeared in a type declaration. The local symtree
864 is set to point to the module symbol and a unique symtree
865 to the local version. This latter ensures a correct clearing
868 /* If the ENTRY proceeds its specification, we need to ensure
869 that this does not raise a "has no IMPLICIT type" error. */
870 if (sym
->ts
.type
== BT_UNKNOWN
)
871 sym
->attr
.untyped
= 1;
873 (*result
)->ts
= sym
->ts
;
875 /* Put the symbol in the procedure namespace so that, should
876 the ENTRY precede its specification, the specification
878 (*result
)->ns
= gfc_current_ns
;
880 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
882 st
= gfc_get_unique_symtree (gfc_current_ns
);
887 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
894 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
896 /* Trap another encompassed procedure with the same name. All
897 these conditions are necessary to avoid picking up an entry
898 whose name clashes with that of the encompassing procedure;
899 this is handled using gsymbols to register unique,globally
901 if (sym
->attr
.flavor
!= 0
902 && sym
->attr
.proc
!= 0
903 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
904 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
905 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
906 name
, &sym
->declared_at
);
908 /* Trap a procedure with a name the same as interface in the
909 encompassing scope. */
910 if (sym
->attr
.generic
!= 0
911 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
912 && !sym
->attr
.mod_proc
)
913 gfc_error_now ("Name '%s' at %C is already defined"
914 " as a generic interface at %L",
915 name
, &sym
->declared_at
);
917 /* Trap declarations of attributes in encompassing scope. The
918 signature for this is that ts.kind is set. Legitimate
919 references only set ts.type. */
920 if (sym
->ts
.kind
!= 0
921 && !sym
->attr
.implicit_type
922 && sym
->attr
.proc
== 0
923 && gfc_current_ns
->parent
!= NULL
924 && sym
->attr
.access
== 0
925 && !module_fcn_entry
)
926 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
927 "and must not have attributes declared at %L",
928 name
, &sym
->declared_at
);
931 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
934 /* Module function entries will already have a symtree in
935 the current namespace but will need one at module level. */
936 if (module_fcn_entry
)
938 /* Present if entry is declared to be a module procedure. */
939 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
941 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
944 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
949 /* See if the procedure should be a module procedure. */
951 if (((sym
->ns
->proc_name
!= NULL
952 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
953 && sym
->attr
.proc
!= PROC_MODULE
)
954 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
955 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
962 /* Verify that the given symbol representing a parameter is C
963 interoperable, by checking to see if it was marked as such after
964 its declaration. If the given symbol is not interoperable, a
965 warning is reported, thus removing the need to return the status to
966 the calling function. The standard does not require the user use
967 one of the iso_c_binding named constants to declare an
968 interoperable parameter, but we can't be sure if the param is C
969 interop or not if the user doesn't. For example, integer(4) may be
970 legal Fortran, but doesn't have meaning in C. It may interop with
971 a number of the C types, which causes a problem because the
972 compiler can't know which one. This code is almost certainly not
973 portable, and the user will get what they deserve if the C type
974 across platforms isn't always interoperable with integer(4). If
975 the user had used something like integer(c_int) or integer(c_long),
976 the compiler could have automatically handled the varying sizes
980 gfc_verify_c_interop_param (gfc_symbol
*sym
)
982 int is_c_interop
= 0;
985 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
986 Don't repeat the checks here. */
987 if (sym
->attr
.implicit_type
)
990 /* For subroutines or functions that are passed to a BIND(C) procedure,
991 they're interoperable if they're BIND(C) and their params are all
993 if (sym
->attr
.flavor
== FL_PROCEDURE
)
995 if (sym
->attr
.is_bind_c
== 0)
997 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
998 "attribute to be C interoperable", sym
->name
,
999 &(sym
->declared_at
));
1005 if (sym
->attr
.is_c_interop
== 1)
1006 /* We've already checked this procedure; don't check it again. */
1009 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1014 /* See if we've stored a reference to a procedure that owns sym. */
1015 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1017 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1019 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1021 if (is_c_interop
!= 1)
1023 /* Make personalized messages to give better feedback. */
1024 if (sym
->ts
.type
== BT_DERIVED
)
1025 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1026 "BIND(C) procedure '%s' but is not C interoperable "
1027 "because derived type '%s' is not C interoperable",
1028 sym
->name
, &(sym
->declared_at
),
1029 sym
->ns
->proc_name
->name
,
1030 sym
->ts
.u
.derived
->name
);
1031 else if (sym
->ts
.type
== BT_CLASS
)
1032 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1033 "BIND(C) procedure '%s' but is not C interoperable "
1034 "because it is polymorphic",
1035 sym
->name
, &(sym
->declared_at
),
1036 sym
->ns
->proc_name
->name
);
1037 else if (gfc_option
.warn_c_binding_type
)
1038 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1039 "BIND(C) procedure '%s' but may not be C "
1041 sym
->name
, &(sym
->declared_at
),
1042 sym
->ns
->proc_name
->name
);
1045 /* Character strings are only C interoperable if they have a
1047 if (sym
->ts
.type
== BT_CHARACTER
)
1049 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1050 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1051 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1053 gfc_error ("Character argument '%s' at %L "
1054 "must be length 1 because "
1055 "procedure '%s' is BIND(C)",
1056 sym
->name
, &sym
->declared_at
,
1057 sym
->ns
->proc_name
->name
);
1062 /* We have to make sure that any param to a bind(c) routine does
1063 not have the allocatable, pointer, or optional attributes,
1064 according to J3/04-007, section 5.1. */
1065 if (sym
->attr
.allocatable
== 1
1066 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1067 "ALLOCATABLE attribute in procedure '%s' "
1068 "with BIND(C)", sym
->name
,
1069 &(sym
->declared_at
),
1070 sym
->ns
->proc_name
->name
))
1073 if (sym
->attr
.pointer
== 1
1074 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' at %L with "
1075 "POINTER attribute in procedure '%s' "
1076 "with BIND(C)", sym
->name
,
1077 &(sym
->declared_at
),
1078 sym
->ns
->proc_name
->name
))
1081 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1083 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1084 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1085 " supported", sym
->name
, &(sym
->declared_at
),
1086 sym
->ns
->proc_name
->name
);
1090 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1092 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1093 "and the VALUE attribute because procedure '%s' "
1094 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1095 sym
->ns
->proc_name
->name
);
1098 else if (sym
->attr
.optional
== 1
1099 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable '%s' "
1100 "at %L with OPTIONAL attribute in "
1101 "procedure '%s' which is BIND(C)",
1102 sym
->name
, &(sym
->declared_at
),
1103 sym
->ns
->proc_name
->name
))
1106 /* Make sure that if it has the dimension attribute, that it is
1107 either assumed size or explicit shape. Deferred shape is already
1108 covered by the pointer/allocatable attribute. */
1109 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1110 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array '%s' "
1111 "at %L as dummy argument to the BIND(C) "
1112 "procedure '%s' at %L", sym
->name
,
1113 &(sym
->declared_at
),
1114 sym
->ns
->proc_name
->name
,
1115 &(sym
->ns
->proc_name
->declared_at
)))
1125 /* Function called by variable_decl() that adds a name to the symbol table. */
1128 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1129 gfc_array_spec
**as
, locus
*var_locus
)
1131 symbol_attribute attr
;
1134 if (gfc_get_symbol (name
, NULL
, &sym
))
1137 /* Start updating the symbol table. Add basic type attribute if present. */
1138 if (current_ts
.type
!= BT_UNKNOWN
1139 && (sym
->attr
.implicit_type
== 0
1140 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1141 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1144 if (sym
->ts
.type
== BT_CHARACTER
)
1147 sym
->ts
.deferred
= cl_deferred
;
1150 /* Add dimension attribute if present. */
1151 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1155 /* Add attribute to symbol. The copy is so that we can reset the
1156 dimension attribute. */
1157 attr
= current_attr
;
1159 attr
.codimension
= 0;
1161 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1164 /* Finish any work that may need to be done for the binding label,
1165 if it's a bind(c). The bind(c) attr is found before the symbol
1166 is made, and before the symbol name (for data decls), so the
1167 current_ts is holding the binding label, or nothing if the
1168 name= attr wasn't given. Therefore, test here if we're dealing
1169 with a bind(c) and make sure the binding label is set correctly. */
1170 if (sym
->attr
.is_bind_c
== 1)
1172 if (!sym
->binding_label
)
1174 /* Set the binding label and verify that if a NAME= was specified
1175 then only one identifier was in the entity-decl-list. */
1176 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1177 num_idents_on_line
))
1182 /* See if we know we're in a common block, and if it's a bind(c)
1183 common then we need to make sure we're an interoperable type. */
1184 if (sym
->attr
.in_common
== 1)
1186 /* Test the common block object. */
1187 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1188 && sym
->ts
.is_c_interop
!= 1)
1190 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1191 "must be declared with a C interoperable "
1192 "kind since common block '%s' is BIND(C)",
1193 sym
->name
, sym
->common_block
->name
,
1194 sym
->common_block
->name
);
1199 sym
->attr
.implied_index
= 0;
1201 if (sym
->ts
.type
== BT_CLASS
)
1202 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1208 /* Set character constant to the given length. The constant will be padded or
1209 truncated. If we're inside an array constructor without a typespec, we
1210 additionally check that all elements have the same length; check_len -1
1211 means no checking. */
1214 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1219 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1220 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1222 slen
= expr
->value
.character
.length
;
1225 s
= gfc_get_wide_string (len
+ 1);
1226 memcpy (s
, expr
->value
.character
.string
,
1227 MIN (len
, slen
) * sizeof (gfc_char_t
));
1229 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1231 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1232 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1233 "(%d/%d)", &expr
->where
, slen
, len
);
1235 /* Apply the standard by 'hand' otherwise it gets cleared for
1237 if (check_len
!= -1 && slen
!= check_len
1238 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1239 gfc_error_now ("The CHARACTER elements of the array constructor "
1240 "at %L must have the same length (%d/%d)",
1241 &expr
->where
, slen
, check_len
);
1244 free (expr
->value
.character
.string
);
1245 expr
->value
.character
.string
= s
;
1246 expr
->value
.character
.length
= len
;
1251 /* Function to create and update the enumerator history
1252 using the information passed as arguments.
1253 Pointer "max_enum" is also updated, to point to
1254 enum history node containing largest initializer.
1256 SYM points to the symbol node of enumerator.
1257 INIT points to its enumerator value. */
1260 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1262 enumerator_history
*new_enum_history
;
1263 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1265 new_enum_history
= XCNEW (enumerator_history
);
1267 new_enum_history
->sym
= sym
;
1268 new_enum_history
->initializer
= init
;
1269 new_enum_history
->next
= NULL
;
1271 if (enum_history
== NULL
)
1273 enum_history
= new_enum_history
;
1274 max_enum
= enum_history
;
1278 new_enum_history
->next
= enum_history
;
1279 enum_history
= new_enum_history
;
1281 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1282 new_enum_history
->initializer
->value
.integer
) < 0)
1283 max_enum
= new_enum_history
;
1288 /* Function to free enum kind history. */
1291 gfc_free_enum_history (void)
1293 enumerator_history
*current
= enum_history
;
1294 enumerator_history
*next
;
1296 while (current
!= NULL
)
1298 next
= current
->next
;
1303 enum_history
= NULL
;
1307 /* Function called by variable_decl() that adds an initialization
1308 expression to a symbol. */
1311 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1313 symbol_attribute attr
;
1318 if (find_special (name
, &sym
, false))
1323 /* If this symbol is confirming an implicit parameter type,
1324 then an initialization expression is not allowed. */
1325 if (attr
.flavor
== FL_PARAMETER
1326 && sym
->value
!= NULL
1329 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1336 /* An initializer is required for PARAMETER declarations. */
1337 if (attr
.flavor
== FL_PARAMETER
)
1339 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1345 /* If a variable appears in a DATA block, it cannot have an
1349 gfc_error ("Variable '%s' at %C with an initializer already "
1350 "appears in a DATA statement", sym
->name
);
1354 /* Check if the assignment can happen. This has to be put off
1355 until later for derived type variables and procedure pointers. */
1356 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1357 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1358 && !sym
->attr
.proc_pointer
1359 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1362 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1363 && init
->ts
.type
== BT_CHARACTER
)
1365 /* Update symbol character length according initializer. */
1366 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1369 if (sym
->ts
.u
.cl
->length
== NULL
)
1372 /* If there are multiple CHARACTER variables declared on the
1373 same line, we don't want them to share the same length. */
1374 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1376 if (sym
->attr
.flavor
== FL_PARAMETER
)
1378 if (init
->expr_type
== EXPR_CONSTANT
)
1380 clen
= init
->value
.character
.length
;
1381 sym
->ts
.u
.cl
->length
1382 = gfc_get_int_expr (gfc_default_integer_kind
,
1385 else if (init
->expr_type
== EXPR_ARRAY
)
1388 c
= gfc_constructor_first (init
->value
.constructor
);
1389 clen
= c
->expr
->value
.character
.length
;
1390 sym
->ts
.u
.cl
->length
1391 = gfc_get_int_expr (gfc_default_integer_kind
,
1394 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1395 sym
->ts
.u
.cl
->length
=
1396 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1399 /* Update initializer character length according symbol. */
1400 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1402 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1404 if (init
->expr_type
== EXPR_CONSTANT
)
1405 gfc_set_constant_character_len (len
, init
, -1);
1406 else if (init
->expr_type
== EXPR_ARRAY
)
1410 /* Build a new charlen to prevent simplification from
1411 deleting the length before it is resolved. */
1412 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1413 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1415 for (c
= gfc_constructor_first (init
->value
.constructor
);
1416 c
; c
= gfc_constructor_next (c
))
1417 gfc_set_constant_character_len (len
, c
->expr
, -1);
1422 /* If sym is implied-shape, set its upper bounds from init. */
1423 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1424 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1428 if (init
->rank
== 0)
1430 gfc_error ("Can't initialize implied-shape array at %L"
1431 " with scalar", &sym
->declared_at
);
1434 gcc_assert (sym
->as
->rank
== init
->rank
);
1436 /* Shape should be present, we get an initialization expression. */
1437 gcc_assert (init
->shape
);
1439 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1445 lower
= sym
->as
->lower
[dim
];
1446 if (lower
->expr_type
!= EXPR_CONSTANT
)
1448 gfc_error ("Non-constant lower bound in implied-shape"
1449 " declaration at %L", &lower
->where
);
1453 /* All dimensions must be without upper bound. */
1454 gcc_assert (!sym
->as
->upper
[dim
]);
1457 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1458 mpz_add (e
->value
.integer
,
1459 lower
->value
.integer
, init
->shape
[dim
]);
1460 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1461 sym
->as
->upper
[dim
] = e
;
1464 sym
->as
->type
= AS_EXPLICIT
;
1467 /* Need to check if the expression we initialized this
1468 to was one of the iso_c_binding named constants. If so,
1469 and we're a parameter (constant), let it be iso_c.
1471 integer(c_int), parameter :: my_int = c_int
1472 integer(my_int) :: my_int_2
1473 If we mark my_int as iso_c (since we can see it's value
1474 is equal to one of the named constants), then my_int_2
1475 will be considered C interoperable. */
1476 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1478 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1479 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1480 /* attr bits needed for module files. */
1481 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1482 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1483 if (init
->ts
.is_iso_c
)
1484 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1487 /* Add initializer. Make sure we keep the ranks sane. */
1488 if (sym
->attr
.dimension
&& init
->rank
== 0)
1493 if (sym
->attr
.flavor
== FL_PARAMETER
1494 && init
->expr_type
== EXPR_CONSTANT
1495 && spec_size (sym
->as
, &size
)
1496 && mpz_cmp_si (size
, 0) > 0)
1498 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1500 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1501 gfc_constructor_append_expr (&array
->value
.constructor
,
1504 : gfc_copy_expr (init
),
1507 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1508 for (n
= 0; n
< sym
->as
->rank
; n
++)
1509 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1514 init
->rank
= sym
->as
->rank
;
1518 if (sym
->attr
.save
== SAVE_NONE
)
1519 sym
->attr
.save
= SAVE_IMPLICIT
;
1527 /* Function called by variable_decl() that adds a name to a structure
1531 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1532 gfc_array_spec
**as
)
1537 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1538 constructing, it must have the pointer attribute. */
1539 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1540 && current_ts
.u
.derived
== gfc_current_block ()
1541 && current_attr
.pointer
== 0)
1543 gfc_error ("Component at %C must have the POINTER attribute");
1547 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1549 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1551 gfc_error ("Array component of structure at %C must have explicit "
1552 "or deferred shape");
1557 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1561 if (c
->ts
.type
== BT_CHARACTER
)
1563 c
->attr
= current_attr
;
1565 c
->initializer
= *init
;
1572 c
->attr
.codimension
= 1;
1574 c
->attr
.dimension
= 1;
1578 /* Should this ever get more complicated, combine with similar section
1579 in add_init_expr_to_sym into a separate function. */
1580 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1582 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1586 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1587 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1588 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1590 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1592 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1593 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1594 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1595 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1597 gfc_constructor
*ctor
;
1598 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1603 bool has_ts
= (c
->initializer
->ts
.u
.cl
1604 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1606 /* Remember the length of the first element for checking
1607 that all elements *in the constructor* have the same
1608 length. This need not be the length of the LHS! */
1609 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1610 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1611 first_len
= ctor
->expr
->value
.character
.length
;
1613 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1614 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1616 gfc_set_constant_character_len (len
, ctor
->expr
,
1617 has_ts
? -1 : first_len
);
1618 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1624 /* Check array components. */
1625 if (!c
->attr
.dimension
)
1628 if (c
->attr
.pointer
)
1630 if (c
->as
->type
!= AS_DEFERRED
)
1632 gfc_error ("Pointer array component of structure at %C must have a "
1637 else if (c
->attr
.allocatable
)
1639 if (c
->as
->type
!= AS_DEFERRED
)
1641 gfc_error ("Allocatable component of structure at %C must have a "
1648 if (c
->as
->type
!= AS_EXPLICIT
)
1650 gfc_error ("Array component of structure at %C must have an "
1657 if (c
->ts
.type
== BT_CLASS
)
1659 bool delayed
= (gfc_state_stack
->sym
== c
->ts
.u
.derived
)
1660 || (!c
->ts
.u
.derived
->components
1661 && !c
->ts
.u
.derived
->attr
.zero_comp
);
1662 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, delayed
);
1672 /* Match a 'NULL()', and possibly take care of some side effects. */
1675 gfc_match_null (gfc_expr
**result
)
1678 match m
, m2
= MATCH_NO
;
1680 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1686 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1688 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1691 old_loc
= gfc_current_locus
;
1692 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1695 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1699 gfc_current_locus
= old_loc
;
1704 /* The NULL symbol now has to be/become an intrinsic function. */
1705 if (gfc_get_symbol ("null", NULL
, &sym
))
1707 gfc_error ("NULL() initialization at %C is ambiguous");
1711 gfc_intrinsic_symbol (sym
);
1713 if (sym
->attr
.proc
!= PROC_INTRINSIC
1714 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1715 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1716 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1719 *result
= gfc_get_null_expr (&gfc_current_locus
);
1721 /* Invalid per F2008, C512. */
1722 if (m2
== MATCH_YES
)
1724 gfc_error ("NULL() initialization at %C may not have MOLD");
1732 /* Match the initialization expr for a data pointer or procedure pointer. */
1735 match_pointer_init (gfc_expr
**init
, int procptr
)
1739 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1741 gfc_error ("Initialization of pointer at %C is not allowed in "
1742 "a PURE procedure");
1746 /* Match NULL() initialization. */
1747 m
= gfc_match_null (init
);
1751 /* Match non-NULL initialization. */
1752 gfc_matching_ptr_assignment
= !procptr
;
1753 gfc_matching_procptr_assignment
= procptr
;
1754 m
= gfc_match_rvalue (init
);
1755 gfc_matching_ptr_assignment
= 0;
1756 gfc_matching_procptr_assignment
= 0;
1757 if (m
== MATCH_ERROR
)
1759 else if (m
== MATCH_NO
)
1761 gfc_error ("Error in pointer initialization at %C");
1766 gfc_resolve_expr (*init
);
1768 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1769 "initialization at %C"))
1777 check_function_name (char *name
)
1779 /* In functions that have a RESULT variable defined, the function name always
1780 refers to function calls. Therefore, the name is not allowed to appear in
1781 specification statements. When checking this, be careful about
1782 'hidden' procedure pointer results ('ppr@'). */
1784 if (gfc_current_state () == COMP_FUNCTION
)
1786 gfc_symbol
*block
= gfc_current_block ();
1787 if (block
&& block
->result
&& block
->result
!= block
1788 && strcmp (block
->result
->name
, "ppr@") != 0
1789 && strcmp (block
->name
, name
) == 0)
1791 gfc_error ("Function name '%s' not allowed at %C", name
);
1800 /* Match a variable name with an optional initializer. When this
1801 subroutine is called, a variable is expected to be parsed next.
1802 Depending on what is happening at the moment, updates either the
1803 symbol table or the current interface. */
1806 variable_decl (int elem
)
1808 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1809 gfc_expr
*initializer
, *char_len
;
1811 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1823 /* When we get here, we've just matched a list of attributes and
1824 maybe a type and a double colon. The next thing we expect to see
1825 is the name of the symbol. */
1826 m
= gfc_match_name (name
);
1830 var_locus
= gfc_current_locus
;
1832 /* Now we could see the optional array spec. or character length. */
1833 m
= gfc_match_array_spec (&as
, true, true);
1834 if (m
== MATCH_ERROR
)
1838 as
= gfc_copy_array_spec (current_as
);
1840 && !merge_array_spec (current_as
, as
, true))
1846 if (gfc_option
.flag_cray_pointer
)
1847 cp_as
= gfc_copy_array_spec (as
);
1849 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1850 determine (and check) whether it can be implied-shape. If it
1851 was parsed as assumed-size, change it because PARAMETERs can not
1855 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1858 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1863 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1864 && current_attr
.flavor
== FL_PARAMETER
)
1865 as
->type
= AS_IMPLIED_SHAPE
;
1867 if (as
->type
== AS_IMPLIED_SHAPE
1868 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1878 cl_deferred
= false;
1880 if (current_ts
.type
== BT_CHARACTER
)
1882 switch (match_char_length (&char_len
, &cl_deferred
, false))
1885 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1887 cl
->length
= char_len
;
1890 /* Non-constant lengths need to be copied after the first
1891 element. Also copy assumed lengths. */
1894 && (current_ts
.u
.cl
->length
== NULL
1895 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1897 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1898 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1901 cl
= current_ts
.u
.cl
;
1903 cl_deferred
= current_ts
.deferred
;
1912 /* If this symbol has already shown up in a Cray Pointer declaration,
1913 then we want to set the type & bail out. */
1914 if (gfc_option
.flag_cray_pointer
)
1916 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1917 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1919 sym
->ts
.type
= current_ts
.type
;
1920 sym
->ts
.kind
= current_ts
.kind
;
1922 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1923 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1924 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1927 /* Check to see if we have an array specification. */
1930 if (sym
->as
!= NULL
)
1932 gfc_error ("Duplicate array spec for Cray pointee at %C");
1933 gfc_free_array_spec (cp_as
);
1939 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1940 gfc_internal_error ("Couldn't set pointee array spec.");
1942 /* Fix the array spec. */
1943 m
= gfc_mod_pointee_as (sym
->as
);
1944 if (m
== MATCH_ERROR
)
1952 gfc_free_array_spec (cp_as
);
1956 /* Procedure pointer as function result. */
1957 if (gfc_current_state () == COMP_FUNCTION
1958 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1959 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1960 strcpy (name
, "ppr@");
1962 if (gfc_current_state () == COMP_FUNCTION
1963 && strcmp (name
, gfc_current_block ()->name
) == 0
1964 && gfc_current_block ()->result
1965 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1966 strcpy (name
, "ppr@");
1968 /* OK, we've successfully matched the declaration. Now put the
1969 symbol in the current namespace, because it might be used in the
1970 optional initialization expression for this symbol, e.g. this is
1973 integer, parameter :: i = huge(i)
1975 This is only true for parameters or variables of a basic type.
1976 For components of derived types, it is not true, so we don't
1977 create a symbol for those yet. If we fail to create the symbol,
1979 if (gfc_current_state () != COMP_DERIVED
1980 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
1986 if (!check_function_name (name
))
1992 /* We allow old-style initializations of the form
1993 integer i /2/, j(4) /3*3, 1/
1994 (if no colon has been seen). These are different from data
1995 statements in that initializers are only allowed to apply to the
1996 variable immediately preceding, i.e.
1998 is not allowed. Therefore we have to do some work manually, that
1999 could otherwise be left to the matchers for DATA statements. */
2001 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2003 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2004 "initialization at %C"))
2007 return match_old_style_init (name
);
2010 /* The double colon must be present in order to have initializers.
2011 Otherwise the statement is ambiguous with an assignment statement. */
2014 if (gfc_match (" =>") == MATCH_YES
)
2016 if (!current_attr
.pointer
)
2018 gfc_error ("Initialization at %C isn't for a pointer variable");
2023 m
= match_pointer_init (&initializer
, 0);
2027 else if (gfc_match_char ('=') == MATCH_YES
)
2029 if (current_attr
.pointer
)
2031 gfc_error ("Pointer initialization at %C requires '=>', "
2037 m
= gfc_match_init_expr (&initializer
);
2040 gfc_error ("Expected an initialization expression at %C");
2044 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2045 && gfc_state_stack
->state
!= COMP_DERIVED
)
2047 gfc_error ("Initialization of variable at %C is not allowed in "
2048 "a PURE procedure");
2057 if (initializer
!= NULL
&& current_attr
.allocatable
2058 && gfc_current_state () == COMP_DERIVED
)
2060 gfc_error ("Initialization of allocatable component at %C is not "
2066 /* Add the initializer. Note that it is fine if initializer is
2067 NULL here, because we sometimes also need to check if a
2068 declaration *must* have an initialization expression. */
2069 if (gfc_current_state () != COMP_DERIVED
)
2070 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2073 if (current_ts
.type
== BT_DERIVED
2074 && !current_attr
.pointer
&& !initializer
)
2075 initializer
= gfc_default_initializer (¤t_ts
);
2076 t
= build_struct (name
, cl
, &initializer
, &as
);
2079 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2082 /* Free stuff up and return. */
2083 gfc_free_expr (initializer
);
2084 gfc_free_array_spec (as
);
2090 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2091 This assumes that the byte size is equal to the kind number for
2092 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2095 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2100 if (gfc_match_char ('*') != MATCH_YES
)
2103 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2107 original_kind
= ts
->kind
;
2109 /* Massage the kind numbers for complex types. */
2110 if (ts
->type
== BT_COMPLEX
)
2114 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2115 gfc_basic_typename (ts
->type
), original_kind
);
2122 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2125 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2129 if (gfc_option
.flag_real4_kind
== 8)
2131 if (gfc_option
.flag_real4_kind
== 10)
2133 if (gfc_option
.flag_real4_kind
== 16)
2139 if (gfc_option
.flag_real8_kind
== 4)
2141 if (gfc_option
.flag_real8_kind
== 10)
2143 if (gfc_option
.flag_real8_kind
== 16)
2148 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2150 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2151 gfc_basic_typename (ts
->type
), original_kind
);
2155 if (!gfc_notify_std (GFC_STD_GNU
,
2156 "Nonstandard type declaration %s*%d at %C",
2157 gfc_basic_typename(ts
->type
), original_kind
))
2164 /* Match a kind specification. Since kinds are generally optional, we
2165 usually return MATCH_NO if something goes wrong. If a "kind="
2166 string is found, then we know we have an error. */
2169 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2181 where
= loc
= gfc_current_locus
;
2186 if (gfc_match_char ('(') == MATCH_NO
)
2189 /* Also gobbles optional text. */
2190 if (gfc_match (" kind = ") == MATCH_YES
)
2193 loc
= gfc_current_locus
;
2196 n
= gfc_match_init_expr (&e
);
2200 if (gfc_matching_function
)
2202 /* The function kind expression might include use associated or
2203 imported parameters and try again after the specification
2205 if (gfc_match_char (')') != MATCH_YES
)
2207 gfc_error ("Missing right parenthesis at %C");
2213 gfc_undo_symbols ();
2218 /* ....or else, the match is real. */
2220 gfc_error ("Expected initialization expression at %C");
2228 gfc_error ("Expected scalar initialization expression at %C");
2233 msg
= gfc_extract_int (e
, &ts
->kind
);
2242 /* Before throwing away the expression, let's see if we had a
2243 C interoperable kind (and store the fact). */
2244 if (e
->ts
.is_c_interop
== 1)
2246 /* Mark this as C interoperable if being declared with one
2247 of the named constants from iso_c_binding. */
2248 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2249 ts
->f90_type
= e
->ts
.f90_type
;
2255 /* Ignore errors to this point, if we've gotten here. This means
2256 we ignore the m=MATCH_ERROR from above. */
2257 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2259 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2260 gfc_basic_typename (ts
->type
));
2261 gfc_current_locus
= where
;
2265 /* Warn if, e.g., c_int is used for a REAL variable, but not
2266 if, e.g., c_double is used for COMPLEX as the standard
2267 explicitly says that the kind type parameter for complex and real
2268 variable is the same, i.e. c_float == c_float_complex. */
2269 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2270 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2271 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2272 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2273 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2274 gfc_basic_typename (ts
->type
));
2276 gfc_gobble_whitespace ();
2277 if ((c
= gfc_next_ascii_char ()) != ')'
2278 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2280 if (ts
->type
== BT_CHARACTER
)
2281 gfc_error ("Missing right parenthesis or comma at %C");
2283 gfc_error ("Missing right parenthesis at %C");
2287 /* All tests passed. */
2290 if(m
== MATCH_ERROR
)
2291 gfc_current_locus
= where
;
2293 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2296 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2300 if (gfc_option
.flag_real4_kind
== 8)
2302 if (gfc_option
.flag_real4_kind
== 10)
2304 if (gfc_option
.flag_real4_kind
== 16)
2310 if (gfc_option
.flag_real8_kind
== 4)
2312 if (gfc_option
.flag_real8_kind
== 10)
2314 if (gfc_option
.flag_real8_kind
== 16)
2319 /* Return what we know from the test(s). */
2324 gfc_current_locus
= where
;
2330 match_char_kind (int * kind
, int * is_iso_c
)
2339 where
= gfc_current_locus
;
2341 n
= gfc_match_init_expr (&e
);
2343 if (n
!= MATCH_YES
&& gfc_matching_function
)
2345 /* The expression might include use-associated or imported
2346 parameters and try again after the specification
2349 gfc_undo_symbols ();
2354 gfc_error ("Expected initialization expression at %C");
2360 gfc_error ("Expected scalar initialization expression at %C");
2365 msg
= gfc_extract_int (e
, kind
);
2366 *is_iso_c
= e
->ts
.is_iso_c
;
2376 /* Ignore errors to this point, if we've gotten here. This means
2377 we ignore the m=MATCH_ERROR from above. */
2378 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2380 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2384 /* All tests passed. */
2387 if (m
== MATCH_ERROR
)
2388 gfc_current_locus
= where
;
2390 /* Return what we know from the test(s). */
2395 gfc_current_locus
= where
;
2400 /* Match the various kind/length specifications in a CHARACTER
2401 declaration. We don't return MATCH_NO. */
2404 gfc_match_char_spec (gfc_typespec
*ts
)
2406 int kind
, seen_length
, is_iso_c
;
2418 /* Try the old-style specification first. */
2419 old_char_selector
= 0;
2421 m
= match_char_length (&len
, &deferred
, true);
2425 old_char_selector
= 1;
2430 m
= gfc_match_char ('(');
2433 m
= MATCH_YES
; /* Character without length is a single char. */
2437 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2438 if (gfc_match (" kind =") == MATCH_YES
)
2440 m
= match_char_kind (&kind
, &is_iso_c
);
2442 if (m
== MATCH_ERROR
)
2447 if (gfc_match (" , len =") == MATCH_NO
)
2450 m
= char_len_param_value (&len
, &deferred
);
2453 if (m
== MATCH_ERROR
)
2460 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2461 if (gfc_match (" len =") == MATCH_YES
)
2463 m
= char_len_param_value (&len
, &deferred
);
2466 if (m
== MATCH_ERROR
)
2470 if (gfc_match_char (')') == MATCH_YES
)
2473 if (gfc_match (" , kind =") != MATCH_YES
)
2476 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2482 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2483 m
= char_len_param_value (&len
, &deferred
);
2486 if (m
== MATCH_ERROR
)
2490 m
= gfc_match_char (')');
2494 if (gfc_match_char (',') != MATCH_YES
)
2497 gfc_match (" kind ="); /* Gobble optional text. */
2499 m
= match_char_kind (&kind
, &is_iso_c
);
2500 if (m
== MATCH_ERROR
)
2506 /* Require a right-paren at this point. */
2507 m
= gfc_match_char (')');
2512 gfc_error ("Syntax error in CHARACTER declaration at %C");
2514 gfc_free_expr (len
);
2518 /* Deal with character functions after USE and IMPORT statements. */
2519 if (gfc_matching_function
)
2521 gfc_free_expr (len
);
2522 gfc_undo_symbols ();
2528 gfc_free_expr (len
);
2532 /* Do some final massaging of the length values. */
2533 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2535 if (seen_length
== 0)
2536 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2541 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2542 ts
->deferred
= deferred
;
2544 /* We have to know if it was a C interoperable kind so we can
2545 do accurate type checking of bind(c) procs, etc. */
2547 /* Mark this as C interoperable if being declared with one
2548 of the named constants from iso_c_binding. */
2549 ts
->is_c_interop
= is_iso_c
;
2550 else if (len
!= NULL
)
2551 /* Here, we might have parsed something such as: character(c_char)
2552 In this case, the parsing code above grabs the c_char when
2553 looking for the length (line 1690, roughly). it's the last
2554 testcase for parsing the kind params of a character variable.
2555 However, it's not actually the length. this seems like it
2557 To see if the user used a C interop kind, test the expr
2558 of the so called length, and see if it's C interoperable. */
2559 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2565 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2566 structure to the matched specification. This is necessary for FUNCTION and
2567 IMPLICIT statements.
2569 If implicit_flag is nonzero, then we don't check for the optional
2570 kind specification. Not doing so is needed for matching an IMPLICIT
2571 statement correctly. */
2574 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2576 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2577 gfc_symbol
*sym
, *dt_sym
;
2580 bool seen_deferred_kind
, matched_type
;
2581 const char *dt_name
;
2583 /* A belt and braces check that the typespec is correctly being treated
2584 as a deferred characteristic association. */
2585 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2586 && (gfc_current_block ()->result
->ts
.kind
== -1)
2587 && (ts
->kind
== -1);
2589 if (seen_deferred_kind
)
2592 /* Clear the current binding label, in case one is given. */
2593 curr_binding_label
= NULL
;
2595 if (gfc_match (" byte") == MATCH_YES
)
2597 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2600 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2602 gfc_error ("BYTE type used at %C "
2603 "is not available on the target machine");
2607 ts
->type
= BT_INTEGER
;
2613 m
= gfc_match (" type (");
2614 matched_type
= (m
== MATCH_YES
);
2617 gfc_gobble_whitespace ();
2618 if (gfc_peek_ascii_char () == '*')
2620 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2622 if (gfc_current_state () == COMP_DERIVED
)
2624 gfc_error ("Assumed type at %C is not allowed for components");
2627 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2630 ts
->type
= BT_ASSUMED
;
2634 m
= gfc_match ("%n", name
);
2635 matched_type
= (m
== MATCH_YES
);
2638 if ((matched_type
&& strcmp ("integer", name
) == 0)
2639 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2641 ts
->type
= BT_INTEGER
;
2642 ts
->kind
= gfc_default_integer_kind
;
2646 if ((matched_type
&& strcmp ("character", name
) == 0)
2647 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2650 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2651 "intrinsic-type-spec at %C"))
2654 ts
->type
= BT_CHARACTER
;
2655 if (implicit_flag
== 0)
2656 m
= gfc_match_char_spec (ts
);
2660 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2666 if ((matched_type
&& strcmp ("real", name
) == 0)
2667 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2670 ts
->kind
= gfc_default_real_kind
;
2675 && (strcmp ("doubleprecision", name
) == 0
2676 || (strcmp ("double", name
) == 0
2677 && gfc_match (" precision") == MATCH_YES
)))
2678 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2681 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2682 "intrinsic-type-spec at %C"))
2684 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2688 ts
->kind
= gfc_default_double_kind
;
2692 if ((matched_type
&& strcmp ("complex", name
) == 0)
2693 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2695 ts
->type
= BT_COMPLEX
;
2696 ts
->kind
= gfc_default_complex_kind
;
2701 && (strcmp ("doublecomplex", name
) == 0
2702 || (strcmp ("double", name
) == 0
2703 && gfc_match (" complex") == MATCH_YES
)))
2704 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2706 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2710 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2711 "intrinsic-type-spec at %C"))
2714 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2717 ts
->type
= BT_COMPLEX
;
2718 ts
->kind
= gfc_default_double_kind
;
2722 if ((matched_type
&& strcmp ("logical", name
) == 0)
2723 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2725 ts
->type
= BT_LOGICAL
;
2726 ts
->kind
= gfc_default_logical_kind
;
2731 m
= gfc_match_char (')');
2734 ts
->type
= BT_DERIVED
;
2737 /* Match CLASS declarations. */
2738 m
= gfc_match (" class ( * )");
2739 if (m
== MATCH_ERROR
)
2741 else if (m
== MATCH_YES
)
2745 ts
->type
= BT_CLASS
;
2746 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2749 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2750 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2752 gfc_set_sym_referenced (upe
);
2754 upe
->ts
.type
= BT_VOID
;
2755 upe
->attr
.unlimited_polymorphic
= 1;
2756 /* This is essential to force the construction of
2757 unlimited polymorphic component class containers. */
2758 upe
->attr
.zero_comp
= 1;
2759 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2760 &gfc_current_locus
))
2765 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2767 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2771 ts
->u
.derived
= upe
;
2775 m
= gfc_match (" class ( %n )", name
);
2778 ts
->type
= BT_CLASS
;
2780 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2784 /* Defer association of the derived type until the end of the
2785 specification block. However, if the derived type can be
2786 found, add it to the typespec. */
2787 if (gfc_matching_function
)
2789 ts
->u
.derived
= NULL
;
2790 if (gfc_current_state () != COMP_INTERFACE
2791 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2793 sym
= gfc_find_dt_in_generic (sym
);
2794 ts
->u
.derived
= sym
;
2799 /* Search for the name but allow the components to be defined later. If
2800 type = -1, this typespec has been seen in a function declaration but
2801 the type could not be accessed at that point. The actual derived type is
2802 stored in a symtree with the first letter of the name capitalized; the
2803 symtree with the all lower-case name contains the associated
2804 generic function. */
2805 dt_name
= gfc_get_string ("%c%s",
2806 (char) TOUPPER ((unsigned char) name
[0]),
2807 (const char*)&name
[1]);
2812 gfc_get_ha_symbol (name
, &sym
);
2813 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2815 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2818 if (sym
->generic
&& !dt_sym
)
2819 dt_sym
= gfc_find_dt_in_generic (sym
);
2821 else if (ts
->kind
== -1)
2823 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2824 || gfc_current_ns
->has_import_set
;
2825 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2826 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2828 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2831 if (sym
&& sym
->generic
&& !dt_sym
)
2832 dt_sym
= gfc_find_dt_in_generic (sym
);
2839 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2840 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2841 || sym
->attr
.subroutine
)
2843 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2844 "entity at %L, which has the same name", name
,
2849 gfc_set_sym_referenced (sym
);
2850 if (!sym
->attr
.generic
2851 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2854 if (!sym
->attr
.function
2855 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2860 gfc_interface
*intr
, *head
;
2862 /* Use upper case to save the actual derived-type symbol. */
2863 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2864 dt_sym
->name
= gfc_get_string (sym
->name
);
2865 head
= sym
->generic
;
2866 intr
= gfc_get_interface ();
2868 intr
->where
= gfc_current_locus
;
2870 sym
->generic
= intr
;
2871 sym
->attr
.if_source
= IFSRC_DECL
;
2874 gfc_set_sym_referenced (dt_sym
);
2876 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2877 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2880 ts
->u
.derived
= dt_sym
;
2886 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2887 "intrinsic-type-spec at %C"))
2890 /* For all types except double, derived and character, look for an
2891 optional kind specifier. MATCH_NO is actually OK at this point. */
2892 if (implicit_flag
== 1)
2894 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2900 if (gfc_current_form
== FORM_FREE
)
2902 c
= gfc_peek_ascii_char ();
2903 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2904 && c
!= ':' && c
!= ',')
2906 if (matched_type
&& c
== ')')
2908 gfc_next_ascii_char ();
2915 m
= gfc_match_kind_spec (ts
, false);
2916 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2917 m
= gfc_match_old_kind_spec (ts
);
2919 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2922 /* Defer association of the KIND expression of function results
2923 until after USE and IMPORT statements. */
2924 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2925 || gfc_matching_function
)
2929 m
= MATCH_YES
; /* No kind specifier found. */
2935 /* Match an IMPLICIT NONE statement. Actually, this statement is
2936 already matched in parse.c, or we would not end up here in the
2937 first place. So the only thing we need to check, is if there is
2938 trailing garbage. If not, the match is successful. */
2941 gfc_match_implicit_none (void)
2943 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2947 /* Match the letter range(s) of an IMPLICIT statement. */
2950 match_implicit_range (void)
2956 cur_loc
= gfc_current_locus
;
2958 gfc_gobble_whitespace ();
2959 c
= gfc_next_ascii_char ();
2962 gfc_error ("Missing character range in IMPLICIT at %C");
2969 gfc_gobble_whitespace ();
2970 c1
= gfc_next_ascii_char ();
2974 gfc_gobble_whitespace ();
2975 c
= gfc_next_ascii_char ();
2980 inner
= 0; /* Fall through. */
2987 gfc_gobble_whitespace ();
2988 c2
= gfc_next_ascii_char ();
2992 gfc_gobble_whitespace ();
2993 c
= gfc_next_ascii_char ();
2995 if ((c
!= ',') && (c
!= ')'))
3008 gfc_error ("Letters must be in alphabetic order in "
3009 "IMPLICIT statement at %C");
3013 /* See if we can add the newly matched range to the pending
3014 implicits from this IMPLICIT statement. We do not check for
3015 conflicts with whatever earlier IMPLICIT statements may have
3016 set. This is done when we've successfully finished matching
3018 if (!gfc_add_new_implicit_range (c1
, c2
))
3025 gfc_syntax_error (ST_IMPLICIT
);
3027 gfc_current_locus
= cur_loc
;
3032 /* Match an IMPLICIT statement, storing the types for
3033 gfc_set_implicit() if the statement is accepted by the parser.
3034 There is a strange looking, but legal syntactic construction
3035 possible. It looks like:
3037 IMPLICIT INTEGER (a-b) (c-d)
3039 This is legal if "a-b" is a constant expression that happens to
3040 equal one of the legal kinds for integers. The real problem
3041 happens with an implicit specification that looks like:
3043 IMPLICIT INTEGER (a-b)
3045 In this case, a typespec matcher that is "greedy" (as most of the
3046 matchers are) gobbles the character range as a kindspec, leaving
3047 nothing left. We therefore have to go a bit more slowly in the
3048 matching process by inhibiting the kindspec checking during
3049 typespec matching and checking for a kind later. */
3052 gfc_match_implicit (void)
3061 /* We don't allow empty implicit statements. */
3062 if (gfc_match_eos () == MATCH_YES
)
3064 gfc_error ("Empty IMPLICIT statement at %C");
3070 /* First cleanup. */
3071 gfc_clear_new_implicit ();
3073 /* A basic type is mandatory here. */
3074 m
= gfc_match_decl_type_spec (&ts
, 1);
3075 if (m
== MATCH_ERROR
)
3080 cur_loc
= gfc_current_locus
;
3081 m
= match_implicit_range ();
3085 /* We may have <TYPE> (<RANGE>). */
3086 gfc_gobble_whitespace ();
3087 c
= gfc_next_ascii_char ();
3088 if ((c
== '\n') || (c
== ','))
3090 /* Check for CHARACTER with no length parameter. */
3091 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3093 ts
.kind
= gfc_default_character_kind
;
3094 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3095 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3099 /* Record the Successful match. */
3100 if (!gfc_merge_new_implicit (&ts
))
3105 gfc_current_locus
= cur_loc
;
3108 /* Discard the (incorrectly) matched range. */
3109 gfc_clear_new_implicit ();
3111 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3112 if (ts
.type
== BT_CHARACTER
)
3113 m
= gfc_match_char_spec (&ts
);
3116 m
= gfc_match_kind_spec (&ts
, false);
3119 m
= gfc_match_old_kind_spec (&ts
);
3120 if (m
== MATCH_ERROR
)
3126 if (m
== MATCH_ERROR
)
3129 m
= match_implicit_range ();
3130 if (m
== MATCH_ERROR
)
3135 gfc_gobble_whitespace ();
3136 c
= gfc_next_ascii_char ();
3137 if ((c
!= '\n') && (c
!= ','))
3140 if (!gfc_merge_new_implicit (&ts
))
3148 gfc_syntax_error (ST_IMPLICIT
);
3156 gfc_match_import (void)
3158 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3163 if (gfc_current_ns
->proc_name
== NULL
3164 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3166 gfc_error ("IMPORT statement at %C only permitted in "
3167 "an INTERFACE body");
3171 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3174 if (gfc_match_eos () == MATCH_YES
)
3176 /* All host variables should be imported. */
3177 gfc_current_ns
->has_import_set
= 1;
3181 if (gfc_match (" ::") == MATCH_YES
)
3183 if (gfc_match_eos () == MATCH_YES
)
3185 gfc_error ("Expecting list of named entities at %C");
3193 m
= gfc_match (" %n", name
);
3197 if (gfc_current_ns
->parent
!= NULL
3198 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3200 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3203 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3204 && gfc_find_symbol (name
,
3205 gfc_current_ns
->proc_name
->ns
->parent
,
3208 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3214 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3215 "at %C - does not exist.", name
);
3219 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3221 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3226 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3229 sym
->attr
.imported
= 1;
3231 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3233 /* The actual derived type is stored in a symtree with the first
3234 letter of the name capitalized; the symtree with the all
3235 lower-case name contains the associated generic function. */
3236 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3237 gfc_get_string ("%c%s",
3238 (char) TOUPPER ((unsigned char) name
[0]),
3242 sym
->attr
.imported
= 1;
3255 if (gfc_match_eos () == MATCH_YES
)
3257 if (gfc_match_char (',') != MATCH_YES
)
3264 gfc_error ("Syntax error in IMPORT statement at %C");
3269 /* A minimal implementation of gfc_match without whitespace, escape
3270 characters or variable arguments. Returns true if the next
3271 characters match the TARGET template exactly. */
3274 match_string_p (const char *target
)
3278 for (p
= target
; *p
; p
++)
3279 if ((char) gfc_next_ascii_char () != *p
)
3284 /* Matches an attribute specification including array specs. If
3285 successful, leaves the variables current_attr and current_as
3286 holding the specification. Also sets the colon_seen variable for
3287 later use by matchers associated with initializations.
3289 This subroutine is a little tricky in the sense that we don't know
3290 if we really have an attr-spec until we hit the double colon.
3291 Until that time, we can only return MATCH_NO. This forces us to
3292 check for duplicate specification at this level. */
3295 match_attr_spec (void)
3297 /* Modifiers that can exist in a type statement. */
3299 { GFC_DECL_BEGIN
= 0,
3300 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3301 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3302 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3303 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3304 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3305 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3308 /* GFC_DECL_END is the sentinel, index starts at 0. */
3309 #define NUM_DECL GFC_DECL_END
3311 locus start
, seen_at
[NUM_DECL
];
3318 gfc_clear_attr (¤t_attr
);
3319 start
= gfc_current_locus
;
3324 /* See if we get all of the keywords up to the final double colon. */
3325 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3333 gfc_gobble_whitespace ();
3335 ch
= gfc_next_ascii_char ();
3338 /* This is the successful exit condition for the loop. */
3339 if (gfc_next_ascii_char () == ':')
3344 gfc_gobble_whitespace ();
3345 switch (gfc_peek_ascii_char ())
3348 gfc_next_ascii_char ();
3349 switch (gfc_next_ascii_char ())
3352 if (match_string_p ("locatable"))
3354 /* Matched "allocatable". */
3355 d
= DECL_ALLOCATABLE
;
3360 if (match_string_p ("ynchronous"))
3362 /* Matched "asynchronous". */
3363 d
= DECL_ASYNCHRONOUS
;
3370 /* Try and match the bind(c). */
3371 m
= gfc_match_bind_c (NULL
, true);
3374 else if (m
== MATCH_ERROR
)
3379 gfc_next_ascii_char ();
3380 if ('o' != gfc_next_ascii_char ())
3382 switch (gfc_next_ascii_char ())
3385 if (match_string_p ("imension"))
3387 d
= DECL_CODIMENSION
;
3391 if (match_string_p ("tiguous"))
3393 d
= DECL_CONTIGUOUS
;
3400 if (match_string_p ("dimension"))
3405 if (match_string_p ("external"))
3410 if (match_string_p ("int"))
3412 ch
= gfc_next_ascii_char ();
3415 if (match_string_p ("nt"))
3417 /* Matched "intent". */
3418 /* TODO: Call match_intent_spec from here. */
3419 if (gfc_match (" ( in out )") == MATCH_YES
)
3421 else if (gfc_match (" ( in )") == MATCH_YES
)
3423 else if (gfc_match (" ( out )") == MATCH_YES
)
3429 if (match_string_p ("insic"))
3431 /* Matched "intrinsic". */
3439 if (match_string_p ("optional"))
3444 gfc_next_ascii_char ();
3445 switch (gfc_next_ascii_char ())
3448 if (match_string_p ("rameter"))
3450 /* Matched "parameter". */
3456 if (match_string_p ("inter"))
3458 /* Matched "pointer". */
3464 ch
= gfc_next_ascii_char ();
3467 if (match_string_p ("vate"))
3469 /* Matched "private". */
3475 if (match_string_p ("tected"))
3477 /* Matched "protected". */
3484 if (match_string_p ("blic"))
3486 /* Matched "public". */
3494 if (match_string_p ("save"))
3499 if (match_string_p ("target"))
3504 gfc_next_ascii_char ();
3505 ch
= gfc_next_ascii_char ();
3508 if (match_string_p ("lue"))
3510 /* Matched "value". */
3516 if (match_string_p ("latile"))
3518 /* Matched "volatile". */
3526 /* No double colon and no recognizable decl_type, so assume that
3527 we've been looking at something else the whole time. */
3534 /* Check to make sure any parens are paired up correctly. */
3535 if (gfc_match_parens () == MATCH_ERROR
)
3542 seen_at
[d
] = gfc_current_locus
;
3544 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3546 gfc_array_spec
*as
= NULL
;
3548 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3549 d
== DECL_CODIMENSION
);
3551 if (current_as
== NULL
)
3553 else if (m
== MATCH_YES
)
3555 if (!merge_array_spec (as
, current_as
, false))
3562 if (d
== DECL_CODIMENSION
)
3563 gfc_error ("Missing codimension specification at %C");
3565 gfc_error ("Missing dimension specification at %C");
3569 if (m
== MATCH_ERROR
)
3574 /* Since we've seen a double colon, we have to be looking at an
3575 attr-spec. This means that we can now issue errors. */
3576 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3581 case DECL_ALLOCATABLE
:
3582 attr
= "ALLOCATABLE";
3584 case DECL_ASYNCHRONOUS
:
3585 attr
= "ASYNCHRONOUS";
3587 case DECL_CODIMENSION
:
3588 attr
= "CODIMENSION";
3590 case DECL_CONTIGUOUS
:
3591 attr
= "CONTIGUOUS";
3593 case DECL_DIMENSION
:
3600 attr
= "INTENT (IN)";
3603 attr
= "INTENT (OUT)";
3606 attr
= "INTENT (IN OUT)";
3608 case DECL_INTRINSIC
:
3614 case DECL_PARAMETER
:
3620 case DECL_PROTECTED
:
3635 case DECL_IS_BIND_C
:
3645 attr
= NULL
; /* This shouldn't happen. */
3648 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3653 /* Now that we've dealt with duplicate attributes, add the attributes
3654 to the current attribute. */
3655 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3660 if (gfc_current_state () == COMP_DERIVED
3661 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3662 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3663 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3665 if (d
== DECL_ALLOCATABLE
)
3667 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3668 "attribute at %C in a TYPE definition"))
3676 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3683 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3684 && gfc_current_state () != COMP_MODULE
)
3686 if (d
== DECL_PRIVATE
)
3690 if (gfc_current_state () == COMP_DERIVED
3691 && gfc_state_stack
->previous
3692 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3694 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3695 "at %L in a TYPE definition", attr
,
3704 gfc_error ("%s attribute at %L is not allowed outside of the "
3705 "specification part of a module", attr
, &seen_at
[d
]);
3713 case DECL_ALLOCATABLE
:
3714 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3717 case DECL_ASYNCHRONOUS
:
3718 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3721 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3724 case DECL_CODIMENSION
:
3725 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3728 case DECL_CONTIGUOUS
:
3729 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3732 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3735 case DECL_DIMENSION
:
3736 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3740 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3744 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3748 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3752 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3755 case DECL_INTRINSIC
:
3756 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3760 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3763 case DECL_PARAMETER
:
3764 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3768 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3771 case DECL_PROTECTED
:
3772 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3774 gfc_error ("PROTECTED at %C only allowed in specification "
3775 "part of a module");
3780 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3783 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3787 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3792 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3797 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3801 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3804 case DECL_IS_BIND_C
:
3805 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3809 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3812 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3816 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3819 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3823 gfc_internal_error ("match_attr_spec(): Bad attribute");
3833 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3834 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3835 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3836 current_attr
.save
= SAVE_IMPLICIT
;
3842 gfc_current_locus
= start
;
3843 gfc_free_array_spec (current_as
);
3849 /* Set the binding label, dest_label, either with the binding label
3850 stored in the given gfc_typespec, ts, or if none was provided, it
3851 will be the symbol name in all lower case, as required by the draft
3852 (J3/04-007, section 15.4.1). If a binding label was given and
3853 there is more than one argument (num_idents), it is an error. */
3856 set_binding_label (const char **dest_label
, const char *sym_name
,
3859 if (num_idents
> 1 && has_name_equals
)
3861 gfc_error ("Multiple identifiers provided with "
3862 "single NAME= specifier at %C");
3866 if (curr_binding_label
)
3867 /* Binding label given; store in temp holder till have sym. */
3868 *dest_label
= curr_binding_label
;
3871 /* No binding label given, and the NAME= specifier did not exist,
3872 which means there was no NAME="". */
3873 if (sym_name
!= NULL
&& has_name_equals
== 0)
3874 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3881 /* Set the status of the given common block as being BIND(C) or not,
3882 depending on the given parameter, is_bind_c. */
3885 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3887 com_block
->is_bind_c
= is_bind_c
;
3892 /* Verify that the given gfc_typespec is for a C interoperable type. */
3895 gfc_verify_c_interop (gfc_typespec
*ts
)
3897 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3898 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3900 else if (ts
->type
== BT_CLASS
)
3902 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
3909 /* Verify that the variables of a given common block, which has been
3910 defined with the attribute specifier bind(c), to be of a C
3911 interoperable type. Errors will be reported here, if
3915 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3917 gfc_symbol
*curr_sym
= NULL
;
3920 curr_sym
= com_block
->head
;
3922 /* Make sure we have at least one symbol. */
3923 if (curr_sym
== NULL
)
3926 /* Here we know we have a symbol, so we'll execute this loop
3930 /* The second to last param, 1, says this is in a common block. */
3931 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3932 curr_sym
= curr_sym
->common_next
;
3933 } while (curr_sym
!= NULL
);
3939 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3940 an appropriate error message is reported. */
3943 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3944 int is_in_common
, gfc_common_head
*com_block
)
3946 bool bind_c_function
= false;
3949 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3950 bind_c_function
= true;
3952 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3954 tmp_sym
= tmp_sym
->result
;
3955 /* Make sure it wasn't an implicitly typed result. */
3956 if (tmp_sym
->attr
.implicit_type
&& gfc_option
.warn_c_binding_type
)
3958 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3959 "%L may not be C interoperable", tmp_sym
->name
,
3960 &tmp_sym
->declared_at
);
3961 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3962 /* Mark it as C interoperable to prevent duplicate warnings. */
3963 tmp_sym
->ts
.is_c_interop
= 1;
3964 tmp_sym
->attr
.is_c_interop
= 1;
3968 /* Here, we know we have the bind(c) attribute, so if we have
3969 enough type info, then verify that it's a C interop kind.
3970 The info could be in the symbol already, or possibly still in
3971 the given ts (current_ts), so look in both. */
3972 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3974 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
3976 /* See if we're dealing with a sym in a common block or not. */
3977 if (is_in_common
== 1 && gfc_option
.warn_c_binding_type
)
3979 gfc_warning ("Variable '%s' in common block '%s' at %L "
3980 "may not be a C interoperable "
3981 "kind though common block '%s' is BIND(C)",
3982 tmp_sym
->name
, com_block
->name
,
3983 &(tmp_sym
->declared_at
), com_block
->name
);
3987 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3988 gfc_error ("Type declaration '%s' at %L is not C "
3989 "interoperable but it is BIND(C)",
3990 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3991 else if (gfc_option
.warn_c_binding_type
)
3992 gfc_warning ("Variable '%s' at %L "
3993 "may not be a C interoperable "
3994 "kind but it is bind(c)",
3995 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3999 /* Variables declared w/in a common block can't be bind(c)
4000 since there's no way for C to see these variables, so there's
4001 semantically no reason for the attribute. */
4002 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4004 gfc_error ("Variable '%s' in common block '%s' at "
4005 "%L cannot be declared with BIND(C) "
4006 "since it is not a global",
4007 tmp_sym
->name
, com_block
->name
,
4008 &(tmp_sym
->declared_at
));
4012 /* Scalar variables that are bind(c) can not have the pointer
4013 or allocatable attributes. */
4014 if (tmp_sym
->attr
.is_bind_c
== 1)
4016 if (tmp_sym
->attr
.pointer
== 1)
4018 gfc_error ("Variable '%s' at %L cannot have both the "
4019 "POINTER and BIND(C) attributes",
4020 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4024 if (tmp_sym
->attr
.allocatable
== 1)
4026 gfc_error ("Variable '%s' at %L cannot have both the "
4027 "ALLOCATABLE and BIND(C) attributes",
4028 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4034 /* If it is a BIND(C) function, make sure the return value is a
4035 scalar value. The previous tests in this function made sure
4036 the type is interoperable. */
4037 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4038 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4039 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4041 /* BIND(C) functions can not return a character string. */
4042 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4043 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4044 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4045 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4046 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4047 "be a character string", tmp_sym
->name
,
4048 &(tmp_sym
->declared_at
));
4051 /* See if the symbol has been marked as private. If it has, make sure
4052 there is no binding label and warn the user if there is one. */
4053 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4054 && tmp_sym
->binding_label
)
4055 /* Use gfc_warning_now because we won't say that the symbol fails
4056 just because of this. */
4057 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4058 "given the binding label '%s'", tmp_sym
->name
,
4059 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4065 /* Set the appropriate fields for a symbol that's been declared as
4066 BIND(C) (the is_bind_c flag and the binding label), and verify that
4067 the type is C interoperable. Errors are reported by the functions
4068 used to set/test these fields. */
4071 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4075 /* TODO: Do we need to make sure the vars aren't marked private? */
4077 /* Set the is_bind_c bit in symbol_attribute. */
4078 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4080 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4087 /* Set the fields marking the given common block as BIND(C), including
4088 a binding label, and report any errors encountered. */
4091 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4095 /* destLabel, common name, typespec (which may have binding label). */
4096 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4100 /* Set the given common block (com_block) to being bind(c) (1). */
4101 set_com_block_bind_c (com_block
, 1);
4107 /* Retrieve the list of one or more identifiers that the given bind(c)
4108 attribute applies to. */
4111 get_bind_c_idents (void)
4113 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4115 gfc_symbol
*tmp_sym
= NULL
;
4117 gfc_common_head
*com_block
= NULL
;
4119 if (gfc_match_name (name
) == MATCH_YES
)
4121 found_id
= MATCH_YES
;
4122 gfc_get_ha_symbol (name
, &tmp_sym
);
4124 else if (match_common_name (name
) == MATCH_YES
)
4126 found_id
= MATCH_YES
;
4127 com_block
= gfc_get_common (name
, 0);
4131 gfc_error ("Need either entity or common block name for "
4132 "attribute specification statement at %C");
4136 /* Save the current identifier and look for more. */
4139 /* Increment the number of identifiers found for this spec stmt. */
4142 /* Make sure we have a sym or com block, and verify that it can
4143 be bind(c). Set the appropriate field(s) and look for more
4145 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4147 if (tmp_sym
!= NULL
)
4149 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4154 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4158 /* Look to see if we have another identifier. */
4160 if (gfc_match_eos () == MATCH_YES
)
4161 found_id
= MATCH_NO
;
4162 else if (gfc_match_char (',') != MATCH_YES
)
4163 found_id
= MATCH_NO
;
4164 else if (gfc_match_name (name
) == MATCH_YES
)
4166 found_id
= MATCH_YES
;
4167 gfc_get_ha_symbol (name
, &tmp_sym
);
4169 else if (match_common_name (name
) == MATCH_YES
)
4171 found_id
= MATCH_YES
;
4172 com_block
= gfc_get_common (name
, 0);
4176 gfc_error ("Missing entity or common block name for "
4177 "attribute specification statement at %C");
4183 gfc_internal_error ("Missing symbol");
4185 } while (found_id
== MATCH_YES
);
4187 /* if we get here we were successful */
4192 /* Try and match a BIND(C) attribute specification statement. */
4195 gfc_match_bind_c_stmt (void)
4197 match found_match
= MATCH_NO
;
4202 /* This may not be necessary. */
4204 /* Clear the temporary binding label holder. */
4205 curr_binding_label
= NULL
;
4207 /* Look for the bind(c). */
4208 found_match
= gfc_match_bind_c (NULL
, true);
4210 if (found_match
== MATCH_YES
)
4212 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4215 /* Look for the :: now, but it is not required. */
4218 /* Get the identifier(s) that needs to be updated. This may need to
4219 change to hand the flag(s) for the attr specified so all identifiers
4220 found can have all appropriate parts updated (assuming that the same
4221 spec stmt can have multiple attrs, such as both bind(c) and
4223 if (!get_bind_c_idents ())
4224 /* Error message should have printed already. */
4232 /* Match a data declaration statement. */
4235 gfc_match_data_decl (void)
4241 num_idents_on_line
= 0;
4243 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4247 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4248 && gfc_current_state () != COMP_DERIVED
)
4250 sym
= gfc_use_derived (current_ts
.u
.derived
);
4258 current_ts
.u
.derived
= sym
;
4261 m
= match_attr_spec ();
4262 if (m
== MATCH_ERROR
)
4268 if (current_ts
.type
== BT_CLASS
4269 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4272 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4273 && current_ts
.u
.derived
->components
== NULL
4274 && !current_ts
.u
.derived
->attr
.zero_comp
)
4277 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4280 gfc_find_symbol (current_ts
.u
.derived
->name
,
4281 current_ts
.u
.derived
->ns
, 1, &sym
);
4283 /* Any symbol that we find had better be a type definition
4284 which has its components defined. */
4285 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4286 && (current_ts
.u
.derived
->components
!= NULL
4287 || current_ts
.u
.derived
->attr
.zero_comp
))
4290 gfc_error ("Derived type at %C has not been previously defined "
4291 "and so cannot appear in a derived type definition");
4297 /* If we have an old-style character declaration, and no new-style
4298 attribute specifications, then there a comma is optional between
4299 the type specification and the variable list. */
4300 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4301 gfc_match_char (',');
4303 /* Give the types/attributes to symbols that follow. Give the element
4304 a number so that repeat character length expressions can be copied. */
4308 num_idents_on_line
++;
4309 m
= variable_decl (elem
++);
4310 if (m
== MATCH_ERROR
)
4315 if (gfc_match_eos () == MATCH_YES
)
4317 if (gfc_match_char (',') != MATCH_YES
)
4321 if (gfc_error_flag_test () == 0)
4322 gfc_error ("Syntax error in data declaration at %C");
4325 gfc_free_data_all (gfc_current_ns
);
4328 gfc_free_array_spec (current_as
);
4334 /* Match a prefix associated with a function or subroutine
4335 declaration. If the typespec pointer is nonnull, then a typespec
4336 can be matched. Note that if nothing matches, MATCH_YES is
4337 returned (the null string was matched). */
4340 gfc_match_prefix (gfc_typespec
*ts
)
4346 gfc_clear_attr (¤t_attr
);
4348 seen_impure
= false;
4350 gcc_assert (!gfc_matching_prefix
);
4351 gfc_matching_prefix
= true;
4355 found_prefix
= false;
4357 if (!seen_type
&& ts
!= NULL
4358 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4359 && gfc_match_space () == MATCH_YES
)
4363 found_prefix
= true;
4366 if (gfc_match ("elemental% ") == MATCH_YES
)
4368 if (!gfc_add_elemental (¤t_attr
, NULL
))
4371 found_prefix
= true;
4374 if (gfc_match ("pure% ") == MATCH_YES
)
4376 if (!gfc_add_pure (¤t_attr
, NULL
))
4379 found_prefix
= true;
4382 if (gfc_match ("recursive% ") == MATCH_YES
)
4384 if (!gfc_add_recursive (¤t_attr
, NULL
))
4387 found_prefix
= true;
4390 /* IMPURE is a somewhat special case, as it needs not set an actual
4391 attribute but rather only prevents ELEMENTAL routines from being
4392 automatically PURE. */
4393 if (gfc_match ("impure% ") == MATCH_YES
)
4395 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4399 found_prefix
= true;
4402 while (found_prefix
);
4404 /* IMPURE and PURE must not both appear, of course. */
4405 if (seen_impure
&& current_attr
.pure
)
4407 gfc_error ("PURE and IMPURE must not appear both at %C");
4411 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4412 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4414 if (!gfc_add_pure (¤t_attr
, NULL
))
4418 /* At this point, the next item is not a prefix. */
4419 gcc_assert (gfc_matching_prefix
);
4420 gfc_matching_prefix
= false;
4424 gcc_assert (gfc_matching_prefix
);
4425 gfc_matching_prefix
= false;
4430 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4433 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4435 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4438 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4441 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4448 /* Match a formal argument list. */
4451 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4453 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4454 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4460 if (gfc_match_char ('(') != MATCH_YES
)
4467 if (gfc_match_char (')') == MATCH_YES
)
4472 if (gfc_match_char ('*') == MATCH_YES
)
4475 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4484 m
= gfc_match_name (name
);
4488 if (gfc_get_symbol (name
, NULL
, &sym
))
4492 p
= gfc_get_formal_arglist ();
4504 /* We don't add the VARIABLE flavor because the name could be a
4505 dummy procedure. We don't apply these attributes to formal
4506 arguments of statement functions. */
4507 if (sym
!= NULL
&& !st_flag
4508 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4509 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4515 /* The name of a program unit can be in a different namespace,
4516 so check for it explicitly. After the statement is accepted,
4517 the name is checked for especially in gfc_get_symbol(). */
4518 if (gfc_new_block
!= NULL
&& sym
!= NULL
4519 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4521 gfc_error ("Name '%s' at %C is the name of the procedure",
4527 if (gfc_match_char (')') == MATCH_YES
)
4530 m
= gfc_match_char (',');
4533 gfc_error ("Unexpected junk in formal argument list at %C");
4539 /* Check for duplicate symbols in the formal argument list. */
4542 for (p
= head
; p
->next
; p
= p
->next
)
4547 for (q
= p
->next
; q
; q
= q
->next
)
4548 if (p
->sym
== q
->sym
)
4550 gfc_error ("Duplicate symbol '%s' in formal argument list "
4551 "at %C", p
->sym
->name
);
4559 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4568 gfc_free_formal_arglist (head
);
4573 /* Match a RESULT specification following a function declaration or
4574 ENTRY statement. Also matches the end-of-statement. */
4577 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4579 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4583 if (gfc_match (" result (") != MATCH_YES
)
4586 m
= gfc_match_name (name
);
4590 /* Get the right paren, and that's it because there could be the
4591 bind(c) attribute after the result clause. */
4592 if (gfc_match_char (')') != MATCH_YES
)
4594 /* TODO: should report the missing right paren here. */
4598 if (strcmp (function
->name
, name
) == 0)
4600 gfc_error ("RESULT variable at %C must be different than function name");
4604 if (gfc_get_symbol (name
, NULL
, &r
))
4607 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4616 /* Match a function suffix, which could be a combination of a result
4617 clause and BIND(C), either one, or neither. The draft does not
4618 require them to come in a specific order. */
4621 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4623 match is_bind_c
; /* Found bind(c). */
4624 match is_result
; /* Found result clause. */
4625 match found_match
; /* Status of whether we've found a good match. */
4626 char peek_char
; /* Character we're going to peek at. */
4627 bool allow_binding_name
;
4629 /* Initialize to having found nothing. */
4630 found_match
= MATCH_NO
;
4631 is_bind_c
= MATCH_NO
;
4632 is_result
= MATCH_NO
;
4634 /* Get the next char to narrow between result and bind(c). */
4635 gfc_gobble_whitespace ();
4636 peek_char
= gfc_peek_ascii_char ();
4638 /* C binding names are not allowed for internal procedures. */
4639 if (gfc_current_state () == COMP_CONTAINS
4640 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4641 allow_binding_name
= false;
4643 allow_binding_name
= true;
4648 /* Look for result clause. */
4649 is_result
= match_result (sym
, result
);
4650 if (is_result
== MATCH_YES
)
4652 /* Now see if there is a bind(c) after it. */
4653 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4654 /* We've found the result clause and possibly bind(c). */
4655 found_match
= MATCH_YES
;
4658 /* This should only be MATCH_ERROR. */
4659 found_match
= is_result
;
4662 /* Look for bind(c) first. */
4663 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4664 if (is_bind_c
== MATCH_YES
)
4666 /* Now see if a result clause followed it. */
4667 is_result
= match_result (sym
, result
);
4668 found_match
= MATCH_YES
;
4672 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4673 found_match
= MATCH_ERROR
;
4677 gfc_error ("Unexpected junk after function declaration at %C");
4678 found_match
= MATCH_ERROR
;
4682 if (is_bind_c
== MATCH_YES
)
4684 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4685 if (gfc_current_state () == COMP_CONTAINS
4686 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4687 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4688 "at %L may not be specified for an internal "
4689 "procedure", &gfc_current_locus
))
4692 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4700 /* Procedure pointer return value without RESULT statement:
4701 Add "hidden" result variable named "ppr@". */
4704 add_hidden_procptr_result (gfc_symbol
*sym
)
4708 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4711 /* First usage case: PROCEDURE and EXTERNAL statements. */
4712 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4713 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4714 && sym
->attr
.external
;
4715 /* Second usage case: INTERFACE statements. */
4716 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4717 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4718 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4724 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4728 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4729 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4730 st2
->n
.sym
= stree
->n
.sym
;
4732 sym
->result
= stree
->n
.sym
;
4734 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4735 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4736 sym
->result
->attr
.external
= sym
->attr
.external
;
4737 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4738 sym
->result
->ts
= sym
->ts
;
4739 sym
->attr
.proc_pointer
= 0;
4740 sym
->attr
.pointer
= 0;
4741 sym
->attr
.external
= 0;
4742 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4744 sym
->result
->attr
.pointer
= 0;
4745 sym
->result
->attr
.proc_pointer
= 1;
4748 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4750 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4751 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4752 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4753 && sym
== gfc_current_ns
->proc_name
4754 && sym
== sym
->result
->ns
->proc_name
4755 && strcmp ("ppr@", sym
->result
->name
) == 0)
4757 sym
->result
->attr
.proc_pointer
= 1;
4758 sym
->attr
.pointer
= 0;
4766 /* Match the interface for a PROCEDURE declaration,
4767 including brackets (R1212). */
4770 match_procedure_interface (gfc_symbol
**proc_if
)
4774 locus old_loc
, entry_loc
;
4775 gfc_namespace
*old_ns
= gfc_current_ns
;
4776 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4778 old_loc
= entry_loc
= gfc_current_locus
;
4779 gfc_clear_ts (¤t_ts
);
4781 if (gfc_match (" (") != MATCH_YES
)
4783 gfc_current_locus
= entry_loc
;
4787 /* Get the type spec. for the procedure interface. */
4788 old_loc
= gfc_current_locus
;
4789 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4790 gfc_gobble_whitespace ();
4791 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4794 if (m
== MATCH_ERROR
)
4797 /* Procedure interface is itself a procedure. */
4798 gfc_current_locus
= old_loc
;
4799 m
= gfc_match_name (name
);
4801 /* First look to see if it is already accessible in the current
4802 namespace because it is use associated or contained. */
4804 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4807 /* If it is still not found, then try the parent namespace, if it
4808 exists and create the symbol there if it is still not found. */
4809 if (gfc_current_ns
->parent
)
4810 gfc_current_ns
= gfc_current_ns
->parent
;
4811 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4814 gfc_current_ns
= old_ns
;
4815 *proc_if
= st
->n
.sym
;
4820 /* Resolve interface if possible. That way, attr.procedure is only set
4821 if it is declared by a later procedure-declaration-stmt, which is
4822 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4823 while ((*proc_if
)->ts
.interface
)
4824 *proc_if
= (*proc_if
)->ts
.interface
;
4826 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4827 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4828 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4829 (*proc_if
)->name
, NULL
))
4834 if (gfc_match (" )") != MATCH_YES
)
4836 gfc_current_locus
= entry_loc
;
4844 /* Match a PROCEDURE declaration (R1211). */
4847 match_procedure_decl (void)
4850 gfc_symbol
*sym
, *proc_if
= NULL
;
4852 gfc_expr
*initializer
= NULL
;
4854 /* Parse interface (with brackets). */
4855 m
= match_procedure_interface (&proc_if
);
4859 /* Parse attributes (with colons). */
4860 m
= match_attr_spec();
4861 if (m
== MATCH_ERROR
)
4864 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4866 current_attr
.is_bind_c
= 1;
4867 has_name_equals
= 0;
4868 curr_binding_label
= NULL
;
4871 /* Get procedure symbols. */
4874 m
= gfc_match_symbol (&sym
, 0);
4877 else if (m
== MATCH_ERROR
)
4880 /* Add current_attr to the symbol attributes. */
4881 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4884 if (sym
->attr
.is_bind_c
)
4886 /* Check for C1218. */
4887 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4889 gfc_error ("BIND(C) attribute at %C requires "
4890 "an interface with BIND(C)");
4893 /* Check for C1217. */
4894 if (has_name_equals
&& sym
->attr
.pointer
)
4896 gfc_error ("BIND(C) procedure with NAME may not have "
4897 "POINTER attribute at %C");
4900 if (has_name_equals
&& sym
->attr
.dummy
)
4902 gfc_error ("Dummy procedure at %C may not have "
4903 "BIND(C) attribute with NAME");
4906 /* Set binding label for BIND(C). */
4907 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
4911 if (!gfc_add_external (&sym
->attr
, NULL
))
4914 if (add_hidden_procptr_result (sym
))
4917 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
4920 /* Set interface. */
4921 if (proc_if
!= NULL
)
4923 if (sym
->ts
.type
!= BT_UNKNOWN
)
4925 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4926 sym
->name
, &gfc_current_locus
,
4927 gfc_basic_typename (sym
->ts
.type
));
4930 sym
->ts
.interface
= proc_if
;
4931 sym
->attr
.untyped
= 1;
4932 sym
->attr
.if_source
= IFSRC_IFBODY
;
4934 else if (current_ts
.type
!= BT_UNKNOWN
)
4936 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
4938 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4939 sym
->ts
.interface
->ts
= current_ts
;
4940 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4941 sym
->ts
.interface
->attr
.function
= 1;
4942 sym
->attr
.function
= 1;
4943 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4946 if (gfc_match (" =>") == MATCH_YES
)
4948 if (!current_attr
.pointer
)
4950 gfc_error ("Initialization at %C isn't for a pointer variable");
4955 m
= match_pointer_init (&initializer
, 1);
4959 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
4964 if (gfc_match_eos () == MATCH_YES
)
4966 if (gfc_match_char (',') != MATCH_YES
)
4971 gfc_error ("Syntax error in PROCEDURE statement at %C");
4975 /* Free stuff up and return. */
4976 gfc_free_expr (initializer
);
4982 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4985 /* Match a procedure pointer component declaration (R445). */
4988 match_ppc_decl (void)
4991 gfc_symbol
*proc_if
= NULL
;
4995 gfc_expr
*initializer
= NULL
;
4996 gfc_typebound_proc
* tb
;
4997 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4999 /* Parse interface (with brackets). */
5000 m
= match_procedure_interface (&proc_if
);
5004 /* Parse attributes. */
5005 tb
= XCNEW (gfc_typebound_proc
);
5006 tb
->where
= gfc_current_locus
;
5007 m
= match_binding_attributes (tb
, false, true);
5008 if (m
== MATCH_ERROR
)
5011 gfc_clear_attr (¤t_attr
);
5012 current_attr
.procedure
= 1;
5013 current_attr
.proc_pointer
= 1;
5014 current_attr
.access
= tb
->access
;
5015 current_attr
.flavor
= FL_PROCEDURE
;
5017 /* Match the colons (required). */
5018 if (gfc_match (" ::") != MATCH_YES
)
5020 gfc_error ("Expected '::' after binding-attributes at %C");
5024 /* Check for C450. */
5025 if (!tb
->nopass
&& proc_if
== NULL
)
5027 gfc_error("NOPASS or explicit interface required at %C");
5031 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5034 /* Match PPC names. */
5038 m
= gfc_match_name (name
);
5041 else if (m
== MATCH_ERROR
)
5044 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5047 /* Add current_attr to the symbol attributes. */
5048 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5051 if (!gfc_add_external (&c
->attr
, NULL
))
5054 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5061 c
->tb
= XCNEW (gfc_typebound_proc
);
5062 c
->tb
->where
= gfc_current_locus
;
5066 /* Set interface. */
5067 if (proc_if
!= NULL
)
5069 c
->ts
.interface
= proc_if
;
5070 c
->attr
.untyped
= 1;
5071 c
->attr
.if_source
= IFSRC_IFBODY
;
5073 else if (ts
.type
!= BT_UNKNOWN
)
5076 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5077 c
->ts
.interface
->result
= c
->ts
.interface
;
5078 c
->ts
.interface
->ts
= ts
;
5079 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5080 c
->ts
.interface
->attr
.function
= 1;
5081 c
->attr
.function
= 1;
5082 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5085 if (gfc_match (" =>") == MATCH_YES
)
5087 m
= match_pointer_init (&initializer
, 1);
5090 gfc_free_expr (initializer
);
5093 c
->initializer
= initializer
;
5096 if (gfc_match_eos () == MATCH_YES
)
5098 if (gfc_match_char (',') != MATCH_YES
)
5103 gfc_error ("Syntax error in procedure pointer component at %C");
5108 /* Match a PROCEDURE declaration inside an interface (R1206). */
5111 match_procedure_in_interface (void)
5115 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5118 if (current_interface
.type
== INTERFACE_NAMELESS
5119 || current_interface
.type
== INTERFACE_ABSTRACT
)
5121 gfc_error ("PROCEDURE at %C must be in a generic interface");
5125 /* Check if the F2008 optional double colon appears. */
5126 gfc_gobble_whitespace ();
5127 old_locus
= gfc_current_locus
;
5128 if (gfc_match ("::") == MATCH_YES
)
5130 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5131 "MODULE PROCEDURE statement at %L", &old_locus
))
5135 gfc_current_locus
= old_locus
;
5139 m
= gfc_match_name (name
);
5142 else if (m
== MATCH_ERROR
)
5144 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5147 if (!gfc_add_interface (sym
))
5150 if (gfc_match_eos () == MATCH_YES
)
5152 if (gfc_match_char (',') != MATCH_YES
)
5159 gfc_error ("Syntax error in PROCEDURE statement at %C");
5164 /* General matcher for PROCEDURE declarations. */
5166 static match
match_procedure_in_type (void);
5169 gfc_match_procedure (void)
5173 switch (gfc_current_state ())
5178 case COMP_SUBROUTINE
:
5181 m
= match_procedure_decl ();
5183 case COMP_INTERFACE
:
5184 m
= match_procedure_in_interface ();
5187 m
= match_ppc_decl ();
5189 case COMP_DERIVED_CONTAINS
:
5190 m
= match_procedure_in_type ();
5199 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5206 /* Warn if a matched procedure has the same name as an intrinsic; this is
5207 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5208 parser-state-stack to find out whether we're in a module. */
5211 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5215 in_module
= (gfc_state_stack
->previous
5216 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5218 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5222 /* Match a function declaration. */
5225 gfc_match_function_decl (void)
5227 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5228 gfc_symbol
*sym
, *result
;
5232 match found_match
; /* Status returned by match func. */
5234 if (gfc_current_state () != COMP_NONE
5235 && gfc_current_state () != COMP_INTERFACE
5236 && gfc_current_state () != COMP_CONTAINS
)
5239 gfc_clear_ts (¤t_ts
);
5241 old_loc
= gfc_current_locus
;
5243 m
= gfc_match_prefix (¤t_ts
);
5246 gfc_current_locus
= old_loc
;
5250 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5252 gfc_current_locus
= old_loc
;
5255 if (get_proc_name (name
, &sym
, false))
5258 if (add_hidden_procptr_result (sym
))
5261 gfc_new_block
= sym
;
5263 m
= gfc_match_formal_arglist (sym
, 0, 0);
5266 gfc_error ("Expected formal argument list in function "
5267 "definition at %C");
5271 else if (m
== MATCH_ERROR
)
5276 /* According to the draft, the bind(c) and result clause can
5277 come in either order after the formal_arg_list (i.e., either
5278 can be first, both can exist together or by themselves or neither
5279 one). Therefore, the match_result can't match the end of the
5280 string, and check for the bind(c) or result clause in either order. */
5281 found_match
= gfc_match_eos ();
5283 /* Make sure that it isn't already declared as BIND(C). If it is, it
5284 must have been marked BIND(C) with a BIND(C) attribute and that is
5285 not allowed for procedures. */
5286 if (sym
->attr
.is_bind_c
== 1)
5288 sym
->attr
.is_bind_c
= 0;
5289 if (sym
->old_symbol
!= NULL
)
5290 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5291 "variables or common blocks",
5292 &(sym
->old_symbol
->declared_at
));
5294 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5295 "variables or common blocks", &gfc_current_locus
);
5298 if (found_match
!= MATCH_YES
)
5300 /* If we haven't found the end-of-statement, look for a suffix. */
5301 suffix_match
= gfc_match_suffix (sym
, &result
);
5302 if (suffix_match
== MATCH_YES
)
5303 /* Need to get the eos now. */
5304 found_match
= gfc_match_eos ();
5306 found_match
= suffix_match
;
5309 if(found_match
!= MATCH_YES
)
5313 /* Make changes to the symbol. */
5316 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5319 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5320 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5323 /* Delay matching the function characteristics until after the
5324 specification block by signalling kind=-1. */
5325 sym
->declared_at
= old_loc
;
5326 if (current_ts
.type
!= BT_UNKNOWN
)
5327 current_ts
.kind
= -1;
5329 current_ts
.kind
= 0;
5333 if (current_ts
.type
!= BT_UNKNOWN
5334 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5340 if (current_ts
.type
!= BT_UNKNOWN
5341 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5343 sym
->result
= result
;
5346 /* Warn if this procedure has the same name as an intrinsic. */
5347 warn_intrinsic_shadow (sym
, true);
5353 gfc_current_locus
= old_loc
;
5358 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5359 pass the name of the entry, rather than the gfc_current_block name, and
5360 to return false upon finding an existing global entry. */
5363 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5367 enum gfc_symbol_type type
;
5369 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5371 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5372 name is a global identifier. */
5373 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5375 s
= gfc_get_gsymbol (name
);
5377 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5379 gfc_global_used (s
, where
);
5388 s
->ns
= gfc_current_ns
;
5392 /* Don't add the symbol multiple times. */
5394 && (!gfc_notification_std (GFC_STD_F2008
)
5395 || strcmp (name
, binding_label
) != 0))
5397 s
= gfc_get_gsymbol (binding_label
);
5399 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5401 gfc_global_used (s
, where
);
5408 s
->binding_label
= binding_label
;
5411 s
->ns
= gfc_current_ns
;
5419 /* Match an ENTRY statement. */
5422 gfc_match_entry (void)
5427 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5428 gfc_compile_state state
;
5432 bool module_procedure
;
5436 m
= gfc_match_name (name
);
5440 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5443 state
= gfc_current_state ();
5444 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5449 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5452 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5454 case COMP_BLOCK_DATA
:
5455 gfc_error ("ENTRY statement at %C cannot appear within "
5458 case COMP_INTERFACE
:
5459 gfc_error ("ENTRY statement at %C cannot appear within "
5463 gfc_error ("ENTRY statement at %C cannot appear within "
5464 "a DERIVED TYPE block");
5467 gfc_error ("ENTRY statement at %C cannot appear within "
5468 "an IF-THEN block");
5471 case COMP_DO_CONCURRENT
:
5472 gfc_error ("ENTRY statement at %C cannot appear within "
5476 gfc_error ("ENTRY statement at %C cannot appear within "
5480 gfc_error ("ENTRY statement at %C cannot appear within "
5484 gfc_error ("ENTRY statement at %C cannot appear within "
5488 gfc_error ("ENTRY statement at %C cannot appear within "
5489 "a contained subprogram");
5492 gfc_internal_error ("gfc_match_entry(): Bad state");
5497 module_procedure
= gfc_current_ns
->parent
!= NULL
5498 && gfc_current_ns
->parent
->proc_name
5499 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5502 if (gfc_current_ns
->parent
!= NULL
5503 && gfc_current_ns
->parent
->proc_name
5504 && !module_procedure
)
5506 gfc_error("ENTRY statement at %C cannot appear in a "
5507 "contained procedure");
5511 /* Module function entries need special care in get_proc_name
5512 because previous references within the function will have
5513 created symbols attached to the current namespace. */
5514 if (get_proc_name (name
, &entry
,
5515 gfc_current_ns
->parent
!= NULL
5516 && module_procedure
))
5519 proc
= gfc_current_block ();
5521 /* Make sure that it isn't already declared as BIND(C). If it is, it
5522 must have been marked BIND(C) with a BIND(C) attribute and that is
5523 not allowed for procedures. */
5524 if (entry
->attr
.is_bind_c
== 1)
5526 entry
->attr
.is_bind_c
= 0;
5527 if (entry
->old_symbol
!= NULL
)
5528 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5529 "variables or common blocks",
5530 &(entry
->old_symbol
->declared_at
));
5532 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5533 "variables or common blocks", &gfc_current_locus
);
5536 /* Check what next non-whitespace character is so we can tell if there
5537 is the required parens if we have a BIND(C). */
5538 old_loc
= gfc_current_locus
;
5539 gfc_gobble_whitespace ();
5540 peek_char
= gfc_peek_ascii_char ();
5542 if (state
== COMP_SUBROUTINE
)
5544 m
= gfc_match_formal_arglist (entry
, 0, 1);
5548 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5549 never be an internal procedure. */
5550 is_bind_c
= gfc_match_bind_c (entry
, true);
5551 if (is_bind_c
== MATCH_ERROR
)
5553 if (is_bind_c
== MATCH_YES
)
5555 if (peek_char
!= '(')
5557 gfc_error ("Missing required parentheses before BIND(C) at %C");
5560 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5561 &(entry
->declared_at
), 1))
5565 if (!gfc_current_ns
->parent
5566 && !add_global_entry (name
, entry
->binding_label
, true,
5570 /* An entry in a subroutine. */
5571 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5572 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5577 /* An entry in a function.
5578 We need to take special care because writing
5583 ENTRY f() RESULT (r)
5585 ENTRY f RESULT (r). */
5586 if (gfc_match_eos () == MATCH_YES
)
5588 gfc_current_locus
= old_loc
;
5589 /* Match the empty argument list, and add the interface to
5591 m
= gfc_match_formal_arglist (entry
, 0, 1);
5594 m
= gfc_match_formal_arglist (entry
, 0, 0);
5601 if (gfc_match_eos () == MATCH_YES
)
5603 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5604 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5607 entry
->result
= entry
;
5611 m
= gfc_match_suffix (entry
, &result
);
5613 gfc_syntax_error (ST_ENTRY
);
5619 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5620 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5621 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5623 entry
->result
= result
;
5627 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5628 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5630 entry
->result
= entry
;
5634 if (!gfc_current_ns
->parent
5635 && !add_global_entry (name
, entry
->binding_label
, false,
5640 if (gfc_match_eos () != MATCH_YES
)
5642 gfc_syntax_error (ST_ENTRY
);
5646 entry
->attr
.recursive
= proc
->attr
.recursive
;
5647 entry
->attr
.elemental
= proc
->attr
.elemental
;
5648 entry
->attr
.pure
= proc
->attr
.pure
;
5650 el
= gfc_get_entry_list ();
5652 el
->next
= gfc_current_ns
->entries
;
5653 gfc_current_ns
->entries
= el
;
5655 el
->id
= el
->next
->id
+ 1;
5659 new_st
.op
= EXEC_ENTRY
;
5660 new_st
.ext
.entry
= el
;
5666 /* Match a subroutine statement, including optional prefixes. */
5669 gfc_match_subroutine (void)
5671 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5676 bool allow_binding_name
;
5678 if (gfc_current_state () != COMP_NONE
5679 && gfc_current_state () != COMP_INTERFACE
5680 && gfc_current_state () != COMP_CONTAINS
)
5683 m
= gfc_match_prefix (NULL
);
5687 m
= gfc_match ("subroutine% %n", name
);
5691 if (get_proc_name (name
, &sym
, false))
5694 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5695 the symbol existed before. */
5696 sym
->declared_at
= gfc_current_locus
;
5698 if (add_hidden_procptr_result (sym
))
5701 gfc_new_block
= sym
;
5703 /* Check what next non-whitespace character is so we can tell if there
5704 is the required parens if we have a BIND(C). */
5705 gfc_gobble_whitespace ();
5706 peek_char
= gfc_peek_ascii_char ();
5708 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5711 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5714 /* Make sure that it isn't already declared as BIND(C). If it is, it
5715 must have been marked BIND(C) with a BIND(C) attribute and that is
5716 not allowed for procedures. */
5717 if (sym
->attr
.is_bind_c
== 1)
5719 sym
->attr
.is_bind_c
= 0;
5720 if (sym
->old_symbol
!= NULL
)
5721 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5722 "variables or common blocks",
5723 &(sym
->old_symbol
->declared_at
));
5725 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5726 "variables or common blocks", &gfc_current_locus
);
5729 /* C binding names are not allowed for internal procedures. */
5730 if (gfc_current_state () == COMP_CONTAINS
5731 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5732 allow_binding_name
= false;
5734 allow_binding_name
= true;
5736 /* Here, we are just checking if it has the bind(c) attribute, and if
5737 so, then we need to make sure it's all correct. If it doesn't,
5738 we still need to continue matching the rest of the subroutine line. */
5739 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5740 if (is_bind_c
== MATCH_ERROR
)
5742 /* There was an attempt at the bind(c), but it was wrong. An
5743 error message should have been printed w/in the gfc_match_bind_c
5744 so here we'll just return the MATCH_ERROR. */
5748 if (is_bind_c
== MATCH_YES
)
5750 /* The following is allowed in the Fortran 2008 draft. */
5751 if (gfc_current_state () == COMP_CONTAINS
5752 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5753 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5754 "at %L may not be specified for an internal "
5755 "procedure", &gfc_current_locus
))
5758 if (peek_char
!= '(')
5760 gfc_error ("Missing required parentheses before BIND(C) at %C");
5763 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5764 &(sym
->declared_at
), 1))
5768 if (gfc_match_eos () != MATCH_YES
)
5770 gfc_syntax_error (ST_SUBROUTINE
);
5774 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5777 /* Warn if it has the same name as an intrinsic. */
5778 warn_intrinsic_shadow (sym
, false);
5784 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5785 given, and set the binding label in either the given symbol (if not
5786 NULL), or in the current_ts. The symbol may be NULL because we may
5787 encounter the BIND(C) before the declaration itself. Return
5788 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5789 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5790 or MATCH_YES if the specifier was correct and the binding label and
5791 bind(c) fields were set correctly for the given symbol or the
5792 current_ts. If allow_binding_name is false, no binding name may be
5796 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5798 /* binding label, if exists */
5799 const char* binding_label
= NULL
;
5803 /* Initialize the flag that specifies whether we encountered a NAME=
5804 specifier or not. */
5805 has_name_equals
= 0;
5807 /* This much we have to be able to match, in this order, if
5808 there is a bind(c) label. */
5809 if (gfc_match (" bind ( c ") != MATCH_YES
)
5812 /* Now see if there is a binding label, or if we've reached the
5813 end of the bind(c) attribute without one. */
5814 if (gfc_match_char (',') == MATCH_YES
)
5816 if (gfc_match (" name = ") != MATCH_YES
)
5818 gfc_error ("Syntax error in NAME= specifier for binding label "
5820 /* should give an error message here */
5824 has_name_equals
= 1;
5826 /* Get the opening quote. */
5827 double_quote
= MATCH_YES
;
5828 single_quote
= MATCH_YES
;
5829 double_quote
= gfc_match_char ('"');
5830 if (double_quote
!= MATCH_YES
)
5831 single_quote
= gfc_match_char ('\'');
5832 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5834 gfc_error ("Syntax error in NAME= specifier for binding label "
5839 /* Grab the binding label, using functions that will not lower
5840 case the names automatically. */
5841 if (gfc_match_name_C (&binding_label
) != MATCH_YES
)
5844 /* Get the closing quotation. */
5845 if (double_quote
== MATCH_YES
)
5847 if (gfc_match_char ('"') != MATCH_YES
)
5849 gfc_error ("Missing closing quote '\"' for binding label at %C");
5850 /* User started string with '"' so looked to match it. */
5856 if (gfc_match_char ('\'') != MATCH_YES
)
5858 gfc_error ("Missing closing quote '\'' for binding label at %C");
5859 /* User started string with "'" char. */
5865 /* Get the required right paren. */
5866 if (gfc_match_char (')') != MATCH_YES
)
5868 gfc_error ("Missing closing paren for binding label at %C");
5872 if (has_name_equals
&& !allow_binding_name
)
5874 gfc_error ("No binding name is allowed in BIND(C) at %C");
5878 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5880 gfc_error ("For dummy procedure %s, no binding name is "
5881 "allowed in BIND(C) at %C", sym
->name
);
5886 /* Save the binding label to the symbol. If sym is null, we're
5887 probably matching the typespec attributes of a declaration and
5888 haven't gotten the name yet, and therefore, no symbol yet. */
5892 sym
->binding_label
= binding_label
;
5894 curr_binding_label
= binding_label
;
5896 else if (allow_binding_name
)
5898 /* No binding label, but if symbol isn't null, we
5899 can set the label for it here.
5900 If name="" or allow_binding_name is false, no C binding name is
5902 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5903 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
5906 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5907 && current_interface
.type
== INTERFACE_ABSTRACT
)
5909 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5917 /* Return nonzero if we're currently compiling a contained procedure. */
5920 contained_procedure (void)
5922 gfc_state_data
*s
= gfc_state_stack
;
5924 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5925 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5931 /* Set the kind of each enumerator. The kind is selected such that it is
5932 interoperable with the corresponding C enumeration type, making
5933 sure that -fshort-enums is honored. */
5938 enumerator_history
*current_history
= NULL
;
5942 if (max_enum
== NULL
|| enum_history
== NULL
)
5945 if (!flag_short_enums
)
5951 kind
= gfc_integer_kinds
[i
++].kind
;
5953 while (kind
< gfc_c_int_kind
5954 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5957 current_history
= enum_history
;
5958 while (current_history
!= NULL
)
5960 current_history
->sym
->ts
.kind
= kind
;
5961 current_history
= current_history
->next
;
5966 /* Match any of the various end-block statements. Returns the type of
5967 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5968 and END BLOCK statements cannot be replaced by a single END statement. */
5971 gfc_match_end (gfc_statement
*st
)
5973 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5974 gfc_compile_state state
;
5976 const char *block_name
;
5980 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
5981 gfc_namespace
**nsp
;
5983 old_loc
= gfc_current_locus
;
5984 if (gfc_match ("end") != MATCH_YES
)
5987 state
= gfc_current_state ();
5988 block_name
= gfc_current_block () == NULL
5989 ? NULL
: gfc_current_block ()->name
;
5993 case COMP_ASSOCIATE
:
5995 if (!strncmp (block_name
, "block@", strlen("block@")))
6000 case COMP_DERIVED_CONTAINS
:
6001 state
= gfc_state_stack
->previous
->state
;
6002 block_name
= gfc_state_stack
->previous
->sym
== NULL
6003 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6014 *st
= ST_END_PROGRAM
;
6015 target
= " program";
6019 case COMP_SUBROUTINE
:
6020 *st
= ST_END_SUBROUTINE
;
6021 target
= " subroutine";
6022 eos_ok
= !contained_procedure ();
6026 *st
= ST_END_FUNCTION
;
6027 target
= " function";
6028 eos_ok
= !contained_procedure ();
6031 case COMP_BLOCK_DATA
:
6032 *st
= ST_END_BLOCK_DATA
;
6033 target
= " block data";
6038 *st
= ST_END_MODULE
;
6043 case COMP_INTERFACE
:
6044 *st
= ST_END_INTERFACE
;
6045 target
= " interface";
6050 case COMP_DERIVED_CONTAINS
:
6056 case COMP_ASSOCIATE
:
6057 *st
= ST_END_ASSOCIATE
;
6058 target
= " associate";
6075 case COMP_DO_CONCURRENT
:
6082 *st
= ST_END_CRITICAL
;
6083 target
= " critical";
6088 case COMP_SELECT_TYPE
:
6089 *st
= ST_END_SELECT
;
6095 *st
= ST_END_FORALL
;
6110 last_initializer
= NULL
;
6112 gfc_free_enum_history ();
6116 gfc_error ("Unexpected END statement at %C");
6120 old_loc
= gfc_current_locus
;
6121 if (gfc_match_eos () == MATCH_YES
)
6123 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6125 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6126 "instead of %s statement at %L",
6127 gfc_ascii_statement(*st
), &old_loc
))
6132 /* We would have required END [something]. */
6133 gfc_error ("%s statement expected at %L",
6134 gfc_ascii_statement (*st
), &old_loc
);
6141 /* Verify that we've got the sort of end-block that we're expecting. */
6142 if (gfc_match (target
) != MATCH_YES
)
6144 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6149 old_loc
= gfc_current_locus
;
6150 /* If we're at the end, make sure a block name wasn't required. */
6151 if (gfc_match_eos () == MATCH_YES
)
6154 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6155 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6156 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6162 gfc_error ("Expected block name of '%s' in %s statement at %L",
6163 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6168 /* END INTERFACE has a special handler for its several possible endings. */
6169 if (*st
== ST_END_INTERFACE
)
6170 return gfc_match_end_interface ();
6172 /* We haven't hit the end of statement, so what is left must be an
6174 m
= gfc_match_space ();
6176 m
= gfc_match_name (name
);
6179 gfc_error ("Expected terminating name at %C");
6183 if (block_name
== NULL
)
6186 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6188 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
6189 gfc_ascii_statement (*st
));
6192 /* Procedure pointer as function result. */
6193 else if (strcmp (block_name
, "ppr@") == 0
6194 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6196 gfc_error ("Expected label '%s' for %s statement at %C",
6197 gfc_current_block ()->ns
->proc_name
->name
,
6198 gfc_ascii_statement (*st
));
6202 if (gfc_match_eos () == MATCH_YES
)
6206 gfc_syntax_error (*st
);
6209 gfc_current_locus
= old_loc
;
6211 /* If we are missing an END BLOCK, we created a half-ready namespace.
6212 Remove it from the parent namespace's sibling list. */
6214 if (state
== COMP_BLOCK
)
6216 parent_ns
= gfc_current_ns
->parent
;
6218 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6224 if (ns
== gfc_current_ns
)
6226 if (prev_ns
== NULL
)
6229 prev_ns
->sibling
= ns
->sibling
;
6235 gfc_free_namespace (gfc_current_ns
);
6236 gfc_current_ns
= parent_ns
;
6244 /***************** Attribute declaration statements ****************/
6246 /* Set the attribute of a single variable. */
6251 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6259 m
= gfc_match_name (name
);
6263 if (find_special (name
, &sym
, false))
6266 if (!check_function_name (name
))
6272 var_locus
= gfc_current_locus
;
6274 /* Deal with possible array specification for certain attributes. */
6275 if (current_attr
.dimension
6276 || current_attr
.codimension
6277 || current_attr
.allocatable
6278 || current_attr
.pointer
6279 || current_attr
.target
)
6281 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6282 !current_attr
.dimension
6283 && !current_attr
.pointer
6284 && !current_attr
.target
);
6285 if (m
== MATCH_ERROR
)
6288 if (current_attr
.dimension
&& m
== MATCH_NO
)
6290 gfc_error ("Missing array specification at %L in DIMENSION "
6291 "statement", &var_locus
);
6296 if (current_attr
.dimension
&& sym
->value
)
6298 gfc_error ("Dimensions specified for %s at %L after its "
6299 "initialisation", sym
->name
, &var_locus
);
6304 if (current_attr
.codimension
&& m
== MATCH_NO
)
6306 gfc_error ("Missing array specification at %L in CODIMENSION "
6307 "statement", &var_locus
);
6312 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6313 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6315 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6321 /* Update symbol table. DIMENSION attribute is set in
6322 gfc_set_array_spec(). For CLASS variables, this must be applied
6323 to the first component, or '_data' field. */
6324 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6326 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6334 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6335 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6342 if (sym
->ts
.type
== BT_CLASS
6343 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false))
6349 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6355 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6357 /* Fix the array spec. */
6358 m
= gfc_mod_pointee_as (sym
->as
);
6359 if (m
== MATCH_ERROR
)
6363 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6369 if ((current_attr
.external
|| current_attr
.intrinsic
)
6370 && sym
->attr
.flavor
!= FL_PROCEDURE
6371 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6377 add_hidden_procptr_result (sym
);
6382 gfc_free_array_spec (as
);
6387 /* Generic attribute declaration subroutine. Used for attributes that
6388 just have a list of names. */
6395 /* Gobble the optional double colon, by simply ignoring the result
6405 if (gfc_match_eos () == MATCH_YES
)
6411 if (gfc_match_char (',') != MATCH_YES
)
6413 gfc_error ("Unexpected character in variable list at %C");
6423 /* This routine matches Cray Pointer declarations of the form:
6424 pointer ( <pointer>, <pointee> )
6426 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6427 The pointer, if already declared, should be an integer. Otherwise, we
6428 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6429 be either a scalar, or an array declaration. No space is allocated for
6430 the pointee. For the statement
6431 pointer (ipt, ar(10))
6432 any subsequent uses of ar will be translated (in C-notation) as
6433 ar(i) => ((<type> *) ipt)(i)
6434 After gimplification, pointee variable will disappear in the code. */
6437 cray_pointer_decl (void)
6440 gfc_array_spec
*as
= NULL
;
6441 gfc_symbol
*cptr
; /* Pointer symbol. */
6442 gfc_symbol
*cpte
; /* Pointee symbol. */
6448 if (gfc_match_char ('(') != MATCH_YES
)
6450 gfc_error ("Expected '(' at %C");
6454 /* Match pointer. */
6455 var_locus
= gfc_current_locus
;
6456 gfc_clear_attr (¤t_attr
);
6457 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6458 current_ts
.type
= BT_INTEGER
;
6459 current_ts
.kind
= gfc_index_integer_kind
;
6461 m
= gfc_match_symbol (&cptr
, 0);
6464 gfc_error ("Expected variable name at %C");
6468 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6471 gfc_set_sym_referenced (cptr
);
6473 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6475 cptr
->ts
.type
= BT_INTEGER
;
6476 cptr
->ts
.kind
= gfc_index_integer_kind
;
6478 else if (cptr
->ts
.type
!= BT_INTEGER
)
6480 gfc_error ("Cray pointer at %C must be an integer");
6483 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6484 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6485 " memory addresses require %d bytes",
6486 cptr
->ts
.kind
, gfc_index_integer_kind
);
6488 if (gfc_match_char (',') != MATCH_YES
)
6490 gfc_error ("Expected \",\" at %C");
6494 /* Match Pointee. */
6495 var_locus
= gfc_current_locus
;
6496 gfc_clear_attr (¤t_attr
);
6497 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6498 current_ts
.type
= BT_UNKNOWN
;
6499 current_ts
.kind
= 0;
6501 m
= gfc_match_symbol (&cpte
, 0);
6504 gfc_error ("Expected variable name at %C");
6508 /* Check for an optional array spec. */
6509 m
= gfc_match_array_spec (&as
, true, false);
6510 if (m
== MATCH_ERROR
)
6512 gfc_free_array_spec (as
);
6515 else if (m
== MATCH_NO
)
6517 gfc_free_array_spec (as
);
6521 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6524 gfc_set_sym_referenced (cpte
);
6526 if (cpte
->as
== NULL
)
6528 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6529 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6531 else if (as
!= NULL
)
6533 gfc_error ("Duplicate array spec for Cray pointee at %C");
6534 gfc_free_array_spec (as
);
6540 if (cpte
->as
!= NULL
)
6542 /* Fix array spec. */
6543 m
= gfc_mod_pointee_as (cpte
->as
);
6544 if (m
== MATCH_ERROR
)
6548 /* Point the Pointee at the Pointer. */
6549 cpte
->cp_pointer
= cptr
;
6551 if (gfc_match_char (')') != MATCH_YES
)
6553 gfc_error ("Expected \")\" at %C");
6556 m
= gfc_match_char (',');
6558 done
= true; /* Stop searching for more declarations. */
6562 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6563 || gfc_match_eos () != MATCH_YES
)
6565 gfc_error ("Expected \",\" or end of statement at %C");
6573 gfc_match_external (void)
6576 gfc_clear_attr (¤t_attr
);
6577 current_attr
.external
= 1;
6579 return attr_decl ();
6584 gfc_match_intent (void)
6588 /* This is not allowed within a BLOCK construct! */
6589 if (gfc_current_state () == COMP_BLOCK
)
6591 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6595 intent
= match_intent_spec ();
6596 if (intent
== INTENT_UNKNOWN
)
6599 gfc_clear_attr (¤t_attr
);
6600 current_attr
.intent
= intent
;
6602 return attr_decl ();
6607 gfc_match_intrinsic (void)
6610 gfc_clear_attr (¤t_attr
);
6611 current_attr
.intrinsic
= 1;
6613 return attr_decl ();
6618 gfc_match_optional (void)
6620 /* This is not allowed within a BLOCK construct! */
6621 if (gfc_current_state () == COMP_BLOCK
)
6623 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6627 gfc_clear_attr (¤t_attr
);
6628 current_attr
.optional
= 1;
6630 return attr_decl ();
6635 gfc_match_pointer (void)
6637 gfc_gobble_whitespace ();
6638 if (gfc_peek_ascii_char () == '(')
6640 if (!gfc_option
.flag_cray_pointer
)
6642 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6646 return cray_pointer_decl ();
6650 gfc_clear_attr (¤t_attr
);
6651 current_attr
.pointer
= 1;
6653 return attr_decl ();
6659 gfc_match_allocatable (void)
6661 gfc_clear_attr (¤t_attr
);
6662 current_attr
.allocatable
= 1;
6664 return attr_decl ();
6669 gfc_match_codimension (void)
6671 gfc_clear_attr (¤t_attr
);
6672 current_attr
.codimension
= 1;
6674 return attr_decl ();
6679 gfc_match_contiguous (void)
6681 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6684 gfc_clear_attr (¤t_attr
);
6685 current_attr
.contiguous
= 1;
6687 return attr_decl ();
6692 gfc_match_dimension (void)
6694 gfc_clear_attr (¤t_attr
);
6695 current_attr
.dimension
= 1;
6697 return attr_decl ();
6702 gfc_match_target (void)
6704 gfc_clear_attr (¤t_attr
);
6705 current_attr
.target
= 1;
6707 return attr_decl ();
6711 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6715 access_attr_decl (gfc_statement st
)
6717 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6718 interface_type type
;
6720 gfc_symbol
*sym
, *dt_sym
;
6721 gfc_intrinsic_op op
;
6724 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6729 m
= gfc_match_generic_spec (&type
, name
, &op
);
6732 if (m
== MATCH_ERROR
)
6737 case INTERFACE_NAMELESS
:
6738 case INTERFACE_ABSTRACT
:
6741 case INTERFACE_GENERIC
:
6742 if (gfc_get_symbol (name
, NULL
, &sym
))
6745 if (!gfc_add_access (&sym
->attr
,
6747 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6751 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6752 && !gfc_add_access (&dt_sym
->attr
,
6754 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6760 case INTERFACE_INTRINSIC_OP
:
6761 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6763 gfc_intrinsic_op other_op
;
6765 gfc_current_ns
->operator_access
[op
] =
6766 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6768 /* Handle the case if there is another op with the same
6769 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6770 other_op
= gfc_equivalent_op (op
);
6772 if (other_op
!= INTRINSIC_NONE
)
6773 gfc_current_ns
->operator_access
[other_op
] =
6774 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6779 gfc_error ("Access specification of the %s operator at %C has "
6780 "already been specified", gfc_op2string (op
));
6786 case INTERFACE_USER_OP
:
6787 uop
= gfc_get_uop (name
);
6789 if (uop
->access
== ACCESS_UNKNOWN
)
6791 uop
->access
= (st
== ST_PUBLIC
)
6792 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6796 gfc_error ("Access specification of the .%s. operator at %C "
6797 "has already been specified", sym
->name
);
6804 if (gfc_match_char (',') == MATCH_NO
)
6808 if (gfc_match_eos () != MATCH_YES
)
6813 gfc_syntax_error (st
);
6821 gfc_match_protected (void)
6826 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6828 gfc_error ("PROTECTED at %C only allowed in specification "
6829 "part of a module");
6834 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6837 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6842 if (gfc_match_eos () == MATCH_YES
)
6847 m
= gfc_match_symbol (&sym
, 0);
6851 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
6863 if (gfc_match_eos () == MATCH_YES
)
6865 if (gfc_match_char (',') != MATCH_YES
)
6872 gfc_error ("Syntax error in PROTECTED statement at %C");
6877 /* The PRIVATE statement is a bit weird in that it can be an attribute
6878 declaration, but also works as a standalone statement inside of a
6879 type declaration or a module. */
6882 gfc_match_private (gfc_statement
*st
)
6885 if (gfc_match ("private") != MATCH_YES
)
6888 if (gfc_current_state () != COMP_MODULE
6889 && !(gfc_current_state () == COMP_DERIVED
6890 && gfc_state_stack
->previous
6891 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6892 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6893 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6894 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6896 gfc_error ("PRIVATE statement at %C is only allowed in the "
6897 "specification part of a module");
6901 if (gfc_current_state () == COMP_DERIVED
)
6903 if (gfc_match_eos () == MATCH_YES
)
6909 gfc_syntax_error (ST_PRIVATE
);
6913 if (gfc_match_eos () == MATCH_YES
)
6920 return access_attr_decl (ST_PRIVATE
);
6925 gfc_match_public (gfc_statement
*st
)
6928 if (gfc_match ("public") != MATCH_YES
)
6931 if (gfc_current_state () != COMP_MODULE
)
6933 gfc_error ("PUBLIC statement at %C is only allowed in the "
6934 "specification part of a module");
6938 if (gfc_match_eos () == MATCH_YES
)
6945 return access_attr_decl (ST_PUBLIC
);
6949 /* Workhorse for gfc_match_parameter. */
6959 m
= gfc_match_symbol (&sym
, 0);
6961 gfc_error ("Expected variable name at %C in PARAMETER statement");
6966 if (gfc_match_char ('=') == MATCH_NO
)
6968 gfc_error ("Expected = sign in PARAMETER statement at %C");
6972 m
= gfc_match_init_expr (&init
);
6974 gfc_error ("Expected expression at %C in PARAMETER statement");
6978 if (sym
->ts
.type
== BT_UNKNOWN
6979 && !gfc_set_default_type (sym
, 1, NULL
))
6985 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
6986 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
6994 gfc_error ("Initializing already initialized variable at %C");
6999 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7000 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7003 gfc_free_expr (init
);
7008 /* Match a parameter statement, with the weird syntax that these have. */
7011 gfc_match_parameter (void)
7015 if (gfc_match_char ('(') == MATCH_NO
)
7024 if (gfc_match (" )%t") == MATCH_YES
)
7027 if (gfc_match_char (',') != MATCH_YES
)
7029 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7039 /* Save statements have a special syntax. */
7042 gfc_match_save (void)
7044 char n
[GFC_MAX_SYMBOL_LEN
+1];
7049 if (gfc_match_eos () == MATCH_YES
)
7051 if (gfc_current_ns
->seen_save
)
7053 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7054 "follows previous SAVE statement"))
7058 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7062 if (gfc_current_ns
->save_all
)
7064 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7065 "blanket SAVE statement"))
7073 m
= gfc_match_symbol (&sym
, 0);
7077 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7078 &gfc_current_locus
))
7089 m
= gfc_match (" / %n /", &n
);
7090 if (m
== MATCH_ERROR
)
7095 c
= gfc_get_common (n
, 0);
7098 gfc_current_ns
->seen_save
= 1;
7101 if (gfc_match_eos () == MATCH_YES
)
7103 if (gfc_match_char (',') != MATCH_YES
)
7110 gfc_error ("Syntax error in SAVE statement at %C");
7116 gfc_match_value (void)
7121 /* This is not allowed within a BLOCK construct! */
7122 if (gfc_current_state () == COMP_BLOCK
)
7124 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7128 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7131 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7136 if (gfc_match_eos () == MATCH_YES
)
7141 m
= gfc_match_symbol (&sym
, 0);
7145 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7157 if (gfc_match_eos () == MATCH_YES
)
7159 if (gfc_match_char (',') != MATCH_YES
)
7166 gfc_error ("Syntax error in VALUE statement at %C");
7172 gfc_match_volatile (void)
7177 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7180 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7185 if (gfc_match_eos () == MATCH_YES
)
7190 /* VOLATILE is special because it can be added to host-associated
7191 symbols locally. Except for coarrays. */
7192 m
= gfc_match_symbol (&sym
, 1);
7196 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7197 for variable in a BLOCK which is defined outside of the BLOCK. */
7198 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7200 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7201 "%C, which is use-/host-associated", sym
->name
);
7204 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7216 if (gfc_match_eos () == MATCH_YES
)
7218 if (gfc_match_char (',') != MATCH_YES
)
7225 gfc_error ("Syntax error in VOLATILE statement at %C");
7231 gfc_match_asynchronous (void)
7236 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7239 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7244 if (gfc_match_eos () == MATCH_YES
)
7249 /* ASYNCHRONOUS is special because it can be added to host-associated
7251 m
= gfc_match_symbol (&sym
, 1);
7255 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7267 if (gfc_match_eos () == MATCH_YES
)
7269 if (gfc_match_char (',') != MATCH_YES
)
7276 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7281 /* Match a module procedure statement. Note that we have to modify
7282 symbols in the parent's namespace because the current one was there
7283 to receive symbols that are in an interface's formal argument list. */
7286 gfc_match_modproc (void)
7288 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7292 gfc_namespace
*module_ns
;
7293 gfc_interface
*old_interface_head
, *interface
;
7295 if (gfc_state_stack
->state
!= COMP_INTERFACE
7296 || gfc_state_stack
->previous
== NULL
7297 || current_interface
.type
== INTERFACE_NAMELESS
7298 || current_interface
.type
== INTERFACE_ABSTRACT
)
7300 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7305 module_ns
= gfc_current_ns
->parent
;
7306 for (; module_ns
; module_ns
= module_ns
->parent
)
7307 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7308 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7309 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7310 && !module_ns
->proc_name
->attr
.contained
))
7313 if (module_ns
== NULL
)
7316 /* Store the current state of the interface. We will need it if we
7317 end up with a syntax error and need to recover. */
7318 old_interface_head
= gfc_current_interface_head ();
7320 /* Check if the F2008 optional double colon appears. */
7321 gfc_gobble_whitespace ();
7322 old_locus
= gfc_current_locus
;
7323 if (gfc_match ("::") == MATCH_YES
)
7325 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7326 "MODULE PROCEDURE statement at %L", &old_locus
))
7330 gfc_current_locus
= old_locus
;
7335 old_locus
= gfc_current_locus
;
7337 m
= gfc_match_name (name
);
7343 /* Check for syntax error before starting to add symbols to the
7344 current namespace. */
7345 if (gfc_match_eos () == MATCH_YES
)
7348 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7351 /* Now we're sure the syntax is valid, we process this item
7353 if (gfc_get_symbol (name
, module_ns
, &sym
))
7356 if (sym
->attr
.intrinsic
)
7358 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7359 "PROCEDURE", &old_locus
);
7363 if (sym
->attr
.proc
!= PROC_MODULE
7364 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7367 if (!gfc_add_interface (sym
))
7370 sym
->attr
.mod_proc
= 1;
7371 sym
->declared_at
= old_locus
;
7380 /* Restore the previous state of the interface. */
7381 interface
= gfc_current_interface_head ();
7382 gfc_set_current_interface_head (old_interface_head
);
7384 /* Free the new interfaces. */
7385 while (interface
!= old_interface_head
)
7387 gfc_interface
*i
= interface
->next
;
7392 /* And issue a syntax error. */
7393 gfc_syntax_error (ST_MODULE_PROC
);
7398 /* Check a derived type that is being extended. */
7401 check_extended_derived_type (char *name
)
7403 gfc_symbol
*extended
;
7405 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7407 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7411 extended
= gfc_find_dt_in_generic (extended
);
7416 gfc_error ("Symbol '%s' at %C has not been previously defined", name
);
7420 if (extended
->attr
.flavor
!= FL_DERIVED
)
7422 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7423 "derived type", name
);
7427 if (extended
->attr
.is_bind_c
)
7429 gfc_error ("'%s' cannot be extended at %C because it "
7430 "is BIND(C)", extended
->name
);
7434 if (extended
->attr
.sequence
)
7436 gfc_error ("'%s' cannot be extended at %C because it "
7437 "is a SEQUENCE type", extended
->name
);
7445 /* Match the optional attribute specifiers for a type declaration.
7446 Return MATCH_ERROR if an error is encountered in one of the handled
7447 attributes (public, private, bind(c)), MATCH_NO if what's found is
7448 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7449 checking on attribute conflicts needs to be done. */
7452 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7454 /* See if the derived type is marked as private. */
7455 if (gfc_match (" , private") == MATCH_YES
)
7457 if (gfc_current_state () != COMP_MODULE
)
7459 gfc_error ("Derived type at %C can only be PRIVATE in the "
7460 "specification part of a module");
7464 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7467 else if (gfc_match (" , public") == MATCH_YES
)
7469 if (gfc_current_state () != COMP_MODULE
)
7471 gfc_error ("Derived type at %C can only be PUBLIC in the "
7472 "specification part of a module");
7476 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7479 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7481 /* If the type is defined to be bind(c) it then needs to make
7482 sure that all fields are interoperable. This will
7483 need to be a semantic check on the finished derived type.
7484 See 15.2.3 (lines 9-12) of F2003 draft. */
7485 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7488 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7490 else if (gfc_match (" , abstract") == MATCH_YES
)
7492 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7495 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7498 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7500 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7506 /* If we get here, something matched. */
7511 /* Match the beginning of a derived type declaration. If a type name
7512 was the result of a function, then it is possible to have a symbol
7513 already to be known as a derived type yet have no components. */
7516 gfc_match_derived_decl (void)
7518 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7519 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7520 symbol_attribute attr
;
7521 gfc_symbol
*sym
, *gensym
;
7522 gfc_symbol
*extended
;
7524 match is_type_attr_spec
= MATCH_NO
;
7525 bool seen_attr
= false;
7526 gfc_interface
*intr
= NULL
, *head
;
7528 if (gfc_current_state () == COMP_DERIVED
)
7533 gfc_clear_attr (&attr
);
7538 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7539 if (is_type_attr_spec
== MATCH_ERROR
)
7541 if (is_type_attr_spec
== MATCH_YES
)
7543 } while (is_type_attr_spec
== MATCH_YES
);
7545 /* Deal with derived type extensions. The extension attribute has
7546 been added to 'attr' but now the parent type must be found and
7549 extended
= check_extended_derived_type (parent
);
7551 if (parent
[0] && !extended
)
7554 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7556 gfc_error ("Expected :: in TYPE definition at %C");
7560 m
= gfc_match (" %n%t", name
);
7564 /* Make sure the name is not the name of an intrinsic type. */
7565 if (gfc_is_intrinsic_typename (name
))
7567 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7572 if (gfc_get_symbol (name
, NULL
, &gensym
))
7575 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7577 gfc_error ("Derived type name '%s' at %C already has a basic type "
7578 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7582 if (!gensym
->attr
.generic
7583 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7586 if (!gensym
->attr
.function
7587 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7590 sym
= gfc_find_dt_in_generic (gensym
);
7592 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7594 gfc_error ("Derived type definition of '%s' at %C has already been "
7595 "defined", sym
->name
);
7601 /* Use upper case to save the actual derived-type symbol. */
7602 gfc_get_symbol (gfc_get_string ("%c%s",
7603 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7604 &gensym
->name
[1]), NULL
, &sym
);
7605 sym
->name
= gfc_get_string (gensym
->name
);
7606 head
= gensym
->generic
;
7607 intr
= gfc_get_interface ();
7609 intr
->where
= gfc_current_locus
;
7610 intr
->sym
->declared_at
= gfc_current_locus
;
7612 gensym
->generic
= intr
;
7613 gensym
->attr
.if_source
= IFSRC_DECL
;
7616 /* The symbol may already have the derived attribute without the
7617 components. The ways this can happen is via a function
7618 definition, an INTRINSIC statement or a subtype in another
7619 derived type that is a pointer. The first part of the AND clause
7620 is true if the symbol is not the return value of a function. */
7621 if (sym
->attr
.flavor
!= FL_DERIVED
7622 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7625 if (attr
.access
!= ACCESS_UNKNOWN
7626 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7628 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7629 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7630 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7634 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7635 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7636 gensym
->attr
.access
= sym
->attr
.access
;
7638 /* See if the derived type was labeled as bind(c). */
7639 if (attr
.is_bind_c
!= 0)
7640 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7642 /* Construct the f2k_derived namespace if it is not yet there. */
7643 if (!sym
->f2k_derived
)
7644 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7646 if (extended
&& !sym
->components
)
7651 /* Add the extended derived type as the first component. */
7652 gfc_add_component (sym
, parent
, &p
);
7654 gfc_set_sym_referenced (extended
);
7656 p
->ts
.type
= BT_DERIVED
;
7657 p
->ts
.u
.derived
= extended
;
7658 p
->initializer
= gfc_default_initializer (&p
->ts
);
7660 /* Set extension level. */
7661 if (extended
->attr
.extension
== 255)
7663 /* Since the extension field is 8 bit wide, we can only have
7664 up to 255 extension levels. */
7665 gfc_error ("Maximum extension level reached with type '%s' at %L",
7666 extended
->name
, &extended
->declared_at
);
7669 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7671 /* Provide the links between the extended type and its extension. */
7672 if (!extended
->f2k_derived
)
7673 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7674 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7678 if (!sym
->hash_value
)
7679 /* Set the hash for the compound name for this type. */
7680 sym
->hash_value
= gfc_hash_value (sym
);
7682 /* Take over the ABSTRACT attribute. */
7683 sym
->attr
.abstract
= attr
.abstract
;
7685 gfc_new_block
= sym
;
7691 /* Cray Pointees can be declared as:
7692 pointer (ipt, a (n,m,...,*)) */
7695 gfc_mod_pointee_as (gfc_array_spec
*as
)
7697 as
->cray_pointee
= true; /* This will be useful to know later. */
7698 if (as
->type
== AS_ASSUMED_SIZE
)
7699 as
->cp_was_assumed
= true;
7700 else if (as
->type
== AS_ASSUMED_SHAPE
)
7702 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7709 /* Match the enum definition statement, here we are trying to match
7710 the first line of enum definition statement.
7711 Returns MATCH_YES if match is found. */
7714 gfc_match_enum (void)
7718 m
= gfc_match_eos ();
7722 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7729 /* Returns an initializer whose value is one higher than the value of the
7730 LAST_INITIALIZER argument. If the argument is NULL, the
7731 initializers value will be set to zero. The initializer's kind
7732 will be set to gfc_c_int_kind.
7734 If -fshort-enums is given, the appropriate kind will be selected
7735 later after all enumerators have been parsed. A warning is issued
7736 here if an initializer exceeds gfc_c_int_kind. */
7739 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7742 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7744 mpz_init (result
->value
.integer
);
7746 if (last_initializer
!= NULL
)
7748 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7749 result
->where
= last_initializer
->where
;
7751 if (gfc_check_integer_range (result
->value
.integer
,
7752 gfc_c_int_kind
) != ARITH_OK
)
7754 gfc_error ("Enumerator exceeds the C integer type at %C");
7760 /* Control comes here, if it's the very first enumerator and no
7761 initializer has been given. It will be initialized to zero. */
7762 mpz_set_si (result
->value
.integer
, 0);
7769 /* Match a variable name with an optional initializer. When this
7770 subroutine is called, a variable is expected to be parsed next.
7771 Depending on what is happening at the moment, updates either the
7772 symbol table or the current interface. */
7775 enumerator_decl (void)
7777 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7778 gfc_expr
*initializer
;
7779 gfc_array_spec
*as
= NULL
;
7787 old_locus
= gfc_current_locus
;
7789 /* When we get here, we've just matched a list of attributes and
7790 maybe a type and a double colon. The next thing we expect to see
7791 is the name of the symbol. */
7792 m
= gfc_match_name (name
);
7796 var_locus
= gfc_current_locus
;
7798 /* OK, we've successfully matched the declaration. Now put the
7799 symbol in the current namespace. If we fail to create the symbol,
7801 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7807 /* The double colon must be present in order to have initializers.
7808 Otherwise the statement is ambiguous with an assignment statement. */
7811 if (gfc_match_char ('=') == MATCH_YES
)
7813 m
= gfc_match_init_expr (&initializer
);
7816 gfc_error ("Expected an initialization expression at %C");
7825 /* If we do not have an initializer, the initialization value of the
7826 previous enumerator (stored in last_initializer) is incremented
7827 by 1 and is used to initialize the current enumerator. */
7828 if (initializer
== NULL
)
7829 initializer
= enum_initializer (last_initializer
, old_locus
);
7831 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7833 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7839 /* Store this current initializer, for the next enumerator variable
7840 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7841 use last_initializer below. */
7842 last_initializer
= initializer
;
7843 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7845 /* Maintain enumerator history. */
7846 gfc_find_symbol (name
, NULL
, 0, &sym
);
7847 create_enum_history (sym
, last_initializer
);
7849 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7852 /* Free stuff up and return. */
7853 gfc_free_expr (initializer
);
7859 /* Match the enumerator definition statement. */
7862 gfc_match_enumerator_def (void)
7867 gfc_clear_ts (¤t_ts
);
7869 m
= gfc_match (" enumerator");
7873 m
= gfc_match (" :: ");
7874 if (m
== MATCH_ERROR
)
7877 colon_seen
= (m
== MATCH_YES
);
7879 if (gfc_current_state () != COMP_ENUM
)
7881 gfc_error ("ENUM definition statement expected before %C");
7882 gfc_free_enum_history ();
7886 (¤t_ts
)->type
= BT_INTEGER
;
7887 (¤t_ts
)->kind
= gfc_c_int_kind
;
7889 gfc_clear_attr (¤t_attr
);
7890 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7899 m
= enumerator_decl ();
7900 if (m
== MATCH_ERROR
)
7902 gfc_free_enum_history ();
7908 if (gfc_match_eos () == MATCH_YES
)
7910 if (gfc_match_char (',') != MATCH_YES
)
7914 if (gfc_current_state () == COMP_ENUM
)
7916 gfc_free_enum_history ();
7917 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7922 gfc_free_array_spec (current_as
);
7929 /* Match binding attributes. */
7932 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7934 bool found_passing
= false;
7935 bool seen_ptr
= false;
7936 match m
= MATCH_YES
;
7938 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7939 this case the defaults are in there. */
7940 ba
->access
= ACCESS_UNKNOWN
;
7941 ba
->pass_arg
= NULL
;
7942 ba
->pass_arg_num
= 0;
7944 ba
->non_overridable
= 0;
7948 /* If we find a comma, we believe there are binding attributes. */
7949 m
= gfc_match_char (',');
7955 /* Access specifier. */
7957 m
= gfc_match (" public");
7958 if (m
== MATCH_ERROR
)
7962 if (ba
->access
!= ACCESS_UNKNOWN
)
7964 gfc_error ("Duplicate access-specifier at %C");
7968 ba
->access
= ACCESS_PUBLIC
;
7972 m
= gfc_match (" private");
7973 if (m
== MATCH_ERROR
)
7977 if (ba
->access
!= ACCESS_UNKNOWN
)
7979 gfc_error ("Duplicate access-specifier at %C");
7983 ba
->access
= ACCESS_PRIVATE
;
7987 /* If inside GENERIC, the following is not allowed. */
7992 m
= gfc_match (" nopass");
7993 if (m
== MATCH_ERROR
)
7999 gfc_error ("Binding attributes already specify passing,"
8000 " illegal NOPASS at %C");
8004 found_passing
= true;
8009 /* PASS possibly including argument. */
8010 m
= gfc_match (" pass");
8011 if (m
== MATCH_ERROR
)
8015 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8019 gfc_error ("Binding attributes already specify passing,"
8020 " illegal PASS at %C");
8024 m
= gfc_match (" ( %n )", arg
);
8025 if (m
== MATCH_ERROR
)
8028 ba
->pass_arg
= gfc_get_string (arg
);
8029 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8031 found_passing
= true;
8039 m
= gfc_match (" pointer");
8040 if (m
== MATCH_ERROR
)
8046 gfc_error ("Duplicate POINTER attribute at %C");
8056 /* NON_OVERRIDABLE flag. */
8057 m
= gfc_match (" non_overridable");
8058 if (m
== MATCH_ERROR
)
8062 if (ba
->non_overridable
)
8064 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8068 ba
->non_overridable
= 1;
8072 /* DEFERRED flag. */
8073 m
= gfc_match (" deferred");
8074 if (m
== MATCH_ERROR
)
8080 gfc_error ("Duplicate DEFERRED at %C");
8091 /* Nothing matching found. */
8093 gfc_error ("Expected access-specifier at %C");
8095 gfc_error ("Expected binding attribute at %C");
8098 while (gfc_match_char (',') == MATCH_YES
);
8100 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8101 if (ba
->non_overridable
&& ba
->deferred
)
8103 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8110 if (ba
->access
== ACCESS_UNKNOWN
)
8111 ba
->access
= gfc_typebound_default_access
;
8113 if (ppc
&& !seen_ptr
)
8115 gfc_error ("POINTER attribute is required for procedure pointer component"
8127 /* Match a PROCEDURE specific binding inside a derived type. */
8130 match_procedure_in_type (void)
8132 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8133 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8134 char* target
= NULL
, *ifc
= NULL
;
8135 gfc_typebound_proc tb
;
8144 /* Check current state. */
8145 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8146 block
= gfc_state_stack
->previous
->sym
;
8149 /* Try to match PROCEDURE(interface). */
8150 if (gfc_match (" (") == MATCH_YES
)
8152 m
= gfc_match_name (target_buf
);
8153 if (m
== MATCH_ERROR
)
8157 gfc_error ("Interface-name expected after '(' at %C");
8161 if (gfc_match (" )") != MATCH_YES
)
8163 gfc_error ("')' expected at %C");
8170 /* Construct the data structure. */
8171 memset (&tb
, 0, sizeof (tb
));
8172 tb
.where
= gfc_current_locus
;
8174 /* Match binding attributes. */
8175 m
= match_binding_attributes (&tb
, false, false);
8176 if (m
== MATCH_ERROR
)
8178 seen_attrs
= (m
== MATCH_YES
);
8180 /* Check that attribute DEFERRED is given if an interface is specified. */
8181 if (tb
.deferred
&& !ifc
)
8183 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8186 if (ifc
&& !tb
.deferred
)
8188 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8192 /* Match the colons. */
8193 m
= gfc_match (" ::");
8194 if (m
== MATCH_ERROR
)
8196 seen_colons
= (m
== MATCH_YES
);
8197 if (seen_attrs
&& !seen_colons
)
8199 gfc_error ("Expected '::' after binding-attributes at %C");
8203 /* Match the binding names. */
8206 m
= gfc_match_name (name
);
8207 if (m
== MATCH_ERROR
)
8211 gfc_error ("Expected binding name at %C");
8215 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8218 /* Try to match the '=> target', if it's there. */
8220 m
= gfc_match (" =>");
8221 if (m
== MATCH_ERROR
)
8227 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8233 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8238 m
= gfc_match_name (target_buf
);
8239 if (m
== MATCH_ERROR
)
8243 gfc_error ("Expected binding target after '=>' at %C");
8246 target
= target_buf
;
8249 /* If no target was found, it has the same name as the binding. */
8253 /* Get the namespace to insert the symbols into. */
8254 ns
= block
->f2k_derived
;
8257 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8258 if (tb
.deferred
&& !block
->attr
.abstract
)
8260 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8261 "is not ABSTRACT", block
->name
);
8265 /* See if we already have a binding with this name in the symtree which
8266 would be an error. If a GENERIC already targeted this binding, it may
8267 be already there but then typebound is still NULL. */
8268 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8269 if (stree
&& stree
->n
.tb
)
8271 gfc_error ("There is already a procedure with binding name '%s' for "
8272 "the derived type '%s' at %C", name
, block
->name
);
8276 /* Insert it and set attributes. */
8280 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8283 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8285 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8288 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8290 if (gfc_match_eos () == MATCH_YES
)
8292 if (gfc_match_char (',') != MATCH_YES
)
8297 gfc_error ("Syntax error in PROCEDURE statement at %C");
8302 /* Match a GENERIC procedure binding inside a derived type. */
8305 gfc_match_generic (void)
8307 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8308 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8310 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8311 gfc_typebound_proc
* tb
;
8313 interface_type op_type
;
8314 gfc_intrinsic_op op
;
8317 /* Check current state. */
8318 if (gfc_current_state () == COMP_DERIVED
)
8320 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8323 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8325 block
= gfc_state_stack
->previous
->sym
;
8326 ns
= block
->f2k_derived
;
8327 gcc_assert (block
&& ns
);
8329 memset (&tbattr
, 0, sizeof (tbattr
));
8330 tbattr
.where
= gfc_current_locus
;
8332 /* See if we get an access-specifier. */
8333 m
= match_binding_attributes (&tbattr
, true, false);
8334 if (m
== MATCH_ERROR
)
8337 /* Now the colons, those are required. */
8338 if (gfc_match (" ::") != MATCH_YES
)
8340 gfc_error ("Expected '::' at %C");
8344 /* Match the binding name; depending on type (operator / generic) format
8345 it for future error messages into bind_name. */
8347 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8348 if (m
== MATCH_ERROR
)
8352 gfc_error ("Expected generic name or operator descriptor at %C");
8358 case INTERFACE_GENERIC
:
8359 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8362 case INTERFACE_USER_OP
:
8363 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8366 case INTERFACE_INTRINSIC_OP
:
8367 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8368 gfc_op2string (op
));
8375 /* Match the required =>. */
8376 if (gfc_match (" =>") != MATCH_YES
)
8378 gfc_error ("Expected '=>' at %C");
8382 /* Try to find existing GENERIC binding with this name / for this operator;
8383 if there is something, check that it is another GENERIC and then extend
8384 it rather than building a new node. Otherwise, create it and put it
8385 at the right position. */
8389 case INTERFACE_USER_OP
:
8390 case INTERFACE_GENERIC
:
8392 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8395 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8407 case INTERFACE_INTRINSIC_OP
:
8417 if (!tb
->is_generic
)
8419 gcc_assert (op_type
== INTERFACE_GENERIC
);
8420 gfc_error ("There's already a non-generic procedure with binding name"
8421 " '%s' for the derived type '%s' at %C",
8422 bind_name
, block
->name
);
8426 if (tb
->access
!= tbattr
.access
)
8428 gfc_error ("Binding at %C must have the same access as already"
8429 " defined binding '%s'", bind_name
);
8435 tb
= gfc_get_typebound_proc (NULL
);
8436 tb
->where
= gfc_current_locus
;
8437 tb
->access
= tbattr
.access
;
8439 tb
->u
.generic
= NULL
;
8443 case INTERFACE_GENERIC
:
8444 case INTERFACE_USER_OP
:
8446 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8449 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8457 case INTERFACE_INTRINSIC_OP
:
8466 /* Now, match all following names as specific targets. */
8469 gfc_symtree
* target_st
;
8470 gfc_tbp_generic
* target
;
8472 m
= gfc_match_name (name
);
8473 if (m
== MATCH_ERROR
)
8477 gfc_error ("Expected specific binding name at %C");
8481 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8483 /* See if this is a duplicate specification. */
8484 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8485 if (target_st
== target
->specific_st
)
8487 gfc_error ("'%s' already defined as specific binding for the"
8488 " generic '%s' at %C", name
, bind_name
);
8492 target
= gfc_get_tbp_generic ();
8493 target
->specific_st
= target_st
;
8494 target
->specific
= NULL
;
8495 target
->next
= tb
->u
.generic
;
8496 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8497 || (op_type
== INTERFACE_INTRINSIC_OP
));
8498 tb
->u
.generic
= target
;
8500 while (gfc_match (" ,") == MATCH_YES
);
8502 /* Here should be the end. */
8503 if (gfc_match_eos () != MATCH_YES
)
8505 gfc_error ("Junk after GENERIC binding at %C");
8516 /* Match a FINAL declaration inside a derived type. */
8519 gfc_match_final_decl (void)
8521 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8524 gfc_namespace
* module_ns
;
8528 if (gfc_current_form
== FORM_FREE
)
8530 char c
= gfc_peek_ascii_char ();
8531 if (!gfc_is_whitespace (c
) && c
!= ':')
8535 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8537 if (gfc_current_form
== FORM_FIXED
)
8540 gfc_error ("FINAL declaration at %C must be inside a derived type "
8541 "CONTAINS section");
8545 block
= gfc_state_stack
->previous
->sym
;
8548 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8549 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8551 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8552 " specification part of a MODULE");
8556 module_ns
= gfc_current_ns
;
8557 gcc_assert (module_ns
);
8558 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8560 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8561 if (gfc_match (" ::") == MATCH_ERROR
)
8564 /* Match the sequence of procedure names. */
8571 if (first
&& gfc_match_eos () == MATCH_YES
)
8573 gfc_error ("Empty FINAL at %C");
8577 m
= gfc_match_name (name
);
8580 gfc_error ("Expected module procedure name at %C");
8583 else if (m
!= MATCH_YES
)
8586 if (gfc_match_eos () == MATCH_YES
)
8588 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8590 gfc_error ("Expected ',' at %C");
8594 if (gfc_get_symbol (name
, module_ns
, &sym
))
8596 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8600 /* Mark the symbol as module procedure. */
8601 if (sym
->attr
.proc
!= PROC_MODULE
8602 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8605 /* Check if we already have this symbol in the list, this is an error. */
8606 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8607 if (f
->proc_sym
== sym
)
8609 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8614 /* Add this symbol to the list of finalizers. */
8615 gcc_assert (block
->f2k_derived
);
8617 f
= XCNEW (gfc_finalizer
);
8619 f
->proc_tree
= NULL
;
8620 f
->where
= gfc_current_locus
;
8621 f
->next
= block
->f2k_derived
->finalizers
;
8622 block
->f2k_derived
->finalizers
= f
;
8632 const ext_attr_t ext_attr_list
[] = {
8633 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8634 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8635 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8636 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8637 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8638 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8639 { NULL
, EXT_ATTR_LAST
, NULL
}
8642 /* Match a !GCC$ ATTRIBUTES statement of the form:
8643 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8644 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8646 TODO: We should support all GCC attributes using the same syntax for
8647 the attribute list, i.e. the list in C
8648 __attributes(( attribute-list ))
8650 !GCC$ ATTRIBUTES attribute-list ::
8651 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8654 As there is absolutely no risk of confusion, we should never return
8657 gfc_match_gcc_attributes (void)
8659 symbol_attribute attr
;
8660 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8665 gfc_clear_attr (&attr
);
8670 if (gfc_match_name (name
) != MATCH_YES
)
8673 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8674 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8677 if (id
== EXT_ATTR_LAST
)
8679 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8683 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8686 gfc_gobble_whitespace ();
8687 ch
= gfc_next_ascii_char ();
8690 /* This is the successful exit condition for the loop. */
8691 if (gfc_next_ascii_char () == ':')
8701 if (gfc_match_eos () == MATCH_YES
)
8706 m
= gfc_match_name (name
);
8710 if (find_special (name
, &sym
, true))
8713 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8715 if (gfc_match_eos () == MATCH_YES
)
8718 if (gfc_match_char (',') != MATCH_YES
)
8725 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");