2019-01-07 15:42:36 -08:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-05-01 12:50:34 -07:00
|
|
|
//
|
|
|
|
// 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-12-06 06:59:37 -08:00
|
|
|
#include "expression.h"
|
2018-07-16 16:23:18 -07:00
|
|
|
#include "mod-file.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"
|
2018-10-22 07:37:38 -07:00
|
|
|
#include "semantics.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "symbol.h"
|
|
|
|
#include "type.h"
|
2019-01-18 12:40:47 -08:00
|
|
|
#include "../common/default-kinds.h"
|
2018-12-11 14:51:08 -08:00
|
|
|
#include "../common/fortran.h"
|
2018-06-18 11:03:43 -07:00
|
|
|
#include "../common/indirection.h"
|
2019-02-12 17:24:43 -08:00
|
|
|
#include "../common/restorer.h"
|
2018-12-06 06:59:37 -08:00
|
|
|
#include "../evaluate/common.h"
|
|
|
|
#include "../evaluate/fold.h"
|
2019-02-22 15:45:30 -08:00
|
|
|
#include "../evaluate/intrinsics.h"
|
2018-12-06 06:59:37 -08:00
|
|
|
#include "../evaluate/tools.h"
|
2018-12-04 10:55:32 -08:00
|
|
|
#include "../evaluate/type.h"
|
2018-04-12 12:23:20 -07:00
|
|
|
#include "../parser/parse-tree-visitor.h"
|
|
|
|
#include "../parser/parse-tree.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <list>
|
2018-12-18 07:59:40 -08:00
|
|
|
#include <map>
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <memory>
|
2018-05-02 14:06:02 -07:00
|
|
|
#include <ostream>
|
2018-05-03 15:57:56 -07:00
|
|
|
#include <set>
|
2018-03-22 17:08:20 -07:00
|
|
|
|
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;
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
template<typename T> using Indirection = common::Indirection<T>;
|
2018-11-16 16:40:00 -08:00
|
|
|
using Message = parser::Message;
|
|
|
|
using Messages = parser::Messages;
|
|
|
|
using MessageFixedText = parser::MessageFixedText;
|
|
|
|
using MessageFormattedText = parser::MessageFormattedText;
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
class ResolveNamesVisitor;
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
static const parser::Name *GetGenericSpecName(const parser::GenericSpec &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2018-08-27 13:32:10 -07:00
|
|
|
// ImplicitRules maps initial character of identifier to the DeclTypeSpec
|
|
|
|
// representing the implicit type; std::nullopt if none.
|
|
|
|
// It also records the presence of IMPLICIT NONE statements.
|
|
|
|
// When inheritFromParent is set, defaults come from the parent rules.
|
2018-04-04 09:03:51 -07:00
|
|
|
class ImplicitRules {
|
|
|
|
public:
|
2018-10-22 07:37:38 -07:00
|
|
|
ImplicitRules() : inheritFromParent_{false} {}
|
|
|
|
ImplicitRules(std::unique_ptr<ImplicitRules> &&parent)
|
|
|
|
: inheritFromParent_{parent.get() != nullptr} {
|
2018-08-27 11:48:49 -07:00
|
|
|
parent_.swap(parent);
|
|
|
|
}
|
2018-10-22 07:37:38 -07:00
|
|
|
void set_context(SemanticsContext &context) { context_ = &context; }
|
2018-08-27 11:48:49 -07:00
|
|
|
std::unique_ptr<ImplicitRules> &&parent() { return std::move(parent_); }
|
|
|
|
bool isImplicitNoneType() const;
|
|
|
|
bool isImplicitNoneExternal() const;
|
2018-04-11 13:11:42 -07:00
|
|
|
void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
|
|
|
|
void set_isImplicitNoneExternal(bool x) { isImplicitNoneExternal_ = x; }
|
2018-08-27 11:48:49 -07:00
|
|
|
void set_inheritFromParent(bool x) { inheritFromParent_ = x; }
|
2018-04-04 09:03:51 -07:00
|
|
|
// Get the implicit type for identifiers starting with ch. May be null.
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *GetType(char ch) const;
|
2018-04-04 09:03:51 -07:00
|
|
|
// Record the implicit type for this range of characters.
|
|
|
|
void SetType(const DeclTypeSpec &type, parser::Location lo, parser::Location,
|
|
|
|
bool isDefault = false);
|
|
|
|
|
|
|
|
private:
|
2018-04-05 17:02:31 -07:00
|
|
|
static char Incr(char ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
std::unique_ptr<ImplicitRules> parent_;
|
|
|
|
std::optional<bool> isImplicitNoneType_;
|
|
|
|
std::optional<bool> isImplicitNoneExternal_;
|
|
|
|
bool inheritFromParent_; // look in parent if not specified here
|
2018-10-22 07:37:38 -07:00
|
|
|
SemanticsContext *context_{nullptr};
|
2018-04-05 17:02:31 -07:00
|
|
|
// map initial character of identifier to nullptr or its default type
|
2018-12-11 14:51:08 -08:00
|
|
|
std::map<char, const DeclTypeSpec *> map_;
|
2018-08-27 11:48:49 -07:00
|
|
|
|
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-11-16 16:40:00 -08:00
|
|
|
// Track statement source locations and save messages.
|
|
|
|
class MessageHandler {
|
|
|
|
public:
|
2018-12-04 10:55:32 -08:00
|
|
|
Messages &messages() { return *messages_; };
|
2018-11-16 16:40:00 -08:00
|
|
|
void set_messages(Messages &messages) { messages_ = &messages; }
|
|
|
|
const SourceName *currStmtSource() { return currStmtSource_; }
|
|
|
|
void set_currStmtSource(const SourceName *);
|
|
|
|
|
|
|
|
// Emit a message associated with the current statement source.
|
|
|
|
Message &Say(MessageFixedText &&);
|
2019-02-18 11:39:46 -08:00
|
|
|
Message &Say(MessageFormattedText &&);
|
2018-11-16 16:40:00 -08:00
|
|
|
// Emit a message about a SourceName
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&);
|
|
|
|
// Emit a formatted message associated with a source location.
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const SourceName &);
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const SourceName &,
|
|
|
|
const SourceName &);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Where messages are emitted:
|
|
|
|
Messages *messages_{nullptr};
|
|
|
|
// Source location of current statement; null if not in a statement
|
|
|
|
const SourceName *currStmtSource_{nullptr};
|
|
|
|
};
|
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
// Inheritance graph for the parse tree visitation classes that follow:
|
|
|
|
// BaseVisitor
|
|
|
|
// + AttrsVisitor
|
|
|
|
// | + DeclTypeSpecVisitor
|
|
|
|
// | + ImplicitRulesVisitor
|
|
|
|
// | + ScopeHandler -----------+--+
|
|
|
|
// | + ModuleVisitor ========|==+
|
|
|
|
// | + InterfaceVisitor | |
|
|
|
|
// | +-+ SubprogramVisitor ==|==+
|
|
|
|
// + ArraySpecVisitor | |
|
|
|
|
// + DeclarationVisitor <--------+ |
|
|
|
|
// + ConstructVisitor |
|
|
|
|
// + ResolveNamesVisitor <------+
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
class BaseVisitor {
|
|
|
|
public:
|
|
|
|
template<typename T> void Walk(const T &);
|
|
|
|
void set_this(ResolveNamesVisitor *x) { this_ = x; }
|
|
|
|
|
|
|
|
MessageHandler &messageHandler() { return messageHandler_; }
|
|
|
|
const SourceName *currStmtSource();
|
|
|
|
SemanticsContext &context() const { return *context_; }
|
|
|
|
void set_context(SemanticsContext &);
|
2018-12-04 10:55:32 -08:00
|
|
|
evaluate::FoldingContext &GetFoldingContext() const {
|
|
|
|
return context_->foldingContext();
|
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
|
2019-01-04 17:10:31 -08:00
|
|
|
// Make a placeholder symbol for a Name that otherwise wouldn't have one.
|
|
|
|
// It is not in any scope and always has MiscDetails.
|
|
|
|
void MakePlaceholder(const parser::Name &, MiscDetails::Kind);
|
|
|
|
|
2019-01-17 16:14:36 -08:00
|
|
|
template<typename T> auto FoldExpr(T &&expr) -> T {
|
|
|
|
return evaluate::Fold(GetFoldingContext(), std::move(expr));
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
template<typename T> MaybeExpr EvaluateExpr(const T &expr) {
|
2019-01-17 16:14:36 -08:00
|
|
|
return FoldExpr(AnalyzeExpr(*context_, expr));
|
2018-12-06 06:59:37 -08:00
|
|
|
}
|
|
|
|
|
2019-01-07 15:05:53 -08:00
|
|
|
template<typename T> MaybeIntExpr EvaluateIntExpr(const T &expr) {
|
|
|
|
if (MaybeExpr maybeExpr{EvaluateExpr(expr)}) {
|
|
|
|
if (auto *intExpr{evaluate::UnwrapExpr<SomeIntExpr>(*maybeExpr)}) {
|
|
|
|
return {std::move(*intExpr)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
MaybeSubscriptIntExpr EvaluateSubscriptIntExpr(const T &expr) {
|
|
|
|
if (MaybeIntExpr maybeIntExpr{EvaluateIntExpr(expr)}) {
|
2019-01-17 16:14:36 -08:00
|
|
|
return FoldExpr(evaluate::ConvertToType<evaluate::SubscriptInteger>(
|
|
|
|
std::move(*maybeIntExpr)));
|
2019-01-07 15:05:53 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
template<typename... A> Message &Say(const parser::Name &name, A... args) {
|
|
|
|
return messageHandler_.Say(name.source, std::forward<A>(args)...);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
|
|
|
template<typename... A> Message &Say(A... args) {
|
|
|
|
return messageHandler_.Say(std::forward<A>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ResolveNamesVisitor *this_{nullptr};
|
|
|
|
SemanticsContext *context_{nullptr};
|
|
|
|
MessageHandler messageHandler_;
|
|
|
|
};
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
// Provide Post methods to collect attributes into a member variable.
|
2018-11-16 16:40:00 -08:00
|
|
|
class AttrsVisitor : public virtual BaseVisitor {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
2018-09-05 16:02:41 -07:00
|
|
|
bool BeginAttrs(); // always returns true
|
2018-05-14 13:51:13 -07:00
|
|
|
Attrs GetAttrs();
|
2018-04-12 12:59:42 -07:00
|
|
|
Attrs EndAttrs();
|
2019-01-04 17:10:31 -08:00
|
|
|
bool SetPassNameOn(Symbol &);
|
|
|
|
bool SetBindNameOn(Symbol &);
|
2018-03-30 19:49:00 -07:00
|
|
|
void Post(const parser::LanguageBindingSpec &);
|
|
|
|
bool Pre(const parser::AccessSpec &);
|
|
|
|
bool Pre(const parser::IntentSpec &);
|
2019-01-04 17:10:31 -08:00
|
|
|
bool Pre(const parser::Pass &);
|
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)
|
2018-08-31 16:20:00 -07:00
|
|
|
HANDLE_ATTR_CLASS(BindAttr::Deferred, DEFERRED)
|
|
|
|
HANDLE_ATTR_CLASS(BindAttr::Non_Overridable, NON_OVERRIDABLE)
|
2018-03-30 13:57:23 -07:00
|
|
|
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(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-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-07-11 17:45:13 -07:00
|
|
|
common::die("unreachable"); // suppress g++ warning
|
|
|
|
}
|
|
|
|
Attr IntentSpecToAttr(const parser::IntentSpec &x) {
|
|
|
|
switch (x.v) {
|
|
|
|
case parser::IntentSpec::Intent::In: return Attr::INTENT_IN;
|
|
|
|
case parser::IntentSpec::Intent::Out: return Attr::INTENT_OUT;
|
|
|
|
case parser::IntentSpec::Intent::InOut: return Attr::INTENT_INOUT;
|
|
|
|
}
|
|
|
|
common::die("unreachable"); // suppress g++ warning
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
MaybeExpr bindName_; // from BIND(C, NAME="...")
|
|
|
|
std::optional<SourceName> passName_; // from PASS(...)
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find and create types from declaration-type-spec nodes.
|
|
|
|
class DeclTypeSpecVisitor : public AttrsVisitor {
|
|
|
|
public:
|
2018-10-22 07:37:38 -07:00
|
|
|
explicit DeclTypeSpecVisitor() {}
|
2018-03-22 17:08:20 -07:00
|
|
|
using AttrsVisitor::Post;
|
2018-03-30 13:57:23 -07:00
|
|
|
using AttrsVisitor::Pre;
|
2018-12-06 06:59:37 -08:00
|
|
|
void Post(const parser::IntrinsicTypeSpec::DoublePrecision &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::DoubleComplex &);
|
2018-12-11 14:51:08 -08:00
|
|
|
void Post(const parser::DeclarationTypeSpec::ClassStar &);
|
|
|
|
void Post(const parser::DeclarationTypeSpec::TypeStar &);
|
2018-05-17 13:06:38 -07:00
|
|
|
bool Pre(const parser::TypeGuardStmt &);
|
|
|
|
void Post(const parser::TypeGuardStmt &);
|
2019-01-22 16:30:32 -08:00
|
|
|
void Post(const parser::TypeSpec &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-12-18 17:19:41 -08:00
|
|
|
struct State {
|
2018-12-04 10:55:32 -08:00
|
|
|
bool expectDeclTypeSpec{false}; // should see decl-type-spec only when true
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec *declTypeSpec{nullptr};
|
|
|
|
struct {
|
|
|
|
DerivedTypeSpec *type{nullptr};
|
|
|
|
DeclTypeSpec::Category category{DeclTypeSpec::TypeDerived};
|
|
|
|
} derived;
|
2018-12-18 17:19:41 -08:00
|
|
|
};
|
|
|
|
|
2019-02-24 10:05:43 -08:00
|
|
|
// Walk the parse tree of a type spec and return the DeclTypeSpec for it.
|
|
|
|
template<typename T> const DeclTypeSpec *ProcessTypeSpec(const T &x) {
|
|
|
|
auto save{common::ScopedSet(state_, State{})};
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
Walk(x);
|
|
|
|
const auto *type{GetDeclTypeSpec()};
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec *GetDeclTypeSpec();
|
2018-06-05 12:18:35 -07:00
|
|
|
void BeginDeclTypeSpec();
|
|
|
|
void EndDeclTypeSpec();
|
2019-01-15 16:59:20 -08:00
|
|
|
void SetDeclTypeSpec(const DeclTypeSpec &);
|
2018-12-04 10:55:32 -08:00
|
|
|
void SetDeclTypeSpecCategory(DeclTypeSpec::Category);
|
|
|
|
DeclTypeSpec::Category GetDeclTypeSpecCategory() const {
|
|
|
|
return state_.derived.category;
|
|
|
|
}
|
|
|
|
KindExpr GetKindParamExpr(
|
|
|
|
TypeCategory, const std::optional<parser::KindSelector> &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
private:
|
2018-12-18 17:19:41 -08:00
|
|
|
State state_;
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-12-17 12:41:43 -08:00
|
|
|
void MakeNumericType(TypeCategory, int kind);
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
|
2018-11-16 16:40:00 -08:00
|
|
|
class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
|
|
|
using DeclTypeSpecVisitor::Post;
|
|
|
|
using DeclTypeSpecVisitor::Pre;
|
2018-04-04 09:03:51 -07:00
|
|
|
using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
ImplicitRules &implicitRules() { return *implicitRules_; }
|
|
|
|
const ImplicitRules &implicitRules() const { return *implicitRules_; }
|
2018-04-11 13:11:42 -07:00
|
|
|
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-10-26 11:57:08 -07:00
|
|
|
void ClearScopes() { implicitRules_.reset(); }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// implicit rules in effect for current scope
|
2018-10-22 07:37:38 -07:00
|
|
|
std::unique_ptr<ImplicitRules> implicitRules_;
|
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
|
2018-11-16 16:40:00 -08:00
|
|
|
class ArraySpecVisitor : public virtual BaseVisitor {
|
2018-04-12 12:59:42 -07:00
|
|
|
public:
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::ArraySpec &);
|
2018-05-17 13:06:38 -07:00
|
|
|
void Post(const parser::AttrSpec &) { PostAttrSpec(); }
|
|
|
|
void Post(const parser::ComponentAttrSpec &) { PostAttrSpec(); }
|
2018-12-06 06:59:37 -08:00
|
|
|
void Post(const parser::DeferredShapeSpecList &);
|
|
|
|
void Post(const parser::AssumedShapeSpec &);
|
|
|
|
void Post(const parser::ExplicitShapeSpec &);
|
|
|
|
void Post(const parser::AssumedImpliedSpec &);
|
|
|
|
void Post(const parser::AssumedRankSpec &);
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
protected:
|
|
|
|
const ArraySpec &arraySpec();
|
|
|
|
void BeginArraySpec();
|
|
|
|
void EndArraySpec();
|
|
|
|
void ClearArraySpec() { arraySpec_.clear(); }
|
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
private:
|
|
|
|
// arraySpec_ is populated by any ArraySpec
|
|
|
|
ArraySpec arraySpec_;
|
2018-05-17 13:06:38 -07:00
|
|
|
// When an ArraySpec is under an AttrSpec or ComponentAttrSpec, it is moved
|
|
|
|
// into attrArraySpec_
|
2018-04-12 12:59:42 -07:00
|
|
|
ArraySpec attrArraySpec_;
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
void PostAttrSpec();
|
2018-05-14 14:26:39 -07:00
|
|
|
Bound GetBound(const parser::SpecificationExpr &);
|
2018-04-12 12:59:42 -07:00
|
|
|
};
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Manage a stack of Scopes
|
2018-09-14 15:04:50 -07:00
|
|
|
class ScopeHandler : public ImplicitRulesVisitor {
|
2018-04-04 09:03:51 -07:00
|
|
|
public:
|
2019-02-21 17:05:46 -08:00
|
|
|
using ImplicitRulesVisitor::Post;
|
|
|
|
using ImplicitRulesVisitor::Pre;
|
|
|
|
|
2018-08-22 16:56:57 -07:00
|
|
|
Scope &currScope() { return *currScope_; }
|
2018-08-27 11:48:49 -07:00
|
|
|
// The enclosing scope, skipping blocks and derived types.
|
|
|
|
Scope &InclusiveScope();
|
2018-09-14 15:04:50 -07:00
|
|
|
// The global scope, containing program units.
|
|
|
|
Scope &GlobalScope();
|
2018-06-22 08:21:19 -07:00
|
|
|
|
|
|
|
// Create a new scope and push it on the scope stack.
|
2018-08-22 16:56:57 -07:00
|
|
|
void PushScope(Scope::Kind kind, Symbol *symbol);
|
2018-08-02 16:21:27 -07:00
|
|
|
void PushScope(Scope &scope);
|
2018-05-14 14:26:39 -07:00
|
|
|
void PopScope();
|
2018-10-23 17:05:12 -07:00
|
|
|
void ClearScopes() {
|
2018-10-23 17:13:59 -07:00
|
|
|
PopScope(); // trigger ConvertToObjectEntity calls
|
2018-10-23 17:05:12 -07:00
|
|
|
currScope_ = &context().globalScope();
|
|
|
|
ImplicitRulesVisitor::ClearScopes();
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-11-28 15:55:55 -08:00
|
|
|
template<typename T> bool Pre(const parser::Statement<T> &x) {
|
2018-11-16 16:40:00 -08:00
|
|
|
messageHandler().set_currStmtSource(&x.source);
|
2018-11-28 15:55:55 -08:00
|
|
|
currScope_->AddSourceRange(x.source);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T> void Post(const parser::Statement<T> &) {
|
2018-11-16 16:40:00 -08:00
|
|
|
messageHandler().set_currStmtSource(nullptr);
|
2018-11-28 15:55:55 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 13:51:36 -08:00
|
|
|
// Special messages: already declared; referencing symbol's declaration;
|
|
|
|
// about a type; two names & locations
|
2019-02-26 13:28:53 -08:00
|
|
|
void SayAlreadyDeclared(const SourceName &, const Symbol &);
|
2018-11-16 16:40:00 -08:00
|
|
|
void SayAlreadyDeclared(const parser::Name &, const Symbol &);
|
2019-02-05 13:51:36 -08:00
|
|
|
void SayWithDecl(const parser::Name &, const Symbol &, MessageFixedText &&);
|
2018-11-16 16:40:00 -08:00
|
|
|
void SayDerivedType(const SourceName &, MessageFixedText &&, const Scope &);
|
2019-02-14 07:59:20 -08:00
|
|
|
void Say2(const SourceName &, MessageFixedText &&, const SourceName &,
|
|
|
|
MessageFixedText &&);
|
2018-11-16 16:40:00 -08:00
|
|
|
void Say2(const parser::Name &, MessageFixedText &&, const Symbol &,
|
|
|
|
MessageFixedText &&);
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Search for symbol by name in current and containing scopes
|
|
|
|
Symbol *FindSymbol(const parser::Name &);
|
|
|
|
Symbol *FindSymbol(const Scope &, const parser::Name &);
|
|
|
|
// Search for name only in scope, not in enclosing scopes.
|
|
|
|
Symbol *FindInScope(const Scope &, const parser::Name &);
|
|
|
|
Symbol *FindInScope(const Scope &, const SourceName &);
|
2018-12-04 10:55:32 -08:00
|
|
|
// Search for name in a derived type scope and its parents.
|
|
|
|
Symbol *FindInTypeOrParents(const Scope &, SourceName);
|
2019-02-08 08:57:28 -08:00
|
|
|
Symbol *FindInTypeOrParents(const Scope &, const parser::Name &);
|
|
|
|
Symbol *FindInTypeOrParents(const parser::Name &);
|
2018-11-16 12:43:08 -08:00
|
|
|
void EraseSymbol(const parser::Name &);
|
|
|
|
// Record that name resolved to symbol
|
|
|
|
Symbol *Resolve(const parser::Name &, Symbol *);
|
|
|
|
Symbol &Resolve(const parser::Name &, Symbol &);
|
|
|
|
// Make a new symbol with the name and attrs of an existing one
|
|
|
|
Symbol &CopySymbol(const Symbol &);
|
|
|
|
|
|
|
|
// Make symbols in the current or named scope
|
|
|
|
Symbol &MakeSymbol(Scope &, const SourceName &, Attrs);
|
|
|
|
Symbol &MakeSymbol(const parser::Name &, Attrs = Attrs{});
|
|
|
|
|
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(const parser::Name &name, D &&details) {
|
|
|
|
return MakeSymbol(name, Attrs{}, std::move(details));
|
|
|
|
}
|
2018-05-22 16:12:56 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
template<typename D>
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &MakeSymbol(
|
|
|
|
const parser::Name &name, const Attrs &attrs, D &&details) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// Note: don't use FindSymbol here. If this is a derived type scope,
|
2018-12-04 10:55:32 -08:00
|
|
|
// we want to detect whether the name is already declared as a component.
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindInScope(currScope(), name)};
|
|
|
|
if (!symbol) {
|
|
|
|
symbol = &MakeSymbol(name, attrs);
|
|
|
|
symbol->set_details(std::move(details));
|
|
|
|
return *symbol;
|
|
|
|
}
|
2018-08-28 14:02:53 -07:00
|
|
|
if constexpr (std::is_same_v<DerivedTypeDetails, D>) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *d{symbol->detailsIf<GenericDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// derived type with same name as a generic
|
2018-07-17 07:02:30 -07:00
|
|
|
auto *derivedType{d->derivedType()};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (!derivedType) {
|
2018-11-05 14:36:11 -08:00
|
|
|
derivedType =
|
2018-11-16 12:43:08 -08:00
|
|
|
&currScope().MakeSymbol(name.source, attrs, std::move(details));
|
2018-06-22 08:21:19 -07:00
|
|
|
d->set_derivedType(*derivedType);
|
|
|
|
} else {
|
2018-10-10 16:20:46 -07:00
|
|
|
SayAlreadyDeclared(name, *derivedType);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
|
|
|
return *derivedType;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (symbol->CanReplaceDetails(details)) {
|
2018-04-25 19:58:42 -07:00
|
|
|
// update the existing symbol
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
symbol->set_details(std::move(details));
|
|
|
|
return *symbol;
|
2018-08-28 14:02:53 -07:00
|
|
|
} else if constexpr (std::is_same_v<UnknownDetails, D>) {
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
return *symbol;
|
2018-04-25 19:58:42 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayAlreadyDeclared(name, *symbol);
|
2019-02-22 15:45:30 -08:00
|
|
|
// replace the old symbol with a new one with correct details
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
2018-04-25 19:58:42 -07:00
|
|
|
return MakeSymbol(name, attrs, details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
protected:
|
|
|
|
// When subpNamesOnly_ is set we are only collecting procedure names.
|
|
|
|
// Create symbols with SubprogramNameDetails of the given kind.
|
|
|
|
std::optional<SubprogramKind> subpNamesOnly_;
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// Apply the implicit type rules to this symbol.
|
2018-09-22 08:05:46 -07:00
|
|
|
void ApplyImplicitRules(Symbol &);
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *GetImplicitType(Symbol &);
|
2018-09-22 08:05:46 -07:00
|
|
|
bool ConvertToObjectEntity(Symbol &);
|
|
|
|
bool ConvertToProcEntity(Symbol &);
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &MakeNumericType(
|
2018-12-04 10:55:32 -08:00
|
|
|
TypeCategory, const std::optional<parser::KindSelector> &);
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &MakeLogicalType(
|
|
|
|
const std::optional<parser::KindSelector> &);
|
2018-12-04 10:55:32 -08:00
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
// Walk the ModuleSubprogramPart or InternalSubprogramPart collecting names.
|
|
|
|
template<typename T>
|
|
|
|
void WalkSubprogramPart(const std::optional<T> &subpPart) {
|
|
|
|
if (subpPart) {
|
|
|
|
if (std::is_same_v<T, parser::ModuleSubprogramPart>) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Module;
|
|
|
|
} else if (std::is_same_v<T, parser::InternalSubprogramPart>) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Internal;
|
|
|
|
} else {
|
|
|
|
static_assert("unexpected type");
|
|
|
|
}
|
|
|
|
Walk(*subpPart);
|
|
|
|
subpNamesOnly_ = std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
private:
|
2018-08-09 17:12:31 -07:00
|
|
|
Scope *currScope_{nullptr};
|
2018-04-25 19:58:42 -07:00
|
|
|
};
|
|
|
|
|
2018-05-04 15:37:56 -07:00
|
|
|
class ModuleVisitor : public virtual ScopeHandler {
|
2018-04-25 19:58:42 -07:00
|
|
|
public:
|
2018-05-14 13:51:13 -07:00
|
|
|
bool Pre(const parser::Module &);
|
|
|
|
void Post(const parser::Module &);
|
2018-08-02 16:21:27 -07:00
|
|
|
bool Pre(const parser::Submodule &);
|
|
|
|
void Post(const parser::Submodule &);
|
2018-04-25 19:58:42 -07:00
|
|
|
bool Pre(const parser::AccessStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::Only &);
|
|
|
|
bool Pre(const parser::Rename::Names &);
|
|
|
|
bool Pre(const parser::UseStmt &);
|
|
|
|
void Post(const parser::UseStmt &);
|
2018-05-03 15:57:56 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
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};
|
2018-05-03 15:57:56 -07:00
|
|
|
// The scope of the module during a UseStmt
|
|
|
|
const Scope *useModuleScope_{nullptr};
|
2018-07-25 06:55:11 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
void SetAccess(const parser::Name &, Attr);
|
|
|
|
void ApplyDefaultAccess();
|
2018-05-14 14:26:39 -07:00
|
|
|
void AddUse(const parser::Rename::Names &);
|
|
|
|
void AddUse(const parser::Name &);
|
2018-05-03 15:57:56 -07:00
|
|
|
// Record a use from useModuleScope_ of useName as localName. location is
|
|
|
|
// where it occurred (either the module or the rename) for error reporting.
|
2018-11-16 12:43:08 -08:00
|
|
|
void AddUse(const SourceName &, const parser::Name &, const parser::Name &);
|
|
|
|
void AddUse(const SourceName &, const Symbol &, Symbol &);
|
|
|
|
Symbol &BeginModule(const parser::Name &, bool isSubmodule,
|
2018-08-02 16:21:27 -07:00
|
|
|
const std::optional<parser::ModuleSubprogramPart> &);
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *FindModule(const parser::Name &, Scope *ancestor = nullptr);
|
2018-04-25 19:58:42 -07:00
|
|
|
};
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
class InterfaceVisitor : public virtual ScopeHandler {
|
|
|
|
public:
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::InterfaceStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
void Post(const parser::EndInterfaceStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::GenericSpec &);
|
|
|
|
bool Pre(const parser::ProcedureStmt &);
|
|
|
|
void Post(const parser::GenericStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
bool inInterfaceBlock() const { return inInterfaceBlock_; }
|
2018-11-16 12:43:08 -08:00
|
|
|
bool isGeneric() const { return genericName_ != nullptr; }
|
2018-05-14 13:51:13 -07:00
|
|
|
bool isAbstract() const { return isAbstract_; }
|
|
|
|
|
|
|
|
protected:
|
2018-11-16 12:43:08 -08:00
|
|
|
GenericDetails &GetGenericDetails();
|
2018-05-22 16:12:56 -07:00
|
|
|
// Add to generic the symbol for the subprogram with the same name
|
2018-07-05 10:28:34 -07:00
|
|
|
void CheckGenericProcedures(Symbol &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool inInterfaceBlock_{false}; // set when in interface block
|
|
|
|
bool isAbstract_{false}; // set when in abstract interface block
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *genericName_{nullptr}; // set in generic interface block
|
2018-12-18 07:59:40 -08:00
|
|
|
using ProcedureKind = parser::ProcedureStmt::Kind;
|
|
|
|
// mapping of generic to its specific proc names and kinds
|
|
|
|
std::multimap<Symbol *, std::pair<const parser::Name *, ProcedureKind>>
|
|
|
|
specificProcs_;
|
2018-07-20 10:46:11 -07:00
|
|
|
|
2018-12-18 07:59:40 -08:00
|
|
|
void AddSpecificProcs(const std::list<parser::Name> &, ProcedureKind);
|
2018-07-20 10:46:11 -07:00
|
|
|
void ResolveSpecificsInGeneric(Symbol &generic);
|
2018-05-14 13:51:13 -07:00
|
|
|
};
|
|
|
|
|
2018-10-15 17:11:24 -07:00
|
|
|
class SubprogramVisitor : public virtual ScopeHandler, public InterfaceVisitor {
|
2018-05-04 15:37:56 -07:00
|
|
|
public:
|
2018-10-10 16:20:46 -07:00
|
|
|
bool HandleStmtFunction(const parser::StmtFunctionStmt &);
|
2018-05-04 15:37:56 -07:00
|
|
|
void Post(const parser::StmtFunctionStmt &);
|
2018-07-11 17:45:13 -07:00
|
|
|
bool Pre(const parser::SubroutineStmt &);
|
2018-05-04 15:37:56 -07:00
|
|
|
void Post(const parser::SubroutineStmt &);
|
|
|
|
bool Pre(const parser::FunctionStmt &);
|
|
|
|
void Post(const parser::FunctionStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
bool Pre(const parser::SubroutineSubprogram &);
|
|
|
|
void Post(const parser::SubroutineSubprogram &);
|
|
|
|
bool Pre(const parser::FunctionSubprogram &);
|
|
|
|
void Post(const parser::FunctionSubprogram &);
|
|
|
|
bool Pre(const parser::InterfaceBody::Subroutine &);
|
|
|
|
void Post(const parser::InterfaceBody::Subroutine &);
|
|
|
|
bool Pre(const parser::InterfaceBody::Function &);
|
|
|
|
void Post(const parser::InterfaceBody::Function &);
|
2018-10-26 07:34:50 -07:00
|
|
|
bool Pre(const parser::SeparateModuleSubprogram &);
|
|
|
|
void Post(const parser::SeparateModuleSubprogram &);
|
2018-05-04 15:37:56 -07:00
|
|
|
bool Pre(const parser::Suffix &);
|
2019-02-24 10:05:43 -08:00
|
|
|
bool Pre(const parser::PrefixSpec &);
|
2018-05-04 15:37:56 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Set when we see a stmt function that is really an array element assignment
|
|
|
|
bool badStmtFuncFound_{false};
|
2019-02-24 10:05:43 -08:00
|
|
|
void HandleFunctionPrefixType();
|
2018-05-04 15:37:56 -07:00
|
|
|
|
|
|
|
private:
|
2019-02-24 10:05:43 -08:00
|
|
|
// Info about the current function: parse tree of the type in the PrefixSpec;
|
|
|
|
// name and symbol of the function result from the Suffix; source location.
|
|
|
|
struct {
|
|
|
|
const parser::DeclarationTypeSpec *parsedType{nullptr};
|
|
|
|
const parser::Name *resultName{nullptr};
|
|
|
|
Symbol *resultSymbol{nullptr};
|
|
|
|
const SourceName *source{nullptr};
|
|
|
|
} funcInfo_;
|
2018-05-04 15:37:56 -07:00
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool BeginSubprogram(const parser::Name &, Symbol::Flag, bool hasModulePrefix,
|
2018-05-14 13:51:13 -07:00
|
|
|
const std::optional<parser::InternalSubprogramPart> &);
|
|
|
|
void EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
// Create a subprogram symbol in the current scope and push a new scope.
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &PushSubprogramScope(const parser::Name &, Symbol::Flag);
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *GetSpecificFromGeneric(const parser::Name &);
|
2018-10-26 07:34:50 -07:00
|
|
|
SubprogramDetails &PostSubprogramStmt(const parser::Name &);
|
2018-05-04 15:37:56 -07:00
|
|
|
};
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
class DeclarationVisitor : public ArraySpecVisitor,
|
|
|
|
public virtual ScopeHandler {
|
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
2019-02-21 17:05:46 -08:00
|
|
|
using ScopeHandler::Post;
|
|
|
|
using ScopeHandler::Pre;
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
void Post(const parser::EntityDecl &);
|
|
|
|
void Post(const parser::ObjectDecl &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::PointerDecl &);
|
2018-09-10 12:20:42 -07:00
|
|
|
bool Pre(const parser::BindStmt &) { return BeginAttrs(); }
|
|
|
|
void Post(const parser::BindStmt &) { EndAttrs(); }
|
|
|
|
bool Pre(const parser::BindEntity &);
|
2019-02-06 17:18:02 -08:00
|
|
|
bool Pre(const parser::NamedConstantDef &);
|
|
|
|
bool Pre(const parser::NamedConstant &);
|
2018-05-30 14:11:45 -07:00
|
|
|
bool Pre(const parser::AsynchronousStmt &);
|
|
|
|
bool Pre(const parser::ContiguousStmt &);
|
|
|
|
bool Pre(const parser::ExternalStmt &);
|
2018-07-11 17:45:13 -07:00
|
|
|
bool Pre(const parser::IntentStmt &);
|
2018-05-30 14:11:45 -07:00
|
|
|
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 &);
|
|
|
|
bool Pre(const parser::AllocatableStmt &) {
|
|
|
|
objectDeclAttr_ = Attr::ALLOCATABLE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Post(const parser::AllocatableStmt &) { objectDeclAttr_ = std::nullopt; }
|
|
|
|
bool Pre(const parser::TargetStmt &x) {
|
|
|
|
objectDeclAttr_ = Attr::TARGET;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Post(const parser::TargetStmt &) { objectDeclAttr_ = std::nullopt; }
|
|
|
|
void Post(const parser::DimensionStmt::Declaration &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::TypeDeclarationStmt &) { return BeginDecl(); }
|
|
|
|
void Post(const parser::TypeDeclarationStmt &) { EndDecl(); }
|
2018-12-04 10:55:32 -08:00
|
|
|
void Post(const parser::IntegerTypeSpec &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Real &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Complex &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Logical &);
|
2018-12-14 14:04:15 -08:00
|
|
|
void Post(const parser::IntrinsicTypeSpec::Character &);
|
2019-02-21 17:05:46 -08:00
|
|
|
void Post(const parser::IntrinsicTypeSpec::NCharacter &);
|
2018-12-14 14:04:15 -08:00
|
|
|
void Post(const parser::CharSelector::LengthAndKind &);
|
|
|
|
void Post(const parser::CharLength &);
|
2019-01-07 15:05:53 -08:00
|
|
|
void Post(const parser::LengthSelector &);
|
2019-02-08 16:03:23 -08:00
|
|
|
bool Pre(const parser::KindParam &);
|
2018-12-04 10:55:32 -08:00
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Type &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Class &);
|
2018-12-11 14:51:08 -08:00
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Record &);
|
2018-12-04 10:55:32 -08:00
|
|
|
void Post(const parser::DerivedTypeSpec &);
|
2019-02-15 16:08:32 -08:00
|
|
|
bool Pre(const parser::DerivedTypeDef &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::DerivedTypeStmt &x);
|
|
|
|
void Post(const parser::DerivedTypeStmt &x);
|
2018-09-04 10:28:27 -07:00
|
|
|
bool Pre(const parser::TypeParamDefStmt &x) { return BeginDecl(); }
|
|
|
|
void Post(const parser::TypeParamDefStmt &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::TypeAttrSpec::Extends &x);
|
|
|
|
bool Pre(const parser::PrivateStmt &x);
|
|
|
|
bool Pre(const parser::SequenceStmt &x);
|
|
|
|
bool Pre(const parser::ComponentDefStmt &) { return BeginDecl(); }
|
|
|
|
void Post(const parser::ComponentDefStmt &) { EndDecl(); }
|
2019-02-15 16:08:32 -08:00
|
|
|
void Post(const parser::ComponentDecl &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::ProcedureDeclarationStmt &);
|
|
|
|
void Post(const parser::ProcedureDeclarationStmt &);
|
|
|
|
bool Pre(const parser::ProcComponentDefStmt &);
|
|
|
|
void Post(const parser::ProcComponentDefStmt &);
|
2019-02-22 15:45:30 -08:00
|
|
|
bool Pre(const parser::ProcPointerInit &);
|
|
|
|
bool Pre(const parser::ProcInterface &);
|
|
|
|
void Post(const parser::ProcInterface &);
|
|
|
|
void Post(const parser::ProcDecl &);
|
2018-09-06 08:01:49 -07:00
|
|
|
bool Pre(const parser::TypeBoundProcedurePart &);
|
2019-02-15 16:08:32 -08:00
|
|
|
void Post(const parser::ContainsStmt &);
|
2018-08-31 16:20:00 -07:00
|
|
|
bool Pre(const parser::TypeBoundProcBinding &) { return BeginAttrs(); }
|
|
|
|
void Post(const parser::TypeBoundProcBinding &) { EndAttrs(); }
|
|
|
|
void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &);
|
|
|
|
void Post(const parser::TypeBoundProcedureStmt::WithInterface &);
|
|
|
|
void Post(const parser::FinalProcedureStmt &);
|
2018-12-26 13:31:13 -08:00
|
|
|
bool Pre(const parser::TypeBoundGenericStmt &);
|
2018-10-18 07:55:48 -07:00
|
|
|
bool Pre(const parser::AllocateStmt &);
|
|
|
|
void Post(const parser::AllocateStmt &);
|
|
|
|
bool Pre(const parser::StructureConstructor &);
|
2019-02-05 14:43:00 -08:00
|
|
|
bool Pre(const parser::NamelistStmt::Group &);
|
|
|
|
bool Pre(const parser::IoControlSpec &);
|
2019-02-14 07:59:20 -08:00
|
|
|
bool Pre(const parser::CommonStmt::Block &);
|
|
|
|
void Post(const parser::CommonStmt::Block &);
|
|
|
|
bool Pre(const parser::CommonBlockObject &);
|
|
|
|
void Post(const parser::CommonBlockObject &);
|
2019-02-20 17:45:39 -08:00
|
|
|
bool Pre(const parser::SaveStmt &);
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool BeginDecl();
|
|
|
|
void EndDecl();
|
2019-01-29 14:31:47 -08:00
|
|
|
Symbol &DeclareObjectEntity(const parser::Name &, Attrs);
|
|
|
|
// Declare a LOCAL/LOCAL_INIT entity. If there isn't a type specified
|
2018-10-18 07:55:48 -07:00
|
|
|
// it comes from the entity in the containing scope, or implicit rules.
|
|
|
|
// Return pointer to the new symbol, or nullptr on error.
|
2019-01-29 14:31:47 -08:00
|
|
|
Symbol *DeclareLocalEntity(const parser::Name &);
|
2019-01-22 16:30:32 -08:00
|
|
|
// Declare a statement entity (e.g., an implied DO loop index).
|
|
|
|
// If there isn't a type specified, implicit rules apply.
|
|
|
|
// Return pointer to the new symbol, or nullptr on error.
|
2019-01-28 16:56:50 -08:00
|
|
|
Symbol *DeclareStatementEntity(
|
|
|
|
const parser::Name &, const std::optional<parser::IntegerTypeSpec> &);
|
2018-11-16 12:43:08 -08:00
|
|
|
bool CheckUseError(const parser::Name &);
|
2018-12-26 13:31:13 -08:00
|
|
|
void CheckAccessibility(const parser::Name &, bool, const Symbol &);
|
2019-02-12 17:24:43 -08:00
|
|
|
bool CheckAccessibleComponent(const SourceName &, const Symbol &);
|
2019-01-29 14:31:47 -08:00
|
|
|
void CheckScalarIntegerType(const parser::Name &);
|
2019-02-14 07:59:20 -08:00
|
|
|
void CheckCommonBlocks();
|
2019-02-20 17:45:39 -08:00
|
|
|
void CheckSaveStmts();
|
2019-02-21 19:14:28 -08:00
|
|
|
bool CheckNotInBlock(const char *);
|
2019-02-26 14:26:28 -08:00
|
|
|
bool NameIsKnownOrIntrinsic(const parser::Name &);
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// The attribute corresponding to the statement containing an ObjectDecl
|
|
|
|
std::optional<Attr> objectDeclAttr_;
|
2018-12-14 14:04:15 -08:00
|
|
|
// Info about current character type while walking DeclTypeSpec
|
|
|
|
struct {
|
|
|
|
std::optional<ParamValue> length;
|
2018-12-04 10:55:32 -08:00
|
|
|
std::optional<KindExpr> kind;
|
2018-12-14 14:04:15 -08:00
|
|
|
} charInfo_;
|
2019-02-15 16:08:32 -08:00
|
|
|
// Info about current derived type while walking DerivedTypeDef
|
2018-09-06 08:01:49 -07:00
|
|
|
struct {
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *extends{nullptr}; // EXTENDS(name)
|
2018-09-06 08:01:49 -07:00
|
|
|
bool privateComps{false}; // components are private by default
|
|
|
|
bool privateBindings{false}; // bindings are private by default
|
|
|
|
bool sawContains{false}; // currently processing bindings
|
|
|
|
bool sequence{false}; // is a sequence type
|
2019-02-15 16:08:32 -08:00
|
|
|
const Symbol *type{nullptr}; // derived type being defined
|
2018-09-06 08:01:49 -07:00
|
|
|
} derivedTypeInfo_;
|
2019-02-14 07:59:20 -08:00
|
|
|
// Info about common blocks in the current scope
|
|
|
|
struct {
|
|
|
|
Symbol *curr{nullptr}; // common block currently being processed
|
|
|
|
std::set<SourceName> names; // names in any common block of scope
|
|
|
|
} commonBlockInfo_;
|
2019-02-20 17:45:39 -08:00
|
|
|
// Info about about SAVE statements and attributes in current scope
|
|
|
|
struct {
|
|
|
|
const SourceName *saveAll{nullptr}; // "SAVE" without entity list
|
|
|
|
std::set<SourceName> entities; // names of entities with save attr
|
|
|
|
std::set<SourceName> commons; // names of common blocks with save attr
|
|
|
|
} saveInfo_;
|
2018-06-05 12:18:35 -07:00
|
|
|
// In a ProcedureDeclarationStmt or ProcComponentDefStmt, this is
|
|
|
|
// the interface name, if any.
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *interfaceName_{nullptr};
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
bool HandleAttributeStmt(Attr, const std::list<parser::Name> &);
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &HandleAttributeStmt(Attr, const parser::Name &);
|
|
|
|
Symbol &DeclareUnknownEntity(const parser::Name &, Attrs);
|
|
|
|
Symbol &DeclareProcEntity(const parser::Name &, Attrs, const ProcInterface &);
|
|
|
|
void SetType(const parser::Name &, const DeclTypeSpec &);
|
2019-01-15 16:59:20 -08:00
|
|
|
const Symbol *ResolveDerivedType(const parser::Name &);
|
2018-08-31 16:20:00 -07:00
|
|
|
bool CanBeTypeBoundProc(const Symbol &);
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *FindExplicitInterface(const parser::Name &);
|
2018-12-26 13:31:13 -08:00
|
|
|
Symbol *MakeTypeSymbol(const parser::Name &, Details &&);
|
2019-01-07 15:05:53 -08:00
|
|
|
bool OkToAddComponent(const parser::Name &, const Symbol * = nullptr);
|
2018-12-04 10:55:32 -08:00
|
|
|
ParamValue GetParamValue(const parser::TypeParamValue &);
|
2019-02-18 11:39:46 -08:00
|
|
|
Symbol &MakeCommonBlockSymbol(const parser::Name &);
|
2019-02-14 07:59:20 -08:00
|
|
|
void CheckCommonBlockDerivedType(const SourceName &, const Symbol &);
|
2019-02-20 17:45:39 -08:00
|
|
|
std::optional<MessageFixedText> CheckSaveAttr(const Symbol &);
|
|
|
|
Attrs HandleSaveName(const SourceName &, Attrs);
|
|
|
|
void AddSaveName(std::set<SourceName> &, const SourceName &);
|
|
|
|
void SetSaveAttr(Symbol &);
|
2019-02-22 15:45:30 -08:00
|
|
|
bool HandleUnrestrictedSpecificIntrinsicFunction(const parser::Name &);
|
2018-06-05 12:18:35 -07:00
|
|
|
|
|
|
|
// Declare an object or procedure entity.
|
2018-08-31 16:20:00 -07:00
|
|
|
// T is one of: EntityDetails, ObjectEntityDetails, ProcEntityDetails
|
2018-06-05 12:18:35 -07:00
|
|
|
template<typename T>
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &DeclareEntity(const parser::Name &name, Attrs attrs) {
|
2018-09-20 14:08:59 -07:00
|
|
|
Symbol &symbol{MakeSymbol(name, attrs)};
|
2018-08-31 16:20:00 -07:00
|
|
|
if (symbol.has<T>()) {
|
2018-06-05 12:18:35 -07:00
|
|
|
// OK
|
2018-08-31 16:20:00 -07:00
|
|
|
} else if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(T{});
|
|
|
|
} else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2019-01-17 16:14:36 -08:00
|
|
|
symbol.set_details(T{std::move(*details)});
|
2018-08-28 14:02:53 -07:00
|
|
|
} else if (std::is_same_v<EntityDetails, T> &&
|
2018-06-05 12:18:35 -07:00
|
|
|
(symbol.has<ObjectEntityDetails>() ||
|
|
|
|
symbol.has<ProcEntityDetails>())) {
|
|
|
|
// OK
|
2018-08-31 16:20:00 -07:00
|
|
|
} else if (auto *details{symbol.detailsIf<UseDetails>()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name.source,
|
2018-06-05 12:18:35 -07:00
|
|
|
"'%s' is use-associated from module '%s' and cannot be re-declared"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
name.source, details->module().name());
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (auto *details{symbol.detailsIf<SubprogramNameDetails>()}) {
|
2018-06-05 12:18:35 -07:00
|
|
|
if (details->kind() == SubprogramKind::Module) {
|
2018-09-20 14:08:59 -07:00
|
|
|
Say2(name,
|
2018-06-05 12:18:35 -07:00
|
|
|
"Declaration of '%s' conflicts with its use as module procedure"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol, "Module procedure definition"_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
} else if (details->kind() == SubprogramKind::Internal) {
|
2018-09-20 14:08:59 -07:00
|
|
|
Say2(name,
|
2018-06-05 12:18:35 -07:00
|
|
|
"Declaration of '%s' conflicts with its use as internal procedure"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol, "Internal procedure definition"_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
|
|
|
CHECK(!"unexpected kind");
|
|
|
|
}
|
2019-02-14 07:59:20 -08:00
|
|
|
} else if (std::is_same_v<ObjectEntityDetails, T> &&
|
|
|
|
symbol.has<ProcEntityDetails>()) {
|
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "'%s' is already declared as a procedure"_err_en_US);
|
|
|
|
} else if (std::is_same_v<ProcEntityDetails, T> &&
|
|
|
|
symbol.has<ObjectEntityDetails>()) {
|
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "'%s' is already declared as an object"_err_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-09-20 14:08:59 -07:00
|
|
|
SayAlreadyDeclared(name, symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
};
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
// Resolve construct entities and statement entities.
|
2018-09-25 08:53:53 -07:00
|
|
|
// Check that construct names don't conflict with other names.
|
2018-10-18 07:55:48 -07:00
|
|
|
class ConstructVisitor : public DeclarationVisitor {
|
2018-09-25 08:53:53 -07:00
|
|
|
public:
|
2018-10-18 07:55:48 -07:00
|
|
|
bool Pre(const parser::ConcurrentHeader &);
|
|
|
|
void Post(const parser::ConcurrentHeader &);
|
|
|
|
bool Pre(const parser::LocalitySpec::Local &);
|
|
|
|
bool Pre(const parser::LocalitySpec::LocalInit &);
|
|
|
|
bool Pre(const parser::LocalitySpec::Shared &);
|
2019-01-22 16:30:32 -08:00
|
|
|
bool Pre(const parser::AcSpec &);
|
|
|
|
bool Pre(const parser::AcImpliedDo &);
|
2018-10-18 07:55:48 -07:00
|
|
|
bool Pre(const parser::DataImpliedDo &);
|
2019-02-06 17:18:02 -08:00
|
|
|
bool Pre(const parser::DataStmtObject &);
|
2018-10-18 07:55:48 -07:00
|
|
|
bool Pre(const parser::DoConstruct &);
|
|
|
|
void Post(const parser::DoConstruct &);
|
|
|
|
void Post(const parser::ConcurrentControl &);
|
|
|
|
bool Pre(const parser::ForallConstruct &);
|
|
|
|
void Post(const parser::ForallConstruct &);
|
|
|
|
bool Pre(const parser::ForallStmt &);
|
|
|
|
void Post(const parser::ForallStmt &);
|
|
|
|
bool Pre(const parser::BlockStmt &);
|
|
|
|
bool Pre(const parser::EndBlockStmt &);
|
2019-01-15 16:59:20 -08:00
|
|
|
void Post(const parser::Selector &);
|
|
|
|
bool Pre(const parser::AssociateStmt &);
|
|
|
|
void Post(const parser::EndAssociateStmt &);
|
|
|
|
void Post(const parser::Association &);
|
|
|
|
void Post(const parser::SelectTypeStmt &);
|
|
|
|
bool Pre(const parser::SelectTypeConstruct::TypeCase &);
|
|
|
|
void Post(const parser::SelectTypeConstruct::TypeCase &);
|
|
|
|
void Post(const parser::TypeGuardStmt::Guard &);
|
2018-10-15 17:11:24 -07:00
|
|
|
|
2018-09-25 08:53:53 -07:00
|
|
|
// Definitions of construct names
|
|
|
|
bool Pre(const parser::WhereConstructStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::ForallConstructStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::ChangeTeamStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::CriticalStmt &x) { return CheckDef(x.t); }
|
2019-01-15 16:59:20 -08:00
|
|
|
bool Pre(const parser::LabelDoStmt &x) { common::die("should not happen"); }
|
2018-09-25 08:53:53 -07:00
|
|
|
bool Pre(const parser::NonLabelDoStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::IfThenStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::SelectCaseStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::SelectRankStmt &x) {
|
|
|
|
return CheckDef(std::get<0>(x.t));
|
|
|
|
}
|
|
|
|
bool Pre(const parser::SelectTypeStmt &x) {
|
|
|
|
return CheckDef(std::get<0>(x.t));
|
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
|
2018-09-25 08:53:53 -07:00
|
|
|
// References to construct names
|
|
|
|
void Post(const parser::MaskedElsewhereStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::ElsewhereStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndWhereStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndForallStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndChangeTeamStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::EndCriticalStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndDoStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::ElseIfStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::ElseStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndIfStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::CaseStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::EndSelectStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::SelectRankCaseStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::TypeGuardStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::CycleStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::ExitStmt &x) { CheckRef(x.v); }
|
|
|
|
|
|
|
|
private:
|
2019-01-16 13:21:15 -08:00
|
|
|
// This represents: associate-name => expr | variable
|
2019-01-15 16:59:20 -08:00
|
|
|
// expr is set unless there were errors
|
|
|
|
struct {
|
|
|
|
const parser::Name *name{nullptr};
|
|
|
|
const parser::Name *variable{nullptr};
|
|
|
|
MaybeExpr expr;
|
|
|
|
} association_;
|
|
|
|
|
2018-09-25 08:53:53 -07:00
|
|
|
template<typename T> bool CheckDef(const T &t) {
|
|
|
|
return CheckDef(std::get<std::optional<parser::Name>>(t));
|
|
|
|
}
|
|
|
|
template<typename T> void CheckRef(const T &t) {
|
|
|
|
CheckRef(std::get<std::optional<parser::Name>>(t));
|
|
|
|
}
|
|
|
|
bool CheckDef(const std::optional<parser::Name> &);
|
|
|
|
void CheckRef(const std::optional<parser::Name> &);
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &ToDeclTypeSpec(evaluate::DynamicType &&);
|
|
|
|
const DeclTypeSpec &ToDeclTypeSpec(
|
|
|
|
evaluate::DynamicType &&, SubscriptIntExpr &&length);
|
2019-01-15 16:59:20 -08:00
|
|
|
Symbol *MakeAssocEntity();
|
|
|
|
void SetTypeFromAssociation(Symbol &);
|
|
|
|
void SetAttrsFromAssociation(Symbol &);
|
2018-09-25 08:53:53 -07:00
|
|
|
};
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Walk the parse tree and resolve names to symbols.
|
2018-10-15 17:11:24 -07:00
|
|
|
class ResolveNamesVisitor : public virtual ScopeHandler,
|
|
|
|
public ModuleVisitor,
|
2018-05-30 14:11:45 -07:00
|
|
|
public SubprogramVisitor,
|
2018-10-18 07:55:48 -07:00
|
|
|
public ConstructVisitor {
|
2018-04-25 19:58:42 -07:00
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
2018-10-18 07:55:48 -07:00
|
|
|
using ConstructVisitor::Post;
|
|
|
|
using ConstructVisitor::Pre;
|
2018-06-14 13:43:02 -07:00
|
|
|
using DeclarationVisitor::Post;
|
|
|
|
using DeclarationVisitor::Pre;
|
2018-05-04 15:37:56 -07:00
|
|
|
using ImplicitRulesVisitor::Post;
|
|
|
|
using ImplicitRulesVisitor::Pre;
|
2018-05-14 13:51:13 -07:00
|
|
|
using InterfaceVisitor::Post;
|
|
|
|
using InterfaceVisitor::Pre;
|
2018-04-25 19:58:42 -07:00
|
|
|
using ModuleVisitor::Post;
|
|
|
|
using ModuleVisitor::Pre;
|
2018-11-28 15:55:55 -08:00
|
|
|
using ScopeHandler::Post;
|
|
|
|
using ScopeHandler::Pre;
|
2018-05-04 15:37:56 -07:00
|
|
|
using SubprogramVisitor::Post;
|
|
|
|
using SubprogramVisitor::Pre;
|
2018-04-25 19:58:42 -07:00
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
ResolveNamesVisitor(SemanticsContext &context) {
|
|
|
|
set_context(context);
|
2018-10-26 07:34:50 -07:00
|
|
|
set_this(this);
|
2018-10-22 07:37:38 -07:00
|
|
|
PushScope(context.globalScope());
|
2018-10-15 17:11:24 -07:00
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
|
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 &) {}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
void Post(const parser::SpecificationPart &);
|
|
|
|
bool Pre(const parser::MainProgram &);
|
|
|
|
void Post(const parser::EndProgramStmt &);
|
2018-03-30 13:57:23 -07:00
|
|
|
void Post(const parser::Program &);
|
2019-02-21 19:14:28 -08:00
|
|
|
bool Pre(const parser::ImplicitStmt &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::PointerObject &);
|
|
|
|
void Post(const parser::AllocateObject &);
|
2019-02-26 14:26:28 -08:00
|
|
|
bool Pre(const parser::PointerAssignmentStmt &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::PointerAssignmentStmt &);
|
|
|
|
void Post(const parser::Designator &);
|
|
|
|
template<typename T> void Post(const parser::LoopBounds<T> &);
|
|
|
|
void Post(const parser::ProcComponentRef &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::FunctionReference &);
|
|
|
|
bool Pre(const parser::CallStmt &);
|
2018-08-22 16:05:06 -07:00
|
|
|
bool Pre(const parser::ImportStmt &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::TypeGuardStmt &);
|
|
|
|
bool Pre(const parser::StmtFunctionStmt &);
|
2018-04-23 12:33:10 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
private:
|
2018-06-05 12:18:35 -07:00
|
|
|
// Kind of procedure we are expecting to see in a ProcedureDesignator
|
|
|
|
std::optional<Symbol::Flag> expectedProcFlag_;
|
2018-08-22 16:05:06 -07:00
|
|
|
const SourceName *prevImportStmt_{nullptr};
|
2018-06-05 12:18:35 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Each of these returns a pointer to a resolved Name (i.e. with symbol)
|
|
|
|
// or nullptr in case of error.
|
|
|
|
const parser::Name *ResolveStructureComponent(
|
|
|
|
const parser::StructureComponent &);
|
|
|
|
const parser::Name *ResolveArrayElement(const parser::ArrayElement &);
|
|
|
|
const parser::Name *ResolveCoindexedNamedObject(
|
|
|
|
const parser::CoindexedNamedObject &);
|
|
|
|
const parser::Name *ResolveDataRef(const parser::DataRef &);
|
|
|
|
const parser::Name *ResolveName(const parser::Name &);
|
|
|
|
const parser::Name *FindComponent(const parser::Name *, const parser::Name &);
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
void CheckImports();
|
|
|
|
void CheckImport(const SourceName &, const SourceName &);
|
2019-02-21 15:01:39 -08:00
|
|
|
void HandleCall(Symbol::Flag, const parser::Call &);
|
|
|
|
void HandleProcedureName(Symbol::Flag, const parser::Name &);
|
|
|
|
bool SetProcFlag(const parser::Name &, Symbol &, Symbol::Flag);
|
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-08-27 11:48:49 -07:00
|
|
|
bool ImplicitRules::isImplicitNoneType() const {
|
|
|
|
if (isImplicitNoneType_.has_value()) {
|
|
|
|
return isImplicitNoneType_.value();
|
|
|
|
} else if (inheritFromParent_) {
|
|
|
|
return parent_->isImplicitNoneType();
|
|
|
|
} else {
|
|
|
|
return false; // default if not specified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRules::isImplicitNoneExternal() const {
|
|
|
|
if (isImplicitNoneExternal_.has_value()) {
|
|
|
|
return isImplicitNoneExternal_.value();
|
|
|
|
} else if (inheritFromParent_) {
|
|
|
|
return parent_->isImplicitNoneExternal();
|
|
|
|
} else {
|
|
|
|
return false; // default if not specified
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *ImplicitRules::GetType(char ch) const {
|
2018-08-28 14:02:53 -07:00
|
|
|
if (auto it{map_.find(ch)}; it != map_.end()) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return it->second;
|
2018-10-22 07:37:38 -07:00
|
|
|
} else if (inheritFromParent_ && parent_->context_) {
|
2018-08-27 11:48:49 -07:00
|
|
|
return parent_->GetType(ch);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'i' && ch <= 'n') {
|
2018-12-17 12:41:43 -08:00
|
|
|
return &context_->MakeNumericType(TypeCategory::Integer);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'a' && ch <= 'z') {
|
2018-12-17 12:41:43 -08:00
|
|
|
return &context_->MakeNumericType(TypeCategory::Real);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else {
|
2018-12-11 14:51:08 -08:00
|
|
|
return nullptr;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
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)) {
|
2018-12-11 14:51:08 -08:00
|
|
|
auto res{map_.emplace(ch, &type)};
|
2018-04-05 17:02:31 -07:00
|
|
|
if (!res.second && !isDefault) {
|
2018-10-22 07:37:38 -07:00
|
|
|
context_->Say(lo,
|
2018-05-03 15:57:56 -07:00
|
|
|
"More than one implicit type specified for '%s'"_err_en_US,
|
2018-10-22 07:37:38 -07:00
|
|
|
std::string(1, ch).c_str());
|
2018-04-05 17:02:31 -07:00
|
|
|
}
|
|
|
|
if (ch == *hi) {
|
|
|
|
break;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto it{implicitRules.map_.find(ch)};
|
2018-04-06 10:46:30 -07:00
|
|
|
if (it != implicitRules.map_.end()) {
|
2018-12-11 14:51:08 -08:00
|
|
|
o << " " << ch << ": " << *it->second << '\n';
|
2018-04-06 10:46:30 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
template<typename T> void BaseVisitor::Walk(const T &x) {
|
|
|
|
parser::Walk(x, *this_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SourceName *BaseVisitor::currStmtSource() {
|
|
|
|
return messageHandler_.currStmtSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseVisitor::set_context(SemanticsContext &context) {
|
|
|
|
context_ = &context;
|
|
|
|
messageHandler_.set_messages(context.messages());
|
|
|
|
}
|
|
|
|
|
2019-01-04 17:10:31 -08:00
|
|
|
void BaseVisitor::MakePlaceholder(
|
|
|
|
const parser::Name &name, MiscDetails::Kind kind) {
|
|
|
|
if (!name.symbol) {
|
|
|
|
name.symbol = &context_->globalScope().MakeSymbol(
|
2019-01-07 10:10:15 -08:00
|
|
|
name.source, Attrs{}, MiscDetails{kind});
|
2019-01-04 17:10:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// AttrsVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-08-31 15:15:28 -07:00
|
|
|
bool 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-08-31 15:15:28 -07:00
|
|
|
return true;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
Attrs AttrsVisitor::GetAttrs() {
|
|
|
|
CHECK(attrs_);
|
|
|
|
return *attrs_;
|
|
|
|
}
|
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();
|
2019-01-04 17:10:31 -08:00
|
|
|
passName_.reset();
|
|
|
|
bindName_.reset();
|
2018-03-30 13:57:23 -07:00
|
|
|
return result;
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
|
|
|
|
bool AttrsVisitor::SetPassNameOn(Symbol &symbol) {
|
|
|
|
if (!passName_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](ProcEntityDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[&](ProcBindingDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[](auto &) { common::die("unexpected pass name"); },
|
|
|
|
},
|
|
|
|
symbol.details());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttrsVisitor::SetBindNameOn(Symbol &symbol) {
|
|
|
|
if (!bindName_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](EntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](ObjectEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](ProcEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](SubprogramDetails &x) { x.set_bindName(std::move(bindName_)); },
|
2019-02-18 11:39:46 -08:00
|
|
|
[&](CommonBlockDetails &x) { x.set_bindName(std::move(bindName_)); },
|
2019-01-04 17:10:31 -08:00
|
|
|
[](auto &) { common::die("unexpected bind name"); },
|
|
|
|
},
|
|
|
|
symbol.details());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void AttrsVisitor::Post(const parser::LanguageBindingSpec &x) {
|
2018-07-16 16:23:18 -07:00
|
|
|
CHECK(attrs_);
|
2019-02-15 10:16:25 -08:00
|
|
|
attrs_->set(Attr::BIND_C);
|
2018-03-30 13:57:23 -07:00
|
|
|
if (x.v) {
|
2019-01-04 17:10:31 -08:00
|
|
|
bindName_ = EvaluateExpr(*x.v);
|
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) {
|
2018-07-11 17:45:13 -07:00
|
|
|
CHECK(attrs_);
|
|
|
|
attrs_->set(IntentSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
bool AttrsVisitor::Pre(const parser::Pass &x) {
|
|
|
|
if (x.v) {
|
|
|
|
passName_ = x.v->source;
|
|
|
|
MakePlaceholder(*x.v, MiscDetails::Kind::PassName);
|
|
|
|
} else {
|
|
|
|
attrs_->set(Attr::PASS);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec *DeclTypeSpecVisitor::GetDeclTypeSpec() {
|
2018-12-18 14:35:58 -08:00
|
|
|
return state_.declTypeSpec;
|
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::BeginDeclTypeSpec() {
|
2018-12-18 14:35:58 -08:00
|
|
|
CHECK(!state_.expectDeclTypeSpec);
|
|
|
|
CHECK(!state_.declTypeSpec);
|
|
|
|
state_.expectDeclTypeSpec = true;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::EndDeclTypeSpec() {
|
2018-12-18 14:35:58 -08:00
|
|
|
CHECK(state_.expectDeclTypeSpec);
|
|
|
|
state_ = {};
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2019-01-17 13:52:10 -08:00
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpecCategory(
|
|
|
|
DeclTypeSpec::Category category) {
|
|
|
|
CHECK(state_.expectDeclTypeSpec);
|
|
|
|
state_.derived.category = category;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::TypeGuardStmt &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeGuardStmt &) {
|
|
|
|
// TODO: TypeGuardStmt
|
|
|
|
EndDeclTypeSpec();
|
2018-12-18 14:35:58 -08:00
|
|
|
}
|
|
|
|
|
2019-01-22 16:30:32 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeSpec &typeSpec) {
|
|
|
|
// Record the resolved DeclTypeSpec in the parse tree for use by
|
|
|
|
// expression semantics if the DeclTypeSpec is a valid TypeSpec.
|
|
|
|
// The grammar ensures that it's an intrinsic or derived type spec,
|
|
|
|
// not TYPE(*) or CLASS(*) or CLASS(T).
|
|
|
|
if (const DeclTypeSpec * spec{state_.declTypeSpec}) {
|
|
|
|
switch (spec->category()) {
|
|
|
|
case DeclTypeSpec::Numeric:
|
|
|
|
case DeclTypeSpec::Logical:
|
|
|
|
case DeclTypeSpec::Character: typeSpec.declTypeSpec = spec; break;
|
|
|
|
case DeclTypeSpec::TypeDerived:
|
|
|
|
if (const DerivedTypeSpec * derived{spec->AsDerived()}) {
|
|
|
|
if (derived->typeSymbol().attrs().test(Attr::ABSTRACT)) {
|
|
|
|
Say("ABSTRACT derived type may not be used here"_err_en_US);
|
|
|
|
}
|
|
|
|
typeSpec.declTypeSpec = spec;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(
|
2018-05-17 13:06:38 -07:00
|
|
|
const parser::IntrinsicTypeSpec::DoublePrecision &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(
|
2018-10-22 07:37:38 -07:00
|
|
|
TypeCategory::Real, context().defaultKinds().doublePrecisionKind());
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(
|
2018-09-10 12:20:42 -07:00
|
|
|
const parser::IntrinsicTypeSpec::DoubleComplex &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(
|
2018-10-22 07:37:38 -07:00
|
|
|
TypeCategory::Complex, context().defaultKinds().doublePrecisionKind());
|
2018-12-06 06:59:37 -08:00
|
|
|
}
|
2018-12-17 12:41:43 -08:00
|
|
|
void DeclTypeSpecVisitor::MakeNumericType(TypeCategory category, int kind) {
|
|
|
|
SetDeclTypeSpec(context().MakeNumericType(category, kind));
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-06-22 14:08:04 -07:00
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::ClassStar &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
SetDeclTypeSpec(context().globalScope().MakeClassStarType());
|
2018-06-22 14:08:04 -07:00
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::TypeStar &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
SetDeclTypeSpec(context().globalScope().MakeTypeStarType());
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Check that we're expecting to see a DeclTypeSpec (and haven't seen one yet)
|
2018-12-18 14:35:58 -08:00
|
|
|
// and save it in state_.declTypeSpec.
|
2019-01-15 16:59:20 -08:00
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec) {
|
2018-12-18 14:35:58 -08:00
|
|
|
CHECK(state_.expectDeclTypeSpec);
|
|
|
|
CHECK(!state_.declTypeSpec);
|
|
|
|
state_.declTypeSpec = &declTypeSpec;
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
KindExpr DeclTypeSpecVisitor::GetKindParamExpr(
|
2018-12-06 06:59:37 -08:00
|
|
|
TypeCategory category, const std::optional<parser::KindSelector> &kind) {
|
2018-12-04 10:55:32 -08:00
|
|
|
return AnalyzeKindSelector(context(), *currStmtSource(), category, kind);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// MessageHandler implementation
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
void MessageHandler::set_currStmtSource(const SourceName *source) {
|
|
|
|
currStmtSource_ = source;
|
|
|
|
}
|
|
|
|
Message &MessageHandler::Say(MessageFixedText &&msg) {
|
2018-04-04 09:03:51 -07:00
|
|
|
CHECK(currStmtSource_);
|
2018-10-22 07:37:38 -07:00
|
|
|
return messages_->Say(*currStmtSource_, std::move(msg));
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2019-02-18 11:39:46 -08:00
|
|
|
Message &MessageHandler::Say(MessageFormattedText &&msg) {
|
|
|
|
CHECK(currStmtSource_);
|
|
|
|
return messages_->Say(*currStmtSource_, std::move(msg));
|
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &name, MessageFixedText &&msg) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return Say(name, std::move(msg), name);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &location, MessageFixedText &&msg,
|
|
|
|
const SourceName &arg1) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return messages_->Say(location, std::move(msg), arg1.ToString().c_str());
|
2018-05-03 15:57:56 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &location, MessageFixedText &&msg,
|
|
|
|
const SourceName &arg1, const SourceName &arg2) {
|
2018-10-22 07:37:38 -07:00
|
|
|
return messages_->Say(location, std::move(msg), arg1.ToString().c_str(),
|
2018-08-27 11:48:49 -07:00
|
|
|
arg2.ToString().c_str());
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
|
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(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-04 09:03:51 -07:00
|
|
|
[&](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-07-17 07:02:30 -07:00
|
|
|
auto loLoc{std::get<parser::Location>(x.t)};
|
|
|
|
auto hiLoc{loLoc};
|
|
|
|
if (auto hiLocOpt{std::get<std::optional<parser::Location>>(x.t)}) {
|
2018-04-04 09:03:51 -07:00
|
|
|
hiLoc = *hiLocOpt;
|
|
|
|
if (*hiLoc < *loLoc) {
|
2018-05-03 15:57:56 -07:00
|
|
|
Say(hiLoc, "'%s' does not follow '%s' alphabetically"_err_en_US,
|
|
|
|
std::string(hiLoc, 1), std::string(loLoc, 1));
|
2018-04-04 09:03:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
implicitRules().SetType(*GetDeclTypeSpec(), 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() {
|
2018-10-22 07:37:38 -07:00
|
|
|
implicitRules_ = std::make_unique<ImplicitRules>(std::move(implicitRules_));
|
|
|
|
implicitRules_->set_context(context());
|
2018-04-04 09:03:51 -07:00
|
|
|
prevImplicit_ = nullptr;
|
|
|
|
prevImplicitNone_ = nullptr;
|
|
|
|
prevImplicitNoneType_ = nullptr;
|
|
|
|
prevParameterStmt_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
void ImplicitRulesVisitor::PopScope() {
|
|
|
|
implicitRules_ = std::move(implicitRules_->parent());
|
2018-04-17 15:04:08 -07:00
|
|
|
}
|
|
|
|
|
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);
|
2018-05-04 15:37:56 -07:00
|
|
|
Say(*prevImplicitNone_, "Previous IMPLICIT NONE statement"_en_US);
|
2018-04-04 09:03:51 -07:00
|
|
|
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-05-14 14:26:39 -07:00
|
|
|
// ArraySpecVisitor implementation
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(const parser::ArraySpec &x) {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::DeferredShapeSpecList &x) {
|
2018-05-14 14:26:39 -07:00
|
|
|
for (int i = 0; i < x.v; ++i) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeDeferred());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedShapeSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &lb{x.v};
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeAssumed(GetBound(*lb)) : ShapeSpec::MakeAssumed());
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::ExplicitShapeSpec &x) {
|
2018-11-06 17:18:06 -08:00
|
|
|
auto &&ub{GetBound(std::get<parser::SpecificationExpr>(x.t))};
|
|
|
|
if (const auto &lb{std::get<std::optional<parser::SpecificationExpr>>(x.t)}) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeExplicit(GetBound(*lb), std::move(ub)));
|
|
|
|
} else {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeExplicit(Bound{1}, std::move(ub)));
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedImpliedSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &lb{x.v};
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeImplied(GetBound(*lb)) : ShapeSpec::MakeImplied());
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedRankSpec &) {
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(ShapeSpec::MakeAssumedRank());
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
const ArraySpec &ArraySpecVisitor::arraySpec() {
|
|
|
|
return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::BeginArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
CHECK(attrArraySpec_.empty());
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::EndArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
attrArraySpec_.clear();
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::PostAttrSpec() {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
Bound ArraySpecVisitor::GetBound(const parser::SpecificationExpr &x) {
|
2019-01-07 15:05:53 -08:00
|
|
|
return Bound{EvaluateSubscriptIntExpr(x.v)};
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ScopeHandler implementation
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
void ScopeHandler::SayAlreadyDeclared(
|
|
|
|
const parser::Name &name, const Symbol &prev) {
|
2019-02-26 13:28:53 -08:00
|
|
|
SayAlreadyDeclared(name.source, prev);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
2019-02-26 13:28:53 -08:00
|
|
|
void ScopeHandler::SayAlreadyDeclared(
|
|
|
|
const SourceName &name, const Symbol &prev) {
|
|
|
|
auto &msg{
|
|
|
|
Say(name, "'%s' is already declared in this scoping unit"_err_en_US)};
|
|
|
|
if (const auto *details{prev.detailsIf<UseDetails>()}) {
|
|
|
|
msg.Attach(details->location(),
|
|
|
|
"It is use-associated with '%s' in module '%s'"_err_en_US,
|
|
|
|
details->symbol().name().ToString().c_str(),
|
|
|
|
details->module().name().ToString().c_str());
|
|
|
|
} else {
|
|
|
|
msg.Attach(prev.name(), "Previous declaration of '%s'"_en_US,
|
|
|
|
prev.name().ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:51:36 -08:00
|
|
|
void ScopeHandler::SayWithDecl(
|
|
|
|
const parser::Name &name, const Symbol &symbol, MessageFixedText &&msg) {
|
2019-02-22 11:38:43 -08:00
|
|
|
Say2(name, std::move(msg), symbol,
|
|
|
|
symbol.test(Symbol::Flag::Implicit) ? "Implicit declaration of '%s'"_en_US
|
|
|
|
: "Declaration of '%s'"_en_US);
|
2019-02-05 13:51:36 -08:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
void ScopeHandler::SayDerivedType(
|
|
|
|
const SourceName &name, MessageFixedText &&msg, const Scope &type) {
|
2018-12-04 10:55:32 -08:00
|
|
|
const Symbol *typeSymbol{type.GetSymbol()};
|
|
|
|
CHECK(typeSymbol != nullptr);
|
|
|
|
Say(name, std::move(msg), name, typeSymbol->name())
|
|
|
|
.Attach(typeSymbol->name(), "Declaration of derived type '%s'"_en_US,
|
|
|
|
typeSymbol->name().ToString().c_str());
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
2019-02-14 07:59:20 -08:00
|
|
|
void ScopeHandler::Say2(const SourceName &name1, MessageFixedText &&msg1,
|
|
|
|
const SourceName &name2, MessageFixedText &&msg2) {
|
|
|
|
Say(name1, std::move(msg1))
|
|
|
|
.Attach(name2, std::move(msg2), name2.ToString().c_str());
|
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
|
|
|
|
const Symbol &symbol, MessageFixedText &&msg2) {
|
2019-02-14 07:59:20 -08:00
|
|
|
Say2(name.source, std::move(msg1), symbol.name(), std::move(msg2));
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
Scope &ScopeHandler::InclusiveScope() {
|
|
|
|
for (auto *scope{&currScope()};; scope = &scope->parent()) {
|
|
|
|
if (scope->kind() != Scope::Kind::Block &&
|
|
|
|
scope->kind() != Scope::Kind::DerivedType) {
|
|
|
|
return *scope;
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
common::die("inclusive scope not found");
|
|
|
|
}
|
|
|
|
Scope &ScopeHandler::GlobalScope() {
|
|
|
|
for (auto *scope = currScope_; scope; scope = &scope->parent()) {
|
|
|
|
if (scope->kind() == Scope::Kind::Global) {
|
|
|
|
return *scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
common::die("global scope not found");
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-08-22 16:56:57 -07:00
|
|
|
void ScopeHandler::PushScope(Scope::Kind kind, Symbol *symbol) {
|
|
|
|
PushScope(currScope().MakeScope(kind, symbol));
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
void ScopeHandler::PushScope(Scope &scope) {
|
2018-08-09 17:12:31 -07:00
|
|
|
currScope_ = &scope;
|
2018-11-16 12:43:08 -08:00
|
|
|
auto kind{currScope_->kind()};
|
|
|
|
if (kind != Scope::Kind::Block) {
|
2018-08-27 11:48:49 -07:00
|
|
|
ImplicitRulesVisitor::PushScope();
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (kind != Scope::Kind::DerivedType) {
|
|
|
|
if (auto *symbol{scope.symbol()}) {
|
2019-02-14 07:59:20 -08:00
|
|
|
// Create a dummy symbol so we can't create another one with the same
|
|
|
|
// name. It might already be there if we previously pushed the scope.
|
2018-11-16 12:43:08 -08:00
|
|
|
if (!FindInScope(scope, symbol->name())) {
|
|
|
|
auto &newSymbol{CopySymbol(*symbol)};
|
|
|
|
if (kind == Scope::Kind::Subprogram) {
|
|
|
|
newSymbol.set_details(symbol->get<SubprogramDetails>());
|
|
|
|
} else {
|
|
|
|
newSymbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ScopeHandler::PopScope() {
|
2018-09-22 08:05:46 -07:00
|
|
|
for (auto &pair : currScope()) {
|
|
|
|
auto &symbol{*pair.second};
|
|
|
|
ConvertToObjectEntity(symbol); // if not a proc by now, it is an object
|
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
if (currScope_->kind() != Scope::Kind::Block) {
|
|
|
|
ImplicitRulesVisitor::PopScope();
|
|
|
|
}
|
2018-08-09 17:12:31 -07:00
|
|
|
currScope_ = &currScope_->parent();
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *ScopeHandler::FindSymbol(const parser::Name &name) {
|
|
|
|
return FindSymbol(currScope(), name);
|
|
|
|
}
|
|
|
|
Symbol *ScopeHandler::FindSymbol(const Scope &scope, const parser::Name &name) {
|
2018-12-04 10:55:32 -08:00
|
|
|
// Scope::FindSymbol() skips over innermost derived type scopes.
|
|
|
|
// Ensure that "bare" type parameter names are not overlooked.
|
|
|
|
if (Symbol * symbol{FindInTypeOrParents(scope, name.source)}) {
|
|
|
|
if (symbol->has<TypeParamDetails>()) {
|
|
|
|
return Resolve(name, symbol);
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
return Resolve(name, scope.FindSymbol(name.source));
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol &ScopeHandler::Resolve(const parser::Name &name, Symbol &symbol) {
|
|
|
|
return *Resolve(name, &symbol);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *ScopeHandler::Resolve(const parser::Name &name, Symbol *symbol) {
|
2019-02-12 17:24:43 -08:00
|
|
|
if (symbol) {
|
|
|
|
// TODO: Should name.symbol be unconditionally updated?
|
|
|
|
// Or should it be an internal error if name.symbol is
|
|
|
|
// set to a distinct symbol?
|
|
|
|
if (name.symbol == nullptr) {
|
|
|
|
name.symbol = symbol;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol &ScopeHandler::MakeSymbol(
|
|
|
|
Scope &scope, const SourceName &name, Attrs attrs) {
|
|
|
|
auto *symbol{FindInScope(scope, name)};
|
|
|
|
if (symbol) {
|
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
} else {
|
|
|
|
const auto pair{scope.try_emplace(name, attrs, UnknownDetails{})};
|
|
|
|
CHECK(pair.second); // name was not found, so must be able to add
|
|
|
|
symbol = pair.first->second;
|
|
|
|
}
|
|
|
|
return *symbol;
|
|
|
|
}
|
|
|
|
Symbol &ScopeHandler::MakeSymbol(const parser::Name &name, Attrs attrs) {
|
|
|
|
return Resolve(name, MakeSymbol(currScope(), name.source, attrs));
|
|
|
|
}
|
|
|
|
Symbol &ScopeHandler::CopySymbol(const Symbol &symbol) {
|
|
|
|
CHECK(!FindInScope(currScope(), symbol.name()));
|
|
|
|
return MakeSymbol(currScope(), symbol.name(), symbol.attrs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for name only in scope, not in enclosing scopes.
|
|
|
|
Symbol *ScopeHandler::FindInScope(
|
|
|
|
const Scope &scope, const parser::Name &name) {
|
|
|
|
return Resolve(name, FindInScope(scope, name.source));
|
|
|
|
}
|
|
|
|
Symbol *ScopeHandler::FindInScope(const Scope &scope, const SourceName &name) {
|
|
|
|
if (auto it{scope.find(name)}; it != scope.end()) {
|
|
|
|
return it->second;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
// Find a component or type parameter by name in a derived type or its parents.
|
|
|
|
Symbol *ScopeHandler::FindInTypeOrParents(const Scope &scope, SourceName name) {
|
|
|
|
if (scope.kind() == Scope::Kind::DerivedType) {
|
|
|
|
if (Symbol * symbol{FindInScope(scope, name)}) {
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
if (const Scope * parent{scope.GetDerivedTypeParent()}) {
|
|
|
|
return FindInTypeOrParents(*parent, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-02-08 08:57:28 -08:00
|
|
|
Symbol *ScopeHandler::FindInTypeOrParents(
|
|
|
|
const Scope &scope, const parser::Name &name) {
|
|
|
|
return Resolve(name, FindInTypeOrParents(scope, name.source));
|
|
|
|
}
|
|
|
|
Symbol *ScopeHandler::FindInTypeOrParents(const parser::Name &name) {
|
|
|
|
return FindInTypeOrParents(currScope(), name);
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
void ScopeHandler::EraseSymbol(const parser::Name &name) {
|
|
|
|
currScope().erase(name.source);
|
|
|
|
name.symbol = nullptr;
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
static bool NeedsType(const Symbol &symbol) {
|
|
|
|
if (symbol.GetType()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
|
|
|
if (details->interface().symbol()) {
|
|
|
|
return false; // the interface determines the type
|
|
|
|
}
|
|
|
|
if (!symbol.test(Symbol::Flag::Function)) {
|
|
|
|
return false; // not known to be a function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
void ScopeHandler::ApplyImplicitRules(Symbol &symbol) {
|
|
|
|
ConvertToObjectEntity(symbol);
|
2018-10-10 16:20:46 -07:00
|
|
|
if (NeedsType(symbol)) {
|
2018-10-18 07:55:48 -07:00
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(symbol.name(), "No explicit type declared for '%s'"_err_en_US);
|
2018-12-11 14:51:08 -08:00
|
|
|
} else if (const auto *type{GetImplicitType(symbol)}) {
|
2018-10-10 16:20:46 -07:00
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *ScopeHandler::GetImplicitType(Symbol &symbol) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &name{symbol.name()};
|
2018-12-11 14:51:08 -08:00
|
|
|
const auto *type{implicitRules().GetType(name.begin()[0])};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (type) {
|
|
|
|
symbol.set(Symbol::Flag::Implicit);
|
|
|
|
} else {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2018-04-25 19:58:42 -07:00
|
|
|
|
2018-09-22 08:05:46 -07:00
|
|
|
// Convert symbol to be a ObjectEntity or return false if it can't be.
|
|
|
|
bool ScopeHandler::ConvertToObjectEntity(Symbol &symbol) {
|
|
|
|
if (symbol.has<ObjectEntityDetails>()) {
|
|
|
|
// nothing to do
|
|
|
|
} else if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(ObjectEntityDetails{});
|
|
|
|
} else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2019-01-04 17:10:31 -08:00
|
|
|
symbol.set_details(ObjectEntityDetails{std::move(*details)});
|
2019-01-31 13:18:30 -08:00
|
|
|
} else if (auto *useDetails{symbol.detailsIf<UseDetails>()}) {
|
|
|
|
return useDetails->symbol().has<ObjectEntityDetails>();
|
2018-09-22 08:05:46 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Convert symbol to be a ProcEntity or return false if it can't be.
|
|
|
|
bool ScopeHandler::ConvertToProcEntity(Symbol &symbol) {
|
|
|
|
if (symbol.has<ProcEntityDetails>()) {
|
|
|
|
// nothing to do
|
|
|
|
} else if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(ProcEntityDetails{});
|
|
|
|
} else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2019-01-04 17:10:31 -08:00
|
|
|
symbol.set_details(ProcEntityDetails{std::move(*details)});
|
2019-02-21 15:01:39 -08:00
|
|
|
if (symbol.GetType() && !symbol.test(Symbol::Flag::Implicit)) {
|
|
|
|
CHECK(!symbol.test(Symbol::Flag::Subroutine));
|
|
|
|
symbol.set(Symbol::Flag::Function);
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &ScopeHandler::MakeNumericType(
|
2018-12-04 10:55:32 -08:00
|
|
|
TypeCategory category, const std::optional<parser::KindSelector> &kind) {
|
|
|
|
KindExpr value{GetKindParamExpr(category, kind)};
|
|
|
|
if (auto known{evaluate::ToInt64(value)}) {
|
|
|
|
return context().MakeNumericType(category, static_cast<int>(*known));
|
|
|
|
} else {
|
|
|
|
return currScope_->MakeNumericType(category, std::move(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &ScopeHandler::MakeLogicalType(
|
2018-12-04 10:55:32 -08:00
|
|
|
const std::optional<parser::KindSelector> &kind) {
|
|
|
|
KindExpr value{GetKindParamExpr(TypeCategory::Logical, kind)};
|
|
|
|
if (auto known{evaluate::ToInt64(value)}) {
|
|
|
|
return context().MakeLogicalType(static_cast<int>(*known));
|
|
|
|
} else {
|
|
|
|
return currScope_->MakeLogicalType(std::move(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ModuleVisitor implementation
|
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Only &x) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2019-01-15 16:59:20 -08:00
|
|
|
[&](const Indirection<parser::GenericSpec> &generic) {
|
2018-05-14 14:26:39 -07:00
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Name &name) { AddUse(name); },
|
2018-06-18 11:03:43 -07:00
|
|
|
[](const auto &) { common::die("TODO: GenericSpec"); },
|
2018-05-14 14:26:39 -07:00
|
|
|
},
|
|
|
|
generic->u);
|
|
|
|
},
|
|
|
|
[&](const parser::Name &name) { AddUse(name); },
|
|
|
|
[&](const parser::Rename &rename) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Rename::Names &names) { AddUse(names); },
|
|
|
|
[&](const parser::Rename::Operators &ops) {
|
2018-06-18 11:03:43 -07:00
|
|
|
common::die("TODO: Rename::Operators");
|
2018-05-14 14:26:39 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rename.u);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleVisitor::Pre(const parser::Rename::Names &x) {
|
|
|
|
AddUse(x);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set useModuleScope_ to the Scope of the module being used.
|
|
|
|
bool ModuleVisitor::Pre(const parser::UseStmt &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
useModuleScope_ = FindModule(x.moduleName);
|
2018-07-25 06:55:11 -07:00
|
|
|
return useModuleScope_ != nullptr;
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ModuleVisitor::Post(const parser::UseStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *list{std::get_if<std::list<parser::Rename>>(&x.u)}) {
|
2018-05-14 14:26:39 -07:00
|
|
|
// Not a use-only: collect the names that were used in renames,
|
|
|
|
// then add a use for each public name that was not renamed.
|
|
|
|
std::set<SourceName> useNames;
|
|
|
|
for (const auto &rename : *list) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Rename::Names &names) {
|
|
|
|
useNames.insert(std::get<1>(names.t).source);
|
|
|
|
},
|
|
|
|
[&](const parser::Rename::Operators &ops) {
|
|
|
|
CHECK(!"TODO: Rename::Operators");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rename.u);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
for (const auto &[name, symbol] : *useModuleScope_) {
|
|
|
|
if (symbol->attrs().test(Attr::PUBLIC) &&
|
|
|
|
!symbol->detailsIf<MiscDetails>()) {
|
2018-05-14 14:26:39 -07:00
|
|
|
if (useNames.count(name) == 0) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *localSymbol{FindInScope(currScope(), name)};
|
|
|
|
if (!localSymbol) {
|
|
|
|
localSymbol = &CopySymbol(*symbol);
|
|
|
|
}
|
|
|
|
AddUse(x.moduleName.source, *symbol, *localSymbol);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useModuleScope_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(const parser::Rename::Names &names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &useName{std::get<0>(names.t)};
|
|
|
|
const auto &localName{std::get<1>(names.t)};
|
|
|
|
AddUse(useName.source, useName, localName);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ModuleVisitor::AddUse(const parser::Name &useName) {
|
2018-11-16 12:43:08 -08:00
|
|
|
AddUse(useName.source, useName, useName);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
void ModuleVisitor::AddUse(const SourceName &location,
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &localName, const parser::Name &useName) {
|
2018-05-14 14:26:39 -07:00
|
|
|
if (!useModuleScope_) {
|
|
|
|
return; // error occurred finding module
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *useSymbol{FindInScope(*useModuleScope_, useName)};
|
|
|
|
if (!useSymbol) {
|
|
|
|
Say(useName, "'%s' not found in module '%s'"_err_en_US, useName.source,
|
2018-05-14 14:26:39 -07:00
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (useSymbol->attrs().test(Attr::PRIVATE)) {
|
|
|
|
Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, useName.source,
|
2018-05-14 14:26:39 -07:00
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
AddUse(location, *useSymbol, MakeSymbol(localName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(
|
|
|
|
const SourceName &location, const Symbol &useSymbol, Symbol &localSymbol) {
|
|
|
|
localSymbol.attrs() = useSymbol.attrs();
|
2018-05-14 14:26:39 -07:00
|
|
|
localSymbol.attrs() &= ~Attrs{Attr::PUBLIC, Attr::PRIVATE};
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.flags() = useSymbol.flags();
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{localSymbol.detailsIf<UseDetails>()}) {
|
2018-08-29 11:38:12 -07:00
|
|
|
// check for use-associating the same symbol again:
|
2018-05-14 14:26:39 -07:00
|
|
|
if (localSymbol.GetUltimate() != useSymbol.GetUltimate()) {
|
|
|
|
localSymbol.set_details(
|
2018-08-29 11:38:12 -07:00
|
|
|
UseErrorDetails{*details}.add_occurrence(location, *useModuleScope_));
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (auto *details{localSymbol.detailsIf<UseErrorDetails>()}) {
|
2018-05-14 14:26:39 -07:00
|
|
|
details->add_occurrence(location, *useModuleScope_);
|
2018-10-10 16:20:46 -07:00
|
|
|
} else if (!localSymbol.has<UnknownDetails>()) {
|
|
|
|
Say(location,
|
|
|
|
"Cannot use-associate '%s'; it is already declared in this scope"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.name())
|
2018-10-10 16:20:46 -07:00
|
|
|
.Attach(localSymbol.name(), "Previous declaration of '%s'"_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.name().ToString().c_str());
|
2018-05-14 14:26:39 -07:00
|
|
|
} else {
|
2018-08-29 11:38:12 -07:00
|
|
|
localSymbol.set_details(UseDetails{location, useSymbol});
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:21:27 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Submodule &x) {
|
|
|
|
auto &stmt{std::get<parser::Statement<parser::SubmoduleStmt>>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(stmt.statement.t)};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
|
|
|
auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &ancestorName{std::get<parser::Name>(parentId.t)};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &parentName{std::get<std::optional<parser::Name>>(parentId.t)};
|
|
|
|
Scope *ancestor{FindModule(ancestorName)};
|
|
|
|
if (!ancestor) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *parentScope{parentName ? FindModule(*parentName, ancestor) : ancestor};
|
2018-08-02 16:21:27 -07:00
|
|
|
if (!parentScope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PushScope(*parentScope); // submodule is hosted in parent
|
2018-11-16 12:43:08 -08:00
|
|
|
BeginModule(name, true, subpPart);
|
|
|
|
if (!ancestor->AddSubmodule(name.source, currScope())) {
|
2018-08-02 16:21:27 -07:00
|
|
|
Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
ancestorName.source, name.source);
|
2018-08-02 16:21:27 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-23 17:13:59 -07:00
|
|
|
void ModuleVisitor::Post(const parser::Submodule &) { ClearScopes(); }
|
2018-08-02 16:21:27 -07:00
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Module &x) {
|
|
|
|
// Make a symbol and push a scope for this module
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{
|
2018-11-16 12:43:08 -08:00
|
|
|
std::get<parser::Statement<parser::ModuleStmt>>(x.t).statement.v};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
2018-11-05 14:36:11 -08:00
|
|
|
BeginModule(name, false, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
void ModuleVisitor::Post(const parser::Module &) {
|
2018-04-25 19:58:42 -07:00
|
|
|
ApplyDefaultAccess();
|
|
|
|
PopScope();
|
2018-05-17 13:06:38 -07:00
|
|
|
prevAccessStmt_ = nullptr;
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &ModuleVisitor::BeginModule(const parser::Name &name, bool isSubmodule,
|
2018-08-02 16:21:27 -07:00
|
|
|
const std::optional<parser::ModuleSubprogramPart> &subpPart) {
|
|
|
|
auto &symbol{MakeSymbol(name, ModuleDetails{isSubmodule})};
|
|
|
|
auto &details{symbol.get<ModuleDetails>()};
|
2018-08-22 16:56:57 -07:00
|
|
|
PushScope(Scope::Kind::Module, &symbol);
|
|
|
|
details.set_scope(&currScope());
|
2018-10-26 07:34:50 -07:00
|
|
|
WalkSubprogramPart(subpPart);
|
2018-08-02 16:21:27 -07:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a module or submodule by name and return its scope.
|
|
|
|
// If ancestor is present, look for a submodule of that ancestor module.
|
|
|
|
// May have to read a .mod file to find it.
|
|
|
|
// If an error occurs, report it and return nullptr.
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *ModuleVisitor::FindModule(const parser::Name &name, Scope *ancestor) {
|
2018-10-22 07:37:38 -07:00
|
|
|
ModFileReader reader{context()};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *scope{reader.Read(name.source, ancestor)};
|
2018-08-02 16:21:27 -07:00
|
|
|
if (!scope) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (scope->kind() != Scope::Kind::Module) {
|
|
|
|
Say(name, "'%s' is not a module"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Resolve(name, scope->symbol());
|
2018-08-02 16:21:27 -07:00
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
void ModuleVisitor::ApplyDefaultAccess() {
|
2018-08-22 16:56:57 -07:00
|
|
|
for (auto &pair : currScope()) {
|
2018-06-19 16:06:41 -07:00
|
|
|
Symbol &symbol = *pair.second;
|
2018-04-25 19:58:42 -07:00
|
|
|
if (!symbol.attrs().HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
symbol.attrs().set(defaultAccess_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
// InterfaceVistor implementation
|
|
|
|
|
|
|
|
bool InterfaceVisitor::Pre(const parser::InterfaceStmt &x) {
|
|
|
|
inInterfaceBlock_ = true;
|
|
|
|
isAbstract_ = std::holds_alternative<parser::Abstract>(x.u);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceVisitor::Post(const parser::EndInterfaceStmt &) {
|
2019-02-26 13:28:53 -08:00
|
|
|
genericName_ = nullptr;
|
2018-05-14 13:51:13 -07:00
|
|
|
inInterfaceBlock_ = false;
|
|
|
|
isAbstract_ = false;
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
// Create a symbol for the name in this GenericSpec, if any.
|
2018-05-14 13:51:13 -07:00
|
|
|
bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericName_ = GetGenericSpecName(x);
|
|
|
|
if (!genericName_) {
|
|
|
|
return false;
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *genericSymbol{FindSymbol(*genericName_)};
|
|
|
|
if (genericSymbol) {
|
|
|
|
if (genericSymbol->has<DerivedTypeDetails>()) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// A generic and derived type with same name: create a generic symbol
|
|
|
|
// and save derived type in it.
|
2018-11-16 12:43:08 -08:00
|
|
|
CHECK(genericSymbol->scope()->symbol() == genericSymbol);
|
2018-06-22 08:21:19 -07:00
|
|
|
GenericDetails details;
|
2018-11-16 12:43:08 -08:00
|
|
|
details.set_derivedType(*genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_);
|
|
|
|
genericSymbol->set_details(details);
|
|
|
|
} else if (!genericSymbol->IsSubprogram()) {
|
|
|
|
SayAlreadyDeclared(*genericName_, *genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = nullptr;
|
|
|
|
} else if (genericSymbol->has<UseDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// copy the USEd symbol into this scope so we can modify it
|
2018-11-16 12:43:08 -08:00
|
|
|
const Symbol &ultimate{genericSymbol->GetUltimate()};
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &CopySymbol(ultimate);
|
2018-12-06 06:59:37 -08:00
|
|
|
genericName_->symbol = genericSymbol;
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *details{ultimate.detailsIf<GenericDetails>()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericSymbol->set_details(GenericDetails{details->specificProcs()});
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{ultimate.detailsIf<SubprogramDetails>()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericSymbol->set_details(SubprogramDetails{*details});
|
2018-05-22 16:12:56 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
common::die("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (!genericSymbol) {
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_);
|
|
|
|
genericSymbol->set_details(GenericDetails{});
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (genericSymbol->has<GenericDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// okay
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (genericSymbol->has<SubprogramDetails>() ||
|
|
|
|
genericSymbol->has<SubprogramNameDetails>()) {
|
2018-06-19 16:06:41 -07:00
|
|
|
GenericDetails genericDetails;
|
2018-11-16 12:43:08 -08:00
|
|
|
genericDetails.set_specific(*genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_, genericDetails);
|
2018-12-06 06:59:37 -08:00
|
|
|
} else {
|
|
|
|
common::die("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
CHECK(genericName_->symbol == genericSymbol);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InterfaceVisitor::Pre(const parser::ProcedureStmt &x) {
|
|
|
|
if (!isGeneric()) {
|
|
|
|
Say("A PROCEDURE statement is only allowed in a generic interface block"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
auto kind{std::get<parser::ProcedureStmt::Kind>(x.t)};
|
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
AddSpecificProcs(names, kind);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceVisitor::Post(const parser::GenericStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericName_->symbol->attrs().set(AccessSpecToAttr(*accessSpec));
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
AddSpecificProcs(names, ProcedureKind::Procedure);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
GenericDetails &InterfaceVisitor::GetGenericDetails() {
|
|
|
|
CHECK(genericName_);
|
2018-12-06 06:59:37 -08:00
|
|
|
CHECK(genericName_->symbol);
|
2018-11-16 12:43:08 -08:00
|
|
|
return genericName_->symbol->get<GenericDetails>();
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2018-12-18 07:59:40 -08:00
|
|
|
void InterfaceVisitor::AddSpecificProcs(
|
|
|
|
const std::list<parser::Name> &names, ProcedureKind kind) {
|
|
|
|
for (const auto &name : names) {
|
|
|
|
specificProcs_.emplace(genericName_->symbol, std::make_pair(&name, kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:46:11 -07:00
|
|
|
// By now we should have seen all specific procedures referenced by name in
|
|
|
|
// this generic interface. Resolve those names to symbols.
|
|
|
|
void InterfaceVisitor::ResolveSpecificsInGeneric(Symbol &generic) {
|
|
|
|
auto &details{generic.get<GenericDetails>()};
|
|
|
|
std::set<SourceName> namesSeen; // to check for duplicate names
|
|
|
|
for (const auto *symbol : details.specificProcs()) {
|
|
|
|
namesSeen.insert(symbol->name());
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
auto range{specificProcs_.equal_range(&generic)};
|
|
|
|
for (auto it{range.first}; it != range.second; ++it) {
|
|
|
|
auto *name{it->second.first};
|
|
|
|
auto kind{it->second.second};
|
|
|
|
const auto *symbol{FindSymbol(*name)};
|
2018-07-20 10:46:11 -07:00
|
|
|
if (!symbol) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "Procedure '%s' not found"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
symbol = &symbol->GetUltimate();
|
2018-07-20 10:46:11 -07:00
|
|
|
if (symbol == &generic) {
|
|
|
|
if (auto *specific{generic.get<GenericDetails>().specific()}) {
|
|
|
|
symbol = specific;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol->has<SubprogramDetails>() &&
|
|
|
|
!symbol->has<SubprogramNameDetails>()) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "'%s' is not a subprogram"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
if (kind == ProcedureKind::ModuleProcedure) {
|
2018-07-20 10:46:11 -07:00
|
|
|
const auto *d{symbol->detailsIf<SubprogramNameDetails>()};
|
|
|
|
if (!d || d->kind() != SubprogramKind::Module) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "'%s' is not a module procedure"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
if (!namesSeen.insert(name->source).second) {
|
|
|
|
Say(*name,
|
|
|
|
"Procedure '%s' is already specified in generic '%s'"_err_en_US,
|
|
|
|
name->source, generic.name());
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-26 13:31:13 -08:00
|
|
|
details.add_specificProc(*symbol);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
specificProcs_.erase(range.first, range.second);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
|
|
|
|
2018-07-05 10:28:34 -07:00
|
|
|
// Check that the specific procedures are all functions or all subroutines.
|
|
|
|
// If there is a derived type with the same name they must be functions.
|
|
|
|
// Set the corresponding flag on generic.
|
|
|
|
void InterfaceVisitor::CheckGenericProcedures(Symbol &generic) {
|
2018-07-20 10:46:11 -07:00
|
|
|
ResolveSpecificsInGeneric(generic);
|
2018-07-16 16:23:18 -07:00
|
|
|
auto &details{generic.get<GenericDetails>()};
|
2019-02-26 13:28:53 -08:00
|
|
|
if (const auto *proc{details.CheckSpecific()}) {
|
|
|
|
SayAlreadyDeclared(generic.name(), *proc);
|
|
|
|
}
|
2018-07-05 10:28:34 -07:00
|
|
|
auto &specifics{details.specificProcs()};
|
|
|
|
if (specifics.empty()) {
|
|
|
|
if (details.derivedType()) {
|
|
|
|
generic.set(Symbol::Flag::Function);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &firstSpecific{*specifics.front()};
|
|
|
|
bool isFunction{firstSpecific.test(Symbol::Flag::Function)};
|
|
|
|
for (auto *specific : specifics) {
|
|
|
|
if (isFunction != specific->test(Symbol::Flag::Function)) {
|
|
|
|
auto &msg{Say(generic.name(),
|
|
|
|
"Generic interface '%s' has both a function and a subroutine"_err_en_US)};
|
|
|
|
if (isFunction) {
|
|
|
|
msg.Attach(firstSpecific.name(), "Function declaration"_en_US);
|
|
|
|
msg.Attach(specific->name(), "Subroutine declaration"_en_US);
|
|
|
|
} else {
|
|
|
|
msg.Attach(firstSpecific.name(), "Subroutine declaration"_en_US);
|
|
|
|
msg.Attach(specific->name(), "Function declaration"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isFunction && details.derivedType()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayDerivedType(generic.name(),
|
2018-07-05 10:28:34 -07:00
|
|
|
"Generic interface '%s' may only contain functions due to derived type"
|
|
|
|
" with same name"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*details.derivedType()->scope());
|
2018-07-05 10:28:34 -07:00
|
|
|
}
|
|
|
|
generic.set(isFunction ? Symbol::Flag::Function : Symbol::Flag::Subroutine);
|
|
|
|
}
|
|
|
|
|
2018-05-04 15:37:56 -07:00
|
|
|
// SubprogramVisitor implementation
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
void SubprogramVisitor::Post(const parser::StmtFunctionStmt &x) {
|
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
return; // This wasn't really a stmt function so no scope was created
|
|
|
|
}
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
// Return false if it is actually an assignment statement.
|
|
|
|
bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *resultType{nullptr};
|
2018-05-04 15:37:56 -07:00
|
|
|
// Look up name: provides return type or tells us if it's an array
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindSymbol(name)}) {
|
2018-10-10 16:20:46 -07:00
|
|
|
auto *details{symbol->detailsIf<EntityDetails>()};
|
|
|
|
if (!details) {
|
2018-06-05 12:18:35 -07:00
|
|
|
badStmtFuncFound_ = true;
|
2018-10-10 16:20:46 -07:00
|
|
|
return false;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
// TODO: check that attrs are compatible with stmt func
|
|
|
|
resultType = details->type();
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
Say(name, "'%s' has not been declared as an array"_err_en_US);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{PushSubprogramScope(name, Symbol::Flag::Function)};
|
|
|
|
auto &details{symbol.get<SubprogramDetails>()};
|
2018-05-04 15:37:56 -07:00
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
EntityDetails dummyDetails{true};
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *dummySymbol{FindInScope(currScope().parent(), dummyName)}) {
|
|
|
|
if (auto *d{dummySymbol->detailsIf<EntityDetails>()}) {
|
2018-05-04 15:37:56 -07:00
|
|
|
if (d->type()) {
|
|
|
|
dummyDetails.set_type(*d->type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 14:10:34 -07:00
|
|
|
details.add_dummyArg(MakeSymbol(dummyName, dummyDetails));
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name); // added by PushSubprogramScope
|
2018-05-04 15:37:56 -07:00
|
|
|
EntityDetails resultDetails;
|
|
|
|
if (resultType) {
|
|
|
|
resultDetails.set_type(*resultType);
|
|
|
|
}
|
|
|
|
details.set_result(MakeSymbol(name, resultDetails));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::Suffix &suffix) {
|
2018-05-17 13:06:38 -07:00
|
|
|
if (suffix.resultName) {
|
2019-02-24 10:05:43 -08:00
|
|
|
funcInfo_.resultName = &suffix.resultName.value();
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2019-02-24 10:05:43 -08:00
|
|
|
bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
|
|
|
|
// Save this to process after UseStmt and ImplicitPart
|
|
|
|
funcInfo_.parsedType = std::get_if<parser::DeclarationTypeSpec>(&x.u);
|
|
|
|
funcInfo_.source = currStmtSource();
|
|
|
|
return funcInfo_.parsedType == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::HandleFunctionPrefixType() {
|
|
|
|
if (funcInfo_.parsedType) {
|
|
|
|
messageHandler().set_currStmtSource(funcInfo_.source);
|
|
|
|
if (const auto *type{ProcessTypeSpec(*funcInfo_.parsedType)}) {
|
|
|
|
funcInfo_.resultSymbol->SetType(*type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
funcInfo_ = {};
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool HasModulePrefix(const std::list<parser::PrefixSpec> &prefixes) {
|
2018-10-26 11:57:08 -07:00
|
|
|
for (const auto &prefix : prefixes) {
|
2018-10-26 07:34:50 -07:00
|
|
|
if (std::holds_alternative<parser::PrefixSpec::Module>(prefix.u)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SubroutineSubprogram &x) {
|
2018-10-26 07:34:50 -07:00
|
|
|
const auto &stmt{
|
|
|
|
std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement};
|
|
|
|
bool hasModulePrefix{
|
|
|
|
HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmt.t))};
|
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Subroutine, hasModulePrefix, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::SubroutineSubprogram &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::FunctionSubprogram &x) {
|
2018-10-26 07:34:50 -07:00
|
|
|
const auto &stmt{
|
|
|
|
std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement};
|
|
|
|
bool hasModulePrefix{
|
|
|
|
HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmt.t))};
|
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Function, hasModulePrefix, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::FunctionSubprogram &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::InterfaceBody::Subroutine &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(
|
|
|
|
std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement.t)};
|
2018-12-26 13:31:13 -08:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Subroutine,
|
|
|
|
/*hasModulePrefix*/ false, std::nullopt);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::InterfaceBody::Subroutine &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::InterfaceBody::Function &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(
|
|
|
|
std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement.t)};
|
2018-10-26 11:57:08 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Function, /*hasModulePrefix*/ false, std::nullopt);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::InterfaceBody::Function &) {
|
|
|
|
EndSubprogram();
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:45:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SubroutineStmt &stmt) {
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-07-11 17:45:13 -07:00
|
|
|
}
|
2018-05-04 15:37:56 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::FunctionStmt &stmt) {
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::SubroutineStmt &stmt) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &details{PostSubprogramStmt(name)};
|
2018-05-04 15:37:56 -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");
|
|
|
|
Symbol &dummy{MakeSymbol(*dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::FunctionStmt &stmt) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &details{PostSubprogramStmt(name)};
|
2018-05-04 15:37:56 -07:00
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(stmt.t)) {
|
|
|
|
Symbol &dummy{MakeSymbol(dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
|
|
|
}
|
|
|
|
const parser::Name *funcResultName;
|
2019-02-24 10:05:43 -08:00
|
|
|
if (funcInfo_.resultName && funcInfo_.resultName->source != name.source) {
|
|
|
|
funcResultName = funcInfo_.resultName;
|
2018-05-04 15:37:56 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name); // was added by PushSubprogramScope
|
2018-05-14 13:51:13 -07:00
|
|
|
funcResultName = &name;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2019-02-24 10:05:43 -08:00
|
|
|
// add function result to function scope
|
|
|
|
EntityDetails funcResultDetails;
|
|
|
|
funcResultDetails.set_funcResult(true);
|
|
|
|
funcInfo_.resultSymbol = &MakeSymbol(*funcResultName, funcResultDetails);
|
|
|
|
details.set_result(*funcInfo_.resultSymbol);
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
SubprogramDetails &SubprogramVisitor::PostSubprogramStmt(
|
|
|
|
const parser::Name &name) {
|
|
|
|
Symbol &symbol{*currScope().symbol()};
|
|
|
|
CHECK(name.source == symbol.name());
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-10-26 07:34:50 -07:00
|
|
|
symbol.attrs() |= EndAttrs();
|
|
|
|
if (symbol.attrs().test(Attr::MODULE)) {
|
|
|
|
symbol.attrs().set(Attr::EXTERNAL, false);
|
|
|
|
}
|
|
|
|
return symbol.get<SubprogramDetails>();
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
|
2018-10-26 07:34:50 -07:00
|
|
|
Symbol::Flag subpFlag, bool hasModulePrefix,
|
2018-05-14 13:51:13 -07:00
|
|
|
const std::optional<parser::InternalSubprogramPart> &subpPart) {
|
|
|
|
if (subpNamesOnly_) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{MakeSymbol(name, SubprogramNameDetails{*subpNamesOnly_})};
|
2018-06-05 12:18:35 -07:00
|
|
|
symbol.set(subpFlag);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-26 07:34:50 -07:00
|
|
|
if (hasModulePrefix && !inInterfaceBlock()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindSymbol(name)};
|
2018-10-26 11:57:08 -07:00
|
|
|
if (!symbol || !symbol->IsSeparateModuleProc()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name, "'%s' was not declared a separate module procedure"_err_en_US);
|
2018-10-26 07:34:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (symbol->owner() == currScope()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
// separate module procedure declared and defined in same module
|
|
|
|
PushScope(*symbol->scope());
|
2018-10-26 07:34:50 -07:00
|
|
|
} else {
|
|
|
|
PushSubprogramScope(name, subpFlag);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PushSubprogramScope(name, subpFlag);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-10-26 07:34:50 -07:00
|
|
|
WalkSubprogramPart(subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void SubprogramVisitor::EndSubprogram() {
|
|
|
|
if (!subpNamesOnly_) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SeparateModuleSubprogram &x) {
|
|
|
|
if (subpNamesOnly_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto &name{
|
|
|
|
std::get<parser::Statement<parser::MpSubprogramStmt>>(x.t).statement.v};
|
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 11:57:08 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Subroutine, /*hasModulePrefix*/ true, subpPart);
|
2018-10-26 07:34:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::SeparateModuleSubprogram &) {
|
|
|
|
EndSubprogram();
|
|
|
|
}
|
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &SubprogramVisitor::PushSubprogramScope(
|
|
|
|
const parser::Name &name, Symbol::Flag subpFlag) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{GetSpecificFromGeneric(name)};
|
2018-05-22 16:12:56 -07:00
|
|
|
if (!symbol) {
|
2019-02-22 11:38:43 -08:00
|
|
|
if (auto *prev{FindSymbol(name)}) {
|
|
|
|
if (prev->attrs().test(Attr::EXTERNAL) &&
|
|
|
|
prev->has<ProcEntityDetails>()) {
|
|
|
|
// this subprogram was previously called, now being declared
|
|
|
|
if (!prev->test(subpFlag)) {
|
|
|
|
Say2(name,
|
|
|
|
subpFlag == Symbol::Flag::Function
|
|
|
|
? "'%s' was previously called as a subroutine"_err_en_US
|
|
|
|
: "'%s' was previously called as a function"_err_en_US,
|
|
|
|
*prev, "Previous call of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
EraseSymbol(name);
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 16:12:56 -07:00
|
|
|
symbol = &MakeSymbol(name, SubprogramDetails{});
|
2018-06-05 12:18:35 -07:00
|
|
|
symbol->set(subpFlag);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
PushScope(Scope::Kind::Subprogram, symbol);
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &details{symbol->get<SubprogramDetails>()};
|
2018-05-14 13:51:13 -07:00
|
|
|
if (inInterfaceBlock()) {
|
|
|
|
details.set_isInterface();
|
|
|
|
if (!isAbstract()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
if (isGeneric()) {
|
2018-12-26 13:31:13 -08:00
|
|
|
GetGenericDetails().add_specificProc(*symbol);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
implicitRules().set_inheritFromParent(false);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
FindSymbol(name)->set(subpFlag);
|
2018-05-22 16:12:56 -07:00
|
|
|
return *symbol;
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// If name is a generic, return specific subprogram with the same name.
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *SubprogramVisitor::GetSpecificFromGeneric(const parser::Name &name) {
|
2018-08-28 14:02:53 -07:00
|
|
|
if (auto *symbol{FindSymbol(name)}) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol->detailsIf<GenericDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// found generic, want subprogram
|
2018-07-17 07:02:30 -07:00
|
|
|
auto *specific{details->specific()};
|
2018-05-22 16:12:56 -07:00
|
|
|
if (isGeneric()) {
|
|
|
|
if (specific) {
|
2018-06-22 08:21:19 -07:00
|
|
|
SayAlreadyDeclared(name, *specific);
|
2018-05-22 16:12:56 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
|
|
|
specific = &MakeSymbol(name, Attrs{}, SubprogramDetails{});
|
|
|
|
GetGenericDetails().set_specific(*specific);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (specific) {
|
|
|
|
if (!specific->has<SubprogramDetails>()) {
|
|
|
|
specific->set_details(SubprogramDetails{});
|
|
|
|
}
|
|
|
|
return specific;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
// DeclarationVisitor implementation
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::BeginDecl() {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginDeclTypeSpec();
|
|
|
|
BeginArraySpec();
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::EndDecl() {
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
|
|
|
EndArraySpec();
|
2018-08-31 15:15:28 -07:00
|
|
|
EndAttrs();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
bool DeclarationVisitor::CheckUseError(const parser::Name &name) {
|
|
|
|
const auto *details{name.symbol->detailsIf<UseErrorDetails>()};
|
2018-08-29 11:38:12 -07:00
|
|
|
if (!details) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Message &msg{Say(name, "Reference to '%s' is ambiguous"_err_en_US)};
|
2018-11-16 12:43:08 -08:00
|
|
|
for (const auto &[location, module] : details->occurrences()) {
|
2018-08-29 11:38:12 -07:00
|
|
|
msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
name.ToString().data(), module->name().ToString().data());
|
2018-08-29 11:38:12 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
// Report error if accessibility of symbol doesn't match isPrivate.
|
|
|
|
void DeclarationVisitor::CheckAccessibility(
|
|
|
|
const parser::Name &name, bool isPrivate, const Symbol &symbol) {
|
|
|
|
if (symbol.attrs().test(Attr::PRIVATE) != isPrivate) {
|
|
|
|
Say2(name,
|
|
|
|
"'%s' does not have the same accessibility as its previous declaration"_err_en_US,
|
|
|
|
symbol, "Previous declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 17:24:43 -08:00
|
|
|
// Check that component is accessible from current scope.
|
|
|
|
bool DeclarationVisitor::CheckAccessibleComponent(
|
|
|
|
const SourceName &name, const Symbol &symbol) {
|
|
|
|
if (!symbol.attrs().test(Attr::PRIVATE)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// component must be in a module/submodule because of PRIVATE:
|
|
|
|
const Scope *moduleScope{&symbol.owner()};
|
|
|
|
CHECK(moduleScope->kind() == Scope::Kind::DerivedType);
|
|
|
|
while (moduleScope->kind() != Scope::Kind::Module &&
|
|
|
|
moduleScope->kind() != Scope::Kind::Global) {
|
|
|
|
moduleScope = &moduleScope->parent();
|
|
|
|
}
|
|
|
|
if (moduleScope->kind() == Scope::Kind::Module) {
|
|
|
|
for (auto *scope{&currScope()}; scope->kind() != Scope::Kind::Global;
|
|
|
|
scope = &scope->parent()) {
|
|
|
|
if (scope == moduleScope) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Say(name,
|
|
|
|
"PRIVATE component '%s' is only accessible within module '%s'"_err_en_US,
|
|
|
|
name.ToString(), moduleScope->name());
|
|
|
|
} else {
|
|
|
|
Say(name,
|
|
|
|
"PRIVATE component '%s' is only accessible within its module"_err_en_US,
|
|
|
|
name.ToString());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-29 14:31:47 -08:00
|
|
|
void DeclarationVisitor::CheckScalarIntegerType(const parser::Name &name) {
|
|
|
|
if (name.symbol != nullptr) {
|
|
|
|
const Symbol &symbol{*name.symbol};
|
2019-02-07 12:25:59 -08:00
|
|
|
if (symbol.IsObjectArray()) {
|
2019-02-06 10:28:31 -08:00
|
|
|
Say(name, "Variable '%s' is not scalar"_err_en_US);
|
|
|
|
return;
|
2019-01-29 14:31:47 -08:00
|
|
|
}
|
|
|
|
if (auto *type{symbol.GetType()}) {
|
|
|
|
if (!type->IsNumeric(TypeCategory::Integer)) {
|
|
|
|
Say(name, "Variable '%s' is not integer"_err_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::DimensionStmt::Declaration &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
DeclareObjectEntity(name, Attrs{});
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::EntityDecl &x) {
|
|
|
|
// TODO: may be under StructureStmt
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t)};
|
2018-05-30 14:11:45 -07:00
|
|
|
// TODO: CoarraySpec, CharLength, Initialization
|
2019-02-20 17:45:39 -08:00
|
|
|
Attrs attrs{attrs_ ? HandleSaveName(name.source, *attrs_) : Attrs{}};
|
2018-11-06 17:18:06 -08:00
|
|
|
Symbol &symbol{DeclareUnknownEntity(name, attrs)};
|
|
|
|
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
|
|
|
|
if (ConvertToObjectEntity(symbol)) {
|
2018-12-06 06:59:37 -08:00
|
|
|
if (auto *expr{std::get_if<parser::ConstantExpr>(&init->u)}) {
|
|
|
|
symbol.get<ObjectEntityDetails>().set_init(EvaluateExpr(*expr));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 16:03:23 -08:00
|
|
|
} else if (attrs.test(Attr::PARAMETER)) {
|
|
|
|
Say(name, "Missing initialization for parameter '%s'"_err_en_US);
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::PointerDecl &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-10-10 16:20:46 -07:00
|
|
|
DeclareUnknownEntity(name, Attrs{Attr::POINTER});
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-09-10 12:20:42 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::BindEntity &x) {
|
2019-02-18 11:39:46 -08:00
|
|
|
auto kind{std::get<parser::BindEntity::Kind>(x.t)};
|
2018-09-10 12:20:42 -07:00
|
|
|
auto &name{std::get<parser::Name>(x.t)};
|
2019-02-18 11:39:46 -08:00
|
|
|
Symbol *symbol;
|
|
|
|
if (kind == parser::BindEntity::Kind::Object) {
|
|
|
|
symbol = &HandleAttributeStmt(Attr::BIND_C, name);
|
2018-09-10 12:20:42 -07:00
|
|
|
} else {
|
2019-02-18 11:39:46 -08:00
|
|
|
symbol = &MakeCommonBlockSymbol(name);
|
|
|
|
symbol->attrs().set(Attr::BIND_C);
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
2019-02-18 11:39:46 -08:00
|
|
|
SetBindNameOn(*symbol);
|
2018-09-10 12:20:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-06 17:18:02 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::NamedConstantDef &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::NamedConstant>(x.t).v};
|
2018-09-10 12:20:42 -07:00
|
|
|
auto &symbol{HandleAttributeStmt(Attr::PARAMETER, name)};
|
2018-11-06 17:18:06 -08:00
|
|
|
if (!ConvertToObjectEntity(symbol)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "PARAMETER attribute not allowed on '%s'"_err_en_US);
|
2019-02-06 17:18:02 -08:00
|
|
|
return false;
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
const auto &expr{std::get<parser::ConstantExpr>(x.t)};
|
2019-02-06 17:18:02 -08:00
|
|
|
Walk(expr);
|
2018-12-06 06:59:37 -08:00
|
|
|
symbol.get<ObjectEntityDetails>().set_init(EvaluateExpr(expr));
|
2018-09-22 08:05:46 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
2019-02-06 17:18:02 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::NamedConstant &x) {
|
|
|
|
const parser::Name &name{x.v};
|
|
|
|
if (!FindSymbol(name)) {
|
|
|
|
Say(name, "Named constant '%s' not found"_err_en_US);
|
|
|
|
} else {
|
|
|
|
CheckUseError(name);
|
|
|
|
}
|
|
|
|
return false;
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::AsynchronousStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::ASYNCHRONOUS, x.v);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ContiguousStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::CONTIGUOUS, x.v);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ExternalStmt &x) {
|
2018-06-05 12:18:35 -07:00
|
|
|
HandleAttributeStmt(Attr::EXTERNAL, x.v);
|
|
|
|
for (const auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindSymbol(name)};
|
2018-07-19 13:28:24 -07:00
|
|
|
if (!ConvertToProcEntity(*symbol)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, *symbol, "EXTERNAL attribute not allowed on '%s'"_err_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-07-11 17:45:13 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::IntentStmt &x) {
|
|
|
|
auto &intentSpec{std::get<parser::IntentSpec>(x.t)};
|
|
|
|
auto &names{std::get<std::list<parser::Name>>(x.t)};
|
2019-02-18 11:39:46 -08:00
|
|
|
return CheckNotInBlock("INTENT") &&
|
|
|
|
HandleAttributeStmt(IntentSpecToAttr(intentSpec), names);
|
2018-07-11 17:45:13 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::IntrinsicStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::INTRINSIC, x.v);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::OptionalStmt &x) {
|
2019-02-18 11:39:46 -08:00
|
|
|
return CheckNotInBlock("OPTIONAL") &&
|
|
|
|
HandleAttributeStmt(Attr::OPTIONAL, x.v);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ProtectedStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::PROTECTED, x.v);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ValueStmt &x) {
|
2019-02-18 11:39:46 -08:00
|
|
|
return CheckNotInBlock("VALUE") && HandleAttributeStmt(Attr::VALUE, x.v);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::VolatileStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::VOLATILE, x.v);
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
// Handle a statement that sets an attribute on a list of names.
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::HandleAttributeStmt(
|
2018-04-11 13:11:42 -07:00
|
|
|
Attr attr, const std::list<parser::Name> &names) {
|
|
|
|
for (const auto &name : names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
HandleAttributeStmt(attr, name);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-10 12:20:42 -07:00
|
|
|
Symbol &DeclarationVisitor::HandleAttributeStmt(
|
2018-11-16 12:43:08 -08:00
|
|
|
Attr attr, const parser::Name &name) {
|
2019-02-26 14:26:28 -08:00
|
|
|
if (attr == Attr::INTRINSIC &&
|
|
|
|
!context().intrinsics().IsIntrinsic(name.source.ToString())) {
|
|
|
|
Say(name.source, "'%s' is not a known intrinsic procedure"_err_en_US);
|
|
|
|
}
|
2019-02-20 17:45:39 -08:00
|
|
|
auto *symbol{FindInScope(currScope(), name)};
|
2019-01-04 17:10:31 -08:00
|
|
|
if (symbol) {
|
2018-09-10 12:20:42 -07:00
|
|
|
// symbol was already there: set attribute on it
|
|
|
|
if (attr == Attr::ASYNCHRONOUS || attr == Attr::VOLATILE) {
|
|
|
|
// TODO: if in a BLOCK, attribute should only be set while in the block
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (symbol->has<UseDetails>()) {
|
2018-09-10 12:20:42 -07:00
|
|
|
Say(*currStmtSource(),
|
|
|
|
"Cannot change %s attribute on use-associated '%s'"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
EnumToString(attr), name.source);
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
} else {
|
2019-01-04 17:10:31 -08:00
|
|
|
symbol = &MakeSymbol(name, EntityDetails{});
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
2019-02-15 10:16:25 -08:00
|
|
|
symbol->attrs().set(attr);
|
2019-02-20 17:45:39 -08:00
|
|
|
symbol->attrs() = HandleSaveName(name.source, symbol->attrs());
|
2019-01-04 17:10:31 -08:00
|
|
|
return *symbol;
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:39:46 -08:00
|
|
|
bool DeclarationVisitor::CheckNotInBlock(const char *stmt) {
|
|
|
|
if (currScope().kind() == Scope::Kind::Block) {
|
|
|
|
Say(MessageFormattedText{
|
|
|
|
"%s statement is not allowed in a BLOCK construct"_err_en_US, stmt});
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::ObjectDecl &x) {
|
2018-04-12 12:59:42 -07:00
|
|
|
CHECK(objectDeclAttr_.has_value());
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
DeclareObjectEntity(name, Attrs{*objectDeclAttr_});
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
// Declare an entity not yet known to be an object or proc.
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareUnknownEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs) {
|
2018-10-10 16:20:46 -07:00
|
|
|
if (!arraySpec().empty()) {
|
2018-10-18 07:55:48 -07:00
|
|
|
return DeclareObjectEntity(name, attrs);
|
2018-10-10 16:20:46 -07:00
|
|
|
} else {
|
|
|
|
Symbol &symbol{DeclareEntity<EntityDetails>(name, attrs)};
|
2018-12-11 14:51:08 -08:00
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SetType(name, *type);
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-10-10 16:20:46 -07:00
|
|
|
if (symbol.attrs().test(Attr::EXTERNAL)) {
|
|
|
|
ConvertToProcEntity(symbol);
|
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareProcEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs, const ProcInterface &interface) {
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &symbol{DeclareEntity<ProcEntityDetails>(name, attrs)};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
2018-06-05 12:18:35 -07:00
|
|
|
if (interface.type()) {
|
|
|
|
symbol.set(Symbol::Flag::Function);
|
|
|
|
} else if (interface.symbol()) {
|
|
|
|
symbol.set(interface.symbol()->test(Symbol::Flag::Function)
|
|
|
|
? Symbol::Flag::Function
|
|
|
|
: Symbol::Flag::Subroutine);
|
|
|
|
}
|
2018-07-28 14:10:34 -07:00
|
|
|
details->set_interface(interface);
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
|
|
|
SetPassNameOn(symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareObjectEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs) {
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, attrs)};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
2018-12-11 14:51:08 -08:00
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SetType(name, *type);
|
2018-04-12 12:59:42 -07:00
|
|
|
}
|
|
|
|
if (!arraySpec().empty()) {
|
2019-02-06 10:28:31 -08:00
|
|
|
if (details->IsArray()) {
|
2018-04-12 12:59:42 -07:00
|
|
|
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
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::IntegerTypeSpec &x) {
|
|
|
|
SetDeclTypeSpec(MakeNumericType(TypeCategory::Integer, x.v));
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Real &x) {
|
|
|
|
SetDeclTypeSpec(MakeNumericType(TypeCategory::Real, x.kind));
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Complex &x) {
|
|
|
|
SetDeclTypeSpec(MakeNumericType(TypeCategory::Complex, x.kind));
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Logical &x) {
|
|
|
|
SetDeclTypeSpec(MakeLogicalType(x.kind));
|
|
|
|
}
|
2018-12-14 14:04:15 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Character &x) {
|
|
|
|
if (!charInfo_.length) {
|
2018-12-17 15:19:11 -08:00
|
|
|
charInfo_.length = ParamValue{1};
|
2018-12-14 14:04:15 -08:00
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
if (!charInfo_.kind.has_value()) {
|
|
|
|
charInfo_.kind = KindExpr{
|
|
|
|
context().defaultKinds().GetDefaultKind(TypeCategory::Character)};
|
2018-12-17 15:46:30 -08:00
|
|
|
}
|
2018-12-17 12:41:43 -08:00
|
|
|
SetDeclTypeSpec(currScope().MakeCharacterType(
|
2018-12-04 10:55:32 -08:00
|
|
|
std::move(*charInfo_.length), std::move(*charInfo_.kind)));
|
2018-12-14 14:04:15 -08:00
|
|
|
charInfo_ = {};
|
|
|
|
}
|
2019-02-21 17:05:46 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::NCharacter &x) {
|
|
|
|
if (!charInfo_.length) {
|
|
|
|
charInfo_.length = ParamValue{1};
|
|
|
|
}
|
|
|
|
CHECK(!charInfo_.kind.has_value());
|
|
|
|
SetDeclTypeSpec(currScope().MakeCharacterType(
|
|
|
|
std::move(*charInfo_.length), KindExpr{2 /* EUC_JP */}));
|
|
|
|
charInfo_ = {};
|
|
|
|
}
|
2018-12-14 14:04:15 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::CharSelector::LengthAndKind &x) {
|
2018-12-04 10:55:32 -08:00
|
|
|
charInfo_.kind = EvaluateSubscriptIntExpr(x.kind);
|
2018-12-14 14:04:15 -08:00
|
|
|
if (x.length) {
|
|
|
|
charInfo_.length = GetParamValue(*x.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::CharLength &x) {
|
|
|
|
if (const auto *length{std::get_if<std::int64_t>(&x.u)}) {
|
2018-12-17 15:19:11 -08:00
|
|
|
charInfo_.length = ParamValue{*length};
|
2019-01-07 15:05:53 -08:00
|
|
|
} else {
|
|
|
|
charInfo_.length = GetParamValue(std::get<parser::TypeParamValue>(x.u));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::LengthSelector &x) {
|
|
|
|
if (const auto *param{std::get_if<parser::TypeParamValue>(&x.u)}) {
|
|
|
|
charInfo_.length = GetParamValue(*param);
|
2018-12-14 14:04:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:03:23 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::KindParam &x) {
|
|
|
|
if (const auto *kind{std::get_if<
|
|
|
|
parser::Scalar<parser::Integer<parser::Constant<parser::Name>>>>(
|
|
|
|
&x.u)}) {
|
|
|
|
const parser::Name &name{kind->thing.thing.thing};
|
|
|
|
if (!FindSymbol(name)) {
|
|
|
|
Say(name, "Parameter '%s' not found"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Type &x) {
|
|
|
|
CHECK(GetDeclTypeSpecCategory() == DeclTypeSpec::Category::TypeDerived);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Class &x) {
|
|
|
|
SetDeclTypeSpecCategory(DeclTypeSpec::Category::ClassDerived);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Record &) {
|
2018-12-04 10:55:32 -08:00
|
|
|
// TODO
|
|
|
|
return true;
|
2018-08-31 16:20:00 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::DerivedTypeSpec &x) {
|
2019-01-07 15:05:53 -08:00
|
|
|
const auto &typeName{std::get<parser::Name>(x.t)};
|
2019-01-17 13:52:10 -08:00
|
|
|
const Symbol *typeSymbol{ResolveDerivedType(typeName)};
|
2018-12-04 10:55:32 -08:00
|
|
|
if (typeSymbol == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This DerivedTypeSpec is created initially as a search key.
|
|
|
|
// If it turns out to have the same name and actual parameter
|
|
|
|
// value expressions as some other DerivedTypeSpec in the current
|
|
|
|
// scope, then we'll use that extant spec; otherwise, when this
|
|
|
|
// spec is distinct from all derived types previously instantiated
|
|
|
|
// in the current scope, this spec will be moved to that collection.
|
|
|
|
DerivedTypeSpec spec{*typeSymbol};
|
|
|
|
|
|
|
|
// The expressions in a derived type specifier whose values define
|
|
|
|
// non-defaulted type parameters are evaluated in the enclosing scope.
|
|
|
|
// Default initialization expressions for the derived type's parameters
|
|
|
|
// may reference other parameters so long as the declaration precedes the
|
|
|
|
// use in the expression (10.1.12). This is not necessarily the same
|
|
|
|
// order as "type parameter order" (7.5.3.2).
|
|
|
|
// Parameters of the most deeply nested "base class" come first when the
|
|
|
|
// derived type is an extension.
|
|
|
|
const DerivedTypeDetails &typeDetails{typeSymbol->get<DerivedTypeDetails>()};
|
|
|
|
auto parameterNames{typeDetails.OrderParameterNames(*typeSymbol)};
|
2019-02-08 08:57:28 -08:00
|
|
|
auto parameterDecls{typeDetails.OrderParameterDeclarations(*typeSymbol)};
|
2018-12-04 10:55:32 -08:00
|
|
|
auto nextNameIter{parameterNames.begin()};
|
|
|
|
bool seenAnyName{false};
|
|
|
|
for (const auto &typeParamSpec :
|
|
|
|
std::get<std::list<parser::TypeParamSpec>>(x.t)) {
|
|
|
|
const auto &optKeyword{
|
|
|
|
std::get<std::optional<parser::Keyword>>(typeParamSpec.t)};
|
|
|
|
SourceName name;
|
|
|
|
if (optKeyword.has_value()) {
|
|
|
|
seenAnyName = true;
|
|
|
|
name = optKeyword->v.source;
|
2019-02-08 08:57:28 -08:00
|
|
|
auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
|
2019-02-13 12:02:42 -08:00
|
|
|
[&](const Symbol *symbol) { return symbol->name() == name; })};
|
2019-02-08 08:57:28 -08:00
|
|
|
if (it == parameterDecls.end()) {
|
2018-12-04 10:55:32 -08:00
|
|
|
Say(name,
|
|
|
|
"'%s' is not the name of a parameter for this type"_err_en_US);
|
2019-02-08 08:57:28 -08:00
|
|
|
} else {
|
2019-02-13 12:02:42 -08:00
|
|
|
Resolve(optKeyword->v, const_cast<Symbol *>(*it));
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
} else if (seenAnyName) {
|
|
|
|
Say(typeName.source, "Type parameter value must have a name"_err_en_US);
|
|
|
|
continue;
|
|
|
|
} else if (nextNameIter != parameterNames.end()) {
|
|
|
|
name = *nextNameIter++;
|
|
|
|
} else {
|
|
|
|
Say(typeName.source,
|
|
|
|
"Too many type parameters given for derived type '%s'"_err_en_US);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (spec.FindParameter(name)) {
|
|
|
|
Say(typeName.source,
|
|
|
|
"Multiple values given for type parameter '%s'"_err_en_US, name);
|
|
|
|
} else {
|
|
|
|
const auto &value{std::get<parser::TypeParamValue>(typeParamSpec.t)};
|
|
|
|
ParamValue param{GetParamValue(value)}; // folded
|
|
|
|
if (!param.isExplicit() || param.GetExplicit().has_value()) {
|
|
|
|
spec.AddParamValue(name, std::move(param));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that any type parameter without an explicit value has a
|
|
|
|
// default initialization in the derived type's definition.
|
|
|
|
const Scope *typeScope{typeSymbol->scope()};
|
|
|
|
CHECK(typeScope != nullptr);
|
|
|
|
for (const SourceName &name : parameterNames) {
|
|
|
|
if (!spec.FindParameter(name)) {
|
2019-02-08 08:57:28 -08:00
|
|
|
auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
|
2019-02-13 12:02:42 -08:00
|
|
|
[&](const Symbol *symbol) { return symbol->name() == name; })};
|
2019-02-08 08:57:28 -08:00
|
|
|
CHECK(it != parameterDecls.end());
|
|
|
|
auto &symbol{**it};
|
|
|
|
const auto *details{symbol.detailsIf<TypeParamDetails>()};
|
2018-12-04 10:55:32 -08:00
|
|
|
if (details == nullptr || !details->init().has_value()) {
|
|
|
|
Say(typeName.source,
|
|
|
|
"Type parameter '%s' lacks a value and has no default"_err_en_US,
|
2019-02-08 08:57:28 -08:00
|
|
|
symbol.name());
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto category{GetDeclTypeSpecCategory()};
|
|
|
|
if (const DeclTypeSpec *
|
|
|
|
extant{currScope().FindInstantiatedDerivedType(spec, category)}) {
|
|
|
|
// This derived type and parameter expressions (if any) are already present
|
|
|
|
// in this scope.
|
|
|
|
SetDeclTypeSpec(*extant);
|
|
|
|
} else {
|
|
|
|
DeclTypeSpec &type{currScope().MakeDerivedType(std::move(spec), category)};
|
|
|
|
if (parameterNames.empty() || currScope().IsParameterizedDerivedType()) {
|
|
|
|
// The derived type being instantiated is not a parameterized derived
|
|
|
|
// type, or the instantiation is within the definition of a parameterized
|
|
|
|
// derived type; don't instantiate a new scope.
|
|
|
|
type.derivedTypeSpec().set_scope(*typeScope);
|
|
|
|
} else {
|
|
|
|
// This is a parameterized derived type and this spec is not in the
|
|
|
|
// context of a parameterized derived type definition, so we need to
|
|
|
|
// clone its contents, specialize them with the actual type parameter
|
|
|
|
// values, and check constraints.
|
2019-01-31 16:04:17 -08:00
|
|
|
auto save{GetFoldingContext().messages().SetLocation(*currStmtSource())};
|
2019-01-31 09:58:40 -08:00
|
|
|
type.derivedTypeSpec().Instantiate(currScope(), context());
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
SetDeclTypeSpec(type);
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
2019-02-08 16:35:02 -08:00
|
|
|
// Capture the DerivedTypeSpec in the parse tree for use in building
|
|
|
|
// structure constructor expressions.
|
|
|
|
x.derivedTypeSpec = &GetDeclTypeSpec()->derivedTypeSpec();
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
|
|
|
|
2019-02-15 16:08:32 -08:00
|
|
|
// The descendents of DerivedTypeDef in the parse tree are visited directly
|
|
|
|
// in this Pre() routine so that recursive use of the derived type can be
|
|
|
|
// supported in the components.
|
|
|
|
bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
|
2019-02-19 10:08:10 -08:00
|
|
|
auto &stmt{std::get<parser::Statement<parser::DerivedTypeStmt>>(x.t)};
|
|
|
|
Walk(stmt);
|
2019-02-15 16:08:32 -08:00
|
|
|
Walk(std::get<std::list<parser::Statement<parser::TypeParamDefStmt>>>(x.t));
|
2018-09-04 10:28:27 -07:00
|
|
|
auto &scope{currScope()};
|
2018-12-04 10:55:32 -08:00
|
|
|
CHECK(scope.symbol() != nullptr);
|
|
|
|
CHECK(scope.symbol()->scope() == &scope);
|
2018-12-06 17:52:43 -08:00
|
|
|
auto &details{scope.symbol()->get<DerivedTypeDetails>()};
|
2019-02-15 16:08:32 -08:00
|
|
|
std::set<SourceName> paramNames;
|
2018-11-16 12:43:08 -08:00
|
|
|
for (auto ¶mName : std::get<std::list<parser::Name>>(stmt.statement.t)) {
|
2018-12-06 17:52:43 -08:00
|
|
|
details.add_paramName(paramName.source);
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindInScope(scope, paramName)};
|
|
|
|
if (!symbol) {
|
2018-09-04 10:28:27 -07:00
|
|
|
Say(paramName,
|
|
|
|
"No definition found for type parameter '%s'"_err_en_US); // C742
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (!symbol->has<TypeParamDetails>()) {
|
|
|
|
Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
|
|
|
|
*symbol, "Definition of '%s'"_en_US); // C741
|
2018-09-04 10:28:27 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (!paramNames.insert(paramName.source).second) {
|
2018-09-04 10:28:27 -07:00
|
|
|
Say(paramName,
|
|
|
|
"Duplicate type parameter name: '%s'"_err_en_US); // C731
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
for (const auto &[name, symbol] : currScope()) {
|
|
|
|
if (symbol->has<TypeParamDetails>() && !paramNames.count(name)) {
|
|
|
|
SayDerivedType(name,
|
2018-09-04 10:28:27 -07:00
|
|
|
"'%s' is not a type parameter of this derived type"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
currScope()); // C742
|
2018-09-04 10:28:27 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 16:08:32 -08:00
|
|
|
Walk(std::get<std::list<parser::Statement<parser::PrivateOrSequence>>>(x.t));
|
2018-09-06 08:01:49 -07:00
|
|
|
if (derivedTypeInfo_.sequence) {
|
|
|
|
details.set_sequence(true);
|
|
|
|
if (derivedTypeInfo_.extends) {
|
|
|
|
Say(stmt.source,
|
|
|
|
"A sequence type may not have the EXTENDS attribute"_err_en_US); // C735
|
|
|
|
}
|
2018-12-06 17:52:43 -08:00
|
|
|
if (!details.paramNames().empty()) {
|
2018-09-06 08:01:49 -07:00
|
|
|
Say(stmt.source,
|
|
|
|
"A sequence type may not have type parameters"_err_en_US); // C740
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 16:08:32 -08:00
|
|
|
Walk(std::get<std::list<parser::Statement<parser::ComponentDefStmt>>>(x.t));
|
|
|
|
Walk(std::get<std::optional<parser::TypeBoundProcedurePart>>(x.t));
|
|
|
|
Walk(std::get<parser::Statement<parser::EndTypeStmt>>(x.t));
|
2018-09-06 08:01:49 -07:00
|
|
|
derivedTypeInfo_ = {};
|
2018-09-04 10:28:27 -07:00
|
|
|
PopScope();
|
2019-02-15 16:08:32 -08:00
|
|
|
return false;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::DerivedTypeStmt &x) {
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
|
2019-02-19 10:08:10 -08:00
|
|
|
auto &name{std::get<parser::Name>(x.t)};
|
2019-02-15 16:21:43 -08:00
|
|
|
// Resolve the EXTENDS() clause before creating the derived
|
|
|
|
// type's symbol to foil attempts to recursively extend a type.
|
|
|
|
auto *extendsName{derivedTypeInfo_.extends};
|
|
|
|
const Symbol *extendsType{nullptr};
|
|
|
|
if (extendsName != nullptr) {
|
2019-02-19 10:08:10 -08:00
|
|
|
if (extendsName->source == name.source) {
|
|
|
|
Say(extendsName->source,
|
|
|
|
"Derived type '%s' cannot extend itself"_err_en_US);
|
|
|
|
} else {
|
|
|
|
extendsType = ResolveDerivedType(*extendsName);
|
|
|
|
}
|
2019-02-15 16:21:43 -08:00
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{MakeSymbol(name, GetAttrs(), DerivedTypeDetails{})};
|
2019-02-15 16:08:32 -08:00
|
|
|
derivedTypeInfo_.type = &symbol;
|
2018-06-22 08:21:19 -07:00
|
|
|
PushScope(Scope::Kind::DerivedType, &symbol);
|
2019-02-15 16:21:43 -08:00
|
|
|
if (extendsType != nullptr) {
|
|
|
|
// Declare the "parent component"; private if the type is
|
|
|
|
// Any symbol stored in the EXTENDS() clause is temporarily
|
|
|
|
// hidden so that a new symbol can be created for the parent
|
|
|
|
// component without producing spurious errors about already
|
|
|
|
// existing.
|
|
|
|
auto restorer{common::ScopedSet(extendsName->symbol, nullptr)};
|
|
|
|
if (OkToAddComponent(*extendsName, extendsType)) {
|
|
|
|
auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
|
|
|
|
comp.attrs().set(Attr::PRIVATE, extendsType->attrs().test(Attr::PRIVATE));
|
|
|
|
comp.set(Symbol::Flag::ParentComp);
|
|
|
|
DeclTypeSpec &type{currScope().MakeDerivedType(*extendsType)};
|
|
|
|
type.derivedTypeSpec().set_scope(*extendsType->scope());
|
|
|
|
comp.SetType(type);
|
|
|
|
DerivedTypeDetails &details{symbol.get<DerivedTypeDetails>()};
|
|
|
|
details.add_component(comp);
|
2018-09-06 08:01:49 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
EndAttrs();
|
|
|
|
}
|
2018-09-04 10:28:27 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
|
2018-12-11 14:51:08 -08:00
|
|
|
auto *type{GetDeclTypeSpec()};
|
2018-09-05 16:02:41 -07:00
|
|
|
auto attr{std::get<common::TypeParamAttr>(x.t)};
|
2018-09-04 10:28:27 -07:00
|
|
|
for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(decl.t)};
|
2018-11-06 17:18:06 -08:00
|
|
|
auto details{TypeParamDetails{attr}};
|
|
|
|
if (auto &init{
|
|
|
|
std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
|
2019-01-07 15:05:53 -08:00
|
|
|
details.set_init(EvaluateIntExpr(*init));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
2018-12-06 17:52:43 -08:00
|
|
|
if (MakeTypeSymbol(name, std::move(details))) {
|
|
|
|
SetType(name, *type);
|
|
|
|
}
|
2018-09-04 10:28:27 -07:00
|
|
|
}
|
|
|
|
EndDecl();
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::TypeAttrSpec::Extends &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
derivedTypeInfo_.extends = &x.v;
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-06 08:01:49 -07:00
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::PrivateStmt &x) {
|
2018-09-07 09:06:27 -07:00
|
|
|
if (!currScope().parent().IsModule()) {
|
2018-09-06 08:01:49 -07:00
|
|
|
Say("PRIVATE is only allowed in a derived type that is"
|
|
|
|
" in a module"_err_en_US); // C766
|
|
|
|
} else if (derivedTypeInfo_.sawContains) {
|
|
|
|
derivedTypeInfo_.privateBindings = true;
|
|
|
|
} else if (!derivedTypeInfo_.privateComps) {
|
|
|
|
derivedTypeInfo_.privateComps = true;
|
|
|
|
} else {
|
|
|
|
Say("PRIVATE may not appear more than once in"
|
2018-09-07 09:06:27 -07:00
|
|
|
" derived type components"_en_US); // C738
|
2018-09-06 08:01:49 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::SequenceStmt &x) {
|
2018-09-06 08:01:49 -07:00
|
|
|
derivedTypeInfo_.sequence = true;
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ComponentDecl &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-09-06 08:01:49 -07:00
|
|
|
auto attrs{GetAttrs()};
|
|
|
|
if (derivedTypeInfo_.privateComps &&
|
|
|
|
!attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
attrs.set(Attr::PRIVATE);
|
|
|
|
}
|
2019-02-15 16:08:32 -08:00
|
|
|
if (!attrs.HasAny({Attr::POINTER, Attr::ALLOCATABLE})) {
|
|
|
|
if (const auto *declType{GetDeclTypeSpec()}) {
|
|
|
|
if (const auto *derived{declType->AsDerived()}) {
|
|
|
|
if (derivedTypeInfo_.type == &derived->typeSymbol()) { // C737
|
|
|
|
Say("Recursive use of the derived type requires "
|
|
|
|
"POINTER or ALLOCATABLE"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
if (OkToAddComponent(name)) {
|
2018-11-06 17:18:06 -08:00
|
|
|
auto &symbol{DeclareObjectEntity(name, attrs)};
|
|
|
|
if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
|
|
|
|
if (auto *initExpr{std::get_if<parser::ConstantExpr>(&init->u)}) {
|
2018-12-06 06:59:37 -08:00
|
|
|
details->set_init(EvaluateExpr(*initExpr));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 10:48:26 -08:00
|
|
|
currScope().symbol()->get<DerivedTypeDetails>().add_component(symbol);
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
ClearArraySpec();
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcedureDeclarationStmt &) {
|
|
|
|
CHECK(!interfaceName_);
|
|
|
|
return BeginDecl();
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcedureDeclarationStmt &) {
|
|
|
|
interfaceName_ = nullptr;
|
|
|
|
EndDecl();
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcComponentDefStmt &) {
|
|
|
|
CHECK(!interfaceName_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcComponentDefStmt &) {
|
|
|
|
interfaceName_ = nullptr;
|
|
|
|
}
|
2019-02-22 15:45:30 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcPointerInit &x) {
|
|
|
|
if (auto *name{std::get_if<parser::Name>(&x.u)}) {
|
2019-02-26 14:26:28 -08:00
|
|
|
return !NameIsKnownOrIntrinsic(*name);
|
2019-02-22 15:45:30 -08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-21 17:05:46 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcInterface &x) {
|
|
|
|
if (auto *name{std::get_if<parser::Name>(&x.u)}) {
|
2019-02-27 11:12:16 -08:00
|
|
|
if (!NameIsKnownOrIntrinsic(*name)) {
|
2019-02-26 14:26:28 -08:00
|
|
|
// Simple names (lacking parameters and size) of intrinsic types re
|
|
|
|
// ambiguous in Fortran when used as instances of proc-interface.
|
|
|
|
// The parser recognizes them as interface-names since they can be
|
|
|
|
// overridden. If they turn out (here) to not be names of explicit
|
|
|
|
// interfaces, we need to replace their parses.
|
|
|
|
auto &proc{const_cast<parser::ProcInterface &>(x)};
|
|
|
|
if (name->source == "integer") {
|
|
|
|
proc.u =
|
|
|
|
parser::IntrinsicTypeSpec{parser::IntegerTypeSpec{std::nullopt}};
|
|
|
|
} else if (name->source == "real") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::Real{std::nullopt}};
|
|
|
|
} else if (name->source == "doubleprecision") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::DoublePrecision{}};
|
|
|
|
} else if (name->source == "complex") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::Complex{std::nullopt}};
|
|
|
|
} else if (name->source == "character") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::Character{std::nullopt}};
|
|
|
|
} else if (name->source == "logical") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::Logical{std::nullopt}};
|
|
|
|
} else if (name->source == "doublecomplex") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::DoubleComplex{}};
|
|
|
|
} else if (name->source == "ncharacter") {
|
|
|
|
proc.u = parser::IntrinsicTypeSpec{
|
|
|
|
parser::IntrinsicTypeSpec::NCharacter{std::nullopt}};
|
|
|
|
}
|
2019-02-21 17:05:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::ProcInterface &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *name{std::get_if<parser::Name>(&x.u)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
interfaceName_ = name;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcDecl &x) {
|
2019-02-20 17:45:39 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-06-05 12:18:35 -07:00
|
|
|
ProcInterface interface;
|
|
|
|
if (interfaceName_) {
|
2019-02-21 17:05:46 -08:00
|
|
|
if (const Symbol * symbol{FindExplicitInterface(*interfaceName_)}) {
|
2018-06-06 11:41:42 -07:00
|
|
|
interface.set_symbol(*symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2019-02-21 17:05:46 -08:00
|
|
|
}
|
|
|
|
if (interface.symbol() == nullptr) {
|
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
|
|
|
interface.set_type(*type);
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2019-02-20 17:45:39 -08:00
|
|
|
auto attrs{HandleSaveName(name.source, GetAttrs())};
|
2018-09-06 12:06:32 -07:00
|
|
|
if (currScope().kind() != Scope::Kind::DerivedType) {
|
|
|
|
attrs.set(Attr::EXTERNAL);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-09-24 11:43:48 -07:00
|
|
|
DeclareProcEntity(name, attrs, interface);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-09-06 08:01:49 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::TypeBoundProcedurePart &x) {
|
|
|
|
derivedTypeInfo_.sawContains = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 16:08:32 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::ContainsStmt &) {
|
|
|
|
if (derivedTypeInfo_.sequence) {
|
|
|
|
Say("A sequence type may not have a CONTAINS statement"_err_en_US); // C740
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:20:00 -07:00
|
|
|
void DeclarationVisitor::Post(
|
|
|
|
const parser::TypeBoundProcedureStmt::WithoutInterface &x) {
|
|
|
|
if (GetAttrs().test(Attr::DEFERRED)) { // C783
|
|
|
|
Say("DEFERRED is only allowed when an interface-name is provided"_err_en_US);
|
|
|
|
}
|
|
|
|
for (auto &declaration : x.declarations) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &bindingName{std::get<parser::Name>(declaration.t)};
|
2018-08-31 16:20:00 -07:00
|
|
|
auto &optName{std::get<std::optional<parser::Name>>(declaration.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &procedureName{optName ? *optName : bindingName};
|
2018-08-31 16:20:00 -07:00
|
|
|
auto *procedure{FindSymbol(procedureName)};
|
|
|
|
if (!procedure) {
|
|
|
|
Say(procedureName, "Procedure '%s' not found"_err_en_US);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
procedure = &procedure->GetUltimate(); // may come from USE
|
|
|
|
if (!CanBeTypeBoundProc(*procedure)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(procedureName, *procedure,
|
2018-08-31 16:20:00 -07:00
|
|
|
"'%s' is not a module procedure or external procedure"
|
2019-02-05 13:51:36 -08:00
|
|
|
" with explicit interface"_err_en_US);
|
2018-08-31 16:20:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
if (auto *s{MakeTypeSymbol(bindingName, ProcBindingDetails{*procedure})}) {
|
|
|
|
SetPassNameOn(*s);
|
|
|
|
}
|
2018-08-31 16:20:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(
|
|
|
|
const parser::TypeBoundProcedureStmt::WithInterface &x) {
|
|
|
|
if (!GetAttrs().test(Attr::DEFERRED)) { // C783
|
|
|
|
Say("DEFERRED is required when an interface-name is provided"_err_en_US);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *interface{FindExplicitInterface(x.interfaceName)};
|
2018-08-31 16:20:00 -07:00
|
|
|
if (!interface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &bindingName : x.bindingNames) {
|
2019-01-04 17:10:31 -08:00
|
|
|
if (auto *s{MakeTypeSymbol(bindingName, ProcBindingDetails{*interface})}) {
|
|
|
|
SetPassNameOn(*s);
|
|
|
|
}
|
2018-08-31 16:20:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::FinalProcedureStmt &x) {
|
|
|
|
for (auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
MakeTypeSymbol(name, FinalProcDetails{});
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::TypeBoundGenericStmt &x) {
|
2019-02-08 08:57:28 -08:00
|
|
|
const auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)};
|
2019-01-15 16:59:20 -08:00
|
|
|
const auto &genericSpec{std::get<Indirection<parser::GenericSpec>>(x.t)};
|
2019-02-08 08:57:28 -08:00
|
|
|
const auto &bindingNames{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
SymbolList specificProcs;
|
|
|
|
for (const auto &bindingName : bindingNames) {
|
|
|
|
const auto *symbol{FindInTypeOrParents(bindingName)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(bindingName,
|
|
|
|
"Binding name '%s' not found in this derived type"_err_en_US);
|
|
|
|
} else if (!symbol->has<ProcBindingDetails>()) {
|
|
|
|
SayWithDecl(bindingName, *symbol,
|
|
|
|
"'%s' is not the name of a specific binding of this type"_err_en_US);
|
|
|
|
} else {
|
|
|
|
specificProcs.push_back(symbol);
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 13:31:13 -08:00
|
|
|
const auto *genericName{GetGenericSpecName(*genericSpec)};
|
|
|
|
if (!genericName) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-08 08:57:28 -08:00
|
|
|
bool isPrivate{accessSpec ? accessSpec->v == parser::AccessSpec::Kind::Private
|
|
|
|
: derivedTypeInfo_.privateBindings};
|
2018-12-26 13:31:13 -08:00
|
|
|
const SymbolList *inheritedProcs{nullptr}; // specific procs from parent type
|
|
|
|
auto *genericSymbol{FindInScope(currScope(), *genericName)};
|
|
|
|
if (genericSymbol) {
|
|
|
|
if (!genericSymbol->has<GenericBindingDetails>()) {
|
|
|
|
genericSymbol = nullptr; // MakeTypeSymbol will report the error below
|
|
|
|
}
|
2019-02-08 08:57:28 -08:00
|
|
|
} else if (const auto *inheritedSymbol{FindInTypeOrParents(*genericName)}) {
|
2018-12-26 13:31:13 -08:00
|
|
|
// look in parent types:
|
|
|
|
if (inheritedSymbol->has<GenericBindingDetails>()) {
|
|
|
|
inheritedProcs =
|
|
|
|
&inheritedSymbol->get<GenericBindingDetails>().specificProcs();
|
|
|
|
CheckAccessibility(*genericName, isPrivate, *inheritedSymbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (genericSymbol) {
|
|
|
|
CheckAccessibility(*genericName, isPrivate, *genericSymbol);
|
|
|
|
} else {
|
|
|
|
genericSymbol = MakeTypeSymbol(*genericName, GenericBindingDetails{});
|
|
|
|
if (!genericSymbol) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isPrivate) {
|
|
|
|
genericSymbol->attrs().set(Attr::PRIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &details{genericSymbol->get<GenericBindingDetails>()};
|
|
|
|
if (inheritedProcs) {
|
|
|
|
details.add_specificProcs(*inheritedProcs);
|
|
|
|
}
|
2019-02-08 08:57:28 -08:00
|
|
|
details.add_specificProcs(specificProcs);
|
2018-12-26 13:31:13 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::AllocateStmt &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::AllocateStmt &) {
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:19:41 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::StructureConstructor &x) {
|
2019-02-06 12:25:28 -08:00
|
|
|
auto &parsedType{std::get<parser::DerivedTypeSpec>(x.t)};
|
2019-02-24 10:05:43 -08:00
|
|
|
const DeclTypeSpec *type{ProcessTypeSpec(parsedType)};
|
2019-02-14 14:37:55 -08:00
|
|
|
if (type == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const DerivedTypeSpec *spec{type->AsDerived()};
|
2019-02-06 12:25:28 -08:00
|
|
|
const Scope *typeScope{spec ? spec->scope() : nullptr};
|
2019-02-14 14:37:55 -08:00
|
|
|
if (typeScope == nullptr) {
|
|
|
|
return false;
|
2019-02-06 12:25:28 -08:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:24:43 -08:00
|
|
|
// N.B C7102 is implicitly enforced by having inaccessible types not
|
|
|
|
// being found in resolution.
|
|
|
|
|
2019-02-04 12:24:25 -08:00
|
|
|
for (const auto &component :
|
|
|
|
std::get<std::list<parser::ComponentSpec>>(x.t)) {
|
2019-02-12 17:24:43 -08:00
|
|
|
// Visit the component spec expression, but not the keyword, since
|
|
|
|
// we need to resolve its symbol in the scope of the derived type.
|
2019-02-14 14:37:55 -08:00
|
|
|
Walk(std::get<parser::ComponentDataSource>(component.t));
|
|
|
|
if (const auto &kw{std::get<std::optional<parser::Keyword>>(component.t)}) {
|
|
|
|
if (const Symbol * symbol{FindInTypeOrParents(*typeScope, kw->v)}) {
|
|
|
|
CheckAccessibleComponent(kw->v.source, *symbol); // C7102
|
|
|
|
} else { // C7101
|
|
|
|
Say(kw->v.source,
|
|
|
|
"Keyword '%s' is not a component of this derived type"_err_en_US);
|
2019-02-06 12:25:28 -08:00
|
|
|
}
|
2019-02-04 12:24:25 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 17:19:41 -08:00
|
|
|
return false;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 14:43:00 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::NamelistStmt::Group &x) {
|
2019-02-18 11:39:46 -08:00
|
|
|
if (!CheckNotInBlock("NAMELIST")) {
|
2019-02-05 14:43:00 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NamelistDetails details;
|
|
|
|
for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
symbol = &MakeSymbol(name, ObjectEntityDetails{});
|
|
|
|
ApplyImplicitRules(*symbol);
|
|
|
|
} else if (!ConvertToObjectEntity(*symbol)) {
|
|
|
|
SayWithDecl(name, *symbol, "'%s' is not a variable"_err_en_US);
|
|
|
|
}
|
|
|
|
details.add_object(*symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &groupName{std::get<parser::Name>(x.t)};
|
|
|
|
auto *groupSymbol{FindInScope(currScope(), groupName)};
|
|
|
|
if (!groupSymbol) {
|
|
|
|
groupSymbol = &MakeSymbol(groupName, std::move(details));
|
|
|
|
} else if (groupSymbol->has<NamelistDetails>()) {
|
|
|
|
groupSymbol->get<NamelistDetails>().add_objects(details.objects());
|
|
|
|
} else {
|
|
|
|
SayAlreadyDeclared(groupName, *groupSymbol);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationVisitor::Pre(const parser::IoControlSpec &x) {
|
|
|
|
if (const auto *name{std::get_if<parser::Name>(&x.u)}) {
|
|
|
|
auto *symbol{FindSymbol(*name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(*name, "Namelist group '%s' not found"_err_en_US);
|
|
|
|
} else if (!symbol->GetUltimate().has<NamelistDetails>()) {
|
|
|
|
SayWithDecl(
|
|
|
|
*name, *symbol, "'%s' is not the name of a namelist group"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-14 07:59:20 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::CommonStmt::Block &x) {
|
2019-02-18 11:39:46 -08:00
|
|
|
CheckNotInBlock("COMMON");
|
2019-02-14 07:59:20 -08:00
|
|
|
const auto &optName{std::get<std::optional<parser::Name>>(x.t)};
|
|
|
|
parser::Name blankCommon;
|
|
|
|
blankCommon.source = SourceName{currStmtSource()->begin(), std::size_t{0}};
|
|
|
|
CHECK(!commonBlockInfo_.curr);
|
2019-02-18 11:39:46 -08:00
|
|
|
commonBlockInfo_.curr =
|
|
|
|
&MakeCommonBlockSymbol(optName ? *optName : blankCommon);
|
2019-02-14 07:59:20 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::CommonStmt::Block &) {
|
|
|
|
commonBlockInfo_.curr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationVisitor::Pre(const parser::CommonBlockObject &x) {
|
|
|
|
BeginArraySpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::CommonBlockObject &x) {
|
|
|
|
CHECK(commonBlockInfo_.curr);
|
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
|
|
|
auto &symbol{DeclareObjectEntity(name, Attrs{})};
|
|
|
|
ClearArraySpec();
|
|
|
|
if (!symbol.has<ObjectEntityDetails>()) {
|
|
|
|
return; // error was reported
|
|
|
|
}
|
|
|
|
commonBlockInfo_.curr->get<CommonBlockDetails>().add_object(symbol);
|
|
|
|
if (!IsExplicit(symbol.get<ObjectEntityDetails>().shape())) {
|
|
|
|
Say(name,
|
|
|
|
"The shape of common block object '%s' must be explicit"_err_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto pair{commonBlockInfo_.names.insert(name.source)};
|
|
|
|
if (!pair.second) {
|
|
|
|
const SourceName &prev{*pair.first};
|
|
|
|
Say2(name.source, "'%s' is already in a COMMON block"_err_en_US, prev,
|
|
|
|
"Previous occurrence of '%s' in a COMMON block"_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:45:39 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::SaveStmt &x) {
|
|
|
|
if (x.v.empty()) {
|
|
|
|
saveInfo_.saveAll = currStmtSource();
|
|
|
|
} else {
|
|
|
|
for (const parser::SavedEntity &y : x.v) {
|
|
|
|
auto kind{std::get<parser::SavedEntity::Kind>(y.t)};
|
|
|
|
const auto &name{std::get<parser::Name>(y.t)};
|
|
|
|
if (kind == parser::SavedEntity::Kind::Common) {
|
|
|
|
MakeCommonBlockSymbol(name);
|
|
|
|
AddSaveName(saveInfo_.commons, name.source);
|
|
|
|
} else {
|
|
|
|
HandleAttributeStmt(Attr::SAVE, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::CheckSaveStmts() {
|
|
|
|
for (const SourceName &name : saveInfo_.entities) {
|
|
|
|
auto *symbol{FindInScope(currScope(), name)};
|
|
|
|
if (!symbol) {
|
|
|
|
// error was reported
|
|
|
|
} else if (saveInfo_.saveAll) {
|
|
|
|
// C889 - note that pgi, ifort, xlf do not enforce this constraint
|
|
|
|
Say2(name,
|
|
|
|
"Explicit SAVE of '%s' is redundant due to global SAVE statement"_err_en_US,
|
|
|
|
*saveInfo_.saveAll, "Global SAVE statement"_en_US);
|
|
|
|
} else if (auto msg{CheckSaveAttr(*symbol)}) {
|
|
|
|
Say(name, *msg);
|
|
|
|
} else {
|
|
|
|
SetSaveAttr(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const SourceName &name : saveInfo_.commons) {
|
|
|
|
if (auto *symbol{currScope().FindCommonBlock(name)}) {
|
|
|
|
auto &objects{symbol->get<CommonBlockDetails>().objects()};
|
|
|
|
if (objects.empty()) {
|
|
|
|
Say(name,
|
|
|
|
"'%s' appears as a COMMON block in a SAVE statement but not in"
|
|
|
|
" a COMMON statement"_err_en_US);
|
|
|
|
} else {
|
|
|
|
for (Symbol *object : symbol->get<CommonBlockDetails>().objects()) {
|
|
|
|
SetSaveAttr(*object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (saveInfo_.saveAll) {
|
|
|
|
// Apply SAVE attribute to applicable symbols
|
|
|
|
for (auto pair : currScope()) {
|
|
|
|
auto &symbol{*pair.second};
|
|
|
|
if (!CheckSaveAttr(symbol)) {
|
|
|
|
SetSaveAttr(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saveInfo_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If SAVE attribute can't be set on symbol, return error message.
|
|
|
|
std::optional<MessageFixedText> DeclarationVisitor::CheckSaveAttr(
|
|
|
|
const Symbol &symbol) {
|
|
|
|
if (symbol.IsDummy()) {
|
|
|
|
return "SAVE attribute may not be applied to dummy argument '%s'"_err_en_US;
|
|
|
|
} else if (symbol.IsFuncResult()) {
|
|
|
|
return "SAVE attribute may not be applied to function result '%s'"_err_en_US;
|
|
|
|
} else if (symbol.has<ProcEntityDetails>() &&
|
|
|
|
!symbol.attrs().test(Attr::POINTER)) {
|
|
|
|
return "Procedure '%s' with SAVE attribute must also have POINTER attribute"_err_en_US;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instead of setting SAVE attribute, record the name in saveInfo_.entities.
|
|
|
|
Attrs DeclarationVisitor::HandleSaveName(const SourceName &name, Attrs attrs) {
|
|
|
|
if (attrs.test(Attr::SAVE)) {
|
|
|
|
attrs.set(Attr::SAVE, false);
|
|
|
|
AddSaveName(saveInfo_.entities, name);
|
|
|
|
}
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record a name in a set of those to be saved.
|
|
|
|
void DeclarationVisitor::AddSaveName(
|
|
|
|
std::set<SourceName> &set, const SourceName &name) {
|
|
|
|
auto pair{set.insert(name)};
|
|
|
|
if (!pair.second) {
|
|
|
|
Say2(name, "SAVE attribute was already specified on '%s'"_err_en_US,
|
|
|
|
*pair.first, "Previous specification of SAVE attribute"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the SAVE attribute on symbol unless it is implicitly saved anyway.
|
|
|
|
void DeclarationVisitor::SetSaveAttr(Symbol &symbol) {
|
|
|
|
auto scopeKind{symbol.owner().kind()};
|
|
|
|
if (scopeKind == Scope::Kind::MainProgram ||
|
|
|
|
scopeKind == Scope::Kind::Module) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
if (details->init()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
symbol.attrs().set(Attr::SAVE);
|
|
|
|
}
|
|
|
|
|
2019-02-14 07:59:20 -08:00
|
|
|
// Check types of common block objects, now that they are known.
|
|
|
|
void DeclarationVisitor::CheckCommonBlocks() {
|
2019-02-18 11:39:46 -08:00
|
|
|
// check for empty common blocks
|
|
|
|
for (const auto pair : currScope().commonBlocks()) {
|
|
|
|
const auto &symbol{*pair.second};
|
|
|
|
if (symbol.get<CommonBlockDetails>().objects().empty() &&
|
|
|
|
symbol.attrs().test(Attr::BIND_C)) {
|
|
|
|
Say(symbol.name(),
|
|
|
|
"'%s' appears as a COMMON block in a BIND statement but not in"
|
|
|
|
" a COMMON statement"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check objects in common blocks
|
2019-02-14 07:59:20 -08:00
|
|
|
for (const auto &name : commonBlockInfo_.names) {
|
|
|
|
const auto *symbol{currScope().FindSymbol(name)};
|
2019-02-18 11:39:46 -08:00
|
|
|
if (symbol == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-14 07:59:20 -08:00
|
|
|
const auto &attrs{symbol->attrs()};
|
|
|
|
if (attrs.test(Attr::ALLOCATABLE)) {
|
|
|
|
Say(name,
|
|
|
|
"ALLOCATABLE object '%s' may not appear in a COMMON block"_err_en_US);
|
|
|
|
} else if (attrs.test(Attr::BIND_C)) {
|
|
|
|
Say(name,
|
|
|
|
"Variable '%s' with BIND attribute may not appear in a COMMON block"_err_en_US);
|
2019-02-20 17:45:39 -08:00
|
|
|
} else if (symbol->IsDummy()) {
|
2019-02-14 07:59:20 -08:00
|
|
|
Say(name,
|
|
|
|
"Dummy argument '%s' may not appear in a COMMON block"_err_en_US);
|
2019-02-20 17:45:39 -08:00
|
|
|
} else if (symbol->IsFuncResult()) {
|
|
|
|
Say(name,
|
|
|
|
"Function result '%s' may not appear in a COMMON block"_err_en_US);
|
|
|
|
} else if (const DeclTypeSpec * type{symbol->GetType()}) {
|
2019-02-14 07:59:20 -08:00
|
|
|
if (type->category() == DeclTypeSpec::ClassStar) {
|
|
|
|
Say(name,
|
|
|
|
"Unlimited polymorphic pointer '%s' may not appear in a COMMON block"_err_en_US);
|
|
|
|
} else if (const auto *derived{type->AsDerived()}) {
|
|
|
|
auto &typeSymbol{derived->typeSymbol()};
|
|
|
|
if (!typeSymbol.attrs().test(Attr::BIND_C) &&
|
|
|
|
!typeSymbol.get<DerivedTypeDetails>().sequence()) {
|
|
|
|
Say(name,
|
|
|
|
"Derived type '%s' in COMMON block must have the BIND or"
|
|
|
|
" SEQUENCE attribute"_err_en_US);
|
|
|
|
}
|
|
|
|
CheckCommonBlockDerivedType(name, typeSymbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
commonBlockInfo_ = {};
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:39:46 -08:00
|
|
|
Symbol &DeclarationVisitor::MakeCommonBlockSymbol(const parser::Name &name) {
|
|
|
|
return Resolve(name, currScope().MakeCommonBlock(name.source));
|
|
|
|
}
|
|
|
|
|
2019-02-26 14:26:28 -08:00
|
|
|
bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
|
|
|
|
return FindSymbol(name) != nullptr ||
|
|
|
|
HandleUnrestrictedSpecificIntrinsicFunction(name);
|
|
|
|
}
|
|
|
|
|
2019-02-14 07:59:20 -08:00
|
|
|
// Check if this derived type can be in a COMMON block.
|
|
|
|
void DeclarationVisitor::CheckCommonBlockDerivedType(
|
|
|
|
const SourceName &name, const Symbol &typeSymbol) {
|
|
|
|
if (const auto *scope{typeSymbol.scope()}) {
|
|
|
|
for (const auto &pair : *scope) {
|
|
|
|
const Symbol &component{*pair.second};
|
|
|
|
if (component.attrs().test(Attr::ALLOCATABLE)) {
|
|
|
|
Say2(name,
|
|
|
|
"Derived type variable '%s' may not appear in a COMMON block"
|
|
|
|
" due to ALLOCATABLE component"_err_en_US,
|
|
|
|
component.name(), "Component with ALLOCATABLE attribute"_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto *details{component.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
if (details->init()) {
|
|
|
|
Say2(name,
|
|
|
|
"Derived type variable '%s' may not appear in a COMMON block"
|
|
|
|
" due to component with default initialization"_err_en_US,
|
|
|
|
component.name(), "Component with default initialization"_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto *type{details->type()}) {
|
|
|
|
if (const auto *derived{type->AsDerived()}) {
|
|
|
|
CheckCommonBlockDerivedType(name, derived->typeSymbol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:45:30 -08:00
|
|
|
bool DeclarationVisitor::HandleUnrestrictedSpecificIntrinsicFunction(
|
|
|
|
const parser::Name &name) {
|
|
|
|
if (context()
|
|
|
|
.intrinsics()
|
|
|
|
.IsUnrestrictedSpecificIntrinsicFunction(name.source.ToString())
|
|
|
|
.has_value()) {
|
|
|
|
// Unrestricted specific intrinsic function names (e.g., "cos")
|
|
|
|
// are acceptable as procedure interfaces.
|
|
|
|
Scope *scope{&currScope()};
|
2019-02-26 14:26:28 -08:00
|
|
|
while (scope->kind() == Scope::Kind::DerivedType) {
|
2019-02-22 15:45:30 -08:00
|
|
|
scope = &scope->parent();
|
|
|
|
}
|
|
|
|
Symbol &symbol{MakeSymbol(*scope, name.source, Attrs{Attr::INTRINSIC})};
|
|
|
|
symbol.set_details(MiscDetails{MiscDetails::Kind::SpecificIntrinsic});
|
|
|
|
CHECK(symbol.HasExplicitInterface());
|
|
|
|
Resolve(name, symbol);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 14:31:47 -08:00
|
|
|
Symbol *DeclarationVisitor::DeclareLocalEntity(const parser::Name &name) {
|
2018-10-18 07:55:48 -07:00
|
|
|
auto *prev{FindSymbol(name)};
|
2019-01-29 14:31:47 -08:00
|
|
|
bool implicit{false};
|
|
|
|
if (prev == nullptr) {
|
|
|
|
// Declare the name as an object in the enclosing scope so that
|
|
|
|
// the name can't be repurposed there later as something else.
|
|
|
|
prev = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
|
|
|
|
ApplyImplicitRules(*prev);
|
|
|
|
implicit = true;
|
|
|
|
}
|
2019-02-04 10:39:41 -08:00
|
|
|
if (!ConvertToObjectEntity(*prev) || prev->attrs().test(Attr::PARAMETER)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, *prev, "Locality attribute not allowed on '%s'"_err_en_US);
|
2019-01-29 14:31:47 -08:00
|
|
|
return nullptr;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
2019-01-29 14:31:47 -08:00
|
|
|
if (prev->owner() == currScope()) {
|
|
|
|
SayAlreadyDeclared(name, *prev);
|
2018-10-18 07:55:48 -07:00
|
|
|
return nullptr;
|
2019-01-29 14:31:47 -08:00
|
|
|
}
|
|
|
|
name.symbol = nullptr;
|
|
|
|
Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, {})};
|
|
|
|
if (auto *type{prev->GetType()}) {
|
|
|
|
if (implicit) {
|
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
} else {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
return &symbol;
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:56:50 -08:00
|
|
|
Symbol *DeclarationVisitor::DeclareStatementEntity(const parser::Name &name,
|
|
|
|
const std::optional<parser::IntegerTypeSpec> &type) {
|
2019-02-22 15:56:39 -08:00
|
|
|
const DeclTypeSpec *declTypeSpec{nullptr};
|
2019-01-22 16:30:32 -08:00
|
|
|
if (auto *prev{FindSymbol(name)}) {
|
|
|
|
if (prev->owner() == currScope()) {
|
|
|
|
SayAlreadyDeclared(name, *prev);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
name.symbol = nullptr;
|
2019-02-22 15:56:39 -08:00
|
|
|
declTypeSpec = prev->GetType();
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
|
|
|
Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, {})};
|
2019-02-22 15:56:39 -08:00
|
|
|
if (!symbol.has<ObjectEntityDetails>()) {
|
|
|
|
return nullptr; // error was reported in DeclareEntity
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
2019-02-22 15:56:39 -08:00
|
|
|
if (type.has_value()) {
|
2019-02-24 10:05:43 -08:00
|
|
|
declTypeSpec = ProcessTypeSpec(*type);
|
2019-02-22 15:56:39 -08:00
|
|
|
}
|
|
|
|
if (declTypeSpec != nullptr) {
|
|
|
|
SetType(name, *declTypeSpec);
|
|
|
|
} else {
|
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
}
|
|
|
|
CheckScalarIntegerType(name);
|
|
|
|
return Resolve(name, &symbol);
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
|
|
|
|
2018-09-20 14:08:59 -07:00
|
|
|
// Set the type of an entity or report an error.
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::SetType(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, const DeclTypeSpec &type) {
|
|
|
|
CHECK(name.symbol);
|
|
|
|
auto &symbol{*name.symbol};
|
2018-09-10 12:20:42 -07:00
|
|
|
auto *prevType{symbol.GetType()};
|
|
|
|
if (!prevType) {
|
|
|
|
symbol.SetType(type);
|
2019-01-31 13:18:30 -08:00
|
|
|
} else if (symbol.has<UseDetails>()) {
|
|
|
|
// error recovery case, redeclaration of use-associated name
|
2018-09-10 12:20:42 -07:00
|
|
|
} else if (!symbol.test(Symbol::Flag::Implicit)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "The type of '%s' has already been declared"_err_en_US);
|
2018-09-10 12:20:42 -07:00
|
|
|
} else if (type != *prevType) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"The type of '%s' has already been implicitly declared"_err_en_US);
|
2018-09-10 12:20:42 -07:00
|
|
|
} else {
|
|
|
|
symbol.set(Symbol::Flag::Implicit, false);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
// Find the Symbol for this derived type.
|
|
|
|
const Symbol *DeclarationVisitor::ResolveDerivedType(const parser::Name &name) {
|
2018-12-04 10:55:32 -08:00
|
|
|
const Symbol *symbol{FindSymbol(name)};
|
2018-08-29 11:38:12 -07:00
|
|
|
if (!symbol) {
|
2019-01-15 16:59:20 -08:00
|
|
|
Say(name, "Derived type '%s' not found"_err_en_US);
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
if (CheckUseError(name)) {
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto *details{symbol->detailsIf<UseDetails>()}) {
|
|
|
|
symbol = &details->symbol();
|
|
|
|
}
|
|
|
|
if (auto *details{symbol->detailsIf<GenericDetails>()}) {
|
|
|
|
if (details->derivedType()) {
|
|
|
|
symbol = details->derivedType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol->has<DerivedTypeDetails>()) {
|
2019-01-15 16:59:20 -08:00
|
|
|
Say(name, "'%s' is not a derived type"_err_en_US);
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:20:00 -07:00
|
|
|
// Check this symbol suitable as a type-bound procedure - C769
|
|
|
|
bool DeclarationVisitor::CanBeTypeBoundProc(const Symbol &symbol) {
|
|
|
|
if (symbol.has<SubprogramNameDetails>()) {
|
|
|
|
return symbol.owner().kind() == Scope::Kind::Module;
|
|
|
|
} else if (auto *details{symbol.detailsIf<SubprogramDetails>()}) {
|
|
|
|
return symbol.owner().kind() == Scope::Kind::Module ||
|
|
|
|
details->isInterface();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *DeclarationVisitor::FindExplicitInterface(const parser::Name &name) {
|
2018-08-31 16:20:00 -07:00
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name, "Explicit interface '%s' not found"_err_en_US);
|
|
|
|
} else if (!symbol->HasExplicitInterface()) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(name, *symbol,
|
2018-08-31 16:20:00 -07:00
|
|
|
"'%s' is not an abstract interface or a procedure with an"
|
2019-02-05 13:51:36 -08:00
|
|
|
" explicit interface"_err_en_US);
|
2018-08-31 16:20:00 -07:00
|
|
|
symbol = nullptr;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symbol for a type parameter, component, or procedure binding in
|
2018-12-06 17:52:43 -08:00
|
|
|
// the current derived type scope. Return false on error.
|
2018-12-26 13:31:13 -08:00
|
|
|
Symbol *DeclarationVisitor::MakeTypeSymbol(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Details &&details) {
|
2018-08-31 16:20:00 -07:00
|
|
|
Scope &derivedType{currScope()};
|
|
|
|
CHECK(derivedType.kind() == Scope::Kind::DerivedType);
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindInScope(derivedType, name)}) {
|
2018-08-31 16:20:00 -07:00
|
|
|
Say2(name,
|
|
|
|
"Type parameter, component, or procedure binding '%s'"
|
|
|
|
" already defined in this type"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*symbol, "Previous definition of '%s'"_en_US);
|
2018-12-26 13:31:13 -08:00
|
|
|
return nullptr;
|
2018-08-31 16:20:00 -07:00
|
|
|
} else {
|
2018-09-06 08:01:49 -07:00
|
|
|
auto attrs{GetAttrs()};
|
|
|
|
// Apply binding-private-stmt if present and this is a procedure binding
|
|
|
|
if (derivedTypeInfo_.privateBindings &&
|
|
|
|
!attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE}) &&
|
|
|
|
std::holds_alternative<ProcBindingDetails>(details)) {
|
|
|
|
attrs.set(Attr::PRIVATE);
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
Symbol &result{MakeSymbol(name, attrs, details)};
|
|
|
|
if (result.has<TypeParamDetails>()) {
|
|
|
|
derivedType.symbol()->get<DerivedTypeDetails>().add_paramDecl(result);
|
|
|
|
}
|
|
|
|
return &result;
|
2018-08-31 16:20:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:08:59 -07:00
|
|
|
// Return true if it is ok to declare this component in the current scope.
|
|
|
|
// Otherwise, emit an error and return false.
|
|
|
|
bool DeclarationVisitor::OkToAddComponent(
|
2019-01-07 15:05:53 -08:00
|
|
|
const parser::Name &name, const Symbol *extends) {
|
2018-12-04 10:55:32 -08:00
|
|
|
for (const Scope *scope{&currScope()}; scope != nullptr;) {
|
2018-09-20 14:08:59 -07:00
|
|
|
CHECK(scope->kind() == Scope::Kind::DerivedType);
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *prev{FindInScope(*scope, name)}) {
|
2018-11-16 16:40:00 -08:00
|
|
|
auto msg{""_en_US};
|
2019-01-07 15:05:53 -08:00
|
|
|
if (extends != nullptr) {
|
2018-09-20 14:08:59 -07:00
|
|
|
msg = "Type cannot be extended as it has a component named"
|
|
|
|
" '%s'"_err_en_US;
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (prev->test(Symbol::Flag::ParentComp)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
msg = "'%s' is a parent type of this type and so cannot be"
|
|
|
|
" a component"_err_en_US;
|
2018-12-04 10:55:32 -08:00
|
|
|
} else if (scope != &currScope()) {
|
2018-09-20 14:08:59 -07:00
|
|
|
msg = "Component '%s' is already declared in a parent of this"
|
|
|
|
" derived type"_err_en_US;
|
|
|
|
} else {
|
|
|
|
msg = "Component '%s' is already declared in this"
|
|
|
|
" derived type"_err_en_US;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Say2(name, std::move(msg), *prev, "Previous declaration of '%s'"_en_US);
|
2018-09-20 14:08:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
if (scope == &currScope() && extends != nullptr) {
|
2019-01-07 15:05:53 -08:00
|
|
|
// The parent component has not yet been added to the scope.
|
|
|
|
scope = extends->scope();
|
|
|
|
} else {
|
2018-12-04 10:55:32 -08:00
|
|
|
scope = scope->GetDerivedTypeParent();
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamValue DeclarationVisitor::GetParamValue(const parser::TypeParamValue &x) {
|
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[=](const parser::ScalarIntExpr &x) {
|
|
|
|
return ParamValue{EvaluateIntExpr(x)};
|
|
|
|
},
|
|
|
|
[](const parser::Star &) { return ParamValue::Assumed(); },
|
|
|
|
[](const parser::TypeParamValue::Deferred &) {
|
|
|
|
return ParamValue::Deferred();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
// ConstructVisitor implementation
|
2018-09-25 08:53:53 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool ConstructVisitor::Pre(const parser::ConcurrentHeader &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::ConcurrentHeader &) {
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::Local &x) {
|
|
|
|
for (auto &name : x.v) {
|
2019-01-29 14:31:47 -08:00
|
|
|
if (auto *symbol{DeclareLocalEntity(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
symbol->set(Symbol::Flag::LocalityLocal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::LocalInit &x) {
|
|
|
|
for (auto &name : x.v) {
|
2019-01-29 14:31:47 -08:00
|
|
|
if (auto *symbol{DeclareLocalEntity(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
symbol->set(Symbol::Flag::LocalityLocalInit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
|
|
|
|
for (auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *prev{FindSymbol(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
if (prev->owner() == currScope()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayAlreadyDeclared(name, *prev);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
auto &symbol{MakeSymbol(name, HostAssocDetails{*prev})};
|
|
|
|
symbol.set(Symbol::Flag::LocalityShared);
|
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name, "Variable '%s' not found"_err_en_US);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-22 16:30:32 -08:00
|
|
|
bool ConstructVisitor::Pre(const parser::AcSpec &x) {
|
2019-02-24 10:05:43 -08:00
|
|
|
ProcessTypeSpec(x.type);
|
2019-01-22 16:30:32 -08:00
|
|
|
PushScope(Scope::Kind::ImpliedDos, nullptr);
|
|
|
|
Walk(x.values);
|
|
|
|
PopScope();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::AcImpliedDo &x) {
|
|
|
|
auto &values{std::get<std::list<parser::AcValue>>(x.t)};
|
|
|
|
auto &control{std::get<parser::AcImpliedDoControl>(x.t)};
|
|
|
|
auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(control.t)};
|
|
|
|
auto &bounds{std::get<parser::LoopBounds<parser::ScalarIntExpr>>(control.t)};
|
2019-01-29 14:31:47 -08:00
|
|
|
DeclareStatementEntity(bounds.name.thing.thing, type);
|
2019-01-22 16:30:32 -08:00
|
|
|
Walk(bounds);
|
|
|
|
Walk(values);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
|
|
|
|
auto &objects{std::get<std::list<parser::DataIDoObject>>(x.t)};
|
|
|
|
auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(x.t)};
|
|
|
|
auto &bounds{
|
|
|
|
std::get<parser::LoopBounds<parser::ScalarIntConstantExpr>>(x.t)};
|
2019-01-29 14:31:47 -08:00
|
|
|
DeclareStatementEntity(bounds.name.thing.thing, type);
|
2018-10-18 07:55:48 -07:00
|
|
|
Walk(bounds);
|
|
|
|
Walk(objects);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-06 17:18:02 -08:00
|
|
|
bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const common::Indirection<parser::Variable> &y) { Walk(*y); },
|
|
|
|
[&](const parser::DataImpliedDo &y) {
|
|
|
|
PushScope(Scope::Kind::ImpliedDos, nullptr);
|
|
|
|
Walk(y);
|
|
|
|
PopScope();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
return false;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::DoConstruct &x) {
|
|
|
|
if (x.IsDoConcurrent()) {
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::DoConstruct &x) {
|
|
|
|
if (x.IsDoConcurrent()) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::ConcurrentControl &x) {
|
2019-01-29 14:31:47 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
|
|
|
auto *prev{FindSymbol(name)};
|
|
|
|
if (prev) {
|
|
|
|
if (prev->owner().kind() == Scope::Kind::Forall ||
|
|
|
|
prev->owner() == currScope()) {
|
|
|
|
SayAlreadyDeclared(name, *prev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
name.symbol = nullptr;
|
|
|
|
}
|
|
|
|
auto &symbol{DeclareObjectEntity(name, {})};
|
|
|
|
if (symbol.GetType()) {
|
|
|
|
// type came from explicit type-spec
|
|
|
|
} else if (!prev) {
|
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
} else if (!prev->has<ObjectEntityDetails>() && !prev->has<EntityDetails>()) {
|
|
|
|
Say2(name, "Index name '%s' conflicts with existing identifier"_err_en_US,
|
|
|
|
*prev, "Previous declaration of '%s'"_en_US);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (auto *type{prev->GetType()}) {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
2019-02-07 12:25:59 -08:00
|
|
|
if (prev->IsObjectArray()) {
|
2019-02-06 10:28:31 -08:00
|
|
|
SayWithDecl(name, *prev, "Index variable '%s' is not scalar"_err_en_US);
|
|
|
|
return;
|
2019-01-29 14:31:47 -08:00
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
2019-01-29 14:31:47 -08:00
|
|
|
CheckScalarIntegerType(name);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::ForallConstruct &) {
|
2018-12-11 14:03:55 -08:00
|
|
|
PushScope(Scope::Kind::Forall, nullptr);
|
2018-10-18 07:55:48 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::ForallConstruct &) { PopScope(); }
|
|
|
|
bool ConstructVisitor::Pre(const parser::ForallStmt &) {
|
2018-12-11 14:03:55 -08:00
|
|
|
PushScope(Scope::Kind::Forall, nullptr);
|
2018-10-18 07:55:48 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::ForallStmt &) { PopScope(); }
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::BlockStmt &x) {
|
|
|
|
CheckDef(x.v);
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ConstructVisitor::Pre(const parser::EndBlockStmt &x) {
|
|
|
|
PopScope();
|
|
|
|
CheckRef(x.v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
void ConstructVisitor::Post(const parser::Selector &x) {
|
|
|
|
association_ = {};
|
|
|
|
const parser::Name *variable{nullptr};
|
|
|
|
MaybeExpr expr{std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Expr &y) { return EvaluateExpr(y); },
|
|
|
|
[&](const parser::Variable &y) {
|
|
|
|
if (const auto *des{
|
|
|
|
std::get_if<Indirection<parser::Designator>>(&y.u)}) {
|
|
|
|
if (const auto *dr{std::get_if<parser::DataRef>(&(*des)->u)}) {
|
|
|
|
variable = std::get_if<parser::Name>(&dr->u);
|
|
|
|
if (variable && !FindSymbol(*variable)) {
|
|
|
|
variable = nullptr;
|
|
|
|
return MaybeExpr{};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 17:18:10 -08:00
|
|
|
return EvaluateExpr(y);
|
2019-01-15 16:59:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u)};
|
|
|
|
if (expr) {
|
|
|
|
association_.expr = std::move(expr);
|
|
|
|
association_.variable = variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::AssociateStmt &x) {
|
|
|
|
CheckDef(x.t);
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::EndAssociateStmt &x) {
|
|
|
|
PopScope();
|
|
|
|
CheckRef(x.v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::Association &x) {
|
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
|
|
|
association_.name = &name;
|
|
|
|
if (auto *symbol{MakeAssocEntity()}) {
|
|
|
|
SetTypeFromAssociation(*symbol);
|
|
|
|
SetAttrsFromAssociation(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::SelectTypeStmt &x) {
|
|
|
|
if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
|
|
|
|
// This isn't a name in the current scope, it is in each TypeGuardStmt
|
|
|
|
MakePlaceholder(*name, MiscDetails::Kind::SelectTypeAssociateName);
|
|
|
|
association_.name = &*name;
|
|
|
|
} else if (!association_.variable) {
|
|
|
|
Say("Selector is not a named variable: 'associate-name =>' is required"_err_en_US);
|
|
|
|
association_ = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::SelectTypeConstruct::TypeCase &) {
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::SelectTypeConstruct::TypeCase &) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::TypeGuardStmt::Guard &x) {
|
|
|
|
if (auto *symbol{MakeAssocEntity()}) {
|
|
|
|
if (std::holds_alternative<parser::Default>(x.u)) {
|
|
|
|
SetTypeFromAssociation(*symbol);
|
|
|
|
} else if (const auto *type{GetDeclTypeSpec()}) {
|
|
|
|
symbol->SetType(*type);
|
|
|
|
}
|
|
|
|
SetAttrsFromAssociation(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool ConstructVisitor::CheckDef(const std::optional<parser::Name> &x) {
|
2018-09-25 08:53:53 -07:00
|
|
|
if (x) {
|
|
|
|
MakeSymbol(*x, MiscDetails{MiscDetails::Kind::ConstructName});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
void ConstructVisitor::CheckRef(const std::optional<parser::Name> &x) {
|
2018-09-25 08:53:53 -07:00
|
|
|
if (x) {
|
|
|
|
// Just add an occurrence of this name; checking is done in ValidateLabels
|
2018-11-16 12:43:08 -08:00
|
|
|
FindSymbol(*x);
|
2018-09-25 08:53:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
// Make a symbol representing an associating entity from association_.
|
|
|
|
Symbol *ConstructVisitor::MakeAssocEntity() {
|
|
|
|
if (!association_.name) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto &symbol{MakeSymbol(*association_.name, UnknownDetails{})};
|
|
|
|
if (symbol.has<AssocEntityDetails>() && symbol.owner() == currScope()) {
|
|
|
|
Say(*association_.name, // C1104
|
|
|
|
"The associate name '%s' is already used in this associate statement"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto &expr{association_.expr}) {
|
|
|
|
symbol.set_details(AssocEntityDetails{std::move(*expr)});
|
2019-01-17 16:14:36 -08:00
|
|
|
association_.expr.reset();
|
|
|
|
} else {
|
|
|
|
symbol.set_details(AssocEntityDetails{});
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
return &symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the type of symbol based on the current association variable or expr.
|
|
|
|
void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
|
|
|
|
if (association_.variable) {
|
|
|
|
if (const Symbol * varSymbol{association_.variable->symbol}) {
|
|
|
|
if (const DeclTypeSpec * type{varSymbol->GetType()}) {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 16:14:36 -08:00
|
|
|
} else {
|
|
|
|
auto &details{symbol.get<AssocEntityDetails>()};
|
|
|
|
if (const MaybeExpr & expr{details.expr()}) {
|
|
|
|
if (std::optional<evaluate::DynamicType> type{expr->GetType()}) {
|
|
|
|
if (const auto *charExpr{
|
|
|
|
evaluate::UnwrapExpr<evaluate::Expr<evaluate::SomeCharacter>>(
|
|
|
|
*expr)}) {
|
|
|
|
symbol.SetType(ToDeclTypeSpec(std::move(*type),
|
|
|
|
FoldExpr(std::visit(
|
|
|
|
[](const auto &kindChar) { return kindChar.LEN(); },
|
|
|
|
charExpr->u))));
|
|
|
|
} else {
|
|
|
|
symbol.SetType(ToDeclTypeSpec(std::move(*type)));
|
|
|
|
}
|
2019-01-17 13:52:10 -08:00
|
|
|
} else {
|
2019-01-17 16:14:36 -08:00
|
|
|
// BOZ literal not acceptable
|
|
|
|
Say(symbol.name(), "Associate name '%s' must have a type"_err_en_US);
|
2019-01-17 13:52:10 -08:00
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If current selector is a variable, set some of its attributes on symbol.
|
|
|
|
void ConstructVisitor::SetAttrsFromAssociation(Symbol &symbol) {
|
|
|
|
if (association_.variable) {
|
|
|
|
if (const auto *varSymbol{association_.variable->symbol}) {
|
|
|
|
symbol.attrs() |= varSymbol->attrs() &
|
|
|
|
Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE,
|
|
|
|
Attr::CONTIGUOUS};
|
|
|
|
if (varSymbol->attrs().test(Attr::POINTER)) {
|
|
|
|
symbol.attrs().set(Attr::TARGET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
|
2019-01-17 13:52:10 -08:00
|
|
|
evaluate::DynamicType &&type) {
|
2019-01-15 16:59:20 -08:00
|
|
|
switch (type.category) {
|
|
|
|
case common::TypeCategory::Integer:
|
|
|
|
case common::TypeCategory::Real:
|
|
|
|
case common::TypeCategory::Complex:
|
|
|
|
return context().MakeNumericType(type.category, type.kind);
|
|
|
|
case common::TypeCategory::Logical:
|
|
|
|
return context().MakeLogicalType(type.kind);
|
|
|
|
case common::TypeCategory::Derived:
|
2019-01-17 13:52:10 -08:00
|
|
|
CHECK(type.derived != nullptr);
|
2019-01-15 16:59:20 -08:00
|
|
|
return currScope().MakeDerivedType(
|
2019-01-17 13:52:10 -08:00
|
|
|
DeclTypeSpec::TypeDerived, DerivedTypeSpec{*type.derived});
|
|
|
|
case common::TypeCategory::Character:
|
2019-01-15 16:59:20 -08:00
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
|
|
|
|
evaluate::DynamicType &&type, SubscriptIntExpr &&length) {
|
|
|
|
CHECK(type.category == common::TypeCategory::Character);
|
|
|
|
return currScope().MakeCharacterType(
|
|
|
|
ParamValue{SomeIntExpr{std::move(length)}}, KindExpr{type.kind});
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
// ResolveNamesVisitor implementation
|
|
|
|
|
2019-02-21 15:01:39 -08:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::FunctionReference &x) {
|
|
|
|
HandleCall(Symbol::Flag::Function, x.v);
|
|
|
|
return false;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2019-02-21 15:01:39 -08:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::CallStmt &x) {
|
|
|
|
HandleCall(Symbol::Flag::Subroutine, x.v);
|
|
|
|
return false;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
|
2018-08-22 16:56:57 -07:00
|
|
|
auto &scope{currScope()};
|
2018-08-23 11:24:12 -07:00
|
|
|
// Check C896 and C899: where IMPORT statements are allowed
|
2018-08-22 16:05:06 -07:00
|
|
|
switch (scope.kind()) {
|
|
|
|
case Scope::Kind::Module:
|
2018-09-07 09:06:27 -07:00
|
|
|
if (scope.IsModule()) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say("IMPORT is not allowed in a module scoping unit"_err_en_US);
|
|
|
|
return false;
|
2018-08-23 11:45:49 -07:00
|
|
|
} else if (x.kind == common::ImportKind::None) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say("IMPORT,NONE is not allowed in a submodule scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Scope::Kind::MainProgram:
|
|
|
|
Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
case Scope::Kind::Subprogram:
|
|
|
|
if (scope.parent().kind() == Scope::Kind::Global) {
|
|
|
|
Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
2018-08-23 11:45:49 -07:00
|
|
|
if (auto error{scope.SetImportKind(x.kind)}) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say(std::move(*error));
|
|
|
|
}
|
|
|
|
for (auto &name : x.names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (FindSymbol(scope.parent(), name)) {
|
|
|
|
scope.add_importName(name.source);
|
|
|
|
} else {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say(name, "'%s' not found in host scope"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevImportStmt_ = currStmtSource();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveStructureComponent(
|
2018-06-22 08:21:19 -07:00
|
|
|
const parser::StructureComponent &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return FindComponent(ResolveDataRef(x.base), x.component);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveArrayElement(
|
2018-08-29 11:38:12 -07:00
|
|
|
const parser::ArrayElement &x) {
|
2018-10-10 16:20:46 -07:00
|
|
|
// TODO: need to resolve these
|
|
|
|
// for (auto &subscript : x.subscripts) {
|
|
|
|
// ResolveSectionSubscript(subscript);
|
|
|
|
//}
|
2018-08-29 11:38:12 -07:00
|
|
|
return ResolveDataRef(x.base);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveCoindexedNamedObject(
|
2018-08-29 11:38:12 -07:00
|
|
|
const parser::CoindexedNamedObject &x) {
|
|
|
|
return nullptr; // TODO
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveDataRef(
|
|
|
|
const parser::DataRef &x) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[=](const parser::Name &y) { return ResolveName(y); },
|
2019-01-15 16:59:20 -08:00
|
|
|
[=](const Indirection<parser::StructureComponent> &y) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return ResolveStructureComponent(*y);
|
|
|
|
},
|
2019-01-15 16:59:20 -08:00
|
|
|
[=](const Indirection<parser::ArrayElement> &y) {
|
2018-08-29 11:38:12 -07:00
|
|
|
return ResolveArrayElement(*y);
|
|
|
|
},
|
2019-01-15 16:59:20 -08:00
|
|
|
[=](const Indirection<parser::CoindexedNamedObject> &y) {
|
2018-08-29 11:38:12 -07:00
|
|
|
return ResolveCoindexedNamedObject(*y);
|
2018-06-22 08:21:19 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
// If implicit types are allowed, ensure name is in the symbol table.
|
|
|
|
// Otherwise, report an error if it hasn't been declared.
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveName(const parser::Name &name) {
|
|
|
|
if (FindSymbol(name)) {
|
|
|
|
if (CheckUseError(name)) {
|
2018-10-10 16:20:46 -07:00
|
|
|
return nullptr; // reported an error
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
return &name;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Create the symbol then ensure it is accessible
|
2018-11-16 12:43:08 -08:00
|
|
|
MakeSymbol(InclusiveScope(), name.source, Attrs{});
|
2018-10-10 16:20:46 -07:00
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name,
|
|
|
|
"'%s' from host scoping unit is not accessible due to IMPORT"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ApplyImplicitRules(*symbol);
|
2018-11-16 12:43:08 -08:00
|
|
|
return &name;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// base is a part-ref of a derived type; find the named component in its type.
|
2018-12-04 10:55:32 -08:00
|
|
|
// Also handles intrinsic type parameter inquiries (%kind, %len) and
|
|
|
|
// COMPLEX component references (%re, %im).
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::FindComponent(
|
|
|
|
const parser::Name *base, const parser::Name &component) {
|
|
|
|
if (!base || !base->symbol) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &symbol{*base->symbol};
|
2019-01-15 16:59:20 -08:00
|
|
|
if (!symbol.has<AssocEntityDetails>() && !ConvertToObjectEntity(symbol)) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(*base, symbol,
|
|
|
|
"'%s' is an invalid base for a component reference"_err_en_US);
|
2018-11-16 12:43:08 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto *type{symbol.GetType()};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (!type) {
|
|
|
|
return nullptr; // should have already reported error
|
|
|
|
}
|
2019-01-07 13:31:50 -08:00
|
|
|
if (const IntrinsicTypeSpec * intrinsic{type->AsIntrinsic()}) {
|
2018-09-24 07:12:38 -07:00
|
|
|
auto name{component.ToString()};
|
2019-01-07 13:31:50 -08:00
|
|
|
auto category{intrinsic->category()};
|
|
|
|
MiscDetails::Kind miscKind{MiscDetails::Kind::None};
|
|
|
|
if (name == "kind") {
|
|
|
|
miscKind = MiscDetails::Kind::KindParamInquiry;
|
|
|
|
} else if (category == TypeCategory::Character) {
|
|
|
|
if (name == "len") {
|
|
|
|
miscKind = MiscDetails::Kind::LenParamInquiry;
|
|
|
|
}
|
|
|
|
} else if (category == TypeCategory::Complex) {
|
|
|
|
if (name == "re") {
|
|
|
|
miscKind = MiscDetails::Kind::ComplexPartRe;
|
|
|
|
} else if (name == "im") {
|
|
|
|
miscKind = MiscDetails::Kind::ComplexPartIm;
|
|
|
|
}
|
2018-09-24 07:12:38 -07:00
|
|
|
}
|
2019-01-07 13:31:50 -08:00
|
|
|
if (miscKind != MiscDetails::Kind::None) {
|
|
|
|
MakePlaceholder(component, miscKind);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
|
2018-12-04 10:55:32 -08:00
|
|
|
if (const Scope * scope{derived->scope()}) {
|
2019-02-12 17:24:43 -08:00
|
|
|
if (Resolve(component, FindInTypeOrParents(*scope, component.source))) {
|
|
|
|
if (CheckAccessibleComponent(component.source, *component.symbol)) {
|
2018-12-04 10:55:32 -08:00
|
|
|
return &component;
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-07 13:31:50 -08:00
|
|
|
SayDerivedType(component.source,
|
|
|
|
"Component '%s' not found in derived type '%s'"_err_en_US, *scope);
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
return nullptr;
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
2019-01-07 13:31:50 -08:00
|
|
|
if (symbol.test(Symbol::Flag::Implicit)) {
|
|
|
|
Say(*base,
|
|
|
|
"'%s' is not an object of derived type; it is implicitly typed"_err_en_US);
|
2018-11-16 12:43:08 -08:00
|
|
|
} else {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
*base, symbol, "'%s' is not an object of derived type"_err_en_US);
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
2019-01-07 13:31:50 -08:00
|
|
|
return nullptr;
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:01:39 -08:00
|
|
|
void ResolveNamesVisitor::HandleCall(
|
|
|
|
Symbol::Flag procFlag, const parser::Call &call) {
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Name &x) { HandleProcedureName(procFlag, x); },
|
|
|
|
[&](const parser::ProcComponentRef &x) { Walk(x); },
|
|
|
|
},
|
|
|
|
std::get<parser::ProcedureDesignator>(call.t).u);
|
|
|
|
Walk(std::get<std::list<parser::ActualArgSpec>>(call.t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::HandleProcedureName(
|
|
|
|
Symbol::Flag flag, const parser::Name &name) {
|
|
|
|
CHECK(flag == Symbol::Flag::Function || flag == Symbol::Flag::Subroutine);
|
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (symbol == nullptr) {
|
|
|
|
symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
|
|
|
|
Resolve(name, *symbol);
|
|
|
|
if (symbol->has<ModuleDetails>()) {
|
|
|
|
SayWithDecl(name, *symbol,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
|
|
|
if (!symbol->has<ProcEntityDetails>()) {
|
2018-09-22 08:05:46 -07:00
|
|
|
ConvertToProcEntity(*symbol);
|
2019-02-21 15:01:39 -08:00
|
|
|
}
|
|
|
|
if (const auto type{GetImplicitType(*symbol)}) {
|
|
|
|
symbol->get<ProcEntityDetails>().interface().set_type(*type);
|
|
|
|
}
|
|
|
|
SetProcFlag(name, *symbol, flag);
|
|
|
|
} else if (symbol->has<UnknownDetails>()) {
|
|
|
|
CHECK(!"unexpected UnknownDetails");
|
|
|
|
} else if (CheckUseError(name)) {
|
|
|
|
// error was reported
|
|
|
|
} else {
|
|
|
|
symbol = Resolve(name, &symbol->GetUltimate());
|
|
|
|
ConvertToProcEntity(*symbol);
|
|
|
|
if (!SetProcFlag(name, *symbol, flag)) {
|
|
|
|
return; // reported error
|
|
|
|
}
|
|
|
|
if (symbol->has<SubprogramNameDetails>() || symbol->has<GenericDetails>() ||
|
|
|
|
symbol->has<DerivedTypeDetails>() || symbol->has<SubprogramDetails>() ||
|
|
|
|
symbol->has<ProcEntityDetails>() ||
|
|
|
|
symbol->has<ObjectEntityDetails>()) {
|
|
|
|
// these are all valid as procedure-designators
|
|
|
|
} else if (symbol->test(Symbol::Flag::Implicit)) {
|
|
|
|
Say(name,
|
|
|
|
"Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
|
|
|
|
} else {
|
|
|
|
SayWithDecl(name, *symbol,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
|
2018-05-30 14:11:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:35:31 -08:00
|
|
|
// Check and set the Function or Subroutine flag on symbol; false on error.
|
|
|
|
bool ResolveNamesVisitor::SetProcFlag(
|
2019-02-21 15:01:39 -08:00
|
|
|
const parser::Name &name, Symbol &symbol, Symbol::Flag flag) {
|
|
|
|
if (symbol.test(Symbol::Flag::Function) && flag == Symbol::Flag::Subroutine) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
|
2018-12-18 16:35:31 -08:00
|
|
|
return false;
|
|
|
|
} else if (symbol.test(Symbol::Flag::Subroutine) &&
|
2019-02-21 15:01:39 -08:00
|
|
|
flag == Symbol::Flag::Function) {
|
2019-02-05 13:51:36 -08:00
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "Cannot call subroutine '%s' like a function"_err_en_US);
|
2018-12-18 16:35:31 -08:00
|
|
|
return false;
|
|
|
|
} else if (symbol.has<ProcEntityDetails>()) {
|
2019-02-21 15:01:39 -08:00
|
|
|
symbol.set(flag); // in case it hasn't been set yet
|
|
|
|
if (flag == Symbol::Flag::Function) {
|
2018-12-18 16:35:31 -08:00
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
|
2018-08-28 14:02:53 -07:00
|
|
|
Attr accessAttr{AccessSpecToAttr(std::get<parser::AccessSpec>(x.t))};
|
2018-08-22 16:56:57 -07:00
|
|
|
if (currScope().kind() != Scope::Kind::Module) {
|
2018-05-03 15:57:56 -07:00
|
|
|
Say(*currStmtSource(),
|
|
|
|
"%s statement may only appear in the specification part of a module"_err_en_US,
|
|
|
|
EnumToString(accessAttr));
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &accessIds{std::get<std::list<parser::AccessId>>(x.t)};
|
2018-04-24 17:05:58 -07:00
|
|
|
if (accessIds.empty()) {
|
|
|
|
if (prevAccessStmt_) {
|
2018-05-04 15:40:40 -07:00
|
|
|
Say("The default accessibility of this module has already been declared"_err_en_US)
|
|
|
|
.Attach(*prevAccessStmt_, "Previous declaration"_en_US);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
prevAccessStmt_ = currStmtSource();
|
|
|
|
defaultAccess_ = accessAttr;
|
|
|
|
} else {
|
|
|
|
for (const auto &accessId : accessIds) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-24 17:05:58 -07:00
|
|
|
[=](const parser::Name &y) { SetAccess(y, accessAttr); },
|
2019-01-15 16:59:20 -08:00
|
|
|
[=](const Indirection<parser::GenericSpec> &y) {
|
2018-04-24 17:05:58 -07:00
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-24 17:05:58 -07:00
|
|
|
[=](const parser::Name &z) {
|
|
|
|
SetAccess(z, accessAttr);
|
|
|
|
},
|
2018-06-18 11:03:43 -07:00
|
|
|
[](const auto &) { common::die("TODO: GenericSpec"); },
|
2018-04-24 17:05:58 -07:00
|
|
|
},
|
|
|
|
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-11-16 12:43:08 -08: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;
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name,
|
2018-05-03 15:57:56 -07:00
|
|
|
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,
|
|
|
|
name.source, EnumToString(prev));
|
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-06-06 11:41:42 -07:00
|
|
|
static bool NeedsExplicitType(const Symbol &symbol) {
|
2018-06-05 12:18:35 -07:00
|
|
|
if (symbol.has<UnknownDetails>()) {
|
2018-06-06 11:41:42 -07:00
|
|
|
return true;
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return !details->type();
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return !details->type();
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
2018-06-06 11:41:42 -07:00
|
|
|
return details->interface().symbol() == nullptr &&
|
|
|
|
details->interface().type() == nullptr;
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-06-06 11:41:42 -07:00
|
|
|
return false;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::SpecificationPart &) {
|
2018-04-18 15:06:35 -07:00
|
|
|
badStmtFuncFound_ = false;
|
2018-08-22 16:05:06 -07:00
|
|
|
CheckImports();
|
2019-02-24 10:05:43 -08:00
|
|
|
HandleFunctionPrefixType();
|
2018-09-22 08:05:46 -07:00
|
|
|
bool inModule{currScope().kind() == Scope::Kind::Module};
|
2018-08-22 16:56:57 -07:00
|
|
|
for (auto &pair : currScope()) {
|
2018-07-05 10:28:34 -07:00
|
|
|
auto &symbol{*pair.second};
|
2018-07-16 16:23:18 -07:00
|
|
|
if (NeedsExplicitType(symbol)) {
|
2018-10-18 07:55:48 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
2018-07-05 10:28:34 -07:00
|
|
|
}
|
|
|
|
if (symbol.has<GenericDetails>()) {
|
|
|
|
CheckGenericProcedures(symbol);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
if (inModule && symbol.attrs().test(Attr::EXTERNAL) &&
|
|
|
|
!symbol.test(Symbol::Flag::Function)) {
|
|
|
|
// in a module, external proc without return type is subroutine
|
|
|
|
symbol.set(Symbol::Flag::Subroutine);
|
|
|
|
}
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2019-02-20 17:45:39 -08:00
|
|
|
CheckSaveStmts();
|
2019-02-14 07:59:20 -08:00
|
|
|
CheckCommonBlocks();
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
void ResolveNamesVisitor::CheckImports() {
|
2018-08-22 16:56:57 -07:00
|
|
|
auto &scope{currScope()};
|
2018-08-23 11:45:49 -07:00
|
|
|
switch (scope.GetImportKind()) {
|
|
|
|
case common::ImportKind::None: break;
|
|
|
|
case common::ImportKind::All:
|
2018-08-22 16:05:06 -07:00
|
|
|
// C8102: all entities in host must not be hidden
|
|
|
|
for (const auto &pair : scope.parent()) {
|
|
|
|
auto &name{pair.first};
|
|
|
|
if (name != scope.name()) {
|
|
|
|
CheckImport(*prevImportStmt_, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-08-23 11:45:49 -07:00
|
|
|
case common::ImportKind::Default:
|
|
|
|
case common::ImportKind::Only:
|
2018-08-22 16:05:06 -07:00
|
|
|
// C8102: entities named in IMPORT must not be hidden
|
|
|
|
for (auto &name : scope.importNames()) {
|
|
|
|
CheckImport(name, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::CheckImport(
|
|
|
|
const SourceName &location, const SourceName &name) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindInScope(currScope(), name)}) {
|
|
|
|
Say(location, "'%s' from host is not accessible"_err_en_US, name)
|
|
|
|
.Attach(symbol->name(), "'%s' is hidden by this entity"_en_US,
|
|
|
|
symbol->name().ToString().c_str());
|
2018-08-22 16:05:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::MainProgram &x) {
|
|
|
|
using stmtType = std::optional<parser::Statement<parser::ProgramStmt>>;
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *symbol{nullptr};
|
2018-08-08 17:28:16 -07:00
|
|
|
if (auto &stmt{std::get<stmtType>(x.t)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol = &MakeSymbol(stmt->statement.v, MainProgramDetails{});
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
PushScope(Scope::Kind::MainProgram, symbol);
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
|
|
|
WalkSubprogramPart(subpPart);
|
2018-04-11 13:11:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:57:56 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::EndProgramStmt &) { PopScope(); }
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2019-02-21 19:14:28 -08:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ImplicitStmt &x) {
|
|
|
|
return CheckNotInBlock("IMPLICIT") && ImplicitRulesVisitor::Pre(x);
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::PointerObject &x) {
|
|
|
|
std::visit(
|
2018-08-29 11:38:12 -07:00
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::StructureComponent &x) {
|
|
|
|
ResolveStructureComponent(x);
|
2018-08-29 11:38:12 -07:00
|
|
|
},
|
2018-10-10 16:20:46 -07:00
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::StructureComponent &x) {
|
|
|
|
ResolveStructureComponent(x);
|
2018-08-29 11:38:12 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2019-02-26 14:26:28 -08:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::PointerAssignmentStmt &x) {
|
|
|
|
// Resolve unrestricted specific intrinsic procedures as in "p => cos".
|
|
|
|
const auto &expr{std::get<parser::Expr>(x.t)};
|
|
|
|
if (const auto *designator{
|
|
|
|
std::get_if<common::Indirection<parser::Designator>>(&expr.u)}) {
|
|
|
|
if (const parser::Name *
|
|
|
|
name{std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[](const parser::ObjectName &n) { return &n; },
|
|
|
|
[](const parser::DataRef &dataRef) {
|
|
|
|
return std::get_if<parser::Name>(&dataRef.u);
|
|
|
|
},
|
|
|
|
[](const auto &) -> const parser::Name * { return nullptr; },
|
|
|
|
},
|
|
|
|
(*designator)->u)}) {
|
|
|
|
return !NameIsKnownOrIntrinsic(*name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::PointerAssignmentStmt &x) {
|
|
|
|
ResolveDataRef(std::get<parser::DataRef>(x.t));
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::Designator &x) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::ObjectName &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::DataRef &x) { ResolveDataRef(x); },
|
2018-09-10 12:20:42 -07:00
|
|
|
[&](const parser::Substring &x) {
|
2018-10-10 16:20:46 -07:00
|
|
|
ResolveDataRef(std::get<parser::DataRef>(x.t));
|
|
|
|
// TODO: SubstringRange
|
2018-04-24 17:05:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
2018-11-06 17:18:06 -08:00
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
template<typename T>
|
|
|
|
void ResolveNamesVisitor::Post(const parser::LoopBounds<T> &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
ResolveName(x.name.thing.thing);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::ProcComponentRef &x) {
|
|
|
|
ResolveStructureComponent(x.v.thing);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::TypeGuardStmt &x) {
|
|
|
|
DeclTypeSpecVisitor::Post(x);
|
2018-10-18 07:55:48 -07:00
|
|
|
ConstructVisitor::Post(x);
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
|
|
|
|
if (!HandleStmtFunction(x)) {
|
|
|
|
// This is an array element assignment: resolve names of indices
|
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
for (auto &name : names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
ResolveName(name);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 17:05:58 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::Program &) {
|
|
|
|
// ensure that all temps were deallocated
|
|
|
|
CHECK(!attrs_);
|
2018-06-05 12:18:35 -07:00
|
|
|
CHECK(!GetDeclTypeSpec());
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
void ResolveNames(SemanticsContext &context, const parser::Program &program) {
|
|
|
|
ResolveNamesVisitor{context}.Walk(program);
|
2018-04-18 15:06:35 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Get the Name out of a GenericSpec, or nullptr if none.
|
|
|
|
static const parser::Name *GetGenericSpecName(const parser::GenericSpec &x) {
|
|
|
|
const auto *op{std::get_if<parser::DefinedOperator>(&x.u)};
|
|
|
|
if (!op) {
|
|
|
|
return std::get_if<parser::Name>(&x.u);
|
|
|
|
} else if (const auto *opName{std::get_if<parser::DefinedOpName>(&op->u)}) {
|
|
|
|
return &opName->v;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|