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 &);
52 template <
typename A,
typename B>
static const A *Unwrap(B *p) {
60 template <
typename A,
typename B,
bool COPY>
62 return Unwrap<A>(x.value());
65 template <
typename A,
typename... Bs>
66 static const A *Unwrap(
const std::variant<Bs...> &x) {
67 return common::visit([](
const auto &y) {
return Unwrap<A>(y); }, x);
70 template <
typename A, std::size_t J = 0,
typename... Bs>
71 static const A *Unwrap(
const std::tuple<Bs...> &x) {
72 if constexpr (J <
sizeof...(Bs)) {
73 if (
auto result{Unwrap<A>(std::get<J>(x))}) {
76 return Unwrap<A, (J + 1)>(x);
82 template <
typename A,
typename B>
83 static const A *Unwrap(
const std::optional<B> &o) {
91 template <
typename A,
typename B>
93 return Unwrap<A>(x.statement);
95 template <
typename A,
typename B>
97 return Unwrap<A>(x.statement);
100 template <
typename A,
typename B>
static const A *Unwrap(B &x) {
101 if constexpr (std::is_same_v<std::decay_t<A>, std::decay_t<B>>) {
103 }
else if constexpr (ConstraintTrait<B>) {
104 return Unwrap<A>(x.thing);
105 }
else if constexpr (WrapperTrait<B>) {
106 return Unwrap<A>(x.v);
107 }
else if constexpr (UnionTrait<B>) {
108 return Unwrap<A>(x.u);
115template <
typename A,
typename B>
const A *Unwrap(
const B &x) {
116 return UnwrapperHelper::Unwrap<A>(x);
118template <
typename A,
typename B> A *Unwrap(B &x) {
119 return const_cast<A *
>(Unwrap<A, B>(
const_cast<const B &
>(x)));
121template <
typename A,
typename B>
const A &UnwrapRef(
const B &x) {
122 return DEREF(Unwrap<A>(x));
124template <
typename A,
typename B> A &UnwrapRef(B &x) {
125 return DEREF(Unwrap<A>(x));
135template <
typename A,
typename =
int>
struct HasSource : std::false_type {};
137struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
141template <
typename A,
typename =
int>
struct HasTypedExpr : std::false_type {};
145template <
typename A,
typename =
int>
struct HasTypedCall : std::false_type {};
149template <
typename A,
typename =
int>
159 using Result = std::optional<CharBlock>;
161 template <
typename A>
static Result GetSource(A *p) {
163 return GetSource(*p);
168 template <
typename A>
170 return GetSource(x.value());
173 template <
typename A,
bool COPY>
175 return GetSource(x.value());
178 template <
typename... As>
179 static Result GetSource(
const std::variant<As...> &x) {
180 return common::visit([](
const auto &y) {
return GetSource(y); }, x);
183 template <std::size_t J = 0,
typename... As>
184 static Result GetSource(
const std::tuple<As...> &x) {
185 if constexpr (J <
sizeof...(As)) {
186 constexpr std::size_t index{GET_FIRST ? J :
sizeof...(As) - J - 1};
187 if (
auto result{GetSource(std::get<index>(x))}) {
190 return GetSource<(J + 1)>(x);
196 template <
typename A>
static Result GetSource(
const std::optional<A> &o) {
198 return GetSource(*o);
204 template <
typename A>
static Result GetSource(
const std::list<A> &x) {
205 if constexpr (GET_FIRST) {
206 for (
const A &y : x) {
207 if (
auto result{GetSource(y)}) {
212 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
213 if (
auto result{GetSource(*iter)}) {
221 template <
typename A>
static Result GetSource(
const std::vector<A> &x) {
222 if constexpr (GET_FIRST) {
223 for (
const A &y : x) {
224 if (
auto result{GetSource(y)}) {
229 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
230 if (
auto result{GetSource(*iter)}) {
238 template <
typename A>
static Result GetSource(A &x) {
241 }
else if constexpr (ConstraintTrait<A>) {
242 return GetSource(x.thing);
243 }
else if constexpr (WrapperTrait<A>) {
244 return GetSource(x.v);
245 }
else if constexpr (UnionTrait<A>) {
246 return GetSource(x.u);
247 }
else if constexpr (TupleTrait<A>) {
248 return GetSource(x.t);
255template <
typename A> std::optional<CharBlock> GetSource(
const A &x) {
256 return GetSourceHelper<true>::GetSource(x);
258template <
typename A> std::optional<CharBlock> GetSource(A &x) {
259 return GetSourceHelper<true>::GetSource(
const_cast<const A &
>(x));
262template <
typename A> std::optional<CharBlock> GetLastSource(
const A &x) {
263 return GetSourceHelper<false>::GetSource(x);
265template <
typename A> std::optional<CharBlock> GetLastSource(A &x) {
266 return GetSourceHelper<false>::GetSource(
const_cast<const A &
>(x));
276 static constexpr bool value{
false};
279 static constexpr bool value{
true};
284std::optional<Label> GetFinalLabel(
const Block &);
Definition indirection.h:31
Definition check-expression.h:19
Definition parse-tree.h:5498
Definition parse-tree.h:1912
Definition parse-tree.h:2005
Definition parse-tree.h:3278
Definition parse-tree.h:1892
Definition parse-tree.h:1808
Definition parse-tree.h:1847
Definition parse-tree.h:1382
Definition parse-tree.h:556
Definition parse-tree.h:3283
Definition parse-tree.h:589
Definition parse-tree.h:5777
Definition parse-tree.h:5473
Definition parse-tree.h:1887
Definition parse-tree.h:3222
Definition parse-tree.h:361
Definition parse-tree.h:1877
Definition parse-tree.h:1825
Definition parse-tree.h:356
Definition parse-tree.h:1855