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 error
= _("$ must be the last specifier");
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 or P descriptor. */
772 /* Assume that we have another format item. */
779 extension_optional_comma
:
780 /* As a GNU extension, permit a missing comma after a string literal. */
798 error
= unexpected_end
;
802 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Missing comma at %C")
812 /* Something went wrong. If the format we're checking is a string,
813 generate a warning, since the program is correct. If the format
814 is in a FORMAT statement, this messes up parsing, which is an
816 if (mode
!= MODE_STRING
)
817 gfc_error ("%s in format string at %C", error
);
820 gfc_warning ("%s in format string at %C", error
);
822 /* TODO: More elaborate measures are needed to show where a problem
823 is within a format string that has been calculated. */
833 /* Given an expression node that is a constant string, see if it looks
834 like a format string. */
837 check_format_string (gfc_expr
* e
)
841 format_string
= e
->value
.character
.string
;
846 /************ Fortran 95 I/O statement matchers *************/
848 /* Match a FORMAT statement. This amounts to actually parsing the
849 format descriptors in order to correctly locate the end of the
853 gfc_match_format (void)
858 if (gfc_current_ns
->proc_name
859 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
861 gfc_error ("Format statement in module main block at %C.");
865 if (gfc_statement_label
== NULL
)
867 gfc_error ("Missing format label at %C");
870 gfc_gobble_whitespace ();
875 start
= gfc_current_locus
;
877 if (check_format () == FAILURE
)
880 if (gfc_match_eos () != MATCH_YES
)
882 gfc_syntax_error (ST_FORMAT
);
886 /* The label doesn't get created until after the statement is done
887 being matched, so we have to leave the string for later. */
889 gfc_current_locus
= start
; /* Back to the beginning */
892 new_st
.op
= EXEC_NOP
;
895 e
->expr_type
= EXPR_CONSTANT
;
896 e
->ts
.type
= BT_CHARACTER
;
897 e
->ts
.kind
= gfc_default_character_kind
;
899 e
->value
.character
.string
= format_string
= gfc_getmem(format_length
+1);
900 e
->value
.character
.length
= format_length
;
901 gfc_statement_label
->format
= e
;
904 check_format (); /* Guaranteed to succeed */
905 gfc_match_eos (); /* Guaranteed to succeed */
911 /* Match an expression I/O tag of some sort. */
914 match_etag (const io_tag
* tag
, gfc_expr
** v
)
919 m
= gfc_match (tag
->spec
, &result
);
925 gfc_error ("Duplicate %s specification at %C", tag
->name
);
926 gfc_free_expr (result
);
935 /* Match a variable I/O tag of some sort. */
938 match_vtag (const io_tag
* tag
, gfc_expr
** v
)
943 m
= gfc_match (tag
->spec
, &result
);
949 gfc_error ("Duplicate %s specification at %C", tag
->name
);
950 gfc_free_expr (result
);
954 if (result
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
956 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
957 gfc_free_expr (result
);
961 if (gfc_pure (NULL
) && gfc_impure_variable (result
->symtree
->n
.sym
))
963 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
964 gfc_free_expr (result
);
973 /* Match I/O tags that cause variables to become redefined. */
976 match_out_tag(const io_tag
*tag
, gfc_expr
**result
)
980 m
= match_vtag(tag
, result
);
982 gfc_check_do_variable((*result
)->symtree
);
988 /* Match a label I/O tag. */
991 match_ltag (const io_tag
* tag
, gfc_st_label
** label
)
997 m
= gfc_match (tag
->spec
, label
);
998 if (m
== MATCH_YES
&& old
!= 0)
1000 gfc_error ("Duplicate %s label specification at %C", tag
->name
);
1005 && gfc_reference_st_label (*label
, ST_LABEL_TARGET
) == FAILURE
)
1012 /* Do expression resolution and type-checking on an expression tag. */
1015 resolve_tag (const io_tag
* tag
, gfc_expr
* e
)
1021 if (gfc_resolve_expr (e
) == FAILURE
)
1024 if (e
->ts
.type
!= tag
->type
&& tag
!= &tag_format
)
1026 gfc_error ("%s tag at %L must be of type %s", tag
->name
,
1027 &e
->where
, gfc_basic_typename (tag
->type
));
1031 if (tag
== &tag_format
)
1033 if (e
->expr_type
== EXPR_CONSTANT
1034 && (e
->ts
.type
!= BT_CHARACTER
1035 || e
->ts
.kind
!= gfc_default_character_kind
))
1037 gfc_error ("Constant expression in FORMAT tag at %L must be "
1038 "of type default CHARACTER", &e
->where
);
1042 /* If e's rank is zero and e is not an element of an array, it should be
1043 of integer or character type. The integer variable should be
1045 if (e
->symtree
== NULL
|| e
->symtree
->n
.sym
->as
== NULL
1046 || e
->symtree
->n
.sym
->as
->rank
== 0)
1048 if (e
->ts
.type
!= BT_CHARACTER
&& e
->ts
.type
!= BT_INTEGER
)
1050 gfc_error ("%s tag at %L must be of type %s or %s", tag
->name
,
1051 &e
->where
, gfc_basic_typename (BT_CHARACTER
),
1052 gfc_basic_typename (BT_INTEGER
));
1055 else if (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_VARIABLE
)
1057 if (gfc_notify_std (GFC_STD_F95_DEL
,
1058 "Obsolete: ASSIGNED variable in FORMAT tag at %L",
1059 &e
->where
) == FAILURE
)
1061 if (e
->symtree
->n
.sym
->attr
.assign
!= 1)
1063 gfc_error ("Variable '%s' at %L has not been assigned a "
1064 "format label", e
->symtree
->n
.sym
->name
, &e
->where
);
1068 else if (e
->ts
.type
== BT_INTEGER
)
1070 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1071 "variable", gfc_basic_typename (e
->ts
.type
), &e
->where
);
1079 /* if rank is nonzero, we allow the type to be character under
1080 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1081 assigned an Hollerith constant. */
1082 if (e
->ts
.type
== BT_CHARACTER
)
1084 if (gfc_notify_std (GFC_STD_GNU
,
1085 "Extension: Character array in FORMAT tag at %L",
1086 &e
->where
) == FAILURE
)
1091 if (gfc_notify_std (GFC_STD_LEGACY
,
1092 "Extension: Non-character in FORMAT tag at %L",
1093 &e
->where
) == FAILURE
)
1103 gfc_error ("%s tag at %L must be scalar", tag
->name
, &e
->where
);
1107 if (tag
== &tag_iomsg
)
1109 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IOMSG tag at %L",
1110 &e
->where
) == FAILURE
)
1114 if (tag
== &tag_iostat
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1116 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1117 "INTEGER in IOSTAT tag at %L",
1118 &e
->where
) == FAILURE
)
1122 if (tag
== &tag_size
&& e
->ts
.kind
!= gfc_default_integer_kind
)
1124 if (gfc_notify_std (GFC_STD_GNU
, "Fortran 95 requires default "
1125 "INTEGER in SIZE tag at %L",
1126 &e
->where
) == FAILURE
)
1130 if (tag
== &tag_convert
)
1132 if (gfc_notify_std (GFC_STD_GNU
, "Extension: CONVERT tag at %L",
1133 &e
->where
) == FAILURE
)
1142 /* Match a single tag of an OPEN statement. */
1145 match_open_element (gfc_open
* open
)
1149 m
= match_etag (&tag_unit
, &open
->unit
);
1152 m
= match_out_tag (&tag_iomsg
, &open
->iomsg
);
1155 m
= match_out_tag (&tag_iostat
, &open
->iostat
);
1158 m
= match_etag (&tag_file
, &open
->file
);
1161 m
= match_etag (&tag_status
, &open
->status
);
1164 m
= match_etag (&tag_e_access
, &open
->access
);
1167 m
= match_etag (&tag_e_form
, &open
->form
);
1170 m
= match_etag (&tag_e_recl
, &open
->recl
);
1173 m
= match_etag (&tag_e_blank
, &open
->blank
);
1176 m
= match_etag (&tag_e_position
, &open
->position
);
1179 m
= match_etag (&tag_e_action
, &open
->action
);
1182 m
= match_etag (&tag_e_delim
, &open
->delim
);
1185 m
= match_etag (&tag_e_pad
, &open
->pad
);
1188 m
= match_ltag (&tag_err
, &open
->err
);
1191 m
= match_etag (&tag_convert
, &open
->convert
);
1199 /* Free the gfc_open structure and all the expressions it contains. */
1202 gfc_free_open (gfc_open
* open
)
1208 gfc_free_expr (open
->unit
);
1209 gfc_free_expr (open
->iomsg
);
1210 gfc_free_expr (open
->iostat
);
1211 gfc_free_expr (open
->file
);
1212 gfc_free_expr (open
->status
);
1213 gfc_free_expr (open
->access
);
1214 gfc_free_expr (open
->form
);
1215 gfc_free_expr (open
->recl
);
1216 gfc_free_expr (open
->blank
);
1217 gfc_free_expr (open
->position
);
1218 gfc_free_expr (open
->action
);
1219 gfc_free_expr (open
->delim
);
1220 gfc_free_expr (open
->pad
);
1221 gfc_free_expr (open
->convert
);
1227 /* Resolve everything in a gfc_open structure. */
1230 gfc_resolve_open (gfc_open
* open
)
1233 RESOLVE_TAG (&tag_unit
, open
->unit
);
1234 RESOLVE_TAG (&tag_iomsg
, open
->iomsg
);
1235 RESOLVE_TAG (&tag_iostat
, open
->iostat
);
1236 RESOLVE_TAG (&tag_file
, open
->file
);
1237 RESOLVE_TAG (&tag_status
, open
->status
);
1238 RESOLVE_TAG (&tag_e_access
, open
->access
);
1239 RESOLVE_TAG (&tag_e_form
, open
->form
);
1240 RESOLVE_TAG (&tag_e_recl
, open
->recl
);
1242 RESOLVE_TAG (&tag_e_blank
, open
->blank
);
1243 RESOLVE_TAG (&tag_e_position
, open
->position
);
1244 RESOLVE_TAG (&tag_e_action
, open
->action
);
1245 RESOLVE_TAG (&tag_e_delim
, open
->delim
);
1246 RESOLVE_TAG (&tag_e_pad
, open
->pad
);
1247 RESOLVE_TAG (&tag_convert
, open
->convert
);
1249 if (gfc_reference_st_label (open
->err
, ST_LABEL_TARGET
) == FAILURE
)
1257 /* Check if a given value for a SPECIFIER is either in the list of values
1258 allowed in F95 or F2003, issuing an error message and returning a zero
1259 value if it is not allowed. */
1261 compare_to_allowed_values (const char * specifier
, const char * allowed
[],
1262 const char * allowed_f2003
[],
1263 const char * allowed_gnu
[], char * value
,
1264 const char * statement
, bool warn
)
1269 len
= strlen(value
);
1272 for (len
--; len
> 0; len
--)
1273 if (value
[len
] != ' ')
1278 for (i
= 0; allowed
[i
]; i
++)
1279 if (len
== strlen(allowed
[i
])
1280 && strncasecmp (value
, allowed
[i
], strlen(allowed
[i
])) == 0)
1283 for (i
= 0; allowed_f2003
&& allowed_f2003
[i
]; i
++)
1284 if (len
== strlen(allowed_f2003
[i
])
1285 && strncasecmp (value
, allowed_f2003
[i
], strlen(allowed_f2003
[i
])) == 0)
1287 notification n
= gfc_notification_std (GFC_STD_F2003
);
1289 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1291 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1292 "has value '%s'", specifier
, statement
,
1299 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: %s specifier in "
1300 "%s statement at %C has value '%s'", specifier
,
1301 statement
, allowed_f2003
[i
]);
1309 for (i
= 0; allowed_gnu
&& allowed_gnu
[i
]; i
++)
1310 if (len
== strlen(allowed_gnu
[i
])
1311 && strncasecmp (value
, allowed_gnu
[i
], strlen(allowed_gnu
[i
])) == 0)
1313 notification n
= gfc_notification_std (GFC_STD_GNU
);
1315 if (n
== WARNING
|| (warn
&& n
== ERROR
))
1317 gfc_warning ("Extension: %s specifier in %s statement at %C "
1318 "has value '%s'", specifier
, statement
,
1325 gfc_notify_std (GFC_STD_GNU
, "Extension: %s specifier in "
1326 "%s statement at %C has value '%s'", specifier
,
1327 statement
, allowed_gnu
[i
]);
1337 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1338 specifier
, statement
, value
);
1343 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1344 specifier
, statement
, value
);
1349 /* Match an OPEN statement. */
1352 gfc_match_open (void)
1358 m
= gfc_match_char ('(');
1362 open
= gfc_getmem (sizeof (gfc_open
));
1364 m
= match_open_element (open
);
1366 if (m
== MATCH_ERROR
)
1370 m
= gfc_match_expr (&open
->unit
);
1373 if (m
== MATCH_ERROR
)
1379 if (gfc_match_char (')') == MATCH_YES
)
1381 if (gfc_match_char (',') != MATCH_YES
)
1384 m
= match_open_element (open
);
1385 if (m
== MATCH_ERROR
)
1391 if (gfc_match_eos () == MATCH_NO
)
1394 if (gfc_pure (NULL
))
1396 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1400 warn
= (open
->err
|| open
->iostat
) ? true : false;
1401 /* Checks on the ACCESS specifier. */
1402 if (open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
)
1404 static const char * access_f95
[] = { "SEQUENTIAL", "DIRECT", NULL
};
1405 static const char * access_f2003
[] = { "STREAM", NULL
};
1406 static const char * access_gnu
[] = { "APPEND", NULL
};
1408 if (!compare_to_allowed_values ("ACCESS", access_f95
, access_f2003
,
1410 open
->access
->value
.character
.string
,
1415 /* Checks on the ACTION specifier. */
1416 if (open
->action
&& open
->action
->expr_type
== EXPR_CONSTANT
)
1418 static const char * action
[] = { "READ", "WRITE", "READWRITE", NULL
};
1420 if (!compare_to_allowed_values ("ACTION", action
, NULL
, NULL
,
1421 open
->action
->value
.character
.string
,
1426 /* Checks on the ASYNCHRONOUS specifier. */
1427 /* TODO: code is ready, just needs uncommenting when async I/O support
1429 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1431 static const char * asynchronous[] = { "YES", "NO", NULL };
1433 if (!compare_to_allowed_values
1434 ("action", asynchronous, NULL, NULL,
1435 open->asynchronous->value.character.string, "OPEN", warn))
1439 /* Checks on the BLANK specifier. */
1440 if (open
->blank
&& open
->blank
->expr_type
== EXPR_CONSTANT
)
1442 static const char * blank
[] = { "ZERO", "NULL", NULL
};
1444 if (!compare_to_allowed_values ("BLANK", blank
, NULL
, NULL
,
1445 open
->blank
->value
.character
.string
,
1450 /* Checks on the DECIMAL specifier. */
1451 /* TODO: uncomment this code when DECIMAL support is added
1452 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1454 static const char * decimal[] = { "COMMA", "POINT", NULL };
1456 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1457 open->decimal->value.character.string,
1462 /* Checks on the DELIM specifier. */
1463 if (open
->delim
&& open
->delim
->expr_type
== EXPR_CONSTANT
)
1465 static const char * delim
[] = { "APOSTROPHE", "QUOTE", "NONE", NULL
};
1467 if (!compare_to_allowed_values ("DELIM", delim
, NULL
, NULL
,
1468 open
->delim
->value
.character
.string
,
1473 /* Checks on the ENCODING specifier. */
1474 /* TODO: uncomment this code when ENCODING support is added
1475 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1477 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1479 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1480 open->encoding->value.character.string,
1485 /* Checks on the FORM specifier. */
1486 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
)
1488 static const char * form
[] = { "FORMATTED", "UNFORMATTED", NULL
};
1490 if (!compare_to_allowed_values ("FORM", form
, NULL
, NULL
,
1491 open
->form
->value
.character
.string
,
1496 /* Checks on the PAD specifier. */
1497 if (open
->pad
&& open
->pad
->expr_type
== EXPR_CONSTANT
)
1499 static const char * pad
[] = { "YES", "NO", NULL
};
1501 if (!compare_to_allowed_values ("PAD", pad
, NULL
, NULL
,
1502 open
->pad
->value
.character
.string
,
1507 /* Checks on the POSITION specifier. */
1508 if (open
->position
&& open
->position
->expr_type
== EXPR_CONSTANT
)
1510 static const char * position
[] = { "ASIS", "REWIND", "APPEND", NULL
};
1512 if (!compare_to_allowed_values ("POSITION", position
, NULL
, NULL
,
1513 open
->position
->value
.character
.string
,
1518 /* Checks on the ROUND specifier. */
1519 /* TODO: uncomment this code when ROUND support is added
1520 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1522 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1523 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1525 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1526 open->round->value.character.string,
1531 /* Checks on the SIGN specifier. */
1532 /* TODO: uncomment this code when SIGN support is added
1533 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1535 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1538 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1539 open->sign->value.character.string,
1544 #define warn_or_error(...) \
1547 gfc_warning (__VA_ARGS__); \
1550 gfc_error (__VA_ARGS__); \
1555 /* Checks on the RECL specifier. */
1556 if (open
->recl
&& open
->recl
->expr_type
== EXPR_CONSTANT
1557 && open
->recl
->ts
.type
== BT_INTEGER
1558 && mpz_sgn (open
->recl
->value
.integer
) != 1)
1560 warn_or_error ("RECL in OPEN statement at %C must be positive");
1563 /* Checks on the STATUS specifier. */
1564 if (open
->status
&& open
->status
->expr_type
== EXPR_CONSTANT
)
1566 static const char * status
[] = { "OLD", "NEW", "SCRATCH",
1567 "REPLACE", "UNKNOWN", NULL
};
1569 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1570 open
->status
->value
.character
.string
,
1574 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1575 the FILE= specifier shall appear. */
1576 if (open
->file
== NULL
&&
1577 (strncasecmp (open
->status
->value
.character
.string
, "replace", 7) == 0
1578 || strncasecmp (open
->status
->value
.character
.string
, "new", 3) == 0))
1580 warn_or_error ("The STATUS specified in OPEN statement at %C is '%s' "
1581 "and no FILE specifier is present",
1582 open
->status
->value
.character
.string
);
1585 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1586 the FILE= specifier shall not appear. */
1587 if (strncasecmp (open
->status
->value
.character
.string
, "scratch", 7) == 0
1590 warn_or_error ("The STATUS specified in OPEN statement at %C cannot "
1591 "have the value SCRATCH if a FILE specifier "
1596 /* Things that are not allowed for unformatted I/O. */
1597 if (open
->form
&& open
->form
->expr_type
== EXPR_CONSTANT
1599 /* TODO uncomment this code when F2003 support is finished */
1600 /* || open->decimal || open->encoding || open->round
1602 || open
->pad
|| open
->blank
)
1603 && strncasecmp (open
->form
->value
.character
.string
,
1604 "unformatted", 11) == 0)
1606 const char * spec
= (open
->delim
? "DELIM " : (open
->pad
? "PAD " :
1607 open
->blank
? "BLANK " : ""));
1609 warn_or_error ("%sspecifier at %C not allowed in OPEN statement for "
1610 "unformatted I/O", spec
);
1613 if (open
->recl
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1614 && strncasecmp (open
->access
->value
.character
.string
, "stream", 6) == 0)
1616 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1620 if (open
->position
&& open
->access
&& open
->access
->expr_type
== EXPR_CONSTANT
1621 && !(strncasecmp (open
->access
->value
.character
.string
,
1622 "sequential", 10) == 0
1623 || strncasecmp (open
->access
->value
.character
.string
,
1625 || strncasecmp (open
->access
->value
.character
.string
,
1628 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1629 "for stream or sequential ACCESS");
1632 #undef warn_or_error
1634 new_st
.op
= EXEC_OPEN
;
1635 new_st
.ext
.open
= open
;
1639 gfc_syntax_error (ST_OPEN
);
1642 gfc_free_open (open
);
1647 /* Free a gfc_close structure an all its expressions. */
1650 gfc_free_close (gfc_close
* close
)
1656 gfc_free_expr (close
->unit
);
1657 gfc_free_expr (close
->iomsg
);
1658 gfc_free_expr (close
->iostat
);
1659 gfc_free_expr (close
->status
);
1665 /* Match elements of a CLOSE statement. */
1668 match_close_element (gfc_close
* close
)
1672 m
= match_etag (&tag_unit
, &close
->unit
);
1675 m
= match_etag (&tag_status
, &close
->status
);
1678 m
= match_out_tag (&tag_iomsg
, &close
->iomsg
);
1681 m
= match_out_tag (&tag_iostat
, &close
->iostat
);
1684 m
= match_ltag (&tag_err
, &close
->err
);
1692 /* Match a CLOSE statement. */
1695 gfc_match_close (void)
1701 m
= gfc_match_char ('(');
1705 close
= gfc_getmem (sizeof (gfc_close
));
1707 m
= match_close_element (close
);
1709 if (m
== MATCH_ERROR
)
1713 m
= gfc_match_expr (&close
->unit
);
1716 if (m
== MATCH_ERROR
)
1722 if (gfc_match_char (')') == MATCH_YES
)
1724 if (gfc_match_char (',') != MATCH_YES
)
1727 m
= match_close_element (close
);
1728 if (m
== MATCH_ERROR
)
1734 if (gfc_match_eos () == MATCH_NO
)
1737 if (gfc_pure (NULL
))
1739 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1743 warn
= (close
->iostat
|| close
->err
) ? true : false;
1745 /* Checks on the STATUS specifier. */
1746 if (close
->status
&& close
->status
->expr_type
== EXPR_CONSTANT
)
1748 static const char * status
[] = { "KEEP", "DELETE" };
1750 if (!compare_to_allowed_values ("STATUS", status
, NULL
, NULL
,
1751 close
->status
->value
.character
.string
,
1756 new_st
.op
= EXEC_CLOSE
;
1757 new_st
.ext
.close
= close
;
1761 gfc_syntax_error (ST_CLOSE
);
1764 gfc_free_close (close
);
1769 /* Resolve everything in a gfc_close structure. */
1772 gfc_resolve_close (gfc_close
* close
)
1775 RESOLVE_TAG (&tag_unit
, close
->unit
);
1776 RESOLVE_TAG (&tag_iomsg
, close
->iomsg
);
1777 RESOLVE_TAG (&tag_iostat
, close
->iostat
);
1778 RESOLVE_TAG (&tag_status
, close
->status
);
1780 if (gfc_reference_st_label (close
->err
, ST_LABEL_TARGET
) == FAILURE
)
1787 /* Free a gfc_filepos structure. */
1790 gfc_free_filepos (gfc_filepos
* fp
)
1793 gfc_free_expr (fp
->unit
);
1794 gfc_free_expr (fp
->iomsg
);
1795 gfc_free_expr (fp
->iostat
);
1800 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1803 match_file_element (gfc_filepos
* fp
)
1807 m
= match_etag (&tag_unit
, &fp
->unit
);
1810 m
= match_out_tag (&tag_iomsg
, &fp
->iomsg
);
1813 m
= match_out_tag (&tag_iostat
, &fp
->iostat
);
1816 m
= match_ltag (&tag_err
, &fp
->err
);
1824 /* Match the second half of the file-positioning statements, REWIND,
1825 BACKSPACE, ENDFILE, or the FLUSH statement. */
1828 match_filepos (gfc_statement st
, gfc_exec_op op
)
1833 fp
= gfc_getmem (sizeof (gfc_filepos
));
1835 if (gfc_match_char ('(') == MATCH_NO
)
1837 m
= gfc_match_expr (&fp
->unit
);
1838 if (m
== MATCH_ERROR
)
1846 m
= match_file_element (fp
);
1847 if (m
== MATCH_ERROR
)
1851 m
= gfc_match_expr (&fp
->unit
);
1852 if (m
== MATCH_ERROR
)
1860 if (gfc_match_char (')') == MATCH_YES
)
1862 if (gfc_match_char (',') != MATCH_YES
)
1865 m
= match_file_element (fp
);
1866 if (m
== MATCH_ERROR
)
1873 if (gfc_match_eos () != MATCH_YES
)
1876 if (gfc_pure (NULL
))
1878 gfc_error ("%s statement not allowed in PURE procedure at %C",
1879 gfc_ascii_statement (st
));
1885 new_st
.ext
.filepos
= fp
;
1889 gfc_syntax_error (st
);
1892 gfc_free_filepos (fp
);
1898 gfc_resolve_filepos (gfc_filepos
* fp
)
1901 RESOLVE_TAG (&tag_unit
, fp
->unit
);
1902 RESOLVE_TAG (&tag_iostat
, fp
->iostat
);
1903 RESOLVE_TAG (&tag_iomsg
, fp
->iomsg
);
1904 if (gfc_reference_st_label (fp
->err
, ST_LABEL_TARGET
) == FAILURE
)
1911 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1912 and the FLUSH statement. */
1915 gfc_match_endfile (void)
1918 return match_filepos (ST_END_FILE
, EXEC_ENDFILE
);
1922 gfc_match_backspace (void)
1925 return match_filepos (ST_BACKSPACE
, EXEC_BACKSPACE
);
1929 gfc_match_rewind (void)
1932 return match_filepos (ST_REWIND
, EXEC_REWIND
);
1936 gfc_match_flush (void)
1938 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: FLUSH statement at %C") == FAILURE
)
1941 return match_filepos (ST_FLUSH
, EXEC_FLUSH
);
1944 /******************** Data Transfer Statements *********************/
1947 { M_READ
, M_WRITE
, M_PRINT
, M_INQUIRE
}
1951 /* Return a default unit number. */
1954 default_unit (io_kind k
)
1963 return gfc_int_expr (unit
);
1967 /* Match a unit specification for a data transfer statement. */
1970 match_dt_unit (io_kind k
, gfc_dt
* dt
)
1974 if (gfc_match_char ('*') == MATCH_YES
)
1976 if (dt
->io_unit
!= NULL
)
1979 dt
->io_unit
= default_unit (k
);
1983 if (gfc_match_expr (&e
) == MATCH_YES
)
1985 if (dt
->io_unit
!= NULL
)
1998 gfc_error ("Duplicate UNIT specification at %C");
2003 /* Match a format specification. */
2006 match_dt_format (gfc_dt
* dt
)
2010 gfc_st_label
*label
;
2012 where
= gfc_current_locus
;
2014 if (gfc_match_char ('*') == MATCH_YES
)
2016 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2019 dt
->format_label
= &format_asterisk
;
2023 if (gfc_match_st_label (&label
) == MATCH_YES
)
2025 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2027 gfc_free_st_label (label
);
2031 if (gfc_reference_st_label (label
, ST_LABEL_FORMAT
) == FAILURE
)
2034 dt
->format_label
= label
;
2038 if (gfc_match_expr (&e
) == MATCH_YES
)
2040 if (dt
->format_expr
!= NULL
|| dt
->format_label
!= NULL
)
2045 dt
->format_expr
= e
;
2049 gfc_current_locus
= where
; /* The only case where we have to restore */
2054 gfc_error ("Duplicate format specification at %C");
2059 /* Traverse a namelist that is part of a READ statement to make sure
2060 that none of the variables in the namelist are INTENT(IN). Returns
2061 nonzero if we find such a variable. */
2064 check_namelist (gfc_symbol
* sym
)
2068 for (p
= sym
->namelist
; p
; p
= p
->next
)
2069 if (p
->sym
->attr
.intent
== INTENT_IN
)
2071 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2072 p
->sym
->name
, sym
->name
);
2080 /* Match a single data transfer element. */
2083 match_dt_element (io_kind k
, gfc_dt
* dt
)
2085 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2089 if (gfc_match (" unit =") == MATCH_YES
)
2091 m
= match_dt_unit (k
, dt
);
2096 if (gfc_match (" fmt =") == MATCH_YES
)
2098 m
= match_dt_format (dt
);
2103 if (gfc_match (" nml = %n", name
) == MATCH_YES
)
2105 if (dt
->namelist
!= NULL
)
2107 gfc_error ("Duplicate NML specification at %C");
2111 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2114 if (sym
== NULL
|| sym
->attr
.flavor
!= FL_NAMELIST
)
2116 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2117 sym
!= NULL
? sym
->name
: name
);
2122 if (k
== M_READ
&& check_namelist (sym
))
2128 m
= match_etag (&tag_rec
, &dt
->rec
);
2131 m
= match_etag (&tag_spos
, &dt
->rec
);
2134 m
= match_out_tag (&tag_iomsg
, &dt
->iomsg
);
2137 m
= match_out_tag (&tag_iostat
, &dt
->iostat
);
2140 m
= match_ltag (&tag_err
, &dt
->err
);
2142 dt
->err_where
= gfc_current_locus
;
2145 m
= match_etag (&tag_advance
, &dt
->advance
);
2148 m
= match_out_tag (&tag_size
, &dt
->size
);
2152 m
= match_ltag (&tag_end
, &dt
->end
);
2157 gfc_error ("END tag at %C not allowed in output statement");
2160 dt
->end_where
= gfc_current_locus
;
2165 m
= match_ltag (&tag_eor
, &dt
->eor
);
2167 dt
->eor_where
= gfc_current_locus
;
2175 /* Free a data transfer structure and everything below it. */
2178 gfc_free_dt (gfc_dt
* dt
)
2184 gfc_free_expr (dt
->io_unit
);
2185 gfc_free_expr (dt
->format_expr
);
2186 gfc_free_expr (dt
->rec
);
2187 gfc_free_expr (dt
->advance
);
2188 gfc_free_expr (dt
->iomsg
);
2189 gfc_free_expr (dt
->iostat
);
2190 gfc_free_expr (dt
->size
);
2196 /* Resolve everything in a gfc_dt structure. */
2199 gfc_resolve_dt (gfc_dt
* dt
)
2203 RESOLVE_TAG (&tag_format
, dt
->format_expr
);
2204 RESOLVE_TAG (&tag_rec
, dt
->rec
);
2205 RESOLVE_TAG (&tag_spos
, dt
->rec
);
2206 RESOLVE_TAG (&tag_advance
, dt
->advance
);
2207 RESOLVE_TAG (&tag_iomsg
, dt
->iomsg
);
2208 RESOLVE_TAG (&tag_iostat
, dt
->iostat
);
2209 RESOLVE_TAG (&tag_size
, dt
->size
);
2212 if (gfc_resolve_expr (e
) == SUCCESS
2213 && (e
->ts
.type
!= BT_INTEGER
2214 && (e
->ts
.type
!= BT_CHARACTER
2215 || e
->expr_type
!= EXPR_VARIABLE
)))
2218 ("UNIT specification at %L must be an INTEGER expression or a "
2219 "CHARACTER variable", &e
->where
);
2223 if (e
->ts
.type
== BT_CHARACTER
)
2225 if (gfc_has_vector_index (e
))
2227 gfc_error ("Internal unit with vector subscript at %L",
2233 if (e
->rank
&& e
->ts
.type
!= BT_CHARACTER
)
2235 gfc_error ("External IO UNIT cannot be an array at %L", &e
->where
);
2241 if (gfc_reference_st_label (dt
->err
, ST_LABEL_TARGET
) == FAILURE
)
2243 if (dt
->err
->defined
== ST_LABEL_UNKNOWN
)
2245 gfc_error ("ERR tag label %d at %L not defined",
2246 dt
->err
->value
, &dt
->err_where
);
2253 if (gfc_reference_st_label (dt
->end
, ST_LABEL_TARGET
) == FAILURE
)
2255 if (dt
->end
->defined
== ST_LABEL_UNKNOWN
)
2257 gfc_error ("END tag label %d at %L not defined",
2258 dt
->end
->value
, &dt
->end_where
);
2265 if (gfc_reference_st_label (dt
->eor
, ST_LABEL_TARGET
) == FAILURE
)
2267 if (dt
->eor
->defined
== ST_LABEL_UNKNOWN
)
2269 gfc_error ("EOR tag label %d at %L not defined",
2270 dt
->eor
->value
, &dt
->eor_where
);
2275 /* Check the format label actually exists. */
2276 if (dt
->format_label
&& dt
->format_label
!= &format_asterisk
2277 && dt
->format_label
->defined
== ST_LABEL_UNKNOWN
)
2279 gfc_error ("FORMAT label %d at %L not defined", dt
->format_label
->value
,
2280 &dt
->format_label
->where
);
2287 /* Given an io_kind, return its name. */
2290 io_kind_name (io_kind k
)
2309 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2316 /* Match an IO iteration statement of the form:
2318 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2320 which is equivalent to a single IO element. This function is
2321 mutually recursive with match_io_element(). */
2323 static match
match_io_element (io_kind k
, gfc_code
**);
2326 match_io_iterator (io_kind k
, gfc_code
** result
)
2328 gfc_code
*head
, *tail
, *new;
2336 old_loc
= gfc_current_locus
;
2338 if (gfc_match_char ('(') != MATCH_YES
)
2341 m
= match_io_element (k
, &head
);
2344 if (m
!= MATCH_YES
|| gfc_match_char (',') != MATCH_YES
)
2350 /* Can't be anything but an IO iterator. Build a list. */
2351 iter
= gfc_get_iterator ();
2355 m
= gfc_match_iterator (iter
, 0);
2356 if (m
== MATCH_ERROR
)
2360 gfc_check_do_variable (iter
->var
->symtree
);
2364 m
= match_io_element (k
, &new);
2365 if (m
== MATCH_ERROR
)
2374 tail
= gfc_append_code (tail
, new);
2376 if (gfc_match_char (',') != MATCH_YES
)
2385 if (gfc_match_char (')') != MATCH_YES
)
2388 new = gfc_get_code ();
2390 new->ext
.iterator
= iter
;
2392 new->block
= gfc_get_code ();
2393 new->block
->op
= EXEC_DO
;
2394 new->block
->next
= head
;
2400 gfc_error ("Syntax error in I/O iterator at %C");
2404 gfc_free_iterator (iter
, 1);
2405 gfc_free_statements (head
);
2406 gfc_current_locus
= old_loc
;
2411 /* Match a single element of an IO list, which is either a single
2412 expression or an IO Iterator. */
2415 match_io_element (io_kind k
, gfc_code
** cpp
)
2423 m
= match_io_iterator (k
, cpp
);
2429 m
= gfc_match_variable (&expr
, 0);
2431 gfc_error ("Expected variable in READ statement at %C");
2435 m
= gfc_match_expr (&expr
);
2437 gfc_error ("Expected expression in %s statement at %C",
2445 if (expr
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
2448 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2449 expr
->symtree
->n
.sym
->name
);
2454 && gfc_impure_variable (expr
->symtree
->n
.sym
)
2455 && current_dt
->io_unit
->ts
.type
== BT_CHARACTER
)
2457 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2458 expr
->symtree
->n
.sym
->name
);
2462 if (gfc_check_do_variable (expr
->symtree
))
2468 if (current_dt
->io_unit
->ts
.type
== BT_CHARACTER
2470 && current_dt
->io_unit
->expr_type
== EXPR_VARIABLE
2471 && gfc_impure_variable (current_dt
->io_unit
->symtree
->n
.sym
))
2474 ("Cannot write to internal file unit '%s' at %C inside a "
2475 "PURE procedure", current_dt
->io_unit
->symtree
->n
.sym
->name
);
2487 gfc_free_expr (expr
);
2491 cp
= gfc_get_code ();
2492 cp
->op
= EXEC_TRANSFER
;
2500 /* Match an I/O list, building gfc_code structures as we go. */
2503 match_io_list (io_kind k
, gfc_code
** head_p
)
2505 gfc_code
*head
, *tail
, *new;
2508 *head_p
= head
= tail
= NULL
;
2509 if (gfc_match_eos () == MATCH_YES
)
2514 m
= match_io_element (k
, &new);
2515 if (m
== MATCH_ERROR
)
2520 tail
= gfc_append_code (tail
, new);
2524 if (gfc_match_eos () == MATCH_YES
)
2526 if (gfc_match_char (',') != MATCH_YES
)
2534 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2537 gfc_free_statements (head
);
2542 /* Attach the data transfer end node. */
2545 terminate_io (gfc_code
* io_code
)
2549 if (io_code
== NULL
)
2550 io_code
= new_st
.block
;
2552 c
= gfc_get_code ();
2553 c
->op
= EXEC_DT_END
;
2555 /* Point to structure that is already there */
2556 c
->ext
.dt
= new_st
.ext
.dt
;
2557 gfc_append_code (io_code
, c
);
2561 /* Check the constraints for a data transfer statement. The majority of the
2562 constraints appearing in 9.4 of the standard appear here. Some are handled
2563 in resolve_tag and others in gfc_resolve_dt. */
2566 check_io_constraints (io_kind k
, gfc_dt
*dt
, gfc_code
* io_code
, locus
* spec_end
)
2568 #define io_constraint(condition,msg,arg)\
2571 gfc_error(msg,arg);\
2577 gfc_symbol
* sym
= NULL
;
2582 if (expr
&& expr
->expr_type
== EXPR_VARIABLE
2583 && expr
->ts
.type
== BT_CHARACTER
)
2585 sym
= expr
->symtree
->n
.sym
;
2587 io_constraint (k
== M_WRITE
&& sym
->attr
.intent
== INTENT_IN
,
2588 "Internal file at %L must not be INTENT(IN)",
2591 io_constraint (gfc_has_vector_index (dt
->io_unit
),
2592 "Internal file incompatible with vector subscript at %L",
2595 io_constraint (dt
->rec
!= NULL
,
2596 "REC tag at %L is incompatible with internal file",
2599 io_constraint (dt
->namelist
!= NULL
,
2600 "Internal file at %L is incompatible with namelist",
2603 io_constraint (dt
->advance
!= NULL
,
2604 "ADVANCE tag at %L is incompatible with internal file",
2605 &dt
->advance
->where
);
2608 if (expr
&& expr
->ts
.type
!= BT_CHARACTER
)
2611 io_constraint (gfc_pure (NULL
)
2612 && (k
== M_READ
|| k
== M_WRITE
),
2613 "IO UNIT in %s statement at %C must be "
2614 "an internal file in a PURE procedure",
2621 io_constraint (dt
->end
,
2622 "END tag not allowed with output at %L",
2625 io_constraint (dt
->eor
,
2626 "EOR tag not allowed with output at %L",
2629 io_constraint (k
!= M_READ
&& dt
->size
,
2630 "SIZE=specifier not allowed with output at %L",
2635 io_constraint (dt
->size
&& dt
->advance
== NULL
,
2636 "SIZE tag at %L requires an ADVANCE tag",
2639 io_constraint (dt
->eor
&& dt
->advance
== NULL
,
2640 "EOR tag at %L requires an ADVANCE tag",
2648 io_constraint (io_code
&& dt
->namelist
,
2649 "NAMELIST cannot be followed by IO-list at %L",
2652 io_constraint (dt
->format_expr
,
2653 "IO spec-list cannot contain both NAMELIST group name "
2654 "and format specification at %L.",
2655 &dt
->format_expr
->where
);
2657 io_constraint (dt
->format_label
,
2658 "IO spec-list cannot contain both NAMELIST group name "
2659 "and format label at %L", spec_end
);
2661 io_constraint (dt
->rec
,
2662 "NAMELIST IO is not allowed with a REC=specifier "
2663 "at %L.", &dt
->rec
->where
);
2665 io_constraint (dt
->advance
,
2666 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2667 "at %L.", &dt
->advance
->where
);
2672 io_constraint (dt
->end
,
2673 "An END tag is not allowed with a "
2674 "REC=specifier at %L.", &dt
->end_where
);
2677 io_constraint (dt
->format_label
== &format_asterisk
,
2678 "FMT=* is not allowed with a REC=specifier "
2679 "at %L.", spec_end
);
2684 int not_yes
, not_no
;
2687 io_constraint (dt
->format_label
== &format_asterisk
,
2688 "List directed format(*) is not allowed with a "
2689 "ADVANCE=specifier at %L.", &expr
->where
);
2691 io_constraint (dt
->format_expr
== NULL
2692 && dt
->format_label
== NULL
2693 && dt
->namelist
== NULL
,
2694 "the ADVANCE=specifier at %L must appear with an "
2695 "explicit format expression", &expr
->where
);
2697 if (expr
->expr_type
== EXPR_CONSTANT
&& expr
->ts
.type
== BT_CHARACTER
)
2699 const char * advance
= expr
->value
.character
.string
;
2700 not_no
= strncasecmp (advance
, "no", 2) != 0;
2701 not_yes
= strncasecmp (advance
, "yes", 2) != 0;
2709 io_constraint (not_no
&& not_yes
,
2710 "ADVANCE=specifier at %L must have value = "
2711 "YES or NO.", &expr
->where
);
2713 io_constraint (dt
->size
&& not_no
&& k
== M_READ
,
2714 "SIZE tag at %L requires an ADVANCE = 'NO'",
2717 io_constraint (dt
->eor
&& not_no
&& k
== M_READ
,
2718 "EOR tag at %L requires an ADVANCE = 'NO'",
2722 expr
= dt
->format_expr
;
2723 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
)
2724 check_format_string (expr
);
2728 #undef io_constraint
2730 /* Match a READ, WRITE or PRINT statement. */
2733 match_io (io_kind k
)
2735 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2744 where
= gfc_current_locus
;
2746 current_dt
= dt
= gfc_getmem (sizeof (gfc_dt
));
2747 if (gfc_match_char ('(') == MATCH_NO
)
2749 where
= gfc_current_locus
;
2752 else if (k
== M_PRINT
)
2754 /* Treat the non-standard case of PRINT namelist. */
2755 if ((gfc_current_form
== FORM_FIXED
|| gfc_peek_char () == ' ')
2756 && gfc_match_name (name
) == MATCH_YES
)
2758 gfc_find_symbol (name
, NULL
, 1, &sym
);
2759 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2761 if (gfc_notify_std (GFC_STD_GNU
, "PRINT namelist at "
2762 "%C is an extension") == FAILURE
)
2768 dt
->io_unit
= default_unit (k
);
2773 gfc_current_locus
= where
;
2777 if (gfc_current_form
== FORM_FREE
)
2779 c
= gfc_peek_char();
2780 if (c
!= ' ' && c
!= '*' && c
!= '\'' && c
!= '"')
2787 m
= match_dt_format (dt
);
2788 if (m
== MATCH_ERROR
)
2794 dt
->io_unit
= default_unit (k
);
2799 /* Error for constructs like print (1,*). */
2804 /* Match a control list */
2805 if (match_dt_element (k
, dt
) == MATCH_YES
)
2807 if (match_dt_unit (k
, dt
) != MATCH_YES
)
2810 if (gfc_match_char (')') == MATCH_YES
)
2812 if (gfc_match_char (',') != MATCH_YES
)
2815 m
= match_dt_element (k
, dt
);
2818 if (m
== MATCH_ERROR
)
2821 m
= match_dt_format (dt
);
2824 if (m
== MATCH_ERROR
)
2827 where
= gfc_current_locus
;
2829 m
= gfc_match_name (name
);
2832 gfc_find_symbol (name
, NULL
, 1, &sym
);
2833 if (sym
&& sym
->attr
.flavor
== FL_NAMELIST
)
2836 if (k
== M_READ
&& check_namelist (sym
))
2845 gfc_current_locus
= where
;
2847 goto loop
; /* No matches, try regular elements */
2850 if (gfc_match_char (')') == MATCH_YES
)
2852 if (gfc_match_char (',') != MATCH_YES
)
2858 m
= match_dt_element (k
, dt
);
2861 if (m
== MATCH_ERROR
)
2864 if (gfc_match_char (')') == MATCH_YES
)
2866 if (gfc_match_char (',') != MATCH_YES
)
2872 /* Used in check_io_constraints, where no locus is available. */
2873 spec_end
= gfc_current_locus
;
2875 /* Optional leading comma (non-standard). */
2877 && gfc_match_char (',') == MATCH_YES
2879 && gfc_notify_std (GFC_STD_GNU
, "Extension: Comma before output "
2880 "item list at %C is an extension") == FAILURE
)
2884 if (gfc_match_eos () != MATCH_YES
)
2886 if (comma_flag
&& gfc_match_char (',') != MATCH_YES
)
2888 gfc_error ("Expected comma in I/O list at %C");
2893 m
= match_io_list (k
, &io_code
);
2894 if (m
== MATCH_ERROR
)
2900 /* A full IO statement has been matched. Check the constraints. spec_end is
2901 supplied for cases where no locus is supplied. */
2902 m
= check_io_constraints (k
, dt
, io_code
, &spec_end
);
2904 if (m
== MATCH_ERROR
)
2907 new_st
.op
= (k
== M_READ
) ? EXEC_READ
: EXEC_WRITE
;
2909 new_st
.block
= gfc_get_code ();
2910 new_st
.block
->op
= new_st
.op
;
2911 new_st
.block
->next
= io_code
;
2913 terminate_io (io_code
);
2918 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k
));
2928 gfc_match_read (void)
2930 return match_io (M_READ
);
2934 gfc_match_write (void)
2936 return match_io (M_WRITE
);
2940 gfc_match_print (void)
2944 m
= match_io (M_PRINT
);
2948 if (gfc_pure (NULL
))
2950 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2958 /* Free a gfc_inquire structure. */
2961 gfc_free_inquire (gfc_inquire
* inquire
)
2964 if (inquire
== NULL
)
2967 gfc_free_expr (inquire
->unit
);
2968 gfc_free_expr (inquire
->file
);
2969 gfc_free_expr (inquire
->iomsg
);
2970 gfc_free_expr (inquire
->iostat
);
2971 gfc_free_expr (inquire
->exist
);
2972 gfc_free_expr (inquire
->opened
);
2973 gfc_free_expr (inquire
->number
);
2974 gfc_free_expr (inquire
->named
);
2975 gfc_free_expr (inquire
->name
);
2976 gfc_free_expr (inquire
->access
);
2977 gfc_free_expr (inquire
->sequential
);
2978 gfc_free_expr (inquire
->direct
);
2979 gfc_free_expr (inquire
->form
);
2980 gfc_free_expr (inquire
->formatted
);
2981 gfc_free_expr (inquire
->unformatted
);
2982 gfc_free_expr (inquire
->recl
);
2983 gfc_free_expr (inquire
->nextrec
);
2984 gfc_free_expr (inquire
->blank
);
2985 gfc_free_expr (inquire
->position
);
2986 gfc_free_expr (inquire
->action
);
2987 gfc_free_expr (inquire
->read
);
2988 gfc_free_expr (inquire
->write
);
2989 gfc_free_expr (inquire
->readwrite
);
2990 gfc_free_expr (inquire
->delim
);
2991 gfc_free_expr (inquire
->pad
);
2992 gfc_free_expr (inquire
->iolength
);
2993 gfc_free_expr (inquire
->convert
);
2994 gfc_free_expr (inquire
->strm_pos
);
3000 /* Match an element of an INQUIRE statement. */
3002 #define RETM if (m != MATCH_NO) return m;
3005 match_inquire_element (gfc_inquire
* inquire
)
3009 m
= match_etag (&tag_unit
, &inquire
->unit
);
3010 RETM m
= match_etag (&tag_file
, &inquire
->file
);
3011 RETM m
= match_ltag (&tag_err
, &inquire
->err
);
3012 RETM m
= match_out_tag (&tag_iomsg
, &inquire
->iomsg
);
3013 RETM m
= match_out_tag (&tag_iostat
, &inquire
->iostat
);
3014 RETM m
= match_vtag (&tag_exist
, &inquire
->exist
);
3015 RETM m
= match_vtag (&tag_opened
, &inquire
->opened
);
3016 RETM m
= match_vtag (&tag_named
, &inquire
->named
);
3017 RETM m
= match_vtag (&tag_name
, &inquire
->name
);
3018 RETM m
= match_out_tag (&tag_number
, &inquire
->number
);
3019 RETM m
= match_vtag (&tag_s_access
, &inquire
->access
);
3020 RETM m
= match_vtag (&tag_sequential
, &inquire
->sequential
);
3021 RETM m
= match_vtag (&tag_direct
, &inquire
->direct
);
3022 RETM m
= match_vtag (&tag_s_form
, &inquire
->form
);
3023 RETM m
= match_vtag (&tag_formatted
, &inquire
->formatted
);
3024 RETM m
= match_vtag (&tag_unformatted
, &inquire
->unformatted
);
3025 RETM m
= match_out_tag (&tag_s_recl
, &inquire
->recl
);
3026 RETM m
= match_out_tag (&tag_nextrec
, &inquire
->nextrec
);
3027 RETM m
= match_vtag (&tag_s_blank
, &inquire
->blank
);
3028 RETM m
= match_vtag (&tag_s_position
, &inquire
->position
);
3029 RETM m
= match_vtag (&tag_s_action
, &inquire
->action
);
3030 RETM m
= match_vtag (&tag_read
, &inquire
->read
);
3031 RETM m
= match_vtag (&tag_write
, &inquire
->write
);
3032 RETM m
= match_vtag (&tag_readwrite
, &inquire
->readwrite
);
3033 RETM m
= match_vtag (&tag_s_delim
, &inquire
->delim
);
3034 RETM m
= match_vtag (&tag_s_pad
, &inquire
->pad
);
3035 RETM m
= match_vtag (&tag_iolength
, &inquire
->iolength
);
3036 RETM m
= match_vtag (&tag_convert
, &inquire
->convert
);
3037 RETM m
= match_out_tag (&tag_strm_out
, &inquire
->strm_pos
);
3038 RETM
return MATCH_NO
;
3045 gfc_match_inquire (void)
3047 gfc_inquire
*inquire
;
3052 m
= gfc_match_char ('(');
3056 inquire
= gfc_getmem (sizeof (gfc_inquire
));
3058 loc
= gfc_current_locus
;
3060 m
= match_inquire_element (inquire
);
3061 if (m
== MATCH_ERROR
)
3065 m
= gfc_match_expr (&inquire
->unit
);
3066 if (m
== MATCH_ERROR
)
3072 /* See if we have the IOLENGTH form of the inquire statement. */
3073 if (inquire
->iolength
!= NULL
)
3075 if (gfc_match_char (')') != MATCH_YES
)
3078 m
= match_io_list (M_INQUIRE
, &code
);
3079 if (m
== MATCH_ERROR
)
3084 new_st
.op
= EXEC_IOLENGTH
;
3085 new_st
.expr
= inquire
->iolength
;
3086 new_st
.ext
.inquire
= inquire
;
3088 if (gfc_pure (NULL
))
3090 gfc_free_statements (code
);
3091 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3095 new_st
.block
= gfc_get_code ();
3096 new_st
.block
->op
= EXEC_IOLENGTH
;
3097 terminate_io (code
);
3098 new_st
.block
->next
= code
;
3102 /* At this point, we have the non-IOLENGTH inquire statement. */
3105 if (gfc_match_char (')') == MATCH_YES
)
3107 if (gfc_match_char (',') != MATCH_YES
)
3110 m
= match_inquire_element (inquire
);
3111 if (m
== MATCH_ERROR
)
3116 if (inquire
->iolength
!= NULL
)
3118 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3123 if (gfc_match_eos () != MATCH_YES
)
3126 if (inquire
->unit
!= NULL
&& inquire
->file
!= NULL
)
3128 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
3129 " UNIT specifiers", &loc
);
3133 if (inquire
->unit
== NULL
&& inquire
->file
== NULL
)
3135 gfc_error ("INQUIRE statement at %L requires either FILE or"
3136 " UNIT specifier", &loc
);
3140 if (gfc_pure (NULL
))
3142 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3146 new_st
.op
= EXEC_INQUIRE
;
3147 new_st
.ext
.inquire
= inquire
;
3151 gfc_syntax_error (ST_INQUIRE
);
3154 gfc_free_inquire (inquire
);
3159 /* Resolve everything in a gfc_inquire structure. */
3162 gfc_resolve_inquire (gfc_inquire
* inquire
)
3165 RESOLVE_TAG (&tag_unit
, inquire
->unit
);
3166 RESOLVE_TAG (&tag_file
, inquire
->file
);
3167 RESOLVE_TAG (&tag_iomsg
, inquire
->iomsg
);
3168 RESOLVE_TAG (&tag_iostat
, inquire
->iostat
);
3169 RESOLVE_TAG (&tag_exist
, inquire
->exist
);
3170 RESOLVE_TAG (&tag_opened
, inquire
->opened
);
3171 RESOLVE_TAG (&tag_number
, inquire
->number
);
3172 RESOLVE_TAG (&tag_named
, inquire
->named
);
3173 RESOLVE_TAG (&tag_name
, inquire
->name
);
3174 RESOLVE_TAG (&tag_s_access
, inquire
->access
);
3175 RESOLVE_TAG (&tag_sequential
, inquire
->sequential
);
3176 RESOLVE_TAG (&tag_direct
, inquire
->direct
);
3177 RESOLVE_TAG (&tag_s_form
, inquire
->form
);
3178 RESOLVE_TAG (&tag_formatted
, inquire
->formatted
);
3179 RESOLVE_TAG (&tag_unformatted
, inquire
->unformatted
);
3180 RESOLVE_TAG (&tag_s_recl
, inquire
->recl
);
3181 RESOLVE_TAG (&tag_nextrec
, inquire
->nextrec
);
3182 RESOLVE_TAG (&tag_s_blank
, inquire
->blank
);
3183 RESOLVE_TAG (&tag_s_position
, inquire
->position
);
3184 RESOLVE_TAG (&tag_s_action
, inquire
->action
);
3185 RESOLVE_TAG (&tag_read
, inquire
->read
);
3186 RESOLVE_TAG (&tag_write
, inquire
->write
);
3187 RESOLVE_TAG (&tag_readwrite
, inquire
->readwrite
);
3188 RESOLVE_TAG (&tag_s_delim
, inquire
->delim
);
3189 RESOLVE_TAG (&tag_s_pad
, inquire
->pad
);
3190 RESOLVE_TAG (&tag_iolength
, inquire
->iolength
);
3191 RESOLVE_TAG (&tag_convert
, inquire
->convert
);
3192 RESOLVE_TAG (&tag_strm_out
, inquire
->strm_pos
);
3194 if (gfc_reference_st_label (inquire
->err
, ST_LABEL_TARGET
) == FAILURE
)