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
14 namespace 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).
19 const Name &GetLastName(const Name &);
20 const Name &GetLastName(const StructureComponent &);
21 const Name &GetLastName(const DataRef &);
22 const Name &GetLastName(const Substring &);
23 const Name &GetLastName(const Designator &);
24 const Name &GetLastName(const ProcComponentRef &);
25 const Name &GetLastName(const ProcedureDesignator &);
26 const Name &GetLastName(const Call &);
27 const Name &GetLastName(const FunctionReference &);
28 const Name &GetLastName(const Variable &);
29 const Name &GetLastName(const AllocateObject &);
30
31 // GetFirstName() isolates and returns a reference to the leftmost Name
32 // in a variable.
33 const Name &GetFirstName(const Name &);
34 const Name &GetFirstName(const StructureComponent &);
35 const Name &GetFirstName(const DataRef &);
36 const Name &GetFirstName(const Substring &);
37 const Name &GetFirstName(const Designator &);
38 const Name &GetFirstName(const ProcComponentRef &);
39 const Name &GetFirstName(const ProcedureDesignator &);
40 const Name &GetFirstName(const Call &);
41 const Name &GetFirstName(const FunctionReference &);
42 const Name &GetFirstName(const Variable &);
43
44 // When a parse tree node is an instance of a specific type wrapped in
45 // layers of packaging, return a pointer to that object.
46 // Implemented with mutually recursive template functions that are
47 // wrapped in a struct to avoid prototypes.
48 struct UnwrapperHelper {
49
50 template <typename A, typename B> static const A *Unwrap(B *p) {
51 if (p) {
52 return Unwrap<A>(*p);
53 } else {
54 return nullptr;
55 }
56 }
57
58 template <typename A, typename B, bool COPY>
59 static const A *Unwrap(const common::Indirection<B, COPY> &x) {
60 return Unwrap<A>(x.value());
61 }
62
63 template <typename A, typename... Bs>
64 static const A *Unwrap(const std::variant<Bs...> &x) {
65 return std::visit([](const auto &y) { return Unwrap<A>(y); }, x);
66 }
67
68 template <typename A, typename B>
69 static const A *Unwrap(const std::optional<B> &o) {
70 if (o) {
71 return Unwrap<A>(*o);
72 } else {
73 return nullptr;
74 }
75 }
76
77 template <typename A, typename B> static const A *Unwrap(B &x) {
78 if constexpr (std::is_same_v<std::decay_t<A>, std::decay_t<B>>) {
79 return &x;
80 } else if constexpr (ConstraintTrait<B>) {
81 return Unwrap<A>(x.thing);
82 } else if constexpr (WrapperTrait<B>) {
83 return Unwrap<A>(x.v);
84 } else if constexpr (UnionTrait<B>) {
85 return Unwrap<A>(x.u);
86 } else {
87 return nullptr;
88 }
89 }
90 };
91
92 template <typename A, typename B> const A *Unwrap(const B &x) {
93 return UnwrapperHelper::Unwrap<A>(x);
94 }
95 template <typename A, typename B> A *Unwrap(B &x) {
96 return const_cast<A *>(Unwrap<A, B>(const_cast<const B &>(x)));
97 }
98
99 // Get the CoindexedNamedObject if the entity is a coindexed object.
100 const CoindexedNamedObject *GetCoindexedNamedObject(const AllocateObject &);
101 const CoindexedNamedObject *GetCoindexedNamedObject(const DataRef &);
102 const CoindexedNamedObject *GetCoindexedNamedObject(const Designator &);
103 const CoindexedNamedObject *GetCoindexedNamedObject(const Variable &);
104
105 // Detects parse tree nodes with "source" members.
106 template <typename A, typename = int> struct HasSource : std::false_type {};
107 template <typename A>
108 struct HasSource<A, decltype(static_cast<void>(A::source), 0)>
109 : std::true_type {};
110
111 } // namespace Fortran::parser
112 #endif // FORTRAN_PARSER_TOOLS_H_
Fortran::parser::HasSource
Definition: tools.h:106
Fortran::parser::UnwrapperHelper
Definition: tools.h:48
Fortran::common::Indirection
Definition: indirection.h:31
Fortran::parser
Definition: check-expression.h:19