2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Semantics/mod-file.cpp ----------------------------------------===//
|
2018-07-17 07:23:18 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-07-17 07:23:18 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-17 07:23:18 +08:00
|
|
|
|
|
|
|
#include "mod-file.h"
|
2019-09-24 08:10:58 +08:00
|
|
|
#include "resolve-names.h"
|
2020-07-14 03:19:17 +08:00
|
|
|
#include "flang/Common/restorer.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Evaluate/tools.h"
|
|
|
|
#include "flang/Parser/message.h"
|
|
|
|
#include "flang/Parser/parsing.h"
|
|
|
|
#include "flang/Semantics/scope.h"
|
|
|
|
#include "flang/Semantics/semantics.h"
|
|
|
|
#include "flang/Semantics/symbol.h"
|
|
|
|
#include "flang/Semantics/tools.h"
|
2020-02-25 23:59:50 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-07-17 07:23:18 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
2019-09-24 08:10:58 +08:00
|
|
|
#include <set>
|
|
|
|
#include <string_view>
|
2018-07-17 07:23:18 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Fortran::semantics {
|
|
|
|
|
|
|
|
using namespace parser::literals;
|
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
// The first line of a file that identifies it as a .mod file.
|
2019-06-13 06:26:37 +08:00
|
|
|
// The first three bytes are a Unicode byte order mark that ensures
|
|
|
|
// that the module file is decoded as UTF-8 even if source files
|
|
|
|
// are using another encoding.
|
2019-09-24 08:10:58 +08:00
|
|
|
struct ModHeader {
|
2019-09-17 07:58:13 +08:00
|
|
|
static constexpr const char bom[3 + 1]{"\xef\xbb\xbf"};
|
2019-09-26 06:39:44 +08:00
|
|
|
static constexpr int magicLen{13};
|
2019-09-24 08:10:58 +08:00
|
|
|
static constexpr int sumLen{16};
|
2019-09-17 07:58:13 +08:00
|
|
|
static constexpr const char magic[magicLen + 1]{"!mod$ v1 sum:"};
|
2019-09-24 08:10:58 +08:00
|
|
|
static constexpr char terminator{'\n'};
|
|
|
|
static constexpr int len{magicLen + 1 + sumLen};
|
|
|
|
};
|
2018-08-03 07:21:27 +08:00
|
|
|
|
2019-08-22 16:12:20 +08:00
|
|
|
static std::optional<SourceName> GetSubmoduleParent(const parser::Program &);
|
2020-12-03 07:13:49 +08:00
|
|
|
static void CollectSymbols(const Scope &, SymbolVector &, SymbolVector &);
|
2020-02-28 23:11:03 +08:00
|
|
|
static void PutEntity(llvm::raw_ostream &, const Symbol &);
|
|
|
|
static void PutObjectEntity(llvm::raw_ostream &, const Symbol &);
|
|
|
|
static void PutProcEntity(llvm::raw_ostream &, const Symbol &);
|
|
|
|
static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &);
|
|
|
|
static void PutTypeParam(llvm::raw_ostream &, const Symbol &);
|
2019-09-17 07:58:13 +08:00
|
|
|
static void PutEntity(
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs);
|
|
|
|
static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &);
|
|
|
|
static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &);
|
|
|
|
static void PutBound(llvm::raw_ostream &, const Bound &);
|
|
|
|
static llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs,
|
2019-02-16 02:16:25 +08:00
|
|
|
const MaybeExpr & = std::nullopt, std::string before = ","s,
|
|
|
|
std::string after = ""s);
|
2020-02-28 23:11:03 +08:00
|
|
|
|
|
|
|
static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr);
|
|
|
|
static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &);
|
|
|
|
static llvm::raw_ostream &PutLower(llvm::raw_ostream &, const std::string &);
|
2020-02-25 23:59:50 +08:00
|
|
|
static std::error_code WriteFile(
|
|
|
|
const std::string &, const std::string &, bool = true);
|
2018-08-09 02:36:24 +08:00
|
|
|
static bool FileContentsMatch(
|
2019-09-24 08:10:58 +08:00
|
|
|
const std::string &, const std::string &, const std::string &);
|
|
|
|
static std::string CheckSum(const std::string_view &);
|
2018-07-17 07:23:18 +08:00
|
|
|
|
2019-03-30 06:04:17 +08:00
|
|
|
// Collect symbols needed for a subprogram interface
|
|
|
|
class SubprogramSymbolCollector {
|
|
|
|
public:
|
2020-03-20 07:31:10 +08:00
|
|
|
SubprogramSymbolCollector(const Symbol &symbol, const Scope &scope)
|
2020-03-29 12:00:16 +08:00
|
|
|
: symbol_{symbol}, scope_{scope} {}
|
2019-09-13 04:43:16 +08:00
|
|
|
const SymbolVector &symbols() const { return need_; }
|
|
|
|
const std::set<SourceName> &imports() const { return imports_; }
|
2019-03-30 06:04:17 +08:00
|
|
|
void Collect();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Symbol &symbol_;
|
|
|
|
const Scope &scope_;
|
|
|
|
bool isInterface_{false};
|
2020-03-29 12:00:16 +08:00
|
|
|
SymbolVector need_; // symbols that are needed
|
|
|
|
SymbolSet needSet_; // symbols already in need_
|
|
|
|
SymbolSet useSet_; // use-associations that might be needed
|
|
|
|
std::set<SourceName> imports_; // imports from host that are needed
|
2019-03-30 06:04:17 +08:00
|
|
|
|
|
|
|
void DoSymbol(const Symbol &);
|
2019-09-13 04:43:16 +08:00
|
|
|
void DoSymbol(const SourceName &, const Symbol &);
|
2019-03-30 06:04:17 +08:00
|
|
|
void DoType(const DeclTypeSpec *);
|
|
|
|
void DoBound(const Bound &);
|
|
|
|
void DoParamValue(const ParamValue &);
|
2019-09-13 04:43:16 +08:00
|
|
|
bool NeedImport(const SourceName &, const Symbol &);
|
2019-03-30 06:04:17 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T> void DoExpr(evaluate::Expr<T> expr) {
|
2019-10-23 07:53:29 +08:00
|
|
|
for (const Symbol &symbol : evaluate::CollectSymbols(expr)) {
|
|
|
|
DoSymbol(symbol);
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-07 09:07:25 +08:00
|
|
|
bool ModFileWriter::WriteAll() {
|
2020-07-14 03:19:17 +08:00
|
|
|
// this flag affects character literals: force it to be consistent
|
|
|
|
auto restorer{
|
|
|
|
common::ScopedSet(parser::useHexadecimalEscapeSequences, false)};
|
2019-03-07 09:07:25 +08:00
|
|
|
WriteAll(context_.globalScope());
|
|
|
|
return !context_.AnyFatalError();
|
|
|
|
}
|
2018-10-22 22:37:38 +08:00
|
|
|
|
|
|
|
void ModFileWriter::WriteAll(const Scope &scope) {
|
2018-08-03 07:21:27 +08:00
|
|
|
for (const auto &child : scope.children()) {
|
|
|
|
WriteOne(child);
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModFileWriter::WriteOne(const Scope &scope) {
|
2018-08-04 02:32:21 +08:00
|
|
|
if (scope.kind() == Scope::Kind::Module) {
|
|
|
|
auto *symbol{scope.symbol()};
|
|
|
|
if (!symbol->test(Symbol::Flag::ModFile)) {
|
|
|
|
Write(*symbol);
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
WriteAll(scope); // write out submodules
|
2018-08-04 02:32:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
// Construct the name of a module file. Non-empty ancestorName means submodule.
|
|
|
|
static std::string ModFileName(const SourceName &name,
|
|
|
|
const std::string &ancestorName, const std::string &suffix) {
|
|
|
|
std::string result{name.ToString() + suffix};
|
|
|
|
return ancestorName.empty() ? result : ancestorName + '-' + result;
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:32:21 +08:00
|
|
|
// Write the module file for symbol, which must be a module or submodule.
|
|
|
|
void ModFileWriter::Write(const Symbol &symbol) {
|
|
|
|
auto *ancestor{symbol.get<ModuleDetails>().ancestor()};
|
2019-08-21 21:29:11 +08:00
|
|
|
auto ancestorName{ancestor ? ancestor->GetName().value().ToString() : ""s};
|
2019-09-24 08:10:58 +08:00
|
|
|
auto path{context_.moduleDirectory() + '/' +
|
|
|
|
ModFileName(symbol.name(), ancestorName, context_.moduleFileSuffix())};
|
2019-10-23 07:53:29 +08:00
|
|
|
PutSymbols(DEREF(symbol.scope()));
|
2020-02-25 23:59:50 +08:00
|
|
|
if (std::error_code error{
|
|
|
|
WriteFile(path, GetAsString(symbol), context_.debugModuleWriter())}) {
|
|
|
|
context_.Say(
|
|
|
|
symbol.name(), "Error writing %s: %s"_err_en_US, path, error.message());
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the entire body of the module file
|
|
|
|
// and clear saved uses, decls, and contains.
|
2018-08-03 07:21:27 +08:00
|
|
|
std::string ModFileWriter::GetAsString(const Symbol &symbol) {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream all{buf};
|
2018-08-03 07:21:27 +08:00
|
|
|
auto &details{symbol.get<ModuleDetails>()};
|
|
|
|
if (!details.isSubmodule()) {
|
2019-09-12 09:21:07 +08:00
|
|
|
all << "module " << symbol.name();
|
2018-08-03 07:21:27 +08:00
|
|
|
} else {
|
|
|
|
auto *parent{details.parent()->symbol()};
|
|
|
|
auto *ancestor{details.ancestor()->symbol()};
|
2019-09-12 09:21:07 +08:00
|
|
|
all << "submodule(" << ancestor->name();
|
2018-08-03 07:21:27 +08:00
|
|
|
if (parent != ancestor) {
|
2019-09-12 09:21:07 +08:00
|
|
|
all << ':' << parent->name();
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
2019-09-12 09:21:07 +08:00
|
|
|
all << ") " << symbol.name();
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
|
|
|
all << '\n' << uses_.str();
|
2020-02-28 23:11:03 +08:00
|
|
|
uses_.str().clear();
|
2018-07-17 21:39:38 +08:00
|
|
|
all << useExtraAttrs_.str();
|
2020-02-28 23:11:03 +08:00
|
|
|
useExtraAttrs_.str().clear();
|
2018-07-17 07:23:18 +08:00
|
|
|
all << decls_.str();
|
2020-02-28 23:11:03 +08:00
|
|
|
decls_.str().clear();
|
2018-07-17 07:23:18 +08:00
|
|
|
auto str{contains_.str()};
|
2020-02-28 23:11:03 +08:00
|
|
|
contains_.str().clear();
|
2018-07-17 07:23:18 +08:00
|
|
|
if (!str.empty()) {
|
|
|
|
all << "contains\n" << str;
|
|
|
|
}
|
|
|
|
all << "end\n";
|
|
|
|
return all.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put out the visible symbols from scope.
|
2020-10-01 04:34:23 +08:00
|
|
|
bool ModFileWriter::PutSymbols(const Scope &scope) {
|
2020-12-03 07:13:49 +08:00
|
|
|
SymbolVector sorted;
|
|
|
|
SymbolVector uses;
|
|
|
|
CollectSymbols(scope, sorted, uses);
|
|
|
|
std::string buf; // stuff after CONTAINS in derived type
|
|
|
|
llvm::raw_string_ostream typeBindings{buf};
|
|
|
|
for (const Symbol &symbol : sorted) {
|
2019-02-27 05:08:59 +08:00
|
|
|
PutSymbol(typeBindings, symbol);
|
2018-12-27 05:31:13 +08:00
|
|
|
}
|
2020-12-03 07:13:49 +08:00
|
|
|
for (const Symbol &symbol : uses) {
|
|
|
|
PutUse(symbol);
|
|
|
|
}
|
2018-12-27 05:31:13 +08:00
|
|
|
if (auto str{typeBindings.str()}; !str.empty()) {
|
2019-07-12 00:27:25 +08:00
|
|
|
CHECK(scope.IsDerivedType());
|
2018-12-27 05:31:13 +08:00
|
|
|
decls_ << "contains\n" << str;
|
2020-10-01 04:34:23 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:12:23 +08:00
|
|
|
static llvm::raw_ostream &PutGenericName(
|
|
|
|
llvm::raw_ostream &os, const Symbol &symbol) {
|
|
|
|
if (IsGenericDefinedOp(symbol)) {
|
|
|
|
return os << "operator(" << symbol.name() << ')';
|
|
|
|
} else {
|
|
|
|
return os << symbol.name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:31:13 +08:00
|
|
|
// Emit a symbol to decls_, except for bindings in a derived type (type-bound
|
|
|
|
// procedures, type-bound generics, final procedures) which go to typeBindings.
|
|
|
|
void ModFileWriter::PutSymbol(
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &typeBindings, const Symbol &symbol) {
|
2020-03-29 12:00:16 +08:00
|
|
|
std::visit(common::visitors{
|
|
|
|
[&](const ModuleDetails &) { /* should be current module */ },
|
|
|
|
[&](const DerivedTypeDetails &) { PutDerivedType(symbol); },
|
|
|
|
[&](const SubprogramDetails &) { PutSubprogram(symbol); },
|
|
|
|
[&](const GenericDetails &x) {
|
|
|
|
if (symbol.owner().IsDerivedType()) {
|
|
|
|
// generic binding
|
|
|
|
for (const Symbol &proc : x.specificProcs()) {
|
2021-01-14 06:12:23 +08:00
|
|
|
PutGenericName(typeBindings << "generic::", symbol)
|
|
|
|
<< "=>" << proc.name() << '\n';
|
2020-03-29 12:00:16 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PutGeneric(symbol);
|
|
|
|
if (x.specific()) {
|
|
|
|
PutSymbol(typeBindings, *x.specific());
|
|
|
|
}
|
|
|
|
if (x.derivedType()) {
|
|
|
|
PutSymbol(typeBindings, *x.derivedType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](const UseDetails &) { PutUse(symbol); },
|
|
|
|
[](const UseErrorDetails &) {},
|
|
|
|
[&](const ProcBindingDetails &x) {
|
|
|
|
bool deferred{symbol.attrs().test(Attr::DEFERRED)};
|
|
|
|
typeBindings << "procedure";
|
|
|
|
if (deferred) {
|
|
|
|
typeBindings << '(' << x.symbol().name() << ')';
|
|
|
|
}
|
|
|
|
PutPassName(typeBindings, x.passName());
|
|
|
|
auto attrs{symbol.attrs()};
|
|
|
|
if (x.passName()) {
|
|
|
|
attrs.reset(Attr::PASS);
|
|
|
|
}
|
|
|
|
PutAttrs(typeBindings, attrs);
|
|
|
|
typeBindings << "::" << symbol.name();
|
|
|
|
if (!deferred && x.symbol().name() != symbol.name()) {
|
|
|
|
typeBindings << "=>" << x.symbol().name();
|
|
|
|
}
|
|
|
|
typeBindings << '\n';
|
|
|
|
},
|
|
|
|
[&](const NamelistDetails &x) {
|
|
|
|
decls_ << "namelist/" << symbol.name();
|
|
|
|
char sep{'/'};
|
|
|
|
for (const Symbol &object : x.objects()) {
|
|
|
|
decls_ << sep << object.name();
|
|
|
|
sep = ',';
|
|
|
|
}
|
|
|
|
decls_ << '\n';
|
|
|
|
},
|
|
|
|
[&](const CommonBlockDetails &x) {
|
|
|
|
decls_ << "common/" << symbol.name();
|
|
|
|
char sep = '/';
|
2020-05-07 03:20:07 +08:00
|
|
|
for (const auto &object : x.objects()) {
|
|
|
|
decls_ << sep << object->name();
|
2020-03-29 12:00:16 +08:00
|
|
|
sep = ',';
|
|
|
|
}
|
|
|
|
decls_ << '\n';
|
|
|
|
if (symbol.attrs().test(Attr::BIND_C)) {
|
|
|
|
PutAttrs(decls_, symbol.attrs(), x.bindName(), ""s);
|
|
|
|
decls_ << "::/" << symbol.name() << "/\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[](const HostAssocDetails &) {},
|
|
|
|
[](const MiscDetails &) {},
|
|
|
|
[&](const auto &) { PutEntity(decls_, symbol); },
|
|
|
|
},
|
2019-10-23 07:53:29 +08:00
|
|
|
symbol.details());
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModFileWriter::PutDerivedType(const Symbol &typeSymbol) {
|
2018-09-06 23:01:49 +08:00
|
|
|
auto &details{typeSymbol.get<DerivedTypeDetails>()};
|
2019-02-16 02:16:25 +08:00
|
|
|
PutAttrs(decls_ << "type", typeSymbol.attrs());
|
2019-02-06 02:48:26 +08:00
|
|
|
if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) {
|
2019-09-13 04:43:16 +08:00
|
|
|
decls_ << ",extends(" << extends->name() << ')';
|
2018-09-06 23:01:49 +08:00
|
|
|
}
|
2019-09-12 09:21:07 +08:00
|
|
|
decls_ << "::" << typeSymbol.name();
|
2018-09-05 01:28:27 +08:00
|
|
|
auto &typeScope{*typeSymbol.scope()};
|
2018-12-07 09:52:43 +08:00
|
|
|
if (!details.paramNames().empty()) {
|
2019-09-12 09:21:07 +08:00
|
|
|
char sep{'('};
|
2018-12-07 09:52:43 +08:00
|
|
|
for (const auto &name : details.paramNames()) {
|
2019-09-12 09:21:07 +08:00
|
|
|
decls_ << sep << name;
|
|
|
|
sep = ',';
|
2018-09-05 01:28:27 +08:00
|
|
|
}
|
|
|
|
decls_ << ')';
|
|
|
|
}
|
|
|
|
decls_ << '\n';
|
2018-09-06 23:01:49 +08:00
|
|
|
if (details.sequence()) {
|
|
|
|
decls_ << "sequence\n";
|
|
|
|
}
|
2020-10-01 04:34:23 +08:00
|
|
|
bool contains{PutSymbols(typeScope)};
|
|
|
|
if (!details.finals().empty()) {
|
|
|
|
const char *sep{contains ? "final::" : "contains\nfinal::"};
|
|
|
|
for (const auto &pair : details.finals()) {
|
|
|
|
decls_ << sep << pair.second->name();
|
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
if (*sep == ',') {
|
|
|
|
decls_ << '\n';
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
decls_ << "end type\n";
|
|
|
|
}
|
|
|
|
|
2019-08-21 05:49:37 +08:00
|
|
|
// Attributes that may be in a subprogram prefix
|
|
|
|
static const Attrs subprogramPrefixAttrs{Attr::ELEMENTAL, Attr::IMPURE,
|
|
|
|
Attr::MODULE, Attr::NON_RECURSIVE, Attr::PURE, Attr::RECURSIVE};
|
|
|
|
|
2018-07-17 07:23:18 +08:00
|
|
|
void ModFileWriter::PutSubprogram(const Symbol &symbol) {
|
|
|
|
auto attrs{symbol.attrs()};
|
2018-10-26 22:34:50 +08:00
|
|
|
auto &details{symbol.get<SubprogramDetails>()};
|
2018-07-17 07:23:18 +08:00
|
|
|
Attrs bindAttrs{};
|
|
|
|
if (attrs.test(Attr::BIND_C)) {
|
|
|
|
// bind(c) is a suffix, not prefix
|
|
|
|
bindAttrs.set(Attr::BIND_C, true);
|
|
|
|
attrs.set(Attr::BIND_C, false);
|
|
|
|
}
|
2020-12-29 00:50:30 +08:00
|
|
|
bool isAbstract{attrs.test(Attr::ABSTRACT)};
|
|
|
|
if (isAbstract) {
|
|
|
|
attrs.set(Attr::ABSTRACT, false);
|
|
|
|
}
|
2019-08-23 23:28:20 +08:00
|
|
|
Attrs prefixAttrs{subprogramPrefixAttrs & attrs};
|
2019-08-21 05:49:37 +08:00
|
|
|
// emit any non-prefix attributes in an attribute statement
|
|
|
|
attrs &= ~subprogramPrefixAttrs;
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string ssBuf;
|
|
|
|
llvm::raw_string_ostream ss{ssBuf};
|
2019-08-21 05:49:37 +08:00
|
|
|
PutAttrs(ss, attrs);
|
|
|
|
if (!ss.str().empty()) {
|
2019-09-12 09:21:07 +08:00
|
|
|
decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n';
|
2019-06-25 03:35:17 +08:00
|
|
|
}
|
2018-10-26 22:34:50 +08:00
|
|
|
bool isInterface{details.isInterface()};
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &os{isInterface ? decls_ : contains_};
|
2018-10-26 22:34:50 +08:00
|
|
|
if (isInterface) {
|
2020-12-29 00:50:30 +08:00
|
|
|
os << (isAbstract ? "abstract " : "") << "interface\n";
|
2018-07-20 04:28:24 +08:00
|
|
|
}
|
2019-08-21 05:49:37 +08:00
|
|
|
PutAttrs(os, prefixAttrs, std::nullopt, ""s, " "s);
|
2018-07-20 04:28:24 +08:00
|
|
|
os << (details.isFunction() ? "function " : "subroutine ");
|
2019-09-12 09:21:07 +08:00
|
|
|
os << symbol.name() << '(';
|
2018-07-17 07:23:18 +08:00
|
|
|
int n = 0;
|
|
|
|
for (const auto &dummy : details.dummyArgs()) {
|
2019-03-30 06:04:17 +08:00
|
|
|
if (n++ > 0) {
|
|
|
|
os << ',';
|
|
|
|
}
|
2020-06-18 22:05:08 +08:00
|
|
|
if (dummy) {
|
|
|
|
os << dummy->name();
|
|
|
|
} else {
|
|
|
|
os << "*";
|
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
2018-07-20 04:28:24 +08:00
|
|
|
os << ')';
|
2019-02-16 02:16:25 +08:00
|
|
|
PutAttrs(os, bindAttrs, details.bindName(), " "s, ""s);
|
2018-07-17 07:23:18 +08:00
|
|
|
if (details.isFunction()) {
|
|
|
|
const Symbol &result{details.result()};
|
|
|
|
if (result.name() != symbol.name()) {
|
2019-09-12 09:21:07 +08:00
|
|
|
os << " result(" << result.name() << ')';
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-30 06:04:17 +08:00
|
|
|
os << '\n';
|
|
|
|
|
2020-03-20 07:31:10 +08:00
|
|
|
// walk symbols, collect ones needed for interface
|
|
|
|
const Scope &scope{
|
|
|
|
details.entryScope() ? *details.entryScope() : DEREF(symbol.scope())};
|
|
|
|
SubprogramSymbolCollector collector{symbol, scope};
|
|
|
|
collector.Collect();
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string typeBindingsBuf;
|
|
|
|
llvm::raw_string_ostream typeBindings{typeBindingsBuf};
|
2020-03-20 07:31:10 +08:00
|
|
|
ModFileWriter writer{context_};
|
2019-10-23 07:53:29 +08:00
|
|
|
for (const Symbol &need : collector.symbols()) {
|
2019-03-30 06:04:17 +08:00
|
|
|
writer.PutSymbol(typeBindings, need);
|
2018-07-20 04:28:24 +08:00
|
|
|
}
|
2019-03-30 06:04:17 +08:00
|
|
|
CHECK(typeBindings.str().empty());
|
|
|
|
os << writer.uses_.str();
|
2019-09-13 04:43:16 +08:00
|
|
|
for (const SourceName &import : collector.imports()) {
|
|
|
|
decls_ << "import::" << import << "\n";
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
|
|
|
os << writer.decls_.str();
|
2018-07-20 04:28:24 +08:00
|
|
|
os << "end\n";
|
2018-10-26 22:34:50 +08:00
|
|
|
if (isInterface) {
|
2018-07-20 04:28:24 +08:00
|
|
|
os << "end interface\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[flang] Handle alternative names for relational operators
10.1.6.2 says:
> The operators <, <=, >, >=, ==, and /= always have the same interpretations
> as the operators .LT., .LE., .GT., .GE., .EQ., and .NE., respectively.
That means we have to treat `operator(<)` like `operator(.lt.)`,
for example. `<>` is a third alias for `.NE.`.
We can't just choose always to use one form (e.g. replacing `operator(.lt.)`
with `operator(<)`). This is because all symbols names are `CharBlock`s
referring to the cooked character stream so that they have proper source
provenance. Also, if a user prefers one style and uses it consistently,
that's the form they should see in messages.
So the fix is to use whatever form is found in the source, but also to
look up symbols by the other names when necessary. To assist this, add
`GenericSpecInfo::GetAllNames()` to return all of the names of a generic
spec. Each place a generic spec can occur we have to use these to look
for the symbol.
Also reorganize the `AddUse()` overloads to work with this change.
Fixes flang-compiler/f18#746.
Original-commit: flang-compiler/f18@7f06f175d5033f0728f67b1be25ecd53df1f8de5
Reviewed-on: https://github.com/flang-compiler/f18/pull/752
2019-09-18 07:57:09 +08:00
|
|
|
static bool IsIntrinsicOp(const Symbol &symbol) {
|
|
|
|
if (const auto *details{symbol.GetUltimate().detailsIf<GenericDetails>()}) {
|
2019-11-23 04:40:37 +08:00
|
|
|
return details->kind().IsIntrinsicOperator();
|
[flang] Handle alternative names for relational operators
10.1.6.2 says:
> The operators <, <=, >, >=, ==, and /= always have the same interpretations
> as the operators .LT., .LE., .GT., .GE., .EQ., and .NE., respectively.
That means we have to treat `operator(<)` like `operator(.lt.)`,
for example. `<>` is a third alias for `.NE.`.
We can't just choose always to use one form (e.g. replacing `operator(.lt.)`
with `operator(<)`). This is because all symbols names are `CharBlock`s
referring to the cooked character stream so that they have proper source
provenance. Also, if a user prefers one style and uses it consistently,
that's the form they should see in messages.
So the fix is to use whatever form is found in the source, but also to
look up symbols by the other names when necessary. To assist this, add
`GenericSpecInfo::GetAllNames()` to return all of the names of a generic
spec. Each place a generic spec can occur we have to use these to look
for the symbol.
Also reorganize the `AddUse()` overloads to work with this change.
Fixes flang-compiler/f18#746.
Original-commit: flang-compiler/f18@7f06f175d5033f0728f67b1be25ecd53df1f8de5
Reviewed-on: https://github.com/flang-compiler/f18/pull/752
2019-09-18 07:57:09 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 06:53:46 +08:00
|
|
|
void ModFileWriter::PutGeneric(const Symbol &symbol) {
|
2020-12-03 07:13:49 +08:00
|
|
|
const auto &genericOwner{symbol.owner()};
|
2019-08-01 06:53:46 +08:00
|
|
|
auto &details{symbol.get<GenericDetails>()};
|
|
|
|
PutGenericName(decls_ << "interface ", symbol) << '\n';
|
2019-10-23 07:53:29 +08:00
|
|
|
for (const Symbol &specific : details.specificProcs()) {
|
2020-12-03 07:13:49 +08:00
|
|
|
if (specific.owner() == genericOwner) {
|
|
|
|
decls_ << "procedure::" << specific.name() << '\n';
|
|
|
|
}
|
2019-08-01 06:53:46 +08:00
|
|
|
}
|
|
|
|
decls_ << "end interface\n";
|
|
|
|
if (symbol.attrs().test(Attr::PRIVATE)) {
|
|
|
|
PutGenericName(decls_ << "private::", symbol) << '\n';
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModFileWriter::PutUse(const Symbol &symbol) {
|
|
|
|
auto &details{symbol.get<UseDetails>()};
|
2018-07-17 21:39:38 +08:00
|
|
|
auto &use{details.symbol()};
|
[flang][NFC] Remove link-time dependency of Evaluate on Semantics
Summary:
Some Symbol-related functions used in Evaluate were moved to
Evaluate/tools.h. This includes changing some member functions that were
replaced by non-member functions `IsDummy`, `GetUsedModule`, and
`CountLenParameters`.
Some member functions were made inline in `Scope`, `Symbol`,
`ArraySpec`, and `DeclTypeSpec`. The definitions were preceded by a
comment explaining why they are inline.
`IsConstantShape` was expanded inline in `IsDescriptor` because it isn't
used anywhere else
After this change, at least when compiling with clang on macos,
`libFortranEvaluate.a` has no undefined symbols that are satisfied by
`libFortranSemantics.a`.
Reviewers: klausler, PeteSteinfeld, sscalpone, jdoerfert, DavidTruby
Reviewed By: PeteSteinfeld
Subscribers: llvm-commits
Tags: #flang, #llvm
Differential Revision: https://reviews.llvm.org/D80762
2020-05-30 07:39:13 +08:00
|
|
|
uses_ << "use " << GetUsedModule(details).name();
|
2019-08-21 03:05:44 +08:00
|
|
|
PutGenericName(uses_ << ",only:", symbol);
|
[flang] Handle alternative names for relational operators
10.1.6.2 says:
> The operators <, <=, >, >=, ==, and /= always have the same interpretations
> as the operators .LT., .LE., .GT., .GE., .EQ., and .NE., respectively.
That means we have to treat `operator(<)` like `operator(.lt.)`,
for example. `<>` is a third alias for `.NE.`.
We can't just choose always to use one form (e.g. replacing `operator(.lt.)`
with `operator(<)`). This is because all symbols names are `CharBlock`s
referring to the cooked character stream so that they have proper source
provenance. Also, if a user prefers one style and uses it consistently,
that's the form they should see in messages.
So the fix is to use whatever form is found in the source, but also to
look up symbols by the other names when necessary. To assist this, add
`GenericSpecInfo::GetAllNames()` to return all of the names of a generic
spec. Each place a generic spec can occur we have to use these to look
for the symbol.
Also reorganize the `AddUse()` overloads to work with this change.
Fixes flang-compiler/f18#746.
Original-commit: flang-compiler/f18@7f06f175d5033f0728f67b1be25ecd53df1f8de5
Reviewed-on: https://github.com/flang-compiler/f18/pull/752
2019-09-18 07:57:09 +08:00
|
|
|
// Can have intrinsic op with different local-name and use-name
|
|
|
|
// (e.g. `operator(<)` and `operator(.lt.)`) but rename is not allowed
|
|
|
|
if (!IsIntrinsicOp(symbol) && use.name() != symbol.name()) {
|
2019-08-21 03:05:44 +08:00
|
|
|
PutGenericName(uses_ << "=>", use);
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
uses_ << '\n';
|
2018-07-17 21:39:38 +08:00
|
|
|
PutUseExtraAttr(Attr::VOLATILE, symbol, use);
|
|
|
|
PutUseExtraAttr(Attr::ASYNCHRONOUS, symbol, use);
|
2021-01-14 06:12:23 +08:00
|
|
|
if (symbol.attrs().test(Attr::PRIVATE)) {
|
|
|
|
PutGenericName(useExtraAttrs_ << "private::", symbol) << '\n';
|
|
|
|
}
|
2018-07-17 21:39:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have "USE local => use" in this module. If attr was added locally
|
|
|
|
// (i.e. on local but not on use), also write it out in the mod file.
|
2018-07-17 22:02:30 +08:00
|
|
|
void ModFileWriter::PutUseExtraAttr(
|
|
|
|
Attr attr, const Symbol &local, const Symbol &use) {
|
2018-07-17 21:39:38 +08:00
|
|
|
if (local.attrs().test(attr) && !use.attrs().test(attr)) {
|
2019-06-25 03:35:17 +08:00
|
|
|
PutAttr(useExtraAttrs_, attr) << "::";
|
2019-09-12 09:21:07 +08:00
|
|
|
useExtraAttrs_ << local.name() << '\n';
|
2018-07-17 21:39:38 +08:00
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:12:23 +08:00
|
|
|
// When a generic interface has the same name as a derived type
|
|
|
|
// in the same scope, the generic shadows the derived type.
|
|
|
|
// If the derived type were declared first, emit the generic
|
|
|
|
// interface at the position of derived type's declaration.
|
|
|
|
// (ReplaceName() is not used for this purpose because doing so
|
|
|
|
// would confusingly position error messages pertaining to the generic
|
|
|
|
// interface upon the derived type's declaration.)
|
|
|
|
static inline SourceName NameInModuleFile(const Symbol &symbol) {
|
|
|
|
if (const auto *generic{symbol.detailsIf<GenericDetails>()}) {
|
|
|
|
if (const auto *derivedTypeOverload{generic->derivedType()}) {
|
|
|
|
if (derivedTypeOverload->name().begin() < symbol.name().begin()) {
|
|
|
|
return derivedTypeOverload->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const auto *use{symbol.detailsIf<UseDetails>()}) {
|
|
|
|
if (use->symbol().attrs().test(Attr::PRIVATE)) {
|
|
|
|
// Avoid the use in sorting of names created to access private
|
|
|
|
// specific procedures as a result of generic resolution;
|
|
|
|
// they're not in the cooked source.
|
|
|
|
return use->symbol().name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return symbol.name();
|
|
|
|
}
|
|
|
|
|
2018-09-08 06:39:20 +08:00
|
|
|
// Collect the symbols of this scope sorted by their original order, not name.
|
2019-02-08 04:25:59 +08:00
|
|
|
// Namelists are an exception: they are sorted after other symbols.
|
2020-12-03 07:13:49 +08:00
|
|
|
void CollectSymbols(
|
|
|
|
const Scope &scope, SymbolVector &sorted, SymbolVector &uses) {
|
2019-10-23 07:53:29 +08:00
|
|
|
SymbolVector namelist;
|
2020-04-23 06:39:24 +08:00
|
|
|
std::size_t commonSize{scope.commonBlocks().size()};
|
|
|
|
auto symbols{scope.GetSymbols()};
|
|
|
|
sorted.reserve(symbols.size() + commonSize);
|
|
|
|
for (SymbolRef symbol : symbols) {
|
|
|
|
if (!symbol->test(Symbol::Flag::ParentComp)) {
|
|
|
|
if (symbol->has<NamelistDetails>()) {
|
|
|
|
namelist.push_back(symbol);
|
|
|
|
} else {
|
|
|
|
sorted.push_back(symbol);
|
2018-09-21 05:08:59 +08:00
|
|
|
}
|
2020-12-03 07:13:49 +08:00
|
|
|
if (const auto *details{symbol->detailsIf<GenericDetails>()}) {
|
|
|
|
uses.insert(uses.end(), details->uses().begin(), details->uses().end());
|
|
|
|
}
|
2018-09-08 06:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 00:50:30 +08:00
|
|
|
// Sort most symbols by name: use of Symbol::ReplaceName ensures the source
|
|
|
|
// location of a symbol's name is the first "real" use.
|
|
|
|
std::sort(sorted.begin(), sorted.end(), [](SymbolRef x, SymbolRef y) {
|
2021-01-14 06:12:23 +08:00
|
|
|
return NameInModuleFile(x).begin() < NameInModuleFile(y).begin();
|
2020-12-29 00:50:30 +08:00
|
|
|
});
|
2020-04-23 06:39:24 +08:00
|
|
|
sorted.insert(sorted.end(), namelist.begin(), namelist.end());
|
2019-02-19 03:39:46 +08:00
|
|
|
for (const auto &pair : scope.commonBlocks()) {
|
2020-04-23 06:39:24 +08:00
|
|
|
sorted.push_back(*pair.second);
|
2019-02-19 03:39:46 +08:00
|
|
|
}
|
2020-04-23 06:39:24 +08:00
|
|
|
std::sort(sorted.end() - commonSize, sorted.end());
|
2018-09-08 06:39:20 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutEntity(llvm::raw_ostream &os, const Symbol &symbol) {
|
2018-07-17 07:23:18 +08:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); },
|
|
|
|
[&](const ProcEntityDetails &) { PutProcEntity(os, symbol); },
|
2018-09-05 01:28:27 +08:00
|
|
|
[&](const TypeParamDetails &) { PutTypeParam(os, symbol); },
|
2018-07-20 04:28:24 +08:00
|
|
|
[&](const auto &) {
|
2018-07-26 08:07:38 +08:00
|
|
|
common::die("PutEntity: unexpected details: %s",
|
2018-07-20 04:28:24 +08:00
|
|
|
DetailsToString(symbol.details()).c_str());
|
|
|
|
},
|
2018-07-17 07:23:18 +08:00
|
|
|
},
|
|
|
|
symbol.details());
|
2018-11-07 09:18:06 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutShapeSpec(llvm::raw_ostream &os, const ShapeSpec &x) {
|
2019-04-05 05:46:40 +08:00
|
|
|
if (x.lbound().isAssumed()) {
|
2018-11-07 09:18:06 +08:00
|
|
|
CHECK(x.ubound().isAssumed());
|
|
|
|
os << "..";
|
|
|
|
} else {
|
|
|
|
if (!x.lbound().isDeferred()) {
|
|
|
|
PutBound(os, x.lbound());
|
|
|
|
}
|
|
|
|
os << ':';
|
|
|
|
if (!x.ubound().isDeferred()) {
|
|
|
|
PutBound(os, x.ubound());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutShape(
|
|
|
|
llvm::raw_ostream &os, const ArraySpec &shape, char open, char close) {
|
2018-11-07 09:18:06 +08:00
|
|
|
if (!shape.empty()) {
|
2019-04-05 05:46:40 +08:00
|
|
|
os << open;
|
2018-11-07 09:18:06 +08:00
|
|
|
bool first{true};
|
|
|
|
for (const auto &shapeSpec : shape) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
os << ',';
|
|
|
|
}
|
|
|
|
PutShapeSpec(os, shapeSpec);
|
|
|
|
}
|
2019-04-05 05:46:40 +08:00
|
|
|
os << close;
|
2018-11-07 09:18:06 +08:00
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutObjectEntity(llvm::raw_ostream &os, const Symbol &symbol) {
|
2019-01-05 09:10:31 +08:00
|
|
|
auto &details{symbol.get<ObjectEntityDetails>()};
|
2020-03-29 12:00:16 +08:00
|
|
|
PutEntity(
|
|
|
|
os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); },
|
2019-09-17 07:58:13 +08:00
|
|
|
symbol.attrs());
|
2019-04-05 05:46:40 +08:00
|
|
|
PutShape(os, details.shape(), '(', ')');
|
|
|
|
PutShape(os, details.coshape(), '[', ']');
|
2019-08-13 05:06:06 +08:00
|
|
|
PutInit(os, symbol, details.init());
|
2019-07-12 06:11:40 +08:00
|
|
|
os << '\n';
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutProcEntity(llvm::raw_ostream &os, const Symbol &symbol) {
|
2019-07-12 06:11:40 +08:00
|
|
|
if (symbol.attrs().test(Attr::INTRINSIC)) {
|
2019-12-26 04:29:50 +08:00
|
|
|
os << "intrinsic::" << symbol.name() << '\n';
|
2019-07-12 06:11:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-05 09:10:31 +08:00
|
|
|
const auto &details{symbol.get<ProcEntityDetails>()};
|
|
|
|
const ProcInterface &interface{details.interface()};
|
2019-09-17 07:58:13 +08:00
|
|
|
Attrs attrs{symbol.attrs()};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (details.passName()) {
|
2019-09-17 07:58:13 +08:00
|
|
|
attrs.reset(Attr::PASS);
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
PutEntity(
|
|
|
|
os, symbol,
|
2019-09-17 07:58:13 +08:00
|
|
|
[&]() {
|
|
|
|
os << "procedure(";
|
|
|
|
if (interface.symbol()) {
|
|
|
|
os << interface.symbol()->name();
|
|
|
|
} else if (interface.type()) {
|
|
|
|
PutType(os, *interface.type());
|
|
|
|
}
|
|
|
|
os << ')';
|
|
|
|
PutPassName(os, details.passName());
|
|
|
|
},
|
|
|
|
attrs);
|
2019-07-12 06:11:40 +08:00
|
|
|
os << '\n';
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutPassName(
|
|
|
|
llvm::raw_ostream &os, const std::optional<SourceName> &passName) {
|
2019-01-05 09:10:31 +08:00
|
|
|
if (passName) {
|
2019-09-12 09:21:07 +08:00
|
|
|
os << ",pass(" << *passName << ')';
|
2019-01-05 09:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
|
2018-11-07 09:18:06 +08:00
|
|
|
auto &details{symbol.get<TypeParamDetails>()};
|
2020-03-29 12:00:16 +08:00
|
|
|
PutEntity(
|
|
|
|
os, symbol,
|
2019-09-17 07:58:13 +08:00
|
|
|
[&]() {
|
|
|
|
PutType(os, DEREF(symbol.GetType()));
|
|
|
|
PutLower(os << ',', common::EnumToString(details.attr()));
|
|
|
|
},
|
|
|
|
symbol.attrs());
|
2018-11-07 09:18:06 +08:00
|
|
|
PutInit(os, details.init());
|
2019-07-12 06:11:40 +08:00
|
|
|
os << '\n';
|
2018-11-07 09:18:06 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutInit(
|
|
|
|
llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init) {
|
2018-12-06 22:59:37 +08:00
|
|
|
if (init) {
|
2019-08-13 05:06:06 +08:00
|
|
|
if (symbol.attrs().test(Attr::PARAMETER) ||
|
|
|
|
symbol.owner().IsDerivedType()) {
|
|
|
|
os << (symbol.attrs().test(Attr::POINTER) ? "=>" : "=");
|
|
|
|
init->AsFortran(os);
|
|
|
|
}
|
2019-01-08 07:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutInit(llvm::raw_ostream &os, const MaybeIntExpr &init) {
|
2019-01-08 07:05:53 +08:00
|
|
|
if (init) {
|
2019-04-02 06:02:23 +08:00
|
|
|
init->AsFortran(os << '=');
|
2018-11-07 09:18:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutBound(llvm::raw_ostream &os, const Bound &x) {
|
2018-11-07 09:18:06 +08:00
|
|
|
if (x.isAssumed()) {
|
|
|
|
os << '*';
|
|
|
|
} else if (x.isDeferred()) {
|
|
|
|
os << ':';
|
|
|
|
} else {
|
2019-04-02 06:02:23 +08:00
|
|
|
x.GetExplicit()->AsFortran(os);
|
2018-11-07 09:18:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:23:18 +08:00
|
|
|
// Write an entity (object or procedure) declaration.
|
|
|
|
// writeType is called to write out the type.
|
2020-02-28 23:11:03 +08:00
|
|
|
void PutEntity(llvm::raw_ostream &os, const Symbol &symbol,
|
2019-09-17 07:58:13 +08:00
|
|
|
std::function<void()> writeType, Attrs attrs) {
|
2018-07-17 07:23:18 +08:00
|
|
|
writeType();
|
2019-02-16 02:16:25 +08:00
|
|
|
MaybeExpr bindName;
|
2020-03-29 12:00:16 +08:00
|
|
|
std::visit(common::visitors{
|
|
|
|
[&](const SubprogramDetails &x) { bindName = x.bindName(); },
|
|
|
|
[&](const ObjectEntityDetails &x) { bindName = x.bindName(); },
|
|
|
|
[&](const ProcEntityDetails &x) { bindName = x.bindName(); },
|
|
|
|
[&](const auto &) {},
|
|
|
|
},
|
2019-02-16 02:16:25 +08:00
|
|
|
symbol.details());
|
2019-09-17 07:58:13 +08:00
|
|
|
PutAttrs(os, attrs, bindName);
|
2019-09-12 09:21:07 +08:00
|
|
|
os << "::" << symbol.name();
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put out each attribute to os, surrounded by `before` and `after` and
|
|
|
|
// mapped to lower case.
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &PutAttrs(llvm::raw_ostream &os, Attrs attrs,
|
|
|
|
const MaybeExpr &bindName, std::string before, std::string after) {
|
2020-03-29 12:00:16 +08:00
|
|
|
attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC
|
|
|
|
attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL
|
2019-02-16 02:16:25 +08:00
|
|
|
if (bindName) {
|
2019-04-02 06:02:23 +08:00
|
|
|
bindName->AsFortran(os << before << "bind(c, name=") << ')' << after;
|
2019-02-16 02:16:25 +08:00
|
|
|
attrs.set(Attr::BIND_C, false);
|
|
|
|
}
|
2018-07-17 07:23:18 +08:00
|
|
|
for (std::size_t i{0}; i < Attr_enumSize; ++i) {
|
|
|
|
Attr attr{static_cast<Attr>(i)};
|
|
|
|
if (attrs.test(attr)) {
|
2019-06-25 03:35:17 +08:00
|
|
|
PutAttr(os << before, attr) << after;
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &PutAttr(llvm::raw_ostream &os, Attr attr) {
|
2019-06-25 03:35:17 +08:00
|
|
|
return PutLower(os, AttrToString(attr));
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &PutType(llvm::raw_ostream &os, const DeclTypeSpec &type) {
|
2019-06-23 01:16:13 +08:00
|
|
|
return PutLower(os, type.AsFortran());
|
2018-07-17 07:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &PutLower(llvm::raw_ostream &os, const std::string &str) {
|
2018-07-17 07:23:18 +08:00
|
|
|
for (char c : str) {
|
|
|
|
os << parser::ToLowerCaseLetter(c);
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
struct Temp {
|
2020-03-05 23:09:29 +08:00
|
|
|
Temp(int fd, std::string path) : fd{fd}, path{path} {}
|
2020-02-25 23:59:50 +08:00
|
|
|
Temp(Temp &&t) : fd{std::exchange(t.fd, -1)}, path{std::move(t.path)} {}
|
2019-09-24 08:10:58 +08:00
|
|
|
~Temp() {
|
2020-02-25 23:59:50 +08:00
|
|
|
if (fd >= 0) {
|
2020-03-05 23:09:29 +08:00
|
|
|
llvm::sys::fs::file_t native{llvm::sys::fs::convertFDToNativeFile(fd)};
|
|
|
|
llvm::sys::fs::closeFile(native);
|
2020-02-25 23:59:50 +08:00
|
|
|
llvm::sys::fs::remove(path.c_str());
|
|
|
|
}
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
2020-03-05 23:09:29 +08:00
|
|
|
int fd;
|
2019-09-24 08:10:58 +08:00
|
|
|
std::string path;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a temp file in the same directory and with the same suffix as path.
|
|
|
|
// Return an open file descriptor and its path.
|
2020-02-25 23:59:50 +08:00
|
|
|
static llvm::ErrorOr<Temp> MkTemp(const std::string &path) {
|
2019-09-24 08:10:58 +08:00
|
|
|
auto length{path.length()};
|
|
|
|
auto dot{path.find_last_of("./")};
|
2020-02-25 23:59:50 +08:00
|
|
|
std::string suffix{
|
|
|
|
dot < length && path[dot] == '.' ? path.substr(dot + 1) : ""};
|
2019-09-24 08:10:58 +08:00
|
|
|
CHECK(length > suffix.length() &&
|
|
|
|
path.substr(length - suffix.length()) == suffix);
|
2020-02-25 23:59:50 +08:00
|
|
|
auto prefix{path.substr(0, length - suffix.length())};
|
2020-03-05 23:09:29 +08:00
|
|
|
int fd;
|
2020-02-25 23:59:50 +08:00
|
|
|
llvm::SmallString<16> tempPath;
|
|
|
|
if (std::error_code err{llvm::sys::fs::createUniqueFile(
|
|
|
|
prefix + "%%%%%%" + suffix, fd, tempPath)}) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return Temp{fd, tempPath.c_str()};
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the module file at path, prepending header. If an error occurs,
|
|
|
|
// return errno, otherwise 0.
|
2020-02-25 23:59:50 +08:00
|
|
|
static std::error_code WriteFile(
|
|
|
|
const std::string &path, const std::string &contents, bool debug) {
|
2019-09-24 08:10:58 +08:00
|
|
|
auto header{std::string{ModHeader::bom} + ModHeader::magic +
|
|
|
|
CheckSum(contents) + ModHeader::terminator};
|
2020-02-25 23:59:50 +08:00
|
|
|
if (debug) {
|
|
|
|
llvm::dbgs() << "Processing module " << path << ": ";
|
|
|
|
}
|
2019-09-24 08:10:58 +08:00
|
|
|
if (FileContentsMatch(path, header, contents)) {
|
2020-02-25 23:59:50 +08:00
|
|
|
if (debug) {
|
|
|
|
llvm::dbgs() << "module unchanged, not writing\n";
|
|
|
|
}
|
|
|
|
return {};
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
llvm::ErrorOr<Temp> temp{MkTemp(path)};
|
|
|
|
if (!temp) {
|
|
|
|
return temp.getError();
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
llvm::raw_fd_ostream writer(temp->fd, /*shouldClose=*/false);
|
|
|
|
writer << header;
|
|
|
|
writer << contents;
|
|
|
|
writer.flush();
|
|
|
|
if (writer.has_error()) {
|
|
|
|
return writer.error();
|
2018-08-09 02:36:24 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
if (debug) {
|
|
|
|
llvm::dbgs() << "module written\n";
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
return llvm::sys::fs::rename(temp->path, path);
|
2018-08-09 02:36:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the stream matches what we would write for the mod file.
|
2019-09-24 08:10:58 +08:00
|
|
|
static bool FileContentsMatch(const std::string &path,
|
|
|
|
const std::string &header, const std::string &contents) {
|
|
|
|
std::size_t hsize{header.size()};
|
|
|
|
std::size_t csize{contents.size()};
|
2020-02-25 23:59:50 +08:00
|
|
|
auto buf_or{llvm::MemoryBuffer::getFile(path)};
|
|
|
|
if (!buf_or) {
|
2019-09-24 08:10:58 +08:00
|
|
|
return false;
|
2018-08-09 02:36:24 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
auto buf = std::move(buf_or.get());
|
|
|
|
if (buf->getBufferSize() != hsize + csize) {
|
2018-08-09 02:36:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
if (!std::equal(header.begin(), header.end(), buf->getBufferStart(),
|
|
|
|
buf->getBufferStart() + hsize)) {
|
|
|
|
return false;
|
2018-08-09 02:36:24 +08:00
|
|
|
}
|
2020-02-25 23:59:50 +08:00
|
|
|
|
|
|
|
return std::equal(contents.begin(), contents.end(),
|
|
|
|
buf->getBufferStart() + hsize, buf->getBufferEnd());
|
2018-08-09 02:36:24 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:23:18 +08:00
|
|
|
// Compute a simple hash of the contents of a module file and
|
|
|
|
// return it as a string of hex digits.
|
|
|
|
// This uses the Fowler-Noll-Vo hash function.
|
2019-09-24 08:10:58 +08:00
|
|
|
static std::string CheckSum(const std::string_view &contents) {
|
2018-07-17 07:23:18 +08:00
|
|
|
std::uint64_t hash{0xcbf29ce484222325ull};
|
2019-09-24 08:10:58 +08:00
|
|
|
for (char c : contents) {
|
2018-07-17 07:23:18 +08:00
|
|
|
hash ^= c & 0xff;
|
|
|
|
hash *= 0x100000001b3;
|
|
|
|
}
|
|
|
|
static const char *digits = "0123456789abcdef";
|
2019-09-24 08:10:58 +08:00
|
|
|
std::string result(ModHeader::sumLen, '0');
|
|
|
|
for (size_t i{ModHeader::sumLen}; hash != 0; hash >>= 4) {
|
2018-07-17 07:23:18 +08:00
|
|
|
result[--i] = digits[hash & 0xf];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:42:56 +08:00
|
|
|
static bool VerifyHeader(llvm::ArrayRef<char> content) {
|
|
|
|
std::string_view sv{content.data(), content.size()};
|
2019-09-24 08:10:58 +08:00
|
|
|
if (sv.substr(0, ModHeader::magicLen) != ModHeader::magic) {
|
2018-08-09 02:36:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-24 08:10:58 +08:00
|
|
|
std::string_view expectSum{sv.substr(ModHeader::magicLen, ModHeader::sumLen)};
|
|
|
|
std::string actualSum{CheckSum(sv.substr(ModHeader::len))};
|
2018-08-09 02:36:24 +08:00
|
|
|
return expectSum == actualSum;
|
|
|
|
}
|
|
|
|
|
2018-10-22 22:37:38 +08:00
|
|
|
Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
|
2020-03-29 12:00:16 +08:00
|
|
|
std::string ancestorName; // empty for module
|
2018-08-03 07:21:27 +08:00
|
|
|
if (ancestor) {
|
|
|
|
if (auto *scope{ancestor->FindSubmodule(name)}) {
|
|
|
|
return scope;
|
|
|
|
}
|
2019-08-21 21:29:11 +08:00
|
|
|
ancestorName = ancestor->GetName().value().ToString();
|
2018-08-03 07:21:27 +08:00
|
|
|
} else {
|
2018-10-22 22:37:38 +08:00
|
|
|
auto it{context_.globalScope().find(name)};
|
|
|
|
if (it != context_.globalScope().end()) {
|
2018-08-03 07:21:27 +08:00
|
|
|
return it->second->scope();
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 03:22:24 +08:00
|
|
|
parser::Parsing parsing{context_.allCookedSources()};
|
2018-07-28 06:18:36 +08:00
|
|
|
parser::Options options;
|
|
|
|
options.isModuleFile = true;
|
2019-11-07 03:15:03 +08:00
|
|
|
options.features.Enable(common::LanguageFeature::BackslashEscapes);
|
2019-09-24 08:10:58 +08:00
|
|
|
options.searchDirectories = context_.searchDirectories();
|
|
|
|
auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())};
|
|
|
|
const auto *sourceFile{parsing.Prescan(path, options)};
|
2019-12-04 01:49:44 +08:00
|
|
|
if (parsing.messages().AnyFatalError()) {
|
2019-09-24 08:10:58 +08:00
|
|
|
for (auto &msg : parsing.messages().messages()) {
|
|
|
|
std::string str{msg.ToString()};
|
|
|
|
Say(name, ancestorName, parser::MessageFixedText{str.c_str(), str.size()},
|
2019-12-04 01:49:44 +08:00
|
|
|
path);
|
2019-09-24 08:10:58 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
2019-12-04 01:49:44 +08:00
|
|
|
}
|
|
|
|
CHECK(sourceFile);
|
2020-02-27 21:42:56 +08:00
|
|
|
if (!VerifyHeader(sourceFile->content())) {
|
2019-09-24 08:10:58 +08:00
|
|
|
Say(name, ancestorName, "File has invalid checksum: %s"_en_US,
|
|
|
|
sourceFile->path());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_null_ostream NullStream;
|
|
|
|
parsing.Parse(NullStream);
|
2018-07-27 01:57:51 +08:00
|
|
|
auto &parseTree{parsing.parseTree()};
|
|
|
|
if (!parsing.messages().empty() || !parsing.consumedWholeFile() ||
|
2019-11-10 01:29:31 +08:00
|
|
|
!parseTree) {
|
2019-09-24 08:10:58 +08:00
|
|
|
Say(name, ancestorName, "Module file is corrupt: %s"_err_en_US,
|
|
|
|
sourceFile->path());
|
2018-08-03 07:21:27 +08:00
|
|
|
return nullptr;
|
2018-07-25 21:55:11 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
Scope *parentScope; // the scope this module/submodule goes into
|
2018-08-03 07:21:27 +08:00
|
|
|
if (!ancestor) {
|
2018-10-22 22:37:38 +08:00
|
|
|
parentScope = &context_.globalScope();
|
2019-08-21 20:33:03 +08:00
|
|
|
} else if (std::optional<SourceName> parent{GetSubmoduleParent(*parseTree)}) {
|
2018-10-22 22:37:38 +08:00
|
|
|
parentScope = Read(*parent, ancestor);
|
2018-08-03 07:21:27 +08:00
|
|
|
} else {
|
2018-08-04 02:32:21 +08:00
|
|
|
parentScope = ancestor;
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
2021-01-14 06:12:23 +08:00
|
|
|
auto pair{parentScope->try_emplace(name, UnknownDetails{})};
|
|
|
|
if (!pair.second) {
|
2018-08-03 07:21:27 +08:00
|
|
|
return nullptr;
|
2018-07-25 21:55:11 +08:00
|
|
|
}
|
2021-01-14 06:12:23 +08:00
|
|
|
Symbol &modSymbol{*pair.first->second};
|
2018-07-25 21:55:11 +08:00
|
|
|
modSymbol.set(Symbol::Flag::ModFile);
|
2021-01-14 06:12:23 +08:00
|
|
|
ResolveNames(context_, *parseTree);
|
|
|
|
CHECK(modSymbol.has<ModuleDetails>());
|
|
|
|
CHECK(modSymbol.test(Symbol::Flag::ModFile));
|
2018-08-03 07:21:27 +08:00
|
|
|
return modSymbol.scope();
|
2018-07-25 21:55:11 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
parser::Message &ModFileReader::Say(const SourceName &name,
|
|
|
|
const std::string &ancestor, parser::MessageFixedText &&msg,
|
|
|
|
const std::string &arg) {
|
2021-02-11 08:20:59 +08:00
|
|
|
return context_.Say(name, "Cannot read module file for %s: %s"_err_en_US,
|
|
|
|
parser::MessageFormattedText{ancestor.empty()
|
|
|
|
? "module '%s'"_en_US
|
|
|
|
: "submodule '%s' of module '%s'"_en_US,
|
|
|
|
name, ancestor}
|
|
|
|
.MoveString(),
|
|
|
|
parser::MessageFormattedText{std::move(msg), arg}.MoveString());
|
2018-07-25 21:55:11 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 07:21:27 +08:00
|
|
|
// program was read from a .mod file for a submodule; return the name of the
|
|
|
|
// submodule's parent submodule, nullptr if none.
|
2019-08-22 16:12:20 +08:00
|
|
|
static std::optional<SourceName> GetSubmoduleParent(
|
2019-08-21 20:33:03 +08:00
|
|
|
const parser::Program &program) {
|
2018-08-03 07:21:27 +08:00
|
|
|
CHECK(program.v.size() == 1);
|
|
|
|
auto &unit{program.v.front()};
|
|
|
|
auto &submod{std::get<common::Indirection<parser::Submodule>>(unit.u)};
|
2019-03-06 04:28:08 +08:00
|
|
|
auto &stmt{
|
|
|
|
std::get<parser::Statement<parser::SubmoduleStmt>>(submod.value().t)};
|
2018-08-03 07:21:27 +08:00
|
|
|
auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
|
|
|
|
if (auto &parent{std::get<std::optional<parser::Name>>(parentId.t)}) {
|
2019-08-21 20:33:03 +08:00
|
|
|
return parent->source;
|
2018-07-25 21:55:11 +08:00
|
|
|
} else {
|
2019-08-21 20:33:03 +08:00
|
|
|
return std::nullopt;
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:04:17 +08:00
|
|
|
void SubprogramSymbolCollector::Collect() {
|
|
|
|
const auto &details{symbol_.get<SubprogramDetails>()};
|
|
|
|
isInterface_ = details.isInterface();
|
|
|
|
for (const Symbol *dummyArg : details.dummyArgs()) {
|
2020-06-18 22:05:08 +08:00
|
|
|
if (dummyArg) {
|
|
|
|
DoSymbol(*dummyArg);
|
|
|
|
}
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
2019-08-13 04:06:59 +08:00
|
|
|
if (details.isFunction()) {
|
|
|
|
DoSymbol(details.result());
|
|
|
|
}
|
2019-03-30 06:04:17 +08:00
|
|
|
for (const auto &pair : scope_) {
|
2019-10-23 07:53:29 +08:00
|
|
|
const Symbol &symbol{*pair.second};
|
|
|
|
if (const auto *useDetails{symbol.detailsIf<UseDetails>()}) {
|
2020-05-12 04:28:05 +08:00
|
|
|
if (useSet_.count(useDetails->symbol().GetUltimate()) > 0) {
|
2019-03-30 06:04:17 +08:00
|
|
|
need_.push_back(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
|
2019-09-13 04:43:16 +08:00
|
|
|
DoSymbol(symbol.name(), symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do symbols this one depends on; then add to need_
|
|
|
|
void SubprogramSymbolCollector::DoSymbol(
|
|
|
|
const SourceName &name, const Symbol &symbol) {
|
2019-03-30 06:04:17 +08:00
|
|
|
const auto &scope{symbol.owner()};
|
2019-07-12 00:27:25 +08:00
|
|
|
if (scope != scope_ && !scope.IsDerivedType()) {
|
2019-03-30 06:04:17 +08:00
|
|
|
if (scope != scope_.parent()) {
|
2019-10-23 07:53:29 +08:00
|
|
|
useSet_.insert(symbol);
|
2019-08-23 04:57:18 +08:00
|
|
|
}
|
2019-09-13 04:43:16 +08:00
|
|
|
if (NeedImport(name, symbol)) {
|
|
|
|
imports_.insert(name);
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-10-23 07:53:29 +08:00
|
|
|
if (!needSet_.insert(symbol).second) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return; // already done
|
|
|
|
}
|
|
|
|
std::visit(common::visitors{
|
|
|
|
[this](const ObjectEntityDetails &details) {
|
|
|
|
for (const ShapeSpec &spec : details.shape()) {
|
|
|
|
DoBound(spec.lbound());
|
|
|
|
DoBound(spec.ubound());
|
|
|
|
}
|
|
|
|
for (const ShapeSpec &spec : details.coshape()) {
|
|
|
|
DoBound(spec.lbound());
|
|
|
|
DoBound(spec.ubound());
|
|
|
|
}
|
|
|
|
if (const Symbol * commonBlock{details.commonBlock()}) {
|
|
|
|
DoSymbol(*commonBlock);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[this](const CommonBlockDetails &details) {
|
2020-05-07 03:20:07 +08:00
|
|
|
for (const auto &object : details.objects()) {
|
|
|
|
DoSymbol(*object);
|
2020-03-29 12:00:16 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[](const auto &) {},
|
|
|
|
},
|
2019-03-30 06:04:17 +08:00
|
|
|
symbol.details());
|
|
|
|
if (!symbol.has<UseDetails>()) {
|
|
|
|
DoType(symbol.GetType());
|
|
|
|
}
|
2019-07-12 00:27:25 +08:00
|
|
|
if (!scope.IsDerivedType()) {
|
2019-10-23 07:53:29 +08:00
|
|
|
need_.push_back(symbol);
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) {
|
|
|
|
if (!type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (type->category()) {
|
|
|
|
case DeclTypeSpec::Numeric:
|
2020-03-29 12:00:16 +08:00
|
|
|
case DeclTypeSpec::Logical:
|
|
|
|
break; // nothing to do
|
2019-03-30 06:04:17 +08:00
|
|
|
case DeclTypeSpec::Character:
|
|
|
|
DoParamValue(type->characterTypeSpec().length());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (const DerivedTypeSpec * derived{type->AsDerived()}) {
|
|
|
|
const auto &typeSymbol{derived->typeSymbol()};
|
|
|
|
if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) {
|
2019-09-13 04:43:16 +08:00
|
|
|
DoSymbol(extends->name(), extends->typeSymbol());
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
2020-02-06 19:27:36 +08:00
|
|
|
for (const auto &pair : derived->parameters()) {
|
2019-03-30 06:04:17 +08:00
|
|
|
DoParamValue(pair.second);
|
|
|
|
}
|
2020-02-06 19:27:36 +08:00
|
|
|
for (const auto &pair : *typeSymbol.scope()) {
|
2019-10-23 07:53:29 +08:00
|
|
|
const Symbol &comp{*pair.second};
|
2019-03-30 06:04:17 +08:00
|
|
|
DoSymbol(comp);
|
|
|
|
}
|
2019-09-13 04:43:16 +08:00
|
|
|
DoSymbol(derived->name(), derived->typeSymbol());
|
2019-03-30 06:04:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramSymbolCollector::DoBound(const Bound &bound) {
|
|
|
|
if (const MaybeSubscriptIntExpr & expr{bound.GetExplicit()}) {
|
|
|
|
DoExpr(*expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void SubprogramSymbolCollector::DoParamValue(const ParamValue ¶mValue) {
|
|
|
|
if (const auto &expr{paramValue.GetExplicit()}) {
|
|
|
|
DoExpr(*expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 04:57:18 +08:00
|
|
|
// Do we need a IMPORT of this symbol into an interface block?
|
2019-09-13 04:43:16 +08:00
|
|
|
bool SubprogramSymbolCollector::NeedImport(
|
|
|
|
const SourceName &name, const Symbol &symbol) {
|
2019-08-23 04:57:18 +08:00
|
|
|
if (!isInterface_) {
|
|
|
|
return false;
|
2021-01-14 06:12:23 +08:00
|
|
|
} else if (symbol.owner().Contains(scope_)) {
|
2019-08-23 04:57:18 +08:00
|
|
|
return true;
|
2021-01-14 06:12:23 +08:00
|
|
|
} else if (const Symbol * found{scope_.FindSymbol(name)}) {
|
|
|
|
// detect import from ancestor of use-associated symbol
|
|
|
|
return found->has<UseDetails>() && found->owner() != scope_;
|
|
|
|
} else {
|
|
|
|
// "found" can be null in the case of a use-associated derived type's parent
|
|
|
|
// type
|
|
|
|
CHECK(symbol.has<DerivedTypeDetails>());
|
|
|
|
return false;
|
2019-08-23 04:57:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::semantics
|