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 a '*'. */
653 char_len_param_value (gfc_expr
**expr
)
657 if (gfc_match_char ('*') == MATCH_YES
)
663 m
= gfc_match_expr (expr
);
666 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
669 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
671 if ((*expr
)->value
.function
.actual
672 && (*expr
)->value
.function
.actual
->expr
->symtree
)
675 e
= (*expr
)->value
.function
.actual
->expr
;
676 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
677 && e
->expr_type
== EXPR_VARIABLE
)
679 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
681 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
682 && e
->symtree
->n
.sym
->ts
.u
.cl
683 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
691 gfc_error ("Conflict in attributes of function argument at %C");
696 /* A character length is a '*' followed by a literal integer or a
697 char_len_param_value in parenthesis. */
700 match_char_length (gfc_expr
**expr
)
705 m
= gfc_match_char ('*');
709 m
= gfc_match_small_literal_int (&length
, NULL
);
710 if (m
== MATCH_ERROR
)
715 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
716 "Old-style character length at %C") == FAILURE
)
718 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
722 if (gfc_match_char ('(') == MATCH_NO
)
725 m
= char_len_param_value (expr
);
726 if (m
!= MATCH_YES
&& gfc_matching_function
)
732 if (m
== MATCH_ERROR
)
737 if (gfc_match_char (')') == MATCH_NO
)
739 gfc_free_expr (*expr
);
747 gfc_error ("Syntax error in character length specification at %C");
752 /* Special subroutine for finding a symbol. Check if the name is found
753 in the current name space. If not, and we're compiling a function or
754 subroutine and the parent compilation unit is an interface, then check
755 to see if the name we've been given is the name of the interface
756 (located in another namespace). */
759 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
765 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
768 *result
= st
? st
->n
.sym
: NULL
;
772 if (gfc_current_state () != COMP_SUBROUTINE
773 && gfc_current_state () != COMP_FUNCTION
)
776 s
= gfc_state_stack
->previous
;
780 if (s
->state
!= COMP_INTERFACE
)
783 goto end
; /* Nameless interface. */
785 if (strcmp (name
, s
->sym
->name
) == 0)
796 /* Special subroutine for getting a symbol node associated with a
797 procedure name, used in SUBROUTINE and FUNCTION statements. The
798 symbol is created in the parent using with symtree node in the
799 child unit pointing to the symbol. If the current namespace has no
800 parent, then the symbol is just created in the current unit. */
803 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
809 /* Module functions have to be left in their own namespace because
810 they have potentially (almost certainly!) already been referenced.
811 In this sense, they are rather like external functions. This is
812 fixed up in resolve.c(resolve_entries), where the symbol name-
813 space is set to point to the master function, so that the fake
814 result mechanism can work. */
815 if (module_fcn_entry
)
817 /* Present if entry is declared to be a module procedure. */
818 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
821 rc
= gfc_get_symbol (name
, NULL
, result
);
822 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
823 && (*result
)->ts
.type
== BT_UNKNOWN
824 && sym
->attr
.flavor
== FL_UNKNOWN
)
825 /* Pick up the typespec for the entry, if declared in the function
826 body. Note that this symbol is FL_UNKNOWN because it will
827 only have appeared in a type declaration. The local symtree
828 is set to point to the module symbol and a unique symtree
829 to the local version. This latter ensures a correct clearing
832 /* If the ENTRY proceeds its specification, we need to ensure
833 that this does not raise a "has no IMPLICIT type" error. */
834 if (sym
->ts
.type
== BT_UNKNOWN
)
835 sym
->attr
.untyped
= 1;
837 (*result
)->ts
= sym
->ts
;
839 /* Put the symbol in the procedure namespace so that, should
840 the ENTRY precede its specification, the specification
842 (*result
)->ns
= gfc_current_ns
;
844 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
846 st
= gfc_get_unique_symtree (gfc_current_ns
);
851 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
857 gfc_current_ns
->refs
++;
859 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
861 /* Trap another encompassed procedure with the same name. All
862 these conditions are necessary to avoid picking up an entry
863 whose name clashes with that of the encompassing procedure;
864 this is handled using gsymbols to register unique,globally
866 if (sym
->attr
.flavor
!= 0
867 && sym
->attr
.proc
!= 0
868 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
869 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
870 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
871 name
, &sym
->declared_at
);
873 /* Trap a procedure with a name the same as interface in the
874 encompassing scope. */
875 if (sym
->attr
.generic
!= 0
876 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
877 && !sym
->attr
.mod_proc
)
878 gfc_error_now ("Name '%s' at %C is already defined"
879 " as a generic interface at %L",
880 name
, &sym
->declared_at
);
882 /* Trap declarations of attributes in encompassing scope. The
883 signature for this is that ts.kind is set. Legitimate
884 references only set ts.type. */
885 if (sym
->ts
.kind
!= 0
886 && !sym
->attr
.implicit_type
887 && sym
->attr
.proc
== 0
888 && gfc_current_ns
->parent
!= NULL
889 && sym
->attr
.access
== 0
890 && !module_fcn_entry
)
891 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
892 "and must not have attributes declared at %L",
893 name
, &sym
->declared_at
);
896 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
899 /* Module function entries will already have a symtree in
900 the current namespace but will need one at module level. */
901 if (module_fcn_entry
)
903 /* Present if entry is declared to be a module procedure. */
904 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
906 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
909 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
914 /* See if the procedure should be a module procedure. */
916 if (((sym
->ns
->proc_name
!= NULL
917 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
918 && sym
->attr
.proc
!= PROC_MODULE
)
919 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
920 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
921 sym
->name
, NULL
) == FAILURE
)
928 /* Verify that the given symbol representing a parameter is C
929 interoperable, by checking to see if it was marked as such after
930 its declaration. If the given symbol is not interoperable, a
931 warning is reported, thus removing the need to return the status to
932 the calling function. The standard does not require the user use
933 one of the iso_c_binding named constants to declare an
934 interoperable parameter, but we can't be sure if the param is C
935 interop or not if the user doesn't. For example, integer(4) may be
936 legal Fortran, but doesn't have meaning in C. It may interop with
937 a number of the C types, which causes a problem because the
938 compiler can't know which one. This code is almost certainly not
939 portable, and the user will get what they deserve if the C type
940 across platforms isn't always interoperable with integer(4). If
941 the user had used something like integer(c_int) or integer(c_long),
942 the compiler could have automatically handled the varying sizes
946 verify_c_interop_param (gfc_symbol
*sym
)
948 int is_c_interop
= 0;
949 gfc_try retval
= SUCCESS
;
951 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
952 Don't repeat the checks here. */
953 if (sym
->attr
.implicit_type
)
956 /* For subroutines or functions that are passed to a BIND(C) procedure,
957 they're interoperable if they're BIND(C) and their params are all
959 if (sym
->attr
.flavor
== FL_PROCEDURE
)
961 if (sym
->attr
.is_bind_c
== 0)
963 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
964 "attribute to be C interoperable", sym
->name
,
965 &(sym
->declared_at
));
971 if (sym
->attr
.is_c_interop
== 1)
972 /* We've already checked this procedure; don't check it again. */
975 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
980 /* See if we've stored a reference to a procedure that owns sym. */
981 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
983 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
986 (verify_c_interop (&(sym
->ts
))
989 if (is_c_interop
!= 1)
991 /* Make personalized messages to give better feedback. */
992 if (sym
->ts
.type
== BT_DERIVED
)
993 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
994 "procedure '%s' but is not C interoperable "
995 "because derived type '%s' is not C interoperable",
996 sym
->name
, &(sym
->declared_at
),
997 sym
->ns
->proc_name
->name
,
998 sym
->ts
.u
.derived
->name
);
1000 gfc_warning ("Variable '%s' at %L is a parameter to the "
1001 "BIND(C) procedure '%s' but may not be C "
1003 sym
->name
, &(sym
->declared_at
),
1004 sym
->ns
->proc_name
->name
);
1007 /* Character strings are only C interoperable if they have a
1009 if (sym
->ts
.type
== BT_CHARACTER
)
1011 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1012 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1013 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1015 gfc_error ("Character argument '%s' at %L "
1016 "must be length 1 because "
1017 "procedure '%s' is BIND(C)",
1018 sym
->name
, &sym
->declared_at
,
1019 sym
->ns
->proc_name
->name
);
1024 /* We have to make sure that any param to a bind(c) routine does
1025 not have the allocatable, pointer, or optional attributes,
1026 according to J3/04-007, section 5.1. */
1027 if (sym
->attr
.allocatable
== 1)
1029 gfc_error ("Variable '%s' at %L cannot have the "
1030 "ALLOCATABLE attribute because procedure '%s'"
1031 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1032 sym
->ns
->proc_name
->name
);
1036 if (sym
->attr
.pointer
== 1)
1038 gfc_error ("Variable '%s' at %L cannot have the "
1039 "POINTER attribute because procedure '%s'"
1040 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1041 sym
->ns
->proc_name
->name
);
1045 if (sym
->attr
.optional
== 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "OPTIONAL attribute because procedure '%s'"
1049 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1050 sym
->ns
->proc_name
->name
);
1054 /* Make sure that if it has the dimension attribute, that it is
1055 either assumed size or explicit shape. */
1056 if (sym
->as
!= NULL
)
1058 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
1060 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1061 "argument to the procedure '%s' at %L because "
1062 "the procedure is BIND(C)", sym
->name
,
1063 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1064 &(sym
->ns
->proc_name
->declared_at
));
1068 if (sym
->as
->type
== AS_DEFERRED
)
1070 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1071 "argument to the procedure '%s' at %L because "
1072 "the procedure is BIND(C)", sym
->name
,
1073 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1074 &(sym
->ns
->proc_name
->declared_at
));
1086 /* Function called by variable_decl() that adds a name to the symbol table. */
1089 build_sym (const char *name
, gfc_charlen
*cl
,
1090 gfc_array_spec
**as
, locus
*var_locus
)
1092 symbol_attribute attr
;
1095 if (gfc_get_symbol (name
, NULL
, &sym
))
1098 /* Start updating the symbol table. Add basic type attribute if present. */
1099 if (current_ts
.type
!= BT_UNKNOWN
1100 && (sym
->attr
.implicit_type
== 0
1101 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1102 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1105 if (sym
->ts
.type
== BT_CHARACTER
)
1108 /* Add dimension attribute if present. */
1109 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1113 /* Add attribute to symbol. The copy is so that we can reset the
1114 dimension attribute. */
1115 attr
= current_attr
;
1117 attr
.codimension
= 0;
1119 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1122 /* Finish any work that may need to be done for the binding label,
1123 if it's a bind(c). The bind(c) attr is found before the symbol
1124 is made, and before the symbol name (for data decls), so the
1125 current_ts is holding the binding label, or nothing if the
1126 name= attr wasn't given. Therefore, test here if we're dealing
1127 with a bind(c) and make sure the binding label is set correctly. */
1128 if (sym
->attr
.is_bind_c
== 1)
1130 if (sym
->binding_label
[0] == '\0')
1132 /* Set the binding label and verify that if a NAME= was specified
1133 then only one identifier was in the entity-decl-list. */
1134 if (set_binding_label (sym
->binding_label
, sym
->name
,
1135 num_idents_on_line
) == FAILURE
)
1140 /* See if we know we're in a common block, and if it's a bind(c)
1141 common then we need to make sure we're an interoperable type. */
1142 if (sym
->attr
.in_common
== 1)
1144 /* Test the common block object. */
1145 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1146 && sym
->ts
.is_c_interop
!= 1)
1148 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1149 "must be declared with a C interoperable "
1150 "kind since common block '%s' is BIND(C)",
1151 sym
->name
, sym
->common_block
->name
,
1152 sym
->common_block
->name
);
1157 sym
->attr
.implied_index
= 0;
1159 if (sym
->ts
.type
== BT_CLASS
1160 && (sym
->attr
.class_ok
= sym
->attr
.dummy
|| sym
->attr
.pointer
1161 || sym
->attr
.allocatable
))
1162 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1168 /* Set character constant to the given length. The constant will be padded or
1169 truncated. If we're inside an array constructor without a typespec, we
1170 additionally check that all elements have the same length; check_len -1
1171 means no checking. */
1174 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1179 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1180 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1182 slen
= expr
->value
.character
.length
;
1185 s
= gfc_get_wide_string (len
+ 1);
1186 memcpy (s
, expr
->value
.character
.string
,
1187 MIN (len
, slen
) * sizeof (gfc_char_t
));
1189 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1191 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1192 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1193 "(%d/%d)", &expr
->where
, slen
, len
);
1195 /* Apply the standard by 'hand' otherwise it gets cleared for
1197 if (check_len
!= -1 && slen
!= check_len
1198 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1199 gfc_error_now ("The CHARACTER elements of the array constructor "
1200 "at %L must have the same length (%d/%d)",
1201 &expr
->where
, slen
, check_len
);
1204 gfc_free (expr
->value
.character
.string
);
1205 expr
->value
.character
.string
= s
;
1206 expr
->value
.character
.length
= len
;
1211 /* Function to create and update the enumerator history
1212 using the information passed as arguments.
1213 Pointer "max_enum" is also updated, to point to
1214 enum history node containing largest initializer.
1216 SYM points to the symbol node of enumerator.
1217 INIT points to its enumerator value. */
1220 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1222 enumerator_history
*new_enum_history
;
1223 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1225 new_enum_history
= XCNEW (enumerator_history
);
1227 new_enum_history
->sym
= sym
;
1228 new_enum_history
->initializer
= init
;
1229 new_enum_history
->next
= NULL
;
1231 if (enum_history
== NULL
)
1233 enum_history
= new_enum_history
;
1234 max_enum
= enum_history
;
1238 new_enum_history
->next
= enum_history
;
1239 enum_history
= new_enum_history
;
1241 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1242 new_enum_history
->initializer
->value
.integer
) < 0)
1243 max_enum
= new_enum_history
;
1248 /* Function to free enum kind history. */
1251 gfc_free_enum_history (void)
1253 enumerator_history
*current
= enum_history
;
1254 enumerator_history
*next
;
1256 while (current
!= NULL
)
1258 next
= current
->next
;
1263 enum_history
= NULL
;
1267 /* Function called by variable_decl() that adds an initialization
1268 expression to a symbol. */
1271 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1273 symbol_attribute attr
;
1278 if (find_special (name
, &sym
, false))
1283 /* If this symbol is confirming an implicit parameter type,
1284 then an initialization expression is not allowed. */
1285 if (attr
.flavor
== FL_PARAMETER
1286 && sym
->value
!= NULL
1289 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1296 /* An initializer is required for PARAMETER declarations. */
1297 if (attr
.flavor
== FL_PARAMETER
)
1299 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1305 /* If a variable appears in a DATA block, it cannot have an
1309 gfc_error ("Variable '%s' at %C with an initializer already "
1310 "appears in a DATA statement", sym
->name
);
1314 /* Check if the assignment can happen. This has to be put off
1315 until later for derived type variables and procedure pointers. */
1316 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1317 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1318 && !sym
->attr
.proc_pointer
1319 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1322 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1323 && init
->ts
.type
== BT_CHARACTER
)
1325 /* Update symbol character length according initializer. */
1326 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1329 if (sym
->ts
.u
.cl
->length
== NULL
)
1332 /* If there are multiple CHARACTER variables declared on the
1333 same line, we don't want them to share the same length. */
1334 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1336 if (sym
->attr
.flavor
== FL_PARAMETER
)
1338 if (init
->expr_type
== EXPR_CONSTANT
)
1340 clen
= init
->value
.character
.length
;
1341 sym
->ts
.u
.cl
->length
1342 = gfc_get_int_expr (gfc_default_integer_kind
,
1345 else if (init
->expr_type
== EXPR_ARRAY
)
1348 c
= gfc_constructor_first (init
->value
.constructor
);
1349 clen
= c
->expr
->value
.character
.length
;
1350 sym
->ts
.u
.cl
->length
1351 = gfc_get_int_expr (gfc_default_integer_kind
,
1354 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1355 sym
->ts
.u
.cl
->length
=
1356 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1359 /* Update initializer character length according symbol. */
1360 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1362 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1364 if (init
->expr_type
== EXPR_CONSTANT
)
1365 gfc_set_constant_character_len (len
, init
, -1);
1366 else if (init
->expr_type
== EXPR_ARRAY
)
1370 /* Build a new charlen to prevent simplification from
1371 deleting the length before it is resolved. */
1372 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1373 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1375 for (c
= gfc_constructor_first (init
->value
.constructor
);
1376 c
; c
= gfc_constructor_next (c
))
1377 gfc_set_constant_character_len (len
, c
->expr
, -1);
1382 /* If sym is implied-shape, set its upper bounds from init. */
1383 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1384 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1388 if (init
->rank
== 0)
1390 gfc_error ("Can't initialize implied-shape array at %L"
1391 " with scalar", &sym
->declared_at
);
1394 gcc_assert (sym
->as
->rank
== init
->rank
);
1396 /* Shape should be present, we get an initialization expression. */
1397 gcc_assert (init
->shape
);
1399 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1405 lower
= sym
->as
->lower
[dim
];
1406 if (lower
->expr_type
!= EXPR_CONSTANT
)
1408 gfc_error ("Non-constant lower bound in implied-shape"
1409 " declaration at %L", &lower
->where
);
1413 /* All dimensions must be without upper bound. */
1414 gcc_assert (!sym
->as
->upper
[dim
]);
1417 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1418 mpz_add (e
->value
.integer
,
1419 lower
->value
.integer
, init
->shape
[dim
]);
1420 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1421 sym
->as
->upper
[dim
] = e
;
1424 sym
->as
->type
= AS_EXPLICIT
;
1427 /* Need to check if the expression we initialized this
1428 to was one of the iso_c_binding named constants. If so,
1429 and we're a parameter (constant), let it be iso_c.
1431 integer(c_int), parameter :: my_int = c_int
1432 integer(my_int) :: my_int_2
1433 If we mark my_int as iso_c (since we can see it's value
1434 is equal to one of the named constants), then my_int_2
1435 will be considered C interoperable. */
1436 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1438 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1439 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1440 /* attr bits needed for module files. */
1441 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1442 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1443 if (init
->ts
.is_iso_c
)
1444 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1447 /* Add initializer. Make sure we keep the ranks sane. */
1448 if (sym
->attr
.dimension
&& init
->rank
== 0)
1453 if (sym
->attr
.flavor
== FL_PARAMETER
1454 && init
->expr_type
== EXPR_CONSTANT
1455 && spec_size (sym
->as
, &size
) == SUCCESS
1456 && mpz_cmp_si (size
, 0) > 0)
1458 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1460 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1461 gfc_constructor_append_expr (&array
->value
.constructor
,
1464 : gfc_copy_expr (init
),
1467 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1468 for (n
= 0; n
< sym
->as
->rank
; n
++)
1469 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1474 init
->rank
= sym
->as
->rank
;
1478 if (sym
->attr
.save
== SAVE_NONE
)
1479 sym
->attr
.save
= SAVE_IMPLICIT
;
1487 /* Function called by variable_decl() that adds a name to a structure
1491 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1492 gfc_array_spec
**as
)
1495 gfc_try t
= SUCCESS
;
1497 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1498 constructing, it must have the pointer attribute. */
1499 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1500 && current_ts
.u
.derived
== gfc_current_block ()
1501 && current_attr
.pointer
== 0)
1503 gfc_error ("Component at %C must have the POINTER attribute");
1507 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1509 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1511 gfc_error ("Array component of structure at %C must have explicit "
1512 "or deferred shape");
1517 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1521 if (c
->ts
.type
== BT_CHARACTER
)
1523 c
->attr
= current_attr
;
1525 c
->initializer
= *init
;
1532 c
->attr
.codimension
= 1;
1534 c
->attr
.dimension
= 1;
1538 /* Should this ever get more complicated, combine with similar section
1539 in add_init_expr_to_sym into a separate function. */
1540 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
&& c
->ts
.u
.cl
1541 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1545 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1546 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1547 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1549 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1551 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1552 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1553 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1554 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1556 gfc_constructor
*ctor
;
1557 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1562 bool has_ts
= (c
->initializer
->ts
.u
.cl
1563 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1565 /* Remember the length of the first element for checking
1566 that all elements *in the constructor* have the same
1567 length. This need not be the length of the LHS! */
1568 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1569 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1570 first_len
= ctor
->expr
->value
.character
.length
;
1572 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1573 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1575 gfc_set_constant_character_len (len
, ctor
->expr
,
1576 has_ts
? -1 : first_len
);
1577 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1583 /* Check array components. */
1584 if (!c
->attr
.dimension
)
1587 if (c
->attr
.pointer
)
1589 if (c
->as
->type
!= AS_DEFERRED
)
1591 gfc_error ("Pointer array component of structure at %C must have a "
1596 else if (c
->attr
.allocatable
)
1598 if (c
->as
->type
!= AS_DEFERRED
)
1600 gfc_error ("Allocatable component of structure at %C must have a "
1607 if (c
->as
->type
!= AS_EXPLICIT
)
1609 gfc_error ("Array component of structure at %C must have an "
1616 if (c
->ts
.type
== BT_CLASS
)
1617 gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, true);
1623 /* Match a 'NULL()', and possibly take care of some side effects. */
1626 gfc_match_null (gfc_expr
**result
)
1631 m
= gfc_match (" null ( )");
1635 /* The NULL symbol now has to be/become an intrinsic function. */
1636 if (gfc_get_symbol ("null", NULL
, &sym
))
1638 gfc_error ("NULL() initialization at %C is ambiguous");
1642 gfc_intrinsic_symbol (sym
);
1644 if (sym
->attr
.proc
!= PROC_INTRINSIC
1645 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1646 sym
->name
, NULL
) == FAILURE
1647 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1650 *result
= gfc_get_null_expr (&gfc_current_locus
);
1656 /* Match the initialization expr for a data pointer or procedure pointer. */
1659 match_pointer_init (gfc_expr
**init
, int procptr
)
1663 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1665 gfc_error ("Initialization of pointer at %C is not allowed in "
1666 "a PURE procedure");
1670 /* Match NULL() initilization. */
1671 m
= gfc_match_null (init
);
1675 /* Match non-NULL initialization. */
1676 gfc_matching_procptr_assignment
= procptr
;
1677 m
= gfc_match_rvalue (init
);
1678 gfc_matching_procptr_assignment
= 0;
1679 if (m
== MATCH_ERROR
)
1681 else if (m
== MATCH_NO
)
1683 gfc_error ("Error in pointer initialization at %C");
1688 gfc_resolve_expr (*init
);
1690 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: non-NULL pointer "
1691 "initialization at %C") == FAILURE
)
1698 /* Match a variable name with an optional initializer. When this
1699 subroutine is called, a variable is expected to be parsed next.
1700 Depending on what is happening at the moment, updates either the
1701 symbol table or the current interface. */
1704 variable_decl (int elem
)
1706 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1707 gfc_expr
*initializer
, *char_len
;
1709 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1720 /* When we get here, we've just matched a list of attributes and
1721 maybe a type and a double colon. The next thing we expect to see
1722 is the name of the symbol. */
1723 m
= gfc_match_name (name
);
1727 var_locus
= gfc_current_locus
;
1729 /* Now we could see the optional array spec. or character length. */
1730 m
= gfc_match_array_spec (&as
, true, true);
1731 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1732 cp_as
= gfc_copy_array_spec (as
);
1733 else if (m
== MATCH_ERROR
)
1737 as
= gfc_copy_array_spec (current_as
);
1738 else if (current_as
)
1739 merge_array_spec (current_as
, as
, true);
1741 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1742 determine (and check) whether it can be implied-shape. If it
1743 was parsed as assumed-size, change it because PARAMETERs can not
1747 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1750 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1755 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1756 && current_attr
.flavor
== FL_PARAMETER
)
1757 as
->type
= AS_IMPLIED_SHAPE
;
1759 if (as
->type
== AS_IMPLIED_SHAPE
1760 && gfc_notify_std (GFC_STD_F2008
,
1761 "Fortran 2008: Implied-shape array at %L",
1762 &var_locus
) == FAILURE
)
1772 if (current_ts
.type
== BT_CHARACTER
)
1774 switch (match_char_length (&char_len
))
1777 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1779 cl
->length
= char_len
;
1782 /* Non-constant lengths need to be copied after the first
1783 element. Also copy assumed lengths. */
1786 && (current_ts
.u
.cl
->length
== NULL
1787 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1789 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1790 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1793 cl
= current_ts
.u
.cl
;
1802 /* If this symbol has already shown up in a Cray Pointer declaration,
1803 then we want to set the type & bail out. */
1804 if (gfc_option
.flag_cray_pointer
)
1806 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1807 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1809 sym
->ts
.type
= current_ts
.type
;
1810 sym
->ts
.kind
= current_ts
.kind
;
1812 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1813 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1814 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1817 /* Check to see if we have an array specification. */
1820 if (sym
->as
!= NULL
)
1822 gfc_error ("Duplicate array spec for Cray pointee at %C");
1823 gfc_free_array_spec (cp_as
);
1829 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1830 gfc_internal_error ("Couldn't set pointee array spec.");
1832 /* Fix the array spec. */
1833 m
= gfc_mod_pointee_as (sym
->as
);
1834 if (m
== MATCH_ERROR
)
1842 gfc_free_array_spec (cp_as
);
1846 /* Procedure pointer as function result. */
1847 if (gfc_current_state () == COMP_FUNCTION
1848 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1849 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1850 strcpy (name
, "ppr@");
1852 if (gfc_current_state () == COMP_FUNCTION
1853 && strcmp (name
, gfc_current_block ()->name
) == 0
1854 && gfc_current_block ()->result
1855 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1856 strcpy (name
, "ppr@");
1858 /* OK, we've successfully matched the declaration. Now put the
1859 symbol in the current namespace, because it might be used in the
1860 optional initialization expression for this symbol, e.g. this is
1863 integer, parameter :: i = huge(i)
1865 This is only true for parameters or variables of a basic type.
1866 For components of derived types, it is not true, so we don't
1867 create a symbol for those yet. If we fail to create the symbol,
1869 if (gfc_current_state () != COMP_DERIVED
1870 && build_sym (name
, cl
, &as
, &var_locus
) == FAILURE
)
1876 /* An interface body specifies all of the procedure's
1877 characteristics and these shall be consistent with those
1878 specified in the procedure definition, except that the interface
1879 may specify a procedure that is not pure if the procedure is
1880 defined to be pure(12.3.2). */
1881 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1882 && gfc_current_ns
->proc_name
1883 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1884 && current_ts
.u
.derived
->ns
!= gfc_current_ns
)
1887 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.u
.derived
->name
);
1888 if (!(current_ts
.u
.derived
->attr
.imported
1890 && st
->n
.sym
== current_ts
.u
.derived
)
1891 && !gfc_current_ns
->has_import_set
)
1893 gfc_error ("the type of '%s' at %C has not been declared within the "
1900 /* In functions that have a RESULT variable defined, the function
1901 name always refers to function calls. Therefore, the name is
1902 not allowed to appear in specification statements. */
1903 if (gfc_current_state () == COMP_FUNCTION
1904 && gfc_current_block () != NULL
1905 && gfc_current_block ()->result
!= NULL
1906 && gfc_current_block ()->result
!= gfc_current_block ()
1907 && strcmp (gfc_current_block ()->name
, name
) == 0)
1909 gfc_error ("Function name '%s' not allowed at %C", name
);
1914 /* We allow old-style initializations of the form
1915 integer i /2/, j(4) /3*3, 1/
1916 (if no colon has been seen). These are different from data
1917 statements in that initializers are only allowed to apply to the
1918 variable immediately preceding, i.e.
1920 is not allowed. Therefore we have to do some work manually, that
1921 could otherwise be left to the matchers for DATA statements. */
1923 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1925 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1926 "initialization at %C") == FAILURE
)
1929 return match_old_style_init (name
);
1932 /* The double colon must be present in order to have initializers.
1933 Otherwise the statement is ambiguous with an assignment statement. */
1936 if (gfc_match (" =>") == MATCH_YES
)
1938 if (!current_attr
.pointer
)
1940 gfc_error ("Initialization at %C isn't for a pointer variable");
1945 m
= match_pointer_init (&initializer
, 0);
1949 else if (gfc_match_char ('=') == MATCH_YES
)
1951 if (current_attr
.pointer
)
1953 gfc_error ("Pointer initialization at %C requires '=>', "
1959 m
= gfc_match_init_expr (&initializer
);
1962 gfc_error ("Expected an initialization expression at %C");
1966 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
1967 && gfc_state_stack
->state
!= COMP_DERIVED
)
1969 gfc_error ("Initialization of variable at %C is not allowed in "
1970 "a PURE procedure");
1979 if (initializer
!= NULL
&& current_attr
.allocatable
1980 && gfc_current_state () == COMP_DERIVED
)
1982 gfc_error ("Initialization of allocatable component at %C is not "
1988 /* Add the initializer. Note that it is fine if initializer is
1989 NULL here, because we sometimes also need to check if a
1990 declaration *must* have an initialization expression. */
1991 if (gfc_current_state () != COMP_DERIVED
)
1992 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
1995 if (current_ts
.type
== BT_DERIVED
1996 && !current_attr
.pointer
&& !initializer
)
1997 initializer
= gfc_default_initializer (¤t_ts
);
1998 t
= build_struct (name
, cl
, &initializer
, &as
);
2001 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
2004 /* Free stuff up and return. */
2005 gfc_free_expr (initializer
);
2006 gfc_free_array_spec (as
);
2012 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2013 This assumes that the byte size is equal to the kind number for
2014 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2017 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2022 if (gfc_match_char ('*') != MATCH_YES
)
2025 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2029 original_kind
= ts
->kind
;
2031 /* Massage the kind numbers for complex types. */
2032 if (ts
->type
== BT_COMPLEX
)
2036 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2037 gfc_basic_typename (ts
->type
), original_kind
);
2043 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2045 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2046 gfc_basic_typename (ts
->type
), original_kind
);
2050 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
2051 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
2058 /* Match a kind specification. Since kinds are generally optional, we
2059 usually return MATCH_NO if something goes wrong. If a "kind="
2060 string is found, then we know we have an error. */
2063 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2075 where
= loc
= gfc_current_locus
;
2080 if (gfc_match_char ('(') == MATCH_NO
)
2083 /* Also gobbles optional text. */
2084 if (gfc_match (" kind = ") == MATCH_YES
)
2087 loc
= gfc_current_locus
;
2090 n
= gfc_match_init_expr (&e
);
2094 if (gfc_matching_function
)
2096 /* The function kind expression might include use associated or
2097 imported parameters and try again after the specification
2099 if (gfc_match_char (')') != MATCH_YES
)
2101 gfc_error ("Missing right parenthesis at %C");
2107 gfc_undo_symbols ();
2112 /* ....or else, the match is real. */
2114 gfc_error ("Expected initialization expression at %C");
2122 gfc_error ("Expected scalar initialization expression at %C");
2127 msg
= gfc_extract_int (e
, &ts
->kind
);
2136 /* Before throwing away the expression, let's see if we had a
2137 C interoperable kind (and store the fact). */
2138 if (e
->ts
.is_c_interop
== 1)
2140 /* Mark this as c interoperable if being declared with one
2141 of the named constants from iso_c_binding. */
2142 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2143 ts
->f90_type
= e
->ts
.f90_type
;
2149 /* Ignore errors to this point, if we've gotten here. This means
2150 we ignore the m=MATCH_ERROR from above. */
2151 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2153 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2154 gfc_basic_typename (ts
->type
));
2155 gfc_current_locus
= where
;
2159 /* Warn if, e.g., c_int is used for a REAL variable, but not
2160 if, e.g., c_double is used for COMPLEX as the standard
2161 explicitly says that the kind type parameter for complex and real
2162 variable is the same, i.e. c_float == c_float_complex. */
2163 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2164 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2165 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2166 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2167 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2168 gfc_basic_typename (ts
->type
));
2170 gfc_gobble_whitespace ();
2171 if ((c
= gfc_next_ascii_char ()) != ')'
2172 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2174 if (ts
->type
== BT_CHARACTER
)
2175 gfc_error ("Missing right parenthesis or comma at %C");
2177 gfc_error ("Missing right parenthesis at %C");
2181 /* All tests passed. */
2184 if(m
== MATCH_ERROR
)
2185 gfc_current_locus
= where
;
2187 /* Return what we know from the test(s). */
2192 gfc_current_locus
= where
;
2198 match_char_kind (int * kind
, int * is_iso_c
)
2207 where
= gfc_current_locus
;
2209 n
= gfc_match_init_expr (&e
);
2211 if (n
!= MATCH_YES
&& gfc_matching_function
)
2213 /* The expression might include use-associated or imported
2214 parameters and try again after the specification
2217 gfc_undo_symbols ();
2222 gfc_error ("Expected initialization expression at %C");
2228 gfc_error ("Expected scalar initialization expression at %C");
2233 msg
= gfc_extract_int (e
, kind
);
2234 *is_iso_c
= e
->ts
.is_iso_c
;
2244 /* Ignore errors to this point, if we've gotten here. This means
2245 we ignore the m=MATCH_ERROR from above. */
2246 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2248 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2252 /* All tests passed. */
2255 if (m
== MATCH_ERROR
)
2256 gfc_current_locus
= where
;
2258 /* Return what we know from the test(s). */
2263 gfc_current_locus
= where
;
2268 /* Match the various kind/length specifications in a CHARACTER
2269 declaration. We don't return MATCH_NO. */
2272 gfc_match_char_spec (gfc_typespec
*ts
)
2274 int kind
, seen_length
, is_iso_c
;
2284 /* Try the old-style specification first. */
2285 old_char_selector
= 0;
2287 m
= match_char_length (&len
);
2291 old_char_selector
= 1;
2296 m
= gfc_match_char ('(');
2299 m
= MATCH_YES
; /* Character without length is a single char. */
2303 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2304 if (gfc_match (" kind =") == MATCH_YES
)
2306 m
= match_char_kind (&kind
, &is_iso_c
);
2308 if (m
== MATCH_ERROR
)
2313 if (gfc_match (" , len =") == MATCH_NO
)
2316 m
= char_len_param_value (&len
);
2319 if (m
== MATCH_ERROR
)
2326 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2327 if (gfc_match (" len =") == MATCH_YES
)
2329 m
= char_len_param_value (&len
);
2332 if (m
== MATCH_ERROR
)
2336 if (gfc_match_char (')') == MATCH_YES
)
2339 if (gfc_match (" , kind =") != MATCH_YES
)
2342 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2348 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2349 m
= char_len_param_value (&len
);
2352 if (m
== MATCH_ERROR
)
2356 m
= gfc_match_char (')');
2360 if (gfc_match_char (',') != MATCH_YES
)
2363 gfc_match (" kind ="); /* Gobble optional text. */
2365 m
= match_char_kind (&kind
, &is_iso_c
);
2366 if (m
== MATCH_ERROR
)
2372 /* Require a right-paren at this point. */
2373 m
= gfc_match_char (')');
2378 gfc_error ("Syntax error in CHARACTER declaration at %C");
2380 gfc_free_expr (len
);
2384 /* Deal with character functions after USE and IMPORT statements. */
2385 if (gfc_matching_function
)
2387 gfc_free_expr (len
);
2388 gfc_undo_symbols ();
2394 gfc_free_expr (len
);
2398 /* Do some final massaging of the length values. */
2399 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2401 if (seen_length
== 0)
2402 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2407 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2409 /* We have to know if it was a c interoperable kind so we can
2410 do accurate type checking of bind(c) procs, etc. */
2412 /* Mark this as c interoperable if being declared with one
2413 of the named constants from iso_c_binding. */
2414 ts
->is_c_interop
= is_iso_c
;
2415 else if (len
!= NULL
)
2416 /* Here, we might have parsed something such as: character(c_char)
2417 In this case, the parsing code above grabs the c_char when
2418 looking for the length (line 1690, roughly). it's the last
2419 testcase for parsing the kind params of a character variable.
2420 However, it's not actually the length. this seems like it
2422 To see if the user used a C interop kind, test the expr
2423 of the so called length, and see if it's C interoperable. */
2424 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2430 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2431 structure to the matched specification. This is necessary for FUNCTION and
2432 IMPLICIT statements.
2434 If implicit_flag is nonzero, then we don't check for the optional
2435 kind specification. Not doing so is needed for matching an IMPLICIT
2436 statement correctly. */
2439 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2441 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2445 bool seen_deferred_kind
, matched_type
;
2447 /* A belt and braces check that the typespec is correctly being treated
2448 as a deferred characteristic association. */
2449 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2450 && (gfc_current_block ()->result
->ts
.kind
== -1)
2451 && (ts
->kind
== -1);
2453 if (seen_deferred_kind
)
2456 /* Clear the current binding label, in case one is given. */
2457 curr_binding_label
[0] = '\0';
2459 if (gfc_match (" byte") == MATCH_YES
)
2461 if (gfc_notify_std (GFC_STD_GNU
, "Extension: BYTE type at %C")
2465 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2467 gfc_error ("BYTE type used at %C "
2468 "is not available on the target machine");
2472 ts
->type
= BT_INTEGER
;
2478 m
= gfc_match (" type ( %n", name
);
2479 matched_type
= (m
== MATCH_YES
);
2481 if ((matched_type
&& strcmp ("integer", name
) == 0)
2482 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2484 ts
->type
= BT_INTEGER
;
2485 ts
->kind
= gfc_default_integer_kind
;
2489 if ((matched_type
&& strcmp ("character", name
) == 0)
2490 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2493 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2494 "intrinsic-type-spec at %C") == FAILURE
)
2497 ts
->type
= BT_CHARACTER
;
2498 if (implicit_flag
== 0)
2499 m
= gfc_match_char_spec (ts
);
2503 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2509 if ((matched_type
&& strcmp ("real", name
) == 0)
2510 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2513 ts
->kind
= gfc_default_real_kind
;
2518 && (strcmp ("doubleprecision", name
) == 0
2519 || (strcmp ("double", name
) == 0
2520 && gfc_match (" precision") == MATCH_YES
)))
2521 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2524 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2525 "intrinsic-type-spec at %C") == FAILURE
)
2527 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2531 ts
->kind
= gfc_default_double_kind
;
2535 if ((matched_type
&& strcmp ("complex", name
) == 0)
2536 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2538 ts
->type
= BT_COMPLEX
;
2539 ts
->kind
= gfc_default_complex_kind
;
2544 && (strcmp ("doublecomplex", name
) == 0
2545 || (strcmp ("double", name
) == 0
2546 && gfc_match (" complex") == MATCH_YES
)))
2547 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2549 if (gfc_notify_std (GFC_STD_GNU
, "Extension: DOUBLE COMPLEX at %C")
2554 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2555 "intrinsic-type-spec at %C") == FAILURE
)
2558 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2561 ts
->type
= BT_COMPLEX
;
2562 ts
->kind
= gfc_default_double_kind
;
2566 if ((matched_type
&& strcmp ("logical", name
) == 0)
2567 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2569 ts
->type
= BT_LOGICAL
;
2570 ts
->kind
= gfc_default_logical_kind
;
2575 m
= gfc_match_char (')');
2578 ts
->type
= BT_DERIVED
;
2581 m
= gfc_match (" class ( %n )", name
);
2584 ts
->type
= BT_CLASS
;
2586 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: CLASS statement at %C")
2591 /* Defer association of the derived type until the end of the
2592 specification block. However, if the derived type can be
2593 found, add it to the typespec. */
2594 if (gfc_matching_function
)
2596 ts
->u
.derived
= NULL
;
2597 if (gfc_current_state () != COMP_INTERFACE
2598 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2599 ts
->u
.derived
= sym
;
2603 /* Search for the name but allow the components to be defined later. If
2604 type = -1, this typespec has been seen in a function declaration but
2605 the type could not be accessed at that point. */
2607 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2609 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2612 else if (ts
->kind
== -1)
2614 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2615 || gfc_current_ns
->has_import_set
;
2616 if (gfc_find_symbol (name
, NULL
, iface
, &sym
))
2618 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2627 if (sym
->attr
.flavor
!= FL_DERIVED
2628 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2631 gfc_set_sym_referenced (sym
);
2632 ts
->u
.derived
= sym
;
2638 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2639 "intrinsic-type-spec at %C") == FAILURE
)
2642 /* For all types except double, derived and character, look for an
2643 optional kind specifier. MATCH_NO is actually OK at this point. */
2644 if (implicit_flag
== 1)
2646 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2652 if (gfc_current_form
== FORM_FREE
)
2654 c
= gfc_peek_ascii_char ();
2655 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2656 && c
!= ':' && c
!= ',')
2658 if (matched_type
&& c
== ')')
2660 gfc_next_ascii_char ();
2667 m
= gfc_match_kind_spec (ts
, false);
2668 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2669 m
= gfc_match_old_kind_spec (ts
);
2671 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2674 /* Defer association of the KIND expression of function results
2675 until after USE and IMPORT statements. */
2676 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2677 || gfc_matching_function
)
2681 m
= MATCH_YES
; /* No kind specifier found. */
2687 /* Match an IMPLICIT NONE statement. Actually, this statement is
2688 already matched in parse.c, or we would not end up here in the
2689 first place. So the only thing we need to check, is if there is
2690 trailing garbage. If not, the match is successful. */
2693 gfc_match_implicit_none (void)
2695 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2699 /* Match the letter range(s) of an IMPLICIT statement. */
2702 match_implicit_range (void)
2708 cur_loc
= gfc_current_locus
;
2710 gfc_gobble_whitespace ();
2711 c
= gfc_next_ascii_char ();
2714 gfc_error ("Missing character range in IMPLICIT at %C");
2721 gfc_gobble_whitespace ();
2722 c1
= gfc_next_ascii_char ();
2726 gfc_gobble_whitespace ();
2727 c
= gfc_next_ascii_char ();
2732 inner
= 0; /* Fall through. */
2739 gfc_gobble_whitespace ();
2740 c2
= gfc_next_ascii_char ();
2744 gfc_gobble_whitespace ();
2745 c
= gfc_next_ascii_char ();
2747 if ((c
!= ',') && (c
!= ')'))
2760 gfc_error ("Letters must be in alphabetic order in "
2761 "IMPLICIT statement at %C");
2765 /* See if we can add the newly matched range to the pending
2766 implicits from this IMPLICIT statement. We do not check for
2767 conflicts with whatever earlier IMPLICIT statements may have
2768 set. This is done when we've successfully finished matching
2770 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2777 gfc_syntax_error (ST_IMPLICIT
);
2779 gfc_current_locus
= cur_loc
;
2784 /* Match an IMPLICIT statement, storing the types for
2785 gfc_set_implicit() if the statement is accepted by the parser.
2786 There is a strange looking, but legal syntactic construction
2787 possible. It looks like:
2789 IMPLICIT INTEGER (a-b) (c-d)
2791 This is legal if "a-b" is a constant expression that happens to
2792 equal one of the legal kinds for integers. The real problem
2793 happens with an implicit specification that looks like:
2795 IMPLICIT INTEGER (a-b)
2797 In this case, a typespec matcher that is "greedy" (as most of the
2798 matchers are) gobbles the character range as a kindspec, leaving
2799 nothing left. We therefore have to go a bit more slowly in the
2800 matching process by inhibiting the kindspec checking during
2801 typespec matching and checking for a kind later. */
2804 gfc_match_implicit (void)
2813 /* We don't allow empty implicit statements. */
2814 if (gfc_match_eos () == MATCH_YES
)
2816 gfc_error ("Empty IMPLICIT statement at %C");
2822 /* First cleanup. */
2823 gfc_clear_new_implicit ();
2825 /* A basic type is mandatory here. */
2826 m
= gfc_match_decl_type_spec (&ts
, 1);
2827 if (m
== MATCH_ERROR
)
2832 cur_loc
= gfc_current_locus
;
2833 m
= match_implicit_range ();
2837 /* We may have <TYPE> (<RANGE>). */
2838 gfc_gobble_whitespace ();
2839 c
= gfc_next_ascii_char ();
2840 if ((c
== '\n') || (c
== ','))
2842 /* Check for CHARACTER with no length parameter. */
2843 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
2845 ts
.kind
= gfc_default_character_kind
;
2846 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2847 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
2851 /* Record the Successful match. */
2852 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2857 gfc_current_locus
= cur_loc
;
2860 /* Discard the (incorrectly) matched range. */
2861 gfc_clear_new_implicit ();
2863 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2864 if (ts
.type
== BT_CHARACTER
)
2865 m
= gfc_match_char_spec (&ts
);
2868 m
= gfc_match_kind_spec (&ts
, false);
2871 m
= gfc_match_old_kind_spec (&ts
);
2872 if (m
== MATCH_ERROR
)
2878 if (m
== MATCH_ERROR
)
2881 m
= match_implicit_range ();
2882 if (m
== MATCH_ERROR
)
2887 gfc_gobble_whitespace ();
2888 c
= gfc_next_ascii_char ();
2889 if ((c
!= '\n') && (c
!= ','))
2892 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2900 gfc_syntax_error (ST_IMPLICIT
);
2908 gfc_match_import (void)
2910 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2915 if (gfc_current_ns
->proc_name
== NULL
2916 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2918 gfc_error ("IMPORT statement at %C only permitted in "
2919 "an INTERFACE body");
2923 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2927 if (gfc_match_eos () == MATCH_YES
)
2929 /* All host variables should be imported. */
2930 gfc_current_ns
->has_import_set
= 1;
2934 if (gfc_match (" ::") == MATCH_YES
)
2936 if (gfc_match_eos () == MATCH_YES
)
2938 gfc_error ("Expecting list of named entities at %C");
2945 m
= gfc_match (" %n", name
);
2949 if (gfc_current_ns
->parent
!= NULL
2950 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2952 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2955 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2956 && gfc_find_symbol (name
,
2957 gfc_current_ns
->proc_name
->ns
->parent
,
2960 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2966 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2967 "at %C - does not exist.", name
);
2971 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
2973 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2978 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, sym
->name
);
2981 sym
->attr
.imported
= 1;
2993 if (gfc_match_eos () == MATCH_YES
)
2995 if (gfc_match_char (',') != MATCH_YES
)
3002 gfc_error ("Syntax error in IMPORT statement at %C");
3007 /* A minimal implementation of gfc_match without whitespace, escape
3008 characters or variable arguments. Returns true if the next
3009 characters match the TARGET template exactly. */
3012 match_string_p (const char *target
)
3016 for (p
= target
; *p
; p
++)
3017 if ((char) gfc_next_ascii_char () != *p
)
3022 /* Matches an attribute specification including array specs. If
3023 successful, leaves the variables current_attr and current_as
3024 holding the specification. Also sets the colon_seen variable for
3025 later use by matchers associated with initializations.
3027 This subroutine is a little tricky in the sense that we don't know
3028 if we really have an attr-spec until we hit the double colon.
3029 Until that time, we can only return MATCH_NO. This forces us to
3030 check for duplicate specification at this level. */
3033 match_attr_spec (void)
3035 /* Modifiers that can exist in a type statement. */
3037 { GFC_DECL_BEGIN
= 0,
3038 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3039 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3040 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3041 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3042 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3043 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3047 /* GFC_DECL_END is the sentinel, index starts at 0. */
3048 #define NUM_DECL GFC_DECL_END
3050 locus start
, seen_at
[NUM_DECL
];
3057 gfc_clear_attr (¤t_attr
);
3058 start
= gfc_current_locus
;
3063 /* See if we get all of the keywords up to the final double colon. */
3064 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3072 gfc_gobble_whitespace ();
3074 ch
= gfc_next_ascii_char ();
3077 /* This is the successful exit condition for the loop. */
3078 if (gfc_next_ascii_char () == ':')
3083 gfc_gobble_whitespace ();
3084 switch (gfc_peek_ascii_char ())
3087 gfc_next_ascii_char ();
3088 switch (gfc_next_ascii_char ())
3091 if (match_string_p ("locatable"))
3093 /* Matched "allocatable". */
3094 d
= DECL_ALLOCATABLE
;
3099 if (match_string_p ("ynchronous"))
3101 /* Matched "asynchronous". */
3102 d
= DECL_ASYNCHRONOUS
;
3109 /* Try and match the bind(c). */
3110 m
= gfc_match_bind_c (NULL
, true);
3113 else if (m
== MATCH_ERROR
)
3118 gfc_next_ascii_char ();
3119 if ('o' != gfc_next_ascii_char ())
3121 switch (gfc_next_ascii_char ())
3124 if (match_string_p ("imension"))
3126 d
= DECL_CODIMENSION
;
3130 if (match_string_p ("tiguous"))
3132 d
= DECL_CONTIGUOUS
;
3139 if (match_string_p ("dimension"))
3144 if (match_string_p ("external"))
3149 if (match_string_p ("int"))
3151 ch
= gfc_next_ascii_char ();
3154 if (match_string_p ("nt"))
3156 /* Matched "intent". */
3157 /* TODO: Call match_intent_spec from here. */
3158 if (gfc_match (" ( in out )") == MATCH_YES
)
3160 else if (gfc_match (" ( in )") == MATCH_YES
)
3162 else if (gfc_match (" ( out )") == MATCH_YES
)
3168 if (match_string_p ("insic"))
3170 /* Matched "intrinsic". */
3178 if (match_string_p ("optional"))
3183 gfc_next_ascii_char ();
3184 switch (gfc_next_ascii_char ())
3187 if (match_string_p ("rameter"))
3189 /* Matched "parameter". */
3195 if (match_string_p ("inter"))
3197 /* Matched "pointer". */
3203 ch
= gfc_next_ascii_char ();
3206 if (match_string_p ("vate"))
3208 /* Matched "private". */
3214 if (match_string_p ("tected"))
3216 /* Matched "protected". */
3223 if (match_string_p ("blic"))
3225 /* Matched "public". */
3233 if (match_string_p ("save"))
3238 if (match_string_p ("target"))
3243 gfc_next_ascii_char ();
3244 ch
= gfc_next_ascii_char ();
3247 if (match_string_p ("lue"))
3249 /* Matched "value". */
3255 if (match_string_p ("latile"))
3257 /* Matched "volatile". */
3265 /* No double colon and no recognizable decl_type, so assume that
3266 we've been looking at something else the whole time. */
3273 /* Check to make sure any parens are paired up correctly. */
3274 if (gfc_match_parens () == MATCH_ERROR
)
3281 seen_at
[d
] = gfc_current_locus
;
3283 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3285 gfc_array_spec
*as
= NULL
;
3287 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3288 d
== DECL_CODIMENSION
);
3290 if (current_as
== NULL
)
3292 else if (m
== MATCH_YES
)
3294 merge_array_spec (as
, current_as
, false);
3300 if (d
== DECL_CODIMENSION
)
3301 gfc_error ("Missing codimension specification at %C");
3303 gfc_error ("Missing dimension specification at %C");
3307 if (m
== MATCH_ERROR
)
3312 /* Since we've seen a double colon, we have to be looking at an
3313 attr-spec. This means that we can now issue errors. */
3314 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3319 case DECL_ALLOCATABLE
:
3320 attr
= "ALLOCATABLE";
3322 case DECL_ASYNCHRONOUS
:
3323 attr
= "ASYNCHRONOUS";
3325 case DECL_CODIMENSION
:
3326 attr
= "CODIMENSION";
3328 case DECL_CONTIGUOUS
:
3329 attr
= "CONTIGUOUS";
3331 case DECL_DIMENSION
:
3338 attr
= "INTENT (IN)";
3341 attr
= "INTENT (OUT)";
3344 attr
= "INTENT (IN OUT)";
3346 case DECL_INTRINSIC
:
3352 case DECL_PARAMETER
:
3358 case DECL_PROTECTED
:
3373 case DECL_IS_BIND_C
:
3383 attr
= NULL
; /* This shouldn't happen. */
3386 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3391 /* Now that we've dealt with duplicate attributes, add the attributes
3392 to the current attribute. */
3393 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3398 if (gfc_current_state () == COMP_DERIVED
3399 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3400 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3401 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3403 if (d
== DECL_ALLOCATABLE
)
3405 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3406 "attribute at %C in a TYPE definition")
3415 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3422 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3423 && gfc_current_state () != COMP_MODULE
)
3425 if (d
== DECL_PRIVATE
)
3429 if (gfc_current_state () == COMP_DERIVED
3430 && gfc_state_stack
->previous
3431 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3433 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3434 "at %L in a TYPE definition", attr
,
3444 gfc_error ("%s attribute at %L is not allowed outside of the "
3445 "specification part of a module", attr
, &seen_at
[d
]);
3453 case DECL_ALLOCATABLE
:
3454 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3457 case DECL_ASYNCHRONOUS
:
3458 if (gfc_notify_std (GFC_STD_F2003
,
3459 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3463 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3466 case DECL_CODIMENSION
:
3467 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3470 case DECL_CONTIGUOUS
:
3471 if (gfc_notify_std (GFC_STD_F2008
,
3472 "Fortran 2008: CONTIGUOUS attribute at %C")
3476 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3479 case DECL_DIMENSION
:
3480 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3484 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3488 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3492 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3496 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3499 case DECL_INTRINSIC
:
3500 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3504 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3507 case DECL_PARAMETER
:
3508 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3512 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3515 case DECL_PROTECTED
:
3516 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3518 gfc_error ("PROTECTED at %C only allowed in specification "
3519 "part of a module");
3524 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3529 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3533 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3538 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3543 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3547 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3550 case DECL_IS_BIND_C
:
3551 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3555 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3560 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3564 if (gfc_notify_std (GFC_STD_F2003
,
3565 "Fortran 2003: VOLATILE attribute at %C")
3569 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3573 gfc_internal_error ("match_attr_spec(): Bad attribute");
3583 /* Module variables implicitly have the SAVE attribute. */
3584 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
)
3585 current_attr
.save
= SAVE_IMPLICIT
;
3591 gfc_current_locus
= start
;
3592 gfc_free_array_spec (current_as
);
3598 /* Set the binding label, dest_label, either with the binding label
3599 stored in the given gfc_typespec, ts, or if none was provided, it
3600 will be the symbol name in all lower case, as required by the draft
3601 (J3/04-007, section 15.4.1). If a binding label was given and
3602 there is more than one argument (num_idents), it is an error. */
3605 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3607 if (num_idents
> 1 && has_name_equals
)
3609 gfc_error ("Multiple identifiers provided with "
3610 "single NAME= specifier at %C");
3614 if (curr_binding_label
[0] != '\0')
3616 /* Binding label given; store in temp holder til have sym. */
3617 strcpy (dest_label
, curr_binding_label
);
3621 /* No binding label given, and the NAME= specifier did not exist,
3622 which means there was no NAME="". */
3623 if (sym_name
!= NULL
&& has_name_equals
== 0)
3624 strcpy (dest_label
, sym_name
);
3631 /* Set the status of the given common block as being BIND(C) or not,
3632 depending on the given parameter, is_bind_c. */
3635 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3637 com_block
->is_bind_c
= is_bind_c
;
3642 /* Verify that the given gfc_typespec is for a C interoperable type. */
3645 verify_c_interop (gfc_typespec
*ts
)
3647 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3648 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3649 ? SUCCESS
: FAILURE
;
3650 else if (ts
->is_c_interop
!= 1)
3657 /* Verify that the variables of a given common block, which has been
3658 defined with the attribute specifier bind(c), to be of a C
3659 interoperable type. Errors will be reported here, if
3663 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3665 gfc_symbol
*curr_sym
= NULL
;
3666 gfc_try retval
= SUCCESS
;
3668 curr_sym
= com_block
->head
;
3670 /* Make sure we have at least one symbol. */
3671 if (curr_sym
== NULL
)
3674 /* Here we know we have a symbol, so we'll execute this loop
3678 /* The second to last param, 1, says this is in a common block. */
3679 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3680 curr_sym
= curr_sym
->common_next
;
3681 } while (curr_sym
!= NULL
);
3687 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3688 an appropriate error message is reported. */
3691 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3692 int is_in_common
, gfc_common_head
*com_block
)
3694 bool bind_c_function
= false;
3695 gfc_try retval
= SUCCESS
;
3697 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3698 bind_c_function
= true;
3700 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3702 tmp_sym
= tmp_sym
->result
;
3703 /* Make sure it wasn't an implicitly typed result. */
3704 if (tmp_sym
->attr
.implicit_type
)
3706 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3707 "%L may not be C interoperable", tmp_sym
->name
,
3708 &tmp_sym
->declared_at
);
3709 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3710 /* Mark it as C interoperable to prevent duplicate warnings. */
3711 tmp_sym
->ts
.is_c_interop
= 1;
3712 tmp_sym
->attr
.is_c_interop
= 1;
3716 /* Here, we know we have the bind(c) attribute, so if we have
3717 enough type info, then verify that it's a C interop kind.
3718 The info could be in the symbol already, or possibly still in
3719 the given ts (current_ts), so look in both. */
3720 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3722 if (verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3724 /* See if we're dealing with a sym in a common block or not. */
3725 if (is_in_common
== 1)
3727 gfc_warning ("Variable '%s' in common block '%s' at %L "
3728 "may not be a C interoperable "
3729 "kind though common block '%s' is BIND(C)",
3730 tmp_sym
->name
, com_block
->name
,
3731 &(tmp_sym
->declared_at
), com_block
->name
);
3735 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3736 gfc_error ("Type declaration '%s' at %L is not C "
3737 "interoperable but it is BIND(C)",
3738 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3740 gfc_warning ("Variable '%s' at %L "
3741 "may not be a C interoperable "
3742 "kind but it is bind(c)",
3743 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3747 /* Variables declared w/in a common block can't be bind(c)
3748 since there's no way for C to see these variables, so there's
3749 semantically no reason for the attribute. */
3750 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3752 gfc_error ("Variable '%s' in common block '%s' at "
3753 "%L cannot be declared with BIND(C) "
3754 "since it is not a global",
3755 tmp_sym
->name
, com_block
->name
,
3756 &(tmp_sym
->declared_at
));
3760 /* Scalar variables that are bind(c) can not have the pointer
3761 or allocatable attributes. */
3762 if (tmp_sym
->attr
.is_bind_c
== 1)
3764 if (tmp_sym
->attr
.pointer
== 1)
3766 gfc_error ("Variable '%s' at %L cannot have both the "
3767 "POINTER and BIND(C) attributes",
3768 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3772 if (tmp_sym
->attr
.allocatable
== 1)
3774 gfc_error ("Variable '%s' at %L cannot have both the "
3775 "ALLOCATABLE and BIND(C) attributes",
3776 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3782 /* If it is a BIND(C) function, make sure the return value is a
3783 scalar value. The previous tests in this function made sure
3784 the type is interoperable. */
3785 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
3786 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3787 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3789 /* BIND(C) functions can not return a character string. */
3790 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
3791 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
3792 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
3793 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
3794 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3795 "be a character string", tmp_sym
->name
,
3796 &(tmp_sym
->declared_at
));
3799 /* See if the symbol has been marked as private. If it has, make sure
3800 there is no binding label and warn the user if there is one. */
3801 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3802 && tmp_sym
->binding_label
[0] != '\0')
3803 /* Use gfc_warning_now because we won't say that the symbol fails
3804 just because of this. */
3805 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3806 "given the binding label '%s'", tmp_sym
->name
,
3807 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3813 /* Set the appropriate fields for a symbol that's been declared as
3814 BIND(C) (the is_bind_c flag and the binding label), and verify that
3815 the type is C interoperable. Errors are reported by the functions
3816 used to set/test these fields. */
3819 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3821 gfc_try retval
= SUCCESS
;
3823 /* TODO: Do we need to make sure the vars aren't marked private? */
3825 /* Set the is_bind_c bit in symbol_attribute. */
3826 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3828 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3829 num_idents
) != SUCCESS
)
3836 /* Set the fields marking the given common block as BIND(C), including
3837 a binding label, and report any errors encountered. */
3840 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3842 gfc_try retval
= SUCCESS
;
3844 /* destLabel, common name, typespec (which may have binding label). */
3845 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3849 /* Set the given common block (com_block) to being bind(c) (1). */
3850 set_com_block_bind_c (com_block
, 1);
3856 /* Retrieve the list of one or more identifiers that the given bind(c)
3857 attribute applies to. */
3860 get_bind_c_idents (void)
3862 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3864 gfc_symbol
*tmp_sym
= NULL
;
3866 gfc_common_head
*com_block
= NULL
;
3868 if (gfc_match_name (name
) == MATCH_YES
)
3870 found_id
= MATCH_YES
;
3871 gfc_get_ha_symbol (name
, &tmp_sym
);
3873 else if (match_common_name (name
) == MATCH_YES
)
3875 found_id
= MATCH_YES
;
3876 com_block
= gfc_get_common (name
, 0);
3880 gfc_error ("Need either entity or common block name for "
3881 "attribute specification statement at %C");
3885 /* Save the current identifier and look for more. */
3888 /* Increment the number of identifiers found for this spec stmt. */
3891 /* Make sure we have a sym or com block, and verify that it can
3892 be bind(c). Set the appropriate field(s) and look for more
3894 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3896 if (tmp_sym
!= NULL
)
3898 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3904 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3909 /* Look to see if we have another identifier. */
3911 if (gfc_match_eos () == MATCH_YES
)
3912 found_id
= MATCH_NO
;
3913 else if (gfc_match_char (',') != MATCH_YES
)
3914 found_id
= MATCH_NO
;
3915 else if (gfc_match_name (name
) == MATCH_YES
)
3917 found_id
= MATCH_YES
;
3918 gfc_get_ha_symbol (name
, &tmp_sym
);
3920 else if (match_common_name (name
) == MATCH_YES
)
3922 found_id
= MATCH_YES
;
3923 com_block
= gfc_get_common (name
, 0);
3927 gfc_error ("Missing entity or common block name for "
3928 "attribute specification statement at %C");
3934 gfc_internal_error ("Missing symbol");
3936 } while (found_id
== MATCH_YES
);
3938 /* if we get here we were successful */
3943 /* Try and match a BIND(C) attribute specification statement. */
3946 gfc_match_bind_c_stmt (void)
3948 match found_match
= MATCH_NO
;
3953 /* This may not be necessary. */
3955 /* Clear the temporary binding label holder. */
3956 curr_binding_label
[0] = '\0';
3958 /* Look for the bind(c). */
3959 found_match
= gfc_match_bind_c (NULL
, true);
3961 if (found_match
== MATCH_YES
)
3963 /* Look for the :: now, but it is not required. */
3966 /* Get the identifier(s) that needs to be updated. This may need to
3967 change to hand the flag(s) for the attr specified so all identifiers
3968 found can have all appropriate parts updated (assuming that the same
3969 spec stmt can have multiple attrs, such as both bind(c) and
3971 if (get_bind_c_idents () != SUCCESS
)
3972 /* Error message should have printed already. */
3980 /* Match a data declaration statement. */
3983 gfc_match_data_decl (void)
3989 num_idents_on_line
= 0;
3991 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
3995 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
3996 && gfc_current_state () != COMP_DERIVED
)
3998 sym
= gfc_use_derived (current_ts
.u
.derived
);
4006 current_ts
.u
.derived
= sym
;
4009 m
= match_attr_spec ();
4010 if (m
== MATCH_ERROR
)
4016 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4017 && current_ts
.u
.derived
->components
== NULL
4018 && !current_ts
.u
.derived
->attr
.zero_comp
)
4021 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4024 gfc_find_symbol (current_ts
.u
.derived
->name
,
4025 current_ts
.u
.derived
->ns
->parent
, 1, &sym
);
4027 /* Any symbol that we find had better be a type definition
4028 which has its components defined. */
4029 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4030 && (current_ts
.u
.derived
->components
!= NULL
4031 || current_ts
.u
.derived
->attr
.zero_comp
))
4034 /* Now we have an error, which we signal, and then fix up
4035 because the knock-on is plain and simple confusing. */
4036 gfc_error_now ("Derived type at %C has not been previously defined "
4037 "and so cannot appear in a derived type definition");
4038 current_attr
.pointer
= 1;
4043 /* If we have an old-style character declaration, and no new-style
4044 attribute specifications, then there a comma is optional between
4045 the type specification and the variable list. */
4046 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4047 gfc_match_char (',');
4049 /* Give the types/attributes to symbols that follow. Give the element
4050 a number so that repeat character length expressions can be copied. */
4054 num_idents_on_line
++;
4055 m
= variable_decl (elem
++);
4056 if (m
== MATCH_ERROR
)
4061 if (gfc_match_eos () == MATCH_YES
)
4063 if (gfc_match_char (',') != MATCH_YES
)
4067 if (gfc_error_flag_test () == 0)
4068 gfc_error ("Syntax error in data declaration at %C");
4071 gfc_free_data_all (gfc_current_ns
);
4074 gfc_free_array_spec (current_as
);
4080 /* Match a prefix associated with a function or subroutine
4081 declaration. If the typespec pointer is nonnull, then a typespec
4082 can be matched. Note that if nothing matches, MATCH_YES is
4083 returned (the null string was matched). */
4086 gfc_match_prefix (gfc_typespec
*ts
)
4092 gfc_clear_attr (¤t_attr
);
4094 seen_impure
= false;
4096 gcc_assert (!gfc_matching_prefix
);
4097 gfc_matching_prefix
= true;
4101 found_prefix
= false;
4103 if (!seen_type
&& ts
!= NULL
4104 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4105 && gfc_match_space () == MATCH_YES
)
4109 found_prefix
= true;
4112 if (gfc_match ("elemental% ") == MATCH_YES
)
4114 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
4117 found_prefix
= true;
4120 if (gfc_match ("pure% ") == MATCH_YES
)
4122 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4125 found_prefix
= true;
4128 if (gfc_match ("recursive% ") == MATCH_YES
)
4130 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
4133 found_prefix
= true;
4136 /* IMPURE is a somewhat special case, as it needs not set an actual
4137 attribute but rather only prevents ELEMENTAL routines from being
4138 automatically PURE. */
4139 if (gfc_match ("impure% ") == MATCH_YES
)
4141 if (gfc_notify_std (GFC_STD_F2008
,
4142 "Fortran 2008: IMPURE procedure at %C")
4147 found_prefix
= true;
4150 while (found_prefix
);
4152 /* IMPURE and PURE must not both appear, of course. */
4153 if (seen_impure
&& current_attr
.pure
)
4155 gfc_error ("PURE and IMPURE must not appear both at %C");
4159 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4160 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4162 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4166 /* At this point, the next item is not a prefix. */
4167 gcc_assert (gfc_matching_prefix
);
4168 gfc_matching_prefix
= false;
4172 gcc_assert (gfc_matching_prefix
);
4173 gfc_matching_prefix
= false;
4178 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4181 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4183 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
4186 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
4189 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
4196 /* Match a formal argument list. */
4199 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4201 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4202 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4208 if (gfc_match_char ('(') != MATCH_YES
)
4215 if (gfc_match_char (')') == MATCH_YES
)
4220 if (gfc_match_char ('*') == MATCH_YES
)
4224 m
= gfc_match_name (name
);
4228 if (gfc_get_symbol (name
, NULL
, &sym
))
4232 p
= gfc_get_formal_arglist ();
4244 /* We don't add the VARIABLE flavor because the name could be a
4245 dummy procedure. We don't apply these attributes to formal
4246 arguments of statement functions. */
4247 if (sym
!= NULL
&& !st_flag
4248 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
4249 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
4255 /* The name of a program unit can be in a different namespace,
4256 so check for it explicitly. After the statement is accepted,
4257 the name is checked for especially in gfc_get_symbol(). */
4258 if (gfc_new_block
!= NULL
&& sym
!= NULL
4259 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4261 gfc_error ("Name '%s' at %C is the name of the procedure",
4267 if (gfc_match_char (')') == MATCH_YES
)
4270 m
= gfc_match_char (',');
4273 gfc_error ("Unexpected junk in formal argument list at %C");
4279 /* Check for duplicate symbols in the formal argument list. */
4282 for (p
= head
; p
->next
; p
= p
->next
)
4287 for (q
= p
->next
; q
; q
= q
->next
)
4288 if (p
->sym
== q
->sym
)
4290 gfc_error ("Duplicate symbol '%s' in formal argument list "
4291 "at %C", p
->sym
->name
);
4299 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
4309 gfc_free_formal_arglist (head
);
4314 /* Match a RESULT specification following a function declaration or
4315 ENTRY statement. Also matches the end-of-statement. */
4318 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4320 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4324 if (gfc_match (" result (") != MATCH_YES
)
4327 m
= gfc_match_name (name
);
4331 /* Get the right paren, and that's it because there could be the
4332 bind(c) attribute after the result clause. */
4333 if (gfc_match_char(')') != MATCH_YES
)
4335 /* TODO: should report the missing right paren here. */
4339 if (strcmp (function
->name
, name
) == 0)
4341 gfc_error ("RESULT variable at %C must be different than function name");
4345 if (gfc_get_symbol (name
, NULL
, &r
))
4348 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
4357 /* Match a function suffix, which could be a combination of a result
4358 clause and BIND(C), either one, or neither. The draft does not
4359 require them to come in a specific order. */
4362 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4364 match is_bind_c
; /* Found bind(c). */
4365 match is_result
; /* Found result clause. */
4366 match found_match
; /* Status of whether we've found a good match. */
4367 char peek_char
; /* Character we're going to peek at. */
4368 bool allow_binding_name
;
4370 /* Initialize to having found nothing. */
4371 found_match
= MATCH_NO
;
4372 is_bind_c
= MATCH_NO
;
4373 is_result
= MATCH_NO
;
4375 /* Get the next char to narrow between result and bind(c). */
4376 gfc_gobble_whitespace ();
4377 peek_char
= gfc_peek_ascii_char ();
4379 /* C binding names are not allowed for internal procedures. */
4380 if (gfc_current_state () == COMP_CONTAINS
4381 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4382 allow_binding_name
= false;
4384 allow_binding_name
= true;
4389 /* Look for result clause. */
4390 is_result
= match_result (sym
, result
);
4391 if (is_result
== MATCH_YES
)
4393 /* Now see if there is a bind(c) after it. */
4394 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4395 /* We've found the result clause and possibly bind(c). */
4396 found_match
= MATCH_YES
;
4399 /* This should only be MATCH_ERROR. */
4400 found_match
= is_result
;
4403 /* Look for bind(c) first. */
4404 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4405 if (is_bind_c
== MATCH_YES
)
4407 /* Now see if a result clause followed it. */
4408 is_result
= match_result (sym
, result
);
4409 found_match
= MATCH_YES
;
4413 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4414 found_match
= MATCH_ERROR
;
4418 gfc_error ("Unexpected junk after function declaration at %C");
4419 found_match
= MATCH_ERROR
;
4423 if (is_bind_c
== MATCH_YES
)
4425 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4426 if (gfc_current_state () == COMP_CONTAINS
4427 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4428 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4429 "at %L may not be specified for an internal "
4430 "procedure", &gfc_current_locus
)
4434 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4443 /* Procedure pointer return value without RESULT statement:
4444 Add "hidden" result variable named "ppr@". */
4447 add_hidden_procptr_result (gfc_symbol
*sym
)
4451 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4454 /* First usage case: PROCEDURE and EXTERNAL statements. */
4455 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4456 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4457 && sym
->attr
.external
;
4458 /* Second usage case: INTERFACE statements. */
4459 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4460 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4461 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4467 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4471 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4472 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4473 st2
->n
.sym
= stree
->n
.sym
;
4475 sym
->result
= stree
->n
.sym
;
4477 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4478 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4479 sym
->result
->attr
.external
= sym
->attr
.external
;
4480 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4481 sym
->result
->ts
= sym
->ts
;
4482 sym
->attr
.proc_pointer
= 0;
4483 sym
->attr
.pointer
= 0;
4484 sym
->attr
.external
= 0;
4485 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4487 sym
->result
->attr
.pointer
= 0;
4488 sym
->result
->attr
.proc_pointer
= 1;
4491 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4493 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4494 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4495 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4496 && sym
== gfc_current_ns
->proc_name
4497 && sym
== sym
->result
->ns
->proc_name
4498 && strcmp ("ppr@", sym
->result
->name
) == 0)
4500 sym
->result
->attr
.proc_pointer
= 1;
4501 sym
->attr
.pointer
= 0;
4509 /* Match the interface for a PROCEDURE declaration,
4510 including brackets (R1212). */
4513 match_procedure_interface (gfc_symbol
**proc_if
)
4517 locus old_loc
, entry_loc
;
4518 gfc_namespace
*old_ns
= gfc_current_ns
;
4519 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4521 old_loc
= entry_loc
= gfc_current_locus
;
4522 gfc_clear_ts (¤t_ts
);
4524 if (gfc_match (" (") != MATCH_YES
)
4526 gfc_current_locus
= entry_loc
;
4530 /* Get the type spec. for the procedure interface. */
4531 old_loc
= gfc_current_locus
;
4532 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4533 gfc_gobble_whitespace ();
4534 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4537 if (m
== MATCH_ERROR
)
4540 /* Procedure interface is itself a procedure. */
4541 gfc_current_locus
= old_loc
;
4542 m
= gfc_match_name (name
);
4544 /* First look to see if it is already accessible in the current
4545 namespace because it is use associated or contained. */
4547 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4550 /* If it is still not found, then try the parent namespace, if it
4551 exists and create the symbol there if it is still not found. */
4552 if (gfc_current_ns
->parent
)
4553 gfc_current_ns
= gfc_current_ns
->parent
;
4554 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4557 gfc_current_ns
= old_ns
;
4558 *proc_if
= st
->n
.sym
;
4560 /* Various interface checks. */
4564 /* Resolve interface if possible. That way, attr.procedure is only set
4565 if it is declared by a later procedure-declaration-stmt, which is
4566 invalid per C1212. */
4567 while ((*proc_if
)->ts
.interface
)
4568 *proc_if
= (*proc_if
)->ts
.interface
;
4570 if ((*proc_if
)->generic
)
4572 gfc_error ("Interface '%s' at %C may not be generic",
4576 if ((*proc_if
)->attr
.proc
== PROC_ST_FUNCTION
)
4578 gfc_error ("Interface '%s' at %C may not be a statement function",
4582 /* Handle intrinsic procedures. */
4583 if (!((*proc_if
)->attr
.external
|| (*proc_if
)->attr
.use_assoc
4584 || (*proc_if
)->attr
.if_source
== IFSRC_IFBODY
)
4585 && (gfc_is_intrinsic ((*proc_if
), 0, gfc_current_locus
)
4586 || gfc_is_intrinsic ((*proc_if
), 1, gfc_current_locus
)))
4587 (*proc_if
)->attr
.intrinsic
= 1;
4588 if ((*proc_if
)->attr
.intrinsic
4589 && !gfc_intrinsic_actual_ok ((*proc_if
)->name
, 0))
4591 gfc_error ("Intrinsic procedure '%s' not allowed "
4592 "in PROCEDURE statement at %C", (*proc_if
)->name
);
4598 if (gfc_match (" )") != MATCH_YES
)
4600 gfc_current_locus
= entry_loc
;
4608 /* Match a PROCEDURE declaration (R1211). */
4611 match_procedure_decl (void)
4614 gfc_symbol
*sym
, *proc_if
= NULL
;
4616 gfc_expr
*initializer
= NULL
;
4618 /* Parse interface (with brackets). */
4619 m
= match_procedure_interface (&proc_if
);
4623 /* Parse attributes (with colons). */
4624 m
= match_attr_spec();
4625 if (m
== MATCH_ERROR
)
4628 /* Get procedure symbols. */
4631 m
= gfc_match_symbol (&sym
, 0);
4634 else if (m
== MATCH_ERROR
)
4637 /* Add current_attr to the symbol attributes. */
4638 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4641 if (sym
->attr
.is_bind_c
)
4643 /* Check for C1218. */
4644 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4646 gfc_error ("BIND(C) attribute at %C requires "
4647 "an interface with BIND(C)");
4650 /* Check for C1217. */
4651 if (has_name_equals
&& sym
->attr
.pointer
)
4653 gfc_error ("BIND(C) procedure with NAME may not have "
4654 "POINTER attribute at %C");
4657 if (has_name_equals
&& sym
->attr
.dummy
)
4659 gfc_error ("Dummy procedure at %C may not have "
4660 "BIND(C) attribute with NAME");
4663 /* Set binding label for BIND(C). */
4664 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4668 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4671 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4674 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4677 /* Set interface. */
4678 if (proc_if
!= NULL
)
4680 if (sym
->ts
.type
!= BT_UNKNOWN
)
4682 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4683 sym
->name
, &gfc_current_locus
,
4684 gfc_basic_typename (sym
->ts
.type
));
4687 sym
->ts
.interface
= proc_if
;
4688 sym
->attr
.untyped
= 1;
4689 sym
->attr
.if_source
= IFSRC_IFBODY
;
4691 else if (current_ts
.type
!= BT_UNKNOWN
)
4693 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4695 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4696 sym
->ts
.interface
->ts
= current_ts
;
4697 sym
->ts
.interface
->attr
.function
= 1;
4698 sym
->attr
.function
= sym
->ts
.interface
->attr
.function
;
4699 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4702 if (gfc_match (" =>") == MATCH_YES
)
4704 if (!current_attr
.pointer
)
4706 gfc_error ("Initialization at %C isn't for a pointer variable");
4711 m
= match_pointer_init (&initializer
, 1);
4715 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4721 gfc_set_sym_referenced (sym
);
4723 if (gfc_match_eos () == MATCH_YES
)
4725 if (gfc_match_char (',') != MATCH_YES
)
4730 gfc_error ("Syntax error in PROCEDURE statement at %C");
4734 /* Free stuff up and return. */
4735 gfc_free_expr (initializer
);
4741 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4744 /* Match a procedure pointer component declaration (R445). */
4747 match_ppc_decl (void)
4750 gfc_symbol
*proc_if
= NULL
;
4754 gfc_expr
*initializer
= NULL
;
4755 gfc_typebound_proc
* tb
;
4756 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4758 /* Parse interface (with brackets). */
4759 m
= match_procedure_interface (&proc_if
);
4763 /* Parse attributes. */
4764 tb
= XCNEW (gfc_typebound_proc
);
4765 tb
->where
= gfc_current_locus
;
4766 m
= match_binding_attributes (tb
, false, true);
4767 if (m
== MATCH_ERROR
)
4770 gfc_clear_attr (¤t_attr
);
4771 current_attr
.procedure
= 1;
4772 current_attr
.proc_pointer
= 1;
4773 current_attr
.access
= tb
->access
;
4774 current_attr
.flavor
= FL_PROCEDURE
;
4776 /* Match the colons (required). */
4777 if (gfc_match (" ::") != MATCH_YES
)
4779 gfc_error ("Expected '::' after binding-attributes at %C");
4783 /* Check for C450. */
4784 if (!tb
->nopass
&& proc_if
== NULL
)
4786 gfc_error("NOPASS or explicit interface required at %C");
4790 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure pointer "
4791 "component at %C") == FAILURE
)
4794 /* Match PPC names. */
4798 m
= gfc_match_name (name
);
4801 else if (m
== MATCH_ERROR
)
4804 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
4807 /* Add current_attr to the symbol attributes. */
4808 if (gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4811 if (gfc_add_external (&c
->attr
, NULL
) == FAILURE
)
4814 if (gfc_add_proc (&c
->attr
, name
, NULL
) == FAILURE
)
4819 /* Set interface. */
4820 if (proc_if
!= NULL
)
4822 c
->ts
.interface
= proc_if
;
4823 c
->attr
.untyped
= 1;
4824 c
->attr
.if_source
= IFSRC_IFBODY
;
4826 else if (ts
.type
!= BT_UNKNOWN
)
4829 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4830 c
->ts
.interface
->ts
= ts
;
4831 c
->ts
.interface
->attr
.function
= 1;
4832 c
->attr
.function
= c
->ts
.interface
->attr
.function
;
4833 c
->attr
.if_source
= IFSRC_UNKNOWN
;
4836 if (gfc_match (" =>") == MATCH_YES
)
4838 m
= match_pointer_init (&initializer
, 1);
4841 gfc_free_expr (initializer
);
4844 c
->initializer
= initializer
;
4847 if (gfc_match_eos () == MATCH_YES
)
4849 if (gfc_match_char (',') != MATCH_YES
)
4854 gfc_error ("Syntax error in procedure pointer component at %C");
4859 /* Match a PROCEDURE declaration inside an interface (R1206). */
4862 match_procedure_in_interface (void)
4866 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4868 if (current_interface
.type
== INTERFACE_NAMELESS
4869 || current_interface
.type
== INTERFACE_ABSTRACT
)
4871 gfc_error ("PROCEDURE at %C must be in a generic interface");
4877 m
= gfc_match_name (name
);
4880 else if (m
== MATCH_ERROR
)
4882 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4885 if (gfc_add_interface (sym
) == FAILURE
)
4888 if (gfc_match_eos () == MATCH_YES
)
4890 if (gfc_match_char (',') != MATCH_YES
)
4897 gfc_error ("Syntax error in PROCEDURE statement at %C");
4902 /* General matcher for PROCEDURE declarations. */
4904 static match
match_procedure_in_type (void);
4907 gfc_match_procedure (void)
4911 switch (gfc_current_state ())
4916 case COMP_SUBROUTINE
:
4918 m
= match_procedure_decl ();
4920 case COMP_INTERFACE
:
4921 m
= match_procedure_in_interface ();
4924 m
= match_ppc_decl ();
4926 case COMP_DERIVED_CONTAINS
:
4927 m
= match_procedure_in_type ();
4936 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4944 /* Warn if a matched procedure has the same name as an intrinsic; this is
4945 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4946 parser-state-stack to find out whether we're in a module. */
4949 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
4953 in_module
= (gfc_state_stack
->previous
4954 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
4956 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
4960 /* Match a function declaration. */
4963 gfc_match_function_decl (void)
4965 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4966 gfc_symbol
*sym
, *result
;
4970 match found_match
; /* Status returned by match func. */
4972 if (gfc_current_state () != COMP_NONE
4973 && gfc_current_state () != COMP_INTERFACE
4974 && gfc_current_state () != COMP_CONTAINS
)
4977 gfc_clear_ts (¤t_ts
);
4979 old_loc
= gfc_current_locus
;
4981 m
= gfc_match_prefix (¤t_ts
);
4984 gfc_current_locus
= old_loc
;
4988 if (gfc_match ("function% %n", name
) != MATCH_YES
)
4990 gfc_current_locus
= old_loc
;
4993 if (get_proc_name (name
, &sym
, false))
4996 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4999 gfc_new_block
= sym
;
5001 m
= gfc_match_formal_arglist (sym
, 0, 0);
5004 gfc_error ("Expected formal argument list in function "
5005 "definition at %C");
5009 else if (m
== MATCH_ERROR
)
5014 /* According to the draft, the bind(c) and result clause can
5015 come in either order after the formal_arg_list (i.e., either
5016 can be first, both can exist together or by themselves or neither
5017 one). Therefore, the match_result can't match the end of the
5018 string, and check for the bind(c) or result clause in either order. */
5019 found_match
= gfc_match_eos ();
5021 /* Make sure that it isn't already declared as BIND(C). If it is, it
5022 must have been marked BIND(C) with a BIND(C) attribute and that is
5023 not allowed for procedures. */
5024 if (sym
->attr
.is_bind_c
== 1)
5026 sym
->attr
.is_bind_c
= 0;
5027 if (sym
->old_symbol
!= NULL
)
5028 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5029 "variables or common blocks",
5030 &(sym
->old_symbol
->declared_at
));
5032 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5033 "variables or common blocks", &gfc_current_locus
);
5036 if (found_match
!= MATCH_YES
)
5038 /* If we haven't found the end-of-statement, look for a suffix. */
5039 suffix_match
= gfc_match_suffix (sym
, &result
);
5040 if (suffix_match
== MATCH_YES
)
5041 /* Need to get the eos now. */
5042 found_match
= gfc_match_eos ();
5044 found_match
= suffix_match
;
5047 if(found_match
!= MATCH_YES
)
5051 /* Make changes to the symbol. */
5054 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5057 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
5058 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5061 /* Delay matching the function characteristics until after the
5062 specification block by signalling kind=-1. */
5063 sym
->declared_at
= old_loc
;
5064 if (current_ts
.type
!= BT_UNKNOWN
)
5065 current_ts
.kind
= -1;
5067 current_ts
.kind
= 0;
5071 if (current_ts
.type
!= BT_UNKNOWN
5072 && gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
5078 if (current_ts
.type
!= BT_UNKNOWN
5079 && gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
)
5082 sym
->result
= result
;
5085 /* Warn if this procedure has the same name as an intrinsic. */
5086 warn_intrinsic_shadow (sym
, true);
5092 gfc_current_locus
= old_loc
;
5097 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5098 pass the name of the entry, rather than the gfc_current_block name, and
5099 to return false upon finding an existing global entry. */
5102 add_global_entry (const char *name
, int sub
)
5105 enum gfc_symbol_type type
;
5107 s
= gfc_get_gsymbol(name
);
5108 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5111 || (s
->type
!= GSYM_UNKNOWN
5112 && s
->type
!= type
))
5113 gfc_global_used(s
, NULL
);
5117 s
->where
= gfc_current_locus
;
5119 s
->ns
= gfc_current_ns
;
5126 /* Match an ENTRY statement. */
5129 gfc_match_entry (void)
5134 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5135 gfc_compile_state state
;
5139 bool module_procedure
;
5143 m
= gfc_match_name (name
);
5147 if (gfc_notify_std (GFC_STD_F2008_OBS
, "Fortran 2008 obsolescent feature: "
5148 "ENTRY statement at %C") == FAILURE
)
5151 state
= gfc_current_state ();
5152 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5157 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5160 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5162 case COMP_BLOCK_DATA
:
5163 gfc_error ("ENTRY statement at %C cannot appear within "
5166 case COMP_INTERFACE
:
5167 gfc_error ("ENTRY statement at %C cannot appear within "
5171 gfc_error ("ENTRY statement at %C cannot appear within "
5172 "a DERIVED TYPE block");
5175 gfc_error ("ENTRY statement at %C cannot appear within "
5176 "an IF-THEN block");
5179 gfc_error ("ENTRY statement at %C cannot appear within "
5183 gfc_error ("ENTRY statement at %C cannot appear within "
5187 gfc_error ("ENTRY statement at %C cannot appear within "
5191 gfc_error ("ENTRY statement at %C cannot appear within "
5195 gfc_error ("ENTRY statement at %C cannot appear within "
5196 "a contained subprogram");
5199 gfc_internal_error ("gfc_match_entry(): Bad state");
5204 module_procedure
= gfc_current_ns
->parent
!= NULL
5205 && gfc_current_ns
->parent
->proc_name
5206 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5209 if (gfc_current_ns
->parent
!= NULL
5210 && gfc_current_ns
->parent
->proc_name
5211 && !module_procedure
)
5213 gfc_error("ENTRY statement at %C cannot appear in a "
5214 "contained procedure");
5218 /* Module function entries need special care in get_proc_name
5219 because previous references within the function will have
5220 created symbols attached to the current namespace. */
5221 if (get_proc_name (name
, &entry
,
5222 gfc_current_ns
->parent
!= NULL
5223 && module_procedure
))
5226 proc
= gfc_current_block ();
5228 /* Make sure that it isn't already declared as BIND(C). If it is, it
5229 must have been marked BIND(C) with a BIND(C) attribute and that is
5230 not allowed for procedures. */
5231 if (entry
->attr
.is_bind_c
== 1)
5233 entry
->attr
.is_bind_c
= 0;
5234 if (entry
->old_symbol
!= NULL
)
5235 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5236 "variables or common blocks",
5237 &(entry
->old_symbol
->declared_at
));
5239 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5240 "variables or common blocks", &gfc_current_locus
);
5243 /* Check what next non-whitespace character is so we can tell if there
5244 is the required parens if we have a BIND(C). */
5245 gfc_gobble_whitespace ();
5246 peek_char
= gfc_peek_ascii_char ();
5248 if (state
== COMP_SUBROUTINE
)
5250 /* An entry in a subroutine. */
5251 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
5254 m
= gfc_match_formal_arglist (entry
, 0, 1);
5258 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5259 never be an internal procedure. */
5260 is_bind_c
= gfc_match_bind_c (entry
, true);
5261 if (is_bind_c
== MATCH_ERROR
)
5263 if (is_bind_c
== MATCH_YES
)
5265 if (peek_char
!= '(')
5267 gfc_error ("Missing required parentheses before BIND(C) at %C");
5270 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
5275 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5276 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5281 /* An entry in a function.
5282 We need to take special care because writing
5287 ENTRY f() RESULT (r)
5289 ENTRY f RESULT (r). */
5290 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
5293 old_loc
= gfc_current_locus
;
5294 if (gfc_match_eos () == MATCH_YES
)
5296 gfc_current_locus
= old_loc
;
5297 /* Match the empty argument list, and add the interface to
5299 m
= gfc_match_formal_arglist (entry
, 0, 1);
5302 m
= gfc_match_formal_arglist (entry
, 0, 0);
5309 if (gfc_match_eos () == MATCH_YES
)
5311 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5312 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5315 entry
->result
= entry
;
5319 m
= gfc_match_suffix (entry
, &result
);
5321 gfc_syntax_error (ST_ENTRY
);
5327 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
5328 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
5329 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
5332 entry
->result
= result
;
5336 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5337 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5339 entry
->result
= entry
;
5344 if (gfc_match_eos () != MATCH_YES
)
5346 gfc_syntax_error (ST_ENTRY
);
5350 entry
->attr
.recursive
= proc
->attr
.recursive
;
5351 entry
->attr
.elemental
= proc
->attr
.elemental
;
5352 entry
->attr
.pure
= proc
->attr
.pure
;
5354 el
= gfc_get_entry_list ();
5356 el
->next
= gfc_current_ns
->entries
;
5357 gfc_current_ns
->entries
= el
;
5359 el
->id
= el
->next
->id
+ 1;
5363 new_st
.op
= EXEC_ENTRY
;
5364 new_st
.ext
.entry
= el
;
5370 /* Match a subroutine statement, including optional prefixes. */
5373 gfc_match_subroutine (void)
5375 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5380 bool allow_binding_name
;
5382 if (gfc_current_state () != COMP_NONE
5383 && gfc_current_state () != COMP_INTERFACE
5384 && gfc_current_state () != COMP_CONTAINS
)
5387 m
= gfc_match_prefix (NULL
);
5391 m
= gfc_match ("subroutine% %n", name
);
5395 if (get_proc_name (name
, &sym
, false))
5398 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5399 the symbol existed before. */
5400 sym
->declared_at
= gfc_current_locus
;
5402 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5405 gfc_new_block
= sym
;
5407 /* Check what next non-whitespace character is so we can tell if there
5408 is the required parens if we have a BIND(C). */
5409 gfc_gobble_whitespace ();
5410 peek_char
= gfc_peek_ascii_char ();
5412 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5415 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5418 /* Make sure that it isn't already declared as BIND(C). If it is, it
5419 must have been marked BIND(C) with a BIND(C) attribute and that is
5420 not allowed for procedures. */
5421 if (sym
->attr
.is_bind_c
== 1)
5423 sym
->attr
.is_bind_c
= 0;
5424 if (sym
->old_symbol
!= NULL
)
5425 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5426 "variables or common blocks",
5427 &(sym
->old_symbol
->declared_at
));
5429 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5430 "variables or common blocks", &gfc_current_locus
);
5433 /* C binding names are not allowed for internal procedures. */
5434 if (gfc_current_state () == COMP_CONTAINS
5435 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5436 allow_binding_name
= false;
5438 allow_binding_name
= true;
5440 /* Here, we are just checking if it has the bind(c) attribute, and if
5441 so, then we need to make sure it's all correct. If it doesn't,
5442 we still need to continue matching the rest of the subroutine line. */
5443 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5444 if (is_bind_c
== MATCH_ERROR
)
5446 /* There was an attempt at the bind(c), but it was wrong. An
5447 error message should have been printed w/in the gfc_match_bind_c
5448 so here we'll just return the MATCH_ERROR. */
5452 if (is_bind_c
== MATCH_YES
)
5454 /* The following is allowed in the Fortran 2008 draft. */
5455 if (gfc_current_state () == COMP_CONTAINS
5456 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5457 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
5458 "at %L may not be specified for an internal "
5459 "procedure", &gfc_current_locus
)
5463 if (peek_char
!= '(')
5465 gfc_error ("Missing required parentheses before BIND(C) at %C");
5468 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
5473 if (gfc_match_eos () != MATCH_YES
)
5475 gfc_syntax_error (ST_SUBROUTINE
);
5479 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5482 /* Warn if it has the same name as an intrinsic. */
5483 warn_intrinsic_shadow (sym
, false);
5489 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5490 given, and set the binding label in either the given symbol (if not
5491 NULL), or in the current_ts. The symbol may be NULL because we may
5492 encounter the BIND(C) before the declaration itself. Return
5493 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5494 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5495 or MATCH_YES if the specifier was correct and the binding label and
5496 bind(c) fields were set correctly for the given symbol or the
5497 current_ts. If allow_binding_name is false, no binding name may be
5501 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5503 /* binding label, if exists */
5504 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
5508 /* Initialize the flag that specifies whether we encountered a NAME=
5509 specifier or not. */
5510 has_name_equals
= 0;
5512 /* Init the first char to nil so we can catch if we don't have
5513 the label (name attr) or the symbol name yet. */
5514 binding_label
[0] = '\0';
5516 /* This much we have to be able to match, in this order, if
5517 there is a bind(c) label. */
5518 if (gfc_match (" bind ( c ") != MATCH_YES
)
5521 /* Now see if there is a binding label, or if we've reached the
5522 end of the bind(c) attribute without one. */
5523 if (gfc_match_char (',') == MATCH_YES
)
5525 if (gfc_match (" name = ") != MATCH_YES
)
5527 gfc_error ("Syntax error in NAME= specifier for binding label "
5529 /* should give an error message here */
5533 has_name_equals
= 1;
5535 /* Get the opening quote. */
5536 double_quote
= MATCH_YES
;
5537 single_quote
= MATCH_YES
;
5538 double_quote
= gfc_match_char ('"');
5539 if (double_quote
!= MATCH_YES
)
5540 single_quote
= gfc_match_char ('\'');
5541 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5543 gfc_error ("Syntax error in NAME= specifier for binding label "
5548 /* Grab the binding label, using functions that will not lower
5549 case the names automatically. */
5550 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
5553 /* Get the closing quotation. */
5554 if (double_quote
== MATCH_YES
)
5556 if (gfc_match_char ('"') != MATCH_YES
)
5558 gfc_error ("Missing closing quote '\"' for binding label at %C");
5559 /* User started string with '"' so looked to match it. */
5565 if (gfc_match_char ('\'') != MATCH_YES
)
5567 gfc_error ("Missing closing quote '\'' for binding label at %C");
5568 /* User started string with "'" char. */
5574 /* Get the required right paren. */
5575 if (gfc_match_char (')') != MATCH_YES
)
5577 gfc_error ("Missing closing paren for binding label at %C");
5581 if (has_name_equals
&& !allow_binding_name
)
5583 gfc_error ("No binding name is allowed in BIND(C) at %C");
5587 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5589 gfc_error ("For dummy procedure %s, no binding name is "
5590 "allowed in BIND(C) at %C", sym
->name
);
5595 /* Save the binding label to the symbol. If sym is null, we're
5596 probably matching the typespec attributes of a declaration and
5597 haven't gotten the name yet, and therefore, no symbol yet. */
5598 if (binding_label
[0] != '\0')
5602 strcpy (sym
->binding_label
, binding_label
);
5605 strcpy (curr_binding_label
, binding_label
);
5607 else if (allow_binding_name
)
5609 /* No binding label, but if symbol isn't null, we
5610 can set the label for it here.
5611 If name="" or allow_binding_name is false, no C binding name is
5613 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5614 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
5617 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5618 && current_interface
.type
== INTERFACE_ABSTRACT
)
5620 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5628 /* Return nonzero if we're currently compiling a contained procedure. */
5631 contained_procedure (void)
5633 gfc_state_data
*s
= gfc_state_stack
;
5635 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5636 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5642 /* Set the kind of each enumerator. The kind is selected such that it is
5643 interoperable with the corresponding C enumeration type, making
5644 sure that -fshort-enums is honored. */
5649 enumerator_history
*current_history
= NULL
;
5653 if (max_enum
== NULL
|| enum_history
== NULL
)
5656 if (!flag_short_enums
)
5662 kind
= gfc_integer_kinds
[i
++].kind
;
5664 while (kind
< gfc_c_int_kind
5665 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5668 current_history
= enum_history
;
5669 while (current_history
!= NULL
)
5671 current_history
->sym
->ts
.kind
= kind
;
5672 current_history
= current_history
->next
;
5677 /* Match any of the various end-block statements. Returns the type of
5678 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5679 and END BLOCK statements cannot be replaced by a single END statement. */
5682 gfc_match_end (gfc_statement
*st
)
5684 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5685 gfc_compile_state state
;
5687 const char *block_name
;
5692 old_loc
= gfc_current_locus
;
5693 if (gfc_match ("end") != MATCH_YES
)
5696 state
= gfc_current_state ();
5697 block_name
= gfc_current_block () == NULL
5698 ? NULL
: gfc_current_block ()->name
;
5702 case COMP_ASSOCIATE
:
5704 if (!strcmp (block_name
, "block@"))
5709 case COMP_DERIVED_CONTAINS
:
5710 state
= gfc_state_stack
->previous
->state
;
5711 block_name
= gfc_state_stack
->previous
->sym
== NULL
5712 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5723 *st
= ST_END_PROGRAM
;
5724 target
= " program";
5728 case COMP_SUBROUTINE
:
5729 *st
= ST_END_SUBROUTINE
;
5730 target
= " subroutine";
5731 eos_ok
= !contained_procedure ();
5735 *st
= ST_END_FUNCTION
;
5736 target
= " function";
5737 eos_ok
= !contained_procedure ();
5740 case COMP_BLOCK_DATA
:
5741 *st
= ST_END_BLOCK_DATA
;
5742 target
= " block data";
5747 *st
= ST_END_MODULE
;
5752 case COMP_INTERFACE
:
5753 *st
= ST_END_INTERFACE
;
5754 target
= " interface";
5759 case COMP_DERIVED_CONTAINS
:
5765 case COMP_ASSOCIATE
:
5766 *st
= ST_END_ASSOCIATE
;
5767 target
= " associate";
5790 *st
= ST_END_CRITICAL
;
5791 target
= " critical";
5796 case COMP_SELECT_TYPE
:
5797 *st
= ST_END_SELECT
;
5803 *st
= ST_END_FORALL
;
5818 last_initializer
= NULL
;
5820 gfc_free_enum_history ();
5824 gfc_error ("Unexpected END statement at %C");
5828 if (gfc_match_eos () == MATCH_YES
)
5830 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
5832 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: END statement "
5833 "instead of %s statement at %L",
5834 gfc_ascii_statement (*st
), &old_loc
) == FAILURE
)
5839 /* We would have required END [something]. */
5840 gfc_error ("%s statement expected at %L",
5841 gfc_ascii_statement (*st
), &old_loc
);
5848 /* Verify that we've got the sort of end-block that we're expecting. */
5849 if (gfc_match (target
) != MATCH_YES
)
5851 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
5855 /* If we're at the end, make sure a block name wasn't required. */
5856 if (gfc_match_eos () == MATCH_YES
)
5859 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
5860 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
5861 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
5867 gfc_error ("Expected block name of '%s' in %s statement at %C",
5868 block_name
, gfc_ascii_statement (*st
));
5873 /* END INTERFACE has a special handler for its several possible endings. */
5874 if (*st
== ST_END_INTERFACE
)
5875 return gfc_match_end_interface ();
5877 /* We haven't hit the end of statement, so what is left must be an
5879 m
= gfc_match_space ();
5881 m
= gfc_match_name (name
);
5884 gfc_error ("Expected terminating name at %C");
5888 if (block_name
== NULL
)
5891 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
5893 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
5894 gfc_ascii_statement (*st
));
5897 /* Procedure pointer as function result. */
5898 else if (strcmp (block_name
, "ppr@") == 0
5899 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
5901 gfc_error ("Expected label '%s' for %s statement at %C",
5902 gfc_current_block ()->ns
->proc_name
->name
,
5903 gfc_ascii_statement (*st
));
5907 if (gfc_match_eos () == MATCH_YES
)
5911 gfc_syntax_error (*st
);
5914 gfc_current_locus
= old_loc
;
5920 /***************** Attribute declaration statements ****************/
5922 /* Set the attribute of a single variable. */
5927 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5935 m
= gfc_match_name (name
);
5939 if (find_special (name
, &sym
, false))
5942 var_locus
= gfc_current_locus
;
5944 /* Deal with possible array specification for certain attributes. */
5945 if (current_attr
.dimension
5946 || current_attr
.codimension
5947 || current_attr
.allocatable
5948 || current_attr
.pointer
5949 || current_attr
.target
)
5951 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
5952 !current_attr
.dimension
5953 && !current_attr
.pointer
5954 && !current_attr
.target
);
5955 if (m
== MATCH_ERROR
)
5958 if (current_attr
.dimension
&& m
== MATCH_NO
)
5960 gfc_error ("Missing array specification at %L in DIMENSION "
5961 "statement", &var_locus
);
5966 if (current_attr
.dimension
&& sym
->value
)
5968 gfc_error ("Dimensions specified for %s at %L after its "
5969 "initialisation", sym
->name
, &var_locus
);
5974 if (current_attr
.codimension
&& m
== MATCH_NO
)
5976 gfc_error ("Missing array specification at %L in CODIMENSION "
5977 "statement", &var_locus
);
5982 if ((current_attr
.allocatable
|| current_attr
.pointer
)
5983 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
5985 gfc_error ("Array specification must be deferred at %L", &var_locus
);
5991 /* Update symbol table. DIMENSION attribute is set in
5992 gfc_set_array_spec(). For CLASS variables, this must be applied
5993 to the first component, or '$data' field. */
5994 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
5996 if (gfc_copy_attr (&CLASS_DATA (sym
)->attr
, ¤t_attr
,&var_locus
)
6005 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6006 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
6013 if (sym
->ts
.type
== BT_CLASS
&& !sym
->attr
.class_ok
6014 && (sym
->attr
.class_ok
= sym
->attr
.class_ok
|| current_attr
.allocatable
6015 || current_attr
.pointer
))
6016 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
6018 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
6024 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6026 /* Fix the array spec. */
6027 m
= gfc_mod_pointee_as (sym
->as
);
6028 if (m
== MATCH_ERROR
)
6032 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
6038 if ((current_attr
.external
|| current_attr
.intrinsic
)
6039 && sym
->attr
.flavor
!= FL_PROCEDURE
6040 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
6046 add_hidden_procptr_result (sym
);
6051 gfc_free_array_spec (as
);
6056 /* Generic attribute declaration subroutine. Used for attributes that
6057 just have a list of names. */
6064 /* Gobble the optional double colon, by simply ignoring the result
6074 if (gfc_match_eos () == MATCH_YES
)
6080 if (gfc_match_char (',') != MATCH_YES
)
6082 gfc_error ("Unexpected character in variable list at %C");
6092 /* This routine matches Cray Pointer declarations of the form:
6093 pointer ( <pointer>, <pointee> )
6095 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6096 The pointer, if already declared, should be an integer. Otherwise, we
6097 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6098 be either a scalar, or an array declaration. No space is allocated for
6099 the pointee. For the statement
6100 pointer (ipt, ar(10))
6101 any subsequent uses of ar will be translated (in C-notation) as
6102 ar(i) => ((<type> *) ipt)(i)
6103 After gimplification, pointee variable will disappear in the code. */
6106 cray_pointer_decl (void)
6109 gfc_array_spec
*as
= NULL
;
6110 gfc_symbol
*cptr
; /* Pointer symbol. */
6111 gfc_symbol
*cpte
; /* Pointee symbol. */
6117 if (gfc_match_char ('(') != MATCH_YES
)
6119 gfc_error ("Expected '(' at %C");
6123 /* Match pointer. */
6124 var_locus
= gfc_current_locus
;
6125 gfc_clear_attr (¤t_attr
);
6126 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6127 current_ts
.type
= BT_INTEGER
;
6128 current_ts
.kind
= gfc_index_integer_kind
;
6130 m
= gfc_match_symbol (&cptr
, 0);
6133 gfc_error ("Expected variable name at %C");
6137 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
6140 gfc_set_sym_referenced (cptr
);
6142 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6144 cptr
->ts
.type
= BT_INTEGER
;
6145 cptr
->ts
.kind
= gfc_index_integer_kind
;
6147 else if (cptr
->ts
.type
!= BT_INTEGER
)
6149 gfc_error ("Cray pointer at %C must be an integer");
6152 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6153 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6154 " memory addresses require %d bytes",
6155 cptr
->ts
.kind
, gfc_index_integer_kind
);
6157 if (gfc_match_char (',') != MATCH_YES
)
6159 gfc_error ("Expected \",\" at %C");
6163 /* Match Pointee. */
6164 var_locus
= gfc_current_locus
;
6165 gfc_clear_attr (¤t_attr
);
6166 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6167 current_ts
.type
= BT_UNKNOWN
;
6168 current_ts
.kind
= 0;
6170 m
= gfc_match_symbol (&cpte
, 0);
6173 gfc_error ("Expected variable name at %C");
6177 /* Check for an optional array spec. */
6178 m
= gfc_match_array_spec (&as
, true, false);
6179 if (m
== MATCH_ERROR
)
6181 gfc_free_array_spec (as
);
6184 else if (m
== MATCH_NO
)
6186 gfc_free_array_spec (as
);
6190 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
6193 gfc_set_sym_referenced (cpte
);
6195 if (cpte
->as
== NULL
)
6197 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
6198 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6200 else if (as
!= NULL
)
6202 gfc_error ("Duplicate array spec for Cray pointee at %C");
6203 gfc_free_array_spec (as
);
6209 if (cpte
->as
!= NULL
)
6211 /* Fix array spec. */
6212 m
= gfc_mod_pointee_as (cpte
->as
);
6213 if (m
== MATCH_ERROR
)
6217 /* Point the Pointee at the Pointer. */
6218 cpte
->cp_pointer
= cptr
;
6220 if (gfc_match_char (')') != MATCH_YES
)
6222 gfc_error ("Expected \")\" at %C");
6225 m
= gfc_match_char (',');
6227 done
= true; /* Stop searching for more declarations. */
6231 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6232 || gfc_match_eos () != MATCH_YES
)
6234 gfc_error ("Expected \",\" or end of statement at %C");
6242 gfc_match_external (void)
6245 gfc_clear_attr (¤t_attr
);
6246 current_attr
.external
= 1;
6248 return attr_decl ();
6253 gfc_match_intent (void)
6257 /* This is not allowed within a BLOCK construct! */
6258 if (gfc_current_state () == COMP_BLOCK
)
6260 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6264 intent
= match_intent_spec ();
6265 if (intent
== INTENT_UNKNOWN
)
6268 gfc_clear_attr (¤t_attr
);
6269 current_attr
.intent
= intent
;
6271 return attr_decl ();
6276 gfc_match_intrinsic (void)
6279 gfc_clear_attr (¤t_attr
);
6280 current_attr
.intrinsic
= 1;
6282 return attr_decl ();
6287 gfc_match_optional (void)
6289 /* This is not allowed within a BLOCK construct! */
6290 if (gfc_current_state () == COMP_BLOCK
)
6292 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6296 gfc_clear_attr (¤t_attr
);
6297 current_attr
.optional
= 1;
6299 return attr_decl ();
6304 gfc_match_pointer (void)
6306 gfc_gobble_whitespace ();
6307 if (gfc_peek_ascii_char () == '(')
6309 if (!gfc_option
.flag_cray_pointer
)
6311 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6315 return cray_pointer_decl ();
6319 gfc_clear_attr (¤t_attr
);
6320 current_attr
.pointer
= 1;
6322 return attr_decl ();
6328 gfc_match_allocatable (void)
6330 gfc_clear_attr (¤t_attr
);
6331 current_attr
.allocatable
= 1;
6333 return attr_decl ();
6338 gfc_match_codimension (void)
6340 gfc_clear_attr (¤t_attr
);
6341 current_attr
.codimension
= 1;
6343 return attr_decl ();
6348 gfc_match_contiguous (void)
6350 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTIGUOUS statement at %C")
6354 gfc_clear_attr (¤t_attr
);
6355 current_attr
.contiguous
= 1;
6357 return attr_decl ();
6362 gfc_match_dimension (void)
6364 gfc_clear_attr (¤t_attr
);
6365 current_attr
.dimension
= 1;
6367 return attr_decl ();
6372 gfc_match_target (void)
6374 gfc_clear_attr (¤t_attr
);
6375 current_attr
.target
= 1;
6377 return attr_decl ();
6381 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6385 access_attr_decl (gfc_statement st
)
6387 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6388 interface_type type
;
6391 gfc_intrinsic_op op
;
6394 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6399 m
= gfc_match_generic_spec (&type
, name
, &op
);
6402 if (m
== MATCH_ERROR
)
6407 case INTERFACE_NAMELESS
:
6408 case INTERFACE_ABSTRACT
:
6411 case INTERFACE_GENERIC
:
6412 if (gfc_get_symbol (name
, NULL
, &sym
))
6415 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
6416 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6417 sym
->name
, NULL
) == FAILURE
)
6422 case INTERFACE_INTRINSIC_OP
:
6423 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6425 gfc_current_ns
->operator_access
[op
] =
6426 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6430 gfc_error ("Access specification of the %s operator at %C has "
6431 "already been specified", gfc_op2string (op
));
6437 case INTERFACE_USER_OP
:
6438 uop
= gfc_get_uop (name
);
6440 if (uop
->access
== ACCESS_UNKNOWN
)
6442 uop
->access
= (st
== ST_PUBLIC
)
6443 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6447 gfc_error ("Access specification of the .%s. operator at %C "
6448 "has already been specified", sym
->name
);
6455 if (gfc_match_char (',') == MATCH_NO
)
6459 if (gfc_match_eos () != MATCH_YES
)
6464 gfc_syntax_error (st
);
6472 gfc_match_protected (void)
6477 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6479 gfc_error ("PROTECTED at %C only allowed in specification "
6480 "part of a module");
6485 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
6489 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6494 if (gfc_match_eos () == MATCH_YES
)
6499 m
= gfc_match_symbol (&sym
, 0);
6503 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6516 if (gfc_match_eos () == MATCH_YES
)
6518 if (gfc_match_char (',') != MATCH_YES
)
6525 gfc_error ("Syntax error in PROTECTED statement at %C");
6530 /* The PRIVATE statement is a bit weird in that it can be an attribute
6531 declaration, but also works as a standalone statement inside of a
6532 type declaration or a module. */
6535 gfc_match_private (gfc_statement
*st
)
6538 if (gfc_match ("private") != MATCH_YES
)
6541 if (gfc_current_state () != COMP_MODULE
6542 && !(gfc_current_state () == COMP_DERIVED
6543 && gfc_state_stack
->previous
6544 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6545 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6546 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6547 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6549 gfc_error ("PRIVATE statement at %C is only allowed in the "
6550 "specification part of a module");
6554 if (gfc_current_state () == COMP_DERIVED
)
6556 if (gfc_match_eos () == MATCH_YES
)
6562 gfc_syntax_error (ST_PRIVATE
);
6566 if (gfc_match_eos () == MATCH_YES
)
6573 return access_attr_decl (ST_PRIVATE
);
6578 gfc_match_public (gfc_statement
*st
)
6581 if (gfc_match ("public") != MATCH_YES
)
6584 if (gfc_current_state () != COMP_MODULE
)
6586 gfc_error ("PUBLIC statement at %C is only allowed in the "
6587 "specification part of a module");
6591 if (gfc_match_eos () == MATCH_YES
)
6598 return access_attr_decl (ST_PUBLIC
);
6602 /* Workhorse for gfc_match_parameter. */
6612 m
= gfc_match_symbol (&sym
, 0);
6614 gfc_error ("Expected variable name at %C in PARAMETER statement");
6619 if (gfc_match_char ('=') == MATCH_NO
)
6621 gfc_error ("Expected = sign in PARAMETER statement at %C");
6625 m
= gfc_match_init_expr (&init
);
6627 gfc_error ("Expected expression at %C in PARAMETER statement");
6631 if (sym
->ts
.type
== BT_UNKNOWN
6632 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6638 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6639 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6647 gfc_error ("Initializing already initialized variable at %C");
6652 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6653 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6656 gfc_free_expr (init
);
6661 /* Match a parameter statement, with the weird syntax that these have. */
6664 gfc_match_parameter (void)
6668 if (gfc_match_char ('(') == MATCH_NO
)
6677 if (gfc_match (" )%t") == MATCH_YES
)
6680 if (gfc_match_char (',') != MATCH_YES
)
6682 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6692 /* Save statements have a special syntax. */
6695 gfc_match_save (void)
6697 char n
[GFC_MAX_SYMBOL_LEN
+1];
6702 if (gfc_match_eos () == MATCH_YES
)
6704 if (gfc_current_ns
->seen_save
)
6706 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6707 "follows previous SAVE statement")
6712 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6716 if (gfc_current_ns
->save_all
)
6718 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6719 "blanket SAVE statement")
6728 m
= gfc_match_symbol (&sym
, 0);
6732 if (gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
6733 &gfc_current_locus
) == FAILURE
)
6744 m
= gfc_match (" / %n /", &n
);
6745 if (m
== MATCH_ERROR
)
6750 c
= gfc_get_common (n
, 0);
6753 gfc_current_ns
->seen_save
= 1;
6756 if (gfc_match_eos () == MATCH_YES
)
6758 if (gfc_match_char (',') != MATCH_YES
)
6765 gfc_error ("Syntax error in SAVE statement at %C");
6771 gfc_match_value (void)
6776 /* This is not allowed within a BLOCK construct! */
6777 if (gfc_current_state () == COMP_BLOCK
)
6779 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6783 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
6787 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6792 if (gfc_match_eos () == MATCH_YES
)
6797 m
= gfc_match_symbol (&sym
, 0);
6801 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6814 if (gfc_match_eos () == MATCH_YES
)
6816 if (gfc_match_char (',') != MATCH_YES
)
6823 gfc_error ("Syntax error in VALUE statement at %C");
6829 gfc_match_volatile (void)
6834 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
6838 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6843 if (gfc_match_eos () == MATCH_YES
)
6848 /* VOLATILE is special because it can be added to host-associated
6849 symbols locally. Except for coarrays. */
6850 m
= gfc_match_symbol (&sym
, 1);
6854 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6855 for variable in a BLOCK which is defined outside of the BLOCK. */
6856 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
6858 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6859 "%C, which is use-/host-associated", sym
->name
);
6862 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6875 if (gfc_match_eos () == MATCH_YES
)
6877 if (gfc_match_char (',') != MATCH_YES
)
6884 gfc_error ("Syntax error in VOLATILE statement at %C");
6890 gfc_match_asynchronous (void)
6895 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASYNCHRONOUS statement at %C")
6899 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6904 if (gfc_match_eos () == MATCH_YES
)
6909 /* ASYNCHRONOUS is special because it can be added to host-associated
6911 m
= gfc_match_symbol (&sym
, 1);
6915 if (gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6928 if (gfc_match_eos () == MATCH_YES
)
6930 if (gfc_match_char (',') != MATCH_YES
)
6937 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6942 /* Match a module procedure statement. Note that we have to modify
6943 symbols in the parent's namespace because the current one was there
6944 to receive symbols that are in an interface's formal argument list. */
6947 gfc_match_modproc (void)
6949 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6952 gfc_namespace
*module_ns
;
6953 gfc_interface
*old_interface_head
, *interface
;
6955 if (gfc_state_stack
->state
!= COMP_INTERFACE
6956 || gfc_state_stack
->previous
== NULL
6957 || current_interface
.type
== INTERFACE_NAMELESS
6958 || current_interface
.type
== INTERFACE_ABSTRACT
)
6960 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6965 module_ns
= gfc_current_ns
->parent
;
6966 for (; module_ns
; module_ns
= module_ns
->parent
)
6967 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
6968 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
6969 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
6970 && !module_ns
->proc_name
->attr
.contained
))
6973 if (module_ns
== NULL
)
6976 /* Store the current state of the interface. We will need it if we
6977 end up with a syntax error and need to recover. */
6978 old_interface_head
= gfc_current_interface_head ();
6982 locus old_locus
= gfc_current_locus
;
6985 m
= gfc_match_name (name
);
6991 /* Check for syntax error before starting to add symbols to the
6992 current namespace. */
6993 if (gfc_match_eos () == MATCH_YES
)
6995 if (!last
&& gfc_match_char (',') != MATCH_YES
)
6998 /* Now we're sure the syntax is valid, we process this item
7000 if (gfc_get_symbol (name
, module_ns
, &sym
))
7003 if (sym
->attr
.intrinsic
)
7005 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7006 "PROCEDURE", &old_locus
);
7010 if (sym
->attr
.proc
!= PROC_MODULE
7011 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7012 sym
->name
, NULL
) == FAILURE
)
7015 if (gfc_add_interface (sym
) == FAILURE
)
7018 sym
->attr
.mod_proc
= 1;
7019 sym
->declared_at
= old_locus
;
7028 /* Restore the previous state of the interface. */
7029 interface
= gfc_current_interface_head ();
7030 gfc_set_current_interface_head (old_interface_head
);
7032 /* Free the new interfaces. */
7033 while (interface
!= old_interface_head
)
7035 gfc_interface
*i
= interface
->next
;
7036 gfc_free (interface
);
7040 /* And issue a syntax error. */
7041 gfc_syntax_error (ST_MODULE_PROC
);
7046 /* Check a derived type that is being extended. */
7048 check_extended_derived_type (char *name
)
7050 gfc_symbol
*extended
;
7052 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7054 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7060 gfc_error ("No such symbol in TYPE definition at %C");
7064 if (extended
->attr
.flavor
!= FL_DERIVED
)
7066 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7067 "derived type", name
);
7071 if (extended
->attr
.is_bind_c
)
7073 gfc_error ("'%s' cannot be extended at %C because it "
7074 "is BIND(C)", extended
->name
);
7078 if (extended
->attr
.sequence
)
7080 gfc_error ("'%s' cannot be extended at %C because it "
7081 "is a SEQUENCE type", extended
->name
);
7089 /* Match the optional attribute specifiers for a type declaration.
7090 Return MATCH_ERROR if an error is encountered in one of the handled
7091 attributes (public, private, bind(c)), MATCH_NO if what's found is
7092 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7093 checking on attribute conflicts needs to be done. */
7096 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7098 /* See if the derived type is marked as private. */
7099 if (gfc_match (" , private") == MATCH_YES
)
7101 if (gfc_current_state () != COMP_MODULE
)
7103 gfc_error ("Derived type at %C can only be PRIVATE in the "
7104 "specification part of a module");
7108 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
7111 else if (gfc_match (" , public") == MATCH_YES
)
7113 if (gfc_current_state () != COMP_MODULE
)
7115 gfc_error ("Derived type at %C can only be PUBLIC in the "
7116 "specification part of a module");
7120 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
7123 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7125 /* If the type is defined to be bind(c) it then needs to make
7126 sure that all fields are interoperable. This will
7127 need to be a semantic check on the finished derived type.
7128 See 15.2.3 (lines 9-12) of F2003 draft. */
7129 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
7132 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7134 else if (gfc_match (" , abstract") == MATCH_YES
)
7136 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
7140 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
7143 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
7145 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
7151 /* If we get here, something matched. */
7156 /* Assign a hash value for a derived type. The algorithm is that of
7157 SDBM. The hashed string is '[module_name #] derived_name'. */
7159 hash_value (gfc_symbol
*sym
)
7161 unsigned int hash
= 0;
7165 /* Hash of the module or procedure name. */
7166 if (sym
->module
!= NULL
)
7168 else if (sym
->ns
&& sym
->ns
->proc_name
7169 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
7170 c
= sym
->ns
->proc_name
->name
;
7177 for (i
= 0; i
< len
; i
++, c
++)
7178 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ (*c
);
7180 /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'. */
7181 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ '#';
7184 /* Hash of the derived type name. */
7185 len
= strlen (sym
->name
);
7187 for (i
= 0; i
< len
; i
++, c
++)
7188 hash
= (hash
<< 6) + (hash
<< 16) - hash
+ (*c
);
7190 /* Return the hash but take the modulus for the sake of module read,
7191 even though this slightly increases the chance of collision. */
7192 return (hash
% 100000000);
7196 /* Match the beginning of a derived type declaration. If a type name
7197 was the result of a function, then it is possible to have a symbol
7198 already to be known as a derived type yet have no components. */
7201 gfc_match_derived_decl (void)
7203 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7204 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7205 symbol_attribute attr
;
7207 gfc_symbol
*extended
;
7209 match is_type_attr_spec
= MATCH_NO
;
7210 bool seen_attr
= false;
7212 if (gfc_current_state () == COMP_DERIVED
)
7217 gfc_clear_attr (&attr
);
7222 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7223 if (is_type_attr_spec
== MATCH_ERROR
)
7225 if (is_type_attr_spec
== MATCH_YES
)
7227 } while (is_type_attr_spec
== MATCH_YES
);
7229 /* Deal with derived type extensions. The extension attribute has
7230 been added to 'attr' but now the parent type must be found and
7233 extended
= check_extended_derived_type (parent
);
7235 if (parent
[0] && !extended
)
7238 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7240 gfc_error ("Expected :: in TYPE definition at %C");
7244 m
= gfc_match (" %n%t", name
);
7248 /* Make sure the name is not the name of an intrinsic type. */
7249 if (gfc_is_intrinsic_typename (name
))
7251 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7256 if (gfc_get_symbol (name
, NULL
, &sym
))
7259 if (sym
->ts
.type
!= BT_UNKNOWN
)
7261 gfc_error ("Derived type name '%s' at %C already has a basic type "
7262 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
7266 /* The symbol may already have the derived attribute without the
7267 components. The ways this can happen is via a function
7268 definition, an INTRINSIC statement or a subtype in another
7269 derived type that is a pointer. The first part of the AND clause
7270 is true if the symbol is not the return value of a function. */
7271 if (sym
->attr
.flavor
!= FL_DERIVED
7272 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
7275 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
7277 gfc_error ("Derived type definition of '%s' at %C has already been "
7278 "defined", sym
->name
);
7282 if (attr
.access
!= ACCESS_UNKNOWN
7283 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
7286 /* See if the derived type was labeled as bind(c). */
7287 if (attr
.is_bind_c
!= 0)
7288 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7290 /* Construct the f2k_derived namespace if it is not yet there. */
7291 if (!sym
->f2k_derived
)
7292 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7294 if (extended
&& !sym
->components
)
7299 /* Add the extended derived type as the first component. */
7300 gfc_add_component (sym
, parent
, &p
);
7302 gfc_set_sym_referenced (extended
);
7304 p
->ts
.type
= BT_DERIVED
;
7305 p
->ts
.u
.derived
= extended
;
7306 p
->initializer
= gfc_default_initializer (&p
->ts
);
7308 /* Set extension level. */
7309 if (extended
->attr
.extension
== 255)
7311 /* Since the extension field is 8 bit wide, we can only have
7312 up to 255 extension levels. */
7313 gfc_error ("Maximum extension level reached with type '%s' at %L",
7314 extended
->name
, &extended
->declared_at
);
7317 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7319 /* Provide the links between the extended type and its extension. */
7320 if (!extended
->f2k_derived
)
7321 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7322 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7326 if (!sym
->hash_value
)
7327 /* Set the hash for the compound name for this type. */
7328 sym
->hash_value
= hash_value (sym
);
7330 /* Take over the ABSTRACT attribute. */
7331 sym
->attr
.abstract
= attr
.abstract
;
7333 gfc_new_block
= sym
;
7339 /* Cray Pointees can be declared as:
7340 pointer (ipt, a (n,m,...,*)) */
7343 gfc_mod_pointee_as (gfc_array_spec
*as
)
7345 as
->cray_pointee
= true; /* This will be useful to know later. */
7346 if (as
->type
== AS_ASSUMED_SIZE
)
7347 as
->cp_was_assumed
= true;
7348 else if (as
->type
== AS_ASSUMED_SHAPE
)
7350 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7357 /* Match the enum definition statement, here we are trying to match
7358 the first line of enum definition statement.
7359 Returns MATCH_YES if match is found. */
7362 gfc_match_enum (void)
7366 m
= gfc_match_eos ();
7370 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
7378 /* Returns an initializer whose value is one higher than the value of the
7379 LAST_INITIALIZER argument. If the argument is NULL, the
7380 initializers value will be set to zero. The initializer's kind
7381 will be set to gfc_c_int_kind.
7383 If -fshort-enums is given, the appropriate kind will be selected
7384 later after all enumerators have been parsed. A warning is issued
7385 here if an initializer exceeds gfc_c_int_kind. */
7388 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7391 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7393 mpz_init (result
->value
.integer
);
7395 if (last_initializer
!= NULL
)
7397 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7398 result
->where
= last_initializer
->where
;
7400 if (gfc_check_integer_range (result
->value
.integer
,
7401 gfc_c_int_kind
) != ARITH_OK
)
7403 gfc_error ("Enumerator exceeds the C integer type at %C");
7409 /* Control comes here, if it's the very first enumerator and no
7410 initializer has been given. It will be initialized to zero. */
7411 mpz_set_si (result
->value
.integer
, 0);
7418 /* Match a variable name with an optional initializer. When this
7419 subroutine is called, a variable is expected to be parsed next.
7420 Depending on what is happening at the moment, updates either the
7421 symbol table or the current interface. */
7424 enumerator_decl (void)
7426 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7427 gfc_expr
*initializer
;
7428 gfc_array_spec
*as
= NULL
;
7436 old_locus
= gfc_current_locus
;
7438 /* When we get here, we've just matched a list of attributes and
7439 maybe a type and a double colon. The next thing we expect to see
7440 is the name of the symbol. */
7441 m
= gfc_match_name (name
);
7445 var_locus
= gfc_current_locus
;
7447 /* OK, we've successfully matched the declaration. Now put the
7448 symbol in the current namespace. If we fail to create the symbol,
7450 if (build_sym (name
, NULL
, &as
, &var_locus
) == FAILURE
)
7456 /* The double colon must be present in order to have initializers.
7457 Otherwise the statement is ambiguous with an assignment statement. */
7460 if (gfc_match_char ('=') == MATCH_YES
)
7462 m
= gfc_match_init_expr (&initializer
);
7465 gfc_error ("Expected an initialization expression at %C");
7474 /* If we do not have an initializer, the initialization value of the
7475 previous enumerator (stored in last_initializer) is incremented
7476 by 1 and is used to initialize the current enumerator. */
7477 if (initializer
== NULL
)
7478 initializer
= enum_initializer (last_initializer
, old_locus
);
7480 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7482 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7488 /* Store this current initializer, for the next enumerator variable
7489 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7490 use last_initializer below. */
7491 last_initializer
= initializer
;
7492 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7494 /* Maintain enumerator history. */
7495 gfc_find_symbol (name
, NULL
, 0, &sym
);
7496 create_enum_history (sym
, last_initializer
);
7498 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
7501 /* Free stuff up and return. */
7502 gfc_free_expr (initializer
);
7508 /* Match the enumerator definition statement. */
7511 gfc_match_enumerator_def (void)
7516 gfc_clear_ts (¤t_ts
);
7518 m
= gfc_match (" enumerator");
7522 m
= gfc_match (" :: ");
7523 if (m
== MATCH_ERROR
)
7526 colon_seen
= (m
== MATCH_YES
);
7528 if (gfc_current_state () != COMP_ENUM
)
7530 gfc_error ("ENUM definition statement expected before %C");
7531 gfc_free_enum_history ();
7535 (¤t_ts
)->type
= BT_INTEGER
;
7536 (¤t_ts
)->kind
= gfc_c_int_kind
;
7538 gfc_clear_attr (¤t_attr
);
7539 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7548 m
= enumerator_decl ();
7549 if (m
== MATCH_ERROR
)
7551 gfc_free_enum_history ();
7557 if (gfc_match_eos () == MATCH_YES
)
7559 if (gfc_match_char (',') != MATCH_YES
)
7563 if (gfc_current_state () == COMP_ENUM
)
7565 gfc_free_enum_history ();
7566 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7571 gfc_free_array_spec (current_as
);
7578 /* Match binding attributes. */
7581 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7583 bool found_passing
= false;
7584 bool seen_ptr
= false;
7585 match m
= MATCH_YES
;
7587 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7588 this case the defaults are in there. */
7589 ba
->access
= ACCESS_UNKNOWN
;
7590 ba
->pass_arg
= NULL
;
7591 ba
->pass_arg_num
= 0;
7593 ba
->non_overridable
= 0;
7597 /* If we find a comma, we believe there are binding attributes. */
7598 m
= gfc_match_char (',');
7604 /* Access specifier. */
7606 m
= gfc_match (" public");
7607 if (m
== MATCH_ERROR
)
7611 if (ba
->access
!= ACCESS_UNKNOWN
)
7613 gfc_error ("Duplicate access-specifier at %C");
7617 ba
->access
= ACCESS_PUBLIC
;
7621 m
= gfc_match (" private");
7622 if (m
== MATCH_ERROR
)
7626 if (ba
->access
!= ACCESS_UNKNOWN
)
7628 gfc_error ("Duplicate access-specifier at %C");
7632 ba
->access
= ACCESS_PRIVATE
;
7636 /* If inside GENERIC, the following is not allowed. */
7641 m
= gfc_match (" nopass");
7642 if (m
== MATCH_ERROR
)
7648 gfc_error ("Binding attributes already specify passing,"
7649 " illegal NOPASS at %C");
7653 found_passing
= true;
7658 /* PASS possibly including argument. */
7659 m
= gfc_match (" pass");
7660 if (m
== MATCH_ERROR
)
7664 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
7668 gfc_error ("Binding attributes already specify passing,"
7669 " illegal PASS at %C");
7673 m
= gfc_match (" ( %n )", arg
);
7674 if (m
== MATCH_ERROR
)
7677 ba
->pass_arg
= gfc_get_string (arg
);
7678 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
7680 found_passing
= true;
7688 m
= gfc_match (" pointer");
7689 if (m
== MATCH_ERROR
)
7695 gfc_error ("Duplicate POINTER attribute at %C");
7705 /* NON_OVERRIDABLE flag. */
7706 m
= gfc_match (" non_overridable");
7707 if (m
== MATCH_ERROR
)
7711 if (ba
->non_overridable
)
7713 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7717 ba
->non_overridable
= 1;
7721 /* DEFERRED flag. */
7722 m
= gfc_match (" deferred");
7723 if (m
== MATCH_ERROR
)
7729 gfc_error ("Duplicate DEFERRED at %C");
7740 /* Nothing matching found. */
7742 gfc_error ("Expected access-specifier at %C");
7744 gfc_error ("Expected binding attribute at %C");
7747 while (gfc_match_char (',') == MATCH_YES
);
7749 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7750 if (ba
->non_overridable
&& ba
->deferred
)
7752 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7759 if (ba
->access
== ACCESS_UNKNOWN
)
7760 ba
->access
= gfc_typebound_default_access
;
7762 if (ppc
&& !seen_ptr
)
7764 gfc_error ("POINTER attribute is required for procedure pointer component"
7776 /* Match a PROCEDURE specific binding inside a derived type. */
7779 match_procedure_in_type (void)
7781 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7782 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
7783 char* target
= NULL
, *ifc
= NULL
;
7784 gfc_typebound_proc tb
;
7793 /* Check current state. */
7794 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
7795 block
= gfc_state_stack
->previous
->sym
;
7798 /* Try to match PROCEDURE(interface). */
7799 if (gfc_match (" (") == MATCH_YES
)
7801 m
= gfc_match_name (target_buf
);
7802 if (m
== MATCH_ERROR
)
7806 gfc_error ("Interface-name expected after '(' at %C");
7810 if (gfc_match (" )") != MATCH_YES
)
7812 gfc_error ("')' expected at %C");
7819 /* Construct the data structure. */
7820 memset (&tb
, 0, sizeof (tb
));
7821 tb
.where
= gfc_current_locus
;
7823 /* Match binding attributes. */
7824 m
= match_binding_attributes (&tb
, false, false);
7825 if (m
== MATCH_ERROR
)
7827 seen_attrs
= (m
== MATCH_YES
);
7829 /* Check that attribute DEFERRED is given if an interface is specified. */
7830 if (tb
.deferred
&& !ifc
)
7832 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7835 if (ifc
&& !tb
.deferred
)
7837 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7841 /* Match the colons. */
7842 m
= gfc_match (" ::");
7843 if (m
== MATCH_ERROR
)
7845 seen_colons
= (m
== MATCH_YES
);
7846 if (seen_attrs
&& !seen_colons
)
7848 gfc_error ("Expected '::' after binding-attributes at %C");
7852 /* Match the binding names. */
7855 m
= gfc_match_name (name
);
7856 if (m
== MATCH_ERROR
)
7860 gfc_error ("Expected binding name at %C");
7864 if (num
>1 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: PROCEDURE list"
7865 " at %C") == FAILURE
)
7868 /* Try to match the '=> target', if it's there. */
7870 m
= gfc_match (" =>");
7871 if (m
== MATCH_ERROR
)
7877 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7883 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7888 m
= gfc_match_name (target_buf
);
7889 if (m
== MATCH_ERROR
)
7893 gfc_error ("Expected binding target after '=>' at %C");
7896 target
= target_buf
;
7899 /* If no target was found, it has the same name as the binding. */
7903 /* Get the namespace to insert the symbols into. */
7904 ns
= block
->f2k_derived
;
7907 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7908 if (tb
.deferred
&& !block
->attr
.abstract
)
7910 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7911 "is not ABSTRACT", block
->name
);
7915 /* See if we already have a binding with this name in the symtree which
7916 would be an error. If a GENERIC already targetted this binding, it may
7917 be already there but then typebound is still NULL. */
7918 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7919 if (stree
&& stree
->n
.tb
)
7921 gfc_error ("There is already a procedure with binding name '%s' for "
7922 "the derived type '%s' at %C", name
, block
->name
);
7926 /* Insert it and set attributes. */
7930 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7933 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
7935 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
7938 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
7940 if (gfc_match_eos () == MATCH_YES
)
7942 if (gfc_match_char (',') != MATCH_YES
)
7947 gfc_error ("Syntax error in PROCEDURE statement at %C");
7952 /* Match a GENERIC procedure binding inside a derived type. */
7955 gfc_match_generic (void)
7957 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7958 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
7960 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
7961 gfc_typebound_proc
* tb
;
7963 interface_type op_type
;
7964 gfc_intrinsic_op op
;
7967 /* Check current state. */
7968 if (gfc_current_state () == COMP_DERIVED
)
7970 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7973 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
7975 block
= gfc_state_stack
->previous
->sym
;
7976 ns
= block
->f2k_derived
;
7977 gcc_assert (block
&& ns
);
7979 memset (&tbattr
, 0, sizeof (tbattr
));
7980 tbattr
.where
= gfc_current_locus
;
7982 /* See if we get an access-specifier. */
7983 m
= match_binding_attributes (&tbattr
, true, false);
7984 if (m
== MATCH_ERROR
)
7987 /* Now the colons, those are required. */
7988 if (gfc_match (" ::") != MATCH_YES
)
7990 gfc_error ("Expected '::' at %C");
7994 /* Match the binding name; depending on type (operator / generic) format
7995 it for future error messages into bind_name. */
7997 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
7998 if (m
== MATCH_ERROR
)
8002 gfc_error ("Expected generic name or operator descriptor at %C");
8008 case INTERFACE_GENERIC
:
8009 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8012 case INTERFACE_USER_OP
:
8013 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8016 case INTERFACE_INTRINSIC_OP
:
8017 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8018 gfc_op2string (op
));
8025 /* Match the required =>. */
8026 if (gfc_match (" =>") != MATCH_YES
)
8028 gfc_error ("Expected '=>' at %C");
8032 /* Try to find existing GENERIC binding with this name / for this operator;
8033 if there is something, check that it is another GENERIC and then extend
8034 it rather than building a new node. Otherwise, create it and put it
8035 at the right position. */
8039 case INTERFACE_USER_OP
:
8040 case INTERFACE_GENERIC
:
8042 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8045 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8057 case INTERFACE_INTRINSIC_OP
:
8067 if (!tb
->is_generic
)
8069 gcc_assert (op_type
== INTERFACE_GENERIC
);
8070 gfc_error ("There's already a non-generic procedure with binding name"
8071 " '%s' for the derived type '%s' at %C",
8072 bind_name
, block
->name
);
8076 if (tb
->access
!= tbattr
.access
)
8078 gfc_error ("Binding at %C must have the same access as already"
8079 " defined binding '%s'", bind_name
);
8085 tb
= gfc_get_typebound_proc (NULL
);
8086 tb
->where
= gfc_current_locus
;
8087 tb
->access
= tbattr
.access
;
8089 tb
->u
.generic
= NULL
;
8093 case INTERFACE_GENERIC
:
8094 case INTERFACE_USER_OP
:
8096 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8099 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8107 case INTERFACE_INTRINSIC_OP
:
8116 /* Now, match all following names as specific targets. */
8119 gfc_symtree
* target_st
;
8120 gfc_tbp_generic
* target
;
8122 m
= gfc_match_name (name
);
8123 if (m
== MATCH_ERROR
)
8127 gfc_error ("Expected specific binding name at %C");
8131 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8133 /* See if this is a duplicate specification. */
8134 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8135 if (target_st
== target
->specific_st
)
8137 gfc_error ("'%s' already defined as specific binding for the"
8138 " generic '%s' at %C", name
, bind_name
);
8142 target
= gfc_get_tbp_generic ();
8143 target
->specific_st
= target_st
;
8144 target
->specific
= NULL
;
8145 target
->next
= tb
->u
.generic
;
8146 tb
->u
.generic
= target
;
8148 while (gfc_match (" ,") == MATCH_YES
);
8150 /* Here should be the end. */
8151 if (gfc_match_eos () != MATCH_YES
)
8153 gfc_error ("Junk after GENERIC binding at %C");
8164 /* Match a FINAL declaration inside a derived type. */
8167 gfc_match_final_decl (void)
8169 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8172 gfc_namespace
* module_ns
;
8176 if (gfc_current_form
== FORM_FREE
)
8178 char c
= gfc_peek_ascii_char ();
8179 if (!gfc_is_whitespace (c
) && c
!= ':')
8183 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8185 if (gfc_current_form
== FORM_FIXED
)
8188 gfc_error ("FINAL declaration at %C must be inside a derived type "
8189 "CONTAINS section");
8193 block
= gfc_state_stack
->previous
->sym
;
8196 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8197 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8199 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8200 " specification part of a MODULE");
8204 module_ns
= gfc_current_ns
;
8205 gcc_assert (module_ns
);
8206 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8208 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8209 if (gfc_match (" ::") == MATCH_ERROR
)
8212 /* Match the sequence of procedure names. */
8219 if (first
&& gfc_match_eos () == MATCH_YES
)
8221 gfc_error ("Empty FINAL at %C");
8225 m
= gfc_match_name (name
);
8228 gfc_error ("Expected module procedure name at %C");
8231 else if (m
!= MATCH_YES
)
8234 if (gfc_match_eos () == MATCH_YES
)
8236 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8238 gfc_error ("Expected ',' at %C");
8242 if (gfc_get_symbol (name
, module_ns
, &sym
))
8244 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8248 /* Mark the symbol as module procedure. */
8249 if (sym
->attr
.proc
!= PROC_MODULE
8250 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
8251 sym
->name
, NULL
) == FAILURE
)
8254 /* Check if we already have this symbol in the list, this is an error. */
8255 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8256 if (f
->proc_sym
== sym
)
8258 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8263 /* Add this symbol to the list of finalizers. */
8264 gcc_assert (block
->f2k_derived
);
8266 f
= XCNEW (gfc_finalizer
);
8268 f
->proc_tree
= NULL
;
8269 f
->where
= gfc_current_locus
;
8270 f
->next
= block
->f2k_derived
->finalizers
;
8271 block
->f2k_derived
->finalizers
= f
;
8281 const ext_attr_t ext_attr_list
[] = {
8282 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8283 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8284 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8285 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8286 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8287 { NULL
, EXT_ATTR_LAST
, NULL
}
8290 /* Match a !GCC$ ATTRIBUTES statement of the form:
8291 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8292 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8294 TODO: We should support all GCC attributes using the same syntax for
8295 the attribute list, i.e. the list in C
8296 __attributes(( attribute-list ))
8298 !GCC$ ATTRIBUTES attribute-list ::
8299 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8302 As there is absolutely no risk of confusion, we should never return
8305 gfc_match_gcc_attributes (void)
8307 symbol_attribute attr
;
8308 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8313 gfc_clear_attr (&attr
);
8318 if (gfc_match_name (name
) != MATCH_YES
)
8321 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8322 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8325 if (id
== EXT_ATTR_LAST
)
8327 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8331 if (gfc_add_ext_attribute (&attr
, (ext_attr_id_t
) id
, &gfc_current_locus
)
8335 gfc_gobble_whitespace ();
8336 ch
= gfc_next_ascii_char ();
8339 /* This is the successful exit condition for the loop. */
8340 if (gfc_next_ascii_char () == ':')
8350 if (gfc_match_eos () == MATCH_YES
)
8355 m
= gfc_match_name (name
);
8359 if (find_special (name
, &sym
, true))
8362 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8364 if (gfc_match_eos () == MATCH_YES
)
8367 if (gfc_match_char (',') != MATCH_YES
)
8374 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");