2018-05-01 12:50:34 -07:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-12 14:20:26 -07:00
|
|
|
#include "resolve-names.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "attr.h"
|
2018-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"
|
|
|
|
#include "symbol.h"
|
|
|
|
#include "type.h"
|
2018-06-18 11:03:43 -07:00
|
|
|
#include "../common/indirection.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>
|
|
|
|
#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
|
|
|
#include <stack>
|
2018-07-25 06:55:11 -07:00
|
|
|
#include <vector>
|
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;
|
|
|
|
|
|
|
|
class MessageHandler;
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
static GenericSpec MapGenericSpec(const parser::GenericSpec &);
|
|
|
|
|
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-08-27 11:48:49 -07:00
|
|
|
ImplicitRules(MessageHandler &messages)
|
|
|
|
: messages_{messages}, inheritFromParent_{false} {}
|
|
|
|
ImplicitRules(std::unique_ptr<ImplicitRules> &&parent)
|
|
|
|
: messages_{parent->messages_}, inheritFromParent_{true} {
|
|
|
|
parent_.swap(parent);
|
|
|
|
}
|
|
|
|
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-06-22 08:21:19 -07:00
|
|
|
std::optional<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_;
|
2018-04-04 09:03:51 -07:00
|
|
|
MessageHandler &messages_;
|
2018-08-27 11:48:49 -07:00
|
|
|
std::optional<bool> isImplicitNoneType_;
|
|
|
|
std::optional<bool> isImplicitNoneExternal_;
|
|
|
|
bool inheritFromParent_; // look in parent if not specified here
|
2018-04-05 17:02:31 -07:00
|
|
|
// map initial character of identifier to nullptr or its default type
|
|
|
|
std::map<char, const DeclTypeSpec> map_;
|
2018-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-03-22 17:08:20 -07:00
|
|
|
// Provide Post methods to collect attributes into a member variable.
|
|
|
|
class AttrsVisitor {
|
|
|
|
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();
|
2018-03-30 19:49:00 -07:00
|
|
|
void Post(const parser::LanguageBindingSpec &);
|
|
|
|
bool Pre(const parser::AccessSpec &);
|
|
|
|
bool Pre(const parser::IntentSpec &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// Simple case: encountering CLASSNAME causes ATTRNAME to be set.
|
|
|
|
#define HANDLE_ATTR_CLASS(CLASSNAME, ATTRNAME) \
|
|
|
|
bool Pre(const parser::CLASSNAME &) { \
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::ATTRNAME); \
|
2018-03-30 13:57:23 -07:00
|
|
|
return false; \
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Elemental, ELEMENTAL)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Impure, IMPURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Module, MODULE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Non_Recursive, NON_RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Pure, PURE)
|
|
|
|
HANDLE_ATTR_CLASS(PrefixSpec::Recursive, RECURSIVE)
|
|
|
|
HANDLE_ATTR_CLASS(TypeAttrSpec::BindC, BIND_C)
|
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(Pass, PASS)
|
|
|
|
HANDLE_ATTR_CLASS(Pointer, POINTER)
|
|
|
|
HANDLE_ATTR_CLASS(Protected, PROTECTED)
|
|
|
|
HANDLE_ATTR_CLASS(Save, SAVE)
|
|
|
|
HANDLE_ATTR_CLASS(Target, TARGET)
|
|
|
|
HANDLE_ATTR_CLASS(Value, VALUE)
|
|
|
|
HANDLE_ATTR_CLASS(Volatile, VOLATILE)
|
|
|
|
#undef HANDLE_ATTR_CLASS
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-03-30 19:49:00 -07:00
|
|
|
std::optional<Attrs> attrs_;
|
2018-03-22 17:08:20 -07:00
|
|
|
std::string langBindingName_{""};
|
2018-04-24 17:05:58 -07:00
|
|
|
|
|
|
|
Attr AccessSpecToAttr(const parser::AccessSpec &x) {
|
|
|
|
switch (x.v) {
|
|
|
|
case parser::AccessSpec::Kind::Public: return Attr::PUBLIC;
|
|
|
|
case parser::AccessSpec::Kind::Private: return Attr::PRIVATE;
|
|
|
|
}
|
2018-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
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find and create types from declaration-type-spec nodes.
|
|
|
|
class DeclTypeSpecVisitor : public AttrsVisitor {
|
|
|
|
public:
|
|
|
|
using AttrsVisitor::Post;
|
2018-03-30 13:57:23 -07:00
|
|
|
using AttrsVisitor::Pre;
|
|
|
|
bool Pre(const parser::IntegerTypeSpec &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Logical &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Real &);
|
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::Complex &);
|
2018-05-17 13:06:38 -07:00
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::DoublePrecision &);
|
2018-09-10 12:20:42 -07:00
|
|
|
bool Pre(const parser::IntrinsicTypeSpec::DoubleComplex &);
|
|
|
|
void Post(const parser::IntrinsicTypeSpec::Character &);
|
2018-03-30 13:57:23 -07:00
|
|
|
bool Pre(const parser::DeclarationTypeSpec::ClassStar &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::TypeStar &);
|
|
|
|
bool Pre(const parser::DeclarationTypeSpec::Record &);
|
|
|
|
void Post(const parser::TypeParamSpec &);
|
2018-09-10 12:20:42 -07:00
|
|
|
void Post(const parser::TypeParamValue &);
|
2018-05-17 13:06:38 -07:00
|
|
|
void Post(const parser::StructureConstructor &);
|
|
|
|
bool Pre(const parser::AllocateStmt &);
|
|
|
|
void Post(const parser::AllocateStmt &);
|
|
|
|
bool Pre(const parser::TypeGuardStmt &);
|
|
|
|
void Post(const parser::TypeGuardStmt &);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
protected:
|
2018-06-05 12:18:35 -07:00
|
|
|
std::unique_ptr<DeclTypeSpec> &GetDeclTypeSpec();
|
|
|
|
void BeginDeclTypeSpec();
|
|
|
|
void EndDeclTypeSpec();
|
2018-06-22 08:21:19 -07:00
|
|
|
void BeginDerivedTypeSpec(DerivedTypeSpec &);
|
2018-06-22 14:08:04 -07:00
|
|
|
void SetDerivedDeclTypeSpec(DeclTypeSpec::Category);
|
2018-03-22 17:08:20 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool expectDeclTypeSpec_{false}; // should only see decl-type-spec when true
|
2018-06-05 12:18:35 -07:00
|
|
|
std::unique_ptr<DeclTypeSpec> declTypeSpec_;
|
2018-06-22 08:21:19 -07:00
|
|
|
DerivedTypeSpec *derivedTypeSpec_{nullptr};
|
|
|
|
std::unique_ptr<ParamValue> typeParamValue_;
|
|
|
|
|
2018-09-11 17:33:42 -07:00
|
|
|
void MakeIntrinsic(TypeCategory, int);
|
2018-03-30 13:57:23 -07:00
|
|
|
void SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec);
|
2018-09-11 17:33:42 -07:00
|
|
|
static int GetKindParamValue(const std::optional<parser::KindSelector> &kind);
|
2018-03-22 17:08:20 -07:00
|
|
|
};
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// Track statement source locations and save messages.
|
|
|
|
class MessageHandler {
|
|
|
|
public:
|
|
|
|
using Message = parser::Message;
|
2018-05-03 15:57:56 -07:00
|
|
|
using MessageFixedText = parser::MessageFixedText;
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-09-16 20:34:20 -07:00
|
|
|
parser::Messages &&messages() { return std::move(messages_); }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
template<typename T> bool Pre(const parser::Statement<T> &x) {
|
|
|
|
currStmtSource_ = &x.source;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T> void Post(const parser::Statement<T> &) {
|
|
|
|
currStmtSource_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *currStmtSource() { return currStmtSource_; }
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-05-03 15:57:56 -07:00
|
|
|
// Add a message to the messages to be emitted.
|
2018-05-04 15:40:40 -07:00
|
|
|
Message &Say(Message &&);
|
2018-05-03 15:57:56 -07:00
|
|
|
// Emit a message associated with the current statement source.
|
2018-05-04 15:40:40 -07:00
|
|
|
Message &Say(MessageFixedText &&);
|
2018-05-03 15:57:56 -07:00
|
|
|
// Emit a message about a SourceName or parser::Name
|
2018-05-04 15:40:40 -07:00
|
|
|
Message &Say(const SourceName &, MessageFixedText &&);
|
|
|
|
Message &Say(const parser::Name &, MessageFixedText &&);
|
2018-05-03 15:57:56 -07:00
|
|
|
// Emit a formatted message associated with a source location.
|
2018-05-04 15:40:40 -07:00
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const std::string &);
|
|
|
|
Message &Say(const SourceName &, MessageFixedText &&, const SourceName &,
|
2018-05-03 15:57:56 -07:00
|
|
|
const SourceName &);
|
2018-05-22 16:12:56 -07:00
|
|
|
void SayAlreadyDeclared(const SourceName &, const Symbol &);
|
2018-05-30 14:49:40 -07:00
|
|
|
// Emit a message and attached message with two names and locations.
|
|
|
|
void Say2(const SourceName &, MessageFixedText &&, const SourceName &,
|
|
|
|
MessageFixedText &&);
|
2018-09-20 14:08:59 -07:00
|
|
|
// As above, but first message has a second argument.
|
|
|
|
void Say2(const SourceName &, MessageFixedText &&, const SourceName &,
|
|
|
|
const SourceName &, MessageFixedText &&);
|
2018-08-08 11:29:05 -07:00
|
|
|
void Annex(parser::Messages &&);
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Where messages are emitted:
|
2018-09-16 20:34:20 -07:00
|
|
|
parser::Messages messages_;
|
2018-04-04 09:03:51 -07:00
|
|
|
// Source location of current statement; null if not in a statement
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *currStmtSource_{nullptr};
|
2018-04-04 09:03:51 -07:00
|
|
|
};
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
|
2018-09-14 15:04:50 -07:00
|
|
|
class ImplicitRulesVisitor : public DeclTypeSpecVisitor, public MessageHandler {
|
2018-03-22 17:08:20 -07:00
|
|
|
public:
|
|
|
|
using DeclTypeSpecVisitor::Post;
|
|
|
|
using DeclTypeSpecVisitor::Pre;
|
2018-04-04 09:03:51 -07:00
|
|
|
using MessageHandler::Post;
|
|
|
|
using MessageHandler::Pre;
|
|
|
|
using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
|
|
|
|
|
|
|
|
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-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// implicit rules in effect for current scope
|
2018-08-27 11:48:49 -07:00
|
|
|
std::unique_ptr<ImplicitRules> implicitRules_{
|
|
|
|
std::make_unique<ImplicitRules>(*this)};
|
2018-04-25 19:58:42 -07:00
|
|
|
const SourceName *prevImplicit_{nullptr};
|
|
|
|
const SourceName *prevImplicitNone_{nullptr};
|
|
|
|
const SourceName *prevImplicitNoneType_{nullptr};
|
|
|
|
const SourceName *prevParameterStmt_{nullptr};
|
2018-04-04 09:03:51 -07:00
|
|
|
|
|
|
|
bool HandleImplicitNone(const std::list<ImplicitNoneNameSpec> &nameSpecs);
|
|
|
|
};
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-04-12 12:59:42 -07:00
|
|
|
// Track array specifications. They can occur in AttrSpec, EntityDecl,
|
|
|
|
// ObjectDecl, DimensionStmt, CommonBlockObject, or BasedPointerStmt.
|
|
|
|
// 1. INTEGER, DIMENSION(10) :: x
|
|
|
|
// 2. INTEGER :: x(10)
|
|
|
|
// 3. ALLOCATABLE :: x(:)
|
|
|
|
// 4. DIMENSION :: x(10)
|
|
|
|
// 5. TODO: COMMON x(10)
|
|
|
|
// 6. TODO: BasedPointerStmt
|
|
|
|
class ArraySpecVisitor {
|
|
|
|
public:
|
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-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::DeferredShapeSpecList &);
|
|
|
|
bool Pre(const parser::AssumedShapeSpec &);
|
|
|
|
bool Pre(const parser::ExplicitShapeSpec &);
|
|
|
|
bool Pre(const parser::AssumedImpliedSpec &);
|
|
|
|
bool Pre(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-03-22 17:08:20 -07:00
|
|
|
|
2018-05-22 16:12:56 -07:00
|
|
|
Symbol *FindSymbol(const SourceName &name);
|
|
|
|
void EraseSymbol(const SourceName &name);
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// Helpers to make a Symbol in the current scope
|
|
|
|
template<typename D>
|
2018-07-28 14:10:34 -07:00
|
|
|
Symbol &MakeSymbol(
|
|
|
|
const SourceName &name, const Attrs &attrs, const 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-08-22 16:56:57 -07:00
|
|
|
const auto &it{currScope().find(name)};
|
|
|
|
if (it == currScope().end()) {
|
|
|
|
const auto pair{currScope().try_emplace(name, attrs, details)};
|
2018-04-25 19:58:42 -07:00
|
|
|
CHECK(pair.second); // name was not found, so must be able to add
|
2018-09-20 14:08:59 -07:00
|
|
|
auto &symbol{*pair.first->second};
|
|
|
|
symbol.add_occurrence(name);
|
|
|
|
return symbol;
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &symbol{*it->second};
|
2018-06-22 08:21:19 -07:00
|
|
|
symbol.add_occurrence(name);
|
2018-08-28 14:02:53 -07:00
|
|
|
if constexpr (std::is_same_v<DerivedTypeDetails, D>) {
|
2018-07-17 07:02:30 -07: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-08-22 16:56:57 -07:00
|
|
|
derivedType = &currScope().MakeSymbol(name, attrs, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (symbol.CanReplaceDetails(details)) {
|
2018-04-25 19:58:42 -07:00
|
|
|
// update the existing symbol
|
2018-06-22 08:21:19 -07:00
|
|
|
symbol.attrs() |= attrs;
|
|
|
|
symbol.set_details(details);
|
|
|
|
return symbol;
|
2018-08-28 14:02:53 -07:00
|
|
|
} else if constexpr (std::is_same_v<UnknownDetails, D>) {
|
2018-06-22 08:21:19 -07:00
|
|
|
symbol.attrs() |= attrs;
|
|
|
|
return symbol;
|
2018-04-25 19:58:42 -07:00
|
|
|
} else {
|
2018-06-22 08:21:19 -07:00
|
|
|
SayAlreadyDeclared(name, symbol);
|
2018-04-25 19:58:42 -07:00
|
|
|
// replace the old symbols with a new one with correct details
|
2018-06-22 08:21:19 -07:00
|
|
|
EraseSymbol(symbol.name());
|
2018-04-25 19:58:42 -07:00
|
|
|
return MakeSymbol(name, attrs, details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename D>
|
2018-05-03 15:57:56 -07:00
|
|
|
Symbol &MakeSymbol(
|
2018-07-28 14:10:34 -07:00
|
|
|
const parser::Name &name, const Attrs &attrs, const D &details) {
|
|
|
|
return MakeSymbol(name.source, attrs, details);
|
2018-05-03 15:57:56 -07:00
|
|
|
}
|
|
|
|
template<typename D>
|
2018-07-28 14:10:34 -07:00
|
|
|
Symbol &MakeSymbol(const parser::Name &name, const D &details) {
|
2018-04-25 19:58:42 -07:00
|
|
|
return MakeSymbol(name, Attrs(), details);
|
|
|
|
}
|
2018-07-28 14:10:34 -07:00
|
|
|
template<typename D>
|
|
|
|
Symbol &MakeSymbol(const SourceName &name, const D &details) {
|
2018-06-19 16:06:41 -07:00
|
|
|
return MakeSymbol(name, Attrs(), details);
|
|
|
|
}
|
2018-05-03 15:57:56 -07:00
|
|
|
Symbol &MakeSymbol(const SourceName &name, Attrs attrs = Attrs{}) {
|
2018-07-28 14:10:34 -07:00
|
|
|
return MakeSymbol(name, attrs, UnknownDetails{});
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
|
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-06-22 08:21:19 -07:00
|
|
|
std::optional<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-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-07-25 06:55:11 -07:00
|
|
|
void add_searchDirectory(const std::string &dir) {
|
|
|
|
searchDirectories_.push_back(dir);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Directories to search for .mod files
|
|
|
|
std::vector<std::string> searchDirectories_;
|
|
|
|
|
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.
|
|
|
|
void AddUse(const SourceName &location, const SourceName &localName,
|
2018-05-14 14:26:39 -07:00
|
|
|
const SourceName &useName);
|
2018-08-02 16:21:27 -07:00
|
|
|
Symbol &BeginModule(const SourceName &, bool isSubmodule,
|
|
|
|
const std::optional<parser::ModuleSubprogramPart> &);
|
2018-08-03 11:32:21 -07:00
|
|
|
Scope *FindModule(const SourceName &, 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 &);
|
2018-05-14 13:51:13 -07:00
|
|
|
bool Pre(const parser::TypeBoundGenericStmt &);
|
|
|
|
void Post(const parser::TypeBoundGenericStmt &);
|
2018-05-14 14:26:39 -07:00
|
|
|
bool Pre(const parser::ProcedureStmt &);
|
|
|
|
void Post(const parser::GenericStmt &);
|
2018-05-14 13:51:13 -07:00
|
|
|
|
|
|
|
bool inInterfaceBlock() const { return inInterfaceBlock_; }
|
|
|
|
bool isGeneric() const { return genericSymbol_ != nullptr; }
|
|
|
|
bool isAbstract() const { return isAbstract_; }
|
|
|
|
|
|
|
|
protected:
|
2018-05-22 16:12:56 -07:00
|
|
|
// Add name or symbol to the generic we are currently processing
|
2018-05-14 13:51:13 -07:00
|
|
|
void AddToGeneric(const parser::Name &name, bool expectModuleProc = false);
|
2018-05-22 16:12:56 -07:00
|
|
|
void AddToGeneric(const Symbol &symbol);
|
|
|
|
// Add to generic the symbol for the subprogram with the same name
|
2018-06-22 08:21:19 -07:00
|
|
|
void SetSpecificInGeneric(Symbol &symbol);
|
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
|
|
|
|
Symbol *genericSymbol_{nullptr}; // set when in generic interface block
|
2018-07-20 10:46:11 -07:00
|
|
|
|
|
|
|
void ResolveSpecificsInGeneric(Symbol &generic);
|
2018-05-14 13:51:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class SubprogramVisitor : 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-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-06-05 12:18:35 -07:00
|
|
|
bool BeginSubprogram(const parser::Name &, Symbol::Flag,
|
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-06-22 08:21:19 -07:00
|
|
|
Symbol *GetSpecificFromGeneric(const SourceName &);
|
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 &);
|
|
|
|
bool Pre(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-06-22 08:21:19 -07:00
|
|
|
void Post(const parser::DeclarationTypeSpec::Type &);
|
2018-08-31 16:20:00 -07:00
|
|
|
void Post(const parser::DeclarationTypeSpec::Class &);
|
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-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool BeginDecl();
|
|
|
|
void EndDecl();
|
2018-08-29 11:38:12 -07:00
|
|
|
bool CheckUseError(const SourceName &, 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-09-06 08:01:49 -07:00
|
|
|
// Info about current derived type while walking DerivedTypeStmt
|
|
|
|
struct {
|
|
|
|
const SourceName *extends{nullptr}; // EXTENDS(name)
|
|
|
|
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.
|
|
|
|
const SourceName *interfaceName_{nullptr};
|
2018-05-30 14:11:45 -07:00
|
|
|
|
|
|
|
bool HandleAttributeStmt(Attr, const std::list<parser::Name> &);
|
2018-09-10 12:20:42 -07:00
|
|
|
Symbol &HandleAttributeStmt(Attr, const SourceName &);
|
2018-10-10 16:20:46 -07:00
|
|
|
void DeclareUnknownEntity(const SourceName &, Attrs);
|
2018-09-20 14:08:59 -07:00
|
|
|
void DeclareObjectEntity(const SourceName &, Attrs);
|
2018-09-24 11:43:48 -07:00
|
|
|
void DeclareProcEntity(const SourceName &, Attrs, const ProcInterface &);
|
2018-09-20 14:08:59 -07:00
|
|
|
void SetType(const SourceName &, Symbol &, const DeclTypeSpec &);
|
2018-06-22 08:21:19 -07:00
|
|
|
const Symbol *ResolveDerivedType(const SourceName &);
|
2018-08-31 16:20:00 -07:00
|
|
|
bool CanBeTypeBoundProc(const Symbol &);
|
|
|
|
Symbol *FindExplicitInterface(const SourceName &);
|
2018-09-04 10:28:27 -07:00
|
|
|
Symbol &MakeTypeSymbol(const SourceName &, const Details &);
|
2018-09-20 14:08:59 -07:00
|
|
|
bool OkToAddComponent(const SourceName &, bool isParentComp = false);
|
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-09-20 14:08:59 -07:00
|
|
|
Symbol &DeclareEntity(const SourceName &name, Attrs attrs) {
|
|
|
|
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-09-20 14:08:59 -07:00
|
|
|
Say(name,
|
2018-06-05 12:18:35 -07:00
|
|
|
"'%s' is use-associated from module '%s' and cannot be re-declared"_err_en_US,
|
2018-09-20 14:08:59 -07:00
|
|
|
name, 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,
|
|
|
|
symbol.name(), "Module procedure definition"_en_US);
|
|
|
|
} 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,
|
|
|
|
symbol.name(), "Internal procedure definition"_en_US);
|
|
|
|
} 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-09-25 08:53:53 -07:00
|
|
|
// Check that construct names don't conflict with other names.
|
|
|
|
class ConstructNamesVisitor : public virtual ScopeHandler {
|
|
|
|
public:
|
|
|
|
// 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::BlockStmt &x) { return CheckDef(x.v); }
|
|
|
|
bool Pre(const parser::ChangeTeamStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::CriticalStmt &x) { return CheckDef(x.t); }
|
|
|
|
bool Pre(const parser::LabelDoStmt &x) { return CheckDef(x.t); }
|
|
|
|
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::EndBlockStmt &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-04-25 19:58:42 -07:00
|
|
|
// Walk the parse tree and resolve names to symbols.
|
2018-05-30 14:11:45 -07:00
|
|
|
class ResolveNamesVisitor : public ModuleVisitor,
|
|
|
|
public SubprogramVisitor,
|
2018-09-25 08:53:53 -07:00
|
|
|
public DeclarationVisitor,
|
|
|
|
public ConstructNamesVisitor {
|
2018-04-25 19:58:42 -07:00
|
|
|
public:
|
|
|
|
using ArraySpecVisitor::Post;
|
|
|
|
using ArraySpecVisitor::Pre;
|
2018-09-25 08:53:53 -07:00
|
|
|
using ConstructNamesVisitor::Post;
|
|
|
|
using ConstructNamesVisitor::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-05-04 15:37:56 -07:00
|
|
|
using SubprogramVisitor::Post;
|
|
|
|
using SubprogramVisitor::Pre;
|
2018-04-25 19:58:42 -07:00
|
|
|
|
2018-09-20 14:08:59 -07:00
|
|
|
ResolveNamesVisitor(Scope &rootScope) { PushScope(rootScope); }
|
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::BlockStmt &);
|
|
|
|
bool Pre(const parser::EndBlockStmt &);
|
|
|
|
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::ConcurrentControl &);
|
|
|
|
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-09-22 08:05:46 -07:00
|
|
|
Symbol *ResolveStructureComponent(const parser::StructureComponent &);
|
|
|
|
Symbol *ResolveArrayElement(const parser::ArrayElement &);
|
|
|
|
Symbol *ResolveCoindexedNamedObject(const parser::CoindexedNamedObject &);
|
|
|
|
Symbol *ResolveDataRef(const parser::DataRef &);
|
2018-10-10 16:20:46 -07:00
|
|
|
Symbol *ResolveName(const SourceName &);
|
2018-09-22 08:05:46 -07:00
|
|
|
Symbol *FindComponent(Symbol &, const SourceName &);
|
2018-09-20 14:08:59 -07:00
|
|
|
Symbol *FindComponent(const Scope &, const SourceName &);
|
|
|
|
bool CheckAccessibleComponent(const Symbol &);
|
2018-08-22 16:05:06 -07:00
|
|
|
void CheckImports();
|
|
|
|
void CheckImport(const SourceName &, const SourceName &);
|
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-06-22 08:21:19 -07:00
|
|
|
std::optional<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-08-27 11:48:49 -07:00
|
|
|
} else if (inheritFromParent_) {
|
|
|
|
return parent_->GetType(ch);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'i' && ch <= 'n') {
|
2018-09-11 17:33:42 -07:00
|
|
|
return DeclTypeSpec{IntrinsicTypeSpec{TypeCategory::Integer}};
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (ch >= 'a' && ch <= 'z') {
|
2018-09-11 17:33:42 -07:00
|
|
|
return DeclTypeSpec{IntrinsicTypeSpec{TypeCategory::Real}};
|
2018-06-22 08:21:19 -07:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
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-07-17 07:02:30 -07:00
|
|
|
auto res{map_.emplace(ch, type)};
|
2018-04-05 17:02:31 -07:00
|
|
|
if (!res.second && !isDefault) {
|
2018-05-03 15:57:56 -07:00
|
|
|
messages_.Say(lo,
|
|
|
|
"More than one implicit type specified for '%s'"_err_en_US,
|
|
|
|
std::string(1, ch));
|
2018-04-05 17:02:31 -07:00
|
|
|
}
|
|
|
|
if (ch == *hi) {
|
|
|
|
break;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:02:31 -07:00
|
|
|
// Return the next char after ch in a way that works for ASCII or EBCDIC.
|
|
|
|
// Return '\0' for the char after 'z'.
|
|
|
|
char ImplicitRules::Incr(char ch) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'i': return 'j';
|
|
|
|
case 'r': return 's';
|
|
|
|
case 'z': return '\0';
|
|
|
|
default: return ch + 1;
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &o, const ImplicitRules &implicitRules) {
|
|
|
|
o << "ImplicitRules:\n";
|
2018-04-05 17:02:31 -07:00
|
|
|
for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, ch);
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
ShowImplicitRule(o, implicitRules, '_');
|
|
|
|
ShowImplicitRule(o, implicitRules, '$');
|
|
|
|
ShowImplicitRule(o, implicitRules, '@');
|
2018-04-04 09:03:51 -07:00
|
|
|
return o;
|
|
|
|
}
|
2018-04-06 10:46:30 -07:00
|
|
|
void ShowImplicitRule(
|
|
|
|
std::ostream &o, const ImplicitRules &implicitRules, char ch) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto it{implicitRules.map_.find(ch)};
|
2018-04-06 10:46:30 -07:00
|
|
|
if (it != implicitRules.map_.end()) {
|
|
|
|
o << " " << ch << ": " << it->second << '\n';
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// AttrsVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-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();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
void AttrsVisitor::Post(const parser::LanguageBindingSpec &x) {
|
2018-07-16 16:23:18 -07:00
|
|
|
CHECK(attrs_);
|
2018-04-05 16:49:48 -07:00
|
|
|
attrs_->set(Attr::BIND_C);
|
2018-03-30 13:57:23 -07:00
|
|
|
if (x.v) {
|
|
|
|
// TODO: set langBindingName_ from ScalarDefaultCharConstantExpr
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::AccessSpec &x) {
|
2018-04-24 17:05:58 -07:00
|
|
|
attrs_->set(AccessSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool AttrsVisitor::Pre(const parser::IntentSpec &x) {
|
2018-07-11 17:45:13 -07:00
|
|
|
CHECK(attrs_);
|
|
|
|
attrs_->set(IntentSpecToAttr(x));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
// DeclTypeSpecVisitor implementation
|
2018-04-04 09:03:51 -07:00
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
std::unique_ptr<DeclTypeSpec> &DeclTypeSpecVisitor::GetDeclTypeSpec() {
|
|
|
|
return declTypeSpec_;
|
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::BeginDeclTypeSpec() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(!expectDeclTypeSpec_);
|
2018-06-22 08:21:19 -07:00
|
|
|
CHECK(!derivedTypeSpec_);
|
2018-03-30 13:57:23 -07:00
|
|
|
expectDeclTypeSpec_ = true;
|
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
void DeclTypeSpecVisitor::EndDeclTypeSpec() {
|
2018-03-30 13:57:23 -07:00
|
|
|
CHECK(expectDeclTypeSpec_);
|
|
|
|
expectDeclTypeSpec_ = false;
|
|
|
|
declTypeSpec_.reset();
|
2018-06-22 08:21:19 -07:00
|
|
|
derivedTypeSpec_ = nullptr;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::ClassStar &x) {
|
2018-06-14 15:34:57 -07:00
|
|
|
SetDeclTypeSpec(DeclTypeSpec{DeclTypeSpec::ClassStar});
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::TypeStar &x) {
|
2018-06-14 15:34:57 -07:00
|
|
|
SetDeclTypeSpec(DeclTypeSpec{DeclTypeSpec::TypeStar});
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeParamSpec &x) {
|
|
|
|
typeParamValue_.reset();
|
|
|
|
}
|
2018-09-10 12:20:42 -07:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::TypeParamValue &x) {
|
2018-04-04 09:03:51 -07:00
|
|
|
typeParamValue_ = std::make_unique<ParamValue>(std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-06-14 13:43:02 -07:00
|
|
|
// TODO: create IntExpr from ScalarIntExpr
|
2018-06-05 12:18:35 -07:00
|
|
|
[&](const parser::ScalarIntExpr &x) { return Bound{IntExpr{}}; },
|
2018-04-04 09:03:51 -07:00
|
|
|
[&](const parser::Star &x) { return Bound::ASSUMED; },
|
|
|
|
[&](const parser::TypeParamValue::Deferred &x) {
|
|
|
|
return Bound::DEFERRED;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u));
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::DeclarationTypeSpec::Record &x) {
|
|
|
|
// TODO
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-17 13:06:38 -07:00
|
|
|
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::StructureConstructor &) {
|
|
|
|
// TODO: StructureConstructor
|
2018-10-10 16:20:46 -07:00
|
|
|
// TODO: name in derived type spec must be resolved
|
2018-06-22 08:21:19 -07:00
|
|
|
derivedTypeSpec_ = nullptr;
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::AllocateStmt &) {
|
|
|
|
BeginDeclTypeSpec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void DeclTypeSpecVisitor::Post(const parser::AllocateStmt &) {
|
|
|
|
// TODO: AllocateStmt
|
|
|
|
EndDeclTypeSpec();
|
2018-06-22 08:21:19 -07:00
|
|
|
derivedTypeSpec_ = nullptr;
|
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-06-22 08:21:19 -07:00
|
|
|
derivedTypeSpec_ = nullptr;
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:57:23 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntegerTypeSpec &x) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Integer, GetKindParamValue(x.v));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-10 12:20:42 -07:00
|
|
|
void DeclTypeSpecVisitor::Post(const parser::IntrinsicTypeSpec::Character &x) {
|
|
|
|
CHECK(!"TODO: character");
|
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Logical &x) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Logical, GetKindParamValue(x.kind));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Real &x) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Real, GetKindParamValue(x.kind));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclTypeSpecVisitor::Pre(const parser::IntrinsicTypeSpec::Complex &x) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Complex, GetKindParamValue(x.kind));
|
2018-03-30 13:57:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-17 13:06:38 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(
|
|
|
|
const parser::IntrinsicTypeSpec::DoublePrecision &) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Real,
|
|
|
|
2 * IntrinsicTypeSpec::GetDefaultKind(TypeCategory::Real));
|
2018-05-17 13:06:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-10 12:20:42 -07:00
|
|
|
bool DeclTypeSpecVisitor::Pre(
|
|
|
|
const parser::IntrinsicTypeSpec::DoubleComplex &) {
|
2018-09-11 17:33:42 -07:00
|
|
|
MakeIntrinsic(TypeCategory::Complex,
|
|
|
|
2 * IntrinsicTypeSpec::GetDefaultKind(TypeCategory::Complex));
|
2018-09-10 12:20:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-11 17:33:42 -07:00
|
|
|
void DeclTypeSpecVisitor::MakeIntrinsic(TypeCategory category, int kind) {
|
|
|
|
SetDeclTypeSpec(DeclTypeSpec{IntrinsicTypeSpec{category, kind}});
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
2018-06-22 14:08:04 -07:00
|
|
|
|
|
|
|
// Set declTypeSpec_ based on derivedTypeSpec_
|
|
|
|
void DeclTypeSpecVisitor::SetDerivedDeclTypeSpec(
|
|
|
|
DeclTypeSpec::Category category) {
|
|
|
|
SetDeclTypeSpec(DeclTypeSpec{category, *derivedTypeSpec_});
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
void DeclTypeSpecVisitor::BeginDerivedTypeSpec(
|
|
|
|
DerivedTypeSpec &derivedTypeSpec) {
|
|
|
|
CHECK(!derivedTypeSpec_);
|
|
|
|
derivedTypeSpec_ = &derivedTypeSpec;
|
|
|
|
}
|
2018-03-30 13:57:23 -07:00
|
|
|
// Check that we're expecting to see a DeclTypeSpec (and haven't seen one yet)
|
|
|
|
// and save it in declTypeSpec_.
|
|
|
|
void DeclTypeSpecVisitor::SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec) {
|
2018-04-04 09:03:51 -07:00
|
|
|
CHECK(expectDeclTypeSpec_);
|
|
|
|
CHECK(!declTypeSpec_);
|
2018-03-30 13:57:23 -07:00
|
|
|
declTypeSpec_ = std::make_unique<DeclTypeSpec>(declTypeSpec);
|
|
|
|
}
|
|
|
|
|
2018-09-11 17:33:42 -07:00
|
|
|
int DeclTypeSpecVisitor::GetKindParamValue(
|
2018-03-30 13:57:23 -07:00
|
|
|
const std::optional<parser::KindSelector> &kind) {
|
2018-05-17 13:06:38 -07:00
|
|
|
if (kind) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *intExpr{std::get_if<parser::ScalarIntConstantExpr>(&kind->u)}) {
|
2018-05-17 14:16:15 -07:00
|
|
|
const parser::Expr &expr{*intExpr->thing.thing.thing};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *lit{std::get_if<parser::LiteralConstant>(&expr.u)}) {
|
|
|
|
if (auto *intLit{std::get_if<parser::IntLiteralConstant>(&lit->u)}) {
|
2018-09-11 17:33:42 -07:00
|
|
|
return std::get<std::uint64_t>(intLit->t);
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(!"TODO: constant evaluation");
|
|
|
|
} else {
|
|
|
|
CHECK(!"TODO: translate star-size to kind");
|
|
|
|
}
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-09-11 17:33:42 -07:00
|
|
|
return 0;
|
2018-03-30 13:57:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:03:51 -07:00
|
|
|
// MessageHandler implementation
|
|
|
|
|
2018-05-04 15:40:40 -07:00
|
|
|
MessageHandler::Message &MessageHandler::Say(MessageFixedText &&msg) {
|
2018-04-04 09:03:51 -07:00
|
|
|
CHECK(currStmtSource_);
|
2018-09-16 20:34:20 -07:00
|
|
|
return messages_.Say(*currStmtSource_, std::move(msg));
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-05-04 15:40:40 -07:00
|
|
|
MessageHandler::Message &MessageHandler::Say(
|
|
|
|
const SourceName &name, MessageFixedText &&msg) {
|
|
|
|
return Say(name, std::move(msg), name.ToString());
|
2018-04-04 09:03:51 -07:00
|
|
|
}
|
2018-05-04 15:40:40 -07:00
|
|
|
MessageHandler::Message &MessageHandler::Say(
|
|
|
|
const parser::Name &name, MessageFixedText &&msg) {
|
2018-09-16 20:34:20 -07:00
|
|
|
return messages_.Say(name.source, std::move(msg), name.ToString().c_str());
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-05-04 15:40:40 -07:00
|
|
|
MessageHandler::Message &MessageHandler::Say(const SourceName &location,
|
|
|
|
MessageFixedText &&msg, const std::string &arg1) {
|
2018-09-16 20:34:20 -07:00
|
|
|
return messages_.Say(location, std::move(msg), arg1.c_str());
|
2018-05-03 15:57:56 -07:00
|
|
|
}
|
2018-05-04 15:40:40 -07:00
|
|
|
MessageHandler::Message &MessageHandler::Say(const SourceName &location,
|
|
|
|
MessageFixedText &&msg, const SourceName &arg1, const SourceName &arg2) {
|
2018-09-16 20:34:20 -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-05-22 16:12:56 -07:00
|
|
|
void MessageHandler::SayAlreadyDeclared(
|
|
|
|
const SourceName &name, const Symbol &prev) {
|
2018-05-30 14:49:40 -07:00
|
|
|
Say2(name, "'%s' is already declared in this scoping unit"_err_en_US,
|
2018-06-14 13:43:02 -07:00
|
|
|
prev.name(), "Previous declaration of '%s'"_en_US);
|
2018-05-30 14:49:40 -07:00
|
|
|
}
|
|
|
|
void MessageHandler::Say2(const SourceName &name1, MessageFixedText &&msg1,
|
|
|
|
const SourceName &name2, MessageFixedText &&msg2) {
|
2018-08-27 11:48:49 -07:00
|
|
|
Say(name1, std::move(msg1)).Attach(name2, msg2, name2.ToString().c_str());
|
2018-08-08 11:29:05 -07:00
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
void MessageHandler::Say2(const SourceName &name1, MessageFixedText &&msg1,
|
|
|
|
const SourceName &arg2, const SourceName &name2, MessageFixedText &&msg2) {
|
|
|
|
Say(name1, std::move(msg1), name1, arg2)
|
|
|
|
.Attach(name2, msg2, name2.ToString().c_str());
|
|
|
|
}
|
2018-08-08 11:29:05 -07:00
|
|
|
void MessageHandler::Annex(parser::Messages &&msgs) {
|
2018-09-16 20:34:20 -07:00
|
|
|
messages_.Annex(std::move(msgs));
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
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-08-27 11:48:49 -07:00
|
|
|
implicitRules_ = std::make_unique<ImplicitRules>(std::move(implicitRules_));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(const parser::DeferredShapeSpecList &x) {
|
|
|
|
for (int i = 0; i < x.v; ++i) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeDeferred());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(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-09-07 09:48:40 -07:00
|
|
|
return true;
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(const parser::ExplicitShapeSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
const auto &lb{std::get<std::optional<parser::SpecificationExpr>>(x.t)};
|
|
|
|
const auto &ub{GetBound(std::get<parser::SpecificationExpr>(x.t))};
|
2018-05-14 14:26:39 -07:00
|
|
|
arraySpec_.push_back(lb ? ShapeSpec::MakeExplicit(GetBound(*lb), ub)
|
|
|
|
: ShapeSpec::MakeExplicit(ub));
|
2018-09-07 09:48:40 -07:00
|
|
|
return true;
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(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());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArraySpecVisitor::Pre(const parser::AssumedRankSpec &) {
|
|
|
|
arraySpec_.push_back(ShapeSpec::MakeAssumedRank());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-06-05 12:18:35 -07:00
|
|
|
return Bound(IntExpr{}); // TODO: convert x.v to IntExpr
|
2018-05-14 14:26:39 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:58:42 -07:00
|
|
|
// ScopeHandler implementation
|
|
|
|
|
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-08-27 11:48:49 -07:00
|
|
|
if (currScope_->kind() != Scope::Kind::Block) {
|
|
|
|
ImplicitRulesVisitor::PushScope();
|
|
|
|
}
|
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-05-22 16:12:56 -07:00
|
|
|
Symbol *ScopeHandler::FindSymbol(const SourceName &name) {
|
2018-08-27 11:48:49 -07:00
|
|
|
return currScope().FindSymbol(name);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
void ScopeHandler::EraseSymbol(const SourceName &name) {
|
2018-08-22 16:56:57 -07:00
|
|
|
currScope().erase(name);
|
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)) {
|
|
|
|
if (const auto type{GetImplicitType(symbol)}) {
|
|
|
|
symbol.SetType(*type);
|
|
|
|
}
|
2018-04-25 19:58:42 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
std::optional<const DeclTypeSpec> ScopeHandler::GetImplicitType(
|
|
|
|
Symbol &symbol) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &name{symbol.name()};
|
|
|
|
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>()}) {
|
|
|
|
symbol.set_details(ObjectEntityDetails{*details});
|
|
|
|
} 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>()}) {
|
|
|
|
symbol.set_details(ProcEntityDetails{*details});
|
|
|
|
} 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-07-25 06:55:11 -07:00
|
|
|
useModuleScope_ = FindModule(x.moduleName.source);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
const SourceName &moduleName{x.moduleName.source};
|
|
|
|
for (const auto &pair : *useModuleScope_) {
|
2018-06-19 16:06:41 -07:00
|
|
|
const Symbol &symbol{*pair.second};
|
2018-05-14 14:26:39 -07:00
|
|
|
if (symbol.attrs().test(Attr::PUBLIC) &&
|
|
|
|
!symbol.detailsIf<ModuleDetails>()) {
|
|
|
|
const SourceName &name{symbol.name()};
|
|
|
|
if (useNames.count(name) == 0) {
|
|
|
|
AddUse(moduleName, name, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useModuleScope_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleVisitor::AddUse(const parser::Rename::Names &names) {
|
|
|
|
const SourceName &useName{std::get<0>(names.t).source};
|
|
|
|
const SourceName &localName{std::get<1>(names.t).source};
|
|
|
|
AddUse(useName, useName, localName);
|
|
|
|
}
|
|
|
|
void ModuleVisitor::AddUse(const parser::Name &useName) {
|
|
|
|
AddUse(useName.source, useName.source, useName.source);
|
|
|
|
}
|
|
|
|
void ModuleVisitor::AddUse(const SourceName &location,
|
|
|
|
const SourceName &localName, const SourceName &useName) {
|
|
|
|
if (!useModuleScope_) {
|
|
|
|
return; // error occurred finding module
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
auto it{useModuleScope_->find(useName)};
|
2018-05-14 14:26:39 -07:00
|
|
|
if (it == useModuleScope_->end()) {
|
|
|
|
Say(useName, "'%s' not found in module '%s'"_err_en_US, useName,
|
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
Symbol &useSymbol{*it->second};
|
|
|
|
useSymbol.add_occurrence(useName);
|
2018-05-14 14:26:39 -07:00
|
|
|
if (useSymbol.attrs().test(Attr::PRIVATE)) {
|
|
|
|
Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, useName,
|
|
|
|
useModuleScope_->name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Symbol &localSymbol{MakeSymbol(localName, useSymbol.attrs())};
|
|
|
|
localSymbol.attrs() &= ~Attrs{Attr::PUBLIC, Attr::PRIVATE};
|
2018-06-05 12:18:35 -07: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>()) {
|
|
|
|
auto name{localName.ToString()};
|
|
|
|
Say(location,
|
|
|
|
"Cannot use-associate '%s'; it is already declared in this scope"_err_en_US,
|
|
|
|
name.c_str())
|
|
|
|
.Attach(localSymbol.name(), "Previous declaration of '%s'"_en_US,
|
|
|
|
name.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)};
|
|
|
|
auto &name{std::get<parser::Name>(stmt.statement.t).source};
|
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
|
|
|
auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
|
|
|
|
auto &ancestorName{std::get<parser::Name>(parentId.t).source};
|
|
|
|
auto &parentName{std::get<std::optional<parser::Name>>(parentId.t)};
|
|
|
|
Scope *ancestor{FindModule(ancestorName)};
|
|
|
|
if (!ancestor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Scope *parentScope{
|
|
|
|
parentName ? FindModule(parentName->source, ancestor) : ancestor};
|
|
|
|
if (!parentScope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PushScope(*parentScope); // submodule is hosted in parent
|
|
|
|
auto &symbol{BeginModule(name, true, subpPart)};
|
2018-08-23 11:24:12 -07:00
|
|
|
if (!ancestor->AddSubmodule(name, currScope())) {
|
2018-08-02 16:21:27 -07:00
|
|
|
Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
|
|
|
|
ancestorName, name);
|
|
|
|
}
|
|
|
|
MakeSymbol(name, symbol.get<ModuleDetails>());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ModuleVisitor::Post(const parser::Submodule &) {
|
|
|
|
PopScope(); // submodule's scope
|
|
|
|
PopScope(); // parent's scope
|
|
|
|
}
|
|
|
|
|
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-08-02 16:21:27 -07:00
|
|
|
std::get<parser::Statement<parser::ModuleStmt>>(x.t).statement.v.source};
|
|
|
|
auto &subpPart{std::get<std::optional<parser::ModuleSubprogramPart>>(x.t)};
|
|
|
|
auto &symbol{BeginModule(name, false, subpPart)};
|
|
|
|
MakeSymbol(name, symbol.details());
|
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-08-02 16:21:27 -07:00
|
|
|
Symbol &ModuleVisitor::BeginModule(const SourceName &name, bool isSubmodule,
|
|
|
|
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-08-02 16:21:27 -07:00
|
|
|
if (subpPart) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Module;
|
|
|
|
parser::Walk(*subpPart, *static_cast<ResolveNamesVisitor *>(this));
|
|
|
|
subpNamesOnly_ = std::nullopt;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
Scope *ModuleVisitor::FindModule(const SourceName &name, Scope *ancestor) {
|
|
|
|
ModFileReader reader{searchDirectories_};
|
2018-09-14 15:04:50 -07:00
|
|
|
auto *scope{reader.Read(GlobalScope(), name, ancestor)};
|
2018-08-02 16:21:27 -07:00
|
|
|
if (!scope) {
|
2018-08-08 11:29:05 -07:00
|
|
|
Annex(std::move(reader.errors()));
|
2018-08-02 16:21:27 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (scope->kind() != Scope::Kind::Module) {
|
|
|
|
Say(name, "'%s' is not a module"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
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-05-22 16:12:56 -07:00
|
|
|
if (genericSymbol_) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *proc{
|
|
|
|
genericSymbol_->get<GenericDetails>().CheckSpecific()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
SayAlreadyDeclared(genericSymbol_->name(), *proc);
|
|
|
|
}
|
|
|
|
genericSymbol_ = nullptr;
|
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
inInterfaceBlock_ = false;
|
|
|
|
isAbstract_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symbol for the generic in genericSymbol_
|
|
|
|
bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
|
|
|
|
const SourceName *genericName{nullptr};
|
|
|
|
GenericSpec genericSpec{MapGenericSpec(x)};
|
|
|
|
switch (genericSpec.kind()) {
|
|
|
|
case GenericSpec::Kind::GENERIC_NAME:
|
|
|
|
genericName = &genericSpec.genericName();
|
|
|
|
break;
|
|
|
|
case GenericSpec::Kind::OP_DEFINED:
|
|
|
|
genericName = &genericSpec.definedOp();
|
|
|
|
break;
|
2018-05-17 13:06:38 -07:00
|
|
|
default: CHECK(!"TODO: intrinsic ops");
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-05-22 16:12:56 -07:00
|
|
|
genericSymbol_ = FindSymbol(*genericName);
|
|
|
|
if (genericSymbol_) {
|
2018-06-22 08:21:19 -07:00
|
|
|
if (genericSymbol_->has<DerivedTypeDetails>()) {
|
|
|
|
// A generic and derived type with same name: create a generic symbol
|
|
|
|
// and save derived type in it.
|
|
|
|
CHECK(genericSymbol_->scope()->symbol() == genericSymbol_);
|
|
|
|
GenericDetails details;
|
|
|
|
details.set_derivedType(*genericSymbol_);
|
|
|
|
EraseSymbol(*genericName);
|
|
|
|
genericSymbol_ = &MakeSymbol(*genericName);
|
2018-07-28 14:10:34 -07:00
|
|
|
genericSymbol_->set_details(details);
|
2018-06-22 08:21:19 -07:00
|
|
|
} else if (!genericSymbol_->isSubprogram()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
SayAlreadyDeclared(*genericName, *genericSymbol_);
|
|
|
|
EraseSymbol(*genericName);
|
|
|
|
genericSymbol_ = nullptr;
|
|
|
|
} else if (genericSymbol_->has<UseDetails>()) {
|
|
|
|
// copy the USEd symbol into this scope so we can modify it
|
|
|
|
const Symbol &ultimate{genericSymbol_->GetUltimate()};
|
|
|
|
EraseSymbol(*genericName);
|
|
|
|
genericSymbol_ = &MakeSymbol(ultimate.name(), ultimate.attrs());
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *details{ultimate.detailsIf<GenericDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
genericSymbol_->set_details(GenericDetails{details->specificProcs()});
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (const auto *details{ultimate.detailsIf<SubprogramDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
genericSymbol_->set_details(SubprogramDetails{*details});
|
|
|
|
} else {
|
|
|
|
CHECK(!"can't happen");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!genericSymbol_) {
|
|
|
|
genericSymbol_ = &MakeSymbol(*genericName);
|
|
|
|
genericSymbol_->set_details(GenericDetails{});
|
|
|
|
}
|
|
|
|
if (genericSymbol_->has<GenericDetails>()) {
|
|
|
|
// okay
|
2018-06-14 13:43:02 -07:00
|
|
|
} else if (genericSymbol_->has<SubprogramDetails>() ||
|
|
|
|
genericSymbol_->has<SubprogramNameDetails>()) {
|
2018-05-22 16:12:56 -07:00
|
|
|
Details details;
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *d{genericSymbol_->detailsIf<SubprogramNameDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
details = *d;
|
2018-07-17 07:02:30 -07:00
|
|
|
} else if (auto *d{genericSymbol_->detailsIf<SubprogramDetails>()}) {
|
2018-05-22 16:12:56 -07:00
|
|
|
details = *d;
|
|
|
|
} else {
|
|
|
|
CHECK(!"can't happen");
|
|
|
|
}
|
2018-06-19 16:06:41 -07:00
|
|
|
GenericDetails genericDetails;
|
2018-06-22 08:21:19 -07:00
|
|
|
genericDetails.set_specific(*genericSymbol_);
|
2018-05-22 16:12:56 -07:00
|
|
|
EraseSymbol(*genericName);
|
2018-06-19 16:06:41 -07:00
|
|
|
genericSymbol_ = &MakeSymbol(*genericName, genericDetails);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
CHECK(genericSymbol_->has<GenericDetails>());
|
2018-05-14 13:51:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InterfaceVisitor::Pre(const parser::TypeBoundGenericStmt &) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void InterfaceVisitor::Post(const parser::TypeBoundGenericStmt &) {
|
2018-05-22 16:12:56 -07:00
|
|
|
// TODO: TypeBoundGenericStmt
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
bool expectModuleProc = std::get<parser::ProcedureStmt::Kind>(x.t) ==
|
|
|
|
parser::ProcedureStmt::Kind::ModuleProcedure;
|
|
|
|
for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
AddToGeneric(name, expectModuleProc);
|
|
|
|
}
|
|
|
|
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-06-05 12:18:35 -07:00
|
|
|
genericSymbol_->attrs().set(AccessSpecToAttr(*accessSpec));
|
2018-05-17 13:06:38 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
|
|
|
|
AddToGeneric(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceVisitor::AddToGeneric(
|
|
|
|
const parser::Name &name, bool expectModuleProc) {
|
2018-07-20 10:46:11 -07:00
|
|
|
genericSymbol_->get<GenericDetails>().add_specificProcName(
|
2018-07-25 06:55:11 -07:00
|
|
|
name.source, expectModuleProc);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
|
|
|
void InterfaceVisitor::AddToGeneric(const Symbol &symbol) {
|
2018-07-16 16:23:18 -07:00
|
|
|
genericSymbol_->get<GenericDetails>().add_specificProc(&symbol);
|
2018-05-14 13:51:13 -07:00
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
void InterfaceVisitor::SetSpecificInGeneric(Symbol &symbol) {
|
2018-07-16 16:23:18 -07:00
|
|
|
genericSymbol_->get<GenericDetails>().set_specific(symbol);
|
2018-05-22 16:12:56 -07:00
|
|
|
}
|
2018-05-14 13:51:13 -07:00
|
|
|
|
2018-07-20 10:46:11 -07:00
|
|
|
// By now we should have seen all specific procedures referenced by name in
|
|
|
|
// this generic interface. Resolve those names to symbols.
|
|
|
|
void InterfaceVisitor::ResolveSpecificsInGeneric(Symbol &generic) {
|
|
|
|
auto &details{generic.get<GenericDetails>()};
|
|
|
|
std::set<SourceName> namesSeen; // to check for duplicate names
|
|
|
|
for (const auto *symbol : details.specificProcs()) {
|
|
|
|
namesSeen.insert(symbol->name());
|
|
|
|
}
|
|
|
|
for (auto &pair : details.specificProcNames()) {
|
|
|
|
const auto &name{*pair.first};
|
|
|
|
auto expectModuleProc{pair.second};
|
|
|
|
const auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name, "Procedure '%s' not found"_err_en_US);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (symbol == &generic) {
|
|
|
|
if (auto *specific{generic.get<GenericDetails>().specific()}) {
|
|
|
|
symbol = specific;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol->has<SubprogramDetails>() &&
|
|
|
|
!symbol->has<SubprogramNameDetails>()) {
|
|
|
|
Say(name, "'%s' is not a subprogram"_err_en_US);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (expectModuleProc) {
|
|
|
|
const auto *d{symbol->detailsIf<SubprogramNameDetails>()};
|
|
|
|
if (!d || d->kind() != SubprogramKind::Module) {
|
|
|
|
Say(name, "'%s' is not a module procedure"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!namesSeen.insert(name).second) {
|
|
|
|
Say(name, "Procedure '%s' is already specified in generic '%s'"_err_en_US,
|
|
|
|
name, generic.name());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
details.add_specificProc(symbol);
|
|
|
|
}
|
|
|
|
details.ClearSpecificProcNames();
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
Say2(generic.name(),
|
|
|
|
"Generic interface '%s' may only contain functions due to derived type"
|
|
|
|
" with same name"_err_en_US,
|
|
|
|
details.derivedType()->name(), "Derived type '%s'"_en_US);
|
|
|
|
}
|
|
|
|
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-05-04 15:37:56 -07:00
|
|
|
std::optional<SourceName> occurrence;
|
|
|
|
std::optional<DeclTypeSpec> resultType;
|
|
|
|
// Look up name: provides return type or tells us if it's an array
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *symbol{FindSymbol(name.source)}) {
|
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();
|
|
|
|
occurrence = symbol->name();
|
|
|
|
EraseSymbol(symbol->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)};
|
2018-05-04 15:37:56 -07:00
|
|
|
if (occurrence) {
|
|
|
|
symbol.add_occurrence(*occurrence);
|
|
|
|
}
|
2018-07-17 07:02:30 -07:00
|
|
|
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-08-22 16:56:57 -07:00
|
|
|
auto it{currScope().parent().find(dummyName.source)};
|
|
|
|
if (it != currScope().parent().end()) {
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *d{it->second->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-05-22 16:12:56 -07:00
|
|
|
EraseSymbol(name.source); // 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-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::Pre(const parser::SubroutineSubprogram &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)};
|
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-06-05 12:18:35 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Subroutine, 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-07-17 07:02:30 -07:00
|
|
|
const auto &name{std::get<parser::Name>(
|
|
|
|
std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement.t)};
|
|
|
|
const auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)};
|
2018-06-05 12:18:35 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Function, 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-06-05 12:18:35 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Subroutine, 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-06-05 12:18:35 -07:00
|
|
|
return BeginSubprogram(name, Symbol::Flag::Function, 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-08-22 16:56:57 -07:00
|
|
|
Symbol &symbol{*currScope().symbol()};
|
2018-05-14 13:51:13 -07:00
|
|
|
CHECK(name.source == symbol.name());
|
2018-07-11 17:45:13 -07:00
|
|
|
symbol.attrs() |= EndAttrs();
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &details{symbol.get<SubprogramDetails>()};
|
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-08-22 16:56:57 -07:00
|
|
|
Symbol &symbol{*currScope().symbol()};
|
2018-05-14 13:51:13 -07:00
|
|
|
CHECK(name.source == symbol.name());
|
2018-07-11 17:45:13 -07:00
|
|
|
symbol.attrs() |= EndAttrs();
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &details{symbol.get<SubprogramDetails>()};
|
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-07-17 07:02:30 -07: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-05-22 16:12:56 -07:00
|
|
|
EraseSymbol(name.source); // 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-05-14 13:51:13 -07:00
|
|
|
bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol::Flag subpFlag,
|
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-06-05 12:18:35 -07:00
|
|
|
PushSubprogramScope(name, subpFlag);
|
2018-05-14 13:51:13 -07:00
|
|
|
if (subpPart) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Internal;
|
|
|
|
parser::Walk(*subpPart, *static_cast<ResolveNamesVisitor *>(this));
|
|
|
|
subpNamesOnly_ = std::nullopt;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void SubprogramVisitor::EndSubprogram() {
|
|
|
|
if (!subpNamesOnly_) {
|
|
|
|
PopScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
Symbol &SubprogramVisitor::PushSubprogramScope(
|
|
|
|
const parser::Name &name, Symbol::Flag subpFlag) {
|
2018-06-22 08:21:19 -07:00
|
|
|
Symbol *symbol = GetSpecificFromGeneric(name.source);
|
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-05-22 16:12:56 -07:00
|
|
|
AddToGeneric(*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-05-04 15:37:56 -07:00
|
|
|
// can't reuse this name inside subprogram:
|
2018-07-28 14:10:34 -07:00
|
|
|
MakeSymbol(name, details).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.
|
|
|
|
Symbol *SubprogramVisitor::GetSpecificFromGeneric(const SourceName &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-06-22 08:21:19 -07:00
|
|
|
symbol->remove_occurrence(name);
|
|
|
|
specific =
|
2018-08-22 16:56:57 -07:00
|
|
|
&currScope().MakeSymbol(name, Attrs{}, SubprogramDetails{});
|
2018-06-22 08:21:19 -07:00
|
|
|
SetSpecificInGeneric(*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-08-29 11:38:12 -07:00
|
|
|
bool DeclarationVisitor::CheckUseError(
|
|
|
|
const SourceName &name, const Symbol &symbol) {
|
|
|
|
const auto *details{symbol.detailsIf<UseErrorDetails>()};
|
|
|
|
if (!details) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Message &msg{Say(name, "Reference to '%s' is ambiguous"_err_en_US)};
|
|
|
|
for (const auto &pair : details->occurrences()) {
|
|
|
|
const SourceName &location{*pair.first};
|
|
|
|
const SourceName &moduleName{pair.second->name()};
|
|
|
|
msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
|
|
|
|
name.ToString().data(), moduleName.ToString().data());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-09-20 14:08:59 -07:00
|
|
|
DeclareObjectEntity(name.source, 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-09-20 14:08:59 -07:00
|
|
|
const auto &name{std::get<parser::ObjectName>(x.t).source};
|
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-10-10 16:20:46 -07:00
|
|
|
DeclareUnknownEntity(name, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::PointerDecl &x) {
|
|
|
|
const auto &name{std::get<parser::Name>(x.t).source};
|
|
|
|
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) {
|
|
|
|
HandleAttributeStmt(Attr::BIND_C, name.source);
|
|
|
|
} else {
|
|
|
|
// TODO: name is common block
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool DeclarationVisitor::Pre(const parser::NamedConstantDef &x) {
|
|
|
|
auto &name{std::get<parser::NamedConstant>(x.t).v.source};
|
|
|
|
// TODO: auto &expr{std::get<parser::ConstantExpr>(x.t)};
|
|
|
|
// TODO: old-style parameters: type based on expr
|
|
|
|
auto &symbol{HandleAttributeStmt(Attr::PARAMETER, name)};
|
2018-09-22 08:05:46 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
2018-09-10 12:20:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
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-07-17 07:02:30 -07:00
|
|
|
auto *symbol{FindSymbol(name.source)};
|
2018-07-19 13:28:24 -07:00
|
|
|
if (!ConvertToProcEntity(*symbol)) {
|
2018-06-14 13:43:02 -07:00
|
|
|
Say2(name.source, "EXTERNAL attribute not allowed on '%s'"_err_en_US,
|
2018-06-05 12:18:35 -07:00
|
|
|
symbol->name(), "Declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-09-10 12:20:42 -07:00
|
|
|
HandleAttributeStmt(attr, name.source);
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-10 12:20:42 -07:00
|
|
|
Symbol &DeclarationVisitor::HandleAttributeStmt(
|
|
|
|
Attr attr, const SourceName &name) {
|
|
|
|
const auto pair{currScope().try_emplace(name, Attrs{attr})};
|
|
|
|
Symbol &symbol{*pair.first->second};
|
|
|
|
if (!pair.second) {
|
|
|
|
// 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
|
|
|
|
} else if (symbol.has<UseDetails>()) {
|
|
|
|
Say(*currStmtSource(),
|
|
|
|
"Cannot change %s attribute on use-associated '%s'"_err_en_US,
|
|
|
|
EnumToString(attr), name);
|
|
|
|
}
|
|
|
|
symbol.attrs().set(attr);
|
|
|
|
symbol.add_occurrence(name);
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
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-09-20 14:08:59 -07:00
|
|
|
DeclareObjectEntity(name.source, 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.
|
|
|
|
void DeclarationVisitor::DeclareUnknownEntity(
|
|
|
|
const SourceName &name, Attrs attrs) {
|
|
|
|
if (!arraySpec().empty()) {
|
|
|
|
DeclareObjectEntity(name, attrs);
|
|
|
|
} else {
|
|
|
|
Symbol &symbol{DeclareEntity<EntityDetails>(name, attrs)};
|
|
|
|
if (auto &type{GetDeclTypeSpec()}) {
|
|
|
|
SetType(name, symbol, *type);
|
|
|
|
}
|
|
|
|
if (symbol.attrs().test(Attr::EXTERNAL)) {
|
|
|
|
ConvertToProcEntity(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 11:43:48 -07:00
|
|
|
void DeclarationVisitor::DeclareProcEntity(
|
2018-09-20 14:08:59 -07:00
|
|
|
const SourceName &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);
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
2018-04-11 13:11:42 -07:00
|
|
|
}
|
2018-04-12 12:59:42 -07:00
|
|
|
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::DeclareObjectEntity(
|
2018-09-20 14:08:59 -07:00
|
|
|
const SourceName &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>()}) {
|
|
|
|
if (auto &type{GetDeclTypeSpec()}) {
|
2018-09-20 14:08:59 -07:00
|
|
|
SetType(name, symbol, *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
|
|
|
}
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Type &x) {
|
2018-06-22 14:08:04 -07:00
|
|
|
SetDerivedDeclTypeSpec(DeclTypeSpec::TypeDerived);
|
2018-06-22 08:21:19 -07:00
|
|
|
DerivedTypeSpec &type{GetDeclTypeSpec()->derivedTypeSpec()};
|
2018-07-17 07:02:30 -07:00
|
|
|
if (const auto *symbol{ResolveDerivedType(type.name())}) {
|
2018-06-22 08:21:19 -07:00
|
|
|
type.set_scope(*symbol->scope());
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 16:20:00 -07:00
|
|
|
void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Class &) {
|
|
|
|
SetDerivedDeclTypeSpec(DeclTypeSpec::ClassDerived);
|
|
|
|
DerivedTypeSpec &type{GetDeclTypeSpec()->derivedTypeSpec()};
|
|
|
|
if (const auto *symbol{ResolveDerivedType(type.name())}) {
|
|
|
|
type.set_scope(*symbol->scope());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
bool DeclarationVisitor::Pre(const parser::DerivedTypeSpec &x) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto &name{std::get<parser::Name>(x.t).source};
|
2018-08-22 16:56:57 -07:00
|
|
|
auto &derivedTypeSpec{currScope().MakeDerivedTypeSpec(name)};
|
2018-06-22 08:21:19 -07:00
|
|
|
BeginDerivedTypeSpec(derivedTypeSpec);
|
|
|
|
return true;
|
|
|
|
}
|
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()};
|
|
|
|
auto &stmt{std::get<parser::Statement<parser::DerivedTypeStmt>>(x.t)};
|
|
|
|
for (auto &name : std::get<std::list<parser::Name>>(stmt.statement.t)) {
|
|
|
|
auto ¶mName{name.source};
|
|
|
|
if (auto it{scope.find(paramName)}; it == scope.end()) {
|
|
|
|
Say(paramName,
|
|
|
|
"No definition found for type parameter '%s'"_err_en_US); // C742
|
|
|
|
} else {
|
|
|
|
auto *symbol{it->second};
|
|
|
|
if (!symbol->has<TypeParamDetails>()) {
|
|
|
|
Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
|
|
|
|
symbol->name(),
|
|
|
|
"Definition of '%s'"_en_US); // C741
|
|
|
|
} else {
|
|
|
|
symbol->add_occurrence(paramName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!paramNames.insert(paramName).second) {
|
|
|
|
Say(paramName,
|
|
|
|
"Duplicate type parameter name: '%s'"_err_en_US); // C731
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 08:01:49 -07:00
|
|
|
auto &details{scope.symbol()->get<DerivedTypeDetails>()};
|
|
|
|
details.set_hasTypeParams(!paramNames.empty());
|
2018-09-04 10:28:27 -07:00
|
|
|
for (const auto &pair : currScope()) {
|
|
|
|
const auto *symbol{pair.second};
|
|
|
|
if (symbol->has<TypeParamDetails>() && !paramNames.count(symbol->name())) {
|
|
|
|
Say2(symbol->name(),
|
|
|
|
"'%s' is not a type parameter of this derived type"_err_en_US,
|
|
|
|
stmt.source, "Derived type statement"_en_US); // C742
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
if (details.hasTypeParams()) {
|
|
|
|
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-07-17 07:02:30 -07:00
|
|
|
auto &name{std::get<parser::Name>(x.t).source};
|
|
|
|
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}) {
|
|
|
|
if (auto *extends{ResolveDerivedType(*extendsName)}) {
|
|
|
|
symbol.get<DerivedTypeDetails>().set_extends(extends);
|
|
|
|
// Declare the "parent component"; private if the type is
|
|
|
|
if (OkToAddComponent(*extendsName, true)) {
|
|
|
|
auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
|
|
|
|
comp.attrs().set(Attr::PRIVATE, extends->attrs().test(Attr::PRIVATE));
|
|
|
|
comp.set(Symbol::Flag::ParentComp);
|
|
|
|
auto &derivedTypeSpec{currScope().MakeDerivedTypeSpec(*extendsName)};
|
|
|
|
derivedTypeSpec.set_scope(currScope());
|
|
|
|
comp.SetType(DeclTypeSpec{DeclTypeSpec::TypeDerived, derivedTypeSpec});
|
|
|
|
}
|
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) {
|
|
|
|
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)) {
|
|
|
|
auto &name{std::get<parser::Name>(decl.t).source};
|
|
|
|
// TODO: initialization
|
|
|
|
// auto &init{
|
|
|
|
// std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)};
|
2018-09-05 16:02:41 -07:00
|
|
|
auto &symbol{MakeTypeSymbol(name, TypeParamDetails{attr})};
|
2018-09-04 10:28:27 -07:00
|
|
|
SetType(name, symbol, *type);
|
|
|
|
}
|
|
|
|
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-09-06 08:01:49 -07:00
|
|
|
derivedTypeInfo_.extends = &x.v.source;
|
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-09-20 14:08:59 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t).source};
|
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)) {
|
|
|
|
DeclareObjectEntity(name, attrs);
|
|
|
|
}
|
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-06-05 12:18:35 -07:00
|
|
|
interfaceName_ = &name->source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-17 07:02:30 -07: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-09-22 08:05:46 -07:00
|
|
|
const auto &name{std::get<parser::Name>(x.t).source};
|
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) {
|
|
|
|
auto &bindingName{std::get<parser::Name>(declaration.t).source};
|
|
|
|
auto &optName{std::get<std::optional<parser::Name>>(declaration.t)};
|
|
|
|
auto &procedureName{optName ? optName->source : bindingName};
|
|
|
|
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,
|
|
|
|
procedure->name(), "Declaration of '%s'"_en_US);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MakeTypeSymbol(bindingName, ProcBindingDetails{*procedure});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Symbol *interface{FindExplicitInterface(x.interfaceName.source)};
|
|
|
|
if (!interface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &bindingName : x.bindingNames) {
|
|
|
|
MakeTypeSymbol(bindingName.source, ProcBindingDetails{*interface});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationVisitor::Post(const parser::FinalProcedureStmt &x) {
|
|
|
|
for (auto &name : x.v) {
|
|
|
|
MakeTypeSymbol(name.source, FinalProcDetails{});
|
2018-06-05 12:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:08:59 -07:00
|
|
|
// Set the type of an entity or report an error.
|
2018-06-05 12:18:35 -07:00
|
|
|
void DeclarationVisitor::SetType(
|
|
|
|
const SourceName &name, Symbol &symbol, const DeclTypeSpec &type) {
|
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-09-11 07:26:54 -07:00
|
|
|
Say2(name, "The type of '%s' has already been declared"_err_en_US,
|
|
|
|
symbol.name(), "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,
|
|
|
|
symbol.name(), "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-08-31 16:20:00 -07:00
|
|
|
// Find the Symbol for this derived type.
|
2018-08-29 11:38:12 -07:00
|
|
|
const Symbol *DeclarationVisitor::ResolveDerivedType(const SourceName &name) {
|
|
|
|
const auto *symbol{FindSymbol(name)};
|
|
|
|
if (!symbol) {
|
|
|
|
Say(name, "Derived type '%s' not found"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (CheckUseError(name, *symbol)) {
|
|
|
|
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>()) {
|
|
|
|
Say(name, "'%s' is not a derived type"_err_en_US);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *DeclarationVisitor::FindExplicitInterface(const SourceName &name) {
|
|
|
|
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,
|
|
|
|
symbol->name(), "Declaration of '%s'"_en_US);
|
|
|
|
symbol = nullptr;
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symbol for a type parameter, component, or procedure binding in
|
|
|
|
// the current derived type scope.
|
2018-09-04 10:28:27 -07:00
|
|
|
Symbol &DeclarationVisitor::MakeTypeSymbol(
|
2018-08-31 16:20:00 -07:00
|
|
|
const SourceName &name, const Details &details) {
|
|
|
|
Scope &derivedType{currScope()};
|
|
|
|
CHECK(derivedType.kind() == Scope::Kind::DerivedType);
|
|
|
|
if (auto it{derivedType.find(name)}; it != derivedType.end()) {
|
|
|
|
Say2(name,
|
|
|
|
"Type parameter, component, or procedure binding '%s'"
|
|
|
|
" already defined in this type"_err_en_US,
|
|
|
|
it->second->name(), "Previous definition of '%s'"_en_US);
|
2018-09-04 10:28:27 -07:00
|
|
|
return *it->second;
|
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);
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
const SourceName &name, bool isParentComp) {
|
|
|
|
const Scope *scope{&currScope()};
|
|
|
|
for (bool inParent{false};; inParent = true) {
|
|
|
|
CHECK(scope->kind() == Scope::Kind::DerivedType);
|
|
|
|
auto it{scope->find(name)};
|
|
|
|
if (it != scope->end()) {
|
|
|
|
Symbol &prev{*it->second};
|
|
|
|
parser::MessageFixedText msg{""_en_US};
|
|
|
|
if (isParentComp) {
|
|
|
|
msg = "Type cannot be extended as it has a component named"
|
|
|
|
" '%s'"_err_en_US;
|
|
|
|
} else if (prev.test(Symbol::Flag::ParentComp)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
Say2(name, std::move(msg), prev.name(),
|
|
|
|
"Previous declaration of '%s'"_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto *extends{scope->symbol()->get<DerivedTypeDetails>().extends()};
|
|
|
|
if (!extends) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
scope = extends->scope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 08:53:53 -07:00
|
|
|
// ConstructNamesVisitor implementation
|
|
|
|
|
|
|
|
bool ConstructNamesVisitor::CheckDef(const std::optional<parser::Name> &x) {
|
|
|
|
if (x) {
|
|
|
|
MakeSymbol(*x, MiscDetails{MiscDetails::Kind::ConstructName});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstructNamesVisitor::CheckRef(const std::optional<parser::Name> &x) {
|
|
|
|
if (x) {
|
|
|
|
// Just add an occurrence of this name; checking is done in ValidateLabels
|
|
|
|
FindSymbol(x->source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (!scope.add_importName(name.source)) {
|
|
|
|
Say(name, "'%s' not found in host scope"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevImportStmt_ = currStmtSource();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 08:05:46 -07:00
|
|
|
Symbol *ResolveNamesVisitor::ResolveStructureComponent(
|
2018-06-22 08:21:19 -07:00
|
|
|
const parser::StructureComponent &x) {
|
2018-09-22 08:05:46 -07:00
|
|
|
Symbol *dataRef{ResolveDataRef(x.base)};
|
2018-06-22 08:21:19 -07:00
|
|
|
return dataRef ? FindComponent(*dataRef, x.component.source) : nullptr;
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
Symbol *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-09-22 08:05:46 -07:00
|
|
|
Symbol *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-09-22 08:05:46 -07:00
|
|
|
Symbol *ResolveNamesVisitor::ResolveDataRef(const parser::DataRef &x) {
|
2018-06-22 08:21:19 -07:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
2018-10-10 16:20:46 -07:00
|
|
|
[=](const parser::Name &y) { return ResolveName(y.source); },
|
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.
|
|
|
|
Symbol *ResolveNamesVisitor::ResolveName(const SourceName &name) {
|
|
|
|
if (auto *symbol{FindSymbol(name)}) {
|
|
|
|
if (CheckUseError(name, *symbol)) {
|
|
|
|
return nullptr; // reported an error
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Create the symbol then ensure it is accessible
|
|
|
|
InclusiveScope().try_emplace(name);
|
|
|
|
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);
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
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-09-22 08:05:46 -07:00
|
|
|
Symbol *ResolveNamesVisitor::FindComponent(
|
|
|
|
Symbol &base, const SourceName &component) {
|
|
|
|
if (!ConvertToObjectEntity(base)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
Say2(base.lastOccurrence(),
|
2018-09-22 08:05:46 -07:00
|
|
|
"'%s' is an invalid base for a component reference"_err_en_US,
|
|
|
|
base.name(), "Declaration of '%s'"_en_US);
|
2018-06-22 08:21:19 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
auto *type{base.GetType()};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (!type) {
|
|
|
|
return nullptr; // should have already reported error
|
|
|
|
}
|
2018-09-24 07:12:38 -07:00
|
|
|
if (type->category() == DeclTypeSpec::Intrinsic &&
|
|
|
|
type->intrinsicTypeSpec().category() == TypeCategory::Complex) {
|
|
|
|
auto name{component.ToString()};
|
|
|
|
if (name == "re" || name == "im") {
|
|
|
|
return nullptr; // complex-part-designator, not structure-component
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
if (type->category() != DeclTypeSpec::TypeDerived) {
|
|
|
|
if (base.test(Symbol::Flag::Implicit)) {
|
2018-09-20 14:08:59 -07:00
|
|
|
Say(base.lastOccurrence(),
|
2018-06-22 08:21:19 -07:00
|
|
|
"'%s' is not an object of derived type; it is implicitly typed"_err_en_US);
|
|
|
|
} else {
|
2018-09-20 14:08:59 -07:00
|
|
|
Say2(base.lastOccurrence(),
|
2018-06-22 08:21:19 -07:00
|
|
|
"'%s' is not an object of derived type"_err_en_US, base.name(),
|
|
|
|
"Declaration of '%s'"_en_US);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-09-22 08:05:46 -07:00
|
|
|
const Scope *scope{type->derivedTypeSpec().scope()};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (!scope) {
|
|
|
|
return nullptr; // previously failed to resolve type
|
2018-09-20 14:08:59 -07:00
|
|
|
} else if (auto *result{FindComponent(*scope, component)}) {
|
|
|
|
result->add_occurrence(component);
|
|
|
|
return CheckAccessibleComponent(*result) ? result : nullptr;
|
|
|
|
} else {
|
|
|
|
auto &typeName{scope->name()};
|
|
|
|
Say2(component, "Component '%s' not found in derived type '%s'"_err_en_US,
|
|
|
|
typeName, typeName, "Declaration of '%s'"_en_US);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that component is accessible from current scope.
|
|
|
|
bool ResolveNamesVisitor::CheckAccessibleComponent(const Symbol &component) {
|
|
|
|
if (!component.attrs().test(Attr::PRIVATE)) {
|
|
|
|
return true;
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
2018-09-20 14:08:59 -07:00
|
|
|
CHECK(component.owner().kind() == Scope::Kind::DerivedType);
|
|
|
|
// component must be in a module/submodule because of PRIVATE:
|
|
|
|
const Scope &moduleScope{component.owner().parent()};
|
|
|
|
CHECK(moduleScope.kind() == Scope::Kind::Module);
|
|
|
|
for (auto *scope{&currScope()}; scope->kind() != Scope::Kind::Global;
|
|
|
|
scope = &scope->parent()) {
|
|
|
|
if (scope == &moduleScope) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Say2(component.lastOccurrence(),
|
|
|
|
"PRIVATE component '%s' is only accessible within module '%s'"_err_en_US,
|
|
|
|
moduleScope.name(), component.name(), "Declaration of '%s'"_en_US);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look in this type's scope and then its parents for component.
|
|
|
|
Symbol *ResolveNamesVisitor::FindComponent(
|
|
|
|
const Scope &type, const SourceName &component) {
|
|
|
|
CHECK(type.kind() == Scope::Kind::DerivedType);
|
|
|
|
auto it{type.find(component)};
|
|
|
|
if (it != type.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
auto &details{type.symbol()->get<DerivedTypeDetails>()};
|
|
|
|
if (auto *extends{details.extends()}) {
|
|
|
|
return FindComponent(*extends->scope(), component);
|
|
|
|
} else {
|
2018-06-22 08:21:19 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-09 15:25:49 -07:00
|
|
|
auto *symbol{FindSymbol(name->source)};
|
|
|
|
if (symbol == nullptr) {
|
|
|
|
symbol = &MakeSymbol(name->source);
|
|
|
|
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-07-09 15:25:49 -07:00
|
|
|
symbol->attrs().set(Attr::EXTERNAL);
|
|
|
|
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-06-05 12:18:35 -07:00
|
|
|
CHECK(expectedProcFlag_);
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->set(*expectedProcFlag_);
|
|
|
|
} else if (symbol->has<UnknownDetails>()) {
|
|
|
|
CHECK(!"unexpected UnknownDetails");
|
|
|
|
} else if (CheckUseError(name->source, *symbol)) {
|
2018-05-30 14:11:45 -07:00
|
|
|
// error was reported
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-07-19 13:28:24 -07:00
|
|
|
symbol = &symbol->GetUltimate();
|
2018-09-22 08:05:46 -07:00
|
|
|
ConvertToProcEntity(*symbol);
|
2018-07-09 15:25:49 -07:00
|
|
|
if (symbol->test(Symbol::Flag::Function) &&
|
2018-06-05 12:18:35 -07:00
|
|
|
expectedProcFlag_ == Symbol::Flag::Subroutine) {
|
|
|
|
Say2(name->source,
|
|
|
|
"Cannot call function '%s' like a subroutine"_err_en_US,
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->name(), "Declaration of '%s'"_en_US);
|
|
|
|
} else if (symbol->test(Symbol::Flag::Subroutine) &&
|
2018-06-05 12:18:35 -07:00
|
|
|
expectedProcFlag_ == Symbol::Flag::Function) {
|
|
|
|
Say2(name->source,
|
|
|
|
"Cannot call subroutine '%s' like a function"_err_en_US,
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->name(), "Declaration of '%s'"_en_US);
|
2018-08-22 16:05:06 -07:00
|
|
|
} else if (symbol->has<ProcEntityDetails>()) {
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->set(*expectedProcFlag_); // in case it hasn't been set yet
|
2018-09-22 08:05:46 -07:00
|
|
|
if (expectedProcFlag_ == Symbol::Flag::Function) {
|
|
|
|
ApplyImplicitRules(*symbol);
|
|
|
|
}
|
2018-08-22 16:05:06 -07:00
|
|
|
} else if (symbol->has<SubprogramDetails>()) {
|
2018-07-05 10:28:34 -07:00
|
|
|
// OK
|
2018-08-22 16:05:06 -07:00
|
|
|
} else if (symbol->has<SubprogramNameDetails>()) {
|
|
|
|
// OK
|
|
|
|
} else if (symbol->has<GenericDetails>()) {
|
|
|
|
// OK
|
|
|
|
} else if (symbol->has<DerivedTypeDetails>()) {
|
2018-07-09 15:25:49 -07:00
|
|
|
// OK: type constructor
|
2018-09-07 09:48:40 -07:00
|
|
|
} else if (auto *details{symbol->detailsIf<ObjectEntityDetails>()};
|
|
|
|
details && details->isArray()) {
|
|
|
|
// OK: array mis-parsed as a call
|
|
|
|
} else if (symbol->test(Symbol::Flag::Implicit)) {
|
|
|
|
Say(name->source,
|
|
|
|
"Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
|
2018-06-05 12:18:35 -07:00
|
|
|
} else {
|
2018-05-30 14:49:40 -07:00
|
|
|
Say2(name->source,
|
|
|
|
"Use of '%s' as a procedure conflicts with its declaration"_err_en_US,
|
2018-07-09 15:25:49 -07:00
|
|
|
symbol->name(), "Declaration of '%s'"_en_US);
|
2018-05-30 14:11:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-03 15:57:56 -07:00
|
|
|
Symbol &symbol{MakeSymbol(name.source)};
|
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-05-03 15:57:56 -07:00
|
|
|
Say(name.source,
|
|
|
|
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 &name{pair.first};
|
|
|
|
auto &symbol{*pair.second};
|
2018-07-16 16:23:18 -07:00
|
|
|
if (NeedsExplicitType(symbol)) {
|
|
|
|
if (isImplicitNoneType()) {
|
|
|
|
Say(name, "No explicit type declared for '%s'"_err_en_US);
|
|
|
|
} else {
|
2018-09-22 08:05:46 -07:00
|
|
|
ApplyImplicitRules(symbol);
|
2018-07-16 16:23:18 -07:00
|
|
|
}
|
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-08-22 16:56:57 -07:00
|
|
|
auto &scope{currScope()};
|
2018-08-22 16:05:06 -07:00
|
|
|
auto it{scope.find(name)};
|
|
|
|
if (it != scope.end()) {
|
|
|
|
Say(location, "'%s' from host is not accessible"_err_en_US,
|
|
|
|
name.ToString().c_str())
|
|
|
|
.Attach(it->second->name(), "'%s' is hidden by this entity"_en_US,
|
|
|
|
it->second->name().ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:11:42 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::MainProgram &x) {
|
|
|
|
using stmtType = std::optional<parser::Statement<parser::ProgramStmt>>;
|
2018-08-08 17:28:16 -07:00
|
|
|
if (auto &stmt{std::get<stmtType>(x.t)}) {
|
2018-04-11 13:11:42 -07:00
|
|
|
const parser::Name &name{stmt->statement.v};
|
2018-06-22 08:21:19 -07:00
|
|
|
Symbol &symbol{MakeSymbol(name, MainProgramDetails{})};
|
|
|
|
PushScope(Scope::Kind::MainProgram, &symbol);
|
|
|
|
MakeSymbol(name, MainProgramDetails{});
|
|
|
|
} else {
|
|
|
|
PushScope(Scope::Kind::MainProgram, nullptr);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
2018-08-08 17:28:16 -07:00
|
|
|
if (auto &subpPart{
|
|
|
|
std::get<std::optional<parser::InternalSubprogramPart>>(x.t)}) {
|
|
|
|
subpNamesOnly_ = SubprogramKind::Internal;
|
|
|
|
parser::Walk(*subpPart, *static_cast<ResolveNamesVisitor *>(this));
|
|
|
|
subpNamesOnly_ = std::nullopt;
|
|
|
|
}
|
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-09-25 08:53:53 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::BlockStmt &x) {
|
|
|
|
ConstructNamesVisitor::Pre(x);
|
2018-08-27 11:48:49 -07:00
|
|
|
PushScope(Scope::Kind::Block, nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-25 08:53:53 -07:00
|
|
|
bool ResolveNamesVisitor::Pre(const parser::EndBlockStmt &x) {
|
2018-08-27 11:48:49 -07:00
|
|
|
PopScope();
|
2018-09-25 08:53:53 -07:00
|
|
|
ConstructNamesVisitor::Post(x);
|
2018-08-27 11:48:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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-10-10 16:20:46 -07:00
|
|
|
[&](const parser::Name &x) { ResolveName(x.source); },
|
|
|
|
[&](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{
|
|
|
|
[&](const parser::Name &x) { ResolveName(x.source); },
|
|
|
|
[&](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-10-10 16:20:46 -07:00
|
|
|
[&](const parser::ObjectName &x) { ResolveName(x.source); },
|
|
|
|
[&](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-10-10 16:20:46 -07:00
|
|
|
template<typename T>
|
|
|
|
void ResolveNamesVisitor::Post(const parser::LoopBounds<T> &x) {
|
|
|
|
ResolveName(x.name.thing.thing.source);
|
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);
|
|
|
|
ConstructNamesVisitor::Post(x);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
ResolveName(name.source);
|
2018-04-24 17:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:20:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ResolveNamesVisitor::Post(const parser::ConcurrentControl &x) {
|
|
|
|
ResolveName(std::get<parser::Name>(x.t).source);
|
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-09-14 15:04:50 -07:00
|
|
|
void ResolveNames(parser::Messages &messages, Scope &rootScope,
|
|
|
|
const parser::Program &program,
|
2018-07-25 06:55:11 -07:00
|
|
|
const std::vector<std::string> &searchDirectories) {
|
2018-09-16 20:34:20 -07:00
|
|
|
ResolveNamesVisitor visitor{rootScope};
|
2018-07-25 06:55:11 -07:00
|
|
|
for (auto &dir : searchDirectories) {
|
|
|
|
visitor.add_searchDirectory(dir);
|
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
parser::Walk(program, visitor);
|
2018-09-16 20:34:20 -07:00
|
|
|
messages.Annex(visitor.messages());
|
2018-04-18 15:06:35 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
// Map the enum in the parser to the one in GenericSpec
|
|
|
|
static GenericSpec::Kind MapIntrinsicOperator(
|
|
|
|
parser::DefinedOperator::IntrinsicOperator x) {
|
|
|
|
switch (x) {
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Add:
|
|
|
|
return GenericSpec::OP_ADD;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::AND:
|
|
|
|
return GenericSpec::OP_AND;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Concat:
|
|
|
|
return GenericSpec::OP_CONCAT;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Divide:
|
|
|
|
return GenericSpec::OP_DIVIDE;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::EQ:
|
|
|
|
return GenericSpec::OP_EQ;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::EQV:
|
|
|
|
return GenericSpec::OP_EQV;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::GE:
|
|
|
|
return GenericSpec::OP_GE;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::GT:
|
|
|
|
return GenericSpec::OP_GT;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::LE:
|
|
|
|
return GenericSpec::OP_LE;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::LT:
|
|
|
|
return GenericSpec::OP_LT;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Multiply:
|
|
|
|
return GenericSpec::OP_MULTIPLY;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::NE:
|
|
|
|
return GenericSpec::OP_NE;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::NEQV:
|
|
|
|
return GenericSpec::OP_NEQV;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::NOT:
|
|
|
|
return GenericSpec::OP_NOT;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::OR:
|
|
|
|
return GenericSpec::OP_OR;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Power:
|
|
|
|
return GenericSpec::OP_POWER;
|
|
|
|
case parser::DefinedOperator::IntrinsicOperator::Subtract:
|
|
|
|
return GenericSpec::OP_SUBTRACT;
|
2018-05-17 13:06:38 -07:00
|
|
|
case parser::DefinedOperator::IntrinsicOperator::XOR:
|
|
|
|
return GenericSpec::OP_XOR;
|
2018-05-14 13:51:13 -07:00
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map a parser::GenericSpec to a semantics::GenericSpec
|
|
|
|
static GenericSpec MapGenericSpec(const parser::GenericSpec &genericSpec) {
|
|
|
|
return std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 13:51:13 -07:00
|
|
|
[](const parser::Name &x) {
|
|
|
|
return GenericSpec::GenericName(x.source);
|
|
|
|
},
|
|
|
|
[](const parser::DefinedOperator &x) {
|
|
|
|
return std::visit(
|
2018-06-18 11:03:43 -07:00
|
|
|
common::visitors{
|
2018-05-14 13:51:13 -07:00
|
|
|
[](const parser::DefinedOpName &name) {
|
|
|
|
return GenericSpec::DefinedOp(name.v.source);
|
|
|
|
},
|
|
|
|
[](const parser::DefinedOperator::IntrinsicOperator &x) {
|
|
|
|
return GenericSpec::IntrinsicOp(MapIntrinsicOperator(x));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
x.u);
|
|
|
|
},
|
|
|
|
[](const parser::GenericSpec::Assignment &) {
|
|
|
|
return GenericSpec::IntrinsicOp(GenericSpec::ASSIGNMENT);
|
|
|
|
},
|
|
|
|
[](const parser::GenericSpec::ReadFormatted &) {
|
|
|
|
return GenericSpec::IntrinsicOp(GenericSpec::READ_FORMATTED);
|
|
|
|
},
|
|
|
|
[](const parser::GenericSpec::ReadUnformatted &) {
|
|
|
|
return GenericSpec::IntrinsicOp(GenericSpec::READ_UNFORMATTED);
|
|
|
|
},
|
|
|
|
[](const parser::GenericSpec::WriteFormatted &) {
|
|
|
|
return GenericSpec::IntrinsicOp(GenericSpec::WRITE_FORMATTED);
|
|
|
|
},
|
|
|
|
[](const parser::GenericSpec::WriteUnformatted &) {
|
|
|
|
return GenericSpec::IntrinsicOp(GenericSpec::WRITE_UNFORMATTED);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
genericSpec.u);
|
|
|
|
}
|
|
|
|
|
2018-03-23 14:02:11 -07:00
|
|
|
} // namespace Fortran::semantics
|