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