2019-01-07 10:55:09 -08:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-08-10 11:44:43 -07:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "tools.h"
|
2019-09-19 12:19:17 -07:00
|
|
|
#include "traverse.h"
|
2018-08-23 10:55:16 -07:00
|
|
|
#include "../common/idioms.h"
|
2018-08-10 11:44:43 -07:00
|
|
|
#include "../parser/message.h"
|
2018-08-23 10:55:16 -07:00
|
|
|
#include <algorithm>
|
2018-08-10 11:44:43 -07:00
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
using namespace Fortran::parser::literals;
|
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
2019-07-23 10:55:56 -07:00
|
|
|
// IsVariable()
|
2019-09-19 14:56:12 -07:00
|
|
|
auto IsVariableHelper::operator()(const ProcedureDesignator &x) const
|
|
|
|
-> Result {
|
|
|
|
const semantics::Symbol *symbol{x.GetSymbol()};
|
|
|
|
return symbol && symbol->attrs().test(semantics::Attr::POINTER);
|
2019-07-23 10:55:56 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:25:10 -07:00
|
|
|
// Conversions of complex component expressions to REAL.
|
2018-08-23 10:55:16 -07:00
|
|
|
ConvertRealOperandsResult ConvertRealOperands(
|
2018-08-20 09:29:08 -07:00
|
|
|
parser::ContextualMessages &messages, Expr<SomeType> &&x,
|
2018-09-18 11:29:01 -07:00
|
|
|
Expr<SomeType> &&y, int defaultRealKind) {
|
2018-08-10 11:44:43 -07:00
|
|
|
return std::visit(
|
2018-11-29 09:27:34 -08:00
|
|
|
common::visitors{
|
|
|
|
[&](Expr<SomeInteger> &&ix,
|
|
|
|
Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
|
|
|
|
// Can happen in a CMPLX() constructor. Per F'2018,
|
|
|
|
// both integer operands are converted to default REAL.
|
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(ix)),
|
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(iy)))};
|
|
|
|
},
|
2018-08-28 15:15:18 -07:00
|
|
|
[&](Expr<SomeInteger> &&ix,
|
|
|
|
Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
|
2018-08-31 16:14:14 -07:00
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
ConvertTo(ry, std::move(ix)), std::move(ry))};
|
2018-08-10 11:44:43 -07:00
|
|
|
},
|
2018-08-28 15:15:18 -07:00
|
|
|
[&](Expr<SomeReal> &&rx,
|
|
|
|
Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
|
2018-08-31 16:14:14 -07:00
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
std::move(rx), ConvertTo(rx, std::move(iy)))};
|
2018-08-10 11:44:43 -07:00
|
|
|
},
|
2018-08-28 15:15:18 -07:00
|
|
|
[&](Expr<SomeReal> &&rx,
|
|
|
|
Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
|
2018-08-31 16:14:14 -07:00
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
std::move(rx), std::move(ry))};
|
2018-08-10 11:44:43 -07:00
|
|
|
},
|
2018-09-07 15:25:10 -07:00
|
|
|
[&](Expr<SomeInteger> &&ix,
|
|
|
|
BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
|
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
2018-09-18 11:29:01 -07:00
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(ix)),
|
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(by)))};
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](BOZLiteralConstant &&bx,
|
|
|
|
Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
|
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
2018-09-18 11:29:01 -07:00
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(bx)),
|
|
|
|
ConvertToKind<TypeCategory::Real>(
|
|
|
|
defaultRealKind, std::move(iy)))};
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeReal> &&rx,
|
|
|
|
BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
|
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
std::move(rx), ConvertTo(rx, std::move(by)))};
|
|
|
|
},
|
|
|
|
[&](BOZLiteralConstant &&bx,
|
|
|
|
Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
|
|
|
|
return {AsSameKindExprs<TypeCategory::Real>(
|
|
|
|
ConvertTo(ry, std::move(bx)), std::move(ry))};
|
|
|
|
},
|
2018-08-28 15:15:18 -07:00
|
|
|
[&](auto &&, auto &&) -> ConvertRealOperandsResult {
|
2018-08-10 11:44:43 -07:00
|
|
|
messages.Say("operands must be INTEGER or REAL"_err_en_US);
|
|
|
|
return std::nullopt;
|
2018-11-29 09:27:34 -08:00
|
|
|
},
|
|
|
|
},
|
2018-08-10 11:44:43 -07:00
|
|
|
std::move(x.u), std::move(y.u));
|
|
|
|
}
|
|
|
|
|
2018-09-07 10:33:32 -07:00
|
|
|
// Helpers for NumericOperation and its subroutines below.
|
|
|
|
static std::optional<Expr<SomeType>> NoExpr() { return std::nullopt; }
|
|
|
|
|
2018-08-30 10:09:44 -07:00
|
|
|
template<TypeCategory CAT>
|
|
|
|
std::optional<Expr<SomeType>> Package(Expr<SomeKind<CAT>> &&catExpr) {
|
|
|
|
return {AsGenericExpr(std::move(catExpr))};
|
2018-08-23 10:55:16 -07:00
|
|
|
}
|
2018-09-04 16:42:32 -07:00
|
|
|
template<TypeCategory CAT>
|
|
|
|
std::optional<Expr<SomeType>> Package(
|
|
|
|
std::optional<Expr<SomeKind<CAT>>> &&catExpr) {
|
|
|
|
if (catExpr.has_value()) {
|
|
|
|
return {AsGenericExpr(std::move(*catExpr))};
|
|
|
|
}
|
2018-09-07 10:33:32 -07:00
|
|
|
return NoExpr();
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:25:10 -07:00
|
|
|
// Mixed REAL+INTEGER operations. REAL**INTEGER is a special case that
|
|
|
|
// does not require conversion of the exponent expression.
|
|
|
|
template<template<typename> class OPR>
|
|
|
|
std::optional<Expr<SomeType>> MixedRealLeft(
|
|
|
|
Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
|
|
|
|
return Package(std::visit(
|
|
|
|
[&](auto &&rxk) -> Expr<SomeReal> {
|
|
|
|
using resultType = ResultType<decltype(rxk)>;
|
|
|
|
if constexpr (std::is_same_v<OPR<resultType>, Power<resultType>>) {
|
2018-09-17 11:31:38 -07:00
|
|
|
return AsCategoryExpr(
|
|
|
|
RealToIntPower<resultType>{std::move(rxk), std::move(iy)});
|
2018-09-07 15:25:10 -07:00
|
|
|
}
|
|
|
|
// G++ 8.1.0 emits bogus warnings about missing return statements if
|
|
|
|
// this statement is wrapped in an "else", as it should be.
|
2018-09-17 11:31:38 -07:00
|
|
|
return AsCategoryExpr(OPR<resultType>{
|
|
|
|
std::move(rxk), ConvertToType<resultType>(std::move(iy))});
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
std::move(rx.u)));
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:42:32 -07:00
|
|
|
std::optional<Expr<SomeComplex>> ConstructComplex(
|
|
|
|
parser::ContextualMessages &messages, Expr<SomeType> &&real,
|
2018-09-18 11:29:01 -07:00
|
|
|
Expr<SomeType> &&imaginary, int defaultRealKind) {
|
2018-09-04 16:42:32 -07:00
|
|
|
if (auto converted{ConvertRealOperands(
|
2018-09-18 11:29:01 -07:00
|
|
|
messages, std::move(real), std::move(imaginary), defaultRealKind)}) {
|
2018-09-04 16:42:32 -07:00
|
|
|
return {std::visit(
|
|
|
|
[](auto &&pair) {
|
|
|
|
return MakeComplex(std::move(pair[0]), std::move(pair[1]));
|
|
|
|
},
|
|
|
|
std::move(*converted))};
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Expr<SomeComplex>> ConstructComplex(
|
|
|
|
parser::ContextualMessages &messages, std::optional<Expr<SomeType>> &&real,
|
2018-09-18 11:29:01 -07:00
|
|
|
std::optional<Expr<SomeType>> &&imaginary, int defaultRealKind) {
|
2018-09-04 16:42:32 -07:00
|
|
|
if (auto parts{common::AllPresent(std::move(real), std::move(imaginary))}) {
|
2019-06-18 15:15:22 -07:00
|
|
|
return ConstructComplex(messages, std::get<0>(std::move(*parts)),
|
|
|
|
std::get<1>(std::move(*parts)), defaultRealKind);
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr<SomeReal> GetComplexPart(const Expr<SomeComplex> &z, bool isImaginary) {
|
|
|
|
return std::visit(
|
|
|
|
[&](const auto &zk) {
|
|
|
|
static constexpr int kind{ResultType<decltype(zk)>::kind};
|
2018-09-17 11:31:38 -07:00
|
|
|
return AsCategoryExpr(ComplexComponent<kind>{isImaginary, zk});
|
2018-09-04 16:42:32 -07:00
|
|
|
},
|
|
|
|
z.u);
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:25:10 -07:00
|
|
|
// Handle mixed COMPLEX+REAL (or INTEGER) operations in a better way
|
2018-09-05 17:12:03 -07:00
|
|
|
// than just converting the second operand to COMPLEX and performing the
|
|
|
|
// corresponding COMPLEX+COMPLEX operation.
|
|
|
|
template<template<typename> class OPR, TypeCategory RCAT>
|
|
|
|
std::optional<Expr<SomeType>> MixedComplexLeft(
|
|
|
|
parser::ContextualMessages &messages, Expr<SomeComplex> &&zx,
|
2018-10-15 17:11:24 -07:00
|
|
|
Expr<SomeKind<RCAT>> &&iry, int defaultRealKind) {
|
2018-09-04 16:42:32 -07:00
|
|
|
Expr<SomeReal> zr{GetComplexPart(zx, false)};
|
|
|
|
Expr<SomeReal> zi{GetComplexPart(zx, true)};
|
2018-10-15 17:11:24 -07:00
|
|
|
if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
|
|
|
|
std::is_same_v<OPR<LargestReal>, Subtract<LargestReal>>) {
|
2018-09-05 17:12:03 -07:00
|
|
|
// (a,b) + x -> (a+x, b)
|
|
|
|
// (a,b) - x -> (a-x, b)
|
2018-10-15 17:11:24 -07:00
|
|
|
if (std::optional<Expr<SomeType>> rr{
|
|
|
|
NumericOperation<OPR>(messages, AsGenericExpr(std::move(zr)),
|
|
|
|
AsGenericExpr(std::move(iry)), defaultRealKind)}) {
|
|
|
|
return Package(ConstructComplex(messages, std::move(*rr),
|
|
|
|
AsGenericExpr(std::move(zi)), defaultRealKind));
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
2018-10-15 17:11:24 -07:00
|
|
|
} else if constexpr (std::is_same_v<OPR<LargestReal>,
|
|
|
|
Multiply<LargestReal>> ||
|
|
|
|
std::is_same_v<OPR<LargestReal>, Divide<LargestReal>>) {
|
2018-09-05 17:12:03 -07:00
|
|
|
// (a,b) * x -> (a*x, b*x)
|
|
|
|
// (a,b) / x -> (a/x, b/x)
|
2018-09-04 16:42:32 -07:00
|
|
|
auto copy{iry};
|
2018-10-15 17:11:24 -07:00
|
|
|
auto rr{NumericOperation<Multiply>(messages, AsGenericExpr(std::move(zr)),
|
|
|
|
AsGenericExpr(std::move(iry)), defaultRealKind)};
|
2018-09-05 17:12:03 -07:00
|
|
|
auto ri{NumericOperation<Multiply>(messages, AsGenericExpr(std::move(zi)),
|
2018-10-15 17:11:24 -07:00
|
|
|
AsGenericExpr(std::move(copy)), defaultRealKind)};
|
2018-09-04 16:42:32 -07:00
|
|
|
if (auto parts{common::AllPresent(std::move(rr), std::move(ri))}) {
|
2019-06-18 15:15:22 -07:00
|
|
|
return Package(ConstructComplex(messages, std::get<0>(std::move(*parts)),
|
|
|
|
std::get<1>(std::move(*parts)), defaultRealKind));
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
2018-09-07 15:25:10 -07:00
|
|
|
} else if constexpr (RCAT == TypeCategory::Integer &&
|
2018-10-15 17:11:24 -07:00
|
|
|
std::is_same_v<OPR<LargestReal>, Power<LargestReal>>) {
|
2018-09-07 15:25:10 -07:00
|
|
|
// COMPLEX**INTEGER is a special case that doesn't convert the exponent.
|
|
|
|
static_assert(RCAT == TypeCategory::Integer);
|
|
|
|
return Package(std::visit(
|
|
|
|
[&](auto &&zxk) {
|
|
|
|
using Ty = ResultType<decltype(zxk)>;
|
|
|
|
return AsCategoryExpr(
|
|
|
|
AsExpr(RealToIntPower<Ty>{std::move(zxk), std::move(iry)}));
|
|
|
|
},
|
|
|
|
std::move(zx.u)));
|
2019-08-15 11:54:51 -07:00
|
|
|
} else if (defaultRealKind != 666) { // dodge unused parameter warning
|
2018-09-07 15:25:10 -07:00
|
|
|
// (a,b) ** x -> (a,b) ** (x,0)
|
2018-09-05 17:12:03 -07:00
|
|
|
Expr<SomeComplex> zy{ConvertTo(zx, std::move(iry))};
|
|
|
|
return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
|
|
|
|
}
|
2018-09-07 10:33:32 -07:00
|
|
|
return NoExpr();
|
2018-09-05 17:12:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mixed COMPLEX operations with the COMPLEX operand on the right.
|
|
|
|
// x + (a,b) -> (x+a, b)
|
|
|
|
// x - (a,b) -> (x-a, -b)
|
|
|
|
// x * (a,b) -> (x*a, x*b)
|
2018-09-07 15:25:10 -07:00
|
|
|
// x / (a,b) -> (x,0) / (a,b) (and **)
|
2018-09-05 17:12:03 -07:00
|
|
|
template<template<typename> class OPR, TypeCategory LCAT>
|
|
|
|
std::optional<Expr<SomeType>> MixedComplexRight(
|
|
|
|
parser::ContextualMessages &messages, Expr<SomeKind<LCAT>> &&irx,
|
2018-10-15 17:11:24 -07:00
|
|
|
Expr<SomeComplex> &&zy, int defaultRealKind) {
|
|
|
|
if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
|
|
|
|
std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>>) {
|
2018-09-05 17:12:03 -07:00
|
|
|
// x + (a,b) -> (a,b) + x -> (a+x, b)
|
|
|
|
// x * (a,b) -> (a,b) * x -> (a*x, b*x)
|
2018-10-15 17:11:24 -07:00
|
|
|
return MixedComplexLeft<Add, LCAT>(
|
|
|
|
messages, std::move(zy), std::move(irx), defaultRealKind);
|
|
|
|
} else if constexpr (std::is_same_v<OPR<LargestReal>,
|
|
|
|
Subtract<LargestReal>>) {
|
2018-09-05 17:12:03 -07:00
|
|
|
// x - (a,b) -> (x-a, -b)
|
|
|
|
Expr<SomeReal> zr{GetComplexPart(zy, false)};
|
|
|
|
Expr<SomeReal> zi{GetComplexPart(zy, true)};
|
2018-10-15 17:11:24 -07:00
|
|
|
if (std::optional<Expr<SomeType>> rr{
|
|
|
|
NumericOperation<Subtract>(messages, AsGenericExpr(std::move(irx)),
|
|
|
|
AsGenericExpr(std::move(zr)), defaultRealKind)}) {
|
|
|
|
return Package(ConstructComplex(messages, std::move(*rr),
|
|
|
|
AsGenericExpr(-std::move(zi)), defaultRealKind));
|
2018-09-05 17:12:03 -07:00
|
|
|
}
|
2019-08-15 11:54:51 -07:00
|
|
|
} else if (defaultRealKind != 666) { // dodge unused parameter warning
|
2018-09-07 15:25:10 -07:00
|
|
|
// x / (a,b) -> (x,0) / (a,b)
|
2018-09-05 17:12:03 -07:00
|
|
|
Expr<SomeComplex> zx{ConvertTo(zy, std::move(irx))};
|
|
|
|
return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
2018-09-07 10:33:32 -07:00
|
|
|
return NoExpr();
|
2018-09-04 16:42:32 -07:00
|
|
|
}
|
2018-08-23 10:55:16 -07:00
|
|
|
|
2018-09-04 14:20:48 -07:00
|
|
|
// N.B. When a "typeless" BOZ literal constant appears as one (not both!) of
|
2018-09-07 15:25:10 -07:00
|
|
|
// the operands to a dyadic operation where one is permitted, it assumes the
|
|
|
|
// type and kind of the other operand.
|
2018-08-23 10:55:16 -07:00
|
|
|
template<template<typename> class OPR>
|
|
|
|
std::optional<Expr<SomeType>> NumericOperation(
|
|
|
|
parser::ContextualMessages &messages, Expr<SomeType> &&x,
|
2018-10-15 17:11:24 -07:00
|
|
|
Expr<SomeType> &&y, int defaultRealKind) {
|
2018-08-23 10:55:16 -07:00
|
|
|
return std::visit(
|
2018-11-29 09:27:34 -08:00
|
|
|
common::visitors{
|
|
|
|
[](Expr<SomeInteger> &&ix, Expr<SomeInteger> &&iy) {
|
|
|
|
return Package(PromoteAndCombine<OPR, TypeCategory::Integer>(
|
|
|
|
std::move(ix), std::move(iy)));
|
|
|
|
},
|
2018-08-23 10:55:16 -07:00
|
|
|
[](Expr<SomeReal> &&rx, Expr<SomeReal> &&ry) {
|
2018-08-30 10:09:44 -07:00
|
|
|
return Package(PromoteAndCombine<OPR, TypeCategory::Real>(
|
|
|
|
std::move(rx), std::move(ry)));
|
2018-08-23 10:55:16 -07:00
|
|
|
},
|
2018-09-07 15:25:10 -07:00
|
|
|
// Mixed REAL/INTEGER operations
|
2018-08-23 10:55:16 -07:00
|
|
|
[](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
|
2018-09-07 15:25:10 -07:00
|
|
|
return MixedRealLeft<OPR>(std::move(rx), std::move(iy));
|
2018-08-23 10:55:16 -07:00
|
|
|
},
|
|
|
|
[](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
|
2018-08-30 10:09:44 -07:00
|
|
|
return Package(std::visit(
|
2018-08-23 10:55:16 -07:00
|
|
|
[&](auto &&ryk) -> Expr<SomeReal> {
|
2018-08-30 10:09:44 -07:00
|
|
|
using resultType = ResultType<decltype(ryk)>;
|
2018-09-17 11:31:38 -07:00
|
|
|
return AsCategoryExpr(
|
2018-09-04 16:42:32 -07:00
|
|
|
OPR<resultType>{ConvertToType<resultType>(std::move(ix)),
|
2018-09-17 11:31:38 -07:00
|
|
|
std::move(ryk)});
|
2018-08-23 10:55:16 -07:00
|
|
|
},
|
2018-08-30 10:09:44 -07:00
|
|
|
std::move(ry.u)));
|
2018-08-23 10:55:16 -07:00
|
|
|
},
|
2018-09-07 15:25:10 -07:00
|
|
|
// Homogeneous and mixed COMPLEX operations
|
2018-08-23 10:55:16 -07:00
|
|
|
[](Expr<SomeComplex> &&zx, Expr<SomeComplex> &&zy) {
|
2018-08-30 10:09:44 -07:00
|
|
|
return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
|
|
|
|
std::move(zx), std::move(zy)));
|
2018-08-23 10:55:16 -07:00
|
|
|
},
|
2018-09-04 16:42:32 -07:00
|
|
|
[&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&zy) {
|
2018-09-05 17:12:03 -07:00
|
|
|
return MixedComplexLeft<OPR>(
|
2018-10-15 17:11:24 -07:00
|
|
|
messages, std::move(zx), std::move(zy), defaultRealKind);
|
2018-09-04 16:42:32 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&zy) {
|
2018-09-05 17:12:03 -07:00
|
|
|
return MixedComplexLeft<OPR>(
|
2018-10-15 17:11:24 -07:00
|
|
|
messages, std::move(zx), std::move(zy), defaultRealKind);
|
2018-09-05 17:12:03 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeInteger> &&zx, Expr<SomeComplex> &&zy) {
|
|
|
|
return MixedComplexRight<OPR>(
|
2018-10-15 17:11:24 -07:00
|
|
|
messages, std::move(zx), std::move(zy), defaultRealKind);
|
2018-09-05 17:12:03 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeReal> &&zx, Expr<SomeComplex> &&zy) {
|
|
|
|
return MixedComplexRight<OPR>(
|
2018-10-15 17:11:24 -07:00
|
|
|
messages, std::move(zx), std::move(zy), defaultRealKind);
|
2018-09-04 16:42:32 -07:00
|
|
|
},
|
2018-09-05 17:12:03 -07:00
|
|
|
// Operations with one typeless operand
|
2018-09-04 14:20:48 -07:00
|
|
|
[&](BOZLiteralConstant &&bx, Expr<SomeInteger> &&iy) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return NumericOperation<OPR>(messages,
|
2018-10-15 17:11:24 -07:00
|
|
|
AsGenericExpr(ConvertTo(iy, std::move(bx))), std::move(y),
|
|
|
|
defaultRealKind);
|
2018-08-31 16:14:14 -07:00
|
|
|
},
|
2018-09-04 14:20:48 -07:00
|
|
|
[&](BOZLiteralConstant &&bx, Expr<SomeReal> &&ry) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return NumericOperation<OPR>(messages,
|
2018-10-15 17:11:24 -07:00
|
|
|
AsGenericExpr(ConvertTo(ry, std::move(bx))), std::move(y),
|
|
|
|
defaultRealKind);
|
2018-08-31 16:14:14 -07:00
|
|
|
},
|
2018-09-04 14:20:48 -07:00
|
|
|
[&](Expr<SomeInteger> &&ix, BOZLiteralConstant &&by) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return NumericOperation<OPR>(messages, std::move(x),
|
2018-10-15 17:11:24 -07:00
|
|
|
AsGenericExpr(ConvertTo(ix, std::move(by))), defaultRealKind);
|
2018-09-04 14:20:48 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeReal> &&rx, BOZLiteralConstant &&by) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return NumericOperation<OPR>(messages, std::move(x),
|
2018-10-15 17:11:24 -07:00
|
|
|
AsGenericExpr(ConvertTo(rx, std::move(by))), defaultRealKind);
|
2018-09-04 14:20:48 -07:00
|
|
|
},
|
2018-09-05 17:12:03 -07:00
|
|
|
// Default case
|
2018-08-23 10:55:16 -07:00
|
|
|
[&](auto &&, auto &&) {
|
2018-09-07 10:33:32 -07:00
|
|
|
// TODO: defined operator
|
2018-08-23 10:55:16 -07:00
|
|
|
messages.Say("non-numeric operands to numeric operation"_err_en_US);
|
2018-09-07 10:33:32 -07:00
|
|
|
return NoExpr();
|
2018-11-29 09:27:34 -08:00
|
|
|
},
|
|
|
|
},
|
2018-08-23 10:55:16 -07:00
|
|
|
std::move(x.u), std::move(y.u));
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:25:10 -07:00
|
|
|
template std::optional<Expr<SomeType>> NumericOperation<Power>(
|
2018-10-15 17:11:24 -07:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
|
|
|
|
int defaultRealKind);
|
2018-08-31 16:14:14 -07:00
|
|
|
template std::optional<Expr<SomeType>> NumericOperation<Multiply>(
|
2018-10-15 17:11:24 -07:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
|
|
|
|
int defaultRealKind);
|
2018-08-31 16:14:14 -07:00
|
|
|
template std::optional<Expr<SomeType>> NumericOperation<Divide>(
|
2018-10-15 17:11:24 -07:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
|
|
|
|
int defaultRealKind);
|
2018-09-07 15:25:10 -07:00
|
|
|
template std::optional<Expr<SomeType>> NumericOperation<Add>(
|
2018-10-15 17:11:24 -07:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
|
|
|
|
int defaultRealKind);
|
2018-09-07 15:25:10 -07:00
|
|
|
template std::optional<Expr<SomeType>> NumericOperation<Subtract>(
|
2018-10-15 17:11:24 -07:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
|
|
|
|
int defaultRealKind);
|
2018-08-23 10:55:16 -07:00
|
|
|
|
2018-09-07 10:33:32 -07:00
|
|
|
std::optional<Expr<SomeType>> Negation(
|
|
|
|
parser::ContextualMessages &messages, Expr<SomeType> &&x) {
|
|
|
|
return std::visit(
|
2018-09-12 16:27:51 -07:00
|
|
|
common::visitors{
|
|
|
|
[&](BOZLiteralConstant &&) {
|
|
|
|
messages.Say("BOZ literal cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
|
|
|
},
|
2019-02-19 17:06:28 -08:00
|
|
|
[&](NullPointer &&) {
|
|
|
|
messages.Say("NULL() cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
|
|
|
},
|
2019-05-03 11:29:15 -07:00
|
|
|
[&](ProcedureDesignator &&) {
|
|
|
|
messages.Say("Subroutine cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
|
|
|
},
|
|
|
|
[&](ProcedureRef &&) {
|
|
|
|
messages.Say("Pointer to subroutine cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
|
|
|
},
|
2018-12-05 13:11:55 -08:00
|
|
|
[&](Expr<SomeInteger> &&x) { return Package(-std::move(x)); },
|
2018-09-07 10:33:32 -07:00
|
|
|
[&](Expr<SomeReal> &&x) { return Package(-std::move(x)); },
|
|
|
|
[&](Expr<SomeComplex> &&x) { return Package(-std::move(x)); },
|
2019-08-15 13:50:27 -07:00
|
|
|
[&](Expr<SomeCharacter> &&) {
|
2018-09-07 10:33:32 -07:00
|
|
|
// TODO: defined operator
|
|
|
|
messages.Say("CHARACTER cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
|
|
|
},
|
2019-08-15 13:50:27 -07:00
|
|
|
[&](Expr<SomeLogical> &&) {
|
2018-09-07 10:33:32 -07:00
|
|
|
// TODO: defined operator
|
|
|
|
messages.Say("LOGICAL cannot be negated"_err_en_US);
|
|
|
|
return NoExpr();
|
2018-09-12 16:27:51 -07:00
|
|
|
},
|
2019-08-15 13:50:27 -07:00
|
|
|
[&](Expr<SomeDerived> &&) {
|
2018-09-12 16:27:51 -07:00
|
|
|
// TODO: defined operator
|
2019-05-13 09:33:18 -07:00
|
|
|
messages.Say("Operand cannot be negated"_err_en_US);
|
2018-09-12 16:27:51 -07:00
|
|
|
return NoExpr();
|
|
|
|
},
|
|
|
|
},
|
2018-09-07 10:33:32 -07:00
|
|
|
std::move(x.u));
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:54:33 -07:00
|
|
|
Expr<SomeLogical> LogicalNegation(Expr<SomeLogical> &&x) {
|
|
|
|
return std::visit(
|
2019-01-07 10:15:27 -08:00
|
|
|
[](auto &&xk) { return AsCategoryExpr(LogicalNegation(std::move(xk))); },
|
2018-09-07 16:54:33 -07:00
|
|
|
std::move(x.u));
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:25:10 -07:00
|
|
|
template<typename T>
|
|
|
|
Expr<LogicalResult> PackageRelation(
|
|
|
|
RelationalOperator opr, Expr<T> &&x, Expr<T> &&y) {
|
2018-11-29 10:25:46 -08:00
|
|
|
static_assert(IsSpecificIntrinsicType<T>);
|
2018-09-07 15:25:10 -07:00
|
|
|
return Expr<LogicalResult>{
|
|
|
|
Relational<SomeType>{Relational<T>{opr, std::move(x), std::move(y)}}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory CAT>
|
|
|
|
Expr<LogicalResult> PromoteAndRelate(
|
|
|
|
RelationalOperator opr, Expr<SomeKind<CAT>> &&x, Expr<SomeKind<CAT>> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &&xy) {
|
|
|
|
return PackageRelation(opr, std::move(xy[0]), std::move(xy[1]));
|
|
|
|
},
|
|
|
|
AsSameKindExprs(std::move(x), std::move(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages,
|
|
|
|
RelationalOperator opr, Expr<SomeType> &&x, Expr<SomeType> &&y) {
|
|
|
|
return std::visit(
|
2018-11-29 09:27:34 -08:00
|
|
|
common::visitors{
|
2019-06-03 10:51:51 -07:00
|
|
|
[=](Expr<SomeInteger> &&ix,
|
|
|
|
Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> {
|
|
|
|
return PromoteAndRelate(opr, std::move(ix), std::move(iy));
|
2018-11-29 09:27:34 -08:00
|
|
|
},
|
2019-06-03 10:51:51 -07:00
|
|
|
[=](Expr<SomeReal> &&rx,
|
|
|
|
Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> {
|
|
|
|
return PromoteAndRelate(opr, std::move(rx), std::move(ry));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr, std::move(x),
|
|
|
|
AsGenericExpr(ConvertTo(rx, std::move(iy))));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr,
|
|
|
|
AsGenericExpr(ConvertTo(ry, std::move(ix))), std::move(y));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
2019-06-03 10:51:51 -07:00
|
|
|
[&](Expr<SomeComplex> &&zx,
|
|
|
|
Expr<SomeComplex> &&zy) -> std::optional<Expr<LogicalResult>> {
|
2018-09-07 15:25:10 -07:00
|
|
|
if (opr != RelationalOperator::EQ &&
|
|
|
|
opr != RelationalOperator::NE) {
|
|
|
|
messages.Say(
|
|
|
|
"COMPLEX data may be compared only for equality"_err_en_US);
|
|
|
|
} else {
|
2018-09-12 16:27:51 -07:00
|
|
|
auto rr{Relate(messages, opr,
|
|
|
|
AsGenericExpr(GetComplexPart(zx, false)),
|
|
|
|
AsGenericExpr(GetComplexPart(zy, false)))};
|
|
|
|
auto ri{
|
|
|
|
Relate(messages, opr, AsGenericExpr(GetComplexPart(zx, true)),
|
|
|
|
AsGenericExpr(GetComplexPart(zy, true)))};
|
2018-09-07 15:25:10 -07:00
|
|
|
if (auto parts{
|
|
|
|
common::AllPresent(std::move(rr), std::move(ri))}) {
|
|
|
|
// (a,b)==(c,d) -> (a==c) .AND. (b==d)
|
|
|
|
// (a,b)/=(c,d) -> (a/=c) .OR. (b/=d)
|
|
|
|
LogicalOperator combine{opr == RelationalOperator::EQ
|
|
|
|
? LogicalOperator::And
|
|
|
|
: LogicalOperator::Or};
|
2019-06-03 10:51:51 -07:00
|
|
|
return Expr<LogicalResult>{
|
|
|
|
LogicalOperation<LogicalResult::kind>{combine,
|
2019-06-18 15:15:22 -07:00
|
|
|
std::get<0>(std::move(*parts)),
|
|
|
|
std::get<1>(std::move(*parts))}};
|
2018-09-07 15:25:10 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 10:51:51 -07:00
|
|
|
return std::nullopt;
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr, std::move(x),
|
|
|
|
AsGenericExpr(ConvertTo(zx, std::move(iy))));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr, std::move(x),
|
|
|
|
AsGenericExpr(ConvertTo(zx, std::move(ry))));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr,
|
|
|
|
AsGenericExpr(ConvertTo(zy, std::move(ix))), std::move(y));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
|
2018-09-12 16:27:51 -07:00
|
|
|
return Relate(messages, opr,
|
|
|
|
AsGenericExpr(ConvertTo(zy, std::move(rx))), std::move(y));
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
[&](Expr<SomeCharacter> &&cx, Expr<SomeCharacter> &&cy) {
|
|
|
|
return std::visit(
|
2019-06-03 10:51:51 -07:00
|
|
|
[&](auto &&cxk,
|
|
|
|
auto &&cyk) -> std::optional<Expr<LogicalResult>> {
|
2018-09-07 15:25:10 -07:00
|
|
|
using Ty = ResultType<decltype(cxk)>;
|
|
|
|
if constexpr (std::is_same_v<Ty, ResultType<decltype(cyk)>>) {
|
2019-06-03 10:51:51 -07:00
|
|
|
return PackageRelation(opr, std::move(cxk), std::move(cyk));
|
2018-09-07 15:25:10 -07:00
|
|
|
} else {
|
|
|
|
messages.Say(
|
|
|
|
"CHARACTER operands do not have same KIND"_err_en_US);
|
2019-08-01 11:41:05 -07:00
|
|
|
return std::nullopt;
|
2018-09-07 15:25:10 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
std::move(cx.u), std::move(cy.u));
|
|
|
|
},
|
|
|
|
// Default case
|
2019-08-01 11:41:05 -07:00
|
|
|
[&](auto &&, auto &&) {
|
2018-09-07 15:25:10 -07:00
|
|
|
// TODO: defined operator
|
2019-09-11 16:48:03 -07:00
|
|
|
auto xtype{x.GetType()};
|
|
|
|
auto ytype{y.GetType()};
|
|
|
|
if (xtype.has_value() && ytype.has_value()) {
|
|
|
|
messages.Say(
|
|
|
|
"Relational operands do not have comparable types (%s vs. %s)"_err_en_US,
|
|
|
|
xtype->AsFortran(), ytype->AsFortran());
|
|
|
|
} else {
|
|
|
|
messages.Say(
|
|
|
|
"Relational operands do not have comparable types"_err_en_US);
|
|
|
|
}
|
2019-08-01 11:41:05 -07:00
|
|
|
return std::optional<Expr<LogicalResult>>{};
|
2018-11-29 09:27:34 -08:00
|
|
|
},
|
|
|
|
},
|
2018-09-07 15:25:10 -07:00
|
|
|
std::move(x.u), std::move(y.u));
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr<SomeLogical> BinaryLogicalOperation(
|
|
|
|
LogicalOperator opr, Expr<SomeLogical> &&x, Expr<SomeLogical> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &&xy) {
|
|
|
|
using Ty = ResultType<decltype(xy[0])>;
|
2019-01-07 10:15:27 -08:00
|
|
|
return Expr<SomeLogical>{BinaryLogicalOperation<Ty::kind>(
|
|
|
|
opr, std::move(xy[0]), std::move(xy[1]))};
|
2018-09-07 15:25:10 -07:00
|
|
|
},
|
|
|
|
AsSameKindExprs(std::move(x), std::move(y)));
|
|
|
|
}
|
2019-01-22 16:30:32 -08:00
|
|
|
|
|
|
|
template<TypeCategory TO>
|
|
|
|
std::optional<Expr<SomeType>> ConvertToNumeric(int kind, Expr<SomeType> &&x) {
|
|
|
|
static_assert(common::IsNumericTypeCategory(TO));
|
|
|
|
return std::visit(
|
|
|
|
[=](auto &&cx) -> std::optional<Expr<SomeType>> {
|
|
|
|
using cxType = std::decay_t<decltype(cx)>;
|
2019-05-03 11:29:15 -07:00
|
|
|
if constexpr (!common::HasMember<cxType, TypelessExpression>) {
|
2019-01-22 16:30:32 -08:00
|
|
|
if constexpr (IsNumericTypeCategory(ResultType<cxType>::category)) {
|
2019-06-03 10:51:51 -07:00
|
|
|
return Expr<SomeType>{ConvertToKind<TO>(kind, std::move(cx))};
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
},
|
|
|
|
std::move(x.u));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Expr<SomeType>> ConvertToType(
|
|
|
|
const DynamicType &type, Expr<SomeType> &&x) {
|
2019-05-13 09:33:18 -07:00
|
|
|
switch (type.category()) {
|
2019-01-22 16:30:32 -08:00
|
|
|
case TypeCategory::Integer:
|
2019-06-03 10:51:51 -07:00
|
|
|
if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
|
|
|
|
// Extension to C7109: allow BOZ literals to appear in integer contexts
|
|
|
|
// when the type is unambiguous.
|
|
|
|
return Expr<SomeType>{
|
|
|
|
ConvertToKind<TypeCategory::Integer>(type.kind(), std::move(*boz))};
|
|
|
|
}
|
2019-05-13 09:33:18 -07:00
|
|
|
return ConvertToNumeric<TypeCategory::Integer>(type.kind(), std::move(x));
|
2019-01-22 16:30:32 -08:00
|
|
|
case TypeCategory::Real:
|
2019-07-17 14:26:35 -07:00
|
|
|
if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
|
|
|
|
return Expr<SomeType>{
|
|
|
|
ConvertToKind<TypeCategory::Real>(type.kind(), std::move(*boz))};
|
|
|
|
}
|
2019-05-13 09:33:18 -07:00
|
|
|
return ConvertToNumeric<TypeCategory::Real>(type.kind(), std::move(x));
|
2019-01-22 16:30:32 -08:00
|
|
|
case TypeCategory::Complex:
|
2019-05-13 09:33:18 -07:00
|
|
|
return ConvertToNumeric<TypeCategory::Complex>(type.kind(), std::move(x));
|
2019-01-22 16:30:32 -08:00
|
|
|
case TypeCategory::Character:
|
2019-02-19 15:38:55 -08:00
|
|
|
if (auto *cx{UnwrapExpr<Expr<SomeCharacter>>(x)}) {
|
2019-02-27 15:51:03 -08:00
|
|
|
auto converted{
|
2019-05-13 09:33:18 -07:00
|
|
|
ConvertToKind<TypeCategory::Character>(type.kind(), std::move(*cx))};
|
|
|
|
if (type.charLength() != nullptr) {
|
|
|
|
if (const auto &len{type.charLength()->GetExplicit()}) {
|
2019-02-27 15:51:03 -08:00
|
|
|
Expr<SomeInteger> lenParam{*len};
|
|
|
|
Expr<SubscriptInteger> length{Convert<SubscriptInteger>{lenParam}};
|
|
|
|
converted = std::visit(
|
|
|
|
[&](auto &&x) {
|
|
|
|
using Ty = std::decay_t<decltype(x)>;
|
|
|
|
using CharacterType = typename Ty::Result;
|
|
|
|
return Expr<SomeCharacter>{
|
|
|
|
Expr<CharacterType>{SetLength<CharacterType::kind>{
|
|
|
|
std::move(x), std::move(length)}}};
|
|
|
|
},
|
|
|
|
std::move(converted.u));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Expr<SomeType>{std::move(converted)};
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TypeCategory::Logical:
|
|
|
|
if (auto *cx{UnwrapExpr<Expr<SomeLogical>>(x)}) {
|
|
|
|
return Expr<SomeType>{
|
2019-05-13 09:33:18 -07:00
|
|
|
ConvertToKind<TypeCategory::Logical>(type.kind(), std::move(*cx))};
|
2019-01-22 16:30:32 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TypeCategory::Derived:
|
|
|
|
if (auto fromType{x.GetType()}) {
|
2019-02-19 15:38:55 -08:00
|
|
|
if (type == *fromType) {
|
2019-01-22 16:30:32 -08:00
|
|
|
return std::move(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:33:18 -07:00
|
|
|
std::optional<Expr<SomeType>> ConvertToType(
|
|
|
|
const DynamicType &to, std::optional<Expr<SomeType>> &&x) {
|
|
|
|
if (x.has_value()) {
|
|
|
|
return ConvertToType(to, std::move(*x));
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:38:55 -08:00
|
|
|
std::optional<Expr<SomeType>> ConvertToType(
|
|
|
|
const semantics::Symbol &symbol, Expr<SomeType> &&x) {
|
|
|
|
if (int xRank{x.Rank()}; xRank > 0) {
|
|
|
|
if (symbol.Rank() != xRank) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 11:29:15 -07:00
|
|
|
if (auto symType{DynamicType::From(symbol)}) {
|
2019-02-19 15:38:55 -08:00
|
|
|
return ConvertToType(*symType, std::move(x));
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2019-01-22 16:30:32 -08:00
|
|
|
std::optional<Expr<SomeType>> ConvertToType(
|
2019-05-13 09:33:18 -07:00
|
|
|
const semantics::Symbol &to, std::optional<Expr<SomeType>> &&x) {
|
2019-01-22 16:30:32 -08:00
|
|
|
if (x.has_value()) {
|
2019-05-13 09:33:18 -07:00
|
|
|
return ConvertToType(to, std::move(*x));
|
2019-01-22 16:30:32 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 11:29:15 -07:00
|
|
|
|
2019-07-31 14:13:42 -07:00
|
|
|
bool IsAssumedRank(const semantics::Symbol &symbol0) {
|
|
|
|
const semantics::Symbol &symbol{ResolveAssociations(symbol0)};
|
2019-05-03 11:29:15 -07:00
|
|
|
if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
|
|
|
|
return details->IsAssumedRank();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAssumedRank(const ActualArgument &arg) {
|
2019-05-21 16:58:46 -07:00
|
|
|
if (const auto *expr{arg.UnwrapExpr()}) {
|
2019-05-03 11:29:15 -07:00
|
|
|
return IsAssumedRank(*expr);
|
|
|
|
} else {
|
|
|
|
const semantics::Symbol *assumedTypeDummy{arg.GetAssumedTypeDummy()};
|
|
|
|
CHECK(assumedTypeDummy != nullptr);
|
|
|
|
return IsAssumedRank(*assumedTypeDummy);
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 10:55:56 -07:00
|
|
|
|
|
|
|
// GetLastTarget()
|
2019-09-19 15:49:13 -07:00
|
|
|
auto GetLastTargetHelper::operator()(const semantics::Symbol &x) const
|
|
|
|
-> Result {
|
2019-07-23 10:55:56 -07:00
|
|
|
if (x.attrs().HasAny({semantics::Attr::POINTER, semantics::Attr::TARGET})) {
|
2019-09-19 15:49:13 -07:00
|
|
|
return &x;
|
2019-07-23 10:55:56 -07:00
|
|
|
} else {
|
2019-09-19 15:49:13 -07:00
|
|
|
return nullptr;
|
2019-07-23 10:55:56 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 15:49:13 -07:00
|
|
|
auto GetLastTargetHelper::operator()(const Component &x) const -> Result {
|
2019-07-23 10:55:56 -07:00
|
|
|
const semantics::Symbol &symbol{x.GetLastSymbol()};
|
|
|
|
if (symbol.attrs().HasAny(
|
|
|
|
{semantics::Attr::POINTER, semantics::Attr::TARGET})) {
|
2019-09-19 15:49:13 -07:00
|
|
|
return &symbol;
|
2019-07-23 13:42:06 -07:00
|
|
|
} else if (symbol.attrs().test(semantics::Attr::ALLOCATABLE)) {
|
2019-09-19 15:49:13 -07:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
2019-07-23 10:55:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:13:42 -07:00
|
|
|
const semantics::Symbol &ResolveAssociations(const semantics::Symbol &symbol) {
|
|
|
|
if (const auto *details{symbol.detailsIf<semantics::AssocEntityDetails>()}) {
|
|
|
|
if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) {
|
|
|
|
return ResolveAssociations(*nested);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2019-09-19 12:19:17 -07:00
|
|
|
struct CollectSymbolsHelper
|
|
|
|
: public SetTraverse<CollectSymbolsHelper, SetOfSymbols> {
|
|
|
|
using Base = SetTraverse<CollectSymbolsHelper, SetOfSymbols>;
|
|
|
|
CollectSymbolsHelper() : Base{*this} {}
|
|
|
|
using Base::operator();
|
|
|
|
SetOfSymbols operator()(const semantics::Symbol &symbol) const {
|
|
|
|
return {&symbol};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename A> SetOfSymbols CollectSymbols(const A &x) {
|
|
|
|
return CollectSymbolsHelper{}(x);
|
|
|
|
}
|
|
|
|
template SetOfSymbols CollectSymbols(const Expr<SomeType> &);
|
|
|
|
template SetOfSymbols CollectSymbols(const Expr<SomeInteger> &);
|
|
|
|
template SetOfSymbols CollectSymbols(const Expr<SubscriptInteger> &);
|
|
|
|
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|