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"
|
2019-06-21 16:13:56 -07:00
|
|
|
#include "assignment.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"
|
2019-05-06 10:12:27 -07:00
|
|
|
#include "program-tree.h"
|
2019-03-18 11:48:02 -07:00
|
|
|
#include "resolve-names-utils.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"
|
2019-04-12 16:30:03 -07:00
|
|
|
#include "tools.h"
|
2019-04-15 09:44:54 -07:00
|
|
|
#include "type.h"
|
2019-02-28 10:48:41 -08:00
|
|
|
#include "../common/Fortran.h"
|
2019-01-18 12:40:47 -08:00
|
|
|
#include "../common/default-kinds.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"
|
2019-07-02 14:00:44 -07:00
|
|
|
#include "../evaluate/characteristics.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"
|
2019-04-16 15:59:04 -07:00
|
|
|
#include "../parser/tools.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>
|
2019-06-14 15:04:13 -07:00
|
|
|
#include <stack>
|
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-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:
|
2019-05-02 11:19:01 -07:00
|
|
|
ImplicitRules(SemanticsContext &context, ImplicitRules *parent)
|
|
|
|
: parent_{parent}, context_{context} {
|
|
|
|
inheritFromParent_ = parent != nullptr;
|
2018-08-27 11:48:49 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-05-02 11:19:01 -07:00
|
|
|
ImplicitRules *parent_;
|
|
|
|
SemanticsContext &context_;
|
|
|
|
bool inheritFromParent_; // look in parent if not specified here
|
2018-08-27 11:48:49 -07:00
|
|
|
std::optional<bool> isImplicitNoneType_;
|
|
|
|
std::optional<bool> isImplicitNoneExternal_;
|
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:
|
2019-03-05 16:52:50 -08:00
|
|
|
Messages &messages() { return context_->messages(); };
|
|
|
|
void set_context(SemanticsContext &context) { context_ = &context; }
|
|
|
|
const SourceName *currStmtSource() { return context_->location(); }
|
|
|
|
void set_currStmtSource(const SourceName *source) {
|
|
|
|
context_->set_location(source);
|
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
|
|
|
|
// 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.
|
2019-06-23 10:59:32 -07:00
|
|
|
template<typename... A>
|
|
|
|
Message &Say(const SourceName &source, MessageFixedText &&msg, A &&... args) {
|
|
|
|
return context_->Say(source, std::move(msg), std::forward<A>(args)...);
|
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
|
|
|
|
private:
|
2019-03-05 16:52:50 -08:00
|
|
|
SemanticsContext *context_{nullptr};
|
2018-11-16 16:40:00 -08:00
|
|
|
};
|
|
|
|
|
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_; }
|
2019-03-05 16:52:50 -08:00
|
|
|
const SourceName *currStmtSource() { return context_->location(); }
|
2018-11-16 16:40:00 -08:00
|
|
|
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-04-09 13:29:40 -07:00
|
|
|
template<typename T> common::IfNoLvalue<T, T> FoldExpr(T &&expr) {
|
2019-01-17 16:14:36 -08:00
|
|
|
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-05-13 09:33:18 -07:00
|
|
|
template<typename T>
|
|
|
|
MaybeExpr EvaluateConvertedExpr(
|
|
|
|
const Symbol &symbol, const T &expr, parser::CharBlock source) {
|
|
|
|
if (auto maybeExpr{AnalyzeExpr(*context_, expr)}) {
|
|
|
|
if (auto converted{
|
|
|
|
evaluate::ConvertToType(symbol, std::move(*maybeExpr))}) {
|
2019-05-24 07:54:47 -07:00
|
|
|
return FoldExpr(std::move(*converted));
|
2019-05-13 09:33:18 -07:00
|
|
|
} else {
|
|
|
|
Say(source,
|
|
|
|
"Initialization expression could not be converted to declared type of symbol '%s'"_err_en_US,
|
|
|
|
symbol.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
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)}) {
|
2019-05-13 09:33:18 -07:00
|
|
|
return std::move(*intExpr);
|
2019-01-07 15:05:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:16:55 -07:00
|
|
|
template<typename... A> Message &Say(A &&... args) {
|
|
|
|
return messageHandler_.Say(std::forward<A>(args)...);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
2019-04-08 16:16:55 -07:00
|
|
|
template<typename... A>
|
|
|
|
Message &Say(
|
|
|
|
const parser::Name &name, MessageFixedText &&text, const A &... args) {
|
|
|
|
return messageHandler_.Say(name.source, std::move(text), args...);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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="...")
|
2019-06-22 10:00:18 -07:00
|
|
|
const SourceName *passName_{nullptr}; // 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:
|
2019-05-02 11:19:01 -07:00
|
|
|
void BeginScope(const Scope &);
|
|
|
|
void SetScope(const Scope &);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
2019-05-02 11:19:01 -07:00
|
|
|
// scope -> implicit rules for that scope
|
|
|
|
std::map<const Scope *, ImplicitRules> implicitRulesMap_;
|
2018-04-04 09:03:51 -07:00
|
|
|
// implicit rules in effect for current scope
|
2019-05-02 11:19:01 -07:00
|
|
|
ImplicitRules *implicitRules_{nullptr};
|
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)
|
2019-04-01 13:08:57 -07:00
|
|
|
// 5. COMMON x(10)
|
2018-04-12 12:59:42 -07:00
|
|
|
// 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:
|
2019-04-04 14:46:40 -07:00
|
|
|
void Post(const parser::ArraySpec &);
|
2019-04-25 15:05:41 -07:00
|
|
|
void Post(const parser::ComponentArraySpec &);
|
2019-04-04 14:46:40 -07:00
|
|
|
void Post(const parser::CoarraySpec &);
|
2018-05-17 13:06:38 -07:00
|
|
|
void Post(const parser::AttrSpec &) { PostAttrSpec(); }
|
|
|
|
void Post(const parser::ComponentAttrSpec &) { PostAttrSpec(); }
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
protected:
|
|
|
|
const ArraySpec &arraySpec();
|
2019-04-04 14:46:40 -07:00
|
|
|
const ArraySpec &coarraySpec();
|
2018-05-17 13:06:38 -07:00
|
|
|
void BeginArraySpec();
|
|
|
|
void EndArraySpec();
|
|
|
|
void ClearArraySpec() { arraySpec_.clear(); }
|
2019-04-04 14:46:40 -07:00
|
|
|
void ClearCoarraySpec() { coarraySpec_.clear(); }
|
2018-05-17 13:06:38 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
private:
|
2019-04-04 14:46:40 -07:00
|
|
|
// arraySpec_/coarraySpec_ are populated from any ArraySpec/CoarraySpec
|
2018-04-12 12:59:42 -07:00
|
|
|
ArraySpec arraySpec_;
|
2019-04-04 14:46:40 -07:00
|
|
|
ArraySpec coarraySpec_;
|
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_;
|
2019-04-04 14:46:40 -07:00
|
|
|
ArraySpec attrCoarraySpec_;
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
void PostAttrSpec();
|
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();
|
2019-05-06 07:26:43 -07:00
|
|
|
void SetScope(Scope &);
|
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-04-25 13:18:33 -07:00
|
|
|
void SayAlreadyDeclared(const SourceName &, Symbol &);
|
|
|
|
void SayAlreadyDeclared(const parser::Name &, Symbol &);
|
|
|
|
void SayWithDecl(const parser::Name &, Symbol &, MessageFixedText &&);
|
2019-07-02 12:10:09 -07:00
|
|
|
void SayLocalMustBeVariable(const parser::Name &, Symbol &);
|
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 &&);
|
2019-04-25 13:18:33 -07:00
|
|
|
void Say2(
|
|
|
|
const SourceName &, MessageFixedText &&, Symbol &, MessageFixedText &&);
|
|
|
|
void Say2(
|
|
|
|
const parser::Name &, MessageFixedText &&, Symbol &, MessageFixedText &&);
|
2018-11-16 16:40:00 -08:00
|
|
|
|
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 &);
|
2019-03-18 11:48:02 -07:00
|
|
|
void EraseSymbol(const Symbol &symbol) { currScope().erase(symbol.name()); }
|
2018-11-16 12:43:08 -08:00
|
|
|
// Make a new symbol with the name and attrs of an existing one
|
2019-07-02 14:00:44 -07:00
|
|
|
Symbol &CopySymbol(const SourceName &, const Symbol &);
|
2018-11-16 12:43:08 -08:00
|
|
|
|
|
|
|
// Make symbols in the current or named scope
|
|
|
|
Symbol &MakeSymbol(Scope &, const SourceName &, Attrs);
|
2019-03-18 11:48:02 -07:00
|
|
|
Symbol &MakeSymbol(const SourceName &, Attrs = Attrs{});
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &MakeSymbol(const parser::Name &, Attrs = Attrs{});
|
|
|
|
|
2019-04-09 13:29:40 -07:00
|
|
|
template<typename D>
|
|
|
|
common::IfNoLvalue<Symbol &, D> MakeSymbol(
|
|
|
|
const parser::Name &name, D &&details) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return MakeSymbol(name, Attrs{}, std::move(details));
|
|
|
|
}
|
2018-05-22 16:12:56 -07:00
|
|
|
|
2019-04-09 13:29:40 -07:00
|
|
|
template<typename D>
|
|
|
|
common::IfNoLvalue<Symbol &, D> MakeSymbol(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, const Attrs &attrs, D &&details) {
|
2019-03-18 11:48:02 -07:00
|
|
|
return Resolve(name, MakeSymbol(name.source, attrs, std::move(details)));
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:29:40 -07:00
|
|
|
template<typename D>
|
|
|
|
common::IfNoLvalue<Symbol &, D> MakeSymbol(
|
|
|
|
const SourceName &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 =
|
2019-03-18 11:48:02 -07:00
|
|
|
&currScope().MakeSymbol(name, 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
|
2019-03-18 11:48:02 -07:00
|
|
|
EraseSymbol(*symbol);
|
2019-04-08 16:16:55 -07:00
|
|
|
return MakeSymbol(name, attrs, std::move(details));
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 11:41:30 -07:00
|
|
|
void MakeExternal(Symbol &);
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
protected:
|
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-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:
|
|
|
|
bool Pre(const parser::AccessStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::Only &);
|
|
|
|
bool Pre(const parser::Rename::Names &);
|
2019-03-18 11:48:02 -07:00
|
|
|
bool Pre(const parser::Rename::Operators &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::UseStmt &);
|
|
|
|
void Post(const parser::UseStmt &);
|
2018-05-03 15:57:56 -07:00
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
void BeginModule(const parser::Name &, bool isSubmodule);
|
|
|
|
bool BeginSubmodule(const parser::Name &, const parser::ParentIdentifier &);
|
|
|
|
void ApplyDefaultAccess();
|
|
|
|
|
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
|
|
|
|
2019-03-18 11:48:02 -07:00
|
|
|
Symbol &SetAccess(const SourceName &, Attr);
|
2018-05-14 14:26:39 -07:00
|
|
|
void AddUse(const parser::Rename::Names &);
|
2019-03-18 11:48:02 -07:00
|
|
|
void AddUse(const parser::Rename::Operators &);
|
|
|
|
Symbol *AddUse(const SourceName &);
|
|
|
|
// A rename in a USE statement: local => use
|
|
|
|
struct SymbolRename {
|
|
|
|
Symbol *local{nullptr};
|
|
|
|
Symbol *use{nullptr};
|
|
|
|
};
|
|
|
|
// Record a use from useModuleScope_ of use Name/Symbol as local Name/Symbol
|
|
|
|
SymbolRename AddUse(const SourceName &localName, const SourceName &useName);
|
|
|
|
void AddUse(const SourceName &, Symbol &localSymbol, const Symbol &useSymbol);
|
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 &);
|
2019-06-14 15:04:13 -07:00
|
|
|
bool Pre(const parser::GenericStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
void Post(const parser::GenericStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2019-06-14 15:04:13 -07:00
|
|
|
bool inInterfaceBlock() const;
|
|
|
|
bool isGeneric() const;
|
|
|
|
bool isAbstract() const;
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
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 &);
|
2019-07-02 14:00:44 -07:00
|
|
|
void CheckSpecificsAreDistinguishable(const Symbol &, const SymbolVector &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
private:
|
2019-06-14 15:04:13 -07:00
|
|
|
// A new GenericInfo is pushed for each interface block and generic stmt
|
|
|
|
struct GenericInfo {
|
|
|
|
GenericInfo(bool isInterface, bool isAbstract = false)
|
|
|
|
: isInterface{isInterface}, isAbstract{isAbstract} {}
|
|
|
|
bool isInterface; // in interface block
|
|
|
|
bool isAbstract; // in abstract interface block
|
|
|
|
Symbol *symbol{nullptr}; // the generic symbol being defined
|
|
|
|
};
|
|
|
|
std::stack<GenericInfo> genericInfo_;
|
|
|
|
const GenericInfo &GetGenericInfo() const { return genericInfo_.top(); }
|
|
|
|
void SetGenericSymbol(Symbol &symbol) { genericInfo_.top().symbol = &symbol; }
|
|
|
|
|
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);
|
2019-07-02 14:00:44 -07:00
|
|
|
void SayNotDistinguishable(const Symbol &, const Symbol &, const Symbol &);
|
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::InterfaceBody::Subroutine &);
|
|
|
|
void Post(const parser::InterfaceBody::Subroutine &);
|
|
|
|
bool Pre(const parser::InterfaceBody::Function &);
|
|
|
|
void Post(const parser::InterfaceBody::Function &);
|
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 &);
|
2019-02-28 09:53:49 -08:00
|
|
|
void Post(const parser::ImplicitPart &);
|
2018-05-04 15:37:56 -07:00
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
bool BeginSubprogram(
|
|
|
|
const parser::Name &, Symbol::Flag, bool hasModulePrefix = false);
|
|
|
|
void EndSubprogram();
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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;
|
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 &);
|
2019-04-04 14:46:40 -07:00
|
|
|
void Post(const parser::CodimensionDecl &);
|
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 &);
|
|
|
|
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 &);
|
|
|
|
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-06-11 18:26:48 -07:00
|
|
|
bool Pre(const parser::EquivalenceStmt &);
|
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);
|
2019-07-03 17:14:12 -07:00
|
|
|
// Make sure that there's an entity in an enclosing scope called Name
|
2019-07-08 15:35:24 -07:00
|
|
|
Symbol &FindOrDeclareEnclosingEntity(const parser::Name &);
|
2019-01-29 14:31:47 -08:00
|
|
|
// 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 &);
|
2019-04-25 13:18:33 -07:00
|
|
|
void CheckAccessibility(const SourceName &, bool, Symbol &);
|
2019-02-12 17:24:43 -08:00
|
|
|
bool CheckAccessibleComponent(const SourceName &, const Symbol &);
|
2019-02-14 07:59:20 -08:00
|
|
|
void CheckCommonBlocks();
|
2019-02-20 17:45:39 -08:00
|
|
|
void CheckSaveStmts();
|
2019-06-11 18:26:48 -07:00
|
|
|
void CheckEquivalenceSets();
|
2019-02-21 19:14:28 -08:00
|
|
|
bool CheckNotInBlock(const char *);
|
2019-07-16 09:56:41 -07:00
|
|
|
bool NameIsKnownOrIntrinsic(const parser::Name &);
|
2018-05-30 14:11:45 -07:00
|
|
|
|
2019-04-15 10:26:20 -07: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 *ResolveDesignator(const parser::Designator &);
|
|
|
|
const parser::Name *ResolveDataRef(const parser::DataRef &);
|
|
|
|
const parser::Name *ResolveVariable(const parser::Variable &);
|
|
|
|
const parser::Name *ResolveName(const parser::Name &);
|
2019-07-03 17:14:12 -07:00
|
|
|
bool PassesSharedLocalityChecks(const parser::Name &name, Symbol &symbol);
|
2019-07-15 17:11:54 -07:00
|
|
|
void CheckExplicitInterface(Symbol &);
|
2019-04-15 10:26:20 -07:00
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
private:
|
|
|
|
// The attribute corresponding to the statement containing an ObjectDecl
|
|
|
|
std::optional<Attr> objectDeclAttr_;
|
2019-07-08 16:02:50 -07:00
|
|
|
// Info about current character type while walking DeclTypeSpec.
|
|
|
|
// Also captures any "*length" specifier on an individual declaration.
|
2018-12-14 14:04:15 -08:00
|
|
|
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-06-11 18:26:48 -07:00
|
|
|
// Collect equivalence sets and process at end of specification part
|
|
|
|
std::vector<const std::list<parser::EquivalenceObject> *> equivalenceSets_;
|
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 &);
|
2019-03-18 11:48:02 -07:00
|
|
|
Symbol *MakeTypeSymbol(const SourceName &, Details &&);
|
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-07-16 09:56:41 -07:00
|
|
|
bool HandleUnrestrictedSpecificIntrinsicFunction(const parser::Name &);
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *FindComponent(const parser::Name *, const parser::Name &);
|
2019-06-27 17:16:29 -07:00
|
|
|
void CheckInitialDataTarget(const Symbol &, const SomeExpr &, SourceName);
|
2019-06-21 16:13:56 -07:00
|
|
|
void Initialization(const parser::Name &, const parser::Initialization &,
|
|
|
|
bool inComponentDecl);
|
2019-07-02 12:10:09 -07:00
|
|
|
bool PassesLocalityChecks(const parser::Name &name, Symbol &symbol);
|
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 &);
|
|
|
|
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 &);
|
|
|
|
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 &);
|
2019-04-15 10:26:20 -07:00
|
|
|
bool Pre(const parser::ChangeTeamStmt &);
|
|
|
|
void Post(const parser::EndChangeTeamStmt &);
|
|
|
|
void Post(const parser::CoarrayAssociation &);
|
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::CriticalStmt &x) { return CheckDef(x.t); }
|
2019-06-03 13:36:47 -07:00
|
|
|
bool Pre(const parser::LabelDoStmt &x) {
|
|
|
|
return false; // error recovery
|
|
|
|
}
|
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::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-04-15 10:26:20 -07:00
|
|
|
// R1105 selector -> expr | variable
|
|
|
|
// expr is set in either case unless there were errors
|
|
|
|
struct Selector {
|
2019-04-15 11:35:12 -07:00
|
|
|
Selector() {}
|
2019-06-06 14:34:54 -07:00
|
|
|
Selector(const parser::CharBlock &source, MaybeExpr &&expr)
|
|
|
|
: source{source}, expr{std::move(expr)} {}
|
2019-04-15 10:26:20 -07:00
|
|
|
operator bool() const { return expr.has_value(); }
|
|
|
|
parser::CharBlock source;
|
|
|
|
MaybeExpr expr;
|
|
|
|
};
|
|
|
|
// association -> [associate-name =>] selector
|
2019-01-15 16:59:20 -08:00
|
|
|
struct {
|
|
|
|
const parser::Name *name{nullptr};
|
2019-04-15 10:26:20 -07:00
|
|
|
Selector selector;
|
2019-01-15 16:59:20 -08:00
|
|
|
} 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(
|
2019-07-03 10:25:43 -07:00
|
|
|
evaluate::DynamicType &&, MaybeSubscriptIntExpr &&length);
|
2019-01-15 16:59:20 -08:00
|
|
|
Symbol *MakeAssocEntity();
|
|
|
|
void SetTypeFromAssociation(Symbol &);
|
|
|
|
void SetAttrsFromAssociation(Symbol &);
|
2019-04-15 10:26:20 -07:00
|
|
|
Selector ResolveSelector(const parser::Selector &);
|
2019-06-20 09:56:59 -07:00
|
|
|
void ResolveControlExpressions(const parser::ConcurrentControl &control);
|
|
|
|
void ResolveIndexName(const parser::ConcurrentControl &control);
|
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;
|
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 &);
|
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::Designator &);
|
2019-05-09 08:32:27 -07:00
|
|
|
template<typename A, typename B>
|
|
|
|
void Post(const parser::LoopBounds<A, B> &x) {
|
|
|
|
ResolveName(*parser::Unwrap<parser::Name>(x.name));
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
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 &);
|
2019-03-18 11:48:02 -07:00
|
|
|
bool Pre(const parser::DefinedOpName &);
|
2019-05-06 07:26:43 -07:00
|
|
|
bool Pre(const parser::ProgramUnit &);
|
|
|
|
|
|
|
|
// These nodes should never be reached: they are handled in ProgramUnit
|
|
|
|
bool Pre(const parser::MainProgram &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::FunctionSubprogram &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::SubroutineSubprogram &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::SeparateModuleSubprogram &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::Module &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::Submodule &) { DIE("unreachable"); }
|
|
|
|
bool Pre(const parser::BlockData &) { DIE("unreachable"); }
|
2018-04-23 12:33:10 -07:00
|
|
|
|
2019-06-26 14:51:58 -07:00
|
|
|
void NoteExecutablePartCall(Symbol::Flag, const parser::Call &);
|
2019-06-25 14:46:02 -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-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 &);
|
2019-06-26 14:51:58 -07:00
|
|
|
bool SetProcFlag(const parser::Name &, Symbol &, Symbol::Flag);
|
2019-07-02 14:00:44 -07:00
|
|
|
void ResolveSpecificationParts(ProgramTree &);
|
2019-05-06 07:26:43 -07:00
|
|
|
void AddSubpNames(const ProgramTree &);
|
|
|
|
bool BeginScope(const ProgramTree &);
|
2019-06-23 10:59:32 -07:00
|
|
|
void FinishSpecificationParts(const ProgramTree &);
|
|
|
|
void FinishDerivedType(Scope &);
|
2019-07-12 12:12:12 -07:00
|
|
|
void SetPassArg(const Symbol &, const Symbol *, WithPassArg &);
|
2019-07-02 14:00:44 -07:00
|
|
|
void ResolveExecutionParts(const ProgramTree &);
|
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;
|
2019-05-02 11:19:01 -07:00
|
|
|
} else if (inheritFromParent_) {
|
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') {
|
2019-05-02 11:19:01 -07:00
|
|
|
return &context_.MakeNumericType(TypeCategory::Integer);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'a' && ch <= 'z') {
|
2019-05-02 11:19:01 -07: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) {
|
2019-05-07 09:22:53 -07:00
|
|
|
context_.Say(parser::CharBlock{lo},
|
|
|
|
"More than one implicit type specified for '%c'"_err_en_US, ch);
|
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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseVisitor::set_context(SemanticsContext &context) {
|
|
|
|
context_ = &context;
|
2019-03-05 16:52:50 -08:00
|
|
|
messageHandler_.set_context(context);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-06-22 10:00:18 -07:00
|
|
|
Attrs result{GetAttrs()};
|
2018-03-30 13:57:23 -07:00
|
|
|
attrs_.reset();
|
2019-06-22 10:00:18 -07:00
|
|
|
passName_ = nullptr;
|
2019-01-04 17:10:31 -08:00
|
|
|
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;
|
|
|
|
}
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](ProcEntityDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[&](ProcBindingDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[](auto &) { common::die("unexpected pass name"); },
|
|
|
|
},
|
2019-01-04 17:10:31 -08:00
|
|
|
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) {
|
2019-06-22 10:00:18 -07:00
|
|
|
passName_ = &x.v->source;
|
2019-01-04 17:10:31 -08:00
|
|
|
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 &) {
|
|
|
|
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 &) {
|
2019-06-11 18:26:48 -07:00
|
|
|
MakeNumericType(TypeCategory::Real, context().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 &) {
|
2019-06-11 18:26:48 -07:00
|
|
|
MakeNumericType(TypeCategory::Complex, context().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) {
|
2019-03-05 16:52:50 -08:00
|
|
|
return AnalyzeKindSelector(context(), 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
|
|
|
Message &MessageHandler::Say(MessageFixedText &&msg) {
|
2019-03-05 16:52:50 -08:00
|
|
|
return context_->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) {
|
2019-03-05 16:52:50 -08:00
|
|
|
return context_->Say(*currStmtSource(), std::move(msg));
|
2019-02-18 11:39:46 -08:00
|
|
|
}
|
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-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) {
|
2019-07-10 17:49:28 -07:00
|
|
|
bool result{std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const std::list<ImplicitNoneNameSpec> &x) {
|
|
|
|
return HandleImplicitNone(x);
|
|
|
|
},
|
|
|
|
[&](const std::list<parser::ImplicitSpec> &x) {
|
|
|
|
if (prevImplicitNoneType_) {
|
|
|
|
Say("IMPLICIT statement after IMPLICIT NONE or "
|
|
|
|
"IMPLICIT NONE(TYPE) statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u)};
|
2018-04-04 09:03:51 -07:00
|
|
|
prevImplicit_ = currStmtSource();
|
2019-07-10 17:49:28 -07:00
|
|
|
return result;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-02 11:19:01 -07:00
|
|
|
void ImplicitRulesVisitor::SetScope(const Scope &scope) {
|
|
|
|
implicitRules_ = &implicitRulesMap_.at(&scope);
|
2018-04-04 09:03:51 -07:00
|
|
|
prevImplicit_ = nullptr;
|
|
|
|
prevImplicitNone_ = nullptr;
|
|
|
|
prevImplicitNoneType_ = nullptr;
|
|
|
|
prevParameterStmt_ = nullptr;
|
|
|
|
}
|
2019-05-02 11:19:01 -07:00
|
|
|
void ImplicitRulesVisitor::BeginScope(const Scope &scope) {
|
|
|
|
// find or create implicit rules for this scope
|
|
|
|
implicitRulesMap_.try_emplace(&scope, context(), implicitRules_);
|
|
|
|
SetScope(scope);
|
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
|
|
|
|
|
2019-04-04 14:46:40 -07:00
|
|
|
void ArraySpecVisitor::Post(const parser::ArraySpec &x) {
|
2019-04-04 16:32:37 -07:00
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
arraySpec_ = AnalyzeArraySpec(context(), x);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2019-04-25 15:05:41 -07:00
|
|
|
void ArraySpecVisitor::Post(const parser::ComponentArraySpec &x) {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
arraySpec_ = AnalyzeArraySpec(context(), x);
|
|
|
|
}
|
2019-04-04 14:46:40 -07:00
|
|
|
void ArraySpecVisitor::Post(const parser::CoarraySpec &x) {
|
2019-04-04 16:32:37 -07:00
|
|
|
CHECK(coarraySpec_.empty());
|
|
|
|
coarraySpec_ = AnalyzeCoarraySpec(context(), x);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
const ArraySpec &ArraySpecVisitor::arraySpec() {
|
|
|
|
return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
|
|
|
|
}
|
2019-04-04 14:46:40 -07:00
|
|
|
const ArraySpec &ArraySpecVisitor::coarraySpec() {
|
|
|
|
return !coarraySpec_.empty() ? coarraySpec_ : attrCoarraySpec_;
|
|
|
|
}
|
2018-05-17 13:06:38 -07:00
|
|
|
void ArraySpecVisitor::BeginArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
2019-04-04 14:46:40 -07:00
|
|
|
CHECK(coarraySpec_.empty());
|
2018-05-17 13:06:38 -07:00
|
|
|
CHECK(attrArraySpec_.empty());
|
2019-04-04 14:46:40 -07:00
|
|
|
CHECK(attrCoarraySpec_.empty());
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
void ArraySpecVisitor::EndArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
2019-04-04 14:46:40 -07:00
|
|
|
CHECK(coarraySpec_.empty());
|
2018-05-17 13:06:38 -07:00
|
|
|
attrArraySpec_.clear();
|
2019-04-04 14:46:40 -07:00
|
|
|
attrCoarraySpec_.clear();
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
void ArraySpecVisitor::PostAttrSpec() {
|
2019-04-04 14:46:40 -07:00
|
|
|
// Save dimension/codimension from attrs so we can process array/coarray-spec
|
|
|
|
// on the entity-decl
|
2018-05-17 13:06:38 -07:00
|
|
|
if (!arraySpec_.empty()) {
|
|
|
|
CHECK(attrArraySpec_.empty());
|
|
|
|
attrArraySpec_.splice(attrArraySpec_.cbegin(), arraySpec_);
|
|
|
|
}
|
2019-04-04 14:46:40 -07:00
|
|
|
if (!coarraySpec_.empty()) {
|
|
|
|
CHECK(attrCoarraySpec_.empty());
|
|
|
|
attrCoarraySpec_.splice(attrCoarraySpec_.cbegin(), coarraySpec_);
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ScopeHandler implementation
|
|
|
|
|
2019-04-25 13:18:33 -07:00
|
|
|
void ScopeHandler::SayAlreadyDeclared(const parser::Name &name, Symbol &prev) {
|
2019-02-26 13:28:53 -08:00
|
|
|
SayAlreadyDeclared(name.source, prev);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
2019-04-25 13:18:33 -07:00
|
|
|
void ScopeHandler::SayAlreadyDeclared(const SourceName &name, Symbol &prev) {
|
2019-02-26 13:28:53 -08:00
|
|
|
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,
|
2019-05-07 09:22:53 -07:00
|
|
|
details->symbol().name(), details->module().name());
|
2019-02-26 13:28:53 -08:00
|
|
|
} else {
|
2019-05-07 09:22:53 -07:00
|
|
|
msg.Attach(prev.name(), "Previous declaration of '%s'"_en_US, prev.name());
|
2019-02-26 13:28:53 -08:00
|
|
|
}
|
2019-04-25 14:47:39 -07:00
|
|
|
context().SetError(prev);
|
2019-02-26 13:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 13:51:36 -08:00
|
|
|
void ScopeHandler::SayWithDecl(
|
2019-04-25 13:18:33 -07:00
|
|
|
const parser::Name &name, 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-04-25 14:47:39 -07:00
|
|
|
context().SetError(symbol, msg.isFatal());
|
2019-02-05 13:51:36 -08:00
|
|
|
}
|
2019-06-27 12:33:36 -07:00
|
|
|
|
2019-07-02 12:10:09 -07:00
|
|
|
void ScopeHandler::SayLocalMustBeVariable(
|
|
|
|
const parser::Name &name, Symbol &symbol) {
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"The name '%s' must be a variable to appear"
|
|
|
|
" in a locality-spec"_err_en_US);
|
2019-06-27 12:33:36 -07: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,
|
2019-05-07 09:22:53 -07:00
|
|
|
typeSymbol->name());
|
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) {
|
2019-05-07 09:22:53 -07:00
|
|
|
Say(name1, std::move(msg1)).Attach(name2, std::move(msg2), name2);
|
2019-02-14 07:59:20 -08:00
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
void ScopeHandler::Say2(const SourceName &name, MessageFixedText &&msg1,
|
2019-04-25 13:18:33 -07:00
|
|
|
Symbol &symbol, MessageFixedText &&msg2) {
|
2019-03-18 11:48:02 -07:00
|
|
|
Say2(name, std::move(msg1), symbol.name(), std::move(msg2));
|
2019-04-25 14:47:39 -07:00
|
|
|
context().SetError(symbol, msg1.isFatal());
|
2019-03-18 11:48:02 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
|
2019-04-25 13:18:33 -07:00
|
|
|
Symbol &symbol, MessageFixedText &&msg2) {
|
2019-02-14 07:59:20 -08:00
|
|
|
Say2(name.source, std::move(msg1), symbol.name(), std::move(msg2));
|
2019-04-25 14:47:39 -07:00
|
|
|
context().SetError(symbol, msg1.isFatal());
|
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()) {
|
2019-07-11 09:27:25 -07:00
|
|
|
if (scope->kind() != Scope::Kind::Block && !scope->IsDerivedType()) {
|
2018-08-27 11:48:49 -07:00
|
|
|
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()) {
|
2019-07-11 09:27:25 -07:00
|
|
|
if (scope->IsGlobal()) {
|
2018-09-14 15:04:50 -07:00
|
|
|
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) {
|
2019-05-02 11:19:01 -07:00
|
|
|
ImplicitRulesVisitor::BeginScope(scope);
|
2018-08-27 11:48:49 -07:00
|
|
|
}
|
2019-07-11 09:27:25 -07:00
|
|
|
if (!currScope_->IsDerivedType()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
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())) {
|
2019-07-10 15:04:39 -07:00
|
|
|
auto &newSymbol{MakeSymbol(symbol->name())};
|
2018-11-16 12:43:08 -08:00
|
|
|
if (kind == Scope::Kind::Subprogram) {
|
2019-07-10 15:04:39 -07:00
|
|
|
// Allow for recursive references. If this symbol is a function
|
|
|
|
// without an explicit RESULT(), this new symbol will be discarded
|
|
|
|
// and replaced with an object of the same name.
|
|
|
|
newSymbol.set_details(HostAssocDetails{*symbol});
|
2018-11-16 12:43:08 -08:00
|
|
|
} else {
|
|
|
|
newSymbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ScopeHandler::PopScope() {
|
2019-05-21 16:58:46 -07:00
|
|
|
// Entities that are not yet classified as objects or procedures are now
|
|
|
|
// assumed to be objects.
|
2019-06-03 13:36:47 -07:00
|
|
|
// TODO: Statement functions
|
2018-09-22 08:05:46 -07:00
|
|
|
for (auto &pair : currScope()) {
|
2019-05-21 16:58:46 -07:00
|
|
|
ConvertToObjectEntity(*pair.second);
|
2018-09-22 08:05:46 -07:00
|
|
|
}
|
2019-05-06 07:26:43 -07:00
|
|
|
SetScope(currScope_->parent());
|
|
|
|
}
|
|
|
|
void ScopeHandler::SetScope(Scope &scope) {
|
|
|
|
currScope_ = &scope;
|
2019-05-02 11:19:01 -07:00
|
|
|
ImplicitRulesVisitor::SetScope(InclusiveScope());
|
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::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;
|
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
Symbol &ScopeHandler::MakeSymbol(const SourceName &name, Attrs attrs) {
|
|
|
|
return MakeSymbol(currScope(), name, attrs);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &ScopeHandler::MakeSymbol(const parser::Name &name, Attrs attrs) {
|
2019-03-18 11:48:02 -07:00
|
|
|
return Resolve(name, MakeSymbol(name.source, attrs));
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
2019-07-02 14:00:44 -07:00
|
|
|
Symbol &ScopeHandler::CopySymbol(const SourceName &name, const Symbol &symbol) {
|
|
|
|
CHECK(!FindInScope(currScope(), name));
|
|
|
|
return MakeSymbol(currScope(), name, symbol.attrs());
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-07-11 09:27:25 -07:00
|
|
|
if (scope.IsDerivedType()) {
|
2018-12-04 10:55:32 -08:00
|
|
|
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) {
|
2019-06-24 15:22:57 -07:00
|
|
|
return symbol.GetType() == nullptr &&
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[](const EntityDetails &) { return true; },
|
|
|
|
[](const ObjectEntityDetails &) { return true; },
|
|
|
|
[](const AssocEntityDetails &) { return true; },
|
|
|
|
[&](const ProcEntityDetails &p) {
|
|
|
|
return symbol.test(Symbol::Flag::Function) &&
|
2019-07-11 11:34:01 -07:00
|
|
|
!symbol.attrs().test(Attr::INTRINSIC) &&
|
2019-07-10 17:49:28 -07:00
|
|
|
p.interface().type() == nullptr &&
|
|
|
|
p.interface().symbol() == nullptr;
|
|
|
|
},
|
|
|
|
[](const auto &) { return false; },
|
|
|
|
},
|
2019-06-24 15:22:57 -07:00
|
|
|
symbol.details());
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
void ScopeHandler::ApplyImplicitRules(Symbol &symbol) {
|
2018-10-10 16:20:46 -07:00
|
|
|
if (NeedsType(symbol)) {
|
2018-10-18 07:55:48 -07:00
|
|
|
if (isImplicitNoneType()) {
|
2019-06-26 15:30:53 -07:00
|
|
|
if (symbol.has<ProcEntityDetails>() &&
|
2019-07-11 11:34:01 -07:00
|
|
|
!symbol.attrs().test(Attr::EXTERNAL) &&
|
2019-06-26 15:30:53 -07:00
|
|
|
context().intrinsics().IsIntrinsic(symbol.name().ToString())) {
|
|
|
|
// type will be determined in expression semantics
|
2019-07-11 11:34:01 -07:00
|
|
|
symbol.attrs().set(Attr::INTRINSIC);
|
2019-06-26 15:30:53 -07:00
|
|
|
} else {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 11:41:30 -07:00
|
|
|
void ScopeHandler::MakeExternal(Symbol &symbol) {
|
|
|
|
if (!symbol.attrs().test(Attr::EXTERNAL)) {
|
|
|
|
symbol.attrs().set(Attr::EXTERNAL);
|
|
|
|
if (symbol.attrs().test(Attr::INTRINSIC)) { // C840
|
|
|
|
Say(symbol.name(),
|
|
|
|
"Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
|
|
|
|
symbol.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-03-18 11:48:02 -07:00
|
|
|
auto info{GenericSpecInfo{generic.value()}};
|
|
|
|
info.Resolve(AddUse(info.symbolName()));
|
2018-05-14 14:26:39 -07:00
|
|
|
},
|
2019-03-18 11:48:02 -07:00
|
|
|
[&](const parser::Name &name) { Resolve(name, AddUse(name.source)); },
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](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); },
|
2019-03-18 11:48:02 -07:00
|
|
|
[&](const parser::Rename::Operators &ops) { AddUse(ops); },
|
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;
|
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Rename::Operators &x) {
|
|
|
|
AddUse(x);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
|
|
|
|
// 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) {
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Rename::Names &names) {
|
|
|
|
useNames.insert(std::get<1>(names.t).source);
|
|
|
|
},
|
|
|
|
[&](const parser::Rename::Operators &ops) {
|
|
|
|
useNames.insert(std::get<1>(ops.t).v.source);
|
|
|
|
},
|
|
|
|
},
|
2018-05-14 14:26:39 -07:00
|
|
|
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) {
|
2019-07-02 14:00:44 -07:00
|
|
|
localSymbol = &CopySymbol(name, *symbol);
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
AddUse(x.moduleName.source, *localSymbol, *symbol);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useModuleScope_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(const parser::Rename::Names &names) {
|
2019-03-18 11:48:02 -07:00
|
|
|
const auto &localName{std::get<0>(names.t)};
|
|
|
|
const auto &useName{std::get<1>(names.t)};
|
|
|
|
SymbolRename rename{AddUse(localName.source, useName.source)};
|
|
|
|
Resolve(useName, rename.use);
|
|
|
|
Resolve(localName, rename.local);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(const parser::Rename::Operators &ops) {
|
|
|
|
const parser::DefinedOpName &local{std::get<0>(ops.t)};
|
|
|
|
const parser::DefinedOpName &use{std::get<1>(ops.t)};
|
|
|
|
GenericSpecInfo localInfo{local};
|
|
|
|
GenericSpecInfo useInfo{use};
|
2019-06-26 15:30:53 -07:00
|
|
|
if (IsIntrinsicOperator(context(), local.v.source)) {
|
2019-03-18 11:48:02 -07:00
|
|
|
Say(local.v,
|
|
|
|
"Intrinsic operator '%s' may not be used as a defined operator"_err_en_US);
|
|
|
|
} else if (IsLogicalConstant(context(), local.v.source)) {
|
|
|
|
Say(local.v,
|
|
|
|
"Logical constant '%s' may not be used as a defined operator"_err_en_US);
|
|
|
|
} else {
|
|
|
|
SymbolRename rename{AddUse(localInfo.symbolName(), useInfo.symbolName())};
|
|
|
|
useInfo.Resolve(rename.use);
|
|
|
|
localInfo.Resolve(rename.local);
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
|
|
|
|
Symbol *ModuleVisitor::AddUse(const SourceName &useName) {
|
|
|
|
return AddUse(useName, useName).use;
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
2019-03-18 11:48:02 -07:00
|
|
|
ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
|
|
|
|
const SourceName &localName, const SourceName &useName) {
|
2018-05-14 14:26:39 -07:00
|
|
|
if (!useModuleScope_) {
|
2019-03-18 11:48:02 -07:00
|
|
|
return {}; // error occurred finding module
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *useSymbol{FindInScope(*useModuleScope_, useName)};
|
|
|
|
if (!useSymbol) {
|
2019-03-18 11:48:02 -07:00
|
|
|
Say(useName,
|
|
|
|
IsDefinedOperator(useName)
|
|
|
|
? "Operator '%s' not found in module '%s'"_err_en_US
|
|
|
|
: "'%s' not found in module '%s'"_err_en_US,
|
|
|
|
useName, useModuleScope_->name());
|
|
|
|
return {};
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (useSymbol->attrs().test(Attr::PRIVATE)) {
|
2019-03-18 11:48:02 -07:00
|
|
|
Say(useName,
|
|
|
|
IsDefinedOperator(useName)
|
|
|
|
? "Operator '%s' is PRIVATE in '%s'"_err_en_US
|
|
|
|
: "'%s' is PRIVATE in '%s'"_err_en_US,
|
|
|
|
useName, useModuleScope_->name());
|
|
|
|
return {};
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
auto &localSymbol{MakeSymbol(localName)};
|
|
|
|
AddUse(useName, localSymbol, *useSymbol);
|
|
|
|
return {&localSymbol, useSymbol};
|
2018-11-16 12:43:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(
|
2019-03-18 11:48:02 -07:00
|
|
|
const SourceName &location, Symbol &localSymbol, const Symbol &useSymbol) {
|
2018-11-16 12:43:08 -08:00
|
|
|
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,
|
2019-05-07 09:22:53 -07:00
|
|
|
localSymbol.name());
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
bool ModuleVisitor::BeginSubmodule(
|
|
|
|
const parser::Name &name, const parser::ParentIdentifier &parentId) {
|
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
|
2019-05-06 07:26:43 -07:00
|
|
|
BeginModule(name, true);
|
2018-11-16 12:43:08 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
void ModuleVisitor::BeginModule(const parser::Name &name, bool isSubmodule) {
|
2018-08-02 16:21:27 -07:00
|
|
|
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());
|
2019-05-06 07:26:43 -07:00
|
|
|
prevAccessStmt_ = nullptr;
|
2018-08-02 16:21:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-06-14 15:04:13 -07:00
|
|
|
bool isAbstract{std::holds_alternative<parser::Abstract>(x.u)};
|
|
|
|
genericInfo_.emplace(/*isInterface*/ true, isAbstract);
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceVisitor::Post(const parser::EndInterfaceStmt &) {
|
2019-06-14 15:04:13 -07:00
|
|
|
genericInfo_.pop();
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
2019-03-18 11:48:02 -07:00
|
|
|
// Create a symbol in genericSymbol_ for this GenericSpec.
|
2018-05-14 13:51:13 -07:00
|
|
|
bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
|
2019-03-18 11:48:02 -07:00
|
|
|
auto info{GenericSpecInfo{x}};
|
|
|
|
const SourceName &symbolName{info.symbolName()};
|
|
|
|
if (IsLogicalConstant(context(), symbolName)) {
|
|
|
|
Say(symbolName,
|
|
|
|
"Logical constant '%s' may not be used as a defined operator"_err_en_US);
|
2018-11-16 12:43:08 -08:00
|
|
|
return false;
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2019-06-14 15:04:13 -07:00
|
|
|
Symbol *symbol{currScope().FindSymbol(symbolName)};
|
|
|
|
if (symbol) {
|
|
|
|
if (symbol->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.
|
2019-06-14 15:04:13 -07:00
|
|
|
CHECK(symbol->scope()->symbol() == symbol);
|
2018-06-22 08:21:19 -07:00
|
|
|
GenericDetails details;
|
2019-06-14 15:04:13 -07:00
|
|
|
details.set_derivedType(*symbol);
|
|
|
|
EraseSymbol(*symbol);
|
|
|
|
symbol = &MakeSymbol(symbolName);
|
|
|
|
symbol->set_details(details);
|
2019-04-01 13:08:57 -07:00
|
|
|
// preserve access attributes
|
2019-06-14 15:04:13 -07:00
|
|
|
symbol->attrs() |=
|
2019-04-01 13:08:57 -07:00
|
|
|
details.derivedType()->attrs() & Attrs{Attr::PUBLIC, Attr::PRIVATE};
|
2019-06-14 15:04:13 -07:00
|
|
|
} else if (symbol->has<UnknownDetails>()) {
|
2019-04-01 13:08:57 -07:00
|
|
|
// okay
|
2019-06-14 15:04:13 -07:00
|
|
|
} else if (!symbol->IsSubprogram()) {
|
|
|
|
SayAlreadyDeclared(symbolName, *symbol);
|
|
|
|
EraseSymbol(*symbol);
|
|
|
|
symbol = nullptr;
|
|
|
|
} else if (symbol->has<UseDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// copy the USEd symbol into this scope so we can modify it
|
2019-06-14 15:04:13 -07:00
|
|
|
const Symbol &ultimate{symbol->GetUltimate()};
|
|
|
|
EraseSymbol(*symbol);
|
2019-07-02 14:00:44 -07:00
|
|
|
symbol = &CopySymbol(symbolName, ultimate);
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *details{ultimate.detailsIf<GenericDetails>()}) {
|
2019-06-14 15:04:13 -07:00
|
|
|
symbol->set_details(GenericDetails{details->specificProcs()});
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{ultimate.detailsIf<SubprogramDetails>()}) {
|
2019-06-14 15:04:13 -07:00
|
|
|
symbol->set_details(SubprogramDetails{*details});
|
2018-05-22 16:12:56 -07:00
|
|
|
} else {
|
2019-07-02 14:00:44 -07:00
|
|
|
DIE("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 15:04:13 -07:00
|
|
|
if (!symbol || symbol->has<UnknownDetails>()) {
|
|
|
|
symbol = &MakeSymbol(symbolName);
|
|
|
|
symbol->set_details(GenericDetails{});
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2019-06-14 15:04:13 -07:00
|
|
|
if (symbol->has<GenericDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// okay
|
2019-06-14 15:04:13 -07:00
|
|
|
} else if (symbol->has<SubprogramDetails>() ||
|
|
|
|
symbol->has<SubprogramNameDetails>()) {
|
2018-06-19 16:06:41 -07:00
|
|
|
GenericDetails genericDetails;
|
2019-06-14 15:04:13 -07:00
|
|
|
genericDetails.set_specific(*symbol);
|
|
|
|
EraseSymbol(*symbol);
|
|
|
|
symbol = &MakeSymbol(symbolName);
|
|
|
|
symbol->set_details(genericDetails);
|
2018-12-06 06:59:37 -08:00
|
|
|
} else {
|
|
|
|
common::die("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2019-06-14 15:04:13 -07:00
|
|
|
info.Resolve(symbol);
|
|
|
|
SetGenericSymbol(*symbol);
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:04:13 -07:00
|
|
|
bool InterfaceVisitor::Pre(const parser::GenericStmt &x) {
|
|
|
|
genericInfo_.emplace(/*isInterface*/ false);
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
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)}) {
|
2019-06-14 15:04:13 -07:00
|
|
|
GetGenericInfo().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);
|
2019-06-14 15:04:13 -07:00
|
|
|
genericInfo_.pop();
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:04:13 -07:00
|
|
|
bool InterfaceVisitor::inInterfaceBlock() const {
|
|
|
|
return !genericInfo_.empty() && GetGenericInfo().isInterface;
|
|
|
|
}
|
|
|
|
bool InterfaceVisitor::isGeneric() const {
|
|
|
|
return !genericInfo_.empty() && GetGenericInfo().symbol != nullptr;
|
|
|
|
}
|
|
|
|
bool InterfaceVisitor::isAbstract() const {
|
|
|
|
return !genericInfo_.empty() && GetGenericInfo().isAbstract;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
GenericDetails &InterfaceVisitor::GetGenericDetails() {
|
2019-06-14 15:04:13 -07:00
|
|
|
return GetGenericInfo().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) {
|
2019-06-14 15:04:13 -07:00
|
|
|
specificProcs_.emplace(
|
|
|
|
GetGenericInfo().symbol, std::make_pair(&name, kind));
|
2018-12-18 07:59:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-06-03 12:18:52 -07:00
|
|
|
if (const auto *nd{symbol->detailsIf<SubprogramNameDetails>()}) {
|
|
|
|
if (nd->kind() != SubprogramKind::Module) {
|
|
|
|
Say(*name, "'%s' is not a module procedure"_err_en_US);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// USE-associated procedure
|
|
|
|
const auto *sd{symbol->detailsIf<SubprogramDetails>()};
|
|
|
|
CHECK(sd != nullptr);
|
|
|
|
if (symbol->owner().kind() != Scope::Kind::Module ||
|
|
|
|
sd->isInterface()) {
|
|
|
|
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,
|
2019-03-18 11:48:02 -07:00
|
|
|
IsDefinedOperator(generic.name())
|
|
|
|
? "Procedure '%s' is already specified in generic operator '%s'"_err_en_US
|
|
|
|
: "Procedure '%s' is already specified in generic '%s'"_err_en_US,
|
2018-12-18 07:59:40 -08:00
|
|
|
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-04-25 13:18:33 -07:00
|
|
|
if (auto *proc{details.CheckSpecific()}) {
|
2019-02-26 13:28:53 -08:00
|
|
|
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)};
|
2019-07-02 14:00:44 -07:00
|
|
|
for (const auto *specific : specifics) {
|
|
|
|
if (isFunction != specific->test(Symbol::Flag::Function)) { // C1514
|
2018-07-05 10:28:34 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-02 14:00:44 -07:00
|
|
|
void InterfaceVisitor::SayNotDistinguishable(
|
|
|
|
const Symbol &generic, const Symbol &proc1, const Symbol &proc2) {
|
|
|
|
auto &&text{IsDefinedOperator(generic.name())
|
|
|
|
? "Generic operator '%s' may not have specific procedures '%s'"
|
|
|
|
" and '%s' as their interfaces are not distinguishable"_err_en_US
|
|
|
|
: "Generic '%s' may not have specific procedures '%s'"
|
|
|
|
" and '%s' as their interfaces are not distinguishable"_err_en_US};
|
|
|
|
auto &msg{context().Say(generic.name(), std::move(text), generic.name(),
|
|
|
|
proc1.name(), proc2.name())};
|
|
|
|
if (!proc1.IsFromModFile()) {
|
|
|
|
msg.Attach(proc1.name(), "Declaration of '%s'"_en_US, proc1.name());
|
|
|
|
}
|
|
|
|
if (!proc2.IsFromModFile()) {
|
|
|
|
msg.Attach(proc2.name(), "Declaration of '%s'"_en_US, proc2.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 13:05:42 -07:00
|
|
|
static GenericKind GetGenericKind(const Symbol &generic) {
|
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const GenericDetails &x) { return x.kind(); },
|
|
|
|
[&](const GenericBindingDetails &x) { return x.kind(); },
|
|
|
|
[](auto &) -> GenericKind { DIE("not a generic"); },
|
|
|
|
},
|
|
|
|
generic.details());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsOperatorOrAssignment(const Symbol &generic) {
|
|
|
|
auto kind{GetGenericKind(generic)};
|
|
|
|
return kind == GenericKind::DefinedOp || kind == GenericKind::Assignment ||
|
|
|
|
(kind >= GenericKind::OpPower && kind <= GenericKind::OpNEQV);
|
|
|
|
}
|
|
|
|
|
2019-07-02 14:00:44 -07:00
|
|
|
// Check that the specifics of this generic are distinguishable from each other
|
|
|
|
void InterfaceVisitor::CheckSpecificsAreDistinguishable(
|
|
|
|
const Symbol &generic, const SymbolVector &specifics) {
|
|
|
|
auto count{specifics.size()};
|
|
|
|
if (specifics.size() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-15 13:05:42 -07:00
|
|
|
auto distinguishable{IsOperatorOrAssignment(generic)
|
|
|
|
? evaluate::characteristics::DistinguishableOpOrAssign
|
|
|
|
: evaluate::characteristics::Distinguishable};
|
2019-07-02 14:00:44 -07:00
|
|
|
using evaluate::characteristics::Procedure;
|
|
|
|
std::vector<Procedure> procs;
|
|
|
|
for (const Symbol *symbol : specifics) {
|
|
|
|
auto proc{Procedure::Characterize(*symbol, context().intrinsics())};
|
|
|
|
if (!proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
procs.emplace_back(*proc);
|
|
|
|
}
|
|
|
|
for (std::size_t i1{0}; i1 < count - 1; ++i1) {
|
|
|
|
auto &proc1{procs[i1]};
|
|
|
|
for (std::size_t i2{i1 + 1}; i2 < count; ++i2) {
|
|
|
|
auto &proc2{procs[i2]};
|
2019-07-15 13:05:42 -07:00
|
|
|
if (!distinguishable(proc1, proc2)) {
|
2019-07-02 14:00:44 -07:00
|
|
|
SayNotDistinguishable(generic, *specifics[i1], *specifics[i2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 16:16:55 -07:00
|
|
|
details.add_dummyArg(MakeSymbol(dummyName, std::move(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);
|
|
|
|
}
|
2019-04-08 16:16:55 -07:00
|
|
|
details.set_result(MakeSymbol(name, std::move(resultDetails)));
|
2018-05-04 15:37:56 -07:00
|
|
|
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
|
2019-02-28 09:53:49 -08:00
|
|
|
if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
|
|
|
|
funcInfo_.parsedType = parsedType;
|
|
|
|
funcInfo_.source = currStmtSource();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-24 10:05:43 -08:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:53:49 -08:00
|
|
|
void SubprogramVisitor::Post(const parser::ImplicitPart &) {
|
|
|
|
// If the function has a type in the prefix, process it now
|
2019-02-24 10:05:43 -08:00
|
|
|
if (funcInfo_.parsedType) {
|
|
|
|
messageHandler().set_currStmtSource(funcInfo_.source);
|
|
|
|
if (const auto *type{ProcessTypeSpec(*funcInfo_.parsedType)}) {
|
|
|
|
funcInfo_.resultSymbol->SetType(*type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
funcInfo_ = {};
|
|
|
|
}
|
|
|
|
|
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)};
|
2019-05-06 07:26:43 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Subroutine);
|
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)};
|
2019-05-06 07:26:43 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Function);
|
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)) {
|
2019-06-03 13:36:47 -07:00
|
|
|
if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
|
|
|
|
Symbol &dummy{MakeSymbol(*dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
|
|
|
} else {
|
|
|
|
details.add_alternateReturn();
|
|
|
|
}
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-04-08 16:16:55 -07:00
|
|
|
funcInfo_.resultSymbol =
|
|
|
|
&MakeSymbol(*funcResultName, std::move(funcResultDetails));
|
2019-02-24 10:05:43 -08:00
|
|
|
details.set_result(*funcInfo_.resultSymbol);
|
2019-06-06 16:09:11 -07:00
|
|
|
name.symbol = currScope().symbol(); // must not be function result symbol
|
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>();
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
bool SubprogramVisitor::BeginSubprogram(
|
|
|
|
const parser::Name &name, Symbol::Flag subpFlag, bool hasModulePrefix) {
|
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
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-06 07:26:43 -07:00
|
|
|
void SubprogramVisitor::EndSubprogram() { PopScope(); }
|
2018-10-26 07:34:50 -07:00
|
|
|
|
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()) {
|
2019-06-04 11:41:30 -07:00
|
|
|
MakeExternal(*symbol);
|
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,
|
2019-05-07 09:22:53 -07:00
|
|
|
name.source, module->name());
|
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(
|
2019-04-25 13:18:33 -07:00
|
|
|
const SourceName &name, bool isPrivate, Symbol &symbol) {
|
2018-12-26 13:31:13 -08:00
|
|
|
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()};
|
2019-07-11 09:27:25 -07:00
|
|
|
CHECK(moduleScope->IsDerivedType());
|
|
|
|
while (
|
|
|
|
moduleScope->kind() != Scope::Kind::Module && !moduleScope->IsGlobal()) {
|
2019-02-12 17:24:43 -08:00
|
|
|
moduleScope = &moduleScope->parent();
|
|
|
|
}
|
|
|
|
if (moduleScope->kind() == Scope::Kind::Module) {
|
2019-07-11 09:27:25 -07:00
|
|
|
for (auto *scope{&currScope()}; !scope->IsGlobal();
|
2019-02-12 17:24:43 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-04-04 14:46:40 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::CodimensionDecl &x) {
|
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
|
|
|
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)};
|
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)) {
|
2019-06-21 16:13:56 -07:00
|
|
|
Initialization(name, *init, false);
|
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-09-22 08:05:46 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
2019-05-13 09:33:18 -07:00
|
|
|
if (auto converted{
|
|
|
|
EvaluateConvertedExpr(symbol, expr, expr.thing.value().source)}) {
|
|
|
|
symbol.get<ObjectEntityDetails>().set_init(std::move(*converted));
|
|
|
|
}
|
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) {
|
2019-06-04 11:41:30 -07:00
|
|
|
HandleAttributeStmt(Attr::INTRINSIC, x.v);
|
|
|
|
for (const auto &name : x.v) {
|
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!ConvertToProcEntity(*symbol)) {
|
|
|
|
SayWithDecl(
|
|
|
|
name, *symbol, "INTRINSIC attribute not allowed on '%s'"_err_en_US);
|
|
|
|
} else if (symbol->attrs().test(Attr::EXTERNAL)) { // C840
|
|
|
|
Say(symbol->name(),
|
|
|
|
"Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
|
|
|
|
symbol->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
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) {
|
2019-04-04 14:46:40 -07:00
|
|
|
if (!arraySpec().empty() || !coarraySpec().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-07-08 16:02:50 -07:00
|
|
|
charInfo_.length.reset();
|
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()) {
|
2019-07-15 17:11:54 -07:00
|
|
|
if (interface.symbol()->test(Symbol::Flag::Function)) {
|
|
|
|
symbol.set(Symbol::Flag::Function);
|
|
|
|
} else if (interface.symbol()->test(Symbol::Flag::Subroutine)) {
|
|
|
|
symbol.set(Symbol::Flag::Subroutine);
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
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);
|
2019-04-25 14:47:39 -07:00
|
|
|
context().SetError(symbol);
|
2018-04-12 12:59:42 -07:00
|
|
|
} else {
|
|
|
|
details->set_shape(arraySpec());
|
|
|
|
}
|
|
|
|
ClearArraySpec();
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2019-04-04 14:46:40 -07:00
|
|
|
if (!coarraySpec().empty()) {
|
|
|
|
if (details->IsCoarray()) {
|
|
|
|
Say(name,
|
|
|
|
"The codimensions of '%s' have already been declared"_err_en_US);
|
2019-04-25 14:47:39 -07:00
|
|
|
context().SetError(symbol);
|
2019-04-04 14:46:40 -07:00
|
|
|
} else {
|
|
|
|
details->set_coshape(coarraySpec());
|
|
|
|
}
|
|
|
|
ClearCoarraySpec();
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2019-07-08 16:02:50 -07:00
|
|
|
charInfo_.length.reset();
|
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()) {
|
2019-06-11 18:26:48 -07:00
|
|
|
charInfo_.kind =
|
|
|
|
KindExpr{context().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_ = {};
|
|
|
|
}
|
|
|
|
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.
|
2019-07-11 06:29:31 -07:00
|
|
|
auto parameterNames{OrderParameterNames(*typeSymbol)};
|
|
|
|
auto parameterDecls{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-06-20 14:23:21 -07:00
|
|
|
if (it != parameterDecls.end()) {
|
|
|
|
const auto *details{(*it)->detailsIf<TypeParamDetails>()};
|
|
|
|
if (details == nullptr || !details->init().has_value()) {
|
|
|
|
Say(typeName.source,
|
|
|
|
"Type parameter '%s' lacks a value and has no default"_err_en_US,
|
|
|
|
name);
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto category{GetDeclTypeSpecCategory()};
|
2019-07-10 18:20:27 -07:00
|
|
|
ProcessParameterExpressions(spec, context().foldingContext());
|
2018-12-04 10:55:32 -08:00
|
|
|
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-07-10 18:20:27 -07:00
|
|
|
InstantiateDerivedType(type.derivedTypeSpec(), 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)};
|
2019-05-13 09:33:18 -07:00
|
|
|
if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{attr})}) {
|
2018-12-06 17:52:43 -08:00
|
|
|
SetType(name, *type);
|
2019-05-13 09:33:18 -07:00
|
|
|
if (auto &init{
|
|
|
|
std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
|
|
|
|
if (auto maybeExpr{EvaluateConvertedExpr(
|
|
|
|
*symbol, *init, init->thing.thing.thing.value().source)}) {
|
|
|
|
auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)};
|
|
|
|
CHECK(intExpr != nullptr);
|
|
|
|
symbol->get<TypeParamDetails>().set_init(std::move(*intExpr));
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 17:52:43 -08:00
|
|
|
}
|
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)};
|
2019-06-25 13:07:32 -07:00
|
|
|
if (symbol.has<ObjectEntityDetails>()) {
|
2018-11-06 17:18:06 -08:00
|
|
|
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
|
2019-06-21 16:13:56 -07:00
|
|
|
Initialization(name, *init, true);
|
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();
|
2019-04-04 14:46:40 -07:00
|
|
|
ClearCoarraySpec();
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
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;
|
2019-07-15 17:11:54 -07:00
|
|
|
// The symbol is checked later to ensure that it defines
|
|
|
|
// an explicit interface.
|
|
|
|
if (!NameIsKnownOrIntrinsic(*name)) {
|
|
|
|
// Forward reference
|
|
|
|
Resolve(*name, MakeSymbol(InclusiveScope(), name->source, Attrs{}));
|
|
|
|
}
|
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-07-15 17:11:54 -07:00
|
|
|
interface.set_symbol(*interfaceName_->symbol);
|
|
|
|
} else 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())};
|
2019-03-11 15:39:11 -07:00
|
|
|
DerivedTypeDetails *dtDetails{nullptr};
|
|
|
|
if (Symbol * symbol{currScope().symbol()}) {
|
|
|
|
dtDetails = symbol->detailsIf<DerivedTypeDetails>();
|
|
|
|
}
|
|
|
|
if (dtDetails == nullptr) {
|
2018-09-06 12:06:32 -07:00
|
|
|
attrs.set(Attr::EXTERNAL);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2019-03-11 15:39:11 -07:00
|
|
|
Symbol &symbol{DeclareProcEntity(name, attrs, interface)};
|
|
|
|
if (dtDetails != nullptr) {
|
|
|
|
dtDetails->add_component(symbol);
|
|
|
|
}
|
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)};
|
2019-07-02 14:00:44 -07:00
|
|
|
SymbolVector specificProcs;
|
2019-02-08 08:57:28 -08:00
|
|
|
for (const auto &bindingName : bindingNames) {
|
2019-04-25 13:18:33 -07:00
|
|
|
auto *symbol{FindInTypeOrParents(bindingName)};
|
2019-02-08 08:57:28 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
auto info{GenericSpecInfo{genericSpec.value()}};
|
|
|
|
const SourceName &symbolName{info.symbolName()};
|
2019-02-08 08:57:28 -08:00
|
|
|
bool isPrivate{accessSpec ? accessSpec->v == parser::AccessSpec::Kind::Private
|
|
|
|
: derivedTypeInfo_.privateBindings};
|
2019-03-18 11:48:02 -07:00
|
|
|
auto *genericSymbol{FindInScope(currScope(), symbolName)};
|
2018-12-26 13:31:13 -08:00
|
|
|
if (genericSymbol) {
|
|
|
|
if (!genericSymbol->has<GenericBindingDetails>()) {
|
|
|
|
genericSymbol = nullptr; // MakeTypeSymbol will report the error below
|
|
|
|
}
|
2019-04-25 13:18:33 -07:00
|
|
|
} else if (auto *inheritedSymbol{
|
2019-03-18 11:48:02 -07:00
|
|
|
FindInTypeOrParents(currScope(), symbolName)}) {
|
2018-12-26 13:31:13 -08:00
|
|
|
// look in parent types:
|
|
|
|
if (inheritedSymbol->has<GenericBindingDetails>()) {
|
2019-03-18 11:48:02 -07:00
|
|
|
CheckAccessibility(symbolName, isPrivate, *inheritedSymbol);
|
2018-12-26 13:31:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (genericSymbol) {
|
2019-03-18 11:48:02 -07:00
|
|
|
CheckAccessibility(symbolName, isPrivate, *genericSymbol);
|
2018-12-26 13:31:13 -08:00
|
|
|
} else {
|
2019-03-18 11:48:02 -07:00
|
|
|
genericSymbol = MakeTypeSymbol(symbolName, GenericBindingDetails{});
|
2018-12-26 13:31:13 -08:00
|
|
|
if (!genericSymbol) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isPrivate) {
|
|
|
|
genericSymbol->attrs().set(Attr::PRIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &details{genericSymbol->get<GenericBindingDetails>()};
|
2019-02-08 08:57:28 -08:00
|
|
|
details.add_specificProcs(specificProcs);
|
2019-03-18 11:48:02 -07:00
|
|
|
info.Resolve(genericSymbol);
|
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-03-11 15:39:11 -07:00
|
|
|
// More constraints are enforced in expression.cc so that they
|
|
|
|
// can apply to structure constructors that have been converted
|
|
|
|
// from misparsed function references.
|
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)}) {
|
2019-03-11 15:39:11 -07:00
|
|
|
if (Symbol * symbol{FindInTypeOrParents(*typeScope, kw->v)}) {
|
|
|
|
if (kw->v.symbol == nullptr) {
|
|
|
|
kw->v.symbol = symbol;
|
|
|
|
}
|
|
|
|
CheckAccessibleComponent(kw->v.source, *symbol);
|
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();
|
2019-04-04 14:46:40 -07:00
|
|
|
ClearCoarraySpec();
|
2019-03-29 15:04:17 -07:00
|
|
|
auto *details{symbol.detailsIf<ObjectEntityDetails>()};
|
|
|
|
if (!details) {
|
2019-02-14 07:59:20 -08:00
|
|
|
return; // error was reported
|
|
|
|
}
|
|
|
|
commonBlockInfo_.curr->get<CommonBlockDetails>().add_object(symbol);
|
2019-06-05 09:12:21 -07:00
|
|
|
if (!IsAllocatableOrPointer(symbol) && !IsExplicit(details->shape())) {
|
2019-02-14 07:59:20 -08:00
|
|
|
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-03-29 15:04:17 -07:00
|
|
|
details->set_commonBlock(*commonBlockInfo_.curr);
|
2019-02-14 07:59:20 -08:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:26:48 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::EquivalenceStmt &x) {
|
|
|
|
// save equivalence sets to be processed after specification part
|
|
|
|
for (const std::list<parser::EquivalenceObject> &set : x.v) {
|
|
|
|
equivalenceSets_.push_back(&set);
|
|
|
|
}
|
|
|
|
return false; // don't implicitly declare names yet
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::CheckEquivalenceSets() {
|
|
|
|
EquivalenceSets equivSets{context()};
|
|
|
|
for (const auto *set : equivalenceSets_) {
|
|
|
|
const auto &source{set->front().v.value().source};
|
|
|
|
if (set->size() <= 1) { // R871
|
|
|
|
Say(source, "Equivalence set must have more than one object"_err_en_US);
|
|
|
|
}
|
|
|
|
for (const parser::EquivalenceObject &object : *set) {
|
|
|
|
const auto &designator{object.v.value()};
|
|
|
|
// The designator was not resolved when it was encountered so do it now.
|
|
|
|
// AnalyzeExpr causes array sections to be changed to substrings as needed
|
|
|
|
Walk(designator);
|
|
|
|
if (AnalyzeExpr(context(), designator)) {
|
|
|
|
equivSets.AddToSet(designator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
equivSets.FinishSet(source);
|
|
|
|
}
|
|
|
|
for (auto &set : equivSets.sets()) {
|
|
|
|
if (!set.empty()) {
|
|
|
|
currScope().add_equivalenceSet(std::move(set));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
equivalenceSets_.clear();
|
|
|
|
}
|
|
|
|
|
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)}) {
|
2019-04-08 16:16:55 -07:00
|
|
|
Say(name, std::move(*msg));
|
2019-02-20 17:45:39 -08:00
|
|
|
} 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-07-16 09:56:41 -07:00
|
|
|
bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
|
2019-07-15 17:11:54 -07:00
|
|
|
if (Symbol * symbol{FindSymbol(name)}) {
|
2019-07-16 09:56:41 -07:00
|
|
|
Resolve(name, *symbol);
|
|
|
|
return true;
|
2019-07-15 17:11:54 -07:00
|
|
|
} else {
|
|
|
|
return HandleUnrestrictedSpecificIntrinsicFunction(name);
|
|
|
|
}
|
2019-02-26 14:26:28 -08:00
|
|
|
}
|
|
|
|
|
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-07-16 09:56:41 -07:00
|
|
|
bool DeclarationVisitor::HandleUnrestrictedSpecificIntrinsicFunction(
|
2019-02-22 15:45:30 -08:00
|
|
|
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.
|
2019-07-11 11:34:01 -07:00
|
|
|
Symbol &symbol{
|
|
|
|
MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC})};
|
|
|
|
symbol.set_details(ProcEntityDetails{});
|
2019-02-22 15:45:30 -08:00
|
|
|
Resolve(name, symbol);
|
2019-07-16 10:53:39 -07:00
|
|
|
return true;
|
2019-02-22 15:45:30 -08:00
|
|
|
} else {
|
2019-07-16 09:56:41 -07:00
|
|
|
return false;
|
2019-02-22 15:45:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:14:12 -07:00
|
|
|
// Checks for all locality-specs: LOCAL, LOCAL_INIT, and SHARED
|
|
|
|
bool DeclarationVisitor::PassesSharedLocalityChecks(
|
2019-07-02 12:10:09 -07:00
|
|
|
const parser::Name &name, Symbol &symbol) {
|
|
|
|
if (!IsVariableName(symbol)) {
|
|
|
|
SayLocalMustBeVariable(name, symbol); // C1124
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-03 17:14:12 -07:00
|
|
|
if (symbol.owner() == currScope()) { // C1125 and C1126
|
|
|
|
SayAlreadyDeclared(name, symbol);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for locality-specs LOCAL and LOCAL_INIT
|
|
|
|
bool DeclarationVisitor::PassesLocalityChecks(
|
|
|
|
const parser::Name &name, Symbol &symbol) {
|
|
|
|
if (!PassesSharedLocalityChecks(name, symbol)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-02 12:10:09 -07:00
|
|
|
if (IsAllocatable(symbol)) { // C1128
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"ALLOCATABLE variable '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsOptional(symbol)) { // C1128
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"OPTIONAL argument '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsIntentIn(symbol)) { // C1128
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"INTENT IN argument '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsFinalizable(symbol)) { // C1128
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"Finalizable variable '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsCoarray(symbol)) { // C1128
|
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "Coarray '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-02 20:34:27 -07:00
|
|
|
if (const DeclTypeSpec * type{symbol.GetType()}) {
|
2019-07-02 12:10:09 -07:00
|
|
|
if (type->IsPolymorphic() && symbol.IsDummy() &&
|
2019-07-02 20:34:27 -07:00
|
|
|
!IsPointer(symbol)) { // C1128
|
2019-07-02 12:10:09 -07:00
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"Nonpointer polymorphic argument '%s' not allowed in a "
|
|
|
|
"locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IsAssumedSizeArray(symbol)) { // C1128
|
|
|
|
SayWithDecl(name, symbol,
|
|
|
|
"Assumed size array '%s' not allowed in a locality-spec"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO: Check to see if the name can appear in a variable definition context
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:35:24 -07:00
|
|
|
Symbol &DeclarationVisitor::FindOrDeclareEnclosingEntity(
|
|
|
|
const parser::Name &name) {
|
2019-07-02 12:10:09 -07:00
|
|
|
Symbol *prev{FindSymbol(name)};
|
2019-01-29 14:31:47 -08:00
|
|
|
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{});
|
2019-06-04 13:35:59 -07:00
|
|
|
ConvertToObjectEntity(*prev);
|
2019-01-29 14:31:47 -08:00
|
|
|
ApplyImplicitRules(*prev);
|
|
|
|
}
|
2019-07-08 15:35:24 -07:00
|
|
|
return *prev;
|
2019-07-03 17:14:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *DeclarationVisitor::DeclareLocalEntity(const parser::Name &name) {
|
2019-07-08 15:35:24 -07:00
|
|
|
Symbol &prev{FindOrDeclareEnclosingEntity(name)};
|
|
|
|
if (!PassesLocalityChecks(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, {})};
|
2019-07-08 15:35:24 -07:00
|
|
|
if (auto *type{prev.GetType()}) {
|
2019-07-03 17:14:12 -07:00
|
|
|
symbol.SetType(*type);
|
2019-07-08 15:35:24 -07:00
|
|
|
symbol.set(Symbol::Flag::Implicit, prev.test(Symbol::Flag::Implicit));
|
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) {
|
2019-07-08 16:02:50 -07:00
|
|
|
// Subtlety: Don't let a "*length" specifier (if any is pending) affect the
|
|
|
|
// declaration of this implied DO loop control variable.
|
|
|
|
auto save{common::ScopedSet(charInfo_.length, std::optional<ParamValue>{})};
|
2019-02-22 15:56:39 -08:00
|
|
|
SetType(name, *declTypeSpec);
|
|
|
|
} else {
|
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
}
|
|
|
|
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};
|
2019-07-08 14:07:56 -07:00
|
|
|
if (charInfo_.length.has_value()) { // Declaration has "*length" (R723)
|
|
|
|
auto length{std::move(*charInfo_.length)};
|
|
|
|
charInfo_.length.reset();
|
|
|
|
if (type.category() == DeclTypeSpec::Character) {
|
|
|
|
auto kind{type.characterTypeSpec().kind()};
|
|
|
|
// Recurse with correct type.
|
|
|
|
SetType(name,
|
|
|
|
currScope().MakeCharacterType(std::move(length), std::move(kind)));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Say(name,
|
|
|
|
"A length specifier cannot be used to declare the non-character entity '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
2019-04-01 13:08:57 -07:00
|
|
|
symbol = &symbol->GetUltimate();
|
2018-08-29 11:38:12 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:11:54 -07:00
|
|
|
void DeclarationVisitor::CheckExplicitInterface(Symbol &symbol) {
|
|
|
|
if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
|
|
|
if (const Symbol * interface{details->interface().symbol()}) {
|
|
|
|
if (!interface->HasExplicitInterface() && !context().HasError(symbol)) {
|
|
|
|
if (!context().HasError(*interface)) {
|
|
|
|
Say(symbol.name(),
|
|
|
|
"The interface of '%s' is not an abstract interface or a "
|
|
|
|
"procedure with an explicit interface"_err_en_US);
|
|
|
|
}
|
|
|
|
context().SetError(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-03-18 11:48:02 -07:00
|
|
|
return Resolve(name, MakeTypeSymbol(name.source, std::move(details)));
|
|
|
|
}
|
|
|
|
Symbol *DeclarationVisitor::MakeTypeSymbol(
|
|
|
|
const SourceName &name, Details &&details) {
|
2018-08-31 16:20:00 -07:00
|
|
|
Scope &derivedType{currScope()};
|
2019-07-11 09:27:25 -07:00
|
|
|
CHECK(derivedType.IsDerivedType());
|
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);
|
|
|
|
}
|
2019-04-08 16:16:55 -07:00
|
|
|
Symbol &result{MakeSymbol(name, attrs, std::move(details))};
|
2018-12-04 10:55:32 -08:00
|
|
|
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;) {
|
2019-07-11 09:27:25 -07:00
|
|
|
CHECK(scope->IsDerivedType());
|
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
|
|
|
|
2019-06-20 09:56:59 -07:00
|
|
|
void ConstructVisitor::ResolveIndexName(
|
|
|
|
const parser::ConcurrentControl &control) {
|
|
|
|
const parser::Name &name{std::get<parser::Name>(control.t)};
|
2019-06-18 15:46:51 -07:00
|
|
|
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 {
|
2019-06-20 11:45:17 -07:00
|
|
|
if (const auto *type{prev->GetType()}) {
|
2019-06-18 15:46:51 -07:00
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
|
|
|
if (prev->IsObjectArray()) {
|
|
|
|
SayWithDecl(name, *prev, "Index variable '%s' is not scalar"_err_en_US);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EvaluateExpr(parser::Scalar{parser::Integer{common::Clone(name)}});
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
2019-06-18 15:46:51 -07:00
|
|
|
|
2019-06-20 09:56:59 -07:00
|
|
|
void ConstructVisitor::ResolveControlExpressions(
|
2019-06-18 15:46:51 -07:00
|
|
|
const parser::ConcurrentControl &control) {
|
2019-06-20 09:56:59 -07:00
|
|
|
Walk(std::get<1>(control.t)); // Initial expression
|
|
|
|
Walk(std::get<2>(control.t)); // Final expression
|
|
|
|
Walk(std::get<3>(control.t)); // Step expression
|
2019-06-18 15:46:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to make sure that all of the index-names get declared before the
|
|
|
|
// expressions in the loop control are evaluated so that references to the
|
|
|
|
// index-names in the expressions are correctly detected.
|
|
|
|
bool ConstructVisitor::Pre(const parser::ConcurrentHeader &header) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
|
|
|
|
// Process the type spec, if present
|
2019-06-20 09:56:59 -07:00
|
|
|
const auto &typeSpec{
|
|
|
|
std::get<std::optional<parser::IntegerTypeSpec>>(header.t)};
|
2019-06-18 15:46:51 -07:00
|
|
|
if (typeSpec.has_value()) {
|
|
|
|
SetDeclTypeSpec(MakeNumericType(TypeCategory::Integer, typeSpec->v));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the index-name nodes in the ConcurrentControl nodes
|
2019-06-20 11:45:17 -07:00
|
|
|
const auto &controls{
|
|
|
|
std::get<std::list<parser::ConcurrentControl>>(header.t)};
|
|
|
|
for (const auto &control : controls) {
|
2019-06-20 09:56:59 -07:00
|
|
|
ResolveIndexName(control);
|
2019-06-18 15:46:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the expressions in ConcurrentControls
|
2019-06-20 11:45:17 -07:00
|
|
|
for (const auto &control : controls) {
|
2019-06-20 09:56:59 -07:00
|
|
|
ResolveControlExpressions(control);
|
2019-06-18 15:46:51 -07:00
|
|
|
}
|
|
|
|
|
2019-06-20 09:56:59 -07:00
|
|
|
// Resolve the names in the scalar-mask-expr
|
|
|
|
Walk(std::get<std::optional<parser::ScalarLogicalExpr>>(header.t));
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
EndDeclTypeSpec();
|
2019-06-18 15:46:51 -07:00
|
|
|
return false;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-06-18 15:46:51 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
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;
|
|
|
|
}
|
2019-06-27 12:33:36 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
|
2019-07-02 12:10:09 -07:00
|
|
|
for (const auto &name : x.v) {
|
2019-07-08 15:35:24 -07:00
|
|
|
if (!FindSymbol(name)) {
|
2019-07-10 12:53:03 -07:00
|
|
|
Say(name, "Variable '%s' with SHARED locality implicitly declared"_en_US);
|
2019-07-08 15:35:24 -07:00
|
|
|
}
|
|
|
|
Symbol &prev{FindOrDeclareEnclosingEntity(name)};
|
|
|
|
if (PassesSharedLocalityChecks(name, prev)) {
|
|
|
|
auto &symbol{MakeSymbol(name, HostAssocDetails{prev})};
|
2019-07-02 12:10:09 -07:00
|
|
|
symbol.set(Symbol::Flag::LocalityShared);
|
|
|
|
name.symbol = &symbol; // override resolution to parent
|
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)};
|
2019-05-09 08:32:27 -07:00
|
|
|
auto &bounds{std::get<parser::AcImpliedDoControl::Bounds>(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)};
|
2019-05-09 08:32:27 -07:00
|
|
|
auto &bounds{std::get<parser::DataImpliedDo::Bounds>(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) {
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const common::Indirection<parser::Variable> &y) {
|
|
|
|
Walk(y.value());
|
|
|
|
},
|
|
|
|
[&](const parser::DataImpliedDo &y) {
|
|
|
|
PushScope(Scope::Kind::ImpliedDos, nullptr);
|
|
|
|
Walk(y);
|
|
|
|
PopScope();
|
|
|
|
},
|
|
|
|
},
|
2019-02-06 17:18:02 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-04-15 10:26:20 -07:00
|
|
|
association_.selector = ResolveSelector(x);
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 10:26:20 -07:00
|
|
|
bool ConstructVisitor::Pre(const parser::ChangeTeamStmt &x) {
|
|
|
|
CheckDef(x.t);
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::CoarrayAssociation &x) {
|
|
|
|
const auto &decl{std::get<parser::CodimensionDecl>(x.t)};
|
|
|
|
const auto &name{std::get<parser::Name>(decl.t)};
|
|
|
|
if (auto *symbol{FindInScope(currScope(), name)}) {
|
|
|
|
const auto &selector{std::get<parser::Selector>(x.t)};
|
|
|
|
if (auto sel{ResolveSelector(selector)}) {
|
2019-06-06 14:34:54 -07:00
|
|
|
const Symbol *whole{UnwrapWholeSymbolDataRef(sel.expr)};
|
|
|
|
if (!whole || whole->Corank() == 0) {
|
2019-04-15 10:26:20 -07:00
|
|
|
Say(sel.source, // C1116
|
|
|
|
"Selector in coarray association must name a coarray"_err_en_US);
|
|
|
|
} else if (auto dynType{sel.expr->GetType()}) {
|
|
|
|
if (!symbol->GetType()) {
|
|
|
|
symbol->SetType(ToDeclTypeSpec(std::move(*dynType)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::EndChangeTeamStmt &x) {
|
|
|
|
PopScope();
|
|
|
|
CheckRef(x.t);
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
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;
|
2019-04-15 10:26:20 -07:00
|
|
|
} else {
|
2019-06-19 15:04:57 -07:00
|
|
|
if (const Symbol *
|
|
|
|
whole{UnwrapWholeSymbolDataRef(association_.selector.expr)}) {
|
|
|
|
ConvertToObjectEntity(const_cast<Symbol &>(*whole));
|
|
|
|
if (!IsVariableName(*whole)) {
|
|
|
|
Say(association_.selector.source, // C901
|
|
|
|
"Selector is not a variable"_err_en_US);
|
|
|
|
association_ = {};
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-15 10:26:20 -07:00
|
|
|
Say(association_.selector.source, // C1157
|
|
|
|
"Selector is not a named variable: 'associate-name =>' is required"_err_en_US);
|
|
|
|
association_ = {};
|
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2019-06-19 15:04:57 -07:00
|
|
|
Symbol *symbol{nullptr};
|
|
|
|
if (association_.name) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else if (const Symbol *
|
|
|
|
whole{UnwrapWholeSymbolDataRef(association_.selector.expr)}) {
|
|
|
|
symbol = &MakeSymbol(whole->name());
|
|
|
|
} else {
|
2019-01-15 16:59:20 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-15 10:26:20 -07:00
|
|
|
if (auto &expr{association_.selector.expr}) {
|
2019-06-19 15:04:57 -07:00
|
|
|
symbol->set_details(AssocEntityDetails{common::Clone(*expr)});
|
2019-01-17 16:14:36 -08:00
|
|
|
} else {
|
2019-06-19 15:04:57 -07:00
|
|
|
symbol->set_details(AssocEntityDetails{});
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
2019-06-19 15:04:57 -07:00
|
|
|
return symbol;
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
|
2019-06-06 14:34:54 -07:00
|
|
|
// Set the type of symbol based on the current association selector.
|
2019-01-15 16:59:20 -08:00
|
|
|
void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
|
2019-06-06 14:34:54 -07:00
|
|
|
auto &details{symbol.get<AssocEntityDetails>()};
|
|
|
|
const MaybeExpr *pexpr{&details.expr()};
|
|
|
|
if (!pexpr->has_value()) {
|
|
|
|
pexpr = &association_.selector.expr;
|
|
|
|
}
|
|
|
|
if (pexpr->has_value()) {
|
|
|
|
const SomeExpr &expr{**pexpr};
|
|
|
|
if (evaluate::IsVariable(expr)) {
|
|
|
|
if (const Symbol * varSymbol{evaluate::GetLastSymbol(expr)}) {
|
|
|
|
if (const DeclTypeSpec * type{varSymbol->GetType()}) {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
return;
|
2019-01-17 16:14:36 -08:00
|
|
|
}
|
2019-06-06 14:34:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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))));
|
2019-01-17 13:52:10 -08:00
|
|
|
} else {
|
2019-06-06 14:34:54 -07:00
|
|
|
symbol.SetType(ToDeclTypeSpec(std::move(*type)));
|
2019-01-17 13:52:10 -08:00
|
|
|
}
|
2019-06-06 14:34:54 -07:00
|
|
|
} else {
|
|
|
|
// BOZ literals, procedure designators, &c. are not acceptable
|
|
|
|
Say(symbol.name(), "Associate name '%s' must have a type"_err_en_US);
|
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) {
|
2019-06-06 14:34:54 -07:00
|
|
|
Attrs attrs{evaluate::GetAttrs(association_.selector.expr)};
|
|
|
|
symbol.attrs() |= attrs &
|
|
|
|
Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE, Attr::CONTIGUOUS};
|
|
|
|
if (attrs.test(Attr::POINTER)) {
|
|
|
|
symbol.attrs().set(Attr::TARGET);
|
2019-01-15 16:59:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 10:26:20 -07:00
|
|
|
ConstructVisitor::Selector ConstructVisitor::ResolveSelector(
|
|
|
|
const parser::Selector &x) {
|
2019-07-10 17:49:28 -07:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Expr &expr) {
|
|
|
|
return Selector{expr.source, EvaluateExpr(expr)};
|
|
|
|
},
|
|
|
|
[&](const parser::Variable &var) {
|
|
|
|
return Selector{var.GetSource(), EvaluateExpr(var)};
|
|
|
|
},
|
|
|
|
},
|
2019-04-15 10:26:20 -07:00
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
|
2019-01-17 13:52:10 -08:00
|
|
|
evaluate::DynamicType &&type) {
|
2019-05-13 09:33:18 -07:00
|
|
|
switch (type.category()) {
|
2019-01-15 16:59:20 -08:00
|
|
|
case common::TypeCategory::Integer:
|
|
|
|
case common::TypeCategory::Real:
|
|
|
|
case common::TypeCategory::Complex:
|
2019-05-13 09:33:18 -07:00
|
|
|
return context().MakeNumericType(type.category(), type.kind());
|
2019-01-15 16:59:20 -08:00
|
|
|
case common::TypeCategory::Logical:
|
2019-05-13 09:33:18 -07:00
|
|
|
return context().MakeLogicalType(type.kind());
|
2019-01-15 16:59:20 -08:00
|
|
|
case common::TypeCategory::Derived:
|
2019-07-01 16:54:53 -07:00
|
|
|
if (type.IsAssumedType()) {
|
|
|
|
return currScope().MakeTypeStarType();
|
|
|
|
} else if (type.IsUnlimitedPolymorphic()) {
|
|
|
|
return currScope().MakeClassStarType();
|
|
|
|
} else {
|
|
|
|
return currScope().MakeDerivedType(type.IsPolymorphic()
|
|
|
|
? DeclTypeSpec::ClassDerived
|
|
|
|
: DeclTypeSpec::TypeDerived,
|
|
|
|
DerivedTypeSpec{type.GetDerivedTypeSpec()});
|
|
|
|
}
|
2019-01-17 13:52:10 -08:00
|
|
|
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(
|
2019-07-03 10:25:43 -07:00
|
|
|
evaluate::DynamicType &&type, MaybeSubscriptIntExpr &&length) {
|
2019-05-13 09:33:18 -07:00
|
|
|
CHECK(type.category() == common::TypeCategory::Character);
|
2019-07-03 10:25:43 -07:00
|
|
|
if (length.has_value()) {
|
|
|
|
return currScope().MakeCharacterType(
|
|
|
|
ParamValue{SomeIntExpr{*std::move(length)}}, KindExpr{type.kind()});
|
|
|
|
} else {
|
|
|
|
return currScope().MakeCharacterType(
|
|
|
|
ParamValue::Deferred(), KindExpr{type.kind()});
|
|
|
|
}
|
2019-01-17 13:52:10 -08:00
|
|
|
}
|
|
|
|
|
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:
|
2019-07-11 09:27:25 -07:00
|
|
|
if (scope.parent().IsGlobal()) {
|
2018-08-22 16:05:06 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *DeclarationVisitor::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
|
|
|
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *DeclarationVisitor::ResolveDesignator(
|
|
|
|
const parser::Designator &x) {
|
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::DataRef &x) { return ResolveDataRef(x); },
|
|
|
|
[&](const parser::Substring &x) {
|
|
|
|
return ResolveDataRef(std::get<parser::DataRef>(x.t));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
2018-08-29 11:38:12 -07:00
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *DeclarationVisitor::ResolveDataRef(
|
2018-11-16 12:43:08 -08:00
|
|
|
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) {
|
2019-03-05 12:28:08 -08:00
|
|
|
return ResolveStructureComponent(y.value());
|
2018-06-22 08:21:19 -07:00
|
|
|
},
|
2019-04-15 10:26:20 -07:00
|
|
|
[=](const auto &y) { return ResolveDataRef(y.value().base); },
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser::Name *DeclarationVisitor::ResolveVariable(
|
|
|
|
const parser::Variable &x) {
|
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const common::Indirection<parser::Designator> &y) {
|
|
|
|
return ResolveDesignator(y.value());
|
2018-08-29 11:38:12 -07:00
|
|
|
},
|
2019-04-15 10:26:20 -07:00
|
|
|
[&](const common::Indirection<parser::FunctionReference> &y) {
|
|
|
|
const auto &proc{
|
|
|
|
std::get<parser::ProcedureDesignator>(y.value().v.t)};
|
2019-07-10 17:49:28 -07:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Name &z) { return &z; },
|
|
|
|
[&](const parser::ProcComponentRef &z) {
|
|
|
|
return ResolveStructureComponent(z.v.thing);
|
|
|
|
},
|
|
|
|
},
|
2019-04-15 10:26:20 -07:00
|
|
|
proc.u);
|
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.
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
|
2019-05-21 16:58:46 -07:00
|
|
|
if (Symbol * symbol{FindSymbol(name)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (CheckUseError(name)) {
|
2018-10-10 16:20:46 -07:00
|
|
|
return nullptr; // reported an error
|
|
|
|
}
|
2019-05-21 16:58:46 -07:00
|
|
|
if (symbol->IsDummy()) {
|
2019-06-04 13:35:59 -07:00
|
|
|
ConvertToObjectEntity(*symbol);
|
2019-05-21 16:58:46 -07:00
|
|
|
ApplyImplicitRules(*symbol);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2019-06-04 13:35:59 -07:00
|
|
|
ConvertToObjectEntity(*symbol);
|
2018-10-10 16:20:46 -07:00
|
|
|
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).
|
2019-04-15 10:26:20 -07:00
|
|
|
const parser::Name *DeclarationVisitor::FindComponent(
|
2018-11-16 12:43:08 -08:00
|
|
|
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-06-21 16:13:56 -07:00
|
|
|
// C764, C765
|
|
|
|
void DeclarationVisitor::CheckInitialDataTarget(
|
2019-06-27 17:16:29 -07:00
|
|
|
const Symbol &pointer, const SomeExpr &expr, SourceName source) {
|
|
|
|
if (!evaluate::IsInitialDataTarget(expr)) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' cannot be initialized with a reference to a designator with non-constant subscripts"_err_en_US,
|
|
|
|
pointer.name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pointer.Rank() != expr.Rank()) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' of rank %d cannot be initialized with a target of different rank (%d)"_err_en_US,
|
|
|
|
pointer.name(), pointer.Rank(), expr.Rank());
|
|
|
|
return;
|
|
|
|
}
|
2019-06-21 16:13:56 -07:00
|
|
|
if (auto base{evaluate::GetBaseObject(expr)}) {
|
|
|
|
if (const Symbol * baseSym{base->symbol()}) {
|
|
|
|
const Symbol &ultimate{baseSym->GetUltimate()};
|
2019-06-27 17:16:29 -07:00
|
|
|
if (IsAllocatable(ultimate)) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' cannot be initialized with a reference to an allocatable '%s'"_err_en_US,
|
|
|
|
pointer.name(), ultimate.name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ultimate.Corank() > 0) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' cannot be initialized with a reference to a coarray '%s'"_err_en_US,
|
|
|
|
pointer.name(), ultimate.name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ultimate.attrs().test(Attr::TARGET)) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' cannot be initialized with a reference to an object '%s' that lacks the TARGET attribute"_err_en_US,
|
|
|
|
pointer.name(), ultimate.name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ultimate.attrs().test(Attr::SAVE)) {
|
|
|
|
Say(source,
|
|
|
|
"Pointer '%s' cannot be initialized with a reference to an object '%s' that lacks the SAVE attribute"_err_en_US,
|
|
|
|
pointer.name(), ultimate.name());
|
2019-06-21 16:13:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 17:16:29 -07:00
|
|
|
// TODO: check type compatibility
|
|
|
|
// TODO: check non-deferred type parameter values
|
|
|
|
// TODO: check contiguity if pointer is CONTIGUOUS
|
2019-06-21 16:13:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Initialization(const parser::Name &name,
|
|
|
|
const parser::Initialization &init, bool inComponentDecl) {
|
|
|
|
if (name.symbol == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Symbol &ultimate{name.symbol->GetUltimate()};
|
|
|
|
if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
// TODO: check C762 - all bounds and type parameters of component
|
|
|
|
// are colons or constant expressions if component is initialized
|
|
|
|
bool isPointer{false};
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::ConstantExpr &expr) {
|
|
|
|
if (inComponentDecl) {
|
|
|
|
// Can't convert to type of component, which might not yet
|
|
|
|
// be known; that's done later during instantiation.
|
|
|
|
if (MaybeExpr value{EvaluateExpr(expr)}) {
|
|
|
|
details->set_init(std::move(*value));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (MaybeExpr folded{EvaluateConvertedExpr(
|
|
|
|
ultimate, expr, expr.thing.value().source)}) {
|
|
|
|
details->set_init(std::move(*folded));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](const parser::NullInit &) {
|
|
|
|
isPointer = true;
|
|
|
|
details->set_init(SomeExpr{evaluate::NullPointer{}});
|
|
|
|
},
|
|
|
|
[&](const parser::InitialDataTarget &initExpr) {
|
|
|
|
isPointer = true;
|
|
|
|
if (MaybeExpr expr{EvaluateExpr(initExpr)}) {
|
2019-06-27 17:16:29 -07:00
|
|
|
CheckInitialDataTarget(
|
|
|
|
ultimate, *expr, initExpr.value().source);
|
2019-06-21 16:13:56 -07:00
|
|
|
details->set_init(std::move(*expr));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](const std::list<common::Indirection<parser::DataStmtValue>>
|
|
|
|
&list) {
|
|
|
|
if (inComponentDecl) {
|
|
|
|
Say(name,
|
|
|
|
"Component '%s' initialized with DATA statement values"_err_en_US);
|
|
|
|
} else {
|
|
|
|
// TODO - DATA statements and DATA-like initialization extension
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
init.u);
|
|
|
|
if (isPointer) {
|
|
|
|
if (!IsPointer(ultimate)) {
|
|
|
|
Say(name,
|
|
|
|
"Non-pointer component '%s' initialized with pointer target"_err_en_US);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IsPointer(ultimate)) {
|
|
|
|
Say(name,
|
|
|
|
"Object pointer component '%s' initialized with non-pointer expression"_err_en_US);
|
|
|
|
} else if (IsAllocatable(ultimate)) {
|
|
|
|
Say(name, "Allocatable component '%s' cannot be initialized"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-07-11 11:34:01 -07:00
|
|
|
if (context().intrinsics().IsIntrinsic(name.source.ToString())) {
|
2019-07-11 15:11:40 -07:00
|
|
|
symbol =
|
|
|
|
&MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC});
|
|
|
|
} else {
|
|
|
|
symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
|
2019-07-11 11:34:01 -07:00
|
|
|
}
|
2019-02-21 15:01:39 -08:00
|
|
|
Resolve(name, *symbol);
|
|
|
|
if (symbol->has<ModuleDetails>()) {
|
|
|
|
SayWithDecl(name, *symbol,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-11 11:34:01 -07:00
|
|
|
if (!symbol->attrs().test(Attr::INTRINSIC)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
MakeExternal(*symbol);
|
2019-02-21 15:01:39 -08:00
|
|
|
}
|
|
|
|
if (!symbol->has<ProcEntityDetails>()) {
|
2018-09-22 08:05:46 -07:00
|
|
|
ConvertToProcEntity(*symbol);
|
2019-02-21 15:01:39 -08:00
|
|
|
}
|
|
|
|
SetProcFlag(name, *symbol, flag);
|
|
|
|
} else if (symbol->has<UnknownDetails>()) {
|
|
|
|
CHECK(!"unexpected UnknownDetails");
|
|
|
|
} else if (CheckUseError(name)) {
|
|
|
|
// error was reported
|
|
|
|
} else {
|
2019-06-26 14:51:58 -07:00
|
|
|
symbol = &Resolve(name, symbol)->GetUltimate();
|
2019-02-21 15:01:39 -08:00
|
|
|
ConvertToProcEntity(*symbol);
|
|
|
|
if (!SetProcFlag(name, *symbol, flag)) {
|
|
|
|
return; // reported error
|
|
|
|
}
|
2019-07-11 11:34:01 -07:00
|
|
|
if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
|
2019-02-21 15:01:39 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:51:58 -07:00
|
|
|
// Variant of HandleProcedureName() for use while skimming the executable
|
|
|
|
// part of a subprogram to catch calls that might be part of the subprogram's
|
|
|
|
// interface, and to mark as procedures any symbols that might otherwise be
|
|
|
|
// miscategorized as objects.
|
|
|
|
void ResolveNamesVisitor::NoteExecutablePartCall(
|
|
|
|
Symbol::Flag flag, const parser::Call &call) {
|
|
|
|
auto &designator{std::get<parser::ProcedureDesignator>(call.t)};
|
|
|
|
if (const auto *name{std::get_if<parser::Name>(&designator.u)}) {
|
|
|
|
if (Symbol * symbol{FindSymbol(*name)}) {
|
|
|
|
Symbol::Flag other{flag == Symbol::Flag::Subroutine
|
|
|
|
? Symbol::Flag::Function
|
|
|
|
: Symbol::Flag::Subroutine};
|
|
|
|
if (!symbol->test(other)) {
|
|
|
|
ConvertToProcEntity(*symbol);
|
2019-06-27 09:57:48 -07:00
|
|
|
if (symbol->has<ProcEntityDetails>()) {
|
2019-06-26 14:51:58 -07:00
|
|
|
symbol->set(flag);
|
|
|
|
if (symbol->IsDummy()) {
|
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
|
|
|
}
|
|
|
|
ApplyImplicitRules(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2019-05-06 07:26:43 -07:00
|
|
|
} else if (symbol.GetType() != nullptr && flag == Symbol::Flag::Subroutine) {
|
|
|
|
SayWithDecl(
|
|
|
|
name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
|
2018-12-18 16:35:31 -08:00
|
|
|
}
|
|
|
|
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) {
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[=](const parser::Name &y) {
|
|
|
|
Resolve(y, SetAccess(y.source, accessAttr));
|
|
|
|
},
|
|
|
|
[=](const Indirection<parser::GenericSpec> &y) {
|
|
|
|
auto info{GenericSpecInfo{y.value()}};
|
|
|
|
info.Resolve(&SetAccess(info.symbolName(), accessAttr));
|
|
|
|
},
|
|
|
|
},
|
2018-04-24 17:05:58 -07:00
|
|
|
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.
|
2019-03-18 11:48:02 -07:00
|
|
|
Symbol &ModuleVisitor::SetAccess(const SourceName &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;
|
2019-03-18 11:48:02 -07:00
|
|
|
auto msg{IsDefinedOperator(name)
|
|
|
|
? "The accessibility of operator '%s' has already been specified as %s"_en_US
|
|
|
|
: "The accessibility of '%s' has already been specified as %s"_en_US};
|
|
|
|
Say(name, WithIsFatal(msg, attr != prev), name, 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
|
|
|
}
|
2019-03-18 11:48:02 -07:00
|
|
|
return symbol;
|
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();
|
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();
|
2019-06-11 18:26:48 -07:00
|
|
|
CheckEquivalenceSets();
|
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,
|
2019-05-07 09:22:53 -07:00
|
|
|
symbol->name());
|
2018-08-22 16:05:06 -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) {
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
|
|
|
[&](const parser::StructureComponent &x) {
|
|
|
|
ResolveStructureComponent(x);
|
|
|
|
},
|
|
|
|
},
|
2018-10-10 16:20:46 -07:00
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
|
2019-07-10 17:49:28 -07:00
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
|
|
|
[&](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-04-12 16:30:03 -07:00
|
|
|
|
2019-02-26 14:26:28 -08:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::PointerAssignmentStmt &x) {
|
2019-03-19 15:30:25 -07:00
|
|
|
const auto &dataRef{std::get<parser::DataRef>(x.t)};
|
2019-04-02 15:36:20 -07:00
|
|
|
const auto &bounds{std::get<parser::PointerAssignmentStmt::Bounds>(x.t)};
|
2019-02-26 14:26:28 -08:00
|
|
|
const auto &expr{std::get<parser::Expr>(x.t)};
|
2019-04-02 15:36:20 -07:00
|
|
|
ResolveDataRef(dataRef);
|
|
|
|
Walk(bounds);
|
2019-03-19 15:30:25 -07:00
|
|
|
// Resolve unrestricted specific intrinsic procedures as in "p => cos".
|
2019-04-16 15:59:04 -07:00
|
|
|
if (const parser::Name * name{parser::Unwrap<parser::Name>(expr)}) {
|
2019-04-12 16:30:03 -07:00
|
|
|
if (NameIsKnownOrIntrinsic(*name)) {
|
|
|
|
return false;
|
2019-02-26 14:26:28 -08:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 15:36:20 -07:00
|
|
|
Walk(expr);
|
|
|
|
return false;
|
2019-02-26 14:26:28 -08:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::Designator &x) {
|
2019-04-15 10:26:20 -07:00
|
|
|
ResolveDesignator(x);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-11-06 17:18:06 -08: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
|
|
|
|
2019-03-18 11:48:02 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::DefinedOpName &x) {
|
|
|
|
const parser::Name &name{x.v};
|
|
|
|
if (FindSymbol(name)) {
|
|
|
|
// OK
|
|
|
|
} else if (IsLogicalConstant(context(), name.source)) {
|
|
|
|
Say(name,
|
|
|
|
"Logical constant '%s' may not be used as a defined operator"_err_en_US);
|
|
|
|
} else {
|
|
|
|
Say(name, "Defined operator '%s' not found"_err_en_US);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
|
|
|
|
auto root{ProgramTree::Build(x)};
|
|
|
|
SetScope(context().globalScope());
|
|
|
|
ResolveSpecificationParts(root);
|
2019-06-23 10:59:32 -07:00
|
|
|
FinishSpecificationParts(root);
|
2019-05-06 07:26:43 -07:00
|
|
|
ResolveExecutionParts(root);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 14:46:02 -07:00
|
|
|
// References to procedures need to record that their symbols are known
|
2019-06-04 13:35:59 -07:00
|
|
|
// to be procedures, so that they don't get converted to objects by default.
|
|
|
|
class ExecutionPartSkimmer {
|
|
|
|
public:
|
2019-06-26 14:51:58 -07:00
|
|
|
explicit ExecutionPartSkimmer(ResolveNamesVisitor &resolver)
|
|
|
|
: resolver_{resolver} {}
|
2019-06-04 13:35:59 -07:00
|
|
|
|
|
|
|
void Walk(const parser::ExecutionPart *exec) {
|
|
|
|
if (exec != nullptr) {
|
|
|
|
parser::Walk(*exec, *this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A> bool Pre(const A &) { return true; }
|
|
|
|
template<typename A> void Post(const A &) {}
|
|
|
|
void Post(const parser::FunctionReference &fr) {
|
2019-06-26 14:51:58 -07:00
|
|
|
resolver_.NoteExecutablePartCall(Symbol::Flag::Function, fr.v);
|
2019-06-04 13:35:59 -07:00
|
|
|
}
|
|
|
|
void Post(const parser::CallStmt &cs) {
|
2019-06-26 14:51:58 -07:00
|
|
|
resolver_.NoteExecutablePartCall(Symbol::Flag::Subroutine, cs.v);
|
2019-06-04 13:35:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-25 14:46:02 -07:00
|
|
|
ResolveNamesVisitor &resolver_;
|
2019-06-04 13:35:59 -07:00
|
|
|
};
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
// Build the scope tree and resolve names in the specification parts of this
|
|
|
|
// node and its children
|
|
|
|
void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
|
|
|
|
if (!BeginScope(node)) {
|
|
|
|
return; // an error prevented scope from being created
|
|
|
|
}
|
|
|
|
Scope &scope{currScope()};
|
|
|
|
node.set_scope(scope);
|
|
|
|
AddSubpNames(node);
|
|
|
|
std::visit([&](const auto *x) { Walk(*x); }, node.stmt());
|
|
|
|
Walk(node.spec());
|
|
|
|
if (node.IsModule()) {
|
|
|
|
ApplyDefaultAccess();
|
|
|
|
}
|
2019-06-04 13:35:59 -07:00
|
|
|
for (auto &child : node.children()) {
|
|
|
|
ResolveSpecificationParts(child);
|
|
|
|
}
|
2019-06-26 14:51:58 -07:00
|
|
|
ExecutionPartSkimmer{*this}.Walk(node.exec());
|
2019-06-24 15:22:57 -07:00
|
|
|
PopScope();
|
2019-06-26 14:51:58 -07:00
|
|
|
// Ensure that every object entity has a type.
|
2019-06-24 15:22:57 -07:00
|
|
|
for (auto &pair : *node.scope()) {
|
|
|
|
ApplyImplicitRules(*pair.second);
|
2019-06-06 17:09:49 -07:00
|
|
|
}
|
2019-05-06 07:26:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add SubprogramNameDetails symbols for contained subprograms
|
|
|
|
void ResolveNamesVisitor::AddSubpNames(const ProgramTree &node) {
|
|
|
|
auto kind{
|
|
|
|
node.IsModule() ? SubprogramKind::Module : SubprogramKind::Internal};
|
|
|
|
for (const auto &child : node.children()) {
|
|
|
|
auto &symbol{MakeSymbol(child.name(), SubprogramNameDetails{kind})};
|
|
|
|
symbol.set(child.GetSubpFlag());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push a new scope for this node or return false on error.
|
|
|
|
bool ResolveNamesVisitor::BeginScope(const ProgramTree &node) {
|
|
|
|
switch (node.GetKind()) {
|
|
|
|
case ProgramTree::Kind::Program:
|
|
|
|
PushScope(Scope::Kind::MainProgram,
|
|
|
|
&MakeSymbol(node.name(), MainProgramDetails{}));
|
|
|
|
return true;
|
|
|
|
case ProgramTree::Kind::Function:
|
|
|
|
case ProgramTree::Kind::Subroutine:
|
|
|
|
return BeginSubprogram(
|
|
|
|
node.name(), node.GetSubpFlag(), node.HasModulePrefix());
|
|
|
|
case ProgramTree::Kind::MpSubprogram:
|
|
|
|
return BeginSubprogram(
|
|
|
|
node.name(), Symbol::Flag::Subroutine, /*hasModulePrefix*/ true);
|
|
|
|
case ProgramTree::Kind::Module: BeginModule(node.name(), false); return true;
|
|
|
|
case ProgramTree::Kind::Submodule:
|
|
|
|
return BeginSubmodule(node.name(), node.GetParentId());
|
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 10:59:32 -07:00
|
|
|
// Perform checks that need to happen after all of the specification parts
|
|
|
|
// but before any of the execution parts.
|
|
|
|
void ResolveNamesVisitor::FinishSpecificationParts(const ProgramTree &node) {
|
|
|
|
if (!node.scope()) {
|
|
|
|
return; // error occurred creating scope
|
|
|
|
}
|
|
|
|
SetScope(*node.scope());
|
2019-07-15 17:11:54 -07:00
|
|
|
for (auto &pair : currScope()) {
|
|
|
|
Symbol &symbol{*pair.second};
|
2019-07-02 14:00:44 -07:00
|
|
|
if (const auto *details{symbol.detailsIf<GenericDetails>()}) {
|
|
|
|
CheckSpecificsAreDistinguishable(symbol, details->specificProcs());
|
2019-07-15 17:11:54 -07:00
|
|
|
} else if (symbol.has<ProcEntityDetails>()) {
|
|
|
|
CheckExplicitInterface(symbol);
|
2019-07-02 14:00:44 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 10:59:32 -07:00
|
|
|
for (Scope &childScope : currScope().children()) {
|
2019-07-11 09:27:25 -07:00
|
|
|
if (childScope.IsDerivedType() && childScope.symbol()) {
|
2019-06-23 10:59:32 -07:00
|
|
|
FinishDerivedType(childScope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &child : node.children()) {
|
|
|
|
FinishSpecificationParts(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FindIndexOfName(
|
|
|
|
const SourceName &name, std::vector<Symbol *> symbols) {
|
|
|
|
for (std::size_t i{0}; i < symbols.size(); ++i) {
|
|
|
|
if (symbols[i] && symbols[i]->name() == name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform checks on procedure bindings of this type
|
|
|
|
void ResolveNamesVisitor::FinishDerivedType(Scope &scope) {
|
2019-07-11 09:27:25 -07:00
|
|
|
CHECK(scope.IsDerivedType());
|
2019-06-23 10:59:32 -07:00
|
|
|
for (auto &pair : scope) {
|
|
|
|
Symbol &comp{*pair.second};
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](ProcEntityDetails &x) {
|
2019-07-12 12:12:12 -07:00
|
|
|
SetPassArg(comp, x.interface().symbol(), x);
|
2019-06-23 10:59:32 -07:00
|
|
|
},
|
2019-07-12 12:12:12 -07:00
|
|
|
[&](ProcBindingDetails &x) { SetPassArg(comp, &x.symbol(), x); },
|
|
|
|
[](auto &x) {},
|
|
|
|
},
|
|
|
|
comp.details());
|
|
|
|
}
|
|
|
|
for (auto &pair : scope) {
|
|
|
|
Symbol &comp{*pair.second};
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
2019-07-02 14:00:44 -07:00
|
|
|
[&](GenericBindingDetails &x) {
|
|
|
|
CheckSpecificsAreDistinguishable(comp, x.specificProcs());
|
|
|
|
},
|
2019-06-23 10:59:32 -07:00
|
|
|
[](auto &x) {},
|
|
|
|
},
|
|
|
|
comp.details());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check C760, constraints on the passed-object dummy argument
|
2019-07-12 12:12:12 -07:00
|
|
|
// If they all pass, set the passIndex in details.
|
|
|
|
void ResolveNamesVisitor::SetPassArg(
|
|
|
|
const Symbol &proc, const Symbol *interface, WithPassArg &details) {
|
2019-06-23 10:59:32 -07:00
|
|
|
if (proc.attrs().test(Attr::NOPASS)) {
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
const auto &name{proc.name()};
|
|
|
|
if (!interface) {
|
|
|
|
Say(name,
|
|
|
|
"Procedure component '%s' must have NOPASS attribute or explicit interface"_err_en_US,
|
|
|
|
name);
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
2019-07-12 12:12:12 -07:00
|
|
|
const SourceName *passName{details.passName()};
|
2019-06-23 10:59:32 -07:00
|
|
|
const auto &dummyArgs{interface->get<SubprogramDetails>().dummyArgs()};
|
|
|
|
if (!passName && dummyArgs.empty()) {
|
|
|
|
Say(name,
|
|
|
|
proc.has<ProcEntityDetails>()
|
|
|
|
? "Procedure component '%s' with no dummy arguments"
|
|
|
|
" must have NOPASS attribute"_err_en_US
|
|
|
|
: "Procedure binding '%s' with no dummy arguments"
|
|
|
|
" must have NOPASS attribute"_err_en_US,
|
|
|
|
name);
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
int passArgIndex{0};
|
|
|
|
if (!passName) {
|
|
|
|
passName = &dummyArgs[0]->name();
|
|
|
|
} else {
|
|
|
|
passArgIndex = FindIndexOfName(*passName, dummyArgs);
|
|
|
|
if (passArgIndex < 0) {
|
|
|
|
Say(*passName,
|
|
|
|
"'%s' is not a dummy argument of procedure interface '%s'"_err_en_US,
|
|
|
|
*passName, interface->name());
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const Symbol &passArg{*dummyArgs[passArgIndex]};
|
|
|
|
std::optional<MessageFixedText> msg;
|
|
|
|
if (!passArg.has<ObjectEntityDetails>()) {
|
|
|
|
msg = "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" must be a data object"_err_en_US;
|
|
|
|
} else if (passArg.attrs().test(Attr::POINTER)) {
|
|
|
|
msg = "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" may not have the POINTER attribute"_err_en_US;
|
|
|
|
} else if (passArg.attrs().test(Attr::ALLOCATABLE)) {
|
|
|
|
msg = "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" may not have the ALLOCATABLE attribute"_err_en_US;
|
|
|
|
} else if (passArg.attrs().test(Attr::VALUE)) {
|
|
|
|
msg = "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" may not have the VALUE attribute"_err_en_US;
|
|
|
|
} else if (passArg.Rank() > 0) {
|
|
|
|
msg = "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" must be scalar"_err_en_US;
|
|
|
|
}
|
|
|
|
if (msg) {
|
|
|
|
Say(name, std::move(*msg), *passName, name);
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
const DeclTypeSpec *type{passArg.GetType()};
|
|
|
|
if (!type) {
|
2019-07-12 12:12:12 -07:00
|
|
|
return; // an error already occurred
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
const Symbol &typeSymbol{*proc.owner().GetSymbol()};
|
|
|
|
const DerivedTypeSpec *derived{type->AsDerived()};
|
|
|
|
if (!derived || derived->typeSymbol() != typeSymbol) {
|
|
|
|
Say(name,
|
|
|
|
"Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" must be of type '%s' but is '%s'"_err_en_US,
|
|
|
|
*passName, name, typeSymbol.name(), type->AsFortran());
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
if (IsExtensibleType(derived) != type->IsPolymorphic()) {
|
|
|
|
Say(name,
|
|
|
|
type->IsPolymorphic()
|
|
|
|
? "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" must not be polymorphic because '%s' is not extensible"_err_en_US
|
|
|
|
: "Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" must polymorphic because '%s' is extensible"_err_en_US,
|
|
|
|
*passName, name, typeSymbol.name());
|
2019-07-12 12:12:12 -07:00
|
|
|
return;
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
for (const auto &[paramName, paramValue] : derived->parameters()) {
|
|
|
|
if (paramValue.isLen() && !paramValue.isAssumed()) {
|
|
|
|
Say(name,
|
|
|
|
"Passed-object dummy argument '%s' of procedure '%s'"
|
|
|
|
" has non-assumed length parameter '%s'"_err_en_US,
|
|
|
|
*passName, name, paramName);
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 12:12:12 -07:00
|
|
|
details.set_passIndex(passArgIndex);
|
2019-06-23 10:59:32 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 07:26:43 -07:00
|
|
|
// Resolve names in the execution part of this node and its children
|
|
|
|
void ResolveNamesVisitor::ResolveExecutionParts(const ProgramTree &node) {
|
|
|
|
if (!node.scope()) {
|
|
|
|
return; // error occurred creating scope
|
|
|
|
}
|
|
|
|
SetScope(*node.scope());
|
|
|
|
if (const auto *exec{node.exec()}) {
|
|
|
|
Walk(*exec);
|
|
|
|
}
|
2019-05-21 16:58:46 -07:00
|
|
|
PopScope(); // converts unclassified entities into objects
|
2019-05-06 07:26:43 -07:00
|
|
|
for (const auto &child : node.children()) {
|
|
|
|
ResolveExecutionParts(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-06 17:07:25 -08:00
|
|
|
bool ResolveNames(SemanticsContext &context, const parser::Program &program) {
|
2018-10-22 07:37:38 -07:00
|
|
|
ResolveNamesVisitor{context}.Walk(program);
|
2019-03-06 17:07:25 -08:00
|
|
|
return !context.AnyFatalError();
|
2018-04-18 15:06:35 -07:00
|
|
|
}
|
|
|
|
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|