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