9#ifndef FORTRAN_PARSER_TOOLS_H_
10#define FORTRAN_PARSER_TOOLS_H_
12#include "parse-tree.h"
19const Name &GetLastName(
const Name &);
20const Name &GetLastName(
const StructureComponent &);
21const Name &GetLastName(
const DataRef &);
22const Name &GetLastName(
const Substring &);
23const Name &GetLastName(
const Designator &);
24const Name &GetLastName(
const ProcComponentRef &);
25const Name &GetLastName(
const ProcedureDesignator &);
26const Name &GetLastName(
const Call &);
27const Name &GetLastName(
const FunctionReference &);
28const Name &GetLastName(
const Variable &);
29const Name &GetLastName(
const AllocateObject &);
33const Name &GetFirstName(
const Name &);
34const Name &GetFirstName(
const StructureComponent &);
35const Name &GetFirstName(
const DataRef &);
36const Name &GetFirstName(
const Substring &);
37const Name &GetFirstName(
const Designator &);
38const Name &GetFirstName(
const ProcComponentRef &);
39const Name &GetFirstName(
const ProcedureDesignator &);
40const Name &GetFirstName(
const Call &);
41const Name &GetFirstName(
const FunctionReference &);
42const Name &GetFirstName(
const Variable &);
43const Name &GetFirstName(
const EntityDecl &);
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)));
122const CoindexedNamedObject *GetCoindexedNamedObject(
const AllocateObject &);
123const CoindexedNamedObject *GetCoindexedNamedObject(
const DataRef &);
124const CoindexedNamedObject *GetCoindexedNamedObject(
const Designator &);
125const CoindexedNamedObject *GetCoindexedNamedObject(
const Variable &);
128template <
typename A,
typename =
int>
struct HasSource : std::false_type {};
130struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
134template <
typename A,
typename =
int>
struct HasTypedExpr : std::false_type {};
143 using Result = std::optional<CharBlock>;
145 template <
typename A>
static Result GetSource(A *p) {
147 return GetSource(*p);
152 template <
typename A>
154 return GetSource(x.value());
157 template <
typename A,
bool COPY>
159 return GetSource(x.value());
162 template <
typename... As>
163 static Result GetSource(
const std::variant<As...> &x) {
164 return common::visit([](
const auto &y) {
return GetSource(y); }, x);
167 template <std::size_t J = 0,
typename... As>
168 static Result GetSource(
const std::tuple<As...> &x) {
169 if constexpr (J <
sizeof...(As)) {
170 constexpr std::size_t index{GET_FIRST ? J :
sizeof...(As) - J - 1};
171 if (
auto result{GetSource(std::get<index>(x))}) {
174 return GetSource<(J + 1)>(x);
180 template <
typename A>
static Result GetSource(
const std::optional<A> &o) {
182 return GetSource(*o);
188 template <
typename A>
static Result GetSource(
const std::list<A> &x) {
189 if constexpr (GET_FIRST) {
190 for (
const A &y : x) {
191 if (
auto result{GetSource(y)}) {
196 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
197 if (
auto result{GetSource(*iter)}) {
205 template <
typename A>
static Result GetSource(
const std::vector<A> &x) {
206 if constexpr (GET_FIRST) {
207 for (
const A &y : x) {
208 if (
auto result{GetSource(y)}) {
213 for (
auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
214 if (
auto result{GetSource(*iter)}) {
222 template <
typename A>
static Result GetSource(A &x) {
225 }
else if constexpr (ConstraintTrait<A>) {
226 return GetSource(x.thing);
227 }
else if constexpr (WrapperTrait<A>) {
228 return GetSource(x.v);
229 }
else if constexpr (UnionTrait<A>) {
230 return GetSource(x.u);
231 }
else if constexpr (TupleTrait<A>) {
232 return GetSource(x.t);
239template <
typename A> std::optional<CharBlock> GetSource(
const A &x) {
242template <
typename A> std::optional<CharBlock> GetSource(A &x) {
243 return GetSourceHelper<true>::GetSource(
const_cast<const A &
>(x));
246template <
typename A> std::optional<CharBlock> GetLastSource(
const A &x) {
247 return GetSourceHelper<false>::GetSource(x);
249template <
typename A> std::optional<CharBlock> GetLastSource(A &x) {
250 return GetSourceHelper<false>::GetSource(
const_cast<const A &
>(x));
Definition: indirection.h:31
Definition: check-expression.h:19
Definition: parse-tree.h:355
Definition: parse-tree.h:350