2019-01-16 01:59:20 +01:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-09-15 00:04:50 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-03-27 02:03:33 +01:00
|
|
|
#include "semantics.h"
|
2018-12-04 19:55:32 +01:00
|
|
|
#include "assignment.h"
|
2018-09-19 23:26:02 +02:00
|
|
|
#include "canonicalize-do.h"
|
2019-03-26 08:33:03 +01:00
|
|
|
#include "check-arithmeticif.h"
|
2019-03-27 23:44:17 +01:00
|
|
|
#include "check-computed-goto.h"
|
2019-04-11 07:17:44 +02:00
|
|
|
#include "check-deallocate.h"
|
2018-09-18 02:19:27 +02:00
|
|
|
#include "check-do-concurrent.h"
|
2019-03-26 08:33:03 +01:00
|
|
|
#include "check-if-stmt.h"
|
2019-04-07 20:29:48 +02:00
|
|
|
#include "check-nullify.h"
|
2018-11-30 23:03:05 +01:00
|
|
|
#include "expression.h"
|
2018-09-15 00:04:50 +02:00
|
|
|
#include "mod-file.h"
|
|
|
|
#include "resolve-labels.h"
|
|
|
|
#include "resolve-names.h"
|
|
|
|
#include "rewrite-parse-tree.h"
|
|
|
|
#include "scope.h"
|
|
|
|
#include "symbol.h"
|
2019-01-18 21:40:47 +01:00
|
|
|
#include "../common/default-kinds.h"
|
2019-03-06 01:52:50 +01:00
|
|
|
#include "../parser/parse-tree-visitor.h"
|
2018-10-22 16:37:38 +02:00
|
|
|
#include <ostream>
|
2018-09-15 00:04:50 +02:00
|
|
|
|
|
|
|
namespace Fortran::semantics {
|
|
|
|
|
|
|
|
static void DoDumpSymbols(std::ostream &, const Scope &, int indent = 0);
|
|
|
|
static void PutIndent(std::ostream &, int indent);
|
|
|
|
|
2019-03-06 01:52:50 +01:00
|
|
|
// A parse tree visitor that calls Enter/Leave functions from each checker
|
|
|
|
// class C supplied as template parameters. Enter is called before the node's
|
|
|
|
// children are visited, Leave is called after. No two checkers may have the
|
|
|
|
// same Enter or Leave function. Each checker must be constructible from
|
|
|
|
// SemanticsContext and have BaseChecker as a virtual base class.
|
2019-03-07 02:07:25 +01:00
|
|
|
template<typename... C> class SemanticsVisitor : public virtual C... {
|
|
|
|
public:
|
2019-03-06 01:52:50 +01:00
|
|
|
using C::Enter...;
|
|
|
|
using C::Leave...;
|
|
|
|
using BaseChecker::Enter;
|
|
|
|
using BaseChecker::Leave;
|
2019-03-07 02:07:25 +01:00
|
|
|
SemanticsVisitor(SemanticsContext &context)
|
|
|
|
: C{context}..., context_{context} {}
|
2019-03-26 20:40:13 +01:00
|
|
|
|
2019-03-06 01:52:50 +01:00
|
|
|
template<typename N> bool Pre(const N &node) {
|
|
|
|
Enter(node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename N> void Post(const N &node) { Leave(node); }
|
2019-03-26 20:40:13 +01:00
|
|
|
|
|
|
|
template<typename T> bool Pre(const parser::Statement<T> &node) {
|
|
|
|
context_.set_location(&node.source);
|
|
|
|
Enter(node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T> void Post(const parser::Statement<T> &node) {
|
|
|
|
Leave(node);
|
|
|
|
context_.set_location(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:07:25 +01:00
|
|
|
bool Walk(const parser::Program &program) {
|
|
|
|
parser::Walk(program, *this);
|
|
|
|
return !context_.AnyFatalError();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SemanticsContext &context_;
|
2019-03-06 01:52:50 +01:00
|
|
|
};
|
|
|
|
|
2019-04-12 02:22:16 +02:00
|
|
|
using StatementSemanticsPass1 = ExprChecker;
|
2019-03-26 08:33:03 +01:00
|
|
|
using StatementSemanticsPass2 = SemanticsVisitor<ArithmeticIfStmtChecker,
|
2019-04-11 07:17:44 +02:00
|
|
|
AssignmentChecker, ComputedGotoStmtChecker, DeallocateChecker,
|
2019-04-12 02:22:16 +02:00
|
|
|
DoConcurrentChecker, IfStmtChecker, NullifyChecker>;
|
2019-03-06 01:52:50 +01:00
|
|
|
|
2019-03-26 08:33:03 +01:00
|
|
|
SemanticsContext::SemanticsContext(
|
|
|
|
const common::IntrinsicTypeDefaultKinds &defaultKinds,
|
|
|
|
const parser::LanguageFeatureControl &languageFeatures)
|
2019-03-18 19:48:02 +01:00
|
|
|
: defaultKinds_{defaultKinds}, languageFeatures_{languageFeatures},
|
2018-10-23 01:41:26 +02:00
|
|
|
intrinsics_{evaluate::IntrinsicProcTable::Configure(defaultKinds)},
|
|
|
|
foldingContext_{evaluate::FoldingContext{
|
|
|
|
parser::ContextualMessages{parser::CharBlock{}, &messages_}}} {}
|
|
|
|
|
2019-03-18 19:48:02 +01:00
|
|
|
bool SemanticsContext::IsEnabled(parser::LanguageFeature feature) const {
|
|
|
|
return languageFeatures_.IsEnabled(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SemanticsContext::ShouldWarn(parser::LanguageFeature feature) const {
|
|
|
|
return languageFeatures_.ShouldWarn(feature);
|
|
|
|
}
|
|
|
|
|
2019-01-16 01:59:20 +01:00
|
|
|
const DeclTypeSpec &SemanticsContext::MakeNumericType(
|
2018-12-17 21:41:43 +01:00
|
|
|
TypeCategory category, int kind) {
|
|
|
|
if (kind == 0) {
|
|
|
|
kind = defaultKinds_.GetDefaultKind(category);
|
|
|
|
}
|
2018-12-04 19:55:32 +01:00
|
|
|
return globalScope_.MakeNumericType(category, KindExpr{kind});
|
2018-12-17 21:41:43 +01:00
|
|
|
}
|
2019-01-16 01:59:20 +01:00
|
|
|
const DeclTypeSpec &SemanticsContext::MakeLogicalType(int kind) {
|
2018-12-17 21:41:43 +01:00
|
|
|
if (kind == 0) {
|
|
|
|
kind = defaultKinds_.GetDefaultKind(TypeCategory::Logical);
|
|
|
|
}
|
2018-12-04 19:55:32 +01:00
|
|
|
return globalScope_.MakeLogicalType(KindExpr{kind});
|
2018-12-17 21:41:43 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 16:37:38 +02:00
|
|
|
bool SemanticsContext::AnyFatalError() const {
|
|
|
|
return !messages_.empty() &&
|
|
|
|
(warningsAreErrors_ || messages_.AnyFatalError());
|
2018-09-15 00:04:50 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 19:58:26 +02:00
|
|
|
const Scope &SemanticsContext::FindScope(parser::CharBlock source) const {
|
2019-04-02 20:56:19 +02:00
|
|
|
return const_cast<SemanticsContext *>(this)->FindScope(source);
|
2019-04-02 19:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Scope &SemanticsContext::FindScope(parser::CharBlock source) {
|
|
|
|
if (auto *scope{globalScope_.FindScope(source)}) {
|
2019-03-02 02:33:20 +01:00
|
|
|
return *scope;
|
|
|
|
} else {
|
|
|
|
common::die("invalid source location");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:37:38 +02:00
|
|
|
bool Semantics::Perform() {
|
2019-03-07 02:07:25 +01:00
|
|
|
return ValidateLabels(context_.messages(), program_) &&
|
|
|
|
parser::CanonicalizeDo(program_) && // force line break
|
|
|
|
ResolveNames(context_, program_) &&
|
|
|
|
RewriteParseTree(context_, program_) &&
|
|
|
|
StatementSemanticsPass1{context_}.Walk(program_) &&
|
|
|
|
StatementSemanticsPass2{context_}.Walk(program_) &&
|
|
|
|
ModFileWriter{context_}.WriteAll();
|
2018-10-22 16:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Semantics::EmitMessages(std::ostream &os) const {
|
|
|
|
context_.messages().Emit(os, cooked_);
|
2018-09-15 00:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Semantics::DumpSymbols(std::ostream &os) {
|
2018-10-22 16:37:38 +02:00
|
|
|
DoDumpSymbols(os, context_.globalScope());
|
2018-09-15 00:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoDumpSymbols(std::ostream &os, const Scope &scope, int indent) {
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << Scope::EnumToString(scope.kind()) << " scope:";
|
|
|
|
if (const auto *symbol{scope.symbol()}) {
|
|
|
|
os << ' ' << symbol->name().ToString();
|
|
|
|
}
|
|
|
|
os << '\n';
|
|
|
|
++indent;
|
|
|
|
for (const auto &pair : scope) {
|
|
|
|
const auto &symbol{*pair.second};
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << symbol << '\n';
|
|
|
|
if (const auto *details{symbol.detailsIf<GenericDetails>()}) {
|
|
|
|
if (const auto &type{details->derivedType()}) {
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << *type << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 20:39:46 +01:00
|
|
|
for (const auto &pair : scope.commonBlocks()) {
|
|
|
|
const auto &symbol{*pair.second};
|
|
|
|
PutIndent(os, indent);
|
|
|
|
os << symbol << '\n';
|
|
|
|
}
|
2018-09-15 00:04:50 +02:00
|
|
|
for (const auto &child : scope.children()) {
|
|
|
|
DoDumpSymbols(os, child, indent);
|
|
|
|
}
|
|
|
|
--indent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PutIndent(std::ostream &os, int indent) {
|
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
os << " ";
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 14:55:23 +02:00
|
|
|
}
|