1 | //===-- Lower/OpenMP/ClauseFinder.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 | // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | #ifndef FORTRAN_LOWER_CLAUSEFINDER_H |
13 | #define FORTRAN_LOWER_CLAUSEFINDER_H |
14 | |
15 | #include "Clauses.h" |
16 | |
17 | namespace Fortran { |
18 | namespace lower { |
19 | namespace omp { |
20 | |
21 | class ClauseFinder { |
22 | using ClauseIterator = List<Clause>::const_iterator; |
23 | |
24 | public: |
25 | /// Utility to find a clause within a range in the clause list. |
26 | template <typename T> |
27 | static ClauseIterator findClause(ClauseIterator begin, ClauseIterator end) { |
28 | for (ClauseIterator it = begin; it != end; ++it) { |
29 | if (std::get_if<T>(&it->u)) |
30 | return it; |
31 | } |
32 | |
33 | return end; |
34 | } |
35 | |
36 | /// Return the first instance of the given clause found in the clause list or |
37 | /// `nullptr` if not present. If more than one instance is expected, use |
38 | /// `findRepeatableClause` instead. |
39 | template <typename T> |
40 | static const T *findUniqueClause(const List<Clause> &clauses, |
41 | const parser::CharBlock **source = nullptr) { |
42 | ClauseIterator it = findClause<T>(clauses.begin(), clauses.end()); |
43 | if (it != clauses.end()) { |
44 | if (source) |
45 | *source = &it->source; |
46 | return &std::get<T>(it->u); |
47 | } |
48 | return nullptr; |
49 | } |
50 | |
51 | /// Call `callbackFn` for each occurrence of the given clause. Return `true` |
52 | /// if at least one instance was found. |
53 | template <typename T> |
54 | static bool findRepeatableClause( |
55 | const List<Clause> &clauses, |
56 | std::function<void(const T &, const parser::CharBlock &source)> |
57 | callbackFn) { |
58 | bool found = false; |
59 | ClauseIterator nextIt, endIt = clauses.end(); |
60 | for (ClauseIterator it = clauses.begin(); it != endIt; it = nextIt) { |
61 | nextIt = findClause<T>(it, endIt); |
62 | |
63 | if (nextIt != endIt) { |
64 | callbackFn(std::get<T>(nextIt->u), nextIt->source); |
65 | found = true; |
66 | ++nextIt; |
67 | } |
68 | } |
69 | return found; |
70 | } |
71 | }; |
72 | } // namespace omp |
73 | } // namespace lower |
74 | } // namespace Fortran |
75 | |
76 | #endif // FORTRAN_LOWER_CLAUSEFINDER_H |
77 | |