99#include " check-io.h"
1010#include " definable.h"
1111#include " flang/Common/format.h"
12+ #include " flang/Common/indirection.h"
1213#include " flang/Evaluate/tools.h"
14+ #include " flang/Parser/characters.h"
1315#include " flang/Parser/tools.h"
1416#include " flang/Semantics/expression.h"
1517#include " flang/Semantics/tools.h"
@@ -576,8 +578,9 @@ void IoChecker::Enter(const parser::IoUnit &spec) {
576578 std::move (mutableVar.u ))};
577579 newExpr.source = source;
578580 newExpr.typedExpr = std::move (typedExpr);
579- mutableSpec.u = parser::FileUnitNumber{
580- parser::ScalarIntExpr{parser::IntExpr{std::move (newExpr)}}};
581+ mutableSpec.u = common::Indirection<parser::Expr>{std::move (newExpr)};
582+ SetSpecifier (IoSpecKind::Unit);
583+ flags_.set (Flag::NumberUnit);
581584 } else if (!dyType || dyType->category () != TypeCategory::Character) {
582585 SetSpecifier (IoSpecKind::Unit);
583586 context_.Say (parser::FindSourceLocation (*var),
@@ -598,6 +601,26 @@ void IoChecker::Enter(const parser::IoUnit &spec) {
598601 } else if (std::get_if<parser::Star>(&spec.u )) {
599602 SetSpecifier (IoSpecKind::Unit);
600603 flags_.set (Flag::StarUnit);
604+ } else if (const common::Indirection<parser::Expr> *pexpr{
605+ std::get_if<common::Indirection<parser::Expr>>(&spec.u )}) {
606+ const auto *expr{GetExpr (context_, *pexpr)};
607+ std::optional<evaluate::DynamicType> dyType;
608+ if (expr) {
609+ dyType = expr->GetType ();
610+ }
611+ if (!expr || !dyType) {
612+ context_.Say (parser::FindSourceLocation (*pexpr),
613+ " I/O unit must be a character variable or scalar integer expression" _err_en_US);
614+ } else if (dyType->category () != TypeCategory::Integer) {
615+ context_.Say (parser::FindSourceLocation (*pexpr),
616+ " I/O unit must be a character variable or a scalar integer expression, but is an expression of type %s" _err_en_US,
617+ parser::ToUpperCaseLetters (dyType->AsFortran ()));
618+ } else if (expr->Rank () != 0 ) {
619+ context_.Say (parser::FindSourceLocation (*pexpr),
620+ " I/O unit number must be scalar" _err_en_US);
621+ }
622+ SetSpecifier (IoSpecKind::Unit);
623+ flags_.set (Flag::NumberUnit);
601624 }
602625}
603626
0 commit comments