1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
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
30 gfc_st_label format_asterisk
=
31 {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
);
141 if (mode
== MODE_COPY
)
142 *format_string
++ = c
;
150 /* Back up one character position. Only works once. */
159 /* Eat up the spaces and return a character. */
162 next_char_not_space(void)
169 while (gfc_is_whitespace (c
));
173 static int value
= 0;
175 /* Simple lexical analyzer for getting the next token in a FORMAT
186 if (saved_token
!= FMT_NONE
)
189 saved_token
= FMT_NONE
;
193 c
= next_char_not_space ();
201 c
= next_char_not_space ();
212 c
= next_char_not_space ();
214 value
= 10 * value
+ c
- '0';
223 token
= FMT_SIGNED_INT
;
242 c
= next_char_not_space ();
246 value
= 10 * value
+ c
- '0';
251 token
= zflag
? FMT_ZERO
: FMT_POSINT
;
275 c
= next_char_not_space ();
276 if (c
!= 'L' && c
!= 'R')
295 c
= next_char_not_space ();
296 if (c
!= 'P' && c
!= 'S')
303 c
= next_char_not_space ();
304 if (c
== 'N' || c
== 'Z')
365 c
= next_char_not_space ();
366 if (c
== 'N' || c
== 'S')
409 /* Check a format statement. The format string, either from a FORMAT
410 statement or a constant in an I/O statement has already been parsed
411 by itself, and we are checking it for validity. The dual origin
412 means that the warning message is a little less than great. */
417 const char *posint_required
= _("Positive width required");
418 const char *nonneg_required
= _("Nonnegative width required");
419 const char *unexpected_element
= _("Unexpected element");
420 const char *unexpected_end
= _("Unexpected end of format string");
429 saved_token
= FMT_NONE
;
437 error
= _("Missing leading left parenthesis");
443 goto finished
; /* Empty format is legal */
447 /* In this state, the next thing has to be a format item. */
471 /* Signed integer can only precede a P format. */
475 error
= _("Expected P edit descriptor");
482 /* P requires a prior number. */
483 error
= _("P descriptor requires leading scale factor");
487 /* X requires a prior number if we're being pedantic. */
488 if (gfc_notify_std (GFC_STD_GNU
, "Extension: X descriptor "
489 "requires leading space count at %C")
499 goto extension_optional_comma
;
508 if (gfc_notify_std (GFC_STD_GNU
, "Extension: $ descriptor at %C")
511 if (t
!= FMT_RPAREN
|| level
> 0)
513 gfc_warning ("$ should be the last specifier in format at %C");
514 goto optional_comma_1
;
534 error
= unexpected_end
;
538 error
= unexpected_element
;
543 /* In this state, t must currently be a data descriptor.
544 Deal with things that can/must follow the descriptor. */
558 error
= _("Repeat count cannot follow P descriptor");
573 switch (gfc_notification_std (GFC_STD_GNU
))
577 ("Extension: Missing positive width after L descriptor at %C");
582 error
= posint_required
;
607 error
= posint_required
;
614 /* Warn if -std=legacy, otherwise error. */
615 if (gfc_option
.warn_std
!= 0)
616 gfc_error_now ("Period required in format specifier at %C");
618 gfc_warning ("Period required in format specifier at %C");
624 if (u
!= FMT_ZERO
&& u
!= FMT_POSINT
)
626 error
= nonneg_required
;
633 /* Look for optional exponent. */
644 error
= _("Positive exponent width required");
653 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
655 error
= nonneg_required
;
662 /* Warn if -std=legacy, otherwise error. */
663 if (gfc_option
.warn_std
!= 0)
664 gfc_error_now ("Period required in format specifier at %C");
666 gfc_warning ("Period required in format specifier at %C");
672 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
674 error
= nonneg_required
;
681 if(mode
== MODE_STRING
)
683 format_string
+= value
;
684 format_length
-= value
;
698 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
700 error
= nonneg_required
;
712 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
714 error
= nonneg_required
;
722 error
= unexpected_element
;
727 /* Between a descriptor and what comes next. */
746 error
= unexpected_end
;
750 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
757 /* Optional comma is a weird between state where we've just finished
758 reading a colon, slash, dollar or P descriptor. */
773 /* Assume that we have another format item. */
780 extension_optional_comma
:
781 /* As a GNU extension, permit a missing comma after a string literal. */
799 error
= unexpected_end
;
803 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
813 /* Something went wrong. If the format we're checking is a string,
814 generate a warning, since the program is correct. If the format
815 is in a FORMAT statement, this messes up parsing, which is an
817 if (mode
!= MODE_STRING
)
818 gfc_error ("%s in format string at %C", error
);
821 gfc_warning ("%s in format string at %C", error
);
823 /* TODO: More elaborate measures are needed to show where a problem
824 is within a format string that has been calculated. */
834 /* Given an expression node that is a constant string, see if it looks
835 like a format string. */
838 check_format_string (gfc_expr
* e
)
842 format_string
= e
->value
.character
.string
;
847 /************ Fortran 95 I/O statement matchers *************/
849 /* Match a FORMAT statement. This amounts to actually parsing the
850 format descriptors in order to correctly locate the end of the
854 gfc_match_format (void)
859 if (gfc_current_ns
->proc_name
860 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
862 gfc_error ("Format statement in module main block at %C");
866 if (gfc_statement_label
== NULL
)
868 gfc_error ("Missing format label at %C");
871 gfc_gobble_whitespace ();
876 start
= gfc_current_locus
;
878 if (check_format () == FAILURE
)
881 if (gfc_match_eos () != MATCH_YES
)
883 gfc_syntax_error (ST_FORMAT
);
887 /* The label doesn't get created until after the statement is done
888 being matched, so we have to leave the string for later. */
890 gfc_current_locus
= start
; /* Back to the beginning */
893 new_st
.op
= EXEC_NOP
;
896 e
->expr_type
= EXPR_CONSTANT
;
897 e
->ts
.type
= BT_CHARACTER
;
898 e
->ts
.kind
= gfc_default_character_kind
;
900 e
->value
.character
.string
= format_string
= gfc_getmem(format_length
+1);
901 e
->value
.character
.length
= format_length
;
902 gfc_statement_label
->format
= e
;
905 check_format (); /* Guaranteed to succeed */
906 gfc_match_eos (); /* Guaranteed to succeed */
912 /* Match an expression I/O tag of some sort. */
915 match_etag (const io_tag
* tag
, gfc_expr
** v
)
920 m
= gfc_match (tag
->spec
, &result
);
926 gfc_error ("Duplicate %s specification at %C", tag
->name
);
927 gfc_free_expr (result
);
936 /* Match a variable I/O tag of some sort. */
939 match_vtag (const io_tag
* tag
, gfc_expr
** v
)
944 m
= gfc_match (tag
->spec
, &result
);
950 gfc_error ("Duplicate %s specification at %C", tag
->name
);
951 gfc_free_expr (result
);
955 if (result
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
957 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
958 gfc_free_expr (result
);
962 if (gfc_pure (NULL
) && gfc_impure_variable (result
->symtree
->n
.sym
))
964 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
965 gfc_free_expr (result
);
974 /* Match I/O tags that cause variables to become redefined. */
977 match_out_tag(const io_tag
*tag
, gfc_expr
**result
)
981 m
= match_vtag(tag
, result
);
983 gfc_check_do_variable((*result
)->symtree
);
989 /* Match a label I/O tag. */
992 match_ltag (const io_tag
* tag
, gfc_st_label
** label
)
998 m
= gfc_match (tag
->spec
, label
);
999 if (m
== MATCH_YES
&& old
!= 0)
1001 gfc_error ("Duplicate %s label specification at %C", tag
->name
);
1006 && gfc_reference_st_label (*label
, ST_LABEL_TARGET
) == FAILURE
)
1013 /* Do expression resolution and type-checking on an expression tag. */
1016 resolve_tag (const io_tag
* tag
, gfc_expr
* e
)
1022 if (gfc_resolve_expr (e
) == FAILURE
)
1025 if (e
->ts
.type
!= tag
->type
&& tag
!= &tag_format
)
1027 gfc_error ("%s tag at %L must be of type %s", tag
->name
,
1028 &e
->where
, gfc_basic_typename (tag
->type
));
1032 if (tag
== &tag_format
)
1034 if (e
->expr_type
== EXPR_CONSTANT
1035 && (e
->ts
.type
!= BT_CHARACTER
1036 || e
->ts
.kind
!= gfc_default_character_kind
))
1038 gfc_error ("Constant expression in FORMAT tag at %L must be "
1039 "of type default CHARACTER", &e
->where
);
1043 /* If e's rank is zero and e is not an element of an array, it should be
1044 of integer or character type. The integer variable should be
1046 if (e
->symtree
== NULL
|| e
->symtree
->n
.sym
->as
== NULL
1047 || e
->symtree
->n
.sym
->as
->rank
== 0)
1049 if (e
->ts
.type
!= BT_CHARACTER
&& e
->ts
.type
!= BT_INTEGER
)
1051 gfc_error ("%s tag at %L must be of type %s or %s", tag
->name
,
1052 &e
->where
, gfc_basic_typename (BT_CHARACTER
),
1053 gfc_basic_typename (BT_INTEGER
));
1056 else if (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_VARIABLE
)
1058 if (gfc_notify_std (GFC_STD_F95_DEL
,
1059 "Obsolete: ASSIGNED variable in FORMAT tag at %L",
1060 &e
->where
) == FAILURE
)
1062 if (e
->symtree
->n
.sym
->attr
.assign
!= 1)
1064 gfc_error ("Variable '%s' at %L has not been assigned a "
1065 "format label", e
->symtree
->n
.sym
->name
, &e
->where
);
1069 else if (e
->ts
.type
== BT_INTEGER
)
1071 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1072 "variable", gfc_basic_typename (e
->ts
.type
), &e
->where
);
1080 /* if rank is nonzero, we allow the type to be character under
1081 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1082 assigned an Hollerith constant. */
1083 if (e
->ts
.type
== BT_CHARACTER
)
1085 if (gfc_notify_std (GFC_STD_GNU
,
1086 "Extension: Character array in FORMAT tag at %L",
1087 &e
->where
) == FAILURE
)
1092 if (gfc_notify_std (GFC_STD_LEGACY
,
1093 "Extension: Non-character in FORMAT tag at %L",
1094 &e
->where
) == FAILURE
)
1104 gfc_error ("%s tag at %L must be scalar", tag
->name
, &e
->where
);
1108 if (tag
== &tag_iomsg
)
1110 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IOMSG tag at %L",
1111 &e
->where
) == FAILURE
)
1115 if (tag
== &tag_iostat
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1117 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1118 "INTEGER in IOSTAT tag at %L",
1119 &e
->where
) == FAILURE
)
1123 if (tag
== &tag_size
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1125 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1126 "INTEGER in SIZE tag at %L",
1127 &e
->where
) == FAILURE
)
1131 if (tag
== &tag_convert
)
1133 if (gfc_notify_std (GFC_STD_GNU
, "Extension: CONVERT tag at %L",
1134 &e
->where
) == FAILURE
)
1143 /* Match a single tag of an OPEN statement. */
1146 match_open_element (gfc_open
* open
)
1150 m
= match_etag (&tag_unit
, &open
->unit
);
1153 m
= match_out_tag (&tag_iomsg
, &open
->iomsg
);
1156 m
= match_out_tag (&tag_iostat
, &open
->iostat
);
1159 m
= match_etag (&tag_file
, &open
->file
);
1162 m
= match_etag (&tag_status
, &open
->status
);
1165 m
= match_etag (&tag_e_access
, &open
->access
);
1168 m
= match_etag (&tag_e_form
, &open
->form
);
1171 m
= match_etag (&tag_e_recl
, &open
->recl
);
1174 m
= match_etag (&tag_e_blank
, &open
->blank
);
1177 m
= match_etag (&tag_e_position
, &open
->position
);
1180 m
= match_etag (&tag_e_action
, &open
->action
);
1183 m
= match_etag (&tag_e_delim
, &open
->delim
);
1186 m
= match_etag (&tag_e_pad
, &open
->pad
);
1189 m
= match_ltag (&tag_err
, &open
->err
);
1192 m
= match_etag (&tag_convert
, &open
->convert
);
1200 /* Free the gfc_open structure and all the expressions it contains. */
1203 gfc_free_open (gfc_open
* open
)
1209 gfc_free_expr (open
->unit
);
1210 gfc_free_expr (open
->iomsg
);
1211 gfc_free_expr (open
->iostat
);
1212 gfc_free_expr (open
->file
);
1213 gfc_free_expr (open
->status
);
1214 gfc_free_expr (open
->access
);
1215 gfc_free_expr (open
->form
);
1216 gfc_free_expr (open
->recl
);
1217 gfc_free_expr (open
->blank
);
1218 gfc_free_expr (open
->position
);
1219 gfc_free_expr (open
->action
);
1220 gfc_free_expr (open
->delim
);
1221 gfc_free_expr (open
->pad
);
1222 gfc_free_expr (open
->convert
);
1228 /* Resolve everything in a gfc_open structure. */
1231 gfc_resolve_open (gfc_open
* open
)
1234 RESOLVE_TAG (&tag_unit
, open
->unit
);
1235 RESOLVE_TAG (&tag_iomsg
, open
->iomsg
);
1236 RESOLVE_TAG (&tag_iostat
, open
->iostat
);
1237 RESOLVE_TAG (&tag_file
, open
->file
);
1238 RESOLVE_TAG (&tag_status
, open
->status
);
1239 RESOLVE_TAG (&tag_e_access
, open
->access
);
1240 RESOLVE_TAG (&tag_e_form
, open
->form
);
1241 RESOLVE_TAG (&tag_e_recl
, open
->recl
);
1243 RESOLVE_TAG (&tag_e_blank
, open
->blank
);
1244 RESOLVE_TAG (&tag_e_position
, open
->position
);
1245 RESOLVE_TAG (&tag_e_action
, open
->action
);
1246 RESOLVE_TAG (&tag_e_delim
, open
->delim
);
1247 RESOLVE_TAG (&tag_e_pad
, open
->pad
);
1248 RESOLVE_TAG (&tag_convert
, open
->convert
);
1250 if (gfc_reference_st_label (open
->err
, ST_LABEL_TARGET
) == FAILURE
)
1258 /* Check if a given value for a SPECIFIER is either in the list of values
1259 allowed in F95 or F2003, issuing an error message and returning a zero
1260 value if it is not allowed. */
1262 compare_to_allowed_values (const char * specifier
, const char * allowed
[],
1263 const char * allowed_f2003
[],
1264 const char * allowed_gnu
[], char * value
,
1265 const char * statement
, bool warn
)
1270 len
= strlen(value
);
1273 for (len
--; len
> 0; len
--)
1274 if (value
[len
] != ' ')
1279 for (i
= 0; allowed
[i
]; i
++)
1280 if (len
== strlen(allowed
[i
])
1281 && strncasecmp (value
, allowed
[i
], strlen(allowed
[i
])) == 0)
1284 for (i
= 0; allowed_f2003
&& allowed_f2003
[i
]; i
++)
1285 if (len
== strlen(allowed_f2003
[i
])
1286 && strncasecmp (value
, allowed_f2003
[i
], strlen(allowed_f2003
[i
])) == 0)
1288 notification n
= gfc_notification_std (GFC_STD_F2003
);
1290 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1292 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1293 "has value '%s'", specifier
, statement
,
1300 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: %s specifier in "
1301 "%s statement at %C has value '%s'", specifier
,
1302 statement
, allowed_f2003
[i
]);
1310 for (i
= 0; allowed_gnu
&& allowed_gnu
[i
]; i
++)
1311 if (len
== strlen(allowed_gnu
[i
])
1312 && strncasecmp (value
, allowed_gnu
[i
], strlen(allowed_gnu
[i
])) == 0)
1314 notification n
= gfc_notification_std (GFC_STD_GNU
);
1316 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1318 gfc_warning ("Extension: %s specifier in %s statement at %C "
1319 "has value '%s'", specifier
, statement
,
1326 gfc_notify_std (GFC_STD_GNU
, "Extension: %s specifier in "
1327 "%s statement at %C has value '%s'", specifier
,
1328 statement
, allowed_gnu
[i
]);
1338 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1339 specifier
, statement
, value
);
1344 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1345 specifier
, statement
, value
);
1350 /* Match an OPEN statement. */
1353 gfc_match_open (void)
1359 m
= gfc_match_char ('(');
1363 open
= gfc_getmem (sizeof (gfc_open
));
1365 m
= match_open_element (open
);
1367 if (m
== MATCH_ERROR
)
1371 m
= gfc_match_expr (&open
->unit
);
1374 if (m
== MATCH_ERROR
)
1380 if (gfc_match_char (')') == MATCH_YES
)
1382 if (gfc_match_char (',') != MATCH_YES
)
1385 m
= match_open_element (open
);
1386 if (m
== MATCH_ERROR
)
1392 if (gfc_match_eos () == MATCH_NO
)
1395 if (gfc_pure (NULL
))
1397 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1401 warn
= (open
->err
|| open
->iostat
) ? true : false;
1402 /* Checks on the ACCESS specifier. */
1403 if (open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
)
1405 static const char * access_f95
[] = { "SEQUENTIAL", "DIRECT", NULL
};
1406 static const char * access_f2003
[] = { "STREAM", NULL
};
1407 static const char * access_gnu
[] = { "APPEND", NULL
};
1409 if (!compare_to_allowed_values ("ACCESS", access_f95
, access_f2003
,
1411 open
->access
->value
.character
.string
,
1416 /* Checks on the ACTION specifier. */
1417 if (open
->action
&& open
->action
->expr_type
== EXPR_CONSTANT
)
1419 static const char * action
[] = { "READ", "WRITE", "READWRITE", NULL
};
1421 if (!compare_to_allowed_values ("ACTION", action
, NULL
, NULL
,
1422 open
->action
->value
.character
.string
,
1427 /* Checks on the ASYNCHRONOUS specifier. */
1428 /* TODO: code is ready, just needs uncommenting when async I/O support
1430 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1432 static const char * asynchronous[] = { "YES", "NO", NULL };
1434 if (!compare_to_allowed_values
1435 ("action", asynchronous, NULL, NULL,
1436 open->asynchronous->value.character.string, "OPEN", warn))
1440 /* Checks on the BLANK specifier. */
1441 if (open
->blank
&& open
->blank
->expr_type
== EXPR_CONSTANT
)
1443 static const char * blank
[] = { "ZERO", "NULL", NULL
};
1445 if (!compare_to_allowed_values ("BLANK", blank
, NULL
, NULL
,
1446 open
->blank
->value
.character
.string
,
1451 /* Checks on the DECIMAL specifier. */
1452 /* TODO: uncomment this code when DECIMAL support is added
1453 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1455 static const char * decimal[] = { "COMMA", "POINT", NULL };
1457 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1458 open->decimal->value.character.string,
1463 /* Checks on the DELIM specifier. */
1464 if (open
->delim
&& open
->delim
->expr_type
== EXPR_CONSTANT
)
1466 static const char * delim
[] = { "APOSTROPHE", "QUOTE", "NONE", NULL
};
1468 if (!compare_to_allowed_values ("DELIM", delim
, NULL
, NULL
,
1469 open
->delim
->value
.character
.string
,
1474 /* Checks on the ENCODING specifier. */
1475 /* TODO: uncomment this code when ENCODING support is added
1476 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1478 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1480 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1481 open->encoding->value.character.string,
1486 /* Checks on the FORM specifier. */
1487 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
)
1489 static const char * form
[] = { "FORMATTED", "UNFORMATTED", NULL
};
1491 if (!compare_to_allowed_values ("FORM", form
, NULL
, NULL
,
1492 open
->form
->value
.character
.string
,
1497 /* Checks on the PAD specifier. */
1498 if (open
->pad
&& open
->pad
->expr_type
== EXPR_CONSTANT
)
1500 static const char * pad
[] = { "YES", "NO", NULL
};
1502 if (!compare_to_allowed_values ("PAD", pad
, NULL
, NULL
,
1503 open
->pad
->value
.character
.string
,
1508 /* Checks on the POSITION specifier. */
1509 if (open
->position
&& open
->position
->expr_type
== EXPR_CONSTANT
)
1511 static const char * position
[] = { "ASIS", "REWIND", "APPEND", NULL
};
1513 if (!compare_to_allowed_values ("POSITION", position
, NULL
, NULL
,
1514 open
->position
->value
.character
.string
,
1519 /* Checks on the ROUND specifier. */
1520 /* TODO: uncomment this code when ROUND support is added
1521 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1523 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1524 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1526 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1527 open->round->value.character.string,
1532 /* Checks on the SIGN specifier. */
1533 /* TODO: uncomment this code when SIGN support is added
1534 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1536 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1539 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1540 open->sign->value.character.string,
1545 #define warn_or_error(...) \
1548 gfc_warning (__VA_ARGS__); \
1551 gfc_error (__VA_ARGS__); \
1556 /* Checks on the RECL specifier. */
1557 if (open
->recl
&& open
->recl
->expr_type
== EXPR_CONSTANT
1558 && open
->recl
->ts
.type
== BT_INTEGER
1559 && mpz_sgn (open
->recl
->value
.integer
) != 1)
1561 warn_or_error ("RECL in OPEN statement at %C must be positive");
1564 /* Checks on the STATUS specifier. */
1565 if (open
->status
&& open
->status
->expr_type
== EXPR_CONSTANT
)
1567 static const char * status
[] = { "OLD", "NEW", "SCRATCH",
1568 "REPLACE", "UNKNOWN", NULL
};
1570 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1571 open
->status
->value
.character
.string
,
1575 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1576 the FILE= specifier shall appear. */
1577 if (open
->file
== NULL
&&
1578 (strncasecmp (open
->status
->value
.character
.string
, "replace", 7) == 0
1579 || strncasecmp (open
->status
->value
.character
.string
, "new", 3) == 0))
1581 warn_or_error ("The STATUS specified in OPEN statement at %C is '%s' "
1582 "and no FILE specifier is present",
1583 open
->status
->value
.character
.string
);
1586 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1587 the FILE= specifier shall not appear. */
1588 if (strncasecmp (open
->status
->value
.character
.string
, "scratch", 7) == 0
1591 warn_or_error ("The STATUS specified in OPEN statement at %C cannot "
1592 "have the value SCRATCH if a FILE specifier "
1597 /* Things that are not allowed for unformatted I/O. */
1598 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
1600 /* TODO uncomment this code when F2003 support is finished */
1601 /* || open->decimal || open->encoding || open->round
1603 || open
->pad
|| open
->blank
)
1604 && strncasecmp (open
->form
->value
.character
.string
,
1605 "unformatted", 11) == 0)
1607 const char * spec
= (open
->delim
? "DELIM " : (open
->pad
? "PAD " :
1608 open
->blank
? "BLANK " : ""));
1610 warn_or_error ("%sspecifier at %C not allowed in OPEN statement for "
1611 "unformatted I/O", spec
);
1614 if (open
->recl
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1615 && strncasecmp (open
->access
->value
.character
.string
, "stream", 6) == 0)
1617 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1621 if (open
->position
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1622 && !(strncasecmp (open
->access
->value
.character
.string
,
1623 "sequential", 10) == 0
1624 || strncasecmp (open
->access
->value
.character
.string
,
1626 || strncasecmp (open
->access
->value
.character
.string
,
1629 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1630 "for stream or sequential ACCESS");
1633 #undef warn_or_error
1635 new_st
.op
= EXEC_OPEN
;
1636 new_st
.ext
.open
= open
;
1640 gfc_syntax_error (ST_OPEN
);
1643 gfc_free_open (open
);
1648 /* Free a gfc_close structure an all its expressions. */
1651 gfc_free_close (gfc_close
* close
)
1657 gfc_free_expr (close
->unit
);
1658 gfc_free_expr (close
->iomsg
);
1659 gfc_free_expr (close
->iostat
);
1660 gfc_free_expr (close
->status
);
1666 /* Match elements of a CLOSE statement. */
1669 match_close_element (gfc_close
* close
)
1673 m
= match_etag (&tag_unit
, &close
->unit
);
1676 m
= match_etag (&tag_status
, &close
->status
);
1679 m
= match_out_tag (&tag_iomsg
, &close
->iomsg
);
1682 m
= match_out_tag (&tag_iostat
, &close
->iostat
);
1685 m
= match_ltag (&tag_err
, &close
->err
);
1693 /* Match a CLOSE statement. */
1696 gfc_match_close (void)
1702 m
= gfc_match_char ('(');
1706 close
= gfc_getmem (sizeof (gfc_close
));
1708 m
= match_close_element (close
);
1710 if (m
== MATCH_ERROR
)
1714 m
= gfc_match_expr (&close
->unit
);
1717 if (m
== MATCH_ERROR
)
1723 if (gfc_match_char (')') == MATCH_YES
)
1725 if (gfc_match_char (',') != MATCH_YES
)
1728 m
= match_close_element (close
);
1729 if (m
== MATCH_ERROR
)
1735 if (gfc_match_eos () == MATCH_NO
)
1738 if (gfc_pure (NULL
))
1740 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1744 warn
= (close
->iostat
|| close
->err
) ? true : false;
1746 /* Checks on the STATUS specifier. */
1747 if (close
->status
&& close
->status
->expr_type
== EXPR_CONSTANT
)
1749 static const char * status
[] = { "KEEP", "DELETE", NULL
};
1751 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1752 close
->status
->value
.character
.string
,
1757 new_st
.op
= EXEC_CLOSE
;
1758 new_st
.ext
.close
= close
;
1762 gfc_syntax_error (ST_CLOSE
);
1765 gfc_free_close (close
);
1770 /* Resolve everything in a gfc_close structure. */
1773 gfc_resolve_close (gfc_close
* close
)
1776 RESOLVE_TAG (&tag_unit
, close
->unit
);
1777 RESOLVE_TAG (&tag_iomsg
, close
->iomsg
);
1778 RESOLVE_TAG (&tag_iostat
, close
->iostat
);
1779 RESOLVE_TAG (&tag_status
, close
->status
);
1781 if (gfc_reference_st_label (close
->err
, ST_LABEL_TARGET
) == FAILURE
)
1788 /* Free a gfc_filepos structure. */
1791 gfc_free_filepos (gfc_filepos
* fp
)
1794 gfc_free_expr (fp
->unit
);
1795 gfc_free_expr (fp
->iomsg
);
1796 gfc_free_expr (fp
->iostat
);
1801 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1804 match_file_element (gfc_filepos
* fp
)
1808 m
= match_etag (&tag_unit
, &fp
->unit
);
1811 m
= match_out_tag (&tag_iomsg
, &fp
->iomsg
);
1814 m
= match_out_tag (&tag_iostat
, &fp
->iostat
);
1817 m
= match_ltag (&tag_err
, &fp
->err
);
1825 /* Match the second half of the file-positioning statements, REWIND,
1826 BACKSPACE, ENDFILE, or the FLUSH statement. */
1829 match_filepos (gfc_statement st
, gfc_exec_op op
)
1834 fp
= gfc_getmem (sizeof (gfc_filepos
));
1836 if (gfc_match_char ('(') == MATCH_NO
)
1838 m
= gfc_match_expr (&fp
->unit
);
1839 if (m
== MATCH_ERROR
)
1847 m
= match_file_element (fp
);
1848 if (m
== MATCH_ERROR
)
1852 m
= gfc_match_expr (&fp
->unit
);
1853 if (m
== MATCH_ERROR
)
1861 if (gfc_match_char (')') == MATCH_YES
)
1863 if (gfc_match_char (',') != MATCH_YES
)
1866 m
= match_file_element (fp
);
1867 if (m
== MATCH_ERROR
)
1874 if (gfc_match_eos () != MATCH_YES
)
1877 if (gfc_pure (NULL
))
1879 gfc_error ("%s statement not allowed in PURE procedure at %C",
1880 gfc_ascii_statement (st
));
1886 new_st
.ext
.filepos
= fp
;
1890 gfc_syntax_error (st
);
1893 gfc_free_filepos (fp
);
1899 gfc_resolve_filepos (gfc_filepos
* fp
)
1902 RESOLVE_TAG (&tag_unit
, fp
->unit
);
1903 RESOLVE_TAG (&tag_iostat
, fp
->iostat
);
1904 RESOLVE_TAG (&tag_iomsg
, fp
->iomsg
);
1905 if (gfc_reference_st_label (fp
->err
, ST_LABEL_TARGET
) == FAILURE
)
1912 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1913 and the FLUSH statement. */
1916 gfc_match_endfile (void)
1919 return match_filepos (ST_END_FILE
, EXEC_ENDFILE
);
1923 gfc_match_backspace (void)
1926 return match_filepos (ST_BACKSPACE
, EXEC_BACKSPACE
);
1930 gfc_match_rewind (void)
1933 return match_filepos (ST_REWIND
, EXEC_REWIND
);
1937 gfc_match_flush (void)
1939 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: FLUSH statement at %C") == FAILURE
)
1942 return match_filepos (ST_FLUSH
, EXEC_FLUSH
);
1945 /******************** Data Transfer Statements *********************/
1948 { M_READ
, M_WRITE
, M_PRINT
, M_INQUIRE
}
1952 /* Return a default unit number. */
1955 default_unit (io_kind k
)
1964 return gfc_int_expr (unit
);
1968 /* Match a unit specification for a data transfer statement. */
1971 match_dt_unit (io_kind k
, gfc_dt
* dt
)
1975 if (gfc_match_char ('*') == MATCH_YES
)
1977 if (dt
->io_unit
!= NULL
)
1980 dt
->io_unit
= default_unit (k
);
1984 if (gfc_match_expr (&e
) == MATCH_YES
)
1986 if (dt
->io_unit
!= NULL
)
1999 gfc_error ("Duplicate UNIT specification at %C");
2004 /* Match a format specification. */
2007 match_dt_format (gfc_dt
* dt
)
2011 gfc_st_label
*label
;
2013 where
= gfc_current_locus
;
2015 if (gfc_match_char ('*') == MATCH_YES
)
2017 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2020 dt
->format_label
= &format_asterisk
;
2024 if (gfc_match_st_label (&label
) == MATCH_YES
)
2026 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2028 gfc_free_st_label (label
);
2032 if (gfc_reference_st_label (label
, ST_LABEL_FORMAT
) == FAILURE
)
2035 dt
->format_label
= label
;
2039 if (gfc_match_expr (&e
) == MATCH_YES
)
2041 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2046 dt
->format_expr
= e
;
2050 gfc_current_locus
= where
; /* The only case where we have to restore */
2055 gfc_error ("Duplicate format specification at %C");
2060 /* Traverse a namelist that is part of a READ statement to make sure
2061 that none of the variables in the namelist are INTENT(IN). Returns
2062 nonzero if we find such a variable. */
2065 check_namelist (gfc_symbol
* sym
)
2069 for (p
= sym
->namelist
; p
; p
= p
->next
)
2070 if (p
->sym
->attr
.intent
== INTENT_IN
)
2072 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2073 p
->sym
->name
, sym
->name
);
2081 /* Match a single data transfer element. */
2084 match_dt_element (io_kind k
, gfc_dt
* dt
)
2086 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2090 if (gfc_match (" unit =") == MATCH_YES
)
2092 m
= match_dt_unit (k
, dt
);
2097 if (gfc_match (" fmt =") == MATCH_YES
)
2099 m
= match_dt_format (dt
);
2104 if (gfc_match (" nml = %n", name
) == MATCH_YES
)
2106 if (dt
->namelist
!= NULL
)
2108 gfc_error ("Duplicate NML specification at %C");
2112 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2115 if (sym
== NULL
|| sym
->attr
.flavor
!= FL_NAMELIST
)
2117 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2118 sym
!= NULL
? sym
->name
: name
);
2123 if (k
== M_READ
&& check_namelist (sym
))
2129 m
= match_etag (&tag_rec
, &dt
->rec
);
2132 m
= match_etag (&tag_spos
, &dt
->rec
);
2135 m
= match_out_tag (&tag_iomsg
, &dt
->iomsg
);
2138 m
= match_out_tag (&tag_iostat
, &dt
->iostat
);
2141 m
= match_ltag (&tag_err
, &dt
->err
);
2143 dt
->err_where
= gfc_current_locus
;
2146 m
= match_etag (&tag_advance
, &dt
->advance
);
2149 m
= match_out_tag (&tag_size
, &dt
->size
);
2153 m
= match_ltag (&tag_end
, &dt
->end
);
2158 gfc_error ("END tag at %C not allowed in output statement");
2161 dt
->end_where
= gfc_current_locus
;
2166 m
= match_ltag (&tag_eor
, &dt
->eor
);
2168 dt
->eor_where
= gfc_current_locus
;
2176 /* Free a data transfer structure and everything below it. */
2179 gfc_free_dt (gfc_dt
* dt
)
2185 gfc_free_expr (dt
->io_unit
);
2186 gfc_free_expr (dt
->format_expr
);
2187 gfc_free_expr (dt
->rec
);
2188 gfc_free_expr (dt
->advance
);
2189 gfc_free_expr (dt
->iomsg
);
2190 gfc_free_expr (dt
->iostat
);
2191 gfc_free_expr (dt
->size
);
2197 /* Resolve everything in a gfc_dt structure. */
2200 gfc_resolve_dt (gfc_dt
* dt
)
2204 RESOLVE_TAG (&tag_format
, dt
->format_expr
);
2205 RESOLVE_TAG (&tag_rec
, dt
->rec
);
2206 RESOLVE_TAG (&tag_spos
, dt
->rec
);
2207 RESOLVE_TAG (&tag_advance
, dt
->advance
);
2208 RESOLVE_TAG (&tag_iomsg
, dt
->iomsg
);
2209 RESOLVE_TAG (&tag_iostat
, dt
->iostat
);
2210 RESOLVE_TAG (&tag_size
, dt
->size
);
2213 if (gfc_resolve_expr (e
) == SUCCESS
2214 && (e
->ts
.type
!= BT_INTEGER
2215 && (e
->ts
.type
!= BT_CHARACTER
2216 || e
->expr_type
!= EXPR_VARIABLE
)))
2219 ("UNIT specification at %L must be an INTEGER expression or a "
2220 "CHARACTER variable", &e
->where
);
2224 if (e
->ts
.type
== BT_CHARACTER
)
2226 if (gfc_has_vector_index (e
))
2228 gfc_error ("Internal unit with vector subscript at %L",
2234 if (e
->rank
&& e
->ts
.type
!= BT_CHARACTER
)
2236 gfc_error ("External IO UNIT cannot be an array at %L", &e
->where
);
2242 if (gfc_reference_st_label (dt
->err
, ST_LABEL_TARGET
) == FAILURE
)
2244 if (dt
->err
->defined
== ST_LABEL_UNKNOWN
)
2246 gfc_error ("ERR tag label %d at %L not defined",
2247 dt
->err
->value
, &dt
->err_where
);
2254 if (gfc_reference_st_label (dt
->end
, ST_LABEL_TARGET
) == FAILURE
)
2256 if (dt
->end
->defined
== ST_LABEL_UNKNOWN
)
2258 gfc_error ("END tag label %d at %L not defined",
2259 dt
->end
->value
, &dt
->end_where
);
2266 if (gfc_reference_st_label (dt
->eor
, ST_LABEL_TARGET
) == FAILURE
)
2268 if (dt
->eor
->defined
== ST_LABEL_UNKNOWN
)
2270 gfc_error ("EOR tag label %d at %L not defined",
2271 dt
->eor
->value
, &dt
->eor_where
);
2276 /* Check the format label actually exists. */
2277 if (dt
->format_label
&& dt
->format_label
!= &format_asterisk
2278 && dt
->format_label
->defined
== ST_LABEL_UNKNOWN
)
2280 gfc_error ("FORMAT label %d at %L not defined", dt
->format_label
->value
,
2281 &dt
->format_label
->where
);
2288 /* Given an io_kind, return its name. */
2291 io_kind_name (io_kind k
)
2310 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2317 /* Match an IO iteration statement of the form:
2319 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2321 which is equivalent to a single IO element. This function is
2322 mutually recursive with match_io_element(). */
2324 static match
match_io_element (io_kind k
, gfc_code
**);
2327 match_io_iterator (io_kind k
, gfc_code
** result
)
2329 gfc_code
*head
, *tail
, *new;
2337 old_loc
= gfc_current_locus
;
2339 if (gfc_match_char ('(') != MATCH_YES
)
2342 m
= match_io_element (k
, &head
);
2345 if (m
!= MATCH_YES
|| gfc_match_char (',') != MATCH_YES
)
2351 /* Can't be anything but an IO iterator. Build a list. */
2352 iter
= gfc_get_iterator ();
2356 m
= gfc_match_iterator (iter
, 0);
2357 if (m
== MATCH_ERROR
)
2361 gfc_check_do_variable (iter
->var
->symtree
);
2365 m
= match_io_element (k
, &new);
2366 if (m
== MATCH_ERROR
)
2375 tail
= gfc_append_code (tail
, new);
2377 if (gfc_match_char (',') != MATCH_YES
)
2386 if (gfc_match_char (')') != MATCH_YES
)
2389 new = gfc_get_code ();
2391 new->ext
.iterator
= iter
;
2393 new->block
= gfc_get_code ();
2394 new->block
->op
= EXEC_DO
;
2395 new->block
->next
= head
;
2401 gfc_error ("Syntax error in I/O iterator at %C");
2405 gfc_free_iterator (iter
, 1);
2406 gfc_free_statements (head
);
2407 gfc_current_locus
= old_loc
;
2412 /* Match a single element of an IO list, which is either a single
2413 expression or an IO Iterator. */
2416 match_io_element (io_kind k
, gfc_code
** cpp
)
2424 m
= match_io_iterator (k
, cpp
);
2430 m
= gfc_match_variable (&expr
, 0);
2432 gfc_error ("Expected variable in READ statement at %C");
2436 m
= gfc_match_expr (&expr
);
2438 gfc_error ("Expected expression in %s statement at %C",
2446 if (expr
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
2449 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2450 expr
->symtree
->n
.sym
->name
);
2455 && gfc_impure_variable (expr
->symtree
->n
.sym
)
2456 && current_dt
->io_unit
->ts
.type
== BT_CHARACTER
)
2458 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2459 expr
->symtree
->n
.sym
->name
);
2463 if (gfc_check_do_variable (expr
->symtree
))
2469 if (current_dt
->io_unit
->ts
.type
== BT_CHARACTER
2471 && current_dt
->io_unit
->expr_type
== EXPR_VARIABLE
2472 && gfc_impure_variable (current_dt
->io_unit
->symtree
->n
.sym
))
2475 ("Cannot write to internal file unit '%s' at %C inside a "
2476 "PURE procedure", current_dt
->io_unit
->symtree
->n
.sym
->name
);
2488 gfc_free_expr (expr
);
2492 cp
= gfc_get_code ();
2493 cp
->op
= EXEC_TRANSFER
;
2501 /* Match an I/O list, building gfc_code structures as we go. */
2504 match_io_list (io_kind k
, gfc_code
** head_p
)
2506 gfc_code
*head
, *tail
, *new;
2509 *head_p
= head
= tail
= NULL
;
2510 if (gfc_match_eos () == MATCH_YES
)
2515 m
= match_io_element (k
, &new);
2516 if (m
== MATCH_ERROR
)
2521 tail
= gfc_append_code (tail
, new);
2525 if (gfc_match_eos () == MATCH_YES
)
2527 if (gfc_match_char (',') != MATCH_YES
)
2535 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2538 gfc_free_statements (head
);
2543 /* Attach the data transfer end node. */
2546 terminate_io (gfc_code
* io_code
)
2550 if (io_code
== NULL
)
2551 io_code
= new_st
.block
;
2553 c
= gfc_get_code ();
2554 c
->op
= EXEC_DT_END
;
2556 /* Point to structure that is already there */
2557 c
->ext
.dt
= new_st
.ext
.dt
;
2558 gfc_append_code (io_code
, c
);
2562 /* Check the constraints for a data transfer statement. The majority of the
2563 constraints appearing in 9.4 of the standard appear here. Some are handled
2564 in resolve_tag and others in gfc_resolve_dt. */
2567 check_io_constraints (io_kind k
, gfc_dt
*dt
, gfc_code
* io_code
, locus
* spec_end
)
2569 #define io_constraint(condition,msg,arg)\
2572 gfc_error(msg,arg);\
2578 gfc_symbol
* sym
= NULL
;
2583 if (expr
&& expr
->expr_type
== EXPR_VARIABLE
2584 && expr
->ts
.type
== BT_CHARACTER
)
2586 sym
= expr
->symtree
->n
.sym
;
2588 io_constraint (k
== M_WRITE
&& sym
->attr
.intent
== INTENT_IN
,
2589 "Internal file at %L must not be INTENT(IN)",
2592 io_constraint (gfc_has_vector_index (dt
->io_unit
),
2593 "Internal file incompatible with vector subscript at %L",
2596 io_constraint (dt
->rec
!= NULL
,
2597 "REC tag at %L is incompatible with internal file",
2600 if (dt
->namelist
!= NULL
)
2602 if (gfc_notify_std(GFC_STD_F2003
,
2603 "Fortran 2003: Internal file at %L with namelist",
2604 &expr
->where
) == FAILURE
)
2608 io_constraint (dt
->advance
!= NULL
,
2609 "ADVANCE tag at %L is incompatible with internal file",
2610 &dt
->advance
->where
);
2613 if (expr
&& expr
->ts
.type
!= BT_CHARACTER
)
2616 io_constraint (gfc_pure (NULL
)
2617 && (k
== M_READ
|| k
== M_WRITE
),
2618 "IO UNIT in %s statement at %C must be "
2619 "an internal file in a PURE procedure",
2626 io_constraint (dt
->end
,
2627 "END tag not allowed with output at %L",
2630 io_constraint (dt
->eor
,
2631 "EOR tag not allowed with output at %L",
2634 io_constraint (k
!= M_READ
&& dt
->size
,
2635 "SIZE=specifier not allowed with output at %L",
2640 io_constraint (dt
->size
&& dt
->advance
== NULL
,
2641 "SIZE tag at %L requires an ADVANCE tag",
2644 io_constraint (dt
->eor
&& dt
->advance
== NULL
,
2645 "EOR tag at %L requires an ADVANCE tag",
2653 io_constraint (io_code
&& dt
->namelist
,
2654 "NAMELIST cannot be followed by IO-list at %L",
2657 io_constraint (dt
->format_expr
,
2658 "IO spec-list cannot contain both NAMELIST group name "
2659 "and format specification at %L.",
2660 &dt
->format_expr
->where
);
2662 io_constraint (dt
->format_label
,
2663 "IO spec-list cannot contain both NAMELIST group name "
2664 "and format label at %L", spec_end
);
2666 io_constraint (dt
->rec
,
2667 "NAMELIST IO is not allowed with a REC=specifier "
2668 "at %L.", &dt
->rec
->where
);
2670 io_constraint (dt
->advance
,
2671 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2672 "at %L.", &dt
->advance
->where
);
2677 io_constraint (dt
->end
,
2678 "An END tag is not allowed with a "
2679 "REC=specifier at %L.", &dt
->end_where
);
2682 io_constraint (dt
->format_label
== &format_asterisk
,
2683 "FMT=* is not allowed with a REC=specifier "
2684 "at %L.", spec_end
);
2689 int not_yes
, not_no
;
2692 io_constraint (dt
->format_label
== &format_asterisk
,
2693 "List directed format(*) is not allowed with a "
2694 "ADVANCE=specifier at %L.", &expr
->where
);
2696 io_constraint (dt
->format_expr
== NULL
2697 && dt
->format_label
== NULL
2698 && dt
->namelist
== NULL
,
2699 "the ADVANCE=specifier at %L must appear with an "
2700 "explicit format expression", &expr
->where
);
2702 if (expr
->expr_type
== EXPR_CONSTANT
&& expr
->ts
.type
== BT_CHARACTER
)
2704 const char * advance
= expr
->value
.character
.string
;
2705 not_no
= strcasecmp (advance
, "no") != 0;
2706 not_yes
= strcasecmp (advance
, "yes") != 0;
2714 io_constraint (not_no
&& not_yes
,
2715 "ADVANCE=specifier at %L must have value = "
2716 "YES or NO.", &expr
->where
);
2718 io_constraint (dt
->size
&& not_no
&& k
== M_READ
,
2719 "SIZE tag at %L requires an ADVANCE = 'NO'",
2722 io_constraint (dt
->eor
&& not_no
&& k
== M_READ
,
2723 "EOR tag at %L requires an ADVANCE = 'NO'",
2727 expr
= dt
->format_expr
;
2728 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
)
2729 check_format_string (expr
);
2733 #undef io_constraint
2735 /* Match a READ, WRITE or PRINT statement. */
2738 match_io (io_kind k
)
2740 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2749 where
= gfc_current_locus
;
2751 current_dt
= dt
= gfc_getmem (sizeof (gfc_dt
));
2752 m
= gfc_match_char ('(');
2755 where
= gfc_current_locus
;
2758 else if (k
== M_PRINT
)
2760 /* Treat the non-standard case of PRINT namelist. */
2761 if ((gfc_current_form
== FORM_FIXED
|| gfc_peek_char () == ' ')
2762 && gfc_match_name (name
) == MATCH_YES
)
2764 gfc_find_symbol (name
, NULL
, 1, &sym
);
2765 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2767 if (gfc_notify_std (GFC_STD_GNU
, "PRINT namelist at "
2768 "%C is an extension") == FAILURE
)
2774 dt
->io_unit
= default_unit (k
);
2779 gfc_current_locus
= where
;
2783 if (gfc_current_form
== FORM_FREE
)
2785 c
= gfc_peek_char();
2786 if (c
!= ' ' && c
!= '*' && c
!= '\'' && c
!= '"')
2793 m
= match_dt_format (dt
);
2794 if (m
== MATCH_ERROR
)
2800 dt
->io_unit
= default_unit (k
);
2805 /* Before issuing an error for a malformed 'print (1,*)' type of
2806 error, check for a default-char-expr of the form ('(I0)'). */
2808 if (k
== M_PRINT
&& m
== MATCH_YES
)
2810 /* Reset current locus to get the initial '(' in an expression. */
2811 gfc_current_locus
= where
;
2812 dt
->format_expr
= NULL
;
2813 m
= match_dt_format (dt
);
2815 if (m
== MATCH_ERROR
)
2817 if (m
== MATCH_NO
|| dt
->format_expr
== NULL
)
2821 dt
->io_unit
= default_unit (k
);
2826 /* Match a control list */
2827 if (match_dt_element (k
, dt
) == MATCH_YES
)
2829 if (match_dt_unit (k
, dt
) != MATCH_YES
)
2832 if (gfc_match_char (')') == MATCH_YES
)
2834 if (gfc_match_char (',') != MATCH_YES
)
2837 m
= match_dt_element (k
, dt
);
2840 if (m
== MATCH_ERROR
)
2843 m
= match_dt_format (dt
);
2846 if (m
== MATCH_ERROR
)
2849 where
= gfc_current_locus
;
2851 m
= gfc_match_name (name
);
2854 gfc_find_symbol (name
, NULL
, 1, &sym
);
2855 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2858 if (k
== M_READ
&& check_namelist (sym
))
2867 gfc_current_locus
= where
;
2869 goto loop
; /* No matches, try regular elements */
2872 if (gfc_match_char (')') == MATCH_YES
)
2874 if (gfc_match_char (',') != MATCH_YES
)
2880 m
= match_dt_element (k
, dt
);
2883 if (m
== MATCH_ERROR
)
2886 if (gfc_match_char (')') == MATCH_YES
)
2888 if (gfc_match_char (',') != MATCH_YES
)
2894 /* Used in check_io_constraints, where no locus is available. */
2895 spec_end
= gfc_current_locus
;
2897 /* Optional leading comma (non-standard). */
2899 && gfc_match_char (',') == MATCH_YES
2901 && gfc_notify_std (GFC_STD_GNU
, "Extension: Comma before output "
2902 "item list at %C is an extension") == FAILURE
)
2906 if (gfc_match_eos () != MATCH_YES
)
2908 if (comma_flag
&& gfc_match_char (',') != MATCH_YES
)
2910 gfc_error ("Expected comma in I/O list at %C");
2915 m
= match_io_list (k
, &io_code
);
2916 if (m
== MATCH_ERROR
)
2922 /* A full IO statement has been matched. Check the constraints. spec_end is
2923 supplied for cases where no locus is supplied. */
2924 m
= check_io_constraints (k
, dt
, io_code
, &spec_end
);
2926 if (m
== MATCH_ERROR
)
2929 new_st
.op
= (k
== M_READ
) ? EXEC_READ
: EXEC_WRITE
;
2931 new_st
.block
= gfc_get_code ();
2932 new_st
.block
->op
= new_st
.op
;
2933 new_st
.block
->next
= io_code
;
2935 terminate_io (io_code
);
2940 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2950 gfc_match_read (void)
2952 return match_io (M_READ
);
2956 gfc_match_write (void)
2958 return match_io (M_WRITE
);
2962 gfc_match_print (void)
2966 m
= match_io (M_PRINT
);
2970 if (gfc_pure (NULL
))
2972 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2980 /* Free a gfc_inquire structure. */
2983 gfc_free_inquire (gfc_inquire
* inquire
)
2986 if (inquire
== NULL
)
2989 gfc_free_expr (inquire
->unit
);
2990 gfc_free_expr (inquire
->file
);
2991 gfc_free_expr (inquire
->iomsg
);
2992 gfc_free_expr (inquire
->iostat
);
2993 gfc_free_expr (inquire
->exist
);
2994 gfc_free_expr (inquire
->opened
);
2995 gfc_free_expr (inquire
->number
);
2996 gfc_free_expr (inquire
->named
);
2997 gfc_free_expr (inquire
->name
);
2998 gfc_free_expr (inquire
->access
);
2999 gfc_free_expr (inquire
->sequential
);
3000 gfc_free_expr (inquire
->direct
);
3001 gfc_free_expr (inquire
->form
);
3002 gfc_free_expr (inquire
->formatted
);
3003 gfc_free_expr (inquire
->unformatted
);
3004 gfc_free_expr (inquire
->recl
);
3005 gfc_free_expr (inquire
->nextrec
);
3006 gfc_free_expr (inquire
->blank
);
3007 gfc_free_expr (inquire
->position
);
3008 gfc_free_expr (inquire
->action
);
3009 gfc_free_expr (inquire
->read
);
3010 gfc_free_expr (inquire
->write
);
3011 gfc_free_expr (inquire
->readwrite
);
3012 gfc_free_expr (inquire
->delim
);
3013 gfc_free_expr (inquire
->pad
);
3014 gfc_free_expr (inquire
->iolength
);
3015 gfc_free_expr (inquire
->convert
);
3016 gfc_free_expr (inquire
->strm_pos
);
3022 /* Match an element of an INQUIRE statement. */
3024 #define RETM if (m != MATCH_NO) return m;
3027 match_inquire_element (gfc_inquire
* inquire
)
3031 m
= match_etag (&tag_unit
, &inquire
->unit
);
3032 RETM m
= match_etag (&tag_file
, &inquire
->file
);
3033 RETM m
= match_ltag (&tag_err
, &inquire
->err
);
3034 RETM m
= match_out_tag (&tag_iomsg
, &inquire
->iomsg
);
3035 RETM m
= match_out_tag (&tag_iostat
, &inquire
->iostat
);
3036 RETM m
= match_vtag (&tag_exist
, &inquire
->exist
);
3037 RETM m
= match_vtag (&tag_opened
, &inquire
->opened
);
3038 RETM m
= match_vtag (&tag_named
, &inquire
->named
);
3039 RETM m
= match_vtag (&tag_name
, &inquire
->name
);
3040 RETM m
= match_out_tag (&tag_number
, &inquire
->number
);
3041 RETM m
= match_vtag (&tag_s_access
, &inquire
->access
);
3042 RETM m
= match_vtag (&tag_sequential
, &inquire
->sequential
);
3043 RETM m
= match_vtag (&tag_direct
, &inquire
->direct
);
3044 RETM m
= match_vtag (&tag_s_form
, &inquire
->form
);
3045 RETM m
= match_vtag (&tag_formatted
, &inquire
->formatted
);
3046 RETM m
= match_vtag (&tag_unformatted
, &inquire
->unformatted
);
3047 RETM m
= match_out_tag (&tag_s_recl
, &inquire
->recl
);
3048 RETM m
= match_out_tag (&tag_nextrec
, &inquire
->nextrec
);
3049 RETM m
= match_vtag (&tag_s_blank
, &inquire
->blank
);
3050 RETM m
= match_vtag (&tag_s_position
, &inquire
->position
);
3051 RETM m
= match_vtag (&tag_s_action
, &inquire
->action
);
3052 RETM m
= match_vtag (&tag_read
, &inquire
->read
);
3053 RETM m
= match_vtag (&tag_write
, &inquire
->write
);
3054 RETM m
= match_vtag (&tag_readwrite
, &inquire
->readwrite
);
3055 RETM m
= match_vtag (&tag_s_delim
, &inquire
->delim
);
3056 RETM m
= match_vtag (&tag_s_pad
, &inquire
->pad
);
3057 RETM m
= match_vtag (&tag_iolength
, &inquire
->iolength
);
3058 RETM m
= match_vtag (&tag_convert
, &inquire
->convert
);
3059 RETM m
= match_out_tag (&tag_strm_out
, &inquire
->strm_pos
);
3060 RETM
return MATCH_NO
;
3067 gfc_match_inquire (void)
3069 gfc_inquire
*inquire
;
3074 m
= gfc_match_char ('(');
3078 inquire
= gfc_getmem (sizeof (gfc_inquire
));
3080 loc
= gfc_current_locus
;
3082 m
= match_inquire_element (inquire
);
3083 if (m
== MATCH_ERROR
)
3087 m
= gfc_match_expr (&inquire
->unit
);
3088 if (m
== MATCH_ERROR
)
3094 /* See if we have the IOLENGTH form of the inquire statement. */
3095 if (inquire
->iolength
!= NULL
)
3097 if (gfc_match_char (')') != MATCH_YES
)
3100 m
= match_io_list (M_INQUIRE
, &code
);
3101 if (m
== MATCH_ERROR
)
3106 new_st
.op
= EXEC_IOLENGTH
;
3107 new_st
.expr
= inquire
->iolength
;
3108 new_st
.ext
.inquire
= inquire
;
3110 if (gfc_pure (NULL
))
3112 gfc_free_statements (code
);
3113 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3117 new_st
.block
= gfc_get_code ();
3118 new_st
.block
->op
= EXEC_IOLENGTH
;
3119 terminate_io (code
);
3120 new_st
.block
->next
= code
;
3124 /* At this point, we have the non-IOLENGTH inquire statement. */
3127 if (gfc_match_char (')') == MATCH_YES
)
3129 if (gfc_match_char (',') != MATCH_YES
)
3132 m
= match_inquire_element (inquire
);
3133 if (m
== MATCH_ERROR
)
3138 if (inquire
->iolength
!= NULL
)
3140 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3145 if (gfc_match_eos () != MATCH_YES
)
3148 if (inquire
->unit
!= NULL
&& inquire
->file
!= NULL
)
3150 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
3151 " UNIT specifiers", &loc
);
3155 if (inquire
->unit
== NULL
&& inquire
->file
== NULL
)
3157 gfc_error ("INQUIRE statement at %L requires either FILE or"
3158 " UNIT specifier", &loc
);
3162 if (gfc_pure (NULL
))
3164 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3168 new_st
.op
= EXEC_INQUIRE
;
3169 new_st
.ext
.inquire
= inquire
;
3173 gfc_syntax_error (ST_INQUIRE
);
3176 gfc_free_inquire (inquire
);
3181 /* Resolve everything in a gfc_inquire structure. */
3184 gfc_resolve_inquire (gfc_inquire
* inquire
)
3187 RESOLVE_TAG (&tag_unit
, inquire
->unit
);
3188 RESOLVE_TAG (&tag_file
, inquire
->file
);
3189 RESOLVE_TAG (&tag_iomsg
, inquire
->iomsg
);
3190 RESOLVE_TAG (&tag_iostat
, inquire
->iostat
);
3191 RESOLVE_TAG (&tag_exist
, inquire
->exist
);
3192 RESOLVE_TAG (&tag_opened
, inquire
->opened
);
3193 RESOLVE_TAG (&tag_number
, inquire
->number
);
3194 RESOLVE_TAG (&tag_named
, inquire
->named
);
3195 RESOLVE_TAG (&tag_name
, inquire
->name
);
3196 RESOLVE_TAG (&tag_s_access
, inquire
->access
);
3197 RESOLVE_TAG (&tag_sequential
, inquire
->sequential
);
3198 RESOLVE_TAG (&tag_direct
, inquire
->direct
);
3199 RESOLVE_TAG (&tag_s_form
, inquire
->form
);
3200 RESOLVE_TAG (&tag_formatted
, inquire
->formatted
);
3201 RESOLVE_TAG (&tag_unformatted
, inquire
->unformatted
);
3202 RESOLVE_TAG (&tag_s_recl
, inquire
->recl
);
3203 RESOLVE_TAG (&tag_nextrec
, inquire
->nextrec
);
3204 RESOLVE_TAG (&tag_s_blank
, inquire
->blank
);
3205 RESOLVE_TAG (&tag_s_position
, inquire
->position
);
3206 RESOLVE_TAG (&tag_s_action
, inquire
->action
);
3207 RESOLVE_TAG (&tag_read
, inquire
->read
);
3208 RESOLVE_TAG (&tag_write
, inquire
->write
);
3209 RESOLVE_TAG (&tag_readwrite
, inquire
->readwrite
);
3210 RESOLVE_TAG (&tag_s_delim
, inquire
->delim
);
3211 RESOLVE_TAG (&tag_s_pad
, inquire
->pad
);
3212 RESOLVE_TAG (&tag_iolength
, inquire
->iolength
);
3213 RESOLVE_TAG (&tag_convert
, inquire
->convert
);
3214 RESOLVE_TAG (&tag_strm_out
, inquire
->strm_pos
);
3216 if (gfc_reference_st_label (inquire
->err
, ST_LABEL_TARGET
) == FAILURE
)