1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 format_asterisk
= {0, NULL
, NULL
, -1, ST_LABEL_FORMAT
, ST_LABEL_FORMAT
, NULL
,
36 const char *name
, *spec
;
42 tag_file
= { "FILE", " file = %e", BT_CHARACTER
},
43 tag_status
= { "STATUS", " status = %e", BT_CHARACTER
},
44 tag_e_access
= {"ACCESS", " access = %e", BT_CHARACTER
},
45 tag_e_form
= {"FORM", " form = %e", BT_CHARACTER
},
46 tag_e_recl
= {"RECL", " recl = %e", BT_INTEGER
},
47 tag_e_blank
= {"BLANK", " blank = %e", BT_CHARACTER
},
48 tag_e_position
= {"POSITION", " position = %e", BT_CHARACTER
},
49 tag_e_action
= {"ACTION", " action = %e", BT_CHARACTER
},
50 tag_e_delim
= {"DELIM", " delim = %e", BT_CHARACTER
},
51 tag_e_pad
= {"PAD", " pad = %e", BT_CHARACTER
},
52 tag_unit
= {"UNIT", " unit = %e", BT_INTEGER
},
53 tag_advance
= {"ADVANCE", " advance = %e", BT_CHARACTER
},
54 tag_rec
= {"REC", " rec = %e", BT_INTEGER
},
55 tag_spos
= {"POSITION", " pos = %e", BT_INTEGER
},
56 tag_format
= {"FORMAT", NULL
, BT_CHARACTER
},
57 tag_iomsg
= {"IOMSG", " iomsg = %e", BT_CHARACTER
},
58 tag_iostat
= {"IOSTAT", " iostat = %v", BT_INTEGER
},
59 tag_size
= {"SIZE", " size = %v", BT_INTEGER
},
60 tag_exist
= {"EXIST", " exist = %v", BT_LOGICAL
},
61 tag_opened
= {"OPENED", " opened = %v", BT_LOGICAL
},
62 tag_named
= {"NAMED", " named = %v", BT_LOGICAL
},
63 tag_name
= {"NAME", " name = %v", BT_CHARACTER
},
64 tag_number
= {"NUMBER", " number = %v", BT_INTEGER
},
65 tag_s_access
= {"ACCESS", " access = %v", BT_CHARACTER
},
66 tag_sequential
= {"SEQUENTIAL", " sequential = %v", BT_CHARACTER
},
67 tag_direct
= {"DIRECT", " direct = %v", BT_CHARACTER
},
68 tag_s_form
= {"FORM", " form = %v", BT_CHARACTER
},
69 tag_formatted
= {"FORMATTED", " formatted = %v", BT_CHARACTER
},
70 tag_unformatted
= {"UNFORMATTED", " unformatted = %v", BT_CHARACTER
},
71 tag_s_recl
= {"RECL", " recl = %v", BT_INTEGER
},
72 tag_nextrec
= {"NEXTREC", " nextrec = %v", BT_INTEGER
},
73 tag_s_blank
= {"BLANK", " blank = %v", BT_CHARACTER
},
74 tag_s_position
= {"POSITION", " position = %v", BT_CHARACTER
},
75 tag_s_action
= {"ACTION", " action = %v", BT_CHARACTER
},
76 tag_read
= {"READ", " read = %v", BT_CHARACTER
},
77 tag_write
= {"WRITE", " write = %v", BT_CHARACTER
},
78 tag_readwrite
= {"READWRITE", " readwrite = %v", BT_CHARACTER
},
79 tag_s_delim
= {"DELIM", " delim = %v", BT_CHARACTER
},
80 tag_s_pad
= {"PAD", " pad = %v", BT_CHARACTER
},
81 tag_iolength
= {"IOLENGTH", " iolength = %v", BT_INTEGER
},
82 tag_convert
= {"CONVERT", " convert = %e", BT_CHARACTER
},
83 tag_strm_out
= {"POS", " pos = %v", BT_INTEGER
},
84 tag_err
= {"ERR", " err = %l", BT_UNKNOWN
},
85 tag_end
= {"END", " end = %l", BT_UNKNOWN
},
86 tag_eor
= {"EOR", " eor = %l", BT_UNKNOWN
};
88 static gfc_dt
*current_dt
;
90 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
93 /**************** Fortran 95 FORMAT parser *****************/
95 /* FORMAT tokens returned by format_lex(). */
98 FMT_NONE
, FMT_UNKNOWN
, FMT_SIGNED_INT
, FMT_ZERO
, FMT_POSINT
, FMT_PERIOD
,
99 FMT_COMMA
, FMT_COLON
, FMT_SLASH
, FMT_DOLLAR
, FMT_POS
, FMT_LPAREN
,
100 FMT_RPAREN
, FMT_X
, FMT_SIGN
, FMT_BLANK
, FMT_CHAR
, FMT_P
, FMT_IBOZ
, FMT_F
,
101 FMT_E
, FMT_EXT
, FMT_G
, FMT_L
, FMT_A
, FMT_D
, FMT_H
, FMT_END
105 /* Local variables for checking format strings. The saved_token is
106 used to back up by a single format token during the parsing
108 static char *format_string
;
109 static int format_length
, use_last_char
;
111 static format_token saved_token
;
114 { MODE_STRING
, MODE_FORMAT
, MODE_COPY
}
118 /* Return the next character in the format string. */
121 next_char (int in_string
)
133 if (mode
== MODE_STRING
)
134 c
= *format_string
++;
137 c
= gfc_next_char_literal (in_string
);
142 if (gfc_option
.flag_backslash
&& c
== '\\')
145 locus old_locus
= gfc_current_locus
;
147 /* Use a temp variable to avoid side effects from gfc_match_special_char
148 since it uses an int * for its argument. */
151 if (gfc_match_special_char (&tmp
) == MATCH_NO
)
152 gfc_current_locus
= old_locus
;
156 if (!(gfc_option
.allow_std
& GFC_STD_GNU
) && !inhibit_warnings
)
157 gfc_warning ("Extension: backslash character at %C");
160 if (mode
== MODE_COPY
)
161 *format_string
++ = c
;
168 /* Back up one character position. Only works once. */
176 /* Eat up the spaces and return a character. */
179 next_char_not_space (void)
186 while (gfc_is_whitespace (c
));
190 static int value
= 0;
192 /* Simple lexical analyzer for getting the next token in a FORMAT
203 if (saved_token
!= FMT_NONE
)
206 saved_token
= FMT_NONE
;
210 c
= next_char_not_space ();
218 c
= next_char_not_space ();
229 c
= next_char_not_space ();
231 value
= 10 * value
+ c
- '0';
240 token
= FMT_SIGNED_INT
;
259 c
= next_char_not_space ();
262 value
= 10 * value
+ c
- '0';
270 token
= zflag
? FMT_ZERO
: FMT_POSINT
;
294 c
= next_char_not_space ();
295 if (c
!= 'L' && c
!= 'R')
314 c
= next_char_not_space ();
315 if (c
!= 'P' && c
!= 'S')
322 c
= next_char_not_space ();
323 if (c
== 'N' || c
== 'Z')
384 c
= next_char_not_space ();
385 if (c
== 'N' || c
== 'S')
428 /* Check a format statement. The format string, either from a FORMAT
429 statement or a constant in an I/O statement has already been parsed
430 by itself, and we are checking it for validity. The dual origin
431 means that the warning message is a little less than great. */
434 check_format (bool is_input
)
436 const char *posint_required
= _("Positive width required");
437 const char *nonneg_required
= _("Nonnegative width required");
438 const char *unexpected_element
= _("Unexpected element");
439 const char *unexpected_end
= _("Unexpected end of format string");
448 saved_token
= FMT_NONE
;
456 error
= _("Missing leading left parenthesis");
462 goto finished
; /* Empty format is legal */
466 /* In this state, the next thing has to be a format item. */
491 /* Signed integer can only precede a P format. */
495 error
= _("Expected P edit descriptor");
502 /* P requires a prior number. */
503 error
= _("P descriptor requires leading scale factor");
507 /* X requires a prior number if we're being pedantic. */
508 if (gfc_notify_std (GFC_STD_GNU
, "Extension: X descriptor "
509 "requires leading space count at %C")
519 goto extension_optional_comma
;
528 if (gfc_notify_std (GFC_STD_GNU
, "Extension: $ descriptor at %C")
531 if (t
!= FMT_RPAREN
|| level
> 0)
533 gfc_warning ("$ should be the last specifier in format at %C");
534 goto optional_comma_1
;
552 error
= unexpected_end
;
556 error
= unexpected_element
;
561 /* In this state, t must currently be a data descriptor.
562 Deal with things that can/must follow the descriptor. */
576 error
= _("Repeat count cannot follow P descriptor");
591 switch (gfc_notification_std (GFC_STD_GNU
))
594 gfc_warning ("Extension: Missing positive width after L "
600 error
= posint_required
;
625 error
= posint_required
;
632 /* Warn if -std=legacy, otherwise error. */
633 if (gfc_option
.warn_std
!= 0)
634 gfc_error_now ("Period required in format specifier at %C");
636 gfc_warning ("Period required in format specifier at %C");
642 if (u
!= FMT_ZERO
&& u
!= FMT_POSINT
)
644 error
= nonneg_required
;
651 /* Look for optional exponent. */
662 error
= _("Positive exponent width required");
671 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
673 error
= nonneg_required
;
676 else if (is_input
&& t
== FMT_ZERO
)
678 error
= posint_required
;
685 /* Warn if -std=legacy, otherwise error. */
686 if (gfc_option
.warn_std
!= 0)
687 gfc_error_now ("Period required in format specifier at %C");
689 gfc_warning ("Period required in format specifier at %C");
695 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
697 error
= nonneg_required
;
704 if (!(gfc_option
.allow_std
& GFC_STD_GNU
) && !inhibit_warnings
)
705 gfc_warning ("The H format specifier at %C is"
706 " a Fortran 95 deleted feature");
708 if(mode
== MODE_STRING
)
710 format_string
+= value
;
711 format_length
-= value
;
725 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
727 error
= nonneg_required
;
730 else if (is_input
&& t
== FMT_ZERO
)
732 error
= posint_required
;
744 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
746 error
= nonneg_required
;
754 error
= unexpected_element
;
759 /* Between a descriptor and what comes next. */
778 error
= unexpected_end
;
782 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
789 /* Optional comma is a weird between state where we've just finished
790 reading a colon, slash, dollar or P descriptor. */
805 /* Assume that we have another format item. */
812 extension_optional_comma
:
813 /* As a GNU extension, permit a missing comma after a string literal. */
831 error
= unexpected_end
;
835 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
845 gfc_error ("%s in format string at %C", error
);
847 /* TODO: More elaborate measures are needed to show where a problem
848 is within a format string that has been calculated. */
856 /* Given an expression node that is a constant string, see if it looks
857 like a format string. */
860 check_format_string (gfc_expr
*e
, bool is_input
)
863 format_string
= e
->value
.character
.string
;
864 return check_format (is_input
);
868 /************ Fortran 95 I/O statement matchers *************/
870 /* Match a FORMAT statement. This amounts to actually parsing the
871 format descriptors in order to correctly locate the end of the
875 gfc_match_format (void)
880 if (gfc_current_ns
->proc_name
881 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
883 gfc_error ("Format statement in module main block at %C");
887 if (gfc_statement_label
== NULL
)
889 gfc_error ("Missing format label at %C");
892 gfc_gobble_whitespace ();
897 start
= gfc_current_locus
;
899 if (check_format (false) == FAILURE
)
902 if (gfc_match_eos () != MATCH_YES
)
904 gfc_syntax_error (ST_FORMAT
);
908 /* The label doesn't get created until after the statement is done
909 being matched, so we have to leave the string for later. */
911 gfc_current_locus
= start
; /* Back to the beginning */
914 new_st
.op
= EXEC_NOP
;
917 e
->expr_type
= EXPR_CONSTANT
;
918 e
->ts
.type
= BT_CHARACTER
;
919 e
->ts
.kind
= gfc_default_character_kind
;
921 e
->value
.character
.string
= format_string
= gfc_getmem (format_length
+ 1);
922 e
->value
.character
.length
= format_length
;
923 gfc_statement_label
->format
= e
;
926 check_format (false); /* Guaranteed to succeed */
927 gfc_match_eos (); /* Guaranteed to succeed */
933 /* Match an expression I/O tag of some sort. */
936 match_etag (const io_tag
*tag
, gfc_expr
**v
)
941 m
= gfc_match (tag
->spec
, &result
);
947 gfc_error ("Duplicate %s specification at %C", tag
->name
);
948 gfc_free_expr (result
);
957 /* Match a variable I/O tag of some sort. */
960 match_vtag (const io_tag
*tag
, gfc_expr
**v
)
965 m
= gfc_match (tag
->spec
, &result
);
971 gfc_error ("Duplicate %s specification at %C", tag
->name
);
972 gfc_free_expr (result
);
976 if (result
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
978 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
979 gfc_free_expr (result
);
983 if (gfc_pure (NULL
) && gfc_impure_variable (result
->symtree
->n
.sym
))
985 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
986 gfc_free_expr (result
);
995 /* Match I/O tags that cause variables to become redefined. */
998 match_out_tag(const io_tag
*tag
, gfc_expr
**result
)
1002 m
= match_vtag(tag
, result
);
1004 gfc_check_do_variable((*result
)->symtree
);
1010 /* Match a label I/O tag. */
1013 match_ltag (const io_tag
*tag
, gfc_st_label
** label
)
1019 m
= gfc_match (tag
->spec
, label
);
1020 if (m
== MATCH_YES
&& old
!= 0)
1022 gfc_error ("Duplicate %s label specification at %C", tag
->name
);
1027 && gfc_reference_st_label (*label
, ST_LABEL_TARGET
) == FAILURE
)
1034 /* Do expression resolution and type-checking on an expression tag. */
1037 resolve_tag (const io_tag
*tag
, gfc_expr
*e
)
1042 if (gfc_resolve_expr (e
) == FAILURE
)
1045 if (e
->ts
.type
!= tag
->type
&& tag
!= &tag_format
)
1047 gfc_error ("%s tag at %L must be of type %s", tag
->name
,
1048 &e
->where
, gfc_basic_typename (tag
->type
));
1052 if (tag
== &tag_format
)
1054 if (e
->expr_type
== EXPR_CONSTANT
1055 && (e
->ts
.type
!= BT_CHARACTER
1056 || e
->ts
.kind
!= gfc_default_character_kind
))
1058 gfc_error ("Constant expression in FORMAT tag at %L must be "
1059 "of type default CHARACTER", &e
->where
);
1063 /* If e's rank is zero and e is not an element of an array, it should be
1064 of integer or character type. The integer variable should be
1066 if (e
->symtree
== NULL
|| e
->symtree
->n
.sym
->as
== NULL
1067 || e
->symtree
->n
.sym
->as
->rank
== 0)
1069 if (e
->ts
.type
!= BT_CHARACTER
&& e
->ts
.type
!= BT_INTEGER
)
1071 gfc_error ("%s tag at %L must be of type %s or %s", tag
->name
,
1072 &e
->where
, gfc_basic_typename (BT_CHARACTER
),
1073 gfc_basic_typename (BT_INTEGER
));
1076 else if (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_VARIABLE
)
1078 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: ASSIGNED "
1079 "variable in FORMAT tag at %L", &e
->where
)
1082 if (e
->symtree
->n
.sym
->attr
.assign
!= 1)
1084 gfc_error ("Variable '%s' at %L has not been assigned a "
1085 "format label", e
->symtree
->n
.sym
->name
,
1090 else if (e
->ts
.type
== BT_INTEGER
)
1092 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1093 "variable", gfc_basic_typename (e
->ts
.type
),
1102 /* if rank is nonzero, we allow the type to be character under
1103 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1104 assigned an Hollerith constant. */
1105 if (e
->ts
.type
== BT_CHARACTER
)
1107 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Character array "
1108 "in FORMAT tag at %L", &e
->where
)
1114 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: Non-character "
1115 "in FORMAT tag at %L", &e
->where
)
1126 gfc_error ("%s tag at %L must be scalar", tag
->name
, &e
->where
);
1130 if (tag
== &tag_iomsg
)
1132 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IOMSG tag at %L",
1133 &e
->where
) == FAILURE
)
1137 if (tag
== &tag_iostat
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1139 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1140 "INTEGER in IOSTAT tag at %L", &e
->where
)
1145 if (tag
== &tag_size
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1147 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 95 requires default "
1148 "INTEGER in SIZE tag at %L", &e
->where
)
1153 if (tag
== &tag_convert
)
1155 if (gfc_notify_std (GFC_STD_GNU
, "Extension: CONVERT tag at %L",
1156 &e
->where
) == FAILURE
)
1160 if (tag
== &tag_iolength
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1162 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 95 requires default "
1163 "INTEGER in IOLENGTH tag at %L", &e
->where
)
1173 /* Match a single tag of an OPEN statement. */
1176 match_open_element (gfc_open
*open
)
1180 m
= match_etag (&tag_unit
, &open
->unit
);
1183 m
= match_out_tag (&tag_iomsg
, &open
->iomsg
);
1186 m
= match_out_tag (&tag_iostat
, &open
->iostat
);
1189 m
= match_etag (&tag_file
, &open
->file
);
1192 m
= match_etag (&tag_status
, &open
->status
);
1195 m
= match_etag (&tag_e_access
, &open
->access
);
1198 m
= match_etag (&tag_e_form
, &open
->form
);
1201 m
= match_etag (&tag_e_recl
, &open
->recl
);
1204 m
= match_etag (&tag_e_blank
, &open
->blank
);
1207 m
= match_etag (&tag_e_position
, &open
->position
);
1210 m
= match_etag (&tag_e_action
, &open
->action
);
1213 m
= match_etag (&tag_e_delim
, &open
->delim
);
1216 m
= match_etag (&tag_e_pad
, &open
->pad
);
1219 m
= match_ltag (&tag_err
, &open
->err
);
1222 m
= match_etag (&tag_convert
, &open
->convert
);
1230 /* Free the gfc_open structure and all the expressions it contains. */
1233 gfc_free_open (gfc_open
*open
)
1238 gfc_free_expr (open
->unit
);
1239 gfc_free_expr (open
->iomsg
);
1240 gfc_free_expr (open
->iostat
);
1241 gfc_free_expr (open
->file
);
1242 gfc_free_expr (open
->status
);
1243 gfc_free_expr (open
->access
);
1244 gfc_free_expr (open
->form
);
1245 gfc_free_expr (open
->recl
);
1246 gfc_free_expr (open
->blank
);
1247 gfc_free_expr (open
->position
);
1248 gfc_free_expr (open
->action
);
1249 gfc_free_expr (open
->delim
);
1250 gfc_free_expr (open
->pad
);
1251 gfc_free_expr (open
->convert
);
1256 /* Resolve everything in a gfc_open structure. */
1259 gfc_resolve_open (gfc_open
*open
)
1262 RESOLVE_TAG (&tag_unit
, open
->unit
);
1263 RESOLVE_TAG (&tag_iomsg
, open
->iomsg
);
1264 RESOLVE_TAG (&tag_iostat
, open
->iostat
);
1265 RESOLVE_TAG (&tag_file
, open
->file
);
1266 RESOLVE_TAG (&tag_status
, open
->status
);
1267 RESOLVE_TAG (&tag_e_access
, open
->access
);
1268 RESOLVE_TAG (&tag_e_form
, open
->form
);
1269 RESOLVE_TAG (&tag_e_recl
, open
->recl
);
1270 RESOLVE_TAG (&tag_e_blank
, open
->blank
);
1271 RESOLVE_TAG (&tag_e_position
, open
->position
);
1272 RESOLVE_TAG (&tag_e_action
, open
->action
);
1273 RESOLVE_TAG (&tag_e_delim
, open
->delim
);
1274 RESOLVE_TAG (&tag_e_pad
, open
->pad
);
1275 RESOLVE_TAG (&tag_convert
, open
->convert
);
1277 if (gfc_reference_st_label (open
->err
, ST_LABEL_TARGET
) == FAILURE
)
1284 /* Check if a given value for a SPECIFIER is either in the list of values
1285 allowed in F95 or F2003, issuing an error message and returning a zero
1286 value if it is not allowed. */
1289 compare_to_allowed_values (const char *specifier
, const char *allowed
[],
1290 const char *allowed_f2003
[],
1291 const char *allowed_gnu
[], char *value
,
1292 const char *statement
, bool warn
)
1297 len
= strlen (value
);
1300 for (len
--; len
> 0; len
--)
1301 if (value
[len
] != ' ')
1306 for (i
= 0; allowed
[i
]; i
++)
1307 if (len
== strlen (allowed
[i
])
1308 && strncasecmp (value
, allowed
[i
], strlen (allowed
[i
])) == 0)
1311 for (i
= 0; allowed_f2003
&& allowed_f2003
[i
]; i
++)
1312 if (len
== strlen (allowed_f2003
[i
])
1313 && strncasecmp (value
, allowed_f2003
[i
], strlen (allowed_f2003
[i
]))
1316 notification n
= gfc_notification_std (GFC_STD_F2003
);
1318 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1320 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1321 "has value '%s'", specifier
, statement
,
1328 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: %s specifier in "
1329 "%s statement at %C has value '%s'", specifier
,
1330 statement
, allowed_f2003
[i
]);
1338 for (i
= 0; allowed_gnu
&& allowed_gnu
[i
]; i
++)
1339 if (len
== strlen (allowed_gnu
[i
])
1340 && strncasecmp (value
, allowed_gnu
[i
], strlen (allowed_gnu
[i
])) == 0)
1342 notification n
= gfc_notification_std (GFC_STD_GNU
);
1344 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1346 gfc_warning ("Extension: %s specifier in %s statement at %C "
1347 "has value '%s'", specifier
, statement
,
1354 gfc_notify_std (GFC_STD_GNU
, "Extension: %s specifier in "
1355 "%s statement at %C has value '%s'", specifier
,
1356 statement
, allowed_gnu
[i
]);
1366 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1367 specifier
, statement
, value
);
1372 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1373 specifier
, statement
, value
);
1379 /* Match an OPEN statement. */
1382 gfc_match_open (void)
1388 m
= gfc_match_char ('(');
1392 open
= gfc_getmem (sizeof (gfc_open
));
1394 m
= match_open_element (open
);
1396 if (m
== MATCH_ERROR
)
1400 m
= gfc_match_expr (&open
->unit
);
1403 if (m
== MATCH_ERROR
)
1409 if (gfc_match_char (')') == MATCH_YES
)
1411 if (gfc_match_char (',') != MATCH_YES
)
1414 m
= match_open_element (open
);
1415 if (m
== MATCH_ERROR
)
1421 if (gfc_match_eos () == MATCH_NO
)
1424 if (gfc_pure (NULL
))
1426 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1430 warn
= (open
->err
|| open
->iostat
) ? true : false;
1431 /* Checks on the ACCESS specifier. */
1432 if (open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
)
1434 static const char *access_f95
[] = { "SEQUENTIAL", "DIRECT", NULL
};
1435 static const char *access_f2003
[] = { "STREAM", NULL
};
1436 static const char *access_gnu
[] = { "APPEND", NULL
};
1438 if (!compare_to_allowed_values ("ACCESS", access_f95
, access_f2003
,
1440 open
->access
->value
.character
.string
,
1445 /* Checks on the ACTION specifier. */
1446 if (open
->action
&& open
->action
->expr_type
== EXPR_CONSTANT
)
1448 static const char *action
[] = { "READ", "WRITE", "READWRITE", NULL
};
1450 if (!compare_to_allowed_values ("ACTION", action
, NULL
, NULL
,
1451 open
->action
->value
.character
.string
,
1456 /* Checks on the ASYNCHRONOUS specifier. */
1457 /* TODO: code is ready, just needs uncommenting when async I/O support
1459 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1461 static const char * asynchronous[] = { "YES", "NO", NULL };
1463 if (!compare_to_allowed_values
1464 ("action", asynchronous, NULL, NULL,
1465 open->asynchronous->value.character.string, "OPEN", warn))
1469 /* Checks on the BLANK specifier. */
1470 if (open
->blank
&& open
->blank
->expr_type
== EXPR_CONSTANT
)
1472 static const char *blank
[] = { "ZERO", "NULL", NULL
};
1474 if (!compare_to_allowed_values ("BLANK", blank
, NULL
, NULL
,
1475 open
->blank
->value
.character
.string
,
1480 /* Checks on the DECIMAL specifier. */
1481 /* TODO: uncomment this code when DECIMAL support is added
1482 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1484 static const char * decimal[] = { "COMMA", "POINT", NULL };
1486 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1487 open->decimal->value.character.string,
1492 /* Checks on the DELIM specifier. */
1493 if (open
->delim
&& open
->delim
->expr_type
== EXPR_CONSTANT
)
1495 static const char *delim
[] = { "APOSTROPHE", "QUOTE", "NONE", NULL
};
1497 if (!compare_to_allowed_values ("DELIM", delim
, NULL
, NULL
,
1498 open
->delim
->value
.character
.string
,
1503 /* Checks on the ENCODING specifier. */
1504 /* TODO: uncomment this code when ENCODING support is added
1505 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1507 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1509 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1510 open->encoding->value.character.string,
1515 /* Checks on the FORM specifier. */
1516 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
)
1518 static const char *form
[] = { "FORMATTED", "UNFORMATTED", NULL
};
1520 if (!compare_to_allowed_values ("FORM", form
, NULL
, NULL
,
1521 open
->form
->value
.character
.string
,
1526 /* Checks on the PAD specifier. */
1527 if (open
->pad
&& open
->pad
->expr_type
== EXPR_CONSTANT
)
1529 static const char *pad
[] = { "YES", "NO", NULL
};
1531 if (!compare_to_allowed_values ("PAD", pad
, NULL
, NULL
,
1532 open
->pad
->value
.character
.string
,
1537 /* Checks on the POSITION specifier. */
1538 if (open
->position
&& open
->position
->expr_type
== EXPR_CONSTANT
)
1540 static const char *position
[] = { "ASIS", "REWIND", "APPEND", NULL
};
1542 if (!compare_to_allowed_values ("POSITION", position
, NULL
, NULL
,
1543 open
->position
->value
.character
.string
,
1548 /* Checks on the ROUND specifier. */
1549 /* TODO: uncomment this code when ROUND support is added
1550 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1552 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1553 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1555 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1556 open->round->value.character.string,
1561 /* Checks on the SIGN specifier. */
1562 /* TODO: uncomment this code when SIGN support is added
1563 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1565 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1568 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1569 open->sign->value.character.string,
1574 #define warn_or_error(...) \
1577 gfc_warning (__VA_ARGS__); \
1580 gfc_error (__VA_ARGS__); \
1585 /* Checks on the RECL specifier. */
1586 if (open
->recl
&& open
->recl
->expr_type
== EXPR_CONSTANT
1587 && open
->recl
->ts
.type
== BT_INTEGER
1588 && mpz_sgn (open
->recl
->value
.integer
) != 1)
1590 warn_or_error ("RECL in OPEN statement at %C must be positive");
1593 /* Checks on the STATUS specifier. */
1594 if (open
->status
&& open
->status
->expr_type
== EXPR_CONSTANT
)
1596 static const char *status
[] = { "OLD", "NEW", "SCRATCH",
1597 "REPLACE", "UNKNOWN", NULL
};
1599 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1600 open
->status
->value
.character
.string
,
1604 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1605 the FILE= specifier shall appear. */
1606 if (open
->file
== NULL
1607 && (strncasecmp (open
->status
->value
.character
.string
, "replace", 7)
1609 || strncasecmp (open
->status
->value
.character
.string
, "new", 3)
1612 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1613 "'%s' and no FILE specifier is present",
1614 open
->status
->value
.character
.string
);
1617 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1618 the FILE= specifier shall not appear. */
1619 if (strncasecmp (open
->status
->value
.character
.string
, "scratch", 7)
1622 warn_or_error ("The STATUS specified in OPEN statement at %C "
1623 "cannot have the value SCRATCH if a FILE specifier "
1628 /* Things that are not allowed for unformatted I/O. */
1629 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
1631 /* TODO uncomment this code when F2003 support is finished */
1632 /* || open->decimal || open->encoding || open->round
1634 || open
->pad
|| open
->blank
)
1635 && strncasecmp (open
->form
->value
.character
.string
,
1636 "unformatted", 11) == 0)
1638 const char *spec
= (open
->delim
? "DELIM "
1639 : (open
->pad
? "PAD " : open
->blank
1642 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1643 "unformatted I/O", spec
);
1646 if (open
->recl
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1647 && strncasecmp (open
->access
->value
.character
.string
, "stream", 6) == 0)
1649 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1654 && open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1655 && !(strncasecmp (open
->access
->value
.character
.string
,
1656 "sequential", 10) == 0
1657 || strncasecmp (open
->access
->value
.character
.string
,
1659 || strncasecmp (open
->access
->value
.character
.string
,
1662 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1663 "for stream or sequential ACCESS");
1666 #undef warn_or_error
1668 new_st
.op
= EXEC_OPEN
;
1669 new_st
.ext
.open
= open
;
1673 gfc_syntax_error (ST_OPEN
);
1676 gfc_free_open (open
);
1681 /* Free a gfc_close structure an all its expressions. */
1684 gfc_free_close (gfc_close
*close
)
1689 gfc_free_expr (close
->unit
);
1690 gfc_free_expr (close
->iomsg
);
1691 gfc_free_expr (close
->iostat
);
1692 gfc_free_expr (close
->status
);
1697 /* Match elements of a CLOSE statement. */
1700 match_close_element (gfc_close
*close
)
1704 m
= match_etag (&tag_unit
, &close
->unit
);
1707 m
= match_etag (&tag_status
, &close
->status
);
1710 m
= match_out_tag (&tag_iomsg
, &close
->iomsg
);
1713 m
= match_out_tag (&tag_iostat
, &close
->iostat
);
1716 m
= match_ltag (&tag_err
, &close
->err
);
1724 /* Match a CLOSE statement. */
1727 gfc_match_close (void)
1733 m
= gfc_match_char ('(');
1737 close
= gfc_getmem (sizeof (gfc_close
));
1739 m
= match_close_element (close
);
1741 if (m
== MATCH_ERROR
)
1745 m
= gfc_match_expr (&close
->unit
);
1748 if (m
== MATCH_ERROR
)
1754 if (gfc_match_char (')') == MATCH_YES
)
1756 if (gfc_match_char (',') != MATCH_YES
)
1759 m
= match_close_element (close
);
1760 if (m
== MATCH_ERROR
)
1766 if (gfc_match_eos () == MATCH_NO
)
1769 if (gfc_pure (NULL
))
1771 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1775 warn
= (close
->iostat
|| close
->err
) ? true : false;
1777 /* Checks on the STATUS specifier. */
1778 if (close
->status
&& close
->status
->expr_type
== EXPR_CONSTANT
)
1780 static const char *status
[] = { "KEEP", "DELETE", NULL
};
1782 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1783 close
->status
->value
.character
.string
,
1788 new_st
.op
= EXEC_CLOSE
;
1789 new_st
.ext
.close
= close
;
1793 gfc_syntax_error (ST_CLOSE
);
1796 gfc_free_close (close
);
1801 /* Resolve everything in a gfc_close structure. */
1804 gfc_resolve_close (gfc_close
*close
)
1806 RESOLVE_TAG (&tag_unit
, close
->unit
);
1807 RESOLVE_TAG (&tag_iomsg
, close
->iomsg
);
1808 RESOLVE_TAG (&tag_iostat
, close
->iostat
);
1809 RESOLVE_TAG (&tag_status
, close
->status
);
1811 if (gfc_reference_st_label (close
->err
, ST_LABEL_TARGET
) == FAILURE
)
1818 /* Free a gfc_filepos structure. */
1821 gfc_free_filepos (gfc_filepos
*fp
)
1823 gfc_free_expr (fp
->unit
);
1824 gfc_free_expr (fp
->iomsg
);
1825 gfc_free_expr (fp
->iostat
);
1830 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1833 match_file_element (gfc_filepos
*fp
)
1837 m
= match_etag (&tag_unit
, &fp
->unit
);
1840 m
= match_out_tag (&tag_iomsg
, &fp
->iomsg
);
1843 m
= match_out_tag (&tag_iostat
, &fp
->iostat
);
1846 m
= match_ltag (&tag_err
, &fp
->err
);
1854 /* Match the second half of the file-positioning statements, REWIND,
1855 BACKSPACE, ENDFILE, or the FLUSH statement. */
1858 match_filepos (gfc_statement st
, gfc_exec_op op
)
1863 fp
= gfc_getmem (sizeof (gfc_filepos
));
1865 if (gfc_match_char ('(') == MATCH_NO
)
1867 m
= gfc_match_expr (&fp
->unit
);
1868 if (m
== MATCH_ERROR
)
1876 m
= match_file_element (fp
);
1877 if (m
== MATCH_ERROR
)
1881 m
= gfc_match_expr (&fp
->unit
);
1882 if (m
== MATCH_ERROR
)
1890 if (gfc_match_char (')') == MATCH_YES
)
1892 if (gfc_match_char (',') != MATCH_YES
)
1895 m
= match_file_element (fp
);
1896 if (m
== MATCH_ERROR
)
1903 if (gfc_match_eos () != MATCH_YES
)
1906 if (gfc_pure (NULL
))
1908 gfc_error ("%s statement not allowed in PURE procedure at %C",
1909 gfc_ascii_statement (st
));
1915 new_st
.ext
.filepos
= fp
;
1919 gfc_syntax_error (st
);
1922 gfc_free_filepos (fp
);
1928 gfc_resolve_filepos (gfc_filepos
*fp
)
1930 RESOLVE_TAG (&tag_unit
, fp
->unit
);
1931 RESOLVE_TAG (&tag_iostat
, fp
->iostat
);
1932 RESOLVE_TAG (&tag_iomsg
, fp
->iomsg
);
1933 if (gfc_reference_st_label (fp
->err
, ST_LABEL_TARGET
) == FAILURE
)
1940 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1941 and the FLUSH statement. */
1944 gfc_match_endfile (void)
1946 return match_filepos (ST_END_FILE
, EXEC_ENDFILE
);
1950 gfc_match_backspace (void)
1952 return match_filepos (ST_BACKSPACE
, EXEC_BACKSPACE
);
1956 gfc_match_rewind (void)
1958 return match_filepos (ST_REWIND
, EXEC_REWIND
);
1962 gfc_match_flush (void)
1964 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: FLUSH statement at %C")
1968 return match_filepos (ST_FLUSH
, EXEC_FLUSH
);
1971 /******************** Data Transfer Statements *********************/
1974 { M_READ
, M_WRITE
, M_PRINT
, M_INQUIRE
}
1978 /* Return a default unit number. */
1981 default_unit (io_kind k
)
1990 return gfc_int_expr (unit
);
1994 /* Match a unit specification for a data transfer statement. */
1997 match_dt_unit (io_kind k
, gfc_dt
*dt
)
2001 if (gfc_match_char ('*') == MATCH_YES
)
2003 if (dt
->io_unit
!= NULL
)
2006 dt
->io_unit
= default_unit (k
);
2010 if (gfc_match_expr (&e
) == MATCH_YES
)
2012 if (dt
->io_unit
!= NULL
)
2025 gfc_error ("Duplicate UNIT specification at %C");
2030 /* Match a format specification. */
2033 match_dt_format (gfc_dt
*dt
)
2037 gfc_st_label
*label
;
2039 where
= gfc_current_locus
;
2041 if (gfc_match_char ('*') == MATCH_YES
)
2043 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2046 dt
->format_label
= &format_asterisk
;
2050 if (gfc_match_st_label (&label
) == MATCH_YES
)
2052 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2054 gfc_free_st_label (label
);
2058 if (gfc_reference_st_label (label
, ST_LABEL_FORMAT
) == FAILURE
)
2061 dt
->format_label
= label
;
2065 if (gfc_match_expr (&e
) == MATCH_YES
)
2067 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2072 dt
->format_expr
= e
;
2076 gfc_current_locus
= where
; /* The only case where we have to restore */
2081 gfc_error ("Duplicate format specification at %C");
2086 /* Traverse a namelist that is part of a READ statement to make sure
2087 that none of the variables in the namelist are INTENT(IN). Returns
2088 nonzero if we find such a variable. */
2091 check_namelist (gfc_symbol
*sym
)
2095 for (p
= sym
->namelist
; p
; p
= p
->next
)
2096 if (p
->sym
->attr
.intent
== INTENT_IN
)
2098 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2099 p
->sym
->name
, sym
->name
);
2107 /* Match a single data transfer element. */
2110 match_dt_element (io_kind k
, gfc_dt
*dt
)
2112 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2116 if (gfc_match (" unit =") == MATCH_YES
)
2118 m
= match_dt_unit (k
, dt
);
2123 if (gfc_match (" fmt =") == MATCH_YES
)
2125 m
= match_dt_format (dt
);
2130 if (gfc_match (" nml = %n", name
) == MATCH_YES
)
2132 if (dt
->namelist
!= NULL
)
2134 gfc_error ("Duplicate NML specification at %C");
2138 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2141 if (sym
== NULL
|| sym
->attr
.flavor
!= FL_NAMELIST
)
2143 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2144 sym
!= NULL
? sym
->name
: name
);
2149 if (k
== M_READ
&& check_namelist (sym
))
2155 m
= match_etag (&tag_rec
, &dt
->rec
);
2158 m
= match_etag (&tag_spos
, &dt
->rec
);
2161 m
= match_out_tag (&tag_iomsg
, &dt
->iomsg
);
2164 m
= match_out_tag (&tag_iostat
, &dt
->iostat
);
2167 m
= match_ltag (&tag_err
, &dt
->err
);
2169 dt
->err_where
= gfc_current_locus
;
2172 m
= match_etag (&tag_advance
, &dt
->advance
);
2175 m
= match_out_tag (&tag_size
, &dt
->size
);
2179 m
= match_ltag (&tag_end
, &dt
->end
);
2184 gfc_error ("END tag at %C not allowed in output statement");
2187 dt
->end_where
= gfc_current_locus
;
2192 m
= match_ltag (&tag_eor
, &dt
->eor
);
2194 dt
->eor_where
= gfc_current_locus
;
2202 /* Free a data transfer structure and everything below it. */
2205 gfc_free_dt (gfc_dt
*dt
)
2210 gfc_free_expr (dt
->io_unit
);
2211 gfc_free_expr (dt
->format_expr
);
2212 gfc_free_expr (dt
->rec
);
2213 gfc_free_expr (dt
->advance
);
2214 gfc_free_expr (dt
->iomsg
);
2215 gfc_free_expr (dt
->iostat
);
2216 gfc_free_expr (dt
->size
);
2221 /* Resolve everything in a gfc_dt structure. */
2224 gfc_resolve_dt (gfc_dt
*dt
)
2228 RESOLVE_TAG (&tag_format
, dt
->format_expr
);
2229 RESOLVE_TAG (&tag_rec
, dt
->rec
);
2230 RESOLVE_TAG (&tag_spos
, dt
->rec
);
2231 RESOLVE_TAG (&tag_advance
, dt
->advance
);
2232 RESOLVE_TAG (&tag_iomsg
, dt
->iomsg
);
2233 RESOLVE_TAG (&tag_iostat
, dt
->iostat
);
2234 RESOLVE_TAG (&tag_size
, dt
->size
);
2237 if (gfc_resolve_expr (e
) == SUCCESS
2238 && (e
->ts
.type
!= BT_INTEGER
2239 && (e
->ts
.type
!= BT_CHARACTER
|| e
->expr_type
!= EXPR_VARIABLE
)))
2241 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2242 "or a CHARACTER variable", &e
->where
);
2246 if (e
->ts
.type
== BT_CHARACTER
)
2248 if (gfc_has_vector_index (e
))
2250 gfc_error ("Internal unit with vector subscript at %L", &e
->where
);
2255 if (e
->rank
&& e
->ts
.type
!= BT_CHARACTER
)
2257 gfc_error ("External IO UNIT cannot be an array at %L", &e
->where
);
2263 if (gfc_reference_st_label (dt
->err
, ST_LABEL_TARGET
) == FAILURE
)
2265 if (dt
->err
->defined
== ST_LABEL_UNKNOWN
)
2267 gfc_error ("ERR tag label %d at %L not defined",
2268 dt
->err
->value
, &dt
->err_where
);
2275 if (gfc_reference_st_label (dt
->end
, ST_LABEL_TARGET
) == FAILURE
)
2277 if (dt
->end
->defined
== ST_LABEL_UNKNOWN
)
2279 gfc_error ("END tag label %d at %L not defined",
2280 dt
->end
->value
, &dt
->end_where
);
2287 if (gfc_reference_st_label (dt
->eor
, ST_LABEL_TARGET
) == FAILURE
)
2289 if (dt
->eor
->defined
== ST_LABEL_UNKNOWN
)
2291 gfc_error ("EOR tag label %d at %L not defined",
2292 dt
->eor
->value
, &dt
->eor_where
);
2297 /* Check the format label actually exists. */
2298 if (dt
->format_label
&& dt
->format_label
!= &format_asterisk
2299 && dt
->format_label
->defined
== ST_LABEL_UNKNOWN
)
2301 gfc_error ("FORMAT label %d at %L not defined", dt
->format_label
->value
,
2302 &dt
->format_label
->where
);
2309 /* Given an io_kind, return its name. */
2312 io_kind_name (io_kind k
)
2331 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2338 /* Match an IO iteration statement of the form:
2340 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2342 which is equivalent to a single IO element. This function is
2343 mutually recursive with match_io_element(). */
2345 static match
match_io_element (io_kind
, gfc_code
**);
2348 match_io_iterator (io_kind k
, gfc_code
**result
)
2350 gfc_code
*head
, *tail
, *new;
2358 old_loc
= gfc_current_locus
;
2360 if (gfc_match_char ('(') != MATCH_YES
)
2363 m
= match_io_element (k
, &head
);
2366 if (m
!= MATCH_YES
|| gfc_match_char (',') != MATCH_YES
)
2372 /* Can't be anything but an IO iterator. Build a list. */
2373 iter
= gfc_get_iterator ();
2377 m
= gfc_match_iterator (iter
, 0);
2378 if (m
== MATCH_ERROR
)
2382 gfc_check_do_variable (iter
->var
->symtree
);
2386 m
= match_io_element (k
, &new);
2387 if (m
== MATCH_ERROR
)
2396 tail
= gfc_append_code (tail
, new);
2398 if (gfc_match_char (',') != MATCH_YES
)
2407 if (gfc_match_char (')') != MATCH_YES
)
2410 new = gfc_get_code ();
2412 new->ext
.iterator
= iter
;
2414 new->block
= gfc_get_code ();
2415 new->block
->op
= EXEC_DO
;
2416 new->block
->next
= head
;
2422 gfc_error ("Syntax error in I/O iterator at %C");
2426 gfc_free_iterator (iter
, 1);
2427 gfc_free_statements (head
);
2428 gfc_current_locus
= old_loc
;
2433 /* Match a single element of an IO list, which is either a single
2434 expression or an IO Iterator. */
2437 match_io_element (io_kind k
, gfc_code
**cpp
)
2445 m
= match_io_iterator (k
, cpp
);
2451 m
= gfc_match_variable (&expr
, 0);
2453 gfc_error ("Expected variable in READ statement at %C");
2457 m
= gfc_match_expr (&expr
);
2459 gfc_error ("Expected expression in %s statement at %C",
2467 if (expr
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
2469 gfc_error ("Variable '%s' in input list at %C cannot be "
2470 "INTENT(IN)", expr
->symtree
->n
.sym
->name
);
2475 && gfc_impure_variable (expr
->symtree
->n
.sym
)
2476 && current_dt
->io_unit
->ts
.type
== BT_CHARACTER
)
2478 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2479 expr
->symtree
->n
.sym
->name
);
2483 if (gfc_check_do_variable (expr
->symtree
))
2489 if (current_dt
->io_unit
->ts
.type
== BT_CHARACTER
2491 && current_dt
->io_unit
->expr_type
== EXPR_VARIABLE
2492 && gfc_impure_variable (current_dt
->io_unit
->symtree
->n
.sym
))
2494 gfc_error ("Cannot write to internal file unit '%s' at %C "
2495 "inside a PURE procedure",
2496 current_dt
->io_unit
->symtree
->n
.sym
->name
);
2508 gfc_free_expr (expr
);
2512 cp
= gfc_get_code ();
2513 cp
->op
= EXEC_TRANSFER
;
2521 /* Match an I/O list, building gfc_code structures as we go. */
2524 match_io_list (io_kind k
, gfc_code
**head_p
)
2526 gfc_code
*head
, *tail
, *new;
2529 *head_p
= head
= tail
= NULL
;
2530 if (gfc_match_eos () == MATCH_YES
)
2535 m
= match_io_element (k
, &new);
2536 if (m
== MATCH_ERROR
)
2541 tail
= gfc_append_code (tail
, new);
2545 if (gfc_match_eos () == MATCH_YES
)
2547 if (gfc_match_char (',') != MATCH_YES
)
2555 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2558 gfc_free_statements (head
);
2563 /* Attach the data transfer end node. */
2566 terminate_io (gfc_code
*io_code
)
2570 if (io_code
== NULL
)
2571 io_code
= new_st
.block
;
2573 c
= gfc_get_code ();
2574 c
->op
= EXEC_DT_END
;
2576 /* Point to structure that is already there */
2577 c
->ext
.dt
= new_st
.ext
.dt
;
2578 gfc_append_code (io_code
, c
);
2582 /* Check the constraints for a data transfer statement. The majority of the
2583 constraints appearing in 9.4 of the standard appear here. Some are handled
2584 in resolve_tag and others in gfc_resolve_dt. */
2587 check_io_constraints (io_kind k
, gfc_dt
*dt
, gfc_code
*io_code
,
2590 #define io_constraint(condition,msg,arg)\
2593 gfc_error(msg,arg);\
2599 gfc_symbol
*sym
= NULL
;
2604 if (expr
&& expr
->expr_type
== EXPR_VARIABLE
2605 && expr
->ts
.type
== BT_CHARACTER
)
2607 sym
= expr
->symtree
->n
.sym
;
2609 io_constraint (k
== M_WRITE
&& sym
->attr
.intent
== INTENT_IN
,
2610 "Internal file at %L must not be INTENT(IN)",
2613 io_constraint (gfc_has_vector_index (dt
->io_unit
),
2614 "Internal file incompatible with vector subscript at %L",
2617 io_constraint (dt
->rec
!= NULL
,
2618 "REC tag at %L is incompatible with internal file",
2621 if (dt
->namelist
!= NULL
)
2623 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Internal file "
2624 "at %L with namelist", &expr
->where
)
2629 io_constraint (dt
->advance
!= NULL
,
2630 "ADVANCE tag at %L is incompatible with internal file",
2631 &dt
->advance
->where
);
2634 if (expr
&& expr
->ts
.type
!= BT_CHARACTER
)
2637 io_constraint (gfc_pure (NULL
) && (k
== M_READ
|| k
== M_WRITE
),
2638 "IO UNIT in %s statement at %C must be "
2639 "an internal file in a PURE procedure",
2646 io_constraint (dt
->end
, "END tag not allowed with output at %L",
2649 io_constraint (dt
->eor
, "EOR tag not allowed with output at %L",
2652 io_constraint (k
!= M_READ
&& dt
->size
,
2653 "SIZE=specifier not allowed with output at %L",
2658 io_constraint (dt
->size
&& dt
->advance
== NULL
,
2659 "SIZE tag at %L requires an ADVANCE tag",
2662 io_constraint (dt
->eor
&& dt
->advance
== NULL
,
2663 "EOR tag at %L requires an ADVANCE tag",
2671 io_constraint (io_code
&& dt
->namelist
,
2672 "NAMELIST cannot be followed by IO-list at %L",
2675 io_constraint (dt
->format_expr
,
2676 "IO spec-list cannot contain both NAMELIST group name "
2677 "and format specification at %L.",
2678 &dt
->format_expr
->where
);
2680 io_constraint (dt
->format_label
,
2681 "IO spec-list cannot contain both NAMELIST group name "
2682 "and format label at %L", spec_end
);
2684 io_constraint (dt
->rec
,
2685 "NAMELIST IO is not allowed with a REC=specifier "
2686 "at %L.", &dt
->rec
->where
);
2688 io_constraint (dt
->advance
,
2689 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2690 "at %L.", &dt
->advance
->where
);
2695 io_constraint (dt
->end
,
2696 "An END tag is not allowed with a "
2697 "REC=specifier at %L.", &dt
->end_where
);
2700 io_constraint (dt
->format_label
== &format_asterisk
,
2701 "FMT=* is not allowed with a REC=specifier "
2702 "at %L.", spec_end
);
2707 int not_yes
, not_no
;
2710 io_constraint (dt
->format_label
== &format_asterisk
,
2711 "List directed format(*) is not allowed with a "
2712 "ADVANCE=specifier at %L.", &expr
->where
);
2714 io_constraint (dt
->format_expr
== NULL
&& dt
->format_label
== NULL
2715 && dt
->namelist
== NULL
,
2716 "the ADVANCE=specifier at %L must appear with an "
2717 "explicit format expression", &expr
->where
);
2719 if (expr
->expr_type
== EXPR_CONSTANT
&& expr
->ts
.type
== BT_CHARACTER
)
2721 const char * advance
= expr
->value
.character
.string
;
2722 not_no
= strcasecmp (advance
, "no") != 0;
2723 not_yes
= strcasecmp (advance
, "yes") != 0;
2731 io_constraint (not_no
&& not_yes
,
2732 "ADVANCE=specifier at %L must have value = "
2733 "YES or NO.", &expr
->where
);
2735 io_constraint (dt
->size
&& not_no
&& k
== M_READ
,
2736 "SIZE tag at %L requires an ADVANCE = 'NO'",
2739 io_constraint (dt
->eor
&& not_no
&& k
== M_READ
,
2740 "EOR tag at %L requires an ADVANCE = 'NO'",
2744 expr
= dt
->format_expr
;
2745 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
2746 && check_format_string (expr
, k
== M_READ
) == FAILURE
)
2751 #undef io_constraint
2754 /* Match a READ, WRITE or PRINT statement. */
2757 match_io (io_kind k
)
2759 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2768 where
= gfc_current_locus
;
2770 current_dt
= dt
= gfc_getmem (sizeof (gfc_dt
));
2771 m
= gfc_match_char ('(');
2774 where
= gfc_current_locus
;
2777 else if (k
== M_PRINT
)
2779 /* Treat the non-standard case of PRINT namelist. */
2780 if ((gfc_current_form
== FORM_FIXED
|| gfc_peek_char () == ' ')
2781 && gfc_match_name (name
) == MATCH_YES
)
2783 gfc_find_symbol (name
, NULL
, 1, &sym
);
2784 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2786 if (gfc_notify_std (GFC_STD_GNU
, "PRINT namelist at "
2787 "%C is an extension") == FAILURE
)
2793 dt
->io_unit
= default_unit (k
);
2798 gfc_current_locus
= where
;
2802 if (gfc_current_form
== FORM_FREE
)
2804 c
= gfc_peek_char();
2805 if (c
!= ' ' && c
!= '*' && c
!= '\'' && c
!= '"')
2812 m
= match_dt_format (dt
);
2813 if (m
== MATCH_ERROR
)
2819 dt
->io_unit
= default_unit (k
);
2824 /* Before issuing an error for a malformed 'print (1,*)' type of
2825 error, check for a default-char-expr of the form ('(I0)'). */
2826 if (k
== M_PRINT
&& m
== MATCH_YES
)
2828 /* Reset current locus to get the initial '(' in an expression. */
2829 gfc_current_locus
= where
;
2830 dt
->format_expr
= NULL
;
2831 m
= match_dt_format (dt
);
2833 if (m
== MATCH_ERROR
)
2835 if (m
== MATCH_NO
|| dt
->format_expr
== NULL
)
2839 dt
->io_unit
= default_unit (k
);
2844 /* Match a control list */
2845 if (match_dt_element (k
, dt
) == MATCH_YES
)
2847 if (match_dt_unit (k
, dt
) != MATCH_YES
)
2850 if (gfc_match_char (')') == MATCH_YES
)
2852 if (gfc_match_char (',') != MATCH_YES
)
2855 m
= match_dt_element (k
, dt
);
2858 if (m
== MATCH_ERROR
)
2861 m
= match_dt_format (dt
);
2864 if (m
== MATCH_ERROR
)
2867 where
= gfc_current_locus
;
2869 m
= gfc_match_name (name
);
2872 gfc_find_symbol (name
, NULL
, 1, &sym
);
2873 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2876 if (k
== M_READ
&& check_namelist (sym
))
2885 gfc_current_locus
= where
;
2887 goto loop
; /* No matches, try regular elements */
2890 if (gfc_match_char (')') == MATCH_YES
)
2892 if (gfc_match_char (',') != MATCH_YES
)
2898 m
= match_dt_element (k
, dt
);
2901 if (m
== MATCH_ERROR
)
2904 if (gfc_match_char (')') == MATCH_YES
)
2906 if (gfc_match_char (',') != MATCH_YES
)
2912 /* Used in check_io_constraints, where no locus is available. */
2913 spec_end
= gfc_current_locus
;
2915 /* Optional leading comma (non-standard). */
2917 && gfc_match_char (',') == MATCH_YES
2919 && gfc_notify_std (GFC_STD_GNU
, "Extension: Comma before output "
2920 "item list at %C is an extension") == FAILURE
)
2924 if (gfc_match_eos () != MATCH_YES
)
2926 if (comma_flag
&& gfc_match_char (',') != MATCH_YES
)
2928 gfc_error ("Expected comma in I/O list at %C");
2933 m
= match_io_list (k
, &io_code
);
2934 if (m
== MATCH_ERROR
)
2940 /* A full IO statement has been matched. Check the constraints. spec_end is
2941 supplied for cases where no locus is supplied. */
2942 m
= check_io_constraints (k
, dt
, io_code
, &spec_end
);
2944 if (m
== MATCH_ERROR
)
2947 new_st
.op
= (k
== M_READ
) ? EXEC_READ
: EXEC_WRITE
;
2949 new_st
.block
= gfc_get_code ();
2950 new_st
.block
->op
= new_st
.op
;
2951 new_st
.block
->next
= io_code
;
2953 terminate_io (io_code
);
2958 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2968 gfc_match_read (void)
2970 return match_io (M_READ
);
2974 gfc_match_write (void)
2976 return match_io (M_WRITE
);
2980 gfc_match_print (void)
2984 m
= match_io (M_PRINT
);
2988 if (gfc_pure (NULL
))
2990 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2998 /* Free a gfc_inquire structure. */
3001 gfc_free_inquire (gfc_inquire
*inquire
)
3004 if (inquire
== NULL
)
3007 gfc_free_expr (inquire
->unit
);
3008 gfc_free_expr (inquire
->file
);
3009 gfc_free_expr (inquire
->iomsg
);
3010 gfc_free_expr (inquire
->iostat
);
3011 gfc_free_expr (inquire
->exist
);
3012 gfc_free_expr (inquire
->opened
);
3013 gfc_free_expr (inquire
->number
);
3014 gfc_free_expr (inquire
->named
);
3015 gfc_free_expr (inquire
->name
);
3016 gfc_free_expr (inquire
->access
);
3017 gfc_free_expr (inquire
->sequential
);
3018 gfc_free_expr (inquire
->direct
);
3019 gfc_free_expr (inquire
->form
);
3020 gfc_free_expr (inquire
->formatted
);
3021 gfc_free_expr (inquire
->unformatted
);
3022 gfc_free_expr (inquire
->recl
);
3023 gfc_free_expr (inquire
->nextrec
);
3024 gfc_free_expr (inquire
->blank
);
3025 gfc_free_expr (inquire
->position
);
3026 gfc_free_expr (inquire
->action
);
3027 gfc_free_expr (inquire
->read
);
3028 gfc_free_expr (inquire
->write
);
3029 gfc_free_expr (inquire
->readwrite
);
3030 gfc_free_expr (inquire
->delim
);
3031 gfc_free_expr (inquire
->pad
);
3032 gfc_free_expr (inquire
->iolength
);
3033 gfc_free_expr (inquire
->convert
);
3034 gfc_free_expr (inquire
->strm_pos
);
3039 /* Match an element of an INQUIRE statement. */
3041 #define RETM if (m != MATCH_NO) return m;
3044 match_inquire_element (gfc_inquire
*inquire
)
3048 m
= match_etag (&tag_unit
, &inquire
->unit
);
3049 RETM m
= match_etag (&tag_file
, &inquire
->file
);
3050 RETM m
= match_ltag (&tag_err
, &inquire
->err
);
3051 RETM m
= match_out_tag (&tag_iomsg
, &inquire
->iomsg
);
3052 RETM m
= match_out_tag (&tag_iostat
, &inquire
->iostat
);
3053 RETM m
= match_vtag (&tag_exist
, &inquire
->exist
);
3054 RETM m
= match_vtag (&tag_opened
, &inquire
->opened
);
3055 RETM m
= match_vtag (&tag_named
, &inquire
->named
);
3056 RETM m
= match_vtag (&tag_name
, &inquire
->name
);
3057 RETM m
= match_out_tag (&tag_number
, &inquire
->number
);
3058 RETM m
= match_vtag (&tag_s_access
, &inquire
->access
);
3059 RETM m
= match_vtag (&tag_sequential
, &inquire
->sequential
);
3060 RETM m
= match_vtag (&tag_direct
, &inquire
->direct
);
3061 RETM m
= match_vtag (&tag_s_form
, &inquire
->form
);
3062 RETM m
= match_vtag (&tag_formatted
, &inquire
->formatted
);
3063 RETM m
= match_vtag (&tag_unformatted
, &inquire
->unformatted
);
3064 RETM m
= match_out_tag (&tag_s_recl
, &inquire
->recl
);
3065 RETM m
= match_out_tag (&tag_nextrec
, &inquire
->nextrec
);
3066 RETM m
= match_vtag (&tag_s_blank
, &inquire
->blank
);
3067 RETM m
= match_vtag (&tag_s_position
, &inquire
->position
);
3068 RETM m
= match_vtag (&tag_s_action
, &inquire
->action
);
3069 RETM m
= match_vtag (&tag_read
, &inquire
->read
);
3070 RETM m
= match_vtag (&tag_write
, &inquire
->write
);
3071 RETM m
= match_vtag (&tag_readwrite
, &inquire
->readwrite
);
3072 RETM m
= match_vtag (&tag_s_delim
, &inquire
->delim
);
3073 RETM m
= match_vtag (&tag_s_pad
, &inquire
->pad
);
3074 RETM m
= match_vtag (&tag_iolength
, &inquire
->iolength
);
3075 RETM m
= match_vtag (&tag_convert
, &inquire
->convert
);
3076 RETM m
= match_out_tag (&tag_strm_out
, &inquire
->strm_pos
);
3077 RETM
return MATCH_NO
;
3084 gfc_match_inquire (void)
3086 gfc_inquire
*inquire
;
3091 m
= gfc_match_char ('(');
3095 inquire
= gfc_getmem (sizeof (gfc_inquire
));
3097 loc
= gfc_current_locus
;
3099 m
= match_inquire_element (inquire
);
3100 if (m
== MATCH_ERROR
)
3104 m
= gfc_match_expr (&inquire
->unit
);
3105 if (m
== MATCH_ERROR
)
3111 /* See if we have the IOLENGTH form of the inquire statement. */
3112 if (inquire
->iolength
!= NULL
)
3114 if (gfc_match_char (')') != MATCH_YES
)
3117 m
= match_io_list (M_INQUIRE
, &code
);
3118 if (m
== MATCH_ERROR
)
3123 new_st
.op
= EXEC_IOLENGTH
;
3124 new_st
.expr
= inquire
->iolength
;
3125 new_st
.ext
.inquire
= inquire
;
3127 if (gfc_pure (NULL
))
3129 gfc_free_statements (code
);
3130 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3134 new_st
.block
= gfc_get_code ();
3135 new_st
.block
->op
= EXEC_IOLENGTH
;
3136 terminate_io (code
);
3137 new_st
.block
->next
= code
;
3141 /* At this point, we have the non-IOLENGTH inquire statement. */
3144 if (gfc_match_char (')') == MATCH_YES
)
3146 if (gfc_match_char (',') != MATCH_YES
)
3149 m
= match_inquire_element (inquire
);
3150 if (m
== MATCH_ERROR
)
3155 if (inquire
->iolength
!= NULL
)
3157 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3162 if (gfc_match_eos () != MATCH_YES
)
3165 if (inquire
->unit
!= NULL
&& inquire
->file
!= NULL
)
3167 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3168 "UNIT specifiers", &loc
);
3172 if (inquire
->unit
== NULL
&& inquire
->file
== NULL
)
3174 gfc_error ("INQUIRE statement at %L requires either FILE or "
3175 "UNIT specifier", &loc
);
3179 if (gfc_pure (NULL
))
3181 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3185 new_st
.op
= EXEC_INQUIRE
;
3186 new_st
.ext
.inquire
= inquire
;
3190 gfc_syntax_error (ST_INQUIRE
);
3193 gfc_free_inquire (inquire
);
3198 /* Resolve everything in a gfc_inquire structure. */
3201 gfc_resolve_inquire (gfc_inquire
*inquire
)
3203 RESOLVE_TAG (&tag_unit
, inquire
->unit
);
3204 RESOLVE_TAG (&tag_file
, inquire
->file
);
3205 RESOLVE_TAG (&tag_iomsg
, inquire
->iomsg
);
3206 RESOLVE_TAG (&tag_iostat
, inquire
->iostat
);
3207 RESOLVE_TAG (&tag_exist
, inquire
->exist
);
3208 RESOLVE_TAG (&tag_opened
, inquire
->opened
);
3209 RESOLVE_TAG (&tag_number
, inquire
->number
);
3210 RESOLVE_TAG (&tag_named
, inquire
->named
);
3211 RESOLVE_TAG (&tag_name
, inquire
->name
);
3212 RESOLVE_TAG (&tag_s_access
, inquire
->access
);
3213 RESOLVE_TAG (&tag_sequential
, inquire
->sequential
);
3214 RESOLVE_TAG (&tag_direct
, inquire
->direct
);
3215 RESOLVE_TAG (&tag_s_form
, inquire
->form
);
3216 RESOLVE_TAG (&tag_formatted
, inquire
->formatted
);
3217 RESOLVE_TAG (&tag_unformatted
, inquire
->unformatted
);
3218 RESOLVE_TAG (&tag_s_recl
, inquire
->recl
);
3219 RESOLVE_TAG (&tag_nextrec
, inquire
->nextrec
);
3220 RESOLVE_TAG (&tag_s_blank
, inquire
->blank
);
3221 RESOLVE_TAG (&tag_s_position
, inquire
->position
);
3222 RESOLVE_TAG (&tag_s_action
, inquire
->action
);
3223 RESOLVE_TAG (&tag_read
, inquire
->read
);
3224 RESOLVE_TAG (&tag_write
, inquire
->write
);
3225 RESOLVE_TAG (&tag_readwrite
, inquire
->readwrite
);
3226 RESOLVE_TAG (&tag_s_delim
, inquire
->delim
);
3227 RESOLVE_TAG (&tag_s_pad
, inquire
->pad
);
3228 RESOLVE_TAG (&tag_iolength
, inquire
->iolength
);
3229 RESOLVE_TAG (&tag_convert
, inquire
->convert
);
3230 RESOLVE_TAG (&tag_strm_out
, inquire
->strm_pos
);
3232 if (gfc_reference_st_label (inquire
->err
, ST_LABEL_TARGET
) == FAILURE
)