2019-01-07 15:42:36 -08:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-05-01 12:50:34 -07:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-12 14:20:26 -07:00
|
|
|
#include "resolve-names.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "attr.h"
|
2018-10-15 17:11:24 -07:00
|
|
|
#include "default-kinds.h"
|
2018-12-06 06:59:37 -08:00
|
|
|
#include "expression.h"
|
2018-07-16 16:23:18 -07:00
|
|
|
#include "mod-file.h"
|
2018-05-02 14:06:02 -07:00
|
|
|
#include "rewrite-parse-tree.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "scope.h"
|
2018-10-22 07:37:38 -07:00
|
|
|
#include "semantics.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "symbol.h"
|
|
|
|
#include "type.h"
|
2018-12-11 14:51:08 -08:00
|
|
|
#include "../common/fortran.h"
|
2018-06-18 11:03:43 -07:00
|
|
|
#include "../common/indirection.h"
|
2018-12-06 06:59:37 -08:00
|
|
|
#include "../evaluate/common.h"
|
|
|
|
#include "../evaluate/fold.h"
|
|
|
|
#include "../evaluate/tools.h"
|
2018-04-12 12:23:20 -07:00
|
|
|
#include "../parser/parse-tree-visitor.h"
|
|
|
|
#include "../parser/parse-tree.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <list>
|
2018-12-18 07:59:40 -08:00
|
|
|
#include <map>
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <memory>
|
2018-05-02 14:06:02 -07:00
|
|
|
#include <ostream>
|
2018-05-03 15:57:56 -07:00
|
|
|
#include <set>
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-23 12:24:29 -07:00
|
|
|
namespace Fortran::semantics {
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
using namespace parser::literals;
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
using Message = parser::Message;
|
|
|
|
using Messages = parser::Messages;
|
|
|
|
using MessageFixedText = parser::MessageFixedText;
|
|
|
|
using MessageFormattedText = parser::MessageFormattedText;
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
class ResolveNamesVisitor;
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
static const parser::Name *GetGenericSpecName(const parser::GenericSpec &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2018-08-27 13:32:10 -07:00
|
|
|
// ImplicitRules maps initial character of identifier to the DeclTypeSpec
|
|
|
|
// representing the implicit type; std::nullopt if none.
|
|
|
|
// It also records the presence of IMPLICIT NONE statements.
|
|
|
|
// When inheritFromParent is set, defaults come from the parent rules.
|
2018-04-04 09:03:51 -07:00
|
|
|
class ImplicitRules {
|
|
|
|
public:
|
2018-10-22 07:37:38 -07:00
|
|
|
ImplicitRules() : inheritFromParent_{false} {}
|
|
|
|
ImplicitRules(std::unique_ptr<ImplicitRules> &&parent)
|
|
|
|
: inheritFromParent_{parent.get() != nullptr} {
|
2018-08-27 11:48:49 -07:00
|
|
|
parent_.swap(parent);
|
|
|
|
}
|
2018-10-22 07:37:38 -07:00
|
|
|
void set_context(SemanticsContext &context) { context_ = &context; }
|
2018-08-27 11:48:49 -07:00
|
|
|
std::unique_ptr<ImplicitRules> &&parent() { return std::move(parent_); }
|
|
|
|
bool isImplicitNoneType() const;
|
|
|
|
bool isImplicitNoneExternal() const;
|
2018-04-11 13:11:42 -07:00
|
|
|
void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
|
|
|
|
void set_isImplicitNoneExternal(bool x) { isImplicitNoneExternal_ = x; }
|
2018-08-27 11:48:49 -07:00
|
|
|
void set_inheritFromParent(bool x) { inheritFromParent_ = x; }
|
2018-04-04 09:03:51 -07:00
|
|
|
// Get the implicit type for identifiers starting with ch. May be null.
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *GetType(char ch) const;
|
2018-04-04 09:03:51 -07:00
|
|
|
// Record the implicit type for this range of characters.
|
|
|
|
void SetType(const DeclTypeSpec &type, parser::Location lo, parser::Location,
|
|
|
|
bool isDefault = false);
|
|
|
|
|
|
|
|
private:
|
2018-04-05 17:02:31 -07:00
|
|
|
static char Incr(char ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
std::unique_ptr<ImplicitRules> parent_;
|
|
|
|
std::optional<bool> isImplicitNoneType_;
|
|
|
|
std::optional<bool> isImplicitNoneExternal_;
|
|
|
|
bool inheritFromParent_; // look in parent if not specified here
|
2018-10-22 07:37:38 -07:00
|
|
|
SemanticsContext *context_{nullptr};
|
2018-04-05 17:02:31 -07:00
|
|
|
// map initial character of identifier to nullptr or its default type
|
2018-12-11 14:51:08 -08:00
|
|
|
std::map<char, const DeclTypeSpec *> map_;
|
2018-08-27 11:48:49 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const ImplicitRules &);
|
2018-04-06 10:46:30 -07:00
|
|
|
friend void ShowImplicitRule(std::ostream &, const ImplicitRules &, char);
|
2018-04-04 09:03:51 -07:00
|
|
|
};
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
// Track statement source locations and save messages.
|
|
|
|
class MessageHandler {
|
|
|
|
public:
|
|
|
|
void set_messages(Messages &messages) { messages_ = &messages; }
|
|
|
|
const SourceName *currStmtSource() { return currStmtSource_; }
|
|
|
|
void set_currStmtSource(const SourceName *);
|
|
|
|
|
|
|
|
// Emit a message
|
|
|
|
Message &Say(Message &&);
|
|
|
|
// Emit a message associated with the current statement source.
|
|
|
|
Message &Say(MessageFixedText &&);
|
|
|
|
// Emit a message about a SourceName
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&);
|
|
|
|
// Emit a formatted message associated with a source location.
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const SourceName &);
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const SourceName &,
|
|
|
|
const SourceName &);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Where messages are emitted:
|
|
|
|
Messages *messages_{nullptr};
|
|
|
|
// Source location of current statement; null if not in a statement
|
|
|
|
const SourceName *currStmtSource_{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
class BaseVisitor {
|
|
|
|
public:
|
|
|
|
template<typename T> void Walk(const T &);
|
|
|
|
void set_this(ResolveNamesVisitor *x) { this_ = x; }
|
|
|
|
|
|
|
|
MessageHandler &messageHandler() { return messageHandler_; }
|
|
|
|
const SourceName *currStmtSource();
|
|
|
|
SemanticsContext &context() const { return *context_; }
|
|
|
|
void set_context(SemanticsContext &);
|
|
|
|
|
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);
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
template<typename T> MaybeExpr EvaluateExpr(const T &expr) {
|
|
|
|
if (auto maybeExpr{AnalyzeExpr(*context_, expr)}) {
|
|
|
|
return evaluate::Fold(context_->foldingContext(), std::move(*maybeExpr));
|
|
|
|
} else {
|
|
|
|
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)}) {
|
|
|
|
return {std::move(*intExpr)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
MaybeSubscriptIntExpr EvaluateSubscriptIntExpr(const T &expr) {
|
|
|
|
if (MaybeIntExpr maybeIntExpr{EvaluateIntExpr(expr)}) {
|
2019-01-07 16:31:59 -08:00
|
|
|
return evaluate::Fold(context_->foldingContext(),
|
|
|
|
evaluate::ConvertToType<evaluate::SubscriptInteger>(
|
|
|
|
std::move(*maybeIntExpr)));
|
2019-01-07 15:05:53 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
template<typename... A> Message &Say(const parser::Name &name, A... args) {
|
|
|
|
return messageHandler_.Say(name.source, std::forward<A>(args)...);
|
2018-11-16 16:40:00 -08:00
|
|
|
}
|
|
|
|
template<typename... A> Message &Say(A... args) {
|
|
|
|
return messageHandler_.Say(std::forward<A>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ResolveNamesVisitor *this_{nullptr};
|
|
|
|
SemanticsContext *context_{nullptr};
|
|
|
|
MessageHandler messageHandler_;
|
|
|
|
};
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
// Provide Post methods to collect attributes into a member variable.
|
2018-11-16 16:40:00 -08:00
|
|
|
class AttrsVisitor : public virtual BaseVisitor {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
2018-09-05 16:02:41 -07:00
|
|
|
bool BeginAttrs(); // always returns true
|
2018-05-14 13:51:13 -07:00
|
|
|
Attrs GetAttrs();
|
2018-04-12 12:59:42 -07:00
|
|
|
Attrs EndAttrs();
|
2019-01-04 17:10:31 -08:00
|
|
|
bool SetPassNameOn(Symbol &);
|
|
|
|
bool SetBindNameOn(Symbol &);
|
2018-03-30 19:49:00 -07:00
|
|
|
void Post(const parser::LanguageBindingSpec &);
|
|
|
|
bool Pre(const parser::AccessSpec &);
|
|
|
|
bool Pre(const parser::IntentSpec &);
|
2019-01-04 17:10:31 -08:00
|
|
|
bool Pre(const parser::Pass &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Simple case: encountering CLASSNAME causes ATTRNAME to be set.
|
|
|
|
#define HANDLE_ATTR_CLASS(CLASSNAME, ATTRNAME) \
|
|
|
|
bool Pre(const parser::CLASSNAME &) { \
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::ATTRNAME); \
|
2018-03-30 13:57:23 -07:00
|
|
|
return false; \
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Elemental, ELEMENTAL)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Impure, IMPURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Module, MODULE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Non_Recursive, NON_RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Pure, PURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Recursive, RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(TypeAttrSpec::BindC, BIND_C)
|
2018-08-31 16:20:00 -07:00
|
|
|
HANDLE_ATTR_CLASS(BindAttr::Deferred, DEFERRED)
|
|
|
|
HANDLE_ATTR_CLASS(BindAttr::Non_Overridable, NON_OVERRIDABLE)
|
2018-03-30 13:57:23 -07:00
|
|
|
HANDLE_ATTR_CLASS(Abstract, ABSTRACT)
|
|
|
|
HANDLE_ATTR_CLASS(Allocatable, ALLOCATABLE)
|
|
|
|
HANDLE_ATTR_CLASS(Asynchronous, ASYNCHRONOUS)
|
|
|
|
HANDLE_ATTR_CLASS(Contiguous, CONTIGUOUS)
|
|
|
|
HANDLE_ATTR_CLASS(External, EXTERNAL)
|
|
|
|
HANDLE_ATTR_CLASS(Intrinsic, INTRINSIC)
|
|
|
|
HANDLE_ATTR_CLASS(NoPass, NOPASS)
|
|
|
|
HANDLE_ATTR_CLASS(Optional, OPTIONAL)
|
|
|
|
HANDLE_ATTR_CLASS(Parameter, PARAMETER)
|
|
|
|
HANDLE_ATTR_CLASS(Pointer, POINTER)
|
|
|
|
HANDLE_ATTR_CLASS(Protected, PROTECTED)
|
|
|
|
HANDLE_ATTR_CLASS(Save, SAVE)
|
|
|
|
HANDLE_ATTR_CLASS(Target, TARGET)
|
|
|
|
HANDLE_ATTR_CLASS(Value, VALUE)
|
|
|
|
HANDLE_ATTR_CLASS(Volatile, VOLATILE)
|
|
|
|
#undef HANDLE_ATTR_CLASS
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-03-30 19:49:00 -07:00
|
|
|
std::optional<Attrs> attrs_;
|
2018-04-24 17:05:58 -07:00
|
|
|
|
|
|
|
Attr AccessSpecToAttr(const parser::AccessSpec &x) {
|
|
|
|
switch (x.v) {
|
|
|
|
case parser::AccessSpec::Kind::Public: return Attr::PUBLIC;
|
|
|
|
case parser::AccessSpec::Kind::Private: return Attr::PRIVATE;
|
|
|
|
}
|
2018-07-11 17:45:13 -07:00
|
|
|
common::die("unreachable"); // suppress g++ warning
|
|
|
|
}
|
|
|
|
Attr IntentSpecToAttr(const parser::IntentSpec &x) {
|
|
|
|
switch (x.v) {
|
|
|
|
case parser::IntentSpec::Intent::In: return Attr::INTENT_IN;
|
|
|
|
case parser::IntentSpec::Intent::Out: return Attr::INTENT_OUT;
|
|
|
|
case parser::IntentSpec::Intent::InOut: return Attr::INTENT_INOUT;
|
|
|
|
}
|
|
|
|
common::die("unreachable"); // suppress g++ warning
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
MaybeExpr bindName_; // from BIND(C, NAME="...")
|
|
|
|
std::optional<SourceName> passName_; // from PASS(...)
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find and create types from declaration-type-spec nodes.
|
|
|
|
class DeclTypeSpecVisitor : public AttrsVisitor {
|
|
|
|
public:
|
2018-10-22 07:37:38 -07:00
|
|
|
explicit DeclTypeSpecVisitor() {}
|
2018-03-22 17:08:20 -07:00
|
|
|
using AttrsVisitor::Post;
|
2018-03-30 13:57:23 -07:00
|
|
|
using AttrsVisitor::Pre;
|
2018-12-06 06:59:37 -08:00
|
|
|
void Post(const parser::IntegerTypeSpec &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Logical &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Real &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Complex &);
|
|
|
|
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-03-30 13:57:23 -07:00
|
|
|
void Post(const parser::TypeParamSpec &);
|
2018-05-17 13:06:38 -07:00
|
|
|
bool Pre(const parser::TypeGuardStmt &);
|
|
|
|
void Post(const parser::TypeGuardStmt &);
|
2018-12-18 14:35:58 -08:00
|
|
|
bool Pre(const parser::AcSpec &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-12-18 17:19:41 -08:00
|
|
|
struct State {
|
|
|
|
bool expectDeclTypeSpec{false}; // should only see decl-type-spec when true
|
|
|
|
DeclTypeSpec *declTypeSpec{nullptr};
|
|
|
|
const parser::Name *derivedTypeName{nullptr};
|
|
|
|
};
|
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
DeclTypeSpec *GetDeclTypeSpec();
|
2018-06-05 12:18:35 -07:00
|
|
|
void BeginDeclTypeSpec();
|
|
|
|
void EndDeclTypeSpec();
|
2018-12-18 17:19:41 -08:00
|
|
|
State SetDeclTypeSpecState(State);
|
2018-12-18 14:35:58 -08:00
|
|
|
const parser::Name *derivedTypeName() const { return state_.derivedTypeName; }
|
2018-12-11 14:51:08 -08:00
|
|
|
void SetDeclTypeSpec(const parser::Name &, DeclTypeSpec &);
|
2018-12-14 14:04:15 -08:00
|
|
|
void SetDeclTypeSpec(DeclTypeSpec &);
|
|
|
|
ParamValue GetParamValue(const parser::TypeParamValue &);
|
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, const std::optional<parser::KindSelector> &);
|
|
|
|
void MakeNumericType(TypeCategory, int kind);
|
2018-12-06 06:59:37 -08:00
|
|
|
int GetKindParamValue(
|
|
|
|
TypeCategory, const std::optional<parser::KindSelector> &);
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
|
2018-11-16 16:40:00 -08:00
|
|
|
class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
|
|
|
using DeclTypeSpecVisitor::Post;
|
|
|
|
using DeclTypeSpecVisitor::Pre;
|
2018-04-04 09:03:51 -07:00
|
|
|
using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
|
|
|
|
|
|
|
|
void Post(const parser::ParameterStmt &);
|
|
|
|
bool Pre(const parser::ImplicitStmt &);
|
|
|
|
bool Pre(const parser::LetterSpec &);
|
|
|
|
bool Pre(const parser::ImplicitSpec &);
|
|
|
|
void Post(const parser::ImplicitSpec &);
|
2018-04-11 13:11:42 -07:00
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
ImplicitRules &implicitRules() { return *implicitRules_; }
|
|
|
|
const ImplicitRules &implicitRules() const { return *implicitRules_; }
|
2018-04-11 13:11:42 -07:00
|
|
|
bool isImplicitNoneType() const {
|
|
|
|
return implicitRules().isImplicitNoneType();
|
|
|
|
}
|
2018-04-23 12:33:10 -07:00
|
|
|
bool isImplicitNoneExternal() const {
|
|
|
|
return implicitRules().isImplicitNoneExternal();
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
protected:
|
2018-04-17 15:04:08 -07:00
|
|
|
void PushScope();
|
2018-04-06 10:46:30 -07:00
|
|
|
void PopScope();
|
2018-10-26 11:57:08 -07:00
|
|
|
void ClearScopes() { implicitRules_.reset(); }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// implicit rules in effect for current scope
|
2018-10-22 07:37:38 -07:00
|
|
|
std::unique_ptr<ImplicitRules> implicitRules_;
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *prevImplicit_{nullptr};
|
|
|
|
const SourceName *prevImplicitNone_{nullptr};
|
|
|
|
const SourceName *prevImplicitNoneType_{nullptr};
|
|
|
|
const SourceName *prevParameterStmt_{nullptr};
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
bool HandleImplicitNone(const std::list<ImplicitNoneNameSpec> &nameSpecs);
|
|
|
|
};
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
// Track array specifications. They can occur in AttrSpec, EntityDecl,
|
|
|
|
// ObjectDecl, DimensionStmt, CommonBlockObject, or BasedPointerStmt.
|
|
|
|
// 1. INTEGER, DIMENSION(10) :: x
|
|
|
|
// 2. INTEGER :: x(10)
|
|
|
|
// 3. ALLOCATABLE :: x(:)
|
|
|
|
// 4. DIMENSION :: x(10)
|
|
|
|
// 5. TODO: COMMON x(10)
|
|
|
|
// 6. TODO: BasedPointerStmt
|
2018-11-16 16:40:00 -08:00
|
|
|
class ArraySpecVisitor : public virtual BaseVisitor {
|
2018-04-12 12:59:42 -07:00
|
|
|
public:
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::ArraySpec &);
|
2018-05-17 13:06:38 -07:00
|
|
|
void Post(const parser::AttrSpec &) { PostAttrSpec(); }
|
|
|
|
void Post(const parser::ComponentAttrSpec &) { PostAttrSpec(); }
|
2018-12-06 06:59:37 -08:00
|
|
|
void Post(const parser::DeferredShapeSpecList &);
|
|
|
|
void Post(const parser::AssumedShapeSpec &);
|
|
|
|
void Post(const parser::ExplicitShapeSpec &);
|
|
|
|
void Post(const parser::AssumedImpliedSpec &);
|
|
|
|
void Post(const parser::AssumedRankSpec &);
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
protected:
|
|
|
|
const ArraySpec &arraySpec();
|
|
|
|
void BeginArraySpec();
|
|
|
|
void EndArraySpec();
|
|
|
|
void ClearArraySpec() { arraySpec_.clear(); }
|
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
private:
|
|
|
|
// arraySpec_ is populated by any ArraySpec
|
|
|
|
ArraySpec arraySpec_;
|
2018-05-17 13:06:38 -07:00
|
|
|
// When an ArraySpec is under an AttrSpec or ComponentAttrSpec, it is moved
|
|
|
|
// into attrArraySpec_
|
2018-04-12 12:59:42 -07:00
|
|
|
ArraySpec attrArraySpec_;
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
void PostAttrSpec();
|
2018-05-14 14:26:39 -07:00
|
|
|
Bound GetBound(const parser::SpecificationExpr &);
|
2018-04-12 12:59:42 -07:00
|
|
|
};
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Manage a stack of Scopes
|
2018-09-14 15:04:50 -07:00
|
|
|
class ScopeHandler : public ImplicitRulesVisitor {
|
2018-04-04 09:03:51 -07:00
|
|
|
public:
|
2018-08-22 16:56:57 -07:00
|
|
|
Scope &currScope() { return *currScope_; }
|
2018-08-27 11:48:49 -07:00
|
|
|
// The enclosing scope, skipping blocks and derived types.
|
|
|
|
Scope &InclusiveScope();
|
2018-09-14 15:04:50 -07:00
|
|
|
// The global scope, containing program units.
|
|
|
|
Scope &GlobalScope();
|
2018-06-22 08:21:19 -07:00
|
|
|
|
|
|
|
// Create a new scope and push it on the scope stack.
|
2018-08-22 16:56:57 -07:00
|
|
|
void PushScope(Scope::Kind kind, Symbol *symbol);
|
2018-08-02 16:21:27 -07:00
|
|
|
void PushScope(Scope &scope);
|
2018-05-14 14:26:39 -07:00
|
|
|
void PopScope();
|
2018-10-23 17:05:12 -07:00
|
|
|
void ClearScopes() {
|
2018-10-23 17:13:59 -07:00
|
|
|
PopScope(); // trigger ConvertToObjectEntity calls
|
2018-10-23 17:05:12 -07:00
|
|
|
currScope_ = &context().globalScope();
|
|
|
|
ImplicitRulesVisitor::ClearScopes();
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-11-28 15:55:55 -08:00
|
|
|
template<typename T> bool Pre(const parser::Statement<T> &x) {
|
2018-11-16 16:40:00 -08:00
|
|
|
messageHandler().set_currStmtSource(&x.source);
|
2018-11-28 15:55:55 -08:00
|
|
|
currScope_->AddSourceRange(x.source);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T> void Post(const parser::Statement<T> &) {
|
2018-11-16 16:40:00 -08:00
|
|
|
messageHandler().set_currStmtSource(nullptr);
|
2018-11-28 15:55:55 -08:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
// Special messages: already declared; about a type; two names & locations
|
|
|
|
void SayAlreadyDeclared(const parser::Name &, const Symbol &);
|
|
|
|
void SayDerivedType(const SourceName &, MessageFixedText &&, const Scope &);
|
|
|
|
void Say2(const parser::Name &, MessageFixedText &&, const Symbol &,
|
|
|
|
MessageFixedText &&);
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Search for symbol by name in current and containing scopes
|
|
|
|
Symbol *FindSymbol(const parser::Name &);
|
|
|
|
Symbol *FindSymbol(const Scope &, const parser::Name &);
|
|
|
|
// Search for name only in scope, not in enclosing scopes.
|
|
|
|
Symbol *FindInScope(const Scope &, const parser::Name &);
|
|
|
|
Symbol *FindInScope(const Scope &, const SourceName &);
|
|
|
|
void EraseSymbol(const parser::Name &);
|
|
|
|
// Record that name resolved to symbol
|
|
|
|
Symbol *Resolve(const parser::Name &, Symbol *);
|
|
|
|
Symbol &Resolve(const parser::Name &, Symbol &);
|
|
|
|
// Make a new symbol with the name and attrs of an existing one
|
|
|
|
Symbol &CopySymbol(const Symbol &);
|
|
|
|
|
|
|
|
// Make symbols in the current or named scope
|
|
|
|
Symbol &MakeSymbol(Scope &, const SourceName &, Attrs);
|
|
|
|
Symbol &MakeSymbol(const parser::Name &, Attrs = Attrs{});
|
|
|
|
|
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(const parser::Name &name, D &&details) {
|
|
|
|
return MakeSymbol(name, Attrs{}, std::move(details));
|
|
|
|
}
|
2018-05-22 16:12:56 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
template<typename D>
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &MakeSymbol(
|
|
|
|
const parser::Name &name, const Attrs &attrs, D &&details) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// Note: don't use FindSymbol here. If this is a derived type scope,
|
|
|
|
// we want to detect if the name is already declared as a component.
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindInScope(currScope(), name)};
|
|
|
|
if (!symbol) {
|
|
|
|
symbol = &MakeSymbol(name, attrs);
|
|
|
|
symbol->set_details(std::move(details));
|
|
|
|
return *symbol;
|
|
|
|
}
|
2018-08-28 14:02:53 -07:00
|
|
|
if constexpr (std::is_same_v<DerivedTypeDetails, D>) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *d{symbol->detailsIf<GenericDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// derived type with same name as a generic
|
2018-07-17 07:02:30 -07:00
|
|
|
auto *derivedType{d->derivedType()};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (!derivedType) {
|
2018-11-05 14:36:11 -08:00
|
|
|
derivedType =
|
2018-11-16 12:43:08 -08:00
|
|
|
&currScope().MakeSymbol(name.source, attrs, std::move(details));
|
2018-06-22 08:21:19 -07:00
|
|
|
d->set_derivedType(*derivedType);
|
|
|
|
} else {
|
2018-10-10 16:20:46 -07:00
|
|
|
SayAlreadyDeclared(name, *derivedType);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
|
|
|
return *derivedType;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (symbol->CanReplaceDetails(details)) {
|
2018-04-25 19:58:42 -07:00
|
|
|
// update the existing symbol
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
symbol->set_details(std::move(details));
|
|
|
|
return *symbol;
|
2018-08-28 14:02:53 -07:00
|
|
|
} else if constexpr (std::is_same_v<UnknownDetails, D>) {
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
return *symbol;
|
2018-04-25 19:58:42 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayAlreadyDeclared(name, *symbol);
|
2018-04-25 19:58:42 -07:00
|
|
|
// replace the old symbols with a new one with correct details
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
2018-04-25 19:58:42 -07:00
|
|
|
return MakeSymbol(name, attrs, details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
protected:
|
|
|
|
// When subpNamesOnly_ is set we are only collecting procedure names.
|
|
|
|
// Create symbols with SubprogramNameDetails of the given kind.
|
|
|
|
std::optional<SubprogramKind> subpNamesOnly_;
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// Apply the implicit type rules to this symbol.
|
2018-09-22 08:05:46 -07:00
|
|
|
void ApplyImplicitRules(Symbol &);
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *GetImplicitType(Symbol &);
|
2018-09-22 08:05:46 -07:00
|
|
|
bool ConvertToObjectEntity(Symbol &);
|
|
|
|
bool ConvertToProcEntity(Symbol &);
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
// Walk the ModuleSubprogramPart or InternalSubprogramPart collecting names.
|
|
|
|
template<typename T>
|
|
|
|
void WalkSubprogramPart(const std::optional<T> &subpPart) {
|
|
|
|
if (subpPart) {
|
|
|
|
if (std::is_same_v<T, parser::ModuleSubprogramPart>) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Module;
|
|
|
|
} else if (std::is_same_v<T, parser::InternalSubprogramPart>) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Internal;
|
|
|
|
} else {
|
|
|
|
static_assert("unexpected type");
|
|
|
|
}
|
|
|
|
Walk(*subpPart);
|
|
|
|
subpNamesOnly_ = std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
private:
|
2018-08-09 17:12:31 -07:00
|
|
|
Scope *currScope_{nullptr};
|
2018-04-25 19:58:42 -07:00
|
|
|
};
|
|
|
|
|
2018-05-04 15:37:56 -07:00
|
|
|
class ModuleVisitor : public virtual ScopeHandler {
|
2018-04-25 19:58:42 -07:00
|
|
|
public:
|
2018-05-14 13:51:13 -07:00
|
|
|
bool Pre(const parser::Module &);
|
|
|
|
void Post(const parser::Module &);
|
2018-08-02 16:21:27 -07:00
|
|
|
bool Pre(const parser::Submodule &);
|
|
|
|
void Post(const parser::Submodule &);
|
2018-04-25 19:58:42 -07:00
|
|
|
bool Pre(const parser::AccessStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::Only &);
|
|
|
|
bool Pre(const parser::Rename::Names &);
|
|
|
|
bool Pre(const parser::UseStmt &);
|
|
|
|
void Post(const parser::UseStmt &);
|
2018-05-03 15:57:56 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
private:
|
|
|
|
// The default access spec for this module.
|
|
|
|
Attr defaultAccess_{Attr::PUBLIC};
|
|
|
|
// The location of the last AccessStmt without access-ids, if any.
|
|
|
|
const SourceName *prevAccessStmt_{nullptr};
|
2018-05-03 15:57:56 -07:00
|
|
|
// The scope of the module during a UseStmt
|
|
|
|
const Scope *useModuleScope_{nullptr};
|
2018-07-25 06:55:11 -07:00
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
void SetAccess(const parser::Name &, Attr);
|
|
|
|
void ApplyDefaultAccess();
|
2018-05-14 14:26:39 -07:00
|
|
|
void AddUse(const parser::Rename::Names &);
|
|
|
|
void AddUse(const parser::Name &);
|
2018-05-03 15:57:56 -07:00
|
|
|
// Record a use from useModuleScope_ of useName as localName. location is
|
|
|
|
// where it occurred (either the module or the rename) for error reporting.
|
2018-11-16 12:43:08 -08:00
|
|
|
void AddUse(const SourceName &, const parser::Name &, const parser::Name &);
|
|
|
|
void AddUse(const SourceName &, const Symbol &, Symbol &);
|
|
|
|
Symbol &BeginModule(const parser::Name &, bool isSubmodule,
|
2018-08-02 16:21:27 -07:00
|
|
|
const std::optional<parser::ModuleSubprogramPart> &);
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *FindModule(const parser::Name &, Scope *ancestor = nullptr);
|
2018-04-25 19:58:42 -07:00
|
|
|
};
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
class InterfaceVisitor : public virtual ScopeHandler {
|
|
|
|
public:
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::InterfaceStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
void Post(const parser::InterfaceStmt &);
|
|
|
|
void Post(const parser::EndInterfaceStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::GenericSpec &);
|
|
|
|
bool Pre(const parser::ProcedureStmt &);
|
|
|
|
void Post(const parser::GenericStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
bool inInterfaceBlock() const { return inInterfaceBlock_; }
|
2018-11-16 12:43:08 -08:00
|
|
|
bool isGeneric() const { return genericName_ != nullptr; }
|
2018-05-14 13:51:13 -07:00
|
|
|
bool isAbstract() const { return isAbstract_; }
|
|
|
|
|
|
|
|
protected:
|
2018-11-16 12:43:08 -08:00
|
|
|
GenericDetails &GetGenericDetails();
|
2018-05-22 16:12:56 -07:00
|
|
|
// Add to generic the symbol for the subprogram with the same name
|
2018-07-05 10:28:34 -07:00
|
|
|
void CheckGenericProcedures(Symbol &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool inInterfaceBlock_{false}; // set when in interface block
|
|
|
|
bool isAbstract_{false}; // set when in abstract interface block
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *genericName_{nullptr}; // set in generic interface block
|
2018-12-18 07:59:40 -08:00
|
|
|
using ProcedureKind = parser::ProcedureStmt::Kind;
|
|
|
|
// mapping of generic to its specific proc names and kinds
|
|
|
|
std::multimap<Symbol *, std::pair<const parser::Name *, ProcedureKind>>
|
|
|
|
specificProcs_;
|
2018-07-20 10:46:11 -07:00
|
|
|
|
2018-12-18 07:59:40 -08:00
|
|
|
void AddSpecificProcs(const std::list<parser::Name> &, ProcedureKind);
|
2018-07-20 10:46:11 -07:00
|
|
|
void ResolveSpecificsInGeneric(Symbol &generic);
|
2018-05-14 13:51:13 -07:00
|
|
|
};
|
|
|
|
|
2018-10-15 17:11:24 -07:00
|
|
|
class SubprogramVisitor : public virtual ScopeHandler, public InterfaceVisitor {
|
2018-05-04 15:37:56 -07:00
|
|
|
public:
|
2018-10-10 16:20:46 -07:00
|
|
|
bool HandleStmtFunction(const parser::StmtFunctionStmt &);
|
2018-05-04 15:37:56 -07:00
|
|
|
void Post(const parser::StmtFunctionStmt &);
|
2018-07-11 17:45:13 -07:00
|
|
|
bool Pre(const parser::SubroutineStmt &);
|
2018-05-04 15:37:56 -07:00
|
|
|
void Post(const parser::SubroutineStmt &);
|
|
|
|
bool Pre(const parser::FunctionStmt &);
|
|
|
|
void Post(const parser::FunctionStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
bool Pre(const parser::SubroutineSubprogram &);
|
|
|
|
void Post(const parser::SubroutineSubprogram &);
|
|
|
|
bool Pre(const parser::FunctionSubprogram &);
|
|
|
|
void Post(const parser::FunctionSubprogram &);
|
|
|
|
bool Pre(const parser::InterfaceBody::Subroutine &);
|
|
|
|
void Post(const parser::InterfaceBody::Subroutine &);
|
|
|
|
bool Pre(const parser::InterfaceBody::Function &);
|
|
|
|
void Post(const parser::InterfaceBody::Function &);
|
2018-10-26 07:34:50 -07:00
|
|
|
bool Pre(const parser::SeparateModuleSubprogram &);
|
|
|
|
void Post(const parser::SeparateModuleSubprogram &);
|
2018-05-04 15:37:56 -07:00
|
|
|
bool Pre(const parser::Suffix &);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Set when we see a stmt function that is really an array element assignment
|
|
|
|
bool badStmtFuncFound_{false};
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Function result name from parser::Suffix, if any.
|
|
|
|
const parser::Name *funcResultName_{nullptr};
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool BeginSubprogram(const parser::Name &, Symbol::Flag, bool hasModulePrefix,
|
2018-05-14 13:51:13 -07:00
|
|
|
const std::optional<parser::InternalSubprogramPart> &);
|
|
|
|
void EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
// Create a subprogram symbol in the current scope and push a new scope.
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &PushSubprogramScope(const parser::Name &, Symbol::Flag);
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *GetSpecificFromGeneric(const parser::Name &);
|
2018-10-26 07:34:50 -07:00
|
|
|
SubprogramDetails &PostSubprogramStmt(const parser::Name &);
|
2018-05-04 15:37:56 -07:00
|
|
|
};
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
class DeclarationVisitor : public ArraySpecVisitor,
|
|
|
|
public virtual ScopeHandler {
|
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
|
|
|
|
|
|
|
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 &);
|
2018-12-06 06:59:37 -08:00
|
|
|
void Post(const parser::NamedConstantDef &);
|
2018-05-30 14:11:45 -07:00
|
|
|
bool Pre(const parser::AsynchronousStmt &);
|
|
|
|
bool Pre(const parser::ContiguousStmt &);
|
|
|
|
bool Pre(const parser::ExternalStmt &);
|
2018-07-11 17:45:13 -07:00
|
|
|
bool Pre(const parser::IntentStmt &);
|
2018-05-30 14:11:45 -07:00
|
|
|
bool Pre(const parser::IntrinsicStmt &);
|
|
|
|
bool Pre(const parser::OptionalStmt &);
|
|
|
|
bool Pre(const parser::ProtectedStmt &);
|
|
|
|
bool Pre(const parser::ValueStmt &);
|
|
|
|
bool Pre(const parser::VolatileStmt &);
|
|
|
|
bool Pre(const parser::AllocatableStmt &) {
|
|
|
|
objectDeclAttr_ = Attr::ALLOCATABLE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Post(const parser::AllocatableStmt &) { objectDeclAttr_ = std::nullopt; }
|
|
|
|
bool Pre(const parser::TargetStmt &x) {
|
|
|
|
objectDeclAttr_ = Attr::TARGET;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Post(const parser::TargetStmt &) { objectDeclAttr_ = std::nullopt; }
|
|
|
|
void Post(const parser::DimensionStmt::Declaration &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::TypeDeclarationStmt &) { return BeginDecl(); }
|
|
|
|
void Post(const parser::TypeDeclarationStmt &) { EndDecl(); }
|
2018-12-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 &);
|
2018-08-31 16:20:00 -07:00
|
|
|
void Post(const parser::DeclarationTypeSpec::Class &);
|
2018-12-11 14:51:08 -08:00
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Record &);
|
2018-06-22 08:21:19 -07:00
|
|
|
bool Pre(const parser::DerivedTypeSpec &);
|
2018-06-05 12:18:35 -07:00
|
|
|
void Post(const parser::DerivedTypeDef &x);
|
|
|
|
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(); }
|
|
|
|
void Post(const parser::ComponentDecl &x);
|
|
|
|
bool Pre(const parser::ProcedureDeclarationStmt &);
|
|
|
|
void Post(const parser::ProcedureDeclarationStmt &);
|
|
|
|
bool Pre(const parser::ProcComponentDefStmt &);
|
|
|
|
void Post(const parser::ProcComponentDefStmt &);
|
|
|
|
void Post(const parser::ProcInterface &x);
|
|
|
|
void Post(const parser::ProcDecl &x);
|
2018-09-06 08:01:49 -07:00
|
|
|
bool Pre(const parser::TypeBoundProcedurePart &);
|
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 &);
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool BeginDecl();
|
|
|
|
void EndDecl();
|
2018-10-18 07:55:48 -07:00
|
|
|
// Declare a construct or statement entity. If there isn't a type specified
|
|
|
|
// it comes from the entity in the containing scope, or implicit rules.
|
|
|
|
// Return pointer to the new symbol, or nullptr on error.
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *DeclareConstructEntity(const parser::Name &);
|
|
|
|
bool CheckUseError(const parser::Name &);
|
2018-12-26 13:31:13 -08:00
|
|
|
void CheckAccessibility(const parser::Name &, bool, const Symbol &);
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// The attribute corresponding to the statement containing an ObjectDecl
|
|
|
|
std::optional<Attr> objectDeclAttr_;
|
2018-12-14 14:04:15 -08:00
|
|
|
// Info about current character type while walking DeclTypeSpec
|
|
|
|
struct {
|
|
|
|
std::optional<ParamValue> length;
|
|
|
|
int kind{0};
|
|
|
|
} charInfo_;
|
2018-09-06 08:01:49 -07:00
|
|
|
// Info about current derived type while walking DerivedTypeStmt
|
|
|
|
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
|
|
|
|
} derivedTypeInfo_;
|
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 &DeclareObjectEntity(const parser::Name &, Attrs);
|
|
|
|
Symbol &DeclareProcEntity(const parser::Name &, Attrs, const ProcInterface &);
|
|
|
|
void SetType(const parser::Name &, const DeclTypeSpec &);
|
2018-12-11 14:51:08 -08:00
|
|
|
const Symbol *ResolveDerivedType(const parser::Name * = nullptr);
|
2018-08-31 16:20:00 -07:00
|
|
|
bool CanBeTypeBoundProc(const Symbol &);
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *FindExplicitInterface(const parser::Name &);
|
2018-12-26 13:31:13 -08:00
|
|
|
const Symbol *FindTypeSymbol(const parser::Name &);
|
|
|
|
Symbol *MakeTypeSymbol(const parser::Name &, Details &&);
|
2019-01-07 15:05:53 -08:00
|
|
|
bool OkToAddComponent(const parser::Name &, const Symbol * = nullptr);
|
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>()}) {
|
|
|
|
symbol.set_details(T{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");
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-20 14:08:59 -07:00
|
|
|
SayAlreadyDeclared(name, symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
};
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
// Resolve construct entities and statement entities.
|
2018-09-25 08:53:53 -07:00
|
|
|
// Check that construct names don't conflict with other names.
|
2018-10-18 07:55:48 -07:00
|
|
|
class ConstructVisitor : public DeclarationVisitor {
|
2018-09-25 08:53:53 -07:00
|
|
|
public:
|
2018-10-18 07:55:48 -07:00
|
|
|
bool Pre(const parser::ConcurrentHeader &);
|
|
|
|
void Post(const parser::ConcurrentHeader &);
|
|
|
|
bool Pre(const parser::LocalitySpec::Local &);
|
|
|
|
bool Pre(const parser::LocalitySpec::LocalInit &);
|
|
|
|
bool Pre(const parser::LocalitySpec::Shared &);
|
|
|
|
bool Pre(const parser::DataImpliedDo &);
|
|
|
|
bool Pre(const parser::DataStmt &);
|
|
|
|
void Post(const parser::DataStmt &);
|
|
|
|
bool Pre(const parser::DoConstruct &);
|
|
|
|
void Post(const parser::DoConstruct &);
|
|
|
|
void Post(const parser::ConcurrentControl &);
|
|
|
|
bool Pre(const parser::ForallConstruct &);
|
|
|
|
void Post(const parser::ForallConstruct &);
|
|
|
|
bool Pre(const parser::ForallStmt &);
|
|
|
|
void Post(const parser::ForallStmt &);
|
|
|
|
bool Pre(const parser::BlockStmt &);
|
|
|
|
bool Pre(const parser::EndBlockStmt &);
|
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::AssociateStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::ChangeTeamStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::CriticalStmt &x) { return CheckDef(x.t); }
|
2018-10-24 14:54:53 -07:00
|
|
|
bool Pre(const parser::LabelDoStmt &x) {
|
|
|
|
CHECK(false);
|
|
|
|
return false;
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
// 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::EndAssociateStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndChangeTeamStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::EndCriticalStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndDoStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::ElseIfStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::ElseStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::EndIfStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::CaseStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::EndSelectStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::SelectRankCaseStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::TypeGuardStmt &x) { CheckRef(x.t); }
|
|
|
|
void Post(const parser::CycleStmt &x) { CheckRef(x.v); }
|
|
|
|
void Post(const parser::ExitStmt &x) { CheckRef(x.v); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
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> &);
|
2018-10-18 07:55:48 -07:00
|
|
|
void CheckIntegerType(const Symbol &);
|
2018-09-25 08:53:53 -07:00
|
|
|
};
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Walk the parse tree and resolve names to symbols.
|
2018-10-15 17:11:24 -07:00
|
|
|
class ResolveNamesVisitor : public virtual ScopeHandler,
|
|
|
|
public ModuleVisitor,
|
2018-05-30 14:11:45 -07:00
|
|
|
public SubprogramVisitor,
|
2018-10-18 07:55:48 -07:00
|
|
|
public ConstructVisitor {
|
2018-04-25 19:58:42 -07:00
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
2018-10-18 07:55:48 -07:00
|
|
|
using ConstructVisitor::Post;
|
|
|
|
using ConstructVisitor::Pre;
|
2018-06-14 13:43:02 -07:00
|
|
|
using DeclarationVisitor::Post;
|
|
|
|
using DeclarationVisitor::Pre;
|
2018-05-04 15:37:56 -07:00
|
|
|
using ImplicitRulesVisitor::Post;
|
|
|
|
using ImplicitRulesVisitor::Pre;
|
2018-05-14 13:51:13 -07:00
|
|
|
using InterfaceVisitor::Post;
|
|
|
|
using InterfaceVisitor::Pre;
|
2018-04-25 19:58:42 -07:00
|
|
|
using ModuleVisitor::Post;
|
|
|
|
using ModuleVisitor::Pre;
|
2018-11-28 15:55:55 -08:00
|
|
|
using ScopeHandler::Post;
|
|
|
|
using ScopeHandler::Pre;
|
2018-05-04 15:37:56 -07:00
|
|
|
using SubprogramVisitor::Post;
|
|
|
|
using SubprogramVisitor::Pre;
|
2018-04-25 19:58:42 -07:00
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
ResolveNamesVisitor(SemanticsContext &context) {
|
|
|
|
set_context(context);
|
2018-10-26 07:34:50 -07:00
|
|
|
set_this(this);
|
2018-10-22 07:37:38 -07:00
|
|
|
PushScope(context.globalScope());
|
2018-10-15 17:11:24 -07:00
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Default action for a parse tree node is to visit children.
|
|
|
|
template<typename T> bool Pre(const T &) { return true; }
|
|
|
|
template<typename T> void Post(const T &) {}
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
bool Pre(const parser::CommonBlockObject &);
|
|
|
|
void Post(const parser::CommonBlockObject &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::PrefixSpec &);
|
2018-04-11 13:11:42 -07:00
|
|
|
void Post(const parser::SpecificationPart &);
|
|
|
|
bool Pre(const parser::MainProgram &);
|
|
|
|
void Post(const parser::EndProgramStmt &);
|
2018-03-30 13:57:23 -07:00
|
|
|
void Post(const parser::Program &);
|
2018-08-27 11:48:49 -07:00
|
|
|
bool Pre(const parser::ImplicitStmt &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::PointerObject &);
|
|
|
|
void Post(const parser::AllocateObject &);
|
|
|
|
void Post(const parser::PointerAssignmentStmt &);
|
|
|
|
void Post(const parser::Designator &);
|
|
|
|
template<typename T> void Post(const parser::LoopBounds<T> &);
|
|
|
|
void Post(const parser::ProcComponentRef &);
|
2018-05-14 14:26:39 -07:00
|
|
|
void Post(const parser::ProcedureDesignator &);
|
2018-06-05 12:18:35 -07:00
|
|
|
bool Pre(const parser::FunctionReference &);
|
|
|
|
void Post(const parser::FunctionReference &);
|
|
|
|
bool Pre(const parser::CallStmt &);
|
|
|
|
void Post(const parser::CallStmt &);
|
2018-08-22 16:05:06 -07:00
|
|
|
bool Pre(const parser::ImportStmt &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void Post(const parser::TypeGuardStmt &);
|
|
|
|
bool Pre(const parser::StmtFunctionStmt &);
|
2018-04-23 12:33:10 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
private:
|
2018-06-05 12:18:35 -07:00
|
|
|
// Kind of procedure we are expecting to see in a ProcedureDesignator
|
|
|
|
std::optional<Symbol::Flag> expectedProcFlag_;
|
2018-08-22 16:05:06 -07:00
|
|
|
const SourceName *prevImportStmt_{nullptr};
|
2018-06-05 12:18:35 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Each of these returns a pointer to a resolved Name (i.e. with symbol)
|
|
|
|
// or nullptr in case of error.
|
|
|
|
const parser::Name *ResolveStructureComponent(
|
|
|
|
const parser::StructureComponent &);
|
|
|
|
const parser::Name *ResolveArrayElement(const parser::ArrayElement &);
|
|
|
|
const parser::Name *ResolveCoindexedNamedObject(
|
|
|
|
const parser::CoindexedNamedObject &);
|
|
|
|
const parser::Name *ResolveDataRef(const parser::DataRef &);
|
|
|
|
const parser::Name *ResolveName(const parser::Name &);
|
|
|
|
const parser::Name *FindComponent(const parser::Name *, const parser::Name &);
|
|
|
|
|
|
|
|
Symbol *FindComponent(const Scope &, const parser::Name &);
|
|
|
|
bool CheckAccessibleComponent(const parser::Name &);
|
2018-08-22 16:05:06 -07:00
|
|
|
void CheckImports();
|
|
|
|
void CheckImport(const SourceName &, const SourceName &);
|
2018-12-18 16:35:31 -08:00
|
|
|
bool SetProcFlag(const parser::Name &, Symbol &);
|
2018-03-30 13:57:23 -07:00
|
|
|
};
|
2018-03-23 12:24:29 -07:00
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// ImplicitRules implementation
|
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
bool ImplicitRules::isImplicitNoneType() const {
|
|
|
|
if (isImplicitNoneType_.has_value()) {
|
|
|
|
return isImplicitNoneType_.value();
|
|
|
|
} else if (inheritFromParent_) {
|
|
|
|
return parent_->isImplicitNoneType();
|
|
|
|
} else {
|
|
|
|
return false; // default if not specified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRules::isImplicitNoneExternal() const {
|
|
|
|
if (isImplicitNoneExternal_.has_value()) {
|
|
|
|
return isImplicitNoneExternal_.value();
|
|
|
|
} else if (inheritFromParent_) {
|
|
|
|
return parent_->isImplicitNoneExternal();
|
|
|
|
} else {
|
|
|
|
return false; // default if not specified
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *ImplicitRules::GetType(char ch) const {
|
2018-08-28 14:02:53 -07:00
|
|
|
if (auto it{map_.find(ch)}; it != map_.end()) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return it->second;
|
2018-10-22 07:37:38 -07:00
|
|
|
} else if (inheritFromParent_ && parent_->context_) {
|
2018-08-27 11:48:49 -07:00
|
|
|
return parent_->GetType(ch);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'i' && ch <= 'n') {
|
2018-12-17 12:41:43 -08:00
|
|
|
return &context_->MakeNumericType(TypeCategory::Integer);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'a' && ch <= 'z') {
|
2018-12-17 12:41:43 -08:00
|
|
|
return &context_->MakeNumericType(TypeCategory::Real);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else {
|
2018-12-11 14:51:08 -08:00
|
|
|
return nullptr;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// isDefault is set when we are applying the default rules, so it is not
|
|
|
|
// an error if the type is already set.
|
|
|
|
void ImplicitRules::SetType(const DeclTypeSpec &type, parser::Location lo,
|
|
|
|
parser::Location hi, bool isDefault) {
|
2018-04-05 17:02:31 -07:00
|
|
|
for (char ch = *lo; ch; ch = ImplicitRules::Incr(ch)) {
|
2018-12-11 14:51:08 -08:00
|
|
|
auto res{map_.emplace(ch, &type)};
|
2018-04-05 17:02:31 -07:00
|
|
|
if (!res.second && !isDefault) {
|
2018-10-22 07:37:38 -07:00
|
|
|
context_->Say(lo,
|
2018-05-03 15:57:56 -07:00
|
|
|
"More than one implicit type specified for '%s'"_err_en_US,
|
2018-10-22 07:37:38 -07:00
|
|
|
std::string(1, ch).c_str());
|
2018-04-05 17:02:31 -07:00
|
|
|
}
|
|
|
|
if (ch == *hi) {
|
|
|
|
break;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Return the next char after ch in a way that works for ASCII or EBCDIC.
|
|
|
|
// Return '\0' for the char after 'z'.
|
|
|
|
char ImplicitRules::Incr(char ch) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'i': return 'j';
|
|
|
|
case 'r': return 's';
|
|
|
|
case 'z': return '\0';
|
|
|
|
default: return ch + 1;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &o, const ImplicitRules &implicitRules) {
|
|
|
|
o << "ImplicitRules:\n";
|
2018-04-05 17:02:31 -07:00
|
|
|
for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, '_');
|
|
|
|
ShowImplicitRule(o, implicitRules, '$');
|
|
|
|
ShowImplicitRule(o, implicitRules, '@');
|
2018-04-04 09:03:51 -07:00
|
|
|
return o;
|
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
void ShowImplicitRule(
|
|
|
|
std::ostream &o, const ImplicitRules &implicitRules, char ch) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto it{implicitRules.map_.find(ch)};
|
2018-04-06 10:46:30 -07:00
|
|
|
if (it != implicitRules.map_.end()) {
|
2018-12-11 14:51:08 -08:00
|
|
|
o << " " << ch << ": " << *it->second << '\n';
|
2018-04-06 10:46:30 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
template<typename T> void BaseVisitor::Walk(const T &x) {
|
|
|
|
parser::Walk(x, *this_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SourceName *BaseVisitor::currStmtSource() {
|
|
|
|
return messageHandler_.currStmtSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseVisitor::set_context(SemanticsContext &context) {
|
|
|
|
context_ = &context;
|
|
|
|
messageHandler_.set_messages(context.messages());
|
|
|
|
}
|
|
|
|
|
2019-01-04 17:10:31 -08:00
|
|
|
void BaseVisitor::MakePlaceholder(
|
|
|
|
const parser::Name &name, MiscDetails::Kind kind) {
|
|
|
|
if (!name.symbol) {
|
|
|
|
name.symbol = &context_->globalScope().MakeSymbol(
|
2019-01-07 10:10:15 -08:00
|
|
|
name.source, Attrs{}, MiscDetails{kind});
|
2019-01-04 17:10:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// AttrsVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-08-31 15:15:28 -07:00
|
|
|
bool AttrsVisitor::BeginAttrs() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(!attrs_);
|
2018-03-30 19:49:00 -07:00
|
|
|
attrs_ = std::make_optional<Attrs>();
|
2018-08-31 15:15:28 -07:00
|
|
|
return true;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
Attrs AttrsVisitor::GetAttrs() {
|
|
|
|
CHECK(attrs_);
|
|
|
|
return *attrs_;
|
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
Attrs AttrsVisitor::EndAttrs() {
|
2018-03-30 19:49:00 -07:00
|
|
|
CHECK(attrs_);
|
|
|
|
Attrs result{*attrs_};
|
2018-03-30 13:57:23 -07:00
|
|
|
attrs_.reset();
|
2019-01-04 17:10:31 -08:00
|
|
|
passName_.reset();
|
|
|
|
bindName_.reset();
|
2018-03-30 13:57:23 -07:00
|
|
|
return result;
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
|
|
|
|
bool AttrsVisitor::SetPassNameOn(Symbol &symbol) {
|
|
|
|
if (!passName_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](ProcEntityDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[&](ProcBindingDetails &x) { x.set_passName(*passName_); },
|
|
|
|
[](auto &) { common::die("unexpected pass name"); },
|
|
|
|
},
|
|
|
|
symbol.details());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttrsVisitor::SetBindNameOn(Symbol &symbol) {
|
|
|
|
if (!bindName_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](EntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](ObjectEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](ProcEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[&](SubprogramDetails &x) { x.set_bindName(std::move(bindName_)); },
|
|
|
|
[](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_);
|
2018-03-30 13:57:23 -07:00
|
|
|
if (x.v) {
|
2019-01-04 17:10:31 -08:00
|
|
|
bindName_ = EvaluateExpr(*x.v);
|
|
|
|
} else {
|
|
|
|
attrs_->set(Attr::BIND_C);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::AccessSpec &x) {
|
2018-04-24 17:05:58 -07:00
|
|
|
attrs_->set(AccessSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::IntentSpec &x) {
|
2018-07-11 17:45:13 -07:00
|
|
|
CHECK(attrs_);
|
|
|
|
attrs_->set(IntentSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
bool AttrsVisitor::Pre(const parser::Pass &x) {
|
|
|
|
if (x.v) {
|
|
|
|
passName_ = x.v->source;
|
|
|
|
MakePlaceholder(*x.v, MiscDetails::Kind::PassName);
|
|
|
|
} else {
|
|
|
|
attrs_->set(Attr::PASS);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// DeclTypeSpecVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-12-18 14:35:58 -08:00
|
|
|
DeclTypeSpec *DeclTypeSpecVisitor::GetDeclTypeSpec() {
|
|
|
|
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
|
|
|
}
|
2018-12-18 17:19:41 -08:00
|
|
|
DeclTypeSpecVisitor::State DeclTypeSpecVisitor::SetDeclTypeSpecState(State x) {
|
|
|
|
auto result{state_};
|
|
|
|
state_ = x;
|
|
|
|
return result;
|
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeParamSpec &x) {
|
2018-12-18 14:35:58 -08:00
|
|
|
DerivedTypeSpec &derivedTypeSpec{state_.declTypeSpec->derivedTypeSpec()};
|
2018-11-06 17:18:06 -08:00
|
|
|
const auto &value{std::get<parser::TypeParamValue>(x.t)};
|
|
|
|
if (const auto &keyword{std::get<std::optional<parser::Keyword>>(x.t)}) {
|
2018-12-11 14:51:08 -08:00
|
|
|
derivedTypeSpec.AddParamValue(keyword->v.source, GetParamValue(value));
|
2018-11-06 17:18:06 -08:00
|
|
|
} else {
|
2018-12-11 14:51:08 -08:00
|
|
|
derivedTypeSpec.AddParamValue(GetParamValue(value));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-11-06 17:18:06 -08:00
|
|
|
|
|
|
|
ParamValue DeclTypeSpecVisitor::GetParamValue(const parser::TypeParamValue &x) {
|
|
|
|
return std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-12-06 06:59:37 -08:00
|
|
|
[=](const parser::ScalarIntExpr &x) {
|
2019-01-07 15:05:53 -08:00
|
|
|
return ParamValue{EvaluateIntExpr(x)};
|
2018-12-06 06:59:37 -08:00
|
|
|
},
|
2018-11-06 17:18:06 -08:00
|
|
|
[](const parser::Star &) { return ParamValue::Assumed(); },
|
|
|
|
[](const parser::TypeParamValue::Deferred &) {
|
|
|
|
return ParamValue::Deferred();
|
2018-04-04 09:03:51 -07:00
|
|
|
},
|
|
|
|
},
|
2018-11-06 17:18:06 -08:00
|
|
|
x.u);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::TypeGuardStmt &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeGuardStmt &) {
|
|
|
|
// TODO: TypeGuardStmt
|
|
|
|
EndDeclTypeSpec();
|
2018-12-18 14:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::AcSpec &x) {
|
|
|
|
// AcSpec can occur within a TypeDeclarationStmt: save and restore state
|
2018-12-18 17:19:41 -08:00
|
|
|
auto savedState{SetDeclTypeSpecState({})};
|
2018-12-18 14:35:58 -08:00
|
|
|
BeginDeclTypeSpec();
|
|
|
|
Walk(x.type);
|
|
|
|
Walk(x.values);
|
|
|
|
EndDeclTypeSpec();
|
2018-12-18 17:19:41 -08:00
|
|
|
SetDeclTypeSpecState(savedState);
|
2018-12-18 14:35:58 -08:00
|
|
|
return false;
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::IntrinsicTypeSpec::Logical &x) {
|
2018-12-17 12:41:43 -08:00
|
|
|
SetDeclTypeSpec(context().MakeLogicalType(
|
|
|
|
GetKindParamValue(TypeCategory::Logical, x.kind)));
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::IntegerTypeSpec &x) {
|
|
|
|
MakeNumericType(TypeCategory::Integer, x.v);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::IntrinsicTypeSpec::Real &x) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(TypeCategory::Real, x.kind);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::IntrinsicTypeSpec::Complex &x) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(TypeCategory::Complex, x.kind);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(
|
2018-05-17 13:06:38 -07:00
|
|
|
const parser::IntrinsicTypeSpec::DoublePrecision &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(
|
2018-10-22 07:37:38 -07:00
|
|
|
TypeCategory::Real, context().defaultKinds().doublePrecisionKind());
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclTypeSpecVisitor::Post(
|
2018-09-10 12:20:42 -07:00
|
|
|
const parser::IntrinsicTypeSpec::DoubleComplex &) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(
|
2018-10-22 07:37:38 -07:00
|
|
|
TypeCategory::Complex, context().defaultKinds().doublePrecisionKind());
|
2018-12-06 06:59:37 -08:00
|
|
|
}
|
2018-12-17 12:41:43 -08:00
|
|
|
void DeclTypeSpecVisitor::MakeNumericType(
|
2018-12-06 06:59:37 -08:00
|
|
|
TypeCategory category, const std::optional<parser::KindSelector> &kind) {
|
2018-12-17 12:41:43 -08:00
|
|
|
MakeNumericType(category, GetKindParamValue(category, kind));
|
2018-09-10 12:20:42 -07: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.
|
2018-12-11 14:51:08 -08:00
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpec(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
|
|
|
}
|
|
|
|
// Set both the derived type name and corresponding DeclTypeSpec.
|
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpec(
|
|
|
|
const parser::Name &name, DeclTypeSpec &declTypeSpec) {
|
2018-12-18 14:35:58 -08:00
|
|
|
state_.derivedTypeName = &name;
|
2018-12-11 14:51:08 -08:00
|
|
|
SetDeclTypeSpec(declTypeSpec);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-09-11 17:33:42 -07:00
|
|
|
int DeclTypeSpecVisitor::GetKindParamValue(
|
2018-12-06 06:59:37 -08:00
|
|
|
TypeCategory category, const std::optional<parser::KindSelector> &kind) {
|
|
|
|
if (!kind) {
|
|
|
|
return 0;
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
// TODO: check that we get a valid kind
|
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
|
|
|
[&](const parser::ScalarIntConstantExpr &x) -> int {
|
|
|
|
if (auto maybeExpr{EvaluateExpr(x)}) {
|
2018-12-28 15:58:17 -08:00
|
|
|
if (auto intConst{evaluate::ToInt64(*maybeExpr)}) {
|
|
|
|
return *intConst;
|
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
}
|
2018-12-28 15:58:17 -08:00
|
|
|
return 0;
|
2018-12-06 06:59:37 -08:00
|
|
|
},
|
|
|
|
[&](const parser::KindSelector::StarSize &x) -> int {
|
|
|
|
std::uint64_t size{x.v};
|
|
|
|
if (category == TypeCategory::Complex) {
|
|
|
|
size /= 2;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kind->u);
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// MessageHandler implementation
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
void MessageHandler::set_currStmtSource(const SourceName *source) {
|
|
|
|
currStmtSource_ = source;
|
|
|
|
}
|
|
|
|
Message &MessageHandler::Say(MessageFixedText &&msg) {
|
2018-04-04 09:03:51 -07:00
|
|
|
CHECK(currStmtSource_);
|
2018-10-22 07:37:38 -07:00
|
|
|
return messages_->Say(*currStmtSource_, std::move(msg));
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &name, MessageFixedText &&msg) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return Say(name, std::move(msg), name);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &location, MessageFixedText &&msg,
|
|
|
|
const SourceName &arg1) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return messages_->Say(location, std::move(msg), arg1.ToString().c_str());
|
2018-05-03 15:57:56 -07:00
|
|
|
}
|
2018-11-16 16:40:00 -08:00
|
|
|
Message &MessageHandler::Say(const SourceName &location, MessageFixedText &&msg,
|
|
|
|
const SourceName &arg1, const SourceName &arg2) {
|
2018-10-22 07:37:38 -07:00
|
|
|
return messages_->Say(location, std::move(msg), arg1.ToString().c_str(),
|
2018-08-27 11:48:49 -07:00
|
|
|
arg2.ToString().c_str());
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// ImplicitRulesVisitor implementation
|
|
|
|
|
|
|
|
void ImplicitRulesVisitor::Post(const parser::ParameterStmt &x) {
|
|
|
|
prevParameterStmt_ = currStmtSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::ImplicitStmt &x) {
|
|
|
|
bool res = std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-04 09:03:51 -07:00
|
|
|
[&](const std::list<ImplicitNoneNameSpec> &x) {
|
|
|
|
return HandleImplicitNone(x);
|
|
|
|
},
|
|
|
|
[&](const std::list<parser::ImplicitSpec> &x) {
|
|
|
|
if (prevImplicitNoneType_) {
|
|
|
|
Say("IMPLICIT statement after IMPLICIT NONE or "
|
|
|
|
"IMPLICIT NONE(TYPE) statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
prevImplicit_ = currStmtSource();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::LetterSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto loLoc{std::get<parser::Location>(x.t)};
|
|
|
|
auto hiLoc{loLoc};
|
|
|
|
if (auto hiLocOpt{std::get<std::optional<parser::Location>>(x.t)}) {
|
2018-04-04 09:03:51 -07:00
|
|
|
hiLoc = *hiLocOpt;
|
|
|
|
if (*hiLoc < *loLoc) {
|
2018-05-03 15:57:56 -07:00
|
|
|
Say(hiLoc, "'%s' does not follow '%s' alphabetically"_err_en_US,
|
|
|
|
std::string(hiLoc, 1), std::string(loLoc, 1));
|
2018-04-04 09:03:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
implicitRules().SetType(*GetDeclTypeSpec(), loLoc, hiLoc);
|
2018-04-04 09:03:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitRulesVisitor::Pre(const parser::ImplicitSpec &) {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginDeclTypeSpec();
|
2018-04-04 09:03:51 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImplicitRulesVisitor::Post(const parser::ImplicitSpec &) {
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:04:08 -07:00
|
|
|
void ImplicitRulesVisitor::PushScope() {
|
2018-10-22 07:37:38 -07:00
|
|
|
implicitRules_ = std::make_unique<ImplicitRules>(std::move(implicitRules_));
|
|
|
|
implicitRules_->set_context(context());
|
2018-04-04 09:03:51 -07:00
|
|
|
prevImplicit_ = nullptr;
|
|
|
|
prevImplicitNone_ = nullptr;
|
|
|
|
prevImplicitNoneType_ = nullptr;
|
|
|
|
prevParameterStmt_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
void ImplicitRulesVisitor::PopScope() {
|
|
|
|
implicitRules_ = std::move(implicitRules_->parent());
|
2018-04-17 15:04:08 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// TODO: for all of these errors, reference previous statement too
|
|
|
|
bool ImplicitRulesVisitor::HandleImplicitNone(
|
|
|
|
const std::list<ImplicitNoneNameSpec> &nameSpecs) {
|
|
|
|
if (prevImplicitNone_ != nullptr) {
|
|
|
|
Say("More than one IMPLICIT NONE statement"_err_en_US);
|
2018-05-04 15:37:56 -07:00
|
|
|
Say(*prevImplicitNone_, "Previous IMPLICIT NONE statement"_en_US);
|
2018-04-04 09:03:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prevParameterStmt_ != nullptr) {
|
|
|
|
Say("IMPLICIT NONE statement after PARAMETER statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
prevImplicitNone_ = currStmtSource();
|
|
|
|
if (nameSpecs.empty()) {
|
|
|
|
prevImplicitNoneType_ = currStmtSource();
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneType(true);
|
2018-04-04 09:03:51 -07:00
|
|
|
if (prevImplicit_) {
|
|
|
|
Say("IMPLICIT NONE statement after IMPLICIT statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int sawType{0};
|
|
|
|
int sawExternal{0};
|
|
|
|
for (const auto noneSpec : nameSpecs) {
|
|
|
|
switch (noneSpec) {
|
|
|
|
case ImplicitNoneNameSpec::External:
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneExternal(true);
|
2018-04-05 17:02:31 -07:00
|
|
|
++sawExternal;
|
2018-04-04 09:03:51 -07:00
|
|
|
break;
|
|
|
|
case ImplicitNoneNameSpec::Type:
|
|
|
|
prevImplicitNoneType_ = currStmtSource();
|
2018-04-11 13:11:42 -07:00
|
|
|
implicitRules().set_isImplicitNoneType(true);
|
2018-04-04 09:03:51 -07:00
|
|
|
if (prevImplicit_) {
|
|
|
|
Say("IMPLICIT NONE(TYPE) after IMPLICIT statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-05 17:02:31 -07:00
|
|
|
++sawType;
|
2018-04-04 09:03:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sawType > 1) {
|
|
|
|
Say("TYPE specified more than once in IMPLICIT NONE statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sawExternal > 1) {
|
|
|
|
Say("EXTERNAL specified more than once in IMPLICIT NONE statement"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
// ArraySpecVisitor implementation
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(const parser::ArraySpec &x) {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::DeferredShapeSpecList &x) {
|
2018-05-14 14:26:39 -07:00
|
|
|
for (int i = 0; i < x.v; ++i) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeDeferred());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedShapeSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &lb{x.v};
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeAssumed(GetBound(*lb)) : ShapeSpec::MakeAssumed());
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::ExplicitShapeSpec &x) {
|
2018-11-06 17:18:06 -08:00
|
|
|
auto &&ub{GetBound(std::get<parser::SpecificationExpr>(x.t))};
|
|
|
|
if (const auto &lb{std::get<std::optional<parser::SpecificationExpr>>(x.t)}) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeExplicit(GetBound(*lb), std::move(ub)));
|
|
|
|
} else {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeExplicit(Bound{1}, std::move(ub)));
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedImpliedSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &lb{x.v};
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(
|
|
|
|
lb ? ShapeSpec::MakeImplied(GetBound(*lb)) : ShapeSpec::MakeImplied());
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:59:37 -08:00
|
|
|
void ArraySpecVisitor::Post(const parser::AssumedRankSpec &) {
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(ShapeSpec::MakeAssumedRank());
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:06:38 -07:00
|
|
|
const ArraySpec &ArraySpecVisitor::arraySpec() {
|
|
|
|
return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::BeginArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
CHECK(attrArraySpec_.empty());
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::EndArraySpec() {
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
attrArraySpec_.clear();
|
|
|
|
}
|
|
|
|
void ArraySpecVisitor::PostAttrSpec() {
|
|
|
|
if (!arraySpec_.empty()) {
|
|
|
|
// Example: integer, dimension(<1>) :: x(<2>)
|
|
|
|
// This saves <1> in attrArraySpec_ so we can process <2> into arraySpec_
|
|
|
|
CHECK(attrArraySpec_.empty());
|
|
|
|
attrArraySpec_.splice(attrArraySpec_.cbegin(), arraySpec_);
|
|
|
|
CHECK(arraySpec_.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
Bound ArraySpecVisitor::GetBound(const parser::SpecificationExpr &x) {
|
2019-01-07 15:05:53 -08:00
|
|
|
return Bound{EvaluateSubscriptIntExpr(x.v)};
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ScopeHandler implementation
|
|
|
|
|
2018-11-16 16:40:00 -08:00
|
|
|
void ScopeHandler::SayAlreadyDeclared(
|
|
|
|
const parser::Name &name, const Symbol &prev) {
|
|
|
|
Say2(name, "'%s' is already declared in this scoping unit"_err_en_US, prev,
|
|
|
|
"Previous declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
void ScopeHandler::SayDerivedType(
|
|
|
|
const SourceName &name, MessageFixedText &&msg, const Scope &type) {
|
|
|
|
Say(name, std::move(msg), name, type.name())
|
|
|
|
.Attach(type.name(), "Declaration of derived type '%s'"_en_US,
|
|
|
|
type.name().ToString().c_str());
|
|
|
|
}
|
|
|
|
void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
|
|
|
|
const Symbol &symbol, MessageFixedText &&msg2) {
|
|
|
|
Say(name.source, std::move(msg1))
|
|
|
|
.Attach(symbol.name(), msg2, symbol.name().ToString().c_str());
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
Scope &ScopeHandler::InclusiveScope() {
|
|
|
|
for (auto *scope{&currScope()};; scope = &scope->parent()) {
|
|
|
|
if (scope->kind() != Scope::Kind::Block &&
|
|
|
|
scope->kind() != Scope::Kind::DerivedType) {
|
|
|
|
return *scope;
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
common::die("inclusive scope not found");
|
|
|
|
}
|
|
|
|
Scope &ScopeHandler::GlobalScope() {
|
|
|
|
for (auto *scope = currScope_; scope; scope = &scope->parent()) {
|
|
|
|
if (scope->kind() == Scope::Kind::Global) {
|
|
|
|
return *scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
common::die("global scope not found");
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-08-22 16:56:57 -07:00
|
|
|
void ScopeHandler::PushScope(Scope::Kind kind, Symbol *symbol) {
|
|
|
|
PushScope(currScope().MakeScope(kind, symbol));
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
void ScopeHandler::PushScope(Scope &scope) {
|
2018-08-09 17:12:31 -07:00
|
|
|
currScope_ = &scope;
|
2018-11-16 12:43:08 -08:00
|
|
|
auto kind{currScope_->kind()};
|
|
|
|
if (kind != Scope::Kind::Block) {
|
2018-08-27 11:48:49 -07:00
|
|
|
ImplicitRulesVisitor::PushScope();
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (kind != Scope::Kind::DerivedType) {
|
|
|
|
if (auto *symbol{scope.symbol()}) {
|
|
|
|
// 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.
|
|
|
|
if (!FindInScope(scope, symbol->name())) {
|
|
|
|
auto &newSymbol{CopySymbol(*symbol)};
|
|
|
|
if (kind == Scope::Kind::Subprogram) {
|
|
|
|
newSymbol.set_details(symbol->get<SubprogramDetails>());
|
|
|
|
} else {
|
|
|
|
newSymbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ScopeHandler::PopScope() {
|
2018-09-22 08:05:46 -07:00
|
|
|
for (auto &pair : currScope()) {
|
|
|
|
auto &symbol{*pair.second};
|
|
|
|
ConvertToObjectEntity(symbol); // if not a proc by now, it is an object
|
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
if (currScope_->kind() != Scope::Kind::Block) {
|
|
|
|
ImplicitRulesVisitor::PopScope();
|
|
|
|
}
|
2018-08-09 17:12:31 -07:00
|
|
|
currScope_ = &currScope_->parent();
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *ScopeHandler::FindSymbol(const parser::Name &name) {
|
|
|
|
return FindSymbol(currScope(), name);
|
|
|
|
}
|
|
|
|
Symbol *ScopeHandler::FindSymbol(const Scope &scope, const parser::Name &name) {
|
|
|
|
return Resolve(name, scope.FindSymbol(name.source));
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol &ScopeHandler::Resolve(const parser::Name &name, Symbol &symbol) {
|
|
|
|
return *Resolve(name, &symbol);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *ScopeHandler::Resolve(const parser::Name &name, Symbol *symbol) {
|
|
|
|
if (symbol && !name.symbol) {
|
|
|
|
name.symbol = symbol;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol &ScopeHandler::MakeSymbol(
|
|
|
|
Scope &scope, const SourceName &name, Attrs attrs) {
|
|
|
|
auto *symbol{FindInScope(scope, name)};
|
|
|
|
if (symbol) {
|
|
|
|
symbol->attrs() |= attrs;
|
|
|
|
} else {
|
|
|
|
const auto pair{scope.try_emplace(name, attrs, UnknownDetails{})};
|
|
|
|
CHECK(pair.second); // name was not found, so must be able to add
|
|
|
|
symbol = pair.first->second;
|
|
|
|
}
|
|
|
|
return *symbol;
|
|
|
|
}
|
|
|
|
Symbol &ScopeHandler::MakeSymbol(const parser::Name &name, Attrs attrs) {
|
|
|
|
return Resolve(name, MakeSymbol(currScope(), name.source, attrs));
|
|
|
|
}
|
|
|
|
Symbol &ScopeHandler::CopySymbol(const Symbol &symbol) {
|
|
|
|
CHECK(!FindInScope(currScope(), symbol.name()));
|
|
|
|
return MakeSymbol(currScope(), symbol.name(), symbol.attrs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for name only in scope, not in enclosing scopes.
|
|
|
|
Symbol *ScopeHandler::FindInScope(
|
|
|
|
const Scope &scope, const parser::Name &name) {
|
|
|
|
return Resolve(name, FindInScope(scope, name.source));
|
|
|
|
}
|
|
|
|
Symbol *ScopeHandler::FindInScope(const Scope &scope, const SourceName &name) {
|
|
|
|
if (auto it{scope.find(name)}; it != scope.end()) {
|
|
|
|
return it->second;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeHandler::EraseSymbol(const parser::Name &name) {
|
|
|
|
currScope().erase(name.source);
|
|
|
|
name.symbol = nullptr;
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 14:26:39 -07:00
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
static bool NeedsType(const Symbol &symbol) {
|
|
|
|
if (symbol.GetType()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
|
|
|
if (details->interface().symbol()) {
|
|
|
|
return false; // the interface determines the type
|
|
|
|
}
|
|
|
|
if (!symbol.test(Symbol::Flag::Function)) {
|
|
|
|
return false; // not known to be a function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
void ScopeHandler::ApplyImplicitRules(Symbol &symbol) {
|
|
|
|
ConvertToObjectEntity(symbol);
|
2018-10-10 16:20:46 -07:00
|
|
|
if (NeedsType(symbol)) {
|
2018-10-18 07:55:48 -07:00
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(symbol.name(), "No explicit type declared for '%s'"_err_en_US);
|
2018-12-11 14:51:08 -08:00
|
|
|
} else if (const auto *type{GetImplicitType(symbol)}) {
|
2018-10-10 16:20:46 -07:00
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *ScopeHandler::GetImplicitType(Symbol &symbol) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &name{symbol.name()};
|
2018-12-11 14:51:08 -08:00
|
|
|
const auto *type{implicitRules().GetType(name.begin()[0])};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (type) {
|
|
|
|
symbol.set(Symbol::Flag::Implicit);
|
|
|
|
} else {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2018-04-25 19:58:42 -07:00
|
|
|
|
2018-09-22 08:05:46 -07:00
|
|
|
// Convert symbol to be a ObjectEntity or return false if it can't be.
|
|
|
|
bool ScopeHandler::ConvertToObjectEntity(Symbol &symbol) {
|
|
|
|
if (symbol.has<ObjectEntityDetails>()) {
|
|
|
|
// nothing to do
|
|
|
|
} else if (symbol.has<UnknownDetails>()) {
|
|
|
|
symbol.set_details(ObjectEntityDetails{});
|
|
|
|
} else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2019-01-04 17:10:31 -08:00
|
|
|
symbol.set_details(ObjectEntityDetails{std::move(*details)});
|
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)});
|
2018-09-22 08:05:46 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (symbol.GetType()) {
|
|
|
|
symbol.set(Symbol::Flag::Function);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
[&](const common::Indirection<parser::GenericSpec> &generic) {
|
2018-05-14 14:26:39 -07:00
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Name &name) { AddUse(name); },
|
2018-06-18 11:03:43 -07:00
|
|
|
[](const auto &) { common::die("TODO: GenericSpec"); },
|
2018-05-14 14:26:39 -07:00
|
|
|
},
|
|
|
|
generic->u);
|
|
|
|
},
|
|
|
|
[&](const parser::Name &name) { AddUse(name); },
|
|
|
|
[&](const parser::Rename &rename) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Rename::Names &names) { AddUse(names); },
|
|
|
|
[&](const parser::Rename::Operators &ops) {
|
2018-06-18 11:03:43 -07:00
|
|
|
common::die("TODO: Rename::Operators");
|
2018-05-14 14:26:39 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rename.u);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleVisitor::Pre(const parser::Rename::Names &x) {
|
|
|
|
AddUse(x);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set useModuleScope_ to the Scope of the module being used.
|
|
|
|
bool ModuleVisitor::Pre(const parser::UseStmt &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
useModuleScope_ = FindModule(x.moduleName);
|
2018-07-25 06:55:11 -07:00
|
|
|
return useModuleScope_ != nullptr;
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ModuleVisitor::Post(const parser::UseStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *list{std::get_if<std::list<parser::Rename>>(&x.u)}) {
|
2018-05-14 14:26:39 -07:00
|
|
|
// Not a use-only: collect the names that were used in renames,
|
|
|
|
// then add a use for each public name that was not renamed.
|
|
|
|
std::set<SourceName> useNames;
|
|
|
|
for (const auto &rename : *list) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 14:26:39 -07:00
|
|
|
[&](const parser::Rename::Names &names) {
|
|
|
|
useNames.insert(std::get<1>(names.t).source);
|
|
|
|
},
|
|
|
|
[&](const parser::Rename::Operators &ops) {
|
|
|
|
CHECK(!"TODO: Rename::Operators");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rename.u);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
for (const auto &[name, symbol] : *useModuleScope_) {
|
|
|
|
if (symbol->attrs().test(Attr::PUBLIC) &&
|
|
|
|
!symbol->detailsIf<MiscDetails>()) {
|
2018-05-14 14:26:39 -07:00
|
|
|
if (useNames.count(name) == 0) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *localSymbol{FindInScope(currScope(), name)};
|
|
|
|
if (!localSymbol) {
|
|
|
|
localSymbol = &CopySymbol(*symbol);
|
|
|
|
}
|
|
|
|
AddUse(x.moduleName.source, *symbol, *localSymbol);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useModuleScope_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(const parser::Rename::Names &names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &useName{std::get<0>(names.t)};
|
|
|
|
const auto &localName{std::get<1>(names.t)};
|
|
|
|
AddUse(useName.source, useName, localName);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
void ModuleVisitor::AddUse(const parser::Name &useName) {
|
2018-11-16 12:43:08 -08:00
|
|
|
AddUse(useName.source, useName, useName);
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
2018-05-14 14:26:39 -07:00
|
|
|
void ModuleVisitor::AddUse(const SourceName &location,
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &localName, const parser::Name &useName) {
|
2018-05-14 14:26:39 -07:00
|
|
|
if (!useModuleScope_) {
|
|
|
|
return; // error occurred finding module
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *useSymbol{FindInScope(*useModuleScope_, useName)};
|
|
|
|
if (!useSymbol) {
|
|
|
|
Say(useName, "'%s' not found in module '%s'"_err_en_US, useName.source,
|
2018-05-14 14:26:39 -07:00
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (useSymbol->attrs().test(Attr::PRIVATE)) {
|
|
|
|
Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, useName.source,
|
2018-05-14 14:26:39 -07:00
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
AddUse(location, *useSymbol, MakeSymbol(localName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(
|
|
|
|
const SourceName &location, const Symbol &useSymbol, Symbol &localSymbol) {
|
|
|
|
localSymbol.attrs() = useSymbol.attrs();
|
2018-05-14 14:26:39 -07:00
|
|
|
localSymbol.attrs() &= ~Attrs{Attr::PUBLIC, Attr::PRIVATE};
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.flags() = useSymbol.flags();
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{localSymbol.detailsIf<UseDetails>()}) {
|
2018-08-29 11:38:12 -07:00
|
|
|
// check for use-associating the same symbol again:
|
2018-05-14 14:26:39 -07:00
|
|
|
if (localSymbol.GetUltimate() != useSymbol.GetUltimate()) {
|
|
|
|
localSymbol.set_details(
|
2018-08-29 11:38:12 -07:00
|
|
|
UseErrorDetails{*details}.add_occurrence(location, *useModuleScope_));
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (auto *details{localSymbol.detailsIf<UseErrorDetails>()}) {
|
2018-05-14 14:26:39 -07:00
|
|
|
details->add_occurrence(location, *useModuleScope_);
|
2018-10-10 16:20:46 -07:00
|
|
|
} else if (!localSymbol.has<UnknownDetails>()) {
|
|
|
|
Say(location,
|
|
|
|
"Cannot use-associate '%s'; it is already declared in this scope"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.name())
|
2018-10-10 16:20:46 -07:00
|
|
|
.Attach(localSymbol.name(), "Previous declaration of '%s'"_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
localSymbol.name().ToString().c_str());
|
2018-05-14 14:26:39 -07:00
|
|
|
} else {
|
2018-08-29 11:38:12 -07:00
|
|
|
localSymbol.set_details(UseDetails{location, useSymbol});
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:21:27 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Submodule &x) {
|
|
|
|
auto &stmt{std::get<parser::Statement<parser::SubmoduleStmt>>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(stmt.statement.t)};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
|
|
|
auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &ancestorName{std::get<parser::Name>(parentId.t)};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &parentName{std::get<std::optional<parser::Name>>(parentId.t)};
|
|
|
|
Scope *ancestor{FindModule(ancestorName)};
|
|
|
|
if (!ancestor) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *parentScope{parentName ? FindModule(*parentName, ancestor) : ancestor};
|
2018-08-02 16:21:27 -07:00
|
|
|
if (!parentScope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PushScope(*parentScope); // submodule is hosted in parent
|
2018-11-16 12:43:08 -08:00
|
|
|
BeginModule(name, true, subpPart);
|
|
|
|
if (!ancestor->AddSubmodule(name.source, currScope())) {
|
2018-08-02 16:21:27 -07:00
|
|
|
Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
ancestorName.source, name.source);
|
2018-08-02 16:21:27 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-23 17:13:59 -07:00
|
|
|
void ModuleVisitor::Post(const parser::Submodule &) { ClearScopes(); }
|
2018-08-02 16:21:27 -07:00
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::Module &x) {
|
|
|
|
// Make a symbol and push a scope for this module
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{
|
2018-11-16 12:43:08 -08:00
|
|
|
std::get<parser::Statement<parser::ModuleStmt>>(x.t).statement.v};
|
2018-08-02 16:21:27 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
2018-11-05 14:36:11 -08:00
|
|
|
BeginModule(name, false, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
void ModuleVisitor::Post(const parser::Module &) {
|
2018-04-25 19:58:42 -07:00
|
|
|
ApplyDefaultAccess();
|
|
|
|
PopScope();
|
2018-05-17 13:06:38 -07:00
|
|
|
prevAccessStmt_ = nullptr;
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &ModuleVisitor::BeginModule(const parser::Name &name, bool isSubmodule,
|
2018-08-02 16:21:27 -07:00
|
|
|
const std::optional<parser::ModuleSubprogramPart> &subpPart) {
|
|
|
|
auto &symbol{MakeSymbol(name, ModuleDetails{isSubmodule})};
|
|
|
|
auto &details{symbol.get<ModuleDetails>()};
|
2018-08-22 16:56:57 -07:00
|
|
|
PushScope(Scope::Kind::Module, &symbol);
|
|
|
|
details.set_scope(&currScope());
|
2018-10-26 07:34:50 -07:00
|
|
|
WalkSubprogramPart(subpPart);
|
2018-08-02 16:21:27 -07:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a module or submodule by name and return its scope.
|
|
|
|
// If ancestor is present, look for a submodule of that ancestor module.
|
|
|
|
// May have to read a .mod file to find it.
|
|
|
|
// If an error occurs, report it and return nullptr.
|
2018-11-16 12:43:08 -08:00
|
|
|
Scope *ModuleVisitor::FindModule(const parser::Name &name, Scope *ancestor) {
|
2018-10-22 07:37:38 -07:00
|
|
|
ModFileReader reader{context()};
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *scope{reader.Read(name.source, ancestor)};
|
2018-08-02 16:21:27 -07:00
|
|
|
if (!scope) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (scope->kind() != Scope::Kind::Module) {
|
|
|
|
Say(name, "'%s' is not a module"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Resolve(name, scope->symbol());
|
2018-08-02 16:21:27 -07:00
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
void ModuleVisitor::ApplyDefaultAccess() {
|
2018-08-22 16:56:57 -07:00
|
|
|
for (auto &pair : currScope()) {
|
2018-06-19 16:06:41 -07:00
|
|
|
Symbol &symbol = *pair.second;
|
2018-04-25 19:58:42 -07:00
|
|
|
if (!symbol.attrs().HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
symbol.attrs().set(defaultAccess_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
// InterfaceVistor implementation
|
|
|
|
|
|
|
|
bool InterfaceVisitor::Pre(const parser::InterfaceStmt &x) {
|
|
|
|
inInterfaceBlock_ = true;
|
|
|
|
isAbstract_ = std::holds_alternative<parser::Abstract>(x.u);
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-17 13:06:38 -07:00
|
|
|
void InterfaceVisitor::Post(const parser::InterfaceStmt &) {}
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
void InterfaceVisitor::Post(const parser::EndInterfaceStmt &) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (genericName_) {
|
|
|
|
if (const auto *proc{GetGenericDetails().CheckSpecific()}) {
|
|
|
|
SayAlreadyDeclared(*genericName_, *proc);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
genericName_ = nullptr;
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
inInterfaceBlock_ = false;
|
|
|
|
isAbstract_ = false;
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
// Create a symbol for the name in this GenericSpec, if any.
|
2018-05-14 13:51:13 -07:00
|
|
|
bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericName_ = GetGenericSpecName(x);
|
|
|
|
if (!genericName_) {
|
|
|
|
return false;
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *genericSymbol{FindSymbol(*genericName_)};
|
|
|
|
if (genericSymbol) {
|
|
|
|
if (genericSymbol->has<DerivedTypeDetails>()) {
|
2018-06-22 08:21:19 -07:00
|
|
|
// A generic and derived type with same name: create a generic symbol
|
|
|
|
// and save derived type in it.
|
2018-11-16 12:43:08 -08:00
|
|
|
CHECK(genericSymbol->scope()->symbol() == genericSymbol);
|
2018-06-22 08:21:19 -07:00
|
|
|
GenericDetails details;
|
2018-11-16 12:43:08 -08:00
|
|
|
details.set_derivedType(*genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_);
|
|
|
|
genericSymbol->set_details(details);
|
|
|
|
} else if (!genericSymbol->IsSubprogram()) {
|
|
|
|
SayAlreadyDeclared(*genericName_, *genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = nullptr;
|
|
|
|
} else if (genericSymbol->has<UseDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// copy the USEd symbol into this scope so we can modify it
|
2018-11-16 12:43:08 -08:00
|
|
|
const Symbol &ultimate{genericSymbol->GetUltimate()};
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &CopySymbol(ultimate);
|
2018-12-06 06:59:37 -08:00
|
|
|
genericName_->symbol = genericSymbol;
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *details{ultimate.detailsIf<GenericDetails>()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericSymbol->set_details(GenericDetails{details->specificProcs()});
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{ultimate.detailsIf<SubprogramDetails>()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericSymbol->set_details(SubprogramDetails{*details});
|
2018-05-22 16:12:56 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
common::die("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (!genericSymbol) {
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_);
|
|
|
|
genericSymbol->set_details(GenericDetails{});
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (genericSymbol->has<GenericDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// okay
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (genericSymbol->has<SubprogramDetails>() ||
|
|
|
|
genericSymbol->has<SubprogramNameDetails>()) {
|
2018-06-19 16:06:41 -07:00
|
|
|
GenericDetails genericDetails;
|
2018-11-16 12:43:08 -08:00
|
|
|
genericDetails.set_specific(*genericSymbol);
|
|
|
|
EraseSymbol(*genericName_);
|
|
|
|
genericSymbol = &MakeSymbol(*genericName_, genericDetails);
|
2018-12-06 06:59:37 -08:00
|
|
|
} else {
|
|
|
|
common::die("unexpected kind of symbol");
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
CHECK(genericName_->symbol == genericSymbol);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InterfaceVisitor::Pre(const parser::ProcedureStmt &x) {
|
|
|
|
if (!isGeneric()) {
|
|
|
|
Say("A PROCEDURE statement is only allowed in a generic interface block"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
auto kind{std::get<parser::ProcedureStmt::Kind>(x.t)};
|
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
AddSpecificProcs(names, kind);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceVisitor::Post(const parser::GenericStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
genericName_->symbol->attrs().set(AccessSpecToAttr(*accessSpec));
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
AddSpecificProcs(names, ProcedureKind::Procedure);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
GenericDetails &InterfaceVisitor::GetGenericDetails() {
|
|
|
|
CHECK(genericName_);
|
2018-12-06 06:59:37 -08:00
|
|
|
CHECK(genericName_->symbol);
|
2018-11-16 12:43:08 -08:00
|
|
|
return genericName_->symbol->get<GenericDetails>();
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2018-12-18 07:59:40 -08:00
|
|
|
void InterfaceVisitor::AddSpecificProcs(
|
|
|
|
const std::list<parser::Name> &names, ProcedureKind kind) {
|
|
|
|
for (const auto &name : names) {
|
|
|
|
specificProcs_.emplace(genericName_->symbol, std::make_pair(&name, kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:46:11 -07:00
|
|
|
// By now we should have seen all specific procedures referenced by name in
|
|
|
|
// this generic interface. Resolve those names to symbols.
|
|
|
|
void InterfaceVisitor::ResolveSpecificsInGeneric(Symbol &generic) {
|
|
|
|
auto &details{generic.get<GenericDetails>()};
|
|
|
|
std::set<SourceName> namesSeen; // to check for duplicate names
|
|
|
|
for (const auto *symbol : details.specificProcs()) {
|
|
|
|
namesSeen.insert(symbol->name());
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
auto range{specificProcs_.equal_range(&generic)};
|
|
|
|
for (auto it{range.first}; it != range.second; ++it) {
|
|
|
|
auto *name{it->second.first};
|
|
|
|
auto kind{it->second.second};
|
|
|
|
const auto *symbol{FindSymbol(*name)};
|
2018-07-20 10:46:11 -07:00
|
|
|
if (!symbol) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "Procedure '%s' not found"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
symbol = &symbol->GetUltimate();
|
2018-07-20 10:46:11 -07:00
|
|
|
if (symbol == &generic) {
|
|
|
|
if (auto *specific{generic.get<GenericDetails>().specific()}) {
|
|
|
|
symbol = specific;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol->has<SubprogramDetails>() &&
|
|
|
|
!symbol->has<SubprogramNameDetails>()) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "'%s' is not a subprogram"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
if (kind == ProcedureKind::ModuleProcedure) {
|
2018-07-20 10:46:11 -07:00
|
|
|
const auto *d{symbol->detailsIf<SubprogramNameDetails>()};
|
|
|
|
if (!d || d->kind() != SubprogramKind::Module) {
|
2018-12-18 07:59:40 -08:00
|
|
|
Say(*name, "'%s' is not a module procedure"_err_en_US);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
if (!namesSeen.insert(name->source).second) {
|
|
|
|
Say(*name,
|
|
|
|
"Procedure '%s' is already specified in generic '%s'"_err_en_US,
|
|
|
|
name->source, generic.name());
|
2018-07-20 10:46:11 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-26 13:31:13 -08:00
|
|
|
details.add_specificProc(*symbol);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
2018-12-18 07:59:40 -08:00
|
|
|
specificProcs_.erase(range.first, range.second);
|
2018-07-20 10:46:11 -07:00
|
|
|
}
|
|
|
|
|
2018-07-05 10:28:34 -07:00
|
|
|
// Check that the specific procedures are all functions or all subroutines.
|
|
|
|
// If there is a derived type with the same name they must be functions.
|
|
|
|
// Set the corresponding flag on generic.
|
|
|
|
void InterfaceVisitor::CheckGenericProcedures(Symbol &generic) {
|
2018-07-20 10:46:11 -07:00
|
|
|
ResolveSpecificsInGeneric(generic);
|
2018-07-16 16:23:18 -07:00
|
|
|
auto &details{generic.get<GenericDetails>()};
|
2018-07-05 10:28:34 -07:00
|
|
|
auto &specifics{details.specificProcs()};
|
|
|
|
if (specifics.empty()) {
|
|
|
|
if (details.derivedType()) {
|
|
|
|
generic.set(Symbol::Flag::Function);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &firstSpecific{*specifics.front()};
|
|
|
|
bool isFunction{firstSpecific.test(Symbol::Flag::Function)};
|
|
|
|
for (auto *specific : specifics) {
|
|
|
|
if (isFunction != specific->test(Symbol::Flag::Function)) {
|
|
|
|
auto &msg{Say(generic.name(),
|
|
|
|
"Generic interface '%s' has both a function and a subroutine"_err_en_US)};
|
|
|
|
if (isFunction) {
|
|
|
|
msg.Attach(firstSpecific.name(), "Function declaration"_en_US);
|
|
|
|
msg.Attach(specific->name(), "Subroutine declaration"_en_US);
|
|
|
|
} else {
|
|
|
|
msg.Attach(firstSpecific.name(), "Subroutine declaration"_en_US);
|
|
|
|
msg.Attach(specific->name(), "Function declaration"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isFunction && details.derivedType()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayDerivedType(generic.name(),
|
2018-07-05 10:28:34 -07:00
|
|
|
"Generic interface '%s' may only contain functions due to derived type"
|
|
|
|
" with same name"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*details.derivedType()->scope());
|
2018-07-05 10:28:34 -07:00
|
|
|
}
|
|
|
|
generic.set(isFunction ? Symbol::Flag::Function : Symbol::Flag::Subroutine);
|
|
|
|
}
|
|
|
|
|
2018-05-04 15:37:56 -07:00
|
|
|
// SubprogramVisitor implementation
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
void SubprogramVisitor::Post(const parser::StmtFunctionStmt &x) {
|
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
return; // This wasn't really a stmt function so no scope was created
|
|
|
|
}
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
// Return false if it is actually an assignment statement.
|
|
|
|
bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-12-11 14:51:08 -08:00
|
|
|
const DeclTypeSpec *resultType{nullptr};
|
2018-05-04 15:37:56 -07:00
|
|
|
// Look up name: provides return type or tells us if it's an array
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindSymbol(name)}) {
|
2018-10-10 16:20:46 -07:00
|
|
|
auto *details{symbol->detailsIf<EntityDetails>()};
|
|
|
|
if (!details) {
|
2018-06-05 12:18:35 -07:00
|
|
|
badStmtFuncFound_ = true;
|
2018-10-10 16:20:46 -07:00
|
|
|
return false;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
// TODO: check that attrs are compatible with stmt func
|
|
|
|
resultType = details->type();
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
if (badStmtFuncFound_) {
|
|
|
|
Say(name, "'%s' has not been declared as an array"_err_en_US);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{PushSubprogramScope(name, Symbol::Flag::Function)};
|
|
|
|
auto &details{symbol.get<SubprogramDetails>()};
|
2018-05-04 15:37:56 -07:00
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
EntityDetails dummyDetails{true};
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *dummySymbol{FindInScope(currScope().parent(), dummyName)}) {
|
|
|
|
if (auto *d{dummySymbol->detailsIf<EntityDetails>()}) {
|
2018-05-04 15:37:56 -07:00
|
|
|
if (d->type()) {
|
|
|
|
dummyDetails.set_type(*d->type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 14:10:34 -07:00
|
|
|
details.add_dummyArg(MakeSymbol(dummyName, dummyDetails));
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name); // added by PushSubprogramScope
|
2018-05-04 15:37:56 -07:00
|
|
|
EntityDetails resultDetails;
|
|
|
|
if (resultType) {
|
|
|
|
resultDetails.set_type(*resultType);
|
|
|
|
}
|
|
|
|
details.set_result(MakeSymbol(name, resultDetails));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::Suffix &suffix) {
|
2018-05-17 13:06:38 -07:00
|
|
|
if (suffix.resultName) {
|
|
|
|
funcResultName_ = &suffix.resultName.value();
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool HasModulePrefix(const std::list<parser::PrefixSpec> &prefixes) {
|
2018-10-26 11:57:08 -07:00
|
|
|
for (const auto &prefix : prefixes) {
|
2018-10-26 07:34:50 -07:00
|
|
|
if (std::holds_alternative<parser::PrefixSpec::Module>(prefix.u)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SubroutineSubprogram &x) {
|
2018-10-26 07:34:50 -07:00
|
|
|
const auto &stmt{
|
|
|
|
std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement};
|
|
|
|
bool hasModulePrefix{
|
|
|
|
HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmt.t))};
|
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Subroutine, hasModulePrefix, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::SubroutineSubprogram &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::FunctionSubprogram &x) {
|
2018-10-26 07:34:50 -07:00
|
|
|
const auto &stmt{
|
|
|
|
std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement};
|
|
|
|
bool hasModulePrefix{
|
|
|
|
HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmt.t))};
|
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Function, hasModulePrefix, subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::FunctionSubprogram &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::InterfaceBody::Subroutine &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(
|
|
|
|
std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement.t)};
|
2018-12-26 13:31:13 -08:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Subroutine,
|
|
|
|
/*hasModulePrefix*/ false, std::nullopt);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::InterfaceBody::Subroutine &) {
|
|
|
|
EndSubprogram();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::InterfaceBody::Function &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(
|
|
|
|
std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement.t)};
|
2018-10-26 11:57:08 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Function, /*hasModulePrefix*/ false, std::nullopt);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
void SubprogramVisitor::Post(const parser::InterfaceBody::Function &) {
|
|
|
|
EndSubprogram();
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:45:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SubroutineStmt &stmt) {
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-07-11 17:45:13 -07:00
|
|
|
}
|
2018-05-04 15:37:56 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::FunctionStmt &stmt) {
|
2018-05-14 13:51:13 -07:00
|
|
|
if (!subpNamesOnly_) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
CHECK(!funcResultName_);
|
|
|
|
}
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::SubroutineStmt &stmt) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &details{PostSubprogramStmt(name)};
|
2018-05-04 15:37:56 -07:00
|
|
|
for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
|
|
|
|
const parser::Name *dummyName = std::get_if<parser::Name>(&dummyArg.u);
|
|
|
|
CHECK(dummyName != nullptr && "TODO: alternate return indicator");
|
|
|
|
Symbol &dummy{MakeSymbol(*dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::FunctionStmt &stmt) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(stmt.t)};
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &details{PostSubprogramStmt(name)};
|
2018-05-04 15:37:56 -07:00
|
|
|
for (const auto &dummyName : std::get<std::list<parser::Name>>(stmt.t)) {
|
|
|
|
Symbol &dummy{MakeSymbol(dummyName, EntityDetails(true))};
|
|
|
|
details.add_dummyArg(dummy);
|
|
|
|
}
|
|
|
|
// add function result to function scope
|
|
|
|
EntityDetails funcResultDetails;
|
2018-12-11 14:51:08 -08:00
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
2018-06-05 12:18:35 -07:00
|
|
|
funcResultDetails.set_type(*type);
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
|
|
|
|
const parser::Name *funcResultName;
|
2018-05-14 13:51:13 -07:00
|
|
|
if (funcResultName_ && funcResultName_->source != name.source) {
|
2018-05-04 15:37:56 -07:00
|
|
|
funcResultName = funcResultName_;
|
|
|
|
} 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
|
|
|
}
|
|
|
|
details.set_result(MakeSymbol(*funcResultName, funcResultDetails));
|
2018-05-17 13:06:38 -07:00
|
|
|
funcResultName_ = nullptr;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
SubprogramDetails &SubprogramVisitor::PostSubprogramStmt(
|
|
|
|
const parser::Name &name) {
|
|
|
|
Symbol &symbol{*currScope().symbol()};
|
|
|
|
CHECK(name.source == symbol.name());
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-10-26 07:34:50 -07:00
|
|
|
symbol.attrs() |= EndAttrs();
|
|
|
|
if (symbol.attrs().test(Attr::MODULE)) {
|
|
|
|
symbol.attrs().set(Attr::EXTERNAL, false);
|
|
|
|
}
|
|
|
|
return symbol.get<SubprogramDetails>();
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
|
2018-10-26 07:34:50 -07:00
|
|
|
Symbol::Flag subpFlag, bool hasModulePrefix,
|
2018-05-14 13:51:13 -07:00
|
|
|
const std::optional<parser::InternalSubprogramPart> &subpPart) {
|
|
|
|
if (subpNamesOnly_) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{MakeSymbol(name, SubprogramNameDetails{*subpNamesOnly_})};
|
2018-06-05 12:18:35 -07:00
|
|
|
symbol.set(subpFlag);
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-26 07:34:50 -07:00
|
|
|
if (hasModulePrefix && !inInterfaceBlock()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindSymbol(name)};
|
2018-10-26 11:57:08 -07:00
|
|
|
if (!symbol || !symbol->IsSeparateModuleProc()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name, "'%s' was not declared a separate module procedure"_err_en_US);
|
2018-10-26 07:34:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (symbol->owner() == currScope()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
// separate module procedure declared and defined in same module
|
|
|
|
PushScope(*symbol->scope());
|
2018-10-26 07:34:50 -07:00
|
|
|
} else {
|
|
|
|
PushSubprogramScope(name, subpFlag);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PushSubprogramScope(name, subpFlag);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-10-26 07:34:50 -07:00
|
|
|
WalkSubprogramPart(subpPart);
|
2018-05-14 13:51:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void SubprogramVisitor::EndSubprogram() {
|
|
|
|
if (!subpNamesOnly_) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:34:50 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SeparateModuleSubprogram &x) {
|
|
|
|
if (subpNamesOnly_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto &name{
|
|
|
|
std::get<parser::Statement<parser::MpSubprogramStmt>>(x.t).statement.v};
|
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-10-26 11:57:08 -07:00
|
|
|
return BeginSubprogram(
|
|
|
|
name, Symbol::Flag::Subroutine, /*hasModulePrefix*/ true, subpPart);
|
2018-10-26 07:34:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubprogramVisitor::Post(const parser::SeparateModuleSubprogram &) {
|
|
|
|
EndSubprogram();
|
|
|
|
}
|
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &SubprogramVisitor::PushSubprogramScope(
|
|
|
|
const parser::Name &name, Symbol::Flag subpFlag) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{GetSpecificFromGeneric(name)};
|
2018-05-22 16:12:56 -07:00
|
|
|
if (!symbol) {
|
|
|
|
symbol = &MakeSymbol(name, SubprogramDetails{});
|
2018-06-05 12:18:35 -07:00
|
|
|
symbol->set(subpFlag);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
PushScope(Scope::Kind::Subprogram, symbol);
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &details{symbol->get<SubprogramDetails>()};
|
2018-05-14 13:51:13 -07:00
|
|
|
if (inInterfaceBlock()) {
|
|
|
|
details.set_isInterface();
|
|
|
|
if (!isAbstract()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
if (isGeneric()) {
|
2018-12-26 13:31:13 -08:00
|
|
|
GetGenericDetails().add_specificProc(*symbol);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-08-27 11:48:49 -07:00
|
|
|
implicitRules().set_inheritFromParent(false);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
FindSymbol(name)->set(subpFlag);
|
2018-05-22 16:12:56 -07:00
|
|
|
return *symbol;
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// If name is a generic, return specific subprogram with the same name.
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *SubprogramVisitor::GetSpecificFromGeneric(const parser::Name &name) {
|
2018-08-28 14:02:53 -07:00
|
|
|
if (auto *symbol{FindSymbol(name)}) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol->detailsIf<GenericDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// found generic, want subprogram
|
2018-07-17 07:02:30 -07:00
|
|
|
auto *specific{details->specific()};
|
2018-05-22 16:12:56 -07:00
|
|
|
if (isGeneric()) {
|
|
|
|
if (specific) {
|
2018-06-22 08:21:19 -07:00
|
|
|
SayAlreadyDeclared(name, *specific);
|
2018-05-22 16:12:56 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
EraseSymbol(name);
|
|
|
|
specific = &MakeSymbol(name, Attrs{}, SubprogramDetails{});
|
|
|
|
GetGenericDetails().set_specific(*specific);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (specific) {
|
|
|
|
if (!specific->has<SubprogramDetails>()) {
|
|
|
|
specific->set_details(SubprogramDetails{});
|
|
|
|
}
|
|
|
|
return specific;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-05-04 15:37:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
// DeclarationVisitor implementation
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::BeginDecl() {
|
2018-04-12 12:59:42 -07:00
|
|
|
BeginDeclTypeSpec();
|
|
|
|
BeginArraySpec();
|
2018-08-31 15:15:28 -07:00
|
|
|
return BeginAttrs();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::EndDecl() {
|
2018-04-12 12:59:42 -07:00
|
|
|
EndDeclTypeSpec();
|
|
|
|
EndArraySpec();
|
2018-08-31 15:15:28 -07:00
|
|
|
EndAttrs();
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
bool DeclarationVisitor::CheckUseError(const parser::Name &name) {
|
|
|
|
const auto *details{name.symbol->detailsIf<UseErrorDetails>()};
|
2018-08-29 11:38:12 -07:00
|
|
|
if (!details) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Message &msg{Say(name, "Reference to '%s' is ambiguous"_err_en_US)};
|
2018-11-16 12:43:08 -08:00
|
|
|
for (const auto &[location, module] : details->occurrences()) {
|
2018-08-29 11:38:12 -07:00
|
|
|
msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
name.ToString().data(), module->name().ToString().data());
|
2018-08-29 11:38:12 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
// Report error if accessibility of symbol doesn't match isPrivate.
|
|
|
|
void DeclarationVisitor::CheckAccessibility(
|
|
|
|
const parser::Name &name, bool isPrivate, const Symbol &symbol) {
|
|
|
|
if (symbol.attrs().test(Attr::PRIVATE) != isPrivate) {
|
|
|
|
Say2(name,
|
|
|
|
"'%s' does not have the same accessibility as its previous declaration"_err_en_US,
|
|
|
|
symbol, "Previous declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::DimensionStmt::Declaration &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
DeclareObjectEntity(name, Attrs{});
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::EntityDecl &x) {
|
|
|
|
// TODO: may be under StructureStmt
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t)};
|
2018-05-30 14:11:45 -07:00
|
|
|
// TODO: CoarraySpec, CharLength, Initialization
|
2018-06-05 12:18:35 -07:00
|
|
|
Attrs attrs{attrs_ ? *attrs_ : Attrs{}};
|
2018-11-06 17:18:06 -08:00
|
|
|
Symbol &symbol{DeclareUnknownEntity(name, attrs)};
|
|
|
|
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
|
|
|
|
if (ConvertToObjectEntity(symbol)) {
|
2018-12-06 06:59:37 -08:00
|
|
|
if (auto *expr{std::get_if<parser::ConstantExpr>(&init->u)}) {
|
|
|
|
symbol.get<ObjectEntityDetails>().set_init(EvaluateExpr(*expr));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
auto &name{std::get<parser::Name>(x.t)};
|
|
|
|
if (std::get<parser::BindEntity::Kind>(x.t) ==
|
|
|
|
parser::BindEntity::Kind::Object) {
|
2018-11-16 12:43:08 -08:00
|
|
|
HandleAttributeStmt(Attr::BIND_C, name);
|
2018-09-10 12:20:42 -07:00
|
|
|
} else {
|
|
|
|
// TODO: name is common block
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-06 06:59:37 -08:00
|
|
|
void DeclarationVisitor::Post(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)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say2(name, "PARAMETER attribute not allowed on '%s'"_err_en_US, symbol,
|
|
|
|
"Declaration of '%s'"_en_US);
|
2018-12-06 06:59:37 -08:00
|
|
|
return;
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
const auto &expr{std::get<parser::ConstantExpr>(x.t)};
|
2018-12-06 06:59:37 -08:00
|
|
|
symbol.get<ObjectEntityDetails>().set_init(EvaluateExpr(expr));
|
2018-09-22 08:05:46 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
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)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say2(name, "EXTERNAL attribute not allowed on '%s'"_err_en_US, *symbol,
|
|
|
|
"Declaration of '%s'"_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)};
|
|
|
|
return HandleAttributeStmt(IntentSpecToAttr(intentSpec), names);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::IntrinsicStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::INTRINSIC, x.v);
|
|
|
|
}
|
2018-05-30 14:11:45 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::OptionalStmt &x) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::OPTIONAL, x.v);
|
|
|
|
}
|
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) {
|
2018-04-11 13:11:42 -07:00
|
|
|
return HandleAttributeStmt(Attr::VALUE, x.v);
|
|
|
|
}
|
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-01-04 17:10:31 -08:00
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
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-01-04 17:10:31 -08:00
|
|
|
if (attr != Attr::BIND_C || !SetBindNameOn(*symbol)) {
|
|
|
|
symbol->attrs().set(attr);
|
|
|
|
}
|
|
|
|
return *symbol;
|
2018-09-10 12:20:42 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::ObjectDecl &x) {
|
2018-04-12 12:59:42 -07:00
|
|
|
CHECK(objectDeclAttr_.has_value());
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t)};
|
2018-11-16 12:43:08 -08:00
|
|
|
DeclareObjectEntity(name, Attrs{*objectDeclAttr_});
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
// Declare an entity not yet known to be an object or proc.
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareUnknownEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs) {
|
2018-10-10 16:20:46 -07:00
|
|
|
if (!arraySpec().empty()) {
|
2018-10-18 07:55:48 -07:00
|
|
|
return DeclareObjectEntity(name, attrs);
|
2018-10-10 16:20:46 -07:00
|
|
|
} else {
|
|
|
|
Symbol &symbol{DeclareEntity<EntityDetails>(name, attrs)};
|
2018-12-11 14:51:08 -08:00
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SetType(name, *type);
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-10-10 16:20:46 -07:00
|
|
|
if (symbol.attrs().test(Attr::EXTERNAL)) {
|
|
|
|
ConvertToProcEntity(symbol);
|
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareProcEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs, const ProcInterface &interface) {
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &symbol{DeclareEntity<ProcEntityDetails>(name, attrs)};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
2018-06-05 12:18:35 -07:00
|
|
|
if (interface.type()) {
|
|
|
|
symbol.set(Symbol::Flag::Function);
|
|
|
|
} else if (interface.symbol()) {
|
|
|
|
symbol.set(interface.symbol()->test(Symbol::Flag::Function)
|
|
|
|
? Symbol::Flag::Function
|
|
|
|
: Symbol::Flag::Subroutine);
|
|
|
|
}
|
2018-07-28 14:10:34 -07:00
|
|
|
details->set_interface(interface);
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
|
|
|
SetPassNameOn(symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
Symbol &DeclarationVisitor::DeclareObjectEntity(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Attrs attrs) {
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, attrs)};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
2018-12-11 14:51:08 -08:00
|
|
|
if (auto *type{GetDeclTypeSpec()}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SetType(name, *type);
|
2018-04-12 12:59:42 -07:00
|
|
|
}
|
|
|
|
if (!arraySpec().empty()) {
|
|
|
|
if (!details->shape().empty()) {
|
|
|
|
Say(name,
|
|
|
|
"The dimensions of '%s' have already been declared"_err_en_US);
|
|
|
|
} else {
|
|
|
|
details->set_shape(arraySpec());
|
|
|
|
}
|
|
|
|
ClearArraySpec();
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2019-01-04 17:10:31 -08:00
|
|
|
SetBindNameOn(symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-10-18 07:55:48 -07:00
|
|
|
return symbol;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-12-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-17 15:46:30 -08:00
|
|
|
if (charInfo_.kind == 0) {
|
|
|
|
charInfo_.kind =
|
|
|
|
context().defaultKinds().GetDefaultKind(TypeCategory::Character);
|
|
|
|
}
|
2018-12-17 12:41:43 -08:00
|
|
|
SetDeclTypeSpec(currScope().MakeCharacterType(
|
2018-12-14 14:04:15 -08:00
|
|
|
std::move(*charInfo_.length), charInfo_.kind));
|
|
|
|
charInfo_ = {};
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::CharSelector::LengthAndKind &x) {
|
|
|
|
if (auto maybeExpr{EvaluateExpr(x.kind)}) {
|
2019-01-07 11:28:20 -08:00
|
|
|
if (std::optional<std::int64_t> kind{evaluate::ToInt64(*maybeExpr)}) {
|
|
|
|
charInfo_.kind = *kind;
|
|
|
|
} else {
|
|
|
|
common::die("TODO: kind did not evaluate to a constant integer");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Class &x) {
|
2018-12-11 14:51:08 -08:00
|
|
|
// created by default with TypeDerived; change to ClassDerived
|
|
|
|
GetDeclTypeSpec()->set_category(DeclTypeSpec::ClassDerived);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Record &) {
|
|
|
|
return true; // TODO
|
2018-08-31 16:20:00 -07:00
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::DerivedTypeSpec &x) {
|
2019-01-07 15:05:53 -08:00
|
|
|
const auto &typeName{std::get<parser::Name>(x.t)};
|
|
|
|
if (const auto *symbol{ResolveDerivedType(&typeName)}) {
|
|
|
|
SetDeclTypeSpec(typeName, currScope().MakeDerivedType(*symbol));
|
2018-12-11 14:51:08 -08:00
|
|
|
GetDeclTypeSpec()->derivedTypeSpec().set_scope(*symbol->scope());
|
|
|
|
}
|
2019-01-07 15:05:53 -08:00
|
|
|
return true;
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::DerivedTypeDef &x) {
|
2018-09-04 10:28:27 -07:00
|
|
|
std::set<SourceName> paramNames;
|
|
|
|
auto &scope{currScope()};
|
2018-12-06 17:52:43 -08:00
|
|
|
auto &details{scope.symbol()->get<DerivedTypeDetails>()};
|
2018-09-04 10:28:27 -07:00
|
|
|
auto &stmt{std::get<parser::Statement<parser::DerivedTypeStmt>>(x.t)};
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
if (derivedTypeInfo_.sawContains) {
|
|
|
|
Say(stmt.source,
|
|
|
|
"A sequence type may not have a CONTAINS statement"_err_en_US); // C740
|
|
|
|
}
|
|
|
|
}
|
|
|
|
derivedTypeInfo_ = {};
|
2018-09-04 10:28:27 -07:00
|
|
|
PopScope();
|
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) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(x.t)};
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{MakeSymbol(name, GetAttrs(), DerivedTypeDetails{})};
|
2018-06-22 08:21:19 -07:00
|
|
|
PushScope(Scope::Kind::DerivedType, &symbol);
|
2018-09-20 14:08:59 -07:00
|
|
|
if (auto *extendsName{derivedTypeInfo_.extends}) {
|
2019-01-07 15:05:53 -08:00
|
|
|
if (const Symbol * extends{ResolveDerivedType(extendsName)}) {
|
|
|
|
symbol.get<DerivedTypeDetails>().set_extends(extendsName->source);
|
2018-09-20 14:08:59 -07:00
|
|
|
// Declare the "parent component"; private if the type is
|
2019-01-07 15:05:53 -08:00
|
|
|
if (OkToAddComponent(*extendsName, extends)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
|
|
|
|
comp.attrs().set(Attr::PRIVATE, extends->attrs().test(Attr::PRIVATE));
|
|
|
|
comp.set(Symbol::Flag::ParentComp);
|
2019-01-07 15:05:53 -08:00
|
|
|
auto &type{currScope().MakeDerivedType(*extends)};
|
2018-12-11 14:51:08 -08:00
|
|
|
type.derivedTypeSpec().set_scope(currScope());
|
|
|
|
comp.SetType(type);
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
2018-09-06 08:01:49 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
EndAttrs();
|
|
|
|
}
|
2018-09-04 10:28:27 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
|
2018-12-11 14:51:08 -08:00
|
|
|
auto *type{GetDeclTypeSpec()};
|
2018-09-05 16:02:41 -07:00
|
|
|
auto attr{std::get<common::TypeParamAttr>(x.t)};
|
2018-09-04 10:28:27 -07:00
|
|
|
for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(decl.t)};
|
2018-11-06 17:18:06 -08:00
|
|
|
auto details{TypeParamDetails{attr}};
|
|
|
|
if (auto &init{
|
|
|
|
std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
|
2019-01-07 15:05:53 -08:00
|
|
|
details.set_init(EvaluateIntExpr(*init));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
2018-12-06 17:52:43 -08:00
|
|
|
if (MakeTypeSymbol(name, std::move(details))) {
|
|
|
|
SetType(name, *type);
|
|
|
|
}
|
2018-09-04 10:28:27 -07:00
|
|
|
}
|
|
|
|
EndDecl();
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::TypeAttrSpec::Extends &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
derivedTypeInfo_.extends = &x.v;
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-06 08:01:49 -07:00
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::PrivateStmt &x) {
|
2018-09-07 09:06:27 -07:00
|
|
|
if (!currScope().parent().IsModule()) {
|
2018-09-06 08:01:49 -07:00
|
|
|
Say("PRIVATE is only allowed in a derived type that is"
|
|
|
|
" in a module"_err_en_US); // C766
|
|
|
|
} else if (derivedTypeInfo_.sawContains) {
|
|
|
|
derivedTypeInfo_.privateBindings = true;
|
|
|
|
} else if (!derivedTypeInfo_.privateComps) {
|
|
|
|
derivedTypeInfo_.privateComps = true;
|
|
|
|
} else {
|
|
|
|
Say("PRIVATE may not appear more than once in"
|
2018-09-07 09:06:27 -07:00
|
|
|
" derived type components"_en_US); // C738
|
2018-09-06 08:01:49 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::SequenceStmt &x) {
|
2018-09-06 08:01:49 -07:00
|
|
|
derivedTypeInfo_.sequence = true;
|
2018-06-05 12:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ComponentDecl &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-09-06 08:01:49 -07:00
|
|
|
auto attrs{GetAttrs()};
|
|
|
|
if (derivedTypeInfo_.privateComps &&
|
|
|
|
!attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
attrs.set(Attr::PRIVATE);
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
if (OkToAddComponent(name)) {
|
2018-11-06 17:18:06 -08:00
|
|
|
auto &symbol{DeclareObjectEntity(name, attrs)};
|
|
|
|
if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
|
|
|
|
if (auto *initExpr{std::get_if<parser::ConstantExpr>(&init->u)}) {
|
2018-12-06 06:59:37 -08:00
|
|
|
details->set_init(EvaluateExpr(*initExpr));
|
2018-11-06 17:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
ClearArraySpec();
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcedureDeclarationStmt &) {
|
|
|
|
CHECK(!interfaceName_);
|
|
|
|
return BeginDecl();
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcedureDeclarationStmt &) {
|
|
|
|
interfaceName_ = nullptr;
|
|
|
|
EndDecl();
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::ProcComponentDefStmt &) {
|
|
|
|
CHECK(!interfaceName_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcComponentDefStmt &) {
|
|
|
|
interfaceName_ = nullptr;
|
|
|
|
}
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcInterface &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *name{std::get_if<parser::Name>(&x.u)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
interfaceName_ = name;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::ProcDecl &x) {
|
|
|
|
ProcInterface interface;
|
|
|
|
if (interfaceName_) {
|
2018-08-31 16:20:00 -07:00
|
|
|
if (auto *symbol{FindExplicitInterface(*interfaceName_)}) {
|
2018-06-06 11:41:42 -07:00
|
|
|
interface.set_symbol(*symbol);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
} else if (auto *type{GetDeclTypeSpec()}) {
|
2018-06-06 11:41:42 -07:00
|
|
|
interface.set_type(*type);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-09-06 12:06:32 -07:00
|
|
|
auto attrs{GetAttrs()};
|
|
|
|
if (currScope().kind() != Scope::Kind::DerivedType) {
|
|
|
|
attrs.set(Attr::EXTERNAL);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
const auto &name{std::get<parser::Name>(x.t)};
|
2018-09-24 11:43:48 -07:00
|
|
|
DeclareProcEntity(name, attrs, interface);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
|
2018-09-06 08:01:49 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::TypeBoundProcedurePart &x) {
|
|
|
|
derivedTypeInfo_.sawContains = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
Say2(procedureName,
|
|
|
|
"'%s' is not a module procedure or external procedure"
|
|
|
|
" with explicit interface"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*procedure, "Declaration of '%s'"_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) {
|
|
|
|
const auto &genericSpec{
|
|
|
|
std::get<common::Indirection<parser::GenericSpec>>(x.t)};
|
|
|
|
const auto *genericName{GetGenericSpecName(*genericSpec)};
|
|
|
|
if (!genericName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isPrivate{derivedTypeInfo_.privateBindings};
|
|
|
|
if (auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)}) {
|
|
|
|
isPrivate = accessSpec->v == parser::AccessSpec::Kind::Private;
|
|
|
|
}
|
|
|
|
const SymbolList *inheritedProcs{nullptr}; // specific procs from parent type
|
|
|
|
auto *genericSymbol{FindInScope(currScope(), *genericName)};
|
|
|
|
if (genericSymbol) {
|
|
|
|
if (!genericSymbol->has<GenericBindingDetails>()) {
|
|
|
|
genericSymbol = nullptr; // MakeTypeSymbol will report the error below
|
|
|
|
}
|
|
|
|
} else if (const auto *inheritedSymbol{FindTypeSymbol(*genericName)}) {
|
|
|
|
// look in parent types:
|
|
|
|
if (inheritedSymbol->has<GenericBindingDetails>()) {
|
|
|
|
inheritedProcs =
|
|
|
|
&inheritedSymbol->get<GenericBindingDetails>().specificProcs();
|
|
|
|
CheckAccessibility(*genericName, isPrivate, *inheritedSymbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (genericSymbol) {
|
|
|
|
CheckAccessibility(*genericName, isPrivate, *genericSymbol);
|
|
|
|
} else {
|
|
|
|
genericSymbol = MakeTypeSymbol(*genericName, GenericBindingDetails{});
|
|
|
|
if (!genericSymbol) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isPrivate) {
|
|
|
|
genericSymbol->attrs().set(Attr::PRIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &details{genericSymbol->get<GenericBindingDetails>()};
|
|
|
|
if (inheritedProcs) {
|
|
|
|
details.add_specificProcs(*inheritedProcs);
|
|
|
|
}
|
|
|
|
for (const auto &bindingName : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
const auto *symbol{FindTypeSymbol(bindingName)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(bindingName,
|
|
|
|
"Binding name '%s' not found in this derived type"_err_en_US);
|
|
|
|
} else if (!symbol->has<ProcBindingDetails>()) {
|
|
|
|
Say2(bindingName,
|
|
|
|
"'%s' is not the name of a specific binding of this type"_err_en_US,
|
|
|
|
*symbol, "Declaration of '%s'"_en_US);
|
|
|
|
} else {
|
|
|
|
details.add_specificProc(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 &) {
|
2018-12-11 14:51:08 -08:00
|
|
|
ResolveDerivedType();
|
2018-10-18 07:55:48 -07:00
|
|
|
EndDeclTypeSpec();
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:19:41 -08:00
|
|
|
bool DeclarationVisitor::Pre(const parser::StructureConstructor &x) {
|
|
|
|
auto savedState{SetDeclTypeSpecState({})};
|
2018-10-18 07:55:48 -07:00
|
|
|
BeginDeclTypeSpec();
|
2018-12-18 17:19:41 -08:00
|
|
|
Walk(std::get<parser::DerivedTypeSpec>(x.t));
|
|
|
|
Walk(std::get<std::list<parser::ComponentSpec>>(x.t));
|
2018-12-11 14:51:08 -08:00
|
|
|
ResolveDerivedType();
|
2018-10-18 07:55:48 -07:00
|
|
|
EndDeclTypeSpec();
|
2018-12-18 17:19:41 -08:00
|
|
|
SetDeclTypeSpecState(savedState);
|
|
|
|
return false;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *DeclarationVisitor::DeclareConstructEntity(const parser::Name &name) {
|
2018-10-18 07:55:48 -07:00
|
|
|
auto *prev{FindSymbol(name)};
|
|
|
|
if (prev) {
|
2018-12-11 14:03:55 -08:00
|
|
|
if (prev->owner().kind() == Scope::Kind::Forall ||
|
|
|
|
prev->owner() == currScope()) {
|
2018-10-18 07:55:48 -07:00
|
|
|
SayAlreadyDeclared(name, *prev);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
name.symbol = nullptr;
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
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,
|
2018-11-16 12:43:08 -08:00
|
|
|
*prev, "Previous declaration of '%s'"_en_US);
|
2018-10-18 07:55:48 -07:00
|
|
|
return nullptr;
|
|
|
|
} else if (auto *type{prev->GetType()}) {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
|
|
|
return &symbol;
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:08:59 -07:00
|
|
|
// Set the type of an entity or report an error.
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::SetType(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, const DeclTypeSpec &type) {
|
|
|
|
CHECK(name.symbol);
|
|
|
|
auto &symbol{*name.symbol};
|
2018-09-10 12:20:42 -07:00
|
|
|
auto *prevType{symbol.GetType()};
|
|
|
|
if (!prevType) {
|
|
|
|
symbol.SetType(type);
|
|
|
|
} else if (!symbol.test(Symbol::Flag::Implicit)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say2(name, "The type of '%s' has already been declared"_err_en_US, symbol,
|
|
|
|
"Declaration of '%s'"_en_US);
|
2018-09-10 12:20:42 -07:00
|
|
|
} else if (type != *prevType) {
|
2018-09-11 07:26:54 -07:00
|
|
|
Say2(name,
|
|
|
|
"The type of '%s' has already been implicitly declared"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol, "Declaration of '%s'"_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
// Find the Symbol for this derived type; derivedTypeName if not specified.
|
|
|
|
const Symbol *DeclarationVisitor::ResolveDerivedType(const parser::Name *name) {
|
|
|
|
if (name == nullptr) {
|
|
|
|
name = derivedTypeName();
|
|
|
|
if (name == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto *symbol{FindSymbol(*name)};
|
2018-08-29 11:38:12 -07:00
|
|
|
if (!symbol) {
|
2018-12-11 14:51:08 -08:00
|
|
|
Say(*name, "Derived type '%s' not found"_err_en_US);
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
if (CheckUseError(*name)) {
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto *details{symbol->detailsIf<UseDetails>()}) {
|
|
|
|
symbol = &details->symbol();
|
|
|
|
}
|
|
|
|
if (auto *details{symbol->detailsIf<GenericDetails>()}) {
|
|
|
|
if (details->derivedType()) {
|
|
|
|
symbol = details->derivedType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol->has<DerivedTypeDetails>()) {
|
2018-12-11 14:51:08 -08:00
|
|
|
Say(*name, "'%s' is not a derived type"_err_en_US);
|
2018-08-29 11:38:12 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:20:00 -07:00
|
|
|
// Check this symbol suitable as a type-bound procedure - C769
|
|
|
|
bool DeclarationVisitor::CanBeTypeBoundProc(const Symbol &symbol) {
|
|
|
|
if (symbol.has<SubprogramNameDetails>()) {
|
|
|
|
return symbol.owner().kind() == Scope::Kind::Module;
|
|
|
|
} else if (auto *details{symbol.detailsIf<SubprogramDetails>()}) {
|
|
|
|
return symbol.owner().kind() == Scope::Kind::Module ||
|
|
|
|
details->isInterface();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *DeclarationVisitor::FindExplicitInterface(const parser::Name &name) {
|
2018-08-31 16:20:00 -07:00
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name, "Explicit interface '%s' not found"_err_en_US);
|
|
|
|
} else if (!symbol->HasExplicitInterface()) {
|
|
|
|
Say2(name,
|
|
|
|
"'%s' is not an abstract interface or a procedure with an"
|
|
|
|
" explicit interface"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*symbol, "Declaration of '%s'"_en_US);
|
2018-08-31 16:20:00 -07:00
|
|
|
symbol = nullptr;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2018-12-26 13:31:13 -08:00
|
|
|
// Find a component by name in the current derived type or its parents.
|
|
|
|
const Symbol *DeclarationVisitor::FindTypeSymbol(const parser::Name &name) {
|
|
|
|
for (const Scope *scope{&currScope()};;) {
|
|
|
|
CHECK(scope->kind() == Scope::Kind::DerivedType);
|
2019-01-07 15:05:53 -08:00
|
|
|
if (const Symbol * symbol{FindInScope(*scope, name)}) {
|
2018-12-26 13:31:13 -08:00
|
|
|
return symbol;
|
|
|
|
}
|
2019-01-07 16:46:11 -08:00
|
|
|
const Symbol *parent{scope->symbol()->GetParent()};
|
|
|
|
if (parent == nullptr) {
|
2018-12-26 13:31:13 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-07 16:46:11 -08:00
|
|
|
scope = parent->scope();
|
2018-12-26 13:31:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:20:00 -07:00
|
|
|
// Create a symbol for a type parameter, component, or procedure binding in
|
2018-12-06 17:52:43 -08:00
|
|
|
// the current derived type scope. Return false on error.
|
2018-12-26 13:31:13 -08:00
|
|
|
Symbol *DeclarationVisitor::MakeTypeSymbol(
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name &name, Details &&details) {
|
2018-08-31 16:20:00 -07:00
|
|
|
Scope &derivedType{currScope()};
|
|
|
|
CHECK(derivedType.kind() == Scope::Kind::DerivedType);
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindInScope(derivedType, name)}) {
|
2018-08-31 16:20:00 -07:00
|
|
|
Say2(name,
|
|
|
|
"Type parameter, component, or procedure binding '%s'"
|
|
|
|
" already defined in this type"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*symbol, "Previous definition of '%s'"_en_US);
|
2018-12-26 13:31:13 -08:00
|
|
|
return nullptr;
|
2018-08-31 16:20:00 -07:00
|
|
|
} else {
|
2018-09-06 08:01:49 -07:00
|
|
|
auto attrs{GetAttrs()};
|
|
|
|
// Apply binding-private-stmt if present and this is a procedure binding
|
|
|
|
if (derivedTypeInfo_.privateBindings &&
|
|
|
|
!attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE}) &&
|
|
|
|
std::holds_alternative<ProcBindingDetails>(details)) {
|
|
|
|
attrs.set(Attr::PRIVATE);
|
|
|
|
}
|
2018-12-26 13:31:13 -08:00
|
|
|
return &MakeSymbol(name, attrs, details);
|
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-09-20 14:08:59 -07:00
|
|
|
const Scope *scope{&currScope()};
|
|
|
|
for (bool inParent{false};; inParent = true) {
|
|
|
|
CHECK(scope->kind() == Scope::Kind::DerivedType);
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *prev{FindInScope(*scope, name)}) {
|
2018-11-16 16:40:00 -08:00
|
|
|
auto msg{""_en_US};
|
2019-01-07 15:05:53 -08:00
|
|
|
if (extends != nullptr) {
|
2018-09-20 14:08:59 -07:00
|
|
|
msg = "Type cannot be extended as it has a component named"
|
|
|
|
" '%s'"_err_en_US;
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (prev->test(Symbol::Flag::ParentComp)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
msg = "'%s' is a parent type of this type and so cannot be"
|
|
|
|
" a component"_err_en_US;
|
|
|
|
} else if (inParent) {
|
|
|
|
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;
|
|
|
|
}
|
2019-01-07 15:05:53 -08:00
|
|
|
if (!inParent && extends != nullptr) {
|
|
|
|
// The parent component has not yet been added to the scope.
|
|
|
|
scope = extends->scope();
|
|
|
|
} else if (const Symbol * parent{scope->symbol()->GetParent()}) {
|
|
|
|
scope = parent->scope();
|
|
|
|
} else {
|
2018-09-20 14:08:59 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
// ConstructVisitor implementation
|
2018-09-25 08:53:53 -07:00
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
bool ConstructVisitor::Pre(const parser::ConcurrentHeader &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::ConcurrentHeader &) {
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::Local &x) {
|
|
|
|
for (auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{DeclareConstructEntity(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
symbol->set(Symbol::Flag::LocalityLocal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::LocalInit &x) {
|
|
|
|
for (auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{DeclareConstructEntity(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
symbol->set(Symbol::Flag::LocalityLocalInit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
|
|
|
|
for (auto &name : x.v) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *prev{FindSymbol(name)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
if (prev->owner() == currScope()) {
|
2018-11-16 12:43:08 -08:00
|
|
|
SayAlreadyDeclared(name, *prev);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
auto &symbol{MakeSymbol(name, HostAssocDetails{*prev})};
|
|
|
|
symbol.set(Symbol::Flag::LocalityShared);
|
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name, "Variable '%s' not found"_err_en_US);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
|
|
|
|
auto &objects{std::get<std::list<parser::DataIDoObject>>(x.t)};
|
|
|
|
auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(x.t)};
|
|
|
|
auto &bounds{
|
|
|
|
std::get<parser::LoopBounds<parser::ScalarIntConstantExpr>>(x.t)};
|
|
|
|
if (type) {
|
|
|
|
BeginDeclTypeSpec();
|
2018-12-06 06:59:37 -08:00
|
|
|
DeclTypeSpecVisitor::Post(*type);
|
2018-10-18 07:55:48 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{DeclareConstructEntity(bounds.name.thing.thing)}) {
|
2018-10-18 07:55:48 -07:00
|
|
|
CheckIntegerType(*symbol);
|
|
|
|
}
|
|
|
|
if (type) {
|
|
|
|
EndDeclTypeSpec();
|
|
|
|
}
|
|
|
|
Walk(bounds);
|
|
|
|
Walk(objects);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::DataStmt &) {
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::DataStmt &) { PopScope(); }
|
|
|
|
|
|
|
|
bool ConstructVisitor::Pre(const parser::DoConstruct &x) {
|
|
|
|
if (x.IsDoConcurrent()) {
|
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ConstructVisitor::Post(const parser::DoConstruct &x) {
|
|
|
|
if (x.IsDoConcurrent()) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructVisitor::Post(const parser::ConcurrentControl &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &name{std::get<parser::Name>(x.t)};
|
2018-10-18 07:55:48 -07:00
|
|
|
if (auto *symbol{DeclareConstructEntity(name)}) {
|
|
|
|
CheckIntegerType(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:55:48 -07:00
|
|
|
void ConstructVisitor::CheckIntegerType(const Symbol &symbol) {
|
|
|
|
if (auto *type{symbol.GetType()}) {
|
2018-12-17 12:41:43 -08:00
|
|
|
if (!type->IsNumeric(TypeCategory::Integer)) {
|
2018-10-18 07:55:48 -07:00
|
|
|
Say(symbol.name(), "Variable '%s' is not scalar integer"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
// ResolveNamesVisitor implementation
|
|
|
|
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::CommonBlockObject &x) {
|
|
|
|
BeginArraySpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::CommonBlockObject &x) {
|
|
|
|
ClearArraySpec();
|
|
|
|
// TODO: CommonBlockObject
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::PrefixSpec &x) {
|
|
|
|
return true; // TODO
|
|
|
|
}
|
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::FunctionReference &) {
|
|
|
|
expectedProcFlag_ = Symbol::Flag::Function;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::FunctionReference &) {
|
|
|
|
expectedProcFlag_ = std::nullopt;
|
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::CallStmt &) {
|
|
|
|
expectedProcFlag_ = Symbol::Flag::Subroutine;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::CallStmt &) {
|
|
|
|
expectedProcFlag_ = std::nullopt;
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
|
2018-08-22 16:56:57 -07:00
|
|
|
auto &scope{currScope()};
|
2018-08-23 11:24:12 -07:00
|
|
|
// Check C896 and C899: where IMPORT statements are allowed
|
2018-08-22 16:05:06 -07:00
|
|
|
switch (scope.kind()) {
|
|
|
|
case Scope::Kind::Module:
|
2018-09-07 09:06:27 -07:00
|
|
|
if (scope.IsModule()) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say("IMPORT is not allowed in a module scoping unit"_err_en_US);
|
|
|
|
return false;
|
2018-08-23 11:45:49 -07:00
|
|
|
} else if (x.kind == common::ImportKind::None) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say("IMPORT,NONE is not allowed in a submodule scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Scope::Kind::MainProgram:
|
|
|
|
Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
case Scope::Kind::Subprogram:
|
|
|
|
if (scope.parent().kind() == Scope::Kind::Global) {
|
|
|
|
Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
2018-08-23 11:45:49 -07:00
|
|
|
if (auto error{scope.SetImportKind(x.kind)}) {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say(std::move(*error));
|
|
|
|
}
|
|
|
|
for (auto &name : x.names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (FindSymbol(scope.parent(), name)) {
|
|
|
|
scope.add_importName(name.source);
|
|
|
|
} else {
|
2018-08-22 16:05:06 -07:00
|
|
|
Say(name, "'%s' not found in host scope"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevImportStmt_ = currStmtSource();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveStructureComponent(
|
2018-06-22 08:21:19 -07:00
|
|
|
const parser::StructureComponent &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return FindComponent(ResolveDataRef(x.base), x.component);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveArrayElement(
|
2018-08-29 11:38:12 -07:00
|
|
|
const parser::ArrayElement &x) {
|
2018-10-10 16:20:46 -07:00
|
|
|
// TODO: need to resolve these
|
|
|
|
// for (auto &subscript : x.subscripts) {
|
|
|
|
// ResolveSectionSubscript(subscript);
|
|
|
|
//}
|
2018-08-29 11:38:12 -07:00
|
|
|
return ResolveDataRef(x.base);
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
|
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveCoindexedNamedObject(
|
2018-08-29 11:38:12 -07:00
|
|
|
const parser::CoindexedNamedObject &x) {
|
|
|
|
return nullptr; // TODO
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveDataRef(
|
|
|
|
const parser::DataRef &x) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[=](const parser::Name &y) { return ResolveName(y); },
|
2018-06-22 08:21:19 -07:00
|
|
|
[=](const common::Indirection<parser::StructureComponent> &y) {
|
|
|
|
return ResolveStructureComponent(*y);
|
|
|
|
},
|
2018-08-29 11:38:12 -07:00
|
|
|
[=](const common::Indirection<parser::ArrayElement> &y) {
|
|
|
|
return ResolveArrayElement(*y);
|
|
|
|
},
|
|
|
|
[=](const common::Indirection<parser::CoindexedNamedObject> &y) {
|
|
|
|
return ResolveCoindexedNamedObject(*y);
|
2018-06-22 08:21:19 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
// If implicit types are allowed, ensure name is in the symbol table.
|
|
|
|
// Otherwise, report an error if it hasn't been declared.
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::ResolveName(const parser::Name &name) {
|
|
|
|
if (FindSymbol(name)) {
|
|
|
|
if (CheckUseError(name)) {
|
2018-10-10 16:20:46 -07:00
|
|
|
return nullptr; // reported an error
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
return &name;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Create the symbol then ensure it is accessible
|
2018-11-16 12:43:08 -08:00
|
|
|
MakeSymbol(InclusiveScope(), name.source, Attrs{});
|
2018-10-10 16:20:46 -07:00
|
|
|
auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name,
|
|
|
|
"'%s' from host scoping unit is not accessible due to IMPORT"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ApplyImplicitRules(*symbol);
|
2018-11-16 12:43:08 -08:00
|
|
|
return &name;
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
// base is a part-ref of a derived type; find the named component in its type.
|
2018-11-16 12:43:08 -08:00
|
|
|
const parser::Name *ResolveNamesVisitor::FindComponent(
|
|
|
|
const parser::Name *base, const parser::Name &component) {
|
|
|
|
if (!base || !base->symbol) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
auto &symbol{*base->symbol};
|
|
|
|
if (!ConvertToObjectEntity(symbol)) {
|
|
|
|
Say2(*base, "'%s' is an invalid base for a component reference"_err_en_US,
|
|
|
|
symbol, "Declaration of '%s'"_en_US);
|
|
|
|
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()}) {
|
|
|
|
if (const auto *scope{derived->scope()}) {
|
|
|
|
if (!FindComponent(*scope, component)) {
|
|
|
|
SayDerivedType(component.source,
|
|
|
|
"Component '%s' not found in derived type '%s'"_err_en_US, *scope);
|
|
|
|
} else if (CheckAccessibleComponent(component)) {
|
|
|
|
return &component;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
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-01-07 13:31:50 -08:00
|
|
|
Say2(*base, "'%s' is not an object of derived type"_err_en_US, symbol,
|
|
|
|
"Declaration of '%s'"_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
|
|
|
}
|
|
|
|
|
|
|
|
// Check that component is accessible from current scope.
|
2018-11-16 12:43:08 -08:00
|
|
|
bool ResolveNamesVisitor::CheckAccessibleComponent(
|
|
|
|
const parser::Name &component) {
|
|
|
|
CHECK(component.symbol);
|
|
|
|
auto &symbol{*component.symbol};
|
|
|
|
if (!symbol.attrs().test(Attr::PRIVATE)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
return true;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
CHECK(symbol.owner().kind() == Scope::Kind::DerivedType);
|
2018-09-20 14:08:59 -07:00
|
|
|
// component must be in a module/submodule because of PRIVATE:
|
2018-11-16 12:43:08 -08:00
|
|
|
const Scope &moduleScope{symbol.owner().parent()};
|
2018-09-20 14:08:59 -07:00
|
|
|
CHECK(moduleScope.kind() == Scope::Kind::Module);
|
|
|
|
for (auto *scope{&currScope()}; scope->kind() != Scope::Kind::Global;
|
|
|
|
scope = &scope->parent()) {
|
|
|
|
if (scope == &moduleScope) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(component,
|
2018-09-20 14:08:59 -07:00
|
|
|
"PRIVATE component '%s' is only accessible within module '%s'"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
component.ToString(), moduleScope.name());
|
2018-09-20 14:08:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look in this type's scope and then its parents for component.
|
|
|
|
Symbol *ResolveNamesVisitor::FindComponent(
|
2018-11-16 12:43:08 -08:00
|
|
|
const Scope &type, const parser::Name &component) {
|
2018-09-20 14:08:59 -07:00
|
|
|
CHECK(type.kind() == Scope::Kind::DerivedType);
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindInScope(type, component)}) {
|
|
|
|
return symbol;
|
2018-09-20 14:08:59 -07:00
|
|
|
}
|
2019-01-07 15:05:53 -08:00
|
|
|
if (const Symbol * parent{type.symbol()->GetParent()}) {
|
|
|
|
return FindComponent(*parent->scope(), component);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2019-01-07 15:05:53 -08:00
|
|
|
return nullptr;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:11:45 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::ProcedureDesignator &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *name{std::get_if<parser::Name>(&x.u)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
auto *symbol{FindSymbol(*name)};
|
2018-07-09 15:25:49 -07:00
|
|
|
if (symbol == nullptr) {
|
2018-12-18 16:35:31 -08:00
|
|
|
symbol = &MakeSymbol(context().globalScope(), name->source, Attrs{});
|
|
|
|
Resolve(*name, *symbol);
|
|
|
|
if (symbol->has<ModuleDetails>()) {
|
|
|
|
Say2(*name,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US,
|
|
|
|
*symbol, "Declaration of '%s'"_en_US);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 15:25:49 -07:00
|
|
|
if (isImplicitNoneExternal() && !symbol->attrs().test(Attr::EXTERNAL)) {
|
2018-05-30 14:11:45 -07:00
|
|
|
Say(*name,
|
|
|
|
"'%s' is an external procedure without the EXTERNAL"
|
|
|
|
" attribute in a scope with IMPLICIT NONE(EXTERNAL)"_err_en_US);
|
2018-12-18 16:35:31 -08:00
|
|
|
return;
|
2018-05-30 14:11:45 -07:00
|
|
|
}
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
2018-12-18 16:35:31 -08:00
|
|
|
if (!symbol->has<ProcEntityDetails>()) {
|
|
|
|
symbol->set_details(ProcEntityDetails{});
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto type{GetImplicitType(*symbol)}) {
|
2018-07-16 16:23:18 -07:00
|
|
|
symbol->get<ProcEntityDetails>().interface().set_type(*type);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-12-18 16:35:31 -08:00
|
|
|
SetProcFlag(*name, *symbol);
|
2018-07-09 15:25:49 -07:00
|
|
|
} else if (symbol->has<UnknownDetails>()) {
|
|
|
|
CHECK(!"unexpected UnknownDetails");
|
2018-11-16 12:43:08 -08:00
|
|
|
} else if (CheckUseError(*name)) {
|
2018-05-30 14:11:45 -07:00
|
|
|
// error was reported
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol = Resolve(*name, &symbol->GetUltimate());
|
2018-09-22 08:05:46 -07:00
|
|
|
ConvertToProcEntity(*symbol);
|
2018-12-18 16:35:31 -08:00
|
|
|
if (!SetProcFlag(*name, *symbol)) {
|
|
|
|
return; // reported error
|
|
|
|
}
|
|
|
|
if (symbol->has<ProcEntityDetails>() ||
|
|
|
|
symbol->has<SubprogramDetails>() ||
|
|
|
|
symbol->has<DerivedTypeDetails>() ||
|
|
|
|
symbol->has<ObjectEntityDetails>() ||
|
|
|
|
symbol->has<SubprogramNameDetails>() ||
|
|
|
|
symbol->has<GenericDetails>()) {
|
|
|
|
// these are all valid as procedure-designators
|
2018-09-07 09:48:40 -07:00
|
|
|
} else if (symbol->test(Symbol::Flag::Implicit)) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(*name,
|
2018-09-07 09:48:40 -07:00
|
|
|
"Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-11-16 12:43:08 -08:00
|
|
|
Say2(*name,
|
2018-05-30 14:49:40 -07:00
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US,
|
2018-11-16 12:43:08 -08:00
|
|
|
*symbol, "Declaration of '%s'"_en_US);
|
2018-05-30 14:11:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:35:31 -08:00
|
|
|
// Check and set the Function or Subroutine flag on symbol; false on error.
|
|
|
|
bool ResolveNamesVisitor::SetProcFlag(
|
|
|
|
const parser::Name &name, Symbol &symbol) {
|
|
|
|
CHECK(expectedProcFlag_);
|
|
|
|
if (symbol.test(Symbol::Flag::Function) &&
|
|
|
|
expectedProcFlag_ == Symbol::Flag::Subroutine) {
|
|
|
|
Say2(name, "Cannot call function '%s' like a subroutine"_err_en_US, symbol,
|
|
|
|
"Declaration of '%s'"_en_US);
|
|
|
|
return false;
|
|
|
|
} else if (symbol.test(Symbol::Flag::Subroutine) &&
|
|
|
|
expectedProcFlag_ == Symbol::Flag::Function) {
|
|
|
|
Say2(name, "Cannot call subroutine '%s' like a function"_err_en_US, symbol,
|
|
|
|
"Declaration of '%s'"_en_US);
|
|
|
|
return false;
|
|
|
|
} else if (symbol.has<ProcEntityDetails>()) {
|
|
|
|
symbol.set(*expectedProcFlag_); // in case it hasn't been set yet
|
|
|
|
if (expectedProcFlag_ == Symbol::Flag::Function) {
|
|
|
|
ApplyImplicitRules(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
|
2018-08-28 14:02:53 -07:00
|
|
|
Attr accessAttr{AccessSpecToAttr(std::get<parser::AccessSpec>(x.t))};
|
2018-08-22 16:56:57 -07:00
|
|
|
if (currScope().kind() != Scope::Kind::Module) {
|
2018-05-03 15:57:56 -07:00
|
|
|
Say(*currStmtSource(),
|
|
|
|
"%s statement may only appear in the specification part of a module"_err_en_US,
|
|
|
|
EnumToString(accessAttr));
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &accessIds{std::get<std::list<parser::AccessId>>(x.t)};
|
2018-04-24 17:05:58 -07:00
|
|
|
if (accessIds.empty()) {
|
|
|
|
if (prevAccessStmt_) {
|
2018-05-04 15:40:40 -07:00
|
|
|
Say("The default accessibility of this module has already been declared"_err_en_US)
|
|
|
|
.Attach(*prevAccessStmt_, "Previous declaration"_en_US);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
prevAccessStmt_ = currStmtSource();
|
|
|
|
defaultAccess_ = accessAttr;
|
|
|
|
} else {
|
|
|
|
for (const auto &accessId : accessIds) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-24 17:05:58 -07:00
|
|
|
[=](const parser::Name &y) { SetAccess(y, accessAttr); },
|
2018-06-18 11:03:43 -07:00
|
|
|
[=](const common::Indirection<parser::GenericSpec> &y) {
|
2018-04-24 17:05:58 -07:00
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-04-24 17:05:58 -07:00
|
|
|
[=](const parser::Name &z) {
|
|
|
|
SetAccess(z, accessAttr);
|
|
|
|
},
|
2018-06-18 11:03:43 -07:00
|
|
|
[](const auto &) { common::die("TODO: GenericSpec"); },
|
2018-04-24 17:05:58 -07:00
|
|
|
},
|
|
|
|
y->u);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
accessId.u);
|
|
|
|
}
|
2018-05-04 11:18:34 +02:00
|
|
|
}
|
2018-04-24 17:05:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the access specification for this name.
|
2018-04-25 19:58:42 -07:00
|
|
|
void ModuleVisitor::SetAccess(const parser::Name &name, Attr attr) {
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol &symbol{MakeSymbol(name)};
|
2018-04-25 10:46:39 -07:00
|
|
|
Attrs &attrs{symbol.attrs()};
|
|
|
|
if (attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
|
|
|
|
// PUBLIC/PRIVATE already set: make it a fatal error if it changed
|
|
|
|
Attr prev = attrs.test(Attr::PUBLIC) ? Attr::PUBLIC : Attr::PRIVATE;
|
2018-11-16 12:43:08 -08:00
|
|
|
Say(name,
|
2018-05-03 15:57:56 -07:00
|
|
|
attr == prev
|
|
|
|
? "The accessibility of '%s' has already been specified as %s"_en_US
|
|
|
|
: "The accessibility of '%s' has already been specified as %s"_err_en_US,
|
|
|
|
name.source, EnumToString(prev));
|
2018-04-24 17:05:58 -07:00
|
|
|
} else {
|
2018-04-25 10:46:39 -07:00
|
|
|
attrs.set(attr);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 11:41:42 -07:00
|
|
|
static bool NeedsExplicitType(const Symbol &symbol) {
|
2018-06-05 12:18:35 -07:00
|
|
|
if (symbol.has<UnknownDetails>()) {
|
2018-06-06 11:41:42 -07:00
|
|
|
return true;
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<EntityDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return !details->type();
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return !details->type();
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
2018-06-06 11:41:42 -07:00
|
|
|
return details->interface().symbol() == nullptr &&
|
|
|
|
details->interface().type() == nullptr;
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-06-06 11:41:42 -07:00
|
|
|
return false;
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::SpecificationPart &) {
|
2018-04-18 15:06:35 -07:00
|
|
|
badStmtFuncFound_ = false;
|
2018-08-22 16:05:06 -07:00
|
|
|
CheckImports();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:05:06 -07:00
|
|
|
void ResolveNamesVisitor::CheckImports() {
|
2018-08-22 16:56:57 -07:00
|
|
|
auto &scope{currScope()};
|
2018-08-23 11:45:49 -07:00
|
|
|
switch (scope.GetImportKind()) {
|
|
|
|
case common::ImportKind::None: break;
|
|
|
|
case common::ImportKind::All:
|
2018-08-22 16:05:06 -07:00
|
|
|
// C8102: all entities in host must not be hidden
|
|
|
|
for (const auto &pair : scope.parent()) {
|
|
|
|
auto &name{pair.first};
|
|
|
|
if (name != scope.name()) {
|
|
|
|
CheckImport(*prevImportStmt_, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-08-23 11:45:49 -07:00
|
|
|
case common::ImportKind::Default:
|
|
|
|
case common::ImportKind::Only:
|
2018-08-22 16:05:06 -07:00
|
|
|
// C8102: entities named in IMPORT must not be hidden
|
|
|
|
for (auto &name : scope.importNames()) {
|
|
|
|
CheckImport(name, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveNamesVisitor::CheckImport(
|
|
|
|
const SourceName &location, const SourceName &name) {
|
2018-11-16 12:43:08 -08:00
|
|
|
if (auto *symbol{FindInScope(currScope(), name)}) {
|
|
|
|
Say(location, "'%s' from host is not accessible"_err_en_US, name)
|
|
|
|
.Attach(symbol->name(), "'%s' is hidden by this entity"_en_US,
|
|
|
|
symbol->name().ToString().c_str());
|
2018-08-22 16:05:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::MainProgram &x) {
|
|
|
|
using stmtType = std::optional<parser::Statement<parser::ProgramStmt>>;
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *symbol{nullptr};
|
2018-08-08 17:28:16 -07:00
|
|
|
if (auto &stmt{std::get<stmtType>(x.t)}) {
|
2018-11-16 12:43:08 -08:00
|
|
|
symbol = &MakeSymbol(stmt->statement.v, MainProgramDetails{});
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
PushScope(Scope::Kind::MainProgram, symbol);
|
2018-10-26 07:34:50 -07:00
|
|
|
auto &subpPart{std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
|
|
|
WalkSubprogramPart(subpPart);
|
2018-04-11 13:11:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:57:56 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::EndProgramStmt &) { PopScope(); }
|
2018-03-30 13:57:23 -07:00
|
|
|
|
2018-08-27 11:48:49 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::ImplicitStmt &x) {
|
|
|
|
if (currScope().kind() == Scope::Kind::Block) {
|
|
|
|
Say("IMPLICIT statement is not allowed in BLOCK construct"_err_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ImplicitRulesVisitor::Pre(x);
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::PointerObject &x) {
|
|
|
|
std::visit(
|
2018-08-29 11:38:12 -07:00
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::StructureComponent &x) {
|
|
|
|
ResolveStructureComponent(x);
|
2018-08-29 11:38:12 -07:00
|
|
|
},
|
2018-10-10 16:20:46 -07:00
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
|
|
|
|
std::visit(
|
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::Name &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::StructureComponent &x) {
|
|
|
|
ResolveStructureComponent(x);
|
2018-08-29 11:38:12 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::PointerAssignmentStmt &x) {
|
|
|
|
ResolveDataRef(std::get<parser::DataRef>(x.t));
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::Designator &x) {
|
|
|
|
std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-11-16 12:43:08 -08:00
|
|
|
[&](const parser::ObjectName &x) { ResolveName(x); },
|
2018-10-10 16:20:46 -07:00
|
|
|
[&](const parser::DataRef &x) { ResolveDataRef(x); },
|
2018-09-10 12:20:42 -07:00
|
|
|
[&](const parser::Substring &x) {
|
2018-10-10 16:20:46 -07:00
|
|
|
ResolveDataRef(std::get<parser::DataRef>(x.t));
|
|
|
|
// TODO: SubstringRange
|
2018-04-24 17:05:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
}
|
2018-11-06 17:18:06 -08:00
|
|
|
|
2018-10-10 16:20:46 -07:00
|
|
|
template<typename T>
|
|
|
|
void ResolveNamesVisitor::Post(const parser::LoopBounds<T> &x) {
|
2018-11-16 12:43:08 -08:00
|
|
|
ResolveName(x.name.thing.thing);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::ProcComponentRef &x) {
|
|
|
|
ResolveStructureComponent(x.v.thing);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::TypeGuardStmt &x) {
|
|
|
|
DeclTypeSpecVisitor::Post(x);
|
2018-10-18 07:55:48 -07:00
|
|
|
ConstructVisitor::Post(x);
|
2018-10-10 16:20:46 -07:00
|
|
|
}
|
|
|
|
bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
|
|
|
|
if (!HandleStmtFunction(x)) {
|
|
|
|
// This is an array element assignment: resolve names of indices
|
|
|
|
const auto &names{std::get<std::list<parser::Name>>(x.t)};
|
|
|
|
for (auto &name : names) {
|
2018-11-16 12:43:08 -08:00
|
|
|
ResolveName(name);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 17:05:58 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
void ResolveNamesVisitor::Post(const parser::Program &) {
|
|
|
|
// ensure that all temps were deallocated
|
|
|
|
CHECK(!attrs_);
|
2018-06-05 12:18:35 -07:00
|
|
|
CHECK(!GetDeclTypeSpec());
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
void ResolveNames(SemanticsContext &context, const parser::Program &program) {
|
|
|
|
ResolveNamesVisitor{context}.Walk(program);
|
2018-04-18 15:06:35 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:43:08 -08:00
|
|
|
// Get the Name out of a GenericSpec, or nullptr if none.
|
|
|
|
static const parser::Name *GetGenericSpecName(const parser::GenericSpec &x) {
|
|
|
|
const auto *op{std::get_if<parser::DefinedOperator>(&x.u)};
|
|
|
|
if (!op) {
|
|
|
|
return std::get_if<parser::Name>(&x.u);
|
|
|
|
} else if (const auto *opName{std::get_if<parser::DefinedOpName>(&op->u)}) {
|
|
|
|
return &opName->v;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|