xref: /llvm-project/flang/lib/Parser/openmp-parsers.cpp (revision 1a6f43991ff7e5249f24660074f0dd784aeecd5f)
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" >> construct<OmpClause>(construct<OmpClause::Allocate>(
161                       parenthesized(Parser<OmpAllocateClause>{}))) ||
162     "ALLOCATOR" >> construct<OmpClause>(construct<OmpClause::Allocator>(
163                        parenthesized(scalarIntExpr))) ||
164     "COLLAPSE" >> construct<OmpClause>(construct<OmpClause::Collapse>(
165                       parenthesized(scalarIntConstantExpr))) ||
166     "COPYIN" >> construct<OmpClause>(construct<OmpClause::Copyin>(
167                     parenthesized(Parser<OmpObjectList>{}))) ||
168     "COPYPRIVATE" >> construct<OmpClause>(construct<OmpClause::Copyprivate>(
169                          (parenthesized(Parser<OmpObjectList>{})))) ||
170     "DEFAULT"_id >> construct<OmpClause>(construct<OmpClause::Default>(
171                         parenthesized(Parser<OmpDefaultClause>{}))) ||
172     "DEFAULTMAP" >>
173         construct<OmpClause>(parenthesized(Parser<OmpDefaultmapClause>{})) ||
174     "DEPEND" >>
175         construct<OmpClause>(parenthesized(Parser<OmpDependClause>{})) ||
176     "DEVICE" >> construct<OmpClause>(construct<OmpClause::Device>(
177                     parenthesized(scalarIntExpr))) ||
178     "DIST_SCHEDULE" >>
179         construct<OmpClause>(construct<OmpClause::DistSchedule>(
180             parenthesized("STATIC" >> maybe("," >> scalarIntExpr)))) ||
181     "FINAL" >> construct<OmpClause>(construct<OmpClause::Final>(
182                    parenthesized(scalarLogicalExpr))) ||
183     "FIRSTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Firstprivate>(
184                           parenthesized(Parser<OmpObjectList>{}))) ||
185     "FROM" >> construct<OmpClause>(construct<OmpClause::From>(
186                   parenthesized(Parser<OmpObjectList>{}))) ||
187     "GRAINSIZE" >> construct<OmpClause>(construct<OmpClause::Grainsize>(
188                        parenthesized(scalarIntExpr))) ||
189     "HINT" >> construct<OmpClause>(
190                   construct<OmpClause::Hint>(parenthesized(constantExpr))) ||
191     "IF" >> construct<OmpClause>(parenthesized(Parser<OmpIfClause>{})) ||
192     "INBRANCH" >> construct<OmpClause>(construct<OmpClause::Inbranch>()) ||
193     "IS_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::IsDevicePtr>(
194                            parenthesized(nonemptyList(name)))) ||
195     "LASTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Lastprivate>(
196                          parenthesized(Parser<OmpObjectList>{}))) ||
197     "LINEAR" >>
198         construct<OmpClause>(parenthesized(Parser<OmpLinearClause>{})) ||
199     "LINK" >> construct<OmpClause>(construct<OmpClause::Link>(
200                   parenthesized(Parser<OmpObjectList>{}))) ||
201     "MAP" >> construct<OmpClause>(parenthesized(Parser<OmpMapClause>{})) ||
202     "MERGEABLE" >> construct<OmpClause>(construct<OmpClause::Mergeable>()) ||
203     "NOGROUP" >> construct<OmpClause>(construct<OmpClause::Nogroup>()) ||
204     "NOTINBRANCH" >>
205         construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
206     "NOWAIT" >> construct<OmpClause>(construct<OmpClause::Nowait>()) ||
207     "NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
208                        parenthesized(scalarIntExpr))) ||
209     "NUM_TEAMS" >> construct<OmpClause>(construct<OmpClause::NumTeams>(
210                        parenthesized(scalarIntExpr))) ||
211     "NUM_THREADS" >> construct<OmpClause>(construct<OmpClause::NumThreads>(
212                          parenthesized(scalarIntExpr))) ||
213     "ORDERED" >> construct<OmpClause>(construct<OmpClause::Ordered>(
214                      maybe(parenthesized(scalarIntConstantExpr)))) ||
215     "PRIORITY" >> construct<OmpClause>(construct<OmpClause::Priority>(
216                       parenthesized(scalarIntExpr))) ||
217     "PRIVATE" >> construct<OmpClause>(construct<OmpClause::Private>(
218                      parenthesized(Parser<OmpObjectList>{}))) ||
219     "PROC_BIND" >> construct<OmpClause>(construct<OmpClause::ProcBind>(
220                        parenthesized(Parser<OmpProcBindClause>{}))) ||
221     "REDUCTION" >>
222         construct<OmpClause>(parenthesized(Parser<OmpReductionClause>{})) ||
223     "RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>()) ||
224     "RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
225     "SAFELEN" >> construct<OmpClause>(construct<OmpClause::Safelen>(
226                      parenthesized(scalarIntConstantExpr))) ||
227     "SCHEDULE" >>
228         construct<OmpClause>(parenthesized(Parser<OmpScheduleClause>{})) ||
229     "SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>()) ||
230     "SHARED" >> construct<OmpClause>(construct<OmpClause::Shared>(
231                     parenthesized(Parser<OmpObjectList>{}))) ||
232     "SIMD"_id >> construct<OmpClause>(construct<OmpClause::Simd>()) ||
233     "SIMDLEN" >> construct<OmpClause>(construct<OmpClause::Simdlen>(
234                      parenthesized(scalarIntConstantExpr))) ||
235     "THREADS" >> construct<OmpClause>(construct<OmpClause::Threads>()) ||
236     "THREAD_LIMIT" >> construct<OmpClause>(construct<OmpClause::ThreadLimit>(
237                           parenthesized(scalarIntExpr))) ||
238     "TO" >> construct<OmpClause>(construct<OmpClause::To>(
239                 parenthesized(Parser<OmpObjectList>{}))) ||
240     "USE_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::UseDevicePtr>(
241                             parenthesized(nonemptyList(name)))) ||
242     "UNIFORM" >> construct<OmpClause>(construct<OmpClause::Uniform>(
243                      parenthesized(nonemptyList(name)))) ||
244     "UNTIED" >> construct<OmpClause>(construct<OmpClause::Untied>()))
245 
246 // [Clause, [Clause], ...]
247 TYPE_PARSER(sourced(construct<OmpClauseList>(
248     many(maybe(","_tok) >> sourced(Parser<OmpClause>{})))))
249 
250 // 2.1 (variable | /common-block | array-sections)
251 TYPE_PARSER(construct<OmpObjectList>(nonemptyList(Parser<OmpObject>{})))
252 
253 // Omp directives enclosing do loop
254 TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
255     "DISTRIBUTE PARALLEL DO SIMD" >>
256         pure(llvm::omp::Directive::OMPD_distribute_parallel_do_simd),
257     "DISTRIBUTE PARALLEL DO" >>
258         pure(llvm::omp::Directive::OMPD_distribute_parallel_do),
259     "DISTRIBUTE SIMD" >> pure(llvm::omp::Directive::OMPD_distribute_simd),
260     "DISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_distribute),
261     "DO SIMD" >> pure(llvm::omp::Directive::OMPD_do_simd),
262     "DO" >> pure(llvm::omp::Directive::OMPD_do),
263     "PARALLEL DO SIMD" >> pure(llvm::omp::Directive::OMPD_parallel_do_simd),
264     "PARALLEL DO" >> pure(llvm::omp::Directive::OMPD_parallel_do),
265     "SIMD" >> pure(llvm::omp::Directive::OMPD_simd),
266     "TARGET PARALLEL DO SIMD" >>
267         pure(llvm::omp::Directive::OMPD_target_parallel_do_simd),
268     "TARGET PARALLEL DO" >> pure(llvm::omp::Directive::OMPD_target_parallel_do),
269     "TARGET SIMD" >> pure(llvm::omp::Directive::OMPD_target_simd),
270     "TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD" >>
271         pure(llvm::omp::Directive::
272                 OMPD_target_teams_distribute_parallel_do_simd),
273     "TARGET TEAMS DISTRIBUTE PARALLEL DO" >>
274         pure(llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do),
275     "TARGET TEAMS DISTRIBUTE SIMD" >>
276         pure(llvm::omp::Directive::OMPD_target_teams_distribute_simd),
277     "TARGET TEAMS DISTRIBUTE" >>
278         pure(llvm::omp::Directive::OMPD_target_teams_distribute),
279     "TASKLOOP SIMD" >> pure(llvm::omp::Directive::OMPD_taskloop_simd),
280     "TASKLOOP" >> pure(llvm::omp::Directive::OMPD_taskloop),
281     "TEAMS DISTRIBUTE PARALLEL DO SIMD" >>
282         pure(llvm::omp::Directive::OMPD_teams_distribute_parallel_do_simd),
283     "TEAMS DISTRIBUTE PARALLEL DO" >>
284         pure(llvm::omp::Directive::OMPD_teams_distribute_parallel_do),
285     "TEAMS DISTRIBUTE SIMD" >>
286         pure(llvm::omp::Directive::OMPD_teams_distribute_simd),
287     "TEAMS DISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_teams_distribute)))))
288 
289 TYPE_PARSER(sourced(construct<OmpBeginLoopDirective>(
290     sourced(Parser<OmpLoopDirective>{}), Parser<OmpClauseList>{})))
291 
292 // 2.14.1 construct-type-clause -> PARALLEL | SECTIONS | DO | TASKGROUP
293 TYPE_PARSER(sourced(construct<OmpCancelType>(
294     first("PARALLEL" >> pure(OmpCancelType::Type::Parallel),
295         "SECTIONS" >> pure(OmpCancelType::Type::Sections),
296         "DO" >> pure(OmpCancelType::Type::Do),
297         "TASKGROUP" >> pure(OmpCancelType::Type::Taskgroup)))))
298 
299 // 2.14.2 Cancellation Point construct
300 TYPE_PARSER(sourced(construct<OpenMPCancellationPointConstruct>(
301     verbatim("CANCELLATION POINT"_tok), Parser<OmpCancelType>{})))
302 
303 // 2.14.1 Cancel construct
304 TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(verbatim("CANCEL"_tok),
305     Parser<OmpCancelType>{}, maybe("IF" >> parenthesized(scalarLogicalExpr)))))
306 
307 // 2.17.7 Atomic construct/2.17.8 Flush construct [OpenMP 5.0]
308 //        memory-order-clause ->
309 //                               seq_cst
310 //                               acq_rel
311 //                               release
312 //                               acquire
313 //                               relaxed
314 TYPE_PARSER(sourced(construct<OmpMemoryOrderClause>(
315     sourced("SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>()) ||
316         "ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
317         "RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
318         "ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
319         "RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>())))))
320 
321 // 2.17.7 Atomic construct
322 //        atomic-clause -> memory-order-clause | HINT(hint-expression)
323 TYPE_PARSER(sourced(construct<OmpAtomicClause>(
324     construct<OmpAtomicClause>(Parser<OmpMemoryOrderClause>{}) ||
325     construct<OmpAtomicClause>("HINT" >>
326         sourced(construct<OmpClause>(
327             construct<OmpClause::Hint>(parenthesized(constantExpr))))))))
328 
329 // atomic-clause-list -> [atomic-clause, [atomic-clause], ...]
330 TYPE_PARSER(sourced(construct<OmpAtomicClauseList>(
331     many(maybe(","_tok) >> sourced(Parser<OmpAtomicClause>{})))))
332 
333 TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
334     many(maybe(","_tok) >> sourced(Parser<OmpMemoryOrderClause>{})),
335     maybe(parenthesized(Parser<OmpObjectList>{})))))
336 
337 // Simple Standalone Directives
338 TYPE_PARSER(sourced(construct<OmpSimpleStandaloneDirective>(first(
339     "BARRIER" >> pure(llvm::omp::Directive::OMPD_barrier),
340     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
341     "TARGET ENTER DATA" >> pure(llvm::omp::Directive::OMPD_target_enter_data),
342     "TARGET EXIT DATA" >> pure(llvm::omp::Directive::OMPD_target_exit_data),
343     "TARGET UPDATE" >> pure(llvm::omp::Directive::OMPD_target_update),
344     "TASKWAIT" >> pure(llvm::omp::Directive::OMPD_taskwait),
345     "TASKYIELD" >> pure(llvm::omp::Directive::OMPD_taskyield)))))
346 
347 TYPE_PARSER(sourced(construct<OpenMPSimpleStandaloneConstruct>(
348     Parser<OmpSimpleStandaloneDirective>{}, Parser<OmpClauseList>{})))
349 
350 // Standalone Constructs
351 TYPE_PARSER(
352     sourced(construct<OpenMPStandaloneConstruct>(
353                 Parser<OpenMPSimpleStandaloneConstruct>{}) ||
354         construct<OpenMPStandaloneConstruct>(Parser<OpenMPFlushConstruct>{}) ||
355         construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
356         construct<OpenMPStandaloneConstruct>(
357             Parser<OpenMPCancellationPointConstruct>{})) /
358     endOfLine)
359 
360 // Directives enclosing structured-block
361 TYPE_PARSER(construct<OmpBlockDirective>(first(
362     "MASTER" >> pure(llvm::omp::Directive::OMPD_master),
363     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
364     "PARALLEL WORKSHARE" >> pure(llvm::omp::Directive::OMPD_parallel_workshare),
365     "PARALLEL" >> pure(llvm::omp::Directive::OMPD_parallel),
366     "SINGLE" >> pure(llvm::omp::Directive::OMPD_single),
367     "TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
368     "TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),
369     "TARGET TEAMS" >> pure(llvm::omp::Directive::OMPD_target_teams),
370     "TARGET" >> pure(llvm::omp::Directive::OMPD_target),
371     "TASK"_id >> pure(llvm::omp::Directive::OMPD_task),
372     "TASKGROUP" >> pure(llvm::omp::Directive::OMPD_taskgroup),
373     "TEAMS" >> pure(llvm::omp::Directive::OMPD_teams),
374     "WORKSHARE" >> pure(llvm::omp::Directive::OMPD_workshare))))
375 
376 TYPE_PARSER(sourced(construct<OmpBeginBlockDirective>(
377     sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{})))
378 
379 TYPE_PARSER(construct<OmpReductionInitializerClause>(
380     "INITIALIZER" >> parenthesized("OMP_PRIV =" >> expr)))
381 
382 // 2.16 Declare Reduction Construct
383 TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
384     verbatim("DECLARE REDUCTION"_tok),
385     "(" >> Parser<OmpReductionOperator>{} / ":",
386     nonemptyList(Parser<DeclarationTypeSpec>{}) / ":",
387     Parser<OmpReductionCombiner>{} / ")",
388     maybe(Parser<OmpReductionInitializerClause>{}))))
389 
390 // declare-target with list
391 TYPE_PARSER(sourced(construct<OmpDeclareTargetWithList>(
392     parenthesized(Parser<OmpObjectList>{}))))
393 
394 // declare-target with clause
395 TYPE_PARSER(
396     sourced(construct<OmpDeclareTargetWithClause>(Parser<OmpClauseList>{})))
397 
398 // declare-target-specifier
399 TYPE_PARSER(
400     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithList>{}) ||
401     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithClause>{}))
402 
403 // 2.10.6 Declare Target Construct
404 TYPE_PARSER(sourced(construct<OpenMPDeclareTargetConstruct>(
405     verbatim("DECLARE TARGET"_tok), Parser<OmpDeclareTargetSpecifier>{})))
406 
407 TYPE_PARSER(construct<OmpReductionCombiner>(Parser<AssignmentStmt>{}) ||
408     construct<OmpReductionCombiner>(
409         construct<OmpReductionCombiner::FunctionCombiner>(
410             construct<Call>(Parser<ProcedureDesignator>{},
411                 parenthesized(optionalList(actualArgSpec))))))
412 
413 // 2.17.7 atomic -> ATOMIC [clause [,]] atomic-clause [[,] clause] |
414 //                  ATOMIC [clause]
415 //       clause -> memory-order-clause | HINT(hint-expression)
416 //       memory-order-clause -> SEQ_CST | ACQ_REL | RELEASE | ACQUIRE | RELAXED
417 //       atomic-clause -> READ | WRITE | UPDATE | CAPTURE
418 
419 // OMP END ATOMIC
420 TYPE_PARSER(construct<OmpEndAtomic>(startOmpLine >> "END ATOMIC"_tok))
421 
422 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] READ [MEMORY-ORDER-CLAUSE-LIST]
423 TYPE_PARSER("ATOMIC" >>
424     construct<OmpAtomicRead>(Parser<OmpAtomicClauseList>{} / maybe(","_tok),
425         verbatim("READ"_tok), Parser<OmpAtomicClauseList>{} / endOmpLine,
426         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
427 
428 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] CAPTURE [MEMORY-ORDER-CLAUSE-LIST]
429 TYPE_PARSER("ATOMIC" >>
430     construct<OmpAtomicCapture>(Parser<OmpAtomicClauseList>{} / maybe(","_tok),
431         verbatim("CAPTURE"_tok), Parser<OmpAtomicClauseList>{} / endOmpLine,
432         statement(assignmentStmt), statement(assignmentStmt),
433         Parser<OmpEndAtomic>{} / endOmpLine))
434 
435 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] UPDATE [MEMORY-ORDER-CLAUSE-LIST]
436 TYPE_PARSER("ATOMIC" >>
437     construct<OmpAtomicUpdate>(Parser<OmpAtomicClauseList>{} / maybe(","_tok),
438         verbatim("UPDATE"_tok), Parser<OmpAtomicClauseList>{} / endOmpLine,
439         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
440 
441 // OMP ATOMIC [atomic-clause-list]
442 TYPE_PARSER(construct<OmpAtomic>(verbatim("ATOMIC"_tok),
443     Parser<OmpAtomicClauseList>{} / endOmpLine, statement(assignmentStmt),
444     maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
445 
446 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] WRITE [MEMORY-ORDER-CLAUSE-LIST]
447 TYPE_PARSER("ATOMIC" >>
448     construct<OmpAtomicWrite>(Parser<OmpAtomicClauseList>{} / maybe(","_tok),
449         verbatim("WRITE"_tok), Parser<OmpAtomicClauseList>{} / endOmpLine,
450         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
451 
452 // Atomic Construct
453 TYPE_PARSER(construct<OpenMPAtomicConstruct>(Parser<OmpAtomicRead>{}) ||
454     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicCapture>{}) ||
455     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicWrite>{}) ||
456     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicUpdate>{}) ||
457     construct<OpenMPAtomicConstruct>(Parser<OmpAtomic>{}))
458 
459 // 2.13.2 OMP CRITICAL
460 TYPE_PARSER(startOmpLine >>
461     sourced(construct<OmpEndCriticalDirective>(
462         verbatim("END CRITICAL"_tok), maybe(parenthesized(name)))) /
463         endOmpLine)
464 TYPE_PARSER(sourced(construct<OmpCriticalDirective>(verbatim("CRITICAL"_tok),
465                 maybe(parenthesized(name)), maybe(Parser<OmpClause>{}))) /
466     endOmpLine)
467 
468 TYPE_PARSER(construct<OpenMPCriticalConstruct>(
469     Parser<OmpCriticalDirective>{}, block, Parser<OmpEndCriticalDirective>{}))
470 
471 // 2.11.3 Executable Allocate directive
472 TYPE_PARSER(
473     sourced(construct<OpenMPExecutableAllocate>(verbatim("ALLOCATE"_tok),
474         maybe(parenthesized(Parser<OmpObjectList>{})), Parser<OmpClauseList>{},
475         maybe(nonemptyList(Parser<OpenMPDeclarativeAllocate>{})) / endOmpLine,
476         statement(allocateStmt))))
477 
478 // 2.8.2 Declare Simd construct
479 TYPE_PARSER(
480     sourced(construct<OpenMPDeclareSimdConstruct>(verbatim("DECLARE SIMD"_tok),
481         maybe(parenthesized(name)), Parser<OmpClauseList>{})))
482 
483 // 2.15.2 Threadprivate directive
484 TYPE_PARSER(sourced(construct<OpenMPThreadprivate>(
485     verbatim("THREADPRIVATE"_tok), parenthesized(Parser<OmpObjectList>{}))))
486 
487 // 2.11.3 Declarative Allocate directive
488 TYPE_PARSER(
489     sourced(construct<OpenMPDeclarativeAllocate>(verbatim("ALLOCATE"_tok),
490         parenthesized(Parser<OmpObjectList>{}), Parser<OmpClauseList>{})) /
491     lookAhead(endOmpLine / !statement(allocateStmt)))
492 
493 // Declarative constructs
494 TYPE_PARSER(startOmpLine >>
495     sourced(construct<OpenMPDeclarativeConstruct>(
496                 Parser<OpenMPDeclareReductionConstruct>{}) ||
497         construct<OpenMPDeclarativeConstruct>(
498             Parser<OpenMPDeclareSimdConstruct>{}) ||
499         construct<OpenMPDeclarativeConstruct>(
500             Parser<OpenMPDeclareTargetConstruct>{}) ||
501         construct<OpenMPDeclarativeConstruct>(
502             Parser<OpenMPDeclarativeAllocate>{}) ||
503         construct<OpenMPDeclarativeConstruct>(Parser<OpenMPThreadprivate>{})) /
504         endOmpLine)
505 
506 // Block Construct
507 TYPE_PARSER(construct<OpenMPBlockConstruct>(
508     Parser<OmpBeginBlockDirective>{} / endOmpLine, block,
509     Parser<OmpEndBlockDirective>{} / endOmpLine))
510 
511 // OMP SECTIONS Directive
512 TYPE_PARSER(construct<OmpSectionsDirective>(first(
513     "SECTIONS" >> pure(llvm::omp::Directive::OMPD_sections),
514     "PARALLEL SECTIONS" >> pure(llvm::omp::Directive::OMPD_parallel_sections))))
515 
516 // OMP BEGIN and END SECTIONS Directive
517 TYPE_PARSER(sourced(construct<OmpBeginSectionsDirective>(
518     sourced(Parser<OmpSectionsDirective>{}), Parser<OmpClauseList>{})))
519 TYPE_PARSER(
520     startOmpLine >> sourced(construct<OmpEndSectionsDirective>(
521                         sourced("END"_tok >> Parser<OmpSectionsDirective>{}),
522                         Parser<OmpClauseList>{})))
523 
524 // OMP SECTION-BLOCK
525 TYPE_PARSER(maybe(startOmpLine >> "SECTION"_tok / endOmpLine) >>
526     construct<OmpSectionBlocks>(
527         nonemptySeparated(block, startOmpLine >> "SECTION"_tok / endOmpLine)))
528 
529 // OMP SECTIONS (2.7.2), PARALLEL SECTIONS (2.11.2)
530 TYPE_PARSER(construct<OpenMPSectionsConstruct>(
531     Parser<OmpBeginSectionsDirective>{} / endOmpLine,
532     Parser<OmpSectionBlocks>{}, Parser<OmpEndSectionsDirective>{} / endOmpLine))
533 
534 TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
535     startOmpLine >>
536         first(construct<OpenMPConstruct>(Parser<OpenMPSectionsConstruct>{}),
537             construct<OpenMPConstruct>(Parser<OpenMPLoopConstruct>{}),
538             construct<OpenMPConstruct>(Parser<OpenMPBlockConstruct>{}),
539             // OpenMPBlockConstruct is attempted before
540             // OpenMPStandaloneConstruct to resolve !$OMP ORDERED
541             construct<OpenMPConstruct>(Parser<OpenMPStandaloneConstruct>{}),
542             construct<OpenMPConstruct>(Parser<OpenMPAtomicConstruct>{}),
543             construct<OpenMPConstruct>(Parser<OpenMPExecutableAllocate>{}),
544             construct<OpenMPConstruct>(Parser<OpenMPDeclarativeAllocate>{}),
545             construct<OpenMPConstruct>(Parser<OpenMPCriticalConstruct>{})))
546 
547 // END OMP Block directives
548 TYPE_PARSER(
549     startOmpLine >> sourced(construct<OmpEndBlockDirective>(
550                         sourced("END"_tok >> Parser<OmpBlockDirective>{}),
551                         Parser<OmpClauseList>{})))
552 
553 // END OMP Loop directives
554 TYPE_PARSER(
555     startOmpLine >> sourced(construct<OmpEndLoopDirective>(
556                         sourced("END"_tok >> Parser<OmpLoopDirective>{}),
557                         Parser<OmpClauseList>{})))
558 
559 TYPE_PARSER(construct<OpenMPLoopConstruct>(
560     Parser<OmpBeginLoopDirective>{} / endOmpLine))
561 } // namespace Fortran::parser
562