|
14 | 14 | #define FORTRAN_PARSER_OPENMP_UTILS_H |
15 | 15 |
|
16 | 16 | #include "flang/Common/indirection.h" |
| 17 | +#include "flang/Common/template.h" |
17 | 18 | #include "flang/Parser/parse-tree.h" |
18 | 19 | #include "llvm/Frontend/OpenMP/OMP.h" |
19 | 20 |
|
@@ -127,7 +128,88 @@ template <typename T> struct IsStatement<Statement<T>> { |
127 | 128 | std::optional<Label> GetStatementLabel(const ExecutionPartConstruct &x); |
128 | 129 | std::optional<Label> GetFinalLabel(const OpenMPConstruct &x); |
129 | 130 |
|
| 131 | +namespace detail { |
| 132 | +// Clauses with flangClass = "OmpObjectList". |
| 133 | +using MemberObjectListClauses = |
| 134 | + std::tuple<OmpClause::Copyin, OmpClause::Copyprivate, OmpClause::Exclusive, |
| 135 | + OmpClause::Firstprivate, OmpClause::HasDeviceAddr, OmpClause::Inclusive, |
| 136 | + OmpClause::IsDevicePtr, OmpClause::Link, OmpClause::Private, |
| 137 | + OmpClause::Shared, OmpClause::UseDeviceAddr, OmpClause::UseDevicePtr>; |
| 138 | + |
| 139 | +// Clauses with flangClass = "OmpSomeClause", and OmpObjectList a |
| 140 | +// member of tuple OmpSomeClause::t. |
| 141 | +using TupleObjectListClauses = std::tuple<OmpClause::AdjustArgs, |
| 142 | + OmpClause::Affinity, OmpClause::Aligned, OmpClause::Allocate, |
| 143 | + OmpClause::Enter, OmpClause::From, OmpClause::InReduction, |
| 144 | + OmpClause::Lastprivate, OmpClause::Linear, OmpClause::Map, |
| 145 | + OmpClause::Reduction, OmpClause::TaskReduction, OmpClause::To>; |
| 146 | + |
| 147 | +// Does U have WrapperTrait (i.e. has a member 'v'), and if so, is T the |
| 148 | +// type of v? |
| 149 | +template <typename T, typename U, bool IsWrapper> struct WrappedInType { |
| 150 | + static constexpr bool value{false}; |
| 151 | +}; |
| 152 | + |
| 153 | +template <typename T, typename U> struct WrappedInType<T, U, true> { |
| 154 | + static constexpr bool value{std::is_same_v<T, decltype(U::v)>}; |
| 155 | +}; |
| 156 | + |
| 157 | +// Same as WrappedInType, but with a list of types Us. Satisfied if any |
| 158 | +// type U in Us satisfies WrappedInType<T, U>. |
| 159 | +template <typename...> struct WrappedInTypes; |
| 160 | + |
| 161 | +template <typename T> struct WrappedInTypes<T> { |
| 162 | + static constexpr bool value{false}; |
| 163 | +}; |
| 164 | + |
| 165 | +template <typename T, typename U, typename... Us> |
| 166 | +struct WrappedInTypes<T, U, Us...> { |
| 167 | + static constexpr bool value{WrappedInType<T, U, WrapperTrait<U>>::value || |
| 168 | + WrappedInTypes<T, Us...>::value}; |
| 169 | +}; |
| 170 | + |
| 171 | +// Same as WrappedInTypes, but takes type list in a form of a tuple or |
| 172 | +// a variant. |
| 173 | +template <typename...> struct WrappedInTupleOrVariant { |
| 174 | + static constexpr bool value{false}; |
| 175 | +}; |
| 176 | +template <typename T, typename... Us> |
| 177 | +struct WrappedInTupleOrVariant<T, std::tuple<Us...>> { |
| 178 | + static constexpr bool value{WrappedInTypes<T, Us...>::value}; |
| 179 | +}; |
| 180 | +template <typename T, typename... Us> |
| 181 | +struct WrappedInTupleOrVariant<T, std::variant<Us...>> { |
| 182 | + static constexpr bool value{WrappedInTypes<T, Us...>::value}; |
| 183 | +}; |
| 184 | +template <typename T, typename U> |
| 185 | +constexpr bool WrappedInTupleOrVariantV{WrappedInTupleOrVariant<T, U>::value}; |
| 186 | +} // namespace detail |
| 187 | + |
| 188 | +template <typename T> const OmpObjectList *GetOmpObjectList(const T &clause) { |
| 189 | + using namespace detail; |
| 190 | + static_assert(std::is_class_v<T>, "Unexpected argument type"); |
| 191 | + |
| 192 | + if constexpr (common::HasMember<T, decltype(OmpClause::u)>) { |
| 193 | + if constexpr (common::HasMember<T, MemberObjectListClauses>) { |
| 194 | + return &clause.v; |
| 195 | + } else if constexpr (common::HasMember<T, TupleObjectListClauses>) { |
| 196 | + return &std::get<OmpObjectList>(clause.v.t); |
| 197 | + } else { |
| 198 | + return nullptr; |
| 199 | + } |
| 200 | + } else if constexpr (WrappedInTupleOrVariantV<T, TupleObjectListClauses>) { |
| 201 | + return &std::get<OmpObjectList>(clause.t); |
| 202 | + } else if constexpr (WrappedInTupleOrVariantV<T, decltype(OmpClause::u)>) { |
| 203 | + return nullptr; |
| 204 | + } else { |
| 205 | + // The condition should be type-dependent, but it should always be false. |
| 206 | + static_assert(sizeof(T) < 0 && "Unexpected argument type"); |
| 207 | + } |
| 208 | +} |
| 209 | + |
130 | 210 | const OmpObjectList *GetOmpObjectList(const OmpClause &clause); |
| 211 | +const OmpObjectList *GetOmpObjectList(const OmpClause::Depend &clause); |
| 212 | +const OmpObjectList *GetOmpObjectList(const OmpDependClause::TaskDep &x); |
131 | 213 |
|
132 | 214 | template <typename T> |
133 | 215 | const T *GetFirstArgument(const OmpDirectiveSpecification &spec) { |
|
0 commit comments