2018-05-01 12:50:34 -07:00
|
|
|
// Copyright (c) 2018, 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.
|
|
|
|
|
2018-04-12 14:20:26 -07:00
|
|
|
#include "resolve-names.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "attr.h"
|
2018-05-02 14:06:02 -07:00
|
|
|
#include "rewrite-parse-tree.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "scope.h"
|
|
|
|
#include "symbol.h"
|
|
|
|
#include "type.h"
|
2018-04-12 12:23:20 -07:00
|
|
|
#include "../parser/indirection.h"
|
|
|
|
#include "../parser/parse-tree-visitor.h"
|
|
|
|
#include "../parser/parse-tree.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
2018-05-02 14:06:02 -07:00
|
|
|
#include <ostream>
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <stack>
|
|
|
|
|
2018-03-23 12:24:29 -07:00
|
|
|
namespace Fortran::semantics {
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
using namespace parser::literals;
|
|
|
|
|
|
|
|
class MessageHandler;
|
|
|
|
|
|
|
|
// ImplicitRules maps initial character of identifier to the DeclTypeSpec*
|
|
|
|
// representing the implicit type; nullptr if none.
|
|
|
|
class ImplicitRules {
|
|
|
|
public:
|
|
|
|
ImplicitRules(MessageHandler &messages);
|
2018-04-11 13:11:42 -07:00
|
|
|
bool isImplicitNoneType() const { return isImplicitNoneType_; }
|
|
|
|
bool isImplicitNoneExternal() const { return isImplicitNoneExternal_; }
|
|
|
|
void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
|
|
|
|
void set_isImplicitNoneExternal(bool x) { isImplicitNoneExternal_ = x; }
|
2018-04-04 09:03:51 -07:00
|
|
|
// Get the implicit type for identifiers starting with ch. May be null.
|
|
|
|
const DeclTypeSpec *GetType(char ch) const;
|
|
|
|
// Record the implicit type for this range of characters.
|
|
|
|
void SetType(const DeclTypeSpec &type, parser::Location lo, parser::Location,
|
|
|
|
bool isDefault = false);
|
|
|
|
// Apply the default implicit rules (if no IMPLICIT NONE).
|
2018-04-11 13:11:42 -07:00
|
|
|
void AddDefaultRules();
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
2018-04-05 17:02:31 -07:00
|
|
|
static char Incr(char ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
MessageHandler &messages_;
|
2018-04-11 13:11:42 -07:00
|
|
|
bool isImplicitNoneType_{false};
|
|
|
|
bool isImplicitNoneExternal_{false};
|
2018-04-05 17:02:31 -07:00
|
|
|
// map initial character of identifier to nullptr or its default type
|
|
|
|
std::map<char, const DeclTypeSpec> map_;
|
2018-04-04 09:03:51 -07:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const ImplicitRules &);
|
2018-04-06 10:46:30 -07:00
|
|
|
friend void ShowImplicitRule(std::ostream &, const ImplicitRules &, char);
|
2018-04-04 09:03:51 -07:00
|
|
|
};
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
// Provide Post methods to collect attributes into a member variable.
|
|
|
|
class AttrsVisitor {
|
|
|
|
public:
|
2018-04-12 12:59:42 -07:00
|
|
|
void BeginAttrs();
|
|
|
|
Attrs EndAttrs();
|
2018-03-30 19:49:00 -07:00
|
|
|
void Post(const parser::LanguageBindingSpec &);
|
|
|
|
bool Pre(const parser::AccessSpec &);
|
|
|
|
bool Pre(const parser::IntentSpec &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Simple case: encountering CLASSNAME causes ATTRNAME to be set.
|
|
|
|
#define HANDLE_ATTR_CLASS(CLASSNAME, ATTRNAME) \
|
|
|
|
bool Pre(const parser::CLASSNAME &) { \
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::ATTRNAME); \
|
2018-03-30 13:57:23 -07:00
|
|
|
return false; \
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Elemental, ELEMENTAL)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Impure, IMPURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Module, MODULE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Non_Recursive, NON_RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Pure, PURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Recursive, RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(TypeAttrSpec::BindC, BIND_C)
|
|
|
|
HANDLE_ATTR_CLASS(Abstract, ABSTRACT)
|
|
|
|
HANDLE_ATTR_CLASS(Allocatable, ALLOCATABLE)
|
|
|
|
HANDLE_ATTR_CLASS(Asynchronous, ASYNCHRONOUS)
|
|
|
|
HANDLE_ATTR_CLASS(Contiguous, CONTIGUOUS)
|
|
|
|
HANDLE_ATTR_CLASS(External, EXTERNAL)
|
|
|
|
HANDLE_ATTR_CLASS(Intrinsic, INTRINSIC)
|
|
|
|
HANDLE_ATTR_CLASS(NoPass, NOPASS)
|
|
|
|
HANDLE_ATTR_CLASS(Optional, OPTIONAL)
|
|
|
|
HANDLE_ATTR_CLASS(Parameter, PARAMETER)
|
|
|
|
HANDLE_ATTR_CLASS(Pass, PASS)
|
|
|
|
HANDLE_ATTR_CLASS(Pointer, POINTER)
|
|
|
|
HANDLE_ATTR_CLASS(Protected, PROTECTED)
|
|
|
|
HANDLE_ATTR_CLASS(Save, SAVE)
|
|
|
|
HANDLE_ATTR_CLASS(Target, TARGET)
|
|
|
|
HANDLE_ATTR_CLASS(Value, VALUE)
|
|
|
|
HANDLE_ATTR_CLASS(Volatile, VOLATILE)
|
|
|
|
#undef HANDLE_ATTR_CLASS
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-03-30 19:49:00 -07:00
|
|
|
std::optional<Attrs> attrs_;
|
2018-03-22 17:08:20 -07:00
|
|
|
std::string langBindingName_{""};
|
2018-04-24 17:05:58 -07:00
|
|
|
|
|
|
|
Attr AccessSpecToAttr(const parser::AccessSpec &x) {
|
|
|
|
switch (x.v) {
|
|
|
|
case parser::AccessSpec::Kind::Public: return Attr::PUBLIC;
|
|
|
|
case parser::AccessSpec::Kind::Private: return Attr::PRIVATE;
|
|
|
|
}
|
2018-04-25 11:44:12 -07:00
|
|
|
// unnecessary but g++ warns "control reaches end of non-void function"
|
|
|
|
parser::die("unreachable");
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find and create types from declaration-type-spec nodes.
|
|
|
|
class DeclTypeSpecVisitor : public AttrsVisitor {
|
|
|
|
public:
|
|
|
|
using AttrsVisitor::Post;
|
2018-03-30 13:57:23 -07:00
|
|
|
using AttrsVisitor::Pre;
|
2018-04-12 12:59:42 -07:00
|
|
|
void BeginDeclTypeSpec();
|
|
|
|
void EndDeclTypeSpec();
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::IntegerTypeSpec &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Logical &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Real &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Complex &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::ClassStar &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::TypeStar &);
|
|
|
|
void Post(const parser::DeclarationTypeSpec::Type &);
|
|
|
|
void Post(const parser::DeclarationTypeSpec::Class &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Record &);
|
|
|
|
bool Pre(const parser::DerivedTypeSpec &);
|
|
|
|
void Post(const parser::TypeParamSpec &);
|
|
|
|
bool Pre(const parser::TypeParamValue &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::unique_ptr<DeclTypeSpec> declTypeSpec_;
|
2018-03-30 13:57:23 -07:00
|
|
|
std::unique_ptr<DerivedTypeSpec> derivedTypeSpec_;
|
|
|
|
std::unique_ptr<ParamValue> typeParamValue_;
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool expectDeclTypeSpec_{false}; // should only see decl-type-spec when true
|
2018-03-30 13:57:23 -07:00
|
|
|
void MakeIntrinsic(const IntrinsicTypeSpec &intrinsicTypeSpec);
|
|
|
|
void SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec);
|
2018-03-22 17:08:20 -07:00
|
|
|
static KindParamValue GetKindParamValue(
|
2018-03-30 13:57:23 -07:00
|
|
|
const std::optional<parser::KindSelector> &kind);
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// Track statement source locations and save messages.
|
|
|
|
class MessageHandler {
|
|
|
|
public:
|
|
|
|
using Message = parser::Message;
|
|
|
|
|
|
|
|
MessageHandler(parser::Messages &messages) : messages_{messages} {}
|
|
|
|
|
|
|
|
template<typename T> bool Pre(const parser::Statement<T> &x) {
|
|
|
|
currStmtSource_ = &x.source;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T> void Post(const parser::Statement<T> &) {
|
|
|
|
currStmtSource_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *currStmtSource() { return currStmtSource_; }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
// Emit a message associated with the current statement source.
|
|
|
|
void Say(Message &&);
|
|
|
|
void Say(parser::MessageFixedText &&);
|
|
|
|
void Say(parser::MessageFormattedText &&);
|
2018-04-11 13:11:42 -07:00
|
|
|
// Emit a message about a name or source location
|
|
|
|
void Say(const parser::Name &, parser::MessageFixedText &&);
|
2018-04-25 19:58:42 -07:00
|
|
|
void Say(const SourceName &, parser::MessageFixedText &&);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Where messages are emitted:
|
|
|
|
parser::Messages &messages_;
|
|
|
|
// Source location of current statement; null if not in a statement
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *currStmtSource_{nullptr};
|
2018-04-04 09:03:51 -07:00
|
|
|
};
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
|
2018-04-04 09:03:51 -07:00
|
|
|
class ImplicitRulesVisitor : public DeclTypeSpecVisitor, public MessageHandler {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
|
|
|
using DeclTypeSpecVisitor::Post;
|
|
|
|
using DeclTypeSpecVisitor::Pre;
|
2018-04-04 09:03:51 -07:00
|
|
|
using MessageHandler::Post;
|
|
|
|
using MessageHandler::Pre;
|
|
|
|
using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
|
|
|
|
|
|
|
|
ImplicitRulesVisitor(parser::Messages &messages) : MessageHandler(messages) {}
|
|
|
|
|
|
|
|
void Post(const parser::ParameterStmt &);
|
|
|
|
bool Pre(const parser::ImplicitStmt &);
|
|
|
|
bool Pre(const parser::LetterSpec &);
|
|
|
|
bool Pre(const parser::ImplicitSpec &);
|
|
|
|
void Post(const parser::ImplicitSpec &);
|
2018-04-11 13:11:42 -07:00
|
|
|
|
|
|
|
ImplicitRules &implicitRules() { return implicitRules_.top(); }
|
|
|
|
const ImplicitRules &implicitRules() const { return implicitRules_.top(); }
|
|
|
|
bool isImplicitNoneType() const {
|
|
|
|
return implicitRules().isImplicitNoneType();
|
|
|
|
}
|
2018-04-23 12:33:10 -07:00
|
|
|
bool isImplicitNoneExternal() const {
|
|
|
|
return implicitRules().isImplicitNoneExternal();
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
protected:
|
2018-04-17 15:04:08 -07:00
|
|
|
void PushScope();
|
2018-04-06 10:46:30 -07:00
|
|
|
void PopScope();
|
2018-04-17 15:04:08 -07:00
|
|
|
void CopyImplicitRules(); // copy from parent into this scope
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// implicit rules in effect for current scope
|
2018-04-06 10:46:30 -07:00
|
|
|
std::stack<ImplicitRules, std::list<ImplicitRules>> implicitRules_;
|
2018-04-04 09:03:51 -07:00
|
|
|
// previous occurence of these kinds of statements:
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *prevImplicit_{nullptr};
|
|
|
|
const SourceName *prevImplicitNone_{nullptr};
|
|
|
|
const SourceName *prevImplicitNoneType_{nullptr};
|
|
|
|
const SourceName *prevParameterStmt_{nullptr};
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
bool HandleImplicitNone(const std::list<ImplicitNoneNameSpec> &nameSpecs);
|
|
|
|
};
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
// Track array specifications. They can occur in AttrSpec, EntityDecl,
|
|
|
|
// ObjectDecl, DimensionStmt, CommonBlockObject, or BasedPointerStmt.
|
|
|
|
// 1. INTEGER, DIMENSION(10) :: x
|
|
|
|
// 2. INTEGER :: x(10)
|
|
|
|
// 3. ALLOCATABLE :: x(:)
|
|
|
|
// 4. DIMENSION :: x(10)
|
|
|
|
// 5. TODO: COMMON x(10)
|
|
|
|
// 6. TODO: BasedPointerStmt
|
|
|
|
class ArraySpecVisitor {
|
|
|
|
public:
|
|
|
|
const ArraySpec &arraySpec() {
|
|
|
|
return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:16:42 -07:00
|
|
|
void BeginArraySpec() { CHECK(attrArraySpec_.empty()); }
|
|
|
|
void EndArraySpec() { attrArraySpec_.clear(); }
|
|
|
|
void ClearArraySpec() { arraySpec_.clear(); }
|
2018-04-12 12:59:42 -07:00
|
|
|
|
|
|
|
bool Pre(const parser::ArraySpec &x) {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Post(const parser::AttrSpec &) {
|
|
|
|
if (!arraySpec_.empty()) {
|
|
|
|
// Example: integer, dimension(<1>) :: x(<2>)
|
|
|
|
// This saves <1> in attrArraySpec_ so we can process <2> into arraySpec_
|
|
|
|
CHECK(attrArraySpec_.empty());
|
|
|
|
attrArraySpec_.splice(attrArraySpec_.cbegin(), arraySpec_);
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pre(const parser::DeferredShapeSpecList &x) {
|
|
|
|
for (int i = 0; i < x.v; ++i) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeDeferred());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pre(const parser::AssumedShapeSpec &x) {
|
|
|
|
const auto &lb = x.v;
|
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeAssumed(GetBound(*lb)) : ShapeSpec::MakeAssumed());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pre(const parser::ExplicitShapeSpec &x) {
|
|
|
|
const auto &lb = std::get<std::optional<parser::SpecificationExpr>>(x.t);
|
|
|
|
const auto &ub = GetBound(std::get<parser::SpecificationExpr>(x.t));
|
|
|
|
arraySpec_.push_back(lb ? ShapeSpec::MakeExplicit(GetBound(*lb), ub)
|
|
|
|
: ShapeSpec::MakeExplicit(ub));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pre(const parser::AssumedImpliedSpec &x) {
|
|
|
|
const auto &lb = x.v;
|
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeImplied(GetBound(*lb)) : ShapeSpec::MakeImplied());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pre(const parser::AssumedRankSpec &) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeAssumedRank());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// arraySpec_ is populated by any ArraySpec
|
|
|
|
ArraySpec arraySpec_;
|
|
|
|
// When an ArraySpec is under an AttrSpec, it is moved into attrArraySpec_
|
|
|
|
ArraySpec attrArraySpec_;
|
|
|
|
|
|
|
|
Bound GetBound(const parser::SpecificationExpr &x) {
|
|
|
|
return Bound(IntExpr(x.v));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Manage a stack of Scopes
|
|
|
|
class ScopeHandler : public ImplicitRulesVisitor {
|
2018-04-04 09:03:51 -07:00
|
|
|
public:
|
2018-04-25 19:58:42 -07:00
|
|
|
ScopeHandler(parser::Messages &messages) : ImplicitRulesVisitor(messages) {
|
2018-04-04 09:03:51 -07:00
|
|
|
PushScope(Scope::globalScope);
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
Scope &CurrScope() { return *scopes_.top(); }
|
2018-04-17 15:04:08 -07:00
|
|
|
void PushScope(Scope &scope) {
|
2018-04-04 09:03:51 -07:00
|
|
|
scopes_.push(&scope);
|
2018-04-17 15:04:08 -07:00
|
|
|
ImplicitRulesVisitor::PushScope();
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
void PopScope() {
|
2018-04-25 19:58:42 -07:00
|
|
|
ApplyImplicitRules();
|
2018-04-04 09:03:51 -07:00
|
|
|
scopes_.pop();
|
2018-04-06 10:46:30 -07:00
|
|
|
ImplicitRulesVisitor::PopScope();
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Helpers to make a Symbol in the current scope
|
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(
|
|
|
|
const parser::Name &name, const Attrs &attrs, D &&details) {
|
|
|
|
const auto &it = CurrScope().find(name.source);
|
|
|
|
auto &symbol = it->second;
|
|
|
|
if (it == CurrScope().end()) {
|
|
|
|
const auto pair = CurrScope().try_emplace(name.source, attrs, details);
|
|
|
|
CHECK(pair.second); // name was not found, so must be able to add
|
|
|
|
return pair.first->second;
|
|
|
|
}
|
|
|
|
symbol.add_occurrence(name.source);
|
|
|
|
if (symbol.has<UnknownDetails>()) {
|
|
|
|
// update the existing symbol
|
|
|
|
symbol.attrs() |= attrs;
|
|
|
|
symbol.set_details(details);
|
|
|
|
return symbol;
|
|
|
|
} else if (std::is_same<UnknownDetails, D>::value) {
|
|
|
|
symbol.attrs() |= attrs;
|
|
|
|
return symbol;
|
|
|
|
} else {
|
|
|
|
Say(name, "'%s' is already declared in this scoping unit"_err_en_US);
|
|
|
|
Say(symbol.name(), "Previous declaration of '%s'"_en_US);
|
|
|
|
// replace the old symbols with a new one with correct details
|
|
|
|
CurrScope().erase(symbol.name());
|
|
|
|
return MakeSymbol(name, attrs, details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(const parser::Name &name, D &&details) {
|
|
|
|
return MakeSymbol(name, Attrs(), details);
|
|
|
|
}
|
|
|
|
Symbol &MakeSymbol(const parser::Name &name, Attrs attrs = Attrs{}) {
|
|
|
|
return MakeSymbol(name, attrs, UnknownDetails());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Stack of containing scopes; memory referenced is owned by parent scopes
|
|
|
|
std::stack<Scope *, std::list<Scope *>> scopes_;
|
|
|
|
|
|
|
|
// On leaving a scope, add implicit types if appropriate.
|
|
|
|
void ApplyImplicitRules();
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModuleVisitor : public ScopeHandler {
|
|
|
|
public:
|
|
|
|
using ImplicitRulesVisitor::Post;
|
|
|
|
using ImplicitRulesVisitor::Pre;
|
|
|
|
|
|
|
|
ModuleVisitor(parser::Messages & messages) : ScopeHandler(messages) {}
|
|
|
|
|
|
|
|
bool Pre(const parser::ModuleStmt &);
|
|
|
|
void Post(const parser::EndModuleStmt &);
|
|
|
|
bool Pre(const parser::AccessStmt &);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The default access spec for this module.
|
|
|
|
Attr defaultAccess_{Attr::PUBLIC};
|
|
|
|
// The location of the last AccessStmt without access-ids, if any.
|
|
|
|
const SourceName *prevAccessStmt_{nullptr};
|
|
|
|
void SetAccess(const parser::Name &, Attr);
|
|
|
|
void ApplyDefaultAccess();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Walk the parse tree and resolve names to symbols.
|
|
|
|
class ResolveNamesVisitor : public ArraySpecVisitor, public ModuleVisitor {
|
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
|
|
|
using ModuleVisitor::Post;
|
|
|
|
using ModuleVisitor::Pre;
|
|
|
|
|
|
|
|
ResolveNamesVisitor(parser::Messages & messages)
|
|
|
|
: ModuleVisitor(messages) {}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Default action for a parse tree node is to visit children.
|
|
|
|
template<typename T> bool Pre(const T &) { return true; }
|
|
|
|
template<typename T> void Post(const T &) {}
|
|
|
|
|
|
|
|
bool Pre(const parser::TypeDeclarationStmt &);
|
|
|
|
void Post(const parser::TypeDeclarationStmt &);
|
|
|
|
void Post(const parser::EntityDecl &);
|
2018-04-12 12:59:42 -07:00
|
|
|
void Post(const parser::ObjectDecl &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::PrefixSpec &);
|
2018-04-11 13:11:42 -07:00
|
|
|
bool Pre(const parser::AsynchronousStmt &);
|
|
|
|
bool Pre(const parser::ContiguousStmt &);
|
|
|
|
bool Pre(const parser::ExternalStmt &);
|
|
|
|
bool Pre(const parser::IntrinsicStmt &);
|
|
|
|
bool Pre(const parser::OptionalStmt &);
|
|
|
|
bool Pre(const parser::ProtectedStmt &);
|
|
|
|
bool Pre(const parser::ValueStmt &);
|
|
|
|
bool Pre(const parser::VolatileStmt &);
|
|
|
|
void Post(const parser::SpecificationPart &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::Suffix &);
|
2018-04-17 14:16:42 -07:00
|
|
|
bool Pre(const parser::StmtFunctionStmt &);
|
|
|
|
void Post(const parser::StmtFunctionStmt &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::SubroutineStmt &);
|
|
|
|
void Post(const parser::SubroutineStmt &);
|
2018-04-17 14:16:42 -07:00
|
|
|
void Post(const parser::EndSubroutineStmt &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::FunctionStmt &);
|
|
|
|
void Post(const parser::FunctionStmt &);
|
2018-04-17 14:16:42 -07:00
|
|
|
void Post(const parser::EndFunctionStmt &);
|
2018-04-11 13:11:42 -07:00
|
|
|
bool Pre(const parser::MainProgram &);
|
|
|
|
void Post(const parser::EndProgramStmt &);
|
2018-03-30 13:57:23 -07:00
|
|
|
void Post(const parser::Program &);
|
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
bool Pre(const parser::AllocatableStmt &) {
|
|
|
|
objectDeclAttr_ = Attr::ALLOCATABLE;
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-17 14:16:42 -07:00
|
|
|
void Post(const parser::AllocatableStmt &) { objectDeclAttr_ = std::nullopt; }
|
2018-04-12 12:59:42 -07:00
|
|
|
bool Pre(const parser::TargetStmt &x) {
|
|
|
|
objectDeclAttr_ = Attr::TARGET;
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-17 14:16:42 -07:00
|
|
|
void Post(const parser::TargetStmt &) { objectDeclAttr_ = std::nullopt; }
|
2018-04-12 12:59:42 -07:00
|
|
|
void Post(const parser::DimensionStmt::Declaration &);
|
2018-04-11 13:11:42 -07:00
|
|
|
|
2018-04-17 14:16:42 -07:00
|
|
|
void Post(const parser::Expr &x) { CheckImplicitSymbol(GetVariableName(x)); }
|
2018-04-11 13:11:42 -07:00
|
|
|
void Post(const parser::Variable &x) {
|
|
|
|
CheckImplicitSymbol(GetVariableName(x));
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:33:10 -07:00
|
|
|
void Post(const parser::ProcedureDesignator &x) {
|
|
|
|
if (const auto *name = std::get_if<parser::Name>(&x.u)) {
|
|
|
|
Symbol &symbol{MakeSymbol(*name)};
|
|
|
|
if (symbol.has<UnknownDetails>()) {
|
|
|
|
if (isImplicitNoneExternal() && !symbol.attrs().test(Attr::EXTERNAL)) {
|
|
|
|
Say(*name,
|
|
|
|
"'%s' is an external procedure without the EXTERNAL"
|
|
|
|
" attribute in a scope with IMPLICIT NONE(EXTERNAL)"_err_en_US);
|
|
|
|
}
|
|
|
|
symbol.attrs().set(Attr::EXTERNAL);
|
|
|
|
symbol.set_details(SubprogramDetails{});
|
|
|
|
} else if (!symbol.has<SubprogramDetails>()) {
|
|
|
|
auto *details = symbol.detailsIf<EntityDetails>();
|
|
|
|
if (!details || !details->isArray()) {
|
|
|
|
Say(*name,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
|
|
|
|
Say(symbol.name(), "Declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
private:
|
2018-04-11 13:11:42 -07:00
|
|
|
// Function result name from parser::Suffix, if any.
|
|
|
|
const parser::Name *funcResultName_{nullptr};
|
2018-04-12 12:59:42 -07:00
|
|
|
// The attribute corresponding to the statement containing an ObjectDecl
|
|
|
|
std::optional<Attr> objectDeclAttr_;
|
2018-04-18 15:50:36 -07:00
|
|
|
// Set when we see a stmt function that is really an array element assignment
|
2018-04-18 15:06:35 -07:00
|
|
|
bool badStmtFuncFound_{false};
|
2018-04-11 13:11:42 -07:00
|
|
|
|
|
|
|
// Create a subprogram symbol in the current scope and push a new scope.
|
2018-04-17 15:04:08 -07:00
|
|
|
Symbol &PushSubprogramScope(const parser::Name &);
|
2018-04-11 13:11:42 -07:00
|
|
|
|
|
|
|
// Handle a statement that sets an attribute on a list of names.
|
|
|
|
bool HandleAttributeStmt(Attr, const std::list<parser::Name> &);
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclareEntity(const parser::Name &, Attrs);
|
2018-04-24 17:05:58 -07:00
|
|
|
const parser::Name *GetVariableName(const parser::DataRef &);
|
|
|
|
const parser::Name *GetVariableName(const parser::Designator &);
|
|
|
|
const parser::Name *GetVariableName(const parser::Expr &);
|
|
|
|
const parser::Name *GetVariableName(const parser::Variable &);
|
|
|
|
void CheckImplicitSymbol(const parser::Name *);
|
2018-03-30 13:57:23 -07:00
|
|
|
};
|
2018-03-23 12:24:29 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// ImplicitRules implementation
|
|
|
|
|
2018-04-06 10:46:30 -07:00
|
|
|
ImplicitRules::ImplicitRules(MessageHandler &messages) : messages_{messages} {}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
const DeclTypeSpec *ImplicitRules::GetType(char ch) const {
|
2018-04-05 17:02:31 -07:00
|
|
|
auto it = map_.find(ch);
|
|
|
|
return it != map_.end() ? &it->second : nullptr;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// isDefault is set when we are applying the default rules, so it is not
|
|
|
|
// an error if the type is already set.
|
|
|
|
void ImplicitRules::SetType(const DeclTypeSpec &type, parser::Location lo,
|
|
|
|
parser::Location hi, bool isDefault) {
|
2018-04-05 17:02:31 -07:00
|
|
|
for (char ch = *lo; ch; ch = ImplicitRules::Incr(ch)) {
|
|
|
|
auto res = map_.emplace(ch, type);
|
|
|
|
if (!res.second && !isDefault) {
|
2018-04-04 09:03:51 -07:00
|
|
|
messages_.Say(parser::Message{lo,
|
|
|
|
parser::MessageFormattedText{
|
2018-04-05 17:02:31 -07:00
|
|
|
"More than one implicit type specified for '%c'"_err_en_US, ch}});
|
|
|
|
}
|
|
|
|
if (ch == *hi) {
|
|
|
|
break;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
void ImplicitRules::AddDefaultRules() {
|
2018-04-04 09:03:51 -07:00
|
|
|
SetType(DeclTypeSpec::MakeIntrinsic(IntegerTypeSpec::Make()), "i", "n", true);
|
|
|
|
SetType(DeclTypeSpec::MakeIntrinsic(RealTypeSpec::Make()), "a", "z", true);
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Return the next char after ch in a way that works for ASCII or EBCDIC.
|
|
|
|
// Return '\0' for the char after 'z'.
|
|
|
|
char ImplicitRules::Incr(char ch) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'i': return 'j';
|
|
|
|
case 'r': return 's';
|
|
|
|
case 'z': return '\0';
|
|
|
|
default: return ch + 1;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &o, const ImplicitRules &implicitRules) {
|
|
|
|
o << "ImplicitRules:\n";
|
2018-04-05 17:02:31 -07:00
|
|
|
for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, '_');
|
|
|
|
ShowImplicitRule(o, implicitRules, '$');
|
|
|
|
ShowImplicitRule(o, implicitRules, '@');
|
2018-04-04 09:03:51 -07:00
|
|
|
return o;
|
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
void ShowImplicitRule(
|
|
|
|
std::ostream &o, const ImplicitRules &implicitRules, char ch) {
|
|
|
|
auto it = implicitRules.map_.find(ch);
|
|
|
|
if (it != implicitRules.map_.end()) {
|
|
|
|
o << " " << ch << ": " << it->second << '\n';
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// AttrsVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void AttrsVisitor::BeginAttrs() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(!attrs_);
|
2018-03-30 19:49:00 -07:00
|
|
|
attrs_ = std::make_optional<Attrs>();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
Attrs AttrsVisitor::EndAttrs() {
|
2018-03-30 19:49:00 -07:00
|
|
|
CHECK(attrs_);
|
|
|
|
Attrs result{*attrs_};
|
2018-03-30 13:57:23 -07:00
|
|
|
attrs_.reset();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
void AttrsVisitor::Post(const parser::LanguageBindingSpec &x) {
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::BIND_C);
|
2018-03-30 13:57:23 -07:00
|
|
|
if (x.v) {
|
|
|
|
// TODO: set langBindingName_ from ScalarDefaultCharConstantExpr
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::AccessSpec &x) {
|
2018-04-24 17:05:58 -07:00
|
|
|
attrs_->set(AccessSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::IntentSpec &x) {
|
|
|
|
switch (x.v) {
|
2018-04-04 09:03:51 -07:00
|
|
|
case parser::IntentSpec::Intent::In: attrs_->set(Attr::INTENT_IN); break;
|
|
|
|
case parser::IntentSpec::Intent::Out: attrs_->set(Attr::INTENT_OUT); break;
|
2018-03-30 13:57:23 -07:00
|
|
|
case parser::IntentSpec::Intent::InOut:
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::INTENT_IN);
|
|
|
|
attrs_->set(Attr::INTENT_OUT);
|
2018-03-30 13:57:23 -07:00
|
|
|
break;
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// DeclTypeSpecVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::BeginDeclTypeSpec() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(!expectDeclTypeSpec_);
|
|
|
|
expectDeclTypeSpec_ = true;
|
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::EndDeclTypeSpec() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(expectDeclTypeSpec_);
|
|
|
|
expectDeclTypeSpec_ = false;
|
|
|
|
declTypeSpec_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::ClassStar &x) {
|
|
|
|
SetDeclTypeSpec(DeclTypeSpec::MakeClassStar());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::TypeStar &x) {
|
|
|
|
SetDeclTypeSpec(DeclTypeSpec::MakeTypeStar());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DerivedTypeSpec &x) {
|
|
|
|
CHECK(!derivedTypeSpec_);
|
|
|
|
derivedTypeSpec_ =
|
|
|
|
std::make_unique<DerivedTypeSpec>(std::get<parser::Name>(x.t).ToString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeParamSpec &x) {
|
|
|
|
if (const auto &keyword = std::get<std::optional<parser::Keyword>>(x.t)) {
|
|
|
|
derivedTypeSpec_->AddParamValue(keyword->v.ToString(), *typeParamValue_);
|
|
|
|
} else {
|
|
|
|
derivedTypeSpec_->AddParamValue(*typeParamValue_);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
typeParamValue_.reset();
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::TypeParamValue &x) {
|
2018-04-04 09:03:51 -07:00
|
|
|
typeParamValue_ = std::make_unique<ParamValue>(std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[&](const parser::ScalarIntExpr &x) { return Bound{IntExpr{x}}; },
|
|
|
|
[&](const parser::Star &x) { return Bound::ASSUMED; },
|
|
|
|
[&](const parser::TypeParamValue::Deferred &x) {
|
|
|
|
return Bound::DEFERRED;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 19:49:00 -07:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::Type &) {
|
2018-03-30 13:57:23 -07:00
|
|
|
SetDeclTypeSpec(
|
2018-03-30 19:49:00 -07:00
|
|
|
DeclTypeSpec::MakeTypeDerivedType(std::move(derivedTypeSpec_)));
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-30 19:49:00 -07:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::Class &) {
|
2018-03-30 13:57:23 -07:00
|
|
|
SetDeclTypeSpec(
|
2018-03-30 19:49:00 -07:00
|
|
|
DeclTypeSpec::MakeClassDerivedType(std::move(derivedTypeSpec_)));
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::Record &x) {
|
|
|
|
// TODO
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntegerTypeSpec &x) {
|
|
|
|
MakeIntrinsic(IntegerTypeSpec::Make(GetKindParamValue(x.v)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Logical &x) {
|
|
|
|
MakeIntrinsic(LogicalTypeSpec::Make(GetKindParamValue(x.kind)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Real &x) {
|
|
|
|
MakeIntrinsic(RealTypeSpec::Make(GetKindParamValue(x.kind)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Complex &x) {
|
|
|
|
MakeIntrinsic(ComplexTypeSpec::Make(GetKindParamValue(x.kind)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::MakeIntrinsic(
|
|
|
|
const IntrinsicTypeSpec &intrinsicTypeSpec) {
|
|
|
|
SetDeclTypeSpec(DeclTypeSpec::MakeIntrinsic(intrinsicTypeSpec));
|
|
|
|
}
|
|
|
|
// Check that we're expecting to see a DeclTypeSpec (and haven't seen one yet)
|
|
|
|
// and save it in declTypeSpec_.
|
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec) {
|
2018-04-04 09:03:51 -07:00
|
|
|
CHECK(expectDeclTypeSpec_);
|
|
|
|
CHECK(!declTypeSpec_);
|
2018-03-30 13:57:23 -07:00
|
|
|
declTypeSpec_ = std::make_unique<DeclTypeSpec>(declTypeSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
KindParamValue DeclTypeSpecVisitor::GetKindParamValue(
|
|
|
|
const std::optional<parser::KindSelector> &kind) {
|
|
|
|
if (!kind) {
|
|
|
|
return KindParamValue();
|
|
|
|
} else if (const auto *expr =
|
|
|
|
std::get_if<parser::ScalarIntConstantExpr>(&kind->u)) {
|
|
|
|
const auto &lit =
|
|
|
|
std::get<parser::LiteralConstant>(expr->thing.thing.thing->u);
|
|
|
|
const auto &intlit = std::get<parser::IntLiteralConstant>(lit.u);
|
|
|
|
return KindParamValue(std::get<std::uint64_t>(intlit.t));
|
|
|
|
} else {
|
|
|
|
CHECK(!"TODO: translate star-size to kind");
|
2018-04-27 12:43:16 -07:00
|
|
|
return {}; // silence compiler warning
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// MessageHandler implementation
|
|
|
|
|
|
|
|
void MessageHandler::Say(Message &&x) { messages_.Put(std::move(x)); }
|
|
|
|
|
|
|
|
void MessageHandler::Say(parser::MessageFixedText &&x) {
|
|
|
|
CHECK(currStmtSource_);
|
2018-04-23 16:18:48 -07:00
|
|
|
messages_.Put(Message{*currStmtSource_, std::move(x)});
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::Say(parser::MessageFormattedText &&x) {
|
|
|
|
CHECK(currStmtSource_);
|
2018-04-23 16:18:48 -07:00
|
|
|
messages_.Put(Message{*currStmtSource_, std::move(x)});
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
void MessageHandler::Say(
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName &name, parser::MessageFixedText &&msg) {
|
2018-04-24 17:05:58 -07:00
|
|
|
Say(Message{
|
2018-04-25 19:58:42 -07:00
|
|
|
name, parser::MessageFormattedText{msg, name.ToString().c_str()}});
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
void MessageHandler::Say(
|
|
|
|
const parser::Name &name, parser::MessageFixedText &&msg) {
|
|
|
|
Say(name.source, std::move(msg));
|
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// ImplicitRulesVisitor implementation
|
|
|
|
|
|
|
|
void ImplicitRulesVisitor::Post(const parser::ParameterStmt &x) {
|
|
|
|
prevParameterStmt_ = currStmtSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::ImplicitStmt &x) {
|
|
|
|
bool res = std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[&](const std::list<ImplicitNoneNameSpec> &x) {
|
|
|
|
return HandleImplicitNone(x);
|
|
|
|
},
|
|
|
|
[&](const std::list<parser::ImplicitSpec> &x) {
|
|
|
|
if (prevImplicitNoneType_) {
|
|
|
|
Say("IMPLICIT statement after IMPLICIT NONE or "
|
|
|
|
"IMPLICIT NONE(TYPE) statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
prevImplicit_ = currStmtSource();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::LetterSpec &x) {
|
2018-04-05 17:02:31 -07:00
|
|
|
auto loLoc = std::get<parser::Location>(x.t);
|
|
|
|
auto hiLoc = loLoc;
|
2018-04-04 09:03:51 -07:00
|
|
|
if (auto hiLocOpt = std::get<std::optional<parser::Location>>(x.t)) {
|
|
|
|
hiLoc = *hiLocOpt;
|
|
|
|
if (*hiLoc < *loLoc) {
|
|
|
|
Say(Message{hiLoc,
|
|
|
|
parser::MessageFormattedText{
|
|
|
|
"'%c' does not follow '%c' alphabetically"_err_en_US, *hiLoc,
|
|
|
|
*loLoc}});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().SetType(*declTypeSpec_.get(), loLoc, hiLoc);
|
2018-04-04 09:03:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::ImplicitSpec &) {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginDeclTypeSpec();
|
2018-04-04 09:03:51 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImplicitRulesVisitor::Post(const parser::ImplicitSpec &) {
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:04:08 -07:00
|
|
|
void ImplicitRulesVisitor::PushScope() {
|
|
|
|
implicitRules_.push(ImplicitRules(*this));
|
2018-04-04 09:03:51 -07:00
|
|
|
prevImplicit_ = nullptr;
|
|
|
|
prevImplicitNone_ = nullptr;
|
|
|
|
prevImplicitNoneType_ = nullptr;
|
|
|
|
prevParameterStmt_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-17 15:04:08 -07:00
|
|
|
void ImplicitRulesVisitor::CopyImplicitRules() {
|
|
|
|
implicitRules_.pop();
|
|
|
|
implicitRules_.push(ImplicitRules(implicitRules_.top()));
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:46:30 -07:00
|
|
|
void ImplicitRulesVisitor::PopScope() { implicitRules_.pop(); }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
// TODO: for all of these errors, reference previous statement too
|
|
|
|
bool ImplicitRulesVisitor::HandleImplicitNone(
|
|
|
|
const std::list<ImplicitNoneNameSpec> &nameSpecs) {
|
|
|
|
if (prevImplicitNone_ != nullptr) {
|
|
|
|
Say("More than one IMPLICIT NONE statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prevParameterStmt_ != nullptr) {
|
|
|
|
Say("IMPLICIT NONE statement after PARAMETER statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
prevImplicitNone_ = currStmtSource();
|
|
|
|
if (nameSpecs.empty()) {
|
|
|
|
prevImplicitNoneType_ = currStmtSource();
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneType(true);
|
2018-04-04 09:03:51 -07:00
|
|
|
if (prevImplicit_) {
|
|
|
|
Say("IMPLICIT NONE statement after IMPLICIT statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int sawType{0};
|
|
|
|
int sawExternal{0};
|
|
|
|
for (const auto noneSpec : nameSpecs) {
|
|
|
|
switch (noneSpec) {
|
|
|
|
case ImplicitNoneNameSpec::External:
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneExternal(true);
|
2018-04-05 17:02:31 -07:00
|
|
|
++sawExternal;
|
2018-04-04 09:03:51 -07:00
|
|
|
break;
|
|
|
|
case ImplicitNoneNameSpec::Type:
|
|
|
|
prevImplicitNoneType_ = currStmtSource();
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneType(true);
|
2018-04-04 09:03:51 -07:00
|
|
|
if (prevImplicit_) {
|
|
|
|
Say("IMPLICIT NONE(TYPE) after IMPLICIT statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-05 17:02:31 -07:00
|
|
|
++sawType;
|
2018-04-04 09:03:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sawType > 1) {
|
|
|
|
Say("TYPE specified more than once in IMPLICIT NONE statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sawExternal > 1) {
|
|
|
|
Say("EXTERNAL specified more than once in IMPLICIT NONE statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ScopeHandler implementation
|
|
|
|
|
|
|
|
void ScopeHandler::ApplyImplicitRules() {
|
|
|
|
if (!isImplicitNoneType()) {
|
|
|
|
implicitRules().AddDefaultRules();
|
|
|
|
for (auto &pair : CurrScope()) {
|
|
|
|
Symbol &symbol = pair.second;
|
|
|
|
if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(EntityDetails());
|
|
|
|
}
|
|
|
|
if (auto *details = symbol.detailsIf<EntityDetails>()) {
|
|
|
|
if (!details->type()) {
|
|
|
|
const auto &name = pair.first;
|
|
|
|
if (const auto *type = implicitRules().GetType(name.begin()[0])) {
|
|
|
|
details->set_type(*type);
|
|
|
|
} else {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleVisitor implementation
|
|
|
|
|
|
|
|
bool ModuleVisitor::Pre(const parser::ModuleStmt &stmt) {
|
|
|
|
const auto &name = stmt.v;
|
|
|
|
auto &symbol = MakeSymbol(name, ModuleDetails{});
|
|
|
|
ModuleDetails &details{symbol.details<ModuleDetails>()};
|
|
|
|
Scope &modScope = CurrScope().MakeScope(Scope::Kind::Module, &symbol);
|
|
|
|
PushScope(modScope);
|
|
|
|
MakeSymbol(name, ModuleDetails{details});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::Post(const parser::EndModuleStmt &) {
|
|
|
|
ApplyDefaultAccess();
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::ApplyDefaultAccess() {
|
|
|
|
for (auto &pair : CurrScope()) {
|
|
|
|
Symbol &symbol = pair.second;
|
|
|
|
if (!symbol.attrs().HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
symbol.attrs().set(defaultAccess_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// ResolveNamesVisitor implementation
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::Post(const parser::EntityDecl &x) {
|
|
|
|
// TODO: may be under StructureStmt
|
2018-03-30 19:49:00 -07:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t)};
|
2018-04-12 12:59:42 -07:00
|
|
|
// TODO: CoarraySpec, CharLength, Initialization
|
|
|
|
DeclareEntity(name, attrs_ ? *attrs_ : Attrs());
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::TypeDeclarationStmt &x) {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginDeclTypeSpec();
|
|
|
|
BeginAttrs();
|
|
|
|
BeginArraySpec();
|
2018-03-30 13:57:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-23 12:24:29 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::TypeDeclarationStmt &x) {
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
|
|
|
EndAttrs();
|
|
|
|
EndArraySpec();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::PrefixSpec &x) {
|
2018-03-30 13:57:23 -07:00
|
|
|
return true; // TODO
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::AsynchronousStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::ASYNCHRONOUS, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ContiguousStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::CONTIGUOUS, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ExternalStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::EXTERNAL, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::IntrinsicStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::INTRINSIC, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::OptionalStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::OPTIONAL, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ProtectedStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::PROTECTED, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ValueStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::VALUE, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::VolatileStmt &x) {
|
|
|
|
return HandleAttributeStmt(Attr::VOLATILE, x.v);
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::HandleAttributeStmt(
|
|
|
|
Attr attr, const std::list<parser::Name> &names) {
|
|
|
|
for (const auto &name : names) {
|
|
|
|
const auto pair = CurrScope().try_emplace(name.source, Attrs{attr});
|
|
|
|
if (!pair.second) {
|
|
|
|
// symbol was already there: set attribute on it
|
|
|
|
pair.first->second.attrs().set(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::ObjectDecl &x) {
|
|
|
|
CHECK(objectDeclAttr_.has_value());
|
|
|
|
const auto &name = std::get<parser::ObjectName>(x.t);
|
|
|
|
DeclareEntity(name, Attrs{*objectDeclAttr_});
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
|
|
|
|
void ResolveNamesVisitor::Post(const parser::DimensionStmt::Declaration &x) {
|
|
|
|
const auto &name = std::get<parser::Name>(x.t);
|
|
|
|
DeclareEntity(name, Attrs{});
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
|
|
|
|
void ResolveNamesVisitor::DeclareEntity(const parser::Name &name, Attrs attrs) {
|
|
|
|
Symbol &symbol{MakeSymbol(name, attrs)}; // TODO: check attribute consistency
|
|
|
|
if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(EntityDetails());
|
|
|
|
}
|
|
|
|
if (EntityDetails *details = symbol.detailsIf<EntityDetails>()) {
|
|
|
|
if (declTypeSpec_) {
|
|
|
|
if (details->type().has_value()) {
|
|
|
|
Say(name, "The type of '%s' has already been declared"_err_en_US);
|
|
|
|
} else {
|
|
|
|
details->set_type(*declTypeSpec_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!arraySpec().empty()) {
|
|
|
|
if (!details->shape().empty()) {
|
|
|
|
Say(name,
|
|
|
|
"The dimensions of '%s' have already been declared"_err_en_US);
|
|
|
|
} else {
|
|
|
|
details->set_shape(arraySpec());
|
|
|
|
}
|
|
|
|
ClearArraySpec();
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
} else {
|
|
|
|
Say(name, "'%s' is already declared in this scoping unit"_err_en_US);
|
|
|
|
Say(symbol.name(), "Previous declaration of '%s'"_en_US);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
|
2018-04-24 17:05:58 -07:00
|
|
|
Attr accessAttr = AccessSpecToAttr(std::get<parser::AccessSpec>(x.t));
|
|
|
|
const auto &accessIds = std::get<std::list<parser::AccessId>>(x.t);
|
|
|
|
if (accessIds.empty()) {
|
|
|
|
if (prevAccessStmt_) {
|
|
|
|
Say("The default accessibility of this module has already "
|
|
|
|
"been declared"_err_en_US);
|
|
|
|
Say(*prevAccessStmt_, "Previous declaration"_en_US);
|
|
|
|
}
|
|
|
|
prevAccessStmt_ = currStmtSource();
|
|
|
|
defaultAccess_ = accessAttr;
|
|
|
|
} else {
|
|
|
|
for (const auto &accessId : accessIds) {
|
|
|
|
std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[=](const parser::Name &y) { SetAccess(y, accessAttr); },
|
|
|
|
[=](const parser::Indirection<parser::GenericSpec> &y) {
|
|
|
|
std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[=](const parser::Name &z) {
|
|
|
|
SetAccess(z, accessAttr);
|
|
|
|
},
|
|
|
|
[](const auto &) { parser::die("TODO: GenericSpec"); },
|
|
|
|
},
|
|
|
|
y->u);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
accessId.u);
|
|
|
|
}
|
2018-05-04 11:18:34 +02:00
|
|
|
}
|
2018-04-24 17:05:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the access specification for this name.
|
2018-04-25 19:58:42 -07:00
|
|
|
void ModuleVisitor::SetAccess(const parser::Name &name, Attr attr) {
|
2018-04-24 17:05:58 -07:00
|
|
|
Symbol &symbol{MakeSymbol(name)};
|
2018-04-25 10:46:39 -07:00
|
|
|
Attrs &attrs{symbol.attrs()};
|
|
|
|
if (attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
// PUBLIC/PRIVATE already set: make it a fatal error if it changed
|
|
|
|
Attr prev = attrs.test(Attr::PUBLIC) ? Attr::PUBLIC : Attr::PRIVATE;
|
|
|
|
const auto &msg = attr == prev
|
|
|
|
? "The accessibility of '%s' has already been specified as %s"_en_US
|
|
|
|
: "The accessibility of '%s' has already been specified as %s"_err_en_US;
|
|
|
|
Say(Message{name.source,
|
|
|
|
parser::MessageFormattedText{
|
|
|
|
msg, name.ToString().c_str(), EnumToString(prev).c_str()}});
|
2018-04-24 17:05:58 -07:00
|
|
|
} else {
|
2018-04-25 10:46:39 -07:00
|
|
|
attrs.set(attr);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::SpecificationPart &s) {
|
2018-04-18 15:06:35 -07:00
|
|
|
badStmtFuncFound_ = false;
|
2018-04-11 13:11:42 -07:00
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
// Check that every name referenced has an explicit type
|
|
|
|
for (const auto &pair : CurrScope()) {
|
|
|
|
const auto &name = pair.first;
|
|
|
|
const auto &symbol = pair.second;
|
|
|
|
if (symbol.has<UnknownDetails>()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
} else if (const auto *details = symbol.detailsIf<EntityDetails>()) {
|
|
|
|
if (!details->type()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::EndSubroutineStmt &subp) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::Post(const parser::EndFunctionStmt &subp) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::Suffix &suffix) {
|
|
|
|
funcResultName_ = &suffix.resultName.value();
|
2018-03-30 13:57:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-23 12:24:29 -07:00
|
|
|
|
2018-04-17 14:16:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
|
|
|
|
const auto &name = std::get<parser::Name>(x.t);
|
|
|
|
std::optional<SourceName> occurrence;
|
|
|
|
std::optional<DeclTypeSpec> resultType;
|
|
|
|
// Look up name: provides return type or tells us if it's an array
|
|
|
|
auto it = CurrScope().find(name.source);
|
|
|
|
if (it != CurrScope().end()) {
|
|
|
|
Symbol &symbol{it->second};
|
|
|
|
if (auto *details = symbol.detailsIf<EntityDetails>()) {
|
|
|
|
if (details->isArray()) {
|
|
|
|
// not a stmt-func at all but an array; do nothing
|
|
|
|
symbol.add_occurrence(name.source);
|
2018-04-18 15:06:35 -07:00
|
|
|
badStmtFuncFound_ = true;
|
2018-04-17 14:16:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// TODO: check that attrs are compatible with stmt func
|
|
|
|
resultType = details->type();
|
|
|
|
occurrence = symbol.name();
|
|
|
|
CurrScope().erase(symbol.name());
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 15:06:35 -07:00
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
Say(name, "'%s' has not been declared as an array"_err_en_US);
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-17 14:16:42 -07:00
|
|
|
BeginAttrs(); // no attrs to collect, but PushSubprogramScope expects this
|
2018-04-17 15:04:08 -07:00
|
|
|
auto &symbol = PushSubprogramScope(name);
|
|
|
|
CopyImplicitRules();
|
2018-04-17 14:16:42 -07:00
|
|
|
if (occurrence) {
|
|
|
|
symbol.add_occurrence(*occurrence);
|
|
|
|
}
|
|
|
|
auto &details = symbol.details<SubprogramDetails>();
|
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
EntityDetails dummyDetails{true};
|
|
|
|
auto it = CurrScope().parent().find(dummyName.source);
|
|
|
|
if (it != CurrScope().parent().end()) {
|
|
|
|
if (auto *d = it->second.detailsIf<EntityDetails>()) {
|
|
|
|
if (d->type()) {
|
|
|
|
dummyDetails.set_type(*d->type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
details.add_dummyArg(MakeSymbol(dummyName, std::move(dummyDetails)));
|
|
|
|
}
|
|
|
|
CurrScope().erase(name.source); // added by PushSubprogramScope
|
|
|
|
EntityDetails resultDetails;
|
|
|
|
if (resultType) {
|
|
|
|
resultDetails.set_type(*resultType);
|
|
|
|
}
|
|
|
|
details.set_result(MakeSymbol(name, resultDetails));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::Post(const parser::StmtFunctionStmt &x) {
|
2018-04-18 15:06:35 -07:00
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
return; // This wasn't really a stmt function so no scope was created
|
|
|
|
}
|
2018-04-17 14:16:42 -07:00
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::SubroutineStmt &stmt) {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginAttrs();
|
2018-03-30 13:57:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::FunctionStmt &stmt) {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginAttrs();
|
|
|
|
BeginDeclTypeSpec();
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(!funcResultName_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::SubroutineStmt &stmt) {
|
|
|
|
const auto &subrName = std::get<parser::Name>(stmt.t);
|
2018-04-17 14:16:42 -07:00
|
|
|
auto &symbol = PushSubprogramScope(subrName);
|
|
|
|
auto &details = symbol.details<SubprogramDetails>();
|
2018-04-11 13:11:42 -07:00
|
|
|
for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
|
|
|
|
const parser::Name *dummyName = std::get_if<parser::Name>(&dummyArg.u);
|
|
|
|
CHECK(dummyName != nullptr && "TODO: alternate return indicator");
|
2018-04-17 14:16:42 -07:00
|
|
|
Symbol &dummy{MakeSymbol(*dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::FunctionStmt &stmt) {
|
2018-04-11 13:11:42 -07:00
|
|
|
const auto &funcName = std::get<parser::Name>(stmt.t);
|
2018-04-17 14:16:42 -07:00
|
|
|
auto &symbol = PushSubprogramScope(funcName);
|
|
|
|
auto &details = symbol.details<SubprogramDetails>();
|
2018-04-11 13:11:42 -07:00
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(stmt.t)) {
|
2018-04-17 14:16:42 -07:00
|
|
|
Symbol &dummy{MakeSymbol(dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
// add function result to function scope
|
|
|
|
EntityDetails funcResultDetails;
|
|
|
|
if (declTypeSpec_) {
|
|
|
|
funcResultDetails.set_type(*declTypeSpec_);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
2018-04-17 14:16:42 -07:00
|
|
|
|
|
|
|
const parser::Name *funcResultName;
|
|
|
|
if (funcResultName_ && funcResultName_->source != funcName.source) {
|
|
|
|
funcResultName = funcResultName_;
|
|
|
|
funcResultName_ = nullptr;
|
2018-04-11 13:11:42 -07:00
|
|
|
} else {
|
|
|
|
CurrScope().erase(funcName.source); // was added by PushSubprogramScope
|
2018-04-17 14:16:42 -07:00
|
|
|
funcResultName = &funcName;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-17 14:16:42 -07:00
|
|
|
details.set_result(MakeSymbol(*funcResultName, funcResultDetails));
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-17 15:04:08 -07:00
|
|
|
Symbol &ResolveNamesVisitor::PushSubprogramScope(const parser::Name &name) {
|
2018-04-17 14:16:42 -07:00
|
|
|
auto &symbol = MakeSymbol(name, EndAttrs(), SubprogramDetails());
|
2018-04-18 15:06:35 -07:00
|
|
|
Scope &subpScope = CurrScope().MakeScope(Scope::Kind::Subprogram, &symbol);
|
2018-04-17 15:04:08 -07:00
|
|
|
PushScope(subpScope);
|
2018-04-17 14:16:42 -07:00
|
|
|
auto &details = symbol.details<SubprogramDetails>();
|
2018-04-17 15:04:08 -07:00
|
|
|
// can't reuse this name inside subprogram:
|
2018-04-17 14:16:42 -07:00
|
|
|
MakeSymbol(name, SubprogramDetails(details));
|
|
|
|
return symbol;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::MainProgram &x) {
|
|
|
|
Scope &scope = CurrScope().MakeScope(Scope::Kind::MainProgram);
|
|
|
|
PushScope(scope);
|
|
|
|
using stmtType = std::optional<parser::Statement<parser::ProgramStmt>>;
|
|
|
|
if (const stmtType &stmt = std::get<stmtType>(x.t)) {
|
|
|
|
const parser::Name &name{stmt->statement.v};
|
|
|
|
MakeSymbol(name, MainProgramDetails());
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-04-11 13:11:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::Post(const parser::EndProgramStmt &) {
|
|
|
|
PopScope();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-24 17:05:58 -07:00
|
|
|
const parser::Name *ResolveNamesVisitor::GetVariableName(
|
|
|
|
const parser::DataRef &x) {
|
|
|
|
return std::get_if<parser::Name>(&x.u);
|
|
|
|
}
|
|
|
|
const parser::Name *ResolveNamesVisitor::GetVariableName(
|
|
|
|
const parser::Designator &x) {
|
|
|
|
return std::visit(
|
|
|
|
parser::visitors{
|
|
|
|
[&](const parser::ObjectName &x) { return &x; },
|
|
|
|
[&](const parser::DataRef &x) { return GetVariableName(x); },
|
|
|
|
[&](const auto &) {
|
|
|
|
return static_cast<const parser::Name *>(nullptr);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
const parser::Name *ResolveNamesVisitor::GetVariableName(
|
|
|
|
const parser::Expr &x) {
|
|
|
|
if (const auto *designator =
|
|
|
|
std::get_if<parser::Indirection<parser::Designator>>(&x.u)) {
|
|
|
|
return GetVariableName(**designator);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const parser::Name *ResolveNamesVisitor::GetVariableName(
|
|
|
|
const parser::Variable &x) {
|
|
|
|
if (const auto *designator =
|
|
|
|
std::get_if<parser::Indirection<parser::Designator>>(&x.u)) {
|
|
|
|
return GetVariableName(**designator);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If implicit types are allowed, ensure name is in the symbol table
|
|
|
|
void ResolveNamesVisitor::CheckImplicitSymbol(const parser::Name *name) {
|
|
|
|
if (name) {
|
|
|
|
if (!isImplicitNoneType()) {
|
|
|
|
// ensure this name is in symbol table:
|
|
|
|
CurrScope().try_emplace(name->source);
|
|
|
|
} else {
|
|
|
|
const auto &it = CurrScope().find(name->source);
|
|
|
|
if (it == CurrScope().end() || it->second.has<UnknownDetails>()) {
|
|
|
|
Say(*name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::Program &) {
|
|
|
|
// ensure that all temps were deallocated
|
|
|
|
CHECK(!attrs_);
|
|
|
|
CHECK(!declTypeSpec_);
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
void ResolveNames(
|
2018-04-18 15:06:35 -07:00
|
|
|
parser::Program &program, const parser::CookedSource &cookedSource) {
|
2018-04-19 15:46:02 -07:00
|
|
|
parser::Messages messages;
|
2018-04-04 09:03:51 -07:00
|
|
|
ResolveNamesVisitor visitor{messages};
|
2018-04-18 16:49:42 -07:00
|
|
|
parser::Walk(static_cast<const parser::Program &>(program), visitor);
|
2018-04-18 15:06:35 -07:00
|
|
|
if (!messages.empty()) {
|
2018-04-19 15:46:02 -07:00
|
|
|
messages.Emit(std::cerr, cookedSource);
|
2018-04-18 15:06:35 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
RewriteParseTree(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PutIndent(std::ostream &os, int indent) {
|
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
os << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpSymbols(std::ostream &os, const Scope &scope, int indent = 0) {
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << Scope::EnumToString(scope.kind()) << " scope:";
|
|
|
|
if (const auto *symbol = scope.symbol()) {
|
|
|
|
os << ' ' << symbol->name().ToString();
|
|
|
|
}
|
|
|
|
os << '\n';
|
|
|
|
++indent;
|
|
|
|
for (const auto &symbol : scope) {
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << symbol.second << "\n";
|
|
|
|
}
|
|
|
|
for (const auto &child : scope.children()) {
|
|
|
|
DumpSymbols(os, child, indent);
|
|
|
|
}
|
|
|
|
--indent;
|
|
|
|
}
|
|
|
|
|
2018-05-02 14:06:02 -07:00
|
|
|
void DumpSymbols(std::ostream &os) { DumpSymbols(os, Scope::globalScope); }
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2018-03-23 14:02:11 -07:00
|
|
|
} // namespace Fortran::semantics
|