1 /* Declaration statement matcher
2 Copyright (C) 2002-2015 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"
36 #include "stringpool.h"
38 /* Macros to access allocate memory for gfc_data_variable,
39 gfc_data_value and gfc_data. */
40 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
41 #define gfc_get_data_value() XCNEW (gfc_data_value)
42 #define gfc_get_data() XCNEW (gfc_data)
45 static bool set_binding_label (const char **, const char *, int);
48 /* This flag is set if an old-style length selector is matched
49 during a type-declaration statement. */
51 static int old_char_selector
;
53 /* When variables acquire types and attributes from a declaration
54 statement, they get them from the following static variables. The
55 first part of a declaration sets these variables and the second
56 part copies these into symbol structures. */
58 static gfc_typespec current_ts
;
60 static symbol_attribute current_attr
;
61 static gfc_array_spec
*current_as
;
62 static int colon_seen
;
64 /* The current binding label (if any). */
65 static const char* curr_binding_label
;
66 /* Need to know how many identifiers are on the current data declaration
67 line in case we're given the BIND(C) attribute with a NAME= specifier. */
68 static int num_idents_on_line
;
69 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
70 can supply a name if the curr_binding_label is nil and NAME= was not. */
71 static int has_name_equals
= 0;
73 /* Initializer of the previous enumerator. */
75 static gfc_expr
*last_initializer
;
77 /* History of all the enumerators is maintained, so that
78 kind values of all the enumerators could be updated depending
79 upon the maximum initialized value. */
81 typedef struct enumerator_history
84 gfc_expr
*initializer
;
85 struct enumerator_history
*next
;
89 /* Header of enum history chain. */
91 static enumerator_history
*enum_history
= NULL
;
93 /* Pointer of enum history node containing largest initializer. */
95 static enumerator_history
*max_enum
= NULL
;
97 /* gfc_new_block points to the symbol of a newly matched block. */
99 gfc_symbol
*gfc_new_block
;
101 bool gfc_matching_function
;
104 /********************* DATA statement subroutines *********************/
106 static bool in_match_data
= false;
109 gfc_in_match_data (void)
111 return in_match_data
;
115 set_in_match_data (bool set_value
)
117 in_match_data
= set_value
;
120 /* Free a gfc_data_variable structure and everything beneath it. */
123 free_variable (gfc_data_variable
*p
)
125 gfc_data_variable
*q
;
130 gfc_free_expr (p
->expr
);
131 gfc_free_iterator (&p
->iter
, 0);
132 free_variable (p
->list
);
138 /* Free a gfc_data_value structure and everything beneath it. */
141 free_value (gfc_data_value
*p
)
148 mpz_clear (p
->repeat
);
149 gfc_free_expr (p
->expr
);
155 /* Free a list of gfc_data structures. */
158 gfc_free_data (gfc_data
*p
)
165 free_variable (p
->var
);
166 free_value (p
->value
);
172 /* Free all data in a namespace. */
175 gfc_free_data_all (gfc_namespace
*ns
)
187 /* Reject data parsed since the last restore point was marked. */
190 gfc_reject_data (gfc_namespace
*ns
)
194 while (ns
->data
&& ns
->data
!= ns
->old_data
)
202 static match
var_element (gfc_data_variable
*);
204 /* Match a list of variables terminated by an iterator and a right
208 var_list (gfc_data_variable
*parent
)
210 gfc_data_variable
*tail
, var
;
213 m
= var_element (&var
);
214 if (m
== MATCH_ERROR
)
219 tail
= gfc_get_data_variable ();
226 if (gfc_match_char (',') != MATCH_YES
)
229 m
= gfc_match_iterator (&parent
->iter
, 1);
232 if (m
== MATCH_ERROR
)
235 m
= var_element (&var
);
236 if (m
== MATCH_ERROR
)
241 tail
->next
= gfc_get_data_variable ();
247 if (gfc_match_char (')') != MATCH_YES
)
252 gfc_syntax_error (ST_DATA
);
257 /* Match a single element in a data variable list, which can be a
258 variable-iterator list. */
261 var_element (gfc_data_variable
*new_var
)
266 memset (new_var
, 0, sizeof (gfc_data_variable
));
268 if (gfc_match_char ('(') == MATCH_YES
)
269 return var_list (new_var
);
271 m
= gfc_match_variable (&new_var
->expr
, 0);
275 sym
= new_var
->expr
->symtree
->n
.sym
;
277 /* Symbol should already have an associated type. */
278 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
281 if (!sym
->attr
.function
&& gfc_current_ns
->parent
282 && gfc_current_ns
->parent
== sym
->ns
)
284 gfc_error ("Host associated variable %qs may not be in the DATA "
285 "statement at %C", sym
->name
);
289 if (gfc_current_state () != COMP_BLOCK_DATA
290 && sym
->attr
.in_common
291 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
292 "common block variable %qs in DATA statement at %C",
296 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
303 /* Match the top-level list of data variables. */
306 top_var_list (gfc_data
*d
)
308 gfc_data_variable var
, *tail
, *new_var
;
315 m
= var_element (&var
);
318 if (m
== MATCH_ERROR
)
321 new_var
= gfc_get_data_variable ();
327 tail
->next
= new_var
;
331 if (gfc_match_char ('/') == MATCH_YES
)
333 if (gfc_match_char (',') != MATCH_YES
)
340 gfc_syntax_error (ST_DATA
);
341 gfc_free_data_all (gfc_current_ns
);
347 match_data_constant (gfc_expr
**result
)
349 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
350 gfc_symbol
*sym
, *dt_sym
= NULL
;
355 m
= gfc_match_literal_constant (&expr
, 1);
362 if (m
== MATCH_ERROR
)
365 m
= gfc_match_null (result
);
369 old_loc
= gfc_current_locus
;
371 /* Should this be a structure component, try to match it
372 before matching a name. */
373 m
= gfc_match_rvalue (result
);
374 if (m
== MATCH_ERROR
)
377 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
379 if (!gfc_simplify_expr (*result
, 0))
383 else if (m
== MATCH_YES
)
384 gfc_free_expr (*result
);
386 gfc_current_locus
= old_loc
;
388 m
= gfc_match_name (name
);
392 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
395 if (sym
&& sym
->attr
.generic
)
396 dt_sym
= gfc_find_dt_in_generic (sym
);
399 || (sym
->attr
.flavor
!= FL_PARAMETER
400 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
402 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
406 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
407 return gfc_match_structure_constructor (dt_sym
, result
);
409 /* Check to see if the value is an initialization array expression. */
410 if (sym
->value
->expr_type
== EXPR_ARRAY
)
412 gfc_current_locus
= old_loc
;
414 m
= gfc_match_init_expr (result
);
415 if (m
== MATCH_ERROR
)
420 if (!gfc_simplify_expr (*result
, 0))
423 if ((*result
)->expr_type
== EXPR_CONSTANT
)
427 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
433 *result
= gfc_copy_expr (sym
->value
);
438 /* Match a list of values in a DATA statement. The leading '/' has
439 already been seen at this point. */
442 top_val_list (gfc_data
*data
)
444 gfc_data_value
*new_val
, *tail
;
452 m
= match_data_constant (&expr
);
455 if (m
== MATCH_ERROR
)
458 new_val
= gfc_get_data_value ();
459 mpz_init (new_val
->repeat
);
462 data
->value
= new_val
;
464 tail
->next
= new_val
;
468 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
471 mpz_set_ui (tail
->repeat
, 1);
475 mpz_set (tail
->repeat
, expr
->value
.integer
);
476 gfc_free_expr (expr
);
478 m
= match_data_constant (&tail
->expr
);
481 if (m
== MATCH_ERROR
)
485 if (gfc_match_char ('/') == MATCH_YES
)
487 if (gfc_match_char (',') == MATCH_NO
)
494 gfc_syntax_error (ST_DATA
);
495 gfc_free_data_all (gfc_current_ns
);
500 /* Matches an old style initialization. */
503 match_old_style_init (const char *name
)
510 /* Set up data structure to hold initializers. */
511 gfc_find_sym_tree (name
, NULL
, 0, &st
);
514 newdata
= gfc_get_data ();
515 newdata
->var
= gfc_get_data_variable ();
516 newdata
->var
->expr
= gfc_get_variable_expr (st
);
517 newdata
->where
= gfc_current_locus
;
519 /* Match initial value list. This also eats the terminal '/'. */
520 m
= top_val_list (newdata
);
529 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
533 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
535 /* Mark the variable as having appeared in a data statement. */
536 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
542 /* Chain in namespace list of DATA initializers. */
543 newdata
->next
= gfc_current_ns
->data
;
544 gfc_current_ns
->data
= newdata
;
550 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
551 we are matching a DATA statement and are therefore issuing an error
552 if we encounter something unexpected, if not, we're trying to match
553 an old-style initialization expression of the form INTEGER I /2/. */
556 gfc_match_data (void)
561 set_in_match_data (true);
565 new_data
= gfc_get_data ();
566 new_data
->where
= gfc_current_locus
;
568 m
= top_var_list (new_data
);
572 m
= top_val_list (new_data
);
576 new_data
->next
= gfc_current_ns
->data
;
577 gfc_current_ns
->data
= new_data
;
579 if (gfc_match_eos () == MATCH_YES
)
582 gfc_match_char (','); /* Optional comma */
585 set_in_match_data (false);
589 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
592 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
597 set_in_match_data (false);
598 gfc_free_data (new_data
);
603 /************************ Declaration statements *********************/
606 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
609 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
613 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
614 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
616 gfc_error ("The assumed-rank array at %C shall not have a codimension");
620 if (to
->rank
== 0 && from
->rank
> 0)
622 to
->rank
= from
->rank
;
623 to
->type
= from
->type
;
624 to
->cray_pointee
= from
->cray_pointee
;
625 to
->cp_was_assumed
= from
->cp_was_assumed
;
627 for (i
= 0; i
< to
->corank
; i
++)
629 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
630 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
632 for (i
= 0; i
< from
->rank
; i
++)
636 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
637 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
641 to
->lower
[i
] = from
->lower
[i
];
642 to
->upper
[i
] = from
->upper
[i
];
646 else if (to
->corank
== 0 && from
->corank
> 0)
648 to
->corank
= from
->corank
;
649 to
->cotype
= from
->cotype
;
651 for (i
= 0; i
< from
->corank
; i
++)
655 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
656 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
660 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
661 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
670 /* Match an intent specification. Since this can only happen after an
671 INTENT word, a legal intent-spec must follow. */
674 match_intent_spec (void)
677 if (gfc_match (" ( in out )") == MATCH_YES
)
679 if (gfc_match (" ( in )") == MATCH_YES
)
681 if (gfc_match (" ( out )") == MATCH_YES
)
684 gfc_error ("Bad INTENT specification at %C");
685 return INTENT_UNKNOWN
;
689 /* Matches a character length specification, which is either a
690 specification expression, '*', or ':'. */
693 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
700 if (gfc_match_char ('*') == MATCH_YES
)
703 if (gfc_match_char (':') == MATCH_YES
)
705 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
714 m
= gfc_match_expr (expr
);
717 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
720 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
722 if ((*expr
)->value
.function
.actual
723 && (*expr
)->value
.function
.actual
->expr
->symtree
)
726 e
= (*expr
)->value
.function
.actual
->expr
;
727 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
728 && e
->expr_type
== EXPR_VARIABLE
)
730 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
732 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
733 && e
->symtree
->n
.sym
->ts
.u
.cl
734 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
742 gfc_error ("Conflict in attributes of function argument at %C");
747 /* A character length is a '*' followed by a literal integer or a
748 char_len_param_value in parenthesis. */
751 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
757 m
= gfc_match_char ('*');
761 m
= gfc_match_small_literal_int (&length
, NULL
);
762 if (m
== MATCH_ERROR
)
767 if (obsolescent_check
768 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
770 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
774 if (gfc_match_char ('(') == MATCH_NO
)
777 m
= char_len_param_value (expr
, deferred
);
778 if (m
!= MATCH_YES
&& gfc_matching_function
)
784 if (m
== MATCH_ERROR
)
789 if (gfc_match_char (')') == MATCH_NO
)
791 gfc_free_expr (*expr
);
799 gfc_error ("Syntax error in character length specification at %C");
804 /* Special subroutine for finding a symbol. Check if the name is found
805 in the current name space. If not, and we're compiling a function or
806 subroutine and the parent compilation unit is an interface, then check
807 to see if the name we've been given is the name of the interface
808 (located in another namespace). */
811 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
817 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
820 *result
= st
? st
->n
.sym
: NULL
;
824 if (gfc_current_state () != COMP_SUBROUTINE
825 && gfc_current_state () != COMP_FUNCTION
)
828 s
= gfc_state_stack
->previous
;
832 if (s
->state
!= COMP_INTERFACE
)
835 goto end
; /* Nameless interface. */
837 if (strcmp (name
, s
->sym
->name
) == 0)
848 /* Special subroutine for getting a symbol node associated with a
849 procedure name, used in SUBROUTINE and FUNCTION statements. The
850 symbol is created in the parent using with symtree node in the
851 child unit pointing to the symbol. If the current namespace has no
852 parent, then the symbol is just created in the current unit. */
855 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
861 /* Module functions have to be left in their own namespace because
862 they have potentially (almost certainly!) already been referenced.
863 In this sense, they are rather like external functions. This is
864 fixed up in resolve.c(resolve_entries), where the symbol name-
865 space is set to point to the master function, so that the fake
866 result mechanism can work. */
867 if (module_fcn_entry
)
869 /* Present if entry is declared to be a module procedure. */
870 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
873 rc
= gfc_get_symbol (name
, NULL
, result
);
874 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
875 && (*result
)->ts
.type
== BT_UNKNOWN
876 && sym
->attr
.flavor
== FL_UNKNOWN
)
877 /* Pick up the typespec for the entry, if declared in the function
878 body. Note that this symbol is FL_UNKNOWN because it will
879 only have appeared in a type declaration. The local symtree
880 is set to point to the module symbol and a unique symtree
881 to the local version. This latter ensures a correct clearing
884 /* If the ENTRY proceeds its specification, we need to ensure
885 that this does not raise a "has no IMPLICIT type" error. */
886 if (sym
->ts
.type
== BT_UNKNOWN
)
887 sym
->attr
.untyped
= 1;
889 (*result
)->ts
= sym
->ts
;
891 /* Put the symbol in the procedure namespace so that, should
892 the ENTRY precede its specification, the specification
894 (*result
)->ns
= gfc_current_ns
;
896 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
898 st
= gfc_get_unique_symtree (gfc_current_ns
);
903 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
910 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
912 /* Trap another encompassed procedure with the same name. All
913 these conditions are necessary to avoid picking up an entry
914 whose name clashes with that of the encompassing procedure;
915 this is handled using gsymbols to register unique,globally
917 if (sym
->attr
.flavor
!= 0
918 && sym
->attr
.proc
!= 0
919 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
920 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
921 gfc_error_now ("Procedure %qs at %C is already defined at %L",
922 name
, &sym
->declared_at
);
924 /* Trap a procedure with a name the same as interface in the
925 encompassing scope. */
926 if (sym
->attr
.generic
!= 0
927 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
928 && !sym
->attr
.mod_proc
)
929 gfc_error_now ("Name %qs at %C is already defined"
930 " as a generic interface at %L",
931 name
, &sym
->declared_at
);
933 /* Trap declarations of attributes in encompassing scope. The
934 signature for this is that ts.kind is set. Legitimate
935 references only set ts.type. */
936 if (sym
->ts
.kind
!= 0
937 && !sym
->attr
.implicit_type
938 && sym
->attr
.proc
== 0
939 && gfc_current_ns
->parent
!= NULL
940 && sym
->attr
.access
== 0
941 && !module_fcn_entry
)
942 gfc_error_now ("Procedure %qs at %C has an explicit interface "
943 "and must not have attributes declared at %L",
944 name
, &sym
->declared_at
);
947 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
950 /* Module function entries will already have a symtree in
951 the current namespace but will need one at module level. */
952 if (module_fcn_entry
)
954 /* Present if entry is declared to be a module procedure. */
955 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
957 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
960 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
965 /* See if the procedure should be a module procedure. */
967 if (((sym
->ns
->proc_name
!= NULL
968 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
969 && sym
->attr
.proc
!= PROC_MODULE
)
970 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
971 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
978 /* Verify that the given symbol representing a parameter is C
979 interoperable, by checking to see if it was marked as such after
980 its declaration. If the given symbol is not interoperable, a
981 warning is reported, thus removing the need to return the status to
982 the calling function. The standard does not require the user use
983 one of the iso_c_binding named constants to declare an
984 interoperable parameter, but we can't be sure if the param is C
985 interop or not if the user doesn't. For example, integer(4) may be
986 legal Fortran, but doesn't have meaning in C. It may interop with
987 a number of the C types, which causes a problem because the
988 compiler can't know which one. This code is almost certainly not
989 portable, and the user will get what they deserve if the C type
990 across platforms isn't always interoperable with integer(4). If
991 the user had used something like integer(c_int) or integer(c_long),
992 the compiler could have automatically handled the varying sizes
996 gfc_verify_c_interop_param (gfc_symbol
*sym
)
998 int is_c_interop
= 0;
1001 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1002 Don't repeat the checks here. */
1003 if (sym
->attr
.implicit_type
)
1006 /* For subroutines or functions that are passed to a BIND(C) procedure,
1007 they're interoperable if they're BIND(C) and their params are all
1009 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1011 if (sym
->attr
.is_bind_c
== 0)
1013 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1014 "attribute to be C interoperable", sym
->name
,
1015 &(sym
->declared_at
));
1020 if (sym
->attr
.is_c_interop
== 1)
1021 /* We've already checked this procedure; don't check it again. */
1024 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1029 /* See if we've stored a reference to a procedure that owns sym. */
1030 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1032 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1034 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1036 if (is_c_interop
!= 1)
1038 /* Make personalized messages to give better feedback. */
1039 if (sym
->ts
.type
== BT_DERIVED
)
1040 gfc_error ("Variable %qs at %L is a dummy argument to the "
1041 "BIND(C) procedure %qs but is not C interoperable "
1042 "because derived type %qs is not C interoperable",
1043 sym
->name
, &(sym
->declared_at
),
1044 sym
->ns
->proc_name
->name
,
1045 sym
->ts
.u
.derived
->name
);
1046 else if (sym
->ts
.type
== BT_CLASS
)
1047 gfc_error ("Variable %qs at %L is a dummy argument to the "
1048 "BIND(C) procedure %qs but is not C interoperable "
1049 "because it is polymorphic",
1050 sym
->name
, &(sym
->declared_at
),
1051 sym
->ns
->proc_name
->name
);
1052 else if (warn_c_binding_type
)
1053 gfc_warning (OPT_Wc_binding_type
,
1054 "Variable %qs at %L is a dummy argument of the "
1055 "BIND(C) procedure %qs but may not be C "
1057 sym
->name
, &(sym
->declared_at
),
1058 sym
->ns
->proc_name
->name
);
1061 /* Character strings are only C interoperable if they have a
1063 if (sym
->ts
.type
== BT_CHARACTER
)
1065 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1066 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1067 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1069 gfc_error ("Character argument %qs at %L "
1070 "must be length 1 because "
1071 "procedure %qs is BIND(C)",
1072 sym
->name
, &sym
->declared_at
,
1073 sym
->ns
->proc_name
->name
);
1078 /* We have to make sure that any param to a bind(c) routine does
1079 not have the allocatable, pointer, or optional attributes,
1080 according to J3/04-007, section 5.1. */
1081 if (sym
->attr
.allocatable
== 1
1082 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1083 "ALLOCATABLE attribute in procedure %qs "
1084 "with BIND(C)", sym
->name
,
1085 &(sym
->declared_at
),
1086 sym
->ns
->proc_name
->name
))
1089 if (sym
->attr
.pointer
== 1
1090 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1091 "POINTER attribute in procedure %qs "
1092 "with BIND(C)", sym
->name
,
1093 &(sym
->declared_at
),
1094 sym
->ns
->proc_name
->name
))
1097 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1099 gfc_error ("Scalar variable %qs at %L with POINTER or "
1100 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1101 " supported", sym
->name
, &(sym
->declared_at
),
1102 sym
->ns
->proc_name
->name
);
1106 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1108 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1109 "and the VALUE attribute because procedure %qs "
1110 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1111 sym
->ns
->proc_name
->name
);
1114 else if (sym
->attr
.optional
== 1
1115 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs "
1116 "at %L with OPTIONAL attribute in "
1117 "procedure %qs which is BIND(C)",
1118 sym
->name
, &(sym
->declared_at
),
1119 sym
->ns
->proc_name
->name
))
1122 /* Make sure that if it has the dimension attribute, that it is
1123 either assumed size or explicit shape. Deferred shape is already
1124 covered by the pointer/allocatable attribute. */
1125 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1126 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array %qs "
1127 "at %L as dummy argument to the BIND(C) "
1128 "procedure '%s' at %L", sym
->name
,
1129 &(sym
->declared_at
),
1130 sym
->ns
->proc_name
->name
,
1131 &(sym
->ns
->proc_name
->declared_at
)))
1141 /* Function called by variable_decl() that adds a name to the symbol table. */
1144 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1145 gfc_array_spec
**as
, locus
*var_locus
)
1147 symbol_attribute attr
;
1150 if (gfc_get_symbol (name
, NULL
, &sym
))
1153 /* Start updating the symbol table. Add basic type attribute if present. */
1154 if (current_ts
.type
!= BT_UNKNOWN
1155 && (sym
->attr
.implicit_type
== 0
1156 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1157 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1160 if (sym
->ts
.type
== BT_CHARACTER
)
1163 sym
->ts
.deferred
= cl_deferred
;
1166 /* Add dimension attribute if present. */
1167 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1171 /* Add attribute to symbol. The copy is so that we can reset the
1172 dimension attribute. */
1173 attr
= current_attr
;
1175 attr
.codimension
= 0;
1177 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1180 /* Finish any work that may need to be done for the binding label,
1181 if it's a bind(c). The bind(c) attr is found before the symbol
1182 is made, and before the symbol name (for data decls), so the
1183 current_ts is holding the binding label, or nothing if the
1184 name= attr wasn't given. Therefore, test here if we're dealing
1185 with a bind(c) and make sure the binding label is set correctly. */
1186 if (sym
->attr
.is_bind_c
== 1)
1188 if (!sym
->binding_label
)
1190 /* Set the binding label and verify that if a NAME= was specified
1191 then only one identifier was in the entity-decl-list. */
1192 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1193 num_idents_on_line
))
1198 /* See if we know we're in a common block, and if it's a bind(c)
1199 common then we need to make sure we're an interoperable type. */
1200 if (sym
->attr
.in_common
== 1)
1202 /* Test the common block object. */
1203 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1204 && sym
->ts
.is_c_interop
!= 1)
1206 gfc_error_now ("Variable %qs in common block %qs at %C "
1207 "must be declared with a C interoperable "
1208 "kind since common block %qs is BIND(C)",
1209 sym
->name
, sym
->common_block
->name
,
1210 sym
->common_block
->name
);
1215 sym
->attr
.implied_index
= 0;
1217 if (sym
->ts
.type
== BT_CLASS
)
1218 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1224 /* Set character constant to the given length. The constant will be padded or
1225 truncated. If we're inside an array constructor without a typespec, we
1226 additionally check that all elements have the same length; check_len -1
1227 means no checking. */
1230 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1235 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1236 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1238 slen
= expr
->value
.character
.length
;
1241 s
= gfc_get_wide_string (len
+ 1);
1242 memcpy (s
, expr
->value
.character
.string
,
1243 MIN (len
, slen
) * sizeof (gfc_char_t
));
1245 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1247 if (warn_character_truncation
&& slen
> len
)
1248 gfc_warning_now (OPT_Wcharacter_truncation
,
1249 "CHARACTER expression at %L is being truncated "
1250 "(%d/%d)", &expr
->where
, slen
, len
);
1252 /* Apply the standard by 'hand' otherwise it gets cleared for
1254 if (check_len
!= -1 && slen
!= check_len
1255 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1256 gfc_error_now ("The CHARACTER elements of the array constructor "
1257 "at %L must have the same length (%d/%d)",
1258 &expr
->where
, slen
, check_len
);
1261 free (expr
->value
.character
.string
);
1262 expr
->value
.character
.string
= s
;
1263 expr
->value
.character
.length
= len
;
1268 /* Function to create and update the enumerator history
1269 using the information passed as arguments.
1270 Pointer "max_enum" is also updated, to point to
1271 enum history node containing largest initializer.
1273 SYM points to the symbol node of enumerator.
1274 INIT points to its enumerator value. */
1277 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1279 enumerator_history
*new_enum_history
;
1280 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1282 new_enum_history
= XCNEW (enumerator_history
);
1284 new_enum_history
->sym
= sym
;
1285 new_enum_history
->initializer
= init
;
1286 new_enum_history
->next
= NULL
;
1288 if (enum_history
== NULL
)
1290 enum_history
= new_enum_history
;
1291 max_enum
= enum_history
;
1295 new_enum_history
->next
= enum_history
;
1296 enum_history
= new_enum_history
;
1298 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1299 new_enum_history
->initializer
->value
.integer
) < 0)
1300 max_enum
= new_enum_history
;
1305 /* Function to free enum kind history. */
1308 gfc_free_enum_history (void)
1310 enumerator_history
*current
= enum_history
;
1311 enumerator_history
*next
;
1313 while (current
!= NULL
)
1315 next
= current
->next
;
1320 enum_history
= NULL
;
1324 /* Function called by variable_decl() that adds an initialization
1325 expression to a symbol. */
1328 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1330 symbol_attribute attr
;
1335 if (find_special (name
, &sym
, false))
1340 /* If this symbol is confirming an implicit parameter type,
1341 then an initialization expression is not allowed. */
1342 if (attr
.flavor
== FL_PARAMETER
1343 && sym
->value
!= NULL
1346 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1353 /* An initializer is required for PARAMETER declarations. */
1354 if (attr
.flavor
== FL_PARAMETER
)
1356 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1362 /* If a variable appears in a DATA block, it cannot have an
1366 gfc_error ("Variable %qs at %C with an initializer already "
1367 "appears in a DATA statement", sym
->name
);
1371 /* Check if the assignment can happen. This has to be put off
1372 until later for derived type variables and procedure pointers. */
1373 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1374 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1375 && !sym
->attr
.proc_pointer
1376 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1379 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1380 && init
->ts
.type
== BT_CHARACTER
)
1382 /* Update symbol character length according initializer. */
1383 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1386 if (sym
->ts
.u
.cl
->length
== NULL
)
1389 /* If there are multiple CHARACTER variables declared on the
1390 same line, we don't want them to share the same length. */
1391 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1393 if (sym
->attr
.flavor
== FL_PARAMETER
)
1395 if (init
->expr_type
== EXPR_CONSTANT
)
1397 clen
= init
->value
.character
.length
;
1398 sym
->ts
.u
.cl
->length
1399 = gfc_get_int_expr (gfc_default_integer_kind
,
1402 else if (init
->expr_type
== EXPR_ARRAY
)
1404 clen
= mpz_get_si (init
->ts
.u
.cl
->length
->value
.integer
);
1405 sym
->ts
.u
.cl
->length
1406 = gfc_get_int_expr (gfc_default_integer_kind
,
1409 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1410 sym
->ts
.u
.cl
->length
=
1411 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1414 /* Update initializer character length according symbol. */
1415 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1417 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1419 if (init
->expr_type
== EXPR_CONSTANT
)
1420 gfc_set_constant_character_len (len
, init
, -1);
1421 else if (init
->expr_type
== EXPR_ARRAY
)
1425 /* Build a new charlen to prevent simplification from
1426 deleting the length before it is resolved. */
1427 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1428 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1430 for (c
= gfc_constructor_first (init
->value
.constructor
);
1431 c
; c
= gfc_constructor_next (c
))
1432 gfc_set_constant_character_len (len
, c
->expr
, -1);
1437 /* If sym is implied-shape, set its upper bounds from init. */
1438 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1439 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1443 if (init
->rank
== 0)
1445 gfc_error ("Can't initialize implied-shape array at %L"
1446 " with scalar", &sym
->declared_at
);
1449 gcc_assert (sym
->as
->rank
== init
->rank
);
1451 /* Shape should be present, we get an initialization expression. */
1452 gcc_assert (init
->shape
);
1454 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1460 lower
= sym
->as
->lower
[dim
];
1461 if (lower
->expr_type
!= EXPR_CONSTANT
)
1463 gfc_error ("Non-constant lower bound in implied-shape"
1464 " declaration at %L", &lower
->where
);
1468 /* All dimensions must be without upper bound. */
1469 gcc_assert (!sym
->as
->upper
[dim
]);
1472 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1473 mpz_add (e
->value
.integer
,
1474 lower
->value
.integer
, init
->shape
[dim
]);
1475 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1476 sym
->as
->upper
[dim
] = e
;
1479 sym
->as
->type
= AS_EXPLICIT
;
1482 /* Need to check if the expression we initialized this
1483 to was one of the iso_c_binding named constants. If so,
1484 and we're a parameter (constant), let it be iso_c.
1486 integer(c_int), parameter :: my_int = c_int
1487 integer(my_int) :: my_int_2
1488 If we mark my_int as iso_c (since we can see it's value
1489 is equal to one of the named constants), then my_int_2
1490 will be considered C interoperable. */
1491 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1493 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1494 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1495 /* attr bits needed for module files. */
1496 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1497 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1498 if (init
->ts
.is_iso_c
)
1499 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1502 /* Add initializer. Make sure we keep the ranks sane. */
1503 if (sym
->attr
.dimension
&& init
->rank
== 0)
1508 if (sym
->attr
.flavor
== FL_PARAMETER
1509 && init
->expr_type
== EXPR_CONSTANT
1510 && spec_size (sym
->as
, &size
)
1511 && mpz_cmp_si (size
, 0) > 0)
1513 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1515 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1516 gfc_constructor_append_expr (&array
->value
.constructor
,
1519 : gfc_copy_expr (init
),
1522 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1523 for (n
= 0; n
< sym
->as
->rank
; n
++)
1524 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1529 init
->rank
= sym
->as
->rank
;
1533 if (sym
->attr
.save
== SAVE_NONE
)
1534 sym
->attr
.save
= SAVE_IMPLICIT
;
1542 /* Function called by variable_decl() that adds a name to a structure
1546 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1547 gfc_array_spec
**as
)
1552 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1553 constructing, it must have the pointer attribute. */
1554 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1555 && current_ts
.u
.derived
== gfc_current_block ()
1556 && current_attr
.pointer
== 0)
1558 gfc_error ("Component at %C must have the POINTER attribute");
1562 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1564 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1566 gfc_error ("Array component of structure at %C must have explicit "
1567 "or deferred shape");
1572 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1576 if (c
->ts
.type
== BT_CHARACTER
)
1578 c
->attr
= current_attr
;
1580 c
->initializer
= *init
;
1587 c
->attr
.codimension
= 1;
1589 c
->attr
.dimension
= 1;
1593 /* Should this ever get more complicated, combine with similar section
1594 in add_init_expr_to_sym into a separate function. */
1595 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1597 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1601 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1602 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1603 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1605 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1607 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1608 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1609 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1610 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1612 gfc_constructor
*ctor
;
1613 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1618 bool has_ts
= (c
->initializer
->ts
.u
.cl
1619 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1621 /* Remember the length of the first element for checking
1622 that all elements *in the constructor* have the same
1623 length. This need not be the length of the LHS! */
1624 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1625 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1626 first_len
= ctor
->expr
->value
.character
.length
;
1628 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1629 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1631 gfc_set_constant_character_len (len
, ctor
->expr
,
1632 has_ts
? -1 : first_len
);
1633 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1639 /* Check array components. */
1640 if (!c
->attr
.dimension
)
1643 if (c
->attr
.pointer
)
1645 if (c
->as
->type
!= AS_DEFERRED
)
1647 gfc_error ("Pointer array component of structure at %C must have a "
1652 else if (c
->attr
.allocatable
)
1654 if (c
->as
->type
!= AS_DEFERRED
)
1656 gfc_error ("Allocatable component of structure at %C must have a "
1663 if (c
->as
->type
!= AS_EXPLICIT
)
1665 gfc_error ("Array component of structure at %C must have an "
1672 if (c
->ts
.type
== BT_CLASS
)
1674 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1684 /* Match a 'NULL()', and possibly take care of some side effects. */
1687 gfc_match_null (gfc_expr
**result
)
1690 match m
, m2
= MATCH_NO
;
1692 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1698 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1700 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1703 old_loc
= gfc_current_locus
;
1704 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1707 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1711 gfc_current_locus
= old_loc
;
1716 /* The NULL symbol now has to be/become an intrinsic function. */
1717 if (gfc_get_symbol ("null", NULL
, &sym
))
1719 gfc_error ("NULL() initialization at %C is ambiguous");
1723 gfc_intrinsic_symbol (sym
);
1725 if (sym
->attr
.proc
!= PROC_INTRINSIC
1726 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1727 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1728 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1731 *result
= gfc_get_null_expr (&gfc_current_locus
);
1733 /* Invalid per F2008, C512. */
1734 if (m2
== MATCH_YES
)
1736 gfc_error ("NULL() initialization at %C may not have MOLD");
1744 /* Match the initialization expr for a data pointer or procedure pointer. */
1747 match_pointer_init (gfc_expr
**init
, int procptr
)
1751 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1753 gfc_error ("Initialization of pointer at %C is not allowed in "
1754 "a PURE procedure");
1757 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
1759 /* Match NULL() initialization. */
1760 m
= gfc_match_null (init
);
1764 /* Match non-NULL initialization. */
1765 gfc_matching_ptr_assignment
= !procptr
;
1766 gfc_matching_procptr_assignment
= procptr
;
1767 m
= gfc_match_rvalue (init
);
1768 gfc_matching_ptr_assignment
= 0;
1769 gfc_matching_procptr_assignment
= 0;
1770 if (m
== MATCH_ERROR
)
1772 else if (m
== MATCH_NO
)
1774 gfc_error ("Error in pointer initialization at %C");
1778 if (!procptr
&& !gfc_resolve_expr (*init
))
1781 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1782 "initialization at %C"))
1790 check_function_name (char *name
)
1792 /* In functions that have a RESULT variable defined, the function name always
1793 refers to function calls. Therefore, the name is not allowed to appear in
1794 specification statements. When checking this, be careful about
1795 'hidden' procedure pointer results ('ppr@'). */
1797 if (gfc_current_state () == COMP_FUNCTION
)
1799 gfc_symbol
*block
= gfc_current_block ();
1800 if (block
&& block
->result
&& block
->result
!= block
1801 && strcmp (block
->result
->name
, "ppr@") != 0
1802 && strcmp (block
->name
, name
) == 0)
1804 gfc_error ("Function name %qs not allowed at %C", name
);
1813 /* Match a variable name with an optional initializer. When this
1814 subroutine is called, a variable is expected to be parsed next.
1815 Depending on what is happening at the moment, updates either the
1816 symbol table or the current interface. */
1819 variable_decl (int elem
)
1821 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1822 gfc_expr
*initializer
, *char_len
;
1824 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1836 /* When we get here, we've just matched a list of attributes and
1837 maybe a type and a double colon. The next thing we expect to see
1838 is the name of the symbol. */
1839 m
= gfc_match_name (name
);
1843 var_locus
= gfc_current_locus
;
1845 /* Now we could see the optional array spec. or character length. */
1846 m
= gfc_match_array_spec (&as
, true, true);
1847 if (m
== MATCH_ERROR
)
1851 as
= gfc_copy_array_spec (current_as
);
1853 && !merge_array_spec (current_as
, as
, true))
1859 if (flag_cray_pointer
)
1860 cp_as
= gfc_copy_array_spec (as
);
1862 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1863 determine (and check) whether it can be implied-shape. If it
1864 was parsed as assumed-size, change it because PARAMETERs can not
1868 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1871 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1876 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1877 && current_attr
.flavor
== FL_PARAMETER
)
1878 as
->type
= AS_IMPLIED_SHAPE
;
1880 if (as
->type
== AS_IMPLIED_SHAPE
1881 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1891 cl_deferred
= false;
1893 if (current_ts
.type
== BT_CHARACTER
)
1895 switch (match_char_length (&char_len
, &cl_deferred
, false))
1898 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1900 cl
->length
= char_len
;
1903 /* Non-constant lengths need to be copied after the first
1904 element. Also copy assumed lengths. */
1907 && (current_ts
.u
.cl
->length
== NULL
1908 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1910 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1911 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1914 cl
= current_ts
.u
.cl
;
1916 cl_deferred
= current_ts
.deferred
;
1925 /* If this symbol has already shown up in a Cray Pointer declaration,
1926 and this is not a component declaration,
1927 then we want to set the type & bail out. */
1928 if (flag_cray_pointer
&& gfc_current_state () != COMP_DERIVED
)
1930 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1931 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1933 sym
->ts
.type
= current_ts
.type
;
1934 sym
->ts
.kind
= current_ts
.kind
;
1936 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1937 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1938 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1941 /* Check to see if we have an array specification. */
1944 if (sym
->as
!= NULL
)
1946 gfc_error ("Duplicate array spec for Cray pointee at %C");
1947 gfc_free_array_spec (cp_as
);
1953 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1954 gfc_internal_error ("Couldn't set pointee array spec.");
1956 /* Fix the array spec. */
1957 m
= gfc_mod_pointee_as (sym
->as
);
1958 if (m
== MATCH_ERROR
)
1966 gfc_free_array_spec (cp_as
);
1970 /* Procedure pointer as function result. */
1971 if (gfc_current_state () == COMP_FUNCTION
1972 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1973 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1974 strcpy (name
, "ppr@");
1976 if (gfc_current_state () == COMP_FUNCTION
1977 && strcmp (name
, gfc_current_block ()->name
) == 0
1978 && gfc_current_block ()->result
1979 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1980 strcpy (name
, "ppr@");
1982 /* OK, we've successfully matched the declaration. Now put the
1983 symbol in the current namespace, because it might be used in the
1984 optional initialization expression for this symbol, e.g. this is
1987 integer, parameter :: i = huge(i)
1989 This is only true for parameters or variables of a basic type.
1990 For components of derived types, it is not true, so we don't
1991 create a symbol for those yet. If we fail to create the symbol,
1993 if (gfc_current_state () != COMP_DERIVED
1994 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
2000 if (!check_function_name (name
))
2006 /* We allow old-style initializations of the form
2007 integer i /2/, j(4) /3*3, 1/
2008 (if no colon has been seen). These are different from data
2009 statements in that initializers are only allowed to apply to the
2010 variable immediately preceding, i.e.
2012 is not allowed. Therefore we have to do some work manually, that
2013 could otherwise be left to the matchers for DATA statements. */
2015 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2017 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2018 "initialization at %C"))
2020 else if (gfc_current_state () == COMP_DERIVED
)
2022 gfc_error ("Invalid old style initialization for derived type "
2028 return match_old_style_init (name
);
2031 /* The double colon must be present in order to have initializers.
2032 Otherwise the statement is ambiguous with an assignment statement. */
2035 if (gfc_match (" =>") == MATCH_YES
)
2037 if (!current_attr
.pointer
)
2039 gfc_error ("Initialization at %C isn't for a pointer variable");
2044 m
= match_pointer_init (&initializer
, 0);
2048 else if (gfc_match_char ('=') == MATCH_YES
)
2050 if (current_attr
.pointer
)
2052 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2058 m
= gfc_match_init_expr (&initializer
);
2061 gfc_error ("Expected an initialization expression at %C");
2065 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2066 && gfc_state_stack
->state
!= COMP_DERIVED
)
2068 gfc_error ("Initialization of variable at %C is not allowed in "
2069 "a PURE procedure");
2073 if (current_attr
.flavor
!= FL_PARAMETER
2074 && gfc_state_stack
->state
!= COMP_DERIVED
)
2075 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2082 if (initializer
!= NULL
&& current_attr
.allocatable
2083 && gfc_current_state () == COMP_DERIVED
)
2085 gfc_error ("Initialization of allocatable component at %C is not "
2091 /* Add the initializer. Note that it is fine if initializer is
2092 NULL here, because we sometimes also need to check if a
2093 declaration *must* have an initialization expression. */
2094 if (gfc_current_state () != COMP_DERIVED
)
2095 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2098 if (current_ts
.type
== BT_DERIVED
2099 && !current_attr
.pointer
&& !initializer
)
2100 initializer
= gfc_default_initializer (¤t_ts
);
2101 t
= build_struct (name
, cl
, &initializer
, &as
);
2104 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2107 /* Free stuff up and return. */
2108 gfc_free_expr (initializer
);
2109 gfc_free_array_spec (as
);
2115 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2116 This assumes that the byte size is equal to the kind number for
2117 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2120 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2125 if (gfc_match_char ('*') != MATCH_YES
)
2128 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2132 original_kind
= ts
->kind
;
2134 /* Massage the kind numbers for complex types. */
2135 if (ts
->type
== BT_COMPLEX
)
2139 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2140 gfc_basic_typename (ts
->type
), original_kind
);
2147 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2150 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2154 if (flag_real4_kind
== 8)
2156 if (flag_real4_kind
== 10)
2158 if (flag_real4_kind
== 16)
2164 if (flag_real8_kind
== 4)
2166 if (flag_real8_kind
== 10)
2168 if (flag_real8_kind
== 16)
2173 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2175 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2176 gfc_basic_typename (ts
->type
), original_kind
);
2180 if (!gfc_notify_std (GFC_STD_GNU
,
2181 "Nonstandard type declaration %s*%d at %C",
2182 gfc_basic_typename(ts
->type
), original_kind
))
2189 /* Match a kind specification. Since kinds are generally optional, we
2190 usually return MATCH_NO if something goes wrong. If a "kind="
2191 string is found, then we know we have an error. */
2194 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2206 where
= loc
= gfc_current_locus
;
2211 if (gfc_match_char ('(') == MATCH_NO
)
2214 /* Also gobbles optional text. */
2215 if (gfc_match (" kind = ") == MATCH_YES
)
2218 loc
= gfc_current_locus
;
2221 n
= gfc_match_init_expr (&e
);
2225 if (gfc_matching_function
)
2227 /* The function kind expression might include use associated or
2228 imported parameters and try again after the specification
2230 if (gfc_match_char (')') != MATCH_YES
)
2232 gfc_error ("Missing right parenthesis at %C");
2238 gfc_undo_symbols ();
2243 /* ....or else, the match is real. */
2245 gfc_error ("Expected initialization expression at %C");
2253 gfc_error ("Expected scalar initialization expression at %C");
2258 msg
= gfc_extract_int (e
, &ts
->kind
);
2267 /* Before throwing away the expression, let's see if we had a
2268 C interoperable kind (and store the fact). */
2269 if (e
->ts
.is_c_interop
== 1)
2271 /* Mark this as C interoperable if being declared with one
2272 of the named constants from iso_c_binding. */
2273 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2274 ts
->f90_type
= e
->ts
.f90_type
;
2280 /* Ignore errors to this point, if we've gotten here. This means
2281 we ignore the m=MATCH_ERROR from above. */
2282 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2284 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2285 gfc_basic_typename (ts
->type
));
2286 gfc_current_locus
= where
;
2290 /* Warn if, e.g., c_int is used for a REAL variable, but not
2291 if, e.g., c_double is used for COMPLEX as the standard
2292 explicitly says that the kind type parameter for complex and real
2293 variable is the same, i.e. c_float == c_float_complex. */
2294 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2295 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2296 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2297 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2298 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2299 gfc_basic_typename (ts
->type
));
2301 gfc_gobble_whitespace ();
2302 if ((c
= gfc_next_ascii_char ()) != ')'
2303 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2305 if (ts
->type
== BT_CHARACTER
)
2306 gfc_error ("Missing right parenthesis or comma at %C");
2308 gfc_error ("Missing right parenthesis at %C");
2312 /* All tests passed. */
2315 if(m
== MATCH_ERROR
)
2316 gfc_current_locus
= where
;
2318 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2321 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2325 if (flag_real4_kind
== 8)
2327 if (flag_real4_kind
== 10)
2329 if (flag_real4_kind
== 16)
2335 if (flag_real8_kind
== 4)
2337 if (flag_real8_kind
== 10)
2339 if (flag_real8_kind
== 16)
2344 /* Return what we know from the test(s). */
2349 gfc_current_locus
= where
;
2355 match_char_kind (int * kind
, int * is_iso_c
)
2364 where
= gfc_current_locus
;
2366 n
= gfc_match_init_expr (&e
);
2368 if (n
!= MATCH_YES
&& gfc_matching_function
)
2370 /* The expression might include use-associated or imported
2371 parameters and try again after the specification
2374 gfc_undo_symbols ();
2379 gfc_error ("Expected initialization expression at %C");
2385 gfc_error ("Expected scalar initialization expression at %C");
2390 msg
= gfc_extract_int (e
, kind
);
2391 *is_iso_c
= e
->ts
.is_iso_c
;
2401 /* Ignore errors to this point, if we've gotten here. This means
2402 we ignore the m=MATCH_ERROR from above. */
2403 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2405 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2409 /* All tests passed. */
2412 if (m
== MATCH_ERROR
)
2413 gfc_current_locus
= where
;
2415 /* Return what we know from the test(s). */
2420 gfc_current_locus
= where
;
2425 /* Match the various kind/length specifications in a CHARACTER
2426 declaration. We don't return MATCH_NO. */
2429 gfc_match_char_spec (gfc_typespec
*ts
)
2431 int kind
, seen_length
, is_iso_c
;
2443 /* Try the old-style specification first. */
2444 old_char_selector
= 0;
2446 m
= match_char_length (&len
, &deferred
, true);
2450 old_char_selector
= 1;
2455 m
= gfc_match_char ('(');
2458 m
= MATCH_YES
; /* Character without length is a single char. */
2462 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2463 if (gfc_match (" kind =") == MATCH_YES
)
2465 m
= match_char_kind (&kind
, &is_iso_c
);
2467 if (m
== MATCH_ERROR
)
2472 if (gfc_match (" , len =") == MATCH_NO
)
2475 m
= char_len_param_value (&len
, &deferred
);
2478 if (m
== MATCH_ERROR
)
2485 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2486 if (gfc_match (" len =") == MATCH_YES
)
2488 m
= char_len_param_value (&len
, &deferred
);
2491 if (m
== MATCH_ERROR
)
2495 if (gfc_match_char (')') == MATCH_YES
)
2498 if (gfc_match (" , kind =") != MATCH_YES
)
2501 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2507 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2508 m
= char_len_param_value (&len
, &deferred
);
2511 if (m
== MATCH_ERROR
)
2515 m
= gfc_match_char (')');
2519 if (gfc_match_char (',') != MATCH_YES
)
2522 gfc_match (" kind ="); /* Gobble optional text. */
2524 m
= match_char_kind (&kind
, &is_iso_c
);
2525 if (m
== MATCH_ERROR
)
2531 /* Require a right-paren at this point. */
2532 m
= gfc_match_char (')');
2537 gfc_error ("Syntax error in CHARACTER declaration at %C");
2539 gfc_free_expr (len
);
2543 /* Deal with character functions after USE and IMPORT statements. */
2544 if (gfc_matching_function
)
2546 gfc_free_expr (len
);
2547 gfc_undo_symbols ();
2553 gfc_free_expr (len
);
2557 /* Do some final massaging of the length values. */
2558 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2560 if (seen_length
== 0)
2561 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2566 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2567 ts
->deferred
= deferred
;
2569 /* We have to know if it was a C interoperable kind so we can
2570 do accurate type checking of bind(c) procs, etc. */
2572 /* Mark this as C interoperable if being declared with one
2573 of the named constants from iso_c_binding. */
2574 ts
->is_c_interop
= is_iso_c
;
2575 else if (len
!= NULL
)
2576 /* Here, we might have parsed something such as: character(c_char)
2577 In this case, the parsing code above grabs the c_char when
2578 looking for the length (line 1690, roughly). it's the last
2579 testcase for parsing the kind params of a character variable.
2580 However, it's not actually the length. this seems like it
2582 To see if the user used a C interop kind, test the expr
2583 of the so called length, and see if it's C interoperable. */
2584 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2590 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2591 structure to the matched specification. This is necessary for FUNCTION and
2592 IMPLICIT statements.
2594 If implicit_flag is nonzero, then we don't check for the optional
2595 kind specification. Not doing so is needed for matching an IMPLICIT
2596 statement correctly. */
2599 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2601 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2602 gfc_symbol
*sym
, *dt_sym
;
2605 bool seen_deferred_kind
, matched_type
;
2606 const char *dt_name
;
2608 /* A belt and braces check that the typespec is correctly being treated
2609 as a deferred characteristic association. */
2610 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2611 && (gfc_current_block ()->result
->ts
.kind
== -1)
2612 && (ts
->kind
== -1);
2614 if (seen_deferred_kind
)
2617 /* Clear the current binding label, in case one is given. */
2618 curr_binding_label
= NULL
;
2620 if (gfc_match (" byte") == MATCH_YES
)
2622 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2625 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2627 gfc_error ("BYTE type used at %C "
2628 "is not available on the target machine");
2632 ts
->type
= BT_INTEGER
;
2638 m
= gfc_match (" type (");
2639 matched_type
= (m
== MATCH_YES
);
2642 gfc_gobble_whitespace ();
2643 if (gfc_peek_ascii_char () == '*')
2645 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2647 if (gfc_current_state () == COMP_DERIVED
)
2649 gfc_error ("Assumed type at %C is not allowed for components");
2652 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2655 ts
->type
= BT_ASSUMED
;
2659 m
= gfc_match ("%n", name
);
2660 matched_type
= (m
== MATCH_YES
);
2663 if ((matched_type
&& strcmp ("integer", name
) == 0)
2664 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2666 ts
->type
= BT_INTEGER
;
2667 ts
->kind
= gfc_default_integer_kind
;
2671 if ((matched_type
&& strcmp ("character", name
) == 0)
2672 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2675 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2676 "intrinsic-type-spec at %C"))
2679 ts
->type
= BT_CHARACTER
;
2680 if (implicit_flag
== 0)
2681 m
= gfc_match_char_spec (ts
);
2685 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2691 if ((matched_type
&& strcmp ("real", name
) == 0)
2692 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2695 ts
->kind
= gfc_default_real_kind
;
2700 && (strcmp ("doubleprecision", name
) == 0
2701 || (strcmp ("double", name
) == 0
2702 && gfc_match (" precision") == MATCH_YES
)))
2703 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2706 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2707 "intrinsic-type-spec at %C"))
2709 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2713 ts
->kind
= gfc_default_double_kind
;
2717 if ((matched_type
&& strcmp ("complex", name
) == 0)
2718 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2720 ts
->type
= BT_COMPLEX
;
2721 ts
->kind
= gfc_default_complex_kind
;
2726 && (strcmp ("doublecomplex", name
) == 0
2727 || (strcmp ("double", name
) == 0
2728 && gfc_match (" complex") == MATCH_YES
)))
2729 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2731 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2735 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2736 "intrinsic-type-spec at %C"))
2739 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2742 ts
->type
= BT_COMPLEX
;
2743 ts
->kind
= gfc_default_double_kind
;
2747 if ((matched_type
&& strcmp ("logical", name
) == 0)
2748 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2750 ts
->type
= BT_LOGICAL
;
2751 ts
->kind
= gfc_default_logical_kind
;
2756 m
= gfc_match_char (')');
2759 ts
->type
= BT_DERIVED
;
2762 /* Match CLASS declarations. */
2763 m
= gfc_match (" class ( * )");
2764 if (m
== MATCH_ERROR
)
2766 else if (m
== MATCH_YES
)
2770 ts
->type
= BT_CLASS
;
2771 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2774 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2775 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2777 gfc_set_sym_referenced (upe
);
2779 upe
->ts
.type
= BT_VOID
;
2780 upe
->attr
.unlimited_polymorphic
= 1;
2781 /* This is essential to force the construction of
2782 unlimited polymorphic component class containers. */
2783 upe
->attr
.zero_comp
= 1;
2784 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2785 &gfc_current_locus
))
2790 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2792 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2796 ts
->u
.derived
= upe
;
2800 m
= gfc_match (" class ( %n )", name
);
2803 ts
->type
= BT_CLASS
;
2805 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2809 /* Defer association of the derived type until the end of the
2810 specification block. However, if the derived type can be
2811 found, add it to the typespec. */
2812 if (gfc_matching_function
)
2814 ts
->u
.derived
= NULL
;
2815 if (gfc_current_state () != COMP_INTERFACE
2816 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2818 sym
= gfc_find_dt_in_generic (sym
);
2819 ts
->u
.derived
= sym
;
2824 /* Search for the name but allow the components to be defined later. If
2825 type = -1, this typespec has been seen in a function declaration but
2826 the type could not be accessed at that point. The actual derived type is
2827 stored in a symtree with the first letter of the name capitalized; the
2828 symtree with the all lower-case name contains the associated
2829 generic function. */
2830 dt_name
= gfc_get_string ("%c%s",
2831 (char) TOUPPER ((unsigned char) name
[0]),
2832 (const char*)&name
[1]);
2837 gfc_get_ha_symbol (name
, &sym
);
2838 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2840 gfc_error ("Type name %qs at %C is ambiguous", name
);
2843 if (sym
->generic
&& !dt_sym
)
2844 dt_sym
= gfc_find_dt_in_generic (sym
);
2846 else if (ts
->kind
== -1)
2848 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2849 || gfc_current_ns
->has_import_set
;
2850 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2851 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2853 gfc_error ("Type name %qs at %C is ambiguous", name
);
2856 if (sym
&& sym
->generic
&& !dt_sym
)
2857 dt_sym
= gfc_find_dt_in_generic (sym
);
2864 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2865 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2866 || sym
->attr
.subroutine
)
2868 gfc_error ("Type name %qs at %C conflicts with previously declared "
2869 "entity at %L, which has the same name", name
,
2874 gfc_save_symbol_data (sym
);
2875 gfc_set_sym_referenced (sym
);
2876 if (!sym
->attr
.generic
2877 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2880 if (!sym
->attr
.function
2881 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2886 gfc_interface
*intr
, *head
;
2888 /* Use upper case to save the actual derived-type symbol. */
2889 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2890 dt_sym
->name
= gfc_get_string (sym
->name
);
2891 head
= sym
->generic
;
2892 intr
= gfc_get_interface ();
2894 intr
->where
= gfc_current_locus
;
2896 sym
->generic
= intr
;
2897 sym
->attr
.if_source
= IFSRC_DECL
;
2900 gfc_save_symbol_data (dt_sym
);
2902 gfc_set_sym_referenced (dt_sym
);
2904 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2905 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2908 ts
->u
.derived
= dt_sym
;
2914 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2915 "intrinsic-type-spec at %C"))
2918 /* For all types except double, derived and character, look for an
2919 optional kind specifier. MATCH_NO is actually OK at this point. */
2920 if (implicit_flag
== 1)
2922 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2928 if (gfc_current_form
== FORM_FREE
)
2930 c
= gfc_peek_ascii_char ();
2931 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2932 && c
!= ':' && c
!= ',')
2934 if (matched_type
&& c
== ')')
2936 gfc_next_ascii_char ();
2943 m
= gfc_match_kind_spec (ts
, false);
2944 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2945 m
= gfc_match_old_kind_spec (ts
);
2947 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2950 /* Defer association of the KIND expression of function results
2951 until after USE and IMPORT statements. */
2952 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2953 || gfc_matching_function
)
2957 m
= MATCH_YES
; /* No kind specifier found. */
2963 /* Match an IMPLICIT NONE statement. Actually, this statement is
2964 already matched in parse.c, or we would not end up here in the
2965 first place. So the only thing we need to check, is if there is
2966 trailing garbage. If not, the match is successful. */
2969 gfc_match_implicit_none (void)
2973 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2975 bool external
= false;
2976 locus cur_loc
= gfc_current_locus
;
2978 if (gfc_current_ns
->seen_implicit_none
2979 || gfc_current_ns
->has_implicit_none_export
)
2981 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
2985 gfc_gobble_whitespace ();
2986 c
= gfc_peek_ascii_char ();
2989 (void) gfc_next_ascii_char ();
2990 if (!gfc_notify_std (GFC_STD_F2015
, "IMPORT NONE with spec list at %C"))
2993 gfc_gobble_whitespace ();
2994 if (gfc_peek_ascii_char () == ')')
2996 (void) gfc_next_ascii_char ();
3002 m
= gfc_match (" %n", name
);
3006 if (strcmp (name
, "type") == 0)
3008 else if (strcmp (name
, "external") == 0)
3013 gfc_gobble_whitespace ();
3014 c
= gfc_next_ascii_char ();
3025 if (gfc_match_eos () != MATCH_YES
)
3028 gfc_set_implicit_none (type
, external
, &cur_loc
);
3034 /* Match the letter range(s) of an IMPLICIT statement. */
3037 match_implicit_range (void)
3043 cur_loc
= gfc_current_locus
;
3045 gfc_gobble_whitespace ();
3046 c
= gfc_next_ascii_char ();
3049 gfc_error ("Missing character range in IMPLICIT at %C");
3056 gfc_gobble_whitespace ();
3057 c1
= gfc_next_ascii_char ();
3061 gfc_gobble_whitespace ();
3062 c
= gfc_next_ascii_char ();
3067 inner
= 0; /* Fall through. */
3074 gfc_gobble_whitespace ();
3075 c2
= gfc_next_ascii_char ();
3079 gfc_gobble_whitespace ();
3080 c
= gfc_next_ascii_char ();
3082 if ((c
!= ',') && (c
!= ')'))
3095 gfc_error ("Letters must be in alphabetic order in "
3096 "IMPLICIT statement at %C");
3100 /* See if we can add the newly matched range to the pending
3101 implicits from this IMPLICIT statement. We do not check for
3102 conflicts with whatever earlier IMPLICIT statements may have
3103 set. This is done when we've successfully finished matching
3105 if (!gfc_add_new_implicit_range (c1
, c2
))
3112 gfc_syntax_error (ST_IMPLICIT
);
3114 gfc_current_locus
= cur_loc
;
3119 /* Match an IMPLICIT statement, storing the types for
3120 gfc_set_implicit() if the statement is accepted by the parser.
3121 There is a strange looking, but legal syntactic construction
3122 possible. It looks like:
3124 IMPLICIT INTEGER (a-b) (c-d)
3126 This is legal if "a-b" is a constant expression that happens to
3127 equal one of the legal kinds for integers. The real problem
3128 happens with an implicit specification that looks like:
3130 IMPLICIT INTEGER (a-b)
3132 In this case, a typespec matcher that is "greedy" (as most of the
3133 matchers are) gobbles the character range as a kindspec, leaving
3134 nothing left. We therefore have to go a bit more slowly in the
3135 matching process by inhibiting the kindspec checking during
3136 typespec matching and checking for a kind later. */
3139 gfc_match_implicit (void)
3146 if (gfc_current_ns
->seen_implicit_none
)
3148 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3155 /* We don't allow empty implicit statements. */
3156 if (gfc_match_eos () == MATCH_YES
)
3158 gfc_error ("Empty IMPLICIT statement at %C");
3164 /* First cleanup. */
3165 gfc_clear_new_implicit ();
3167 /* A basic type is mandatory here. */
3168 m
= gfc_match_decl_type_spec (&ts
, 1);
3169 if (m
== MATCH_ERROR
)
3174 cur_loc
= gfc_current_locus
;
3175 m
= match_implicit_range ();
3179 /* We may have <TYPE> (<RANGE>). */
3180 gfc_gobble_whitespace ();
3181 c
= gfc_peek_ascii_char ();
3182 if (c
== ',' || c
== '\n' || c
== ';' || c
== '!')
3184 /* Check for CHARACTER with no length parameter. */
3185 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3187 ts
.kind
= gfc_default_character_kind
;
3188 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3189 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3193 /* Record the Successful match. */
3194 if (!gfc_merge_new_implicit (&ts
))
3197 c
= gfc_next_ascii_char ();
3198 else if (gfc_match_eos () == MATCH_ERROR
)
3203 gfc_current_locus
= cur_loc
;
3206 /* Discard the (incorrectly) matched range. */
3207 gfc_clear_new_implicit ();
3209 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3210 if (ts
.type
== BT_CHARACTER
)
3211 m
= gfc_match_char_spec (&ts
);
3214 m
= gfc_match_kind_spec (&ts
, false);
3217 m
= gfc_match_old_kind_spec (&ts
);
3218 if (m
== MATCH_ERROR
)
3224 if (m
== MATCH_ERROR
)
3227 m
= match_implicit_range ();
3228 if (m
== MATCH_ERROR
)
3233 gfc_gobble_whitespace ();
3234 c
= gfc_next_ascii_char ();
3235 if (c
!= ',' && gfc_match_eos () != MATCH_YES
)
3238 if (!gfc_merge_new_implicit (&ts
))
3246 gfc_syntax_error (ST_IMPLICIT
);
3254 gfc_match_import (void)
3256 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3261 if (gfc_current_ns
->proc_name
== NULL
3262 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3264 gfc_error ("IMPORT statement at %C only permitted in "
3265 "an INTERFACE body");
3269 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3272 if (gfc_match_eos () == MATCH_YES
)
3274 /* All host variables should be imported. */
3275 gfc_current_ns
->has_import_set
= 1;
3279 if (gfc_match (" ::") == MATCH_YES
)
3281 if (gfc_match_eos () == MATCH_YES
)
3283 gfc_error ("Expecting list of named entities at %C");
3291 m
= gfc_match (" %n", name
);
3295 if (gfc_current_ns
->parent
!= NULL
3296 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3298 gfc_error ("Type name %qs at %C is ambiguous", name
);
3301 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3302 && gfc_find_symbol (name
,
3303 gfc_current_ns
->proc_name
->ns
->parent
,
3306 gfc_error ("Type name %qs at %C is ambiguous", name
);
3312 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3313 "at %C - does not exist.", name
);
3317 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3319 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3324 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3327 sym
->attr
.imported
= 1;
3329 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3331 /* The actual derived type is stored in a symtree with the first
3332 letter of the name capitalized; the symtree with the all
3333 lower-case name contains the associated generic function. */
3334 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3335 gfc_get_string ("%c%s",
3336 (char) TOUPPER ((unsigned char) name
[0]),
3340 sym
->attr
.imported
= 1;
3353 if (gfc_match_eos () == MATCH_YES
)
3355 if (gfc_match_char (',') != MATCH_YES
)
3362 gfc_error ("Syntax error in IMPORT statement at %C");
3367 /* A minimal implementation of gfc_match without whitespace, escape
3368 characters or variable arguments. Returns true if the next
3369 characters match the TARGET template exactly. */
3372 match_string_p (const char *target
)
3376 for (p
= target
; *p
; p
++)
3377 if ((char) gfc_next_ascii_char () != *p
)
3382 /* Matches an attribute specification including array specs. If
3383 successful, leaves the variables current_attr and current_as
3384 holding the specification. Also sets the colon_seen variable for
3385 later use by matchers associated with initializations.
3387 This subroutine is a little tricky in the sense that we don't know
3388 if we really have an attr-spec until we hit the double colon.
3389 Until that time, we can only return MATCH_NO. This forces us to
3390 check for duplicate specification at this level. */
3393 match_attr_spec (void)
3395 /* Modifiers that can exist in a type statement. */
3397 { GFC_DECL_BEGIN
= 0,
3398 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3399 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3400 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3401 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3402 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3403 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3406 /* GFC_DECL_END is the sentinel, index starts at 0. */
3407 #define NUM_DECL GFC_DECL_END
3409 locus start
, seen_at
[NUM_DECL
];
3416 gfc_clear_attr (¤t_attr
);
3417 start
= gfc_current_locus
;
3422 /* See if we get all of the keywords up to the final double colon. */
3423 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3431 gfc_gobble_whitespace ();
3433 ch
= gfc_next_ascii_char ();
3436 /* This is the successful exit condition for the loop. */
3437 if (gfc_next_ascii_char () == ':')
3442 gfc_gobble_whitespace ();
3443 switch (gfc_peek_ascii_char ())
3446 gfc_next_ascii_char ();
3447 switch (gfc_next_ascii_char ())
3450 if (match_string_p ("locatable"))
3452 /* Matched "allocatable". */
3453 d
= DECL_ALLOCATABLE
;
3458 if (match_string_p ("ynchronous"))
3460 /* Matched "asynchronous". */
3461 d
= DECL_ASYNCHRONOUS
;
3468 /* Try and match the bind(c). */
3469 m
= gfc_match_bind_c (NULL
, true);
3472 else if (m
== MATCH_ERROR
)
3477 gfc_next_ascii_char ();
3478 if ('o' != gfc_next_ascii_char ())
3480 switch (gfc_next_ascii_char ())
3483 if (match_string_p ("imension"))
3485 d
= DECL_CODIMENSION
;
3489 if (match_string_p ("tiguous"))
3491 d
= DECL_CONTIGUOUS
;
3498 if (match_string_p ("dimension"))
3503 if (match_string_p ("external"))
3508 if (match_string_p ("int"))
3510 ch
= gfc_next_ascii_char ();
3513 if (match_string_p ("nt"))
3515 /* Matched "intent". */
3516 /* TODO: Call match_intent_spec from here. */
3517 if (gfc_match (" ( in out )") == MATCH_YES
)
3519 else if (gfc_match (" ( in )") == MATCH_YES
)
3521 else if (gfc_match (" ( out )") == MATCH_YES
)
3527 if (match_string_p ("insic"))
3529 /* Matched "intrinsic". */
3537 if (match_string_p ("optional"))
3542 gfc_next_ascii_char ();
3543 switch (gfc_next_ascii_char ())
3546 if (match_string_p ("rameter"))
3548 /* Matched "parameter". */
3554 if (match_string_p ("inter"))
3556 /* Matched "pointer". */
3562 ch
= gfc_next_ascii_char ();
3565 if (match_string_p ("vate"))
3567 /* Matched "private". */
3573 if (match_string_p ("tected"))
3575 /* Matched "protected". */
3582 if (match_string_p ("blic"))
3584 /* Matched "public". */
3592 if (match_string_p ("save"))
3597 if (match_string_p ("target"))
3602 gfc_next_ascii_char ();
3603 ch
= gfc_next_ascii_char ();
3606 if (match_string_p ("lue"))
3608 /* Matched "value". */
3614 if (match_string_p ("latile"))
3616 /* Matched "volatile". */
3624 /* No double colon and no recognizable decl_type, so assume that
3625 we've been looking at something else the whole time. */
3632 /* Check to make sure any parens are paired up correctly. */
3633 if (gfc_match_parens () == MATCH_ERROR
)
3640 seen_at
[d
] = gfc_current_locus
;
3642 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3644 gfc_array_spec
*as
= NULL
;
3646 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3647 d
== DECL_CODIMENSION
);
3649 if (current_as
== NULL
)
3651 else if (m
== MATCH_YES
)
3653 if (!merge_array_spec (as
, current_as
, false))
3660 if (d
== DECL_CODIMENSION
)
3661 gfc_error ("Missing codimension specification at %C");
3663 gfc_error ("Missing dimension specification at %C");
3667 if (m
== MATCH_ERROR
)
3672 /* Since we've seen a double colon, we have to be looking at an
3673 attr-spec. This means that we can now issue errors. */
3674 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3679 case DECL_ALLOCATABLE
:
3680 attr
= "ALLOCATABLE";
3682 case DECL_ASYNCHRONOUS
:
3683 attr
= "ASYNCHRONOUS";
3685 case DECL_CODIMENSION
:
3686 attr
= "CODIMENSION";
3688 case DECL_CONTIGUOUS
:
3689 attr
= "CONTIGUOUS";
3691 case DECL_DIMENSION
:
3698 attr
= "INTENT (IN)";
3701 attr
= "INTENT (OUT)";
3704 attr
= "INTENT (IN OUT)";
3706 case DECL_INTRINSIC
:
3712 case DECL_PARAMETER
:
3718 case DECL_PROTECTED
:
3733 case DECL_IS_BIND_C
:
3743 attr
= NULL
; /* This shouldn't happen. */
3746 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3751 /* Now that we've dealt with duplicate attributes, add the attributes
3752 to the current attribute. */
3753 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3758 if (gfc_current_state () == COMP_DERIVED
3759 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3760 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3761 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3763 if (d
== DECL_ALLOCATABLE
)
3765 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3766 "attribute at %C in a TYPE definition"))
3774 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3781 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3782 && gfc_current_state () != COMP_MODULE
)
3784 if (d
== DECL_PRIVATE
)
3788 if (gfc_current_state () == COMP_DERIVED
3789 && gfc_state_stack
->previous
3790 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3792 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3793 "at %L in a TYPE definition", attr
,
3802 gfc_error ("%s attribute at %L is not allowed outside of the "
3803 "specification part of a module", attr
, &seen_at
[d
]);
3811 case DECL_ALLOCATABLE
:
3812 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3815 case DECL_ASYNCHRONOUS
:
3816 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3819 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3822 case DECL_CODIMENSION
:
3823 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3826 case DECL_CONTIGUOUS
:
3827 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3830 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3833 case DECL_DIMENSION
:
3834 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3838 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3842 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3846 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3850 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3853 case DECL_INTRINSIC
:
3854 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3858 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3861 case DECL_PARAMETER
:
3862 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3866 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3869 case DECL_PROTECTED
:
3870 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3872 gfc_error ("PROTECTED at %C only allowed in specification "
3873 "part of a module");
3878 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3881 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3885 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3890 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3895 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3899 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3902 case DECL_IS_BIND_C
:
3903 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3907 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3910 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3914 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3917 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3921 gfc_internal_error ("match_attr_spec(): Bad attribute");
3931 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3932 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3933 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3934 current_attr
.save
= SAVE_IMPLICIT
;
3940 gfc_current_locus
= start
;
3941 gfc_free_array_spec (current_as
);
3947 /* Set the binding label, dest_label, either with the binding label
3948 stored in the given gfc_typespec, ts, or if none was provided, it
3949 will be the symbol name in all lower case, as required by the draft
3950 (J3/04-007, section 15.4.1). If a binding label was given and
3951 there is more than one argument (num_idents), it is an error. */
3954 set_binding_label (const char **dest_label
, const char *sym_name
,
3957 if (num_idents
> 1 && has_name_equals
)
3959 gfc_error ("Multiple identifiers provided with "
3960 "single NAME= specifier at %C");
3964 if (curr_binding_label
)
3965 /* Binding label given; store in temp holder till have sym. */
3966 *dest_label
= curr_binding_label
;
3969 /* No binding label given, and the NAME= specifier did not exist,
3970 which means there was no NAME="". */
3971 if (sym_name
!= NULL
&& has_name_equals
== 0)
3972 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3979 /* Set the status of the given common block as being BIND(C) or not,
3980 depending on the given parameter, is_bind_c. */
3983 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3985 com_block
->is_bind_c
= is_bind_c
;
3990 /* Verify that the given gfc_typespec is for a C interoperable type. */
3993 gfc_verify_c_interop (gfc_typespec
*ts
)
3995 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3996 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3998 else if (ts
->type
== BT_CLASS
)
4000 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
4007 /* Verify that the variables of a given common block, which has been
4008 defined with the attribute specifier bind(c), to be of a C
4009 interoperable type. Errors will be reported here, if
4013 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
4015 gfc_symbol
*curr_sym
= NULL
;
4018 curr_sym
= com_block
->head
;
4020 /* Make sure we have at least one symbol. */
4021 if (curr_sym
== NULL
)
4024 /* Here we know we have a symbol, so we'll execute this loop
4028 /* The second to last param, 1, says this is in a common block. */
4029 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
4030 curr_sym
= curr_sym
->common_next
;
4031 } while (curr_sym
!= NULL
);
4037 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4038 an appropriate error message is reported. */
4041 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
4042 int is_in_common
, gfc_common_head
*com_block
)
4044 bool bind_c_function
= false;
4047 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
4048 bind_c_function
= true;
4050 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
4052 tmp_sym
= tmp_sym
->result
;
4053 /* Make sure it wasn't an implicitly typed result. */
4054 if (tmp_sym
->attr
.implicit_type
&& warn_c_binding_type
)
4056 gfc_warning (OPT_Wc_binding_type
,
4057 "Implicitly declared BIND(C) function %qs at "
4058 "%L may not be C interoperable", tmp_sym
->name
,
4059 &tmp_sym
->declared_at
);
4060 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
4061 /* Mark it as C interoperable to prevent duplicate warnings. */
4062 tmp_sym
->ts
.is_c_interop
= 1;
4063 tmp_sym
->attr
.is_c_interop
= 1;
4067 /* Here, we know we have the bind(c) attribute, so if we have
4068 enough type info, then verify that it's a C interop kind.
4069 The info could be in the symbol already, or possibly still in
4070 the given ts (current_ts), so look in both. */
4071 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
4073 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
4075 /* See if we're dealing with a sym in a common block or not. */
4076 if (is_in_common
== 1 && warn_c_binding_type
)
4078 gfc_warning (OPT_Wc_binding_type
,
4079 "Variable %qs in common block %qs at %L "
4080 "may not be a C interoperable "
4081 "kind though common block %qs is BIND(C)",
4082 tmp_sym
->name
, com_block
->name
,
4083 &(tmp_sym
->declared_at
), com_block
->name
);
4087 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
4088 gfc_error ("Type declaration %qs at %L is not C "
4089 "interoperable but it is BIND(C)",
4090 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4091 else if (warn_c_binding_type
)
4092 gfc_warning (OPT_Wc_binding_type
, "Variable %qs at %L "
4093 "may not be a C interoperable "
4094 "kind but it is BIND(C)",
4095 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4099 /* Variables declared w/in a common block can't be bind(c)
4100 since there's no way for C to see these variables, so there's
4101 semantically no reason for the attribute. */
4102 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4104 gfc_error ("Variable %qs in common block %qs at "
4105 "%L cannot be declared with BIND(C) "
4106 "since it is not a global",
4107 tmp_sym
->name
, com_block
->name
,
4108 &(tmp_sym
->declared_at
));
4112 /* Scalar variables that are bind(c) can not have the pointer
4113 or allocatable attributes. */
4114 if (tmp_sym
->attr
.is_bind_c
== 1)
4116 if (tmp_sym
->attr
.pointer
== 1)
4118 gfc_error ("Variable %qs at %L cannot have both the "
4119 "POINTER and BIND(C) attributes",
4120 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4124 if (tmp_sym
->attr
.allocatable
== 1)
4126 gfc_error ("Variable %qs at %L cannot have both the "
4127 "ALLOCATABLE and BIND(C) attributes",
4128 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4134 /* If it is a BIND(C) function, make sure the return value is a
4135 scalar value. The previous tests in this function made sure
4136 the type is interoperable. */
4137 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4138 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4139 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4141 /* BIND(C) functions can not return a character string. */
4142 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4143 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4144 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4145 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4146 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4147 "be a character string", tmp_sym
->name
,
4148 &(tmp_sym
->declared_at
));
4151 /* See if the symbol has been marked as private. If it has, make sure
4152 there is no binding label and warn the user if there is one. */
4153 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4154 && tmp_sym
->binding_label
)
4155 /* Use gfc_warning_now because we won't say that the symbol fails
4156 just because of this. */
4157 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4158 "given the binding label %qs", tmp_sym
->name
,
4159 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4165 /* Set the appropriate fields for a symbol that's been declared as
4166 BIND(C) (the is_bind_c flag and the binding label), and verify that
4167 the type is C interoperable. Errors are reported by the functions
4168 used to set/test these fields. */
4171 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4175 /* TODO: Do we need to make sure the vars aren't marked private? */
4177 /* Set the is_bind_c bit in symbol_attribute. */
4178 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4180 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4187 /* Set the fields marking the given common block as BIND(C), including
4188 a binding label, and report any errors encountered. */
4191 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4195 /* destLabel, common name, typespec (which may have binding label). */
4196 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4200 /* Set the given common block (com_block) to being bind(c) (1). */
4201 set_com_block_bind_c (com_block
, 1);
4207 /* Retrieve the list of one or more identifiers that the given bind(c)
4208 attribute applies to. */
4211 get_bind_c_idents (void)
4213 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4215 gfc_symbol
*tmp_sym
= NULL
;
4217 gfc_common_head
*com_block
= NULL
;
4219 if (gfc_match_name (name
) == MATCH_YES
)
4221 found_id
= MATCH_YES
;
4222 gfc_get_ha_symbol (name
, &tmp_sym
);
4224 else if (match_common_name (name
) == MATCH_YES
)
4226 found_id
= MATCH_YES
;
4227 com_block
= gfc_get_common (name
, 0);
4231 gfc_error ("Need either entity or common block name for "
4232 "attribute specification statement at %C");
4236 /* Save the current identifier and look for more. */
4239 /* Increment the number of identifiers found for this spec stmt. */
4242 /* Make sure we have a sym or com block, and verify that it can
4243 be bind(c). Set the appropriate field(s) and look for more
4245 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4247 if (tmp_sym
!= NULL
)
4249 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4254 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4258 /* Look to see if we have another identifier. */
4260 if (gfc_match_eos () == MATCH_YES
)
4261 found_id
= MATCH_NO
;
4262 else if (gfc_match_char (',') != MATCH_YES
)
4263 found_id
= MATCH_NO
;
4264 else if (gfc_match_name (name
) == MATCH_YES
)
4266 found_id
= MATCH_YES
;
4267 gfc_get_ha_symbol (name
, &tmp_sym
);
4269 else if (match_common_name (name
) == MATCH_YES
)
4271 found_id
= MATCH_YES
;
4272 com_block
= gfc_get_common (name
, 0);
4276 gfc_error ("Missing entity or common block name for "
4277 "attribute specification statement at %C");
4283 gfc_internal_error ("Missing symbol");
4285 } while (found_id
== MATCH_YES
);
4287 /* if we get here we were successful */
4292 /* Try and match a BIND(C) attribute specification statement. */
4295 gfc_match_bind_c_stmt (void)
4297 match found_match
= MATCH_NO
;
4302 /* This may not be necessary. */
4304 /* Clear the temporary binding label holder. */
4305 curr_binding_label
= NULL
;
4307 /* Look for the bind(c). */
4308 found_match
= gfc_match_bind_c (NULL
, true);
4310 if (found_match
== MATCH_YES
)
4312 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4315 /* Look for the :: now, but it is not required. */
4318 /* Get the identifier(s) that needs to be updated. This may need to
4319 change to hand the flag(s) for the attr specified so all identifiers
4320 found can have all appropriate parts updated (assuming that the same
4321 spec stmt can have multiple attrs, such as both bind(c) and
4323 if (!get_bind_c_idents ())
4324 /* Error message should have printed already. */
4332 /* Match a data declaration statement. */
4335 gfc_match_data_decl (void)
4341 num_idents_on_line
= 0;
4343 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4347 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4348 && gfc_current_state () != COMP_DERIVED
)
4350 sym
= gfc_use_derived (current_ts
.u
.derived
);
4358 current_ts
.u
.derived
= sym
;
4361 m
= match_attr_spec ();
4362 if (m
== MATCH_ERROR
)
4368 if (current_ts
.type
== BT_CLASS
4369 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4372 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4373 && current_ts
.u
.derived
->components
== NULL
4374 && !current_ts
.u
.derived
->attr
.zero_comp
)
4377 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4380 gfc_find_symbol (current_ts
.u
.derived
->name
,
4381 current_ts
.u
.derived
->ns
, 1, &sym
);
4383 /* Any symbol that we find had better be a type definition
4384 which has its components defined. */
4385 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4386 && (current_ts
.u
.derived
->components
!= NULL
4387 || current_ts
.u
.derived
->attr
.zero_comp
))
4390 gfc_error ("Derived type at %C has not been previously defined "
4391 "and so cannot appear in a derived type definition");
4397 /* If we have an old-style character declaration, and no new-style
4398 attribute specifications, then there a comma is optional between
4399 the type specification and the variable list. */
4400 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4401 gfc_match_char (',');
4403 /* Give the types/attributes to symbols that follow. Give the element
4404 a number so that repeat character length expressions can be copied. */
4408 num_idents_on_line
++;
4409 m
= variable_decl (elem
++);
4410 if (m
== MATCH_ERROR
)
4415 if (gfc_match_eos () == MATCH_YES
)
4417 if (gfc_match_char (',') != MATCH_YES
)
4421 if (!gfc_error_flag_test ())
4422 gfc_error ("Syntax error in data declaration at %C");
4425 gfc_free_data_all (gfc_current_ns
);
4428 gfc_free_array_spec (current_as
);
4434 /* Match a prefix associated with a function or subroutine
4435 declaration. If the typespec pointer is nonnull, then a typespec
4436 can be matched. Note that if nothing matches, MATCH_YES is
4437 returned (the null string was matched). */
4440 gfc_match_prefix (gfc_typespec
*ts
)
4446 gfc_clear_attr (¤t_attr
);
4448 seen_impure
= false;
4450 gcc_assert (!gfc_matching_prefix
);
4451 gfc_matching_prefix
= true;
4455 found_prefix
= false;
4457 if (!seen_type
&& ts
!= NULL
4458 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4459 && gfc_match_space () == MATCH_YES
)
4463 found_prefix
= true;
4466 if (gfc_match ("elemental% ") == MATCH_YES
)
4468 if (!gfc_add_elemental (¤t_attr
, NULL
))
4471 found_prefix
= true;
4474 if (gfc_match ("pure% ") == MATCH_YES
)
4476 if (!gfc_add_pure (¤t_attr
, NULL
))
4479 found_prefix
= true;
4482 if (gfc_match ("recursive% ") == MATCH_YES
)
4484 if (!gfc_add_recursive (¤t_attr
, NULL
))
4487 found_prefix
= true;
4490 /* IMPURE is a somewhat special case, as it needs not set an actual
4491 attribute but rather only prevents ELEMENTAL routines from being
4492 automatically PURE. */
4493 if (gfc_match ("impure% ") == MATCH_YES
)
4495 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4499 found_prefix
= true;
4502 while (found_prefix
);
4504 /* IMPURE and PURE must not both appear, of course. */
4505 if (seen_impure
&& current_attr
.pure
)
4507 gfc_error ("PURE and IMPURE must not appear both at %C");
4511 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4512 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4514 if (!gfc_add_pure (¤t_attr
, NULL
))
4518 /* At this point, the next item is not a prefix. */
4519 gcc_assert (gfc_matching_prefix
);
4520 gfc_matching_prefix
= false;
4524 gcc_assert (gfc_matching_prefix
);
4525 gfc_matching_prefix
= false;
4530 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4533 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4535 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4538 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4541 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4548 /* Match a formal argument list. */
4551 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4553 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4554 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4560 if (gfc_match_char ('(') != MATCH_YES
)
4567 if (gfc_match_char (')') == MATCH_YES
)
4572 if (gfc_match_char ('*') == MATCH_YES
)
4575 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4584 m
= gfc_match_name (name
);
4588 if (gfc_get_symbol (name
, NULL
, &sym
))
4592 p
= gfc_get_formal_arglist ();
4604 /* We don't add the VARIABLE flavor because the name could be a
4605 dummy procedure. We don't apply these attributes to formal
4606 arguments of statement functions. */
4607 if (sym
!= NULL
&& !st_flag
4608 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4609 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4615 /* The name of a program unit can be in a different namespace,
4616 so check for it explicitly. After the statement is accepted,
4617 the name is checked for especially in gfc_get_symbol(). */
4618 if (gfc_new_block
!= NULL
&& sym
!= NULL
4619 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4621 gfc_error ("Name %qs at %C is the name of the procedure",
4627 if (gfc_match_char (')') == MATCH_YES
)
4630 m
= gfc_match_char (',');
4633 gfc_error ("Unexpected junk in formal argument list at %C");
4639 /* Check for duplicate symbols in the formal argument list. */
4642 for (p
= head
; p
->next
; p
= p
->next
)
4647 for (q
= p
->next
; q
; q
= q
->next
)
4648 if (p
->sym
== q
->sym
)
4650 gfc_error ("Duplicate symbol %qs in formal argument list "
4651 "at %C", p
->sym
->name
);
4659 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4668 gfc_free_formal_arglist (head
);
4673 /* Match a RESULT specification following a function declaration or
4674 ENTRY statement. Also matches the end-of-statement. */
4677 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4679 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4683 if (gfc_match (" result (") != MATCH_YES
)
4686 m
= gfc_match_name (name
);
4690 /* Get the right paren, and that's it because there could be the
4691 bind(c) attribute after the result clause. */
4692 if (gfc_match_char (')') != MATCH_YES
)
4694 /* TODO: should report the missing right paren here. */
4698 if (strcmp (function
->name
, name
) == 0)
4700 gfc_error ("RESULT variable at %C must be different than function name");
4704 if (gfc_get_symbol (name
, NULL
, &r
))
4707 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4716 /* Match a function suffix, which could be a combination of a result
4717 clause and BIND(C), either one, or neither. The draft does not
4718 require them to come in a specific order. */
4721 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4723 match is_bind_c
; /* Found bind(c). */
4724 match is_result
; /* Found result clause. */
4725 match found_match
; /* Status of whether we've found a good match. */
4726 char peek_char
; /* Character we're going to peek at. */
4727 bool allow_binding_name
;
4729 /* Initialize to having found nothing. */
4730 found_match
= MATCH_NO
;
4731 is_bind_c
= MATCH_NO
;
4732 is_result
= MATCH_NO
;
4734 /* Get the next char to narrow between result and bind(c). */
4735 gfc_gobble_whitespace ();
4736 peek_char
= gfc_peek_ascii_char ();
4738 /* C binding names are not allowed for internal procedures. */
4739 if (gfc_current_state () == COMP_CONTAINS
4740 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4741 allow_binding_name
= false;
4743 allow_binding_name
= true;
4748 /* Look for result clause. */
4749 is_result
= match_result (sym
, result
);
4750 if (is_result
== MATCH_YES
)
4752 /* Now see if there is a bind(c) after it. */
4753 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4754 /* We've found the result clause and possibly bind(c). */
4755 found_match
= MATCH_YES
;
4758 /* This should only be MATCH_ERROR. */
4759 found_match
= is_result
;
4762 /* Look for bind(c) first. */
4763 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4764 if (is_bind_c
== MATCH_YES
)
4766 /* Now see if a result clause followed it. */
4767 is_result
= match_result (sym
, result
);
4768 found_match
= MATCH_YES
;
4772 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4773 found_match
= MATCH_ERROR
;
4777 gfc_error ("Unexpected junk after function declaration at %C");
4778 found_match
= MATCH_ERROR
;
4782 if (is_bind_c
== MATCH_YES
)
4784 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4785 if (gfc_current_state () == COMP_CONTAINS
4786 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4787 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4788 "at %L may not be specified for an internal "
4789 "procedure", &gfc_current_locus
))
4792 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4800 /* Procedure pointer return value without RESULT statement:
4801 Add "hidden" result variable named "ppr@". */
4804 add_hidden_procptr_result (gfc_symbol
*sym
)
4808 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4811 /* First usage case: PROCEDURE and EXTERNAL statements. */
4812 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4813 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4814 && sym
->attr
.external
;
4815 /* Second usage case: INTERFACE statements. */
4816 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4817 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4818 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4824 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4828 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4829 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4830 st2
->n
.sym
= stree
->n
.sym
;
4832 sym
->result
= stree
->n
.sym
;
4834 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4835 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4836 sym
->result
->attr
.external
= sym
->attr
.external
;
4837 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4838 sym
->result
->ts
= sym
->ts
;
4839 sym
->attr
.proc_pointer
= 0;
4840 sym
->attr
.pointer
= 0;
4841 sym
->attr
.external
= 0;
4842 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4844 sym
->result
->attr
.pointer
= 0;
4845 sym
->result
->attr
.proc_pointer
= 1;
4848 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4850 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4851 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4852 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4853 && sym
== gfc_current_ns
->proc_name
4854 && sym
== sym
->result
->ns
->proc_name
4855 && strcmp ("ppr@", sym
->result
->name
) == 0)
4857 sym
->result
->attr
.proc_pointer
= 1;
4858 sym
->attr
.pointer
= 0;
4866 /* Match the interface for a PROCEDURE declaration,
4867 including brackets (R1212). */
4870 match_procedure_interface (gfc_symbol
**proc_if
)
4874 locus old_loc
, entry_loc
;
4875 gfc_namespace
*old_ns
= gfc_current_ns
;
4876 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4878 old_loc
= entry_loc
= gfc_current_locus
;
4879 gfc_clear_ts (¤t_ts
);
4881 if (gfc_match (" (") != MATCH_YES
)
4883 gfc_current_locus
= entry_loc
;
4887 /* Get the type spec. for the procedure interface. */
4888 old_loc
= gfc_current_locus
;
4889 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4890 gfc_gobble_whitespace ();
4891 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4894 if (m
== MATCH_ERROR
)
4897 /* Procedure interface is itself a procedure. */
4898 gfc_current_locus
= old_loc
;
4899 m
= gfc_match_name (name
);
4901 /* First look to see if it is already accessible in the current
4902 namespace because it is use associated or contained. */
4904 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4907 /* If it is still not found, then try the parent namespace, if it
4908 exists and create the symbol there if it is still not found. */
4909 if (gfc_current_ns
->parent
)
4910 gfc_current_ns
= gfc_current_ns
->parent
;
4911 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4914 gfc_current_ns
= old_ns
;
4915 *proc_if
= st
->n
.sym
;
4920 /* Resolve interface if possible. That way, attr.procedure is only set
4921 if it is declared by a later procedure-declaration-stmt, which is
4922 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4923 while ((*proc_if
)->ts
.interface
)
4924 *proc_if
= (*proc_if
)->ts
.interface
;
4926 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4927 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4928 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4929 (*proc_if
)->name
, NULL
))
4934 if (gfc_match (" )") != MATCH_YES
)
4936 gfc_current_locus
= entry_loc
;
4944 /* Match a PROCEDURE declaration (R1211). */
4947 match_procedure_decl (void)
4950 gfc_symbol
*sym
, *proc_if
= NULL
;
4952 gfc_expr
*initializer
= NULL
;
4954 /* Parse interface (with brackets). */
4955 m
= match_procedure_interface (&proc_if
);
4959 /* Parse attributes (with colons). */
4960 m
= match_attr_spec();
4961 if (m
== MATCH_ERROR
)
4964 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4966 current_attr
.is_bind_c
= 1;
4967 has_name_equals
= 0;
4968 curr_binding_label
= NULL
;
4971 /* Get procedure symbols. */
4974 m
= gfc_match_symbol (&sym
, 0);
4977 else if (m
== MATCH_ERROR
)
4980 /* Add current_attr to the symbol attributes. */
4981 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4984 if (sym
->attr
.is_bind_c
)
4986 /* Check for C1218. */
4987 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4989 gfc_error ("BIND(C) attribute at %C requires "
4990 "an interface with BIND(C)");
4993 /* Check for C1217. */
4994 if (has_name_equals
&& sym
->attr
.pointer
)
4996 gfc_error ("BIND(C) procedure with NAME may not have "
4997 "POINTER attribute at %C");
5000 if (has_name_equals
&& sym
->attr
.dummy
)
5002 gfc_error ("Dummy procedure at %C may not have "
5003 "BIND(C) attribute with NAME");
5006 /* Set binding label for BIND(C). */
5007 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
5011 if (!gfc_add_external (&sym
->attr
, NULL
))
5014 if (add_hidden_procptr_result (sym
))
5017 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
5020 /* Set interface. */
5021 if (proc_if
!= NULL
)
5023 if (sym
->ts
.type
!= BT_UNKNOWN
)
5025 gfc_error ("Procedure %qs at %L already has basic type of %s",
5026 sym
->name
, &gfc_current_locus
,
5027 gfc_basic_typename (sym
->ts
.type
));
5030 sym
->ts
.interface
= proc_if
;
5031 sym
->attr
.untyped
= 1;
5032 sym
->attr
.if_source
= IFSRC_IFBODY
;
5034 else if (current_ts
.type
!= BT_UNKNOWN
)
5036 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5038 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5039 sym
->ts
.interface
->ts
= current_ts
;
5040 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5041 sym
->ts
.interface
->attr
.function
= 1;
5042 sym
->attr
.function
= 1;
5043 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
5046 if (gfc_match (" =>") == MATCH_YES
)
5048 if (!current_attr
.pointer
)
5050 gfc_error ("Initialization at %C isn't for a pointer variable");
5055 m
= match_pointer_init (&initializer
, 1);
5059 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
5064 if (gfc_match_eos () == MATCH_YES
)
5066 if (gfc_match_char (',') != MATCH_YES
)
5071 gfc_error ("Syntax error in PROCEDURE statement at %C");
5075 /* Free stuff up and return. */
5076 gfc_free_expr (initializer
);
5082 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
5085 /* Match a procedure pointer component declaration (R445). */
5088 match_ppc_decl (void)
5091 gfc_symbol
*proc_if
= NULL
;
5095 gfc_expr
*initializer
= NULL
;
5096 gfc_typebound_proc
* tb
;
5097 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5099 /* Parse interface (with brackets). */
5100 m
= match_procedure_interface (&proc_if
);
5104 /* Parse attributes. */
5105 tb
= XCNEW (gfc_typebound_proc
);
5106 tb
->where
= gfc_current_locus
;
5107 m
= match_binding_attributes (tb
, false, true);
5108 if (m
== MATCH_ERROR
)
5111 gfc_clear_attr (¤t_attr
);
5112 current_attr
.procedure
= 1;
5113 current_attr
.proc_pointer
= 1;
5114 current_attr
.access
= tb
->access
;
5115 current_attr
.flavor
= FL_PROCEDURE
;
5117 /* Match the colons (required). */
5118 if (gfc_match (" ::") != MATCH_YES
)
5120 gfc_error ("Expected %<::%> after binding-attributes at %C");
5124 /* Check for C450. */
5125 if (!tb
->nopass
&& proc_if
== NULL
)
5127 gfc_error("NOPASS or explicit interface required at %C");
5131 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5134 /* Match PPC names. */
5138 m
= gfc_match_name (name
);
5141 else if (m
== MATCH_ERROR
)
5144 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5147 /* Add current_attr to the symbol attributes. */
5148 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5151 if (!gfc_add_external (&c
->attr
, NULL
))
5154 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5161 c
->tb
= XCNEW (gfc_typebound_proc
);
5162 c
->tb
->where
= gfc_current_locus
;
5166 /* Set interface. */
5167 if (proc_if
!= NULL
)
5169 c
->ts
.interface
= proc_if
;
5170 c
->attr
.untyped
= 1;
5171 c
->attr
.if_source
= IFSRC_IFBODY
;
5173 else if (ts
.type
!= BT_UNKNOWN
)
5176 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5177 c
->ts
.interface
->result
= c
->ts
.interface
;
5178 c
->ts
.interface
->ts
= ts
;
5179 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5180 c
->ts
.interface
->attr
.function
= 1;
5181 c
->attr
.function
= 1;
5182 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5185 if (gfc_match (" =>") == MATCH_YES
)
5187 m
= match_pointer_init (&initializer
, 1);
5190 gfc_free_expr (initializer
);
5193 c
->initializer
= initializer
;
5196 if (gfc_match_eos () == MATCH_YES
)
5198 if (gfc_match_char (',') != MATCH_YES
)
5203 gfc_error ("Syntax error in procedure pointer component at %C");
5208 /* Match a PROCEDURE declaration inside an interface (R1206). */
5211 match_procedure_in_interface (void)
5215 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5218 if (current_interface
.type
== INTERFACE_NAMELESS
5219 || current_interface
.type
== INTERFACE_ABSTRACT
)
5221 gfc_error ("PROCEDURE at %C must be in a generic interface");
5225 /* Check if the F2008 optional double colon appears. */
5226 gfc_gobble_whitespace ();
5227 old_locus
= gfc_current_locus
;
5228 if (gfc_match ("::") == MATCH_YES
)
5230 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5231 "MODULE PROCEDURE statement at %L", &old_locus
))
5235 gfc_current_locus
= old_locus
;
5239 m
= gfc_match_name (name
);
5242 else if (m
== MATCH_ERROR
)
5244 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5247 if (!gfc_add_interface (sym
))
5250 if (gfc_match_eos () == MATCH_YES
)
5252 if (gfc_match_char (',') != MATCH_YES
)
5259 gfc_error ("Syntax error in PROCEDURE statement at %C");
5264 /* General matcher for PROCEDURE declarations. */
5266 static match
match_procedure_in_type (void);
5269 gfc_match_procedure (void)
5273 switch (gfc_current_state ())
5278 case COMP_SUBROUTINE
:
5281 m
= match_procedure_decl ();
5283 case COMP_INTERFACE
:
5284 m
= match_procedure_in_interface ();
5287 m
= match_ppc_decl ();
5289 case COMP_DERIVED_CONTAINS
:
5290 m
= match_procedure_in_type ();
5299 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5306 /* Warn if a matched procedure has the same name as an intrinsic; this is
5307 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5308 parser-state-stack to find out whether we're in a module. */
5311 do_warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5315 in_module
= (gfc_state_stack
->previous
5316 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5318 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5322 /* Match a function declaration. */
5325 gfc_match_function_decl (void)
5327 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5328 gfc_symbol
*sym
, *result
;
5332 match found_match
; /* Status returned by match func. */
5334 if (gfc_current_state () != COMP_NONE
5335 && gfc_current_state () != COMP_INTERFACE
5336 && gfc_current_state () != COMP_CONTAINS
)
5339 gfc_clear_ts (¤t_ts
);
5341 old_loc
= gfc_current_locus
;
5343 m
= gfc_match_prefix (¤t_ts
);
5346 gfc_current_locus
= old_loc
;
5350 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5352 gfc_current_locus
= old_loc
;
5355 if (get_proc_name (name
, &sym
, false))
5358 if (add_hidden_procptr_result (sym
))
5361 gfc_new_block
= sym
;
5363 m
= gfc_match_formal_arglist (sym
, 0, 0);
5366 gfc_error ("Expected formal argument list in function "
5367 "definition at %C");
5371 else if (m
== MATCH_ERROR
)
5376 /* According to the draft, the bind(c) and result clause can
5377 come in either order after the formal_arg_list (i.e., either
5378 can be first, both can exist together or by themselves or neither
5379 one). Therefore, the match_result can't match the end of the
5380 string, and check for the bind(c) or result clause in either order. */
5381 found_match
= gfc_match_eos ();
5383 /* Make sure that it isn't already declared as BIND(C). If it is, it
5384 must have been marked BIND(C) with a BIND(C) attribute and that is
5385 not allowed for procedures. */
5386 if (sym
->attr
.is_bind_c
== 1)
5388 sym
->attr
.is_bind_c
= 0;
5389 if (sym
->old_symbol
!= NULL
)
5390 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5391 "variables or common blocks",
5392 &(sym
->old_symbol
->declared_at
));
5394 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5395 "variables or common blocks", &gfc_current_locus
);
5398 if (found_match
!= MATCH_YES
)
5400 /* If we haven't found the end-of-statement, look for a suffix. */
5401 suffix_match
= gfc_match_suffix (sym
, &result
);
5402 if (suffix_match
== MATCH_YES
)
5403 /* Need to get the eos now. */
5404 found_match
= gfc_match_eos ();
5406 found_match
= suffix_match
;
5409 if(found_match
!= MATCH_YES
)
5413 /* Make changes to the symbol. */
5416 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5419 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5420 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5423 /* Delay matching the function characteristics until after the
5424 specification block by signalling kind=-1. */
5425 sym
->declared_at
= old_loc
;
5426 if (current_ts
.type
!= BT_UNKNOWN
)
5427 current_ts
.kind
= -1;
5429 current_ts
.kind
= 0;
5433 if (current_ts
.type
!= BT_UNKNOWN
5434 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5440 if (current_ts
.type
!= BT_UNKNOWN
5441 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5443 sym
->result
= result
;
5446 /* Warn if this procedure has the same name as an intrinsic. */
5447 do_warn_intrinsic_shadow (sym
, true);
5453 gfc_current_locus
= old_loc
;
5458 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5459 pass the name of the entry, rather than the gfc_current_block name, and
5460 to return false upon finding an existing global entry. */
5463 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5467 enum gfc_symbol_type type
;
5469 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5471 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5472 name is a global identifier. */
5473 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5475 s
= gfc_get_gsymbol (name
);
5477 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5479 gfc_global_used (s
, where
);
5488 s
->ns
= gfc_current_ns
;
5492 /* Don't add the symbol multiple times. */
5494 && (!gfc_notification_std (GFC_STD_F2008
)
5495 || strcmp (name
, binding_label
) != 0))
5497 s
= gfc_get_gsymbol (binding_label
);
5499 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5501 gfc_global_used (s
, where
);
5508 s
->binding_label
= binding_label
;
5511 s
->ns
= gfc_current_ns
;
5519 /* Match an ENTRY statement. */
5522 gfc_match_entry (void)
5527 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5528 gfc_compile_state state
;
5532 bool module_procedure
;
5536 m
= gfc_match_name (name
);
5540 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5543 state
= gfc_current_state ();
5544 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5549 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5552 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5554 case COMP_BLOCK_DATA
:
5555 gfc_error ("ENTRY statement at %C cannot appear within "
5558 case COMP_INTERFACE
:
5559 gfc_error ("ENTRY statement at %C cannot appear within "
5563 gfc_error ("ENTRY statement at %C cannot appear within "
5564 "a DERIVED TYPE block");
5567 gfc_error ("ENTRY statement at %C cannot appear within "
5568 "an IF-THEN block");
5571 case COMP_DO_CONCURRENT
:
5572 gfc_error ("ENTRY statement at %C cannot appear within "
5576 gfc_error ("ENTRY statement at %C cannot appear within "
5580 gfc_error ("ENTRY statement at %C cannot appear within "
5584 gfc_error ("ENTRY statement at %C cannot appear within "
5588 gfc_error ("ENTRY statement at %C cannot appear within "
5589 "a contained subprogram");
5592 gfc_error ("Unexpected ENTRY statement at %C");
5597 module_procedure
= gfc_current_ns
->parent
!= NULL
5598 && gfc_current_ns
->parent
->proc_name
5599 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5602 if (gfc_current_ns
->parent
!= NULL
5603 && gfc_current_ns
->parent
->proc_name
5604 && !module_procedure
)
5606 gfc_error("ENTRY statement at %C cannot appear in a "
5607 "contained procedure");
5611 /* Module function entries need special care in get_proc_name
5612 because previous references within the function will have
5613 created symbols attached to the current namespace. */
5614 if (get_proc_name (name
, &entry
,
5615 gfc_current_ns
->parent
!= NULL
5616 && module_procedure
))
5619 proc
= gfc_current_block ();
5621 /* Make sure that it isn't already declared as BIND(C). If it is, it
5622 must have been marked BIND(C) with a BIND(C) attribute and that is
5623 not allowed for procedures. */
5624 if (entry
->attr
.is_bind_c
== 1)
5626 entry
->attr
.is_bind_c
= 0;
5627 if (entry
->old_symbol
!= NULL
)
5628 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5629 "variables or common blocks",
5630 &(entry
->old_symbol
->declared_at
));
5632 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5633 "variables or common blocks", &gfc_current_locus
);
5636 /* Check what next non-whitespace character is so we can tell if there
5637 is the required parens if we have a BIND(C). */
5638 old_loc
= gfc_current_locus
;
5639 gfc_gobble_whitespace ();
5640 peek_char
= gfc_peek_ascii_char ();
5642 if (state
== COMP_SUBROUTINE
)
5644 m
= gfc_match_formal_arglist (entry
, 0, 1);
5648 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5649 never be an internal procedure. */
5650 is_bind_c
= gfc_match_bind_c (entry
, true);
5651 if (is_bind_c
== MATCH_ERROR
)
5653 if (is_bind_c
== MATCH_YES
)
5655 if (peek_char
!= '(')
5657 gfc_error ("Missing required parentheses before BIND(C) at %C");
5660 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5661 &(entry
->declared_at
), 1))
5665 if (!gfc_current_ns
->parent
5666 && !add_global_entry (name
, entry
->binding_label
, true,
5670 /* An entry in a subroutine. */
5671 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5672 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5677 /* An entry in a function.
5678 We need to take special care because writing
5683 ENTRY f() RESULT (r)
5685 ENTRY f RESULT (r). */
5686 if (gfc_match_eos () == MATCH_YES
)
5688 gfc_current_locus
= old_loc
;
5689 /* Match the empty argument list, and add the interface to
5691 m
= gfc_match_formal_arglist (entry
, 0, 1);
5694 m
= gfc_match_formal_arglist (entry
, 0, 0);
5701 if (gfc_match_eos () == MATCH_YES
)
5703 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5704 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5707 entry
->result
= entry
;
5711 m
= gfc_match_suffix (entry
, &result
);
5713 gfc_syntax_error (ST_ENTRY
);
5719 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5720 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5721 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5723 entry
->result
= result
;
5727 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5728 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5730 entry
->result
= entry
;
5734 if (!gfc_current_ns
->parent
5735 && !add_global_entry (name
, entry
->binding_label
, false,
5740 if (gfc_match_eos () != MATCH_YES
)
5742 gfc_syntax_error (ST_ENTRY
);
5746 entry
->attr
.recursive
= proc
->attr
.recursive
;
5747 entry
->attr
.elemental
= proc
->attr
.elemental
;
5748 entry
->attr
.pure
= proc
->attr
.pure
;
5750 el
= gfc_get_entry_list ();
5752 el
->next
= gfc_current_ns
->entries
;
5753 gfc_current_ns
->entries
= el
;
5755 el
->id
= el
->next
->id
+ 1;
5759 new_st
.op
= EXEC_ENTRY
;
5760 new_st
.ext
.entry
= el
;
5766 /* Match a subroutine statement, including optional prefixes. */
5769 gfc_match_subroutine (void)
5771 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5776 bool allow_binding_name
;
5778 if (gfc_current_state () != COMP_NONE
5779 && gfc_current_state () != COMP_INTERFACE
5780 && gfc_current_state () != COMP_CONTAINS
)
5783 m
= gfc_match_prefix (NULL
);
5787 m
= gfc_match ("subroutine% %n", name
);
5791 if (get_proc_name (name
, &sym
, false))
5794 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5795 the symbol existed before. */
5796 sym
->declared_at
= gfc_current_locus
;
5798 if (add_hidden_procptr_result (sym
))
5801 gfc_new_block
= sym
;
5803 /* Check what next non-whitespace character is so we can tell if there
5804 is the required parens if we have a BIND(C). */
5805 gfc_gobble_whitespace ();
5806 peek_char
= gfc_peek_ascii_char ();
5808 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5811 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5814 /* Make sure that it isn't already declared as BIND(C). If it is, it
5815 must have been marked BIND(C) with a BIND(C) attribute and that is
5816 not allowed for procedures. */
5817 if (sym
->attr
.is_bind_c
== 1)
5819 sym
->attr
.is_bind_c
= 0;
5820 if (sym
->old_symbol
!= NULL
)
5821 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5822 "variables or common blocks",
5823 &(sym
->old_symbol
->declared_at
));
5825 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5826 "variables or common blocks", &gfc_current_locus
);
5829 /* C binding names are not allowed for internal procedures. */
5830 if (gfc_current_state () == COMP_CONTAINS
5831 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5832 allow_binding_name
= false;
5834 allow_binding_name
= true;
5836 /* Here, we are just checking if it has the bind(c) attribute, and if
5837 so, then we need to make sure it's all correct. If it doesn't,
5838 we still need to continue matching the rest of the subroutine line. */
5839 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5840 if (is_bind_c
== MATCH_ERROR
)
5842 /* There was an attempt at the bind(c), but it was wrong. An
5843 error message should have been printed w/in the gfc_match_bind_c
5844 so here we'll just return the MATCH_ERROR. */
5848 if (is_bind_c
== MATCH_YES
)
5850 /* The following is allowed in the Fortran 2008 draft. */
5851 if (gfc_current_state () == COMP_CONTAINS
5852 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5853 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5854 "at %L may not be specified for an internal "
5855 "procedure", &gfc_current_locus
))
5858 if (peek_char
!= '(')
5860 gfc_error ("Missing required parentheses before BIND(C) at %C");
5863 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5864 &(sym
->declared_at
), 1))
5868 if (gfc_match_eos () != MATCH_YES
)
5870 gfc_syntax_error (ST_SUBROUTINE
);
5874 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5877 /* Warn if it has the same name as an intrinsic. */
5878 do_warn_intrinsic_shadow (sym
, false);
5884 /* Check that the NAME identifier in a BIND attribute or statement
5885 is conform to C identifier rules. */
5888 check_bind_name_identifier (char **name
)
5890 char *n
= *name
, *p
;
5892 /* Remove leading spaces. */
5896 /* On an empty string, free memory and set name to NULL. */
5904 /* Remove trailing spaces. */
5905 p
= n
+ strlen(n
) - 1;
5909 /* Insert the identifier into the symbol table. */
5914 /* Now check that identifier is valid under C rules. */
5917 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5922 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
5924 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5932 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5933 given, and set the binding label in either the given symbol (if not
5934 NULL), or in the current_ts. The symbol may be NULL because we may
5935 encounter the BIND(C) before the declaration itself. Return
5936 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5937 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5938 or MATCH_YES if the specifier was correct and the binding label and
5939 bind(c) fields were set correctly for the given symbol or the
5940 current_ts. If allow_binding_name is false, no binding name may be
5944 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5946 char *binding_label
= NULL
;
5949 /* Initialize the flag that specifies whether we encountered a NAME=
5950 specifier or not. */
5951 has_name_equals
= 0;
5953 /* This much we have to be able to match, in this order, if
5954 there is a bind(c) label. */
5955 if (gfc_match (" bind ( c ") != MATCH_YES
)
5958 /* Now see if there is a binding label, or if we've reached the
5959 end of the bind(c) attribute without one. */
5960 if (gfc_match_char (',') == MATCH_YES
)
5962 if (gfc_match (" name = ") != MATCH_YES
)
5964 gfc_error ("Syntax error in NAME= specifier for binding label "
5966 /* should give an error message here */
5970 has_name_equals
= 1;
5972 if (gfc_match_init_expr (&e
) != MATCH_YES
)
5978 if (!gfc_simplify_expr(e
, 0))
5980 gfc_error ("NAME= specifier at %C should be a constant expression");
5985 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
5986 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
5988 gfc_error ("NAME= specifier at %C should be a scalar of "
5989 "default character kind");
5994 // Get a C string from the Fortran string constant
5995 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
5996 e
->value
.character
.length
);
5999 // Check that it is valid (old gfc_match_name_C)
6000 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
6004 /* Get the required right paren. */
6005 if (gfc_match_char (')') != MATCH_YES
)
6007 gfc_error ("Missing closing paren for binding label at %C");
6011 if (has_name_equals
&& !allow_binding_name
)
6013 gfc_error ("No binding name is allowed in BIND(C) at %C");
6017 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
6019 gfc_error ("For dummy procedure %s, no binding name is "
6020 "allowed in BIND(C) at %C", sym
->name
);
6025 /* Save the binding label to the symbol. If sym is null, we're
6026 probably matching the typespec attributes of a declaration and
6027 haven't gotten the name yet, and therefore, no symbol yet. */
6031 sym
->binding_label
= binding_label
;
6033 curr_binding_label
= binding_label
;
6035 else if (allow_binding_name
)
6037 /* No binding label, but if symbol isn't null, we
6038 can set the label for it here.
6039 If name="" or allow_binding_name is false, no C binding name is
6041 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
6042 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
6045 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
6046 && current_interface
.type
== INTERFACE_ABSTRACT
)
6048 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6056 /* Return nonzero if we're currently compiling a contained procedure. */
6059 contained_procedure (void)
6061 gfc_state_data
*s
= gfc_state_stack
;
6063 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
6064 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
6070 /* Set the kind of each enumerator. The kind is selected such that it is
6071 interoperable with the corresponding C enumeration type, making
6072 sure that -fshort-enums is honored. */
6077 enumerator_history
*current_history
= NULL
;
6081 if (max_enum
== NULL
|| enum_history
== NULL
)
6084 if (!flag_short_enums
)
6090 kind
= gfc_integer_kinds
[i
++].kind
;
6092 while (kind
< gfc_c_int_kind
6093 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6096 current_history
= enum_history
;
6097 while (current_history
!= NULL
)
6099 current_history
->sym
->ts
.kind
= kind
;
6100 current_history
= current_history
->next
;
6105 /* Match any of the various end-block statements. Returns the type of
6106 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6107 and END BLOCK statements cannot be replaced by a single END statement. */
6110 gfc_match_end (gfc_statement
*st
)
6112 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6113 gfc_compile_state state
;
6115 const char *block_name
;
6119 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6120 gfc_namespace
**nsp
;
6122 old_loc
= gfc_current_locus
;
6123 if (gfc_match ("end") != MATCH_YES
)
6126 state
= gfc_current_state ();
6127 block_name
= gfc_current_block () == NULL
6128 ? NULL
: gfc_current_block ()->name
;
6132 case COMP_ASSOCIATE
:
6134 if (!strncmp (block_name
, "block@", strlen("block@")))
6139 case COMP_DERIVED_CONTAINS
:
6140 state
= gfc_state_stack
->previous
->state
;
6141 block_name
= gfc_state_stack
->previous
->sym
== NULL
6142 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6153 *st
= ST_END_PROGRAM
;
6154 target
= " program";
6158 case COMP_SUBROUTINE
:
6159 *st
= ST_END_SUBROUTINE
;
6160 target
= " subroutine";
6161 eos_ok
= !contained_procedure ();
6165 *st
= ST_END_FUNCTION
;
6166 target
= " function";
6167 eos_ok
= !contained_procedure ();
6170 case COMP_BLOCK_DATA
:
6171 *st
= ST_END_BLOCK_DATA
;
6172 target
= " block data";
6177 *st
= ST_END_MODULE
;
6182 case COMP_INTERFACE
:
6183 *st
= ST_END_INTERFACE
;
6184 target
= " interface";
6189 case COMP_DERIVED_CONTAINS
:
6195 case COMP_ASSOCIATE
:
6196 *st
= ST_END_ASSOCIATE
;
6197 target
= " associate";
6214 case COMP_DO_CONCURRENT
:
6221 *st
= ST_END_CRITICAL
;
6222 target
= " critical";
6227 case COMP_SELECT_TYPE
:
6228 *st
= ST_END_SELECT
;
6234 *st
= ST_END_FORALL
;
6249 last_initializer
= NULL
;
6251 gfc_free_enum_history ();
6255 gfc_error ("Unexpected END statement at %C");
6259 old_loc
= gfc_current_locus
;
6260 if (gfc_match_eos () == MATCH_YES
)
6262 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6264 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6265 "instead of %s statement at %L",
6266 gfc_ascii_statement(*st
), &old_loc
))
6271 /* We would have required END [something]. */
6272 gfc_error ("%s statement expected at %L",
6273 gfc_ascii_statement (*st
), &old_loc
);
6280 /* Verify that we've got the sort of end-block that we're expecting. */
6281 if (gfc_match (target
) != MATCH_YES
)
6283 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6288 old_loc
= gfc_current_locus
;
6289 /* If we're at the end, make sure a block name wasn't required. */
6290 if (gfc_match_eos () == MATCH_YES
)
6293 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6294 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6295 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6301 gfc_error ("Expected block name of %qs in %s statement at %L",
6302 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6307 /* END INTERFACE has a special handler for its several possible endings. */
6308 if (*st
== ST_END_INTERFACE
)
6309 return gfc_match_end_interface ();
6311 /* We haven't hit the end of statement, so what is left must be an
6313 m
= gfc_match_space ();
6315 m
= gfc_match_name (name
);
6318 gfc_error ("Expected terminating name at %C");
6322 if (block_name
== NULL
)
6325 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6327 gfc_error ("Expected label %qs for %s statement at %C", block_name
,
6328 gfc_ascii_statement (*st
));
6331 /* Procedure pointer as function result. */
6332 else if (strcmp (block_name
, "ppr@") == 0
6333 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6335 gfc_error ("Expected label %qs for %s statement at %C",
6336 gfc_current_block ()->ns
->proc_name
->name
,
6337 gfc_ascii_statement (*st
));
6341 if (gfc_match_eos () == MATCH_YES
)
6345 gfc_syntax_error (*st
);
6348 gfc_current_locus
= old_loc
;
6350 /* If we are missing an END BLOCK, we created a half-ready namespace.
6351 Remove it from the parent namespace's sibling list. */
6353 if (state
== COMP_BLOCK
)
6355 parent_ns
= gfc_current_ns
->parent
;
6357 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6363 if (ns
== gfc_current_ns
)
6365 if (prev_ns
== NULL
)
6368 prev_ns
->sibling
= ns
->sibling
;
6374 gfc_free_namespace (gfc_current_ns
);
6375 gfc_current_ns
= parent_ns
;
6383 /***************** Attribute declaration statements ****************/
6385 /* Set the attribute of a single variable. */
6390 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6393 /* Workaround -Wmaybe-uninitialized false positive during
6394 profiledbootstrap by initializing them. */
6395 gfc_symbol
*sym
= NULL
;
6401 m
= gfc_match_name (name
);
6405 if (find_special (name
, &sym
, false))
6408 if (!check_function_name (name
))
6414 var_locus
= gfc_current_locus
;
6416 /* Deal with possible array specification for certain attributes. */
6417 if (current_attr
.dimension
6418 || current_attr
.codimension
6419 || current_attr
.allocatable
6420 || current_attr
.pointer
6421 || current_attr
.target
)
6423 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6424 !current_attr
.dimension
6425 && !current_attr
.pointer
6426 && !current_attr
.target
);
6427 if (m
== MATCH_ERROR
)
6430 if (current_attr
.dimension
&& m
== MATCH_NO
)
6432 gfc_error ("Missing array specification at %L in DIMENSION "
6433 "statement", &var_locus
);
6438 if (current_attr
.dimension
&& sym
->value
)
6440 gfc_error ("Dimensions specified for %s at %L after its "
6441 "initialisation", sym
->name
, &var_locus
);
6446 if (current_attr
.codimension
&& m
== MATCH_NO
)
6448 gfc_error ("Missing array specification at %L in CODIMENSION "
6449 "statement", &var_locus
);
6454 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6455 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6457 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6463 /* Update symbol table. DIMENSION attribute is set in
6464 gfc_set_array_spec(). For CLASS variables, this must be applied
6465 to the first component, or '_data' field. */
6466 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6468 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6476 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6477 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6484 if (sym
->ts
.type
== BT_CLASS
6485 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6491 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6497 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6499 /* Fix the array spec. */
6500 m
= gfc_mod_pointee_as (sym
->as
);
6501 if (m
== MATCH_ERROR
)
6505 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6511 if ((current_attr
.external
|| current_attr
.intrinsic
)
6512 && sym
->attr
.flavor
!= FL_PROCEDURE
6513 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6519 add_hidden_procptr_result (sym
);
6524 gfc_free_array_spec (as
);
6529 /* Generic attribute declaration subroutine. Used for attributes that
6530 just have a list of names. */
6537 /* Gobble the optional double colon, by simply ignoring the result
6547 if (gfc_match_eos () == MATCH_YES
)
6553 if (gfc_match_char (',') != MATCH_YES
)
6555 gfc_error ("Unexpected character in variable list at %C");
6565 /* This routine matches Cray Pointer declarations of the form:
6566 pointer ( <pointer>, <pointee> )
6568 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6569 The pointer, if already declared, should be an integer. Otherwise, we
6570 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6571 be either a scalar, or an array declaration. No space is allocated for
6572 the pointee. For the statement
6573 pointer (ipt, ar(10))
6574 any subsequent uses of ar will be translated (in C-notation) as
6575 ar(i) => ((<type> *) ipt)(i)
6576 After gimplification, pointee variable will disappear in the code. */
6579 cray_pointer_decl (void)
6582 gfc_array_spec
*as
= NULL
;
6583 gfc_symbol
*cptr
; /* Pointer symbol. */
6584 gfc_symbol
*cpte
; /* Pointee symbol. */
6590 if (gfc_match_char ('(') != MATCH_YES
)
6592 gfc_error ("Expected %<(%> at %C");
6596 /* Match pointer. */
6597 var_locus
= gfc_current_locus
;
6598 gfc_clear_attr (¤t_attr
);
6599 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6600 current_ts
.type
= BT_INTEGER
;
6601 current_ts
.kind
= gfc_index_integer_kind
;
6603 m
= gfc_match_symbol (&cptr
, 0);
6606 gfc_error ("Expected variable name at %C");
6610 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6613 gfc_set_sym_referenced (cptr
);
6615 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6617 cptr
->ts
.type
= BT_INTEGER
;
6618 cptr
->ts
.kind
= gfc_index_integer_kind
;
6620 else if (cptr
->ts
.type
!= BT_INTEGER
)
6622 gfc_error ("Cray pointer at %C must be an integer");
6625 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6626 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6627 " memory addresses require %d bytes",
6628 cptr
->ts
.kind
, gfc_index_integer_kind
);
6630 if (gfc_match_char (',') != MATCH_YES
)
6632 gfc_error ("Expected \",\" at %C");
6636 /* Match Pointee. */
6637 var_locus
= gfc_current_locus
;
6638 gfc_clear_attr (¤t_attr
);
6639 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6640 current_ts
.type
= BT_UNKNOWN
;
6641 current_ts
.kind
= 0;
6643 m
= gfc_match_symbol (&cpte
, 0);
6646 gfc_error ("Expected variable name at %C");
6650 /* Check for an optional array spec. */
6651 m
= gfc_match_array_spec (&as
, true, false);
6652 if (m
== MATCH_ERROR
)
6654 gfc_free_array_spec (as
);
6657 else if (m
== MATCH_NO
)
6659 gfc_free_array_spec (as
);
6663 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6666 gfc_set_sym_referenced (cpte
);
6668 if (cpte
->as
== NULL
)
6670 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6671 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6673 else if (as
!= NULL
)
6675 gfc_error ("Duplicate array spec for Cray pointee at %C");
6676 gfc_free_array_spec (as
);
6682 if (cpte
->as
!= NULL
)
6684 /* Fix array spec. */
6685 m
= gfc_mod_pointee_as (cpte
->as
);
6686 if (m
== MATCH_ERROR
)
6690 /* Point the Pointee at the Pointer. */
6691 cpte
->cp_pointer
= cptr
;
6693 if (gfc_match_char (')') != MATCH_YES
)
6695 gfc_error ("Expected \")\" at %C");
6698 m
= gfc_match_char (',');
6700 done
= true; /* Stop searching for more declarations. */
6704 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6705 || gfc_match_eos () != MATCH_YES
)
6707 gfc_error ("Expected %<,%> or end of statement at %C");
6715 gfc_match_external (void)
6718 gfc_clear_attr (¤t_attr
);
6719 current_attr
.external
= 1;
6721 return attr_decl ();
6726 gfc_match_intent (void)
6730 /* This is not allowed within a BLOCK construct! */
6731 if (gfc_current_state () == COMP_BLOCK
)
6733 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6737 intent
= match_intent_spec ();
6738 if (intent
== INTENT_UNKNOWN
)
6741 gfc_clear_attr (¤t_attr
);
6742 current_attr
.intent
= intent
;
6744 return attr_decl ();
6749 gfc_match_intrinsic (void)
6752 gfc_clear_attr (¤t_attr
);
6753 current_attr
.intrinsic
= 1;
6755 return attr_decl ();
6760 gfc_match_optional (void)
6762 /* This is not allowed within a BLOCK construct! */
6763 if (gfc_current_state () == COMP_BLOCK
)
6765 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6769 gfc_clear_attr (¤t_attr
);
6770 current_attr
.optional
= 1;
6772 return attr_decl ();
6777 gfc_match_pointer (void)
6779 gfc_gobble_whitespace ();
6780 if (gfc_peek_ascii_char () == '(')
6782 if (!flag_cray_pointer
)
6784 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6788 return cray_pointer_decl ();
6792 gfc_clear_attr (¤t_attr
);
6793 current_attr
.pointer
= 1;
6795 return attr_decl ();
6801 gfc_match_allocatable (void)
6803 gfc_clear_attr (¤t_attr
);
6804 current_attr
.allocatable
= 1;
6806 return attr_decl ();
6811 gfc_match_codimension (void)
6813 gfc_clear_attr (¤t_attr
);
6814 current_attr
.codimension
= 1;
6816 return attr_decl ();
6821 gfc_match_contiguous (void)
6823 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6826 gfc_clear_attr (¤t_attr
);
6827 current_attr
.contiguous
= 1;
6829 return attr_decl ();
6834 gfc_match_dimension (void)
6836 gfc_clear_attr (¤t_attr
);
6837 current_attr
.dimension
= 1;
6839 return attr_decl ();
6844 gfc_match_target (void)
6846 gfc_clear_attr (¤t_attr
);
6847 current_attr
.target
= 1;
6849 return attr_decl ();
6853 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6857 access_attr_decl (gfc_statement st
)
6859 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6860 interface_type type
;
6862 gfc_symbol
*sym
, *dt_sym
;
6863 gfc_intrinsic_op op
;
6866 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6871 m
= gfc_match_generic_spec (&type
, name
, &op
);
6874 if (m
== MATCH_ERROR
)
6879 case INTERFACE_NAMELESS
:
6880 case INTERFACE_ABSTRACT
:
6883 case INTERFACE_GENERIC
:
6884 if (gfc_get_symbol (name
, NULL
, &sym
))
6887 if (!gfc_add_access (&sym
->attr
,
6889 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6893 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6894 && !gfc_add_access (&dt_sym
->attr
,
6896 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6902 case INTERFACE_INTRINSIC_OP
:
6903 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6905 gfc_intrinsic_op other_op
;
6907 gfc_current_ns
->operator_access
[op
] =
6908 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6910 /* Handle the case if there is another op with the same
6911 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6912 other_op
= gfc_equivalent_op (op
);
6914 if (other_op
!= INTRINSIC_NONE
)
6915 gfc_current_ns
->operator_access
[other_op
] =
6916 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6921 gfc_error ("Access specification of the %s operator at %C has "
6922 "already been specified", gfc_op2string (op
));
6928 case INTERFACE_USER_OP
:
6929 uop
= gfc_get_uop (name
);
6931 if (uop
->access
== ACCESS_UNKNOWN
)
6933 uop
->access
= (st
== ST_PUBLIC
)
6934 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6938 gfc_error ("Access specification of the .%s. operator at %C "
6939 "has already been specified", sym
->name
);
6946 if (gfc_match_char (',') == MATCH_NO
)
6950 if (gfc_match_eos () != MATCH_YES
)
6955 gfc_syntax_error (st
);
6963 gfc_match_protected (void)
6968 if (!gfc_current_ns
->proc_name
6969 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6971 gfc_error ("PROTECTED at %C only allowed in specification "
6972 "part of a module");
6977 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6980 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6985 if (gfc_match_eos () == MATCH_YES
)
6990 m
= gfc_match_symbol (&sym
, 0);
6994 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7006 if (gfc_match_eos () == MATCH_YES
)
7008 if (gfc_match_char (',') != MATCH_YES
)
7015 gfc_error ("Syntax error in PROTECTED statement at %C");
7020 /* The PRIVATE statement is a bit weird in that it can be an attribute
7021 declaration, but also works as a standalone statement inside of a
7022 type declaration or a module. */
7025 gfc_match_private (gfc_statement
*st
)
7028 if (gfc_match ("private") != MATCH_YES
)
7031 if (gfc_current_state () != COMP_MODULE
7032 && !(gfc_current_state () == COMP_DERIVED
7033 && gfc_state_stack
->previous
7034 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
7035 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7036 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
7037 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
7039 gfc_error ("PRIVATE statement at %C is only allowed in the "
7040 "specification part of a module");
7044 if (gfc_current_state () == COMP_DERIVED
)
7046 if (gfc_match_eos () == MATCH_YES
)
7052 gfc_syntax_error (ST_PRIVATE
);
7056 if (gfc_match_eos () == MATCH_YES
)
7063 return access_attr_decl (ST_PRIVATE
);
7068 gfc_match_public (gfc_statement
*st
)
7071 if (gfc_match ("public") != MATCH_YES
)
7074 if (gfc_current_state () != COMP_MODULE
)
7076 gfc_error ("PUBLIC statement at %C is only allowed in the "
7077 "specification part of a module");
7081 if (gfc_match_eos () == MATCH_YES
)
7088 return access_attr_decl (ST_PUBLIC
);
7092 /* Workhorse for gfc_match_parameter. */
7102 m
= gfc_match_symbol (&sym
, 0);
7104 gfc_error ("Expected variable name at %C in PARAMETER statement");
7109 if (gfc_match_char ('=') == MATCH_NO
)
7111 gfc_error ("Expected = sign in PARAMETER statement at %C");
7115 m
= gfc_match_init_expr (&init
);
7117 gfc_error ("Expected expression at %C in PARAMETER statement");
7121 if (sym
->ts
.type
== BT_UNKNOWN
7122 && !gfc_set_default_type (sym
, 1, NULL
))
7128 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7129 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7137 gfc_error ("Initializing already initialized variable at %C");
7142 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7143 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7146 gfc_free_expr (init
);
7151 /* Match a parameter statement, with the weird syntax that these have. */
7154 gfc_match_parameter (void)
7158 if (gfc_match_char ('(') == MATCH_NO
)
7167 if (gfc_match (" )%t") == MATCH_YES
)
7170 if (gfc_match_char (',') != MATCH_YES
)
7172 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7182 /* Save statements have a special syntax. */
7185 gfc_match_save (void)
7187 char n
[GFC_MAX_SYMBOL_LEN
+1];
7192 if (gfc_match_eos () == MATCH_YES
)
7194 if (gfc_current_ns
->seen_save
)
7196 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7197 "follows previous SAVE statement"))
7201 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7205 if (gfc_current_ns
->save_all
)
7207 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7208 "blanket SAVE statement"))
7216 m
= gfc_match_symbol (&sym
, 0);
7220 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7221 &gfc_current_locus
))
7232 m
= gfc_match (" / %n /", &n
);
7233 if (m
== MATCH_ERROR
)
7238 c
= gfc_get_common (n
, 0);
7241 gfc_current_ns
->seen_save
= 1;
7244 if (gfc_match_eos () == MATCH_YES
)
7246 if (gfc_match_char (',') != MATCH_YES
)
7253 gfc_error ("Syntax error in SAVE statement at %C");
7259 gfc_match_value (void)
7264 /* This is not allowed within a BLOCK construct! */
7265 if (gfc_current_state () == COMP_BLOCK
)
7267 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7271 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7274 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7279 if (gfc_match_eos () == MATCH_YES
)
7284 m
= gfc_match_symbol (&sym
, 0);
7288 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7300 if (gfc_match_eos () == MATCH_YES
)
7302 if (gfc_match_char (',') != MATCH_YES
)
7309 gfc_error ("Syntax error in VALUE statement at %C");
7315 gfc_match_volatile (void)
7320 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7323 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7328 if (gfc_match_eos () == MATCH_YES
)
7333 /* VOLATILE is special because it can be added to host-associated
7334 symbols locally. Except for coarrays. */
7335 m
= gfc_match_symbol (&sym
, 1);
7339 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7340 for variable in a BLOCK which is defined outside of the BLOCK. */
7341 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7343 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7344 "%C, which is use-/host-associated", sym
->name
);
7347 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7359 if (gfc_match_eos () == MATCH_YES
)
7361 if (gfc_match_char (',') != MATCH_YES
)
7368 gfc_error ("Syntax error in VOLATILE statement at %C");
7374 gfc_match_asynchronous (void)
7379 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7382 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7387 if (gfc_match_eos () == MATCH_YES
)
7392 /* ASYNCHRONOUS is special because it can be added to host-associated
7394 m
= gfc_match_symbol (&sym
, 1);
7398 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7410 if (gfc_match_eos () == MATCH_YES
)
7412 if (gfc_match_char (',') != MATCH_YES
)
7419 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7424 /* Match a module procedure statement. Note that we have to modify
7425 symbols in the parent's namespace because the current one was there
7426 to receive symbols that are in an interface's formal argument list. */
7429 gfc_match_modproc (void)
7431 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7435 gfc_namespace
*module_ns
;
7436 gfc_interface
*old_interface_head
, *interface
;
7438 if (gfc_state_stack
->state
!= COMP_INTERFACE
7439 || gfc_state_stack
->previous
== NULL
7440 || current_interface
.type
== INTERFACE_NAMELESS
7441 || current_interface
.type
== INTERFACE_ABSTRACT
)
7443 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7448 module_ns
= gfc_current_ns
->parent
;
7449 for (; module_ns
; module_ns
= module_ns
->parent
)
7450 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7451 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7452 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7453 && !module_ns
->proc_name
->attr
.contained
))
7456 if (module_ns
== NULL
)
7459 /* Store the current state of the interface. We will need it if we
7460 end up with a syntax error and need to recover. */
7461 old_interface_head
= gfc_current_interface_head ();
7463 /* Check if the F2008 optional double colon appears. */
7464 gfc_gobble_whitespace ();
7465 old_locus
= gfc_current_locus
;
7466 if (gfc_match ("::") == MATCH_YES
)
7468 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7469 "MODULE PROCEDURE statement at %L", &old_locus
))
7473 gfc_current_locus
= old_locus
;
7478 old_locus
= gfc_current_locus
;
7480 m
= gfc_match_name (name
);
7486 /* Check for syntax error before starting to add symbols to the
7487 current namespace. */
7488 if (gfc_match_eos () == MATCH_YES
)
7491 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7494 /* Now we're sure the syntax is valid, we process this item
7496 if (gfc_get_symbol (name
, module_ns
, &sym
))
7499 if (sym
->attr
.intrinsic
)
7501 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7502 "PROCEDURE", &old_locus
);
7506 if (sym
->attr
.proc
!= PROC_MODULE
7507 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7510 if (!gfc_add_interface (sym
))
7513 sym
->attr
.mod_proc
= 1;
7514 sym
->declared_at
= old_locus
;
7523 /* Restore the previous state of the interface. */
7524 interface
= gfc_current_interface_head ();
7525 gfc_set_current_interface_head (old_interface_head
);
7527 /* Free the new interfaces. */
7528 while (interface
!= old_interface_head
)
7530 gfc_interface
*i
= interface
->next
;
7535 /* And issue a syntax error. */
7536 gfc_syntax_error (ST_MODULE_PROC
);
7541 /* Check a derived type that is being extended. */
7544 check_extended_derived_type (char *name
)
7546 gfc_symbol
*extended
;
7548 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7550 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7554 extended
= gfc_find_dt_in_generic (extended
);
7559 gfc_error ("Symbol %qs at %C has not been previously defined", name
);
7563 if (extended
->attr
.flavor
!= FL_DERIVED
)
7565 gfc_error ("%qs in EXTENDS expression at %C is not a "
7566 "derived type", name
);
7570 if (extended
->attr
.is_bind_c
)
7572 gfc_error ("%qs cannot be extended at %C because it "
7573 "is BIND(C)", extended
->name
);
7577 if (extended
->attr
.sequence
)
7579 gfc_error ("%qs cannot be extended at %C because it "
7580 "is a SEQUENCE type", extended
->name
);
7588 /* Match the optional attribute specifiers for a type declaration.
7589 Return MATCH_ERROR if an error is encountered in one of the handled
7590 attributes (public, private, bind(c)), MATCH_NO if what's found is
7591 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7592 checking on attribute conflicts needs to be done. */
7595 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7597 /* See if the derived type is marked as private. */
7598 if (gfc_match (" , private") == MATCH_YES
)
7600 if (gfc_current_state () != COMP_MODULE
)
7602 gfc_error ("Derived type at %C can only be PRIVATE in the "
7603 "specification part of a module");
7607 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7610 else if (gfc_match (" , public") == MATCH_YES
)
7612 if (gfc_current_state () != COMP_MODULE
)
7614 gfc_error ("Derived type at %C can only be PUBLIC in the "
7615 "specification part of a module");
7619 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7622 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7624 /* If the type is defined to be bind(c) it then needs to make
7625 sure that all fields are interoperable. This will
7626 need to be a semantic check on the finished derived type.
7627 See 15.2.3 (lines 9-12) of F2003 draft. */
7628 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7631 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7633 else if (gfc_match (" , abstract") == MATCH_YES
)
7635 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7638 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7641 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7643 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7649 /* If we get here, something matched. */
7654 /* Match the beginning of a derived type declaration. If a type name
7655 was the result of a function, then it is possible to have a symbol
7656 already to be known as a derived type yet have no components. */
7659 gfc_match_derived_decl (void)
7661 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7662 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7663 symbol_attribute attr
;
7664 gfc_symbol
*sym
, *gensym
;
7665 gfc_symbol
*extended
;
7667 match is_type_attr_spec
= MATCH_NO
;
7668 bool seen_attr
= false;
7669 gfc_interface
*intr
= NULL
, *head
;
7671 if (gfc_current_state () == COMP_DERIVED
)
7676 gfc_clear_attr (&attr
);
7681 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7682 if (is_type_attr_spec
== MATCH_ERROR
)
7684 if (is_type_attr_spec
== MATCH_YES
)
7686 } while (is_type_attr_spec
== MATCH_YES
);
7688 /* Deal with derived type extensions. The extension attribute has
7689 been added to 'attr' but now the parent type must be found and
7692 extended
= check_extended_derived_type (parent
);
7694 if (parent
[0] && !extended
)
7697 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7699 gfc_error ("Expected :: in TYPE definition at %C");
7703 m
= gfc_match (" %n%t", name
);
7707 /* Make sure the name is not the name of an intrinsic type. */
7708 if (gfc_is_intrinsic_typename (name
))
7710 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7715 if (gfc_get_symbol (name
, NULL
, &gensym
))
7718 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7720 gfc_error ("Derived type name %qs at %C already has a basic type "
7721 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7725 if (!gensym
->attr
.generic
7726 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7729 if (!gensym
->attr
.function
7730 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7733 sym
= gfc_find_dt_in_generic (gensym
);
7735 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7737 gfc_error ("Derived type definition of %qs at %C has already been "
7738 "defined", sym
->name
);
7744 /* Use upper case to save the actual derived-type symbol. */
7745 gfc_get_symbol (gfc_get_string ("%c%s",
7746 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7747 &gensym
->name
[1]), NULL
, &sym
);
7748 sym
->name
= gfc_get_string (gensym
->name
);
7749 head
= gensym
->generic
;
7750 intr
= gfc_get_interface ();
7752 intr
->where
= gfc_current_locus
;
7753 intr
->sym
->declared_at
= gfc_current_locus
;
7755 gensym
->generic
= intr
;
7756 gensym
->attr
.if_source
= IFSRC_DECL
;
7759 /* The symbol may already have the derived attribute without the
7760 components. The ways this can happen is via a function
7761 definition, an INTRINSIC statement or a subtype in another
7762 derived type that is a pointer. The first part of the AND clause
7763 is true if the symbol is not the return value of a function. */
7764 if (sym
->attr
.flavor
!= FL_DERIVED
7765 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7768 if (attr
.access
!= ACCESS_UNKNOWN
7769 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7771 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7772 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7773 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7777 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7778 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7779 gensym
->attr
.access
= sym
->attr
.access
;
7781 /* See if the derived type was labeled as bind(c). */
7782 if (attr
.is_bind_c
!= 0)
7783 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7785 /* Construct the f2k_derived namespace if it is not yet there. */
7786 if (!sym
->f2k_derived
)
7787 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7789 if (extended
&& !sym
->components
)
7793 /* Add the extended derived type as the first component. */
7794 gfc_add_component (sym
, parent
, &p
);
7796 gfc_set_sym_referenced (extended
);
7798 p
->ts
.type
= BT_DERIVED
;
7799 p
->ts
.u
.derived
= extended
;
7800 p
->initializer
= gfc_default_initializer (&p
->ts
);
7802 /* Set extension level. */
7803 if (extended
->attr
.extension
== 255)
7805 /* Since the extension field is 8 bit wide, we can only have
7806 up to 255 extension levels. */
7807 gfc_error ("Maximum extension level reached with type %qs at %L",
7808 extended
->name
, &extended
->declared_at
);
7811 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7813 /* Provide the links between the extended type and its extension. */
7814 if (!extended
->f2k_derived
)
7815 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7818 if (!sym
->hash_value
)
7819 /* Set the hash for the compound name for this type. */
7820 sym
->hash_value
= gfc_hash_value (sym
);
7822 /* Take over the ABSTRACT attribute. */
7823 sym
->attr
.abstract
= attr
.abstract
;
7825 gfc_new_block
= sym
;
7831 /* Cray Pointees can be declared as:
7832 pointer (ipt, a (n,m,...,*)) */
7835 gfc_mod_pointee_as (gfc_array_spec
*as
)
7837 as
->cray_pointee
= true; /* This will be useful to know later. */
7838 if (as
->type
== AS_ASSUMED_SIZE
)
7839 as
->cp_was_assumed
= true;
7840 else if (as
->type
== AS_ASSUMED_SHAPE
)
7842 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7849 /* Match the enum definition statement, here we are trying to match
7850 the first line of enum definition statement.
7851 Returns MATCH_YES if match is found. */
7854 gfc_match_enum (void)
7858 m
= gfc_match_eos ();
7862 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7869 /* Returns an initializer whose value is one higher than the value of the
7870 LAST_INITIALIZER argument. If the argument is NULL, the
7871 initializers value will be set to zero. The initializer's kind
7872 will be set to gfc_c_int_kind.
7874 If -fshort-enums is given, the appropriate kind will be selected
7875 later after all enumerators have been parsed. A warning is issued
7876 here if an initializer exceeds gfc_c_int_kind. */
7879 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7882 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7884 mpz_init (result
->value
.integer
);
7886 if (last_initializer
!= NULL
)
7888 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7889 result
->where
= last_initializer
->where
;
7891 if (gfc_check_integer_range (result
->value
.integer
,
7892 gfc_c_int_kind
) != ARITH_OK
)
7894 gfc_error ("Enumerator exceeds the C integer type at %C");
7900 /* Control comes here, if it's the very first enumerator and no
7901 initializer has been given. It will be initialized to zero. */
7902 mpz_set_si (result
->value
.integer
, 0);
7909 /* Match a variable name with an optional initializer. When this
7910 subroutine is called, a variable is expected to be parsed next.
7911 Depending on what is happening at the moment, updates either the
7912 symbol table or the current interface. */
7915 enumerator_decl (void)
7917 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7918 gfc_expr
*initializer
;
7919 gfc_array_spec
*as
= NULL
;
7927 old_locus
= gfc_current_locus
;
7929 /* When we get here, we've just matched a list of attributes and
7930 maybe a type and a double colon. The next thing we expect to see
7931 is the name of the symbol. */
7932 m
= gfc_match_name (name
);
7936 var_locus
= gfc_current_locus
;
7938 /* OK, we've successfully matched the declaration. Now put the
7939 symbol in the current namespace. If we fail to create the symbol,
7941 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7947 /* The double colon must be present in order to have initializers.
7948 Otherwise the statement is ambiguous with an assignment statement. */
7951 if (gfc_match_char ('=') == MATCH_YES
)
7953 m
= gfc_match_init_expr (&initializer
);
7956 gfc_error ("Expected an initialization expression at %C");
7965 /* If we do not have an initializer, the initialization value of the
7966 previous enumerator (stored in last_initializer) is incremented
7967 by 1 and is used to initialize the current enumerator. */
7968 if (initializer
== NULL
)
7969 initializer
= enum_initializer (last_initializer
, old_locus
);
7971 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7973 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7979 /* Store this current initializer, for the next enumerator variable
7980 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7981 use last_initializer below. */
7982 last_initializer
= initializer
;
7983 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7985 /* Maintain enumerator history. */
7986 gfc_find_symbol (name
, NULL
, 0, &sym
);
7987 create_enum_history (sym
, last_initializer
);
7989 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7992 /* Free stuff up and return. */
7993 gfc_free_expr (initializer
);
7999 /* Match the enumerator definition statement. */
8002 gfc_match_enumerator_def (void)
8007 gfc_clear_ts (¤t_ts
);
8009 m
= gfc_match (" enumerator");
8013 m
= gfc_match (" :: ");
8014 if (m
== MATCH_ERROR
)
8017 colon_seen
= (m
== MATCH_YES
);
8019 if (gfc_current_state () != COMP_ENUM
)
8021 gfc_error ("ENUM definition statement expected before %C");
8022 gfc_free_enum_history ();
8026 (¤t_ts
)->type
= BT_INTEGER
;
8027 (¤t_ts
)->kind
= gfc_c_int_kind
;
8029 gfc_clear_attr (¤t_attr
);
8030 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
8039 m
= enumerator_decl ();
8040 if (m
== MATCH_ERROR
)
8042 gfc_free_enum_history ();
8048 if (gfc_match_eos () == MATCH_YES
)
8050 if (gfc_match_char (',') != MATCH_YES
)
8054 if (gfc_current_state () == COMP_ENUM
)
8056 gfc_free_enum_history ();
8057 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8062 gfc_free_array_spec (current_as
);
8069 /* Match binding attributes. */
8072 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
8074 bool found_passing
= false;
8075 bool seen_ptr
= false;
8076 match m
= MATCH_YES
;
8078 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8079 this case the defaults are in there. */
8080 ba
->access
= ACCESS_UNKNOWN
;
8081 ba
->pass_arg
= NULL
;
8082 ba
->pass_arg_num
= 0;
8084 ba
->non_overridable
= 0;
8088 /* If we find a comma, we believe there are binding attributes. */
8089 m
= gfc_match_char (',');
8095 /* Access specifier. */
8097 m
= gfc_match (" public");
8098 if (m
== MATCH_ERROR
)
8102 if (ba
->access
!= ACCESS_UNKNOWN
)
8104 gfc_error ("Duplicate access-specifier at %C");
8108 ba
->access
= ACCESS_PUBLIC
;
8112 m
= gfc_match (" private");
8113 if (m
== MATCH_ERROR
)
8117 if (ba
->access
!= ACCESS_UNKNOWN
)
8119 gfc_error ("Duplicate access-specifier at %C");
8123 ba
->access
= ACCESS_PRIVATE
;
8127 /* If inside GENERIC, the following is not allowed. */
8132 m
= gfc_match (" nopass");
8133 if (m
== MATCH_ERROR
)
8139 gfc_error ("Binding attributes already specify passing,"
8140 " illegal NOPASS at %C");
8144 found_passing
= true;
8149 /* PASS possibly including argument. */
8150 m
= gfc_match (" pass");
8151 if (m
== MATCH_ERROR
)
8155 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8159 gfc_error ("Binding attributes already specify passing,"
8160 " illegal PASS at %C");
8164 m
= gfc_match (" ( %n )", arg
);
8165 if (m
== MATCH_ERROR
)
8168 ba
->pass_arg
= gfc_get_string (arg
);
8169 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8171 found_passing
= true;
8179 m
= gfc_match (" pointer");
8180 if (m
== MATCH_ERROR
)
8186 gfc_error ("Duplicate POINTER attribute at %C");
8196 /* NON_OVERRIDABLE flag. */
8197 m
= gfc_match (" non_overridable");
8198 if (m
== MATCH_ERROR
)
8202 if (ba
->non_overridable
)
8204 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8208 ba
->non_overridable
= 1;
8212 /* DEFERRED flag. */
8213 m
= gfc_match (" deferred");
8214 if (m
== MATCH_ERROR
)
8220 gfc_error ("Duplicate DEFERRED at %C");
8231 /* Nothing matching found. */
8233 gfc_error ("Expected access-specifier at %C");
8235 gfc_error ("Expected binding attribute at %C");
8238 while (gfc_match_char (',') == MATCH_YES
);
8240 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8241 if (ba
->non_overridable
&& ba
->deferred
)
8243 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8250 if (ba
->access
== ACCESS_UNKNOWN
)
8251 ba
->access
= gfc_typebound_default_access
;
8253 if (ppc
&& !seen_ptr
)
8255 gfc_error ("POINTER attribute is required for procedure pointer component"
8267 /* Match a PROCEDURE specific binding inside a derived type. */
8270 match_procedure_in_type (void)
8272 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8273 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8274 char* target
= NULL
, *ifc
= NULL
;
8275 gfc_typebound_proc tb
;
8284 /* Check current state. */
8285 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8286 block
= gfc_state_stack
->previous
->sym
;
8289 /* Try to match PROCEDURE(interface). */
8290 if (gfc_match (" (") == MATCH_YES
)
8292 m
= gfc_match_name (target_buf
);
8293 if (m
== MATCH_ERROR
)
8297 gfc_error ("Interface-name expected after %<(%> at %C");
8301 if (gfc_match (" )") != MATCH_YES
)
8303 gfc_error ("%<)%> expected at %C");
8310 /* Construct the data structure. */
8311 memset (&tb
, 0, sizeof (tb
));
8312 tb
.where
= gfc_current_locus
;
8314 /* Match binding attributes. */
8315 m
= match_binding_attributes (&tb
, false, false);
8316 if (m
== MATCH_ERROR
)
8318 seen_attrs
= (m
== MATCH_YES
);
8320 /* Check that attribute DEFERRED is given if an interface is specified. */
8321 if (tb
.deferred
&& !ifc
)
8323 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8326 if (ifc
&& !tb
.deferred
)
8328 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8332 /* Match the colons. */
8333 m
= gfc_match (" ::");
8334 if (m
== MATCH_ERROR
)
8336 seen_colons
= (m
== MATCH_YES
);
8337 if (seen_attrs
&& !seen_colons
)
8339 gfc_error ("Expected %<::%> after binding-attributes at %C");
8343 /* Match the binding names. */
8346 m
= gfc_match_name (name
);
8347 if (m
== MATCH_ERROR
)
8351 gfc_error ("Expected binding name at %C");
8355 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8358 /* Try to match the '=> target', if it's there. */
8360 m
= gfc_match (" =>");
8361 if (m
== MATCH_ERROR
)
8367 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8373 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8378 m
= gfc_match_name (target_buf
);
8379 if (m
== MATCH_ERROR
)
8383 gfc_error ("Expected binding target after %<=>%> at %C");
8386 target
= target_buf
;
8389 /* If no target was found, it has the same name as the binding. */
8393 /* Get the namespace to insert the symbols into. */
8394 ns
= block
->f2k_derived
;
8397 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8398 if (tb
.deferred
&& !block
->attr
.abstract
)
8400 gfc_error ("Type %qs containing DEFERRED binding at %C "
8401 "is not ABSTRACT", block
->name
);
8405 /* See if we already have a binding with this name in the symtree which
8406 would be an error. If a GENERIC already targeted this binding, it may
8407 be already there but then typebound is still NULL. */
8408 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8409 if (stree
&& stree
->n
.tb
)
8411 gfc_error ("There is already a procedure with binding name %qs for "
8412 "the derived type %qs at %C", name
, block
->name
);
8416 /* Insert it and set attributes. */
8420 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8423 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8425 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8428 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8430 if (gfc_match_eos () == MATCH_YES
)
8432 if (gfc_match_char (',') != MATCH_YES
)
8437 gfc_error ("Syntax error in PROCEDURE statement at %C");
8442 /* Match a GENERIC procedure binding inside a derived type. */
8445 gfc_match_generic (void)
8447 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8448 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8450 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8451 gfc_typebound_proc
* tb
;
8453 interface_type op_type
;
8454 gfc_intrinsic_op op
;
8457 /* Check current state. */
8458 if (gfc_current_state () == COMP_DERIVED
)
8460 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8463 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8465 block
= gfc_state_stack
->previous
->sym
;
8466 ns
= block
->f2k_derived
;
8467 gcc_assert (block
&& ns
);
8469 memset (&tbattr
, 0, sizeof (tbattr
));
8470 tbattr
.where
= gfc_current_locus
;
8472 /* See if we get an access-specifier. */
8473 m
= match_binding_attributes (&tbattr
, true, false);
8474 if (m
== MATCH_ERROR
)
8477 /* Now the colons, those are required. */
8478 if (gfc_match (" ::") != MATCH_YES
)
8480 gfc_error ("Expected %<::%> at %C");
8484 /* Match the binding name; depending on type (operator / generic) format
8485 it for future error messages into bind_name. */
8487 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8488 if (m
== MATCH_ERROR
)
8492 gfc_error ("Expected generic name or operator descriptor at %C");
8498 case INTERFACE_GENERIC
:
8499 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8502 case INTERFACE_USER_OP
:
8503 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8506 case INTERFACE_INTRINSIC_OP
:
8507 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8508 gfc_op2string (op
));
8511 case INTERFACE_NAMELESS
:
8512 gfc_error ("Malformed GENERIC statement at %C");
8520 /* Match the required =>. */
8521 if (gfc_match (" =>") != MATCH_YES
)
8523 gfc_error ("Expected %<=>%> at %C");
8527 /* Try to find existing GENERIC binding with this name / for this operator;
8528 if there is something, check that it is another GENERIC and then extend
8529 it rather than building a new node. Otherwise, create it and put it
8530 at the right position. */
8534 case INTERFACE_USER_OP
:
8535 case INTERFACE_GENERIC
:
8537 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8540 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8552 case INTERFACE_INTRINSIC_OP
:
8562 if (!tb
->is_generic
)
8564 gcc_assert (op_type
== INTERFACE_GENERIC
);
8565 gfc_error ("There's already a non-generic procedure with binding name"
8566 " %qs for the derived type %qs at %C",
8567 bind_name
, block
->name
);
8571 if (tb
->access
!= tbattr
.access
)
8573 gfc_error ("Binding at %C must have the same access as already"
8574 " defined binding %qs", bind_name
);
8580 tb
= gfc_get_typebound_proc (NULL
);
8581 tb
->where
= gfc_current_locus
;
8582 tb
->access
= tbattr
.access
;
8584 tb
->u
.generic
= NULL
;
8588 case INTERFACE_GENERIC
:
8589 case INTERFACE_USER_OP
:
8591 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8594 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8602 case INTERFACE_INTRINSIC_OP
:
8611 /* Now, match all following names as specific targets. */
8614 gfc_symtree
* target_st
;
8615 gfc_tbp_generic
* target
;
8617 m
= gfc_match_name (name
);
8618 if (m
== MATCH_ERROR
)
8622 gfc_error ("Expected specific binding name at %C");
8626 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8628 /* See if this is a duplicate specification. */
8629 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8630 if (target_st
== target
->specific_st
)
8632 gfc_error ("%qs already defined as specific binding for the"
8633 " generic %qs at %C", name
, bind_name
);
8637 target
= gfc_get_tbp_generic ();
8638 target
->specific_st
= target_st
;
8639 target
->specific
= NULL
;
8640 target
->next
= tb
->u
.generic
;
8641 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8642 || (op_type
== INTERFACE_INTRINSIC_OP
));
8643 tb
->u
.generic
= target
;
8645 while (gfc_match (" ,") == MATCH_YES
);
8647 /* Here should be the end. */
8648 if (gfc_match_eos () != MATCH_YES
)
8650 gfc_error ("Junk after GENERIC binding at %C");
8661 /* Match a FINAL declaration inside a derived type. */
8664 gfc_match_final_decl (void)
8666 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8669 gfc_namespace
* module_ns
;
8673 if (gfc_current_form
== FORM_FREE
)
8675 char c
= gfc_peek_ascii_char ();
8676 if (!gfc_is_whitespace (c
) && c
!= ':')
8680 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8682 if (gfc_current_form
== FORM_FIXED
)
8685 gfc_error ("FINAL declaration at %C must be inside a derived type "
8686 "CONTAINS section");
8690 block
= gfc_state_stack
->previous
->sym
;
8693 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8694 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8696 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8697 " specification part of a MODULE");
8701 module_ns
= gfc_current_ns
;
8702 gcc_assert (module_ns
);
8703 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8705 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8706 if (gfc_match (" ::") == MATCH_ERROR
)
8709 /* Match the sequence of procedure names. */
8716 if (first
&& gfc_match_eos () == MATCH_YES
)
8718 gfc_error ("Empty FINAL at %C");
8722 m
= gfc_match_name (name
);
8725 gfc_error ("Expected module procedure name at %C");
8728 else if (m
!= MATCH_YES
)
8731 if (gfc_match_eos () == MATCH_YES
)
8733 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8735 gfc_error ("Expected %<,%> at %C");
8739 if (gfc_get_symbol (name
, module_ns
, &sym
))
8741 gfc_error ("Unknown procedure name %qs at %C", name
);
8745 /* Mark the symbol as module procedure. */
8746 if (sym
->attr
.proc
!= PROC_MODULE
8747 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8750 /* Check if we already have this symbol in the list, this is an error. */
8751 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8752 if (f
->proc_sym
== sym
)
8754 gfc_error ("%qs at %C is already defined as FINAL procedure!",
8759 /* Add this symbol to the list of finalizers. */
8760 gcc_assert (block
->f2k_derived
);
8762 f
= XCNEW (gfc_finalizer
);
8764 f
->proc_tree
= NULL
;
8765 f
->where
= gfc_current_locus
;
8766 f
->next
= block
->f2k_derived
->finalizers
;
8767 block
->f2k_derived
->finalizers
= f
;
8777 const ext_attr_t ext_attr_list
[] = {
8778 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8779 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8780 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8781 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8782 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8783 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8784 { NULL
, EXT_ATTR_LAST
, NULL
}
8787 /* Match a !GCC$ ATTRIBUTES statement of the form:
8788 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8789 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8791 TODO: We should support all GCC attributes using the same syntax for
8792 the attribute list, i.e. the list in C
8793 __attributes(( attribute-list ))
8795 !GCC$ ATTRIBUTES attribute-list ::
8796 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8799 As there is absolutely no risk of confusion, we should never return
8802 gfc_match_gcc_attributes (void)
8804 symbol_attribute attr
;
8805 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8810 gfc_clear_attr (&attr
);
8815 if (gfc_match_name (name
) != MATCH_YES
)
8818 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8819 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8822 if (id
== EXT_ATTR_LAST
)
8824 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8828 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8831 gfc_gobble_whitespace ();
8832 ch
= gfc_next_ascii_char ();
8835 /* This is the successful exit condition for the loop. */
8836 if (gfc_next_ascii_char () == ':')
8846 if (gfc_match_eos () == MATCH_YES
)
8851 m
= gfc_match_name (name
);
8855 if (find_special (name
, &sym
, true))
8858 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8860 if (gfc_match_eos () == MATCH_YES
)
8863 if (gfc_match_char (',') != MATCH_YES
)
8870 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");