[flang] Address review comments

Original-commit: flang-compiler/f18@7bc3c97f1e
Reviewed-on: https://github.com/flang-compiler/f18/pull/416
Tree-same-pre-rewrite: false
This commit is contained in:
peter klausler 2019-04-16 12:28:18 -07:00
parent 1498911aa2
commit bfb65b5476
3 changed files with 82 additions and 54 deletions

View file

@ -135,12 +135,12 @@ std::string DynamicType::AsFortran() const {
} else if (charLength != nullptr) {
std::string result{"CHARACTER(KIND="s + std::to_string(kind) + ",LEN="};
if (charLength->isAssumed()) {
result += ",LEN=*";
result += '*';
} else if (charLength->isDeferred()) {
result += ",LEN=:";
result += ':';
} else if (const auto &length{charLength->GetExplicit()}) {
std::stringstream ss;
length->AsFortran(ss << ",LEN=");
length->AsFortran(ss);
result += ss.str();
}
return result + ')';

76
flang/lib/parser/tools.h Normal file
View file

@ -0,0 +1,76 @@
// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FORTRAN_PARSER_TOOLS_H_
#define FORTRAN_PARSER_TOOLS_H_
#include "parse-tree.h"
namespace Fortran::parser {
// GetLastName() isolates and returns a reference to the rightmost Name
// in a variable (i.e., the Name whose symbol's type determines the type
// of the variable or expression).
const Name &GetLastName(const Name &x) { return x; }
const Name &GetLastName(const StructureComponent &x) {
return GetLastName(x.component);
}
const Name &GetLastName(const DataRef &x) {
return std::visit(
common::visitors{
[](const Name &name) { return GetLastName(name); },
[](const common::Indirection<StructureComponent> &sc) {
return GetLastName(sc.value());
},
[](const common::Indirection<ArrayElement> &sc) {
return GetLastName(sc.value().base);
},
[](const common::Indirection<CoindexedNamedObject> &ci) {
return GetLastName(ci.value().base);
},
},
x.u);
}
const Name &GetLastName(const Substring &x) {
return GetType(std::get<DataRef>(x.t));
}
const Name &GetLastName(const Designator &x) {
return std::visit([](const auto &y) { return GetType(y); }, x.u);
}
const Name &GetLastName(const ProcComponentRef &x) {
return GetType(x.v.thing);
}
const Name &GetLastName(const ProcedureDesignator &x) {
return std::visit([](const auto &y) { return GetType(y); }, x.u);
}
const Name &GetLastName(const Call &x) {
return GetType(std::get<ProcedureDesignator>(x.t));
}
const Name &GetLastName(const FunctionReference &x) { return GetType(x.v); }
const Name &GetLastName(const Variable &x) {
return std::visit(
[](const auto &indirection) { return GetType(indirection.value()); },
x.u);
}
}
#endif // FORTRAN_PARSER_TOOLS_H_

View file

@ -20,6 +20,7 @@
#include "../common/indirection.h"
#include "../parser/parse-tree-visitor.h"
#include "../parser/parse-tree.h"
#include "../parser/tools.h"
#include <list>
namespace Fortran::semantics {
@ -108,59 +109,10 @@ bool RewriteMutator::Pre(parser::ExecutionPart &x) {
return true;
}
static DeclTypeSpec *GetType(const parser::Name &x) {
if (x.symbol != nullptr) {
return x.symbol->GetType();
} else {
return nullptr;
}
}
static DeclTypeSpec *GetType(const parser::StructureComponent &x) {
return GetType(x.component);
}
static DeclTypeSpec *GetType(const parser::DataRef &x) {
return std::visit(
common::visitors{
[](const parser::Name &name) { return GetType(name); },
[](const common::Indirection<parser::StructureComponent> &sc) {
return GetType(sc.value());
},
[](const common::Indirection<parser::ArrayElement> &sc) {
return GetType(sc.value().base);
},
[](const common::Indirection<parser::CoindexedNamedObject> &ci) {
return GetType(ci.value().base);
},
},
x.u);
}
static DeclTypeSpec *GetType(const parser::Substring &x) {
return GetType(std::get<parser::DataRef>(x.t));
}
static DeclTypeSpec *GetType(const parser::Designator &x) {
return std::visit([](const auto &y) { return GetType(y); }, x.u);
}
static DeclTypeSpec *GetType(const parser::ProcComponentRef &x) {
return GetType(x.v.thing);
}
static DeclTypeSpec *GetType(const parser::ProcedureDesignator &x) {
return std::visit([](const auto &y) { return GetType(y); }, x.u);
}
static DeclTypeSpec *GetType(const parser::Call &x) {
return GetType(std::get<parser::ProcedureDesignator>(x.t));
}
static DeclTypeSpec *GetType(const parser::FunctionReference &x) {
return GetType(x.v);
}
static DeclTypeSpec *GetType(const parser::Variable &x) {
return std::visit(
[](const auto &indirection) { return GetType(indirection.value()); },
x.u);
}
void RewriteMutator::Post(parser::IoUnit &x) {
if (auto *var{std::get_if<parser::Variable>(&x.u)}) {
DeclTypeSpec *type{GetType(*var)};
parser::Name &last{parser::GetLastName(*var)};
DeclTypeSpec *type{last.symbol ? last.symbol->GetType() : nullptr};
if (type == nullptr || type->category() != DeclTypeSpec::Character) {
// If the Variable is not known to be character (any kind), transform
// the I/O unit in situ to a FileUnitNumber so that automatic expression