llvm/flang/lib/semantics/mod-file.cc

883 lines
28 KiB
C++
Raw Normal View History

// Copyright (c) 2018-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.
#include "mod-file.h"
#include "scope.h"
#include "semantics.h"
#include "symbol.h"
#include "../evaluate/traversal.h"
#include "../parser/parsing.h"
#include <algorithm>
#include <cerrno>
#include <fstream>
#include <ostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
namespace Fortran::semantics {
using namespace parser::literals;
// The initial characters of a file that identify it as a .mod file.
static constexpr auto magic{"!mod$ v1 sum:"};
static const SourceName *GetSubmoduleParent(const parser::Program &);
static std::string ModFilePath(const std::string &dir, const SourceName &,
const std::string &ancestor, const std::string &suffix);
static std::vector<const Symbol *> CollectSymbols(const Scope &);
static void PutEntity(std::ostream &, const Symbol &);
static void PutObjectEntity(std::ostream &, const Symbol &);
static void PutProcEntity(std::ostream &, const Symbol &);
static void PutPassName(std::ostream &, const std::optional<SourceName> &);
static void PutTypeParam(std::ostream &, const Symbol &);
static void PutEntity(std::ostream &, const Symbol &, std::function<void()>);
static void PutInit(std::ostream &, const MaybeExpr &);
static void PutInit(std::ostream &, const MaybeIntExpr &);
static void PutBound(std::ostream &, const Bound &);
static std::ostream &PutAttrs(std::ostream &, Attrs,
const MaybeExpr & = std::nullopt, std::string before = ","s,
std::string after = ""s);
static std::ostream &PutLower(std::ostream &, const Symbol &);
static std::ostream &PutLower(std::ostream &, const DeclTypeSpec &);
static std::ostream &PutLower(std::ostream &, const std::string &);
static bool WriteFile(const std::string &, std::string &&);
static bool FileContentsMatch(
std::fstream &, const std::string &, const std::string &);
static std::string GetHeader(const std::string &);
static std::size_t GetFileSize(const std::string &);
// Collect symbols needed for a subprogram interface
class SubprogramSymbolCollector {
public:
using SymbolSet = std::set<const Symbol *>;
SubprogramSymbolCollector(const Symbol &symbol)
: symbol_{symbol}, scope_{*symbol.scope()} {}
SymbolList symbols() const { return need_; }
SymbolSet imports() const { return imports_; }
void Collect();
private:
const Symbol &symbol_;
const Scope &scope_;
bool isInterface_{false};
SymbolList need_; // symbols that are needed
SymbolSet needSet_; // symbols already in need_
SymbolSet useSet_; // use-associations that might be needed
SymbolSet imports_; // imports from host that are needed
void DoSymbol(const Symbol &);
void DoType(const DeclTypeSpec *);
void DoBound(const Bound &);
void DoParamValue(const ParamValue &);
using SymbolVector = std::vector<const Symbol *>;
struct SymbolVisitor : public virtual evaluate::VisitorBase<SymbolVector> {
using Result = SymbolVector;
explicit SymbolVisitor(int) {}
void Handle(const Symbol *symbol) { result().push_back(symbol); }
};
template<typename T> void DoExpr(evaluate::Expr<T> expr) {
evaluate::Visitor<SymbolVisitor> visitor{0};
for (const Symbol *symbol : visitor.Traverse(expr)) {
CHECK(symbol && "bad symbol from Traverse");
DoSymbol(*symbol);
}
}
};
bool ModFileWriter::WriteAll() {
WriteAll(context_.globalScope());
return !context_.AnyFatalError();
}
void ModFileWriter::WriteAll(const Scope &scope) {
for (const auto &child : scope.children()) {
WriteOne(child);
}
}
void ModFileWriter::WriteOne(const Scope &scope) {
if (scope.kind() == Scope::Kind::Module) {
auto *symbol{scope.symbol()};
if (!symbol->test(Symbol::Flag::ModFile)) {
Write(*symbol);
}
WriteAll(scope); // write out submodules
}
}
// 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()};
auto ancestorName{ancestor ? ancestor->name().ToString() : ""s};
auto path{ModFilePath(context_.moduleDirectory(), symbol.name(), ancestorName,
context_.moduleFileSuffix())};
PutSymbols(*symbol.scope());
if (!WriteFile(path, GetAsString(symbol))) {
context_.Say(symbol.name(), "Error writing %s: %s"_err_en_US, path,
std::strerror(errno));
}
}
// Return the entire body of the module file
// and clear saved uses, decls, and contains.
std::string ModFileWriter::GetAsString(const Symbol &symbol) {
std::stringstream all;
auto &details{symbol.get<ModuleDetails>()};
if (!details.isSubmodule()) {
PutLower(all << "module ", symbol);
} else {
auto *parent{details.parent()->symbol()};
auto *ancestor{details.ancestor()->symbol()};
PutLower(all << "submodule(", *ancestor);
if (parent != ancestor) {
PutLower(all << ':', *parent);
}
PutLower(all << ") ", symbol);
}
all << '\n' << uses_.str();
uses_.str(""s);
all << useExtraAttrs_.str();
useExtraAttrs_.str(""s);
all << decls_.str();
decls_.str(""s);
auto str{contains_.str()};
contains_.str(""s);
if (!str.empty()) {
all << "contains\n" << str;
}
all << "end\n";
return all.str();
}
// Put out the visible symbols from scope.
void ModFileWriter::PutSymbols(const Scope &scope) {
std::stringstream typeBindings; // stuff after CONTAINS in derived type
for (const auto *symbol : CollectSymbols(scope)) {
PutSymbol(typeBindings, symbol);
}
if (auto str{typeBindings.str()}; !str.empty()) {
CHECK(scope.kind() == Scope::Kind::DerivedType);
decls_ << "contains\n" << str;
}
}
// 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(
std::stringstream &typeBindings, const Symbol *symbol) {
if (symbol == nullptr) {
return;
}
std::visit(
common::visitors{
[&](const ModuleDetails &) { /* should be current module */ },
[&](const DerivedTypeDetails &) { PutDerivedType(*symbol); },
[&](const SubprogramDetails &) { PutSubprogram(*symbol); },
[&](const GenericDetails &x) {
PutGeneric(*symbol);
PutSymbol(typeBindings, x.specific());
PutSymbol(typeBindings, x.derivedType());
},
[&](const UseDetails &) { PutUse(*symbol); },
[](const UseErrorDetails &) {},
[&](const ProcBindingDetails &x) {
bool deferred{symbol->attrs().test(Attr::DEFERRED)};
typeBindings << "procedure";
if (deferred) {
PutLower(typeBindings << '(', x.symbol()) << ')';
}
PutPassName(typeBindings, x.passName());
PutAttrs(typeBindings, symbol->attrs());
PutLower(typeBindings << "::", *symbol);
if (!deferred && x.symbol().name() != symbol->name()) {
PutLower(typeBindings << "=>", x.symbol());
}
typeBindings << '\n';
},
[&](const GenericBindingDetails &x) {
for (const auto *proc : x.specificProcs()) {
PutLower(typeBindings << "generic::", *symbol);
PutLower(typeBindings << "=>", *proc) << '\n';
}
},
[&](const NamelistDetails &x) {
PutLower(decls_ << "namelist/", *symbol);
char sep{'/'};
for (const auto *object : x.objects()) {
PutLower(decls_ << sep, *object);
sep = ',';
}
decls_ << '\n';
},
[&](const CommonBlockDetails &x) {
PutLower(decls_ << "common/", *symbol);
char sep = '/';
for (const auto *object : x.objects()) {
PutLower(decls_ << sep, *object);
sep = ',';
}
decls_ << '\n';
if (symbol->attrs().test(Attr::BIND_C)) {
PutAttrs(decls_, symbol->attrs(), x.bindName(), ""s);
PutLower(decls_ << "::/", *symbol) << "/\n";
}
},
[&](const FinalProcDetails &) {
PutLower(typeBindings << "final::", *symbol) << '\n';
},
[flang] More name resolution for construct entities Push a new scope for constructs and statements that require one (DataStmt, DO CONCURRENT, ForallConstruct, ForallStmt -- there are more to do). Currently we use the Block kind of scope because there is no difference. Perhaps that kind should be renamed to Construct, though it does apply to statements as well as constructs. Add DeclareConstructEntity to create a construct or statement entity. When the type is not specified it can come from the type of a symbol in the enclosing scope with the same name. Change DeclareObjectEntity et al. to return the symbol declared, for the benefit of DeclareConstructEntity. Use DeclareConstructEntity for DO CONCURRENT index-name, LOCAL, and LOCAL_INIT variables and the data-i-do-variable in DataImpliedDo Names in SHARED locality spec need special handling: create a new kinds of symbol with HostAssocDetails to represent the host-association of the shared variables within the construct scope. That symbol gets the LocalityShared flag without affecting the symbol in the outer scope. HostAssoc symbols may be useful in other contexts, e.g. up-level references to local variables. Add parser::DoConstruct::IsDoConcurrent() because DO CONCURRENT loops introduce a construct scope while other DO loops do not. Move CanonicalizeDo to before name resolution so that name resolution doesn't have to deal with labeled DO CONCURRENT loops. Allow for type of index name to be specified in ConcurrentHeader. Resolve the derived type name in an AllocateStmt, StructureConstructor Original-commit: flang-compiler/f18@bc7b9891367f3174c9b5018ce5636a36a5a76c1c Reviewed-on: https://github.com/flang-compiler/f18/pull/214
2018-10-18 16:55:48 +02:00
[](const HostAssocDetails &) {},
[flang] Change when symbol is set in parser::Name Rework how `parser::Name` is resolved to contain a `Symbol`. so that constants in types can be evaluated. For example: ``` integer, parameter :: k = 8 integer(k) :: i ``` The old approach of collecting the symbols at the end of name resolution and filling in the `parser::Name` does not work because the type of `i` needs to be set in the symbol table. The symbol field in `parser::Name` is now mutable so that we can set it during name resolution. `RewriteParseTree` no longer needs to do that (it still warns about unresolved ones), so it does not need to collect symbols and fill them in. Consequently, we can eliminate "occurrences" from symbols -- we just need the name where each is first defined. This requires a lot of refactoring in `resolve-names.cc` to pass around `parser::Name` rather than `SourceName` so that we can resolve the name to a symbol. Fix some bugs where we stored `SourceName *` instead of `SourceName` in the symbol table. The pointers were into the parse tree, so they were only valid as long as the parse tree was around. The symbol table needs to remain valid longer than that, so the names need to be copied. `parser::Name` is not used in the symbol table. Eliminate `GenericSpec`. Currently all we need to do is to resolve the kinds of GenericSpec that contain names. Add `ScopeName` kind of `MiscDetails` for when we need a symbol in the scope to match the name of the scope. For example, `module m` cannot contain a declaration of a new `m`. Subprograms need real details because they can be called recursively. Fix output of partially resolved modules where we know it is a submodule but have not yet resolved the ancestor. Original-commit: flang-compiler/f18@5c1a4b99d2421f5b32e83426488d3fdf7951cfba Reviewed-on: https://github.com/flang-compiler/f18/pull/238 Tree-same-pre-rewrite: false
2018-11-16 21:43:08 +01:00
[](const MiscDetails &) {},
[&](const auto &) { PutEntity(decls_, *symbol); },
},
symbol->details());
}
void ModFileWriter::PutDerivedType(const Symbol &typeSymbol) {
auto &details{typeSymbol.get<DerivedTypeDetails>()};
PutAttrs(decls_ << "type", typeSymbol.attrs());
if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) {
PutLower(decls_ << ",extends(", extends->typeSymbol()) << ')';
}
PutLower(decls_ << "::", typeSymbol);
auto &typeScope{*typeSymbol.scope()};
if (!details.paramNames().empty()) {
bool first{true};
decls_ << '(';
for (const auto &name : details.paramNames()) {
PutLower(first ? decls_ : decls_ << ',', name.ToString());
first = false;
}
decls_ << ')';
}
decls_ << '\n';
if (details.sequence()) {
decls_ << "sequence\n";
}
PutSymbols(typeScope);
decls_ << "end type\n";
}
void ModFileWriter::PutSubprogram(const Symbol &symbol) {
auto attrs{symbol.attrs()};
auto &details{symbol.get<SubprogramDetails>()};
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);
}
bool isInterface{details.isInterface()};
std::ostream &os{isInterface ? decls_ : contains_};
if (isInterface) {
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
os << "interface\n";
}
PutAttrs(os, attrs, std::nullopt, ""s, " "s);
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
os << (details.isFunction() ? "function " : "subroutine ");
PutLower(os, symbol) << '(';
int n = 0;
for (const auto &dummy : details.dummyArgs()) {
if (n++ > 0) {
os << ',';
}
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
PutLower(os, *dummy);
}
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
os << ')';
PutAttrs(os, bindAttrs, details.bindName(), " "s, ""s);
if (details.isFunction()) {
const Symbol &result{details.result()};
if (result.name() != symbol.name()) {
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
PutLower(os << " result(", result) << ')';
}
}
os << '\n';
// walk symbols, collect ones needed
ModFileWriter writer{context_};
std::stringstream typeBindings;
SubprogramSymbolCollector collector{symbol};
collector.Collect();
for (const Symbol *need : collector.symbols()) {
writer.PutSymbol(typeBindings, need);
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
}
CHECK(typeBindings.str().empty());
os << writer.uses_.str();
for (const Symbol *import : collector.imports()) {
decls_ << "import::" << import->name().ToString() << "\n";
}
os << writer.decls_.str();
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
os << "end\n";
if (isInterface) {
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
os << "end interface\n";
}
}
void ModFileWriter::PutGeneric(const Symbol &symbol) {
auto &details{symbol.get<GenericDetails>()};
decls_ << "generic";
[flang] Name resolution for defined operators Instead of tracking just genericName_ while in a generic interface block or generic statement, now we immediately create a symbol for it. A parser::Name isn't good enough because a defined-operator or defined-io-generic-spec doesn't have a name. Change the parse tree to add a source field to GenericSpec. Use these as names for symbols for defined-operator and defined-io-generic-spec (e.g. "operator(+)" or "read(formatted)"). Change the source for defined-op-name to include the dots so that they can be distinguished from normal symbols with the same name (e.g. you can have both ".foo." and "foo"). These symbols have names in the symbol table like ".foo.", not "operator(.foo.)", because references to them have that form. Add GenericKind enum to GenericDetails and GenericBindingDetails. This allows us to know a symbol is "assignment(=)", for example, without having to do a string comparison. Add GenericSpecInfo to handle analyzing the various kinds of generic-spec and generating symbol names and GenericKind for them. Add reference to LanguageFeatureControl to SemanticsContext so that they can be checked during semantics. For this change, if LogicalAbbreviations is enabled, report an error if the user tries to define an operator named ".T." or ".F.". Add resolve-name-utils.cc to hold utility functions and classes that don't have to be in the ResolveNamesVisitor class hierarchy. The goal is to reduce the size of resolve-names.cc where possible. Original-commit: flang-compiler/f18@3081f694e21dbcaef2554198a682c9af57f2e185 Reviewed-on: https://github.com/flang-compiler/f18/pull/338
2019-03-18 19:48:02 +01:00
PutAttrs(decls_, symbol.attrs()) << "::";
if (details.kind() == GenericKind::DefinedOp) {
PutLower(decls_ << "operator(", symbol) << ')';
} else {
PutLower(decls_, symbol);
}
decls_ << "=>";
int n = 0;
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
for (auto *specific : details.specificProcs()) {
if (n++ > 0) decls_ << ',';
PutLower(decls_, *specific);
}
decls_ << '\n';
}
void ModFileWriter::PutUse(const Symbol &symbol) {
auto &details{symbol.get<UseDetails>()};
auto &use{details.symbol()};
PutLower(uses_ << "use ", details.module());
PutLower(uses_ << ",only:", symbol);
if (use.name() != symbol.name()) {
PutLower(uses_ << "=>", use);
}
uses_ << '\n';
PutUseExtraAttr(Attr::VOLATILE, symbol, use);
PutUseExtraAttr(Attr::ASYNCHRONOUS, symbol, use);
}
// 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.
void ModFileWriter::PutUseExtraAttr(
Attr attr, const Symbol &local, const Symbol &use) {
if (local.attrs().test(attr) && !use.attrs().test(attr)) {
PutLower(useExtraAttrs_, AttrToString(attr)) << "::";
PutLower(useExtraAttrs_, local) << '\n';
}
}
// Collect the symbols of this scope sorted by their original order, not name.
// Namelists are an exception: they are sorted after other symbols.
std::vector<const Symbol *> CollectSymbols(const Scope &scope) {
std::set<const Symbol *> symbols; // to prevent duplicates
std::vector<const Symbol *> sorted;
std::vector<const Symbol *> namelist;
std::vector<const Symbol *> common;
sorted.reserve(scope.size() + scope.commonBlocks().size());
for (const auto &pair : scope) {
auto *symbol{pair.second};
if (!symbol->test(Symbol::Flag::ParentComp)) {
if (symbols.insert(symbol).second) {
if (symbol->has<NamelistDetails>()) {
namelist.push_back(symbol);
} else {
sorted.push_back(symbol);
}
}
}
}
for (const auto &pair : scope.commonBlocks()) {
const Symbol *symbol{pair.second};
if (symbols.insert(symbol).second) {
common.push_back(symbol);
}
}
// sort normal symbols, then namelists, then common blocks:
auto compareByOrder = [](const Symbol *x, const Symbol *y) {
CHECK(x != nullptr);
return x->name().begin() < y->name().begin();
};
auto cursor{sorted.begin()};
std::sort(cursor, sorted.end(), compareByOrder);
cursor = sorted.insert(sorted.end(), namelist.begin(), namelist.end());
std::sort(cursor, sorted.end(), compareByOrder);
cursor = sorted.insert(sorted.end(), common.begin(), common.end());
std::sort(cursor, sorted.end(), compareByOrder);
return sorted;
}
void PutEntity(std::ostream &os, const Symbol &symbol) {
std::visit(
common::visitors{
[&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); },
[&](const ProcEntityDetails &) { PutProcEntity(os, symbol); },
[&](const TypeParamDetails &) { PutTypeParam(os, symbol); },
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
[&](const auto &) {
common::die("PutEntity: unexpected details: %s",
[flang] Support writing interfaces in module files. Write symbols for external subprogram interfaces as interface-stmts. Those go in the decls part of the module file, as opposed to contained subprograms which go in the contains part. See modfile06.f90. Write symbols with GenericDetails to module files. The specific procedures of a generic interface are always written as procedure-stmts. If they also have specific interfaces those are written in a separate interface-stmt. See modfile07.f90. Fix a bug where `real, external :: f` was not written like `real f; external f`. We have to notice the EXTERNAL attribute on the type-declaration-stmt and convert the entity to a procedure entity. See modfile08.f90. Fix a bug where a use-associated symbol is referenced in a procedure-designator. We were not resolving that correctly. Change ModFileWriter::PutEntity to include the kind of Details when it reports an internal error due to a kind it can't handle. Make DetailsToString public to support that. Change test_errors.sh to fail if the f18 command exits due to a signal. We were missing bugs where the correct errors were written out but then module file writing crashed (due to failure to handle generics mentioned above). Non-zero exit status is okay because we are expecting compilation errors. Change test_modfile.sh to allow for the expected module file contents to be indented so the tests are easier to read. Original-commit: flang-compiler/f18@82a7931e51c63ba21f17261727e1e9dc4167dcc9 Reviewed-on: https://github.com/flang-compiler/f18/pull/132 Tree-same-pre-rewrite: false
2018-07-19 22:28:24 +02:00
DetailsToString(symbol.details()).c_str());
},
},
symbol.details());
os << '\n';
}
void PutShapeSpec(std::ostream &os, const ShapeSpec &x) {
if (x.lbound().isAssumed()) {
CHECK(x.ubound().isAssumed());
os << "..";
} else {
if (!x.lbound().isDeferred()) {
PutBound(os, x.lbound());
}
os << ':';
if (!x.ubound().isDeferred()) {
PutBound(os, x.ubound());
}
}
}
void PutShape(std::ostream &os, const ArraySpec &shape, char open, char close) {
if (!shape.empty()) {
os << open;
bool first{true};
for (const auto &shapeSpec : shape) {
if (first) {
first = false;
} else {
os << ',';
}
PutShapeSpec(os, shapeSpec);
}
os << close;
}
}
void PutObjectEntity(std::ostream &os, const Symbol &symbol) {
auto &details{symbol.get<ObjectEntityDetails>()};
PutEntity(os, symbol, [&]() {
auto *type{symbol.GetType()};
CHECK(type);
PutLower(os, *type);
});
PutShape(os, details.shape(), '(', ')');
PutShape(os, details.coshape(), '[', ']');
PutInit(os, details.init());
}
void PutProcEntity(std::ostream &os, const Symbol &symbol) {
const auto &details{symbol.get<ProcEntityDetails>()};
const ProcInterface &interface{details.interface()};
PutEntity(os, symbol, [&]() {
os << "procedure(";
if (interface.symbol()) {
PutLower(os, *interface.symbol());
} else if (interface.type()) {
PutLower(os, *interface.type());
}
os << ')';
PutPassName(os, details.passName());
});
}
void PutPassName(std::ostream &os, const std::optional<SourceName> &passName) {
if (passName) {
PutLower(os << ",pass(", passName->ToString()) << ')';
}
}
void PutTypeParam(std::ostream &os, const Symbol &symbol) {
auto &details{symbol.get<TypeParamDetails>()};
PutEntity(os, symbol, [&]() {
auto *type{symbol.GetType()};
CHECK(type);
PutLower(os, *type);
PutLower(os << ',', common::EnumToString(details.attr()));
});
PutInit(os, details.init());
}
void PutInit(std::ostream &os, const MaybeExpr &init) {
if (init) {
init->AsFortran(os << '=');
}
}
void PutInit(std::ostream &os, const MaybeIntExpr &init) {
if (init) {
init->AsFortran(os << '=');
}
}
void PutBound(std::ostream &os, const Bound &x) {
if (x.isAssumed()) {
os << '*';
} else if (x.isDeferred()) {
os << ':';
} else {
x.GetExplicit()->AsFortran(os);
}
}
// Write an entity (object or procedure) declaration.
// writeType is called to write out the type.
void PutEntity(
std::ostream &os, const Symbol &symbol, std::function<void()> writeType) {
writeType();
MaybeExpr bindName;
std::visit(
common::visitors{
[&](const SubprogramDetails &x) { bindName = x.bindName(); },
[&](const ObjectEntityDetails &x) { bindName = x.bindName(); },
[&](const ProcEntityDetails &x) { bindName = x.bindName(); },
[&](const auto &) {},
},
symbol.details());
PutAttrs(os, symbol.attrs(), bindName);
PutLower(os << "::", symbol);
}
// Put out each attribute to os, surrounded by `before` and `after` and
// mapped to lower case.
std::ostream &PutAttrs(std::ostream &os, Attrs attrs, const MaybeExpr &bindName,
std::string before, std::string after) {
attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC
attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL
if (bindName) {
bindName->AsFortran(os << before << "bind(c, name=") << ')' << after;
attrs.set(Attr::BIND_C, false);
}
for (std::size_t i{0}; i < Attr_enumSize; ++i) {
Attr attr{static_cast<Attr>(i)};
if (attrs.test(attr)) {
PutLower(os << before, AttrToString(attr)) << after;
}
}
return os;
}
std::ostream &PutLower(std::ostream &os, const Symbol &symbol) {
return PutLower(os, symbol.name().ToString());
}
std::ostream &PutLower(std::ostream &os, const DeclTypeSpec &type) {
std::stringstream s;
s << type;
return PutLower(os, s.str());
}
std::ostream &PutLower(std::ostream &os, const std::string &str) {
for (char c : str) {
os << parser::ToLowerCaseLetter(c);
}
return os;
}
// Write the module file at path, prepending header. Return false on error.
static bool WriteFile(const std::string &path, std::string &&contents) {
std::fstream stream;
auto header{GetHeader(contents)};
auto size{GetFileSize(path)};
if (size == header.size() + 1 + contents.size()) {
// file exists and has the right size, check the contents
stream.open(path, std::ios::in | std::ios::out);
if (FileContentsMatch(stream, header, contents)) {
return true;
}
stream.seekp(0);
} else {
stream.open(path, std::ios::out);
}
stream << header << '\n' << contents;
stream.close();
return !stream.fail();
}
// Return true if the stream matches what we would write for the mod file.
static bool FileContentsMatch(std::fstream &stream, const std::string &header,
const std::string &contents) {
char c;
for (std::size_t i{0}; i < header.size(); ++i) {
if (!stream.get(c) || c != header[i]) {
return false;
}
}
if (!stream.get(c) || c != '\n') {
return false;
}
for (std::size_t i{0}; i < contents.size(); ++i) {
if (!stream.get(c) || c != contents[i]) {
return false;
}
}
return !stream.get(c);
}
// 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.
template<typename Iter> static std::string CheckSum(Iter begin, Iter end) {
std::uint64_t hash{0xcbf29ce484222325ull};
for (auto it{begin}; it != end; ++it) {
char c{*it};
hash ^= c & 0xff;
hash *= 0x100000001b3;
}
static const char *digits = "0123456789abcdef";
std::string result(16, '0');
for (size_t i{16}; hash != 0; hash >>= 4) {
result[--i] = digits[hash & 0xf];
}
return result;
}
static bool VerifyHeader(const std::string &path) {
std::fstream stream{path};
std::string header;
std::getline(stream, header);
auto magicLen{strlen(magic)};
if (header.compare(0, magicLen, magic) != 0) {
return false;
}
std::string expectSum{header.substr(magicLen, 16)};
std::string actualSum{CheckSum(std::istreambuf_iterator<char>(stream),
std::istreambuf_iterator<char>())};
return expectSum == actualSum;
}
static std::string GetHeader(const std::string &all) {
std::stringstream ss;
ss << magic << CheckSum(all.begin(), all.end());
return ss.str();
}
static std::size_t GetFileSize(const std::string &path) {
struct stat statbuf;
if (stat(path.c_str(), &statbuf) == 0) {
return static_cast<std::size_t>(statbuf.st_size);
} else {
return 0;
}
}
Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
std::string ancestorName; // empty for module
if (ancestor) {
if (auto *scope{ancestor->FindSubmodule(name)}) {
return scope;
}
ancestorName = ancestor->name().ToString();
} else {
auto it{context_.globalScope().find(name)};
if (it != context_.globalScope().end()) {
return it->second->scope();
}
}
auto path{FindModFile(name, ancestorName)};
if (!path.has_value()) {
return nullptr;
}
// TODO: We are reading the file once to verify the checksum and then again
// to parse. Do it only reading the file once.
if (!VerifyHeader(*path)) {
context_.Say(name,
"Module file for '%s' has invalid checksum: %s"_err_en_US, name, *path);
return nullptr;
}
// TODO: Construct parsing with an AllSources reference to share provenance
parser::Parsing parsing;
parser::Options options;
options.isModuleFile = true;
parsing.Prescan(*path, options);
parsing.Parse(nullptr);
auto &parseTree{parsing.parseTree()};
if (!parsing.messages().empty() || !parsing.consumedWholeFile() ||
!parseTree.has_value()) {
context_.Say(
name, "Module file for '%s' is corrupt: %s"_err_en_US, name, *path);
return nullptr;
}
Scope *parentScope; // the scope this module/submodule goes into
if (!ancestor) {
parentScope = &context_.globalScope();
} else if (auto *parent{GetSubmoduleParent(*parseTree)}) {
parentScope = Read(*parent, ancestor);
} else {
parentScope = ancestor;
}
// TODO: Check that default kinds of intrinsic types match?
ResolveNames(context_, *parseTree);
const auto &it{parentScope->find(name)};
if (it == parentScope->end()) {
return nullptr;
}
auto &modSymbol{*it->second};
// TODO: Preserve the CookedSource rather than acquiring its string.
modSymbol.scope()->set_chars(std::string{parsing.cooked().AcquireData()});
modSymbol.set(Symbol::Flag::ModFile);
return modSymbol.scope();
}
std::optional<std::string> ModFileReader::FindModFile(
const SourceName &name, const std::string &ancestor) {
parser::Messages attachments;
for (auto &dir : context_.searchDirectories()) {
std::string path{
ModFilePath(dir, name, ancestor, context_.moduleFileSuffix())};
std::ifstream ifstream{path};
if (!ifstream.good()) {
attachments.Say(name, "%s: %s"_en_US, path, std::strerror(errno));
} else {
std::string line;
std::getline(ifstream, line);
if (line.compare(0, strlen(magic), magic) == 0) {
return path;
}
attachments.Say(name, "%s: Not a valid module file"_en_US, path);
}
}
auto error{parser::Message{name,
ancestor.empty()
? "Cannot find module file for '%s'"_err_en_US
: "Cannot find module file for submodule '%s' of module '%s'"_err_en_US,
name, ancestor}};
attachments.AttachTo(error);
context_.Say(std::move(error));
return std::nullopt;
}
// program was read from a .mod file for a submodule; return the name of the
// submodule's parent submodule, nullptr if none.
static const SourceName *GetSubmoduleParent(const parser::Program &program) {
CHECK(program.v.size() == 1);
auto &unit{program.v.front()};
auto &submod{std::get<common::Indirection<parser::Submodule>>(unit.u)};
auto &stmt{
std::get<parser::Statement<parser::SubmoduleStmt>>(submod.value().t)};
auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
if (auto &parent{std::get<std::optional<parser::Name>>(parentId.t)}) {
return &parent->source;
} else {
return nullptr;
}
}
// Construct the path to a module file. ancestorName not empty means submodule.
static std::string ModFilePath(const std::string &dir, const SourceName &name,
const std::string &ancestorName, const std::string &suffix) {
std::stringstream path;
if (dir != "."s) {
path << dir << '/';
}
if (!ancestorName.empty()) {
PutLower(path, ancestorName) << '-';
}
PutLower(path, name.ToString()) << suffix;
return path.str();
}
void SubprogramSymbolCollector::Collect() {
const auto &details{symbol_.get<SubprogramDetails>()};
isInterface_ = details.isInterface();
if (details.isFunction()) {
DoSymbol(details.result());
}
for (const Symbol *dummyArg : details.dummyArgs()) {
CHECK(dummyArg);
DoSymbol(*dummyArg);
}
for (const auto &pair : scope_) {
const Symbol *symbol{pair.second};
if (const auto *useDetails{symbol->detailsIf<UseDetails>()}) {
if (useSet_.count(&useDetails->symbol()) > 0) {
need_.push_back(symbol);
}
}
}
}
// Do symbols this one depends on; then add to need_
void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
const auto &scope{symbol.owner()};
if (scope != scope_ && scope.kind() != Scope::Kind::DerivedType) {
if (scope != scope_.parent()) {
useSet_.insert(&symbol);
} else if (isInterface_) {
imports_.insert(&symbol);
}
return;
}
if (!needSet_.insert(&symbol).second) {
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) {
for (const Symbol *object : details.objects()) {
DoSymbol(*object);
}
},
[](const auto &) {},
},
symbol.details());
if (!symbol.has<UseDetails>()) {
DoType(symbol.GetType());
}
if (scope.kind() != Scope::Kind::DerivedType) {
need_.push_back(&symbol);
}
}
void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) {
if (!type) {
return;
}
switch (type->category()) {
case DeclTypeSpec::Numeric:
case DeclTypeSpec::Logical: break; // nothing to do
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()}) {
DoSymbol(extends->typeSymbol());
}
for (const auto pair : derived->parameters()) {
DoParamValue(pair.second);
}
for (const auto pair : *typeSymbol.scope()) {
const auto &comp{*pair.second};
DoSymbol(comp);
}
DoSymbol(typeSymbol);
}
}
}
void SubprogramSymbolCollector::DoBound(const Bound &bound) {
if (const MaybeSubscriptIntExpr & expr{bound.GetExplicit()}) {
DoExpr(*expr);
}
}
void SubprogramSymbolCollector::DoParamValue(const ParamValue &paramValue) {
if (const auto &expr{paramValue.GetExplicit()}) {
DoExpr(*expr);
}
}
}