1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 /* Mark the variable as having appeared in a data statement. */
506 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
512 /* Chain in namespace list of DATA initializers. */
513 newdata
->next
= gfc_current_ns
->data
;
514 gfc_current_ns
->data
= newdata
;
520 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
521 we are matching a DATA statement and are therefore issuing an error
522 if we encounter something unexpected, if not, we're trying to match
523 an old-style initialization expression of the form INTEGER I /2/. */
526 gfc_match_data (void)
531 set_in_match_data (true);
535 new_data
= gfc_get_data ();
536 new_data
->where
= gfc_current_locus
;
538 m
= top_var_list (new_data
);
542 m
= top_val_list (new_data
);
546 new_data
->next
= gfc_current_ns
->data
;
547 gfc_current_ns
->data
= new_data
;
549 if (gfc_match_eos () == MATCH_YES
)
552 gfc_match_char (','); /* Optional comma */
555 set_in_match_data (false);
559 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
566 set_in_match_data (false);
567 gfc_free_data (new_data
);
572 /************************ Declaration statements *********************/
575 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
578 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
582 if (to
->rank
== 0 && from
->rank
> 0)
584 to
->rank
= from
->rank
;
585 to
->type
= from
->type
;
586 to
->cray_pointee
= from
->cray_pointee
;
587 to
->cp_was_assumed
= from
->cp_was_assumed
;
589 for (i
= 0; i
< to
->corank
; i
++)
591 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
592 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
594 for (i
= 0; i
< from
->rank
; i
++)
598 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
599 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
603 to
->lower
[i
] = from
->lower
[i
];
604 to
->upper
[i
] = from
->upper
[i
];
608 else if (to
->corank
== 0 && from
->corank
> 0)
610 to
->corank
= from
->corank
;
611 to
->cotype
= from
->cotype
;
613 for (i
= 0; i
< from
->corank
; i
++)
617 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
618 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
622 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
623 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
630 /* Match an intent specification. Since this can only happen after an
631 INTENT word, a legal intent-spec must follow. */
634 match_intent_spec (void)
637 if (gfc_match (" ( in out )") == MATCH_YES
)
639 if (gfc_match (" ( in )") == MATCH_YES
)
641 if (gfc_match (" ( out )") == MATCH_YES
)
644 gfc_error ("Bad INTENT specification at %C");
645 return INTENT_UNKNOWN
;
649 /* Matches a character length specification, which is either a
650 specification expression, '*', or ':'. */
653 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
660 if (gfc_match_char ('*') == MATCH_YES
)
663 if (gfc_match_char (':') == MATCH_YES
)
665 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: deferred type "
666 "parameter at %C") == FAILURE
)
674 m
= gfc_match_expr (expr
);
677 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
680 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
682 if ((*expr
)->value
.function
.actual
683 && (*expr
)->value
.function
.actual
->expr
->symtree
)
686 e
= (*expr
)->value
.function
.actual
->expr
;
687 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
688 && e
->expr_type
== EXPR_VARIABLE
)
690 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
692 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
693 && e
->symtree
->n
.sym
->ts
.u
.cl
694 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
702 gfc_error ("Conflict in attributes of function argument at %C");
707 /* A character length is a '*' followed by a literal integer or a
708 char_len_param_value in parenthesis. */
711 match_char_length (gfc_expr
**expr
, bool *deferred
)
717 m
= gfc_match_char ('*');
721 m
= gfc_match_small_literal_int (&length
, NULL
);
722 if (m
== MATCH_ERROR
)
727 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
728 "Old-style character length at %C") == FAILURE
)
730 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
734 if (gfc_match_char ('(') == MATCH_NO
)
737 m
= char_len_param_value (expr
, deferred
);
738 if (m
!= MATCH_YES
&& gfc_matching_function
)
744 if (m
== MATCH_ERROR
)
749 if (gfc_match_char (')') == MATCH_NO
)
751 gfc_free_expr (*expr
);
759 gfc_error ("Syntax error in character length specification at %C");
764 /* Special subroutine for finding a symbol. Check if the name is found
765 in the current name space. If not, and we're compiling a function or
766 subroutine and the parent compilation unit is an interface, then check
767 to see if the name we've been given is the name of the interface
768 (located in another namespace). */
771 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
777 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
780 *result
= st
? st
->n
.sym
: NULL
;
784 if (gfc_current_state () != COMP_SUBROUTINE
785 && gfc_current_state () != COMP_FUNCTION
)
788 s
= gfc_state_stack
->previous
;
792 if (s
->state
!= COMP_INTERFACE
)
795 goto end
; /* Nameless interface. */
797 if (strcmp (name
, s
->sym
->name
) == 0)
808 /* Special subroutine for getting a symbol node associated with a
809 procedure name, used in SUBROUTINE and FUNCTION statements. The
810 symbol is created in the parent using with symtree node in the
811 child unit pointing to the symbol. If the current namespace has no
812 parent, then the symbol is just created in the current unit. */
815 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
821 /* Module functions have to be left in their own namespace because
822 they have potentially (almost certainly!) already been referenced.
823 In this sense, they are rather like external functions. This is
824 fixed up in resolve.c(resolve_entries), where the symbol name-
825 space is set to point to the master function, so that the fake
826 result mechanism can work. */
827 if (module_fcn_entry
)
829 /* Present if entry is declared to be a module procedure. */
830 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
833 rc
= gfc_get_symbol (name
, NULL
, result
);
834 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
835 && (*result
)->ts
.type
== BT_UNKNOWN
836 && sym
->attr
.flavor
== FL_UNKNOWN
)
837 /* Pick up the typespec for the entry, if declared in the function
838 body. Note that this symbol is FL_UNKNOWN because it will
839 only have appeared in a type declaration. The local symtree
840 is set to point to the module symbol and a unique symtree
841 to the local version. This latter ensures a correct clearing
844 /* If the ENTRY proceeds its specification, we need to ensure
845 that this does not raise a "has no IMPLICIT type" error. */
846 if (sym
->ts
.type
== BT_UNKNOWN
)
847 sym
->attr
.untyped
= 1;
849 (*result
)->ts
= sym
->ts
;
851 /* Put the symbol in the procedure namespace so that, should
852 the ENTRY precede its specification, the specification
854 (*result
)->ns
= gfc_current_ns
;
856 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
858 st
= gfc_get_unique_symtree (gfc_current_ns
);
863 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
869 gfc_current_ns
->refs
++;
871 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
873 /* Trap another encompassed procedure with the same name. All
874 these conditions are necessary to avoid picking up an entry
875 whose name clashes with that of the encompassing procedure;
876 this is handled using gsymbols to register unique,globally
878 if (sym
->attr
.flavor
!= 0
879 && sym
->attr
.proc
!= 0
880 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
881 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
882 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
883 name
, &sym
->declared_at
);
885 /* Trap a procedure with a name the same as interface in the
886 encompassing scope. */
887 if (sym
->attr
.generic
!= 0
888 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
889 && !sym
->attr
.mod_proc
)
890 gfc_error_now ("Name '%s' at %C is already defined"
891 " as a generic interface at %L",
892 name
, &sym
->declared_at
);
894 /* Trap declarations of attributes in encompassing scope. The
895 signature for this is that ts.kind is set. Legitimate
896 references only set ts.type. */
897 if (sym
->ts
.kind
!= 0
898 && !sym
->attr
.implicit_type
899 && sym
->attr
.proc
== 0
900 && gfc_current_ns
->parent
!= NULL
901 && sym
->attr
.access
== 0
902 && !module_fcn_entry
)
903 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
904 "and must not have attributes declared at %L",
905 name
, &sym
->declared_at
);
908 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
911 /* Module function entries will already have a symtree in
912 the current namespace but will need one at module level. */
913 if (module_fcn_entry
)
915 /* Present if entry is declared to be a module procedure. */
916 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
918 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
921 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
926 /* See if the procedure should be a module procedure. */
928 if (((sym
->ns
->proc_name
!= NULL
929 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
930 && sym
->attr
.proc
!= PROC_MODULE
)
931 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
932 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
933 sym
->name
, NULL
) == FAILURE
)
940 /* Verify that the given symbol representing a parameter is C
941 interoperable, by checking to see if it was marked as such after
942 its declaration. If the given symbol is not interoperable, a
943 warning is reported, thus removing the need to return the status to
944 the calling function. The standard does not require the user use
945 one of the iso_c_binding named constants to declare an
946 interoperable parameter, but we can't be sure if the param is C
947 interop or not if the user doesn't. For example, integer(4) may be
948 legal Fortran, but doesn't have meaning in C. It may interop with
949 a number of the C types, which causes a problem because the
950 compiler can't know which one. This code is almost certainly not
951 portable, and the user will get what they deserve if the C type
952 across platforms isn't always interoperable with integer(4). If
953 the user had used something like integer(c_int) or integer(c_long),
954 the compiler could have automatically handled the varying sizes
958 verify_c_interop_param (gfc_symbol
*sym
)
960 int is_c_interop
= 0;
961 gfc_try retval
= SUCCESS
;
963 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
964 Don't repeat the checks here. */
965 if (sym
->attr
.implicit_type
)
968 /* For subroutines or functions that are passed to a BIND(C) procedure,
969 they're interoperable if they're BIND(C) and their params are all
971 if (sym
->attr
.flavor
== FL_PROCEDURE
)
973 if (sym
->attr
.is_bind_c
== 0)
975 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
976 "attribute to be C interoperable", sym
->name
,
977 &(sym
->declared_at
));
983 if (sym
->attr
.is_c_interop
== 1)
984 /* We've already checked this procedure; don't check it again. */
987 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
992 /* See if we've stored a reference to a procedure that owns sym. */
993 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
995 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
998 (verify_c_interop (&(sym
->ts
))
1001 if (is_c_interop
!= 1)
1003 /* Make personalized messages to give better feedback. */
1004 if (sym
->ts
.type
== BT_DERIVED
)
1005 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1006 "procedure '%s' but is not C interoperable "
1007 "because derived type '%s' is not C interoperable",
1008 sym
->name
, &(sym
->declared_at
),
1009 sym
->ns
->proc_name
->name
,
1010 sym
->ts
.u
.derived
->name
);
1012 gfc_warning ("Variable '%s' at %L is a parameter to the "
1013 "BIND(C) procedure '%s' but may not be C "
1015 sym
->name
, &(sym
->declared_at
),
1016 sym
->ns
->proc_name
->name
);
1019 /* Character strings are only C interoperable if they have a
1021 if (sym
->ts
.type
== BT_CHARACTER
)
1023 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1024 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1025 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1027 gfc_error ("Character argument '%s' at %L "
1028 "must be length 1 because "
1029 "procedure '%s' is BIND(C)",
1030 sym
->name
, &sym
->declared_at
,
1031 sym
->ns
->proc_name
->name
);
1036 /* We have to make sure that any param to a bind(c) routine does
1037 not have the allocatable, pointer, or optional attributes,
1038 according to J3/04-007, section 5.1. */
1039 if (sym
->attr
.allocatable
== 1)
1041 gfc_error ("Variable '%s' at %L cannot have the "
1042 "ALLOCATABLE attribute because procedure '%s'"
1043 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1044 sym
->ns
->proc_name
->name
);
1048 if (sym
->attr
.pointer
== 1)
1050 gfc_error ("Variable '%s' at %L cannot have the "
1051 "POINTER attribute because procedure '%s'"
1052 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1053 sym
->ns
->proc_name
->name
);
1057 if (sym
->attr
.optional
== 1)
1059 gfc_error ("Variable '%s' at %L cannot have the "
1060 "OPTIONAL attribute because procedure '%s'"
1061 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1062 sym
->ns
->proc_name
->name
);
1066 /* Make sure that if it has the dimension attribute, that it is
1067 either assumed size or explicit shape. */
1068 if (sym
->as
!= NULL
)
1070 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
1072 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1073 "argument to the procedure '%s' at %L because "
1074 "the procedure is BIND(C)", sym
->name
,
1075 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1076 &(sym
->ns
->proc_name
->declared_at
));
1080 if (sym
->as
->type
== AS_DEFERRED
)
1082 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1083 "argument to the procedure '%s' at %L because "
1084 "the procedure is BIND(C)", sym
->name
,
1085 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1086 &(sym
->ns
->proc_name
->declared_at
));
1098 /* Function called by variable_decl() that adds a name to the symbol table. */
1101 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1102 gfc_array_spec
**as
, locus
*var_locus
)
1104 symbol_attribute attr
;
1107 if (gfc_get_symbol (name
, NULL
, &sym
))
1110 /* Start updating the symbol table. Add basic type attribute if present. */
1111 if (current_ts
.type
!= BT_UNKNOWN
1112 && (sym
->attr
.implicit_type
== 0
1113 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1114 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1117 if (sym
->ts
.type
== BT_CHARACTER
)
1120 sym
->ts
.deferred
= cl_deferred
;
1123 /* Add dimension attribute if present. */
1124 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1128 /* Add attribute to symbol. The copy is so that we can reset the
1129 dimension attribute. */
1130 attr
= current_attr
;
1132 attr
.codimension
= 0;
1134 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1137 /* Finish any work that may need to be done for the binding label,
1138 if it's a bind(c). The bind(c) attr is found before the symbol
1139 is made, and before the symbol name (for data decls), so the
1140 current_ts is holding the binding label, or nothing if the
1141 name= attr wasn't given. Therefore, test here if we're dealing
1142 with a bind(c) and make sure the binding label is set correctly. */
1143 if (sym
->attr
.is_bind_c
== 1)
1145 if (sym
->binding_label
[0] == '\0')
1147 /* Set the binding label and verify that if a NAME= was specified
1148 then only one identifier was in the entity-decl-list. */
1149 if (set_binding_label (sym
->binding_label
, sym
->name
,
1150 num_idents_on_line
) == FAILURE
)
1155 /* See if we know we're in a common block, and if it's a bind(c)
1156 common then we need to make sure we're an interoperable type. */
1157 if (sym
->attr
.in_common
== 1)
1159 /* Test the common block object. */
1160 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1161 && sym
->ts
.is_c_interop
!= 1)
1163 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1164 "must be declared with a C interoperable "
1165 "kind since common block '%s' is BIND(C)",
1166 sym
->name
, sym
->common_block
->name
,
1167 sym
->common_block
->name
);
1172 sym
->attr
.implied_index
= 0;
1174 if (sym
->ts
.type
== BT_CLASS
1175 && (sym
->attr
.class_ok
= sym
->attr
.dummy
|| sym
->attr
.pointer
1176 || sym
->attr
.allocatable
))
1177 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1183 /* Set character constant to the given length. The constant will be padded or
1184 truncated. If we're inside an array constructor without a typespec, we
1185 additionally check that all elements have the same length; check_len -1
1186 means no checking. */
1189 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1194 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1195 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1197 slen
= expr
->value
.character
.length
;
1200 s
= gfc_get_wide_string (len
+ 1);
1201 memcpy (s
, expr
->value
.character
.string
,
1202 MIN (len
, slen
) * sizeof (gfc_char_t
));
1204 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1206 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1207 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1208 "(%d/%d)", &expr
->where
, slen
, len
);
1210 /* Apply the standard by 'hand' otherwise it gets cleared for
1212 if (check_len
!= -1 && slen
!= check_len
1213 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1214 gfc_error_now ("The CHARACTER elements of the array constructor "
1215 "at %L must have the same length (%d/%d)",
1216 &expr
->where
, slen
, check_len
);
1219 gfc_free (expr
->value
.character
.string
);
1220 expr
->value
.character
.string
= s
;
1221 expr
->value
.character
.length
= len
;
1226 /* Function to create and update the enumerator history
1227 using the information passed as arguments.
1228 Pointer "max_enum" is also updated, to point to
1229 enum history node containing largest initializer.
1231 SYM points to the symbol node of enumerator.
1232 INIT points to its enumerator value. */
1235 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1237 enumerator_history
*new_enum_history
;
1238 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1240 new_enum_history
= XCNEW (enumerator_history
);
1242 new_enum_history
->sym
= sym
;
1243 new_enum_history
->initializer
= init
;
1244 new_enum_history
->next
= NULL
;
1246 if (enum_history
== NULL
)
1248 enum_history
= new_enum_history
;
1249 max_enum
= enum_history
;
1253 new_enum_history
->next
= enum_history
;
1254 enum_history
= new_enum_history
;
1256 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1257 new_enum_history
->initializer
->value
.integer
) < 0)
1258 max_enum
= new_enum_history
;
1263 /* Function to free enum kind history. */
1266 gfc_free_enum_history (void)
1268 enumerator_history
*current
= enum_history
;
1269 enumerator_history
*next
;
1271 while (current
!= NULL
)
1273 next
= current
->next
;
1278 enum_history
= NULL
;
1282 /* Function called by variable_decl() that adds an initialization
1283 expression to a symbol. */
1286 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1288 symbol_attribute attr
;
1293 if (find_special (name
, &sym
, false))
1298 /* If this symbol is confirming an implicit parameter type,
1299 then an initialization expression is not allowed. */
1300 if (attr
.flavor
== FL_PARAMETER
1301 && sym
->value
!= NULL
1304 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1311 /* An initializer is required for PARAMETER declarations. */
1312 if (attr
.flavor
== FL_PARAMETER
)
1314 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1320 /* If a variable appears in a DATA block, it cannot have an
1324 gfc_error ("Variable '%s' at %C with an initializer already "
1325 "appears in a DATA statement", sym
->name
);
1329 /* Check if the assignment can happen. This has to be put off
1330 until later for derived type variables and procedure pointers. */
1331 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1332 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1333 && !sym
->attr
.proc_pointer
1334 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1337 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1338 && init
->ts
.type
== BT_CHARACTER
)
1340 /* Update symbol character length according initializer. */
1341 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1344 if (sym
->ts
.u
.cl
->length
== NULL
)
1347 /* If there are multiple CHARACTER variables declared on the
1348 same line, we don't want them to share the same length. */
1349 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1351 if (sym
->attr
.flavor
== FL_PARAMETER
)
1353 if (init
->expr_type
== EXPR_CONSTANT
)
1355 clen
= init
->value
.character
.length
;
1356 sym
->ts
.u
.cl
->length
1357 = gfc_get_int_expr (gfc_default_integer_kind
,
1360 else if (init
->expr_type
== EXPR_ARRAY
)
1363 c
= gfc_constructor_first (init
->value
.constructor
);
1364 clen
= c
->expr
->value
.character
.length
;
1365 sym
->ts
.u
.cl
->length
1366 = gfc_get_int_expr (gfc_default_integer_kind
,
1369 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1370 sym
->ts
.u
.cl
->length
=
1371 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1374 /* Update initializer character length according symbol. */
1375 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1377 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1379 if (init
->expr_type
== EXPR_CONSTANT
)
1380 gfc_set_constant_character_len (len
, init
, -1);
1381 else if (init
->expr_type
== EXPR_ARRAY
)
1385 /* Build a new charlen to prevent simplification from
1386 deleting the length before it is resolved. */
1387 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1388 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1390 for (c
= gfc_constructor_first (init
->value
.constructor
);
1391 c
; c
= gfc_constructor_next (c
))
1392 gfc_set_constant_character_len (len
, c
->expr
, -1);
1397 /* If sym is implied-shape, set its upper bounds from init. */
1398 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1399 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1403 if (init
->rank
== 0)
1405 gfc_error ("Can't initialize implied-shape array at %L"
1406 " with scalar", &sym
->declared_at
);
1409 gcc_assert (sym
->as
->rank
== init
->rank
);
1411 /* Shape should be present, we get an initialization expression. */
1412 gcc_assert (init
->shape
);
1414 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1420 lower
= sym
->as
->lower
[dim
];
1421 if (lower
->expr_type
!= EXPR_CONSTANT
)
1423 gfc_error ("Non-constant lower bound in implied-shape"
1424 " declaration at %L", &lower
->where
);
1428 /* All dimensions must be without upper bound. */
1429 gcc_assert (!sym
->as
->upper
[dim
]);
1432 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1433 mpz_add (e
->value
.integer
,
1434 lower
->value
.integer
, init
->shape
[dim
]);
1435 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1436 sym
->as
->upper
[dim
] = e
;
1439 sym
->as
->type
= AS_EXPLICIT
;
1442 /* Need to check if the expression we initialized this
1443 to was one of the iso_c_binding named constants. If so,
1444 and we're a parameter (constant), let it be iso_c.
1446 integer(c_int), parameter :: my_int = c_int
1447 integer(my_int) :: my_int_2
1448 If we mark my_int as iso_c (since we can see it's value
1449 is equal to one of the named constants), then my_int_2
1450 will be considered C interoperable. */
1451 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1453 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1454 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1455 /* attr bits needed for module files. */
1456 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1457 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1458 if (init
->ts
.is_iso_c
)
1459 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1462 /* Add initializer. Make sure we keep the ranks sane. */
1463 if (sym
->attr
.dimension
&& init
->rank
== 0)
1468 if (sym
->attr
.flavor
== FL_PARAMETER
1469 && init
->expr_type
== EXPR_CONSTANT
1470 && spec_size (sym
->as
, &size
) == SUCCESS
1471 && mpz_cmp_si (size
, 0) > 0)
1473 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1475 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1476 gfc_constructor_append_expr (&array
->value
.constructor
,
1479 : gfc_copy_expr (init
),
1482 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1483 for (n
= 0; n
< sym
->as
->rank
; n
++)
1484 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1489 init
->rank
= sym
->as
->rank
;
1493 if (sym
->attr
.save
== SAVE_NONE
)
1494 sym
->attr
.save
= SAVE_IMPLICIT
;
1502 /* Function called by variable_decl() that adds a name to a structure
1506 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1507 gfc_array_spec
**as
)
1510 gfc_try t
= SUCCESS
;
1512 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1513 constructing, it must have the pointer attribute. */
1514 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1515 && current_ts
.u
.derived
== gfc_current_block ()
1516 && current_attr
.pointer
== 0)
1518 gfc_error ("Component at %C must have the POINTER attribute");
1522 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1524 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1526 gfc_error ("Array component of structure at %C must have explicit "
1527 "or deferred shape");
1532 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1536 if (c
->ts
.type
== BT_CHARACTER
)
1538 c
->attr
= current_attr
;
1540 c
->initializer
= *init
;
1547 c
->attr
.codimension
= 1;
1549 c
->attr
.dimension
= 1;
1553 /* Should this ever get more complicated, combine with similar section
1554 in add_init_expr_to_sym into a separate function. */
1555 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
&& c
->ts
.u
.cl
1556 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1560 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1561 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1562 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1564 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1566 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1567 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1568 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1569 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1571 gfc_constructor
*ctor
;
1572 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1577 bool has_ts
= (c
->initializer
->ts
.u
.cl
1578 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1580 /* Remember the length of the first element for checking
1581 that all elements *in the constructor* have the same
1582 length. This need not be the length of the LHS! */
1583 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1584 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1585 first_len
= ctor
->expr
->value
.character
.length
;
1587 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1588 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1590 gfc_set_constant_character_len (len
, ctor
->expr
,
1591 has_ts
? -1 : first_len
);
1592 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1598 /* Check array components. */
1599 if (!c
->attr
.dimension
)
1602 if (c
->attr
.pointer
)
1604 if (c
->as
->type
!= AS_DEFERRED
)
1606 gfc_error ("Pointer array component of structure at %C must have a "
1611 else if (c
->attr
.allocatable
)
1613 if (c
->as
->type
!= AS_DEFERRED
)
1615 gfc_error ("Allocatable component of structure at %C must have a "
1622 if (c
->as
->type
!= AS_EXPLICIT
)
1624 gfc_error ("Array component of structure at %C must have an "
1631 if (c
->ts
.type
== BT_CLASS
)
1633 bool delayed
= (gfc_state_stack
->sym
== c
->ts
.u
.derived
)
1634 || (!c
->ts
.u
.derived
->components
1635 && !c
->ts
.u
.derived
->attr
.zero_comp
);
1636 gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, delayed
);
1644 /* Match a 'NULL()', and possibly take care of some side effects. */
1647 gfc_match_null (gfc_expr
**result
)
1652 m
= gfc_match (" null ( )");
1656 /* The NULL symbol now has to be/become an intrinsic function. */
1657 if (gfc_get_symbol ("null", NULL
, &sym
))
1659 gfc_error ("NULL() initialization at %C is ambiguous");
1663 gfc_intrinsic_symbol (sym
);
1665 if (sym
->attr
.proc
!= PROC_INTRINSIC
1666 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1667 sym
->name
, NULL
) == FAILURE
1668 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1671 *result
= gfc_get_null_expr (&gfc_current_locus
);
1677 /* Match the initialization expr for a data pointer or procedure pointer. */
1680 match_pointer_init (gfc_expr
**init
, int procptr
)
1684 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1686 gfc_error ("Initialization of pointer at %C is not allowed in "
1687 "a PURE procedure");
1691 /* Match NULL() initilization. */
1692 m
= gfc_match_null (init
);
1696 /* Match non-NULL initialization. */
1697 gfc_matching_ptr_assignment
= !procptr
;
1698 gfc_matching_procptr_assignment
= procptr
;
1699 m
= gfc_match_rvalue (init
);
1700 gfc_matching_ptr_assignment
= 0;
1701 gfc_matching_procptr_assignment
= 0;
1702 if (m
== MATCH_ERROR
)
1704 else if (m
== MATCH_NO
)
1706 gfc_error ("Error in pointer initialization at %C");
1711 gfc_resolve_expr (*init
);
1713 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: non-NULL pointer "
1714 "initialization at %C") == FAILURE
)
1721 /* Match a variable name with an optional initializer. When this
1722 subroutine is called, a variable is expected to be parsed next.
1723 Depending on what is happening at the moment, updates either the
1724 symbol table or the current interface. */
1727 variable_decl (int elem
)
1729 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1730 gfc_expr
*initializer
, *char_len
;
1732 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1744 /* When we get here, we've just matched a list of attributes and
1745 maybe a type and a double colon. The next thing we expect to see
1746 is the name of the symbol. */
1747 m
= gfc_match_name (name
);
1751 var_locus
= gfc_current_locus
;
1753 /* Now we could see the optional array spec. or character length. */
1754 m
= gfc_match_array_spec (&as
, true, true);
1755 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1756 cp_as
= gfc_copy_array_spec (as
);
1757 else if (m
== MATCH_ERROR
)
1761 as
= gfc_copy_array_spec (current_as
);
1762 else if (current_as
)
1763 merge_array_spec (current_as
, as
, true);
1765 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1766 determine (and check) whether it can be implied-shape. If it
1767 was parsed as assumed-size, change it because PARAMETERs can not
1771 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1774 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1779 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1780 && current_attr
.flavor
== FL_PARAMETER
)
1781 as
->type
= AS_IMPLIED_SHAPE
;
1783 if (as
->type
== AS_IMPLIED_SHAPE
1784 && gfc_notify_std (GFC_STD_F2008
,
1785 "Fortran 2008: Implied-shape array at %L",
1786 &var_locus
) == FAILURE
)
1795 cl_deferred
= false;
1797 if (current_ts
.type
== BT_CHARACTER
)
1799 switch (match_char_length (&char_len
, &cl_deferred
))
1802 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1804 cl
->length
= char_len
;
1807 /* Non-constant lengths need to be copied after the first
1808 element. Also copy assumed lengths. */
1811 && (current_ts
.u
.cl
->length
== NULL
1812 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1814 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1815 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1818 cl
= current_ts
.u
.cl
;
1820 cl_deferred
= current_ts
.deferred
;
1829 /* If this symbol has already shown up in a Cray Pointer declaration,
1830 then we want to set the type & bail out. */
1831 if (gfc_option
.flag_cray_pointer
)
1833 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1834 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1836 sym
->ts
.type
= current_ts
.type
;
1837 sym
->ts
.kind
= current_ts
.kind
;
1839 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1840 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1841 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1844 /* Check to see if we have an array specification. */
1847 if (sym
->as
!= NULL
)
1849 gfc_error ("Duplicate array spec for Cray pointee at %C");
1850 gfc_free_array_spec (cp_as
);
1856 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1857 gfc_internal_error ("Couldn't set pointee array spec.");
1859 /* Fix the array spec. */
1860 m
= gfc_mod_pointee_as (sym
->as
);
1861 if (m
== MATCH_ERROR
)
1869 gfc_free_array_spec (cp_as
);
1873 /* Procedure pointer as function result. */
1874 if (gfc_current_state () == COMP_FUNCTION
1875 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1876 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1877 strcpy (name
, "ppr@");
1879 if (gfc_current_state () == COMP_FUNCTION
1880 && strcmp (name
, gfc_current_block ()->name
) == 0
1881 && gfc_current_block ()->result
1882 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1883 strcpy (name
, "ppr@");
1885 /* OK, we've successfully matched the declaration. Now put the
1886 symbol in the current namespace, because it might be used in the
1887 optional initialization expression for this symbol, e.g. this is
1890 integer, parameter :: i = huge(i)
1892 This is only true for parameters or variables of a basic type.
1893 For components of derived types, it is not true, so we don't
1894 create a symbol for those yet. If we fail to create the symbol,
1896 if (gfc_current_state () != COMP_DERIVED
1897 && build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
) == FAILURE
)
1903 /* An interface body specifies all of the procedure's
1904 characteristics and these shall be consistent with those
1905 specified in the procedure definition, except that the interface
1906 may specify a procedure that is not pure if the procedure is
1907 defined to be pure(12.3.2). */
1908 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1909 && gfc_current_ns
->proc_name
1910 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1911 && current_ts
.u
.derived
->ns
!= gfc_current_ns
)
1914 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.u
.derived
->name
);
1915 if (!(current_ts
.u
.derived
->attr
.imported
1917 && st
->n
.sym
== current_ts
.u
.derived
)
1918 && !gfc_current_ns
->has_import_set
)
1920 gfc_error ("the type of '%s' at %C has not been declared within the "
1927 /* In functions that have a RESULT variable defined, the function
1928 name always refers to function calls. Therefore, the name is
1929 not allowed to appear in specification statements. */
1930 if (gfc_current_state () == COMP_FUNCTION
1931 && gfc_current_block () != NULL
1932 && gfc_current_block ()->result
!= NULL
1933 && gfc_current_block ()->result
!= gfc_current_block ()
1934 && strcmp (gfc_current_block ()->name
, name
) == 0)
1936 gfc_error ("Function name '%s' not allowed at %C", name
);
1941 /* We allow old-style initializations of the form
1942 integer i /2/, j(4) /3*3, 1/
1943 (if no colon has been seen). These are different from data
1944 statements in that initializers are only allowed to apply to the
1945 variable immediately preceding, i.e.
1947 is not allowed. Therefore we have to do some work manually, that
1948 could otherwise be left to the matchers for DATA statements. */
1950 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1952 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1953 "initialization at %C") == FAILURE
)
1956 return match_old_style_init (name
);
1959 /* The double colon must be present in order to have initializers.
1960 Otherwise the statement is ambiguous with an assignment statement. */
1963 if (gfc_match (" =>") == MATCH_YES
)
1965 if (!current_attr
.pointer
)
1967 gfc_error ("Initialization at %C isn't for a pointer variable");
1972 m
= match_pointer_init (&initializer
, 0);
1976 else if (gfc_match_char ('=') == MATCH_YES
)
1978 if (current_attr
.pointer
)
1980 gfc_error ("Pointer initialization at %C requires '=>', "
1986 m
= gfc_match_init_expr (&initializer
);
1989 gfc_error ("Expected an initialization expression at %C");
1993 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
1994 && gfc_state_stack
->state
!= COMP_DERIVED
)
1996 gfc_error ("Initialization of variable at %C is not allowed in "
1997 "a PURE procedure");
2006 if (initializer
!= NULL
&& current_attr
.allocatable
2007 && gfc_current_state () == COMP_DERIVED
)
2009 gfc_error ("Initialization of allocatable component at %C is not "
2015 /* Add the initializer. Note that it is fine if initializer is
2016 NULL here, because we sometimes also need to check if a
2017 declaration *must* have an initialization expression. */
2018 if (gfc_current_state () != COMP_DERIVED
)
2019 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2022 if (current_ts
.type
== BT_DERIVED
2023 && !current_attr
.pointer
&& !initializer
)
2024 initializer
= gfc_default_initializer (¤t_ts
);
2025 t
= build_struct (name
, cl
, &initializer
, &as
);
2028 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
2031 /* Free stuff up and return. */
2032 gfc_free_expr (initializer
);
2033 gfc_free_array_spec (as
);
2039 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2040 This assumes that the byte size is equal to the kind number for
2041 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2044 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2049 if (gfc_match_char ('*') != MATCH_YES
)
2052 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2056 original_kind
= ts
->kind
;
2058 /* Massage the kind numbers for complex types. */
2059 if (ts
->type
== BT_COMPLEX
)
2063 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2064 gfc_basic_typename (ts
->type
), original_kind
);
2070 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2072 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2073 gfc_basic_typename (ts
->type
), original_kind
);
2077 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
2078 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
2085 /* Match a kind specification. Since kinds are generally optional, we
2086 usually return MATCH_NO if something goes wrong. If a "kind="
2087 string is found, then we know we have an error. */
2090 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2102 where
= loc
= gfc_current_locus
;
2107 if (gfc_match_char ('(') == MATCH_NO
)
2110 /* Also gobbles optional text. */
2111 if (gfc_match (" kind = ") == MATCH_YES
)
2114 loc
= gfc_current_locus
;
2117 n
= gfc_match_init_expr (&e
);
2121 if (gfc_matching_function
)
2123 /* The function kind expression might include use associated or
2124 imported parameters and try again after the specification
2126 if (gfc_match_char (')') != MATCH_YES
)
2128 gfc_error ("Missing right parenthesis at %C");
2134 gfc_undo_symbols ();
2139 /* ....or else, the match is real. */
2141 gfc_error ("Expected initialization expression at %C");
2149 gfc_error ("Expected scalar initialization expression at %C");
2154 msg
= gfc_extract_int (e
, &ts
->kind
);
2163 /* Before throwing away the expression, let's see if we had a
2164 C interoperable kind (and store the fact). */
2165 if (e
->ts
.is_c_interop
== 1)
2167 /* Mark this as c interoperable if being declared with one
2168 of the named constants from iso_c_binding. */
2169 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2170 ts
->f90_type
= e
->ts
.f90_type
;
2176 /* Ignore errors to this point, if we've gotten here. This means
2177 we ignore the m=MATCH_ERROR from above. */
2178 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2180 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2181 gfc_basic_typename (ts
->type
));
2182 gfc_current_locus
= where
;
2186 /* Warn if, e.g., c_int is used for a REAL variable, but not
2187 if, e.g., c_double is used for COMPLEX as the standard
2188 explicitly says that the kind type parameter for complex and real
2189 variable is the same, i.e. c_float == c_float_complex. */
2190 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2191 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2192 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2193 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2194 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2195 gfc_basic_typename (ts
->type
));
2197 gfc_gobble_whitespace ();
2198 if ((c
= gfc_next_ascii_char ()) != ')'
2199 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2201 if (ts
->type
== BT_CHARACTER
)
2202 gfc_error ("Missing right parenthesis or comma at %C");
2204 gfc_error ("Missing right parenthesis at %C");
2208 /* All tests passed. */
2211 if(m
== MATCH_ERROR
)
2212 gfc_current_locus
= where
;
2214 /* Return what we know from the test(s). */
2219 gfc_current_locus
= where
;
2225 match_char_kind (int * kind
, int * is_iso_c
)
2234 where
= gfc_current_locus
;
2236 n
= gfc_match_init_expr (&e
);
2238 if (n
!= MATCH_YES
&& gfc_matching_function
)
2240 /* The expression might include use-associated or imported
2241 parameters and try again after the specification
2244 gfc_undo_symbols ();
2249 gfc_error ("Expected initialization expression at %C");
2255 gfc_error ("Expected scalar initialization expression at %C");
2260 msg
= gfc_extract_int (e
, kind
);
2261 *is_iso_c
= e
->ts
.is_iso_c
;
2271 /* Ignore errors to this point, if we've gotten here. This means
2272 we ignore the m=MATCH_ERROR from above. */
2273 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2275 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2279 /* All tests passed. */
2282 if (m
== MATCH_ERROR
)
2283 gfc_current_locus
= where
;
2285 /* Return what we know from the test(s). */
2290 gfc_current_locus
= where
;
2295 /* Match the various kind/length specifications in a CHARACTER
2296 declaration. We don't return MATCH_NO. */
2299 gfc_match_char_spec (gfc_typespec
*ts
)
2301 int kind
, seen_length
, is_iso_c
;
2313 /* Try the old-style specification first. */
2314 old_char_selector
= 0;
2316 m
= match_char_length (&len
, &deferred
);
2320 old_char_selector
= 1;
2325 m
= gfc_match_char ('(');
2328 m
= MATCH_YES
; /* Character without length is a single char. */
2332 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2333 if (gfc_match (" kind =") == MATCH_YES
)
2335 m
= match_char_kind (&kind
, &is_iso_c
);
2337 if (m
== MATCH_ERROR
)
2342 if (gfc_match (" , len =") == MATCH_NO
)
2345 m
= char_len_param_value (&len
, &deferred
);
2348 if (m
== MATCH_ERROR
)
2355 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2356 if (gfc_match (" len =") == MATCH_YES
)
2358 m
= char_len_param_value (&len
, &deferred
);
2361 if (m
== MATCH_ERROR
)
2365 if (gfc_match_char (')') == MATCH_YES
)
2368 if (gfc_match (" , kind =") != MATCH_YES
)
2371 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2377 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2378 m
= char_len_param_value (&len
, &deferred
);
2381 if (m
== MATCH_ERROR
)
2385 m
= gfc_match_char (')');
2389 if (gfc_match_char (',') != MATCH_YES
)
2392 gfc_match (" kind ="); /* Gobble optional text. */
2394 m
= match_char_kind (&kind
, &is_iso_c
);
2395 if (m
== MATCH_ERROR
)
2401 /* Require a right-paren at this point. */
2402 m
= gfc_match_char (')');
2407 gfc_error ("Syntax error in CHARACTER declaration at %C");
2409 gfc_free_expr (len
);
2413 /* Deal with character functions after USE and IMPORT statements. */
2414 if (gfc_matching_function
)
2416 gfc_free_expr (len
);
2417 gfc_undo_symbols ();
2423 gfc_free_expr (len
);
2427 /* Do some final massaging of the length values. */
2428 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2430 if (seen_length
== 0)
2431 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2436 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2437 ts
->deferred
= deferred
;
2439 /* We have to know if it was a c interoperable kind so we can
2440 do accurate type checking of bind(c) procs, etc. */
2442 /* Mark this as c interoperable if being declared with one
2443 of the named constants from iso_c_binding. */
2444 ts
->is_c_interop
= is_iso_c
;
2445 else if (len
!= NULL
)
2446 /* Here, we might have parsed something such as: character(c_char)
2447 In this case, the parsing code above grabs the c_char when
2448 looking for the length (line 1690, roughly). it's the last
2449 testcase for parsing the kind params of a character variable.
2450 However, it's not actually the length. this seems like it
2452 To see if the user used a C interop kind, test the expr
2453 of the so called length, and see if it's C interoperable. */
2454 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2460 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2461 structure to the matched specification. This is necessary for FUNCTION and
2462 IMPLICIT statements.
2464 If implicit_flag is nonzero, then we don't check for the optional
2465 kind specification. Not doing so is needed for matching an IMPLICIT
2466 statement correctly. */
2469 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2471 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2475 bool seen_deferred_kind
, matched_type
;
2477 /* A belt and braces check that the typespec is correctly being treated
2478 as a deferred characteristic association. */
2479 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2480 && (gfc_current_block ()->result
->ts
.kind
== -1)
2481 && (ts
->kind
== -1);
2483 if (seen_deferred_kind
)
2486 /* Clear the current binding label, in case one is given. */
2487 curr_binding_label
[0] = '\0';
2489 if (gfc_match (" byte") == MATCH_YES
)
2491 if (gfc_notify_std (GFC_STD_GNU
, "Extension: BYTE type at %C")
2495 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2497 gfc_error ("BYTE type used at %C "
2498 "is not available on the target machine");
2502 ts
->type
= BT_INTEGER
;
2508 m
= gfc_match (" type ( %n", name
);
2509 matched_type
= (m
== MATCH_YES
);
2511 if ((matched_type
&& strcmp ("integer", name
) == 0)
2512 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2514 ts
->type
= BT_INTEGER
;
2515 ts
->kind
= gfc_default_integer_kind
;
2519 if ((matched_type
&& strcmp ("character", name
) == 0)
2520 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2523 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2524 "intrinsic-type-spec at %C") == FAILURE
)
2527 ts
->type
= BT_CHARACTER
;
2528 if (implicit_flag
== 0)
2529 m
= gfc_match_char_spec (ts
);
2533 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2539 if ((matched_type
&& strcmp ("real", name
) == 0)
2540 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2543 ts
->kind
= gfc_default_real_kind
;
2548 && (strcmp ("doubleprecision", name
) == 0
2549 || (strcmp ("double", name
) == 0
2550 && gfc_match (" precision") == MATCH_YES
)))
2551 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2554 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2555 "intrinsic-type-spec at %C") == FAILURE
)
2557 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2561 ts
->kind
= gfc_default_double_kind
;
2565 if ((matched_type
&& strcmp ("complex", name
) == 0)
2566 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2568 ts
->type
= BT_COMPLEX
;
2569 ts
->kind
= gfc_default_complex_kind
;
2574 && (strcmp ("doublecomplex", name
) == 0
2575 || (strcmp ("double", name
) == 0
2576 && gfc_match (" complex") == MATCH_YES
)))
2577 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2579 if (gfc_notify_std (GFC_STD_GNU
, "Extension: DOUBLE COMPLEX at %C")
2584 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2585 "intrinsic-type-spec at %C") == FAILURE
)
2588 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2591 ts
->type
= BT_COMPLEX
;
2592 ts
->kind
= gfc_default_double_kind
;
2596 if ((matched_type
&& strcmp ("logical", name
) == 0)
2597 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2599 ts
->type
= BT_LOGICAL
;
2600 ts
->kind
= gfc_default_logical_kind
;
2605 m
= gfc_match_char (')');
2608 ts
->type
= BT_DERIVED
;
2611 m
= gfc_match (" class ( %n )", name
);
2614 ts
->type
= BT_CLASS
;
2616 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: CLASS statement at %C")
2621 /* Defer association of the derived type until the end of the
2622 specification block. However, if the derived type can be
2623 found, add it to the typespec. */
2624 if (gfc_matching_function
)
2626 ts
->u
.derived
= NULL
;
2627 if (gfc_current_state () != COMP_INTERFACE
2628 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2629 ts
->u
.derived
= sym
;
2633 /* Search for the name but allow the components to be defined later. If
2634 type = -1, this typespec has been seen in a function declaration but
2635 the type could not be accessed at that point. */
2637 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2639 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2642 else if (ts
->kind
== -1)
2644 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2645 || gfc_current_ns
->has_import_set
;
2646 if (gfc_find_symbol (name
, NULL
, iface
, &sym
))
2648 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2657 if (sym
->attr
.flavor
!= FL_DERIVED
2658 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2661 gfc_set_sym_referenced (sym
);
2662 ts
->u
.derived
= sym
;
2668 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2669 "intrinsic-type-spec at %C") == FAILURE
)
2672 /* For all types except double, derived and character, look for an
2673 optional kind specifier. MATCH_NO is actually OK at this point. */
2674 if (implicit_flag
== 1)
2676 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2682 if (gfc_current_form
== FORM_FREE
)
2684 c
= gfc_peek_ascii_char ();
2685 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2686 && c
!= ':' && c
!= ',')
2688 if (matched_type
&& c
== ')')
2690 gfc_next_ascii_char ();
2697 m
= gfc_match_kind_spec (ts
, false);
2698 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2699 m
= gfc_match_old_kind_spec (ts
);
2701 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2704 /* Defer association of the KIND expression of function results
2705 until after USE and IMPORT statements. */
2706 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2707 || gfc_matching_function
)
2711 m
= MATCH_YES
; /* No kind specifier found. */
2717 /* Match an IMPLICIT NONE statement. Actually, this statement is
2718 already matched in parse.c, or we would not end up here in the
2719 first place. So the only thing we need to check, is if there is
2720 trailing garbage. If not, the match is successful. */
2723 gfc_match_implicit_none (void)
2725 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2729 /* Match the letter range(s) of an IMPLICIT statement. */
2732 match_implicit_range (void)
2738 cur_loc
= gfc_current_locus
;
2740 gfc_gobble_whitespace ();
2741 c
= gfc_next_ascii_char ();
2744 gfc_error ("Missing character range in IMPLICIT at %C");
2751 gfc_gobble_whitespace ();
2752 c1
= gfc_next_ascii_char ();
2756 gfc_gobble_whitespace ();
2757 c
= gfc_next_ascii_char ();
2762 inner
= 0; /* Fall through. */
2769 gfc_gobble_whitespace ();
2770 c2
= gfc_next_ascii_char ();
2774 gfc_gobble_whitespace ();
2775 c
= gfc_next_ascii_char ();
2777 if ((c
!= ',') && (c
!= ')'))
2790 gfc_error ("Letters must be in alphabetic order in "
2791 "IMPLICIT statement at %C");
2795 /* See if we can add the newly matched range to the pending
2796 implicits from this IMPLICIT statement. We do not check for
2797 conflicts with whatever earlier IMPLICIT statements may have
2798 set. This is done when we've successfully finished matching
2800 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2807 gfc_syntax_error (ST_IMPLICIT
);
2809 gfc_current_locus
= cur_loc
;
2814 /* Match an IMPLICIT statement, storing the types for
2815 gfc_set_implicit() if the statement is accepted by the parser.
2816 There is a strange looking, but legal syntactic construction
2817 possible. It looks like:
2819 IMPLICIT INTEGER (a-b) (c-d)
2821 This is legal if "a-b" is a constant expression that happens to
2822 equal one of the legal kinds for integers. The real problem
2823 happens with an implicit specification that looks like:
2825 IMPLICIT INTEGER (a-b)
2827 In this case, a typespec matcher that is "greedy" (as most of the
2828 matchers are) gobbles the character range as a kindspec, leaving
2829 nothing left. We therefore have to go a bit more slowly in the
2830 matching process by inhibiting the kindspec checking during
2831 typespec matching and checking for a kind later. */
2834 gfc_match_implicit (void)
2843 /* We don't allow empty implicit statements. */
2844 if (gfc_match_eos () == MATCH_YES
)
2846 gfc_error ("Empty IMPLICIT statement at %C");
2852 /* First cleanup. */
2853 gfc_clear_new_implicit ();
2855 /* A basic type is mandatory here. */
2856 m
= gfc_match_decl_type_spec (&ts
, 1);
2857 if (m
== MATCH_ERROR
)
2862 cur_loc
= gfc_current_locus
;
2863 m
= match_implicit_range ();
2867 /* We may have <TYPE> (<RANGE>). */
2868 gfc_gobble_whitespace ();
2869 c
= gfc_next_ascii_char ();
2870 if ((c
== '\n') || (c
== ','))
2872 /* Check for CHARACTER with no length parameter. */
2873 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
2875 ts
.kind
= gfc_default_character_kind
;
2876 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2877 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
2881 /* Record the Successful match. */
2882 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2887 gfc_current_locus
= cur_loc
;
2890 /* Discard the (incorrectly) matched range. */
2891 gfc_clear_new_implicit ();
2893 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2894 if (ts
.type
== BT_CHARACTER
)
2895 m
= gfc_match_char_spec (&ts
);
2898 m
= gfc_match_kind_spec (&ts
, false);
2901 m
= gfc_match_old_kind_spec (&ts
);
2902 if (m
== MATCH_ERROR
)
2908 if (m
== MATCH_ERROR
)
2911 m
= match_implicit_range ();
2912 if (m
== MATCH_ERROR
)
2917 gfc_gobble_whitespace ();
2918 c
= gfc_next_ascii_char ();
2919 if ((c
!= '\n') && (c
!= ','))
2922 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2930 gfc_syntax_error (ST_IMPLICIT
);
2938 gfc_match_import (void)
2940 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2945 if (gfc_current_ns
->proc_name
== NULL
2946 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2948 gfc_error ("IMPORT statement at %C only permitted in "
2949 "an INTERFACE body");
2953 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2957 if (gfc_match_eos () == MATCH_YES
)
2959 /* All host variables should be imported. */
2960 gfc_current_ns
->has_import_set
= 1;
2964 if (gfc_match (" ::") == MATCH_YES
)
2966 if (gfc_match_eos () == MATCH_YES
)
2968 gfc_error ("Expecting list of named entities at %C");
2975 m
= gfc_match (" %n", name
);
2979 if (gfc_current_ns
->parent
!= NULL
2980 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2982 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2985 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2986 && gfc_find_symbol (name
,
2987 gfc_current_ns
->proc_name
->ns
->parent
,
2990 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2996 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2997 "at %C - does not exist.", name
);
3001 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
3003 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3008 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, sym
->name
);
3011 sym
->attr
.imported
= 1;
3023 if (gfc_match_eos () == MATCH_YES
)
3025 if (gfc_match_char (',') != MATCH_YES
)
3032 gfc_error ("Syntax error in IMPORT statement at %C");
3037 /* A minimal implementation of gfc_match without whitespace, escape
3038 characters or variable arguments. Returns true if the next
3039 characters match the TARGET template exactly. */
3042 match_string_p (const char *target
)
3046 for (p
= target
; *p
; p
++)
3047 if ((char) gfc_next_ascii_char () != *p
)
3052 /* Matches an attribute specification including array specs. If
3053 successful, leaves the variables current_attr and current_as
3054 holding the specification. Also sets the colon_seen variable for
3055 later use by matchers associated with initializations.
3057 This subroutine is a little tricky in the sense that we don't know
3058 if we really have an attr-spec until we hit the double colon.
3059 Until that time, we can only return MATCH_NO. This forces us to
3060 check for duplicate specification at this level. */
3063 match_attr_spec (void)
3065 /* Modifiers that can exist in a type statement. */
3067 { GFC_DECL_BEGIN
= 0,
3068 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3069 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3070 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3071 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3072 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3073 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3077 /* GFC_DECL_END is the sentinel, index starts at 0. */
3078 #define NUM_DECL GFC_DECL_END
3080 locus start
, seen_at
[NUM_DECL
];
3087 gfc_clear_attr (¤t_attr
);
3088 start
= gfc_current_locus
;
3093 /* See if we get all of the keywords up to the final double colon. */
3094 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3102 gfc_gobble_whitespace ();
3104 ch
= gfc_next_ascii_char ();
3107 /* This is the successful exit condition for the loop. */
3108 if (gfc_next_ascii_char () == ':')
3113 gfc_gobble_whitespace ();
3114 switch (gfc_peek_ascii_char ())
3117 gfc_next_ascii_char ();
3118 switch (gfc_next_ascii_char ())
3121 if (match_string_p ("locatable"))
3123 /* Matched "allocatable". */
3124 d
= DECL_ALLOCATABLE
;
3129 if (match_string_p ("ynchronous"))
3131 /* Matched "asynchronous". */
3132 d
= DECL_ASYNCHRONOUS
;
3139 /* Try and match the bind(c). */
3140 m
= gfc_match_bind_c (NULL
, true);
3143 else if (m
== MATCH_ERROR
)
3148 gfc_next_ascii_char ();
3149 if ('o' != gfc_next_ascii_char ())
3151 switch (gfc_next_ascii_char ())
3154 if (match_string_p ("imension"))
3156 d
= DECL_CODIMENSION
;
3160 if (match_string_p ("tiguous"))
3162 d
= DECL_CONTIGUOUS
;
3169 if (match_string_p ("dimension"))
3174 if (match_string_p ("external"))
3179 if (match_string_p ("int"))
3181 ch
= gfc_next_ascii_char ();
3184 if (match_string_p ("nt"))
3186 /* Matched "intent". */
3187 /* TODO: Call match_intent_spec from here. */
3188 if (gfc_match (" ( in out )") == MATCH_YES
)
3190 else if (gfc_match (" ( in )") == MATCH_YES
)
3192 else if (gfc_match (" ( out )") == MATCH_YES
)
3198 if (match_string_p ("insic"))
3200 /* Matched "intrinsic". */
3208 if (match_string_p ("optional"))
3213 gfc_next_ascii_char ();
3214 switch (gfc_next_ascii_char ())
3217 if (match_string_p ("rameter"))
3219 /* Matched "parameter". */
3225 if (match_string_p ("inter"))
3227 /* Matched "pointer". */
3233 ch
= gfc_next_ascii_char ();
3236 if (match_string_p ("vate"))
3238 /* Matched "private". */
3244 if (match_string_p ("tected"))
3246 /* Matched "protected". */
3253 if (match_string_p ("blic"))
3255 /* Matched "public". */
3263 if (match_string_p ("save"))
3268 if (match_string_p ("target"))
3273 gfc_next_ascii_char ();
3274 ch
= gfc_next_ascii_char ();
3277 if (match_string_p ("lue"))
3279 /* Matched "value". */
3285 if (match_string_p ("latile"))
3287 /* Matched "volatile". */
3295 /* No double colon and no recognizable decl_type, so assume that
3296 we've been looking at something else the whole time. */
3303 /* Check to make sure any parens are paired up correctly. */
3304 if (gfc_match_parens () == MATCH_ERROR
)
3311 seen_at
[d
] = gfc_current_locus
;
3313 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3315 gfc_array_spec
*as
= NULL
;
3317 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3318 d
== DECL_CODIMENSION
);
3320 if (current_as
== NULL
)
3322 else if (m
== MATCH_YES
)
3324 merge_array_spec (as
, current_as
, false);
3330 if (d
== DECL_CODIMENSION
)
3331 gfc_error ("Missing codimension specification at %C");
3333 gfc_error ("Missing dimension specification at %C");
3337 if (m
== MATCH_ERROR
)
3342 /* Since we've seen a double colon, we have to be looking at an
3343 attr-spec. This means that we can now issue errors. */
3344 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3349 case DECL_ALLOCATABLE
:
3350 attr
= "ALLOCATABLE";
3352 case DECL_ASYNCHRONOUS
:
3353 attr
= "ASYNCHRONOUS";
3355 case DECL_CODIMENSION
:
3356 attr
= "CODIMENSION";
3358 case DECL_CONTIGUOUS
:
3359 attr
= "CONTIGUOUS";
3361 case DECL_DIMENSION
:
3368 attr
= "INTENT (IN)";
3371 attr
= "INTENT (OUT)";
3374 attr
= "INTENT (IN OUT)";
3376 case DECL_INTRINSIC
:
3382 case DECL_PARAMETER
:
3388 case DECL_PROTECTED
:
3403 case DECL_IS_BIND_C
:
3413 attr
= NULL
; /* This shouldn't happen. */
3416 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3421 /* Now that we've dealt with duplicate attributes, add the attributes
3422 to the current attribute. */
3423 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3428 if (gfc_current_state () == COMP_DERIVED
3429 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3430 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3431 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3433 if (d
== DECL_ALLOCATABLE
)
3435 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3436 "attribute at %C in a TYPE definition")
3445 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3452 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3453 && gfc_current_state () != COMP_MODULE
)
3455 if (d
== DECL_PRIVATE
)
3459 if (gfc_current_state () == COMP_DERIVED
3460 && gfc_state_stack
->previous
3461 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3463 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3464 "at %L in a TYPE definition", attr
,
3474 gfc_error ("%s attribute at %L is not allowed outside of the "
3475 "specification part of a module", attr
, &seen_at
[d
]);
3483 case DECL_ALLOCATABLE
:
3484 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3487 case DECL_ASYNCHRONOUS
:
3488 if (gfc_notify_std (GFC_STD_F2003
,
3489 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3493 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3496 case DECL_CODIMENSION
:
3497 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3500 case DECL_CONTIGUOUS
:
3501 if (gfc_notify_std (GFC_STD_F2008
,
3502 "Fortran 2008: CONTIGUOUS attribute at %C")
3506 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3509 case DECL_DIMENSION
:
3510 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3514 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3518 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3522 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3526 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3529 case DECL_INTRINSIC
:
3530 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3534 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3537 case DECL_PARAMETER
:
3538 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3542 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3545 case DECL_PROTECTED
:
3546 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3548 gfc_error ("PROTECTED at %C only allowed in specification "
3549 "part of a module");
3554 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3559 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3563 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3568 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3573 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3577 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3580 case DECL_IS_BIND_C
:
3581 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3585 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3590 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3594 if (gfc_notify_std (GFC_STD_F2003
,
3595 "Fortran 2003: VOLATILE attribute at %C")
3599 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3603 gfc_internal_error ("match_attr_spec(): Bad attribute");
3613 /* Module variables implicitly have the SAVE attribute. */
3614 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
)
3615 current_attr
.save
= SAVE_IMPLICIT
;
3621 gfc_current_locus
= start
;
3622 gfc_free_array_spec (current_as
);
3628 /* Set the binding label, dest_label, either with the binding label
3629 stored in the given gfc_typespec, ts, or if none was provided, it
3630 will be the symbol name in all lower case, as required by the draft
3631 (J3/04-007, section 15.4.1). If a binding label was given and
3632 there is more than one argument (num_idents), it is an error. */
3635 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3637 if (num_idents
> 1 && has_name_equals
)
3639 gfc_error ("Multiple identifiers provided with "
3640 "single NAME= specifier at %C");
3644 if (curr_binding_label
[0] != '\0')
3646 /* Binding label given; store in temp holder til have sym. */
3647 strcpy (dest_label
, curr_binding_label
);
3651 /* No binding label given, and the NAME= specifier did not exist,
3652 which means there was no NAME="". */
3653 if (sym_name
!= NULL
&& has_name_equals
== 0)
3654 strcpy (dest_label
, sym_name
);
3661 /* Set the status of the given common block as being BIND(C) or not,
3662 depending on the given parameter, is_bind_c. */
3665 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3667 com_block
->is_bind_c
= is_bind_c
;
3672 /* Verify that the given gfc_typespec is for a C interoperable type. */
3675 verify_c_interop (gfc_typespec
*ts
)
3677 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3678 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3679 ? SUCCESS
: FAILURE
;
3680 else if (ts
->is_c_interop
!= 1)
3687 /* Verify that the variables of a given common block, which has been
3688 defined with the attribute specifier bind(c), to be of a C
3689 interoperable type. Errors will be reported here, if
3693 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3695 gfc_symbol
*curr_sym
= NULL
;
3696 gfc_try retval
= SUCCESS
;
3698 curr_sym
= com_block
->head
;
3700 /* Make sure we have at least one symbol. */
3701 if (curr_sym
== NULL
)
3704 /* Here we know we have a symbol, so we'll execute this loop
3708 /* The second to last param, 1, says this is in a common block. */
3709 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3710 curr_sym
= curr_sym
->common_next
;
3711 } while (curr_sym
!= NULL
);
3717 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3718 an appropriate error message is reported. */
3721 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3722 int is_in_common
, gfc_common_head
*com_block
)
3724 bool bind_c_function
= false;
3725 gfc_try retval
= SUCCESS
;
3727 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3728 bind_c_function
= true;
3730 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3732 tmp_sym
= tmp_sym
->result
;
3733 /* Make sure it wasn't an implicitly typed result. */
3734 if (tmp_sym
->attr
.implicit_type
)
3736 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3737 "%L may not be C interoperable", tmp_sym
->name
,
3738 &tmp_sym
->declared_at
);
3739 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3740 /* Mark it as C interoperable to prevent duplicate warnings. */
3741 tmp_sym
->ts
.is_c_interop
= 1;
3742 tmp_sym
->attr
.is_c_interop
= 1;
3746 /* Here, we know we have the bind(c) attribute, so if we have
3747 enough type info, then verify that it's a C interop kind.
3748 The info could be in the symbol already, or possibly still in
3749 the given ts (current_ts), so look in both. */
3750 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3752 if (verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3754 /* See if we're dealing with a sym in a common block or not. */
3755 if (is_in_common
== 1)
3757 gfc_warning ("Variable '%s' in common block '%s' at %L "
3758 "may not be a C interoperable "
3759 "kind though common block '%s' is BIND(C)",
3760 tmp_sym
->name
, com_block
->name
,
3761 &(tmp_sym
->declared_at
), com_block
->name
);
3765 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3766 gfc_error ("Type declaration '%s' at %L is not C "
3767 "interoperable but it is BIND(C)",
3768 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3770 gfc_warning ("Variable '%s' at %L "
3771 "may not be a C interoperable "
3772 "kind but it is bind(c)",
3773 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3777 /* Variables declared w/in a common block can't be bind(c)
3778 since there's no way for C to see these variables, so there's
3779 semantically no reason for the attribute. */
3780 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3782 gfc_error ("Variable '%s' in common block '%s' at "
3783 "%L cannot be declared with BIND(C) "
3784 "since it is not a global",
3785 tmp_sym
->name
, com_block
->name
,
3786 &(tmp_sym
->declared_at
));
3790 /* Scalar variables that are bind(c) can not have the pointer
3791 or allocatable attributes. */
3792 if (tmp_sym
->attr
.is_bind_c
== 1)
3794 if (tmp_sym
->attr
.pointer
== 1)
3796 gfc_error ("Variable '%s' at %L cannot have both the "
3797 "POINTER and BIND(C) attributes",
3798 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3802 if (tmp_sym
->attr
.allocatable
== 1)
3804 gfc_error ("Variable '%s' at %L cannot have both the "
3805 "ALLOCATABLE and BIND(C) attributes",
3806 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3812 /* If it is a BIND(C) function, make sure the return value is a
3813 scalar value. The previous tests in this function made sure
3814 the type is interoperable. */
3815 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
3816 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3817 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3819 /* BIND(C) functions can not return a character string. */
3820 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
3821 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
3822 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
3823 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
3824 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3825 "be a character string", tmp_sym
->name
,
3826 &(tmp_sym
->declared_at
));
3829 /* See if the symbol has been marked as private. If it has, make sure
3830 there is no binding label and warn the user if there is one. */
3831 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3832 && tmp_sym
->binding_label
[0] != '\0')
3833 /* Use gfc_warning_now because we won't say that the symbol fails
3834 just because of this. */
3835 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3836 "given the binding label '%s'", tmp_sym
->name
,
3837 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3843 /* Set the appropriate fields for a symbol that's been declared as
3844 BIND(C) (the is_bind_c flag and the binding label), and verify that
3845 the type is C interoperable. Errors are reported by the functions
3846 used to set/test these fields. */
3849 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3851 gfc_try retval
= SUCCESS
;
3853 /* TODO: Do we need to make sure the vars aren't marked private? */
3855 /* Set the is_bind_c bit in symbol_attribute. */
3856 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3858 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3859 num_idents
) != SUCCESS
)
3866 /* Set the fields marking the given common block as BIND(C), including
3867 a binding label, and report any errors encountered. */
3870 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3872 gfc_try retval
= SUCCESS
;
3874 /* destLabel, common name, typespec (which may have binding label). */
3875 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3879 /* Set the given common block (com_block) to being bind(c) (1). */
3880 set_com_block_bind_c (com_block
, 1);
3886 /* Retrieve the list of one or more identifiers that the given bind(c)
3887 attribute applies to. */
3890 get_bind_c_idents (void)
3892 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3894 gfc_symbol
*tmp_sym
= NULL
;
3896 gfc_common_head
*com_block
= NULL
;
3898 if (gfc_match_name (name
) == MATCH_YES
)
3900 found_id
= MATCH_YES
;
3901 gfc_get_ha_symbol (name
, &tmp_sym
);
3903 else if (match_common_name (name
) == MATCH_YES
)
3905 found_id
= MATCH_YES
;
3906 com_block
= gfc_get_common (name
, 0);
3910 gfc_error ("Need either entity or common block name for "
3911 "attribute specification statement at %C");
3915 /* Save the current identifier and look for more. */
3918 /* Increment the number of identifiers found for this spec stmt. */
3921 /* Make sure we have a sym or com block, and verify that it can
3922 be bind(c). Set the appropriate field(s) and look for more
3924 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3926 if (tmp_sym
!= NULL
)
3928 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3934 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3939 /* Look to see if we have another identifier. */
3941 if (gfc_match_eos () == MATCH_YES
)
3942 found_id
= MATCH_NO
;
3943 else if (gfc_match_char (',') != MATCH_YES
)
3944 found_id
= MATCH_NO
;
3945 else if (gfc_match_name (name
) == MATCH_YES
)
3947 found_id
= MATCH_YES
;
3948 gfc_get_ha_symbol (name
, &tmp_sym
);
3950 else if (match_common_name (name
) == MATCH_YES
)
3952 found_id
= MATCH_YES
;
3953 com_block
= gfc_get_common (name
, 0);
3957 gfc_error ("Missing entity or common block name for "
3958 "attribute specification statement at %C");
3964 gfc_internal_error ("Missing symbol");
3966 } while (found_id
== MATCH_YES
);
3968 /* if we get here we were successful */
3973 /* Try and match a BIND(C) attribute specification statement. */
3976 gfc_match_bind_c_stmt (void)
3978 match found_match
= MATCH_NO
;
3983 /* This may not be necessary. */
3985 /* Clear the temporary binding label holder. */
3986 curr_binding_label
[0] = '\0';
3988 /* Look for the bind(c). */
3989 found_match
= gfc_match_bind_c (NULL
, true);
3991 if (found_match
== MATCH_YES
)
3993 /* Look for the :: now, but it is not required. */
3996 /* Get the identifier(s) that needs to be updated. This may need to
3997 change to hand the flag(s) for the attr specified so all identifiers
3998 found can have all appropriate parts updated (assuming that the same
3999 spec stmt can have multiple attrs, such as both bind(c) and
4001 if (get_bind_c_idents () != SUCCESS
)
4002 /* Error message should have printed already. */
4010 /* Match a data declaration statement. */
4013 gfc_match_data_decl (void)
4019 num_idents_on_line
= 0;
4021 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4025 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4026 && gfc_current_state () != COMP_DERIVED
)
4028 sym
= gfc_use_derived (current_ts
.u
.derived
);
4036 current_ts
.u
.derived
= sym
;
4039 m
= match_attr_spec ();
4040 if (m
== MATCH_ERROR
)
4046 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4047 && current_ts
.u
.derived
->components
== NULL
4048 && !current_ts
.u
.derived
->attr
.zero_comp
)
4051 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4054 gfc_find_symbol (current_ts
.u
.derived
->name
,
4055 current_ts
.u
.derived
->ns
->parent
, 1, &sym
);
4057 /* Any symbol that we find had better be a type definition
4058 which has its components defined. */
4059 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4060 && (current_ts
.u
.derived
->components
!= NULL
4061 || current_ts
.u
.derived
->attr
.zero_comp
))
4064 /* Now we have an error, which we signal, and then fix up
4065 because the knock-on is plain and simple confusing. */
4066 gfc_error_now ("Derived type at %C has not been previously defined "
4067 "and so cannot appear in a derived type definition");
4068 current_attr
.pointer
= 1;
4073 /* If we have an old-style character declaration, and no new-style
4074 attribute specifications, then there a comma is optional between
4075 the type specification and the variable list. */
4076 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4077 gfc_match_char (',');
4079 /* Give the types/attributes to symbols that follow. Give the element
4080 a number so that repeat character length expressions can be copied. */
4084 num_idents_on_line
++;
4085 m
= variable_decl (elem
++);
4086 if (m
== MATCH_ERROR
)
4091 if (gfc_match_eos () == MATCH_YES
)
4093 if (gfc_match_char (',') != MATCH_YES
)
4097 if (gfc_error_flag_test () == 0)
4098 gfc_error ("Syntax error in data declaration at %C");
4101 gfc_free_data_all (gfc_current_ns
);
4104 gfc_free_array_spec (current_as
);
4110 /* Match a prefix associated with a function or subroutine
4111 declaration. If the typespec pointer is nonnull, then a typespec
4112 can be matched. Note that if nothing matches, MATCH_YES is
4113 returned (the null string was matched). */
4116 gfc_match_prefix (gfc_typespec
*ts
)
4122 gfc_clear_attr (¤t_attr
);
4124 seen_impure
= false;
4126 gcc_assert (!gfc_matching_prefix
);
4127 gfc_matching_prefix
= true;
4131 found_prefix
= false;
4133 if (!seen_type
&& ts
!= NULL
4134 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4135 && gfc_match_space () == MATCH_YES
)
4139 found_prefix
= true;
4142 if (gfc_match ("elemental% ") == MATCH_YES
)
4144 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
4147 found_prefix
= true;
4150 if (gfc_match ("pure% ") == MATCH_YES
)
4152 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4155 found_prefix
= true;
4158 if (gfc_match ("recursive% ") == MATCH_YES
)
4160 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
4163 found_prefix
= true;
4166 /* IMPURE is a somewhat special case, as it needs not set an actual
4167 attribute but rather only prevents ELEMENTAL routines from being
4168 automatically PURE. */
4169 if (gfc_match ("impure% ") == MATCH_YES
)
4171 if (gfc_notify_std (GFC_STD_F2008
,
4172 "Fortran 2008: IMPURE procedure at %C")
4177 found_prefix
= true;
4180 while (found_prefix
);
4182 /* IMPURE and PURE must not both appear, of course. */
4183 if (seen_impure
&& current_attr
.pure
)
4185 gfc_error ("PURE and IMPURE must not appear both at %C");
4189 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4190 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4192 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4196 /* At this point, the next item is not a prefix. */
4197 gcc_assert (gfc_matching_prefix
);
4198 gfc_matching_prefix
= false;
4202 gcc_assert (gfc_matching_prefix
);
4203 gfc_matching_prefix
= false;
4208 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4211 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4213 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
4216 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
4219 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
4226 /* Match a formal argument list. */
4229 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4231 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4232 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4238 if (gfc_match_char ('(') != MATCH_YES
)
4245 if (gfc_match_char (')') == MATCH_YES
)
4250 if (gfc_match_char ('*') == MATCH_YES
)
4254 m
= gfc_match_name (name
);
4258 if (gfc_get_symbol (name
, NULL
, &sym
))
4262 p
= gfc_get_formal_arglist ();
4274 /* We don't add the VARIABLE flavor because the name could be a
4275 dummy procedure. We don't apply these attributes to formal
4276 arguments of statement functions. */
4277 if (sym
!= NULL
&& !st_flag
4278 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
4279 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
4285 /* The name of a program unit can be in a different namespace,
4286 so check for it explicitly. After the statement is accepted,
4287 the name is checked for especially in gfc_get_symbol(). */
4288 if (gfc_new_block
!= NULL
&& sym
!= NULL
4289 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4291 gfc_error ("Name '%s' at %C is the name of the procedure",
4297 if (gfc_match_char (')') == MATCH_YES
)
4300 m
= gfc_match_char (',');
4303 gfc_error ("Unexpected junk in formal argument list at %C");
4309 /* Check for duplicate symbols in the formal argument list. */
4312 for (p
= head
; p
->next
; p
= p
->next
)
4317 for (q
= p
->next
; q
; q
= q
->next
)
4318 if (p
->sym
== q
->sym
)
4320 gfc_error ("Duplicate symbol '%s' in formal argument list "
4321 "at %C", p
->sym
->name
);
4329 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
4339 gfc_free_formal_arglist (head
);
4344 /* Match a RESULT specification following a function declaration or
4345 ENTRY statement. Also matches the end-of-statement. */
4348 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4350 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4354 if (gfc_match (" result (") != MATCH_YES
)
4357 m
= gfc_match_name (name
);
4361 /* Get the right paren, and that's it because there could be the
4362 bind(c) attribute after the result clause. */
4363 if (gfc_match_char(')') != MATCH_YES
)
4365 /* TODO: should report the missing right paren here. */
4369 if (strcmp (function
->name
, name
) == 0)
4371 gfc_error ("RESULT variable at %C must be different than function name");
4375 if (gfc_get_symbol (name
, NULL
, &r
))
4378 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
4387 /* Match a function suffix, which could be a combination of a result
4388 clause and BIND(C), either one, or neither. The draft does not
4389 require them to come in a specific order. */
4392 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4394 match is_bind_c
; /* Found bind(c). */
4395 match is_result
; /* Found result clause. */
4396 match found_match
; /* Status of whether we've found a good match. */
4397 char peek_char
; /* Character we're going to peek at. */
4398 bool allow_binding_name
;
4400 /* Initialize to having found nothing. */
4401 found_match
= MATCH_NO
;
4402 is_bind_c
= MATCH_NO
;
4403 is_result
= MATCH_NO
;
4405 /* Get the next char to narrow between result and bind(c). */
4406 gfc_gobble_whitespace ();
4407 peek_char
= gfc_peek_ascii_char ();
4409 /* C binding names are not allowed for internal procedures. */
4410 if (gfc_current_state () == COMP_CONTAINS
4411 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4412 allow_binding_name
= false;
4414 allow_binding_name
= true;
4419 /* Look for result clause. */
4420 is_result
= match_result (sym
, result
);
4421 if (is_result
== MATCH_YES
)
4423 /* Now see if there is a bind(c) after it. */
4424 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4425 /* We've found the result clause and possibly bind(c). */
4426 found_match
= MATCH_YES
;
4429 /* This should only be MATCH_ERROR. */
4430 found_match
= is_result
;
4433 /* Look for bind(c) first. */
4434 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4435 if (is_bind_c
== MATCH_YES
)
4437 /* Now see if a result clause followed it. */
4438 is_result
= match_result (sym
, result
);
4439 found_match
= MATCH_YES
;
4443 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4444 found_match
= MATCH_ERROR
;
4448 gfc_error ("Unexpected junk after function declaration at %C");
4449 found_match
= MATCH_ERROR
;
4453 if (is_bind_c
== MATCH_YES
)
4455 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4456 if (gfc_current_state () == COMP_CONTAINS
4457 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4458 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4459 "at %L may not be specified for an internal "
4460 "procedure", &gfc_current_locus
)
4464 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4473 /* Procedure pointer return value without RESULT statement:
4474 Add "hidden" result variable named "ppr@". */
4477 add_hidden_procptr_result (gfc_symbol
*sym
)
4481 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4484 /* First usage case: PROCEDURE and EXTERNAL statements. */
4485 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4486 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4487 && sym
->attr
.external
;
4488 /* Second usage case: INTERFACE statements. */
4489 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4490 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4491 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4497 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4501 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4502 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4503 st2
->n
.sym
= stree
->n
.sym
;
4505 sym
->result
= stree
->n
.sym
;
4507 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4508 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4509 sym
->result
->attr
.external
= sym
->attr
.external
;
4510 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4511 sym
->result
->ts
= sym
->ts
;
4512 sym
->attr
.proc_pointer
= 0;
4513 sym
->attr
.pointer
= 0;
4514 sym
->attr
.external
= 0;
4515 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4517 sym
->result
->attr
.pointer
= 0;
4518 sym
->result
->attr
.proc_pointer
= 1;
4521 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4523 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4524 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4525 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4526 && sym
== gfc_current_ns
->proc_name
4527 && sym
== sym
->result
->ns
->proc_name
4528 && strcmp ("ppr@", sym
->result
->name
) == 0)
4530 sym
->result
->attr
.proc_pointer
= 1;
4531 sym
->attr
.pointer
= 0;
4539 /* Match the interface for a PROCEDURE declaration,
4540 including brackets (R1212). */
4543 match_procedure_interface (gfc_symbol
**proc_if
)
4547 locus old_loc
, entry_loc
;
4548 gfc_namespace
*old_ns
= gfc_current_ns
;
4549 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4551 old_loc
= entry_loc
= gfc_current_locus
;
4552 gfc_clear_ts (¤t_ts
);
4554 if (gfc_match (" (") != MATCH_YES
)
4556 gfc_current_locus
= entry_loc
;
4560 /* Get the type spec. for the procedure interface. */
4561 old_loc
= gfc_current_locus
;
4562 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4563 gfc_gobble_whitespace ();
4564 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4567 if (m
== MATCH_ERROR
)
4570 /* Procedure interface is itself a procedure. */
4571 gfc_current_locus
= old_loc
;
4572 m
= gfc_match_name (name
);
4574 /* First look to see if it is already accessible in the current
4575 namespace because it is use associated or contained. */
4577 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4580 /* If it is still not found, then try the parent namespace, if it
4581 exists and create the symbol there if it is still not found. */
4582 if (gfc_current_ns
->parent
)
4583 gfc_current_ns
= gfc_current_ns
->parent
;
4584 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4587 gfc_current_ns
= old_ns
;
4588 *proc_if
= st
->n
.sym
;
4590 /* Various interface checks. */
4594 /* Resolve interface if possible. That way, attr.procedure is only set
4595 if it is declared by a later procedure-declaration-stmt, which is
4596 invalid per C1212. */
4597 while ((*proc_if
)->ts
.interface
)
4598 *proc_if
= (*proc_if
)->ts
.interface
;
4600 if ((*proc_if
)->generic
)
4602 gfc_error ("Interface '%s' at %C may not be generic",
4606 if ((*proc_if
)->attr
.proc
== PROC_ST_FUNCTION
)
4608 gfc_error ("Interface '%s' at %C may not be a statement function",
4612 /* Handle intrinsic procedures. */
4613 if (!((*proc_if
)->attr
.external
|| (*proc_if
)->attr
.use_assoc
4614 || (*proc_if
)->attr
.if_source
== IFSRC_IFBODY
)
4615 && (gfc_is_intrinsic ((*proc_if
), 0, gfc_current_locus
)
4616 || gfc_is_intrinsic ((*proc_if
), 1, gfc_current_locus
)))
4617 (*proc_if
)->attr
.intrinsic
= 1;
4618 if ((*proc_if
)->attr
.intrinsic
4619 && !gfc_intrinsic_actual_ok ((*proc_if
)->name
, 0))
4621 gfc_error ("Intrinsic procedure '%s' not allowed "
4622 "in PROCEDURE statement at %C", (*proc_if
)->name
);
4628 if (gfc_match (" )") != MATCH_YES
)
4630 gfc_current_locus
= entry_loc
;
4638 /* Match a PROCEDURE declaration (R1211). */
4641 match_procedure_decl (void)
4644 gfc_symbol
*sym
, *proc_if
= NULL
;
4646 gfc_expr
*initializer
= NULL
;
4648 /* Parse interface (with brackets). */
4649 m
= match_procedure_interface (&proc_if
);
4653 /* Parse attributes (with colons). */
4654 m
= match_attr_spec();
4655 if (m
== MATCH_ERROR
)
4658 /* Get procedure symbols. */
4661 m
= gfc_match_symbol (&sym
, 0);
4664 else if (m
== MATCH_ERROR
)
4667 /* Add current_attr to the symbol attributes. */
4668 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4671 if (sym
->attr
.is_bind_c
)
4673 /* Check for C1218. */
4674 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4676 gfc_error ("BIND(C) attribute at %C requires "
4677 "an interface with BIND(C)");
4680 /* Check for C1217. */
4681 if (has_name_equals
&& sym
->attr
.pointer
)
4683 gfc_error ("BIND(C) procedure with NAME may not have "
4684 "POINTER attribute at %C");
4687 if (has_name_equals
&& sym
->attr
.dummy
)
4689 gfc_error ("Dummy procedure at %C may not have "
4690 "BIND(C) attribute with NAME");
4693 /* Set binding label for BIND(C). */
4694 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4698 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4701 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4704 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4707 /* Set interface. */
4708 if (proc_if
!= NULL
)
4710 if (sym
->ts
.type
!= BT_UNKNOWN
)
4712 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4713 sym
->name
, &gfc_current_locus
,
4714 gfc_basic_typename (sym
->ts
.type
));
4717 sym
->ts
.interface
= proc_if
;
4718 sym
->attr
.untyped
= 1;
4719 sym
->attr
.if_source
= IFSRC_IFBODY
;
4721 else if (current_ts
.type
!= BT_UNKNOWN
)
4723 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4725 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4726 sym
->ts
.interface
->ts
= current_ts
;
4727 sym
->ts
.interface
->attr
.function
= 1;
4728 sym
->attr
.function
= sym
->ts
.interface
->attr
.function
;
4729 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4732 if (gfc_match (" =>") == MATCH_YES
)
4734 if (!current_attr
.pointer
)
4736 gfc_error ("Initialization at %C isn't for a pointer variable");
4741 m
= match_pointer_init (&initializer
, 1);
4745 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4751 gfc_set_sym_referenced (sym
);
4753 if (gfc_match_eos () == MATCH_YES
)
4755 if (gfc_match_char (',') != MATCH_YES
)
4760 gfc_error ("Syntax error in PROCEDURE statement at %C");
4764 /* Free stuff up and return. */
4765 gfc_free_expr (initializer
);
4771 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4774 /* Match a procedure pointer component declaration (R445). */
4777 match_ppc_decl (void)
4780 gfc_symbol
*proc_if
= NULL
;
4784 gfc_expr
*initializer
= NULL
;
4785 gfc_typebound_proc
* tb
;
4786 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4788 /* Parse interface (with brackets). */
4789 m
= match_procedure_interface (&proc_if
);
4793 /* Parse attributes. */
4794 tb
= XCNEW (gfc_typebound_proc
);
4795 tb
->where
= gfc_current_locus
;
4796 m
= match_binding_attributes (tb
, false, true);
4797 if (m
== MATCH_ERROR
)
4800 gfc_clear_attr (¤t_attr
);
4801 current_attr
.procedure
= 1;
4802 current_attr
.proc_pointer
= 1;
4803 current_attr
.access
= tb
->access
;
4804 current_attr
.flavor
= FL_PROCEDURE
;
4806 /* Match the colons (required). */
4807 if (gfc_match (" ::") != MATCH_YES
)
4809 gfc_error ("Expected '::' after binding-attributes at %C");
4813 /* Check for C450. */
4814 if (!tb
->nopass
&& proc_if
== NULL
)
4816 gfc_error("NOPASS or explicit interface required at %C");
4820 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure pointer "
4821 "component at %C") == FAILURE
)
4824 /* Match PPC names. */
4828 m
= gfc_match_name (name
);
4831 else if (m
== MATCH_ERROR
)
4834 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
4837 /* Add current_attr to the symbol attributes. */
4838 if (gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4841 if (gfc_add_external (&c
->attr
, NULL
) == FAILURE
)
4844 if (gfc_add_proc (&c
->attr
, name
, NULL
) == FAILURE
)
4849 /* Set interface. */
4850 if (proc_if
!= NULL
)
4852 c
->ts
.interface
= proc_if
;
4853 c
->attr
.untyped
= 1;
4854 c
->attr
.if_source
= IFSRC_IFBODY
;
4856 else if (ts
.type
!= BT_UNKNOWN
)
4859 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4860 c
->ts
.interface
->ts
= ts
;
4861 c
->ts
.interface
->attr
.function
= 1;
4862 c
->attr
.function
= c
->ts
.interface
->attr
.function
;
4863 c
->attr
.if_source
= IFSRC_UNKNOWN
;
4866 if (gfc_match (" =>") == MATCH_YES
)
4868 m
= match_pointer_init (&initializer
, 1);
4871 gfc_free_expr (initializer
);
4874 c
->initializer
= initializer
;
4877 if (gfc_match_eos () == MATCH_YES
)
4879 if (gfc_match_char (',') != MATCH_YES
)
4884 gfc_error ("Syntax error in procedure pointer component at %C");
4889 /* Match a PROCEDURE declaration inside an interface (R1206). */
4892 match_procedure_in_interface (void)
4896 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4898 if (current_interface
.type
== INTERFACE_NAMELESS
4899 || current_interface
.type
== INTERFACE_ABSTRACT
)
4901 gfc_error ("PROCEDURE at %C must be in a generic interface");
4907 m
= gfc_match_name (name
);
4910 else if (m
== MATCH_ERROR
)
4912 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4915 if (gfc_add_interface (sym
) == FAILURE
)
4918 if (gfc_match_eos () == MATCH_YES
)
4920 if (gfc_match_char (',') != MATCH_YES
)
4927 gfc_error ("Syntax error in PROCEDURE statement at %C");
4932 /* General matcher for PROCEDURE declarations. */
4934 static match
match_procedure_in_type (void);
4937 gfc_match_procedure (void)
4941 switch (gfc_current_state ())
4946 case COMP_SUBROUTINE
:
4948 m
= match_procedure_decl ();
4950 case COMP_INTERFACE
:
4951 m
= match_procedure_in_interface ();
4954 m
= match_ppc_decl ();
4956 case COMP_DERIVED_CONTAINS
:
4957 m
= match_procedure_in_type ();
4966 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4974 /* Warn if a matched procedure has the same name as an intrinsic; this is
4975 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4976 parser-state-stack to find out whether we're in a module. */
4979 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
4983 in_module
= (gfc_state_stack
->previous
4984 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
4986 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
4990 /* Match a function declaration. */
4993 gfc_match_function_decl (void)
4995 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4996 gfc_symbol
*sym
, *result
;
5000 match found_match
; /* Status returned by match func. */
5002 if (gfc_current_state () != COMP_NONE
5003 && gfc_current_state () != COMP_INTERFACE
5004 && gfc_current_state () != COMP_CONTAINS
)
5007 gfc_clear_ts (¤t_ts
);
5009 old_loc
= gfc_current_locus
;
5011 m
= gfc_match_prefix (¤t_ts
);
5014 gfc_current_locus
= old_loc
;
5018 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5020 gfc_current_locus
= old_loc
;
5023 if (get_proc_name (name
, &sym
, false))
5026 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5029 gfc_new_block
= sym
;
5031 m
= gfc_match_formal_arglist (sym
, 0, 0);
5034 gfc_error ("Expected formal argument list in function "
5035 "definition at %C");
5039 else if (m
== MATCH_ERROR
)
5044 /* According to the draft, the bind(c) and result clause can
5045 come in either order after the formal_arg_list (i.e., either
5046 can be first, both can exist together or by themselves or neither
5047 one). Therefore, the match_result can't match the end of the
5048 string, and check for the bind(c) or result clause in either order. */
5049 found_match
= gfc_match_eos ();
5051 /* Make sure that it isn't already declared as BIND(C). If it is, it
5052 must have been marked BIND(C) with a BIND(C) attribute and that is
5053 not allowed for procedures. */
5054 if (sym
->attr
.is_bind_c
== 1)
5056 sym
->attr
.is_bind_c
= 0;
5057 if (sym
->old_symbol
!= NULL
)
5058 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5059 "variables or common blocks",
5060 &(sym
->old_symbol
->declared_at
));
5062 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5063 "variables or common blocks", &gfc_current_locus
);
5066 if (found_match
!= MATCH_YES
)
5068 /* If we haven't found the end-of-statement, look for a suffix. */
5069 suffix_match
= gfc_match_suffix (sym
, &result
);
5070 if (suffix_match
== MATCH_YES
)
5071 /* Need to get the eos now. */
5072 found_match
= gfc_match_eos ();
5074 found_match
= suffix_match
;
5077 if(found_match
!= MATCH_YES
)
5081 /* Make changes to the symbol. */
5084 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5087 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
5088 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5091 /* Delay matching the function characteristics until after the
5092 specification block by signalling kind=-1. */
5093 sym
->declared_at
= old_loc
;
5094 if (current_ts
.type
!= BT_UNKNOWN
)
5095 current_ts
.kind
= -1;
5097 current_ts
.kind
= 0;
5101 if (current_ts
.type
!= BT_UNKNOWN
5102 && gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
5108 if (current_ts
.type
!= BT_UNKNOWN
5109 && gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
)
5112 sym
->result
= result
;
5115 /* Warn if this procedure has the same name as an intrinsic. */
5116 warn_intrinsic_shadow (sym
, true);
5122 gfc_current_locus
= old_loc
;
5127 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5128 pass the name of the entry, rather than the gfc_current_block name, and
5129 to return false upon finding an existing global entry. */
5132 add_global_entry (const char *name
, int sub
)
5135 enum gfc_symbol_type type
;
5137 s
= gfc_get_gsymbol(name
);
5138 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5141 || (s
->type
!= GSYM_UNKNOWN
5142 && s
->type
!= type
))
5143 gfc_global_used(s
, NULL
);
5147 s
->where
= gfc_current_locus
;
5149 s
->ns
= gfc_current_ns
;
5156 /* Match an ENTRY statement. */
5159 gfc_match_entry (void)
5164 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5165 gfc_compile_state state
;
5169 bool module_procedure
;
5173 m
= gfc_match_name (name
);
5177 if (gfc_notify_std (GFC_STD_F2008_OBS
, "Fortran 2008 obsolescent feature: "
5178 "ENTRY statement at %C") == FAILURE
)
5181 state
= gfc_current_state ();
5182 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5187 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5190 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5192 case COMP_BLOCK_DATA
:
5193 gfc_error ("ENTRY statement at %C cannot appear within "
5196 case COMP_INTERFACE
:
5197 gfc_error ("ENTRY statement at %C cannot appear within "
5201 gfc_error ("ENTRY statement at %C cannot appear within "
5202 "a DERIVED TYPE block");
5205 gfc_error ("ENTRY statement at %C cannot appear within "
5206 "an IF-THEN block");
5209 gfc_error ("ENTRY statement at %C cannot appear within "
5213 gfc_error ("ENTRY statement at %C cannot appear within "
5217 gfc_error ("ENTRY statement at %C cannot appear within "
5221 gfc_error ("ENTRY statement at %C cannot appear within "
5225 gfc_error ("ENTRY statement at %C cannot appear within "
5226 "a contained subprogram");
5229 gfc_internal_error ("gfc_match_entry(): Bad state");
5234 module_procedure
= gfc_current_ns
->parent
!= NULL
5235 && gfc_current_ns
->parent
->proc_name
5236 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5239 if (gfc_current_ns
->parent
!= NULL
5240 && gfc_current_ns
->parent
->proc_name
5241 && !module_procedure
)
5243 gfc_error("ENTRY statement at %C cannot appear in a "
5244 "contained procedure");
5248 /* Module function entries need special care in get_proc_name
5249 because previous references within the function will have
5250 created symbols attached to the current namespace. */
5251 if (get_proc_name (name
, &entry
,
5252 gfc_current_ns
->parent
!= NULL
5253 && module_procedure
))
5256 proc
= gfc_current_block ();
5258 /* Make sure that it isn't already declared as BIND(C). If it is, it
5259 must have been marked BIND(C) with a BIND(C) attribute and that is
5260 not allowed for procedures. */
5261 if (entry
->attr
.is_bind_c
== 1)
5263 entry
->attr
.is_bind_c
= 0;
5264 if (entry
->old_symbol
!= NULL
)
5265 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5266 "variables or common blocks",
5267 &(entry
->old_symbol
->declared_at
));
5269 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5270 "variables or common blocks", &gfc_current_locus
);
5273 /* Check what next non-whitespace character is so we can tell if there
5274 is the required parens if we have a BIND(C). */
5275 gfc_gobble_whitespace ();
5276 peek_char
= gfc_peek_ascii_char ();
5278 if (state
== COMP_SUBROUTINE
)
5280 /* An entry in a subroutine. */
5281 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
5284 m
= gfc_match_formal_arglist (entry
, 0, 1);
5288 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5289 never be an internal procedure. */
5290 is_bind_c
= gfc_match_bind_c (entry
, true);
5291 if (is_bind_c
== MATCH_ERROR
)
5293 if (is_bind_c
== MATCH_YES
)
5295 if (peek_char
!= '(')
5297 gfc_error ("Missing required parentheses before BIND(C) at %C");
5300 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
5305 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5306 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5311 /* An entry in a function.
5312 We need to take special care because writing
5317 ENTRY f() RESULT (r)
5319 ENTRY f RESULT (r). */
5320 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
5323 old_loc
= gfc_current_locus
;
5324 if (gfc_match_eos () == MATCH_YES
)
5326 gfc_current_locus
= old_loc
;
5327 /* Match the empty argument list, and add the interface to
5329 m
= gfc_match_formal_arglist (entry
, 0, 1);
5332 m
= gfc_match_formal_arglist (entry
, 0, 0);
5339 if (gfc_match_eos () == MATCH_YES
)
5341 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5342 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5345 entry
->result
= entry
;
5349 m
= gfc_match_suffix (entry
, &result
);
5351 gfc_syntax_error (ST_ENTRY
);
5357 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
5358 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
5359 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
5362 entry
->result
= result
;
5366 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5367 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5369 entry
->result
= entry
;
5374 if (gfc_match_eos () != MATCH_YES
)
5376 gfc_syntax_error (ST_ENTRY
);
5380 entry
->attr
.recursive
= proc
->attr
.recursive
;
5381 entry
->attr
.elemental
= proc
->attr
.elemental
;
5382 entry
->attr
.pure
= proc
->attr
.pure
;
5384 el
= gfc_get_entry_list ();
5386 el
->next
= gfc_current_ns
->entries
;
5387 gfc_current_ns
->entries
= el
;
5389 el
->id
= el
->next
->id
+ 1;
5393 new_st
.op
= EXEC_ENTRY
;
5394 new_st
.ext
.entry
= el
;
5400 /* Match a subroutine statement, including optional prefixes. */
5403 gfc_match_subroutine (void)
5405 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5410 bool allow_binding_name
;
5412 if (gfc_current_state () != COMP_NONE
5413 && gfc_current_state () != COMP_INTERFACE
5414 && gfc_current_state () != COMP_CONTAINS
)
5417 m
= gfc_match_prefix (NULL
);
5421 m
= gfc_match ("subroutine% %n", name
);
5425 if (get_proc_name (name
, &sym
, false))
5428 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5429 the symbol existed before. */
5430 sym
->declared_at
= gfc_current_locus
;
5432 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5435 gfc_new_block
= sym
;
5437 /* Check what next non-whitespace character is so we can tell if there
5438 is the required parens if we have a BIND(C). */
5439 gfc_gobble_whitespace ();
5440 peek_char
= gfc_peek_ascii_char ();
5442 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5445 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5448 /* Make sure that it isn't already declared as BIND(C). If it is, it
5449 must have been marked BIND(C) with a BIND(C) attribute and that is
5450 not allowed for procedures. */
5451 if (sym
->attr
.is_bind_c
== 1)
5453 sym
->attr
.is_bind_c
= 0;
5454 if (sym
->old_symbol
!= NULL
)
5455 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5456 "variables or common blocks",
5457 &(sym
->old_symbol
->declared_at
));
5459 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5460 "variables or common blocks", &gfc_current_locus
);
5463 /* C binding names are not allowed for internal procedures. */
5464 if (gfc_current_state () == COMP_CONTAINS
5465 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5466 allow_binding_name
= false;
5468 allow_binding_name
= true;
5470 /* Here, we are just checking if it has the bind(c) attribute, and if
5471 so, then we need to make sure it's all correct. If it doesn't,
5472 we still need to continue matching the rest of the subroutine line. */
5473 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5474 if (is_bind_c
== MATCH_ERROR
)
5476 /* There was an attempt at the bind(c), but it was wrong. An
5477 error message should have been printed w/in the gfc_match_bind_c
5478 so here we'll just return the MATCH_ERROR. */
5482 if (is_bind_c
== MATCH_YES
)
5484 /* The following is allowed in the Fortran 2008 draft. */
5485 if (gfc_current_state () == COMP_CONTAINS
5486 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5487 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
5488 "at %L may not be specified for an internal "
5489 "procedure", &gfc_current_locus
)
5493 if (peek_char
!= '(')
5495 gfc_error ("Missing required parentheses before BIND(C) at %C");
5498 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
5503 if (gfc_match_eos () != MATCH_YES
)
5505 gfc_syntax_error (ST_SUBROUTINE
);
5509 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5512 /* Warn if it has the same name as an intrinsic. */
5513 warn_intrinsic_shadow (sym
, false);
5519 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5520 given, and set the binding label in either the given symbol (if not
5521 NULL), or in the current_ts. The symbol may be NULL because we may
5522 encounter the BIND(C) before the declaration itself. Return
5523 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5524 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5525 or MATCH_YES if the specifier was correct and the binding label and
5526 bind(c) fields were set correctly for the given symbol or the
5527 current_ts. If allow_binding_name is false, no binding name may be
5531 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5533 /* binding label, if exists */
5534 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
5538 /* Initialize the flag that specifies whether we encountered a NAME=
5539 specifier or not. */
5540 has_name_equals
= 0;
5542 /* Init the first char to nil so we can catch if we don't have
5543 the label (name attr) or the symbol name yet. */
5544 binding_label
[0] = '\0';
5546 /* This much we have to be able to match, in this order, if
5547 there is a bind(c) label. */
5548 if (gfc_match (" bind ( c ") != MATCH_YES
)
5551 /* Now see if there is a binding label, or if we've reached the
5552 end of the bind(c) attribute without one. */
5553 if (gfc_match_char (',') == MATCH_YES
)
5555 if (gfc_match (" name = ") != MATCH_YES
)
5557 gfc_error ("Syntax error in NAME= specifier for binding label "
5559 /* should give an error message here */
5563 has_name_equals
= 1;
5565 /* Get the opening quote. */
5566 double_quote
= MATCH_YES
;
5567 single_quote
= MATCH_YES
;
5568 double_quote
= gfc_match_char ('"');
5569 if (double_quote
!= MATCH_YES
)
5570 single_quote
= gfc_match_char ('\'');
5571 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5573 gfc_error ("Syntax error in NAME= specifier for binding label "
5578 /* Grab the binding label, using functions that will not lower
5579 case the names automatically. */
5580 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
5583 /* Get the closing quotation. */
5584 if (double_quote
== MATCH_YES
)
5586 if (gfc_match_char ('"') != MATCH_YES
)
5588 gfc_error ("Missing closing quote '\"' for binding label at %C");
5589 /* User started string with '"' so looked to match it. */
5595 if (gfc_match_char ('\'') != MATCH_YES
)
5597 gfc_error ("Missing closing quote '\'' for binding label at %C");
5598 /* User started string with "'" char. */
5604 /* Get the required right paren. */
5605 if (gfc_match_char (')') != MATCH_YES
)
5607 gfc_error ("Missing closing paren for binding label at %C");
5611 if (has_name_equals
&& !allow_binding_name
)
5613 gfc_error ("No binding name is allowed in BIND(C) at %C");
5617 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5619 gfc_error ("For dummy procedure %s, no binding name is "
5620 "allowed in BIND(C) at %C", sym
->name
);
5625 /* Save the binding label to the symbol. If sym is null, we're
5626 probably matching the typespec attributes of a declaration and
5627 haven't gotten the name yet, and therefore, no symbol yet. */
5628 if (binding_label
[0] != '\0')
5632 strcpy (sym
->binding_label
, binding_label
);
5635 strcpy (curr_binding_label
, binding_label
);
5637 else if (allow_binding_name
)
5639 /* No binding label, but if symbol isn't null, we
5640 can set the label for it here.
5641 If name="" or allow_binding_name is false, no C binding name is
5643 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5644 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
5647 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5648 && current_interface
.type
== INTERFACE_ABSTRACT
)
5650 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5658 /* Return nonzero if we're currently compiling a contained procedure. */
5661 contained_procedure (void)
5663 gfc_state_data
*s
= gfc_state_stack
;
5665 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5666 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5672 /* Set the kind of each enumerator. The kind is selected such that it is
5673 interoperable with the corresponding C enumeration type, making
5674 sure that -fshort-enums is honored. */
5679 enumerator_history
*current_history
= NULL
;
5683 if (max_enum
== NULL
|| enum_history
== NULL
)
5686 if (!flag_short_enums
)
5692 kind
= gfc_integer_kinds
[i
++].kind
;
5694 while (kind
< gfc_c_int_kind
5695 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5698 current_history
= enum_history
;
5699 while (current_history
!= NULL
)
5701 current_history
->sym
->ts
.kind
= kind
;
5702 current_history
= current_history
->next
;
5707 /* Match any of the various end-block statements. Returns the type of
5708 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5709 and END BLOCK statements cannot be replaced by a single END statement. */
5712 gfc_match_end (gfc_statement
*st
)
5714 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5715 gfc_compile_state state
;
5717 const char *block_name
;
5722 old_loc
= gfc_current_locus
;
5723 if (gfc_match ("end") != MATCH_YES
)
5726 state
= gfc_current_state ();
5727 block_name
= gfc_current_block () == NULL
5728 ? NULL
: gfc_current_block ()->name
;
5732 case COMP_ASSOCIATE
:
5734 if (!strcmp (block_name
, "block@"))
5739 case COMP_DERIVED_CONTAINS
:
5740 state
= gfc_state_stack
->previous
->state
;
5741 block_name
= gfc_state_stack
->previous
->sym
== NULL
5742 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5753 *st
= ST_END_PROGRAM
;
5754 target
= " program";
5758 case COMP_SUBROUTINE
:
5759 *st
= ST_END_SUBROUTINE
;
5760 target
= " subroutine";
5761 eos_ok
= !contained_procedure ();
5765 *st
= ST_END_FUNCTION
;
5766 target
= " function";
5767 eos_ok
= !contained_procedure ();
5770 case COMP_BLOCK_DATA
:
5771 *st
= ST_END_BLOCK_DATA
;
5772 target
= " block data";
5777 *st
= ST_END_MODULE
;
5782 case COMP_INTERFACE
:
5783 *st
= ST_END_INTERFACE
;
5784 target
= " interface";
5789 case COMP_DERIVED_CONTAINS
:
5795 case COMP_ASSOCIATE
:
5796 *st
= ST_END_ASSOCIATE
;
5797 target
= " associate";
5820 *st
= ST_END_CRITICAL
;
5821 target
= " critical";
5826 case COMP_SELECT_TYPE
:
5827 *st
= ST_END_SELECT
;
5833 *st
= ST_END_FORALL
;
5848 last_initializer
= NULL
;
5850 gfc_free_enum_history ();
5854 gfc_error ("Unexpected END statement at %C");
5858 if (gfc_match_eos () == MATCH_YES
)
5860 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
5862 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: END statement "
5863 "instead of %s statement at %L",
5864 gfc_ascii_statement (*st
), &old_loc
) == FAILURE
)
5869 /* We would have required END [something]. */
5870 gfc_error ("%s statement expected at %L",
5871 gfc_ascii_statement (*st
), &old_loc
);
5878 /* Verify that we've got the sort of end-block that we're expecting. */
5879 if (gfc_match (target
) != MATCH_YES
)
5881 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
5885 /* If we're at the end, make sure a block name wasn't required. */
5886 if (gfc_match_eos () == MATCH_YES
)
5889 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
5890 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
5891 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
5897 gfc_error ("Expected block name of '%s' in %s statement at %C",
5898 block_name
, gfc_ascii_statement (*st
));
5903 /* END INTERFACE has a special handler for its several possible endings. */
5904 if (*st
== ST_END_INTERFACE
)
5905 return gfc_match_end_interface ();
5907 /* We haven't hit the end of statement, so what is left must be an
5909 m
= gfc_match_space ();
5911 m
= gfc_match_name (name
);
5914 gfc_error ("Expected terminating name at %C");
5918 if (block_name
== NULL
)
5921 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
5923 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
5924 gfc_ascii_statement (*st
));
5927 /* Procedure pointer as function result. */
5928 else if (strcmp (block_name
, "ppr@") == 0
5929 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
5931 gfc_error ("Expected label '%s' for %s statement at %C",
5932 gfc_current_block ()->ns
->proc_name
->name
,
5933 gfc_ascii_statement (*st
));
5937 if (gfc_match_eos () == MATCH_YES
)
5941 gfc_syntax_error (*st
);
5944 gfc_current_locus
= old_loc
;
5950 /***************** Attribute declaration statements ****************/
5952 /* Set the attribute of a single variable. */
5957 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5965 m
= gfc_match_name (name
);
5969 if (find_special (name
, &sym
, false))
5972 var_locus
= gfc_current_locus
;
5974 /* Deal with possible array specification for certain attributes. */
5975 if (current_attr
.dimension
5976 || current_attr
.codimension
5977 || current_attr
.allocatable
5978 || current_attr
.pointer
5979 || current_attr
.target
)
5981 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
5982 !current_attr
.dimension
5983 && !current_attr
.pointer
5984 && !current_attr
.target
);
5985 if (m
== MATCH_ERROR
)
5988 if (current_attr
.dimension
&& m
== MATCH_NO
)
5990 gfc_error ("Missing array specification at %L in DIMENSION "
5991 "statement", &var_locus
);
5996 if (current_attr
.dimension
&& sym
->value
)
5998 gfc_error ("Dimensions specified for %s at %L after its "
5999 "initialisation", sym
->name
, &var_locus
);
6004 if (current_attr
.codimension
&& m
== MATCH_NO
)
6006 gfc_error ("Missing array specification at %L in CODIMENSION "
6007 "statement", &var_locus
);
6012 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6013 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6015 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6021 /* Update symbol table. DIMENSION attribute is set in
6022 gfc_set_array_spec(). For CLASS variables, this must be applied
6023 to the first component, or '_data' field. */
6024 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6026 if (gfc_copy_attr (&CLASS_DATA (sym
)->attr
, ¤t_attr
, &var_locus
)
6035 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6036 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
6043 if (sym
->ts
.type
== BT_CLASS
&& !sym
->attr
.class_ok
6044 && (sym
->attr
.class_ok
= sym
->attr
.class_ok
|| current_attr
.allocatable
6045 || current_attr
.pointer
))
6046 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
6048 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
6054 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6056 /* Fix the array spec. */
6057 m
= gfc_mod_pointee_as (sym
->as
);
6058 if (m
== MATCH_ERROR
)
6062 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
6068 if ((current_attr
.external
|| current_attr
.intrinsic
)
6069 && sym
->attr
.flavor
!= FL_PROCEDURE
6070 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
6076 add_hidden_procptr_result (sym
);
6081 gfc_free_array_spec (as
);
6086 /* Generic attribute declaration subroutine. Used for attributes that
6087 just have a list of names. */
6094 /* Gobble the optional double colon, by simply ignoring the result
6104 if (gfc_match_eos () == MATCH_YES
)
6110 if (gfc_match_char (',') != MATCH_YES
)
6112 gfc_error ("Unexpected character in variable list at %C");
6122 /* This routine matches Cray Pointer declarations of the form:
6123 pointer ( <pointer>, <pointee> )
6125 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6126 The pointer, if already declared, should be an integer. Otherwise, we
6127 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6128 be either a scalar, or an array declaration. No space is allocated for
6129 the pointee. For the statement
6130 pointer (ipt, ar(10))
6131 any subsequent uses of ar will be translated (in C-notation) as
6132 ar(i) => ((<type> *) ipt)(i)
6133 After gimplification, pointee variable will disappear in the code. */
6136 cray_pointer_decl (void)
6139 gfc_array_spec
*as
= NULL
;
6140 gfc_symbol
*cptr
; /* Pointer symbol. */
6141 gfc_symbol
*cpte
; /* Pointee symbol. */
6147 if (gfc_match_char ('(') != MATCH_YES
)
6149 gfc_error ("Expected '(' at %C");
6153 /* Match pointer. */
6154 var_locus
= gfc_current_locus
;
6155 gfc_clear_attr (¤t_attr
);
6156 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6157 current_ts
.type
= BT_INTEGER
;
6158 current_ts
.kind
= gfc_index_integer_kind
;
6160 m
= gfc_match_symbol (&cptr
, 0);
6163 gfc_error ("Expected variable name at %C");
6167 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
6170 gfc_set_sym_referenced (cptr
);
6172 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6174 cptr
->ts
.type
= BT_INTEGER
;
6175 cptr
->ts
.kind
= gfc_index_integer_kind
;
6177 else if (cptr
->ts
.type
!= BT_INTEGER
)
6179 gfc_error ("Cray pointer at %C must be an integer");
6182 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6183 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6184 " memory addresses require %d bytes",
6185 cptr
->ts
.kind
, gfc_index_integer_kind
);
6187 if (gfc_match_char (',') != MATCH_YES
)
6189 gfc_error ("Expected \",\" at %C");
6193 /* Match Pointee. */
6194 var_locus
= gfc_current_locus
;
6195 gfc_clear_attr (¤t_attr
);
6196 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6197 current_ts
.type
= BT_UNKNOWN
;
6198 current_ts
.kind
= 0;
6200 m
= gfc_match_symbol (&cpte
, 0);
6203 gfc_error ("Expected variable name at %C");
6207 /* Check for an optional array spec. */
6208 m
= gfc_match_array_spec (&as
, true, false);
6209 if (m
== MATCH_ERROR
)
6211 gfc_free_array_spec (as
);
6214 else if (m
== MATCH_NO
)
6216 gfc_free_array_spec (as
);
6220 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
6223 gfc_set_sym_referenced (cpte
);
6225 if (cpte
->as
== NULL
)
6227 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
6228 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6230 else if (as
!= NULL
)
6232 gfc_error ("Duplicate array spec for Cray pointee at %C");
6233 gfc_free_array_spec (as
);
6239 if (cpte
->as
!= NULL
)
6241 /* Fix array spec. */
6242 m
= gfc_mod_pointee_as (cpte
->as
);
6243 if (m
== MATCH_ERROR
)
6247 /* Point the Pointee at the Pointer. */
6248 cpte
->cp_pointer
= cptr
;
6250 if (gfc_match_char (')') != MATCH_YES
)
6252 gfc_error ("Expected \")\" at %C");
6255 m
= gfc_match_char (',');
6257 done
= true; /* Stop searching for more declarations. */
6261 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6262 || gfc_match_eos () != MATCH_YES
)
6264 gfc_error ("Expected \",\" or end of statement at %C");
6272 gfc_match_external (void)
6275 gfc_clear_attr (¤t_attr
);
6276 current_attr
.external
= 1;
6278 return attr_decl ();
6283 gfc_match_intent (void)
6287 /* This is not allowed within a BLOCK construct! */
6288 if (gfc_current_state () == COMP_BLOCK
)
6290 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6294 intent
= match_intent_spec ();
6295 if (intent
== INTENT_UNKNOWN
)
6298 gfc_clear_attr (¤t_attr
);
6299 current_attr
.intent
= intent
;
6301 return attr_decl ();
6306 gfc_match_intrinsic (void)
6309 gfc_clear_attr (¤t_attr
);
6310 current_attr
.intrinsic
= 1;
6312 return attr_decl ();
6317 gfc_match_optional (void)
6319 /* This is not allowed within a BLOCK construct! */
6320 if (gfc_current_state () == COMP_BLOCK
)
6322 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6326 gfc_clear_attr (¤t_attr
);
6327 current_attr
.optional
= 1;
6329 return attr_decl ();
6334 gfc_match_pointer (void)
6336 gfc_gobble_whitespace ();
6337 if (gfc_peek_ascii_char () == '(')
6339 if (!gfc_option
.flag_cray_pointer
)
6341 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6345 return cray_pointer_decl ();
6349 gfc_clear_attr (¤t_attr
);
6350 current_attr
.pointer
= 1;
6352 return attr_decl ();
6358 gfc_match_allocatable (void)
6360 gfc_clear_attr (¤t_attr
);
6361 current_attr
.allocatable
= 1;
6363 return attr_decl ();
6368 gfc_match_codimension (void)
6370 gfc_clear_attr (¤t_attr
);
6371 current_attr
.codimension
= 1;
6373 return attr_decl ();
6378 gfc_match_contiguous (void)
6380 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTIGUOUS statement at %C")
6384 gfc_clear_attr (¤t_attr
);
6385 current_attr
.contiguous
= 1;
6387 return attr_decl ();
6392 gfc_match_dimension (void)
6394 gfc_clear_attr (¤t_attr
);
6395 current_attr
.dimension
= 1;
6397 return attr_decl ();
6402 gfc_match_target (void)
6404 gfc_clear_attr (¤t_attr
);
6405 current_attr
.target
= 1;
6407 return attr_decl ();
6411 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6415 access_attr_decl (gfc_statement st
)
6417 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6418 interface_type type
;
6421 gfc_intrinsic_op op
;
6424 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6429 m
= gfc_match_generic_spec (&type
, name
, &op
);
6432 if (m
== MATCH_ERROR
)
6437 case INTERFACE_NAMELESS
:
6438 case INTERFACE_ABSTRACT
:
6441 case INTERFACE_GENERIC
:
6442 if (gfc_get_symbol (name
, NULL
, &sym
))
6445 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
6446 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6447 sym
->name
, NULL
) == FAILURE
)
6452 case INTERFACE_INTRINSIC_OP
:
6453 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6455 gfc_current_ns
->operator_access
[op
] =
6456 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6460 gfc_error ("Access specification of the %s operator at %C has "
6461 "already been specified", gfc_op2string (op
));
6467 case INTERFACE_USER_OP
:
6468 uop
= gfc_get_uop (name
);
6470 if (uop
->access
== ACCESS_UNKNOWN
)
6472 uop
->access
= (st
== ST_PUBLIC
)
6473 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6477 gfc_error ("Access specification of the .%s. operator at %C "
6478 "has already been specified", sym
->name
);
6485 if (gfc_match_char (',') == MATCH_NO
)
6489 if (gfc_match_eos () != MATCH_YES
)
6494 gfc_syntax_error (st
);
6502 gfc_match_protected (void)
6507 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6509 gfc_error ("PROTECTED at %C only allowed in specification "
6510 "part of a module");
6515 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
6519 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6524 if (gfc_match_eos () == MATCH_YES
)
6529 m
= gfc_match_symbol (&sym
, 0);
6533 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6546 if (gfc_match_eos () == MATCH_YES
)
6548 if (gfc_match_char (',') != MATCH_YES
)
6555 gfc_error ("Syntax error in PROTECTED statement at %C");
6560 /* The PRIVATE statement is a bit weird in that it can be an attribute
6561 declaration, but also works as a standalone statement inside of a
6562 type declaration or a module. */
6565 gfc_match_private (gfc_statement
*st
)
6568 if (gfc_match ("private") != MATCH_YES
)
6571 if (gfc_current_state () != COMP_MODULE
6572 && !(gfc_current_state () == COMP_DERIVED
6573 && gfc_state_stack
->previous
6574 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6575 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6576 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6577 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6579 gfc_error ("PRIVATE statement at %C is only allowed in the "
6580 "specification part of a module");
6584 if (gfc_current_state () == COMP_DERIVED
)
6586 if (gfc_match_eos () == MATCH_YES
)
6592 gfc_syntax_error (ST_PRIVATE
);
6596 if (gfc_match_eos () == MATCH_YES
)
6603 return access_attr_decl (ST_PRIVATE
);
6608 gfc_match_public (gfc_statement
*st
)
6611 if (gfc_match ("public") != MATCH_YES
)
6614 if (gfc_current_state () != COMP_MODULE
)
6616 gfc_error ("PUBLIC statement at %C is only allowed in the "
6617 "specification part of a module");
6621 if (gfc_match_eos () == MATCH_YES
)
6628 return access_attr_decl (ST_PUBLIC
);
6632 /* Workhorse for gfc_match_parameter. */
6642 m
= gfc_match_symbol (&sym
, 0);
6644 gfc_error ("Expected variable name at %C in PARAMETER statement");
6649 if (gfc_match_char ('=') == MATCH_NO
)
6651 gfc_error ("Expected = sign in PARAMETER statement at %C");
6655 m
= gfc_match_init_expr (&init
);
6657 gfc_error ("Expected expression at %C in PARAMETER statement");
6661 if (sym
->ts
.type
== BT_UNKNOWN
6662 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6668 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6669 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6677 gfc_error ("Initializing already initialized variable at %C");
6682 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6683 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6686 gfc_free_expr (init
);
6691 /* Match a parameter statement, with the weird syntax that these have. */
6694 gfc_match_parameter (void)
6698 if (gfc_match_char ('(') == MATCH_NO
)
6707 if (gfc_match (" )%t") == MATCH_YES
)
6710 if (gfc_match_char (',') != MATCH_YES
)
6712 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6722 /* Save statements have a special syntax. */
6725 gfc_match_save (void)
6727 char n
[GFC_MAX_SYMBOL_LEN
+1];
6732 if (gfc_match_eos () == MATCH_YES
)
6734 if (gfc_current_ns
->seen_save
)
6736 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6737 "follows previous SAVE statement")
6742 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6746 if (gfc_current_ns
->save_all
)
6748 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6749 "blanket SAVE statement")
6758 m
= gfc_match_symbol (&sym
, 0);
6762 if (gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
6763 &gfc_current_locus
) == FAILURE
)
6774 m
= gfc_match (" / %n /", &n
);
6775 if (m
== MATCH_ERROR
)
6780 c
= gfc_get_common (n
, 0);
6783 gfc_current_ns
->seen_save
= 1;
6786 if (gfc_match_eos () == MATCH_YES
)
6788 if (gfc_match_char (',') != MATCH_YES
)
6795 gfc_error ("Syntax error in SAVE statement at %C");
6801 gfc_match_value (void)
6806 /* This is not allowed within a BLOCK construct! */
6807 if (gfc_current_state () == COMP_BLOCK
)
6809 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6813 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
6817 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6822 if (gfc_match_eos () == MATCH_YES
)
6827 m
= gfc_match_symbol (&sym
, 0);
6831 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6844 if (gfc_match_eos () == MATCH_YES
)
6846 if (gfc_match_char (',') != MATCH_YES
)
6853 gfc_error ("Syntax error in VALUE statement at %C");
6859 gfc_match_volatile (void)
6864 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
6868 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6873 if (gfc_match_eos () == MATCH_YES
)
6878 /* VOLATILE is special because it can be added to host-associated
6879 symbols locally. Except for coarrays. */
6880 m
= gfc_match_symbol (&sym
, 1);
6884 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6885 for variable in a BLOCK which is defined outside of the BLOCK. */
6886 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
6888 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6889 "%C, which is use-/host-associated", sym
->name
);
6892 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6905 if (gfc_match_eos () == MATCH_YES
)
6907 if (gfc_match_char (',') != MATCH_YES
)
6914 gfc_error ("Syntax error in VOLATILE statement at %C");
6920 gfc_match_asynchronous (void)
6925 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASYNCHRONOUS statement at %C")
6929 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6934 if (gfc_match_eos () == MATCH_YES
)
6939 /* ASYNCHRONOUS is special because it can be added to host-associated
6941 m
= gfc_match_symbol (&sym
, 1);
6945 if (gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6958 if (gfc_match_eos () == MATCH_YES
)
6960 if (gfc_match_char (',') != MATCH_YES
)
6967 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6972 /* Match a module procedure statement. Note that we have to modify
6973 symbols in the parent's namespace because the current one was there
6974 to receive symbols that are in an interface's formal argument list. */
6977 gfc_match_modproc (void)
6979 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6982 gfc_namespace
*module_ns
;
6983 gfc_interface
*old_interface_head
, *interface
;
6985 if (gfc_state_stack
->state
!= COMP_INTERFACE
6986 || gfc_state_stack
->previous
== NULL
6987 || current_interface
.type
== INTERFACE_NAMELESS
6988 || current_interface
.type
== INTERFACE_ABSTRACT
)
6990 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6995 module_ns
= gfc_current_ns
->parent
;
6996 for (; module_ns
; module_ns
= module_ns
->parent
)
6997 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
6998 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
6999 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7000 && !module_ns
->proc_name
->attr
.contained
))
7003 if (module_ns
== NULL
)
7006 /* Store the current state of the interface. We will need it if we
7007 end up with a syntax error and need to recover. */
7008 old_interface_head
= gfc_current_interface_head ();
7012 locus old_locus
= gfc_current_locus
;
7015 m
= gfc_match_name (name
);
7021 /* Check for syntax error before starting to add symbols to the
7022 current namespace. */
7023 if (gfc_match_eos () == MATCH_YES
)
7025 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7028 /* Now we're sure the syntax is valid, we process this item
7030 if (gfc_get_symbol (name
, module_ns
, &sym
))
7033 if (sym
->attr
.intrinsic
)
7035 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7036 "PROCEDURE", &old_locus
);
7040 if (sym
->attr
.proc
!= PROC_MODULE
7041 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7042 sym
->name
, NULL
) == FAILURE
)
7045 if (gfc_add_interface (sym
) == FAILURE
)
7048 sym
->attr
.mod_proc
= 1;
7049 sym
->declared_at
= old_locus
;
7058 /* Restore the previous state of the interface. */
7059 interface
= gfc_current_interface_head ();
7060 gfc_set_current_interface_head (old_interface_head
);
7062 /* Free the new interfaces. */
7063 while (interface
!= old_interface_head
)
7065 gfc_interface
*i
= interface
->next
;
7066 gfc_free (interface
);
7070 /* And issue a syntax error. */
7071 gfc_syntax_error (ST_MODULE_PROC
);
7076 /* Check a derived type that is being extended. */
7078 check_extended_derived_type (char *name
)
7080 gfc_symbol
*extended
;
7082 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7084 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7090 gfc_error ("No such symbol in TYPE definition at %C");
7094 if (extended
->attr
.flavor
!= FL_DERIVED
)
7096 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7097 "derived type", name
);
7101 if (extended
->attr
.is_bind_c
)
7103 gfc_error ("'%s' cannot be extended at %C because it "
7104 "is BIND(C)", extended
->name
);
7108 if (extended
->attr
.sequence
)
7110 gfc_error ("'%s' cannot be extended at %C because it "
7111 "is a SEQUENCE type", extended
->name
);
7119 /* Match the optional attribute specifiers for a type declaration.
7120 Return MATCH_ERROR if an error is encountered in one of the handled
7121 attributes (public, private, bind(c)), MATCH_NO if what's found is
7122 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7123 checking on attribute conflicts needs to be done. */
7126 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7128 /* See if the derived type is marked as private. */
7129 if (gfc_match (" , private") == MATCH_YES
)
7131 if (gfc_current_state () != COMP_MODULE
)
7133 gfc_error ("Derived type at %C can only be PRIVATE in the "
7134 "specification part of a module");
7138 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
7141 else if (gfc_match (" , public") == MATCH_YES
)
7143 if (gfc_current_state () != COMP_MODULE
)
7145 gfc_error ("Derived type at %C can only be PUBLIC in the "
7146 "specification part of a module");
7150 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
7153 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7155 /* If the type is defined to be bind(c) it then needs to make
7156 sure that all fields are interoperable. This will
7157 need to be a semantic check on the finished derived type.
7158 See 15.2.3 (lines 9-12) of F2003 draft. */
7159 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
7162 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7164 else if (gfc_match (" , abstract") == MATCH_YES
)
7166 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
7170 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
7173 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
7175 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
7181 /* If we get here, something matched. */
7186 /* Assign a hash value for a derived type. The algorithm is that of
7187 SDBM. The hashed string is '[module_name #] derived_name'. */
7189 hash_value (gfc_symbol
*sym
)
7191 unsigned int hash
= 0;
7195 /* Hash of the module or procedure name. */
7196 if (sym
->module
!= NULL
)
7198 else if (sym
->ns
&& sym
->ns
->proc_name
7199 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
7200 c
= sym
->ns
->proc_name
->name
;
7207 for (i
= 0; i
< len
; i
++, c
++)
7208 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ (*c
);
7210 /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'. */
7211 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ '#';
7214 /* Hash of the derived type name. */
7215 len
= strlen (sym
->name
);
7217 for (i
= 0; i
< len
; i
++, c
++)
7218 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ (*c
);
7220 /* Return the hash but take the modulus for the sake of module read,
7221 even though this slightly increases the chance of collision. */
7222 return (hash
% 100000000);
7226 /* Match the beginning of a derived type declaration. If a type name
7227 was the result of a function, then it is possible to have a symbol
7228 already to be known as a derived type yet have no components. */
7231 gfc_match_derived_decl (void)
7233 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7234 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7235 symbol_attribute attr
;
7237 gfc_symbol
*extended
;
7239 match is_type_attr_spec
= MATCH_NO
;
7240 bool seen_attr
= false;
7242 if (gfc_current_state () == COMP_DERIVED
)
7247 gfc_clear_attr (&attr
);
7252 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7253 if (is_type_attr_spec
== MATCH_ERROR
)
7255 if (is_type_attr_spec
== MATCH_YES
)
7257 } while (is_type_attr_spec
== MATCH_YES
);
7259 /* Deal with derived type extensions. The extension attribute has
7260 been added to 'attr' but now the parent type must be found and
7263 extended
= check_extended_derived_type (parent
);
7265 if (parent
[0] && !extended
)
7268 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7270 gfc_error ("Expected :: in TYPE definition at %C");
7274 m
= gfc_match (" %n%t", name
);
7278 /* Make sure the name is not the name of an intrinsic type. */
7279 if (gfc_is_intrinsic_typename (name
))
7281 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7286 if (gfc_get_symbol (name
, NULL
, &sym
))
7289 if (sym
->ts
.type
!= BT_UNKNOWN
)
7291 gfc_error ("Derived type name '%s' at %C already has a basic type "
7292 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
7296 /* The symbol may already have the derived attribute without the
7297 components. The ways this can happen is via a function
7298 definition, an INTRINSIC statement or a subtype in another
7299 derived type that is a pointer. The first part of the AND clause
7300 is true if the symbol is not the return value of a function. */
7301 if (sym
->attr
.flavor
!= FL_DERIVED
7302 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
7305 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
7307 gfc_error ("Derived type definition of '%s' at %C has already been "
7308 "defined", sym
->name
);
7312 if (attr
.access
!= ACCESS_UNKNOWN
7313 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
7316 /* See if the derived type was labeled as bind(c). */
7317 if (attr
.is_bind_c
!= 0)
7318 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7320 /* Construct the f2k_derived namespace if it is not yet there. */
7321 if (!sym
->f2k_derived
)
7322 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7324 if (extended
&& !sym
->components
)
7329 /* Add the extended derived type as the first component. */
7330 gfc_add_component (sym
, parent
, &p
);
7332 gfc_set_sym_referenced (extended
);
7334 p
->ts
.type
= BT_DERIVED
;
7335 p
->ts
.u
.derived
= extended
;
7336 p
->initializer
= gfc_default_initializer (&p
->ts
);
7338 /* Set extension level. */
7339 if (extended
->attr
.extension
== 255)
7341 /* Since the extension field is 8 bit wide, we can only have
7342 up to 255 extension levels. */
7343 gfc_error ("Maximum extension level reached with type '%s' at %L",
7344 extended
->name
, &extended
->declared_at
);
7347 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7349 /* Provide the links between the extended type and its extension. */
7350 if (!extended
->f2k_derived
)
7351 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7352 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7356 if (!sym
->hash_value
)
7357 /* Set the hash for the compound name for this type. */
7358 sym
->hash_value
= hash_value (sym
);
7360 /* Take over the ABSTRACT attribute. */
7361 sym
->attr
.abstract
= attr
.abstract
;
7363 gfc_new_block
= sym
;
7369 /* Cray Pointees can be declared as:
7370 pointer (ipt, a (n,m,...,*)) */
7373 gfc_mod_pointee_as (gfc_array_spec
*as
)
7375 as
->cray_pointee
= true; /* This will be useful to know later. */
7376 if (as
->type
== AS_ASSUMED_SIZE
)
7377 as
->cp_was_assumed
= true;
7378 else if (as
->type
== AS_ASSUMED_SHAPE
)
7380 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7387 /* Match the enum definition statement, here we are trying to match
7388 the first line of enum definition statement.
7389 Returns MATCH_YES if match is found. */
7392 gfc_match_enum (void)
7396 m
= gfc_match_eos ();
7400 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
7408 /* Returns an initializer whose value is one higher than the value of the
7409 LAST_INITIALIZER argument. If the argument is NULL, the
7410 initializers value will be set to zero. The initializer's kind
7411 will be set to gfc_c_int_kind.
7413 If -fshort-enums is given, the appropriate kind will be selected
7414 later after all enumerators have been parsed. A warning is issued
7415 here if an initializer exceeds gfc_c_int_kind. */
7418 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7421 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7423 mpz_init (result
->value
.integer
);
7425 if (last_initializer
!= NULL
)
7427 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7428 result
->where
= last_initializer
->where
;
7430 if (gfc_check_integer_range (result
->value
.integer
,
7431 gfc_c_int_kind
) != ARITH_OK
)
7433 gfc_error ("Enumerator exceeds the C integer type at %C");
7439 /* Control comes here, if it's the very first enumerator and no
7440 initializer has been given. It will be initialized to zero. */
7441 mpz_set_si (result
->value
.integer
, 0);
7448 /* Match a variable name with an optional initializer. When this
7449 subroutine is called, a variable is expected to be parsed next.
7450 Depending on what is happening at the moment, updates either the
7451 symbol table or the current interface. */
7454 enumerator_decl (void)
7456 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7457 gfc_expr
*initializer
;
7458 gfc_array_spec
*as
= NULL
;
7466 old_locus
= gfc_current_locus
;
7468 /* When we get here, we've just matched a list of attributes and
7469 maybe a type and a double colon. The next thing we expect to see
7470 is the name of the symbol. */
7471 m
= gfc_match_name (name
);
7475 var_locus
= gfc_current_locus
;
7477 /* OK, we've successfully matched the declaration. Now put the
7478 symbol in the current namespace. If we fail to create the symbol,
7480 if (build_sym (name
, NULL
, false, &as
, &var_locus
) == FAILURE
)
7486 /* The double colon must be present in order to have initializers.
7487 Otherwise the statement is ambiguous with an assignment statement. */
7490 if (gfc_match_char ('=') == MATCH_YES
)
7492 m
= gfc_match_init_expr (&initializer
);
7495 gfc_error ("Expected an initialization expression at %C");
7504 /* If we do not have an initializer, the initialization value of the
7505 previous enumerator (stored in last_initializer) is incremented
7506 by 1 and is used to initialize the current enumerator. */
7507 if (initializer
== NULL
)
7508 initializer
= enum_initializer (last_initializer
, old_locus
);
7510 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7512 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7518 /* Store this current initializer, for the next enumerator variable
7519 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7520 use last_initializer below. */
7521 last_initializer
= initializer
;
7522 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7524 /* Maintain enumerator history. */
7525 gfc_find_symbol (name
, NULL
, 0, &sym
);
7526 create_enum_history (sym
, last_initializer
);
7528 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
7531 /* Free stuff up and return. */
7532 gfc_free_expr (initializer
);
7538 /* Match the enumerator definition statement. */
7541 gfc_match_enumerator_def (void)
7546 gfc_clear_ts (¤t_ts
);
7548 m
= gfc_match (" enumerator");
7552 m
= gfc_match (" :: ");
7553 if (m
== MATCH_ERROR
)
7556 colon_seen
= (m
== MATCH_YES
);
7558 if (gfc_current_state () != COMP_ENUM
)
7560 gfc_error ("ENUM definition statement expected before %C");
7561 gfc_free_enum_history ();
7565 (¤t_ts
)->type
= BT_INTEGER
;
7566 (¤t_ts
)->kind
= gfc_c_int_kind
;
7568 gfc_clear_attr (¤t_attr
);
7569 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7578 m
= enumerator_decl ();
7579 if (m
== MATCH_ERROR
)
7581 gfc_free_enum_history ();
7587 if (gfc_match_eos () == MATCH_YES
)
7589 if (gfc_match_char (',') != MATCH_YES
)
7593 if (gfc_current_state () == COMP_ENUM
)
7595 gfc_free_enum_history ();
7596 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7601 gfc_free_array_spec (current_as
);
7608 /* Match binding attributes. */
7611 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7613 bool found_passing
= false;
7614 bool seen_ptr
= false;
7615 match m
= MATCH_YES
;
7617 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7618 this case the defaults are in there. */
7619 ba
->access
= ACCESS_UNKNOWN
;
7620 ba
->pass_arg
= NULL
;
7621 ba
->pass_arg_num
= 0;
7623 ba
->non_overridable
= 0;
7627 /* If we find a comma, we believe there are binding attributes. */
7628 m
= gfc_match_char (',');
7634 /* Access specifier. */
7636 m
= gfc_match (" public");
7637 if (m
== MATCH_ERROR
)
7641 if (ba
->access
!= ACCESS_UNKNOWN
)
7643 gfc_error ("Duplicate access-specifier at %C");
7647 ba
->access
= ACCESS_PUBLIC
;
7651 m
= gfc_match (" private");
7652 if (m
== MATCH_ERROR
)
7656 if (ba
->access
!= ACCESS_UNKNOWN
)
7658 gfc_error ("Duplicate access-specifier at %C");
7662 ba
->access
= ACCESS_PRIVATE
;
7666 /* If inside GENERIC, the following is not allowed. */
7671 m
= gfc_match (" nopass");
7672 if (m
== MATCH_ERROR
)
7678 gfc_error ("Binding attributes already specify passing,"
7679 " illegal NOPASS at %C");
7683 found_passing
= true;
7688 /* PASS possibly including argument. */
7689 m
= gfc_match (" pass");
7690 if (m
== MATCH_ERROR
)
7694 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
7698 gfc_error ("Binding attributes already specify passing,"
7699 " illegal PASS at %C");
7703 m
= gfc_match (" ( %n )", arg
);
7704 if (m
== MATCH_ERROR
)
7707 ba
->pass_arg
= gfc_get_string (arg
);
7708 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
7710 found_passing
= true;
7718 m
= gfc_match (" pointer");
7719 if (m
== MATCH_ERROR
)
7725 gfc_error ("Duplicate POINTER attribute at %C");
7735 /* NON_OVERRIDABLE flag. */
7736 m
= gfc_match (" non_overridable");
7737 if (m
== MATCH_ERROR
)
7741 if (ba
->non_overridable
)
7743 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7747 ba
->non_overridable
= 1;
7751 /* DEFERRED flag. */
7752 m
= gfc_match (" deferred");
7753 if (m
== MATCH_ERROR
)
7759 gfc_error ("Duplicate DEFERRED at %C");
7770 /* Nothing matching found. */
7772 gfc_error ("Expected access-specifier at %C");
7774 gfc_error ("Expected binding attribute at %C");
7777 while (gfc_match_char (',') == MATCH_YES
);
7779 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7780 if (ba
->non_overridable
&& ba
->deferred
)
7782 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7789 if (ba
->access
== ACCESS_UNKNOWN
)
7790 ba
->access
= gfc_typebound_default_access
;
7792 if (ppc
&& !seen_ptr
)
7794 gfc_error ("POINTER attribute is required for procedure pointer component"
7806 /* Match a PROCEDURE specific binding inside a derived type. */
7809 match_procedure_in_type (void)
7811 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7812 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
7813 char* target
= NULL
, *ifc
= NULL
;
7814 gfc_typebound_proc tb
;
7823 /* Check current state. */
7824 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
7825 block
= gfc_state_stack
->previous
->sym
;
7828 /* Try to match PROCEDURE(interface). */
7829 if (gfc_match (" (") == MATCH_YES
)
7831 m
= gfc_match_name (target_buf
);
7832 if (m
== MATCH_ERROR
)
7836 gfc_error ("Interface-name expected after '(' at %C");
7840 if (gfc_match (" )") != MATCH_YES
)
7842 gfc_error ("')' expected at %C");
7849 /* Construct the data structure. */
7850 memset (&tb
, 0, sizeof (tb
));
7851 tb
.where
= gfc_current_locus
;
7853 /* Match binding attributes. */
7854 m
= match_binding_attributes (&tb
, false, false);
7855 if (m
== MATCH_ERROR
)
7857 seen_attrs
= (m
== MATCH_YES
);
7859 /* Check that attribute DEFERRED is given if an interface is specified. */
7860 if (tb
.deferred
&& !ifc
)
7862 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7865 if (ifc
&& !tb
.deferred
)
7867 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7871 /* Match the colons. */
7872 m
= gfc_match (" ::");
7873 if (m
== MATCH_ERROR
)
7875 seen_colons
= (m
== MATCH_YES
);
7876 if (seen_attrs
&& !seen_colons
)
7878 gfc_error ("Expected '::' after binding-attributes at %C");
7882 /* Match the binding names. */
7885 m
= gfc_match_name (name
);
7886 if (m
== MATCH_ERROR
)
7890 gfc_error ("Expected binding name at %C");
7894 if (num
>1 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: PROCEDURE list"
7895 " at %C") == FAILURE
)
7898 /* Try to match the '=> target', if it's there. */
7900 m
= gfc_match (" =>");
7901 if (m
== MATCH_ERROR
)
7907 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7913 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7918 m
= gfc_match_name (target_buf
);
7919 if (m
== MATCH_ERROR
)
7923 gfc_error ("Expected binding target after '=>' at %C");
7926 target
= target_buf
;
7929 /* If no target was found, it has the same name as the binding. */
7933 /* Get the namespace to insert the symbols into. */
7934 ns
= block
->f2k_derived
;
7937 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7938 if (tb
.deferred
&& !block
->attr
.abstract
)
7940 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7941 "is not ABSTRACT", block
->name
);
7945 /* See if we already have a binding with this name in the symtree which
7946 would be an error. If a GENERIC already targetted this binding, it may
7947 be already there but then typebound is still NULL. */
7948 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7949 if (stree
&& stree
->n
.tb
)
7951 gfc_error ("There is already a procedure with binding name '%s' for "
7952 "the derived type '%s' at %C", name
, block
->name
);
7956 /* Insert it and set attributes. */
7960 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7963 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
7965 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
7968 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
7970 if (gfc_match_eos () == MATCH_YES
)
7972 if (gfc_match_char (',') != MATCH_YES
)
7977 gfc_error ("Syntax error in PROCEDURE statement at %C");
7982 /* Match a GENERIC procedure binding inside a derived type. */
7985 gfc_match_generic (void)
7987 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7988 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
7990 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
7991 gfc_typebound_proc
* tb
;
7993 interface_type op_type
;
7994 gfc_intrinsic_op op
;
7997 /* Check current state. */
7998 if (gfc_current_state () == COMP_DERIVED
)
8000 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8003 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8005 block
= gfc_state_stack
->previous
->sym
;
8006 ns
= block
->f2k_derived
;
8007 gcc_assert (block
&& ns
);
8009 memset (&tbattr
, 0, sizeof (tbattr
));
8010 tbattr
.where
= gfc_current_locus
;
8012 /* See if we get an access-specifier. */
8013 m
= match_binding_attributes (&tbattr
, true, false);
8014 if (m
== MATCH_ERROR
)
8017 /* Now the colons, those are required. */
8018 if (gfc_match (" ::") != MATCH_YES
)
8020 gfc_error ("Expected '::' at %C");
8024 /* Match the binding name; depending on type (operator / generic) format
8025 it for future error messages into bind_name. */
8027 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8028 if (m
== MATCH_ERROR
)
8032 gfc_error ("Expected generic name or operator descriptor at %C");
8038 case INTERFACE_GENERIC
:
8039 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8042 case INTERFACE_USER_OP
:
8043 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8046 case INTERFACE_INTRINSIC_OP
:
8047 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8048 gfc_op2string (op
));
8055 /* Match the required =>. */
8056 if (gfc_match (" =>") != MATCH_YES
)
8058 gfc_error ("Expected '=>' at %C");
8062 /* Try to find existing GENERIC binding with this name / for this operator;
8063 if there is something, check that it is another GENERIC and then extend
8064 it rather than building a new node. Otherwise, create it and put it
8065 at the right position. */
8069 case INTERFACE_USER_OP
:
8070 case INTERFACE_GENERIC
:
8072 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8075 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8087 case INTERFACE_INTRINSIC_OP
:
8097 if (!tb
->is_generic
)
8099 gcc_assert (op_type
== INTERFACE_GENERIC
);
8100 gfc_error ("There's already a non-generic procedure with binding name"
8101 " '%s' for the derived type '%s' at %C",
8102 bind_name
, block
->name
);
8106 if (tb
->access
!= tbattr
.access
)
8108 gfc_error ("Binding at %C must have the same access as already"
8109 " defined binding '%s'", bind_name
);
8115 tb
= gfc_get_typebound_proc (NULL
);
8116 tb
->where
= gfc_current_locus
;
8117 tb
->access
= tbattr
.access
;
8119 tb
->u
.generic
= NULL
;
8123 case INTERFACE_GENERIC
:
8124 case INTERFACE_USER_OP
:
8126 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8129 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8137 case INTERFACE_INTRINSIC_OP
:
8146 /* Now, match all following names as specific targets. */
8149 gfc_symtree
* target_st
;
8150 gfc_tbp_generic
* target
;
8152 m
= gfc_match_name (name
);
8153 if (m
== MATCH_ERROR
)
8157 gfc_error ("Expected specific binding name at %C");
8161 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8163 /* See if this is a duplicate specification. */
8164 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8165 if (target_st
== target
->specific_st
)
8167 gfc_error ("'%s' already defined as specific binding for the"
8168 " generic '%s' at %C", name
, bind_name
);
8172 target
= gfc_get_tbp_generic ();
8173 target
->specific_st
= target_st
;
8174 target
->specific
= NULL
;
8175 target
->next
= tb
->u
.generic
;
8176 tb
->u
.generic
= target
;
8178 while (gfc_match (" ,") == MATCH_YES
);
8180 /* Here should be the end. */
8181 if (gfc_match_eos () != MATCH_YES
)
8183 gfc_error ("Junk after GENERIC binding at %C");
8194 /* Match a FINAL declaration inside a derived type. */
8197 gfc_match_final_decl (void)
8199 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8202 gfc_namespace
* module_ns
;
8206 if (gfc_current_form
== FORM_FREE
)
8208 char c
= gfc_peek_ascii_char ();
8209 if (!gfc_is_whitespace (c
) && c
!= ':')
8213 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8215 if (gfc_current_form
== FORM_FIXED
)
8218 gfc_error ("FINAL declaration at %C must be inside a derived type "
8219 "CONTAINS section");
8223 block
= gfc_state_stack
->previous
->sym
;
8226 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8227 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8229 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8230 " specification part of a MODULE");
8234 module_ns
= gfc_current_ns
;
8235 gcc_assert (module_ns
);
8236 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8238 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8239 if (gfc_match (" ::") == MATCH_ERROR
)
8242 /* Match the sequence of procedure names. */
8249 if (first
&& gfc_match_eos () == MATCH_YES
)
8251 gfc_error ("Empty FINAL at %C");
8255 m
= gfc_match_name (name
);
8258 gfc_error ("Expected module procedure name at %C");
8261 else if (m
!= MATCH_YES
)
8264 if (gfc_match_eos () == MATCH_YES
)
8266 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8268 gfc_error ("Expected ',' at %C");
8272 if (gfc_get_symbol (name
, module_ns
, &sym
))
8274 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8278 /* Mark the symbol as module procedure. */
8279 if (sym
->attr
.proc
!= PROC_MODULE
8280 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
8281 sym
->name
, NULL
) == FAILURE
)
8284 /* Check if we already have this symbol in the list, this is an error. */
8285 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8286 if (f
->proc_sym
== sym
)
8288 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8293 /* Add this symbol to the list of finalizers. */
8294 gcc_assert (block
->f2k_derived
);
8296 f
= XCNEW (gfc_finalizer
);
8298 f
->proc_tree
= NULL
;
8299 f
->where
= gfc_current_locus
;
8300 f
->next
= block
->f2k_derived
->finalizers
;
8301 block
->f2k_derived
->finalizers
= f
;
8311 const ext_attr_t ext_attr_list
[] = {
8312 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8313 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8314 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8315 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8316 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8317 { NULL
, EXT_ATTR_LAST
, NULL
}
8320 /* Match a !GCC$ ATTRIBUTES statement of the form:
8321 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8322 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8324 TODO: We should support all GCC attributes using the same syntax for
8325 the attribute list, i.e. the list in C
8326 __attributes(( attribute-list ))
8328 !GCC$ ATTRIBUTES attribute-list ::
8329 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8332 As there is absolutely no risk of confusion, we should never return
8335 gfc_match_gcc_attributes (void)
8337 symbol_attribute attr
;
8338 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8343 gfc_clear_attr (&attr
);
8348 if (gfc_match_name (name
) != MATCH_YES
)
8351 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8352 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8355 if (id
== EXT_ATTR_LAST
)
8357 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8361 if (gfc_add_ext_attribute (&attr
, (ext_attr_id_t
) id
, &gfc_current_locus
)
8365 gfc_gobble_whitespace ();
8366 ch
= gfc_next_ascii_char ();
8369 /* This is the successful exit condition for the loop. */
8370 if (gfc_next_ascii_char () == ':')
8380 if (gfc_match_eos () == MATCH_YES
)
8385 m
= gfc_match_name (name
);
8389 if (find_special (name
, &sym
, true))
8392 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8394 if (gfc_match_eos () == MATCH_YES
)
8397 if (gfc_match_char (',') != MATCH_YES
)
8404 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");