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_for, OMPD_simd, OMPD_for_simd},
38 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
39 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
40 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
41 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}};
42 auto Tok = P.getCurToken();
46 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
48 bool TokenMatched = false;
49 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
50 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
53 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
55 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
59 Tok = P.getPreprocessor().LookAhead(0);
60 auto TokenIsAnnotation = Tok.isAnnotation();
64 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
66 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
69 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
70 ((i == 1) && !P.getPreprocessor().getSpelling(Tok).compare("data"));
72 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
84 /// \brief Parsing of declarative OpenMP directives.
86 /// threadprivate-directive:
87 /// annot_pragma_openmp 'threadprivate' simple-variable-list
89 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
90 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
91 ParenBraceBracketBalancer BalancerRAIIObj(*this);
93 SourceLocation Loc = ConsumeToken();
94 SmallVector<Expr *, 5> Identifiers;
95 auto DKind = ParseOpenMPDirectiveKind(*this);
98 case OMPD_threadprivate:
100 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
101 // The last seen token is annot_pragma_openmp_end - need to check for
103 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
104 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
105 << getOpenMPDirectiveName(OMPD_threadprivate);
106 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
108 // Skip the last annot_pragma_openmp_end.
110 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
114 Diag(Tok, diag::err_omp_unknown_directive);
132 case OMPD_parallel_for:
133 case OMPD_parallel_for_simd:
134 case OMPD_parallel_sections:
138 case OMPD_cancellation_point:
140 case OMPD_target_data:
142 case OMPD_taskloop_simd:
143 case OMPD_distribute:
144 Diag(Tok, diag::err_omp_unexpected_directive)
145 << getOpenMPDirectiveName(DKind);
148 SkipUntil(tok::annot_pragma_openmp_end);
149 return DeclGroupPtrTy();
152 /// \brief Parsing of declarative or executable OpenMP directives.
154 /// threadprivate-directive:
155 /// annot_pragma_openmp 'threadprivate' simple-variable-list
156 /// annot_pragma_openmp_end
158 /// executable-directive:
159 /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
160 /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
161 /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
162 /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
163 /// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
164 /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause} |
166 /// annot_pragma_openmp_end
168 StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
169 AllowedContsructsKind Allowed) {
170 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
171 ParenBraceBracketBalancer BalancerRAIIObj(*this);
172 SmallVector<Expr *, 5> Identifiers;
173 SmallVector<OMPClause *, 5> Clauses;
174 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
175 FirstClauses(OMPC_unknown + 1);
176 unsigned ScopeFlags =
177 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
178 SourceLocation Loc = ConsumeToken(), EndLoc;
179 auto DKind = ParseOpenMPDirectiveKind(*this);
180 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
181 // Name of critical directive.
182 DeclarationNameInfo DirName;
183 StmtResult Directive = StmtError();
184 bool HasAssociatedStatement = true;
185 bool FlushHasClause = false;
188 case OMPD_threadprivate:
189 if (Allowed != ACK_Any) {
190 Diag(Tok, diag::err_omp_immediate_directive)
191 << getOpenMPDirectiveName(DKind) << 0;
194 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
195 // The last seen token is annot_pragma_openmp_end - need to check for
197 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
198 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
199 << getOpenMPDirectiveName(OMPD_threadprivate);
200 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
203 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
204 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
206 SkipUntil(tok::annot_pragma_openmp_end);
209 if (PP.LookAhead(0).is(tok::l_paren)) {
210 FlushHasClause = true;
211 // Push copy of the current token back to stream to properly parse
212 // pseudo-clause OMPFlushClause.
218 case OMPD_cancellation_point:
220 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
221 Diag(Tok, diag::err_omp_immediate_directive)
222 << getOpenMPDirectiveName(DKind) << 0;
224 HasAssociatedStatement = false;
225 // Fall through for further analysis.
235 case OMPD_parallel_for:
236 case OMPD_parallel_for_simd:
237 case OMPD_parallel_sections:
244 case OMPD_target_data:
246 case OMPD_taskloop_simd:
247 case OMPD_distribute: {
249 // Parse directive name of the 'critical' directive if any.
250 if (DKind == OMPD_critical) {
251 BalancedDelimiterTracker T(*this, tok::l_paren,
252 tok::annot_pragma_openmp_end);
253 if (!T.consumeOpen()) {
254 if (Tok.isAnyIdentifier()) {
256 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
259 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
263 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
264 CancelRegion = ParseOpenMPDirectiveKind(*this);
265 if (Tok.isNot(tok::annot_pragma_openmp_end))
269 if (isOpenMPLoopDirective(DKind))
270 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
271 if (isOpenMPSimdDirective(DKind))
272 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
273 ParseScope OMPDirectiveScope(this, ScopeFlags);
274 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
276 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
277 OpenMPClauseKind CKind =
280 : FlushHasClause ? OMPC_flush
281 : getOpenMPClauseKind(PP.getSpelling(Tok));
282 Actions.StartOpenMPClause(CKind);
283 FlushHasClause = false;
285 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
286 FirstClauses[CKind].setInt(true);
288 FirstClauses[CKind].setPointer(Clause);
289 Clauses.push_back(Clause);
293 if (Tok.is(tok::comma))
295 Actions.EndOpenMPClause();
297 // End location of the directive.
298 EndLoc = Tok.getLocation();
299 // Consume final annot_pragma_openmp_end.
302 // OpenMP [2.13.8, ordered Construct, Syntax]
303 // If the depend clause is specified, the ordered construct is a stand-alone
305 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
306 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
307 Diag(Loc, diag::err_omp_immediate_directive)
308 << getOpenMPDirectiveName(DKind) << 1
309 << getOpenMPClauseName(OMPC_depend);
311 HasAssociatedStatement = false;
314 StmtResult AssociatedStmt;
315 if (HasAssociatedStatement) {
316 // The body is a block scope like in Lambdas and Blocks.
317 Sema::CompoundScopeRAII CompoundScope(Actions);
318 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
319 Actions.ActOnStartOfCompoundStmt();
321 AssociatedStmt = ParseStatement();
322 Actions.ActOnFinishOfCompoundStmt();
323 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
325 Directive = Actions.ActOnOpenMPExecutableDirective(
326 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
330 Actions.EndOpenMPDSABlock(Directive.get());
331 OMPDirectiveScope.Exit();
335 Diag(Tok, diag::err_omp_unknown_directive);
336 SkipUntil(tok::annot_pragma_openmp_end);
342 /// \brief Parses list of simple variables for '#pragma omp threadprivate'
345 /// simple-variable-list:
346 /// '(' id-expression {, id-expression} ')'
348 bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
349 SmallVectorImpl<Expr *> &VarList,
350 bool AllowScopeSpecifier) {
353 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
354 if (T.expectAndConsume(diag::err_expected_lparen_after,
355 getOpenMPDirectiveName(Kind)))
357 bool IsCorrect = true;
358 bool NoIdentIsFound = true;
360 // Read tokens while ')' or annot_pragma_openmp_end is not found.
361 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
363 SourceLocation TemplateKWLoc;
367 NoIdentIsFound = false;
369 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
370 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
372 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
374 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
375 TemplateKWLoc, Name)) {
377 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
379 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
380 Tok.isNot(tok::annot_pragma_openmp_end)) {
382 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
384 Diag(PrevTok.getLocation(), diag::err_expected)
386 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
388 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
390 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
392 VarList.push_back(Res.get());
395 if (Tok.is(tok::comma)) {
400 if (NoIdentIsFound) {
401 Diag(Tok, diag::err_expected) << tok::identifier;
406 IsCorrect = !T.consumeClose() && IsCorrect;
408 return !IsCorrect && VarList.empty();
411 /// \brief Parsing of OpenMP clauses.
414 /// if-clause | final-clause | num_threads-clause | safelen-clause |
415 /// default-clause | private-clause | firstprivate-clause | shared-clause
416 /// | linear-clause | aligned-clause | collapse-clause |
417 /// lastprivate-clause | reduction-clause | proc_bind-clause |
418 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
419 /// mergeable-clause | flush-clause | read-clause | write-clause |
420 /// update-clause | capture-clause | seq_cst-clause | device-clause |
421 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
422 /// thread_limit-clause | priority-clause | grainsize-clause |
423 /// nogroup-clause | num_tasks-clause | hint-clause
425 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
426 OpenMPClauseKind CKind, bool FirstClause) {
427 OMPClause *Clause = nullptr;
428 bool ErrorFound = false;
429 // Check if clause is allowed for the given directive.
430 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
431 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
432 << getOpenMPDirectiveName(DKind);
438 case OMPC_num_threads:
445 case OMPC_thread_limit:
450 // OpenMP [2.5, Restrictions]
451 // At most one num_threads clause can appear on the directive.
452 // OpenMP [2.8.1, simd construct, Restrictions]
453 // Only one safelen clause can appear on a simd directive.
454 // Only one simdlen clause can appear on a simd directive.
455 // Only one collapse clause can appear on a simd directive.
456 // OpenMP [2.9.1, target data construct, Restrictions]
457 // At most one device clause can appear on the directive.
458 // OpenMP [2.11.1, task Construct, Restrictions]
459 // At most one if clause can appear on the directive.
460 // At most one final clause can appear on the directive.
461 // OpenMP [teams Construct, Restrictions]
462 // At most one num_teams clause can appear on the directive.
463 // At most one thread_limit clause can appear on the directive.
464 // OpenMP [2.9.1, task Construct, Restrictions]
465 // At most one priority clause can appear on the directive.
466 // OpenMP [2.9.2, taskloop Construct, Restrictions]
467 // At most one grainsize clause can appear on the directive.
468 // OpenMP [2.9.2, taskloop Construct, Restrictions]
469 // At most one num_tasks clause can appear on the directive.
471 Diag(Tok, diag::err_omp_more_one_clause)
472 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
476 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
477 Clause = ParseOpenMPClause(CKind);
479 Clause = ParseOpenMPSingleExprClause(CKind);
483 // OpenMP [2.14.3.1, Restrictions]
484 // Only a single default clause may be specified on a parallel, task or
486 // OpenMP [2.5, parallel Construct, Restrictions]
487 // At most one proc_bind clause can appear on the directive.
489 Diag(Tok, diag::err_omp_more_one_clause)
490 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
494 Clause = ParseOpenMPSimpleClause(CKind);
497 // OpenMP [2.7.1, Restrictions, p. 3]
498 // Only one schedule clause can appear on a loop directive.
500 Diag(Tok, diag::err_omp_more_one_clause)
501 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
506 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
519 // OpenMP [2.7.1, Restrictions, p. 9]
520 // Only one ordered clause can appear on a loop directive.
521 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
522 // Only one nowait clause can appear on a for directive.
524 Diag(Tok, diag::err_omp_more_one_clause)
525 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
529 Clause = ParseOpenMPClause(CKind);
532 case OMPC_firstprivate:
533 case OMPC_lastprivate:
539 case OMPC_copyprivate:
543 Clause = ParseOpenMPVarListClause(DKind, CKind);
546 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
547 << getOpenMPDirectiveName(DKind);
548 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
550 case OMPC_threadprivate:
551 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
552 << getOpenMPDirectiveName(DKind);
553 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
556 return ErrorFound ? nullptr : Clause;
559 /// \brief Parsing of OpenMP clauses with single expressions like 'final',
560 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
561 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
564 /// 'final' '(' expression ')'
566 /// num_threads-clause:
567 /// 'num_threads' '(' expression ')'
570 /// 'safelen' '(' expression ')'
573 /// 'simdlen' '(' expression ')'
576 /// 'collapse' '(' expression ')'
579 /// 'priority' '(' expression ')'
581 /// grainsize-clause:
582 /// 'grainsize' '(' expression ')'
584 /// num_tasks-clause:
585 /// 'num_tasks' '(' expression ')'
588 /// 'hint' '(' expression ')'
590 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
591 SourceLocation Loc = ConsumeToken();
593 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
594 if (T.expectAndConsume(diag::err_expected_lparen_after,
595 getOpenMPClauseName(Kind)))
598 SourceLocation ELoc = Tok.getLocation();
599 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
600 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
601 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
609 return Actions.ActOnOpenMPSingleExprClause(
610 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
613 /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
616 /// 'default' '(' 'none' | 'shared' ')
618 /// proc_bind-clause:
619 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
621 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
622 SourceLocation Loc = Tok.getLocation();
623 SourceLocation LOpen = ConsumeToken();
625 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
626 if (T.expectAndConsume(diag::err_expected_lparen_after,
627 getOpenMPClauseName(Kind)))
630 unsigned Type = getOpenMPSimpleClauseType(
631 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
632 SourceLocation TypeLoc = Tok.getLocation();
633 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
634 Tok.isNot(tok::annot_pragma_openmp_end))
640 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
644 /// \brief Parsing of OpenMP clauses like 'ordered'.
655 /// mergeable-clause:
670 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
671 SourceLocation Loc = Tok.getLocation();
674 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
678 /// \brief Parsing of OpenMP clauses with single expressions and some additional
679 /// argument like 'schedule' or 'dist_schedule'.
682 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
686 /// 'if' '(' [ directive-name-modifier ':' ] expression ')'
688 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
689 SourceLocation Loc = ConsumeToken();
690 SourceLocation DelimLoc;
692 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
693 if (T.expectAndConsume(diag::err_expected_lparen_after,
694 getOpenMPClauseName(Kind)))
698 SmallVector<unsigned, 4> Arg;
699 SmallVector<SourceLocation, 4> KLoc;
700 if (Kind == OMPC_schedule) {
701 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
702 Arg.resize(NumberOfElements);
703 KLoc.resize(NumberOfElements);
704 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
705 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
706 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
707 auto KindModifier = getOpenMPSimpleClauseType(
708 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
709 if (KindModifier > OMPC_SCHEDULE_unknown) {
711 Arg[Modifier1] = KindModifier;
712 KLoc[Modifier1] = Tok.getLocation();
713 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
714 Tok.isNot(tok::annot_pragma_openmp_end))
716 if (Tok.is(tok::comma)) {
717 // Parse ',' 'modifier'
719 KindModifier = getOpenMPSimpleClauseType(
720 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
721 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
723 : (unsigned)OMPC_SCHEDULE_unknown;
724 KLoc[Modifier2] = Tok.getLocation();
725 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
726 Tok.isNot(tok::annot_pragma_openmp_end))
730 if (Tok.is(tok::colon))
733 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
734 KindModifier = getOpenMPSimpleClauseType(
735 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
737 Arg[ScheduleKind] = KindModifier;
738 KLoc[ScheduleKind] = Tok.getLocation();
739 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
740 Tok.isNot(tok::annot_pragma_openmp_end))
742 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
743 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
744 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
746 DelimLoc = ConsumeAnyToken();
748 assert(Kind == OMPC_if);
749 KLoc.push_back(Tok.getLocation());
750 Arg.push_back(ParseOpenMPDirectiveKind(*this));
751 if (Arg.back() != OMPD_unknown) {
753 if (Tok.is(tok::colon))
754 DelimLoc = ConsumeToken();
756 Diag(Tok, diag::warn_pragma_expected_colon)
757 << "directive name modifier";
761 bool NeedAnExpression =
762 (Kind == OMPC_schedule && DelimLoc.isValid()) || Kind == OMPC_if;
763 if (NeedAnExpression) {
764 SourceLocation ELoc = Tok.getLocation();
765 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
766 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
767 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
773 if (NeedAnExpression && Val.isInvalid())
776 return Actions.ActOnOpenMPSingleExprWithArgClause(
777 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
778 T.getCloseLocation());
781 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
782 UnqualifiedId &ReductionId) {
783 SourceLocation TemplateKWLoc;
784 if (ReductionIdScopeSpec.isEmpty()) {
786 switch (P.getCurToken().getKind()) {
814 if (OOK != OO_None) {
815 SourceLocation OpLoc = P.ConsumeToken();
816 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
817 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
821 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
822 /*AllowDestructorName*/ false,
823 /*AllowConstructorName*/ false, ParsedType(),
824 TemplateKWLoc, ReductionId);
827 /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
828 /// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
831 /// 'private' '(' list ')'
832 /// firstprivate-clause:
833 /// 'firstprivate' '(' list ')'
834 /// lastprivate-clause:
835 /// 'lastprivate' '(' list ')'
837 /// 'shared' '(' list ')'
839 /// 'linear' '(' linear-list [ ':' linear-step ] ')'
841 /// 'aligned' '(' list [ ':' alignment ] ')'
842 /// reduction-clause:
843 /// 'reduction' '(' reduction-identifier ':' list ')'
844 /// copyprivate-clause:
845 /// 'copyprivate' '(' list ')'
847 /// 'flush' '(' list ')'
849 /// 'depend' '(' in | out | inout : list | source ')'
851 /// 'map' '(' [ [ always , ]
852 /// to | from | tofrom | alloc | release | delete ':' ] list ')';
854 /// For 'linear' clause linear-list may have the following forms:
857 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
858 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
859 OpenMPClauseKind Kind) {
860 SourceLocation Loc = Tok.getLocation();
861 SourceLocation LOpen = ConsumeToken();
862 SourceLocation ColonLoc = SourceLocation();
863 // Optional scope specifier and unqualified id for reduction identifier.
864 CXXScopeSpec ReductionIdScopeSpec;
865 UnqualifiedId ReductionId;
866 bool InvalidReductionId = false;
867 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
868 // OpenMP 4.1 [2.15.3.7, linear Clause]
869 // If no modifier is specified it is assumed to be val.
870 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
871 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
872 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
873 bool MapTypeModifierSpecified = false;
874 bool UnexpectedId = false;
875 SourceLocation DepLinMapLoc;
878 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
879 if (T.expectAndConsume(diag::err_expected_lparen_after,
880 getOpenMPClauseName(Kind)))
883 bool NeedRParenForLinear = false;
884 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
885 tok::annot_pragma_openmp_end);
886 // Handle reduction-identifier for reduction clause.
887 if (Kind == OMPC_reduction) {
888 ColonProtectionRAIIObject ColonRAII(*this);
889 if (getLangOpts().CPlusPlus) {
890 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
893 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
894 if (InvalidReductionId) {
895 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
898 if (Tok.is(tok::colon)) {
899 ColonLoc = ConsumeToken();
901 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
903 } else if (Kind == OMPC_depend) {
904 // Handle dependency type for depend clause.
905 ColonProtectionRAIIObject ColonRAII(*this);
906 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
907 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
908 DepLinMapLoc = Tok.getLocation();
910 if (DepKind == OMPC_DEPEND_unknown) {
911 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
915 // Special processing for depend(source) clause.
916 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
919 return Actions.ActOnOpenMPVarListClause(
920 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
921 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
922 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
923 LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);
926 if (Tok.is(tok::colon)) {
927 ColonLoc = ConsumeToken();
929 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
930 : diag::warn_pragma_expected_colon)
931 << "dependency type";
933 } else if (Kind == OMPC_linear) {
934 // Try to parse modifier if any.
935 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
936 LinearModifier = static_cast<OpenMPLinearClauseKind>(
937 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
938 DepLinMapLoc = ConsumeToken();
939 LinearT.consumeOpen();
940 NeedRParenForLinear = true;
942 } else if (Kind == OMPC_map) {
943 // Handle map type for map clause.
944 ColonProtectionRAIIObject ColonRAII(*this);
946 // the first identifier may be a list item, a map-type or
947 // a map-type-modifier
948 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
949 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
950 DepLinMapLoc = Tok.getLocation();
951 bool ColonExpected = false;
953 if (Tok.is(tok::identifier)) {
954 if (PP.LookAhead(0).is(tok::colon)) {
955 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
956 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
957 if (MapType == OMPC_MAP_unknown) {
958 Diag(Tok, diag::err_omp_unknown_map_type);
959 } else if (MapType == OMPC_MAP_always) {
960 Diag(Tok, diag::err_omp_map_type_missing);
963 } else if (PP.LookAhead(0).is(tok::comma)) {
964 if (PP.LookAhead(1).is(tok::identifier) &&
965 PP.LookAhead(2).is(tok::colon)) {
967 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
968 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
969 if (MapTypeModifier != OMPC_MAP_always) {
970 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
971 MapTypeModifier = OMPC_MAP_unknown;
973 MapTypeModifierSpecified = true;
979 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
980 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
981 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
982 Diag(Tok, diag::err_omp_unknown_map_type);
986 MapType = OMPC_MAP_tofrom;
989 MapType = OMPC_MAP_tofrom;
995 if (Tok.is(tok::colon)) {
996 ColonLoc = ConsumeToken();
997 } else if (ColonExpected) {
998 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1002 SmallVector<Expr *, 5> Vars;
1004 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1005 (Kind != OMPC_map)) ||
1006 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
1007 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
1008 (!MapTypeModifierSpecified ||
1009 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1010 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
1011 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1012 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1013 Tok.isNot(tok::annot_pragma_openmp_end))) {
1014 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1016 ExprResult VarExpr =
1017 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1018 if (VarExpr.isUsable()) {
1019 Vars.push_back(VarExpr.get());
1021 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1025 IsComma = Tok.is(tok::comma);
1028 else if (Tok.isNot(tok::r_paren) &&
1029 Tok.isNot(tok::annot_pragma_openmp_end) &&
1030 (!MayHaveTail || Tok.isNot(tok::colon)))
1031 Diag(Tok, diag::err_omp_expected_punc)
1032 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1033 : getOpenMPClauseName(Kind))
1034 << (Kind == OMPC_flush);
1037 // Parse ')' for linear clause with modifier.
1038 if (NeedRParenForLinear)
1039 LinearT.consumeClose();
1041 // Parse ':' linear-step (or ':' alignment).
1042 Expr *TailExpr = nullptr;
1043 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1045 ColonLoc = Tok.getLocation();
1046 SourceLocation ELoc = ConsumeToken();
1047 ExprResult Tail = ParseAssignmentExpression();
1048 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1049 if (Tail.isUsable())
1050 TailExpr = Tail.get();
1052 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1058 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
1059 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
1060 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
1061 InvalidReductionId) {
1065 return Actions.ActOnOpenMPVarListClause(
1066 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1067 ReductionIdScopeSpec,
1068 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
1069 : DeclarationNameInfo(),
1070 DepKind, LinearModifier, MapTypeModifier, MapType, DepLinMapLoc);