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 &);
51 template <
typename A,
typename B>
static const A *Unwrap(B *p) {
59 template <
typename A,
typename B,
bool COPY>
61 return Unwrap<A>(x.value());
64 template <
typename A,
typename... Bs>
65 static const A *Unwrap(
const std::variant<Bs...> &x) {
66 return common::visit([](
const auto &y) {
return Unwrap<A>(y); }, x);
69 template <
typename A, std::size_t J = 0,
typename... Bs>
70 static const A *Unwrap(
const std::tuple<Bs...> &x) {
71 if constexpr (J <
sizeof...(Bs)) {
72 if (
auto result{Unwrap<A>(std::get<J>(x))}) {
75 return Unwrap<A, (J + 1)>(x);
81 template <
typename A,
typename B>
82 static const A *Unwrap(
const std::optional<B> &o) {
90 template <
typename A,
typename B>
92 return Unwrap<A>(x.statement);
94 template <
typename A,
typename B>
96 return Unwrap<A>(x.statement);
99 template <
typename A,
typename B>
static const A *Unwrap(B &x) {
100 if constexpr (std::is_same_v<std::decay_t<A>, std::decay_t<B>>) {
102 }
else if constexpr (ConstraintTrait<B>) {
103 return Unwrap<A>(x.thing);
104 }
else if constexpr (WrapperTrait<B>) {
105 return Unwrap<A>(x.v);
106 }
else if constexpr (UnionTrait<B>) {
107 return Unwrap<A>(x.u);
114template <
typename A,
typename B>
const A *Unwrap(
const B &x) {
115 return UnwrapperHelper::Unwrap<A>(x);
117template <
typename A,
typename B> A *Unwrap(B &x) {
118 return const_cast<A *
>(Unwrap<A, B>(
const_cast<const B &
>(x)));
120template <
typename A,
typename B>
const A &UnwrapRef(
const B &x) {
121 return DEREF(Unwrap<A>(x));
123template <
typename A,
typename B> A &UnwrapRef(B &x) {
124 return DEREF(Unwrap<A>(x));
134template <
typename A,
typename =
int>
struct HasSource : std::false_type {};
136struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
140template <
typename A,
typename =
int>
struct HasTypedExpr : std::false_type {};
144template <
typename A,
typename =
int>
struct HasTypedCall : std::false_type {};
148template <
typename A,
typename =
int>
158 using Result = std::optional<CharBlock>;
160 template <
typename A>
static Result GetSource(A *p) {
162 return GetSource(*p);
167 template <
typename A>
169 return GetSource(x.value());
172 template <
typename A,
bool COPY>
174 return GetSource(x.value());
177 template <
typename... As>
178 static Result GetSource(
const std::variant<As...> &x) {
179 return common::visit([](
const auto &y) {
return GetSource(y); }, x);
182 template <std::size_t J = 0,
typename... As>
183 static Result GetSource(
const std::tuple<As...> &x) {
184 if constexpr (J <
sizeof...(As)) {
185 constexpr std::size_t index{GET_FIRST ? J :
sizeof...(As) - J - 1};
186 if (
auto result{GetSource(std::get<index>(x))}) {
189 return GetSource<(J + 1)>(x);
195 template <
typename A>
static Result GetSource(
const std::optional<A> &o) {
197 return GetSource(*o);
203 template <
typename A>
static Result GetSource(
const std::list<A> &x) {
204 if constexpr (GET_FIRST) {
205 for (
const A &y : x) {
206 if (
auto result{GetSource(y)}) {
211 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
212 if (
auto result{GetSource(*iter)}) {
220 template <
typename A>
static Result GetSource(
const std::vector<A> &x) {
221 if constexpr (GET_FIRST) {
222 for (
const A &y : x) {
223 if (
auto result{GetSource(y)}) {
228 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
229 if (
auto result{GetSource(*iter)}) {
237 template <
typename A>
static Result GetSource(A &x) {
240 }
else if constexpr (ConstraintTrait<A>) {
241 return GetSource(x.thing);
242 }
else if constexpr (WrapperTrait<A>) {
243 return GetSource(x.v);
244 }
else if constexpr (UnionTrait<A>) {
245 return GetSource(x.u);
246 }
else if constexpr (TupleTrait<A>) {
247 return GetSource(x.t);
254template <
typename A> std::optional<CharBlock> GetSource(
const A &x) {
255 return GetSourceHelper<true>::GetSource(x);
257template <
typename A> std::optional<CharBlock> GetSource(A &x) {
258 return GetSourceHelper<true>::GetSource(
const_cast<const A &
>(x));
261template <
typename A> std::optional<CharBlock> GetLastSource(
const A &x) {
262 return GetSourceHelper<false>::GetSource(x);
264template <
typename A> std::optional<CharBlock> GetLastSource(A &x) {
265 return GetSourceHelper<false>::GetSource(
const_cast<const A &
>(x));
Definition indirection.h:31
Definition check-expression.h:19
Definition parse-tree.h:1936
Definition parse-tree.h:2029
Definition parse-tree.h:3271
Definition parse-tree.h:1915
Definition parse-tree.h:1831
Definition parse-tree.h:1870
Definition parse-tree.h:1410
Definition parse-tree.h:3276
Definition parse-tree.h:587
Definition parse-tree.h:1910
Definition parse-tree.h:3243
Definition parse-tree.h:359
Definition parse-tree.h:1900
Definition parse-tree.h:1848
Definition parse-tree.h:354
Definition parse-tree.h:1878