2018-08-10 20:44:43 +02: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.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_EVALUATE_TOOLS_H_
|
|
|
|
#define FORTRAN_EVALUATE_TOOLS_H_
|
|
|
|
|
|
|
|
#include "expression.h"
|
2018-08-13 22:33:31 +02:00
|
|
|
#include "../common/idioms.h"
|
2018-08-10 20:44:43 +02:00
|
|
|
#include "../parser/message.h"
|
|
|
|
#include <optional>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
2018-08-20 18:29:08 +02:00
|
|
|
// Convenience functions and operator overloadings for expression construction.
|
|
|
|
template<TypeCategory C, int K>
|
|
|
|
Expr<Type<C, K>> operator-(Expr<Type<C, K>> &&x) {
|
|
|
|
return {Negate<Type<C, K>>{std::move(x)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C, int K>
|
|
|
|
Expr<Type<C, K>> operator+(Expr<Type<C, K>> &&x, Expr<Type<C, K>> &&y) {
|
|
|
|
return {Add<Type<C, K>>{std::move(x), std::move(y)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C, int K>
|
|
|
|
Expr<Type<C, K>> operator-(Expr<Type<C, K>> &&x, Expr<Type<C, K>> &&y) {
|
|
|
|
return {Subtract<Type<C, K>>{std::move(x), std::move(y)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C, int K>
|
|
|
|
Expr<Type<C, K>> operator*(Expr<Type<C, K>> &&x, Expr<Type<C, K>> &&y) {
|
|
|
|
return {Multiply<Type<C, K>>{std::move(x), std::move(y)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C, int K>
|
|
|
|
Expr<Type<C, K>> operator/(Expr<Type<C, K>> &&x, Expr<Type<C, K>> &&y) {
|
|
|
|
return {Divide<Type<C, K>>{std::move(x), std::move(y)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C> Expr<SomeKind<C>> operator-(Expr<SomeKind<C>> &&x) {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &xk) { return Expr<SomeKind<C>>{-std::move(xk)}; }, x.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C>
|
|
|
|
Expr<SomeKind<C>> operator+(Expr<SomeKind<C>> &&x, Expr<SomeKind<C>> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &xk, auto &yk) {
|
|
|
|
return Expr<SomeKind<C>>{std::move(xk) + std::move(yk)};
|
|
|
|
},
|
|
|
|
x.u, y.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C>
|
|
|
|
Expr<SomeKind<C>> operator-(Expr<SomeKind<C>> &&x, Expr<SomeKind<C>> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &xk, auto &yk) {
|
|
|
|
return Expr<SomeKind<C>>{std::move(xk) - std::move(yk)};
|
|
|
|
},
|
|
|
|
x.u, y.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C>
|
|
|
|
Expr<SomeKind<C>> operator*(Expr<SomeKind<C>> &&x, Expr<SomeKind<C>> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &xk, auto &yk) {
|
|
|
|
return Expr<SomeKind<C>>{std::move(xk) * std::move(yk)};
|
|
|
|
},
|
|
|
|
x.u, y.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory C>
|
|
|
|
Expr<SomeKind<C>> operator/(Expr<SomeKind<C>> &&x, Expr<SomeKind<C>> &&y) {
|
|
|
|
return std::visit(
|
|
|
|
[](auto &xk, auto &yk) {
|
|
|
|
return Expr<SomeKind<C>>{std::move(xk) / std::move(yk)};
|
|
|
|
},
|
|
|
|
x.u, y.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the second argument expression to an expression of the same type
|
|
|
|
// and kind as that of the first.
|
|
|
|
template<TypeCategory TC, typename F>
|
|
|
|
Expr<SomeKind<TC>> ConvertToTypeAndKindOf(
|
|
|
|
const Expr<SomeKind<TC>> &to, Expr<F> &&from) {
|
|
|
|
return std::visit(
|
|
|
|
[&](const auto &tk) -> Expr<SomeKind<TC>> {
|
|
|
|
using SpecificExpr = std::decay_t<decltype(tk)>;
|
|
|
|
return {SpecificExpr{std::move(from)}};
|
|
|
|
},
|
|
|
|
to.u);
|
|
|
|
}
|
2018-08-10 20:44:43 +02:00
|
|
|
|
|
|
|
// Ensure that both operands of an intrinsic REAL operation or CMPLX()
|
|
|
|
// are INTEGER or REAL, and convert them as necessary to the same REAL type.
|
2018-08-13 22:33:31 +02:00
|
|
|
using ConvertRealOperandsResult =
|
2018-08-15 00:11:53 +02:00
|
|
|
std::optional<std::pair<Expr<SomeReal>, Expr<SomeReal>>>;
|
2018-08-13 22:33:31 +02:00
|
|
|
ConvertRealOperandsResult ConvertRealOperands(
|
2018-08-20 18:29:08 +02:00
|
|
|
parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&);
|
2018-08-10 20:44:43 +02:00
|
|
|
|
2018-08-14 22:39:59 +02:00
|
|
|
template<TypeCategory CAT>
|
|
|
|
void ConvertToSameKind(Expr<SomeKind<CAT>> &x, Expr<SomeKind<CAT>> &y) {
|
|
|
|
std::visit(
|
|
|
|
[&](auto &xk, auto &yk) {
|
|
|
|
using xt = ResultType<decltype(xk)>;
|
|
|
|
using yt = ResultType<decltype(yk)>;
|
|
|
|
if constexpr (xt::kind < yt::kind) {
|
|
|
|
x.u = Expr<yt>{xk};
|
|
|
|
} else if constexpr (xt::kind > yt::kind) {
|
|
|
|
y.u = Expr<xt>{yk};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
x.u, y.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A> Expr<ScalarValueType<A>> ScalarConstantToExpr(const A &x) {
|
|
|
|
return {x};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory CAT, int KIND>
|
|
|
|
Expr<SomeKind<CAT>> ToSomeKindExpr(const Expr<Type<CAT, KIND>> &x) {
|
|
|
|
return Expr<SomeKind<CAT>>{x};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory CAT>
|
|
|
|
Expr<SomeKind<CAT>> SomeKindScalarToExpr(const SomeKindScalar<CAT> &x) {
|
|
|
|
return std::visit(
|
|
|
|
[](const auto &c) { return ToSomeKindExpr(ScalarConstantToExpr(c)); },
|
|
|
|
x.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr<SomeType> GenericScalarToExpr(const Scalar<SomeType> &);
|
|
|
|
|
|
|
|
template<TypeCategory CAT>
|
|
|
|
Expr<SomeType> ToGenericExpr(const Expr<SomeKind<CAT>> &x) {
|
|
|
|
return Expr<SomeType>{x};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<TypeCategory CAT>
|
|
|
|
Expr<SomeType> ToGenericExpr(Expr<SomeKind<CAT>> &&x) {
|
|
|
|
return Expr<SomeType>{std::move(x)};
|
|
|
|
}
|
|
|
|
|
2018-08-10 20:44:43 +02:00
|
|
|
} // namespace Fortran::evaluate
|
|
|
|
#endif // FORTRAN_EVALUATE_TOOLS_H_
|