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