9#ifndef FORTRAN_PARSER_TOOLS_H_
10#define FORTRAN_PARSER_TOOLS_H_
12#include "parse-tree.h"
19const Name &GetLastName(
const Name &);
26const Name &GetLastName(
const Call &);
33const Name &GetFirstName(
const Name &);
40const Name &GetFirstName(
const Call &);
48bool IsBOZLiteral(
const ScalarIntConstantExpr &);
56 template <
typename A,
typename B>
static const A *Unwrap(B *p) {
64 template <
typename A,
typename B,
bool COPY>
66 return Unwrap<A>(x.value());
69 template <
typename A,
typename... Bs>
70 static const A *Unwrap(
const std::variant<Bs...> &x) {
71 return common::visit([](
const auto &y) {
return Unwrap<A>(y); }, x);
74 template <
typename A, std::size_t J = 0,
typename... Bs>
75 static const A *Unwrap(
const std::tuple<Bs...> &x) {
76 if constexpr (J <
sizeof...(Bs)) {
77 if (
auto result{Unwrap<A>(std::get<J>(x))}) {
80 return Unwrap<A, (J + 1)>(x);
86 template <
typename A,
typename B>
87 static const A *Unwrap(
const std::optional<B> &o) {
95 template <
typename A,
typename B>
97 return Unwrap<A>(x.statement);
99 template <
typename A,
typename B>
101 return Unwrap<A>(x.statement);
104 template <
typename A,
typename B>
static const A *Unwrap(B &x) {
105 if constexpr (std::is_same_v<std::decay_t<A>, std::decay_t<B>>) {
107 }
else if constexpr (ConstraintTrait<B>) {
108 return Unwrap<A>(x.thing);
109 }
else if constexpr (WrapperTrait<B>) {
110 return Unwrap<A>(x.v);
111 }
else if constexpr (UnionTrait<B>) {
112 return Unwrap<A>(x.u);
119template <
typename A,
typename B>
const A *Unwrap(
const B &x) {
120 return UnwrapperHelper::Unwrap<A>(x);
122template <
typename A,
typename B> A *Unwrap(B &x) {
123 return const_cast<A *
>(Unwrap<A, B>(
const_cast<const B &
>(x)));
125template <
typename A,
typename B>
const A &UnwrapRef(
const B &x) {
126 return DEREF(Unwrap<A>(x));
128template <
typename A,
typename B> A &UnwrapRef(B &x) {
129 return DEREF(Unwrap<A>(x));
139template <
typename A,
typename =
int>
struct HasSource : std::false_type {};
141struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
145template <
typename A,
typename =
int>
struct HasTypedExpr : std::false_type {};
149template <
typename A,
typename =
int>
struct HasTypedCall : std::false_type {};
153template <
typename A,
typename =
int>
163 using Result = std::optional<CharBlock>;
165 template <
typename A>
static Result GetSource(A *p) {
167 return GetSource(*p);
172 template <
typename A>
174 return GetSource(x.value());
177 template <
typename A,
bool COPY>
179 return GetSource(x.value());
182 template <
typename... As>
183 static Result GetSource(
const std::variant<As...> &x) {
184 return common::visit([](
const auto &y) {
return GetSource(y); }, x);
187 template <std::size_t J = 0,
typename... As>
188 static Result GetSource(
const std::tuple<As...> &x) {
189 if constexpr (J <
sizeof...(As)) {
190 constexpr std::size_t index{GET_FIRST ? J :
sizeof...(As) - J - 1};
191 if (
auto result{GetSource(std::get<index>(x))}) {
194 return GetSource<(J + 1)>(x);
200 template <
typename A>
static Result GetSource(
const std::optional<A> &o) {
202 return GetSource(*o);
208 template <
typename A>
static Result GetSource(
const std::list<A> &x) {
209 if constexpr (GET_FIRST) {
210 for (
const A &y : x) {
211 if (
auto result{GetSource(y)}) {
216 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
217 if (
auto result{GetSource(*iter)}) {
225 template <
typename A>
static Result GetSource(
const std::vector<A> &x) {
226 if constexpr (GET_FIRST) {
227 for (
const A &y : x) {
228 if (
auto result{GetSource(y)}) {
233 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
234 if (
auto result{GetSource(*iter)}) {
242 template <
typename A>
static Result GetSource(A &x) {
245 }
else if constexpr (ConstraintTrait<A>) {
246 return GetSource(x.thing);
247 }
else if constexpr (WrapperTrait<A>) {
248 return GetSource(x.v);
249 }
else if constexpr (UnionTrait<A>) {
250 return GetSource(x.u);
251 }
else if constexpr (TupleTrait<A>) {
252 return GetSource(x.t);
259template <
typename A> std::optional<CharBlock> GetSource(
const A &x) {
260 return GetSourceHelper<true>::GetSource(x);
262template <
typename A> std::optional<CharBlock> GetSource(A &x) {
263 return GetSourceHelper<true>::GetSource(
const_cast<const A &
>(x));
266template <
typename A> std::optional<CharBlock> GetLastSource(
const A &x) {
267 return GetSourceHelper<false>::GetSource(x);
269template <
typename A> std::optional<CharBlock> GetLastSource(A &x) {
270 return GetSourceHelper<false>::GetSource(
const_cast<const A &
>(x));
280 static constexpr bool value{
false};
283 static constexpr bool value{
true};
288std::optional<Label> GetFinalLabel(
const Block &);
Definition indirection.h:31
Definition check-expression.h:19
Definition parse-tree.h:5578
Definition parse-tree.h:1954
Definition parse-tree.h:2047
Definition parse-tree.h:3328
Definition parse-tree.h:1934
Definition parse-tree.h:1850
Definition parse-tree.h:1889
Definition parse-tree.h:1424
Definition parse-tree.h:558
Definition parse-tree.h:3333
Definition parse-tree.h:591
Definition parse-tree.h:5857
Definition parse-tree.h:5553
Definition parse-tree.h:1929
Definition parse-tree.h:3272
Definition parse-tree.h:361
Definition parse-tree.h:1919
Definition parse-tree.h:1867
Definition parse-tree.h:356
Definition parse-tree.h:1897