1 //===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \brief This file implements parsing of all OpenMP directives and clauses.
12 //===----------------------------------------------------------------------===//
14 #include "RAIIObjectsForParser.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/StmtOpenMP.h"
18 #include "clang/Parse/ParseDiagnostic.h"
19 #include "clang/Parse/Parser.h"
20 #include "clang/Sema/Scope.h"
21 #include "llvm/ADT/PointerIntPair.h"
23 using namespace clang;
25 //===----------------------------------------------------------------------===//
26 // OpenMP declarative directives.
27 //===----------------------------------------------------------------------===//
29 static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
30 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
31 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
32 // TODO: add other combined directives in topological order.
33 const OpenMPDirectiveKind F[][3] = {
34 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
35 OMPD_cancellation_point},
36 {OMPD_target, OMPD_unknown /*data*/, OMPD_target_data},
37 {OMPD_target, OMPD_unknown /*enter/exit*/,
38 OMPD_unknown /*target enter/exit*/},
39 {OMPD_unknown /*target enter*/, OMPD_unknown /*data*/,
40 OMPD_target_enter_data},
41 {OMPD_unknown /*target exit*/, OMPD_unknown /*data*/,
42 OMPD_target_exit_data},
43 {OMPD_for, OMPD_simd, OMPD_for_simd},
44 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
45 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
46 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
47 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}};
48 auto Tok = P.getCurToken();
52 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
54 bool TokenMatched = false;
55 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
56 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
59 !P.getPreprocessor().getSpelling(Tok).compare("cancellation")) ||
61 !P.getPreprocessor().getSpelling(Tok).compare("enter")) ||
62 ((i == 4) && !P.getPreprocessor().getSpelling(Tok).compare("exit"));
64 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
68 Tok = P.getPreprocessor().LookAhead(0);
69 auto TokenIsAnnotation = Tok.isAnnotation();
73 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
75 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
78 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
79 ((i == 1 || i == 3 || i == 4) &&
80 !P.getPreprocessor().getSpelling(Tok).compare("data")) ||
82 (!P.getPreprocessor().getSpelling(Tok).compare("enter") ||
83 !P.getPreprocessor().getSpelling(Tok).compare("exit")));
85 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
97 /// \brief Parsing of declarative OpenMP directives.
99 /// threadprivate-directive:
100 /// annot_pragma_openmp 'threadprivate' simple-variable-list
102 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
103 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
104 ParenBraceBracketBalancer BalancerRAIIObj(*this);
106 SourceLocation Loc = ConsumeToken();
107 SmallVector<Expr *, 5> Identifiers;
108 auto DKind = ParseOpenMPDirectiveKind(*this);
111 case OMPD_threadprivate:
113 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
114 // The last seen token is annot_pragma_openmp_end - need to check for
116 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
117 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
118 << getOpenMPDirectiveName(OMPD_threadprivate);
119 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
121 // Skip the last annot_pragma_openmp_end.
123 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
127 Diag(Tok, diag::err_omp_unknown_directive);
145 case OMPD_parallel_for:
146 case OMPD_parallel_for_simd:
147 case OMPD_parallel_sections:
151 case OMPD_cancellation_point:
153 case OMPD_target_data:
154 case OMPD_target_enter_data:
155 case OMPD_target_exit_data:
157 case OMPD_taskloop_simd:
158 case OMPD_distribute:
159 Diag(Tok, diag::err_omp_unexpected_directive)
160 << getOpenMPDirectiveName(DKind);
163 SkipUntil(tok::annot_pragma_openmp_end);
167 /// \brief Parsing of declarative or executable OpenMP directives.
169 /// threadprivate-directive:
170 /// annot_pragma_openmp 'threadprivate' simple-variable-list
171 /// annot_pragma_openmp_end
173 /// executable-directive:
174 /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
175 /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
176 /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
177 /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
178 /// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
179 /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause} |
180 /// 'distribute' | 'target enter data' | 'target exit data'
181 /// annot_pragma_openmp_end
183 StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
184 AllowedContsructsKind Allowed) {
185 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
186 ParenBraceBracketBalancer BalancerRAIIObj(*this);
187 SmallVector<Expr *, 5> Identifiers;
188 SmallVector<OMPClause *, 5> Clauses;
189 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
190 FirstClauses(OMPC_unknown + 1);
191 unsigned ScopeFlags =
192 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
193 SourceLocation Loc = ConsumeToken(), EndLoc;
194 auto DKind = ParseOpenMPDirectiveKind(*this);
195 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
196 // Name of critical directive.
197 DeclarationNameInfo DirName;
198 StmtResult Directive = StmtError();
199 bool HasAssociatedStatement = true;
200 bool FlushHasClause = false;
203 case OMPD_threadprivate:
204 if (Allowed != ACK_Any) {
205 Diag(Tok, diag::err_omp_immediate_directive)
206 << getOpenMPDirectiveName(DKind) << 0;
209 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
210 // The last seen token is annot_pragma_openmp_end - need to check for
212 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
213 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
214 << getOpenMPDirectiveName(OMPD_threadprivate);
215 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
218 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
219 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
221 SkipUntil(tok::annot_pragma_openmp_end);
224 if (PP.LookAhead(0).is(tok::l_paren)) {
225 FlushHasClause = true;
226 // Push copy of the current token back to stream to properly parse
227 // pseudo-clause OMPFlushClause.
233 case OMPD_cancellation_point:
235 case OMPD_target_enter_data:
236 case OMPD_target_exit_data:
237 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
238 Diag(Tok, diag::err_omp_immediate_directive)
239 << getOpenMPDirectiveName(DKind) << 0;
241 HasAssociatedStatement = false;
242 // Fall through for further analysis.
252 case OMPD_parallel_for:
253 case OMPD_parallel_for_simd:
254 case OMPD_parallel_sections:
261 case OMPD_target_data:
263 case OMPD_taskloop_simd:
264 case OMPD_distribute: {
266 // Parse directive name of the 'critical' directive if any.
267 if (DKind == OMPD_critical) {
268 BalancedDelimiterTracker T(*this, tok::l_paren,
269 tok::annot_pragma_openmp_end);
270 if (!T.consumeOpen()) {
271 if (Tok.isAnyIdentifier()) {
273 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
276 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
280 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
281 CancelRegion = ParseOpenMPDirectiveKind(*this);
282 if (Tok.isNot(tok::annot_pragma_openmp_end))
286 if (isOpenMPLoopDirective(DKind))
287 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
288 if (isOpenMPSimdDirective(DKind))
289 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
290 ParseScope OMPDirectiveScope(this, ScopeFlags);
291 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
293 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
294 OpenMPClauseKind CKind =
297 : FlushHasClause ? OMPC_flush
298 : getOpenMPClauseKind(PP.getSpelling(Tok));
299 Actions.StartOpenMPClause(CKind);
300 FlushHasClause = false;
302 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
303 FirstClauses[CKind].setInt(true);
305 FirstClauses[CKind].setPointer(Clause);
306 Clauses.push_back(Clause);
310 if (Tok.is(tok::comma))
312 Actions.EndOpenMPClause();
314 // End location of the directive.
315 EndLoc = Tok.getLocation();
316 // Consume final annot_pragma_openmp_end.
319 // OpenMP [2.13.8, ordered Construct, Syntax]
320 // If the depend clause is specified, the ordered construct is a stand-alone
322 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
323 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
324 Diag(Loc, diag::err_omp_immediate_directive)
325 << getOpenMPDirectiveName(DKind) << 1
326 << getOpenMPClauseName(OMPC_depend);
328 HasAssociatedStatement = false;
331 StmtResult AssociatedStmt;
332 if (HasAssociatedStatement) {
333 // The body is a block scope like in Lambdas and Blocks.
334 Sema::CompoundScopeRAII CompoundScope(Actions);
335 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
336 Actions.ActOnStartOfCompoundStmt();
338 AssociatedStmt = ParseStatement();
339 Actions.ActOnFinishOfCompoundStmt();
340 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
342 Directive = Actions.ActOnOpenMPExecutableDirective(
343 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
347 Actions.EndOpenMPDSABlock(Directive.get());
348 OMPDirectiveScope.Exit();
352 Diag(Tok, diag::err_omp_unknown_directive);
353 SkipUntil(tok::annot_pragma_openmp_end);
359 /// \brief Parses list of simple variables for '#pragma omp threadprivate'
362 /// simple-variable-list:
363 /// '(' id-expression {, id-expression} ')'
365 bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
366 SmallVectorImpl<Expr *> &VarList,
367 bool AllowScopeSpecifier) {
370 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
371 if (T.expectAndConsume(diag::err_expected_lparen_after,
372 getOpenMPDirectiveName(Kind)))
374 bool IsCorrect = true;
375 bool NoIdentIsFound = true;
377 // Read tokens while ')' or annot_pragma_openmp_end is not found.
378 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
380 SourceLocation TemplateKWLoc;
384 NoIdentIsFound = false;
386 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
387 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
389 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
391 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
392 TemplateKWLoc, Name)) {
394 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
396 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
397 Tok.isNot(tok::annot_pragma_openmp_end)) {
399 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
401 Diag(PrevTok.getLocation(), diag::err_expected)
403 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
405 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
407 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
409 VarList.push_back(Res.get());
412 if (Tok.is(tok::comma)) {
417 if (NoIdentIsFound) {
418 Diag(Tok, diag::err_expected) << tok::identifier;
423 IsCorrect = !T.consumeClose() && IsCorrect;
425 return !IsCorrect && VarList.empty();
428 /// \brief Parsing of OpenMP clauses.
431 /// if-clause | final-clause | num_threads-clause | safelen-clause |
432 /// default-clause | private-clause | firstprivate-clause | shared-clause
433 /// | linear-clause | aligned-clause | collapse-clause |
434 /// lastprivate-clause | reduction-clause | proc_bind-clause |
435 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
436 /// mergeable-clause | flush-clause | read-clause | write-clause |
437 /// update-clause | capture-clause | seq_cst-clause | device-clause |
438 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
439 /// thread_limit-clause | priority-clause | grainsize-clause |
440 /// nogroup-clause | num_tasks-clause | hint-clause
442 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
443 OpenMPClauseKind CKind, bool FirstClause) {
444 OMPClause *Clause = nullptr;
445 bool ErrorFound = false;
446 // Check if clause is allowed for the given directive.
447 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
448 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
449 << getOpenMPDirectiveName(DKind);
455 case OMPC_num_threads:
462 case OMPC_thread_limit:
467 // OpenMP [2.5, Restrictions]
468 // At most one num_threads clause can appear on the directive.
469 // OpenMP [2.8.1, simd construct, Restrictions]
470 // Only one safelen clause can appear on a simd directive.
471 // Only one simdlen clause can appear on a simd directive.
472 // Only one collapse clause can appear on a simd directive.
473 // OpenMP [2.9.1, target data construct, Restrictions]
474 // At most one device clause can appear on the directive.
475 // OpenMP [2.11.1, task Construct, Restrictions]
476 // At most one if clause can appear on the directive.
477 // At most one final clause can appear on the directive.
478 // OpenMP [teams Construct, Restrictions]
479 // At most one num_teams clause can appear on the directive.
480 // At most one thread_limit clause can appear on the directive.
481 // OpenMP [2.9.1, task Construct, Restrictions]
482 // At most one priority clause can appear on the directive.
483 // OpenMP [2.9.2, taskloop Construct, Restrictions]
484 // At most one grainsize clause can appear on the directive.
485 // OpenMP [2.9.2, taskloop Construct, Restrictions]
486 // At most one num_tasks clause can appear on the directive.
488 Diag(Tok, diag::err_omp_more_one_clause)
489 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
493 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
494 Clause = ParseOpenMPClause(CKind);
496 Clause = ParseOpenMPSingleExprClause(CKind);
500 // OpenMP [2.14.3.1, Restrictions]
501 // Only a single default clause may be specified on a parallel, task or
503 // OpenMP [2.5, parallel Construct, Restrictions]
504 // At most one proc_bind clause can appear on the directive.
506 Diag(Tok, diag::err_omp_more_one_clause)
507 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
511 Clause = ParseOpenMPSimpleClause(CKind);
514 case OMPC_dist_schedule:
515 case OMPC_defaultmap:
516 // OpenMP [2.7.1, Restrictions, p. 3]
517 // Only one schedule clause can appear on a loop directive.
518 // OpenMP [2.10.4, Restrictions, p. 106]
519 // At most one defaultmap clause can appear on the directive.
521 Diag(Tok, diag::err_omp_more_one_clause)
522 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
527 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
540 // OpenMP [2.7.1, Restrictions, p. 9]
541 // Only one ordered clause can appear on a loop directive.
542 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
543 // Only one nowait clause can appear on a for directive.
545 Diag(Tok, diag::err_omp_more_one_clause)
546 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
550 Clause = ParseOpenMPClause(CKind);
553 case OMPC_firstprivate:
554 case OMPC_lastprivate:
560 case OMPC_copyprivate:
564 Clause = ParseOpenMPVarListClause(DKind, CKind);
567 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
568 << getOpenMPDirectiveName(DKind);
569 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
571 case OMPC_threadprivate:
572 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
573 << getOpenMPDirectiveName(DKind);
574 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
577 return ErrorFound ? nullptr : Clause;
580 /// \brief Parsing of OpenMP clauses with single expressions like 'final',
581 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
582 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
585 /// 'final' '(' expression ')'
587 /// num_threads-clause:
588 /// 'num_threads' '(' expression ')'
591 /// 'safelen' '(' expression ')'
594 /// 'simdlen' '(' expression ')'
597 /// 'collapse' '(' expression ')'
600 /// 'priority' '(' expression ')'
602 /// grainsize-clause:
603 /// 'grainsize' '(' expression ')'
605 /// num_tasks-clause:
606 /// 'num_tasks' '(' expression ')'
609 /// 'hint' '(' expression ')'
611 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
612 SourceLocation Loc = ConsumeToken();
614 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
615 if (T.expectAndConsume(diag::err_expected_lparen_after,
616 getOpenMPClauseName(Kind)))
619 SourceLocation ELoc = Tok.getLocation();
620 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
621 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
622 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
630 return Actions.ActOnOpenMPSingleExprClause(
631 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
634 /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
637 /// 'default' '(' 'none' | 'shared' ')
639 /// proc_bind-clause:
640 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
642 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
643 SourceLocation Loc = Tok.getLocation();
644 SourceLocation LOpen = ConsumeToken();
646 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
647 if (T.expectAndConsume(diag::err_expected_lparen_after,
648 getOpenMPClauseName(Kind)))
651 unsigned Type = getOpenMPSimpleClauseType(
652 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
653 SourceLocation TypeLoc = Tok.getLocation();
654 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
655 Tok.isNot(tok::annot_pragma_openmp_end))
661 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
665 /// \brief Parsing of OpenMP clauses like 'ordered'.
676 /// mergeable-clause:
691 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
692 SourceLocation Loc = Tok.getLocation();
695 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
699 /// \brief Parsing of OpenMP clauses with single expressions and some additional
700 /// argument like 'schedule' or 'dist_schedule'.
703 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
707 /// 'if' '(' [ directive-name-modifier ':' ] expression ')'
710 /// 'defaultmap' '(' modifier ':' kind ')'
712 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
713 SourceLocation Loc = ConsumeToken();
714 SourceLocation DelimLoc;
716 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
717 if (T.expectAndConsume(diag::err_expected_lparen_after,
718 getOpenMPClauseName(Kind)))
722 SmallVector<unsigned, 4> Arg;
723 SmallVector<SourceLocation, 4> KLoc;
724 if (Kind == OMPC_schedule) {
725 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
726 Arg.resize(NumberOfElements);
727 KLoc.resize(NumberOfElements);
728 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
729 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
730 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
731 auto KindModifier = getOpenMPSimpleClauseType(
732 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
733 if (KindModifier > OMPC_SCHEDULE_unknown) {
735 Arg[Modifier1] = KindModifier;
736 KLoc[Modifier1] = Tok.getLocation();
737 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
738 Tok.isNot(tok::annot_pragma_openmp_end))
740 if (Tok.is(tok::comma)) {
741 // Parse ',' 'modifier'
743 KindModifier = getOpenMPSimpleClauseType(
744 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
745 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
747 : (unsigned)OMPC_SCHEDULE_unknown;
748 KLoc[Modifier2] = Tok.getLocation();
749 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
750 Tok.isNot(tok::annot_pragma_openmp_end))
754 if (Tok.is(tok::colon))
757 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
758 KindModifier = getOpenMPSimpleClauseType(
759 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
761 Arg[ScheduleKind] = KindModifier;
762 KLoc[ScheduleKind] = Tok.getLocation();
763 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
764 Tok.isNot(tok::annot_pragma_openmp_end))
766 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
767 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
768 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
770 DelimLoc = ConsumeAnyToken();
771 } else if (Kind == OMPC_dist_schedule) {
772 Arg.push_back(getOpenMPSimpleClauseType(
773 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
774 KLoc.push_back(Tok.getLocation());
775 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
776 Tok.isNot(tok::annot_pragma_openmp_end))
778 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
779 DelimLoc = ConsumeAnyToken();
780 } else if (Kind == OMPC_defaultmap) {
781 // Get a defaultmap modifier
782 Arg.push_back(getOpenMPSimpleClauseType(
783 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
784 KLoc.push_back(Tok.getLocation());
785 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
786 Tok.isNot(tok::annot_pragma_openmp_end))
789 if (Tok.is(tok::colon))
791 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
792 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
793 // Get a defaultmap kind
794 Arg.push_back(getOpenMPSimpleClauseType(
795 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
796 KLoc.push_back(Tok.getLocation());
797 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
798 Tok.isNot(tok::annot_pragma_openmp_end))
801 assert(Kind == OMPC_if);
802 KLoc.push_back(Tok.getLocation());
803 Arg.push_back(ParseOpenMPDirectiveKind(*this));
804 if (Arg.back() != OMPD_unknown) {
806 if (Tok.is(tok::colon))
807 DelimLoc = ConsumeToken();
809 Diag(Tok, diag::warn_pragma_expected_colon)
810 << "directive name modifier";
814 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
815 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
817 if (NeedAnExpression) {
818 SourceLocation ELoc = Tok.getLocation();
819 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
820 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
821 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
827 if (NeedAnExpression && Val.isInvalid())
830 return Actions.ActOnOpenMPSingleExprWithArgClause(
831 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
832 T.getCloseLocation());
835 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
836 UnqualifiedId &ReductionId) {
837 SourceLocation TemplateKWLoc;
838 if (ReductionIdScopeSpec.isEmpty()) {
840 switch (P.getCurToken().getKind()) {
868 if (OOK != OO_None) {
869 SourceLocation OpLoc = P.ConsumeToken();
870 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
871 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
875 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
876 /*AllowDestructorName*/ false,
877 /*AllowConstructorName*/ false, nullptr,
878 TemplateKWLoc, ReductionId);
881 /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
882 /// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
885 /// 'private' '(' list ')'
886 /// firstprivate-clause:
887 /// 'firstprivate' '(' list ')'
888 /// lastprivate-clause:
889 /// 'lastprivate' '(' list ')'
891 /// 'shared' '(' list ')'
893 /// 'linear' '(' linear-list [ ':' linear-step ] ')'
895 /// 'aligned' '(' list [ ':' alignment ] ')'
896 /// reduction-clause:
897 /// 'reduction' '(' reduction-identifier ':' list ')'
898 /// copyprivate-clause:
899 /// 'copyprivate' '(' list ')'
901 /// 'flush' '(' list ')'
903 /// 'depend' '(' in | out | inout : list | source ')'
905 /// 'map' '(' [ [ always , ]
906 /// to | from | tofrom | alloc | release | delete ':' ] list ')';
908 /// For 'linear' clause linear-list may have the following forms:
911 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
912 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
913 OpenMPClauseKind Kind) {
914 SourceLocation Loc = Tok.getLocation();
915 SourceLocation LOpen = ConsumeToken();
916 SourceLocation ColonLoc = SourceLocation();
917 // Optional scope specifier and unqualified id for reduction identifier.
918 CXXScopeSpec ReductionIdScopeSpec;
919 UnqualifiedId ReductionId;
920 bool InvalidReductionId = false;
921 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
922 // OpenMP 4.1 [2.15.3.7, linear Clause]
923 // If no modifier is specified it is assumed to be val.
924 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
925 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
926 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
927 bool MapTypeIsImplicit = false;
928 bool MapTypeModifierSpecified = false;
929 SourceLocation DepLinMapLoc;
932 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
933 if (T.expectAndConsume(diag::err_expected_lparen_after,
934 getOpenMPClauseName(Kind)))
937 bool NeedRParenForLinear = false;
938 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
939 tok::annot_pragma_openmp_end);
940 // Handle reduction-identifier for reduction clause.
941 if (Kind == OMPC_reduction) {
942 ColonProtectionRAIIObject ColonRAII(*this);
943 if (getLangOpts().CPlusPlus) {
944 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
947 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
948 if (InvalidReductionId) {
949 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
952 if (Tok.is(tok::colon)) {
953 ColonLoc = ConsumeToken();
955 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
957 } else if (Kind == OMPC_depend) {
958 // Handle dependency type for depend clause.
959 ColonProtectionRAIIObject ColonRAII(*this);
960 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
961 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
962 DepLinMapLoc = Tok.getLocation();
964 if (DepKind == OMPC_DEPEND_unknown) {
965 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
969 // Special processing for depend(source) clause.
970 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
973 return Actions.ActOnOpenMPVarListClause(
974 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
975 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
976 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
977 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
981 if (Tok.is(tok::colon)) {
982 ColonLoc = ConsumeToken();
984 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
985 : diag::warn_pragma_expected_colon)
986 << "dependency type";
988 } else if (Kind == OMPC_linear) {
989 // Try to parse modifier if any.
990 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
991 LinearModifier = static_cast<OpenMPLinearClauseKind>(
992 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
993 DepLinMapLoc = ConsumeToken();
994 LinearT.consumeOpen();
995 NeedRParenForLinear = true;
997 } else if (Kind == OMPC_map) {
998 // Handle map type for map clause.
999 ColonProtectionRAIIObject ColonRAII(*this);
1001 // the first identifier may be a list item, a map-type or
1002 // a map-type-modifier
1003 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1004 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1005 DepLinMapLoc = Tok.getLocation();
1006 bool ColonExpected = false;
1008 if (Tok.is(tok::identifier)) {
1009 if (PP.LookAhead(0).is(tok::colon)) {
1010 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1011 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1012 if (MapType == OMPC_MAP_unknown) {
1013 Diag(Tok, diag::err_omp_unknown_map_type);
1014 } else if (MapType == OMPC_MAP_always) {
1015 Diag(Tok, diag::err_omp_map_type_missing);
1018 } else if (PP.LookAhead(0).is(tok::comma)) {
1019 if (PP.LookAhead(1).is(tok::identifier) &&
1020 PP.LookAhead(2).is(tok::colon)) {
1022 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1023 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1024 if (MapTypeModifier != OMPC_MAP_always) {
1025 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1026 MapTypeModifier = OMPC_MAP_unknown;
1028 MapTypeModifierSpecified = true;
1034 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1035 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1036 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1037 Diag(Tok, diag::err_omp_unknown_map_type);
1041 MapType = OMPC_MAP_tofrom;
1042 MapTypeIsImplicit = true;
1045 MapType = OMPC_MAP_tofrom;
1046 MapTypeIsImplicit = true;
1049 MapType = OMPC_MAP_tofrom;
1050 MapTypeIsImplicit = true;
1053 if (Tok.is(tok::colon)) {
1054 ColonLoc = ConsumeToken();
1055 } else if (ColonExpected) {
1056 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1060 SmallVector<Expr *, 5> Vars;
1062 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1063 (Kind != OMPC_map)) ||
1064 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
1065 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
1066 (!MapTypeModifierSpecified ||
1067 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1068 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
1069 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1070 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1071 Tok.isNot(tok::annot_pragma_openmp_end))) {
1072 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1074 ExprResult VarExpr =
1075 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1076 if (VarExpr.isUsable()) {
1077 Vars.push_back(VarExpr.get());
1079 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1083 IsComma = Tok.is(tok::comma);
1086 else if (Tok.isNot(tok::r_paren) &&
1087 Tok.isNot(tok::annot_pragma_openmp_end) &&
1088 (!MayHaveTail || Tok.isNot(tok::colon)))
1089 Diag(Tok, diag::err_omp_expected_punc)
1090 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1091 : getOpenMPClauseName(Kind))
1092 << (Kind == OMPC_flush);
1095 // Parse ')' for linear clause with modifier.
1096 if (NeedRParenForLinear)
1097 LinearT.consumeClose();
1099 // Parse ':' linear-step (or ':' alignment).
1100 Expr *TailExpr = nullptr;
1101 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1103 ColonLoc = Tok.getLocation();
1104 SourceLocation ELoc = ConsumeToken();
1105 ExprResult Tail = ParseAssignmentExpression();
1106 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1107 if (Tail.isUsable())
1108 TailExpr = Tail.get();
1110 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1116 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
1117 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1118 (MustHaveTail && !TailExpr) || InvalidReductionId) {
1122 return Actions.ActOnOpenMPVarListClause(
1123 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1124 ReductionIdScopeSpec,
1125 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
1126 : DeclarationNameInfo(),
1127 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,