xref: /llvm-project/flang/lib/Parser/openmp-parsers.cpp (revision c9e967af3fc7ce824cd811379c5e99a998819779)
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     "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 >>
171         construct<OmpClause>(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<OmpDistScheduleClause>(
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<OmpNowait>()) ||
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" >>
220         construct<OmpClause>(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 TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
322     maybe(Parser<OmpMemoryOrderClause>{}),
323     maybe(parenthesized(Parser<OmpObjectList>{})))))
324 
325 // Simple Standalone Directives
326 TYPE_PARSER(sourced(construct<OmpSimpleStandaloneDirective>(first(
327     "BARRIER" >> pure(llvm::omp::Directive::OMPD_barrier),
328     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
329     "TARGET ENTER DATA" >> pure(llvm::omp::Directive::OMPD_target_enter_data),
330     "TARGET EXIT DATA" >> pure(llvm::omp::Directive::OMPD_target_exit_data),
331     "TARGET UPDATE" >> pure(llvm::omp::Directive::OMPD_target_update),
332     "TASKWAIT" >> pure(llvm::omp::Directive::OMPD_taskwait),
333     "TASKYIELD" >> pure(llvm::omp::Directive::OMPD_taskyield)))))
334 
335 TYPE_PARSER(sourced(construct<OpenMPSimpleStandaloneConstruct>(
336     Parser<OmpSimpleStandaloneDirective>{}, Parser<OmpClauseList>{})))
337 
338 // Standalone Constructs
339 TYPE_PARSER(
340     sourced(construct<OpenMPStandaloneConstruct>(
341                 Parser<OpenMPSimpleStandaloneConstruct>{}) ||
342         construct<OpenMPStandaloneConstruct>(Parser<OpenMPFlushConstruct>{}) ||
343         construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
344         construct<OpenMPStandaloneConstruct>(
345             Parser<OpenMPCancellationPointConstruct>{})) /
346     endOfLine)
347 
348 // Directives enclosing structured-block
349 TYPE_PARSER(construct<OmpBlockDirective>(first(
350     "MASTER" >> pure(llvm::omp::Directive::OMPD_master),
351     "ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
352     "PARALLEL WORKSHARE" >> pure(llvm::omp::Directive::OMPD_parallel_workshare),
353     "PARALLEL" >> pure(llvm::omp::Directive::OMPD_parallel),
354     "SINGLE" >> pure(llvm::omp::Directive::OMPD_single),
355     "TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
356     "TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),
357     "TARGET TEAMS" >> pure(llvm::omp::Directive::OMPD_target_teams),
358     "TARGET" >> pure(llvm::omp::Directive::OMPD_target),
359     "TASK"_id >> pure(llvm::omp::Directive::OMPD_task),
360     "TASKGROUP" >> pure(llvm::omp::Directive::OMPD_taskgroup),
361     "TEAMS" >> pure(llvm::omp::Directive::OMPD_teams),
362     "WORKSHARE" >> pure(llvm::omp::Directive::OMPD_workshare))))
363 
364 TYPE_PARSER(sourced(construct<OmpBeginBlockDirective>(
365     sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{})))
366 
367 TYPE_PARSER(construct<OmpReductionInitializerClause>(
368     "INITIALIZER" >> parenthesized("OMP_PRIV =" >> expr)))
369 
370 // 2.16 Declare Reduction Construct
371 TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
372     verbatim("DECLARE REDUCTION"_tok),
373     "(" >> Parser<OmpReductionOperator>{} / ":",
374     nonemptyList(Parser<DeclarationTypeSpec>{}) / ":",
375     Parser<OmpReductionCombiner>{} / ")",
376     maybe(Parser<OmpReductionInitializerClause>{}))))
377 
378 // declare-target with list
379 TYPE_PARSER(sourced(construct<OmpDeclareTargetWithList>(
380     parenthesized(Parser<OmpObjectList>{}))))
381 
382 // declare-target with clause
383 TYPE_PARSER(
384     sourced(construct<OmpDeclareTargetWithClause>(Parser<OmpClauseList>{})))
385 
386 // declare-target-specifier
387 TYPE_PARSER(
388     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithList>{}) ||
389     construct<OmpDeclareTargetSpecifier>(Parser<OmpDeclareTargetWithClause>{}))
390 
391 // 2.10.6 Declare Target Construct
392 TYPE_PARSER(sourced(construct<OpenMPDeclareTargetConstruct>(
393     verbatim("DECLARE TARGET"_tok), Parser<OmpDeclareTargetSpecifier>{})))
394 
395 TYPE_PARSER(construct<OmpReductionCombiner>(Parser<AssignmentStmt>{}) ||
396     construct<OmpReductionCombiner>(
397         construct<OmpReductionCombiner::FunctionCombiner>(
398             construct<Call>(Parser<ProcedureDesignator>{},
399                 parenthesized(optionalList(actualArgSpec))))))
400 
401 // 2.17.7 atomic -> ATOMIC [clause [,]] atomic-clause [[,] clause] |
402 //                  ATOMIC [clause]
403 //       clause -> memory-order-clause | HINT(hint-expression)
404 //       memory-order-clause -> SEQ_CST | ACQ_REL | RELEASE | ACQUIRE | RELAXED
405 //       atomic-clause -> READ | WRITE | UPDATE | CAPTURE
406 
407 // OMP END ATOMIC
408 TYPE_PARSER(construct<OmpEndAtomic>(startOmpLine >> "END ATOMIC"_tok))
409 
410 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] READ [MEMORY-ORDER-CLAUSE-LIST]
411 TYPE_PARSER("ATOMIC" >>
412     construct<OmpAtomicRead>(Parser<OmpClauseList>{} / maybe(","_tok),
413         verbatim("READ"_tok), Parser<OmpClauseList>{} / endOmpLine,
414         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
415 
416 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] CAPTURE [MEMORY-ORDER-CLAUSE-LIST]
417 TYPE_PARSER("ATOMIC" >>
418     construct<OmpAtomicCapture>(Parser<OmpClauseList>{} / maybe(","_tok),
419         verbatim("CAPTURE"_tok), Parser<OmpClauseList>{} / endOmpLine,
420         statement(assignmentStmt), statement(assignmentStmt),
421         Parser<OmpEndAtomic>{} / endOmpLine))
422 
423 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] UPDATE [MEMORY-ORDER-CLAUSE-LIST]
424 TYPE_PARSER("ATOMIC" >>
425     construct<OmpAtomicUpdate>(Parser<OmpClauseList>{} / maybe(","_tok),
426         verbatim("UPDATE"_tok), Parser<OmpClauseList>{} / endOmpLine,
427         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
428 
429 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST]
430 TYPE_PARSER(construct<OmpAtomic>(verbatim("ATOMIC"_tok),
431     Parser<OmpClauseList>{} / endOmpLine, statement(assignmentStmt),
432     maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
433 
434 // OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] WRITE [MEMORY-ORDER-CLAUSE-LIST]
435 TYPE_PARSER("ATOMIC" >>
436     construct<OmpAtomicWrite>(Parser<OmpClauseList>{} / maybe(","_tok),
437         verbatim("WRITE"_tok), Parser<OmpClauseList>{} / endOmpLine,
438         statement(assignmentStmt), maybe(Parser<OmpEndAtomic>{} / endOmpLine)))
439 
440 // Atomic Construct
441 TYPE_PARSER(construct<OpenMPAtomicConstruct>(Parser<OmpAtomicRead>{}) ||
442     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicCapture>{}) ||
443     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicWrite>{}) ||
444     construct<OpenMPAtomicConstruct>(Parser<OmpAtomicUpdate>{}) ||
445     construct<OpenMPAtomicConstruct>(Parser<OmpAtomic>{}))
446 
447 // 2.13.2 OMP CRITICAL
448 TYPE_PARSER(startOmpLine >>
449     sourced(construct<OmpEndCriticalDirective>(
450         verbatim("END CRITICAL"_tok), maybe(parenthesized(name)))) /
451         endOmpLine)
452 TYPE_PARSER(sourced(construct<OmpCriticalDirective>(verbatim("CRITICAL"_tok),
453                 maybe(parenthesized(name)), maybe(Parser<OmpClause>{}))) /
454     endOmpLine)
455 
456 TYPE_PARSER(construct<OpenMPCriticalConstruct>(
457     Parser<OmpCriticalDirective>{}, block, Parser<OmpEndCriticalDirective>{}))
458 
459 // 2.11.3 Executable Allocate directive
460 TYPE_PARSER(
461     sourced(construct<OpenMPExecutableAllocate>(verbatim("ALLOCATE"_tok),
462         maybe(parenthesized(Parser<OmpObjectList>{})), Parser<OmpClauseList>{},
463         maybe(nonemptyList(Parser<OpenMPDeclarativeAllocate>{})) / endOmpLine,
464         statement(allocateStmt))))
465 
466 // 2.8.2 Declare Simd construct
467 TYPE_PARSER(
468     sourced(construct<OpenMPDeclareSimdConstruct>(verbatim("DECLARE SIMD"_tok),
469         maybe(parenthesized(name)), Parser<OmpClauseList>{})))
470 
471 // 2.15.2 Threadprivate directive
472 TYPE_PARSER(sourced(construct<OpenMPThreadprivate>(
473     verbatim("THREADPRIVATE"_tok), parenthesized(Parser<OmpObjectList>{}))))
474 
475 // 2.11.3 Declarative Allocate directive
476 TYPE_PARSER(
477     sourced(construct<OpenMPDeclarativeAllocate>(verbatim("ALLOCATE"_tok),
478         parenthesized(Parser<OmpObjectList>{}), Parser<OmpClauseList>{})) /
479     lookAhead(endOmpLine / !statement(allocateStmt)))
480 
481 // Declarative constructs
482 TYPE_PARSER(startOmpLine >>
483     sourced(construct<OpenMPDeclarativeConstruct>(
484                 Parser<OpenMPDeclareReductionConstruct>{}) ||
485         construct<OpenMPDeclarativeConstruct>(
486             Parser<OpenMPDeclareSimdConstruct>{}) ||
487         construct<OpenMPDeclarativeConstruct>(
488             Parser<OpenMPDeclareTargetConstruct>{}) ||
489         construct<OpenMPDeclarativeConstruct>(
490             Parser<OpenMPDeclarativeAllocate>{}) ||
491         construct<OpenMPDeclarativeConstruct>(Parser<OpenMPThreadprivate>{})) /
492         endOmpLine)
493 
494 // Block Construct
495 TYPE_PARSER(construct<OpenMPBlockConstruct>(
496     Parser<OmpBeginBlockDirective>{} / endOmpLine, block,
497     Parser<OmpEndBlockDirective>{} / endOmpLine))
498 
499 // OMP SECTIONS Directive
500 TYPE_PARSER(construct<OmpSectionsDirective>(first(
501     "SECTIONS" >> pure(llvm::omp::Directive::OMPD_sections),
502     "PARALLEL SECTIONS" >> pure(llvm::omp::Directive::OMPD_parallel_sections))))
503 
504 // OMP BEGIN and END SECTIONS Directive
505 TYPE_PARSER(sourced(construct<OmpBeginSectionsDirective>(
506     sourced(Parser<OmpSectionsDirective>{}), Parser<OmpClauseList>{})))
507 TYPE_PARSER(
508     startOmpLine >> sourced(construct<OmpEndSectionsDirective>(
509                         sourced("END"_tok >> Parser<OmpSectionsDirective>{}),
510                         Parser<OmpClauseList>{})))
511 
512 // OMP SECTION-BLOCK
513 TYPE_PARSER(maybe(startOmpLine >> "SECTION"_tok / endOmpLine) >>
514     construct<OmpSectionBlocks>(
515         nonemptySeparated(block, startOmpLine >> "SECTION"_tok / endOmpLine)))
516 
517 // OMP SECTIONS (2.7.2), PARALLEL SECTIONS (2.11.2)
518 TYPE_PARSER(construct<OpenMPSectionsConstruct>(
519     Parser<OmpBeginSectionsDirective>{} / endOmpLine,
520     Parser<OmpSectionBlocks>{}, Parser<OmpEndSectionsDirective>{} / endOmpLine))
521 
522 TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
523     startOmpLine >>
524         first(construct<OpenMPConstruct>(Parser<OpenMPSectionsConstruct>{}),
525             construct<OpenMPConstruct>(Parser<OpenMPLoopConstruct>{}),
526             construct<OpenMPConstruct>(Parser<OpenMPBlockConstruct>{}),
527             // OpenMPBlockConstruct is attempted before
528             // OpenMPStandaloneConstruct to resolve !$OMP ORDERED
529             construct<OpenMPConstruct>(Parser<OpenMPStandaloneConstruct>{}),
530             construct<OpenMPConstruct>(Parser<OpenMPAtomicConstruct>{}),
531             construct<OpenMPConstruct>(Parser<OpenMPExecutableAllocate>{}),
532             construct<OpenMPConstruct>(Parser<OpenMPDeclarativeAllocate>{}),
533             construct<OpenMPConstruct>(Parser<OpenMPCriticalConstruct>{})))
534 
535 // END OMP Block directives
536 TYPE_PARSER(
537     startOmpLine >> sourced(construct<OmpEndBlockDirective>(
538                         sourced("END"_tok >> Parser<OmpBlockDirective>{}),
539                         Parser<OmpClauseList>{})))
540 
541 // END OMP Loop directives
542 TYPE_PARSER(
543     startOmpLine >> sourced(construct<OmpEndLoopDirective>(
544                         sourced("END"_tok >> Parser<OmpLoopDirective>{}),
545                         Parser<OmpClauseList>{})))
546 
547 TYPE_PARSER(construct<OpenMPLoopConstruct>(
548     Parser<OmpBeginLoopDirective>{} / endOmpLine))
549 } // namespace Fortran::parser
550