Skip to content

[flang] Better renaming in module files #93106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions flang/include/flang/Evaluate/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ class ConstantBase : public ConstantBounds {
constexpr Result result() const { return result_; }

constexpr DynamicType GetType() const { return result_.GetType(); }
llvm::raw_ostream &AsFortran(llvm::raw_ostream &,
const parser::CharBlock *derivedTypeRename = nullptr) const;
llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;

protected:
std::vector<Element> Reshape(const ConstantSubscripts &) const;
Expand Down
3 changes: 1 addition & 2 deletions flang/include/flang/Evaluate/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,7 @@ class StructureConstructor {
StructureConstructor &Add(const semantics::Symbol &, Expr<SomeType> &&);
int Rank() const { return 0; }
DynamicType GetType() const;
llvm::raw_ostream &AsFortran(llvm::raw_ostream &,
const parser::CharBlock *derivedTypeRename = nullptr) const;
llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;

private:
std::optional<Expr<SomeType>> CreateParentComponent(const Symbol &) const;
Expand Down
3 changes: 0 additions & 3 deletions flang/include/flang/Evaluate/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ const semantics::DerivedTypeSpec *GetDerivedTypeSpec(
const semantics::DerivedTypeSpec *GetParentTypeSpec(
const semantics::DerivedTypeSpec &);

std::string DerivedTypeSpecAsFortran(const semantics::DerivedTypeSpec &,
const parser::CharBlock *derivedTypeRename = nullptr);

template <TypeCategory CATEGORY, int KIND = 0> struct TypeBase {
static constexpr TypeCategory category{CATEGORY};
static constexpr int kind{KIND};
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Semantics/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class SemanticsContext {
evaluate::FoldingContext &foldingContext() { return foldingContext_; }
parser::AllCookedSources &allCookedSources() { return allCookedSources_; }
ModuleDependences &moduleDependences() { return moduleDependences_; }
std::map<const Symbol *, SourceName> &moduleFileOutputRenamings() {
return moduleFileOutputRenamings_;
}

SemanticsContext &set_location(
const std::optional<parser::CharBlock> &location) {
Expand Down Expand Up @@ -299,6 +302,7 @@ class SemanticsContext {
std::list<parser::Program> modFileParseTrees_;
std::unique_ptr<CommonBlockMap> commonBlockMap_;
ModuleDependences moduleDependences_;
std::map<const Symbol *, SourceName> moduleFileOutputRenamings_;
};

class Semantics {
Expand Down
213 changes: 110 additions & 103 deletions flang/lib/Evaluate/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "flang/Evaluate/fold.h"
#include "flang/Evaluate/tools.h"
#include "flang/Parser/characters.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/symbol.h"
#include "llvm/Support/raw_ostream.h"

Expand Down Expand Up @@ -53,7 +54,7 @@ static void ShapeAsFortran(llvm::raw_ostream &o,

template <typename RESULT, typename VALUE>
llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
llvm::raw_ostream &o, const parser::CharBlock *derivedTypeRename) const {
llvm::raw_ostream &o) const {
bool hasNonDefaultLowerBound{printLbounds && HasNonDefaultLowerBound()};
if (Rank() > 1 || hasNonDefaultLowerBound) {
o << "reshape(";
Expand Down Expand Up @@ -85,8 +86,7 @@ llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
o << ".false." << '_' << Result::kind;
}
} else {
StructureConstructor{result_.derivedTypeSpec(), value}.AsFortran(
o, derivedTypeRename);
StructureConstructor{result_.derivedTypeSpec(), value}.AsFortran(o);
}
}
if (Rank() > 0) {
Expand Down Expand Up @@ -124,9 +124,89 @@ llvm::raw_ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
return o;
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const Symbol &symbol,
std::optional<parser::CharBlock> name = std::nullopt) {
const auto &renamings{symbol.owner().context().moduleFileOutputRenamings()};
if (auto iter{renamings.find(&symbol)}; iter != renamings.end()) {
return o << iter->second.ToString();
} else if (name) {
return o << name->ToString();
} else {
return o << symbol.name().ToString();
}
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u16string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u32string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const A &x) {
return x.AsFortran(o);
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, common::Reference<A> x) {
return EmitVar(o, *x);
}

template <typename A>
llvm::raw_ostream &EmitVar(
llvm::raw_ostream &o, const A *p, const char *kw = nullptr) {
if (p) {
if (kw) {
o << kw;
}
EmitVar(o, *p);
}
return o;
}

template <typename A>
llvm::raw_ostream &EmitVar(
llvm::raw_ostream &o, const std::optional<A> &x, const char *kw = nullptr) {
if (x) {
if (kw) {
o << kw;
}
EmitVar(o, *x);
}
return o;
}

template <typename A, bool COPY>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o,
const common::Indirection<A, COPY> &p, const char *kw = nullptr) {
if (kw) {
o << kw;
}
EmitVar(o, p.value());
return o;
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::shared_ptr<A> &p) {
CHECK(p);
return EmitVar(o, *p);
}

template <typename... A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::variant<A...> &u) {
common::visit([&](const auto &x) { EmitVar(o, x); }, u);
return o;
}

llvm::raw_ostream &ActualArgument::AssumedType::AsFortran(
llvm::raw_ostream &o) const {
return o << symbol_->name().ToString();
return EmitVar(o, *symbol_);
}

llvm::raw_ostream &ActualArgument::AsFortran(llvm::raw_ostream &o) const {
Expand Down Expand Up @@ -504,15 +584,37 @@ llvm::raw_ostream &ExpressionBase<RESULT>::AsFortran(
return o;
}

llvm::raw_ostream &StructureConstructor::AsFortran(
llvm::raw_ostream &o, const parser::CharBlock *derivedTypeRename) const {
o << DerivedTypeSpecAsFortran(result_.derivedTypeSpec(), derivedTypeRename);
static std::string DerivedTypeSpecAsFortran(
const semantics::DerivedTypeSpec &spec) {
std::string buf;
llvm::raw_string_ostream ss{buf};
EmitVar(ss, spec.typeSymbol(), spec.name());
char ch{'('};
for (const auto &[name, value] : spec.parameters()) {
ss << ch << name.ToString() << '=';
ch = ',';
if (value.isAssumed()) {
ss << '*';
} else if (value.isDeferred()) {
ss << ':';
} else {
value.GetExplicit()->AsFortran(ss);
}
}
if (ch != '(') {
ss << ')';
}
return ss.str();
}

llvm::raw_ostream &StructureConstructor::AsFortran(llvm::raw_ostream &o) const {
o << DerivedTypeSpecAsFortran(result_.derivedTypeSpec());
if (values_.empty()) {
o << '(';
} else {
char ch{'('};
for (const auto &[symbol, value] : values_) {
value.value().AsFortran(o << ch << symbol->name().ToString() << '=');
value.value().AsFortran(EmitVar(o << ch, *symbol) << '=');
ch = ',';
}
}
Expand Down Expand Up @@ -568,101 +670,6 @@ std::string SomeDerived::AsFortran() const {
}
}

std::string DerivedTypeSpecAsFortran(const semantics::DerivedTypeSpec &spec,
const parser::CharBlock *derivedTypeRename) {
std::string buf;
llvm::raw_string_ostream ss{buf};
ss << (derivedTypeRename ? *derivedTypeRename : spec.name()).ToString();
char ch{'('};
for (const auto &[name, value] : spec.parameters()) {
ss << ch << name.ToString() << '=';
ch = ',';
if (value.isAssumed()) {
ss << '*';
} else if (value.isDeferred()) {
ss << ':';
} else {
value.GetExplicit()->AsFortran(ss);
}
}
if (ch != '(') {
ss << ')';
}
return ss.str();
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const Symbol &symbol) {
return o << symbol.name().ToString();
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u16string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::u32string &lit) {
return o << parser::QuoteCharacterLiteral(lit);
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const A &x) {
return x.AsFortran(o);
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, common::Reference<A> x) {
return EmitVar(o, *x);
}

template <typename A>
llvm::raw_ostream &EmitVar(
llvm::raw_ostream &o, const A *p, const char *kw = nullptr) {
if (p) {
if (kw) {
o << kw;
}
EmitVar(o, *p);
}
return o;
}

template <typename A>
llvm::raw_ostream &EmitVar(
llvm::raw_ostream &o, const std::optional<A> &x, const char *kw = nullptr) {
if (x) {
if (kw) {
o << kw;
}
EmitVar(o, *x);
}
return o;
}

template <typename A, bool COPY>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o,
const common::Indirection<A, COPY> &p, const char *kw = nullptr) {
if (kw) {
o << kw;
}
EmitVar(o, p.value());
return o;
}

template <typename A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::shared_ptr<A> &p) {
CHECK(p);
return EmitVar(o, *p);
}

template <typename... A>
llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const std::variant<A...> &u) {
common::visit([&](const auto &x) { EmitVar(o, x); }, u);
return o;
}

llvm::raw_ostream &BaseObject::AsFortran(llvm::raw_ostream &o) const {
return EmitVar(o, u);
}
Expand Down
Loading
Loading