FLANG
tools.h
1//===-- include/flang/Parser/tools.h ----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_PARSER_TOOLS_H_
10#define FORTRAN_PARSER_TOOLS_H_
11
12#include "parse-tree.h"
13
14namespace Fortran::parser {
15
16// GetLastName() isolates and returns a reference to the rightmost Name
17// in a variable (i.e., the Name whose symbol's type determines the type
18// of the variable or expression).
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 &);
30
31// GetFirstName() isolates and returns a reference to the leftmost Name
32// in a variable or entity declaration.
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 &);
44
45// When a parse tree node is an instance of a specific type wrapped in
46// layers of packaging, return a pointer to that object.
47// Implemented with mutually recursive template functions that are
48// wrapped in a struct to avoid prototypes.
50
51 template <typename A, typename B> static const A *Unwrap(B *p) {
52 if (p) {
53 return Unwrap<A>(*p);
54 } else {
55 return nullptr;
56 }
57 }
58
59 template <typename A, typename B, bool COPY>
60 static const A *Unwrap(const common::Indirection<B, COPY> &x) {
61 return Unwrap<A>(x.value());
62 }
63
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);
67 }
68
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))}) {
73 return result;
74 }
75 return Unwrap<A, (J + 1)>(x);
76 } else {
77 return nullptr;
78 }
79 }
80
81 template <typename A, typename B>
82 static const A *Unwrap(const std::optional<B> &o) {
83 if (o) {
84 return Unwrap<A>(*o);
85 } else {
86 return nullptr;
87 }
88 }
89
90 template <typename A, typename B>
91 static const A *Unwrap(const UnlabeledStatement<B> &x) {
92 return Unwrap<A>(x.statement);
93 }
94 template <typename A, typename B>
95 static const A *Unwrap(const Statement<B> &x) {
96 return Unwrap<A>(x.statement);
97 }
98
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>>) {
101 return &x;
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);
108 } else {
109 return nullptr;
110 }
111 }
112};
113
114template <typename A, typename B> const A *Unwrap(const B &x) {
115 return UnwrapperHelper::Unwrap<A>(x);
116}
117template <typename A, typename B> A *Unwrap(B &x) {
118 return const_cast<A *>(Unwrap<A, B>(const_cast<const B &>(x)));
119}
120template <typename A, typename B> const A &UnwrapRef(const B &x) {
121 return DEREF(Unwrap<A>(x));
122}
123template <typename A, typename B> A &UnwrapRef(B &x) {
124 return DEREF(Unwrap<A>(x));
125}
126
127// Get the CoindexedNamedObject if the entity is a coindexed object.
128const CoindexedNamedObject *GetCoindexedNamedObject(const AllocateObject &);
129const CoindexedNamedObject *GetCoindexedNamedObject(const DataRef &);
130const CoindexedNamedObject *GetCoindexedNamedObject(const Designator &);
131const CoindexedNamedObject *GetCoindexedNamedObject(const Variable &);
132
133// Detects parse tree nodes with "source" members.
134template <typename A, typename = int> struct HasSource : std::false_type {};
135template <typename A>
136struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
137 : std::true_type {};
138
139// Detects parse tree nodes with "typedExpr" members.
140template <typename A, typename = int> struct HasTypedExpr : std::false_type {};
141template <typename A>
142struct HasTypedExpr<A, decltype(static_cast<void>(A::typedExpr), 0)>
143 : std::true_type {};
144
145// GetSource()
146
147template <bool GET_FIRST> struct GetSourceHelper {
148
149 using Result = std::optional<CharBlock>;
150
151 template <typename A> static Result GetSource(A *p) {
152 if (p) {
153 return GetSource(*p);
154 } else {
155 return std::nullopt;
156 }
157 }
158 template <typename A>
159 static Result GetSource(const common::Indirection<A> &x) {
160 return GetSource(x.value());
161 }
162
163 template <typename A, bool COPY>
164 static Result GetSource(const common::Indirection<A, COPY> &x) {
165 return GetSource(x.value());
166 }
167
168 template <typename... As>
169 static Result GetSource(const std::variant<As...> &x) {
170 return common::visit([](const auto &y) { return GetSource(y); }, x);
171 }
172
173 template <std::size_t J = 0, typename... As>
174 static Result GetSource(const std::tuple<As...> &x) {
175 if constexpr (J < sizeof...(As)) {
176 constexpr std::size_t index{GET_FIRST ? J : sizeof...(As) - J - 1};
177 if (auto result{GetSource(std::get<index>(x))}) {
178 return result;
179 }
180 return GetSource<(J + 1)>(x);
181 } else {
182 return {};
183 }
184 }
185
186 template <typename A> static Result GetSource(const std::optional<A> &o) {
187 if (o) {
188 return GetSource(*o);
189 } else {
190 return {};
191 }
192 }
193
194 template <typename A> static Result GetSource(const std::list<A> &x) {
195 if constexpr (GET_FIRST) {
196 for (const A &y : x) {
197 if (auto result{GetSource(y)}) {
198 return result;
199 }
200 }
201 } else {
202 for (auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
203 if (auto result{GetSource(*iter)}) {
204 return result;
205 }
206 }
207 }
208 return {};
209 }
210
211 template <typename A> static Result GetSource(const std::vector<A> &x) {
212 if constexpr (GET_FIRST) {
213 for (const A &y : x) {
214 if (auto result{GetSource(y)}) {
215 return result;
216 }
217 }
218 } else {
219 for (auto iter{x.rbegin()}; iter != x.rend(); ++iter) {
220 if (auto result{GetSource(*iter)}) {
221 return result;
222 }
223 }
224 }
225 return {};
226 }
227
228 template <typename A> static Result GetSource(A &x) {
229 if constexpr (HasSource<A>::value) {
230 return x.source;
231 } else if constexpr (ConstraintTrait<A>) {
232 return GetSource(x.thing);
233 } else if constexpr (WrapperTrait<A>) {
234 return GetSource(x.v);
235 } else if constexpr (UnionTrait<A>) {
236 return GetSource(x.u);
237 } else if constexpr (TupleTrait<A>) {
238 return GetSource(x.t);
239 } else {
240 return {};
241 }
242 }
243};
244
245template <typename A> std::optional<CharBlock> GetSource(const A &x) {
246 return GetSourceHelper<true>::GetSource(x);
247}
248template <typename A> std::optional<CharBlock> GetSource(A &x) {
249 return GetSourceHelper<true>::GetSource(const_cast<const A &>(x));
250}
251
252template <typename A> std::optional<CharBlock> GetLastSource(const A &x) {
253 return GetSourceHelper<false>::GetSource(x);
254}
255template <typename A> std::optional<CharBlock> GetLastSource(A &x) {
256 return GetSourceHelper<false>::GetSource(const_cast<const A &>(x));
257}
258
259// Checks whether the assignment statement has a single variable on the RHS.
260bool CheckForSingleVariableOnRHS(const AssignmentStmt &);
261
262const Name *GetDesignatorNameIfDataRef(const Designator &);
263
264} // namespace Fortran::parser
265#endif // FORTRAN_PARSER_TOOLS_H_
Definition indirection.h:31
Definition check-expression.h:19
Definition parse-tree.h:1925
Definition parse-tree.h:2018
Definition parse-tree.h:3261
Definition parse-tree.h:1904
Definition parse-tree.h:1820
Definition parse-tree.h:1859
Definition parse-tree.h:1404
Definition parse-tree.h:3266
Definition tools.h:147
Definition tools.h:134
Definition tools.h:140
Definition parse-tree.h:581
Definition parse-tree.h:1899
Definition parse-tree.h:3233
Definition parse-tree.h:356
Definition parse-tree.h:1889
Definition parse-tree.h:1837
Definition parse-tree.h:351
Definition parse-tree.h:1867