1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "constructor.h"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector
;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts
;
49 static symbol_attribute current_attr
;
50 static gfc_array_spec
*current_as
;
51 static int colon_seen
;
53 /* The current binding label (if any). */
54 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line
;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals
= 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr
*last_initializer
;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr
*initializer
;
74 struct enumerator_history
*next
;
78 /* Header of enum history chain. */
80 static enumerator_history
*enum_history
= NULL
;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history
*max_enum
= NULL
;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol
*gfc_new_block
;
90 bool gfc_matching_function
;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data
= false;
98 gfc_in_match_data (void)
100 return in_match_data
;
104 set_in_match_data (bool set_value
)
106 in_match_data
= set_value
;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable
*p
)
114 gfc_data_variable
*q
;
119 gfc_free_expr (p
->expr
);
120 gfc_free_iterator (&p
->iter
, 0);
121 free_variable (p
->list
);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value
*p
)
137 mpz_clear (p
->repeat
);
138 gfc_free_expr (p
->expr
);
144 /* Free a list of gfc_data structures. */
147 gfc_free_data (gfc_data
*p
)
154 free_variable (p
->var
);
155 free_value (p
->value
);
161 /* Free all data in a namespace. */
164 gfc_free_data_all (gfc_namespace
*ns
)
177 static match
var_element (gfc_data_variable
*);
179 /* Match a list of variables terminated by an iterator and a right
183 var_list (gfc_data_variable
*parent
)
185 gfc_data_variable
*tail
, var
;
188 m
= var_element (&var
);
189 if (m
== MATCH_ERROR
)
194 tail
= gfc_get_data_variable ();
201 if (gfc_match_char (',') != MATCH_YES
)
204 m
= gfc_match_iterator (&parent
->iter
, 1);
207 if (m
== MATCH_ERROR
)
210 m
= var_element (&var
);
211 if (m
== MATCH_ERROR
)
216 tail
->next
= gfc_get_data_variable ();
222 if (gfc_match_char (')') != MATCH_YES
)
227 gfc_syntax_error (ST_DATA
);
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
236 var_element (gfc_data_variable
*new_var
)
241 memset (new_var
, 0, sizeof (gfc_data_variable
));
243 if (gfc_match_char ('(') == MATCH_YES
)
244 return var_list (new_var
);
246 m
= gfc_match_variable (&new_var
->expr
, 0);
250 sym
= new_var
->expr
->symtree
->n
.sym
;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym
, gfc_current_ns
,
254 false, gfc_current_locus
) == FAILURE
)
257 if (!sym
->attr
.function
&& gfc_current_ns
->parent
258 && gfc_current_ns
->parent
== sym
->ns
)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym
->name
);
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym
->attr
.in_common
267 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym
->name
) == FAILURE
)
272 if (gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
) == FAILURE
)
279 /* Match the top-level list of data variables. */
282 top_var_list (gfc_data
*d
)
284 gfc_data_variable var
, *tail
, *new_var
;
291 m
= var_element (&var
);
294 if (m
== MATCH_ERROR
)
297 new_var
= gfc_get_data_variable ();
303 tail
->next
= new_var
;
307 if (gfc_match_char ('/') == MATCH_YES
)
309 if (gfc_match_char (',') != MATCH_YES
)
316 gfc_syntax_error (ST_DATA
);
317 gfc_free_data_all (gfc_current_ns
);
323 match_data_constant (gfc_expr
**result
)
325 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
331 m
= gfc_match_literal_constant (&expr
, 1);
338 if (m
== MATCH_ERROR
)
341 m
= gfc_match_null (result
);
345 old_loc
= gfc_current_locus
;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m
= gfc_match_rvalue (result
);
350 if (m
== MATCH_ERROR
)
353 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
355 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
360 gfc_current_locus
= old_loc
;
362 m
= gfc_match_name (name
);
366 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
370 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
376 else if (sym
->attr
.flavor
== FL_DERIVED
)
377 return gfc_match_structure_constructor (sym
, result
, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym
->value
->expr_type
== EXPR_ARRAY
)
382 gfc_current_locus
= old_loc
;
384 m
= gfc_match_init_expr (result
);
385 if (m
== MATCH_ERROR
)
390 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
393 if ((*result
)->expr_type
== EXPR_CONSTANT
)
397 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
403 *result
= gfc_copy_expr (sym
->value
);
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
412 top_val_list (gfc_data
*data
)
414 gfc_data_value
*new_val
, *tail
;
422 m
= match_data_constant (&expr
);
425 if (m
== MATCH_ERROR
)
428 new_val
= gfc_get_data_value ();
429 mpz_init (new_val
->repeat
);
432 data
->value
= new_val
;
434 tail
->next
= new_val
;
438 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
441 mpz_set_ui (tail
->repeat
, 1);
445 if (expr
->ts
.type
== BT_INTEGER
)
446 mpz_set (tail
->repeat
, expr
->value
.integer
);
447 gfc_free_expr (expr
);
449 m
= match_data_constant (&tail
->expr
);
452 if (m
== MATCH_ERROR
)
456 if (gfc_match_char ('/') == MATCH_YES
)
458 if (gfc_match_char (',') == MATCH_NO
)
465 gfc_syntax_error (ST_DATA
);
466 gfc_free_data_all (gfc_current_ns
);
471 /* Matches an old style initialization. */
474 match_old_style_init (const char *name
)
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name
, NULL
, 0, &st
);
485 newdata
= gfc_get_data ();
486 newdata
->var
= gfc_get_data_variable ();
487 newdata
->var
->expr
= gfc_get_variable_expr (st
);
488 newdata
->where
= gfc_current_locus
;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m
= top_val_list (newdata
);
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
505 if (gfc_implicit_pure (NULL
))
506 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
508 /* Mark the variable as having appeared in a data statement. */
509 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
515 /* Chain in namespace list of DATA initializers. */
516 newdata
->next
= gfc_current_ns
->data
;
517 gfc_current_ns
->data
= newdata
;
523 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
524 we are matching a DATA statement and are therefore issuing an error
525 if we encounter something unexpected, if not, we're trying to match
526 an old-style initialization expression of the form INTEGER I /2/. */
529 gfc_match_data (void)
534 set_in_match_data (true);
538 new_data
= gfc_get_data ();
539 new_data
->where
= gfc_current_locus
;
541 m
= top_var_list (new_data
);
545 m
= top_val_list (new_data
);
549 new_data
->next
= gfc_current_ns
->data
;
550 gfc_current_ns
->data
= new_data
;
552 if (gfc_match_eos () == MATCH_YES
)
555 gfc_match_char (','); /* Optional comma */
558 set_in_match_data (false);
562 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
566 if (gfc_implicit_pure (NULL
))
567 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
572 set_in_match_data (false);
573 gfc_free_data (new_data
);
578 /************************ Declaration statements *********************/
581 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
584 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
588 if (to
->rank
== 0 && from
->rank
> 0)
590 to
->rank
= from
->rank
;
591 to
->type
= from
->type
;
592 to
->cray_pointee
= from
->cray_pointee
;
593 to
->cp_was_assumed
= from
->cp_was_assumed
;
595 for (i
= 0; i
< to
->corank
; i
++)
597 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
598 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
600 for (i
= 0; i
< from
->rank
; i
++)
604 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
605 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
609 to
->lower
[i
] = from
->lower
[i
];
610 to
->upper
[i
] = from
->upper
[i
];
614 else if (to
->corank
== 0 && from
->corank
> 0)
616 to
->corank
= from
->corank
;
617 to
->cotype
= from
->cotype
;
619 for (i
= 0; i
< from
->corank
; i
++)
623 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
624 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
628 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
629 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
636 /* Match an intent specification. Since this can only happen after an
637 INTENT word, a legal intent-spec must follow. */
640 match_intent_spec (void)
643 if (gfc_match (" ( in out )") == MATCH_YES
)
645 if (gfc_match (" ( in )") == MATCH_YES
)
647 if (gfc_match (" ( out )") == MATCH_YES
)
650 gfc_error ("Bad INTENT specification at %C");
651 return INTENT_UNKNOWN
;
655 /* Matches a character length specification, which is either a
656 specification expression, '*', or ':'. */
659 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
666 if (gfc_match_char ('*') == MATCH_YES
)
669 if (gfc_match_char (':') == MATCH_YES
)
671 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: deferred type "
672 "parameter at %C") == FAILURE
)
680 m
= gfc_match_expr (expr
);
683 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
686 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
688 if ((*expr
)->value
.function
.actual
689 && (*expr
)->value
.function
.actual
->expr
->symtree
)
692 e
= (*expr
)->value
.function
.actual
->expr
;
693 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
694 && e
->expr_type
== EXPR_VARIABLE
)
696 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
698 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
699 && e
->symtree
->n
.sym
->ts
.u
.cl
700 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
708 gfc_error ("Conflict in attributes of function argument at %C");
713 /* A character length is a '*' followed by a literal integer or a
714 char_len_param_value in parenthesis. */
717 match_char_length (gfc_expr
**expr
, bool *deferred
)
723 m
= gfc_match_char ('*');
727 m
= gfc_match_small_literal_int (&length
, NULL
);
728 if (m
== MATCH_ERROR
)
733 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
734 "Old-style character length at %C") == FAILURE
)
736 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
740 if (gfc_match_char ('(') == MATCH_NO
)
743 m
= char_len_param_value (expr
, deferred
);
744 if (m
!= MATCH_YES
&& gfc_matching_function
)
750 if (m
== MATCH_ERROR
)
755 if (gfc_match_char (')') == MATCH_NO
)
757 gfc_free_expr (*expr
);
765 gfc_error ("Syntax error in character length specification at %C");
770 /* Special subroutine for finding a symbol. Check if the name is found
771 in the current name space. If not, and we're compiling a function or
772 subroutine and the parent compilation unit is an interface, then check
773 to see if the name we've been given is the name of the interface
774 (located in another namespace). */
777 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
783 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
786 *result
= st
? st
->n
.sym
: NULL
;
790 if (gfc_current_state () != COMP_SUBROUTINE
791 && gfc_current_state () != COMP_FUNCTION
)
794 s
= gfc_state_stack
->previous
;
798 if (s
->state
!= COMP_INTERFACE
)
801 goto end
; /* Nameless interface. */
803 if (strcmp (name
, s
->sym
->name
) == 0)
814 /* Special subroutine for getting a symbol node associated with a
815 procedure name, used in SUBROUTINE and FUNCTION statements. The
816 symbol is created in the parent using with symtree node in the
817 child unit pointing to the symbol. If the current namespace has no
818 parent, then the symbol is just created in the current unit. */
821 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
827 /* Module functions have to be left in their own namespace because
828 they have potentially (almost certainly!) already been referenced.
829 In this sense, they are rather like external functions. This is
830 fixed up in resolve.c(resolve_entries), where the symbol name-
831 space is set to point to the master function, so that the fake
832 result mechanism can work. */
833 if (module_fcn_entry
)
835 /* Present if entry is declared to be a module procedure. */
836 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
839 rc
= gfc_get_symbol (name
, NULL
, result
);
840 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
841 && (*result
)->ts
.type
== BT_UNKNOWN
842 && sym
->attr
.flavor
== FL_UNKNOWN
)
843 /* Pick up the typespec for the entry, if declared in the function
844 body. Note that this symbol is FL_UNKNOWN because it will
845 only have appeared in a type declaration. The local symtree
846 is set to point to the module symbol and a unique symtree
847 to the local version. This latter ensures a correct clearing
850 /* If the ENTRY proceeds its specification, we need to ensure
851 that this does not raise a "has no IMPLICIT type" error. */
852 if (sym
->ts
.type
== BT_UNKNOWN
)
853 sym
->attr
.untyped
= 1;
855 (*result
)->ts
= sym
->ts
;
857 /* Put the symbol in the procedure namespace so that, should
858 the ENTRY precede its specification, the specification
860 (*result
)->ns
= gfc_current_ns
;
862 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
864 st
= gfc_get_unique_symtree (gfc_current_ns
);
869 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
875 gfc_current_ns
->refs
++;
877 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
879 /* Trap another encompassed procedure with the same name. All
880 these conditions are necessary to avoid picking up an entry
881 whose name clashes with that of the encompassing procedure;
882 this is handled using gsymbols to register unique,globally
884 if (sym
->attr
.flavor
!= 0
885 && sym
->attr
.proc
!= 0
886 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
887 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
888 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
889 name
, &sym
->declared_at
);
891 /* Trap a procedure with a name the same as interface in the
892 encompassing scope. */
893 if (sym
->attr
.generic
!= 0
894 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
895 && !sym
->attr
.mod_proc
)
896 gfc_error_now ("Name '%s' at %C is already defined"
897 " as a generic interface at %L",
898 name
, &sym
->declared_at
);
900 /* Trap declarations of attributes in encompassing scope. The
901 signature for this is that ts.kind is set. Legitimate
902 references only set ts.type. */
903 if (sym
->ts
.kind
!= 0
904 && !sym
->attr
.implicit_type
905 && sym
->attr
.proc
== 0
906 && gfc_current_ns
->parent
!= NULL
907 && sym
->attr
.access
== 0
908 && !module_fcn_entry
)
909 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
910 "and must not have attributes declared at %L",
911 name
, &sym
->declared_at
);
914 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
917 /* Module function entries will already have a symtree in
918 the current namespace but will need one at module level. */
919 if (module_fcn_entry
)
921 /* Present if entry is declared to be a module procedure. */
922 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
924 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
927 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
932 /* See if the procedure should be a module procedure. */
934 if (((sym
->ns
->proc_name
!= NULL
935 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
936 && sym
->attr
.proc
!= PROC_MODULE
)
937 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
938 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
939 sym
->name
, NULL
) == FAILURE
)
946 /* Verify that the given symbol representing a parameter is C
947 interoperable, by checking to see if it was marked as such after
948 its declaration. If the given symbol is not interoperable, a
949 warning is reported, thus removing the need to return the status to
950 the calling function. The standard does not require the user use
951 one of the iso_c_binding named constants to declare an
952 interoperable parameter, but we can't be sure if the param is C
953 interop or not if the user doesn't. For example, integer(4) may be
954 legal Fortran, but doesn't have meaning in C. It may interop with
955 a number of the C types, which causes a problem because the
956 compiler can't know which one. This code is almost certainly not
957 portable, and the user will get what they deserve if the C type
958 across platforms isn't always interoperable with integer(4). If
959 the user had used something like integer(c_int) or integer(c_long),
960 the compiler could have automatically handled the varying sizes
964 verify_c_interop_param (gfc_symbol
*sym
)
966 int is_c_interop
= 0;
967 gfc_try retval
= SUCCESS
;
969 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
970 Don't repeat the checks here. */
971 if (sym
->attr
.implicit_type
)
974 /* For subroutines or functions that are passed to a BIND(C) procedure,
975 they're interoperable if they're BIND(C) and their params are all
977 if (sym
->attr
.flavor
== FL_PROCEDURE
)
979 if (sym
->attr
.is_bind_c
== 0)
981 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
982 "attribute to be C interoperable", sym
->name
,
983 &(sym
->declared_at
));
989 if (sym
->attr
.is_c_interop
== 1)
990 /* We've already checked this procedure; don't check it again. */
993 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
998 /* See if we've stored a reference to a procedure that owns sym. */
999 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1001 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1004 (verify_c_interop (&(sym
->ts
))
1005 == SUCCESS
? 1 : 0);
1007 if (is_c_interop
!= 1)
1009 /* Make personalized messages to give better feedback. */
1010 if (sym
->ts
.type
== BT_DERIVED
)
1011 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1012 "procedure '%s' but is not C interoperable "
1013 "because derived type '%s' is not C interoperable",
1014 sym
->name
, &(sym
->declared_at
),
1015 sym
->ns
->proc_name
->name
,
1016 sym
->ts
.u
.derived
->name
);
1018 gfc_warning ("Variable '%s' at %L is a parameter to the "
1019 "BIND(C) procedure '%s' but may not be C "
1021 sym
->name
, &(sym
->declared_at
),
1022 sym
->ns
->proc_name
->name
);
1025 /* Character strings are only C interoperable if they have a
1027 if (sym
->ts
.type
== BT_CHARACTER
)
1029 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1030 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1031 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1033 gfc_error ("Character argument '%s' at %L "
1034 "must be length 1 because "
1035 "procedure '%s' is BIND(C)",
1036 sym
->name
, &sym
->declared_at
,
1037 sym
->ns
->proc_name
->name
);
1042 /* We have to make sure that any param to a bind(c) routine does
1043 not have the allocatable, pointer, or optional attributes,
1044 according to J3/04-007, section 5.1. */
1045 if (sym
->attr
.allocatable
== 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "ALLOCATABLE attribute because procedure '%s'"
1049 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1050 sym
->ns
->proc_name
->name
);
1054 if (sym
->attr
.pointer
== 1)
1056 gfc_error ("Variable '%s' at %L cannot have the "
1057 "POINTER attribute because procedure '%s'"
1058 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1059 sym
->ns
->proc_name
->name
);
1063 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1065 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1066 "and the VALUE attribute because procedure '%s' "
1067 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1068 sym
->ns
->proc_name
->name
);
1071 else if (sym
->attr
.optional
== 1
1072 && gfc_notify_std (GFC_STD_F2008_TR
, "TR29113: Variable '%s' "
1073 "at %L with OPTIONAL attribute in "
1074 "procedure '%s' which is BIND(C)",
1075 sym
->name
, &(sym
->declared_at
),
1076 sym
->ns
->proc_name
->name
)
1080 /* Make sure that if it has the dimension attribute, that it is
1081 either assumed size or explicit shape. */
1082 if (sym
->as
!= NULL
)
1084 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
1086 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1087 "argument to the procedure '%s' at %L because "
1088 "the procedure is BIND(C)", sym
->name
,
1089 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1090 &(sym
->ns
->proc_name
->declared_at
));
1094 if (sym
->as
->type
== AS_DEFERRED
)
1096 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1097 "argument to the procedure '%s' at %L because "
1098 "the procedure is BIND(C)", sym
->name
,
1099 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1100 &(sym
->ns
->proc_name
->declared_at
));
1112 /* Function called by variable_decl() that adds a name to the symbol table. */
1115 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1116 gfc_array_spec
**as
, locus
*var_locus
)
1118 symbol_attribute attr
;
1121 if (gfc_get_symbol (name
, NULL
, &sym
))
1124 /* Start updating the symbol table. Add basic type attribute if present. */
1125 if (current_ts
.type
!= BT_UNKNOWN
1126 && (sym
->attr
.implicit_type
== 0
1127 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1128 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1131 if (sym
->ts
.type
== BT_CHARACTER
)
1134 sym
->ts
.deferred
= cl_deferred
;
1137 /* Add dimension attribute if present. */
1138 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1142 /* Add attribute to symbol. The copy is so that we can reset the
1143 dimension attribute. */
1144 attr
= current_attr
;
1146 attr
.codimension
= 0;
1148 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1151 /* Finish any work that may need to be done for the binding label,
1152 if it's a bind(c). The bind(c) attr is found before the symbol
1153 is made, and before the symbol name (for data decls), so the
1154 current_ts is holding the binding label, or nothing if the
1155 name= attr wasn't given. Therefore, test here if we're dealing
1156 with a bind(c) and make sure the binding label is set correctly. */
1157 if (sym
->attr
.is_bind_c
== 1)
1159 if (sym
->binding_label
[0] == '\0')
1161 /* Set the binding label and verify that if a NAME= was specified
1162 then only one identifier was in the entity-decl-list. */
1163 if (set_binding_label (sym
->binding_label
, sym
->name
,
1164 num_idents_on_line
) == FAILURE
)
1169 /* See if we know we're in a common block, and if it's a bind(c)
1170 common then we need to make sure we're an interoperable type. */
1171 if (sym
->attr
.in_common
== 1)
1173 /* Test the common block object. */
1174 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1175 && sym
->ts
.is_c_interop
!= 1)
1177 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1178 "must be declared with a C interoperable "
1179 "kind since common block '%s' is BIND(C)",
1180 sym
->name
, sym
->common_block
->name
,
1181 sym
->common_block
->name
);
1186 sym
->attr
.implied_index
= 0;
1188 if (sym
->ts
.type
== BT_CLASS
)
1189 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1195 /* Set character constant to the given length. The constant will be padded or
1196 truncated. If we're inside an array constructor without a typespec, we
1197 additionally check that all elements have the same length; check_len -1
1198 means no checking. */
1201 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1206 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1207 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1209 slen
= expr
->value
.character
.length
;
1212 s
= gfc_get_wide_string (len
+ 1);
1213 memcpy (s
, expr
->value
.character
.string
,
1214 MIN (len
, slen
) * sizeof (gfc_char_t
));
1216 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1218 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1219 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1220 "(%d/%d)", &expr
->where
, slen
, len
);
1222 /* Apply the standard by 'hand' otherwise it gets cleared for
1224 if (check_len
!= -1 && slen
!= check_len
1225 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1226 gfc_error_now ("The CHARACTER elements of the array constructor "
1227 "at %L must have the same length (%d/%d)",
1228 &expr
->where
, slen
, check_len
);
1231 free (expr
->value
.character
.string
);
1232 expr
->value
.character
.string
= s
;
1233 expr
->value
.character
.length
= len
;
1238 /* Function to create and update the enumerator history
1239 using the information passed as arguments.
1240 Pointer "max_enum" is also updated, to point to
1241 enum history node containing largest initializer.
1243 SYM points to the symbol node of enumerator.
1244 INIT points to its enumerator value. */
1247 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1249 enumerator_history
*new_enum_history
;
1250 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1252 new_enum_history
= XCNEW (enumerator_history
);
1254 new_enum_history
->sym
= sym
;
1255 new_enum_history
->initializer
= init
;
1256 new_enum_history
->next
= NULL
;
1258 if (enum_history
== NULL
)
1260 enum_history
= new_enum_history
;
1261 max_enum
= enum_history
;
1265 new_enum_history
->next
= enum_history
;
1266 enum_history
= new_enum_history
;
1268 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1269 new_enum_history
->initializer
->value
.integer
) < 0)
1270 max_enum
= new_enum_history
;
1275 /* Function to free enum kind history. */
1278 gfc_free_enum_history (void)
1280 enumerator_history
*current
= enum_history
;
1281 enumerator_history
*next
;
1283 while (current
!= NULL
)
1285 next
= current
->next
;
1290 enum_history
= NULL
;
1294 /* Function called by variable_decl() that adds an initialization
1295 expression to a symbol. */
1298 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1300 symbol_attribute attr
;
1305 if (find_special (name
, &sym
, false))
1310 /* If this symbol is confirming an implicit parameter type,
1311 then an initialization expression is not allowed. */
1312 if (attr
.flavor
== FL_PARAMETER
1313 && sym
->value
!= NULL
1316 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1323 /* An initializer is required for PARAMETER declarations. */
1324 if (attr
.flavor
== FL_PARAMETER
)
1326 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1332 /* If a variable appears in a DATA block, it cannot have an
1336 gfc_error ("Variable '%s' at %C with an initializer already "
1337 "appears in a DATA statement", sym
->name
);
1341 /* Check if the assignment can happen. This has to be put off
1342 until later for derived type variables and procedure pointers. */
1343 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1344 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1345 && !sym
->attr
.proc_pointer
1346 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1349 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1350 && init
->ts
.type
== BT_CHARACTER
)
1352 /* Update symbol character length according initializer. */
1353 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1356 if (sym
->ts
.u
.cl
->length
== NULL
)
1359 /* If there are multiple CHARACTER variables declared on the
1360 same line, we don't want them to share the same length. */
1361 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1363 if (sym
->attr
.flavor
== FL_PARAMETER
)
1365 if (init
->expr_type
== EXPR_CONSTANT
)
1367 clen
= init
->value
.character
.length
;
1368 sym
->ts
.u
.cl
->length
1369 = gfc_get_int_expr (gfc_default_integer_kind
,
1372 else if (init
->expr_type
== EXPR_ARRAY
)
1375 c
= gfc_constructor_first (init
->value
.constructor
);
1376 clen
= c
->expr
->value
.character
.length
;
1377 sym
->ts
.u
.cl
->length
1378 = gfc_get_int_expr (gfc_default_integer_kind
,
1381 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1382 sym
->ts
.u
.cl
->length
=
1383 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1386 /* Update initializer character length according symbol. */
1387 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1389 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1391 if (init
->expr_type
== EXPR_CONSTANT
)
1392 gfc_set_constant_character_len (len
, init
, -1);
1393 else if (init
->expr_type
== EXPR_ARRAY
)
1397 /* Build a new charlen to prevent simplification from
1398 deleting the length before it is resolved. */
1399 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1400 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1402 for (c
= gfc_constructor_first (init
->value
.constructor
);
1403 c
; c
= gfc_constructor_next (c
))
1404 gfc_set_constant_character_len (len
, c
->expr
, -1);
1409 /* If sym is implied-shape, set its upper bounds from init. */
1410 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1411 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1415 if (init
->rank
== 0)
1417 gfc_error ("Can't initialize implied-shape array at %L"
1418 " with scalar", &sym
->declared_at
);
1421 gcc_assert (sym
->as
->rank
== init
->rank
);
1423 /* Shape should be present, we get an initialization expression. */
1424 gcc_assert (init
->shape
);
1426 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1432 lower
= sym
->as
->lower
[dim
];
1433 if (lower
->expr_type
!= EXPR_CONSTANT
)
1435 gfc_error ("Non-constant lower bound in implied-shape"
1436 " declaration at %L", &lower
->where
);
1440 /* All dimensions must be without upper bound. */
1441 gcc_assert (!sym
->as
->upper
[dim
]);
1444 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1445 mpz_add (e
->value
.integer
,
1446 lower
->value
.integer
, init
->shape
[dim
]);
1447 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1448 sym
->as
->upper
[dim
] = e
;
1451 sym
->as
->type
= AS_EXPLICIT
;
1454 /* Need to check if the expression we initialized this
1455 to was one of the iso_c_binding named constants. If so,
1456 and we're a parameter (constant), let it be iso_c.
1458 integer(c_int), parameter :: my_int = c_int
1459 integer(my_int) :: my_int_2
1460 If we mark my_int as iso_c (since we can see it's value
1461 is equal to one of the named constants), then my_int_2
1462 will be considered C interoperable. */
1463 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1465 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1466 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1467 /* attr bits needed for module files. */
1468 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1469 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1470 if (init
->ts
.is_iso_c
)
1471 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1474 /* Add initializer. Make sure we keep the ranks sane. */
1475 if (sym
->attr
.dimension
&& init
->rank
== 0)
1480 if (sym
->attr
.flavor
== FL_PARAMETER
1481 && init
->expr_type
== EXPR_CONSTANT
1482 && spec_size (sym
->as
, &size
) == SUCCESS
1483 && mpz_cmp_si (size
, 0) > 0)
1485 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1487 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1488 gfc_constructor_append_expr (&array
->value
.constructor
,
1491 : gfc_copy_expr (init
),
1494 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1495 for (n
= 0; n
< sym
->as
->rank
; n
++)
1496 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1501 init
->rank
= sym
->as
->rank
;
1505 if (sym
->attr
.save
== SAVE_NONE
)
1506 sym
->attr
.save
= SAVE_IMPLICIT
;
1514 /* Function called by variable_decl() that adds a name to a structure
1518 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1519 gfc_array_spec
**as
)
1522 gfc_try t
= SUCCESS
;
1524 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1525 constructing, it must have the pointer attribute. */
1526 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1527 && current_ts
.u
.derived
== gfc_current_block ()
1528 && current_attr
.pointer
== 0)
1530 gfc_error ("Component at %C must have the POINTER attribute");
1534 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1536 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1538 gfc_error ("Array component of structure at %C must have explicit "
1539 "or deferred shape");
1544 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1548 if (c
->ts
.type
== BT_CHARACTER
)
1550 c
->attr
= current_attr
;
1552 c
->initializer
= *init
;
1559 c
->attr
.codimension
= 1;
1561 c
->attr
.dimension
= 1;
1565 /* Should this ever get more complicated, combine with similar section
1566 in add_init_expr_to_sym into a separate function. */
1567 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
&& c
->ts
.u
.cl
1568 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1572 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1573 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1574 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1576 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1578 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1579 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1580 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1581 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1583 gfc_constructor
*ctor
;
1584 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1589 bool has_ts
= (c
->initializer
->ts
.u
.cl
1590 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1592 /* Remember the length of the first element for checking
1593 that all elements *in the constructor* have the same
1594 length. This need not be the length of the LHS! */
1595 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1596 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1597 first_len
= ctor
->expr
->value
.character
.length
;
1599 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1600 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1602 gfc_set_constant_character_len (len
, ctor
->expr
,
1603 has_ts
? -1 : first_len
);
1604 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1610 /* Check array components. */
1611 if (!c
->attr
.dimension
)
1614 if (c
->attr
.pointer
)
1616 if (c
->as
->type
!= AS_DEFERRED
)
1618 gfc_error ("Pointer array component of structure at %C must have a "
1623 else if (c
->attr
.allocatable
)
1625 if (c
->as
->type
!= AS_DEFERRED
)
1627 gfc_error ("Allocatable component of structure at %C must have a "
1634 if (c
->as
->type
!= AS_EXPLICIT
)
1636 gfc_error ("Array component of structure at %C must have an "
1643 if (c
->ts
.type
== BT_CLASS
)
1645 bool delayed
= (gfc_state_stack
->sym
== c
->ts
.u
.derived
)
1646 || (!c
->ts
.u
.derived
->components
1647 && !c
->ts
.u
.derived
->attr
.zero_comp
);
1648 return gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, delayed
);
1655 /* Match a 'NULL()', and possibly take care of some side effects. */
1658 gfc_match_null (gfc_expr
**result
)
1663 m
= gfc_match (" null ( )");
1667 /* The NULL symbol now has to be/become an intrinsic function. */
1668 if (gfc_get_symbol ("null", NULL
, &sym
))
1670 gfc_error ("NULL() initialization at %C is ambiguous");
1674 gfc_intrinsic_symbol (sym
);
1676 if (sym
->attr
.proc
!= PROC_INTRINSIC
1677 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1678 sym
->name
, NULL
) == FAILURE
1679 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1682 *result
= gfc_get_null_expr (&gfc_current_locus
);
1688 /* Match the initialization expr for a data pointer or procedure pointer. */
1691 match_pointer_init (gfc_expr
**init
, int procptr
)
1695 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1697 gfc_error ("Initialization of pointer at %C is not allowed in "
1698 "a PURE procedure");
1702 /* Match NULL() initilization. */
1703 m
= gfc_match_null (init
);
1707 /* Match non-NULL initialization. */
1708 gfc_matching_ptr_assignment
= !procptr
;
1709 gfc_matching_procptr_assignment
= procptr
;
1710 m
= gfc_match_rvalue (init
);
1711 gfc_matching_ptr_assignment
= 0;
1712 gfc_matching_procptr_assignment
= 0;
1713 if (m
== MATCH_ERROR
)
1715 else if (m
== MATCH_NO
)
1717 gfc_error ("Error in pointer initialization at %C");
1722 gfc_resolve_expr (*init
);
1724 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: non-NULL pointer "
1725 "initialization at %C") == FAILURE
)
1732 /* Match a variable name with an optional initializer. When this
1733 subroutine is called, a variable is expected to be parsed next.
1734 Depending on what is happening at the moment, updates either the
1735 symbol table or the current interface. */
1738 variable_decl (int elem
)
1740 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1741 gfc_expr
*initializer
, *char_len
;
1743 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1755 /* When we get here, we've just matched a list of attributes and
1756 maybe a type and a double colon. The next thing we expect to see
1757 is the name of the symbol. */
1758 m
= gfc_match_name (name
);
1762 var_locus
= gfc_current_locus
;
1764 /* Now we could see the optional array spec. or character length. */
1765 m
= gfc_match_array_spec (&as
, true, true);
1766 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1767 cp_as
= gfc_copy_array_spec (as
);
1768 else if (m
== MATCH_ERROR
)
1772 as
= gfc_copy_array_spec (current_as
);
1773 else if (current_as
)
1774 merge_array_spec (current_as
, as
, true);
1776 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1777 determine (and check) whether it can be implied-shape. If it
1778 was parsed as assumed-size, change it because PARAMETERs can not
1782 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1785 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1790 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1791 && current_attr
.flavor
== FL_PARAMETER
)
1792 as
->type
= AS_IMPLIED_SHAPE
;
1794 if (as
->type
== AS_IMPLIED_SHAPE
1795 && gfc_notify_std (GFC_STD_F2008
,
1796 "Fortran 2008: Implied-shape array at %L",
1797 &var_locus
) == FAILURE
)
1806 cl_deferred
= false;
1808 if (current_ts
.type
== BT_CHARACTER
)
1810 switch (match_char_length (&char_len
, &cl_deferred
))
1813 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1815 cl
->length
= char_len
;
1818 /* Non-constant lengths need to be copied after the first
1819 element. Also copy assumed lengths. */
1822 && (current_ts
.u
.cl
->length
== NULL
1823 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1825 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1826 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1829 cl
= current_ts
.u
.cl
;
1831 cl_deferred
= current_ts
.deferred
;
1840 /* If this symbol has already shown up in a Cray Pointer declaration,
1841 then we want to set the type & bail out. */
1842 if (gfc_option
.flag_cray_pointer
)
1844 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1845 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1847 sym
->ts
.type
= current_ts
.type
;
1848 sym
->ts
.kind
= current_ts
.kind
;
1850 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1851 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1852 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1855 /* Check to see if we have an array specification. */
1858 if (sym
->as
!= NULL
)
1860 gfc_error ("Duplicate array spec for Cray pointee at %C");
1861 gfc_free_array_spec (cp_as
);
1867 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1868 gfc_internal_error ("Couldn't set pointee array spec.");
1870 /* Fix the array spec. */
1871 m
= gfc_mod_pointee_as (sym
->as
);
1872 if (m
== MATCH_ERROR
)
1880 gfc_free_array_spec (cp_as
);
1884 /* Procedure pointer as function result. */
1885 if (gfc_current_state () == COMP_FUNCTION
1886 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1887 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1888 strcpy (name
, "ppr@");
1890 if (gfc_current_state () == COMP_FUNCTION
1891 && strcmp (name
, gfc_current_block ()->name
) == 0
1892 && gfc_current_block ()->result
1893 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1894 strcpy (name
, "ppr@");
1896 /* OK, we've successfully matched the declaration. Now put the
1897 symbol in the current namespace, because it might be used in the
1898 optional initialization expression for this symbol, e.g. this is
1901 integer, parameter :: i = huge(i)
1903 This is only true for parameters or variables of a basic type.
1904 For components of derived types, it is not true, so we don't
1905 create a symbol for those yet. If we fail to create the symbol,
1907 if (gfc_current_state () != COMP_DERIVED
1908 && build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
) == FAILURE
)
1914 /* An interface body specifies all of the procedure's
1915 characteristics and these shall be consistent with those
1916 specified in the procedure definition, except that the interface
1917 may specify a procedure that is not pure if the procedure is
1918 defined to be pure(12.3.2). */
1919 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1920 && gfc_current_ns
->proc_name
1921 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1922 && current_ts
.u
.derived
->ns
!= gfc_current_ns
)
1925 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.u
.derived
->name
);
1926 if (!(current_ts
.u
.derived
->attr
.imported
1928 && st
->n
.sym
== current_ts
.u
.derived
)
1929 && !gfc_current_ns
->has_import_set
)
1931 gfc_error ("the type of '%s' at %C has not been declared within the "
1938 /* In functions that have a RESULT variable defined, the function
1939 name always refers to function calls. Therefore, the name is
1940 not allowed to appear in specification statements. */
1941 if (gfc_current_state () == COMP_FUNCTION
1942 && gfc_current_block () != NULL
1943 && gfc_current_block ()->result
!= NULL
1944 && gfc_current_block ()->result
!= gfc_current_block ()
1945 && strcmp (gfc_current_block ()->name
, name
) == 0)
1947 gfc_error ("Function name '%s' not allowed at %C", name
);
1952 /* We allow old-style initializations of the form
1953 integer i /2/, j(4) /3*3, 1/
1954 (if no colon has been seen). These are different from data
1955 statements in that initializers are only allowed to apply to the
1956 variable immediately preceding, i.e.
1958 is not allowed. Therefore we have to do some work manually, that
1959 could otherwise be left to the matchers for DATA statements. */
1961 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1963 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1964 "initialization at %C") == FAILURE
)
1967 return match_old_style_init (name
);
1970 /* The double colon must be present in order to have initializers.
1971 Otherwise the statement is ambiguous with an assignment statement. */
1974 if (gfc_match (" =>") == MATCH_YES
)
1976 if (!current_attr
.pointer
)
1978 gfc_error ("Initialization at %C isn't for a pointer variable");
1983 m
= match_pointer_init (&initializer
, 0);
1987 else if (gfc_match_char ('=') == MATCH_YES
)
1989 if (current_attr
.pointer
)
1991 gfc_error ("Pointer initialization at %C requires '=>', "
1997 m
= gfc_match_init_expr (&initializer
);
2000 gfc_error ("Expected an initialization expression at %C");
2004 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2005 && gfc_state_stack
->state
!= COMP_DERIVED
)
2007 gfc_error ("Initialization of variable at %C is not allowed in "
2008 "a PURE procedure");
2017 if (initializer
!= NULL
&& current_attr
.allocatable
2018 && gfc_current_state () == COMP_DERIVED
)
2020 gfc_error ("Initialization of allocatable component at %C is not "
2026 /* Add the initializer. Note that it is fine if initializer is
2027 NULL here, because we sometimes also need to check if a
2028 declaration *must* have an initialization expression. */
2029 if (gfc_current_state () != COMP_DERIVED
)
2030 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2033 if (current_ts
.type
== BT_DERIVED
2034 && !current_attr
.pointer
&& !initializer
)
2035 initializer
= gfc_default_initializer (¤t_ts
);
2036 t
= build_struct (name
, cl
, &initializer
, &as
);
2039 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
2042 /* Free stuff up and return. */
2043 gfc_free_expr (initializer
);
2044 gfc_free_array_spec (as
);
2050 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2051 This assumes that the byte size is equal to the kind number for
2052 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2055 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2060 if (gfc_match_char ('*') != MATCH_YES
)
2063 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2067 original_kind
= ts
->kind
;
2069 /* Massage the kind numbers for complex types. */
2070 if (ts
->type
== BT_COMPLEX
)
2074 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2075 gfc_basic_typename (ts
->type
), original_kind
);
2081 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2083 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2084 gfc_basic_typename (ts
->type
), original_kind
);
2088 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
2089 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
2096 /* Match a kind specification. Since kinds are generally optional, we
2097 usually return MATCH_NO if something goes wrong. If a "kind="
2098 string is found, then we know we have an error. */
2101 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2113 where
= loc
= gfc_current_locus
;
2118 if (gfc_match_char ('(') == MATCH_NO
)
2121 /* Also gobbles optional text. */
2122 if (gfc_match (" kind = ") == MATCH_YES
)
2125 loc
= gfc_current_locus
;
2128 n
= gfc_match_init_expr (&e
);
2132 if (gfc_matching_function
)
2134 /* The function kind expression might include use associated or
2135 imported parameters and try again after the specification
2137 if (gfc_match_char (')') != MATCH_YES
)
2139 gfc_error ("Missing right parenthesis at %C");
2145 gfc_undo_symbols ();
2150 /* ....or else, the match is real. */
2152 gfc_error ("Expected initialization expression at %C");
2160 gfc_error ("Expected scalar initialization expression at %C");
2165 msg
= gfc_extract_int (e
, &ts
->kind
);
2174 /* Before throwing away the expression, let's see if we had a
2175 C interoperable kind (and store the fact). */
2176 if (e
->ts
.is_c_interop
== 1)
2178 /* Mark this as c interoperable if being declared with one
2179 of the named constants from iso_c_binding. */
2180 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2181 ts
->f90_type
= e
->ts
.f90_type
;
2187 /* Ignore errors to this point, if we've gotten here. This means
2188 we ignore the m=MATCH_ERROR from above. */
2189 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2191 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2192 gfc_basic_typename (ts
->type
));
2193 gfc_current_locus
= where
;
2197 /* Warn if, e.g., c_int is used for a REAL variable, but not
2198 if, e.g., c_double is used for COMPLEX as the standard
2199 explicitly says that the kind type parameter for complex and real
2200 variable is the same, i.e. c_float == c_float_complex. */
2201 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2202 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2203 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2204 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2205 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2206 gfc_basic_typename (ts
->type
));
2208 gfc_gobble_whitespace ();
2209 if ((c
= gfc_next_ascii_char ()) != ')'
2210 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2212 if (ts
->type
== BT_CHARACTER
)
2213 gfc_error ("Missing right parenthesis or comma at %C");
2215 gfc_error ("Missing right parenthesis at %C");
2219 /* All tests passed. */
2222 if(m
== MATCH_ERROR
)
2223 gfc_current_locus
= where
;
2225 /* Return what we know from the test(s). */
2230 gfc_current_locus
= where
;
2236 match_char_kind (int * kind
, int * is_iso_c
)
2245 where
= gfc_current_locus
;
2247 n
= gfc_match_init_expr (&e
);
2249 if (n
!= MATCH_YES
&& gfc_matching_function
)
2251 /* The expression might include use-associated or imported
2252 parameters and try again after the specification
2255 gfc_undo_symbols ();
2260 gfc_error ("Expected initialization expression at %C");
2266 gfc_error ("Expected scalar initialization expression at %C");
2271 msg
= gfc_extract_int (e
, kind
);
2272 *is_iso_c
= e
->ts
.is_iso_c
;
2282 /* Ignore errors to this point, if we've gotten here. This means
2283 we ignore the m=MATCH_ERROR from above. */
2284 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2286 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2290 /* All tests passed. */
2293 if (m
== MATCH_ERROR
)
2294 gfc_current_locus
= where
;
2296 /* Return what we know from the test(s). */
2301 gfc_current_locus
= where
;
2306 /* Match the various kind/length specifications in a CHARACTER
2307 declaration. We don't return MATCH_NO. */
2310 gfc_match_char_spec (gfc_typespec
*ts
)
2312 int kind
, seen_length
, is_iso_c
;
2324 /* Try the old-style specification first. */
2325 old_char_selector
= 0;
2327 m
= match_char_length (&len
, &deferred
);
2331 old_char_selector
= 1;
2336 m
= gfc_match_char ('(');
2339 m
= MATCH_YES
; /* Character without length is a single char. */
2343 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2344 if (gfc_match (" kind =") == MATCH_YES
)
2346 m
= match_char_kind (&kind
, &is_iso_c
);
2348 if (m
== MATCH_ERROR
)
2353 if (gfc_match (" , len =") == MATCH_NO
)
2356 m
= char_len_param_value (&len
, &deferred
);
2359 if (m
== MATCH_ERROR
)
2366 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2367 if (gfc_match (" len =") == MATCH_YES
)
2369 m
= char_len_param_value (&len
, &deferred
);
2372 if (m
== MATCH_ERROR
)
2376 if (gfc_match_char (')') == MATCH_YES
)
2379 if (gfc_match (" , kind =") != MATCH_YES
)
2382 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2388 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2389 m
= char_len_param_value (&len
, &deferred
);
2392 if (m
== MATCH_ERROR
)
2396 m
= gfc_match_char (')');
2400 if (gfc_match_char (',') != MATCH_YES
)
2403 gfc_match (" kind ="); /* Gobble optional text. */
2405 m
= match_char_kind (&kind
, &is_iso_c
);
2406 if (m
== MATCH_ERROR
)
2412 /* Require a right-paren at this point. */
2413 m
= gfc_match_char (')');
2418 gfc_error ("Syntax error in CHARACTER declaration at %C");
2420 gfc_free_expr (len
);
2424 /* Deal with character functions after USE and IMPORT statements. */
2425 if (gfc_matching_function
)
2427 gfc_free_expr (len
);
2428 gfc_undo_symbols ();
2434 gfc_free_expr (len
);
2438 /* Do some final massaging of the length values. */
2439 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2441 if (seen_length
== 0)
2442 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2447 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2448 ts
->deferred
= deferred
;
2450 /* We have to know if it was a c interoperable kind so we can
2451 do accurate type checking of bind(c) procs, etc. */
2453 /* Mark this as c interoperable if being declared with one
2454 of the named constants from iso_c_binding. */
2455 ts
->is_c_interop
= is_iso_c
;
2456 else if (len
!= NULL
)
2457 /* Here, we might have parsed something such as: character(c_char)
2458 In this case, the parsing code above grabs the c_char when
2459 looking for the length (line 1690, roughly). it's the last
2460 testcase for parsing the kind params of a character variable.
2461 However, it's not actually the length. this seems like it
2463 To see if the user used a C interop kind, test the expr
2464 of the so called length, and see if it's C interoperable. */
2465 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2471 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2472 structure to the matched specification. This is necessary for FUNCTION and
2473 IMPLICIT statements.
2475 If implicit_flag is nonzero, then we don't check for the optional
2476 kind specification. Not doing so is needed for matching an IMPLICIT
2477 statement correctly. */
2480 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2482 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2486 bool seen_deferred_kind
, matched_type
;
2488 /* A belt and braces check that the typespec is correctly being treated
2489 as a deferred characteristic association. */
2490 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2491 && (gfc_current_block ()->result
->ts
.kind
== -1)
2492 && (ts
->kind
== -1);
2494 if (seen_deferred_kind
)
2497 /* Clear the current binding label, in case one is given. */
2498 curr_binding_label
[0] = '\0';
2500 if (gfc_match (" byte") == MATCH_YES
)
2502 if (gfc_notify_std (GFC_STD_GNU
, "Extension: BYTE type at %C")
2506 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2508 gfc_error ("BYTE type used at %C "
2509 "is not available on the target machine");
2513 ts
->type
= BT_INTEGER
;
2519 m
= gfc_match (" type ( %n", name
);
2520 matched_type
= (m
== MATCH_YES
);
2522 if ((matched_type
&& strcmp ("integer", name
) == 0)
2523 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2525 ts
->type
= BT_INTEGER
;
2526 ts
->kind
= gfc_default_integer_kind
;
2530 if ((matched_type
&& strcmp ("character", name
) == 0)
2531 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2534 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2535 "intrinsic-type-spec at %C") == FAILURE
)
2538 ts
->type
= BT_CHARACTER
;
2539 if (implicit_flag
== 0)
2540 m
= gfc_match_char_spec (ts
);
2544 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2550 if ((matched_type
&& strcmp ("real", name
) == 0)
2551 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2554 ts
->kind
= gfc_default_real_kind
;
2559 && (strcmp ("doubleprecision", name
) == 0
2560 || (strcmp ("double", name
) == 0
2561 && gfc_match (" precision") == MATCH_YES
)))
2562 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2565 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2566 "intrinsic-type-spec at %C") == FAILURE
)
2568 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2572 ts
->kind
= gfc_default_double_kind
;
2576 if ((matched_type
&& strcmp ("complex", name
) == 0)
2577 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2579 ts
->type
= BT_COMPLEX
;
2580 ts
->kind
= gfc_default_complex_kind
;
2585 && (strcmp ("doublecomplex", name
) == 0
2586 || (strcmp ("double", name
) == 0
2587 && gfc_match (" complex") == MATCH_YES
)))
2588 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2590 if (gfc_notify_std (GFC_STD_GNU
, "Extension: DOUBLE COMPLEX at %C")
2595 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2596 "intrinsic-type-spec at %C") == FAILURE
)
2599 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2602 ts
->type
= BT_COMPLEX
;
2603 ts
->kind
= gfc_default_double_kind
;
2607 if ((matched_type
&& strcmp ("logical", name
) == 0)
2608 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2610 ts
->type
= BT_LOGICAL
;
2611 ts
->kind
= gfc_default_logical_kind
;
2616 m
= gfc_match_char (')');
2619 ts
->type
= BT_DERIVED
;
2622 /* Match CLASS declarations. */
2623 m
= gfc_match (" class ( * )");
2624 if (m
== MATCH_ERROR
)
2626 else if (m
== MATCH_YES
)
2628 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2632 m
= gfc_match (" class ( %n )", name
);
2635 ts
->type
= BT_CLASS
;
2637 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: CLASS statement at %C")
2642 /* Defer association of the derived type until the end of the
2643 specification block. However, if the derived type can be
2644 found, add it to the typespec. */
2645 if (gfc_matching_function
)
2647 ts
->u
.derived
= NULL
;
2648 if (gfc_current_state () != COMP_INTERFACE
2649 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2650 ts
->u
.derived
= sym
;
2654 /* Search for the name but allow the components to be defined later. If
2655 type = -1, this typespec has been seen in a function declaration but
2656 the type could not be accessed at that point. */
2658 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2660 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2663 else if (ts
->kind
== -1)
2665 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2666 || gfc_current_ns
->has_import_set
;
2667 if (gfc_find_symbol (name
, NULL
, iface
, &sym
))
2669 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2678 if (sym
->attr
.flavor
!= FL_DERIVED
2679 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2682 gfc_set_sym_referenced (sym
);
2683 ts
->u
.derived
= sym
;
2689 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2690 "intrinsic-type-spec at %C") == FAILURE
)
2693 /* For all types except double, derived and character, look for an
2694 optional kind specifier. MATCH_NO is actually OK at this point. */
2695 if (implicit_flag
== 1)
2697 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2703 if (gfc_current_form
== FORM_FREE
)
2705 c
= gfc_peek_ascii_char ();
2706 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2707 && c
!= ':' && c
!= ',')
2709 if (matched_type
&& c
== ')')
2711 gfc_next_ascii_char ();
2718 m
= gfc_match_kind_spec (ts
, false);
2719 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2720 m
= gfc_match_old_kind_spec (ts
);
2722 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2725 /* Defer association of the KIND expression of function results
2726 until after USE and IMPORT statements. */
2727 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2728 || gfc_matching_function
)
2732 m
= MATCH_YES
; /* No kind specifier found. */
2738 /* Match an IMPLICIT NONE statement. Actually, this statement is
2739 already matched in parse.c, or we would not end up here in the
2740 first place. So the only thing we need to check, is if there is
2741 trailing garbage. If not, the match is successful. */
2744 gfc_match_implicit_none (void)
2746 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2750 /* Match the letter range(s) of an IMPLICIT statement. */
2753 match_implicit_range (void)
2759 cur_loc
= gfc_current_locus
;
2761 gfc_gobble_whitespace ();
2762 c
= gfc_next_ascii_char ();
2765 gfc_error ("Missing character range in IMPLICIT at %C");
2772 gfc_gobble_whitespace ();
2773 c1
= gfc_next_ascii_char ();
2777 gfc_gobble_whitespace ();
2778 c
= gfc_next_ascii_char ();
2783 inner
= 0; /* Fall through. */
2790 gfc_gobble_whitespace ();
2791 c2
= gfc_next_ascii_char ();
2795 gfc_gobble_whitespace ();
2796 c
= gfc_next_ascii_char ();
2798 if ((c
!= ',') && (c
!= ')'))
2811 gfc_error ("Letters must be in alphabetic order in "
2812 "IMPLICIT statement at %C");
2816 /* See if we can add the newly matched range to the pending
2817 implicits from this IMPLICIT statement. We do not check for
2818 conflicts with whatever earlier IMPLICIT statements may have
2819 set. This is done when we've successfully finished matching
2821 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2828 gfc_syntax_error (ST_IMPLICIT
);
2830 gfc_current_locus
= cur_loc
;
2835 /* Match an IMPLICIT statement, storing the types for
2836 gfc_set_implicit() if the statement is accepted by the parser.
2837 There is a strange looking, but legal syntactic construction
2838 possible. It looks like:
2840 IMPLICIT INTEGER (a-b) (c-d)
2842 This is legal if "a-b" is a constant expression that happens to
2843 equal one of the legal kinds for integers. The real problem
2844 happens with an implicit specification that looks like:
2846 IMPLICIT INTEGER (a-b)
2848 In this case, a typespec matcher that is "greedy" (as most of the
2849 matchers are) gobbles the character range as a kindspec, leaving
2850 nothing left. We therefore have to go a bit more slowly in the
2851 matching process by inhibiting the kindspec checking during
2852 typespec matching and checking for a kind later. */
2855 gfc_match_implicit (void)
2864 /* We don't allow empty implicit statements. */
2865 if (gfc_match_eos () == MATCH_YES
)
2867 gfc_error ("Empty IMPLICIT statement at %C");
2873 /* First cleanup. */
2874 gfc_clear_new_implicit ();
2876 /* A basic type is mandatory here. */
2877 m
= gfc_match_decl_type_spec (&ts
, 1);
2878 if (m
== MATCH_ERROR
)
2883 cur_loc
= gfc_current_locus
;
2884 m
= match_implicit_range ();
2888 /* We may have <TYPE> (<RANGE>). */
2889 gfc_gobble_whitespace ();
2890 c
= gfc_next_ascii_char ();
2891 if ((c
== '\n') || (c
== ','))
2893 /* Check for CHARACTER with no length parameter. */
2894 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
2896 ts
.kind
= gfc_default_character_kind
;
2897 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2898 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
2902 /* Record the Successful match. */
2903 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2908 gfc_current_locus
= cur_loc
;
2911 /* Discard the (incorrectly) matched range. */
2912 gfc_clear_new_implicit ();
2914 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2915 if (ts
.type
== BT_CHARACTER
)
2916 m
= gfc_match_char_spec (&ts
);
2919 m
= gfc_match_kind_spec (&ts
, false);
2922 m
= gfc_match_old_kind_spec (&ts
);
2923 if (m
== MATCH_ERROR
)
2929 if (m
== MATCH_ERROR
)
2932 m
= match_implicit_range ();
2933 if (m
== MATCH_ERROR
)
2938 gfc_gobble_whitespace ();
2939 c
= gfc_next_ascii_char ();
2940 if ((c
!= '\n') && (c
!= ','))
2943 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2951 gfc_syntax_error (ST_IMPLICIT
);
2959 gfc_match_import (void)
2961 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2966 if (gfc_current_ns
->proc_name
== NULL
2967 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2969 gfc_error ("IMPORT statement at %C only permitted in "
2970 "an INTERFACE body");
2974 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2978 if (gfc_match_eos () == MATCH_YES
)
2980 /* All host variables should be imported. */
2981 gfc_current_ns
->has_import_set
= 1;
2985 if (gfc_match (" ::") == MATCH_YES
)
2987 if (gfc_match_eos () == MATCH_YES
)
2989 gfc_error ("Expecting list of named entities at %C");
2997 m
= gfc_match (" %n", name
);
3001 if (gfc_current_ns
->parent
!= NULL
3002 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3004 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3007 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3008 && gfc_find_symbol (name
,
3009 gfc_current_ns
->proc_name
->ns
->parent
,
3012 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3018 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3019 "at %C - does not exist.", name
);
3023 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
3025 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3030 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, sym
->name
);
3033 sym
->attr
.imported
= 1;
3045 if (gfc_match_eos () == MATCH_YES
)
3047 if (gfc_match_char (',') != MATCH_YES
)
3054 gfc_error ("Syntax error in IMPORT statement at %C");
3059 /* A minimal implementation of gfc_match without whitespace, escape
3060 characters or variable arguments. Returns true if the next
3061 characters match the TARGET template exactly. */
3064 match_string_p (const char *target
)
3068 for (p
= target
; *p
; p
++)
3069 if ((char) gfc_next_ascii_char () != *p
)
3074 /* Matches an attribute specification including array specs. If
3075 successful, leaves the variables current_attr and current_as
3076 holding the specification. Also sets the colon_seen variable for
3077 later use by matchers associated with initializations.
3079 This subroutine is a little tricky in the sense that we don't know
3080 if we really have an attr-spec until we hit the double colon.
3081 Until that time, we can only return MATCH_NO. This forces us to
3082 check for duplicate specification at this level. */
3085 match_attr_spec (void)
3087 /* Modifiers that can exist in a type statement. */
3089 { GFC_DECL_BEGIN
= 0,
3090 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3091 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3092 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3093 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3094 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3095 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3099 /* GFC_DECL_END is the sentinel, index starts at 0. */
3100 #define NUM_DECL GFC_DECL_END
3102 locus start
, seen_at
[NUM_DECL
];
3109 gfc_clear_attr (¤t_attr
);
3110 start
= gfc_current_locus
;
3115 /* See if we get all of the keywords up to the final double colon. */
3116 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3124 gfc_gobble_whitespace ();
3126 ch
= gfc_next_ascii_char ();
3129 /* This is the successful exit condition for the loop. */
3130 if (gfc_next_ascii_char () == ':')
3135 gfc_gobble_whitespace ();
3136 switch (gfc_peek_ascii_char ())
3139 gfc_next_ascii_char ();
3140 switch (gfc_next_ascii_char ())
3143 if (match_string_p ("locatable"))
3145 /* Matched "allocatable". */
3146 d
= DECL_ALLOCATABLE
;
3151 if (match_string_p ("ynchronous"))
3153 /* Matched "asynchronous". */
3154 d
= DECL_ASYNCHRONOUS
;
3161 /* Try and match the bind(c). */
3162 m
= gfc_match_bind_c (NULL
, true);
3165 else if (m
== MATCH_ERROR
)
3170 gfc_next_ascii_char ();
3171 if ('o' != gfc_next_ascii_char ())
3173 switch (gfc_next_ascii_char ())
3176 if (match_string_p ("imension"))
3178 d
= DECL_CODIMENSION
;
3182 if (match_string_p ("tiguous"))
3184 d
= DECL_CONTIGUOUS
;
3191 if (match_string_p ("dimension"))
3196 if (match_string_p ("external"))
3201 if (match_string_p ("int"))
3203 ch
= gfc_next_ascii_char ();
3206 if (match_string_p ("nt"))
3208 /* Matched "intent". */
3209 /* TODO: Call match_intent_spec from here. */
3210 if (gfc_match (" ( in out )") == MATCH_YES
)
3212 else if (gfc_match (" ( in )") == MATCH_YES
)
3214 else if (gfc_match (" ( out )") == MATCH_YES
)
3220 if (match_string_p ("insic"))
3222 /* Matched "intrinsic". */
3230 if (match_string_p ("optional"))
3235 gfc_next_ascii_char ();
3236 switch (gfc_next_ascii_char ())
3239 if (match_string_p ("rameter"))
3241 /* Matched "parameter". */
3247 if (match_string_p ("inter"))
3249 /* Matched "pointer". */
3255 ch
= gfc_next_ascii_char ();
3258 if (match_string_p ("vate"))
3260 /* Matched "private". */
3266 if (match_string_p ("tected"))
3268 /* Matched "protected". */
3275 if (match_string_p ("blic"))
3277 /* Matched "public". */
3285 if (match_string_p ("save"))
3290 if (match_string_p ("target"))
3295 gfc_next_ascii_char ();
3296 ch
= gfc_next_ascii_char ();
3299 if (match_string_p ("lue"))
3301 /* Matched "value". */
3307 if (match_string_p ("latile"))
3309 /* Matched "volatile". */
3317 /* No double colon and no recognizable decl_type, so assume that
3318 we've been looking at something else the whole time. */
3325 /* Check to make sure any parens are paired up correctly. */
3326 if (gfc_match_parens () == MATCH_ERROR
)
3333 seen_at
[d
] = gfc_current_locus
;
3335 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3337 gfc_array_spec
*as
= NULL
;
3339 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3340 d
== DECL_CODIMENSION
);
3342 if (current_as
== NULL
)
3344 else if (m
== MATCH_YES
)
3346 merge_array_spec (as
, current_as
, false);
3352 if (d
== DECL_CODIMENSION
)
3353 gfc_error ("Missing codimension specification at %C");
3355 gfc_error ("Missing dimension specification at %C");
3359 if (m
== MATCH_ERROR
)
3364 /* Since we've seen a double colon, we have to be looking at an
3365 attr-spec. This means that we can now issue errors. */
3366 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3371 case DECL_ALLOCATABLE
:
3372 attr
= "ALLOCATABLE";
3374 case DECL_ASYNCHRONOUS
:
3375 attr
= "ASYNCHRONOUS";
3377 case DECL_CODIMENSION
:
3378 attr
= "CODIMENSION";
3380 case DECL_CONTIGUOUS
:
3381 attr
= "CONTIGUOUS";
3383 case DECL_DIMENSION
:
3390 attr
= "INTENT (IN)";
3393 attr
= "INTENT (OUT)";
3396 attr
= "INTENT (IN OUT)";
3398 case DECL_INTRINSIC
:
3404 case DECL_PARAMETER
:
3410 case DECL_PROTECTED
:
3425 case DECL_IS_BIND_C
:
3435 attr
= NULL
; /* This shouldn't happen. */
3438 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3443 /* Now that we've dealt with duplicate attributes, add the attributes
3444 to the current attribute. */
3445 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3450 if (gfc_current_state () == COMP_DERIVED
3451 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3452 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3453 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3455 if (d
== DECL_ALLOCATABLE
)
3457 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3458 "attribute at %C in a TYPE definition")
3467 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3474 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3475 && gfc_current_state () != COMP_MODULE
)
3477 if (d
== DECL_PRIVATE
)
3481 if (gfc_current_state () == COMP_DERIVED
3482 && gfc_state_stack
->previous
3483 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3485 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3486 "at %L in a TYPE definition", attr
,
3496 gfc_error ("%s attribute at %L is not allowed outside of the "
3497 "specification part of a module", attr
, &seen_at
[d
]);
3505 case DECL_ALLOCATABLE
:
3506 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3509 case DECL_ASYNCHRONOUS
:
3510 if (gfc_notify_std (GFC_STD_F2003
,
3511 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3515 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3518 case DECL_CODIMENSION
:
3519 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3522 case DECL_CONTIGUOUS
:
3523 if (gfc_notify_std (GFC_STD_F2008
,
3524 "Fortran 2008: CONTIGUOUS attribute at %C")
3528 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3531 case DECL_DIMENSION
:
3532 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3536 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3540 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3544 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3548 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3551 case DECL_INTRINSIC
:
3552 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3556 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3559 case DECL_PARAMETER
:
3560 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3564 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3567 case DECL_PROTECTED
:
3568 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3570 gfc_error ("PROTECTED at %C only allowed in specification "
3571 "part of a module");
3576 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3581 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3585 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3590 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3595 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3599 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3602 case DECL_IS_BIND_C
:
3603 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3607 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3612 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3616 if (gfc_notify_std (GFC_STD_F2003
,
3617 "Fortran 2003: VOLATILE attribute at %C")
3621 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3625 gfc_internal_error ("match_attr_spec(): Bad attribute");
3635 /* Module variables implicitly have the SAVE attribute. */
3636 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
)
3637 current_attr
.save
= SAVE_IMPLICIT
;
3643 gfc_current_locus
= start
;
3644 gfc_free_array_spec (current_as
);
3650 /* Set the binding label, dest_label, either with the binding label
3651 stored in the given gfc_typespec, ts, or if none was provided, it
3652 will be the symbol name in all lower case, as required by the draft
3653 (J3/04-007, section 15.4.1). If a binding label was given and
3654 there is more than one argument (num_idents), it is an error. */
3657 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3659 if (num_idents
> 1 && has_name_equals
)
3661 gfc_error ("Multiple identifiers provided with "
3662 "single NAME= specifier at %C");
3666 if (curr_binding_label
[0] != '\0')
3668 /* Binding label given; store in temp holder til have sym. */
3669 strcpy (dest_label
, curr_binding_label
);
3673 /* No binding label given, and the NAME= specifier did not exist,
3674 which means there was no NAME="". */
3675 if (sym_name
!= NULL
&& has_name_equals
== 0)
3676 strcpy (dest_label
, sym_name
);
3683 /* Set the status of the given common block as being BIND(C) or not,
3684 depending on the given parameter, is_bind_c. */
3687 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3689 com_block
->is_bind_c
= is_bind_c
;
3694 /* Verify that the given gfc_typespec is for a C interoperable type. */
3697 verify_c_interop (gfc_typespec
*ts
)
3699 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3700 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3701 ? SUCCESS
: FAILURE
;
3702 else if (ts
->is_c_interop
!= 1)
3709 /* Verify that the variables of a given common block, which has been
3710 defined with the attribute specifier bind(c), to be of a C
3711 interoperable type. Errors will be reported here, if
3715 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3717 gfc_symbol
*curr_sym
= NULL
;
3718 gfc_try retval
= SUCCESS
;
3720 curr_sym
= com_block
->head
;
3722 /* Make sure we have at least one symbol. */
3723 if (curr_sym
== NULL
)
3726 /* Here we know we have a symbol, so we'll execute this loop
3730 /* The second to last param, 1, says this is in a common block. */
3731 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3732 curr_sym
= curr_sym
->common_next
;
3733 } while (curr_sym
!= NULL
);
3739 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3740 an appropriate error message is reported. */
3743 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3744 int is_in_common
, gfc_common_head
*com_block
)
3746 bool bind_c_function
= false;
3747 gfc_try retval
= SUCCESS
;
3749 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3750 bind_c_function
= true;
3752 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3754 tmp_sym
= tmp_sym
->result
;
3755 /* Make sure it wasn't an implicitly typed result. */
3756 if (tmp_sym
->attr
.implicit_type
)
3758 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3759 "%L may not be C interoperable", tmp_sym
->name
,
3760 &tmp_sym
->declared_at
);
3761 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3762 /* Mark it as C interoperable to prevent duplicate warnings. */
3763 tmp_sym
->ts
.is_c_interop
= 1;
3764 tmp_sym
->attr
.is_c_interop
= 1;
3768 /* Here, we know we have the bind(c) attribute, so if we have
3769 enough type info, then verify that it's a C interop kind.
3770 The info could be in the symbol already, or possibly still in
3771 the given ts (current_ts), so look in both. */
3772 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3774 if (verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3776 /* See if we're dealing with a sym in a common block or not. */
3777 if (is_in_common
== 1)
3779 gfc_warning ("Variable '%s' in common block '%s' at %L "
3780 "may not be a C interoperable "
3781 "kind though common block '%s' is BIND(C)",
3782 tmp_sym
->name
, com_block
->name
,
3783 &(tmp_sym
->declared_at
), com_block
->name
);
3787 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3788 gfc_error ("Type declaration '%s' at %L is not C "
3789 "interoperable but it is BIND(C)",
3790 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3792 gfc_warning ("Variable '%s' at %L "
3793 "may not be a C interoperable "
3794 "kind but it is bind(c)",
3795 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3799 /* Variables declared w/in a common block can't be bind(c)
3800 since there's no way for C to see these variables, so there's
3801 semantically no reason for the attribute. */
3802 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3804 gfc_error ("Variable '%s' in common block '%s' at "
3805 "%L cannot be declared with BIND(C) "
3806 "since it is not a global",
3807 tmp_sym
->name
, com_block
->name
,
3808 &(tmp_sym
->declared_at
));
3812 /* Scalar variables that are bind(c) can not have the pointer
3813 or allocatable attributes. */
3814 if (tmp_sym
->attr
.is_bind_c
== 1)
3816 if (tmp_sym
->attr
.pointer
== 1)
3818 gfc_error ("Variable '%s' at %L cannot have both the "
3819 "POINTER and BIND(C) attributes",
3820 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3824 if (tmp_sym
->attr
.allocatable
== 1)
3826 gfc_error ("Variable '%s' at %L cannot have both the "
3827 "ALLOCATABLE and BIND(C) attributes",
3828 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3834 /* If it is a BIND(C) function, make sure the return value is a
3835 scalar value. The previous tests in this function made sure
3836 the type is interoperable. */
3837 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
3838 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3839 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3841 /* BIND(C) functions can not return a character string. */
3842 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
3843 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
3844 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
3845 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
3846 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3847 "be a character string", tmp_sym
->name
,
3848 &(tmp_sym
->declared_at
));
3851 /* See if the symbol has been marked as private. If it has, make sure
3852 there is no binding label and warn the user if there is one. */
3853 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3854 && tmp_sym
->binding_label
[0] != '\0')
3855 /* Use gfc_warning_now because we won't say that the symbol fails
3856 just because of this. */
3857 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3858 "given the binding label '%s'", tmp_sym
->name
,
3859 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3865 /* Set the appropriate fields for a symbol that's been declared as
3866 BIND(C) (the is_bind_c flag and the binding label), and verify that
3867 the type is C interoperable. Errors are reported by the functions
3868 used to set/test these fields. */
3871 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3873 gfc_try retval
= SUCCESS
;
3875 /* TODO: Do we need to make sure the vars aren't marked private? */
3877 /* Set the is_bind_c bit in symbol_attribute. */
3878 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3880 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3881 num_idents
) != SUCCESS
)
3888 /* Set the fields marking the given common block as BIND(C), including
3889 a binding label, and report any errors encountered. */
3892 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3894 gfc_try retval
= SUCCESS
;
3896 /* destLabel, common name, typespec (which may have binding label). */
3897 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3901 /* Set the given common block (com_block) to being bind(c) (1). */
3902 set_com_block_bind_c (com_block
, 1);
3908 /* Retrieve the list of one or more identifiers that the given bind(c)
3909 attribute applies to. */
3912 get_bind_c_idents (void)
3914 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3916 gfc_symbol
*tmp_sym
= NULL
;
3918 gfc_common_head
*com_block
= NULL
;
3920 if (gfc_match_name (name
) == MATCH_YES
)
3922 found_id
= MATCH_YES
;
3923 gfc_get_ha_symbol (name
, &tmp_sym
);
3925 else if (match_common_name (name
) == MATCH_YES
)
3927 found_id
= MATCH_YES
;
3928 com_block
= gfc_get_common (name
, 0);
3932 gfc_error ("Need either entity or common block name for "
3933 "attribute specification statement at %C");
3937 /* Save the current identifier and look for more. */
3940 /* Increment the number of identifiers found for this spec stmt. */
3943 /* Make sure we have a sym or com block, and verify that it can
3944 be bind(c). Set the appropriate field(s) and look for more
3946 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3948 if (tmp_sym
!= NULL
)
3950 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3956 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3961 /* Look to see if we have another identifier. */
3963 if (gfc_match_eos () == MATCH_YES
)
3964 found_id
= MATCH_NO
;
3965 else if (gfc_match_char (',') != MATCH_YES
)
3966 found_id
= MATCH_NO
;
3967 else if (gfc_match_name (name
) == MATCH_YES
)
3969 found_id
= MATCH_YES
;
3970 gfc_get_ha_symbol (name
, &tmp_sym
);
3972 else if (match_common_name (name
) == MATCH_YES
)
3974 found_id
= MATCH_YES
;
3975 com_block
= gfc_get_common (name
, 0);
3979 gfc_error ("Missing entity or common block name for "
3980 "attribute specification statement at %C");
3986 gfc_internal_error ("Missing symbol");
3988 } while (found_id
== MATCH_YES
);
3990 /* if we get here we were successful */
3995 /* Try and match a BIND(C) attribute specification statement. */
3998 gfc_match_bind_c_stmt (void)
4000 match found_match
= MATCH_NO
;
4005 /* This may not be necessary. */
4007 /* Clear the temporary binding label holder. */
4008 curr_binding_label
[0] = '\0';
4010 /* Look for the bind(c). */
4011 found_match
= gfc_match_bind_c (NULL
, true);
4013 if (found_match
== MATCH_YES
)
4015 /* Look for the :: now, but it is not required. */
4018 /* Get the identifier(s) that needs to be updated. This may need to
4019 change to hand the flag(s) for the attr specified so all identifiers
4020 found can have all appropriate parts updated (assuming that the same
4021 spec stmt can have multiple attrs, such as both bind(c) and
4023 if (get_bind_c_idents () != SUCCESS
)
4024 /* Error message should have printed already. */
4032 /* Match a data declaration statement. */
4035 gfc_match_data_decl (void)
4041 num_idents_on_line
= 0;
4043 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4047 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4048 && gfc_current_state () != COMP_DERIVED
)
4050 sym
= gfc_use_derived (current_ts
.u
.derived
);
4058 current_ts
.u
.derived
= sym
;
4061 m
= match_attr_spec ();
4062 if (m
== MATCH_ERROR
)
4068 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4069 && current_ts
.u
.derived
->components
== NULL
4070 && !current_ts
.u
.derived
->attr
.zero_comp
)
4073 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4076 gfc_find_symbol (current_ts
.u
.derived
->name
,
4077 current_ts
.u
.derived
->ns
->parent
, 1, &sym
);
4079 /* Any symbol that we find had better be a type definition
4080 which has its components defined. */
4081 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4082 && (current_ts
.u
.derived
->components
!= NULL
4083 || current_ts
.u
.derived
->attr
.zero_comp
))
4086 /* Now we have an error, which we signal, and then fix up
4087 because the knock-on is plain and simple confusing. */
4088 gfc_error_now ("Derived type at %C has not been previously defined "
4089 "and so cannot appear in a derived type definition");
4090 current_attr
.pointer
= 1;
4095 /* If we have an old-style character declaration, and no new-style
4096 attribute specifications, then there a comma is optional between
4097 the type specification and the variable list. */
4098 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4099 gfc_match_char (',');
4101 /* Give the types/attributes to symbols that follow. Give the element
4102 a number so that repeat character length expressions can be copied. */
4106 num_idents_on_line
++;
4107 m
= variable_decl (elem
++);
4108 if (m
== MATCH_ERROR
)
4113 if (gfc_match_eos () == MATCH_YES
)
4115 if (gfc_match_char (',') != MATCH_YES
)
4119 if (gfc_error_flag_test () == 0)
4120 gfc_error ("Syntax error in data declaration at %C");
4123 gfc_free_data_all (gfc_current_ns
);
4126 gfc_free_array_spec (current_as
);
4132 /* Match a prefix associated with a function or subroutine
4133 declaration. If the typespec pointer is nonnull, then a typespec
4134 can be matched. Note that if nothing matches, MATCH_YES is
4135 returned (the null string was matched). */
4138 gfc_match_prefix (gfc_typespec
*ts
)
4144 gfc_clear_attr (¤t_attr
);
4146 seen_impure
= false;
4148 gcc_assert (!gfc_matching_prefix
);
4149 gfc_matching_prefix
= true;
4153 found_prefix
= false;
4155 if (!seen_type
&& ts
!= NULL
4156 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4157 && gfc_match_space () == MATCH_YES
)
4161 found_prefix
= true;
4164 if (gfc_match ("elemental% ") == MATCH_YES
)
4166 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
4169 found_prefix
= true;
4172 if (gfc_match ("pure% ") == MATCH_YES
)
4174 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4177 found_prefix
= true;
4180 if (gfc_match ("recursive% ") == MATCH_YES
)
4182 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
4185 found_prefix
= true;
4188 /* IMPURE is a somewhat special case, as it needs not set an actual
4189 attribute but rather only prevents ELEMENTAL routines from being
4190 automatically PURE. */
4191 if (gfc_match ("impure% ") == MATCH_YES
)
4193 if (gfc_notify_std (GFC_STD_F2008
,
4194 "Fortran 2008: IMPURE procedure at %C")
4199 found_prefix
= true;
4202 while (found_prefix
);
4204 /* IMPURE and PURE must not both appear, of course. */
4205 if (seen_impure
&& current_attr
.pure
)
4207 gfc_error ("PURE and IMPURE must not appear both at %C");
4211 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4212 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4214 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4218 /* At this point, the next item is not a prefix. */
4219 gcc_assert (gfc_matching_prefix
);
4220 gfc_matching_prefix
= false;
4224 gcc_assert (gfc_matching_prefix
);
4225 gfc_matching_prefix
= false;
4230 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4233 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4235 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
4238 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
4241 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
4248 /* Match a formal argument list. */
4251 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4253 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4254 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4260 if (gfc_match_char ('(') != MATCH_YES
)
4267 if (gfc_match_char (')') == MATCH_YES
)
4272 if (gfc_match_char ('*') == MATCH_YES
)
4276 m
= gfc_match_name (name
);
4280 if (gfc_get_symbol (name
, NULL
, &sym
))
4284 p
= gfc_get_formal_arglist ();
4296 /* We don't add the VARIABLE flavor because the name could be a
4297 dummy procedure. We don't apply these attributes to formal
4298 arguments of statement functions. */
4299 if (sym
!= NULL
&& !st_flag
4300 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
4301 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
4307 /* The name of a program unit can be in a different namespace,
4308 so check for it explicitly. After the statement is accepted,
4309 the name is checked for especially in gfc_get_symbol(). */
4310 if (gfc_new_block
!= NULL
&& sym
!= NULL
4311 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4313 gfc_error ("Name '%s' at %C is the name of the procedure",
4319 if (gfc_match_char (')') == MATCH_YES
)
4322 m
= gfc_match_char (',');
4325 gfc_error ("Unexpected junk in formal argument list at %C");
4331 /* Check for duplicate symbols in the formal argument list. */
4334 for (p
= head
; p
->next
; p
= p
->next
)
4339 for (q
= p
->next
; q
; q
= q
->next
)
4340 if (p
->sym
== q
->sym
)
4342 gfc_error ("Duplicate symbol '%s' in formal argument list "
4343 "at %C", p
->sym
->name
);
4351 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
4361 gfc_free_formal_arglist (head
);
4366 /* Match a RESULT specification following a function declaration or
4367 ENTRY statement. Also matches the end-of-statement. */
4370 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4372 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4376 if (gfc_match (" result (") != MATCH_YES
)
4379 m
= gfc_match_name (name
);
4383 /* Get the right paren, and that's it because there could be the
4384 bind(c) attribute after the result clause. */
4385 if (gfc_match_char(')') != MATCH_YES
)
4387 /* TODO: should report the missing right paren here. */
4391 if (strcmp (function
->name
, name
) == 0)
4393 gfc_error ("RESULT variable at %C must be different than function name");
4397 if (gfc_get_symbol (name
, NULL
, &r
))
4400 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
4409 /* Match a function suffix, which could be a combination of a result
4410 clause and BIND(C), either one, or neither. The draft does not
4411 require them to come in a specific order. */
4414 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4416 match is_bind_c
; /* Found bind(c). */
4417 match is_result
; /* Found result clause. */
4418 match found_match
; /* Status of whether we've found a good match. */
4419 char peek_char
; /* Character we're going to peek at. */
4420 bool allow_binding_name
;
4422 /* Initialize to having found nothing. */
4423 found_match
= MATCH_NO
;
4424 is_bind_c
= MATCH_NO
;
4425 is_result
= MATCH_NO
;
4427 /* Get the next char to narrow between result and bind(c). */
4428 gfc_gobble_whitespace ();
4429 peek_char
= gfc_peek_ascii_char ();
4431 /* C binding names are not allowed for internal procedures. */
4432 if (gfc_current_state () == COMP_CONTAINS
4433 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4434 allow_binding_name
= false;
4436 allow_binding_name
= true;
4441 /* Look for result clause. */
4442 is_result
= match_result (sym
, result
);
4443 if (is_result
== MATCH_YES
)
4445 /* Now see if there is a bind(c) after it. */
4446 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4447 /* We've found the result clause and possibly bind(c). */
4448 found_match
= MATCH_YES
;
4451 /* This should only be MATCH_ERROR. */
4452 found_match
= is_result
;
4455 /* Look for bind(c) first. */
4456 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4457 if (is_bind_c
== MATCH_YES
)
4459 /* Now see if a result clause followed it. */
4460 is_result
= match_result (sym
, result
);
4461 found_match
= MATCH_YES
;
4465 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4466 found_match
= MATCH_ERROR
;
4470 gfc_error ("Unexpected junk after function declaration at %C");
4471 found_match
= MATCH_ERROR
;
4475 if (is_bind_c
== MATCH_YES
)
4477 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4478 if (gfc_current_state () == COMP_CONTAINS
4479 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4480 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4481 "at %L may not be specified for an internal "
4482 "procedure", &gfc_current_locus
)
4486 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4495 /* Procedure pointer return value without RESULT statement:
4496 Add "hidden" result variable named "ppr@". */
4499 add_hidden_procptr_result (gfc_symbol
*sym
)
4503 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4506 /* First usage case: PROCEDURE and EXTERNAL statements. */
4507 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4508 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4509 && sym
->attr
.external
;
4510 /* Second usage case: INTERFACE statements. */
4511 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4512 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4513 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4519 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4523 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4524 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4525 st2
->n
.sym
= stree
->n
.sym
;
4527 sym
->result
= stree
->n
.sym
;
4529 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4530 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4531 sym
->result
->attr
.external
= sym
->attr
.external
;
4532 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4533 sym
->result
->ts
= sym
->ts
;
4534 sym
->attr
.proc_pointer
= 0;
4535 sym
->attr
.pointer
= 0;
4536 sym
->attr
.external
= 0;
4537 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4539 sym
->result
->attr
.pointer
= 0;
4540 sym
->result
->attr
.proc_pointer
= 1;
4543 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4545 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4546 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4547 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4548 && sym
== gfc_current_ns
->proc_name
4549 && sym
== sym
->result
->ns
->proc_name
4550 && strcmp ("ppr@", sym
->result
->name
) == 0)
4552 sym
->result
->attr
.proc_pointer
= 1;
4553 sym
->attr
.pointer
= 0;
4561 /* Match the interface for a PROCEDURE declaration,
4562 including brackets (R1212). */
4565 match_procedure_interface (gfc_symbol
**proc_if
)
4569 locus old_loc
, entry_loc
;
4570 gfc_namespace
*old_ns
= gfc_current_ns
;
4571 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4573 old_loc
= entry_loc
= gfc_current_locus
;
4574 gfc_clear_ts (¤t_ts
);
4576 if (gfc_match (" (") != MATCH_YES
)
4578 gfc_current_locus
= entry_loc
;
4582 /* Get the type spec. for the procedure interface. */
4583 old_loc
= gfc_current_locus
;
4584 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4585 gfc_gobble_whitespace ();
4586 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4589 if (m
== MATCH_ERROR
)
4592 /* Procedure interface is itself a procedure. */
4593 gfc_current_locus
= old_loc
;
4594 m
= gfc_match_name (name
);
4596 /* First look to see if it is already accessible in the current
4597 namespace because it is use associated or contained. */
4599 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4602 /* If it is still not found, then try the parent namespace, if it
4603 exists and create the symbol there if it is still not found. */
4604 if (gfc_current_ns
->parent
)
4605 gfc_current_ns
= gfc_current_ns
->parent
;
4606 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4609 gfc_current_ns
= old_ns
;
4610 *proc_if
= st
->n
.sym
;
4612 /* Various interface checks. */
4616 /* Resolve interface if possible. That way, attr.procedure is only set
4617 if it is declared by a later procedure-declaration-stmt, which is
4618 invalid per C1212. */
4619 while ((*proc_if
)->ts
.interface
)
4620 *proc_if
= (*proc_if
)->ts
.interface
;
4622 if ((*proc_if
)->generic
)
4624 gfc_error ("Interface '%s' at %C may not be generic",
4628 if ((*proc_if
)->attr
.proc
== PROC_ST_FUNCTION
)
4630 gfc_error ("Interface '%s' at %C may not be a statement function",
4634 /* Handle intrinsic procedures. */
4635 if (!((*proc_if
)->attr
.external
|| (*proc_if
)->attr
.use_assoc
4636 || (*proc_if
)->attr
.if_source
== IFSRC_IFBODY
)
4637 && (gfc_is_intrinsic ((*proc_if
), 0, gfc_current_locus
)
4638 || gfc_is_intrinsic ((*proc_if
), 1, gfc_current_locus
)))
4639 (*proc_if
)->attr
.intrinsic
= 1;
4640 if ((*proc_if
)->attr
.intrinsic
4641 && !gfc_intrinsic_actual_ok ((*proc_if
)->name
, 0))
4643 gfc_error ("Intrinsic procedure '%s' not allowed "
4644 "in PROCEDURE statement at %C", (*proc_if
)->name
);
4650 if (gfc_match (" )") != MATCH_YES
)
4652 gfc_current_locus
= entry_loc
;
4660 /* Match a PROCEDURE declaration (R1211). */
4663 match_procedure_decl (void)
4666 gfc_symbol
*sym
, *proc_if
= NULL
;
4668 gfc_expr
*initializer
= NULL
;
4670 /* Parse interface (with brackets). */
4671 m
= match_procedure_interface (&proc_if
);
4675 /* Parse attributes (with colons). */
4676 m
= match_attr_spec();
4677 if (m
== MATCH_ERROR
)
4680 /* Get procedure symbols. */
4683 m
= gfc_match_symbol (&sym
, 0);
4686 else if (m
== MATCH_ERROR
)
4689 /* Add current_attr to the symbol attributes. */
4690 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4693 if (sym
->attr
.is_bind_c
)
4695 /* Check for C1218. */
4696 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4698 gfc_error ("BIND(C) attribute at %C requires "
4699 "an interface with BIND(C)");
4702 /* Check for C1217. */
4703 if (has_name_equals
&& sym
->attr
.pointer
)
4705 gfc_error ("BIND(C) procedure with NAME may not have "
4706 "POINTER attribute at %C");
4709 if (has_name_equals
&& sym
->attr
.dummy
)
4711 gfc_error ("Dummy procedure at %C may not have "
4712 "BIND(C) attribute with NAME");
4715 /* Set binding label for BIND(C). */
4716 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4720 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4723 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4726 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4729 /* Set interface. */
4730 if (proc_if
!= NULL
)
4732 if (sym
->ts
.type
!= BT_UNKNOWN
)
4734 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4735 sym
->name
, &gfc_current_locus
,
4736 gfc_basic_typename (sym
->ts
.type
));
4739 sym
->ts
.interface
= proc_if
;
4740 sym
->attr
.untyped
= 1;
4741 sym
->attr
.if_source
= IFSRC_IFBODY
;
4743 else if (current_ts
.type
!= BT_UNKNOWN
)
4745 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4747 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4748 sym
->ts
.interface
->ts
= current_ts
;
4749 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4750 sym
->ts
.interface
->attr
.function
= 1;
4751 sym
->attr
.function
= 1;
4752 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4755 if (gfc_match (" =>") == MATCH_YES
)
4757 if (!current_attr
.pointer
)
4759 gfc_error ("Initialization at %C isn't for a pointer variable");
4764 m
= match_pointer_init (&initializer
, 1);
4768 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4774 gfc_set_sym_referenced (sym
);
4776 if (gfc_match_eos () == MATCH_YES
)
4778 if (gfc_match_char (',') != MATCH_YES
)
4783 gfc_error ("Syntax error in PROCEDURE statement at %C");
4787 /* Free stuff up and return. */
4788 gfc_free_expr (initializer
);
4794 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4797 /* Match a procedure pointer component declaration (R445). */
4800 match_ppc_decl (void)
4803 gfc_symbol
*proc_if
= NULL
;
4807 gfc_expr
*initializer
= NULL
;
4808 gfc_typebound_proc
* tb
;
4809 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4811 /* Parse interface (with brackets). */
4812 m
= match_procedure_interface (&proc_if
);
4816 /* Parse attributes. */
4817 tb
= XCNEW (gfc_typebound_proc
);
4818 tb
->where
= gfc_current_locus
;
4819 m
= match_binding_attributes (tb
, false, true);
4820 if (m
== MATCH_ERROR
)
4823 gfc_clear_attr (¤t_attr
);
4824 current_attr
.procedure
= 1;
4825 current_attr
.proc_pointer
= 1;
4826 current_attr
.access
= tb
->access
;
4827 current_attr
.flavor
= FL_PROCEDURE
;
4829 /* Match the colons (required). */
4830 if (gfc_match (" ::") != MATCH_YES
)
4832 gfc_error ("Expected '::' after binding-attributes at %C");
4836 /* Check for C450. */
4837 if (!tb
->nopass
&& proc_if
== NULL
)
4839 gfc_error("NOPASS or explicit interface required at %C");
4843 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure pointer "
4844 "component at %C") == FAILURE
)
4847 /* Match PPC names. */
4851 m
= gfc_match_name (name
);
4854 else if (m
== MATCH_ERROR
)
4857 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
4860 /* Add current_attr to the symbol attributes. */
4861 if (gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4864 if (gfc_add_external (&c
->attr
, NULL
) == FAILURE
)
4867 if (gfc_add_proc (&c
->attr
, name
, NULL
) == FAILURE
)
4872 /* Set interface. */
4873 if (proc_if
!= NULL
)
4875 c
->ts
.interface
= proc_if
;
4876 c
->attr
.untyped
= 1;
4877 c
->attr
.if_source
= IFSRC_IFBODY
;
4879 else if (ts
.type
!= BT_UNKNOWN
)
4882 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4883 c
->ts
.interface
->ts
= ts
;
4884 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4885 c
->ts
.interface
->attr
.function
= 1;
4886 c
->attr
.function
= 1;
4887 c
->attr
.if_source
= IFSRC_UNKNOWN
;
4890 if (gfc_match (" =>") == MATCH_YES
)
4892 m
= match_pointer_init (&initializer
, 1);
4895 gfc_free_expr (initializer
);
4898 c
->initializer
= initializer
;
4901 if (gfc_match_eos () == MATCH_YES
)
4903 if (gfc_match_char (',') != MATCH_YES
)
4908 gfc_error ("Syntax error in procedure pointer component at %C");
4913 /* Match a PROCEDURE declaration inside an interface (R1206). */
4916 match_procedure_in_interface (void)
4920 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4922 if (current_interface
.type
== INTERFACE_NAMELESS
4923 || current_interface
.type
== INTERFACE_ABSTRACT
)
4925 gfc_error ("PROCEDURE at %C must be in a generic interface");
4931 m
= gfc_match_name (name
);
4934 else if (m
== MATCH_ERROR
)
4936 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4939 if (gfc_add_interface (sym
) == FAILURE
)
4942 if (gfc_match_eos () == MATCH_YES
)
4944 if (gfc_match_char (',') != MATCH_YES
)
4951 gfc_error ("Syntax error in PROCEDURE statement at %C");
4956 /* General matcher for PROCEDURE declarations. */
4958 static match
match_procedure_in_type (void);
4961 gfc_match_procedure (void)
4965 switch (gfc_current_state ())
4970 case COMP_SUBROUTINE
:
4972 m
= match_procedure_decl ();
4974 case COMP_INTERFACE
:
4975 m
= match_procedure_in_interface ();
4978 m
= match_ppc_decl ();
4980 case COMP_DERIVED_CONTAINS
:
4981 m
= match_procedure_in_type ();
4990 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4998 /* Warn if a matched procedure has the same name as an intrinsic; this is
4999 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5000 parser-state-stack to find out whether we're in a module. */
5003 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5007 in_module
= (gfc_state_stack
->previous
5008 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5010 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5014 /* Match a function declaration. */
5017 gfc_match_function_decl (void)
5019 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5020 gfc_symbol
*sym
, *result
;
5024 match found_match
; /* Status returned by match func. */
5026 if (gfc_current_state () != COMP_NONE
5027 && gfc_current_state () != COMP_INTERFACE
5028 && gfc_current_state () != COMP_CONTAINS
)
5031 gfc_clear_ts (¤t_ts
);
5033 old_loc
= gfc_current_locus
;
5035 m
= gfc_match_prefix (¤t_ts
);
5038 gfc_current_locus
= old_loc
;
5042 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5044 gfc_current_locus
= old_loc
;
5047 if (get_proc_name (name
, &sym
, false))
5050 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5053 gfc_new_block
= sym
;
5055 m
= gfc_match_formal_arglist (sym
, 0, 0);
5058 gfc_error ("Expected formal argument list in function "
5059 "definition at %C");
5063 else if (m
== MATCH_ERROR
)
5068 /* According to the draft, the bind(c) and result clause can
5069 come in either order after the formal_arg_list (i.e., either
5070 can be first, both can exist together or by themselves or neither
5071 one). Therefore, the match_result can't match the end of the
5072 string, and check for the bind(c) or result clause in either order. */
5073 found_match
= gfc_match_eos ();
5075 /* Make sure that it isn't already declared as BIND(C). If it is, it
5076 must have been marked BIND(C) with a BIND(C) attribute and that is
5077 not allowed for procedures. */
5078 if (sym
->attr
.is_bind_c
== 1)
5080 sym
->attr
.is_bind_c
= 0;
5081 if (sym
->old_symbol
!= NULL
)
5082 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5083 "variables or common blocks",
5084 &(sym
->old_symbol
->declared_at
));
5086 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5087 "variables or common blocks", &gfc_current_locus
);
5090 if (found_match
!= MATCH_YES
)
5092 /* If we haven't found the end-of-statement, look for a suffix. */
5093 suffix_match
= gfc_match_suffix (sym
, &result
);
5094 if (suffix_match
== MATCH_YES
)
5095 /* Need to get the eos now. */
5096 found_match
= gfc_match_eos ();
5098 found_match
= suffix_match
;
5101 if(found_match
!= MATCH_YES
)
5105 /* Make changes to the symbol. */
5108 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5111 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
5112 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5115 /* Delay matching the function characteristics until after the
5116 specification block by signalling kind=-1. */
5117 sym
->declared_at
= old_loc
;
5118 if (current_ts
.type
!= BT_UNKNOWN
)
5119 current_ts
.kind
= -1;
5121 current_ts
.kind
= 0;
5125 if (current_ts
.type
!= BT_UNKNOWN
5126 && gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
5132 if (current_ts
.type
!= BT_UNKNOWN
5133 && gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
)
5136 sym
->result
= result
;
5139 /* Warn if this procedure has the same name as an intrinsic. */
5140 warn_intrinsic_shadow (sym
, true);
5146 gfc_current_locus
= old_loc
;
5151 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5152 pass the name of the entry, rather than the gfc_current_block name, and
5153 to return false upon finding an existing global entry. */
5156 add_global_entry (const char *name
, int sub
)
5159 enum gfc_symbol_type type
;
5161 s
= gfc_get_gsymbol(name
);
5162 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5165 || (s
->type
!= GSYM_UNKNOWN
5166 && s
->type
!= type
))
5167 gfc_global_used(s
, NULL
);
5171 s
->where
= gfc_current_locus
;
5173 s
->ns
= gfc_current_ns
;
5180 /* Match an ENTRY statement. */
5183 gfc_match_entry (void)
5188 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5189 gfc_compile_state state
;
5193 bool module_procedure
;
5197 m
= gfc_match_name (name
);
5201 if (gfc_notify_std (GFC_STD_F2008_OBS
, "Fortran 2008 obsolescent feature: "
5202 "ENTRY statement at %C") == FAILURE
)
5205 state
= gfc_current_state ();
5206 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5211 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5214 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5216 case COMP_BLOCK_DATA
:
5217 gfc_error ("ENTRY statement at %C cannot appear within "
5220 case COMP_INTERFACE
:
5221 gfc_error ("ENTRY statement at %C cannot appear within "
5225 gfc_error ("ENTRY statement at %C cannot appear within "
5226 "a DERIVED TYPE block");
5229 gfc_error ("ENTRY statement at %C cannot appear within "
5230 "an IF-THEN block");
5233 gfc_error ("ENTRY statement at %C cannot appear within "
5237 gfc_error ("ENTRY statement at %C cannot appear within "
5241 gfc_error ("ENTRY statement at %C cannot appear within "
5245 gfc_error ("ENTRY statement at %C cannot appear within "
5249 gfc_error ("ENTRY statement at %C cannot appear within "
5250 "a contained subprogram");
5253 gfc_internal_error ("gfc_match_entry(): Bad state");
5258 module_procedure
= gfc_current_ns
->parent
!= NULL
5259 && gfc_current_ns
->parent
->proc_name
5260 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5263 if (gfc_current_ns
->parent
!= NULL
5264 && gfc_current_ns
->parent
->proc_name
5265 && !module_procedure
)
5267 gfc_error("ENTRY statement at %C cannot appear in a "
5268 "contained procedure");
5272 /* Module function entries need special care in get_proc_name
5273 because previous references within the function will have
5274 created symbols attached to the current namespace. */
5275 if (get_proc_name (name
, &entry
,
5276 gfc_current_ns
->parent
!= NULL
5277 && module_procedure
))
5280 proc
= gfc_current_block ();
5282 /* Make sure that it isn't already declared as BIND(C). If it is, it
5283 must have been marked BIND(C) with a BIND(C) attribute and that is
5284 not allowed for procedures. */
5285 if (entry
->attr
.is_bind_c
== 1)
5287 entry
->attr
.is_bind_c
= 0;
5288 if (entry
->old_symbol
!= NULL
)
5289 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5290 "variables or common blocks",
5291 &(entry
->old_symbol
->declared_at
));
5293 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5294 "variables or common blocks", &gfc_current_locus
);
5297 /* Check what next non-whitespace character is so we can tell if there
5298 is the required parens if we have a BIND(C). */
5299 gfc_gobble_whitespace ();
5300 peek_char
= gfc_peek_ascii_char ();
5302 if (state
== COMP_SUBROUTINE
)
5304 /* An entry in a subroutine. */
5305 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
5308 m
= gfc_match_formal_arglist (entry
, 0, 1);
5312 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5313 never be an internal procedure. */
5314 is_bind_c
= gfc_match_bind_c (entry
, true);
5315 if (is_bind_c
== MATCH_ERROR
)
5317 if (is_bind_c
== MATCH_YES
)
5319 if (peek_char
!= '(')
5321 gfc_error ("Missing required parentheses before BIND(C) at %C");
5324 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
5329 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5330 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5335 /* An entry in a function.
5336 We need to take special care because writing
5341 ENTRY f() RESULT (r)
5343 ENTRY f RESULT (r). */
5344 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
5347 old_loc
= gfc_current_locus
;
5348 if (gfc_match_eos () == MATCH_YES
)
5350 gfc_current_locus
= old_loc
;
5351 /* Match the empty argument list, and add the interface to
5353 m
= gfc_match_formal_arglist (entry
, 0, 1);
5356 m
= gfc_match_formal_arglist (entry
, 0, 0);
5363 if (gfc_match_eos () == MATCH_YES
)
5365 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5366 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5369 entry
->result
= entry
;
5373 m
= gfc_match_suffix (entry
, &result
);
5375 gfc_syntax_error (ST_ENTRY
);
5381 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
5382 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
5383 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
5386 entry
->result
= result
;
5390 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5391 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5393 entry
->result
= entry
;
5398 if (gfc_match_eos () != MATCH_YES
)
5400 gfc_syntax_error (ST_ENTRY
);
5404 entry
->attr
.recursive
= proc
->attr
.recursive
;
5405 entry
->attr
.elemental
= proc
->attr
.elemental
;
5406 entry
->attr
.pure
= proc
->attr
.pure
;
5408 el
= gfc_get_entry_list ();
5410 el
->next
= gfc_current_ns
->entries
;
5411 gfc_current_ns
->entries
= el
;
5413 el
->id
= el
->next
->id
+ 1;
5417 new_st
.op
= EXEC_ENTRY
;
5418 new_st
.ext
.entry
= el
;
5424 /* Match a subroutine statement, including optional prefixes. */
5427 gfc_match_subroutine (void)
5429 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5434 bool allow_binding_name
;
5436 if (gfc_current_state () != COMP_NONE
5437 && gfc_current_state () != COMP_INTERFACE
5438 && gfc_current_state () != COMP_CONTAINS
)
5441 m
= gfc_match_prefix (NULL
);
5445 m
= gfc_match ("subroutine% %n", name
);
5449 if (get_proc_name (name
, &sym
, false))
5452 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5453 the symbol existed before. */
5454 sym
->declared_at
= gfc_current_locus
;
5456 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5459 gfc_new_block
= sym
;
5461 /* Check what next non-whitespace character is so we can tell if there
5462 is the required parens if we have a BIND(C). */
5463 gfc_gobble_whitespace ();
5464 peek_char
= gfc_peek_ascii_char ();
5466 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5469 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5472 /* Make sure that it isn't already declared as BIND(C). If it is, it
5473 must have been marked BIND(C) with a BIND(C) attribute and that is
5474 not allowed for procedures. */
5475 if (sym
->attr
.is_bind_c
== 1)
5477 sym
->attr
.is_bind_c
= 0;
5478 if (sym
->old_symbol
!= NULL
)
5479 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5480 "variables or common blocks",
5481 &(sym
->old_symbol
->declared_at
));
5483 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5484 "variables or common blocks", &gfc_current_locus
);
5487 /* C binding names are not allowed for internal procedures. */
5488 if (gfc_current_state () == COMP_CONTAINS
5489 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5490 allow_binding_name
= false;
5492 allow_binding_name
= true;
5494 /* Here, we are just checking if it has the bind(c) attribute, and if
5495 so, then we need to make sure it's all correct. If it doesn't,
5496 we still need to continue matching the rest of the subroutine line. */
5497 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5498 if (is_bind_c
== MATCH_ERROR
)
5500 /* There was an attempt at the bind(c), but it was wrong. An
5501 error message should have been printed w/in the gfc_match_bind_c
5502 so here we'll just return the MATCH_ERROR. */
5506 if (is_bind_c
== MATCH_YES
)
5508 /* The following is allowed in the Fortran 2008 draft. */
5509 if (gfc_current_state () == COMP_CONTAINS
5510 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5511 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
5512 "at %L may not be specified for an internal "
5513 "procedure", &gfc_current_locus
)
5517 if (peek_char
!= '(')
5519 gfc_error ("Missing required parentheses before BIND(C) at %C");
5522 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
5527 if (gfc_match_eos () != MATCH_YES
)
5529 gfc_syntax_error (ST_SUBROUTINE
);
5533 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5536 /* Warn if it has the same name as an intrinsic. */
5537 warn_intrinsic_shadow (sym
, false);
5543 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5544 given, and set the binding label in either the given symbol (if not
5545 NULL), or in the current_ts. The symbol may be NULL because we may
5546 encounter the BIND(C) before the declaration itself. Return
5547 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5548 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5549 or MATCH_YES if the specifier was correct and the binding label and
5550 bind(c) fields were set correctly for the given symbol or the
5551 current_ts. If allow_binding_name is false, no binding name may be
5555 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5557 /* binding label, if exists */
5558 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
5562 /* Initialize the flag that specifies whether we encountered a NAME=
5563 specifier or not. */
5564 has_name_equals
= 0;
5566 /* Init the first char to nil so we can catch if we don't have
5567 the label (name attr) or the symbol name yet. */
5568 binding_label
[0] = '\0';
5570 /* This much we have to be able to match, in this order, if
5571 there is a bind(c) label. */
5572 if (gfc_match (" bind ( c ") != MATCH_YES
)
5575 /* Now see if there is a binding label, or if we've reached the
5576 end of the bind(c) attribute without one. */
5577 if (gfc_match_char (',') == MATCH_YES
)
5579 if (gfc_match (" name = ") != MATCH_YES
)
5581 gfc_error ("Syntax error in NAME= specifier for binding label "
5583 /* should give an error message here */
5587 has_name_equals
= 1;
5589 /* Get the opening quote. */
5590 double_quote
= MATCH_YES
;
5591 single_quote
= MATCH_YES
;
5592 double_quote
= gfc_match_char ('"');
5593 if (double_quote
!= MATCH_YES
)
5594 single_quote
= gfc_match_char ('\'');
5595 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5597 gfc_error ("Syntax error in NAME= specifier for binding label "
5602 /* Grab the binding label, using functions that will not lower
5603 case the names automatically. */
5604 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
5607 /* Get the closing quotation. */
5608 if (double_quote
== MATCH_YES
)
5610 if (gfc_match_char ('"') != MATCH_YES
)
5612 gfc_error ("Missing closing quote '\"' for binding label at %C");
5613 /* User started string with '"' so looked to match it. */
5619 if (gfc_match_char ('\'') != MATCH_YES
)
5621 gfc_error ("Missing closing quote '\'' for binding label at %C");
5622 /* User started string with "'" char. */
5628 /* Get the required right paren. */
5629 if (gfc_match_char (')') != MATCH_YES
)
5631 gfc_error ("Missing closing paren for binding label at %C");
5635 if (has_name_equals
&& !allow_binding_name
)
5637 gfc_error ("No binding name is allowed in BIND(C) at %C");
5641 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5643 gfc_error ("For dummy procedure %s, no binding name is "
5644 "allowed in BIND(C) at %C", sym
->name
);
5649 /* Save the binding label to the symbol. If sym is null, we're
5650 probably matching the typespec attributes of a declaration and
5651 haven't gotten the name yet, and therefore, no symbol yet. */
5652 if (binding_label
[0] != '\0')
5656 strcpy (sym
->binding_label
, binding_label
);
5659 strcpy (curr_binding_label
, binding_label
);
5661 else if (allow_binding_name
)
5663 /* No binding label, but if symbol isn't null, we
5664 can set the label for it here.
5665 If name="" or allow_binding_name is false, no C binding name is
5667 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5668 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
5671 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5672 && current_interface
.type
== INTERFACE_ABSTRACT
)
5674 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5682 /* Return nonzero if we're currently compiling a contained procedure. */
5685 contained_procedure (void)
5687 gfc_state_data
*s
= gfc_state_stack
;
5689 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5690 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5696 /* Set the kind of each enumerator. The kind is selected such that it is
5697 interoperable with the corresponding C enumeration type, making
5698 sure that -fshort-enums is honored. */
5703 enumerator_history
*current_history
= NULL
;
5707 if (max_enum
== NULL
|| enum_history
== NULL
)
5710 if (!flag_short_enums
)
5716 kind
= gfc_integer_kinds
[i
++].kind
;
5718 while (kind
< gfc_c_int_kind
5719 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5722 current_history
= enum_history
;
5723 while (current_history
!= NULL
)
5725 current_history
->sym
->ts
.kind
= kind
;
5726 current_history
= current_history
->next
;
5731 /* Match any of the various end-block statements. Returns the type of
5732 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5733 and END BLOCK statements cannot be replaced by a single END statement. */
5736 gfc_match_end (gfc_statement
*st
)
5738 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5739 gfc_compile_state state
;
5741 const char *block_name
;
5746 old_loc
= gfc_current_locus
;
5747 if (gfc_match ("end") != MATCH_YES
)
5750 state
= gfc_current_state ();
5751 block_name
= gfc_current_block () == NULL
5752 ? NULL
: gfc_current_block ()->name
;
5756 case COMP_ASSOCIATE
:
5758 if (!strncmp (block_name
, "block@", strlen("block@")))
5763 case COMP_DERIVED_CONTAINS
:
5764 state
= gfc_state_stack
->previous
->state
;
5765 block_name
= gfc_state_stack
->previous
->sym
== NULL
5766 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5777 *st
= ST_END_PROGRAM
;
5778 target
= " program";
5782 case COMP_SUBROUTINE
:
5783 *st
= ST_END_SUBROUTINE
;
5784 target
= " subroutine";
5785 eos_ok
= !contained_procedure ();
5789 *st
= ST_END_FUNCTION
;
5790 target
= " function";
5791 eos_ok
= !contained_procedure ();
5794 case COMP_BLOCK_DATA
:
5795 *st
= ST_END_BLOCK_DATA
;
5796 target
= " block data";
5801 *st
= ST_END_MODULE
;
5806 case COMP_INTERFACE
:
5807 *st
= ST_END_INTERFACE
;
5808 target
= " interface";
5813 case COMP_DERIVED_CONTAINS
:
5819 case COMP_ASSOCIATE
:
5820 *st
= ST_END_ASSOCIATE
;
5821 target
= " associate";
5844 *st
= ST_END_CRITICAL
;
5845 target
= " critical";
5850 case COMP_SELECT_TYPE
:
5851 *st
= ST_END_SELECT
;
5857 *st
= ST_END_FORALL
;
5872 last_initializer
= NULL
;
5874 gfc_free_enum_history ();
5878 gfc_error ("Unexpected END statement at %C");
5882 if (gfc_match_eos () == MATCH_YES
)
5884 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
5886 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: END statement "
5887 "instead of %s statement at %L",
5888 gfc_ascii_statement (*st
), &old_loc
) == FAILURE
)
5893 /* We would have required END [something]. */
5894 gfc_error ("%s statement expected at %L",
5895 gfc_ascii_statement (*st
), &old_loc
);
5902 /* Verify that we've got the sort of end-block that we're expecting. */
5903 if (gfc_match (target
) != MATCH_YES
)
5905 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
5909 /* If we're at the end, make sure a block name wasn't required. */
5910 if (gfc_match_eos () == MATCH_YES
)
5913 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
5914 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
5915 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
5921 gfc_error ("Expected block name of '%s' in %s statement at %C",
5922 block_name
, gfc_ascii_statement (*st
));
5927 /* END INTERFACE has a special handler for its several possible endings. */
5928 if (*st
== ST_END_INTERFACE
)
5929 return gfc_match_end_interface ();
5931 /* We haven't hit the end of statement, so what is left must be an
5933 m
= gfc_match_space ();
5935 m
= gfc_match_name (name
);
5938 gfc_error ("Expected terminating name at %C");
5942 if (block_name
== NULL
)
5945 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
5947 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
5948 gfc_ascii_statement (*st
));
5951 /* Procedure pointer as function result. */
5952 else if (strcmp (block_name
, "ppr@") == 0
5953 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
5955 gfc_error ("Expected label '%s' for %s statement at %C",
5956 gfc_current_block ()->ns
->proc_name
->name
,
5957 gfc_ascii_statement (*st
));
5961 if (gfc_match_eos () == MATCH_YES
)
5965 gfc_syntax_error (*st
);
5968 gfc_current_locus
= old_loc
;
5974 /***************** Attribute declaration statements ****************/
5976 /* Set the attribute of a single variable. */
5981 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5989 m
= gfc_match_name (name
);
5993 if (find_special (name
, &sym
, false))
5996 var_locus
= gfc_current_locus
;
5998 /* Deal with possible array specification for certain attributes. */
5999 if (current_attr
.dimension
6000 || current_attr
.codimension
6001 || current_attr
.allocatable
6002 || current_attr
.pointer
6003 || current_attr
.target
)
6005 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6006 !current_attr
.dimension
6007 && !current_attr
.pointer
6008 && !current_attr
.target
);
6009 if (m
== MATCH_ERROR
)
6012 if (current_attr
.dimension
&& m
== MATCH_NO
)
6014 gfc_error ("Missing array specification at %L in DIMENSION "
6015 "statement", &var_locus
);
6020 if (current_attr
.dimension
&& sym
->value
)
6022 gfc_error ("Dimensions specified for %s at %L after its "
6023 "initialisation", sym
->name
, &var_locus
);
6028 if (current_attr
.codimension
&& m
== MATCH_NO
)
6030 gfc_error ("Missing array specification at %L in CODIMENSION "
6031 "statement", &var_locus
);
6036 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6037 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6039 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6045 /* Update symbol table. DIMENSION attribute is set in
6046 gfc_set_array_spec(). For CLASS variables, this must be applied
6047 to the first component, or '_data' field. */
6048 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6050 if (gfc_copy_attr (&CLASS_DATA (sym
)->attr
, ¤t_attr
, &var_locus
)
6059 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6060 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
6067 if (sym
->ts
.type
== BT_CLASS
6068 && gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false) == FAILURE
)
6074 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
6080 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6082 /* Fix the array spec. */
6083 m
= gfc_mod_pointee_as (sym
->as
);
6084 if (m
== MATCH_ERROR
)
6088 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
6094 if ((current_attr
.external
|| current_attr
.intrinsic
)
6095 && sym
->attr
.flavor
!= FL_PROCEDURE
6096 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
6102 add_hidden_procptr_result (sym
);
6107 gfc_free_array_spec (as
);
6112 /* Generic attribute declaration subroutine. Used for attributes that
6113 just have a list of names. */
6120 /* Gobble the optional double colon, by simply ignoring the result
6130 if (gfc_match_eos () == MATCH_YES
)
6136 if (gfc_match_char (',') != MATCH_YES
)
6138 gfc_error ("Unexpected character in variable list at %C");
6148 /* This routine matches Cray Pointer declarations of the form:
6149 pointer ( <pointer>, <pointee> )
6151 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6152 The pointer, if already declared, should be an integer. Otherwise, we
6153 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6154 be either a scalar, or an array declaration. No space is allocated for
6155 the pointee. For the statement
6156 pointer (ipt, ar(10))
6157 any subsequent uses of ar will be translated (in C-notation) as
6158 ar(i) => ((<type> *) ipt)(i)
6159 After gimplification, pointee variable will disappear in the code. */
6162 cray_pointer_decl (void)
6165 gfc_array_spec
*as
= NULL
;
6166 gfc_symbol
*cptr
; /* Pointer symbol. */
6167 gfc_symbol
*cpte
; /* Pointee symbol. */
6173 if (gfc_match_char ('(') != MATCH_YES
)
6175 gfc_error ("Expected '(' at %C");
6179 /* Match pointer. */
6180 var_locus
= gfc_current_locus
;
6181 gfc_clear_attr (¤t_attr
);
6182 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6183 current_ts
.type
= BT_INTEGER
;
6184 current_ts
.kind
= gfc_index_integer_kind
;
6186 m
= gfc_match_symbol (&cptr
, 0);
6189 gfc_error ("Expected variable name at %C");
6193 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
6196 gfc_set_sym_referenced (cptr
);
6198 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6200 cptr
->ts
.type
= BT_INTEGER
;
6201 cptr
->ts
.kind
= gfc_index_integer_kind
;
6203 else if (cptr
->ts
.type
!= BT_INTEGER
)
6205 gfc_error ("Cray pointer at %C must be an integer");
6208 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6209 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6210 " memory addresses require %d bytes",
6211 cptr
->ts
.kind
, gfc_index_integer_kind
);
6213 if (gfc_match_char (',') != MATCH_YES
)
6215 gfc_error ("Expected \",\" at %C");
6219 /* Match Pointee. */
6220 var_locus
= gfc_current_locus
;
6221 gfc_clear_attr (¤t_attr
);
6222 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6223 current_ts
.type
= BT_UNKNOWN
;
6224 current_ts
.kind
= 0;
6226 m
= gfc_match_symbol (&cpte
, 0);
6229 gfc_error ("Expected variable name at %C");
6233 /* Check for an optional array spec. */
6234 m
= gfc_match_array_spec (&as
, true, false);
6235 if (m
== MATCH_ERROR
)
6237 gfc_free_array_spec (as
);
6240 else if (m
== MATCH_NO
)
6242 gfc_free_array_spec (as
);
6246 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
6249 gfc_set_sym_referenced (cpte
);
6251 if (cpte
->as
== NULL
)
6253 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
6254 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6256 else if (as
!= NULL
)
6258 gfc_error ("Duplicate array spec for Cray pointee at %C");
6259 gfc_free_array_spec (as
);
6265 if (cpte
->as
!= NULL
)
6267 /* Fix array spec. */
6268 m
= gfc_mod_pointee_as (cpte
->as
);
6269 if (m
== MATCH_ERROR
)
6273 /* Point the Pointee at the Pointer. */
6274 cpte
->cp_pointer
= cptr
;
6276 if (gfc_match_char (')') != MATCH_YES
)
6278 gfc_error ("Expected \")\" at %C");
6281 m
= gfc_match_char (',');
6283 done
= true; /* Stop searching for more declarations. */
6287 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6288 || gfc_match_eos () != MATCH_YES
)
6290 gfc_error ("Expected \",\" or end of statement at %C");
6298 gfc_match_external (void)
6301 gfc_clear_attr (¤t_attr
);
6302 current_attr
.external
= 1;
6304 return attr_decl ();
6309 gfc_match_intent (void)
6313 /* This is not allowed within a BLOCK construct! */
6314 if (gfc_current_state () == COMP_BLOCK
)
6316 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6320 intent
= match_intent_spec ();
6321 if (intent
== INTENT_UNKNOWN
)
6324 gfc_clear_attr (¤t_attr
);
6325 current_attr
.intent
= intent
;
6327 return attr_decl ();
6332 gfc_match_intrinsic (void)
6335 gfc_clear_attr (¤t_attr
);
6336 current_attr
.intrinsic
= 1;
6338 return attr_decl ();
6343 gfc_match_optional (void)
6345 /* This is not allowed within a BLOCK construct! */
6346 if (gfc_current_state () == COMP_BLOCK
)
6348 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6352 gfc_clear_attr (¤t_attr
);
6353 current_attr
.optional
= 1;
6355 return attr_decl ();
6360 gfc_match_pointer (void)
6362 gfc_gobble_whitespace ();
6363 if (gfc_peek_ascii_char () == '(')
6365 if (!gfc_option
.flag_cray_pointer
)
6367 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6371 return cray_pointer_decl ();
6375 gfc_clear_attr (¤t_attr
);
6376 current_attr
.pointer
= 1;
6378 return attr_decl ();
6384 gfc_match_allocatable (void)
6386 gfc_clear_attr (¤t_attr
);
6387 current_attr
.allocatable
= 1;
6389 return attr_decl ();
6394 gfc_match_codimension (void)
6396 gfc_clear_attr (¤t_attr
);
6397 current_attr
.codimension
= 1;
6399 return attr_decl ();
6404 gfc_match_contiguous (void)
6406 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTIGUOUS statement at %C")
6410 gfc_clear_attr (¤t_attr
);
6411 current_attr
.contiguous
= 1;
6413 return attr_decl ();
6418 gfc_match_dimension (void)
6420 gfc_clear_attr (¤t_attr
);
6421 current_attr
.dimension
= 1;
6423 return attr_decl ();
6428 gfc_match_target (void)
6430 gfc_clear_attr (¤t_attr
);
6431 current_attr
.target
= 1;
6433 return attr_decl ();
6437 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6441 access_attr_decl (gfc_statement st
)
6443 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6444 interface_type type
;
6447 gfc_intrinsic_op op
;
6450 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6455 m
= gfc_match_generic_spec (&type
, name
, &op
);
6458 if (m
== MATCH_ERROR
)
6463 case INTERFACE_NAMELESS
:
6464 case INTERFACE_ABSTRACT
:
6467 case INTERFACE_GENERIC
:
6468 if (gfc_get_symbol (name
, NULL
, &sym
))
6471 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
6472 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6473 sym
->name
, NULL
) == FAILURE
)
6478 case INTERFACE_INTRINSIC_OP
:
6479 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6481 gfc_intrinsic_op other_op
;
6483 gfc_current_ns
->operator_access
[op
] =
6484 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6486 /* Handle the case if there is another op with the same
6487 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6488 other_op
= gfc_equivalent_op (op
);
6490 if (other_op
!= INTRINSIC_NONE
)
6491 gfc_current_ns
->operator_access
[other_op
] =
6492 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6497 gfc_error ("Access specification of the %s operator at %C has "
6498 "already been specified", gfc_op2string (op
));
6504 case INTERFACE_USER_OP
:
6505 uop
= gfc_get_uop (name
);
6507 if (uop
->access
== ACCESS_UNKNOWN
)
6509 uop
->access
= (st
== ST_PUBLIC
)
6510 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6514 gfc_error ("Access specification of the .%s. operator at %C "
6515 "has already been specified", sym
->name
);
6522 if (gfc_match_char (',') == MATCH_NO
)
6526 if (gfc_match_eos () != MATCH_YES
)
6531 gfc_syntax_error (st
);
6539 gfc_match_protected (void)
6544 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6546 gfc_error ("PROTECTED at %C only allowed in specification "
6547 "part of a module");
6552 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
6556 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6561 if (gfc_match_eos () == MATCH_YES
)
6566 m
= gfc_match_symbol (&sym
, 0);
6570 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6583 if (gfc_match_eos () == MATCH_YES
)
6585 if (gfc_match_char (',') != MATCH_YES
)
6592 gfc_error ("Syntax error in PROTECTED statement at %C");
6597 /* The PRIVATE statement is a bit weird in that it can be an attribute
6598 declaration, but also works as a standalone statement inside of a
6599 type declaration or a module. */
6602 gfc_match_private (gfc_statement
*st
)
6605 if (gfc_match ("private") != MATCH_YES
)
6608 if (gfc_current_state () != COMP_MODULE
6609 && !(gfc_current_state () == COMP_DERIVED
6610 && gfc_state_stack
->previous
6611 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6612 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6613 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6614 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6616 gfc_error ("PRIVATE statement at %C is only allowed in the "
6617 "specification part of a module");
6621 if (gfc_current_state () == COMP_DERIVED
)
6623 if (gfc_match_eos () == MATCH_YES
)
6629 gfc_syntax_error (ST_PRIVATE
);
6633 if (gfc_match_eos () == MATCH_YES
)
6640 return access_attr_decl (ST_PRIVATE
);
6645 gfc_match_public (gfc_statement
*st
)
6648 if (gfc_match ("public") != MATCH_YES
)
6651 if (gfc_current_state () != COMP_MODULE
)
6653 gfc_error ("PUBLIC statement at %C is only allowed in the "
6654 "specification part of a module");
6658 if (gfc_match_eos () == MATCH_YES
)
6665 return access_attr_decl (ST_PUBLIC
);
6669 /* Workhorse for gfc_match_parameter. */
6679 m
= gfc_match_symbol (&sym
, 0);
6681 gfc_error ("Expected variable name at %C in PARAMETER statement");
6686 if (gfc_match_char ('=') == MATCH_NO
)
6688 gfc_error ("Expected = sign in PARAMETER statement at %C");
6692 m
= gfc_match_init_expr (&init
);
6694 gfc_error ("Expected expression at %C in PARAMETER statement");
6698 if (sym
->ts
.type
== BT_UNKNOWN
6699 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6705 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6706 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6714 gfc_error ("Initializing already initialized variable at %C");
6719 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6720 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6723 gfc_free_expr (init
);
6728 /* Match a parameter statement, with the weird syntax that these have. */
6731 gfc_match_parameter (void)
6735 if (gfc_match_char ('(') == MATCH_NO
)
6744 if (gfc_match (" )%t") == MATCH_YES
)
6747 if (gfc_match_char (',') != MATCH_YES
)
6749 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6759 /* Save statements have a special syntax. */
6762 gfc_match_save (void)
6764 char n
[GFC_MAX_SYMBOL_LEN
+1];
6769 if (gfc_match_eos () == MATCH_YES
)
6771 if (gfc_current_ns
->seen_save
)
6773 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6774 "follows previous SAVE statement")
6779 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6783 if (gfc_current_ns
->save_all
)
6785 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6786 "blanket SAVE statement")
6795 m
= gfc_match_symbol (&sym
, 0);
6799 if (gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
6800 &gfc_current_locus
) == FAILURE
)
6811 m
= gfc_match (" / %n /", &n
);
6812 if (m
== MATCH_ERROR
)
6817 c
= gfc_get_common (n
, 0);
6820 gfc_current_ns
->seen_save
= 1;
6823 if (gfc_match_eos () == MATCH_YES
)
6825 if (gfc_match_char (',') != MATCH_YES
)
6832 gfc_error ("Syntax error in SAVE statement at %C");
6838 gfc_match_value (void)
6843 /* This is not allowed within a BLOCK construct! */
6844 if (gfc_current_state () == COMP_BLOCK
)
6846 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6850 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
6854 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6859 if (gfc_match_eos () == MATCH_YES
)
6864 m
= gfc_match_symbol (&sym
, 0);
6868 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6881 if (gfc_match_eos () == MATCH_YES
)
6883 if (gfc_match_char (',') != MATCH_YES
)
6890 gfc_error ("Syntax error in VALUE statement at %C");
6896 gfc_match_volatile (void)
6901 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
6905 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6910 if (gfc_match_eos () == MATCH_YES
)
6915 /* VOLATILE is special because it can be added to host-associated
6916 symbols locally. Except for coarrays. */
6917 m
= gfc_match_symbol (&sym
, 1);
6921 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6922 for variable in a BLOCK which is defined outside of the BLOCK. */
6923 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
6925 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6926 "%C, which is use-/host-associated", sym
->name
);
6929 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6942 if (gfc_match_eos () == MATCH_YES
)
6944 if (gfc_match_char (',') != MATCH_YES
)
6951 gfc_error ("Syntax error in VOLATILE statement at %C");
6957 gfc_match_asynchronous (void)
6962 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASYNCHRONOUS statement at %C")
6966 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6971 if (gfc_match_eos () == MATCH_YES
)
6976 /* ASYNCHRONOUS is special because it can be added to host-associated
6978 m
= gfc_match_symbol (&sym
, 1);
6982 if (gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6995 if (gfc_match_eos () == MATCH_YES
)
6997 if (gfc_match_char (',') != MATCH_YES
)
7004 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7009 /* Match a module procedure statement. Note that we have to modify
7010 symbols in the parent's namespace because the current one was there
7011 to receive symbols that are in an interface's formal argument list. */
7014 gfc_match_modproc (void)
7016 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7020 gfc_namespace
*module_ns
;
7021 gfc_interface
*old_interface_head
, *interface
;
7023 if (gfc_state_stack
->state
!= COMP_INTERFACE
7024 || gfc_state_stack
->previous
== NULL
7025 || current_interface
.type
== INTERFACE_NAMELESS
7026 || current_interface
.type
== INTERFACE_ABSTRACT
)
7028 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7033 module_ns
= gfc_current_ns
->parent
;
7034 for (; module_ns
; module_ns
= module_ns
->parent
)
7035 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7036 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7037 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7038 && !module_ns
->proc_name
->attr
.contained
))
7041 if (module_ns
== NULL
)
7044 /* Store the current state of the interface. We will need it if we
7045 end up with a syntax error and need to recover. */
7046 old_interface_head
= gfc_current_interface_head ();
7048 /* Check if the F2008 optional double colon appears. */
7049 gfc_gobble_whitespace ();
7050 old_locus
= gfc_current_locus
;
7051 if (gfc_match ("::") == MATCH_YES
)
7053 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: double colon in "
7054 "MODULE PROCEDURE statement at %L", &old_locus
)
7059 gfc_current_locus
= old_locus
;
7064 old_locus
= gfc_current_locus
;
7066 m
= gfc_match_name (name
);
7072 /* Check for syntax error before starting to add symbols to the
7073 current namespace. */
7074 if (gfc_match_eos () == MATCH_YES
)
7077 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7080 /* Now we're sure the syntax is valid, we process this item
7082 if (gfc_get_symbol (name
, module_ns
, &sym
))
7085 if (sym
->attr
.intrinsic
)
7087 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7088 "PROCEDURE", &old_locus
);
7092 if (sym
->attr
.proc
!= PROC_MODULE
7093 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7094 sym
->name
, NULL
) == FAILURE
)
7097 if (gfc_add_interface (sym
) == FAILURE
)
7100 sym
->attr
.mod_proc
= 1;
7101 sym
->declared_at
= old_locus
;
7110 /* Restore the previous state of the interface. */
7111 interface
= gfc_current_interface_head ();
7112 gfc_set_current_interface_head (old_interface_head
);
7114 /* Free the new interfaces. */
7115 while (interface
!= old_interface_head
)
7117 gfc_interface
*i
= interface
->next
;
7122 /* And issue a syntax error. */
7123 gfc_syntax_error (ST_MODULE_PROC
);
7128 /* Check a derived type that is being extended. */
7130 check_extended_derived_type (char *name
)
7132 gfc_symbol
*extended
;
7134 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7136 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7142 gfc_error ("No such symbol in TYPE definition at %C");
7146 if (extended
->attr
.flavor
!= FL_DERIVED
)
7148 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7149 "derived type", name
);
7153 if (extended
->attr
.is_bind_c
)
7155 gfc_error ("'%s' cannot be extended at %C because it "
7156 "is BIND(C)", extended
->name
);
7160 if (extended
->attr
.sequence
)
7162 gfc_error ("'%s' cannot be extended at %C because it "
7163 "is a SEQUENCE type", extended
->name
);
7171 /* Match the optional attribute specifiers for a type declaration.
7172 Return MATCH_ERROR if an error is encountered in one of the handled
7173 attributes (public, private, bind(c)), MATCH_NO if what's found is
7174 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7175 checking on attribute conflicts needs to be done. */
7178 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7180 /* See if the derived type is marked as private. */
7181 if (gfc_match (" , private") == MATCH_YES
)
7183 if (gfc_current_state () != COMP_MODULE
)
7185 gfc_error ("Derived type at %C can only be PRIVATE in the "
7186 "specification part of a module");
7190 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
7193 else if (gfc_match (" , public") == MATCH_YES
)
7195 if (gfc_current_state () != COMP_MODULE
)
7197 gfc_error ("Derived type at %C can only be PUBLIC in the "
7198 "specification part of a module");
7202 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
7205 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7207 /* If the type is defined to be bind(c) it then needs to make
7208 sure that all fields are interoperable. This will
7209 need to be a semantic check on the finished derived type.
7210 See 15.2.3 (lines 9-12) of F2003 draft. */
7211 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
7214 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7216 else if (gfc_match (" , abstract") == MATCH_YES
)
7218 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
7222 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
7225 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
7227 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
7233 /* If we get here, something matched. */
7238 /* Match the beginning of a derived type declaration. If a type name
7239 was the result of a function, then it is possible to have a symbol
7240 already to be known as a derived type yet have no components. */
7243 gfc_match_derived_decl (void)
7245 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7246 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7247 symbol_attribute attr
;
7249 gfc_symbol
*extended
;
7251 match is_type_attr_spec
= MATCH_NO
;
7252 bool seen_attr
= false;
7254 if (gfc_current_state () == COMP_DERIVED
)
7259 gfc_clear_attr (&attr
);
7264 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7265 if (is_type_attr_spec
== MATCH_ERROR
)
7267 if (is_type_attr_spec
== MATCH_YES
)
7269 } while (is_type_attr_spec
== MATCH_YES
);
7271 /* Deal with derived type extensions. The extension attribute has
7272 been added to 'attr' but now the parent type must be found and
7275 extended
= check_extended_derived_type (parent
);
7277 if (parent
[0] && !extended
)
7280 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7282 gfc_error ("Expected :: in TYPE definition at %C");
7286 m
= gfc_match (" %n%t", name
);
7290 /* Make sure the name is not the name of an intrinsic type. */
7291 if (gfc_is_intrinsic_typename (name
))
7293 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7298 if (gfc_get_symbol (name
, NULL
, &sym
))
7301 if (sym
->ts
.type
!= BT_UNKNOWN
)
7303 gfc_error ("Derived type name '%s' at %C already has a basic type "
7304 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
7308 /* The symbol may already have the derived attribute without the
7309 components. The ways this can happen is via a function
7310 definition, an INTRINSIC statement or a subtype in another
7311 derived type that is a pointer. The first part of the AND clause
7312 is true if the symbol is not the return value of a function. */
7313 if (sym
->attr
.flavor
!= FL_DERIVED
7314 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
7317 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
7319 gfc_error ("Derived type definition of '%s' at %C has already been "
7320 "defined", sym
->name
);
7324 if (attr
.access
!= ACCESS_UNKNOWN
7325 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
7328 /* See if the derived type was labeled as bind(c). */
7329 if (attr
.is_bind_c
!= 0)
7330 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7332 /* Construct the f2k_derived namespace if it is not yet there. */
7333 if (!sym
->f2k_derived
)
7334 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7336 if (extended
&& !sym
->components
)
7341 /* Add the extended derived type as the first component. */
7342 gfc_add_component (sym
, parent
, &p
);
7344 gfc_set_sym_referenced (extended
);
7346 p
->ts
.type
= BT_DERIVED
;
7347 p
->ts
.u
.derived
= extended
;
7348 p
->initializer
= gfc_default_initializer (&p
->ts
);
7350 /* Set extension level. */
7351 if (extended
->attr
.extension
== 255)
7353 /* Since the extension field is 8 bit wide, we can only have
7354 up to 255 extension levels. */
7355 gfc_error ("Maximum extension level reached with type '%s' at %L",
7356 extended
->name
, &extended
->declared_at
);
7359 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7361 /* Provide the links between the extended type and its extension. */
7362 if (!extended
->f2k_derived
)
7363 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7364 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7368 if (!sym
->hash_value
)
7369 /* Set the hash for the compound name for this type. */
7370 sym
->hash_value
= gfc_hash_value (sym
);
7372 /* Take over the ABSTRACT attribute. */
7373 sym
->attr
.abstract
= attr
.abstract
;
7375 gfc_new_block
= sym
;
7381 /* Cray Pointees can be declared as:
7382 pointer (ipt, a (n,m,...,*)) */
7385 gfc_mod_pointee_as (gfc_array_spec
*as
)
7387 as
->cray_pointee
= true; /* This will be useful to know later. */
7388 if (as
->type
== AS_ASSUMED_SIZE
)
7389 as
->cp_was_assumed
= true;
7390 else if (as
->type
== AS_ASSUMED_SHAPE
)
7392 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7399 /* Match the enum definition statement, here we are trying to match
7400 the first line of enum definition statement.
7401 Returns MATCH_YES if match is found. */
7404 gfc_match_enum (void)
7408 m
= gfc_match_eos ();
7412 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
7420 /* Returns an initializer whose value is one higher than the value of the
7421 LAST_INITIALIZER argument. If the argument is NULL, the
7422 initializers value will be set to zero. The initializer's kind
7423 will be set to gfc_c_int_kind.
7425 If -fshort-enums is given, the appropriate kind will be selected
7426 later after all enumerators have been parsed. A warning is issued
7427 here if an initializer exceeds gfc_c_int_kind. */
7430 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7433 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7435 mpz_init (result
->value
.integer
);
7437 if (last_initializer
!= NULL
)
7439 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7440 result
->where
= last_initializer
->where
;
7442 if (gfc_check_integer_range (result
->value
.integer
,
7443 gfc_c_int_kind
) != ARITH_OK
)
7445 gfc_error ("Enumerator exceeds the C integer type at %C");
7451 /* Control comes here, if it's the very first enumerator and no
7452 initializer has been given. It will be initialized to zero. */
7453 mpz_set_si (result
->value
.integer
, 0);
7460 /* Match a variable name with an optional initializer. When this
7461 subroutine is called, a variable is expected to be parsed next.
7462 Depending on what is happening at the moment, updates either the
7463 symbol table or the current interface. */
7466 enumerator_decl (void)
7468 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7469 gfc_expr
*initializer
;
7470 gfc_array_spec
*as
= NULL
;
7478 old_locus
= gfc_current_locus
;
7480 /* When we get here, we've just matched a list of attributes and
7481 maybe a type and a double colon. The next thing we expect to see
7482 is the name of the symbol. */
7483 m
= gfc_match_name (name
);
7487 var_locus
= gfc_current_locus
;
7489 /* OK, we've successfully matched the declaration. Now put the
7490 symbol in the current namespace. If we fail to create the symbol,
7492 if (build_sym (name
, NULL
, false, &as
, &var_locus
) == FAILURE
)
7498 /* The double colon must be present in order to have initializers.
7499 Otherwise the statement is ambiguous with an assignment statement. */
7502 if (gfc_match_char ('=') == MATCH_YES
)
7504 m
= gfc_match_init_expr (&initializer
);
7507 gfc_error ("Expected an initialization expression at %C");
7516 /* If we do not have an initializer, the initialization value of the
7517 previous enumerator (stored in last_initializer) is incremented
7518 by 1 and is used to initialize the current enumerator. */
7519 if (initializer
== NULL
)
7520 initializer
= enum_initializer (last_initializer
, old_locus
);
7522 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7524 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7530 /* Store this current initializer, for the next enumerator variable
7531 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7532 use last_initializer below. */
7533 last_initializer
= initializer
;
7534 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7536 /* Maintain enumerator history. */
7537 gfc_find_symbol (name
, NULL
, 0, &sym
);
7538 create_enum_history (sym
, last_initializer
);
7540 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
7543 /* Free stuff up and return. */
7544 gfc_free_expr (initializer
);
7550 /* Match the enumerator definition statement. */
7553 gfc_match_enumerator_def (void)
7558 gfc_clear_ts (¤t_ts
);
7560 m
= gfc_match (" enumerator");
7564 m
= gfc_match (" :: ");
7565 if (m
== MATCH_ERROR
)
7568 colon_seen
= (m
== MATCH_YES
);
7570 if (gfc_current_state () != COMP_ENUM
)
7572 gfc_error ("ENUM definition statement expected before %C");
7573 gfc_free_enum_history ();
7577 (¤t_ts
)->type
= BT_INTEGER
;
7578 (¤t_ts
)->kind
= gfc_c_int_kind
;
7580 gfc_clear_attr (¤t_attr
);
7581 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7590 m
= enumerator_decl ();
7591 if (m
== MATCH_ERROR
)
7593 gfc_free_enum_history ();
7599 if (gfc_match_eos () == MATCH_YES
)
7601 if (gfc_match_char (',') != MATCH_YES
)
7605 if (gfc_current_state () == COMP_ENUM
)
7607 gfc_free_enum_history ();
7608 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7613 gfc_free_array_spec (current_as
);
7620 /* Match binding attributes. */
7623 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7625 bool found_passing
= false;
7626 bool seen_ptr
= false;
7627 match m
= MATCH_YES
;
7629 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7630 this case the defaults are in there. */
7631 ba
->access
= ACCESS_UNKNOWN
;
7632 ba
->pass_arg
= NULL
;
7633 ba
->pass_arg_num
= 0;
7635 ba
->non_overridable
= 0;
7639 /* If we find a comma, we believe there are binding attributes. */
7640 m
= gfc_match_char (',');
7646 /* Access specifier. */
7648 m
= gfc_match (" public");
7649 if (m
== MATCH_ERROR
)
7653 if (ba
->access
!= ACCESS_UNKNOWN
)
7655 gfc_error ("Duplicate access-specifier at %C");
7659 ba
->access
= ACCESS_PUBLIC
;
7663 m
= gfc_match (" private");
7664 if (m
== MATCH_ERROR
)
7668 if (ba
->access
!= ACCESS_UNKNOWN
)
7670 gfc_error ("Duplicate access-specifier at %C");
7674 ba
->access
= ACCESS_PRIVATE
;
7678 /* If inside GENERIC, the following is not allowed. */
7683 m
= gfc_match (" nopass");
7684 if (m
== MATCH_ERROR
)
7690 gfc_error ("Binding attributes already specify passing,"
7691 " illegal NOPASS at %C");
7695 found_passing
= true;
7700 /* PASS possibly including argument. */
7701 m
= gfc_match (" pass");
7702 if (m
== MATCH_ERROR
)
7706 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
7710 gfc_error ("Binding attributes already specify passing,"
7711 " illegal PASS at %C");
7715 m
= gfc_match (" ( %n )", arg
);
7716 if (m
== MATCH_ERROR
)
7719 ba
->pass_arg
= gfc_get_string (arg
);
7720 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
7722 found_passing
= true;
7730 m
= gfc_match (" pointer");
7731 if (m
== MATCH_ERROR
)
7737 gfc_error ("Duplicate POINTER attribute at %C");
7747 /* NON_OVERRIDABLE flag. */
7748 m
= gfc_match (" non_overridable");
7749 if (m
== MATCH_ERROR
)
7753 if (ba
->non_overridable
)
7755 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7759 ba
->non_overridable
= 1;
7763 /* DEFERRED flag. */
7764 m
= gfc_match (" deferred");
7765 if (m
== MATCH_ERROR
)
7771 gfc_error ("Duplicate DEFERRED at %C");
7782 /* Nothing matching found. */
7784 gfc_error ("Expected access-specifier at %C");
7786 gfc_error ("Expected binding attribute at %C");
7789 while (gfc_match_char (',') == MATCH_YES
);
7791 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7792 if (ba
->non_overridable
&& ba
->deferred
)
7794 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7801 if (ba
->access
== ACCESS_UNKNOWN
)
7802 ba
->access
= gfc_typebound_default_access
;
7804 if (ppc
&& !seen_ptr
)
7806 gfc_error ("POINTER attribute is required for procedure pointer component"
7818 /* Match a PROCEDURE specific binding inside a derived type. */
7821 match_procedure_in_type (void)
7823 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7824 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
7825 char* target
= NULL
, *ifc
= NULL
;
7826 gfc_typebound_proc tb
;
7835 /* Check current state. */
7836 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
7837 block
= gfc_state_stack
->previous
->sym
;
7840 /* Try to match PROCEDURE(interface). */
7841 if (gfc_match (" (") == MATCH_YES
)
7843 m
= gfc_match_name (target_buf
);
7844 if (m
== MATCH_ERROR
)
7848 gfc_error ("Interface-name expected after '(' at %C");
7852 if (gfc_match (" )") != MATCH_YES
)
7854 gfc_error ("')' expected at %C");
7861 /* Construct the data structure. */
7862 memset (&tb
, 0, sizeof (tb
));
7863 tb
.where
= gfc_current_locus
;
7865 /* Match binding attributes. */
7866 m
= match_binding_attributes (&tb
, false, false);
7867 if (m
== MATCH_ERROR
)
7869 seen_attrs
= (m
== MATCH_YES
);
7871 /* Check that attribute DEFERRED is given if an interface is specified. */
7872 if (tb
.deferred
&& !ifc
)
7874 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7877 if (ifc
&& !tb
.deferred
)
7879 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7883 /* Match the colons. */
7884 m
= gfc_match (" ::");
7885 if (m
== MATCH_ERROR
)
7887 seen_colons
= (m
== MATCH_YES
);
7888 if (seen_attrs
&& !seen_colons
)
7890 gfc_error ("Expected '::' after binding-attributes at %C");
7894 /* Match the binding names. */
7897 m
= gfc_match_name (name
);
7898 if (m
== MATCH_ERROR
)
7902 gfc_error ("Expected binding name at %C");
7906 if (num
>1 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: PROCEDURE list"
7907 " at %C") == FAILURE
)
7910 /* Try to match the '=> target', if it's there. */
7912 m
= gfc_match (" =>");
7913 if (m
== MATCH_ERROR
)
7919 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7925 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7930 m
= gfc_match_name (target_buf
);
7931 if (m
== MATCH_ERROR
)
7935 gfc_error ("Expected binding target after '=>' at %C");
7938 target
= target_buf
;
7941 /* If no target was found, it has the same name as the binding. */
7945 /* Get the namespace to insert the symbols into. */
7946 ns
= block
->f2k_derived
;
7949 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7950 if (tb
.deferred
&& !block
->attr
.abstract
)
7952 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7953 "is not ABSTRACT", block
->name
);
7957 /* See if we already have a binding with this name in the symtree which
7958 would be an error. If a GENERIC already targetted this binding, it may
7959 be already there but then typebound is still NULL. */
7960 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7961 if (stree
&& stree
->n
.tb
)
7963 gfc_error ("There is already a procedure with binding name '%s' for "
7964 "the derived type '%s' at %C", name
, block
->name
);
7968 /* Insert it and set attributes. */
7972 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7975 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
7977 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
7980 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
7982 if (gfc_match_eos () == MATCH_YES
)
7984 if (gfc_match_char (',') != MATCH_YES
)
7989 gfc_error ("Syntax error in PROCEDURE statement at %C");
7994 /* Match a GENERIC procedure binding inside a derived type. */
7997 gfc_match_generic (void)
7999 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8000 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8002 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8003 gfc_typebound_proc
* tb
;
8005 interface_type op_type
;
8006 gfc_intrinsic_op op
;
8009 /* Check current state. */
8010 if (gfc_current_state () == COMP_DERIVED
)
8012 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8015 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8017 block
= gfc_state_stack
->previous
->sym
;
8018 ns
= block
->f2k_derived
;
8019 gcc_assert (block
&& ns
);
8021 memset (&tbattr
, 0, sizeof (tbattr
));
8022 tbattr
.where
= gfc_current_locus
;
8024 /* See if we get an access-specifier. */
8025 m
= match_binding_attributes (&tbattr
, true, false);
8026 if (m
== MATCH_ERROR
)
8029 /* Now the colons, those are required. */
8030 if (gfc_match (" ::") != MATCH_YES
)
8032 gfc_error ("Expected '::' at %C");
8036 /* Match the binding name; depending on type (operator / generic) format
8037 it for future error messages into bind_name. */
8039 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8040 if (m
== MATCH_ERROR
)
8044 gfc_error ("Expected generic name or operator descriptor at %C");
8050 case INTERFACE_GENERIC
:
8051 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8054 case INTERFACE_USER_OP
:
8055 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8058 case INTERFACE_INTRINSIC_OP
:
8059 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8060 gfc_op2string (op
));
8067 /* Match the required =>. */
8068 if (gfc_match (" =>") != MATCH_YES
)
8070 gfc_error ("Expected '=>' at %C");
8074 /* Try to find existing GENERIC binding with this name / for this operator;
8075 if there is something, check that it is another GENERIC and then extend
8076 it rather than building a new node. Otherwise, create it and put it
8077 at the right position. */
8081 case INTERFACE_USER_OP
:
8082 case INTERFACE_GENERIC
:
8084 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8087 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8099 case INTERFACE_INTRINSIC_OP
:
8109 if (!tb
->is_generic
)
8111 gcc_assert (op_type
== INTERFACE_GENERIC
);
8112 gfc_error ("There's already a non-generic procedure with binding name"
8113 " '%s' for the derived type '%s' at %C",
8114 bind_name
, block
->name
);
8118 if (tb
->access
!= tbattr
.access
)
8120 gfc_error ("Binding at %C must have the same access as already"
8121 " defined binding '%s'", bind_name
);
8127 tb
= gfc_get_typebound_proc (NULL
);
8128 tb
->where
= gfc_current_locus
;
8129 tb
->access
= tbattr
.access
;
8131 tb
->u
.generic
= NULL
;
8135 case INTERFACE_GENERIC
:
8136 case INTERFACE_USER_OP
:
8138 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8141 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8149 case INTERFACE_INTRINSIC_OP
:
8158 /* Now, match all following names as specific targets. */
8161 gfc_symtree
* target_st
;
8162 gfc_tbp_generic
* target
;
8164 m
= gfc_match_name (name
);
8165 if (m
== MATCH_ERROR
)
8169 gfc_error ("Expected specific binding name at %C");
8173 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8175 /* See if this is a duplicate specification. */
8176 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8177 if (target_st
== target
->specific_st
)
8179 gfc_error ("'%s' already defined as specific binding for the"
8180 " generic '%s' at %C", name
, bind_name
);
8184 target
= gfc_get_tbp_generic ();
8185 target
->specific_st
= target_st
;
8186 target
->specific
= NULL
;
8187 target
->next
= tb
->u
.generic
;
8188 tb
->u
.generic
= target
;
8190 while (gfc_match (" ,") == MATCH_YES
);
8192 /* Here should be the end. */
8193 if (gfc_match_eos () != MATCH_YES
)
8195 gfc_error ("Junk after GENERIC binding at %C");
8206 /* Match a FINAL declaration inside a derived type. */
8209 gfc_match_final_decl (void)
8211 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8214 gfc_namespace
* module_ns
;
8218 if (gfc_current_form
== FORM_FREE
)
8220 char c
= gfc_peek_ascii_char ();
8221 if (!gfc_is_whitespace (c
) && c
!= ':')
8225 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8227 if (gfc_current_form
== FORM_FIXED
)
8230 gfc_error ("FINAL declaration at %C must be inside a derived type "
8231 "CONTAINS section");
8235 block
= gfc_state_stack
->previous
->sym
;
8238 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8239 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8241 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8242 " specification part of a MODULE");
8246 module_ns
= gfc_current_ns
;
8247 gcc_assert (module_ns
);
8248 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8250 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8251 if (gfc_match (" ::") == MATCH_ERROR
)
8254 /* Match the sequence of procedure names. */
8261 if (first
&& gfc_match_eos () == MATCH_YES
)
8263 gfc_error ("Empty FINAL at %C");
8267 m
= gfc_match_name (name
);
8270 gfc_error ("Expected module procedure name at %C");
8273 else if (m
!= MATCH_YES
)
8276 if (gfc_match_eos () == MATCH_YES
)
8278 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8280 gfc_error ("Expected ',' at %C");
8284 if (gfc_get_symbol (name
, module_ns
, &sym
))
8286 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8290 /* Mark the symbol as module procedure. */
8291 if (sym
->attr
.proc
!= PROC_MODULE
8292 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
8293 sym
->name
, NULL
) == FAILURE
)
8296 /* Check if we already have this symbol in the list, this is an error. */
8297 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8298 if (f
->proc_sym
== sym
)
8300 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8305 /* Add this symbol to the list of finalizers. */
8306 gcc_assert (block
->f2k_derived
);
8308 f
= XCNEW (gfc_finalizer
);
8310 f
->proc_tree
= NULL
;
8311 f
->where
= gfc_current_locus
;
8312 f
->next
= block
->f2k_derived
->finalizers
;
8313 block
->f2k_derived
->finalizers
= f
;
8323 const ext_attr_t ext_attr_list
[] = {
8324 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8325 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8326 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8327 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8328 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8329 { NULL
, EXT_ATTR_LAST
, NULL
}
8332 /* Match a !GCC$ ATTRIBUTES statement of the form:
8333 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8334 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8336 TODO: We should support all GCC attributes using the same syntax for
8337 the attribute list, i.e. the list in C
8338 __attributes(( attribute-list ))
8340 !GCC$ ATTRIBUTES attribute-list ::
8341 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8344 As there is absolutely no risk of confusion, we should never return
8347 gfc_match_gcc_attributes (void)
8349 symbol_attribute attr
;
8350 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8355 gfc_clear_attr (&attr
);
8360 if (gfc_match_name (name
) != MATCH_YES
)
8363 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8364 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8367 if (id
== EXT_ATTR_LAST
)
8369 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8373 if (gfc_add_ext_attribute (&attr
, (ext_attr_id_t
) id
, &gfc_current_locus
)
8377 gfc_gobble_whitespace ();
8378 ch
= gfc_next_ascii_char ();
8381 /* This is the successful exit condition for the loop. */
8382 if (gfc_next_ascii_char () == ':')
8392 if (gfc_match_eos () == MATCH_YES
)
8397 m
= gfc_match_name (name
);
8401 if (find_special (name
, &sym
, true))
8404 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8406 if (gfc_match_eos () == MATCH_YES
)
8409 if (gfc_match_char (',') != MATCH_YES
)
8416 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");