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