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"
32 #include "double-int.h"
39 #include "stringpool.h"
41 /* Macros to access allocate memory for gfc_data_variable,
42 gfc_data_value and gfc_data. */
43 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
44 #define gfc_get_data_value() XCNEW (gfc_data_value)
45 #define gfc_get_data() XCNEW (gfc_data)
48 static bool set_binding_label (const char **, const char *, int);
51 /* This flag is set if an old-style length selector is matched
52 during a type-declaration statement. */
54 static int old_char_selector
;
56 /* When variables acquire types and attributes from a declaration
57 statement, they get them from the following static variables. The
58 first part of a declaration sets these variables and the second
59 part copies these into symbol structures. */
61 static gfc_typespec current_ts
;
63 static symbol_attribute current_attr
;
64 static gfc_array_spec
*current_as
;
65 static int colon_seen
;
67 /* The current binding label (if any). */
68 static const char* curr_binding_label
;
69 /* Need to know how many identifiers are on the current data declaration
70 line in case we're given the BIND(C) attribute with a NAME= specifier. */
71 static int num_idents_on_line
;
72 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
73 can supply a name if the curr_binding_label is nil and NAME= was not. */
74 static int has_name_equals
= 0;
76 /* Initializer of the previous enumerator. */
78 static gfc_expr
*last_initializer
;
80 /* History of all the enumerators is maintained, so that
81 kind values of all the enumerators could be updated depending
82 upon the maximum initialized value. */
84 typedef struct enumerator_history
87 gfc_expr
*initializer
;
88 struct enumerator_history
*next
;
92 /* Header of enum history chain. */
94 static enumerator_history
*enum_history
= NULL
;
96 /* Pointer of enum history node containing largest initializer. */
98 static enumerator_history
*max_enum
= NULL
;
100 /* gfc_new_block points to the symbol of a newly matched block. */
102 gfc_symbol
*gfc_new_block
;
104 bool gfc_matching_function
;
107 /********************* DATA statement subroutines *********************/
109 static bool in_match_data
= false;
112 gfc_in_match_data (void)
114 return in_match_data
;
118 set_in_match_data (bool set_value
)
120 in_match_data
= set_value
;
123 /* Free a gfc_data_variable structure and everything beneath it. */
126 free_variable (gfc_data_variable
*p
)
128 gfc_data_variable
*q
;
133 gfc_free_expr (p
->expr
);
134 gfc_free_iterator (&p
->iter
, 0);
135 free_variable (p
->list
);
141 /* Free a gfc_data_value structure and everything beneath it. */
144 free_value (gfc_data_value
*p
)
151 mpz_clear (p
->repeat
);
152 gfc_free_expr (p
->expr
);
158 /* Free a list of gfc_data structures. */
161 gfc_free_data (gfc_data
*p
)
168 free_variable (p
->var
);
169 free_value (p
->value
);
175 /* Free all data in a namespace. */
178 gfc_free_data_all (gfc_namespace
*ns
)
190 /* Reject data parsed since the last restore point was marked. */
193 gfc_reject_data (gfc_namespace
*ns
)
197 while (ns
->data
&& ns
->data
!= ns
->old_data
)
205 static match
var_element (gfc_data_variable
*);
207 /* Match a list of variables terminated by an iterator and a right
211 var_list (gfc_data_variable
*parent
)
213 gfc_data_variable
*tail
, var
;
216 m
= var_element (&var
);
217 if (m
== MATCH_ERROR
)
222 tail
= gfc_get_data_variable ();
229 if (gfc_match_char (',') != MATCH_YES
)
232 m
= gfc_match_iterator (&parent
->iter
, 1);
235 if (m
== MATCH_ERROR
)
238 m
= var_element (&var
);
239 if (m
== MATCH_ERROR
)
244 tail
->next
= gfc_get_data_variable ();
250 if (gfc_match_char (')') != MATCH_YES
)
255 gfc_syntax_error (ST_DATA
);
260 /* Match a single element in a data variable list, which can be a
261 variable-iterator list. */
264 var_element (gfc_data_variable
*new_var
)
269 memset (new_var
, 0, sizeof (gfc_data_variable
));
271 if (gfc_match_char ('(') == MATCH_YES
)
272 return var_list (new_var
);
274 m
= gfc_match_variable (&new_var
->expr
, 0);
278 sym
= new_var
->expr
->symtree
->n
.sym
;
280 /* Symbol should already have an associated type. */
281 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
284 if (!sym
->attr
.function
&& gfc_current_ns
->parent
285 && gfc_current_ns
->parent
== sym
->ns
)
287 gfc_error ("Host associated variable %qs may not be in the DATA "
288 "statement at %C", sym
->name
);
292 if (gfc_current_state () != COMP_BLOCK_DATA
293 && sym
->attr
.in_common
294 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
295 "common block variable %qs in DATA statement at %C",
299 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
306 /* Match the top-level list of data variables. */
309 top_var_list (gfc_data
*d
)
311 gfc_data_variable var
, *tail
, *new_var
;
318 m
= var_element (&var
);
321 if (m
== MATCH_ERROR
)
324 new_var
= gfc_get_data_variable ();
330 tail
->next
= new_var
;
334 if (gfc_match_char ('/') == MATCH_YES
)
336 if (gfc_match_char (',') != MATCH_YES
)
343 gfc_syntax_error (ST_DATA
);
344 gfc_free_data_all (gfc_current_ns
);
350 match_data_constant (gfc_expr
**result
)
352 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
353 gfc_symbol
*sym
, *dt_sym
= NULL
;
358 m
= gfc_match_literal_constant (&expr
, 1);
365 if (m
== MATCH_ERROR
)
368 m
= gfc_match_null (result
);
372 old_loc
= gfc_current_locus
;
374 /* Should this be a structure component, try to match it
375 before matching a name. */
376 m
= gfc_match_rvalue (result
);
377 if (m
== MATCH_ERROR
)
380 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
382 if (!gfc_simplify_expr (*result
, 0))
386 else if (m
== MATCH_YES
)
387 gfc_free_expr (*result
);
389 gfc_current_locus
= old_loc
;
391 m
= gfc_match_name (name
);
395 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
398 if (sym
&& sym
->attr
.generic
)
399 dt_sym
= gfc_find_dt_in_generic (sym
);
402 || (sym
->attr
.flavor
!= FL_PARAMETER
403 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
405 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
409 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
410 return gfc_match_structure_constructor (dt_sym
, result
);
412 /* Check to see if the value is an initialization array expression. */
413 if (sym
->value
->expr_type
== EXPR_ARRAY
)
415 gfc_current_locus
= old_loc
;
417 m
= gfc_match_init_expr (result
);
418 if (m
== MATCH_ERROR
)
423 if (!gfc_simplify_expr (*result
, 0))
426 if ((*result
)->expr_type
== EXPR_CONSTANT
)
430 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
436 *result
= gfc_copy_expr (sym
->value
);
441 /* Match a list of values in a DATA statement. The leading '/' has
442 already been seen at this point. */
445 top_val_list (gfc_data
*data
)
447 gfc_data_value
*new_val
, *tail
;
455 m
= match_data_constant (&expr
);
458 if (m
== MATCH_ERROR
)
461 new_val
= gfc_get_data_value ();
462 mpz_init (new_val
->repeat
);
465 data
->value
= new_val
;
467 tail
->next
= new_val
;
471 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
474 mpz_set_ui (tail
->repeat
, 1);
478 mpz_set (tail
->repeat
, expr
->value
.integer
);
479 gfc_free_expr (expr
);
481 m
= match_data_constant (&tail
->expr
);
484 if (m
== MATCH_ERROR
)
488 if (gfc_match_char ('/') == MATCH_YES
)
490 if (gfc_match_char (',') == MATCH_NO
)
497 gfc_syntax_error (ST_DATA
);
498 gfc_free_data_all (gfc_current_ns
);
503 /* Matches an old style initialization. */
506 match_old_style_init (const char *name
)
513 /* Set up data structure to hold initializers. */
514 gfc_find_sym_tree (name
, NULL
, 0, &st
);
517 newdata
= gfc_get_data ();
518 newdata
->var
= gfc_get_data_variable ();
519 newdata
->var
->expr
= gfc_get_variable_expr (st
);
520 newdata
->where
= gfc_current_locus
;
522 /* Match initial value list. This also eats the terminal '/'. */
523 m
= top_val_list (newdata
);
532 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
536 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
538 /* Mark the variable as having appeared in a data statement. */
539 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
545 /* Chain in namespace list of DATA initializers. */
546 newdata
->next
= gfc_current_ns
->data
;
547 gfc_current_ns
->data
= newdata
;
553 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
554 we are matching a DATA statement and are therefore issuing an error
555 if we encounter something unexpected, if not, we're trying to match
556 an old-style initialization expression of the form INTEGER I /2/. */
559 gfc_match_data (void)
564 set_in_match_data (true);
568 new_data
= gfc_get_data ();
569 new_data
->where
= gfc_current_locus
;
571 m
= top_var_list (new_data
);
575 m
= top_val_list (new_data
);
579 new_data
->next
= gfc_current_ns
->data
;
580 gfc_current_ns
->data
= new_data
;
582 if (gfc_match_eos () == MATCH_YES
)
585 gfc_match_char (','); /* Optional comma */
588 set_in_match_data (false);
592 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
595 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
600 set_in_match_data (false);
601 gfc_free_data (new_data
);
606 /************************ Declaration statements *********************/
609 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
612 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
616 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
617 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
619 gfc_error ("The assumed-rank array at %C shall not have a codimension");
623 if (to
->rank
== 0 && from
->rank
> 0)
625 to
->rank
= from
->rank
;
626 to
->type
= from
->type
;
627 to
->cray_pointee
= from
->cray_pointee
;
628 to
->cp_was_assumed
= from
->cp_was_assumed
;
630 for (i
= 0; i
< to
->corank
; i
++)
632 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
633 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
635 for (i
= 0; i
< from
->rank
; i
++)
639 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
640 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
644 to
->lower
[i
] = from
->lower
[i
];
645 to
->upper
[i
] = from
->upper
[i
];
649 else if (to
->corank
== 0 && from
->corank
> 0)
651 to
->corank
= from
->corank
;
652 to
->cotype
= from
->cotype
;
654 for (i
= 0; i
< from
->corank
; i
++)
658 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
659 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
663 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
664 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
673 /* Match an intent specification. Since this can only happen after an
674 INTENT word, a legal intent-spec must follow. */
677 match_intent_spec (void)
680 if (gfc_match (" ( in out )") == MATCH_YES
)
682 if (gfc_match (" ( in )") == MATCH_YES
)
684 if (gfc_match (" ( out )") == MATCH_YES
)
687 gfc_error ("Bad INTENT specification at %C");
688 return INTENT_UNKNOWN
;
692 /* Matches a character length specification, which is either a
693 specification expression, '*', or ':'. */
696 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
703 if (gfc_match_char ('*') == MATCH_YES
)
706 if (gfc_match_char (':') == MATCH_YES
)
708 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
717 m
= gfc_match_expr (expr
);
720 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
723 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
725 if ((*expr
)->value
.function
.actual
726 && (*expr
)->value
.function
.actual
->expr
->symtree
)
729 e
= (*expr
)->value
.function
.actual
->expr
;
730 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
731 && e
->expr_type
== EXPR_VARIABLE
)
733 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
735 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
736 && e
->symtree
->n
.sym
->ts
.u
.cl
737 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
745 gfc_error ("Conflict in attributes of function argument at %C");
750 /* A character length is a '*' followed by a literal integer or a
751 char_len_param_value in parenthesis. */
754 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
760 m
= gfc_match_char ('*');
764 m
= gfc_match_small_literal_int (&length
, NULL
);
765 if (m
== MATCH_ERROR
)
770 if (obsolescent_check
771 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
773 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
777 if (gfc_match_char ('(') == MATCH_NO
)
780 m
= char_len_param_value (expr
, deferred
);
781 if (m
!= MATCH_YES
&& gfc_matching_function
)
787 if (m
== MATCH_ERROR
)
792 if (gfc_match_char (')') == MATCH_NO
)
794 gfc_free_expr (*expr
);
802 gfc_error ("Syntax error in character length specification at %C");
807 /* Special subroutine for finding a symbol. Check if the name is found
808 in the current name space. If not, and we're compiling a function or
809 subroutine and the parent compilation unit is an interface, then check
810 to see if the name we've been given is the name of the interface
811 (located in another namespace). */
814 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
820 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
823 *result
= st
? st
->n
.sym
: NULL
;
827 if (gfc_current_state () != COMP_SUBROUTINE
828 && gfc_current_state () != COMP_FUNCTION
)
831 s
= gfc_state_stack
->previous
;
835 if (s
->state
!= COMP_INTERFACE
)
838 goto end
; /* Nameless interface. */
840 if (strcmp (name
, s
->sym
->name
) == 0)
851 /* Special subroutine for getting a symbol node associated with a
852 procedure name, used in SUBROUTINE and FUNCTION statements. The
853 symbol is created in the parent using with symtree node in the
854 child unit pointing to the symbol. If the current namespace has no
855 parent, then the symbol is just created in the current unit. */
858 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
864 /* Module functions have to be left in their own namespace because
865 they have potentially (almost certainly!) already been referenced.
866 In this sense, they are rather like external functions. This is
867 fixed up in resolve.c(resolve_entries), where the symbol name-
868 space is set to point to the master function, so that the fake
869 result mechanism can work. */
870 if (module_fcn_entry
)
872 /* Present if entry is declared to be a module procedure. */
873 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
876 rc
= gfc_get_symbol (name
, NULL
, result
);
877 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
878 && (*result
)->ts
.type
== BT_UNKNOWN
879 && sym
->attr
.flavor
== FL_UNKNOWN
)
880 /* Pick up the typespec for the entry, if declared in the function
881 body. Note that this symbol is FL_UNKNOWN because it will
882 only have appeared in a type declaration. The local symtree
883 is set to point to the module symbol and a unique symtree
884 to the local version. This latter ensures a correct clearing
887 /* If the ENTRY proceeds its specification, we need to ensure
888 that this does not raise a "has no IMPLICIT type" error. */
889 if (sym
->ts
.type
== BT_UNKNOWN
)
890 sym
->attr
.untyped
= 1;
892 (*result
)->ts
= sym
->ts
;
894 /* Put the symbol in the procedure namespace so that, should
895 the ENTRY precede its specification, the specification
897 (*result
)->ns
= gfc_current_ns
;
899 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
901 st
= gfc_get_unique_symtree (gfc_current_ns
);
906 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
913 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
915 /* Trap another encompassed procedure with the same name. All
916 these conditions are necessary to avoid picking up an entry
917 whose name clashes with that of the encompassing procedure;
918 this is handled using gsymbols to register unique,globally
920 if (sym
->attr
.flavor
!= 0
921 && sym
->attr
.proc
!= 0
922 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
923 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
924 gfc_error_now_1 ("Procedure '%s' at %C is already defined at %L",
925 name
, &sym
->declared_at
);
927 /* Trap a procedure with a name the same as interface in the
928 encompassing scope. */
929 if (sym
->attr
.generic
!= 0
930 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
931 && !sym
->attr
.mod_proc
)
932 gfc_error_now_1 ("Name '%s' at %C is already defined"
933 " as a generic interface at %L",
934 name
, &sym
->declared_at
);
936 /* Trap declarations of attributes in encompassing scope. The
937 signature for this is that ts.kind is set. Legitimate
938 references only set ts.type. */
939 if (sym
->ts
.kind
!= 0
940 && !sym
->attr
.implicit_type
941 && sym
->attr
.proc
== 0
942 && gfc_current_ns
->parent
!= NULL
943 && sym
->attr
.access
== 0
944 && !module_fcn_entry
)
945 gfc_error_now_1 ("Procedure '%s' at %C has an explicit interface "
946 "and must not have attributes declared at %L",
947 name
, &sym
->declared_at
);
950 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
953 /* Module function entries will already have a symtree in
954 the current namespace but will need one at module level. */
955 if (module_fcn_entry
)
957 /* Present if entry is declared to be a module procedure. */
958 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
960 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
963 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
968 /* See if the procedure should be a module procedure. */
970 if (((sym
->ns
->proc_name
!= NULL
971 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
972 && sym
->attr
.proc
!= PROC_MODULE
)
973 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
974 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
981 /* Verify that the given symbol representing a parameter is C
982 interoperable, by checking to see if it was marked as such after
983 its declaration. If the given symbol is not interoperable, a
984 warning is reported, thus removing the need to return the status to
985 the calling function. The standard does not require the user use
986 one of the iso_c_binding named constants to declare an
987 interoperable parameter, but we can't be sure if the param is C
988 interop or not if the user doesn't. For example, integer(4) may be
989 legal Fortran, but doesn't have meaning in C. It may interop with
990 a number of the C types, which causes a problem because the
991 compiler can't know which one. This code is almost certainly not
992 portable, and the user will get what they deserve if the C type
993 across platforms isn't always interoperable with integer(4). If
994 the user had used something like integer(c_int) or integer(c_long),
995 the compiler could have automatically handled the varying sizes
999 gfc_verify_c_interop_param (gfc_symbol
*sym
)
1001 int is_c_interop
= 0;
1004 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1005 Don't repeat the checks here. */
1006 if (sym
->attr
.implicit_type
)
1009 /* For subroutines or functions that are passed to a BIND(C) procedure,
1010 they're interoperable if they're BIND(C) and their params are all
1012 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1014 if (sym
->attr
.is_bind_c
== 0)
1016 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1017 "attribute to be C interoperable", sym
->name
,
1018 &(sym
->declared_at
));
1023 if (sym
->attr
.is_c_interop
== 1)
1024 /* We've already checked this procedure; don't check it again. */
1027 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1032 /* See if we've stored a reference to a procedure that owns sym. */
1033 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1035 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1037 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1039 if (is_c_interop
!= 1)
1041 /* Make personalized messages to give better feedback. */
1042 if (sym
->ts
.type
== BT_DERIVED
)
1043 gfc_error ("Variable %qs at %L is a dummy argument to the "
1044 "BIND(C) procedure %qs but is not C interoperable "
1045 "because derived type %qs is not C interoperable",
1046 sym
->name
, &(sym
->declared_at
),
1047 sym
->ns
->proc_name
->name
,
1048 sym
->ts
.u
.derived
->name
);
1049 else if (sym
->ts
.type
== BT_CLASS
)
1050 gfc_error ("Variable %qs at %L is a dummy argument to the "
1051 "BIND(C) procedure %qs but is not C interoperable "
1052 "because it is polymorphic",
1053 sym
->name
, &(sym
->declared_at
),
1054 sym
->ns
->proc_name
->name
);
1055 else if (warn_c_binding_type
)
1056 gfc_warning (OPT_Wc_binding_type
,
1057 "Variable %qs at %L is a dummy argument of the "
1058 "BIND(C) procedure %qs but may not be C "
1060 sym
->name
, &(sym
->declared_at
),
1061 sym
->ns
->proc_name
->name
);
1064 /* Character strings are only C interoperable if they have a
1066 if (sym
->ts
.type
== BT_CHARACTER
)
1068 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1069 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1070 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1072 gfc_error ("Character argument %qs at %L "
1073 "must be length 1 because "
1074 "procedure %qs is BIND(C)",
1075 sym
->name
, &sym
->declared_at
,
1076 sym
->ns
->proc_name
->name
);
1081 /* We have to make sure that any param to a bind(c) routine does
1082 not have the allocatable, pointer, or optional attributes,
1083 according to J3/04-007, section 5.1. */
1084 if (sym
->attr
.allocatable
== 1
1085 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1086 "ALLOCATABLE attribute in procedure %qs "
1087 "with BIND(C)", sym
->name
,
1088 &(sym
->declared_at
),
1089 sym
->ns
->proc_name
->name
))
1092 if (sym
->attr
.pointer
== 1
1093 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1094 "POINTER attribute in procedure %qs "
1095 "with BIND(C)", sym
->name
,
1096 &(sym
->declared_at
),
1097 sym
->ns
->proc_name
->name
))
1100 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1102 gfc_error ("Scalar variable %qs at %L with POINTER or "
1103 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1104 " supported", sym
->name
, &(sym
->declared_at
),
1105 sym
->ns
->proc_name
->name
);
1109 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1111 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1112 "and the VALUE attribute because procedure %qs "
1113 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1114 sym
->ns
->proc_name
->name
);
1117 else if (sym
->attr
.optional
== 1
1118 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs "
1119 "at %L with OPTIONAL attribute in "
1120 "procedure %qs which is BIND(C)",
1121 sym
->name
, &(sym
->declared_at
),
1122 sym
->ns
->proc_name
->name
))
1125 /* Make sure that if it has the dimension attribute, that it is
1126 either assumed size or explicit shape. Deferred shape is already
1127 covered by the pointer/allocatable attribute. */
1128 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1129 && !gfc_notify_std_1 (GFC_STD_F2008_TS
, "Assumed-shape array '%s' "
1130 "at %L as dummy argument to the BIND(C) "
1131 "procedure '%s' at %L", sym
->name
,
1132 &(sym
->declared_at
),
1133 sym
->ns
->proc_name
->name
,
1134 &(sym
->ns
->proc_name
->declared_at
)))
1144 /* Function called by variable_decl() that adds a name to the symbol table. */
1147 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1148 gfc_array_spec
**as
, locus
*var_locus
)
1150 symbol_attribute attr
;
1153 if (gfc_get_symbol (name
, NULL
, &sym
))
1156 /* Start updating the symbol table. Add basic type attribute if present. */
1157 if (current_ts
.type
!= BT_UNKNOWN
1158 && (sym
->attr
.implicit_type
== 0
1159 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1160 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1163 if (sym
->ts
.type
== BT_CHARACTER
)
1166 sym
->ts
.deferred
= cl_deferred
;
1169 /* Add dimension attribute if present. */
1170 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1174 /* Add attribute to symbol. The copy is so that we can reset the
1175 dimension attribute. */
1176 attr
= current_attr
;
1178 attr
.codimension
= 0;
1180 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1183 /* Finish any work that may need to be done for the binding label,
1184 if it's a bind(c). The bind(c) attr is found before the symbol
1185 is made, and before the symbol name (for data decls), so the
1186 current_ts is holding the binding label, or nothing if the
1187 name= attr wasn't given. Therefore, test here if we're dealing
1188 with a bind(c) and make sure the binding label is set correctly. */
1189 if (sym
->attr
.is_bind_c
== 1)
1191 if (!sym
->binding_label
)
1193 /* Set the binding label and verify that if a NAME= was specified
1194 then only one identifier was in the entity-decl-list. */
1195 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1196 num_idents_on_line
))
1201 /* See if we know we're in a common block, and if it's a bind(c)
1202 common then we need to make sure we're an interoperable type. */
1203 if (sym
->attr
.in_common
== 1)
1205 /* Test the common block object. */
1206 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1207 && sym
->ts
.is_c_interop
!= 1)
1209 gfc_error_now ("Variable %qs in common block %qs at %C "
1210 "must be declared with a C interoperable "
1211 "kind since common block %qs is BIND(C)",
1212 sym
->name
, sym
->common_block
->name
,
1213 sym
->common_block
->name
);
1218 sym
->attr
.implied_index
= 0;
1220 if (sym
->ts
.type
== BT_CLASS
)
1221 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1227 /* Set character constant to the given length. The constant will be padded or
1228 truncated. If we're inside an array constructor without a typespec, we
1229 additionally check that all elements have the same length; check_len -1
1230 means no checking. */
1233 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1238 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1239 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1241 slen
= expr
->value
.character
.length
;
1244 s
= gfc_get_wide_string (len
+ 1);
1245 memcpy (s
, expr
->value
.character
.string
,
1246 MIN (len
, slen
) * sizeof (gfc_char_t
));
1248 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1250 if (warn_character_truncation
&& slen
> len
)
1251 gfc_warning_now (OPT_Wcharacter_truncation
,
1252 "CHARACTER expression at %L is being truncated "
1253 "(%d/%d)", &expr
->where
, slen
, len
);
1255 /* Apply the standard by 'hand' otherwise it gets cleared for
1257 if (check_len
!= -1 && slen
!= check_len
1258 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1259 gfc_error_now ("The CHARACTER elements of the array constructor "
1260 "at %L must have the same length (%d/%d)",
1261 &expr
->where
, slen
, check_len
);
1264 free (expr
->value
.character
.string
);
1265 expr
->value
.character
.string
= s
;
1266 expr
->value
.character
.length
= len
;
1271 /* Function to create and update the enumerator history
1272 using the information passed as arguments.
1273 Pointer "max_enum" is also updated, to point to
1274 enum history node containing largest initializer.
1276 SYM points to the symbol node of enumerator.
1277 INIT points to its enumerator value. */
1280 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1282 enumerator_history
*new_enum_history
;
1283 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1285 new_enum_history
= XCNEW (enumerator_history
);
1287 new_enum_history
->sym
= sym
;
1288 new_enum_history
->initializer
= init
;
1289 new_enum_history
->next
= NULL
;
1291 if (enum_history
== NULL
)
1293 enum_history
= new_enum_history
;
1294 max_enum
= enum_history
;
1298 new_enum_history
->next
= enum_history
;
1299 enum_history
= new_enum_history
;
1301 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1302 new_enum_history
->initializer
->value
.integer
) < 0)
1303 max_enum
= new_enum_history
;
1308 /* Function to free enum kind history. */
1311 gfc_free_enum_history (void)
1313 enumerator_history
*current
= enum_history
;
1314 enumerator_history
*next
;
1316 while (current
!= NULL
)
1318 next
= current
->next
;
1323 enum_history
= NULL
;
1327 /* Function called by variable_decl() that adds an initialization
1328 expression to a symbol. */
1331 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1333 symbol_attribute attr
;
1338 if (find_special (name
, &sym
, false))
1343 /* If this symbol is confirming an implicit parameter type,
1344 then an initialization expression is not allowed. */
1345 if (attr
.flavor
== FL_PARAMETER
1346 && sym
->value
!= NULL
1349 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1356 /* An initializer is required for PARAMETER declarations. */
1357 if (attr
.flavor
== FL_PARAMETER
)
1359 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1365 /* If a variable appears in a DATA block, it cannot have an
1369 gfc_error ("Variable %qs at %C with an initializer already "
1370 "appears in a DATA statement", sym
->name
);
1374 /* Check if the assignment can happen. This has to be put off
1375 until later for derived type variables and procedure pointers. */
1376 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1377 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1378 && !sym
->attr
.proc_pointer
1379 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1382 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1383 && init
->ts
.type
== BT_CHARACTER
)
1385 /* Update symbol character length according initializer. */
1386 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1389 if (sym
->ts
.u
.cl
->length
== NULL
)
1392 /* If there are multiple CHARACTER variables declared on the
1393 same line, we don't want them to share the same length. */
1394 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1396 if (sym
->attr
.flavor
== FL_PARAMETER
)
1398 if (init
->expr_type
== EXPR_CONSTANT
)
1400 clen
= init
->value
.character
.length
;
1401 sym
->ts
.u
.cl
->length
1402 = gfc_get_int_expr (gfc_default_integer_kind
,
1405 else if (init
->expr_type
== EXPR_ARRAY
)
1408 c
= gfc_constructor_first (init
->value
.constructor
);
1409 clen
= c
->expr
->value
.character
.length
;
1410 sym
->ts
.u
.cl
->length
1411 = gfc_get_int_expr (gfc_default_integer_kind
,
1414 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1415 sym
->ts
.u
.cl
->length
=
1416 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1419 /* Update initializer character length according symbol. */
1420 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1422 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1424 if (init
->expr_type
== EXPR_CONSTANT
)
1425 gfc_set_constant_character_len (len
, init
, -1);
1426 else if (init
->expr_type
== EXPR_ARRAY
)
1430 /* Build a new charlen to prevent simplification from
1431 deleting the length before it is resolved. */
1432 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1433 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1435 for (c
= gfc_constructor_first (init
->value
.constructor
);
1436 c
; c
= gfc_constructor_next (c
))
1437 gfc_set_constant_character_len (len
, c
->expr
, -1);
1442 /* If sym is implied-shape, set its upper bounds from init. */
1443 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1444 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1448 if (init
->rank
== 0)
1450 gfc_error ("Can't initialize implied-shape array at %L"
1451 " with scalar", &sym
->declared_at
);
1454 gcc_assert (sym
->as
->rank
== init
->rank
);
1456 /* Shape should be present, we get an initialization expression. */
1457 gcc_assert (init
->shape
);
1459 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1465 lower
= sym
->as
->lower
[dim
];
1466 if (lower
->expr_type
!= EXPR_CONSTANT
)
1468 gfc_error ("Non-constant lower bound in implied-shape"
1469 " declaration at %L", &lower
->where
);
1473 /* All dimensions must be without upper bound. */
1474 gcc_assert (!sym
->as
->upper
[dim
]);
1477 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1478 mpz_add (e
->value
.integer
,
1479 lower
->value
.integer
, init
->shape
[dim
]);
1480 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1481 sym
->as
->upper
[dim
] = e
;
1484 sym
->as
->type
= AS_EXPLICIT
;
1487 /* Need to check if the expression we initialized this
1488 to was one of the iso_c_binding named constants. If so,
1489 and we're a parameter (constant), let it be iso_c.
1491 integer(c_int), parameter :: my_int = c_int
1492 integer(my_int) :: my_int_2
1493 If we mark my_int as iso_c (since we can see it's value
1494 is equal to one of the named constants), then my_int_2
1495 will be considered C interoperable. */
1496 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1498 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1499 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1500 /* attr bits needed for module files. */
1501 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1502 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1503 if (init
->ts
.is_iso_c
)
1504 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1507 /* Add initializer. Make sure we keep the ranks sane. */
1508 if (sym
->attr
.dimension
&& init
->rank
== 0)
1513 if (sym
->attr
.flavor
== FL_PARAMETER
1514 && init
->expr_type
== EXPR_CONSTANT
1515 && spec_size (sym
->as
, &size
)
1516 && mpz_cmp_si (size
, 0) > 0)
1518 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1520 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1521 gfc_constructor_append_expr (&array
->value
.constructor
,
1524 : gfc_copy_expr (init
),
1527 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1528 for (n
= 0; n
< sym
->as
->rank
; n
++)
1529 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1534 init
->rank
= sym
->as
->rank
;
1538 if (sym
->attr
.save
== SAVE_NONE
)
1539 sym
->attr
.save
= SAVE_IMPLICIT
;
1547 /* Function called by variable_decl() that adds a name to a structure
1551 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1552 gfc_array_spec
**as
)
1557 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1558 constructing, it must have the pointer attribute. */
1559 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1560 && current_ts
.u
.derived
== gfc_current_block ()
1561 && current_attr
.pointer
== 0)
1563 gfc_error ("Component at %C must have the POINTER attribute");
1567 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1569 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1571 gfc_error ("Array component of structure at %C must have explicit "
1572 "or deferred shape");
1577 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1581 if (c
->ts
.type
== BT_CHARACTER
)
1583 c
->attr
= current_attr
;
1585 c
->initializer
= *init
;
1592 c
->attr
.codimension
= 1;
1594 c
->attr
.dimension
= 1;
1598 /* Should this ever get more complicated, combine with similar section
1599 in add_init_expr_to_sym into a separate function. */
1600 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1602 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1606 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1607 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1608 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1610 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1612 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1613 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1614 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1615 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1617 gfc_constructor
*ctor
;
1618 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1623 bool has_ts
= (c
->initializer
->ts
.u
.cl
1624 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1626 /* Remember the length of the first element for checking
1627 that all elements *in the constructor* have the same
1628 length. This need not be the length of the LHS! */
1629 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1630 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1631 first_len
= ctor
->expr
->value
.character
.length
;
1633 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1634 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1636 gfc_set_constant_character_len (len
, ctor
->expr
,
1637 has_ts
? -1 : first_len
);
1638 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1644 /* Check array components. */
1645 if (!c
->attr
.dimension
)
1648 if (c
->attr
.pointer
)
1650 if (c
->as
->type
!= AS_DEFERRED
)
1652 gfc_error ("Pointer array component of structure at %C must have a "
1657 else if (c
->attr
.allocatable
)
1659 if (c
->as
->type
!= AS_DEFERRED
)
1661 gfc_error ("Allocatable component of structure at %C must have a "
1668 if (c
->as
->type
!= AS_EXPLICIT
)
1670 gfc_error ("Array component of structure at %C must have an "
1677 if (c
->ts
.type
== BT_CLASS
)
1679 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1689 /* Match a 'NULL()', and possibly take care of some side effects. */
1692 gfc_match_null (gfc_expr
**result
)
1695 match m
, m2
= MATCH_NO
;
1697 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1703 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1705 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1708 old_loc
= gfc_current_locus
;
1709 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1712 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1716 gfc_current_locus
= old_loc
;
1721 /* The NULL symbol now has to be/become an intrinsic function. */
1722 if (gfc_get_symbol ("null", NULL
, &sym
))
1724 gfc_error ("NULL() initialization at %C is ambiguous");
1728 gfc_intrinsic_symbol (sym
);
1730 if (sym
->attr
.proc
!= PROC_INTRINSIC
1731 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1732 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1733 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1736 *result
= gfc_get_null_expr (&gfc_current_locus
);
1738 /* Invalid per F2008, C512. */
1739 if (m2
== MATCH_YES
)
1741 gfc_error ("NULL() initialization at %C may not have MOLD");
1749 /* Match the initialization expr for a data pointer or procedure pointer. */
1752 match_pointer_init (gfc_expr
**init
, int procptr
)
1756 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1758 gfc_error ("Initialization of pointer at %C is not allowed in "
1759 "a PURE procedure");
1762 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
1764 /* Match NULL() initialization. */
1765 m
= gfc_match_null (init
);
1769 /* Match non-NULL initialization. */
1770 gfc_matching_ptr_assignment
= !procptr
;
1771 gfc_matching_procptr_assignment
= procptr
;
1772 m
= gfc_match_rvalue (init
);
1773 gfc_matching_ptr_assignment
= 0;
1774 gfc_matching_procptr_assignment
= 0;
1775 if (m
== MATCH_ERROR
)
1777 else if (m
== MATCH_NO
)
1779 gfc_error ("Error in pointer initialization at %C");
1784 gfc_resolve_expr (*init
);
1786 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1787 "initialization at %C"))
1795 check_function_name (char *name
)
1797 /* In functions that have a RESULT variable defined, the function name always
1798 refers to function calls. Therefore, the name is not allowed to appear in
1799 specification statements. When checking this, be careful about
1800 'hidden' procedure pointer results ('ppr@'). */
1802 if (gfc_current_state () == COMP_FUNCTION
)
1804 gfc_symbol
*block
= gfc_current_block ();
1805 if (block
&& block
->result
&& block
->result
!= block
1806 && strcmp (block
->result
->name
, "ppr@") != 0
1807 && strcmp (block
->name
, name
) == 0)
1809 gfc_error ("Function name %qs not allowed at %C", name
);
1818 /* Match a variable name with an optional initializer. When this
1819 subroutine is called, a variable is expected to be parsed next.
1820 Depending on what is happening at the moment, updates either the
1821 symbol table or the current interface. */
1824 variable_decl (int elem
)
1826 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1827 gfc_expr
*initializer
, *char_len
;
1829 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1841 /* When we get here, we've just matched a list of attributes and
1842 maybe a type and a double colon. The next thing we expect to see
1843 is the name of the symbol. */
1844 m
= gfc_match_name (name
);
1848 var_locus
= gfc_current_locus
;
1850 /* Now we could see the optional array spec. or character length. */
1851 m
= gfc_match_array_spec (&as
, true, true);
1852 if (m
== MATCH_ERROR
)
1856 as
= gfc_copy_array_spec (current_as
);
1858 && !merge_array_spec (current_as
, as
, true))
1864 if (flag_cray_pointer
)
1865 cp_as
= gfc_copy_array_spec (as
);
1867 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1868 determine (and check) whether it can be implied-shape. If it
1869 was parsed as assumed-size, change it because PARAMETERs can not
1873 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1876 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1881 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1882 && current_attr
.flavor
== FL_PARAMETER
)
1883 as
->type
= AS_IMPLIED_SHAPE
;
1885 if (as
->type
== AS_IMPLIED_SHAPE
1886 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1896 cl_deferred
= false;
1898 if (current_ts
.type
== BT_CHARACTER
)
1900 switch (match_char_length (&char_len
, &cl_deferred
, false))
1903 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1905 cl
->length
= char_len
;
1908 /* Non-constant lengths need to be copied after the first
1909 element. Also copy assumed lengths. */
1912 && (current_ts
.u
.cl
->length
== NULL
1913 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1915 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1916 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1919 cl
= current_ts
.u
.cl
;
1921 cl_deferred
= current_ts
.deferred
;
1930 /* If this symbol has already shown up in a Cray Pointer declaration,
1931 and this is not a component declaration,
1932 then we want to set the type & bail out. */
1933 if (flag_cray_pointer
&& gfc_current_state () != COMP_DERIVED
)
1935 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1936 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1938 sym
->ts
.type
= current_ts
.type
;
1939 sym
->ts
.kind
= current_ts
.kind
;
1941 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1942 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1943 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1946 /* Check to see if we have an array specification. */
1949 if (sym
->as
!= NULL
)
1951 gfc_error ("Duplicate array spec for Cray pointee at %C");
1952 gfc_free_array_spec (cp_as
);
1958 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1959 gfc_internal_error ("Couldn't set pointee array spec.");
1961 /* Fix the array spec. */
1962 m
= gfc_mod_pointee_as (sym
->as
);
1963 if (m
== MATCH_ERROR
)
1971 gfc_free_array_spec (cp_as
);
1975 /* Procedure pointer as function result. */
1976 if (gfc_current_state () == COMP_FUNCTION
1977 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1978 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1979 strcpy (name
, "ppr@");
1981 if (gfc_current_state () == COMP_FUNCTION
1982 && strcmp (name
, gfc_current_block ()->name
) == 0
1983 && gfc_current_block ()->result
1984 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1985 strcpy (name
, "ppr@");
1987 /* OK, we've successfully matched the declaration. Now put the
1988 symbol in the current namespace, because it might be used in the
1989 optional initialization expression for this symbol, e.g. this is
1992 integer, parameter :: i = huge(i)
1994 This is only true for parameters or variables of a basic type.
1995 For components of derived types, it is not true, so we don't
1996 create a symbol for those yet. If we fail to create the symbol,
1998 if (gfc_current_state () != COMP_DERIVED
1999 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
2005 if (!check_function_name (name
))
2011 /* We allow old-style initializations of the form
2012 integer i /2/, j(4) /3*3, 1/
2013 (if no colon has been seen). These are different from data
2014 statements in that initializers are only allowed to apply to the
2015 variable immediately preceding, i.e.
2017 is not allowed. Therefore we have to do some work manually, that
2018 could otherwise be left to the matchers for DATA statements. */
2020 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2022 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2023 "initialization at %C"))
2025 else if (gfc_current_state () == COMP_DERIVED
)
2027 gfc_error ("Invalid old style initialization for derived type "
2033 return match_old_style_init (name
);
2036 /* The double colon must be present in order to have initializers.
2037 Otherwise the statement is ambiguous with an assignment statement. */
2040 if (gfc_match (" =>") == MATCH_YES
)
2042 if (!current_attr
.pointer
)
2044 gfc_error ("Initialization at %C isn't for a pointer variable");
2049 m
= match_pointer_init (&initializer
, 0);
2053 else if (gfc_match_char ('=') == MATCH_YES
)
2055 if (current_attr
.pointer
)
2057 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2063 m
= gfc_match_init_expr (&initializer
);
2066 gfc_error ("Expected an initialization expression at %C");
2070 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2071 && gfc_state_stack
->state
!= COMP_DERIVED
)
2073 gfc_error ("Initialization of variable at %C is not allowed in "
2074 "a PURE procedure");
2078 if (current_attr
.flavor
!= FL_PARAMETER
2079 && gfc_state_stack
->state
!= COMP_DERIVED
)
2080 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2087 if (initializer
!= NULL
&& current_attr
.allocatable
2088 && gfc_current_state () == COMP_DERIVED
)
2090 gfc_error ("Initialization of allocatable component at %C is not "
2096 /* Add the initializer. Note that it is fine if initializer is
2097 NULL here, because we sometimes also need to check if a
2098 declaration *must* have an initialization expression. */
2099 if (gfc_current_state () != COMP_DERIVED
)
2100 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2103 if (current_ts
.type
== BT_DERIVED
2104 && !current_attr
.pointer
&& !initializer
)
2105 initializer
= gfc_default_initializer (¤t_ts
);
2106 t
= build_struct (name
, cl
, &initializer
, &as
);
2109 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2112 /* Free stuff up and return. */
2113 gfc_free_expr (initializer
);
2114 gfc_free_array_spec (as
);
2120 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2121 This assumes that the byte size is equal to the kind number for
2122 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2125 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2130 if (gfc_match_char ('*') != MATCH_YES
)
2133 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2137 original_kind
= ts
->kind
;
2139 /* Massage the kind numbers for complex types. */
2140 if (ts
->type
== BT_COMPLEX
)
2144 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2145 gfc_basic_typename (ts
->type
), original_kind
);
2152 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2155 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2159 if (flag_real4_kind
== 8)
2161 if (flag_real4_kind
== 10)
2163 if (flag_real4_kind
== 16)
2169 if (flag_real8_kind
== 4)
2171 if (flag_real8_kind
== 10)
2173 if (flag_real8_kind
== 16)
2178 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2180 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2181 gfc_basic_typename (ts
->type
), original_kind
);
2185 if (!gfc_notify_std (GFC_STD_GNU
,
2186 "Nonstandard type declaration %s*%d at %C",
2187 gfc_basic_typename(ts
->type
), original_kind
))
2194 /* Match a kind specification. Since kinds are generally optional, we
2195 usually return MATCH_NO if something goes wrong. If a "kind="
2196 string is found, then we know we have an error. */
2199 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2211 where
= loc
= gfc_current_locus
;
2216 if (gfc_match_char ('(') == MATCH_NO
)
2219 /* Also gobbles optional text. */
2220 if (gfc_match (" kind = ") == MATCH_YES
)
2223 loc
= gfc_current_locus
;
2226 n
= gfc_match_init_expr (&e
);
2230 if (gfc_matching_function
)
2232 /* The function kind expression might include use associated or
2233 imported parameters and try again after the specification
2235 if (gfc_match_char (')') != MATCH_YES
)
2237 gfc_error ("Missing right parenthesis at %C");
2243 gfc_undo_symbols ();
2248 /* ....or else, the match is real. */
2250 gfc_error ("Expected initialization expression at %C");
2258 gfc_error ("Expected scalar initialization expression at %C");
2263 msg
= gfc_extract_int (e
, &ts
->kind
);
2272 /* Before throwing away the expression, let's see if we had a
2273 C interoperable kind (and store the fact). */
2274 if (e
->ts
.is_c_interop
== 1)
2276 /* Mark this as C interoperable if being declared with one
2277 of the named constants from iso_c_binding. */
2278 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2279 ts
->f90_type
= e
->ts
.f90_type
;
2285 /* Ignore errors to this point, if we've gotten here. This means
2286 we ignore the m=MATCH_ERROR from above. */
2287 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2289 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2290 gfc_basic_typename (ts
->type
));
2291 gfc_current_locus
= where
;
2295 /* Warn if, e.g., c_int is used for a REAL variable, but not
2296 if, e.g., c_double is used for COMPLEX as the standard
2297 explicitly says that the kind type parameter for complex and real
2298 variable is the same, i.e. c_float == c_float_complex. */
2299 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2300 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2301 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2302 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2303 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2304 gfc_basic_typename (ts
->type
));
2306 gfc_gobble_whitespace ();
2307 if ((c
= gfc_next_ascii_char ()) != ')'
2308 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2310 if (ts
->type
== BT_CHARACTER
)
2311 gfc_error ("Missing right parenthesis or comma at %C");
2313 gfc_error ("Missing right parenthesis at %C");
2317 /* All tests passed. */
2320 if(m
== MATCH_ERROR
)
2321 gfc_current_locus
= where
;
2323 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2326 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2330 if (flag_real4_kind
== 8)
2332 if (flag_real4_kind
== 10)
2334 if (flag_real4_kind
== 16)
2340 if (flag_real8_kind
== 4)
2342 if (flag_real8_kind
== 10)
2344 if (flag_real8_kind
== 16)
2349 /* Return what we know from the test(s). */
2354 gfc_current_locus
= where
;
2360 match_char_kind (int * kind
, int * is_iso_c
)
2369 where
= gfc_current_locus
;
2371 n
= gfc_match_init_expr (&e
);
2373 if (n
!= MATCH_YES
&& gfc_matching_function
)
2375 /* The expression might include use-associated or imported
2376 parameters and try again after the specification
2379 gfc_undo_symbols ();
2384 gfc_error ("Expected initialization expression at %C");
2390 gfc_error ("Expected scalar initialization expression at %C");
2395 msg
= gfc_extract_int (e
, kind
);
2396 *is_iso_c
= e
->ts
.is_iso_c
;
2406 /* Ignore errors to this point, if we've gotten here. This means
2407 we ignore the m=MATCH_ERROR from above. */
2408 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2410 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2414 /* All tests passed. */
2417 if (m
== MATCH_ERROR
)
2418 gfc_current_locus
= where
;
2420 /* Return what we know from the test(s). */
2425 gfc_current_locus
= where
;
2430 /* Match the various kind/length specifications in a CHARACTER
2431 declaration. We don't return MATCH_NO. */
2434 gfc_match_char_spec (gfc_typespec
*ts
)
2436 int kind
, seen_length
, is_iso_c
;
2448 /* Try the old-style specification first. */
2449 old_char_selector
= 0;
2451 m
= match_char_length (&len
, &deferred
, true);
2455 old_char_selector
= 1;
2460 m
= gfc_match_char ('(');
2463 m
= MATCH_YES
; /* Character without length is a single char. */
2467 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2468 if (gfc_match (" kind =") == MATCH_YES
)
2470 m
= match_char_kind (&kind
, &is_iso_c
);
2472 if (m
== MATCH_ERROR
)
2477 if (gfc_match (" , len =") == MATCH_NO
)
2480 m
= char_len_param_value (&len
, &deferred
);
2483 if (m
== MATCH_ERROR
)
2490 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2491 if (gfc_match (" len =") == MATCH_YES
)
2493 m
= char_len_param_value (&len
, &deferred
);
2496 if (m
== MATCH_ERROR
)
2500 if (gfc_match_char (')') == MATCH_YES
)
2503 if (gfc_match (" , kind =") != MATCH_YES
)
2506 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2512 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2513 m
= char_len_param_value (&len
, &deferred
);
2516 if (m
== MATCH_ERROR
)
2520 m
= gfc_match_char (')');
2524 if (gfc_match_char (',') != MATCH_YES
)
2527 gfc_match (" kind ="); /* Gobble optional text. */
2529 m
= match_char_kind (&kind
, &is_iso_c
);
2530 if (m
== MATCH_ERROR
)
2536 /* Require a right-paren at this point. */
2537 m
= gfc_match_char (')');
2542 gfc_error ("Syntax error in CHARACTER declaration at %C");
2544 gfc_free_expr (len
);
2548 /* Deal with character functions after USE and IMPORT statements. */
2549 if (gfc_matching_function
)
2551 gfc_free_expr (len
);
2552 gfc_undo_symbols ();
2558 gfc_free_expr (len
);
2562 /* Do some final massaging of the length values. */
2563 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2565 if (seen_length
== 0)
2566 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2571 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2572 ts
->deferred
= deferred
;
2574 /* We have to know if it was a C interoperable kind so we can
2575 do accurate type checking of bind(c) procs, etc. */
2577 /* Mark this as C interoperable if being declared with one
2578 of the named constants from iso_c_binding. */
2579 ts
->is_c_interop
= is_iso_c
;
2580 else if (len
!= NULL
)
2581 /* Here, we might have parsed something such as: character(c_char)
2582 In this case, the parsing code above grabs the c_char when
2583 looking for the length (line 1690, roughly). it's the last
2584 testcase for parsing the kind params of a character variable.
2585 However, it's not actually the length. this seems like it
2587 To see if the user used a C interop kind, test the expr
2588 of the so called length, and see if it's C interoperable. */
2589 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2595 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2596 structure to the matched specification. This is necessary for FUNCTION and
2597 IMPLICIT statements.
2599 If implicit_flag is nonzero, then we don't check for the optional
2600 kind specification. Not doing so is needed for matching an IMPLICIT
2601 statement correctly. */
2604 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2606 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2607 gfc_symbol
*sym
, *dt_sym
;
2610 bool seen_deferred_kind
, matched_type
;
2611 const char *dt_name
;
2613 /* A belt and braces check that the typespec is correctly being treated
2614 as a deferred characteristic association. */
2615 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2616 && (gfc_current_block ()->result
->ts
.kind
== -1)
2617 && (ts
->kind
== -1);
2619 if (seen_deferred_kind
)
2622 /* Clear the current binding label, in case one is given. */
2623 curr_binding_label
= NULL
;
2625 if (gfc_match (" byte") == MATCH_YES
)
2627 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2630 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2632 gfc_error ("BYTE type used at %C "
2633 "is not available on the target machine");
2637 ts
->type
= BT_INTEGER
;
2643 m
= gfc_match (" type (");
2644 matched_type
= (m
== MATCH_YES
);
2647 gfc_gobble_whitespace ();
2648 if (gfc_peek_ascii_char () == '*')
2650 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2652 if (gfc_current_state () == COMP_DERIVED
)
2654 gfc_error ("Assumed type at %C is not allowed for components");
2657 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2660 ts
->type
= BT_ASSUMED
;
2664 m
= gfc_match ("%n", name
);
2665 matched_type
= (m
== MATCH_YES
);
2668 if ((matched_type
&& strcmp ("integer", name
) == 0)
2669 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2671 ts
->type
= BT_INTEGER
;
2672 ts
->kind
= gfc_default_integer_kind
;
2676 if ((matched_type
&& strcmp ("character", name
) == 0)
2677 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2680 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2681 "intrinsic-type-spec at %C"))
2684 ts
->type
= BT_CHARACTER
;
2685 if (implicit_flag
== 0)
2686 m
= gfc_match_char_spec (ts
);
2690 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2696 if ((matched_type
&& strcmp ("real", name
) == 0)
2697 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2700 ts
->kind
= gfc_default_real_kind
;
2705 && (strcmp ("doubleprecision", name
) == 0
2706 || (strcmp ("double", name
) == 0
2707 && gfc_match (" precision") == MATCH_YES
)))
2708 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2711 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2712 "intrinsic-type-spec at %C"))
2714 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2718 ts
->kind
= gfc_default_double_kind
;
2722 if ((matched_type
&& strcmp ("complex", name
) == 0)
2723 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2725 ts
->type
= BT_COMPLEX
;
2726 ts
->kind
= gfc_default_complex_kind
;
2731 && (strcmp ("doublecomplex", name
) == 0
2732 || (strcmp ("double", name
) == 0
2733 && gfc_match (" complex") == MATCH_YES
)))
2734 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2736 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2740 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2741 "intrinsic-type-spec at %C"))
2744 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2747 ts
->type
= BT_COMPLEX
;
2748 ts
->kind
= gfc_default_double_kind
;
2752 if ((matched_type
&& strcmp ("logical", name
) == 0)
2753 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2755 ts
->type
= BT_LOGICAL
;
2756 ts
->kind
= gfc_default_logical_kind
;
2761 m
= gfc_match_char (')');
2764 ts
->type
= BT_DERIVED
;
2767 /* Match CLASS declarations. */
2768 m
= gfc_match (" class ( * )");
2769 if (m
== MATCH_ERROR
)
2771 else if (m
== MATCH_YES
)
2775 ts
->type
= BT_CLASS
;
2776 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2779 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2780 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2782 gfc_set_sym_referenced (upe
);
2784 upe
->ts
.type
= BT_VOID
;
2785 upe
->attr
.unlimited_polymorphic
= 1;
2786 /* This is essential to force the construction of
2787 unlimited polymorphic component class containers. */
2788 upe
->attr
.zero_comp
= 1;
2789 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2790 &gfc_current_locus
))
2795 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2797 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2801 ts
->u
.derived
= upe
;
2805 m
= gfc_match (" class ( %n )", name
);
2808 ts
->type
= BT_CLASS
;
2810 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2814 /* Defer association of the derived type until the end of the
2815 specification block. However, if the derived type can be
2816 found, add it to the typespec. */
2817 if (gfc_matching_function
)
2819 ts
->u
.derived
= NULL
;
2820 if (gfc_current_state () != COMP_INTERFACE
2821 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2823 sym
= gfc_find_dt_in_generic (sym
);
2824 ts
->u
.derived
= sym
;
2829 /* Search for the name but allow the components to be defined later. If
2830 type = -1, this typespec has been seen in a function declaration but
2831 the type could not be accessed at that point. The actual derived type is
2832 stored in a symtree with the first letter of the name capitalized; the
2833 symtree with the all lower-case name contains the associated
2834 generic function. */
2835 dt_name
= gfc_get_string ("%c%s",
2836 (char) TOUPPER ((unsigned char) name
[0]),
2837 (const char*)&name
[1]);
2842 gfc_get_ha_symbol (name
, &sym
);
2843 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2845 gfc_error ("Type name %qs at %C is ambiguous", name
);
2848 if (sym
->generic
&& !dt_sym
)
2849 dt_sym
= gfc_find_dt_in_generic (sym
);
2851 else if (ts
->kind
== -1)
2853 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2854 || gfc_current_ns
->has_import_set
;
2855 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2856 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2858 gfc_error ("Type name %qs at %C is ambiguous", name
);
2861 if (sym
&& sym
->generic
&& !dt_sym
)
2862 dt_sym
= gfc_find_dt_in_generic (sym
);
2869 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2870 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2871 || sym
->attr
.subroutine
)
2873 gfc_error_1 ("Type name '%s' at %C conflicts with previously declared "
2874 "entity at %L, which has the same name", name
,
2879 gfc_set_sym_referenced (sym
);
2880 if (!sym
->attr
.generic
2881 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2884 if (!sym
->attr
.function
2885 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2890 gfc_interface
*intr
, *head
;
2892 /* Use upper case to save the actual derived-type symbol. */
2893 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2894 dt_sym
->name
= gfc_get_string (sym
->name
);
2895 head
= sym
->generic
;
2896 intr
= gfc_get_interface ();
2898 intr
->where
= gfc_current_locus
;
2900 sym
->generic
= intr
;
2901 sym
->attr
.if_source
= IFSRC_DECL
;
2904 gfc_set_sym_referenced (dt_sym
);
2906 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2907 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2910 ts
->u
.derived
= dt_sym
;
2916 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2917 "intrinsic-type-spec at %C"))
2920 /* For all types except double, derived and character, look for an
2921 optional kind specifier. MATCH_NO is actually OK at this point. */
2922 if (implicit_flag
== 1)
2924 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2930 if (gfc_current_form
== FORM_FREE
)
2932 c
= gfc_peek_ascii_char ();
2933 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2934 && c
!= ':' && c
!= ',')
2936 if (matched_type
&& c
== ')')
2938 gfc_next_ascii_char ();
2945 m
= gfc_match_kind_spec (ts
, false);
2946 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2947 m
= gfc_match_old_kind_spec (ts
);
2949 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2952 /* Defer association of the KIND expression of function results
2953 until after USE and IMPORT statements. */
2954 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2955 || gfc_matching_function
)
2959 m
= MATCH_YES
; /* No kind specifier found. */
2965 /* Match an IMPLICIT NONE statement. Actually, this statement is
2966 already matched in parse.c, or we would not end up here in the
2967 first place. So the only thing we need to check, is if there is
2968 trailing garbage. If not, the match is successful. */
2971 gfc_match_implicit_none (void)
2975 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2977 bool external
= false;
2978 locus cur_loc
= gfc_current_locus
;
2980 if (gfc_current_ns
->seen_implicit_none
2981 || gfc_current_ns
->has_implicit_none_export
)
2983 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
2987 gfc_gobble_whitespace ();
2988 c
= gfc_peek_ascii_char ();
2991 (void) gfc_next_ascii_char ();
2992 if (!gfc_notify_std (GFC_STD_F2015
, "IMPORT NONE with spec list at %C"))
2995 gfc_gobble_whitespace ();
2996 if (gfc_peek_ascii_char () == ')')
2998 (void) gfc_next_ascii_char ();
3004 m
= gfc_match (" %n", name
);
3008 if (strcmp (name
, "type") == 0)
3010 else if (strcmp (name
, "external") == 0)
3015 gfc_gobble_whitespace ();
3016 c
= gfc_next_ascii_char ();
3027 if (gfc_match_eos () != MATCH_YES
)
3030 gfc_set_implicit_none (type
, external
, &cur_loc
);
3036 /* Match the letter range(s) of an IMPLICIT statement. */
3039 match_implicit_range (void)
3045 cur_loc
= gfc_current_locus
;
3047 gfc_gobble_whitespace ();
3048 c
= gfc_next_ascii_char ();
3051 gfc_error ("Missing character range in IMPLICIT at %C");
3058 gfc_gobble_whitespace ();
3059 c1
= gfc_next_ascii_char ();
3063 gfc_gobble_whitespace ();
3064 c
= gfc_next_ascii_char ();
3069 inner
= 0; /* Fall through. */
3076 gfc_gobble_whitespace ();
3077 c2
= gfc_next_ascii_char ();
3081 gfc_gobble_whitespace ();
3082 c
= gfc_next_ascii_char ();
3084 if ((c
!= ',') && (c
!= ')'))
3097 gfc_error ("Letters must be in alphabetic order in "
3098 "IMPLICIT statement at %C");
3102 /* See if we can add the newly matched range to the pending
3103 implicits from this IMPLICIT statement. We do not check for
3104 conflicts with whatever earlier IMPLICIT statements may have
3105 set. This is done when we've successfully finished matching
3107 if (!gfc_add_new_implicit_range (c1
, c2
))
3114 gfc_syntax_error (ST_IMPLICIT
);
3116 gfc_current_locus
= cur_loc
;
3121 /* Match an IMPLICIT statement, storing the types for
3122 gfc_set_implicit() if the statement is accepted by the parser.
3123 There is a strange looking, but legal syntactic construction
3124 possible. It looks like:
3126 IMPLICIT INTEGER (a-b) (c-d)
3128 This is legal if "a-b" is a constant expression that happens to
3129 equal one of the legal kinds for integers. The real problem
3130 happens with an implicit specification that looks like:
3132 IMPLICIT INTEGER (a-b)
3134 In this case, a typespec matcher that is "greedy" (as most of the
3135 matchers are) gobbles the character range as a kindspec, leaving
3136 nothing left. We therefore have to go a bit more slowly in the
3137 matching process by inhibiting the kindspec checking during
3138 typespec matching and checking for a kind later. */
3141 gfc_match_implicit (void)
3148 if (gfc_current_ns
->seen_implicit_none
)
3150 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3157 /* We don't allow empty implicit statements. */
3158 if (gfc_match_eos () == MATCH_YES
)
3160 gfc_error ("Empty IMPLICIT statement at %C");
3166 /* First cleanup. */
3167 gfc_clear_new_implicit ();
3169 /* A basic type is mandatory here. */
3170 m
= gfc_match_decl_type_spec (&ts
, 1);
3171 if (m
== MATCH_ERROR
)
3176 cur_loc
= gfc_current_locus
;
3177 m
= match_implicit_range ();
3181 /* We may have <TYPE> (<RANGE>). */
3182 gfc_gobble_whitespace ();
3183 c
= gfc_peek_ascii_char ();
3184 if (c
== ',' || c
== '\n' || c
== ';' || c
== '!')
3186 /* Check for CHARACTER with no length parameter. */
3187 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3189 ts
.kind
= gfc_default_character_kind
;
3190 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3191 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3195 /* Record the Successful match. */
3196 if (!gfc_merge_new_implicit (&ts
))
3199 c
= gfc_next_ascii_char ();
3200 else if (gfc_match_eos () == MATCH_ERROR
)
3205 gfc_current_locus
= cur_loc
;
3208 /* Discard the (incorrectly) matched range. */
3209 gfc_clear_new_implicit ();
3211 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3212 if (ts
.type
== BT_CHARACTER
)
3213 m
= gfc_match_char_spec (&ts
);
3216 m
= gfc_match_kind_spec (&ts
, false);
3219 m
= gfc_match_old_kind_spec (&ts
);
3220 if (m
== MATCH_ERROR
)
3226 if (m
== MATCH_ERROR
)
3229 m
= match_implicit_range ();
3230 if (m
== MATCH_ERROR
)
3235 gfc_gobble_whitespace ();
3236 c
= gfc_next_ascii_char ();
3237 if (c
!= ',' && gfc_match_eos () != MATCH_YES
)
3240 if (!gfc_merge_new_implicit (&ts
))
3248 gfc_syntax_error (ST_IMPLICIT
);
3256 gfc_match_import (void)
3258 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3263 if (gfc_current_ns
->proc_name
== NULL
3264 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3266 gfc_error ("IMPORT statement at %C only permitted in "
3267 "an INTERFACE body");
3271 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3274 if (gfc_match_eos () == MATCH_YES
)
3276 /* All host variables should be imported. */
3277 gfc_current_ns
->has_import_set
= 1;
3281 if (gfc_match (" ::") == MATCH_YES
)
3283 if (gfc_match_eos () == MATCH_YES
)
3285 gfc_error ("Expecting list of named entities at %C");
3293 m
= gfc_match (" %n", name
);
3297 if (gfc_current_ns
->parent
!= NULL
3298 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3300 gfc_error ("Type name %qs at %C is ambiguous", name
);
3303 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3304 && gfc_find_symbol (name
,
3305 gfc_current_ns
->proc_name
->ns
->parent
,
3308 gfc_error ("Type name %qs at %C is ambiguous", name
);
3314 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3315 "at %C - does not exist.", name
);
3319 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3321 gfc_warning ("%qs is already IMPORTed from host scoping unit "
3326 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3329 sym
->attr
.imported
= 1;
3331 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3333 /* The actual derived type is stored in a symtree with the first
3334 letter of the name capitalized; the symtree with the all
3335 lower-case name contains the associated generic function. */
3336 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3337 gfc_get_string ("%c%s",
3338 (char) TOUPPER ((unsigned char) name
[0]),
3342 sym
->attr
.imported
= 1;
3355 if (gfc_match_eos () == MATCH_YES
)
3357 if (gfc_match_char (',') != MATCH_YES
)
3364 gfc_error ("Syntax error in IMPORT statement at %C");
3369 /* A minimal implementation of gfc_match without whitespace, escape
3370 characters or variable arguments. Returns true if the next
3371 characters match the TARGET template exactly. */
3374 match_string_p (const char *target
)
3378 for (p
= target
; *p
; p
++)
3379 if ((char) gfc_next_ascii_char () != *p
)
3384 /* Matches an attribute specification including array specs. If
3385 successful, leaves the variables current_attr and current_as
3386 holding the specification. Also sets the colon_seen variable for
3387 later use by matchers associated with initializations.
3389 This subroutine is a little tricky in the sense that we don't know
3390 if we really have an attr-spec until we hit the double colon.
3391 Until that time, we can only return MATCH_NO. This forces us to
3392 check for duplicate specification at this level. */
3395 match_attr_spec (void)
3397 /* Modifiers that can exist in a type statement. */
3399 { GFC_DECL_BEGIN
= 0,
3400 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3401 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3402 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3403 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3404 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3405 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3408 /* GFC_DECL_END is the sentinel, index starts at 0. */
3409 #define NUM_DECL GFC_DECL_END
3411 locus start
, seen_at
[NUM_DECL
];
3418 gfc_clear_attr (¤t_attr
);
3419 start
= gfc_current_locus
;
3424 /* See if we get all of the keywords up to the final double colon. */
3425 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3433 gfc_gobble_whitespace ();
3435 ch
= gfc_next_ascii_char ();
3438 /* This is the successful exit condition for the loop. */
3439 if (gfc_next_ascii_char () == ':')
3444 gfc_gobble_whitespace ();
3445 switch (gfc_peek_ascii_char ())
3448 gfc_next_ascii_char ();
3449 switch (gfc_next_ascii_char ())
3452 if (match_string_p ("locatable"))
3454 /* Matched "allocatable". */
3455 d
= DECL_ALLOCATABLE
;
3460 if (match_string_p ("ynchronous"))
3462 /* Matched "asynchronous". */
3463 d
= DECL_ASYNCHRONOUS
;
3470 /* Try and match the bind(c). */
3471 m
= gfc_match_bind_c (NULL
, true);
3474 else if (m
== MATCH_ERROR
)
3479 gfc_next_ascii_char ();
3480 if ('o' != gfc_next_ascii_char ())
3482 switch (gfc_next_ascii_char ())
3485 if (match_string_p ("imension"))
3487 d
= DECL_CODIMENSION
;
3491 if (match_string_p ("tiguous"))
3493 d
= DECL_CONTIGUOUS
;
3500 if (match_string_p ("dimension"))
3505 if (match_string_p ("external"))
3510 if (match_string_p ("int"))
3512 ch
= gfc_next_ascii_char ();
3515 if (match_string_p ("nt"))
3517 /* Matched "intent". */
3518 /* TODO: Call match_intent_spec from here. */
3519 if (gfc_match (" ( in out )") == MATCH_YES
)
3521 else if (gfc_match (" ( in )") == MATCH_YES
)
3523 else if (gfc_match (" ( out )") == MATCH_YES
)
3529 if (match_string_p ("insic"))
3531 /* Matched "intrinsic". */
3539 if (match_string_p ("optional"))
3544 gfc_next_ascii_char ();
3545 switch (gfc_next_ascii_char ())
3548 if (match_string_p ("rameter"))
3550 /* Matched "parameter". */
3556 if (match_string_p ("inter"))
3558 /* Matched "pointer". */
3564 ch
= gfc_next_ascii_char ();
3567 if (match_string_p ("vate"))
3569 /* Matched "private". */
3575 if (match_string_p ("tected"))
3577 /* Matched "protected". */
3584 if (match_string_p ("blic"))
3586 /* Matched "public". */
3594 if (match_string_p ("save"))
3599 if (match_string_p ("target"))
3604 gfc_next_ascii_char ();
3605 ch
= gfc_next_ascii_char ();
3608 if (match_string_p ("lue"))
3610 /* Matched "value". */
3616 if (match_string_p ("latile"))
3618 /* Matched "volatile". */
3626 /* No double colon and no recognizable decl_type, so assume that
3627 we've been looking at something else the whole time. */
3634 /* Check to make sure any parens are paired up correctly. */
3635 if (gfc_match_parens () == MATCH_ERROR
)
3642 seen_at
[d
] = gfc_current_locus
;
3644 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3646 gfc_array_spec
*as
= NULL
;
3648 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3649 d
== DECL_CODIMENSION
);
3651 if (current_as
== NULL
)
3653 else if (m
== MATCH_YES
)
3655 if (!merge_array_spec (as
, current_as
, false))
3662 if (d
== DECL_CODIMENSION
)
3663 gfc_error ("Missing codimension specification at %C");
3665 gfc_error ("Missing dimension specification at %C");
3669 if (m
== MATCH_ERROR
)
3674 /* Since we've seen a double colon, we have to be looking at an
3675 attr-spec. This means that we can now issue errors. */
3676 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3681 case DECL_ALLOCATABLE
:
3682 attr
= "ALLOCATABLE";
3684 case DECL_ASYNCHRONOUS
:
3685 attr
= "ASYNCHRONOUS";
3687 case DECL_CODIMENSION
:
3688 attr
= "CODIMENSION";
3690 case DECL_CONTIGUOUS
:
3691 attr
= "CONTIGUOUS";
3693 case DECL_DIMENSION
:
3700 attr
= "INTENT (IN)";
3703 attr
= "INTENT (OUT)";
3706 attr
= "INTENT (IN OUT)";
3708 case DECL_INTRINSIC
:
3714 case DECL_PARAMETER
:
3720 case DECL_PROTECTED
:
3735 case DECL_IS_BIND_C
:
3745 attr
= NULL
; /* This shouldn't happen. */
3748 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3753 /* Now that we've dealt with duplicate attributes, add the attributes
3754 to the current attribute. */
3755 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3760 if (gfc_current_state () == COMP_DERIVED
3761 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3762 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3763 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3765 if (d
== DECL_ALLOCATABLE
)
3767 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3768 "attribute at %C in a TYPE definition"))
3776 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3783 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3784 && gfc_current_state () != COMP_MODULE
)
3786 if (d
== DECL_PRIVATE
)
3790 if (gfc_current_state () == COMP_DERIVED
3791 && gfc_state_stack
->previous
3792 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3794 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3795 "at %L in a TYPE definition", attr
,
3804 gfc_error ("%s attribute at %L is not allowed outside of the "
3805 "specification part of a module", attr
, &seen_at
[d
]);
3813 case DECL_ALLOCATABLE
:
3814 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3817 case DECL_ASYNCHRONOUS
:
3818 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3821 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3824 case DECL_CODIMENSION
:
3825 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3828 case DECL_CONTIGUOUS
:
3829 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3832 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3835 case DECL_DIMENSION
:
3836 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3840 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3844 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3848 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3852 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3855 case DECL_INTRINSIC
:
3856 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3860 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3863 case DECL_PARAMETER
:
3864 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3868 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3871 case DECL_PROTECTED
:
3872 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3874 gfc_error ("PROTECTED at %C only allowed in specification "
3875 "part of a module");
3880 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3883 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3887 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3892 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3897 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3901 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3904 case DECL_IS_BIND_C
:
3905 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3909 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3912 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3916 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3919 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3923 gfc_internal_error ("match_attr_spec(): Bad attribute");
3933 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3934 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3935 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3936 current_attr
.save
= SAVE_IMPLICIT
;
3942 gfc_current_locus
= start
;
3943 gfc_free_array_spec (current_as
);
3949 /* Set the binding label, dest_label, either with the binding label
3950 stored in the given gfc_typespec, ts, or if none was provided, it
3951 will be the symbol name in all lower case, as required by the draft
3952 (J3/04-007, section 15.4.1). If a binding label was given and
3953 there is more than one argument (num_idents), it is an error. */
3956 set_binding_label (const char **dest_label
, const char *sym_name
,
3959 if (num_idents
> 1 && has_name_equals
)
3961 gfc_error ("Multiple identifiers provided with "
3962 "single NAME= specifier at %C");
3966 if (curr_binding_label
)
3967 /* Binding label given; store in temp holder till have sym. */
3968 *dest_label
= curr_binding_label
;
3971 /* No binding label given, and the NAME= specifier did not exist,
3972 which means there was no NAME="". */
3973 if (sym_name
!= NULL
&& has_name_equals
== 0)
3974 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3981 /* Set the status of the given common block as being BIND(C) or not,
3982 depending on the given parameter, is_bind_c. */
3985 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3987 com_block
->is_bind_c
= is_bind_c
;
3992 /* Verify that the given gfc_typespec is for a C interoperable type. */
3995 gfc_verify_c_interop (gfc_typespec
*ts
)
3997 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3998 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
4000 else if (ts
->type
== BT_CLASS
)
4002 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
4009 /* Verify that the variables of a given common block, which has been
4010 defined with the attribute specifier bind(c), to be of a C
4011 interoperable type. Errors will be reported here, if
4015 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
4017 gfc_symbol
*curr_sym
= NULL
;
4020 curr_sym
= com_block
->head
;
4022 /* Make sure we have at least one symbol. */
4023 if (curr_sym
== NULL
)
4026 /* Here we know we have a symbol, so we'll execute this loop
4030 /* The second to last param, 1, says this is in a common block. */
4031 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
4032 curr_sym
= curr_sym
->common_next
;
4033 } while (curr_sym
!= NULL
);
4039 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4040 an appropriate error message is reported. */
4043 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
4044 int is_in_common
, gfc_common_head
*com_block
)
4046 bool bind_c_function
= false;
4049 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
4050 bind_c_function
= true;
4052 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
4054 tmp_sym
= tmp_sym
->result
;
4055 /* Make sure it wasn't an implicitly typed result. */
4056 if (tmp_sym
->attr
.implicit_type
&& warn_c_binding_type
)
4058 gfc_warning (OPT_Wc_binding_type
,
4059 "Implicitly declared BIND(C) function %qs at "
4060 "%L may not be C interoperable", tmp_sym
->name
,
4061 &tmp_sym
->declared_at
);
4062 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
4063 /* Mark it as C interoperable to prevent duplicate warnings. */
4064 tmp_sym
->ts
.is_c_interop
= 1;
4065 tmp_sym
->attr
.is_c_interop
= 1;
4069 /* Here, we know we have the bind(c) attribute, so if we have
4070 enough type info, then verify that it's a C interop kind.
4071 The info could be in the symbol already, or possibly still in
4072 the given ts (current_ts), so look in both. */
4073 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
4075 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
4077 /* See if we're dealing with a sym in a common block or not. */
4078 if (is_in_common
== 1 && warn_c_binding_type
)
4080 gfc_warning (OPT_Wc_binding_type
,
4081 "Variable %qs in common block %qs at %L "
4082 "may not be a C interoperable "
4083 "kind though common block %qs is BIND(C)",
4084 tmp_sym
->name
, com_block
->name
,
4085 &(tmp_sym
->declared_at
), com_block
->name
);
4089 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
4090 gfc_error ("Type declaration %qs at %L is not C "
4091 "interoperable but it is BIND(C)",
4092 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4093 else if (warn_c_binding_type
)
4094 gfc_warning (OPT_Wc_binding_type
, "Variable %qs at %L "
4095 "may not be a C interoperable "
4096 "kind but it is BIND(C)",
4097 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4101 /* Variables declared w/in a common block can't be bind(c)
4102 since there's no way for C to see these variables, so there's
4103 semantically no reason for the attribute. */
4104 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4106 gfc_error ("Variable %qs in common block %qs at "
4107 "%L cannot be declared with BIND(C) "
4108 "since it is not a global",
4109 tmp_sym
->name
, com_block
->name
,
4110 &(tmp_sym
->declared_at
));
4114 /* Scalar variables that are bind(c) can not have the pointer
4115 or allocatable attributes. */
4116 if (tmp_sym
->attr
.is_bind_c
== 1)
4118 if (tmp_sym
->attr
.pointer
== 1)
4120 gfc_error ("Variable %qs at %L cannot have both the "
4121 "POINTER and BIND(C) attributes",
4122 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4126 if (tmp_sym
->attr
.allocatable
== 1)
4128 gfc_error ("Variable %qs at %L cannot have both the "
4129 "ALLOCATABLE and BIND(C) attributes",
4130 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4136 /* If it is a BIND(C) function, make sure the return value is a
4137 scalar value. The previous tests in this function made sure
4138 the type is interoperable. */
4139 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4140 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4141 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4143 /* BIND(C) functions can not return a character string. */
4144 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4145 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4146 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4147 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4148 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4149 "be a character string", tmp_sym
->name
,
4150 &(tmp_sym
->declared_at
));
4153 /* See if the symbol has been marked as private. If it has, make sure
4154 there is no binding label and warn the user if there is one. */
4155 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4156 && tmp_sym
->binding_label
)
4157 /* Use gfc_warning_now because we won't say that the symbol fails
4158 just because of this. */
4159 gfc_warning_now ("Symbol %qs at %L is marked PRIVATE but has been "
4160 "given the binding label %qs", tmp_sym
->name
,
4161 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4167 /* Set the appropriate fields for a symbol that's been declared as
4168 BIND(C) (the is_bind_c flag and the binding label), and verify that
4169 the type is C interoperable. Errors are reported by the functions
4170 used to set/test these fields. */
4173 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4177 /* TODO: Do we need to make sure the vars aren't marked private? */
4179 /* Set the is_bind_c bit in symbol_attribute. */
4180 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4182 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4189 /* Set the fields marking the given common block as BIND(C), including
4190 a binding label, and report any errors encountered. */
4193 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4197 /* destLabel, common name, typespec (which may have binding label). */
4198 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4202 /* Set the given common block (com_block) to being bind(c) (1). */
4203 set_com_block_bind_c (com_block
, 1);
4209 /* Retrieve the list of one or more identifiers that the given bind(c)
4210 attribute applies to. */
4213 get_bind_c_idents (void)
4215 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4217 gfc_symbol
*tmp_sym
= NULL
;
4219 gfc_common_head
*com_block
= NULL
;
4221 if (gfc_match_name (name
) == MATCH_YES
)
4223 found_id
= MATCH_YES
;
4224 gfc_get_ha_symbol (name
, &tmp_sym
);
4226 else if (match_common_name (name
) == MATCH_YES
)
4228 found_id
= MATCH_YES
;
4229 com_block
= gfc_get_common (name
, 0);
4233 gfc_error ("Need either entity or common block name for "
4234 "attribute specification statement at %C");
4238 /* Save the current identifier and look for more. */
4241 /* Increment the number of identifiers found for this spec stmt. */
4244 /* Make sure we have a sym or com block, and verify that it can
4245 be bind(c). Set the appropriate field(s) and look for more
4247 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4249 if (tmp_sym
!= NULL
)
4251 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4256 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4260 /* Look to see if we have another identifier. */
4262 if (gfc_match_eos () == MATCH_YES
)
4263 found_id
= MATCH_NO
;
4264 else if (gfc_match_char (',') != MATCH_YES
)
4265 found_id
= MATCH_NO
;
4266 else if (gfc_match_name (name
) == MATCH_YES
)
4268 found_id
= MATCH_YES
;
4269 gfc_get_ha_symbol (name
, &tmp_sym
);
4271 else if (match_common_name (name
) == MATCH_YES
)
4273 found_id
= MATCH_YES
;
4274 com_block
= gfc_get_common (name
, 0);
4278 gfc_error ("Missing entity or common block name for "
4279 "attribute specification statement at %C");
4285 gfc_internal_error ("Missing symbol");
4287 } while (found_id
== MATCH_YES
);
4289 /* if we get here we were successful */
4294 /* Try and match a BIND(C) attribute specification statement. */
4297 gfc_match_bind_c_stmt (void)
4299 match found_match
= MATCH_NO
;
4304 /* This may not be necessary. */
4306 /* Clear the temporary binding label holder. */
4307 curr_binding_label
= NULL
;
4309 /* Look for the bind(c). */
4310 found_match
= gfc_match_bind_c (NULL
, true);
4312 if (found_match
== MATCH_YES
)
4314 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4317 /* Look for the :: now, but it is not required. */
4320 /* Get the identifier(s) that needs to be updated. This may need to
4321 change to hand the flag(s) for the attr specified so all identifiers
4322 found can have all appropriate parts updated (assuming that the same
4323 spec stmt can have multiple attrs, such as both bind(c) and
4325 if (!get_bind_c_idents ())
4326 /* Error message should have printed already. */
4334 /* Match a data declaration statement. */
4337 gfc_match_data_decl (void)
4343 num_idents_on_line
= 0;
4345 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4349 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4350 && gfc_current_state () != COMP_DERIVED
)
4352 sym
= gfc_use_derived (current_ts
.u
.derived
);
4360 current_ts
.u
.derived
= sym
;
4363 m
= match_attr_spec ();
4364 if (m
== MATCH_ERROR
)
4370 if (current_ts
.type
== BT_CLASS
4371 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4374 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4375 && current_ts
.u
.derived
->components
== NULL
4376 && !current_ts
.u
.derived
->attr
.zero_comp
)
4379 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4382 gfc_find_symbol (current_ts
.u
.derived
->name
,
4383 current_ts
.u
.derived
->ns
, 1, &sym
);
4385 /* Any symbol that we find had better be a type definition
4386 which has its components defined. */
4387 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4388 && (current_ts
.u
.derived
->components
!= NULL
4389 || current_ts
.u
.derived
->attr
.zero_comp
))
4392 gfc_error ("Derived type at %C has not been previously defined "
4393 "and so cannot appear in a derived type definition");
4399 /* If we have an old-style character declaration, and no new-style
4400 attribute specifications, then there a comma is optional between
4401 the type specification and the variable list. */
4402 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4403 gfc_match_char (',');
4405 /* Give the types/attributes to symbols that follow. Give the element
4406 a number so that repeat character length expressions can be copied. */
4410 num_idents_on_line
++;
4411 m
= variable_decl (elem
++);
4412 if (m
== MATCH_ERROR
)
4417 if (gfc_match_eos () == MATCH_YES
)
4419 if (gfc_match_char (',') != MATCH_YES
)
4423 if (!gfc_error_flag_test ())
4424 gfc_error ("Syntax error in data declaration at %C");
4427 gfc_free_data_all (gfc_current_ns
);
4430 gfc_free_array_spec (current_as
);
4436 /* Match a prefix associated with a function or subroutine
4437 declaration. If the typespec pointer is nonnull, then a typespec
4438 can be matched. Note that if nothing matches, MATCH_YES is
4439 returned (the null string was matched). */
4442 gfc_match_prefix (gfc_typespec
*ts
)
4448 gfc_clear_attr (¤t_attr
);
4450 seen_impure
= false;
4452 gcc_assert (!gfc_matching_prefix
);
4453 gfc_matching_prefix
= true;
4457 found_prefix
= false;
4459 if (!seen_type
&& ts
!= NULL
4460 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4461 && gfc_match_space () == MATCH_YES
)
4465 found_prefix
= true;
4468 if (gfc_match ("elemental% ") == MATCH_YES
)
4470 if (!gfc_add_elemental (¤t_attr
, NULL
))
4473 found_prefix
= true;
4476 if (gfc_match ("pure% ") == MATCH_YES
)
4478 if (!gfc_add_pure (¤t_attr
, NULL
))
4481 found_prefix
= true;
4484 if (gfc_match ("recursive% ") == MATCH_YES
)
4486 if (!gfc_add_recursive (¤t_attr
, NULL
))
4489 found_prefix
= true;
4492 /* IMPURE is a somewhat special case, as it needs not set an actual
4493 attribute but rather only prevents ELEMENTAL routines from being
4494 automatically PURE. */
4495 if (gfc_match ("impure% ") == MATCH_YES
)
4497 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4501 found_prefix
= true;
4504 while (found_prefix
);
4506 /* IMPURE and PURE must not both appear, of course. */
4507 if (seen_impure
&& current_attr
.pure
)
4509 gfc_error ("PURE and IMPURE must not appear both at %C");
4513 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4514 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4516 if (!gfc_add_pure (¤t_attr
, NULL
))
4520 /* At this point, the next item is not a prefix. */
4521 gcc_assert (gfc_matching_prefix
);
4522 gfc_matching_prefix
= false;
4526 gcc_assert (gfc_matching_prefix
);
4527 gfc_matching_prefix
= false;
4532 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4535 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4537 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4540 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4543 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4550 /* Match a formal argument list. */
4553 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4555 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4556 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4562 if (gfc_match_char ('(') != MATCH_YES
)
4569 if (gfc_match_char (')') == MATCH_YES
)
4574 if (gfc_match_char ('*') == MATCH_YES
)
4577 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4586 m
= gfc_match_name (name
);
4590 if (gfc_get_symbol (name
, NULL
, &sym
))
4594 p
= gfc_get_formal_arglist ();
4606 /* We don't add the VARIABLE flavor because the name could be a
4607 dummy procedure. We don't apply these attributes to formal
4608 arguments of statement functions. */
4609 if (sym
!= NULL
&& !st_flag
4610 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4611 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4617 /* The name of a program unit can be in a different namespace,
4618 so check for it explicitly. After the statement is accepted,
4619 the name is checked for especially in gfc_get_symbol(). */
4620 if (gfc_new_block
!= NULL
&& sym
!= NULL
4621 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4623 gfc_error ("Name %qs at %C is the name of the procedure",
4629 if (gfc_match_char (')') == MATCH_YES
)
4632 m
= gfc_match_char (',');
4635 gfc_error ("Unexpected junk in formal argument list at %C");
4641 /* Check for duplicate symbols in the formal argument list. */
4644 for (p
= head
; p
->next
; p
= p
->next
)
4649 for (q
= p
->next
; q
; q
= q
->next
)
4650 if (p
->sym
== q
->sym
)
4652 gfc_error ("Duplicate symbol %qs in formal argument list "
4653 "at %C", p
->sym
->name
);
4661 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4670 gfc_free_formal_arglist (head
);
4675 /* Match a RESULT specification following a function declaration or
4676 ENTRY statement. Also matches the end-of-statement. */
4679 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4681 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4685 if (gfc_match (" result (") != MATCH_YES
)
4688 m
= gfc_match_name (name
);
4692 /* Get the right paren, and that's it because there could be the
4693 bind(c) attribute after the result clause. */
4694 if (gfc_match_char (')') != MATCH_YES
)
4696 /* TODO: should report the missing right paren here. */
4700 if (strcmp (function
->name
, name
) == 0)
4702 gfc_error ("RESULT variable at %C must be different than function name");
4706 if (gfc_get_symbol (name
, NULL
, &r
))
4709 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4718 /* Match a function suffix, which could be a combination of a result
4719 clause and BIND(C), either one, or neither. The draft does not
4720 require them to come in a specific order. */
4723 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4725 match is_bind_c
; /* Found bind(c). */
4726 match is_result
; /* Found result clause. */
4727 match found_match
; /* Status of whether we've found a good match. */
4728 char peek_char
; /* Character we're going to peek at. */
4729 bool allow_binding_name
;
4731 /* Initialize to having found nothing. */
4732 found_match
= MATCH_NO
;
4733 is_bind_c
= MATCH_NO
;
4734 is_result
= MATCH_NO
;
4736 /* Get the next char to narrow between result and bind(c). */
4737 gfc_gobble_whitespace ();
4738 peek_char
= gfc_peek_ascii_char ();
4740 /* C binding names are not allowed for internal procedures. */
4741 if (gfc_current_state () == COMP_CONTAINS
4742 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4743 allow_binding_name
= false;
4745 allow_binding_name
= true;
4750 /* Look for result clause. */
4751 is_result
= match_result (sym
, result
);
4752 if (is_result
== MATCH_YES
)
4754 /* Now see if there is a bind(c) after it. */
4755 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4756 /* We've found the result clause and possibly bind(c). */
4757 found_match
= MATCH_YES
;
4760 /* This should only be MATCH_ERROR. */
4761 found_match
= is_result
;
4764 /* Look for bind(c) first. */
4765 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4766 if (is_bind_c
== MATCH_YES
)
4768 /* Now see if a result clause followed it. */
4769 is_result
= match_result (sym
, result
);
4770 found_match
= MATCH_YES
;
4774 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4775 found_match
= MATCH_ERROR
;
4779 gfc_error ("Unexpected junk after function declaration at %C");
4780 found_match
= MATCH_ERROR
;
4784 if (is_bind_c
== MATCH_YES
)
4786 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4787 if (gfc_current_state () == COMP_CONTAINS
4788 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4789 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4790 "at %L may not be specified for an internal "
4791 "procedure", &gfc_current_locus
))
4794 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4802 /* Procedure pointer return value without RESULT statement:
4803 Add "hidden" result variable named "ppr@". */
4806 add_hidden_procptr_result (gfc_symbol
*sym
)
4810 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4813 /* First usage case: PROCEDURE and EXTERNAL statements. */
4814 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4815 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4816 && sym
->attr
.external
;
4817 /* Second usage case: INTERFACE statements. */
4818 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4819 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4820 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4826 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4830 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4831 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4832 st2
->n
.sym
= stree
->n
.sym
;
4834 sym
->result
= stree
->n
.sym
;
4836 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4837 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4838 sym
->result
->attr
.external
= sym
->attr
.external
;
4839 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4840 sym
->result
->ts
= sym
->ts
;
4841 sym
->attr
.proc_pointer
= 0;
4842 sym
->attr
.pointer
= 0;
4843 sym
->attr
.external
= 0;
4844 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4846 sym
->result
->attr
.pointer
= 0;
4847 sym
->result
->attr
.proc_pointer
= 1;
4850 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4852 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4853 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4854 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4855 && sym
== gfc_current_ns
->proc_name
4856 && sym
== sym
->result
->ns
->proc_name
4857 && strcmp ("ppr@", sym
->result
->name
) == 0)
4859 sym
->result
->attr
.proc_pointer
= 1;
4860 sym
->attr
.pointer
= 0;
4868 /* Match the interface for a PROCEDURE declaration,
4869 including brackets (R1212). */
4872 match_procedure_interface (gfc_symbol
**proc_if
)
4876 locus old_loc
, entry_loc
;
4877 gfc_namespace
*old_ns
= gfc_current_ns
;
4878 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4880 old_loc
= entry_loc
= gfc_current_locus
;
4881 gfc_clear_ts (¤t_ts
);
4883 if (gfc_match (" (") != MATCH_YES
)
4885 gfc_current_locus
= entry_loc
;
4889 /* Get the type spec. for the procedure interface. */
4890 old_loc
= gfc_current_locus
;
4891 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4892 gfc_gobble_whitespace ();
4893 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4896 if (m
== MATCH_ERROR
)
4899 /* Procedure interface is itself a procedure. */
4900 gfc_current_locus
= old_loc
;
4901 m
= gfc_match_name (name
);
4903 /* First look to see if it is already accessible in the current
4904 namespace because it is use associated or contained. */
4906 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4909 /* If it is still not found, then try the parent namespace, if it
4910 exists and create the symbol there if it is still not found. */
4911 if (gfc_current_ns
->parent
)
4912 gfc_current_ns
= gfc_current_ns
->parent
;
4913 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4916 gfc_current_ns
= old_ns
;
4917 *proc_if
= st
->n
.sym
;
4922 /* Resolve interface if possible. That way, attr.procedure is only set
4923 if it is declared by a later procedure-declaration-stmt, which is
4924 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4925 while ((*proc_if
)->ts
.interface
)
4926 *proc_if
= (*proc_if
)->ts
.interface
;
4928 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4929 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4930 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4931 (*proc_if
)->name
, NULL
))
4936 if (gfc_match (" )") != MATCH_YES
)
4938 gfc_current_locus
= entry_loc
;
4946 /* Match a PROCEDURE declaration (R1211). */
4949 match_procedure_decl (void)
4952 gfc_symbol
*sym
, *proc_if
= NULL
;
4954 gfc_expr
*initializer
= NULL
;
4956 /* Parse interface (with brackets). */
4957 m
= match_procedure_interface (&proc_if
);
4961 /* Parse attributes (with colons). */
4962 m
= match_attr_spec();
4963 if (m
== MATCH_ERROR
)
4966 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4968 current_attr
.is_bind_c
= 1;
4969 has_name_equals
= 0;
4970 curr_binding_label
= NULL
;
4973 /* Get procedure symbols. */
4976 m
= gfc_match_symbol (&sym
, 0);
4979 else if (m
== MATCH_ERROR
)
4982 /* Add current_attr to the symbol attributes. */
4983 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4986 if (sym
->attr
.is_bind_c
)
4988 /* Check for C1218. */
4989 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4991 gfc_error ("BIND(C) attribute at %C requires "
4992 "an interface with BIND(C)");
4995 /* Check for C1217. */
4996 if (has_name_equals
&& sym
->attr
.pointer
)
4998 gfc_error ("BIND(C) procedure with NAME may not have "
4999 "POINTER attribute at %C");
5002 if (has_name_equals
&& sym
->attr
.dummy
)
5004 gfc_error ("Dummy procedure at %C may not have "
5005 "BIND(C) attribute with NAME");
5008 /* Set binding label for BIND(C). */
5009 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
5013 if (!gfc_add_external (&sym
->attr
, NULL
))
5016 if (add_hidden_procptr_result (sym
))
5019 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
5022 /* Set interface. */
5023 if (proc_if
!= NULL
)
5025 if (sym
->ts
.type
!= BT_UNKNOWN
)
5027 gfc_error ("Procedure %qs at %L already has basic type of %s",
5028 sym
->name
, &gfc_current_locus
,
5029 gfc_basic_typename (sym
->ts
.type
));
5032 sym
->ts
.interface
= proc_if
;
5033 sym
->attr
.untyped
= 1;
5034 sym
->attr
.if_source
= IFSRC_IFBODY
;
5036 else if (current_ts
.type
!= BT_UNKNOWN
)
5038 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5040 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5041 sym
->ts
.interface
->ts
= current_ts
;
5042 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5043 sym
->ts
.interface
->attr
.function
= 1;
5044 sym
->attr
.function
= 1;
5045 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
5048 if (gfc_match (" =>") == MATCH_YES
)
5050 if (!current_attr
.pointer
)
5052 gfc_error ("Initialization at %C isn't for a pointer variable");
5057 m
= match_pointer_init (&initializer
, 1);
5061 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
5066 if (gfc_match_eos () == MATCH_YES
)
5068 if (gfc_match_char (',') != MATCH_YES
)
5073 gfc_error ("Syntax error in PROCEDURE statement at %C");
5077 /* Free stuff up and return. */
5078 gfc_free_expr (initializer
);
5084 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
5087 /* Match a procedure pointer component declaration (R445). */
5090 match_ppc_decl (void)
5093 gfc_symbol
*proc_if
= NULL
;
5097 gfc_expr
*initializer
= NULL
;
5098 gfc_typebound_proc
* tb
;
5099 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5101 /* Parse interface (with brackets). */
5102 m
= match_procedure_interface (&proc_if
);
5106 /* Parse attributes. */
5107 tb
= XCNEW (gfc_typebound_proc
);
5108 tb
->where
= gfc_current_locus
;
5109 m
= match_binding_attributes (tb
, false, true);
5110 if (m
== MATCH_ERROR
)
5113 gfc_clear_attr (¤t_attr
);
5114 current_attr
.procedure
= 1;
5115 current_attr
.proc_pointer
= 1;
5116 current_attr
.access
= tb
->access
;
5117 current_attr
.flavor
= FL_PROCEDURE
;
5119 /* Match the colons (required). */
5120 if (gfc_match (" ::") != MATCH_YES
)
5122 gfc_error ("Expected %<::%> after binding-attributes at %C");
5126 /* Check for C450. */
5127 if (!tb
->nopass
&& proc_if
== NULL
)
5129 gfc_error("NOPASS or explicit interface required at %C");
5133 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5136 /* Match PPC names. */
5140 m
= gfc_match_name (name
);
5143 else if (m
== MATCH_ERROR
)
5146 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5149 /* Add current_attr to the symbol attributes. */
5150 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5153 if (!gfc_add_external (&c
->attr
, NULL
))
5156 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5163 c
->tb
= XCNEW (gfc_typebound_proc
);
5164 c
->tb
->where
= gfc_current_locus
;
5168 /* Set interface. */
5169 if (proc_if
!= NULL
)
5171 c
->ts
.interface
= proc_if
;
5172 c
->attr
.untyped
= 1;
5173 c
->attr
.if_source
= IFSRC_IFBODY
;
5175 else if (ts
.type
!= BT_UNKNOWN
)
5178 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5179 c
->ts
.interface
->result
= c
->ts
.interface
;
5180 c
->ts
.interface
->ts
= ts
;
5181 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5182 c
->ts
.interface
->attr
.function
= 1;
5183 c
->attr
.function
= 1;
5184 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5187 if (gfc_match (" =>") == MATCH_YES
)
5189 m
= match_pointer_init (&initializer
, 1);
5192 gfc_free_expr (initializer
);
5195 c
->initializer
= initializer
;
5198 if (gfc_match_eos () == MATCH_YES
)
5200 if (gfc_match_char (',') != MATCH_YES
)
5205 gfc_error ("Syntax error in procedure pointer component at %C");
5210 /* Match a PROCEDURE declaration inside an interface (R1206). */
5213 match_procedure_in_interface (void)
5217 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5220 if (current_interface
.type
== INTERFACE_NAMELESS
5221 || current_interface
.type
== INTERFACE_ABSTRACT
)
5223 gfc_error ("PROCEDURE at %C must be in a generic interface");
5227 /* Check if the F2008 optional double colon appears. */
5228 gfc_gobble_whitespace ();
5229 old_locus
= gfc_current_locus
;
5230 if (gfc_match ("::") == MATCH_YES
)
5232 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5233 "MODULE PROCEDURE statement at %L", &old_locus
))
5237 gfc_current_locus
= old_locus
;
5241 m
= gfc_match_name (name
);
5244 else if (m
== MATCH_ERROR
)
5246 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5249 if (!gfc_add_interface (sym
))
5252 if (gfc_match_eos () == MATCH_YES
)
5254 if (gfc_match_char (',') != MATCH_YES
)
5261 gfc_error ("Syntax error in PROCEDURE statement at %C");
5266 /* General matcher for PROCEDURE declarations. */
5268 static match
match_procedure_in_type (void);
5271 gfc_match_procedure (void)
5275 switch (gfc_current_state ())
5280 case COMP_SUBROUTINE
:
5283 m
= match_procedure_decl ();
5285 case COMP_INTERFACE
:
5286 m
= match_procedure_in_interface ();
5289 m
= match_ppc_decl ();
5291 case COMP_DERIVED_CONTAINS
:
5292 m
= match_procedure_in_type ();
5301 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5308 /* Warn if a matched procedure has the same name as an intrinsic; this is
5309 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5310 parser-state-stack to find out whether we're in a module. */
5313 do_warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5317 in_module
= (gfc_state_stack
->previous
5318 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5320 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5324 /* Match a function declaration. */
5327 gfc_match_function_decl (void)
5329 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5330 gfc_symbol
*sym
, *result
;
5334 match found_match
; /* Status returned by match func. */
5336 if (gfc_current_state () != COMP_NONE
5337 && gfc_current_state () != COMP_INTERFACE
5338 && gfc_current_state () != COMP_CONTAINS
)
5341 gfc_clear_ts (¤t_ts
);
5343 old_loc
= gfc_current_locus
;
5345 m
= gfc_match_prefix (¤t_ts
);
5348 gfc_current_locus
= old_loc
;
5352 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5354 gfc_current_locus
= old_loc
;
5357 if (get_proc_name (name
, &sym
, false))
5360 if (add_hidden_procptr_result (sym
))
5363 gfc_new_block
= sym
;
5365 m
= gfc_match_formal_arglist (sym
, 0, 0);
5368 gfc_error ("Expected formal argument list in function "
5369 "definition at %C");
5373 else if (m
== MATCH_ERROR
)
5378 /* According to the draft, the bind(c) and result clause can
5379 come in either order after the formal_arg_list (i.e., either
5380 can be first, both can exist together or by themselves or neither
5381 one). Therefore, the match_result can't match the end of the
5382 string, and check for the bind(c) or result clause in either order. */
5383 found_match
= gfc_match_eos ();
5385 /* Make sure that it isn't already declared as BIND(C). If it is, it
5386 must have been marked BIND(C) with a BIND(C) attribute and that is
5387 not allowed for procedures. */
5388 if (sym
->attr
.is_bind_c
== 1)
5390 sym
->attr
.is_bind_c
= 0;
5391 if (sym
->old_symbol
!= NULL
)
5392 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5393 "variables or common blocks",
5394 &(sym
->old_symbol
->declared_at
));
5396 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5397 "variables or common blocks", &gfc_current_locus
);
5400 if (found_match
!= MATCH_YES
)
5402 /* If we haven't found the end-of-statement, look for a suffix. */
5403 suffix_match
= gfc_match_suffix (sym
, &result
);
5404 if (suffix_match
== MATCH_YES
)
5405 /* Need to get the eos now. */
5406 found_match
= gfc_match_eos ();
5408 found_match
= suffix_match
;
5411 if(found_match
!= MATCH_YES
)
5415 /* Make changes to the symbol. */
5418 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5421 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5422 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5425 /* Delay matching the function characteristics until after the
5426 specification block by signalling kind=-1. */
5427 sym
->declared_at
= old_loc
;
5428 if (current_ts
.type
!= BT_UNKNOWN
)
5429 current_ts
.kind
= -1;
5431 current_ts
.kind
= 0;
5435 if (current_ts
.type
!= BT_UNKNOWN
5436 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5442 if (current_ts
.type
!= BT_UNKNOWN
5443 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5445 sym
->result
= result
;
5448 /* Warn if this procedure has the same name as an intrinsic. */
5449 do_warn_intrinsic_shadow (sym
, true);
5455 gfc_current_locus
= old_loc
;
5460 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5461 pass the name of the entry, rather than the gfc_current_block name, and
5462 to return false upon finding an existing global entry. */
5465 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5469 enum gfc_symbol_type type
;
5471 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5473 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5474 name is a global identifier. */
5475 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5477 s
= gfc_get_gsymbol (name
);
5479 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5481 gfc_global_used (s
, where
);
5490 s
->ns
= gfc_current_ns
;
5494 /* Don't add the symbol multiple times. */
5496 && (!gfc_notification_std (GFC_STD_F2008
)
5497 || strcmp (name
, binding_label
) != 0))
5499 s
= gfc_get_gsymbol (binding_label
);
5501 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5503 gfc_global_used (s
, where
);
5510 s
->binding_label
= binding_label
;
5513 s
->ns
= gfc_current_ns
;
5521 /* Match an ENTRY statement. */
5524 gfc_match_entry (void)
5529 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5530 gfc_compile_state state
;
5534 bool module_procedure
;
5538 m
= gfc_match_name (name
);
5542 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5545 state
= gfc_current_state ();
5546 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5551 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5554 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5556 case COMP_BLOCK_DATA
:
5557 gfc_error ("ENTRY statement at %C cannot appear within "
5560 case COMP_INTERFACE
:
5561 gfc_error ("ENTRY statement at %C cannot appear within "
5565 gfc_error ("ENTRY statement at %C cannot appear within "
5566 "a DERIVED TYPE block");
5569 gfc_error ("ENTRY statement at %C cannot appear within "
5570 "an IF-THEN block");
5573 case COMP_DO_CONCURRENT
:
5574 gfc_error ("ENTRY statement at %C cannot appear within "
5578 gfc_error ("ENTRY statement at %C cannot appear within "
5582 gfc_error ("ENTRY statement at %C cannot appear within "
5586 gfc_error ("ENTRY statement at %C cannot appear within "
5590 gfc_error ("ENTRY statement at %C cannot appear within "
5591 "a contained subprogram");
5594 gfc_internal_error ("gfc_match_entry(): Bad state");
5599 module_procedure
= gfc_current_ns
->parent
!= NULL
5600 && gfc_current_ns
->parent
->proc_name
5601 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5604 if (gfc_current_ns
->parent
!= NULL
5605 && gfc_current_ns
->parent
->proc_name
5606 && !module_procedure
)
5608 gfc_error("ENTRY statement at %C cannot appear in a "
5609 "contained procedure");
5613 /* Module function entries need special care in get_proc_name
5614 because previous references within the function will have
5615 created symbols attached to the current namespace. */
5616 if (get_proc_name (name
, &entry
,
5617 gfc_current_ns
->parent
!= NULL
5618 && module_procedure
))
5621 proc
= gfc_current_block ();
5623 /* Make sure that it isn't already declared as BIND(C). If it is, it
5624 must have been marked BIND(C) with a BIND(C) attribute and that is
5625 not allowed for procedures. */
5626 if (entry
->attr
.is_bind_c
== 1)
5628 entry
->attr
.is_bind_c
= 0;
5629 if (entry
->old_symbol
!= NULL
)
5630 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5631 "variables or common blocks",
5632 &(entry
->old_symbol
->declared_at
));
5634 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5635 "variables or common blocks", &gfc_current_locus
);
5638 /* Check what next non-whitespace character is so we can tell if there
5639 is the required parens if we have a BIND(C). */
5640 old_loc
= gfc_current_locus
;
5641 gfc_gobble_whitespace ();
5642 peek_char
= gfc_peek_ascii_char ();
5644 if (state
== COMP_SUBROUTINE
)
5646 m
= gfc_match_formal_arglist (entry
, 0, 1);
5650 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5651 never be an internal procedure. */
5652 is_bind_c
= gfc_match_bind_c (entry
, true);
5653 if (is_bind_c
== MATCH_ERROR
)
5655 if (is_bind_c
== MATCH_YES
)
5657 if (peek_char
!= '(')
5659 gfc_error ("Missing required parentheses before BIND(C) at %C");
5662 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5663 &(entry
->declared_at
), 1))
5667 if (!gfc_current_ns
->parent
5668 && !add_global_entry (name
, entry
->binding_label
, true,
5672 /* An entry in a subroutine. */
5673 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5674 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5679 /* An entry in a function.
5680 We need to take special care because writing
5685 ENTRY f() RESULT (r)
5687 ENTRY f RESULT (r). */
5688 if (gfc_match_eos () == MATCH_YES
)
5690 gfc_current_locus
= old_loc
;
5691 /* Match the empty argument list, and add the interface to
5693 m
= gfc_match_formal_arglist (entry
, 0, 1);
5696 m
= gfc_match_formal_arglist (entry
, 0, 0);
5703 if (gfc_match_eos () == MATCH_YES
)
5705 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5706 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5709 entry
->result
= entry
;
5713 m
= gfc_match_suffix (entry
, &result
);
5715 gfc_syntax_error (ST_ENTRY
);
5721 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5722 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5723 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5725 entry
->result
= result
;
5729 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5730 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5732 entry
->result
= entry
;
5736 if (!gfc_current_ns
->parent
5737 && !add_global_entry (name
, entry
->binding_label
, false,
5742 if (gfc_match_eos () != MATCH_YES
)
5744 gfc_syntax_error (ST_ENTRY
);
5748 entry
->attr
.recursive
= proc
->attr
.recursive
;
5749 entry
->attr
.elemental
= proc
->attr
.elemental
;
5750 entry
->attr
.pure
= proc
->attr
.pure
;
5752 el
= gfc_get_entry_list ();
5754 el
->next
= gfc_current_ns
->entries
;
5755 gfc_current_ns
->entries
= el
;
5757 el
->id
= el
->next
->id
+ 1;
5761 new_st
.op
= EXEC_ENTRY
;
5762 new_st
.ext
.entry
= el
;
5768 /* Match a subroutine statement, including optional prefixes. */
5771 gfc_match_subroutine (void)
5773 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5778 bool allow_binding_name
;
5780 if (gfc_current_state () != COMP_NONE
5781 && gfc_current_state () != COMP_INTERFACE
5782 && gfc_current_state () != COMP_CONTAINS
)
5785 m
= gfc_match_prefix (NULL
);
5789 m
= gfc_match ("subroutine% %n", name
);
5793 if (get_proc_name (name
, &sym
, false))
5796 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5797 the symbol existed before. */
5798 sym
->declared_at
= gfc_current_locus
;
5800 if (add_hidden_procptr_result (sym
))
5803 gfc_new_block
= sym
;
5805 /* Check what next non-whitespace character is so we can tell if there
5806 is the required parens if we have a BIND(C). */
5807 gfc_gobble_whitespace ();
5808 peek_char
= gfc_peek_ascii_char ();
5810 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5813 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5816 /* Make sure that it isn't already declared as BIND(C). If it is, it
5817 must have been marked BIND(C) with a BIND(C) attribute and that is
5818 not allowed for procedures. */
5819 if (sym
->attr
.is_bind_c
== 1)
5821 sym
->attr
.is_bind_c
= 0;
5822 if (sym
->old_symbol
!= NULL
)
5823 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5824 "variables or common blocks",
5825 &(sym
->old_symbol
->declared_at
));
5827 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5828 "variables or common blocks", &gfc_current_locus
);
5831 /* C binding names are not allowed for internal procedures. */
5832 if (gfc_current_state () == COMP_CONTAINS
5833 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5834 allow_binding_name
= false;
5836 allow_binding_name
= true;
5838 /* Here, we are just checking if it has the bind(c) attribute, and if
5839 so, then we need to make sure it's all correct. If it doesn't,
5840 we still need to continue matching the rest of the subroutine line. */
5841 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5842 if (is_bind_c
== MATCH_ERROR
)
5844 /* There was an attempt at the bind(c), but it was wrong. An
5845 error message should have been printed w/in the gfc_match_bind_c
5846 so here we'll just return the MATCH_ERROR. */
5850 if (is_bind_c
== MATCH_YES
)
5852 /* The following is allowed in the Fortran 2008 draft. */
5853 if (gfc_current_state () == COMP_CONTAINS
5854 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5855 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5856 "at %L may not be specified for an internal "
5857 "procedure", &gfc_current_locus
))
5860 if (peek_char
!= '(')
5862 gfc_error ("Missing required parentheses before BIND(C) at %C");
5865 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5866 &(sym
->declared_at
), 1))
5870 if (gfc_match_eos () != MATCH_YES
)
5872 gfc_syntax_error (ST_SUBROUTINE
);
5876 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5879 /* Warn if it has the same name as an intrinsic. */
5880 do_warn_intrinsic_shadow (sym
, false);
5886 /* Check that the NAME identifier in a BIND attribute or statement
5887 is conform to C identifier rules. */
5890 check_bind_name_identifier (char **name
)
5892 char *n
= *name
, *p
;
5894 /* Remove leading spaces. */
5898 /* On an empty string, free memory and set name to NULL. */
5906 /* Remove trailing spaces. */
5907 p
= n
+ strlen(n
) - 1;
5911 /* Insert the identifier into the symbol table. */
5916 /* Now check that identifier is valid under C rules. */
5919 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5924 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
5926 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5934 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5935 given, and set the binding label in either the given symbol (if not
5936 NULL), or in the current_ts. The symbol may be NULL because we may
5937 encounter the BIND(C) before the declaration itself. Return
5938 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5939 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5940 or MATCH_YES if the specifier was correct and the binding label and
5941 bind(c) fields were set correctly for the given symbol or the
5942 current_ts. If allow_binding_name is false, no binding name may be
5946 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5948 char *binding_label
= NULL
;
5951 /* Initialize the flag that specifies whether we encountered a NAME=
5952 specifier or not. */
5953 has_name_equals
= 0;
5955 /* This much we have to be able to match, in this order, if
5956 there is a bind(c) label. */
5957 if (gfc_match (" bind ( c ") != MATCH_YES
)
5960 /* Now see if there is a binding label, or if we've reached the
5961 end of the bind(c) attribute without one. */
5962 if (gfc_match_char (',') == MATCH_YES
)
5964 if (gfc_match (" name = ") != MATCH_YES
)
5966 gfc_error ("Syntax error in NAME= specifier for binding label "
5968 /* should give an error message here */
5972 has_name_equals
= 1;
5974 if (gfc_match_init_expr (&e
) != MATCH_YES
)
5980 if (!gfc_simplify_expr(e
, 0))
5982 gfc_error ("NAME= specifier at %C should be a constant expression");
5987 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
5988 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
5990 gfc_error ("NAME= specifier at %C should be a scalar of "
5991 "default character kind");
5996 // Get a C string from the Fortran string constant
5997 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
5998 e
->value
.character
.length
);
6001 // Check that it is valid (old gfc_match_name_C)
6002 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
6006 /* Get the required right paren. */
6007 if (gfc_match_char (')') != MATCH_YES
)
6009 gfc_error ("Missing closing paren for binding label at %C");
6013 if (has_name_equals
&& !allow_binding_name
)
6015 gfc_error ("No binding name is allowed in BIND(C) at %C");
6019 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
6021 gfc_error ("For dummy procedure %s, no binding name is "
6022 "allowed in BIND(C) at %C", sym
->name
);
6027 /* Save the binding label to the symbol. If sym is null, we're
6028 probably matching the typespec attributes of a declaration and
6029 haven't gotten the name yet, and therefore, no symbol yet. */
6033 sym
->binding_label
= binding_label
;
6035 curr_binding_label
= binding_label
;
6037 else if (allow_binding_name
)
6039 /* No binding label, but if symbol isn't null, we
6040 can set the label for it here.
6041 If name="" or allow_binding_name is false, no C binding name is
6043 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
6044 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
6047 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
6048 && current_interface
.type
== INTERFACE_ABSTRACT
)
6050 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6058 /* Return nonzero if we're currently compiling a contained procedure. */
6061 contained_procedure (void)
6063 gfc_state_data
*s
= gfc_state_stack
;
6065 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
6066 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
6072 /* Set the kind of each enumerator. The kind is selected such that it is
6073 interoperable with the corresponding C enumeration type, making
6074 sure that -fshort-enums is honored. */
6079 enumerator_history
*current_history
= NULL
;
6083 if (max_enum
== NULL
|| enum_history
== NULL
)
6086 if (!flag_short_enums
)
6092 kind
= gfc_integer_kinds
[i
++].kind
;
6094 while (kind
< gfc_c_int_kind
6095 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6098 current_history
= enum_history
;
6099 while (current_history
!= NULL
)
6101 current_history
->sym
->ts
.kind
= kind
;
6102 current_history
= current_history
->next
;
6107 /* Match any of the various end-block statements. Returns the type of
6108 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6109 and END BLOCK statements cannot be replaced by a single END statement. */
6112 gfc_match_end (gfc_statement
*st
)
6114 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6115 gfc_compile_state state
;
6117 const char *block_name
;
6121 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6122 gfc_namespace
**nsp
;
6124 old_loc
= gfc_current_locus
;
6125 if (gfc_match ("end") != MATCH_YES
)
6128 state
= gfc_current_state ();
6129 block_name
= gfc_current_block () == NULL
6130 ? NULL
: gfc_current_block ()->name
;
6134 case COMP_ASSOCIATE
:
6136 if (!strncmp (block_name
, "block@", strlen("block@")))
6141 case COMP_DERIVED_CONTAINS
:
6142 state
= gfc_state_stack
->previous
->state
;
6143 block_name
= gfc_state_stack
->previous
->sym
== NULL
6144 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6155 *st
= ST_END_PROGRAM
;
6156 target
= " program";
6160 case COMP_SUBROUTINE
:
6161 *st
= ST_END_SUBROUTINE
;
6162 target
= " subroutine";
6163 eos_ok
= !contained_procedure ();
6167 *st
= ST_END_FUNCTION
;
6168 target
= " function";
6169 eos_ok
= !contained_procedure ();
6172 case COMP_BLOCK_DATA
:
6173 *st
= ST_END_BLOCK_DATA
;
6174 target
= " block data";
6179 *st
= ST_END_MODULE
;
6184 case COMP_INTERFACE
:
6185 *st
= ST_END_INTERFACE
;
6186 target
= " interface";
6191 case COMP_DERIVED_CONTAINS
:
6197 case COMP_ASSOCIATE
:
6198 *st
= ST_END_ASSOCIATE
;
6199 target
= " associate";
6216 case COMP_DO_CONCURRENT
:
6223 *st
= ST_END_CRITICAL
;
6224 target
= " critical";
6229 case COMP_SELECT_TYPE
:
6230 *st
= ST_END_SELECT
;
6236 *st
= ST_END_FORALL
;
6251 last_initializer
= NULL
;
6253 gfc_free_enum_history ();
6257 gfc_error ("Unexpected END statement at %C");
6261 old_loc
= gfc_current_locus
;
6262 if (gfc_match_eos () == MATCH_YES
)
6264 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6266 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6267 "instead of %s statement at %L",
6268 gfc_ascii_statement(*st
), &old_loc
))
6273 /* We would have required END [something]. */
6274 gfc_error ("%s statement expected at %L",
6275 gfc_ascii_statement (*st
), &old_loc
);
6282 /* Verify that we've got the sort of end-block that we're expecting. */
6283 if (gfc_match (target
) != MATCH_YES
)
6285 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6290 old_loc
= gfc_current_locus
;
6291 /* If we're at the end, make sure a block name wasn't required. */
6292 if (gfc_match_eos () == MATCH_YES
)
6295 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6296 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6297 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6303 gfc_error ("Expected block name of %qs in %s statement at %L",
6304 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6309 /* END INTERFACE has a special handler for its several possible endings. */
6310 if (*st
== ST_END_INTERFACE
)
6311 return gfc_match_end_interface ();
6313 /* We haven't hit the end of statement, so what is left must be an
6315 m
= gfc_match_space ();
6317 m
= gfc_match_name (name
);
6320 gfc_error ("Expected terminating name at %C");
6324 if (block_name
== NULL
)
6327 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6329 gfc_error ("Expected label %qs for %s statement at %C", block_name
,
6330 gfc_ascii_statement (*st
));
6333 /* Procedure pointer as function result. */
6334 else if (strcmp (block_name
, "ppr@") == 0
6335 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6337 gfc_error ("Expected label %qs for %s statement at %C",
6338 gfc_current_block ()->ns
->proc_name
->name
,
6339 gfc_ascii_statement (*st
));
6343 if (gfc_match_eos () == MATCH_YES
)
6347 gfc_syntax_error (*st
);
6350 gfc_current_locus
= old_loc
;
6352 /* If we are missing an END BLOCK, we created a half-ready namespace.
6353 Remove it from the parent namespace's sibling list. */
6355 if (state
== COMP_BLOCK
)
6357 parent_ns
= gfc_current_ns
->parent
;
6359 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6365 if (ns
== gfc_current_ns
)
6367 if (prev_ns
== NULL
)
6370 prev_ns
->sibling
= ns
->sibling
;
6376 gfc_free_namespace (gfc_current_ns
);
6377 gfc_current_ns
= parent_ns
;
6385 /***************** Attribute declaration statements ****************/
6387 /* Set the attribute of a single variable. */
6392 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6400 m
= gfc_match_name (name
);
6404 if (find_special (name
, &sym
, false))
6407 if (!check_function_name (name
))
6413 var_locus
= gfc_current_locus
;
6415 /* Deal with possible array specification for certain attributes. */
6416 if (current_attr
.dimension
6417 || current_attr
.codimension
6418 || current_attr
.allocatable
6419 || current_attr
.pointer
6420 || current_attr
.target
)
6422 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6423 !current_attr
.dimension
6424 && !current_attr
.pointer
6425 && !current_attr
.target
);
6426 if (m
== MATCH_ERROR
)
6429 if (current_attr
.dimension
&& m
== MATCH_NO
)
6431 gfc_error ("Missing array specification at %L in DIMENSION "
6432 "statement", &var_locus
);
6437 if (current_attr
.dimension
&& sym
->value
)
6439 gfc_error ("Dimensions specified for %s at %L after its "
6440 "initialisation", sym
->name
, &var_locus
);
6445 if (current_attr
.codimension
&& m
== MATCH_NO
)
6447 gfc_error ("Missing array specification at %L in CODIMENSION "
6448 "statement", &var_locus
);
6453 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6454 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6456 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6462 /* Update symbol table. DIMENSION attribute is set in
6463 gfc_set_array_spec(). For CLASS variables, this must be applied
6464 to the first component, or '_data' field. */
6465 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6467 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6475 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6476 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6483 if (sym
->ts
.type
== BT_CLASS
6484 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6490 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6496 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6498 /* Fix the array spec. */
6499 m
= gfc_mod_pointee_as (sym
->as
);
6500 if (m
== MATCH_ERROR
)
6504 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6510 if ((current_attr
.external
|| current_attr
.intrinsic
)
6511 && sym
->attr
.flavor
!= FL_PROCEDURE
6512 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6518 add_hidden_procptr_result (sym
);
6523 gfc_free_array_spec (as
);
6528 /* Generic attribute declaration subroutine. Used for attributes that
6529 just have a list of names. */
6536 /* Gobble the optional double colon, by simply ignoring the result
6546 if (gfc_match_eos () == MATCH_YES
)
6552 if (gfc_match_char (',') != MATCH_YES
)
6554 gfc_error ("Unexpected character in variable list at %C");
6564 /* This routine matches Cray Pointer declarations of the form:
6565 pointer ( <pointer>, <pointee> )
6567 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6568 The pointer, if already declared, should be an integer. Otherwise, we
6569 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6570 be either a scalar, or an array declaration. No space is allocated for
6571 the pointee. For the statement
6572 pointer (ipt, ar(10))
6573 any subsequent uses of ar will be translated (in C-notation) as
6574 ar(i) => ((<type> *) ipt)(i)
6575 After gimplification, pointee variable will disappear in the code. */
6578 cray_pointer_decl (void)
6581 gfc_array_spec
*as
= NULL
;
6582 gfc_symbol
*cptr
; /* Pointer symbol. */
6583 gfc_symbol
*cpte
; /* Pointee symbol. */
6589 if (gfc_match_char ('(') != MATCH_YES
)
6591 gfc_error ("Expected %<(%> at %C");
6595 /* Match pointer. */
6596 var_locus
= gfc_current_locus
;
6597 gfc_clear_attr (¤t_attr
);
6598 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6599 current_ts
.type
= BT_INTEGER
;
6600 current_ts
.kind
= gfc_index_integer_kind
;
6602 m
= gfc_match_symbol (&cptr
, 0);
6605 gfc_error ("Expected variable name at %C");
6609 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6612 gfc_set_sym_referenced (cptr
);
6614 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6616 cptr
->ts
.type
= BT_INTEGER
;
6617 cptr
->ts
.kind
= gfc_index_integer_kind
;
6619 else if (cptr
->ts
.type
!= BT_INTEGER
)
6621 gfc_error ("Cray pointer at %C must be an integer");
6624 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6625 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6626 " memory addresses require %d bytes",
6627 cptr
->ts
.kind
, gfc_index_integer_kind
);
6629 if (gfc_match_char (',') != MATCH_YES
)
6631 gfc_error ("Expected \",\" at %C");
6635 /* Match Pointee. */
6636 var_locus
= gfc_current_locus
;
6637 gfc_clear_attr (¤t_attr
);
6638 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6639 current_ts
.type
= BT_UNKNOWN
;
6640 current_ts
.kind
= 0;
6642 m
= gfc_match_symbol (&cpte
, 0);
6645 gfc_error ("Expected variable name at %C");
6649 /* Check for an optional array spec. */
6650 m
= gfc_match_array_spec (&as
, true, false);
6651 if (m
== MATCH_ERROR
)
6653 gfc_free_array_spec (as
);
6656 else if (m
== MATCH_NO
)
6658 gfc_free_array_spec (as
);
6662 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6665 gfc_set_sym_referenced (cpte
);
6667 if (cpte
->as
== NULL
)
6669 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6670 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6672 else if (as
!= NULL
)
6674 gfc_error ("Duplicate array spec for Cray pointee at %C");
6675 gfc_free_array_spec (as
);
6681 if (cpte
->as
!= NULL
)
6683 /* Fix array spec. */
6684 m
= gfc_mod_pointee_as (cpte
->as
);
6685 if (m
== MATCH_ERROR
)
6689 /* Point the Pointee at the Pointer. */
6690 cpte
->cp_pointer
= cptr
;
6692 if (gfc_match_char (')') != MATCH_YES
)
6694 gfc_error ("Expected \")\" at %C");
6697 m
= gfc_match_char (',');
6699 done
= true; /* Stop searching for more declarations. */
6703 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6704 || gfc_match_eos () != MATCH_YES
)
6706 gfc_error ("Expected %<,%> or end of statement at %C");
6714 gfc_match_external (void)
6717 gfc_clear_attr (¤t_attr
);
6718 current_attr
.external
= 1;
6720 return attr_decl ();
6725 gfc_match_intent (void)
6729 /* This is not allowed within a BLOCK construct! */
6730 if (gfc_current_state () == COMP_BLOCK
)
6732 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6736 intent
= match_intent_spec ();
6737 if (intent
== INTENT_UNKNOWN
)
6740 gfc_clear_attr (¤t_attr
);
6741 current_attr
.intent
= intent
;
6743 return attr_decl ();
6748 gfc_match_intrinsic (void)
6751 gfc_clear_attr (¤t_attr
);
6752 current_attr
.intrinsic
= 1;
6754 return attr_decl ();
6759 gfc_match_optional (void)
6761 /* This is not allowed within a BLOCK construct! */
6762 if (gfc_current_state () == COMP_BLOCK
)
6764 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6768 gfc_clear_attr (¤t_attr
);
6769 current_attr
.optional
= 1;
6771 return attr_decl ();
6776 gfc_match_pointer (void)
6778 gfc_gobble_whitespace ();
6779 if (gfc_peek_ascii_char () == '(')
6781 if (!flag_cray_pointer
)
6783 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6787 return cray_pointer_decl ();
6791 gfc_clear_attr (¤t_attr
);
6792 current_attr
.pointer
= 1;
6794 return attr_decl ();
6800 gfc_match_allocatable (void)
6802 gfc_clear_attr (¤t_attr
);
6803 current_attr
.allocatable
= 1;
6805 return attr_decl ();
6810 gfc_match_codimension (void)
6812 gfc_clear_attr (¤t_attr
);
6813 current_attr
.codimension
= 1;
6815 return attr_decl ();
6820 gfc_match_contiguous (void)
6822 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6825 gfc_clear_attr (¤t_attr
);
6826 current_attr
.contiguous
= 1;
6828 return attr_decl ();
6833 gfc_match_dimension (void)
6835 gfc_clear_attr (¤t_attr
);
6836 current_attr
.dimension
= 1;
6838 return attr_decl ();
6843 gfc_match_target (void)
6845 gfc_clear_attr (¤t_attr
);
6846 current_attr
.target
= 1;
6848 return attr_decl ();
6852 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6856 access_attr_decl (gfc_statement st
)
6858 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6859 interface_type type
;
6861 gfc_symbol
*sym
, *dt_sym
;
6862 gfc_intrinsic_op op
;
6865 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6870 m
= gfc_match_generic_spec (&type
, name
, &op
);
6873 if (m
== MATCH_ERROR
)
6878 case INTERFACE_NAMELESS
:
6879 case INTERFACE_ABSTRACT
:
6882 case INTERFACE_GENERIC
:
6883 if (gfc_get_symbol (name
, NULL
, &sym
))
6886 if (!gfc_add_access (&sym
->attr
,
6888 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6892 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6893 && !gfc_add_access (&dt_sym
->attr
,
6895 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6901 case INTERFACE_INTRINSIC_OP
:
6902 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6904 gfc_intrinsic_op other_op
;
6906 gfc_current_ns
->operator_access
[op
] =
6907 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6909 /* Handle the case if there is another op with the same
6910 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6911 other_op
= gfc_equivalent_op (op
);
6913 if (other_op
!= INTRINSIC_NONE
)
6914 gfc_current_ns
->operator_access
[other_op
] =
6915 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6920 gfc_error ("Access specification of the %s operator at %C has "
6921 "already been specified", gfc_op2string (op
));
6927 case INTERFACE_USER_OP
:
6928 uop
= gfc_get_uop (name
);
6930 if (uop
->access
== ACCESS_UNKNOWN
)
6932 uop
->access
= (st
== ST_PUBLIC
)
6933 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6937 gfc_error ("Access specification of the .%s. operator at %C "
6938 "has already been specified", sym
->name
);
6945 if (gfc_match_char (',') == MATCH_NO
)
6949 if (gfc_match_eos () != MATCH_YES
)
6954 gfc_syntax_error (st
);
6962 gfc_match_protected (void)
6967 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6969 gfc_error ("PROTECTED at %C only allowed in specification "
6970 "part of a module");
6975 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6978 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6983 if (gfc_match_eos () == MATCH_YES
)
6988 m
= gfc_match_symbol (&sym
, 0);
6992 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7004 if (gfc_match_eos () == MATCH_YES
)
7006 if (gfc_match_char (',') != MATCH_YES
)
7013 gfc_error ("Syntax error in PROTECTED statement at %C");
7018 /* The PRIVATE statement is a bit weird in that it can be an attribute
7019 declaration, but also works as a standalone statement inside of a
7020 type declaration or a module. */
7023 gfc_match_private (gfc_statement
*st
)
7026 if (gfc_match ("private") != MATCH_YES
)
7029 if (gfc_current_state () != COMP_MODULE
7030 && !(gfc_current_state () == COMP_DERIVED
7031 && gfc_state_stack
->previous
7032 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
7033 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7034 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
7035 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
7037 gfc_error ("PRIVATE statement at %C is only allowed in the "
7038 "specification part of a module");
7042 if (gfc_current_state () == COMP_DERIVED
)
7044 if (gfc_match_eos () == MATCH_YES
)
7050 gfc_syntax_error (ST_PRIVATE
);
7054 if (gfc_match_eos () == MATCH_YES
)
7061 return access_attr_decl (ST_PRIVATE
);
7066 gfc_match_public (gfc_statement
*st
)
7069 if (gfc_match ("public") != MATCH_YES
)
7072 if (gfc_current_state () != COMP_MODULE
)
7074 gfc_error ("PUBLIC statement at %C is only allowed in the "
7075 "specification part of a module");
7079 if (gfc_match_eos () == MATCH_YES
)
7086 return access_attr_decl (ST_PUBLIC
);
7090 /* Workhorse for gfc_match_parameter. */
7100 m
= gfc_match_symbol (&sym
, 0);
7102 gfc_error ("Expected variable name at %C in PARAMETER statement");
7107 if (gfc_match_char ('=') == MATCH_NO
)
7109 gfc_error ("Expected = sign in PARAMETER statement at %C");
7113 m
= gfc_match_init_expr (&init
);
7115 gfc_error ("Expected expression at %C in PARAMETER statement");
7119 if (sym
->ts
.type
== BT_UNKNOWN
7120 && !gfc_set_default_type (sym
, 1, NULL
))
7126 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7127 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7135 gfc_error ("Initializing already initialized variable at %C");
7140 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7141 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7144 gfc_free_expr (init
);
7149 /* Match a parameter statement, with the weird syntax that these have. */
7152 gfc_match_parameter (void)
7156 if (gfc_match_char ('(') == MATCH_NO
)
7165 if (gfc_match (" )%t") == MATCH_YES
)
7168 if (gfc_match_char (',') != MATCH_YES
)
7170 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7180 /* Save statements have a special syntax. */
7183 gfc_match_save (void)
7185 char n
[GFC_MAX_SYMBOL_LEN
+1];
7190 if (gfc_match_eos () == MATCH_YES
)
7192 if (gfc_current_ns
->seen_save
)
7194 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7195 "follows previous SAVE statement"))
7199 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7203 if (gfc_current_ns
->save_all
)
7205 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7206 "blanket SAVE statement"))
7214 m
= gfc_match_symbol (&sym
, 0);
7218 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7219 &gfc_current_locus
))
7230 m
= gfc_match (" / %n /", &n
);
7231 if (m
== MATCH_ERROR
)
7236 c
= gfc_get_common (n
, 0);
7239 gfc_current_ns
->seen_save
= 1;
7242 if (gfc_match_eos () == MATCH_YES
)
7244 if (gfc_match_char (',') != MATCH_YES
)
7251 gfc_error ("Syntax error in SAVE statement at %C");
7257 gfc_match_value (void)
7262 /* This is not allowed within a BLOCK construct! */
7263 if (gfc_current_state () == COMP_BLOCK
)
7265 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7269 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7272 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7277 if (gfc_match_eos () == MATCH_YES
)
7282 m
= gfc_match_symbol (&sym
, 0);
7286 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7298 if (gfc_match_eos () == MATCH_YES
)
7300 if (gfc_match_char (',') != MATCH_YES
)
7307 gfc_error ("Syntax error in VALUE statement at %C");
7313 gfc_match_volatile (void)
7318 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7321 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7326 if (gfc_match_eos () == MATCH_YES
)
7331 /* VOLATILE is special because it can be added to host-associated
7332 symbols locally. Except for coarrays. */
7333 m
= gfc_match_symbol (&sym
, 1);
7337 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7338 for variable in a BLOCK which is defined outside of the BLOCK. */
7339 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7341 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7342 "%C, which is use-/host-associated", sym
->name
);
7345 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7357 if (gfc_match_eos () == MATCH_YES
)
7359 if (gfc_match_char (',') != MATCH_YES
)
7366 gfc_error ("Syntax error in VOLATILE statement at %C");
7372 gfc_match_asynchronous (void)
7377 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7380 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7385 if (gfc_match_eos () == MATCH_YES
)
7390 /* ASYNCHRONOUS is special because it can be added to host-associated
7392 m
= gfc_match_symbol (&sym
, 1);
7396 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7408 if (gfc_match_eos () == MATCH_YES
)
7410 if (gfc_match_char (',') != MATCH_YES
)
7417 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7422 /* Match a module procedure statement. Note that we have to modify
7423 symbols in the parent's namespace because the current one was there
7424 to receive symbols that are in an interface's formal argument list. */
7427 gfc_match_modproc (void)
7429 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7433 gfc_namespace
*module_ns
;
7434 gfc_interface
*old_interface_head
, *interface
;
7436 if (gfc_state_stack
->state
!= COMP_INTERFACE
7437 || gfc_state_stack
->previous
== NULL
7438 || current_interface
.type
== INTERFACE_NAMELESS
7439 || current_interface
.type
== INTERFACE_ABSTRACT
)
7441 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7446 module_ns
= gfc_current_ns
->parent
;
7447 for (; module_ns
; module_ns
= module_ns
->parent
)
7448 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7449 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7450 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7451 && !module_ns
->proc_name
->attr
.contained
))
7454 if (module_ns
== NULL
)
7457 /* Store the current state of the interface. We will need it if we
7458 end up with a syntax error and need to recover. */
7459 old_interface_head
= gfc_current_interface_head ();
7461 /* Check if the F2008 optional double colon appears. */
7462 gfc_gobble_whitespace ();
7463 old_locus
= gfc_current_locus
;
7464 if (gfc_match ("::") == MATCH_YES
)
7466 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7467 "MODULE PROCEDURE statement at %L", &old_locus
))
7471 gfc_current_locus
= old_locus
;
7476 old_locus
= gfc_current_locus
;
7478 m
= gfc_match_name (name
);
7484 /* Check for syntax error before starting to add symbols to the
7485 current namespace. */
7486 if (gfc_match_eos () == MATCH_YES
)
7489 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7492 /* Now we're sure the syntax is valid, we process this item
7494 if (gfc_get_symbol (name
, module_ns
, &sym
))
7497 if (sym
->attr
.intrinsic
)
7499 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7500 "PROCEDURE", &old_locus
);
7504 if (sym
->attr
.proc
!= PROC_MODULE
7505 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7508 if (!gfc_add_interface (sym
))
7511 sym
->attr
.mod_proc
= 1;
7512 sym
->declared_at
= old_locus
;
7521 /* Restore the previous state of the interface. */
7522 interface
= gfc_current_interface_head ();
7523 gfc_set_current_interface_head (old_interface_head
);
7525 /* Free the new interfaces. */
7526 while (interface
!= old_interface_head
)
7528 gfc_interface
*i
= interface
->next
;
7533 /* And issue a syntax error. */
7534 gfc_syntax_error (ST_MODULE_PROC
);
7539 /* Check a derived type that is being extended. */
7542 check_extended_derived_type (char *name
)
7544 gfc_symbol
*extended
;
7546 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7548 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7552 extended
= gfc_find_dt_in_generic (extended
);
7557 gfc_error ("Symbol %qs at %C has not been previously defined", name
);
7561 if (extended
->attr
.flavor
!= FL_DERIVED
)
7563 gfc_error ("%qs in EXTENDS expression at %C is not a "
7564 "derived type", name
);
7568 if (extended
->attr
.is_bind_c
)
7570 gfc_error ("%qs cannot be extended at %C because it "
7571 "is BIND(C)", extended
->name
);
7575 if (extended
->attr
.sequence
)
7577 gfc_error ("%qs cannot be extended at %C because it "
7578 "is a SEQUENCE type", extended
->name
);
7586 /* Match the optional attribute specifiers for a type declaration.
7587 Return MATCH_ERROR if an error is encountered in one of the handled
7588 attributes (public, private, bind(c)), MATCH_NO if what's found is
7589 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7590 checking on attribute conflicts needs to be done. */
7593 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7595 /* See if the derived type is marked as private. */
7596 if (gfc_match (" , private") == MATCH_YES
)
7598 if (gfc_current_state () != COMP_MODULE
)
7600 gfc_error ("Derived type at %C can only be PRIVATE in the "
7601 "specification part of a module");
7605 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7608 else if (gfc_match (" , public") == MATCH_YES
)
7610 if (gfc_current_state () != COMP_MODULE
)
7612 gfc_error ("Derived type at %C can only be PUBLIC in the "
7613 "specification part of a module");
7617 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7620 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7622 /* If the type is defined to be bind(c) it then needs to make
7623 sure that all fields are interoperable. This will
7624 need to be a semantic check on the finished derived type.
7625 See 15.2.3 (lines 9-12) of F2003 draft. */
7626 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7629 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7631 else if (gfc_match (" , abstract") == MATCH_YES
)
7633 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7636 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7639 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7641 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7647 /* If we get here, something matched. */
7652 /* Match the beginning of a derived type declaration. If a type name
7653 was the result of a function, then it is possible to have a symbol
7654 already to be known as a derived type yet have no components. */
7657 gfc_match_derived_decl (void)
7659 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7660 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7661 symbol_attribute attr
;
7662 gfc_symbol
*sym
, *gensym
;
7663 gfc_symbol
*extended
;
7665 match is_type_attr_spec
= MATCH_NO
;
7666 bool seen_attr
= false;
7667 gfc_interface
*intr
= NULL
, *head
;
7669 if (gfc_current_state () == COMP_DERIVED
)
7674 gfc_clear_attr (&attr
);
7679 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7680 if (is_type_attr_spec
== MATCH_ERROR
)
7682 if (is_type_attr_spec
== MATCH_YES
)
7684 } while (is_type_attr_spec
== MATCH_YES
);
7686 /* Deal with derived type extensions. The extension attribute has
7687 been added to 'attr' but now the parent type must be found and
7690 extended
= check_extended_derived_type (parent
);
7692 if (parent
[0] && !extended
)
7695 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7697 gfc_error ("Expected :: in TYPE definition at %C");
7701 m
= gfc_match (" %n%t", name
);
7705 /* Make sure the name is not the name of an intrinsic type. */
7706 if (gfc_is_intrinsic_typename (name
))
7708 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7713 if (gfc_get_symbol (name
, NULL
, &gensym
))
7716 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7718 gfc_error ("Derived type name %qs at %C already has a basic type "
7719 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7723 if (!gensym
->attr
.generic
7724 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7727 if (!gensym
->attr
.function
7728 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7731 sym
= gfc_find_dt_in_generic (gensym
);
7733 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7735 gfc_error ("Derived type definition of %qs at %C has already been "
7736 "defined", sym
->name
);
7742 /* Use upper case to save the actual derived-type symbol. */
7743 gfc_get_symbol (gfc_get_string ("%c%s",
7744 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7745 &gensym
->name
[1]), NULL
, &sym
);
7746 sym
->name
= gfc_get_string (gensym
->name
);
7747 head
= gensym
->generic
;
7748 intr
= gfc_get_interface ();
7750 intr
->where
= gfc_current_locus
;
7751 intr
->sym
->declared_at
= gfc_current_locus
;
7753 gensym
->generic
= intr
;
7754 gensym
->attr
.if_source
= IFSRC_DECL
;
7757 /* The symbol may already have the derived attribute without the
7758 components. The ways this can happen is via a function
7759 definition, an INTRINSIC statement or a subtype in another
7760 derived type that is a pointer. The first part of the AND clause
7761 is true if the symbol is not the return value of a function. */
7762 if (sym
->attr
.flavor
!= FL_DERIVED
7763 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7766 if (attr
.access
!= ACCESS_UNKNOWN
7767 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7769 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7770 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7771 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7775 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7776 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7777 gensym
->attr
.access
= sym
->attr
.access
;
7779 /* See if the derived type was labeled as bind(c). */
7780 if (attr
.is_bind_c
!= 0)
7781 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7783 /* Construct the f2k_derived namespace if it is not yet there. */
7784 if (!sym
->f2k_derived
)
7785 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7787 if (extended
&& !sym
->components
)
7792 /* Add the extended derived type as the first component. */
7793 gfc_add_component (sym
, parent
, &p
);
7795 gfc_set_sym_referenced (extended
);
7797 p
->ts
.type
= BT_DERIVED
;
7798 p
->ts
.u
.derived
= extended
;
7799 p
->initializer
= gfc_default_initializer (&p
->ts
);
7801 /* Set extension level. */
7802 if (extended
->attr
.extension
== 255)
7804 /* Since the extension field is 8 bit wide, we can only have
7805 up to 255 extension levels. */
7806 gfc_error ("Maximum extension level reached with type %qs at %L",
7807 extended
->name
, &extended
->declared_at
);
7810 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7812 /* Provide the links between the extended type and its extension. */
7813 if (!extended
->f2k_derived
)
7814 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7815 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7819 if (!sym
->hash_value
)
7820 /* Set the hash for the compound name for this type. */
7821 sym
->hash_value
= gfc_hash_value (sym
);
7823 /* Take over the ABSTRACT attribute. */
7824 sym
->attr
.abstract
= attr
.abstract
;
7826 gfc_new_block
= sym
;
7832 /* Cray Pointees can be declared as:
7833 pointer (ipt, a (n,m,...,*)) */
7836 gfc_mod_pointee_as (gfc_array_spec
*as
)
7838 as
->cray_pointee
= true; /* This will be useful to know later. */
7839 if (as
->type
== AS_ASSUMED_SIZE
)
7840 as
->cp_was_assumed
= true;
7841 else if (as
->type
== AS_ASSUMED_SHAPE
)
7843 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7850 /* Match the enum definition statement, here we are trying to match
7851 the first line of enum definition statement.
7852 Returns MATCH_YES if match is found. */
7855 gfc_match_enum (void)
7859 m
= gfc_match_eos ();
7863 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7870 /* Returns an initializer whose value is one higher than the value of the
7871 LAST_INITIALIZER argument. If the argument is NULL, the
7872 initializers value will be set to zero. The initializer's kind
7873 will be set to gfc_c_int_kind.
7875 If -fshort-enums is given, the appropriate kind will be selected
7876 later after all enumerators have been parsed. A warning is issued
7877 here if an initializer exceeds gfc_c_int_kind. */
7880 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7883 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7885 mpz_init (result
->value
.integer
);
7887 if (last_initializer
!= NULL
)
7889 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7890 result
->where
= last_initializer
->where
;
7892 if (gfc_check_integer_range (result
->value
.integer
,
7893 gfc_c_int_kind
) != ARITH_OK
)
7895 gfc_error ("Enumerator exceeds the C integer type at %C");
7901 /* Control comes here, if it's the very first enumerator and no
7902 initializer has been given. It will be initialized to zero. */
7903 mpz_set_si (result
->value
.integer
, 0);
7910 /* Match a variable name with an optional initializer. When this
7911 subroutine is called, a variable is expected to be parsed next.
7912 Depending on what is happening at the moment, updates either the
7913 symbol table or the current interface. */
7916 enumerator_decl (void)
7918 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7919 gfc_expr
*initializer
;
7920 gfc_array_spec
*as
= NULL
;
7928 old_locus
= gfc_current_locus
;
7930 /* When we get here, we've just matched a list of attributes and
7931 maybe a type and a double colon. The next thing we expect to see
7932 is the name of the symbol. */
7933 m
= gfc_match_name (name
);
7937 var_locus
= gfc_current_locus
;
7939 /* OK, we've successfully matched the declaration. Now put the
7940 symbol in the current namespace. If we fail to create the symbol,
7942 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7948 /* The double colon must be present in order to have initializers.
7949 Otherwise the statement is ambiguous with an assignment statement. */
7952 if (gfc_match_char ('=') == MATCH_YES
)
7954 m
= gfc_match_init_expr (&initializer
);
7957 gfc_error ("Expected an initialization expression at %C");
7966 /* If we do not have an initializer, the initialization value of the
7967 previous enumerator (stored in last_initializer) is incremented
7968 by 1 and is used to initialize the current enumerator. */
7969 if (initializer
== NULL
)
7970 initializer
= enum_initializer (last_initializer
, old_locus
);
7972 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7974 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7980 /* Store this current initializer, for the next enumerator variable
7981 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7982 use last_initializer below. */
7983 last_initializer
= initializer
;
7984 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7986 /* Maintain enumerator history. */
7987 gfc_find_symbol (name
, NULL
, 0, &sym
);
7988 create_enum_history (sym
, last_initializer
);
7990 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7993 /* Free stuff up and return. */
7994 gfc_free_expr (initializer
);
8000 /* Match the enumerator definition statement. */
8003 gfc_match_enumerator_def (void)
8008 gfc_clear_ts (¤t_ts
);
8010 m
= gfc_match (" enumerator");
8014 m
= gfc_match (" :: ");
8015 if (m
== MATCH_ERROR
)
8018 colon_seen
= (m
== MATCH_YES
);
8020 if (gfc_current_state () != COMP_ENUM
)
8022 gfc_error ("ENUM definition statement expected before %C");
8023 gfc_free_enum_history ();
8027 (¤t_ts
)->type
= BT_INTEGER
;
8028 (¤t_ts
)->kind
= gfc_c_int_kind
;
8030 gfc_clear_attr (¤t_attr
);
8031 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
8040 m
= enumerator_decl ();
8041 if (m
== MATCH_ERROR
)
8043 gfc_free_enum_history ();
8049 if (gfc_match_eos () == MATCH_YES
)
8051 if (gfc_match_char (',') != MATCH_YES
)
8055 if (gfc_current_state () == COMP_ENUM
)
8057 gfc_free_enum_history ();
8058 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8063 gfc_free_array_spec (current_as
);
8070 /* Match binding attributes. */
8073 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
8075 bool found_passing
= false;
8076 bool seen_ptr
= false;
8077 match m
= MATCH_YES
;
8079 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8080 this case the defaults are in there. */
8081 ba
->access
= ACCESS_UNKNOWN
;
8082 ba
->pass_arg
= NULL
;
8083 ba
->pass_arg_num
= 0;
8085 ba
->non_overridable
= 0;
8089 /* If we find a comma, we believe there are binding attributes. */
8090 m
= gfc_match_char (',');
8096 /* Access specifier. */
8098 m
= gfc_match (" public");
8099 if (m
== MATCH_ERROR
)
8103 if (ba
->access
!= ACCESS_UNKNOWN
)
8105 gfc_error ("Duplicate access-specifier at %C");
8109 ba
->access
= ACCESS_PUBLIC
;
8113 m
= gfc_match (" private");
8114 if (m
== MATCH_ERROR
)
8118 if (ba
->access
!= ACCESS_UNKNOWN
)
8120 gfc_error ("Duplicate access-specifier at %C");
8124 ba
->access
= ACCESS_PRIVATE
;
8128 /* If inside GENERIC, the following is not allowed. */
8133 m
= gfc_match (" nopass");
8134 if (m
== MATCH_ERROR
)
8140 gfc_error ("Binding attributes already specify passing,"
8141 " illegal NOPASS at %C");
8145 found_passing
= true;
8150 /* PASS possibly including argument. */
8151 m
= gfc_match (" pass");
8152 if (m
== MATCH_ERROR
)
8156 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8160 gfc_error ("Binding attributes already specify passing,"
8161 " illegal PASS at %C");
8165 m
= gfc_match (" ( %n )", arg
);
8166 if (m
== MATCH_ERROR
)
8169 ba
->pass_arg
= gfc_get_string (arg
);
8170 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8172 found_passing
= true;
8180 m
= gfc_match (" pointer");
8181 if (m
== MATCH_ERROR
)
8187 gfc_error ("Duplicate POINTER attribute at %C");
8197 /* NON_OVERRIDABLE flag. */
8198 m
= gfc_match (" non_overridable");
8199 if (m
== MATCH_ERROR
)
8203 if (ba
->non_overridable
)
8205 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8209 ba
->non_overridable
= 1;
8213 /* DEFERRED flag. */
8214 m
= gfc_match (" deferred");
8215 if (m
== MATCH_ERROR
)
8221 gfc_error ("Duplicate DEFERRED at %C");
8232 /* Nothing matching found. */
8234 gfc_error ("Expected access-specifier at %C");
8236 gfc_error ("Expected binding attribute at %C");
8239 while (gfc_match_char (',') == MATCH_YES
);
8241 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8242 if (ba
->non_overridable
&& ba
->deferred
)
8244 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8251 if (ba
->access
== ACCESS_UNKNOWN
)
8252 ba
->access
= gfc_typebound_default_access
;
8254 if (ppc
&& !seen_ptr
)
8256 gfc_error ("POINTER attribute is required for procedure pointer component"
8268 /* Match a PROCEDURE specific binding inside a derived type. */
8271 match_procedure_in_type (void)
8273 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8274 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8275 char* target
= NULL
, *ifc
= NULL
;
8276 gfc_typebound_proc tb
;
8285 /* Check current state. */
8286 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8287 block
= gfc_state_stack
->previous
->sym
;
8290 /* Try to match PROCEDURE(interface). */
8291 if (gfc_match (" (") == MATCH_YES
)
8293 m
= gfc_match_name (target_buf
);
8294 if (m
== MATCH_ERROR
)
8298 gfc_error ("Interface-name expected after %<(%> at %C");
8302 if (gfc_match (" )") != MATCH_YES
)
8304 gfc_error ("%<)%> expected at %C");
8311 /* Construct the data structure. */
8312 memset (&tb
, 0, sizeof (tb
));
8313 tb
.where
= gfc_current_locus
;
8315 /* Match binding attributes. */
8316 m
= match_binding_attributes (&tb
, false, false);
8317 if (m
== MATCH_ERROR
)
8319 seen_attrs
= (m
== MATCH_YES
);
8321 /* Check that attribute DEFERRED is given if an interface is specified. */
8322 if (tb
.deferred
&& !ifc
)
8324 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8327 if (ifc
&& !tb
.deferred
)
8329 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8333 /* Match the colons. */
8334 m
= gfc_match (" ::");
8335 if (m
== MATCH_ERROR
)
8337 seen_colons
= (m
== MATCH_YES
);
8338 if (seen_attrs
&& !seen_colons
)
8340 gfc_error ("Expected %<::%> after binding-attributes at %C");
8344 /* Match the binding names. */
8347 m
= gfc_match_name (name
);
8348 if (m
== MATCH_ERROR
)
8352 gfc_error ("Expected binding name at %C");
8356 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8359 /* Try to match the '=> target', if it's there. */
8361 m
= gfc_match (" =>");
8362 if (m
== MATCH_ERROR
)
8368 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8374 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8379 m
= gfc_match_name (target_buf
);
8380 if (m
== MATCH_ERROR
)
8384 gfc_error ("Expected binding target after %<=>%> at %C");
8387 target
= target_buf
;
8390 /* If no target was found, it has the same name as the binding. */
8394 /* Get the namespace to insert the symbols into. */
8395 ns
= block
->f2k_derived
;
8398 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8399 if (tb
.deferred
&& !block
->attr
.abstract
)
8401 gfc_error ("Type %qs containing DEFERRED binding at %C "
8402 "is not ABSTRACT", block
->name
);
8406 /* See if we already have a binding with this name in the symtree which
8407 would be an error. If a GENERIC already targeted this binding, it may
8408 be already there but then typebound is still NULL. */
8409 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8410 if (stree
&& stree
->n
.tb
)
8412 gfc_error ("There is already a procedure with binding name %qs for "
8413 "the derived type %qs at %C", name
, block
->name
);
8417 /* Insert it and set attributes. */
8421 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8424 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8426 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8429 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8431 if (gfc_match_eos () == MATCH_YES
)
8433 if (gfc_match_char (',') != MATCH_YES
)
8438 gfc_error ("Syntax error in PROCEDURE statement at %C");
8443 /* Match a GENERIC procedure binding inside a derived type. */
8446 gfc_match_generic (void)
8448 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8449 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8451 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8452 gfc_typebound_proc
* tb
;
8454 interface_type op_type
;
8455 gfc_intrinsic_op op
;
8458 /* Check current state. */
8459 if (gfc_current_state () == COMP_DERIVED
)
8461 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8464 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8466 block
= gfc_state_stack
->previous
->sym
;
8467 ns
= block
->f2k_derived
;
8468 gcc_assert (block
&& ns
);
8470 memset (&tbattr
, 0, sizeof (tbattr
));
8471 tbattr
.where
= gfc_current_locus
;
8473 /* See if we get an access-specifier. */
8474 m
= match_binding_attributes (&tbattr
, true, false);
8475 if (m
== MATCH_ERROR
)
8478 /* Now the colons, those are required. */
8479 if (gfc_match (" ::") != MATCH_YES
)
8481 gfc_error ("Expected %<::%> at %C");
8485 /* Match the binding name; depending on type (operator / generic) format
8486 it for future error messages into bind_name. */
8488 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8489 if (m
== MATCH_ERROR
)
8493 gfc_error ("Expected generic name or operator descriptor at %C");
8499 case INTERFACE_GENERIC
:
8500 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8503 case INTERFACE_USER_OP
:
8504 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8507 case INTERFACE_INTRINSIC_OP
:
8508 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8509 gfc_op2string (op
));
8516 /* Match the required =>. */
8517 if (gfc_match (" =>") != MATCH_YES
)
8519 gfc_error ("Expected %<=>%> at %C");
8523 /* Try to find existing GENERIC binding with this name / for this operator;
8524 if there is something, check that it is another GENERIC and then extend
8525 it rather than building a new node. Otherwise, create it and put it
8526 at the right position. */
8530 case INTERFACE_USER_OP
:
8531 case INTERFACE_GENERIC
:
8533 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8536 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8548 case INTERFACE_INTRINSIC_OP
:
8558 if (!tb
->is_generic
)
8560 gcc_assert (op_type
== INTERFACE_GENERIC
);
8561 gfc_error ("There's already a non-generic procedure with binding name"
8562 " %qs for the derived type %qs at %C",
8563 bind_name
, block
->name
);
8567 if (tb
->access
!= tbattr
.access
)
8569 gfc_error ("Binding at %C must have the same access as already"
8570 " defined binding %qs", bind_name
);
8576 tb
= gfc_get_typebound_proc (NULL
);
8577 tb
->where
= gfc_current_locus
;
8578 tb
->access
= tbattr
.access
;
8580 tb
->u
.generic
= NULL
;
8584 case INTERFACE_GENERIC
:
8585 case INTERFACE_USER_OP
:
8587 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8590 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8598 case INTERFACE_INTRINSIC_OP
:
8607 /* Now, match all following names as specific targets. */
8610 gfc_symtree
* target_st
;
8611 gfc_tbp_generic
* target
;
8613 m
= gfc_match_name (name
);
8614 if (m
== MATCH_ERROR
)
8618 gfc_error ("Expected specific binding name at %C");
8622 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8624 /* See if this is a duplicate specification. */
8625 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8626 if (target_st
== target
->specific_st
)
8628 gfc_error ("%qs already defined as specific binding for the"
8629 " generic %qs at %C", name
, bind_name
);
8633 target
= gfc_get_tbp_generic ();
8634 target
->specific_st
= target_st
;
8635 target
->specific
= NULL
;
8636 target
->next
= tb
->u
.generic
;
8637 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8638 || (op_type
== INTERFACE_INTRINSIC_OP
));
8639 tb
->u
.generic
= target
;
8641 while (gfc_match (" ,") == MATCH_YES
);
8643 /* Here should be the end. */
8644 if (gfc_match_eos () != MATCH_YES
)
8646 gfc_error ("Junk after GENERIC binding at %C");
8657 /* Match a FINAL declaration inside a derived type. */
8660 gfc_match_final_decl (void)
8662 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8665 gfc_namespace
* module_ns
;
8669 if (gfc_current_form
== FORM_FREE
)
8671 char c
= gfc_peek_ascii_char ();
8672 if (!gfc_is_whitespace (c
) && c
!= ':')
8676 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8678 if (gfc_current_form
== FORM_FIXED
)
8681 gfc_error ("FINAL declaration at %C must be inside a derived type "
8682 "CONTAINS section");
8686 block
= gfc_state_stack
->previous
->sym
;
8689 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8690 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8692 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8693 " specification part of a MODULE");
8697 module_ns
= gfc_current_ns
;
8698 gcc_assert (module_ns
);
8699 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8701 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8702 if (gfc_match (" ::") == MATCH_ERROR
)
8705 /* Match the sequence of procedure names. */
8712 if (first
&& gfc_match_eos () == MATCH_YES
)
8714 gfc_error ("Empty FINAL at %C");
8718 m
= gfc_match_name (name
);
8721 gfc_error ("Expected module procedure name at %C");
8724 else if (m
!= MATCH_YES
)
8727 if (gfc_match_eos () == MATCH_YES
)
8729 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8731 gfc_error ("Expected %<,%> at %C");
8735 if (gfc_get_symbol (name
, module_ns
, &sym
))
8737 gfc_error ("Unknown procedure name %qs at %C", name
);
8741 /* Mark the symbol as module procedure. */
8742 if (sym
->attr
.proc
!= PROC_MODULE
8743 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8746 /* Check if we already have this symbol in the list, this is an error. */
8747 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8748 if (f
->proc_sym
== sym
)
8750 gfc_error ("%qs at %C is already defined as FINAL procedure!",
8755 /* Add this symbol to the list of finalizers. */
8756 gcc_assert (block
->f2k_derived
);
8758 f
= XCNEW (gfc_finalizer
);
8760 f
->proc_tree
= NULL
;
8761 f
->where
= gfc_current_locus
;
8762 f
->next
= block
->f2k_derived
->finalizers
;
8763 block
->f2k_derived
->finalizers
= f
;
8773 const ext_attr_t ext_attr_list
[] = {
8774 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8775 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8776 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8777 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8778 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8779 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8780 { NULL
, EXT_ATTR_LAST
, NULL
}
8783 /* Match a !GCC$ ATTRIBUTES statement of the form:
8784 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8785 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8787 TODO: We should support all GCC attributes using the same syntax for
8788 the attribute list, i.e. the list in C
8789 __attributes(( attribute-list ))
8791 !GCC$ ATTRIBUTES attribute-list ::
8792 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8795 As there is absolutely no risk of confusion, we should never return
8798 gfc_match_gcc_attributes (void)
8800 symbol_attribute attr
;
8801 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8806 gfc_clear_attr (&attr
);
8811 if (gfc_match_name (name
) != MATCH_YES
)
8814 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8815 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8818 if (id
== EXT_ATTR_LAST
)
8820 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8824 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8827 gfc_gobble_whitespace ();
8828 ch
= gfc_next_ascii_char ();
8831 /* This is the successful exit condition for the loop. */
8832 if (gfc_next_ascii_char () == ':')
8842 if (gfc_match_eos () == MATCH_YES
)
8847 m
= gfc_match_name (name
);
8851 if (find_special (name
, &sym
, true))
8854 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8856 if (gfc_match_eos () == MATCH_YES
)
8859 if (gfc_match_char (',') != MATCH_YES
)
8866 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");