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_F2003
, "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
)
1138 if (tag
== &tag_iolength
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1140 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 95 requires default "
1141 "INTEGER in IOLENGTH tag at %L",
1142 &e
->where
) == FAILURE
)
1151 /* Match a single tag of an OPEN statement. */
1154 match_open_element (gfc_open
* open
)
1158 m
= match_etag (&tag_unit
, &open
->unit
);
1161 m
= match_out_tag (&tag_iomsg
, &open
->iomsg
);
1164 m
= match_out_tag (&tag_iostat
, &open
->iostat
);
1167 m
= match_etag (&tag_file
, &open
->file
);
1170 m
= match_etag (&tag_status
, &open
->status
);
1173 m
= match_etag (&tag_e_access
, &open
->access
);
1176 m
= match_etag (&tag_e_form
, &open
->form
);
1179 m
= match_etag (&tag_e_recl
, &open
->recl
);
1182 m
= match_etag (&tag_e_blank
, &open
->blank
);
1185 m
= match_etag (&tag_e_position
, &open
->position
);
1188 m
= match_etag (&tag_e_action
, &open
->action
);
1191 m
= match_etag (&tag_e_delim
, &open
->delim
);
1194 m
= match_etag (&tag_e_pad
, &open
->pad
);
1197 m
= match_ltag (&tag_err
, &open
->err
);
1200 m
= match_etag (&tag_convert
, &open
->convert
);
1208 /* Free the gfc_open structure and all the expressions it contains. */
1211 gfc_free_open (gfc_open
* open
)
1217 gfc_free_expr (open
->unit
);
1218 gfc_free_expr (open
->iomsg
);
1219 gfc_free_expr (open
->iostat
);
1220 gfc_free_expr (open
->file
);
1221 gfc_free_expr (open
->status
);
1222 gfc_free_expr (open
->access
);
1223 gfc_free_expr (open
->form
);
1224 gfc_free_expr (open
->recl
);
1225 gfc_free_expr (open
->blank
);
1226 gfc_free_expr (open
->position
);
1227 gfc_free_expr (open
->action
);
1228 gfc_free_expr (open
->delim
);
1229 gfc_free_expr (open
->pad
);
1230 gfc_free_expr (open
->convert
);
1236 /* Resolve everything in a gfc_open structure. */
1239 gfc_resolve_open (gfc_open
* open
)
1242 RESOLVE_TAG (&tag_unit
, open
->unit
);
1243 RESOLVE_TAG (&tag_iomsg
, open
->iomsg
);
1244 RESOLVE_TAG (&tag_iostat
, open
->iostat
);
1245 RESOLVE_TAG (&tag_file
, open
->file
);
1246 RESOLVE_TAG (&tag_status
, open
->status
);
1247 RESOLVE_TAG (&tag_e_access
, open
->access
);
1248 RESOLVE_TAG (&tag_e_form
, open
->form
);
1249 RESOLVE_TAG (&tag_e_recl
, open
->recl
);
1251 RESOLVE_TAG (&tag_e_blank
, open
->blank
);
1252 RESOLVE_TAG (&tag_e_position
, open
->position
);
1253 RESOLVE_TAG (&tag_e_action
, open
->action
);
1254 RESOLVE_TAG (&tag_e_delim
, open
->delim
);
1255 RESOLVE_TAG (&tag_e_pad
, open
->pad
);
1256 RESOLVE_TAG (&tag_convert
, open
->convert
);
1258 if (gfc_reference_st_label (open
->err
, ST_LABEL_TARGET
) == FAILURE
)
1266 /* Check if a given value for a SPECIFIER is either in the list of values
1267 allowed in F95 or F2003, issuing an error message and returning a zero
1268 value if it is not allowed. */
1270 compare_to_allowed_values (const char * specifier
, const char * allowed
[],
1271 const char * allowed_f2003
[],
1272 const char * allowed_gnu
[], char * value
,
1273 const char * statement
, bool warn
)
1278 len
= strlen(value
);
1281 for (len
--; len
> 0; len
--)
1282 if (value
[len
] != ' ')
1287 for (i
= 0; allowed
[i
]; i
++)
1288 if (len
== strlen(allowed
[i
])
1289 && strncasecmp (value
, allowed
[i
], strlen(allowed
[i
])) == 0)
1292 for (i
= 0; allowed_f2003
&& allowed_f2003
[i
]; i
++)
1293 if (len
== strlen(allowed_f2003
[i
])
1294 && strncasecmp (value
, allowed_f2003
[i
], strlen(allowed_f2003
[i
])) == 0)
1296 notification n
= gfc_notification_std (GFC_STD_F2003
);
1298 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1300 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1301 "has value '%s'", specifier
, statement
,
1308 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: %s specifier in "
1309 "%s statement at %C has value '%s'", specifier
,
1310 statement
, allowed_f2003
[i
]);
1318 for (i
= 0; allowed_gnu
&& allowed_gnu
[i
]; i
++)
1319 if (len
== strlen(allowed_gnu
[i
])
1320 && strncasecmp (value
, allowed_gnu
[i
], strlen(allowed_gnu
[i
])) == 0)
1322 notification n
= gfc_notification_std (GFC_STD_GNU
);
1324 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1326 gfc_warning ("Extension: %s specifier in %s statement at %C "
1327 "has value '%s'", specifier
, statement
,
1334 gfc_notify_std (GFC_STD_GNU
, "Extension: %s specifier in "
1335 "%s statement at %C has value '%s'", specifier
,
1336 statement
, allowed_gnu
[i
]);
1346 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1347 specifier
, statement
, value
);
1352 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1353 specifier
, statement
, value
);
1358 /* Match an OPEN statement. */
1361 gfc_match_open (void)
1367 m
= gfc_match_char ('(');
1371 open
= gfc_getmem (sizeof (gfc_open
));
1373 m
= match_open_element (open
);
1375 if (m
== MATCH_ERROR
)
1379 m
= gfc_match_expr (&open
->unit
);
1382 if (m
== MATCH_ERROR
)
1388 if (gfc_match_char (')') == MATCH_YES
)
1390 if (gfc_match_char (',') != MATCH_YES
)
1393 m
= match_open_element (open
);
1394 if (m
== MATCH_ERROR
)
1400 if (gfc_match_eos () == MATCH_NO
)
1403 if (gfc_pure (NULL
))
1405 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1409 warn
= (open
->err
|| open
->iostat
) ? true : false;
1410 /* Checks on the ACCESS specifier. */
1411 if (open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
)
1413 static const char * access_f95
[] = { "SEQUENTIAL", "DIRECT", NULL
};
1414 static const char * access_f2003
[] = { "STREAM", NULL
};
1415 static const char * access_gnu
[] = { "APPEND", NULL
};
1417 if (!compare_to_allowed_values ("ACCESS", access_f95
, access_f2003
,
1419 open
->access
->value
.character
.string
,
1424 /* Checks on the ACTION specifier. */
1425 if (open
->action
&& open
->action
->expr_type
== EXPR_CONSTANT
)
1427 static const char * action
[] = { "READ", "WRITE", "READWRITE", NULL
};
1429 if (!compare_to_allowed_values ("ACTION", action
, NULL
, NULL
,
1430 open
->action
->value
.character
.string
,
1435 /* Checks on the ASYNCHRONOUS specifier. */
1436 /* TODO: code is ready, just needs uncommenting when async I/O support
1438 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1440 static const char * asynchronous[] = { "YES", "NO", NULL };
1442 if (!compare_to_allowed_values
1443 ("action", asynchronous, NULL, NULL,
1444 open->asynchronous->value.character.string, "OPEN", warn))
1448 /* Checks on the BLANK specifier. */
1449 if (open
->blank
&& open
->blank
->expr_type
== EXPR_CONSTANT
)
1451 static const char * blank
[] = { "ZERO", "NULL", NULL
};
1453 if (!compare_to_allowed_values ("BLANK", blank
, NULL
, NULL
,
1454 open
->blank
->value
.character
.string
,
1459 /* Checks on the DECIMAL specifier. */
1460 /* TODO: uncomment this code when DECIMAL support is added
1461 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1463 static const char * decimal[] = { "COMMA", "POINT", NULL };
1465 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1466 open->decimal->value.character.string,
1471 /* Checks on the DELIM specifier. */
1472 if (open
->delim
&& open
->delim
->expr_type
== EXPR_CONSTANT
)
1474 static const char * delim
[] = { "APOSTROPHE", "QUOTE", "NONE", NULL
};
1476 if (!compare_to_allowed_values ("DELIM", delim
, NULL
, NULL
,
1477 open
->delim
->value
.character
.string
,
1482 /* Checks on the ENCODING specifier. */
1483 /* TODO: uncomment this code when ENCODING support is added
1484 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1486 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1488 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1489 open->encoding->value.character.string,
1494 /* Checks on the FORM specifier. */
1495 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
)
1497 static const char * form
[] = { "FORMATTED", "UNFORMATTED", NULL
};
1499 if (!compare_to_allowed_values ("FORM", form
, NULL
, NULL
,
1500 open
->form
->value
.character
.string
,
1505 /* Checks on the PAD specifier. */
1506 if (open
->pad
&& open
->pad
->expr_type
== EXPR_CONSTANT
)
1508 static const char * pad
[] = { "YES", "NO", NULL
};
1510 if (!compare_to_allowed_values ("PAD", pad
, NULL
, NULL
,
1511 open
->pad
->value
.character
.string
,
1516 /* Checks on the POSITION specifier. */
1517 if (open
->position
&& open
->position
->expr_type
== EXPR_CONSTANT
)
1519 static const char * position
[] = { "ASIS", "REWIND", "APPEND", NULL
};
1521 if (!compare_to_allowed_values ("POSITION", position
, NULL
, NULL
,
1522 open
->position
->value
.character
.string
,
1527 /* Checks on the ROUND specifier. */
1528 /* TODO: uncomment this code when ROUND support is added
1529 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1531 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1532 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1534 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1535 open->round->value.character.string,
1540 /* Checks on the SIGN specifier. */
1541 /* TODO: uncomment this code when SIGN support is added
1542 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1544 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1547 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1548 open->sign->value.character.string,
1553 #define warn_or_error(...) \
1556 gfc_warning (__VA_ARGS__); \
1559 gfc_error (__VA_ARGS__); \
1564 /* Checks on the RECL specifier. */
1565 if (open
->recl
&& open
->recl
->expr_type
== EXPR_CONSTANT
1566 && open
->recl
->ts
.type
== BT_INTEGER
1567 && mpz_sgn (open
->recl
->value
.integer
) != 1)
1569 warn_or_error ("RECL in OPEN statement at %C must be positive");
1572 /* Checks on the STATUS specifier. */
1573 if (open
->status
&& open
->status
->expr_type
== EXPR_CONSTANT
)
1575 static const char * status
[] = { "OLD", "NEW", "SCRATCH",
1576 "REPLACE", "UNKNOWN", NULL
};
1578 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1579 open
->status
->value
.character
.string
,
1583 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1584 the FILE= specifier shall appear. */
1585 if (open
->file
== NULL
&&
1586 (strncasecmp (open
->status
->value
.character
.string
, "replace", 7) == 0
1587 || strncasecmp (open
->status
->value
.character
.string
, "new", 3) == 0))
1589 warn_or_error ("The STATUS specified in OPEN statement at %C is '%s' "
1590 "and no FILE specifier is present",
1591 open
->status
->value
.character
.string
);
1594 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1595 the FILE= specifier shall not appear. */
1596 if (strncasecmp (open
->status
->value
.character
.string
, "scratch", 7) == 0
1599 warn_or_error ("The STATUS specified in OPEN statement at %C cannot "
1600 "have the value SCRATCH if a FILE specifier "
1605 /* Things that are not allowed for unformatted I/O. */
1606 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
1608 /* TODO uncomment this code when F2003 support is finished */
1609 /* || open->decimal || open->encoding || open->round
1611 || open
->pad
|| open
->blank
)
1612 && strncasecmp (open
->form
->value
.character
.string
,
1613 "unformatted", 11) == 0)
1615 const char * spec
= (open
->delim
? "DELIM " : (open
->pad
? "PAD " :
1616 open
->blank
? "BLANK " : ""));
1618 warn_or_error ("%sspecifier at %C not allowed in OPEN statement for "
1619 "unformatted I/O", spec
);
1622 if (open
->recl
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1623 && strncasecmp (open
->access
->value
.character
.string
, "stream", 6) == 0)
1625 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1629 if (open
->position
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1630 && !(strncasecmp (open
->access
->value
.character
.string
,
1631 "sequential", 10) == 0
1632 || strncasecmp (open
->access
->value
.character
.string
,
1634 || strncasecmp (open
->access
->value
.character
.string
,
1637 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1638 "for stream or sequential ACCESS");
1641 #undef warn_or_error
1643 new_st
.op
= EXEC_OPEN
;
1644 new_st
.ext
.open
= open
;
1648 gfc_syntax_error (ST_OPEN
);
1651 gfc_free_open (open
);
1656 /* Free a gfc_close structure an all its expressions. */
1659 gfc_free_close (gfc_close
* close
)
1665 gfc_free_expr (close
->unit
);
1666 gfc_free_expr (close
->iomsg
);
1667 gfc_free_expr (close
->iostat
);
1668 gfc_free_expr (close
->status
);
1674 /* Match elements of a CLOSE statement. */
1677 match_close_element (gfc_close
* close
)
1681 m
= match_etag (&tag_unit
, &close
->unit
);
1684 m
= match_etag (&tag_status
, &close
->status
);
1687 m
= match_out_tag (&tag_iomsg
, &close
->iomsg
);
1690 m
= match_out_tag (&tag_iostat
, &close
->iostat
);
1693 m
= match_ltag (&tag_err
, &close
->err
);
1701 /* Match a CLOSE statement. */
1704 gfc_match_close (void)
1710 m
= gfc_match_char ('(');
1714 close
= gfc_getmem (sizeof (gfc_close
));
1716 m
= match_close_element (close
);
1718 if (m
== MATCH_ERROR
)
1722 m
= gfc_match_expr (&close
->unit
);
1725 if (m
== MATCH_ERROR
)
1731 if (gfc_match_char (')') == MATCH_YES
)
1733 if (gfc_match_char (',') != MATCH_YES
)
1736 m
= match_close_element (close
);
1737 if (m
== MATCH_ERROR
)
1743 if (gfc_match_eos () == MATCH_NO
)
1746 if (gfc_pure (NULL
))
1748 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1752 warn
= (close
->iostat
|| close
->err
) ? true : false;
1754 /* Checks on the STATUS specifier. */
1755 if (close
->status
&& close
->status
->expr_type
== EXPR_CONSTANT
)
1757 static const char * status
[] = { "KEEP", "DELETE", NULL
};
1759 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1760 close
->status
->value
.character
.string
,
1765 new_st
.op
= EXEC_CLOSE
;
1766 new_st
.ext
.close
= close
;
1770 gfc_syntax_error (ST_CLOSE
);
1773 gfc_free_close (close
);
1778 /* Resolve everything in a gfc_close structure. */
1781 gfc_resolve_close (gfc_close
* close
)
1784 RESOLVE_TAG (&tag_unit
, close
->unit
);
1785 RESOLVE_TAG (&tag_iomsg
, close
->iomsg
);
1786 RESOLVE_TAG (&tag_iostat
, close
->iostat
);
1787 RESOLVE_TAG (&tag_status
, close
->status
);
1789 if (gfc_reference_st_label (close
->err
, ST_LABEL_TARGET
) == FAILURE
)
1796 /* Free a gfc_filepos structure. */
1799 gfc_free_filepos (gfc_filepos
* fp
)
1802 gfc_free_expr (fp
->unit
);
1803 gfc_free_expr (fp
->iomsg
);
1804 gfc_free_expr (fp
->iostat
);
1809 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1812 match_file_element (gfc_filepos
* fp
)
1816 m
= match_etag (&tag_unit
, &fp
->unit
);
1819 m
= match_out_tag (&tag_iomsg
, &fp
->iomsg
);
1822 m
= match_out_tag (&tag_iostat
, &fp
->iostat
);
1825 m
= match_ltag (&tag_err
, &fp
->err
);
1833 /* Match the second half of the file-positioning statements, REWIND,
1834 BACKSPACE, ENDFILE, or the FLUSH statement. */
1837 match_filepos (gfc_statement st
, gfc_exec_op op
)
1842 fp
= gfc_getmem (sizeof (gfc_filepos
));
1844 if (gfc_match_char ('(') == MATCH_NO
)
1846 m
= gfc_match_expr (&fp
->unit
);
1847 if (m
== MATCH_ERROR
)
1855 m
= match_file_element (fp
);
1856 if (m
== MATCH_ERROR
)
1860 m
= gfc_match_expr (&fp
->unit
);
1861 if (m
== MATCH_ERROR
)
1869 if (gfc_match_char (')') == MATCH_YES
)
1871 if (gfc_match_char (',') != MATCH_YES
)
1874 m
= match_file_element (fp
);
1875 if (m
== MATCH_ERROR
)
1882 if (gfc_match_eos () != MATCH_YES
)
1885 if (gfc_pure (NULL
))
1887 gfc_error ("%s statement not allowed in PURE procedure at %C",
1888 gfc_ascii_statement (st
));
1894 new_st
.ext
.filepos
= fp
;
1898 gfc_syntax_error (st
);
1901 gfc_free_filepos (fp
);
1907 gfc_resolve_filepos (gfc_filepos
* fp
)
1910 RESOLVE_TAG (&tag_unit
, fp
->unit
);
1911 RESOLVE_TAG (&tag_iostat
, fp
->iostat
);
1912 RESOLVE_TAG (&tag_iomsg
, fp
->iomsg
);
1913 if (gfc_reference_st_label (fp
->err
, ST_LABEL_TARGET
) == FAILURE
)
1920 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1921 and the FLUSH statement. */
1924 gfc_match_endfile (void)
1927 return match_filepos (ST_END_FILE
, EXEC_ENDFILE
);
1931 gfc_match_backspace (void)
1934 return match_filepos (ST_BACKSPACE
, EXEC_BACKSPACE
);
1938 gfc_match_rewind (void)
1941 return match_filepos (ST_REWIND
, EXEC_REWIND
);
1945 gfc_match_flush (void)
1947 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: FLUSH statement at %C") == FAILURE
)
1950 return match_filepos (ST_FLUSH
, EXEC_FLUSH
);
1953 /******************** Data Transfer Statements *********************/
1956 { M_READ
, M_WRITE
, M_PRINT
, M_INQUIRE
}
1960 /* Return a default unit number. */
1963 default_unit (io_kind k
)
1972 return gfc_int_expr (unit
);
1976 /* Match a unit specification for a data transfer statement. */
1979 match_dt_unit (io_kind k
, gfc_dt
* dt
)
1983 if (gfc_match_char ('*') == MATCH_YES
)
1985 if (dt
->io_unit
!= NULL
)
1988 dt
->io_unit
= default_unit (k
);
1992 if (gfc_match_expr (&e
) == MATCH_YES
)
1994 if (dt
->io_unit
!= NULL
)
2007 gfc_error ("Duplicate UNIT specification at %C");
2012 /* Match a format specification. */
2015 match_dt_format (gfc_dt
* dt
)
2019 gfc_st_label
*label
;
2021 where
= gfc_current_locus
;
2023 if (gfc_match_char ('*') == MATCH_YES
)
2025 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2028 dt
->format_label
= &format_asterisk
;
2032 if (gfc_match_st_label (&label
) == MATCH_YES
)
2034 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2036 gfc_free_st_label (label
);
2040 if (gfc_reference_st_label (label
, ST_LABEL_FORMAT
) == FAILURE
)
2043 dt
->format_label
= label
;
2047 if (gfc_match_expr (&e
) == MATCH_YES
)
2049 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2054 dt
->format_expr
= e
;
2058 gfc_current_locus
= where
; /* The only case where we have to restore */
2063 gfc_error ("Duplicate format specification at %C");
2068 /* Traverse a namelist that is part of a READ statement to make sure
2069 that none of the variables in the namelist are INTENT(IN). Returns
2070 nonzero if we find such a variable. */
2073 check_namelist (gfc_symbol
* sym
)
2077 for (p
= sym
->namelist
; p
; p
= p
->next
)
2078 if (p
->sym
->attr
.intent
== INTENT_IN
)
2080 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2081 p
->sym
->name
, sym
->name
);
2089 /* Match a single data transfer element. */
2092 match_dt_element (io_kind k
, gfc_dt
* dt
)
2094 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2098 if (gfc_match (" unit =") == MATCH_YES
)
2100 m
= match_dt_unit (k
, dt
);
2105 if (gfc_match (" fmt =") == MATCH_YES
)
2107 m
= match_dt_format (dt
);
2112 if (gfc_match (" nml = %n", name
) == MATCH_YES
)
2114 if (dt
->namelist
!= NULL
)
2116 gfc_error ("Duplicate NML specification at %C");
2120 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2123 if (sym
== NULL
|| sym
->attr
.flavor
!= FL_NAMELIST
)
2125 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2126 sym
!= NULL
? sym
->name
: name
);
2131 if (k
== M_READ
&& check_namelist (sym
))
2137 m
= match_etag (&tag_rec
, &dt
->rec
);
2140 m
= match_etag (&tag_spos
, &dt
->rec
);
2143 m
= match_out_tag (&tag_iomsg
, &dt
->iomsg
);
2146 m
= match_out_tag (&tag_iostat
, &dt
->iostat
);
2149 m
= match_ltag (&tag_err
, &dt
->err
);
2151 dt
->err_where
= gfc_current_locus
;
2154 m
= match_etag (&tag_advance
, &dt
->advance
);
2157 m
= match_out_tag (&tag_size
, &dt
->size
);
2161 m
= match_ltag (&tag_end
, &dt
->end
);
2166 gfc_error ("END tag at %C not allowed in output statement");
2169 dt
->end_where
= gfc_current_locus
;
2174 m
= match_ltag (&tag_eor
, &dt
->eor
);
2176 dt
->eor_where
= gfc_current_locus
;
2184 /* Free a data transfer structure and everything below it. */
2187 gfc_free_dt (gfc_dt
* dt
)
2193 gfc_free_expr (dt
->io_unit
);
2194 gfc_free_expr (dt
->format_expr
);
2195 gfc_free_expr (dt
->rec
);
2196 gfc_free_expr (dt
->advance
);
2197 gfc_free_expr (dt
->iomsg
);
2198 gfc_free_expr (dt
->iostat
);
2199 gfc_free_expr (dt
->size
);
2205 /* Resolve everything in a gfc_dt structure. */
2208 gfc_resolve_dt (gfc_dt
* dt
)
2212 RESOLVE_TAG (&tag_format
, dt
->format_expr
);
2213 RESOLVE_TAG (&tag_rec
, dt
->rec
);
2214 RESOLVE_TAG (&tag_spos
, dt
->rec
);
2215 RESOLVE_TAG (&tag_advance
, dt
->advance
);
2216 RESOLVE_TAG (&tag_iomsg
, dt
->iomsg
);
2217 RESOLVE_TAG (&tag_iostat
, dt
->iostat
);
2218 RESOLVE_TAG (&tag_size
, dt
->size
);
2221 if (gfc_resolve_expr (e
) == SUCCESS
2222 && (e
->ts
.type
!= BT_INTEGER
2223 && (e
->ts
.type
!= BT_CHARACTER
2224 || e
->expr_type
!= EXPR_VARIABLE
)))
2227 ("UNIT specification at %L must be an INTEGER expression or a "
2228 "CHARACTER variable", &e
->where
);
2232 if (e
->ts
.type
== BT_CHARACTER
)
2234 if (gfc_has_vector_index (e
))
2236 gfc_error ("Internal unit with vector subscript at %L",
2242 if (e
->rank
&& e
->ts
.type
!= BT_CHARACTER
)
2244 gfc_error ("External IO UNIT cannot be an array at %L", &e
->where
);
2250 if (gfc_reference_st_label (dt
->err
, ST_LABEL_TARGET
) == FAILURE
)
2252 if (dt
->err
->defined
== ST_LABEL_UNKNOWN
)
2254 gfc_error ("ERR tag label %d at %L not defined",
2255 dt
->err
->value
, &dt
->err_where
);
2262 if (gfc_reference_st_label (dt
->end
, ST_LABEL_TARGET
) == FAILURE
)
2264 if (dt
->end
->defined
== ST_LABEL_UNKNOWN
)
2266 gfc_error ("END tag label %d at %L not defined",
2267 dt
->end
->value
, &dt
->end_where
);
2274 if (gfc_reference_st_label (dt
->eor
, ST_LABEL_TARGET
) == FAILURE
)
2276 if (dt
->eor
->defined
== ST_LABEL_UNKNOWN
)
2278 gfc_error ("EOR tag label %d at %L not defined",
2279 dt
->eor
->value
, &dt
->eor_where
);
2284 /* Check the format label actually exists. */
2285 if (dt
->format_label
&& dt
->format_label
!= &format_asterisk
2286 && dt
->format_label
->defined
== ST_LABEL_UNKNOWN
)
2288 gfc_error ("FORMAT label %d at %L not defined", dt
->format_label
->value
,
2289 &dt
->format_label
->where
);
2296 /* Given an io_kind, return its name. */
2299 io_kind_name (io_kind k
)
2318 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2325 /* Match an IO iteration statement of the form:
2327 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2329 which is equivalent to a single IO element. This function is
2330 mutually recursive with match_io_element(). */
2332 static match
match_io_element (io_kind k
, gfc_code
**);
2335 match_io_iterator (io_kind k
, gfc_code
** result
)
2337 gfc_code
*head
, *tail
, *new;
2345 old_loc
= gfc_current_locus
;
2347 if (gfc_match_char ('(') != MATCH_YES
)
2350 m
= match_io_element (k
, &head
);
2353 if (m
!= MATCH_YES
|| gfc_match_char (',') != MATCH_YES
)
2359 /* Can't be anything but an IO iterator. Build a list. */
2360 iter
= gfc_get_iterator ();
2364 m
= gfc_match_iterator (iter
, 0);
2365 if (m
== MATCH_ERROR
)
2369 gfc_check_do_variable (iter
->var
->symtree
);
2373 m
= match_io_element (k
, &new);
2374 if (m
== MATCH_ERROR
)
2383 tail
= gfc_append_code (tail
, new);
2385 if (gfc_match_char (',') != MATCH_YES
)
2394 if (gfc_match_char (')') != MATCH_YES
)
2397 new = gfc_get_code ();
2399 new->ext
.iterator
= iter
;
2401 new->block
= gfc_get_code ();
2402 new->block
->op
= EXEC_DO
;
2403 new->block
->next
= head
;
2409 gfc_error ("Syntax error in I/O iterator at %C");
2413 gfc_free_iterator (iter
, 1);
2414 gfc_free_statements (head
);
2415 gfc_current_locus
= old_loc
;
2420 /* Match a single element of an IO list, which is either a single
2421 expression or an IO Iterator. */
2424 match_io_element (io_kind k
, gfc_code
** cpp
)
2432 m
= match_io_iterator (k
, cpp
);
2438 m
= gfc_match_variable (&expr
, 0);
2440 gfc_error ("Expected variable in READ statement at %C");
2444 m
= gfc_match_expr (&expr
);
2446 gfc_error ("Expected expression in %s statement at %C",
2454 if (expr
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
2457 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2458 expr
->symtree
->n
.sym
->name
);
2463 && gfc_impure_variable (expr
->symtree
->n
.sym
)
2464 && current_dt
->io_unit
->ts
.type
== BT_CHARACTER
)
2466 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2467 expr
->symtree
->n
.sym
->name
);
2471 if (gfc_check_do_variable (expr
->symtree
))
2477 if (current_dt
->io_unit
->ts
.type
== BT_CHARACTER
2479 && current_dt
->io_unit
->expr_type
== EXPR_VARIABLE
2480 && gfc_impure_variable (current_dt
->io_unit
->symtree
->n
.sym
))
2483 ("Cannot write to internal file unit '%s' at %C inside a "
2484 "PURE procedure", current_dt
->io_unit
->symtree
->n
.sym
->name
);
2496 gfc_free_expr (expr
);
2500 cp
= gfc_get_code ();
2501 cp
->op
= EXEC_TRANSFER
;
2509 /* Match an I/O list, building gfc_code structures as we go. */
2512 match_io_list (io_kind k
, gfc_code
** head_p
)
2514 gfc_code
*head
, *tail
, *new;
2517 *head_p
= head
= tail
= NULL
;
2518 if (gfc_match_eos () == MATCH_YES
)
2523 m
= match_io_element (k
, &new);
2524 if (m
== MATCH_ERROR
)
2529 tail
= gfc_append_code (tail
, new);
2533 if (gfc_match_eos () == MATCH_YES
)
2535 if (gfc_match_char (',') != MATCH_YES
)
2543 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2546 gfc_free_statements (head
);
2551 /* Attach the data transfer end node. */
2554 terminate_io (gfc_code
* io_code
)
2558 if (io_code
== NULL
)
2559 io_code
= new_st
.block
;
2561 c
= gfc_get_code ();
2562 c
->op
= EXEC_DT_END
;
2564 /* Point to structure that is already there */
2565 c
->ext
.dt
= new_st
.ext
.dt
;
2566 gfc_append_code (io_code
, c
);
2570 /* Check the constraints for a data transfer statement. The majority of the
2571 constraints appearing in 9.4 of the standard appear here. Some are handled
2572 in resolve_tag and others in gfc_resolve_dt. */
2575 check_io_constraints (io_kind k
, gfc_dt
*dt
, gfc_code
* io_code
, locus
* spec_end
)
2577 #define io_constraint(condition,msg,arg)\
2580 gfc_error(msg,arg);\
2586 gfc_symbol
* sym
= NULL
;
2591 if (expr
&& expr
->expr_type
== EXPR_VARIABLE
2592 && expr
->ts
.type
== BT_CHARACTER
)
2594 sym
= expr
->symtree
->n
.sym
;
2596 io_constraint (k
== M_WRITE
&& sym
->attr
.intent
== INTENT_IN
,
2597 "Internal file at %L must not be INTENT(IN)",
2600 io_constraint (gfc_has_vector_index (dt
->io_unit
),
2601 "Internal file incompatible with vector subscript at %L",
2604 io_constraint (dt
->rec
!= NULL
,
2605 "REC tag at %L is incompatible with internal file",
2608 if (dt
->namelist
!= NULL
)
2610 if (gfc_notify_std(GFC_STD_F2003
,
2611 "Fortran 2003: Internal file at %L with namelist",
2612 &expr
->where
) == FAILURE
)
2616 io_constraint (dt
->advance
!= NULL
,
2617 "ADVANCE tag at %L is incompatible with internal file",
2618 &dt
->advance
->where
);
2621 if (expr
&& expr
->ts
.type
!= BT_CHARACTER
)
2624 io_constraint (gfc_pure (NULL
)
2625 && (k
== M_READ
|| k
== M_WRITE
),
2626 "IO UNIT in %s statement at %C must be "
2627 "an internal file in a PURE procedure",
2634 io_constraint (dt
->end
,
2635 "END tag not allowed with output at %L",
2638 io_constraint (dt
->eor
,
2639 "EOR tag not allowed with output at %L",
2642 io_constraint (k
!= M_READ
&& dt
->size
,
2643 "SIZE=specifier not allowed with output at %L",
2648 io_constraint (dt
->size
&& dt
->advance
== NULL
,
2649 "SIZE tag at %L requires an ADVANCE tag",
2652 io_constraint (dt
->eor
&& dt
->advance
== NULL
,
2653 "EOR tag at %L requires an ADVANCE tag",
2661 io_constraint (io_code
&& dt
->namelist
,
2662 "NAMELIST cannot be followed by IO-list at %L",
2665 io_constraint (dt
->format_expr
,
2666 "IO spec-list cannot contain both NAMELIST group name "
2667 "and format specification at %L.",
2668 &dt
->format_expr
->where
);
2670 io_constraint (dt
->format_label
,
2671 "IO spec-list cannot contain both NAMELIST group name "
2672 "and format label at %L", spec_end
);
2674 io_constraint (dt
->rec
,
2675 "NAMELIST IO is not allowed with a REC=specifier "
2676 "at %L.", &dt
->rec
->where
);
2678 io_constraint (dt
->advance
,
2679 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2680 "at %L.", &dt
->advance
->where
);
2685 io_constraint (dt
->end
,
2686 "An END tag is not allowed with a "
2687 "REC=specifier at %L.", &dt
->end_where
);
2690 io_constraint (dt
->format_label
== &format_asterisk
,
2691 "FMT=* is not allowed with a REC=specifier "
2692 "at %L.", spec_end
);
2697 int not_yes
, not_no
;
2700 io_constraint (dt
->format_label
== &format_asterisk
,
2701 "List directed format(*) is not allowed with a "
2702 "ADVANCE=specifier at %L.", &expr
->where
);
2704 io_constraint (dt
->format_expr
== NULL
2705 && dt
->format_label
== NULL
2706 && dt
->namelist
== NULL
,
2707 "the ADVANCE=specifier at %L must appear with an "
2708 "explicit format expression", &expr
->where
);
2710 if (expr
->expr_type
== EXPR_CONSTANT
&& expr
->ts
.type
== BT_CHARACTER
)
2712 const char * advance
= expr
->value
.character
.string
;
2713 not_no
= strcasecmp (advance
, "no") != 0;
2714 not_yes
= strcasecmp (advance
, "yes") != 0;
2722 io_constraint (not_no
&& not_yes
,
2723 "ADVANCE=specifier at %L must have value = "
2724 "YES or NO.", &expr
->where
);
2726 io_constraint (dt
->size
&& not_no
&& k
== M_READ
,
2727 "SIZE tag at %L requires an ADVANCE = 'NO'",
2730 io_constraint (dt
->eor
&& not_no
&& k
== M_READ
,
2731 "EOR tag at %L requires an ADVANCE = 'NO'",
2735 expr
= dt
->format_expr
;
2736 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
)
2737 check_format_string (expr
);
2741 #undef io_constraint
2743 /* Match a READ, WRITE or PRINT statement. */
2746 match_io (io_kind k
)
2748 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2757 where
= gfc_current_locus
;
2759 current_dt
= dt
= gfc_getmem (sizeof (gfc_dt
));
2760 m
= gfc_match_char ('(');
2763 where
= gfc_current_locus
;
2766 else if (k
== M_PRINT
)
2768 /* Treat the non-standard case of PRINT namelist. */
2769 if ((gfc_current_form
== FORM_FIXED
|| gfc_peek_char () == ' ')
2770 && gfc_match_name (name
) == MATCH_YES
)
2772 gfc_find_symbol (name
, NULL
, 1, &sym
);
2773 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2775 if (gfc_notify_std (GFC_STD_GNU
, "PRINT namelist at "
2776 "%C is an extension") == FAILURE
)
2782 dt
->io_unit
= default_unit (k
);
2787 gfc_current_locus
= where
;
2791 if (gfc_current_form
== FORM_FREE
)
2793 c
= gfc_peek_char();
2794 if (c
!= ' ' && c
!= '*' && c
!= '\'' && c
!= '"')
2801 m
= match_dt_format (dt
);
2802 if (m
== MATCH_ERROR
)
2808 dt
->io_unit
= default_unit (k
);
2813 /* Before issuing an error for a malformed 'print (1,*)' type of
2814 error, check for a default-char-expr of the form ('(I0)'). */
2816 if (k
== M_PRINT
&& m
== MATCH_YES
)
2818 /* Reset current locus to get the initial '(' in an expression. */
2819 gfc_current_locus
= where
;
2820 dt
->format_expr
= NULL
;
2821 m
= match_dt_format (dt
);
2823 if (m
== MATCH_ERROR
)
2825 if (m
== MATCH_NO
|| dt
->format_expr
== NULL
)
2829 dt
->io_unit
= default_unit (k
);
2834 /* Match a control list */
2835 if (match_dt_element (k
, dt
) == MATCH_YES
)
2837 if (match_dt_unit (k
, dt
) != MATCH_YES
)
2840 if (gfc_match_char (')') == MATCH_YES
)
2842 if (gfc_match_char (',') != MATCH_YES
)
2845 m
= match_dt_element (k
, dt
);
2848 if (m
== MATCH_ERROR
)
2851 m
= match_dt_format (dt
);
2854 if (m
== MATCH_ERROR
)
2857 where
= gfc_current_locus
;
2859 m
= gfc_match_name (name
);
2862 gfc_find_symbol (name
, NULL
, 1, &sym
);
2863 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2866 if (k
== M_READ
&& check_namelist (sym
))
2875 gfc_current_locus
= where
;
2877 goto loop
; /* No matches, try regular elements */
2880 if (gfc_match_char (')') == MATCH_YES
)
2882 if (gfc_match_char (',') != MATCH_YES
)
2888 m
= match_dt_element (k
, dt
);
2891 if (m
== MATCH_ERROR
)
2894 if (gfc_match_char (')') == MATCH_YES
)
2896 if (gfc_match_char (',') != MATCH_YES
)
2902 /* Used in check_io_constraints, where no locus is available. */
2903 spec_end
= gfc_current_locus
;
2905 /* Optional leading comma (non-standard). */
2907 && gfc_match_char (',') == MATCH_YES
2909 && gfc_notify_std (GFC_STD_GNU
, "Extension: Comma before output "
2910 "item list at %C is an extension") == FAILURE
)
2914 if (gfc_match_eos () != MATCH_YES
)
2916 if (comma_flag
&& gfc_match_char (',') != MATCH_YES
)
2918 gfc_error ("Expected comma in I/O list at %C");
2923 m
= match_io_list (k
, &io_code
);
2924 if (m
== MATCH_ERROR
)
2930 /* A full IO statement has been matched. Check the constraints. spec_end is
2931 supplied for cases where no locus is supplied. */
2932 m
= check_io_constraints (k
, dt
, io_code
, &spec_end
);
2934 if (m
== MATCH_ERROR
)
2937 new_st
.op
= (k
== M_READ
) ? EXEC_READ
: EXEC_WRITE
;
2939 new_st
.block
= gfc_get_code ();
2940 new_st
.block
->op
= new_st
.op
;
2941 new_st
.block
->next
= io_code
;
2943 terminate_io (io_code
);
2948 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2958 gfc_match_read (void)
2960 return match_io (M_READ
);
2964 gfc_match_write (void)
2966 return match_io (M_WRITE
);
2970 gfc_match_print (void)
2974 m
= match_io (M_PRINT
);
2978 if (gfc_pure (NULL
))
2980 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2988 /* Free a gfc_inquire structure. */
2991 gfc_free_inquire (gfc_inquire
* inquire
)
2994 if (inquire
== NULL
)
2997 gfc_free_expr (inquire
->unit
);
2998 gfc_free_expr (inquire
->file
);
2999 gfc_free_expr (inquire
->iomsg
);
3000 gfc_free_expr (inquire
->iostat
);
3001 gfc_free_expr (inquire
->exist
);
3002 gfc_free_expr (inquire
->opened
);
3003 gfc_free_expr (inquire
->number
);
3004 gfc_free_expr (inquire
->named
);
3005 gfc_free_expr (inquire
->name
);
3006 gfc_free_expr (inquire
->access
);
3007 gfc_free_expr (inquire
->sequential
);
3008 gfc_free_expr (inquire
->direct
);
3009 gfc_free_expr (inquire
->form
);
3010 gfc_free_expr (inquire
->formatted
);
3011 gfc_free_expr (inquire
->unformatted
);
3012 gfc_free_expr (inquire
->recl
);
3013 gfc_free_expr (inquire
->nextrec
);
3014 gfc_free_expr (inquire
->blank
);
3015 gfc_free_expr (inquire
->position
);
3016 gfc_free_expr (inquire
->action
);
3017 gfc_free_expr (inquire
->read
);
3018 gfc_free_expr (inquire
->write
);
3019 gfc_free_expr (inquire
->readwrite
);
3020 gfc_free_expr (inquire
->delim
);
3021 gfc_free_expr (inquire
->pad
);
3022 gfc_free_expr (inquire
->iolength
);
3023 gfc_free_expr (inquire
->convert
);
3024 gfc_free_expr (inquire
->strm_pos
);
3030 /* Match an element of an INQUIRE statement. */
3032 #define RETM if (m != MATCH_NO) return m;
3035 match_inquire_element (gfc_inquire
* inquire
)
3039 m
= match_etag (&tag_unit
, &inquire
->unit
);
3040 RETM m
= match_etag (&tag_file
, &inquire
->file
);
3041 RETM m
= match_ltag (&tag_err
, &inquire
->err
);
3042 RETM m
= match_out_tag (&tag_iomsg
, &inquire
->iomsg
);
3043 RETM m
= match_out_tag (&tag_iostat
, &inquire
->iostat
);
3044 RETM m
= match_vtag (&tag_exist
, &inquire
->exist
);
3045 RETM m
= match_vtag (&tag_opened
, &inquire
->opened
);
3046 RETM m
= match_vtag (&tag_named
, &inquire
->named
);
3047 RETM m
= match_vtag (&tag_name
, &inquire
->name
);
3048 RETM m
= match_out_tag (&tag_number
, &inquire
->number
);
3049 RETM m
= match_vtag (&tag_s_access
, &inquire
->access
);
3050 RETM m
= match_vtag (&tag_sequential
, &inquire
->sequential
);
3051 RETM m
= match_vtag (&tag_direct
, &inquire
->direct
);
3052 RETM m
= match_vtag (&tag_s_form
, &inquire
->form
);
3053 RETM m
= match_vtag (&tag_formatted
, &inquire
->formatted
);
3054 RETM m
= match_vtag (&tag_unformatted
, &inquire
->unformatted
);
3055 RETM m
= match_out_tag (&tag_s_recl
, &inquire
->recl
);
3056 RETM m
= match_out_tag (&tag_nextrec
, &inquire
->nextrec
);
3057 RETM m
= match_vtag (&tag_s_blank
, &inquire
->blank
);
3058 RETM m
= match_vtag (&tag_s_position
, &inquire
->position
);
3059 RETM m
= match_vtag (&tag_s_action
, &inquire
->action
);
3060 RETM m
= match_vtag (&tag_read
, &inquire
->read
);
3061 RETM m
= match_vtag (&tag_write
, &inquire
->write
);
3062 RETM m
= match_vtag (&tag_readwrite
, &inquire
->readwrite
);
3063 RETM m
= match_vtag (&tag_s_delim
, &inquire
->delim
);
3064 RETM m
= match_vtag (&tag_s_pad
, &inquire
->pad
);
3065 RETM m
= match_vtag (&tag_iolength
, &inquire
->iolength
);
3066 RETM m
= match_vtag (&tag_convert
, &inquire
->convert
);
3067 RETM m
= match_out_tag (&tag_strm_out
, &inquire
->strm_pos
);
3068 RETM
return MATCH_NO
;
3075 gfc_match_inquire (void)
3077 gfc_inquire
*inquire
;
3082 m
= gfc_match_char ('(');
3086 inquire
= gfc_getmem (sizeof (gfc_inquire
));
3088 loc
= gfc_current_locus
;
3090 m
= match_inquire_element (inquire
);
3091 if (m
== MATCH_ERROR
)
3095 m
= gfc_match_expr (&inquire
->unit
);
3096 if (m
== MATCH_ERROR
)
3102 /* See if we have the IOLENGTH form of the inquire statement. */
3103 if (inquire
->iolength
!= NULL
)
3105 if (gfc_match_char (')') != MATCH_YES
)
3108 m
= match_io_list (M_INQUIRE
, &code
);
3109 if (m
== MATCH_ERROR
)
3114 new_st
.op
= EXEC_IOLENGTH
;
3115 new_st
.expr
= inquire
->iolength
;
3116 new_st
.ext
.inquire
= inquire
;
3118 if (gfc_pure (NULL
))
3120 gfc_free_statements (code
);
3121 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3125 new_st
.block
= gfc_get_code ();
3126 new_st
.block
->op
= EXEC_IOLENGTH
;
3127 terminate_io (code
);
3128 new_st
.block
->next
= code
;
3132 /* At this point, we have the non-IOLENGTH inquire statement. */
3135 if (gfc_match_char (')') == MATCH_YES
)
3137 if (gfc_match_char (',') != MATCH_YES
)
3140 m
= match_inquire_element (inquire
);
3141 if (m
== MATCH_ERROR
)
3146 if (inquire
->iolength
!= NULL
)
3148 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3153 if (gfc_match_eos () != MATCH_YES
)
3156 if (inquire
->unit
!= NULL
&& inquire
->file
!= NULL
)
3158 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
3159 " UNIT specifiers", &loc
);
3163 if (inquire
->unit
== NULL
&& inquire
->file
== NULL
)
3165 gfc_error ("INQUIRE statement at %L requires either FILE or"
3166 " UNIT specifier", &loc
);
3170 if (gfc_pure (NULL
))
3172 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3176 new_st
.op
= EXEC_INQUIRE
;
3177 new_st
.ext
.inquire
= inquire
;
3181 gfc_syntax_error (ST_INQUIRE
);
3184 gfc_free_inquire (inquire
);
3189 /* Resolve everything in a gfc_inquire structure. */
3192 gfc_resolve_inquire (gfc_inquire
* inquire
)
3195 RESOLVE_TAG (&tag_unit
, inquire
->unit
);
3196 RESOLVE_TAG (&tag_file
, inquire
->file
);
3197 RESOLVE_TAG (&tag_iomsg
, inquire
->iomsg
);
3198 RESOLVE_TAG (&tag_iostat
, inquire
->iostat
);
3199 RESOLVE_TAG (&tag_exist
, inquire
->exist
);
3200 RESOLVE_TAG (&tag_opened
, inquire
->opened
);
3201 RESOLVE_TAG (&tag_number
, inquire
->number
);
3202 RESOLVE_TAG (&tag_named
, inquire
->named
);
3203 RESOLVE_TAG (&tag_name
, inquire
->name
);
3204 RESOLVE_TAG (&tag_s_access
, inquire
->access
);
3205 RESOLVE_TAG (&tag_sequential
, inquire
->sequential
);
3206 RESOLVE_TAG (&tag_direct
, inquire
->direct
);
3207 RESOLVE_TAG (&tag_s_form
, inquire
->form
);
3208 RESOLVE_TAG (&tag_formatted
, inquire
->formatted
);
3209 RESOLVE_TAG (&tag_unformatted
, inquire
->unformatted
);
3210 RESOLVE_TAG (&tag_s_recl
, inquire
->recl
);
3211 RESOLVE_TAG (&tag_nextrec
, inquire
->nextrec
);
3212 RESOLVE_TAG (&tag_s_blank
, inquire
->blank
);
3213 RESOLVE_TAG (&tag_s_position
, inquire
->position
);
3214 RESOLVE_TAG (&tag_s_action
, inquire
->action
);
3215 RESOLVE_TAG (&tag_read
, inquire
->read
);
3216 RESOLVE_TAG (&tag_write
, inquire
->write
);
3217 RESOLVE_TAG (&tag_readwrite
, inquire
->readwrite
);
3218 RESOLVE_TAG (&tag_s_delim
, inquire
->delim
);
3219 RESOLVE_TAG (&tag_s_pad
, inquire
->pad
);
3220 RESOLVE_TAG (&tag_iolength
, inquire
->iolength
);
3221 RESOLVE_TAG (&tag_convert
, inquire
->convert
);
3222 RESOLVE_TAG (&tag_strm_out
, inquire
->strm_pos
);
3224 if (gfc_reference_st_label (inquire
->err
, ST_LABEL_TARGET
) == FAILURE
)