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 ();
263 value
= 10 * value
+ c
- '0';
268 token
= zflag
? FMT_ZERO
: FMT_POSINT
;
292 c
= next_char_not_space ();
293 if (c
!= 'L' && c
!= 'R')
312 c
= next_char_not_space ();
313 if (c
!= 'P' && c
!= 'S')
320 c
= next_char_not_space ();
321 if (c
== 'N' || c
== 'Z')
382 c
= next_char_not_space ();
383 if (c
== 'N' || c
== 'S')
426 /* Check a format statement. The format string, either from a FORMAT
427 statement or a constant in an I/O statement has already been parsed
428 by itself, and we are checking it for validity. The dual origin
429 means that the warning message is a little less than great. */
434 const char *posint_required
= _("Positive width required");
435 const char *nonneg_required
= _("Nonnegative width required");
436 const char *unexpected_element
= _("Unexpected element");
437 const char *unexpected_end
= _("Unexpected end of format string");
446 saved_token
= FMT_NONE
;
454 error
= _("Missing leading left parenthesis");
460 goto finished
; /* Empty format is legal */
464 /* In this state, the next thing has to be a format item. */
488 /* Signed integer can only precede a P format. */
492 error
= _("Expected P edit descriptor");
499 /* P requires a prior number. */
500 error
= _("P descriptor requires leading scale factor");
504 /* X requires a prior number if we're being pedantic. */
505 if (gfc_notify_std (GFC_STD_GNU
, "Extension: X descriptor "
506 "requires leading space count at %C")
516 goto extension_optional_comma
;
525 if (gfc_notify_std (GFC_STD_GNU
, "Extension: $ descriptor at %C")
528 if (t
!= FMT_RPAREN
|| level
> 0)
530 gfc_warning ("$ should be the last specifier in format at %C");
531 goto optional_comma_1
;
549 error
= unexpected_end
;
553 error
= unexpected_element
;
558 /* In this state, t must currently be a data descriptor.
559 Deal with things that can/must follow the descriptor. */
573 error
= _("Repeat count cannot follow P descriptor");
588 switch (gfc_notification_std (GFC_STD_GNU
))
591 gfc_warning ("Extension: Missing positive width after L "
597 error
= posint_required
;
622 error
= posint_required
;
629 /* Warn if -std=legacy, otherwise error. */
630 if (gfc_option
.warn_std
!= 0)
631 gfc_error_now ("Period required in format specifier at %C");
633 gfc_warning ("Period required in format specifier at %C");
639 if (u
!= FMT_ZERO
&& u
!= FMT_POSINT
)
641 error
= nonneg_required
;
648 /* Look for optional exponent. */
659 error
= _("Positive exponent width required");
668 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
670 error
= nonneg_required
;
677 /* Warn if -std=legacy, otherwise error. */
678 if (gfc_option
.warn_std
!= 0)
679 gfc_error_now ("Period required in format specifier at %C");
681 gfc_warning ("Period required in format specifier at %C");
687 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
689 error
= nonneg_required
;
696 if (!(gfc_option
.allow_std
& GFC_STD_GNU
) && !inhibit_warnings
)
697 gfc_warning ("The H format specifier at %C is"
698 " a Fortran 95 deleted feature");
700 if(mode
== MODE_STRING
)
702 format_string
+= value
;
703 format_length
-= value
;
717 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
719 error
= nonneg_required
;
731 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
733 error
= nonneg_required
;
741 error
= unexpected_element
;
746 /* Between a descriptor and what comes next. */
765 error
= unexpected_end
;
769 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
776 /* Optional comma is a weird between state where we've just finished
777 reading a colon, slash, dollar or P descriptor. */
792 /* Assume that we have another format item. */
799 extension_optional_comma
:
800 /* As a GNU extension, permit a missing comma after a string literal. */
818 error
= unexpected_end
;
822 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
832 /* Something went wrong. If the format we're checking is a string,
833 generate a warning, since the program is correct. If the format
834 is in a FORMAT statement, this messes up parsing, which is an
836 if (mode
!= MODE_STRING
)
837 gfc_error ("%s in format string at %C", error
);
840 gfc_warning ("%s in format string at %C", error
);
842 /* TODO: More elaborate measures are needed to show where a problem
843 is within a format string that has been calculated. */
853 /* Given an expression node that is a constant string, see if it looks
854 like a format string. */
857 check_format_string (gfc_expr
*e
)
860 format_string
= e
->value
.character
.string
;
865 /************ Fortran 95 I/O statement matchers *************/
867 /* Match a FORMAT statement. This amounts to actually parsing the
868 format descriptors in order to correctly locate the end of the
872 gfc_match_format (void)
877 if (gfc_current_ns
->proc_name
878 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
880 gfc_error ("Format statement in module main block at %C");
884 if (gfc_statement_label
== NULL
)
886 gfc_error ("Missing format label at %C");
889 gfc_gobble_whitespace ();
894 start
= gfc_current_locus
;
896 if (check_format () == FAILURE
)
899 if (gfc_match_eos () != MATCH_YES
)
901 gfc_syntax_error (ST_FORMAT
);
905 /* The label doesn't get created until after the statement is done
906 being matched, so we have to leave the string for later. */
908 gfc_current_locus
= start
; /* Back to the beginning */
911 new_st
.op
= EXEC_NOP
;
914 e
->expr_type
= EXPR_CONSTANT
;
915 e
->ts
.type
= BT_CHARACTER
;
916 e
->ts
.kind
= gfc_default_character_kind
;
918 e
->value
.character
.string
= format_string
= gfc_getmem (format_length
+ 1);
919 e
->value
.character
.length
= format_length
;
920 gfc_statement_label
->format
= e
;
923 check_format (); /* Guaranteed to succeed */
924 gfc_match_eos (); /* Guaranteed to succeed */
930 /* Match an expression I/O tag of some sort. */
933 match_etag (const io_tag
*tag
, gfc_expr
**v
)
938 m
= gfc_match (tag
->spec
, &result
);
944 gfc_error ("Duplicate %s specification at %C", tag
->name
);
945 gfc_free_expr (result
);
954 /* Match a variable I/O tag of some sort. */
957 match_vtag (const io_tag
*tag
, gfc_expr
**v
)
962 m
= gfc_match (tag
->spec
, &result
);
968 gfc_error ("Duplicate %s specification at %C", tag
->name
);
969 gfc_free_expr (result
);
973 if (result
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
975 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
976 gfc_free_expr (result
);
980 if (gfc_pure (NULL
) && gfc_impure_variable (result
->symtree
->n
.sym
))
982 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
983 gfc_free_expr (result
);
992 /* Match I/O tags that cause variables to become redefined. */
995 match_out_tag(const io_tag
*tag
, gfc_expr
**result
)
999 m
= match_vtag(tag
, result
);
1001 gfc_check_do_variable((*result
)->symtree
);
1007 /* Match a label I/O tag. */
1010 match_ltag (const io_tag
*tag
, gfc_st_label
** label
)
1016 m
= gfc_match (tag
->spec
, label
);
1017 if (m
== MATCH_YES
&& old
!= 0)
1019 gfc_error ("Duplicate %s label specification at %C", tag
->name
);
1024 && gfc_reference_st_label (*label
, ST_LABEL_TARGET
) == FAILURE
)
1031 /* Do expression resolution and type-checking on an expression tag. */
1034 resolve_tag (const io_tag
*tag
, gfc_expr
*e
)
1039 if (gfc_resolve_expr (e
) == FAILURE
)
1042 if (e
->ts
.type
!= tag
->type
&& tag
!= &tag_format
)
1044 gfc_error ("%s tag at %L must be of type %s", tag
->name
,
1045 &e
->where
, gfc_basic_typename (tag
->type
));
1049 if (tag
== &tag_format
)
1051 if (e
->expr_type
== EXPR_CONSTANT
1052 && (e
->ts
.type
!= BT_CHARACTER
1053 || e
->ts
.kind
!= gfc_default_character_kind
))
1055 gfc_error ("Constant expression in FORMAT tag at %L must be "
1056 "of type default CHARACTER", &e
->where
);
1060 /* If e's rank is zero and e is not an element of an array, it should be
1061 of integer or character type. The integer variable should be
1063 if (e
->symtree
== NULL
|| e
->symtree
->n
.sym
->as
== NULL
1064 || e
->symtree
->n
.sym
->as
->rank
== 0)
1066 if (e
->ts
.type
!= BT_CHARACTER
&& e
->ts
.type
!= BT_INTEGER
)
1068 gfc_error ("%s tag at %L must be of type %s or %s", tag
->name
,
1069 &e
->where
, gfc_basic_typename (BT_CHARACTER
),
1070 gfc_basic_typename (BT_INTEGER
));
1073 else if (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_VARIABLE
)
1075 if (gfc_notify_std (GFC_STD_F95_DEL
, "Obsolete: ASSIGNED "
1076 "variable in FORMAT tag at %L", &e
->where
)
1079 if (e
->symtree
->n
.sym
->attr
.assign
!= 1)
1081 gfc_error ("Variable '%s' at %L has not been assigned a "
1082 "format label", e
->symtree
->n
.sym
->name
,
1087 else if (e
->ts
.type
== BT_INTEGER
)
1089 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1090 "variable", gfc_basic_typename (e
->ts
.type
),
1099 /* if rank is nonzero, we allow the type to be character under
1100 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1101 assigned an Hollerith constant. */
1102 if (e
->ts
.type
== BT_CHARACTER
)
1104 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Character array "
1105 "in FORMAT tag at %L", &e
->where
)
1111 if (gfc_notify_std (GFC_STD_LEGACY
, "Extension: Non-character "
1112 "in FORMAT tag at %L", &e
->where
)
1123 gfc_error ("%s tag at %L must be scalar", tag
->name
, &e
->where
);
1127 if (tag
== &tag_iomsg
)
1129 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IOMSG tag at %L",
1130 &e
->where
) == FAILURE
)
1134 if (tag
== &tag_iostat
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1136 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1137 "INTEGER in IOSTAT tag at %L", &e
->where
)
1142 if (tag
== &tag_size
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1144 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 95 requires default "
1145 "INTEGER in SIZE tag at %L", &e
->where
)
1150 if (tag
== &tag_convert
)
1152 if (gfc_notify_std (GFC_STD_GNU
, "Extension: CONVERT tag at %L",
1153 &e
->where
) == FAILURE
)
1157 if (tag
== &tag_iolength
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1159 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 95 requires default "
1160 "INTEGER in IOLENGTH tag at %L", &e
->where
)
1170 /* Match a single tag of an OPEN statement. */
1173 match_open_element (gfc_open
*open
)
1177 m
= match_etag (&tag_unit
, &open
->unit
);
1180 m
= match_out_tag (&tag_iomsg
, &open
->iomsg
);
1183 m
= match_out_tag (&tag_iostat
, &open
->iostat
);
1186 m
= match_etag (&tag_file
, &open
->file
);
1189 m
= match_etag (&tag_status
, &open
->status
);
1192 m
= match_etag (&tag_e_access
, &open
->access
);
1195 m
= match_etag (&tag_e_form
, &open
->form
);
1198 m
= match_etag (&tag_e_recl
, &open
->recl
);
1201 m
= match_etag (&tag_e_blank
, &open
->blank
);
1204 m
= match_etag (&tag_e_position
, &open
->position
);
1207 m
= match_etag (&tag_e_action
, &open
->action
);
1210 m
= match_etag (&tag_e_delim
, &open
->delim
);
1213 m
= match_etag (&tag_e_pad
, &open
->pad
);
1216 m
= match_ltag (&tag_err
, &open
->err
);
1219 m
= match_etag (&tag_convert
, &open
->convert
);
1227 /* Free the gfc_open structure and all the expressions it contains. */
1230 gfc_free_open (gfc_open
*open
)
1235 gfc_free_expr (open
->unit
);
1236 gfc_free_expr (open
->iomsg
);
1237 gfc_free_expr (open
->iostat
);
1238 gfc_free_expr (open
->file
);
1239 gfc_free_expr (open
->status
);
1240 gfc_free_expr (open
->access
);
1241 gfc_free_expr (open
->form
);
1242 gfc_free_expr (open
->recl
);
1243 gfc_free_expr (open
->blank
);
1244 gfc_free_expr (open
->position
);
1245 gfc_free_expr (open
->action
);
1246 gfc_free_expr (open
->delim
);
1247 gfc_free_expr (open
->pad
);
1248 gfc_free_expr (open
->convert
);
1253 /* Resolve everything in a gfc_open structure. */
1256 gfc_resolve_open (gfc_open
*open
)
1259 RESOLVE_TAG (&tag_unit
, open
->unit
);
1260 RESOLVE_TAG (&tag_iomsg
, open
->iomsg
);
1261 RESOLVE_TAG (&tag_iostat
, open
->iostat
);
1262 RESOLVE_TAG (&tag_file
, open
->file
);
1263 RESOLVE_TAG (&tag_status
, open
->status
);
1264 RESOLVE_TAG (&tag_e_access
, open
->access
);
1265 RESOLVE_TAG (&tag_e_form
, open
->form
);
1266 RESOLVE_TAG (&tag_e_recl
, open
->recl
);
1267 RESOLVE_TAG (&tag_e_blank
, open
->blank
);
1268 RESOLVE_TAG (&tag_e_position
, open
->position
);
1269 RESOLVE_TAG (&tag_e_action
, open
->action
);
1270 RESOLVE_TAG (&tag_e_delim
, open
->delim
);
1271 RESOLVE_TAG (&tag_e_pad
, open
->pad
);
1272 RESOLVE_TAG (&tag_convert
, open
->convert
);
1274 if (gfc_reference_st_label (open
->err
, ST_LABEL_TARGET
) == FAILURE
)
1281 /* Check if a given value for a SPECIFIER is either in the list of values
1282 allowed in F95 or F2003, issuing an error message and returning a zero
1283 value if it is not allowed. */
1286 compare_to_allowed_values (const char *specifier
, const char *allowed
[],
1287 const char *allowed_f2003
[],
1288 const char *allowed_gnu
[], char *value
,
1289 const char *statement
, bool warn
)
1294 len
= strlen (value
);
1297 for (len
--; len
> 0; len
--)
1298 if (value
[len
] != ' ')
1303 for (i
= 0; allowed
[i
]; i
++)
1304 if (len
== strlen (allowed
[i
])
1305 && strncasecmp (value
, allowed
[i
], strlen (allowed
[i
])) == 0)
1308 for (i
= 0; allowed_f2003
&& allowed_f2003
[i
]; i
++)
1309 if (len
== strlen (allowed_f2003
[i
])
1310 && strncasecmp (value
, allowed_f2003
[i
], strlen (allowed_f2003
[i
]))
1313 notification n
= gfc_notification_std (GFC_STD_F2003
);
1315 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1317 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1318 "has value '%s'", specifier
, statement
,
1325 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: %s specifier in "
1326 "%s statement at %C has value '%s'", specifier
,
1327 statement
, allowed_f2003
[i
]);
1335 for (i
= 0; allowed_gnu
&& allowed_gnu
[i
]; i
++)
1336 if (len
== strlen (allowed_gnu
[i
])
1337 && strncasecmp (value
, allowed_gnu
[i
], strlen (allowed_gnu
[i
])) == 0)
1339 notification n
= gfc_notification_std (GFC_STD_GNU
);
1341 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1343 gfc_warning ("Extension: %s specifier in %s statement at %C "
1344 "has value '%s'", specifier
, statement
,
1351 gfc_notify_std (GFC_STD_GNU
, "Extension: %s specifier in "
1352 "%s statement at %C has value '%s'", specifier
,
1353 statement
, allowed_gnu
[i
]);
1363 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1364 specifier
, statement
, value
);
1369 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1370 specifier
, statement
, value
);
1376 /* Match an OPEN statement. */
1379 gfc_match_open (void)
1385 m
= gfc_match_char ('(');
1389 open
= gfc_getmem (sizeof (gfc_open
));
1391 m
= match_open_element (open
);
1393 if (m
== MATCH_ERROR
)
1397 m
= gfc_match_expr (&open
->unit
);
1400 if (m
== MATCH_ERROR
)
1406 if (gfc_match_char (')') == MATCH_YES
)
1408 if (gfc_match_char (',') != MATCH_YES
)
1411 m
= match_open_element (open
);
1412 if (m
== MATCH_ERROR
)
1418 if (gfc_match_eos () == MATCH_NO
)
1421 if (gfc_pure (NULL
))
1423 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1427 warn
= (open
->err
|| open
->iostat
) ? true : false;
1428 /* Checks on the ACCESS specifier. */
1429 if (open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
)
1431 static const char *access_f95
[] = { "SEQUENTIAL", "DIRECT", NULL
};
1432 static const char *access_f2003
[] = { "STREAM", NULL
};
1433 static const char *access_gnu
[] = { "APPEND", NULL
};
1435 if (!compare_to_allowed_values ("ACCESS", access_f95
, access_f2003
,
1437 open
->access
->value
.character
.string
,
1442 /* Checks on the ACTION specifier. */
1443 if (open
->action
&& open
->action
->expr_type
== EXPR_CONSTANT
)
1445 static const char *action
[] = { "READ", "WRITE", "READWRITE", NULL
};
1447 if (!compare_to_allowed_values ("ACTION", action
, NULL
, NULL
,
1448 open
->action
->value
.character
.string
,
1453 /* Checks on the ASYNCHRONOUS specifier. */
1454 /* TODO: code is ready, just needs uncommenting when async I/O support
1456 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1458 static const char * asynchronous[] = { "YES", "NO", NULL };
1460 if (!compare_to_allowed_values
1461 ("action", asynchronous, NULL, NULL,
1462 open->asynchronous->value.character.string, "OPEN", warn))
1466 /* Checks on the BLANK specifier. */
1467 if (open
->blank
&& open
->blank
->expr_type
== EXPR_CONSTANT
)
1469 static const char *blank
[] = { "ZERO", "NULL", NULL
};
1471 if (!compare_to_allowed_values ("BLANK", blank
, NULL
, NULL
,
1472 open
->blank
->value
.character
.string
,
1477 /* Checks on the DECIMAL specifier. */
1478 /* TODO: uncomment this code when DECIMAL support is added
1479 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1481 static const char * decimal[] = { "COMMA", "POINT", NULL };
1483 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1484 open->decimal->value.character.string,
1489 /* Checks on the DELIM specifier. */
1490 if (open
->delim
&& open
->delim
->expr_type
== EXPR_CONSTANT
)
1492 static const char *delim
[] = { "APOSTROPHE", "QUOTE", "NONE", NULL
};
1494 if (!compare_to_allowed_values ("DELIM", delim
, NULL
, NULL
,
1495 open
->delim
->value
.character
.string
,
1500 /* Checks on the ENCODING specifier. */
1501 /* TODO: uncomment this code when ENCODING support is added
1502 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1504 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1506 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1507 open->encoding->value.character.string,
1512 /* Checks on the FORM specifier. */
1513 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
)
1515 static const char *form
[] = { "FORMATTED", "UNFORMATTED", NULL
};
1517 if (!compare_to_allowed_values ("FORM", form
, NULL
, NULL
,
1518 open
->form
->value
.character
.string
,
1523 /* Checks on the PAD specifier. */
1524 if (open
->pad
&& open
->pad
->expr_type
== EXPR_CONSTANT
)
1526 static const char *pad
[] = { "YES", "NO", NULL
};
1528 if (!compare_to_allowed_values ("PAD", pad
, NULL
, NULL
,
1529 open
->pad
->value
.character
.string
,
1534 /* Checks on the POSITION specifier. */
1535 if (open
->position
&& open
->position
->expr_type
== EXPR_CONSTANT
)
1537 static const char *position
[] = { "ASIS", "REWIND", "APPEND", NULL
};
1539 if (!compare_to_allowed_values ("POSITION", position
, NULL
, NULL
,
1540 open
->position
->value
.character
.string
,
1545 /* Checks on the ROUND specifier. */
1546 /* TODO: uncomment this code when ROUND support is added
1547 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1549 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1550 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1552 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1553 open->round->value.character.string,
1558 /* Checks on the SIGN specifier. */
1559 /* TODO: uncomment this code when SIGN support is added
1560 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1562 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1565 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1566 open->sign->value.character.string,
1571 #define warn_or_error(...) \
1574 gfc_warning (__VA_ARGS__); \
1577 gfc_error (__VA_ARGS__); \
1582 /* Checks on the RECL specifier. */
1583 if (open
->recl
&& open
->recl
->expr_type
== EXPR_CONSTANT
1584 && open
->recl
->ts
.type
== BT_INTEGER
1585 && mpz_sgn (open
->recl
->value
.integer
) != 1)
1587 warn_or_error ("RECL in OPEN statement at %C must be positive");
1590 /* Checks on the STATUS specifier. */
1591 if (open
->status
&& open
->status
->expr_type
== EXPR_CONSTANT
)
1593 static const char *status
[] = { "OLD", "NEW", "SCRATCH",
1594 "REPLACE", "UNKNOWN", NULL
};
1596 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1597 open
->status
->value
.character
.string
,
1601 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1602 the FILE= specifier shall appear. */
1603 if (open
->file
== NULL
1604 && (strncasecmp (open
->status
->value
.character
.string
, "replace", 7)
1606 || strncasecmp (open
->status
->value
.character
.string
, "new", 3)
1609 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1610 "'%s' and no FILE specifier is present",
1611 open
->status
->value
.character
.string
);
1614 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1615 the FILE= specifier shall not appear. */
1616 if (strncasecmp (open
->status
->value
.character
.string
, "scratch", 7)
1619 warn_or_error ("The STATUS specified in OPEN statement at %C "
1620 "cannot have the value SCRATCH if a FILE specifier "
1625 /* Things that are not allowed for unformatted I/O. */
1626 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
1628 /* TODO uncomment this code when F2003 support is finished */
1629 /* || open->decimal || open->encoding || open->round
1631 || open
->pad
|| open
->blank
)
1632 && strncasecmp (open
->form
->value
.character
.string
,
1633 "unformatted", 11) == 0)
1635 const char *spec
= (open
->delim
? "DELIM "
1636 : (open
->pad
? "PAD " : open
->blank
1639 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1640 "unformatted I/O", spec
);
1643 if (open
->recl
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1644 && strncasecmp (open
->access
->value
.character
.string
, "stream", 6) == 0)
1646 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1651 && open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1652 && !(strncasecmp (open
->access
->value
.character
.string
,
1653 "sequential", 10) == 0
1654 || strncasecmp (open
->access
->value
.character
.string
,
1656 || strncasecmp (open
->access
->value
.character
.string
,
1659 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1660 "for stream or sequential ACCESS");
1663 #undef warn_or_error
1665 new_st
.op
= EXEC_OPEN
;
1666 new_st
.ext
.open
= open
;
1670 gfc_syntax_error (ST_OPEN
);
1673 gfc_free_open (open
);
1678 /* Free a gfc_close structure an all its expressions. */
1681 gfc_free_close (gfc_close
*close
)
1686 gfc_free_expr (close
->unit
);
1687 gfc_free_expr (close
->iomsg
);
1688 gfc_free_expr (close
->iostat
);
1689 gfc_free_expr (close
->status
);
1694 /* Match elements of a CLOSE statement. */
1697 match_close_element (gfc_close
*close
)
1701 m
= match_etag (&tag_unit
, &close
->unit
);
1704 m
= match_etag (&tag_status
, &close
->status
);
1707 m
= match_out_tag (&tag_iomsg
, &close
->iomsg
);
1710 m
= match_out_tag (&tag_iostat
, &close
->iostat
);
1713 m
= match_ltag (&tag_err
, &close
->err
);
1721 /* Match a CLOSE statement. */
1724 gfc_match_close (void)
1730 m
= gfc_match_char ('(');
1734 close
= gfc_getmem (sizeof (gfc_close
));
1736 m
= match_close_element (close
);
1738 if (m
== MATCH_ERROR
)
1742 m
= gfc_match_expr (&close
->unit
);
1745 if (m
== MATCH_ERROR
)
1751 if (gfc_match_char (')') == MATCH_YES
)
1753 if (gfc_match_char (',') != MATCH_YES
)
1756 m
= match_close_element (close
);
1757 if (m
== MATCH_ERROR
)
1763 if (gfc_match_eos () == MATCH_NO
)
1766 if (gfc_pure (NULL
))
1768 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1772 warn
= (close
->iostat
|| close
->err
) ? true : false;
1774 /* Checks on the STATUS specifier. */
1775 if (close
->status
&& close
->status
->expr_type
== EXPR_CONSTANT
)
1777 static const char *status
[] = { "KEEP", "DELETE", NULL
};
1779 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1780 close
->status
->value
.character
.string
,
1785 new_st
.op
= EXEC_CLOSE
;
1786 new_st
.ext
.close
= close
;
1790 gfc_syntax_error (ST_CLOSE
);
1793 gfc_free_close (close
);
1798 /* Resolve everything in a gfc_close structure. */
1801 gfc_resolve_close (gfc_close
*close
)
1803 RESOLVE_TAG (&tag_unit
, close
->unit
);
1804 RESOLVE_TAG (&tag_iomsg
, close
->iomsg
);
1805 RESOLVE_TAG (&tag_iostat
, close
->iostat
);
1806 RESOLVE_TAG (&tag_status
, close
->status
);
1808 if (gfc_reference_st_label (close
->err
, ST_LABEL_TARGET
) == FAILURE
)
1815 /* Free a gfc_filepos structure. */
1818 gfc_free_filepos (gfc_filepos
*fp
)
1820 gfc_free_expr (fp
->unit
);
1821 gfc_free_expr (fp
->iomsg
);
1822 gfc_free_expr (fp
->iostat
);
1827 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1830 match_file_element (gfc_filepos
*fp
)
1834 m
= match_etag (&tag_unit
, &fp
->unit
);
1837 m
= match_out_tag (&tag_iomsg
, &fp
->iomsg
);
1840 m
= match_out_tag (&tag_iostat
, &fp
->iostat
);
1843 m
= match_ltag (&tag_err
, &fp
->err
);
1851 /* Match the second half of the file-positioning statements, REWIND,
1852 BACKSPACE, ENDFILE, or the FLUSH statement. */
1855 match_filepos (gfc_statement st
, gfc_exec_op op
)
1860 fp
= gfc_getmem (sizeof (gfc_filepos
));
1862 if (gfc_match_char ('(') == MATCH_NO
)
1864 m
= gfc_match_expr (&fp
->unit
);
1865 if (m
== MATCH_ERROR
)
1873 m
= match_file_element (fp
);
1874 if (m
== MATCH_ERROR
)
1878 m
= gfc_match_expr (&fp
->unit
);
1879 if (m
== MATCH_ERROR
)
1887 if (gfc_match_char (')') == MATCH_YES
)
1889 if (gfc_match_char (',') != MATCH_YES
)
1892 m
= match_file_element (fp
);
1893 if (m
== MATCH_ERROR
)
1900 if (gfc_match_eos () != MATCH_YES
)
1903 if (gfc_pure (NULL
))
1905 gfc_error ("%s statement not allowed in PURE procedure at %C",
1906 gfc_ascii_statement (st
));
1912 new_st
.ext
.filepos
= fp
;
1916 gfc_syntax_error (st
);
1919 gfc_free_filepos (fp
);
1925 gfc_resolve_filepos (gfc_filepos
*fp
)
1927 RESOLVE_TAG (&tag_unit
, fp
->unit
);
1928 RESOLVE_TAG (&tag_iostat
, fp
->iostat
);
1929 RESOLVE_TAG (&tag_iomsg
, fp
->iomsg
);
1930 if (gfc_reference_st_label (fp
->err
, ST_LABEL_TARGET
) == FAILURE
)
1937 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1938 and the FLUSH statement. */
1941 gfc_match_endfile (void)
1943 return match_filepos (ST_END_FILE
, EXEC_ENDFILE
);
1947 gfc_match_backspace (void)
1949 return match_filepos (ST_BACKSPACE
, EXEC_BACKSPACE
);
1953 gfc_match_rewind (void)
1955 return match_filepos (ST_REWIND
, EXEC_REWIND
);
1959 gfc_match_flush (void)
1961 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: FLUSH statement at %C")
1965 return match_filepos (ST_FLUSH
, EXEC_FLUSH
);
1968 /******************** Data Transfer Statements *********************/
1971 { M_READ
, M_WRITE
, M_PRINT
, M_INQUIRE
}
1975 /* Return a default unit number. */
1978 default_unit (io_kind k
)
1987 return gfc_int_expr (unit
);
1991 /* Match a unit specification for a data transfer statement. */
1994 match_dt_unit (io_kind k
, gfc_dt
*dt
)
1998 if (gfc_match_char ('*') == MATCH_YES
)
2000 if (dt
->io_unit
!= NULL
)
2003 dt
->io_unit
= default_unit (k
);
2007 if (gfc_match_expr (&e
) == MATCH_YES
)
2009 if (dt
->io_unit
!= NULL
)
2022 gfc_error ("Duplicate UNIT specification at %C");
2027 /* Match a format specification. */
2030 match_dt_format (gfc_dt
*dt
)
2034 gfc_st_label
*label
;
2036 where
= gfc_current_locus
;
2038 if (gfc_match_char ('*') == MATCH_YES
)
2040 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2043 dt
->format_label
= &format_asterisk
;
2047 if (gfc_match_st_label (&label
) == MATCH_YES
)
2049 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2051 gfc_free_st_label (label
);
2055 if (gfc_reference_st_label (label
, ST_LABEL_FORMAT
) == FAILURE
)
2058 dt
->format_label
= label
;
2062 if (gfc_match_expr (&e
) == MATCH_YES
)
2064 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2069 dt
->format_expr
= e
;
2073 gfc_current_locus
= where
; /* The only case where we have to restore */
2078 gfc_error ("Duplicate format specification at %C");
2083 /* Traverse a namelist that is part of a READ statement to make sure
2084 that none of the variables in the namelist are INTENT(IN). Returns
2085 nonzero if we find such a variable. */
2088 check_namelist (gfc_symbol
*sym
)
2092 for (p
= sym
->namelist
; p
; p
= p
->next
)
2093 if (p
->sym
->attr
.intent
== INTENT_IN
)
2095 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2096 p
->sym
->name
, sym
->name
);
2104 /* Match a single data transfer element. */
2107 match_dt_element (io_kind k
, gfc_dt
*dt
)
2109 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2113 if (gfc_match (" unit =") == MATCH_YES
)
2115 m
= match_dt_unit (k
, dt
);
2120 if (gfc_match (" fmt =") == MATCH_YES
)
2122 m
= match_dt_format (dt
);
2127 if (gfc_match (" nml = %n", name
) == MATCH_YES
)
2129 if (dt
->namelist
!= NULL
)
2131 gfc_error ("Duplicate NML specification at %C");
2135 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2138 if (sym
== NULL
|| sym
->attr
.flavor
!= FL_NAMELIST
)
2140 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2141 sym
!= NULL
? sym
->name
: name
);
2146 if (k
== M_READ
&& check_namelist (sym
))
2152 m
= match_etag (&tag_rec
, &dt
->rec
);
2155 m
= match_etag (&tag_spos
, &dt
->rec
);
2158 m
= match_out_tag (&tag_iomsg
, &dt
->iomsg
);
2161 m
= match_out_tag (&tag_iostat
, &dt
->iostat
);
2164 m
= match_ltag (&tag_err
, &dt
->err
);
2166 dt
->err_where
= gfc_current_locus
;
2169 m
= match_etag (&tag_advance
, &dt
->advance
);
2172 m
= match_out_tag (&tag_size
, &dt
->size
);
2176 m
= match_ltag (&tag_end
, &dt
->end
);
2181 gfc_error ("END tag at %C not allowed in output statement");
2184 dt
->end_where
= gfc_current_locus
;
2189 m
= match_ltag (&tag_eor
, &dt
->eor
);
2191 dt
->eor_where
= gfc_current_locus
;
2199 /* Free a data transfer structure and everything below it. */
2202 gfc_free_dt (gfc_dt
*dt
)
2207 gfc_free_expr (dt
->io_unit
);
2208 gfc_free_expr (dt
->format_expr
);
2209 gfc_free_expr (dt
->rec
);
2210 gfc_free_expr (dt
->advance
);
2211 gfc_free_expr (dt
->iomsg
);
2212 gfc_free_expr (dt
->iostat
);
2213 gfc_free_expr (dt
->size
);
2218 /* Resolve everything in a gfc_dt structure. */
2221 gfc_resolve_dt (gfc_dt
*dt
)
2225 RESOLVE_TAG (&tag_format
, dt
->format_expr
);
2226 RESOLVE_TAG (&tag_rec
, dt
->rec
);
2227 RESOLVE_TAG (&tag_spos
, dt
->rec
);
2228 RESOLVE_TAG (&tag_advance
, dt
->advance
);
2229 RESOLVE_TAG (&tag_iomsg
, dt
->iomsg
);
2230 RESOLVE_TAG (&tag_iostat
, dt
->iostat
);
2231 RESOLVE_TAG (&tag_size
, dt
->size
);
2234 if (gfc_resolve_expr (e
) == SUCCESS
2235 && (e
->ts
.type
!= BT_INTEGER
2236 && (e
->ts
.type
!= BT_CHARACTER
|| e
->expr_type
!= EXPR_VARIABLE
)))
2238 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2239 "or a CHARACTER variable", &e
->where
);
2243 if (e
->ts
.type
== BT_CHARACTER
)
2245 if (gfc_has_vector_index (e
))
2247 gfc_error ("Internal unit with vector subscript at %L", &e
->where
);
2252 if (e
->rank
&& e
->ts
.type
!= BT_CHARACTER
)
2254 gfc_error ("External IO UNIT cannot be an array at %L", &e
->where
);
2260 if (gfc_reference_st_label (dt
->err
, ST_LABEL_TARGET
) == FAILURE
)
2262 if (dt
->err
->defined
== ST_LABEL_UNKNOWN
)
2264 gfc_error ("ERR tag label %d at %L not defined",
2265 dt
->err
->value
, &dt
->err_where
);
2272 if (gfc_reference_st_label (dt
->end
, ST_LABEL_TARGET
) == FAILURE
)
2274 if (dt
->end
->defined
== ST_LABEL_UNKNOWN
)
2276 gfc_error ("END tag label %d at %L not defined",
2277 dt
->end
->value
, &dt
->end_where
);
2284 if (gfc_reference_st_label (dt
->eor
, ST_LABEL_TARGET
) == FAILURE
)
2286 if (dt
->eor
->defined
== ST_LABEL_UNKNOWN
)
2288 gfc_error ("EOR tag label %d at %L not defined",
2289 dt
->eor
->value
, &dt
->eor_where
);
2294 /* Check the format label actually exists. */
2295 if (dt
->format_label
&& dt
->format_label
!= &format_asterisk
2296 && dt
->format_label
->defined
== ST_LABEL_UNKNOWN
)
2298 gfc_error ("FORMAT label %d at %L not defined", dt
->format_label
->value
,
2299 &dt
->format_label
->where
);
2306 /* Given an io_kind, return its name. */
2309 io_kind_name (io_kind k
)
2328 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2335 /* Match an IO iteration statement of the form:
2337 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2339 which is equivalent to a single IO element. This function is
2340 mutually recursive with match_io_element(). */
2342 static match
match_io_element (io_kind
, gfc_code
**);
2345 match_io_iterator (io_kind k
, gfc_code
**result
)
2347 gfc_code
*head
, *tail
, *new;
2355 old_loc
= gfc_current_locus
;
2357 if (gfc_match_char ('(') != MATCH_YES
)
2360 m
= match_io_element (k
, &head
);
2363 if (m
!= MATCH_YES
|| gfc_match_char (',') != MATCH_YES
)
2369 /* Can't be anything but an IO iterator. Build a list. */
2370 iter
= gfc_get_iterator ();
2374 m
= gfc_match_iterator (iter
, 0);
2375 if (m
== MATCH_ERROR
)
2379 gfc_check_do_variable (iter
->var
->symtree
);
2383 m
= match_io_element (k
, &new);
2384 if (m
== MATCH_ERROR
)
2393 tail
= gfc_append_code (tail
, new);
2395 if (gfc_match_char (',') != MATCH_YES
)
2404 if (gfc_match_char (')') != MATCH_YES
)
2407 new = gfc_get_code ();
2409 new->ext
.iterator
= iter
;
2411 new->block
= gfc_get_code ();
2412 new->block
->op
= EXEC_DO
;
2413 new->block
->next
= head
;
2419 gfc_error ("Syntax error in I/O iterator at %C");
2423 gfc_free_iterator (iter
, 1);
2424 gfc_free_statements (head
);
2425 gfc_current_locus
= old_loc
;
2430 /* Match a single element of an IO list, which is either a single
2431 expression or an IO Iterator. */
2434 match_io_element (io_kind k
, gfc_code
**cpp
)
2442 m
= match_io_iterator (k
, cpp
);
2448 m
= gfc_match_variable (&expr
, 0);
2450 gfc_error ("Expected variable in READ statement at %C");
2454 m
= gfc_match_expr (&expr
);
2456 gfc_error ("Expected expression in %s statement at %C",
2464 if (expr
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
2466 gfc_error ("Variable '%s' in input list at %C cannot be "
2467 "INTENT(IN)", expr
->symtree
->n
.sym
->name
);
2472 && gfc_impure_variable (expr
->symtree
->n
.sym
)
2473 && current_dt
->io_unit
->ts
.type
== BT_CHARACTER
)
2475 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2476 expr
->symtree
->n
.sym
->name
);
2480 if (gfc_check_do_variable (expr
->symtree
))
2486 if (current_dt
->io_unit
->ts
.type
== BT_CHARACTER
2488 && current_dt
->io_unit
->expr_type
== EXPR_VARIABLE
2489 && gfc_impure_variable (current_dt
->io_unit
->symtree
->n
.sym
))
2491 gfc_error ("Cannot write to internal file unit '%s' at %C "
2492 "inside a PURE procedure",
2493 current_dt
->io_unit
->symtree
->n
.sym
->name
);
2505 gfc_free_expr (expr
);
2509 cp
= gfc_get_code ();
2510 cp
->op
= EXEC_TRANSFER
;
2518 /* Match an I/O list, building gfc_code structures as we go. */
2521 match_io_list (io_kind k
, gfc_code
**head_p
)
2523 gfc_code
*head
, *tail
, *new;
2526 *head_p
= head
= tail
= NULL
;
2527 if (gfc_match_eos () == MATCH_YES
)
2532 m
= match_io_element (k
, &new);
2533 if (m
== MATCH_ERROR
)
2538 tail
= gfc_append_code (tail
, new);
2542 if (gfc_match_eos () == MATCH_YES
)
2544 if (gfc_match_char (',') != MATCH_YES
)
2552 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2555 gfc_free_statements (head
);
2560 /* Attach the data transfer end node. */
2563 terminate_io (gfc_code
*io_code
)
2567 if (io_code
== NULL
)
2568 io_code
= new_st
.block
;
2570 c
= gfc_get_code ();
2571 c
->op
= EXEC_DT_END
;
2573 /* Point to structure that is already there */
2574 c
->ext
.dt
= new_st
.ext
.dt
;
2575 gfc_append_code (io_code
, c
);
2579 /* Check the constraints for a data transfer statement. The majority of the
2580 constraints appearing in 9.4 of the standard appear here. Some are handled
2581 in resolve_tag and others in gfc_resolve_dt. */
2584 check_io_constraints (io_kind k
, gfc_dt
*dt
, gfc_code
*io_code
,
2587 #define io_constraint(condition,msg,arg)\
2590 gfc_error(msg,arg);\
2596 gfc_symbol
*sym
= NULL
;
2601 if (expr
&& expr
->expr_type
== EXPR_VARIABLE
2602 && expr
->ts
.type
== BT_CHARACTER
)
2604 sym
= expr
->symtree
->n
.sym
;
2606 io_constraint (k
== M_WRITE
&& sym
->attr
.intent
== INTENT_IN
,
2607 "Internal file at %L must not be INTENT(IN)",
2610 io_constraint (gfc_has_vector_index (dt
->io_unit
),
2611 "Internal file incompatible with vector subscript at %L",
2614 io_constraint (dt
->rec
!= NULL
,
2615 "REC tag at %L is incompatible with internal file",
2618 if (dt
->namelist
!= NULL
)
2620 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Internal file "
2621 "at %L with namelist", &expr
->where
)
2626 io_constraint (dt
->advance
!= NULL
,
2627 "ADVANCE tag at %L is incompatible with internal file",
2628 &dt
->advance
->where
);
2631 if (expr
&& expr
->ts
.type
!= BT_CHARACTER
)
2634 io_constraint (gfc_pure (NULL
) && (k
== M_READ
|| k
== M_WRITE
),
2635 "IO UNIT in %s statement at %C must be "
2636 "an internal file in a PURE procedure",
2643 io_constraint (dt
->end
, "END tag not allowed with output at %L",
2646 io_constraint (dt
->eor
, "EOR tag not allowed with output at %L",
2649 io_constraint (k
!= M_READ
&& dt
->size
,
2650 "SIZE=specifier not allowed with output at %L",
2655 io_constraint (dt
->size
&& dt
->advance
== NULL
,
2656 "SIZE tag at %L requires an ADVANCE tag",
2659 io_constraint (dt
->eor
&& dt
->advance
== NULL
,
2660 "EOR tag at %L requires an ADVANCE tag",
2668 io_constraint (io_code
&& dt
->namelist
,
2669 "NAMELIST cannot be followed by IO-list at %L",
2672 io_constraint (dt
->format_expr
,
2673 "IO spec-list cannot contain both NAMELIST group name "
2674 "and format specification at %L.",
2675 &dt
->format_expr
->where
);
2677 io_constraint (dt
->format_label
,
2678 "IO spec-list cannot contain both NAMELIST group name "
2679 "and format label at %L", spec_end
);
2681 io_constraint (dt
->rec
,
2682 "NAMELIST IO is not allowed with a REC=specifier "
2683 "at %L.", &dt
->rec
->where
);
2685 io_constraint (dt
->advance
,
2686 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2687 "at %L.", &dt
->advance
->where
);
2692 io_constraint (dt
->end
,
2693 "An END tag is not allowed with a "
2694 "REC=specifier at %L.", &dt
->end_where
);
2697 io_constraint (dt
->format_label
== &format_asterisk
,
2698 "FMT=* is not allowed with a REC=specifier "
2699 "at %L.", spec_end
);
2704 int not_yes
, not_no
;
2707 io_constraint (dt
->format_label
== &format_asterisk
,
2708 "List directed format(*) is not allowed with a "
2709 "ADVANCE=specifier at %L.", &expr
->where
);
2711 io_constraint (dt
->format_expr
== NULL
&& dt
->format_label
== NULL
2712 && dt
->namelist
== NULL
,
2713 "the ADVANCE=specifier at %L must appear with an "
2714 "explicit format expression", &expr
->where
);
2716 if (expr
->expr_type
== EXPR_CONSTANT
&& expr
->ts
.type
== BT_CHARACTER
)
2718 const char * advance
= expr
->value
.character
.string
;
2719 not_no
= strcasecmp (advance
, "no") != 0;
2720 not_yes
= strcasecmp (advance
, "yes") != 0;
2728 io_constraint (not_no
&& not_yes
,
2729 "ADVANCE=specifier at %L must have value = "
2730 "YES or NO.", &expr
->where
);
2732 io_constraint (dt
->size
&& not_no
&& k
== M_READ
,
2733 "SIZE tag at %L requires an ADVANCE = 'NO'",
2736 io_constraint (dt
->eor
&& not_no
&& k
== M_READ
,
2737 "EOR tag at %L requires an ADVANCE = 'NO'",
2741 expr
= dt
->format_expr
;
2742 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
)
2743 check_format_string (expr
);
2747 #undef io_constraint
2750 /* Match a READ, WRITE or PRINT statement. */
2753 match_io (io_kind k
)
2755 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2764 where
= gfc_current_locus
;
2766 current_dt
= dt
= gfc_getmem (sizeof (gfc_dt
));
2767 m
= gfc_match_char ('(');
2770 where
= gfc_current_locus
;
2773 else if (k
== M_PRINT
)
2775 /* Treat the non-standard case of PRINT namelist. */
2776 if ((gfc_current_form
== FORM_FIXED
|| gfc_peek_char () == ' ')
2777 && gfc_match_name (name
) == MATCH_YES
)
2779 gfc_find_symbol (name
, NULL
, 1, &sym
);
2780 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2782 if (gfc_notify_std (GFC_STD_GNU
, "PRINT namelist at "
2783 "%C is an extension") == FAILURE
)
2789 dt
->io_unit
= default_unit (k
);
2794 gfc_current_locus
= where
;
2798 if (gfc_current_form
== FORM_FREE
)
2800 c
= gfc_peek_char();
2801 if (c
!= ' ' && c
!= '*' && c
!= '\'' && c
!= '"')
2808 m
= match_dt_format (dt
);
2809 if (m
== MATCH_ERROR
)
2815 dt
->io_unit
= default_unit (k
);
2820 /* Before issuing an error for a malformed 'print (1,*)' type of
2821 error, check for a default-char-expr of the form ('(I0)'). */
2822 if (k
== M_PRINT
&& m
== MATCH_YES
)
2824 /* Reset current locus to get the initial '(' in an expression. */
2825 gfc_current_locus
= where
;
2826 dt
->format_expr
= NULL
;
2827 m
= match_dt_format (dt
);
2829 if (m
== MATCH_ERROR
)
2831 if (m
== MATCH_NO
|| dt
->format_expr
== NULL
)
2835 dt
->io_unit
= default_unit (k
);
2840 /* Match a control list */
2841 if (match_dt_element (k
, dt
) == MATCH_YES
)
2843 if (match_dt_unit (k
, dt
) != MATCH_YES
)
2846 if (gfc_match_char (')') == MATCH_YES
)
2848 if (gfc_match_char (',') != MATCH_YES
)
2851 m
= match_dt_element (k
, dt
);
2854 if (m
== MATCH_ERROR
)
2857 m
= match_dt_format (dt
);
2860 if (m
== MATCH_ERROR
)
2863 where
= gfc_current_locus
;
2865 m
= gfc_match_name (name
);
2868 gfc_find_symbol (name
, NULL
, 1, &sym
);
2869 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2872 if (k
== M_READ
&& check_namelist (sym
))
2881 gfc_current_locus
= where
;
2883 goto loop
; /* No matches, try regular elements */
2886 if (gfc_match_char (')') == MATCH_YES
)
2888 if (gfc_match_char (',') != MATCH_YES
)
2894 m
= match_dt_element (k
, dt
);
2897 if (m
== MATCH_ERROR
)
2900 if (gfc_match_char (')') == MATCH_YES
)
2902 if (gfc_match_char (',') != MATCH_YES
)
2908 /* Used in check_io_constraints, where no locus is available. */
2909 spec_end
= gfc_current_locus
;
2911 /* Optional leading comma (non-standard). */
2913 && gfc_match_char (',') == MATCH_YES
2915 && gfc_notify_std (GFC_STD_GNU
, "Extension: Comma before output "
2916 "item list at %C is an extension") == FAILURE
)
2920 if (gfc_match_eos () != MATCH_YES
)
2922 if (comma_flag
&& gfc_match_char (',') != MATCH_YES
)
2924 gfc_error ("Expected comma in I/O list at %C");
2929 m
= match_io_list (k
, &io_code
);
2930 if (m
== MATCH_ERROR
)
2936 /* A full IO statement has been matched. Check the constraints. spec_end is
2937 supplied for cases where no locus is supplied. */
2938 m
= check_io_constraints (k
, dt
, io_code
, &spec_end
);
2940 if (m
== MATCH_ERROR
)
2943 new_st
.op
= (k
== M_READ
) ? EXEC_READ
: EXEC_WRITE
;
2945 new_st
.block
= gfc_get_code ();
2946 new_st
.block
->op
= new_st
.op
;
2947 new_st
.block
->next
= io_code
;
2949 terminate_io (io_code
);
2954 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2964 gfc_match_read (void)
2966 return match_io (M_READ
);
2970 gfc_match_write (void)
2972 return match_io (M_WRITE
);
2976 gfc_match_print (void)
2980 m
= match_io (M_PRINT
);
2984 if (gfc_pure (NULL
))
2986 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2994 /* Free a gfc_inquire structure. */
2997 gfc_free_inquire (gfc_inquire
*inquire
)
3000 if (inquire
== NULL
)
3003 gfc_free_expr (inquire
->unit
);
3004 gfc_free_expr (inquire
->file
);
3005 gfc_free_expr (inquire
->iomsg
);
3006 gfc_free_expr (inquire
->iostat
);
3007 gfc_free_expr (inquire
->exist
);
3008 gfc_free_expr (inquire
->opened
);
3009 gfc_free_expr (inquire
->number
);
3010 gfc_free_expr (inquire
->named
);
3011 gfc_free_expr (inquire
->name
);
3012 gfc_free_expr (inquire
->access
);
3013 gfc_free_expr (inquire
->sequential
);
3014 gfc_free_expr (inquire
->direct
);
3015 gfc_free_expr (inquire
->form
);
3016 gfc_free_expr (inquire
->formatted
);
3017 gfc_free_expr (inquire
->unformatted
);
3018 gfc_free_expr (inquire
->recl
);
3019 gfc_free_expr (inquire
->nextrec
);
3020 gfc_free_expr (inquire
->blank
);
3021 gfc_free_expr (inquire
->position
);
3022 gfc_free_expr (inquire
->action
);
3023 gfc_free_expr (inquire
->read
);
3024 gfc_free_expr (inquire
->write
);
3025 gfc_free_expr (inquire
->readwrite
);
3026 gfc_free_expr (inquire
->delim
);
3027 gfc_free_expr (inquire
->pad
);
3028 gfc_free_expr (inquire
->iolength
);
3029 gfc_free_expr (inquire
->convert
);
3030 gfc_free_expr (inquire
->strm_pos
);
3035 /* Match an element of an INQUIRE statement. */
3037 #define RETM if (m != MATCH_NO) return m;
3040 match_inquire_element (gfc_inquire
*inquire
)
3044 m
= match_etag (&tag_unit
, &inquire
->unit
);
3045 RETM m
= match_etag (&tag_file
, &inquire
->file
);
3046 RETM m
= match_ltag (&tag_err
, &inquire
->err
);
3047 RETM m
= match_out_tag (&tag_iomsg
, &inquire
->iomsg
);
3048 RETM m
= match_out_tag (&tag_iostat
, &inquire
->iostat
);
3049 RETM m
= match_vtag (&tag_exist
, &inquire
->exist
);
3050 RETM m
= match_vtag (&tag_opened
, &inquire
->opened
);
3051 RETM m
= match_vtag (&tag_named
, &inquire
->named
);
3052 RETM m
= match_vtag (&tag_name
, &inquire
->name
);
3053 RETM m
= match_out_tag (&tag_number
, &inquire
->number
);
3054 RETM m
= match_vtag (&tag_s_access
, &inquire
->access
);
3055 RETM m
= match_vtag (&tag_sequential
, &inquire
->sequential
);
3056 RETM m
= match_vtag (&tag_direct
, &inquire
->direct
);
3057 RETM m
= match_vtag (&tag_s_form
, &inquire
->form
);
3058 RETM m
= match_vtag (&tag_formatted
, &inquire
->formatted
);
3059 RETM m
= match_vtag (&tag_unformatted
, &inquire
->unformatted
);
3060 RETM m
= match_out_tag (&tag_s_recl
, &inquire
->recl
);
3061 RETM m
= match_out_tag (&tag_nextrec
, &inquire
->nextrec
);
3062 RETM m
= match_vtag (&tag_s_blank
, &inquire
->blank
);
3063 RETM m
= match_vtag (&tag_s_position
, &inquire
->position
);
3064 RETM m
= match_vtag (&tag_s_action
, &inquire
->action
);
3065 RETM m
= match_vtag (&tag_read
, &inquire
->read
);
3066 RETM m
= match_vtag (&tag_write
, &inquire
->write
);
3067 RETM m
= match_vtag (&tag_readwrite
, &inquire
->readwrite
);
3068 RETM m
= match_vtag (&tag_s_delim
, &inquire
->delim
);
3069 RETM m
= match_vtag (&tag_s_pad
, &inquire
->pad
);
3070 RETM m
= match_vtag (&tag_iolength
, &inquire
->iolength
);
3071 RETM m
= match_vtag (&tag_convert
, &inquire
->convert
);
3072 RETM m
= match_out_tag (&tag_strm_out
, &inquire
->strm_pos
);
3073 RETM
return MATCH_NO
;
3080 gfc_match_inquire (void)
3082 gfc_inquire
*inquire
;
3087 m
= gfc_match_char ('(');
3091 inquire
= gfc_getmem (sizeof (gfc_inquire
));
3093 loc
= gfc_current_locus
;
3095 m
= match_inquire_element (inquire
);
3096 if (m
== MATCH_ERROR
)
3100 m
= gfc_match_expr (&inquire
->unit
);
3101 if (m
== MATCH_ERROR
)
3107 /* See if we have the IOLENGTH form of the inquire statement. */
3108 if (inquire
->iolength
!= NULL
)
3110 if (gfc_match_char (')') != MATCH_YES
)
3113 m
= match_io_list (M_INQUIRE
, &code
);
3114 if (m
== MATCH_ERROR
)
3119 new_st
.op
= EXEC_IOLENGTH
;
3120 new_st
.expr
= inquire
->iolength
;
3121 new_st
.ext
.inquire
= inquire
;
3123 if (gfc_pure (NULL
))
3125 gfc_free_statements (code
);
3126 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3130 new_st
.block
= gfc_get_code ();
3131 new_st
.block
->op
= EXEC_IOLENGTH
;
3132 terminate_io (code
);
3133 new_st
.block
->next
= code
;
3137 /* At this point, we have the non-IOLENGTH inquire statement. */
3140 if (gfc_match_char (')') == MATCH_YES
)
3142 if (gfc_match_char (',') != MATCH_YES
)
3145 m
= match_inquire_element (inquire
);
3146 if (m
== MATCH_ERROR
)
3151 if (inquire
->iolength
!= NULL
)
3153 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3158 if (gfc_match_eos () != MATCH_YES
)
3161 if (inquire
->unit
!= NULL
&& inquire
->file
!= NULL
)
3163 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3164 "UNIT specifiers", &loc
);
3168 if (inquire
->unit
== NULL
&& inquire
->file
== NULL
)
3170 gfc_error ("INQUIRE statement at %L requires either FILE or "
3171 "UNIT specifier", &loc
);
3175 if (gfc_pure (NULL
))
3177 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3181 new_st
.op
= EXEC_INQUIRE
;
3182 new_st
.ext
.inquire
= inquire
;
3186 gfc_syntax_error (ST_INQUIRE
);
3189 gfc_free_inquire (inquire
);
3194 /* Resolve everything in a gfc_inquire structure. */
3197 gfc_resolve_inquire (gfc_inquire
*inquire
)
3199 RESOLVE_TAG (&tag_unit
, inquire
->unit
);
3200 RESOLVE_TAG (&tag_file
, inquire
->file
);
3201 RESOLVE_TAG (&tag_iomsg
, inquire
->iomsg
);
3202 RESOLVE_TAG (&tag_iostat
, inquire
->iostat
);
3203 RESOLVE_TAG (&tag_exist
, inquire
->exist
);
3204 RESOLVE_TAG (&tag_opened
, inquire
->opened
);
3205 RESOLVE_TAG (&tag_number
, inquire
->number
);
3206 RESOLVE_TAG (&tag_named
, inquire
->named
);
3207 RESOLVE_TAG (&tag_name
, inquire
->name
);
3208 RESOLVE_TAG (&tag_s_access
, inquire
->access
);
3209 RESOLVE_TAG (&tag_sequential
, inquire
->sequential
);
3210 RESOLVE_TAG (&tag_direct
, inquire
->direct
);
3211 RESOLVE_TAG (&tag_s_form
, inquire
->form
);
3212 RESOLVE_TAG (&tag_formatted
, inquire
->formatted
);
3213 RESOLVE_TAG (&tag_unformatted
, inquire
->unformatted
);
3214 RESOLVE_TAG (&tag_s_recl
, inquire
->recl
);
3215 RESOLVE_TAG (&tag_nextrec
, inquire
->nextrec
);
3216 RESOLVE_TAG (&tag_s_blank
, inquire
->blank
);
3217 RESOLVE_TAG (&tag_s_position
, inquire
->position
);
3218 RESOLVE_TAG (&tag_s_action
, inquire
->action
);
3219 RESOLVE_TAG (&tag_read
, inquire
->read
);
3220 RESOLVE_TAG (&tag_write
, inquire
->write
);
3221 RESOLVE_TAG (&tag_readwrite
, inquire
->readwrite
);
3222 RESOLVE_TAG (&tag_s_delim
, inquire
->delim
);
3223 RESOLVE_TAG (&tag_s_pad
, inquire
->pad
);
3224 RESOLVE_TAG (&tag_iolength
, inquire
->iolength
);
3225 RESOLVE_TAG (&tag_convert
, inquire
->convert
);
3226 RESOLVE_TAG (&tag_strm_out
, inquire
->strm_pos
);
3228 if (gfc_reference_st_label (inquire
->err
, ST_LABEL_TARGET
) == FAILURE
)