xref: /llvm-project/flang/lib/Parser/openmp-parsers.cpp (revision 42ecf188b5ae1199d5b7405c521a3e72f80e7e94)
1 //===-- lib/Parser/openmp-parsers.cpp -------------------------------------===//
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 // Top-level grammar specification for OpenMP.
10 // See OpenMP-4.5-grammar.txt for documentation.
11 
12 #include "basic-parsers.h"
13 #include "expr-parsers.h"
14 #include "misc-parsers.h"
15 #include "stmt-parser.h"
16 #include "token-parsers.h"
17 #include "type-parser-implementation.h"
18 #include "flang/Parser/parse-tree.h"
19 
20 // OpenMP Directives and Clauses
21 namespace Fortran::parser {
22 
23 constexpr auto startOmpLine = skipStuffBeforeStatement >> "!$OMP "_sptok;
24 constexpr auto endOmpLine = space >> endOfLine;
25 
26 // OpenMP Clauses
27 // 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
28 TYPE_PARSER(construct<OmpDefaultClause>(
29     "PRIVATE" >> pure(OmpDefaultClause::Type::Private) ||
30     "FIRSTPRIVATE" >> pure(OmpDefaultClause::Type::Firstprivate) ||
31     "SHARED" >> pure(OmpDefaultClause::Type::Shared) ||
32     "NONE" >> pure(OmpDefaultClause::Type::None)))
33 
34 // 2.5 PROC_BIND (MASTER | CLOSE | SPREAD)
35 TYPE_PARSER(construct<OmpProcBindClause>(
36     "CLOSE" >> pure(OmpProcBindClause::Type::Close) ||
37     "MASTER" >> pure(OmpProcBindClause::Type::Master) ||
38     "SPREAD" >> pure(OmpProcBindClause::Type::Spread)))
39 
40 // 2.15.5.1 MAP ([ [ALWAYS[,]] map-type : ] variable-name-list)
41 //          map-type -> TO | FROM | TOFROM | ALLOC | RELEASE | DELETE
42 TYPE_PARSER(construct<OmpMapType>(
43     maybe("ALWAYS" >> construct<OmpMapType::Always>() / maybe(","_tok)),
44     ("TO"_id >> pure(OmpMapType::Type::To) ||
45         "FROM" >> pure(OmpMapType::Type::From) ||
46         "TOFROM" >> pure(OmpMapType::Type::Tofrom) ||
47         "ALLOC" >> pure(OmpMapType::Type::Alloc) ||
48         "RELEASE" >> pure(OmpMapType::Type::Release) ||
49         "DELETE" >> pure(OmpMapType::Type::Delete)) /
50         ":"))
51 
52 TYPE_PARSER(construct<OmpMapClause>(
53     maybe(Parser<OmpMapType>{}), Parser<OmpObjectList>{}))
54 
55 // 2.15.5.2 defaultmap -> DEFAULTMAP (TOFROM:SCALAR)
56 TYPE_PARSER(construct<OmpDefaultmapClause>(
57     construct<OmpDefaultmapClause::ImplicitBehavior>(
58         "TOFROM" >> pure(OmpDefaultmapClause::ImplicitBehavior::Tofrom)),
59     maybe(":" >> construct<OmpDefaultmapClause::VariableCategory>("SCALAR" >>
60                      pure(OmpDefaultmapClause::VariableCategory::Scalar)))))
61 
62 // 2.7.1 SCHEDULE ([modifier1 [, modifier2]:]kind[, chunk_size])
63 //       Modifier ->  MONITONIC | NONMONOTONIC | SIMD
64 //       kind -> STATIC | DYNAMIC | GUIDED | AUTO | RUNTIME
65 //       chunk_size -> ScalarIntExpr
66 TYPE_PARSER(construct<OmpScheduleModifierType>(
67     "MONOTONIC" >> pure(OmpScheduleModifierType::ModType::Monotonic) ||
68     "NONMONOTONIC" >> pure(OmpScheduleModifierType::ModType::Nonmonotonic) ||
69     "SIMD" >> pure(OmpScheduleModifierType::ModType::Simd)))
70 
71 TYPE_PARSER(construct<OmpScheduleModifier>(Parser<OmpScheduleModifierType>{},
72     maybe("," >> Parser<OmpScheduleModifierType>{}) / ":"))
73 
74 TYPE_PARSER(construct<OmpScheduleClause>(maybe(Parser<OmpScheduleModifier>{}),
75     "STATIC" >> pure(OmpScheduleClause::ScheduleType::Static) ||
76         "DYNAMIC" >> pure(OmpScheduleClause::ScheduleType::Dynamic) ||
77         "GUIDED" >> pure(OmpScheduleClause::ScheduleType::Guided) ||
78         "AUTO" >> pure(OmpScheduleClause::ScheduleType::Auto) ||
79         "RUNTIME" >> pure(OmpScheduleClause::ScheduleType::Runtime),
80     maybe("," >> scalarIntExpr)))
81 
82 // 2.12 IF (directive-name-modifier: scalar-logical-expr)
83 TYPE_PARSER(construct<OmpIfClause>(
84     maybe(
85         ("PARALLEL" >> pure(OmpIfClause::DirectiveNameModifier::Parallel) ||
86             "TARGET ENTER DATA" >>
87                 pure(OmpIfClause::DirectiveNameModifier::TargetEnterData) ||
88             "TARGET EXIT DATA" >>
89                 pure(OmpIfClause::DirectiveNameModifier::TargetExitData) ||
90             "TARGET DATA" >>
91                 pure(OmpIfClause::DirectiveNameModifier::TargetData) ||
92             "TARGET UPDATE" >>
93                 pure(OmpIfClause::DirectiveNameModifier::TargetUpdate) ||
94             "TARGET" >> pure(OmpIfClause::DirectiveNameModifier::Target) ||
95             "TASK"_id >> pure(OmpIfClause::DirectiveNameModifier::Task) ||
96             "TASKLOOP" >> pure(OmpIfClause::DirectiveNameModifier::Taskloop)) /
97         ":"),
98     scalarLogicalExpr))
99 
100 // 2.15.3.6 REDUCTION (reduction-identifier: variable-name-list)
101 TYPE_PARSER(construct<OmpReductionOperator>(Parser<DefinedOperator>{}) ||
102     construct<OmpReductionOperator>(Parser<ProcedureDesignator>{}))
103 
104 TYPE_PARSER(construct<OmpReductionClause>(
105     Parser<OmpReductionOperator>{} / ":", nonemptyList(designator)))
106 
107 // OMP 5.0 2.11.4  ALLOCATE ([allocator:] variable-name-list)
108 TYPE_PARSER(construct<OmpAllocateClause>(
109     maybe(construct<OmpAllocateClause::Allocator>(scalarIntExpr) / ":"),
110     Parser<OmpObjectList>{}))
111 
112 // 2.13.9 DEPEND (SOURCE | SINK : vec | (IN | OUT | INOUT) : list
113 TYPE_PARSER(construct<OmpDependSinkVecLength>(
114     Parser<DefinedOperator>{}, scalarIntConstantExpr))
115 
116 TYPE_PARSER(
117     construct<OmpDependSinkVec>(name, maybe(Parser<OmpDependSinkVecLength>{})))
118 
119 TYPE_PARSER(
120     construct<OmpDependenceType>("IN"_id >> pure(OmpDependenceType::Type::In) ||
121         "INOUT" >> pure(OmpDependenceType::Type::Inout) ||
122         "OUT" >> pure(OmpDependenceType::Type::Out)))
123 
124 TYPE_CONTEXT_PARSER("Omp Depend clause"_en_US,
125     construct<OmpDependClause>(construct<OmpDependClause::Sink>(
126         "SINK :" >> nonemptyList(Parser<OmpDependSinkVec>{}))) ||
127         construct<OmpDependClause>(
128             construct<OmpDependClause::Source>("SOURCE"_tok)) ||
129         construct<OmpDependClause>(construct<OmpDependClause::InOut>(
130             Parser<OmpDependenceType>{}, ":" >> nonemptyList(designator))))
131 
132 // 2.15.3.7 LINEAR (linear-list: linear-step)
133 //          linear-list -> list | modifier(list)
134 //          linear-modifier -> REF | VAL | UVAL
135 TYPE_PARSER(
136     construct<OmpLinearModifier>("REF" >> pure(OmpLinearModifier::Type::Ref) ||
137         "VAL" >> pure(OmpLinearModifier::Type::Val) ||
138         "UVAL" >> pure(OmpLinearModifier::Type::Uval)))
139 
140 TYPE_CONTEXT_PARSER("Omp LINEAR clause"_en_US,
141     construct<OmpLinearClause>(
142         construct<OmpLinearClause>(construct<OmpLinearClause::WithModifier>(
143             Parser<OmpLinearModifier>{}, parenthesized(nonemptyList(name)),
144             maybe(":" >> scalarIntConstantExpr))) ||
145         construct<OmpLinearClause>(construct<OmpLinearClause::WithoutModifier>(
146             nonemptyList(name), maybe(":" >> scalarIntConstantExpr)))))
147 
148 // 2.8.1 ALIGNED (list: alignment)
149 TYPE_PARSER(construct<OmpAlignedClause>(
150     nonemptyList(name), maybe(":" >> scalarIntConstantExpr)))
151 
152 TYPE_PARSER(
153     construct<OmpObject>(designator) || construct<OmpObject>("/" >> name / "/"))
154 
155 TYPE_PARSER(
156     "ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
157     "ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
158     "ALIGNED" >>
159         construct<OmpClause>(parenthesized(Parser<OmpAlignedClause>{})) ||
160     "ALLOCATE" >>
161         construct<OmpClause>(parenthesized(Parser<OmpAllocateClause>{})) ||
162     "COLLAPSE" >> construct<OmpClause>(construct<OmpClause::Collapse>(
163                       parenthesized(scalarIntConstantExpr))) ||
164     "COPYIN" >> construct<OmpClause>(construct<OmpClause::Copyin>(
165                     parenthesized(Parser<OmpObjectList>{}))) ||
166     "COPYPRIVATE" >> construct<OmpClause>(construct<OmpClause::Copyprivate>(
167                          (parenthesized(Parser<OmpObjectList>{})))) ||
168     "DEFAULT"_id >>
169         construct<OmpClause>(parenthesized(Parser<OmpDefaultClause>{})) ||
170     "DEFAULTMAP" >>
171         construct<OmpClause>(parenthesized(Parser<OmpDefaultmapClause>{})) ||
172     "DEPEND" >>
173         construct<OmpClause>(parenthesized(Parser<OmpDependClause>{})) ||
174     "DEVICE" >> construct<OmpClause>(construct<OmpClause::Device>(
175                     parenthesized(scalarIntExpr))) ||
176     "DIST_SCHEDULE" >>
177         construct<OmpClause>(construct<OmpDistScheduleClause>(
178             parenthesized("STATIC" >> maybe("," >> scalarIntExpr)))) ||
179     "FINAL" >> construct<OmpClause>(construct<OmpClause::Final>(
180                    parenthesized(scalarLogicalExpr))) ||
181     "FIRSTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Firstprivate>(
182                           parenthesized(Parser<OmpObjectList>{}))) ||
183     "FROM" >> construct<OmpClause>(construct<OmpClause::From>(
184                   parenthesized(Parser<OmpObjectList>{}))) ||
185     "GRAINSIZE" >> construct<OmpClause>(construct<OmpClause::Grainsize>(
186                        parenthesized(scalarIntExpr))) ||
187     "HINT" >> construct<OmpClause>(
188                   construct<OmpClause::Hint>(parenthesized(constantExpr))) ||
189     "IF" >> construct<OmpClause>(parenthesized(Parser<OmpIfClause>{})) ||
190     "INBRANCH" >> construct<OmpClause>(construct<OmpClause::Inbranch>()) ||
191     "IS_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::IsDevicePtr>(
192                            parenthesized(nonemptyList(name)))) ||
193     "LASTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Lastprivate>(
194                          parenthesized(Parser<OmpObjectList>{}))) ||
195     "LINEAR" >>
196         construct<OmpClause>(parenthesized(Parser<OmpLinearClause>{})) ||
197     "LINK" >> construct<OmpClause>(construct<OmpClause::Link>(
198                   parenthesized(Parser<OmpObjectList>{}))) ||
199     "MAP" >> construct<OmpClause>(parenthesized(Parser<OmpMapClause>{})) ||
200     "MERGEABLE" >> construct<OmpClause>(construct<OmpClause::Mergeable>()) ||
201     "NOGROUP" >> construct<OmpClause>(construct<OmpClause::Nogroup>()) ||
202     "NOTINBRANCH" >>
203         construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
204     "NOWAIT" >> construct<OmpClause>(construct<OmpNowait>()) ||
205     "NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
206                        parenthesized(scalarIntExpr))) ||
207     "NUM_TEAMS" >> construct<OmpClause>(construct<OmpClause::NumTeams>(
208                        parenthesized(scalarIntExpr))) ||
209     "NUM_THREADS" >> construct<OmpClause>(construct<OmpClause::NumThreads>(
210                          parenthesized(scalarIntExpr))) ||
211     "ORDERED" >> construct<OmpClause>(construct<OmpClause::Ordered>(
212                      maybe(parenthesized(scalarIntConstantExpr)))) ||
213     "PRIORITY" >> construct<OmpClause>(construct<OmpClause::Priority>(
214                       parenthesized(scalarIntExpr))) ||
215     "PRIVATE" >> construct<OmpClause>(construct<OmpClause::Private>(
216                      parenthesized(Parser<OmpObjectList>{}))) ||
217     "PROC_BIND" >>
218         construct<OmpClause>(parenthesized(Parser<OmpProcBindClause>{})) ||
219     "REDUCTION" >>
220         construct<OmpClause>(parenthesized(Parser<OmpReductionClause>{})) ||
221     "RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>()) ||
222     "RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
223     "SAFELEN" >> construct<OmpClause>(construct<OmpClause::Safelen>(
224                      parenthesized(scalarIntConstantExpr))) ||
225     "SCHEDULE" >>
226         construct<OmpClause>(parenthesized(Parser<OmpScheduleClause>{})) ||
227     "SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>()) ||
228     "SHARED" >> construct<OmpClause>(construct<OmpClause::Shared>(
229                     parenthesized(Parser<OmpObjectList>{}))) ||
230     "SIMD"_id >> construct<OmpClause>(construct<OmpClause::Simd>()) ||
231     "SIMDLEN" >> construct<OmpClause>(construct<OmpClause::Simdlen>(
232                      parenthesized(scalarIntConstantExpr))) ||
233     "THREADS" >> construct<OmpClause>(construct<OmpClause::Threads>()) ||
234     "THREAD_LIMIT" >> construct<OmpClause>(construct<OmpClause::ThreadLimit>(
235                           parenthesized(scalarIntExpr))) ||
236     "TO" >> construct<OmpClause>(construct<OmpClause::To>(
237                 parenthesized(Parser<OmpObjectList>{}))) ||
238     "USE_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::UseDevicePtr>(
239                             parenthesized(nonemptyList(name)))) ||
240     "UNIFORM" >> construct<OmpClause>(construct<OmpClause::Uniform>(
241                      parenthesized(nonemptyList(name)))) ||
242     "UNTIED" >> construct<OmpClause>(construct<OmpClause::Untied>()))
243 
244 // [Clause, [Clause], ...]
245 TYPE_PARSER(sourced(construct<OmpClauseList>(
246     many(maybe(","_tok) >> sourced(Parser<OmpClause>{})))))
247 
248 // 2.1 (variable | /common-block | array-sections)
249 TYPE_PARSER(construct<OmpObjectList>(nonemptyList(Parser<OmpObject>{})))
250 
251 // Omp directives enclosing do loop
252 TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
253     "DISTRIBUTE PARALLEL DO SIMD" >>
254         pure(llvm::omp::Directive::OMPD_distribute_parallel_do_simd),
255     "DISTRIBUTE PARALLEL DO" >>
256         pure(llvm::omp::Directive::OMPD_distribute_parallel_do),
257     "DISTRIBUTE SIMD" >> pure(llvm::omp::Directive::OMPD_distribute_simd),
258     "DISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_distribute),
259     "DO SIMD" >> pure(llvm::omp::Directive::OMPD_do_simd),
260     "DO" >> pure(llvm::omp::Directive::OMPD_do),
261     "PARALLEL DO SIMD" >> pure(llvm::omp::Directive::OMPD_parallel_do_simd),
262     "PARALLEL DO" >> pure(llvm::omp::Directive::OMPD_parallel_do),
263     "SIMD" >> pure(llvm::omp::Directive::OMPD_simd),
264     "TARGET PARALLEL DO SIMD" >>
265         pure(llvm::omp::Directive::OMPD_target_parallel_do_simd),
266     "TARGET PARALLEL DO" >> pure(llvm::omp::Directive::OMPD_target_parallel_do),
267     "TARGET SIMD" >> pure(llvm::omp::Directive::OMPD_target_simd),
268     "TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD" >>
269         pure(llvm::omp::Directive::
270                 OMPD_target_teams_distribute_parallel_do_simd),
271     "TARGET TEAMS DISTRIBUTE PARALLEL DO" >>
272         pure(llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do),
273     "TARGET TEAMS DISTRIBUTE SIMD" >>
274         pure(llvm::omp::Directive::OMPD_target_teams_distribute_simd),
275     "TARGET TEAMS DISTRIBUTE" >>
276         pure(llvm::omp::Directive::OMPD_target_teams_distribute),
277     "TASKLOOP SIMD" >> pure(llvm::omp::Directive::OMPD_taskloop_simd),
278     "TASKLOOP" >> pure(llvm::omp::Directive::OMPD_taskloop),
279     "TEAMS DISTRIBUTE PARALLEL DO SIMD" >>
280         pure(llvm::omp::Directive::OMPD_teams_distribute_parallel_do_simd),
281     "TEAMS DISTRIBUTE PARALLEL DO" >>
282         pure(llvm::omp::Directive::OMPD_teams_distribute_parallel_do),
283     "TEAMS DISTRIBUTE SIMD" >>
284         pure(llvm::omp::Directive::OMPD_teams_distribute_simd),
285     "TEAMS DISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_teams_distribute)))))
286 
287 TYPE_PARSER(sourced(construct<OmpBeginLoopDirective>(
288     sourced(Parser<OmpLoopDirective>{}), Parser<OmpClauseList>{})))
289 
290 // 2.14.1 construct-type-clause -> PARALLEL | SECTIONS | DO | TASKGROUP
291 TYPE_PARSER(sourced(construct<OmpCancelType>(
292     first("PARALLEL" >> pure(OmpCancelType::Type::Parallel),
293         "SECTIONS" >> pure(OmpCancelType::Type::Sections),
294         "DO" >> pure(OmpCancelType::Type::Do),
295         "TASKGROUP" >> pure(OmpCancelType::Type::Taskgroup)))))
296 
297 // 2.14.2 Cancellation Point construct
298 TYPE_PARSER(sourced(construct<OpenMPCancellationPointConstruct>(
299     verbatim("CANCELLATION POINT"_tok), Parser<OmpCancelType>{})))
300 
301 // 2.14.1 Cancel construct
302 TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(verbatim("CANCEL"_tok),
303     Parser<OmpCancelType>{}, maybe("IF" >> parenthesized(scalarLogicalExpr)))))
304 
305 // 2.17.8 Flush construct [OpenMP 5.0]
306 //        flush -> FLUSH [memory-order-clause] [(variable-name-list)]
307 //        memory-order-clause -> acq_rel
308 //                               release
309 //                               acquire
310 TYPE_PARSER(sourced(construct<OmpFlushMemoryClause>(
311     "ACQ_REL" >> pure(llvm::omp::Clause::OMPC_acq_rel) ||
312     "RELEASE" >> pure(llvm::omp::Clause::OMPC_release) ||
313     "ACQUIRE" >> pure(llvm::omp::Clause::OMPC_acquire))))
314 
315 TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
316     maybe(Parser<OmpFlushMemoryClause>{}),
317     maybe(parenthesized(Parser<OmpObjectList>{})))))
318 
319 // Simple Standalone Directives
320 TYPE_PARSER(sourced(construct<OmpSimpleStandaloneDirective>(first(
321     "BARRIER" >> pure(llvm::omp::Directive::OMPD_barrier),
322     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
323     "TARGET ENTER DATA" >> pure(llvm::omp::Directive::OMPD_target_enter_data),
324     "TARGET EXIT DATA" >> pure(llvm::omp::Directive::OMPD_target_exit_data),
325     "TARGET UPDATE" >> pure(llvm::omp::Directive::OMPD_target_update),
326     "TASKWAIT" >> pure(llvm::omp::Directive::OMPD_taskwait),
327     "TASKYIELD" >> pure(llvm::omp::Directive::OMPD_taskyield)))))
328 
329 TYPE_PARSER(sourced(construct<OpenMPSimpleStandaloneConstruct>(
330     Parser<OmpSimpleStandaloneDirective>{}, Parser<OmpClauseList>{})))
331 
332 // Standalone Constructs
333 TYPE_PARSER(
334     sourced(construct<OpenMPStandaloneConstruct>(
335                 Parser<OpenMPSimpleStandaloneConstruct>{}) ||
336         construct<OpenMPStandaloneConstruct>(Parser<OpenMPFlushConstruct>{}) ||
337         construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
338         construct<OpenMPStandaloneConstruct>(
339             Parser<OpenMPCancellationPointConstruct>{})) /
340     endOfLine)
341 
342 // Directives enclosing structured-block
343 TYPE_PARSER(construct<OmpBlockDirective>(first(
344     "MASTER" >> pure(llvm::omp::Directive::OMPD_master),
345     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
346     "PARALLEL WORKSHARE" >> pure(llvm::omp::Directive::OMPD_parallel_workshare),
347     "PARALLEL" >> pure(llvm::omp::Directive::OMPD_parallel),
348     "SINGLE" >> pure(llvm::omp::Directive::OMPD_single),
349     "TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
350     "TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),
351     "TARGET TEAMS" >> pure(llvm::omp::Directive::OMPD_target_teams),
352     "TARGET" >> pure(llvm::omp::Directive::OMPD_target),
353     "TASK"_id >> pure(llvm::omp::Directive::OMPD_task),
354     "TASKGROUP" >> pure(llvm::omp::Directive::OMPD_taskgroup),
355     "TEAMS" >> pure(llvm::omp::Directive::OMPD_teams),
356     "WORKSHARE" >> pure(llvm::omp::Directive::OMPD_workshare))))
357 
358 TYPE_PARSER(sourced(construct<OmpBeginBlockDirective>(
359     sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{})))
360 
361 TYPE_PARSER(construct<OmpReductionInitializerClause>(
362     "INITIALIZER" >> parenthesized("OMP_PRIV =" >> expr)))
363 
364 // 2.16 Declare Reduction Construct
365 TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
366     verbatim("DECLARE REDUCTION"_tok),
367     "(" >> Parser<OmpReductionOperator>{} / ":",
368     nonemptyList(Parser<DeclarationTypeSpec>{}) / ":",
369     Parser<OmpReductionCombiner>{} / ")",
370     maybe(Parser<OmpReductionInitializerClause>{}))))
371 
372 // declare-target with list
373 TYPE_PARSER(sourced(construct<OmpDeclareTargetWithList>(
374     parenthesized(Parser<OmpObjectList>{}))))
375 
376 // declare-target with clause
377 TYPE_PARSER(
378     sourced(construct<OmpDeclareTargetWithClause>(Parser<OmpClauseList>{})))
379 
380 // declare-target-specifier
381 TYPE_PARSER(
382     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithList>{}) ||
383     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithClause>{}))
384 
385 // 2.10.6 Declare Target Construct
386 TYPE_PARSER(sourced(construct<OpenMPDeclareTargetConstruct>(
387     verbatim("DECLARE TARGET"_tok), Parser<OmpDeclareTargetSpecifier>{})))
388 
389 TYPE_PARSER(construct<OmpReductionCombiner>(Parser<AssignmentStmt>{}) ||
390     construct<OmpReductionCombiner>(
391         construct<OmpReductionCombiner::FunctionCombiner>(
392             construct<Call>(Parser<ProcedureDesignator>{},
393                 parenthesized(optionalList(actualArgSpec))))))
394 
395 // 2.17.7 atomic -> ATOMIC [clause [,]] atomic-clause [[,] clause] |
396 //                  ATOMIC [clause]
397 //       clause -> memory-order-clause | HINT(hint-expression)
398 //       memory-order-clause -> SEQ_CST | ACQ_REL | RELEASE | ACQUIRE | RELAXED
399 //       atomic-clause -> READ | WRITE | UPDATE | CAPTURE
400 
401 // OMP END ATOMIC
402 TYPE_PARSER(construct<OmpEndAtomic>(startOmpLine >> "END ATOMIC"_tok))
403 
404 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] READ [MEMORY-ORDER-CLAUSE-LIST]
405 TYPE_PARSER("ATOMIC" >>
406     construct<OmpAtomicRead>(Parser<OmpClauseList>{} / maybe(","_tok),
407         verbatim("READ"_tok), Parser<OmpClauseList>{} / endOmpLine,
408         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
409 
410 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] CAPTURE [MEMORY-ORDER-CLAUSE-LIST]
411 TYPE_PARSER("ATOMIC" >>
412     construct<OmpAtomicCapture>(Parser<OmpClauseList>{} / maybe(","_tok),
413         verbatim("CAPTURE"_tok), Parser<OmpClauseList>{} / endOmpLine,
414         statement(assignmentStmt), statement(assignmentStmt),
415         Parser<OmpEndAtomic>{} / endOmpLine))
416 
417 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] UPDATE [MEMORY-ORDER-CLAUSE-LIST]
418 TYPE_PARSER("ATOMIC" >>
419     construct<OmpAtomicUpdate>(Parser<OmpClauseList>{} / maybe(","_tok),
420         verbatim("UPDATE"_tok), Parser<OmpClauseList>{} / endOmpLine,
421         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
422 
423 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST]
424 TYPE_PARSER(construct<OmpAtomic>(verbatim("ATOMIC"_tok),
425     Parser<OmpClauseList>{} / endOmpLine, statement(assignmentStmt),
426     maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
427 
428 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] WRITE [MEMORY-ORDER-CLAUSE-LIST]
429 TYPE_PARSER("ATOMIC" >>
430     construct<OmpAtomicWrite>(Parser<OmpClauseList>{} / maybe(","_tok),
431         verbatim("WRITE"_tok), Parser<OmpClauseList>{} / endOmpLine,
432         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
433 
434 // Atomic Construct
435 TYPE_PARSER(construct<OpenMPAtomicConstruct>(Parser<OmpAtomicRead>{}) ||
436     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicCapture>{}) ||
437     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicWrite>{}) ||
438     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicUpdate>{}) ||
439     construct<OpenMPAtomicConstruct>(Parser<OmpAtomic>{}))
440 
441 // 2.13.2 OMP CRITICAL
442 TYPE_PARSER(startOmpLine >>
443     sourced(construct<OmpEndCriticalDirective>(
444         verbatim("END CRITICAL"_tok), maybe(parenthesized(name)))) /
445         endOmpLine)
446 TYPE_PARSER(sourced(construct<OmpCriticalDirective>(verbatim("CRITICAL"_tok),
447                 maybe(parenthesized(name)), maybe(Parser<OmpClause>{}))) /
448     endOmpLine)
449 
450 TYPE_PARSER(construct<OpenMPCriticalConstruct>(
451     Parser<OmpCriticalDirective>{}, block, Parser<OmpEndCriticalDirective>{}))
452 
453 // 2.8.2 Declare Simd construct
454 TYPE_PARSER(
455     sourced(construct<OpenMPDeclareSimdConstruct>(verbatim("DECLARE SIMD"_tok),
456         maybe(parenthesized(name)), Parser<OmpClauseList>{})))
457 
458 // 2.15.2 Threadprivate directive
459 TYPE_PARSER(sourced(construct<OpenMPThreadprivate>(
460     verbatim("THREADPRIVATE"_tok), parenthesized(Parser<OmpObjectList>{}))))
461 
462 // Declarative constructs
463 TYPE_PARSER(startOmpLine >>
464     sourced(construct<OpenMPDeclarativeConstruct>(
465                 Parser<OpenMPDeclareReductionConstruct>{}) ||
466         construct<OpenMPDeclarativeConstruct>(
467             Parser<OpenMPDeclareSimdConstruct>{}) ||
468         construct<OpenMPDeclarativeConstruct>(
469             Parser<OpenMPDeclareTargetConstruct>{}) ||
470         construct<OpenMPDeclarativeConstruct>(Parser<OpenMPThreadprivate>{})) /
471         endOmpLine)
472 
473 // Block Construct
474 TYPE_PARSER(construct<OpenMPBlockConstruct>(
475     Parser<OmpBeginBlockDirective>{} / endOmpLine, block,
476     Parser<OmpEndBlockDirective>{} / endOmpLine))
477 
478 // OMP SECTIONS Directive
479 TYPE_PARSER(construct<OmpSectionsDirective>(first(
480     "SECTIONS" >> pure(llvm::omp::Directive::OMPD_sections),
481     "PARALLEL SECTIONS" >> pure(llvm::omp::Directive::OMPD_parallel_sections))))
482 
483 // OMP BEGIN and END SECTIONS Directive
484 TYPE_PARSER(sourced(construct<OmpBeginSectionsDirective>(
485     sourced(Parser<OmpSectionsDirective>{}), Parser<OmpClauseList>{})))
486 TYPE_PARSER(
487     startOmpLine >> sourced(construct<OmpEndSectionsDirective>(
488                         sourced("END"_tok >> Parser<OmpSectionsDirective>{}),
489                         Parser<OmpClauseList>{})))
490 
491 // OMP SECTION-BLOCK
492 TYPE_PARSER(maybe(startOmpLine >> "SECTION"_tok / endOmpLine) >>
493     construct<OmpSectionBlocks>(
494         nonemptySeparated(block, startOmpLine >> "SECTION"_tok / endOmpLine)))
495 
496 // OMP SECTIONS (2.7.2), PARALLEL SECTIONS (2.11.2)
497 TYPE_PARSER(construct<OpenMPSectionsConstruct>(
498     Parser<OmpBeginSectionsDirective>{} / endOmpLine,
499     Parser<OmpSectionBlocks>{}, Parser<OmpEndSectionsDirective>{} / endOmpLine))
500 
501 TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
502     startOmpLine >>
503         first(construct<OpenMPConstruct>(Parser<OpenMPSectionsConstruct>{}),
504             construct<OpenMPConstruct>(Parser<OpenMPLoopConstruct>{}),
505             construct<OpenMPConstruct>(Parser<OpenMPBlockConstruct>{}),
506             // OpenMPBlockConstruct is attempted before
507             // OpenMPStandaloneConstruct to resolve !$OMP ORDERED
508             construct<OpenMPConstruct>(Parser<OpenMPStandaloneConstruct>{}),
509             construct<OpenMPConstruct>(Parser<OpenMPAtomicConstruct>{}),
510             construct<OpenMPConstruct>(Parser<OpenMPCriticalConstruct>{})))
511 
512 // END OMP Block directives
513 TYPE_PARSER(
514     startOmpLine >> sourced(construct<OmpEndBlockDirective>(
515                         sourced("END"_tok >> Parser<OmpBlockDirective>{}),
516                         Parser<OmpClauseList>{})))
517 
518 // END OMP Loop directives
519 TYPE_PARSER(
520     startOmpLine >> sourced(construct<OmpEndLoopDirective>(
521                         sourced("END"_tok >> Parser<OmpLoopDirective>{}),
522                         Parser<OmpClauseList>{})))
523 
524 TYPE_PARSER(construct<OpenMPLoopConstruct>(
525     Parser<OmpBeginLoopDirective>{} / endOmpLine))
526 } // namespace Fortran::parser
527