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 /// This file implements parsing of all OpenMP directives and clauses.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/StmtOpenMP.h"
16 #include "clang/Parse/ParseDiagnostic.h"
17 #include "clang/Parse/Parser.h"
18 #include "clang/Parse/RAIIObjectsForParser.h"
19 #include "clang/Sema/Scope.h"
20 #include "llvm/ADT/PointerIntPair.h"
22 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // OpenMP declarative directives.
26 //===----------------------------------------------------------------------===//
29 enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
42 OMPD_distribute_parallel,
43 OMPD_teams_distribute_parallel,
44 OMPD_target_teams_distribute_parallel
47 class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
57 Identifiers.push_back(Res.get());
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
63 // Map token string to extended OMP token kind that are
64 // OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65 static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
73 .Case("declare", OMPD_declare)
74 .Case("end", OMPD_end)
75 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
78 .Case("reduction", OMPD_reduction)
79 .Case("update", OMPD_update)
80 .Default(OMPD_unknown);
83 static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
84 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
87 static const unsigned F[][3] = {
88 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
89 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
90 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
91 {OMPD_declare, OMPD_target, OMPD_declare_target},
92 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
93 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
94 {OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd},
96 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
97 {OMPD_end, OMPD_declare, OMPD_end_declare},
98 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
99 {OMPD_target, OMPD_data, OMPD_target_data},
100 {OMPD_target, OMPD_enter, OMPD_target_enter},
101 {OMPD_target, OMPD_exit, OMPD_target_exit},
102 {OMPD_target, OMPD_update, OMPD_target_update},
103 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
104 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
105 {OMPD_for, OMPD_simd, OMPD_for_simd},
106 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
107 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
108 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
109 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
110 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
111 {OMPD_target, OMPD_simd, OMPD_target_simd},
112 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
113 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
114 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
115 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
116 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
117 {OMPD_teams_distribute_parallel, OMPD_for,
118 OMPD_teams_distribute_parallel_for},
119 {OMPD_teams_distribute_parallel_for, OMPD_simd,
120 OMPD_teams_distribute_parallel_for_simd},
121 {OMPD_target, OMPD_teams, OMPD_target_teams},
122 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
123 {OMPD_target_teams_distribute, OMPD_parallel,
124 OMPD_target_teams_distribute_parallel},
125 {OMPD_target_teams_distribute, OMPD_simd,
126 OMPD_target_teams_distribute_simd},
127 {OMPD_target_teams_distribute_parallel, OMPD_for,
128 OMPD_target_teams_distribute_parallel_for},
129 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
130 OMPD_target_teams_distribute_parallel_for_simd}};
131 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
132 Token Tok = P.getCurToken();
135 ? static_cast<unsigned>(OMPD_unknown)
136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
137 if (DKind == OMPD_unknown)
140 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
141 if (DKind != F[I][0])
144 Tok = P.getPreprocessor().LookAhead(0);
147 ? static_cast<unsigned>(OMPD_unknown)
148 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
149 if (SDKind == OMPD_unknown)
152 if (SDKind == F[I][1]) {
157 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
161 static DeclarationName parseOpenMPReductionId(Parser &P) {
162 Token Tok = P.getCurToken();
163 Sema &Actions = P.getActions();
164 OverloadedOperatorKind OOK = OO_None;
165 // Allow to use 'operator' keyword for C++ operators
166 bool WithOperator = false;
167 if (Tok.is(tok::kw_operator)) {
169 Tok = P.getCurToken();
172 switch (Tok.getKind()) {
173 case tok::plus: // '+'
176 case tok::minus: // '-'
179 case tok::star: // '*'
182 case tok::amp: // '&'
185 case tok::pipe: // '|'
188 case tok::caret: // '^'
191 case tok::ampamp: // '&&'
194 case tok::pipepipe: // '||'
197 case tok::identifier: // identifier
202 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
203 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
204 Parser::StopBeforeMatch);
205 return DeclarationName();
208 auto &DeclNames = Actions.getASTContext().DeclarationNames;
209 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
210 : DeclNames.getCXXOperatorName(OOK);
213 /// Parse 'omp declare reduction' construct.
215 /// declare-reduction-directive:
216 /// annot_pragma_openmp 'declare' 'reduction'
217 /// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
218 /// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
219 /// annot_pragma_openmp_end
220 /// <reduction_id> is either a base language identifier or one of the following
221 /// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
223 Parser::DeclGroupPtrTy
224 Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
227 if (T.expectAndConsume(diag::err_expected_lparen_after,
228 getOpenMPDirectiveName(OMPD_declare_reduction))) {
229 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
230 return DeclGroupPtrTy();
233 DeclarationName Name = parseOpenMPReductionId(*this);
234 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
235 return DeclGroupPtrTy();
238 bool IsCorrect = !ExpectAndConsume(tok::colon);
240 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
241 return DeclGroupPtrTy();
243 IsCorrect = IsCorrect && !Name.isEmpty();
245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
246 Diag(Tok.getLocation(), diag::err_expected_type);
250 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
251 return DeclGroupPtrTy();
253 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
254 // Parse list of types until ':' token.
256 ColonProtectionRAIIObject ColonRAII(*this);
259 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
261 QualType ReductionType =
262 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
263 if (!ReductionType.isNull()) {
264 ReductionTypes.push_back(
265 std::make_pair(ReductionType, Range.getBegin()));
268 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
272 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
276 if (ExpectAndConsume(tok::comma)) {
278 if (Tok.is(tok::annot_pragma_openmp_end)) {
279 Diag(Tok.getLocation(), diag::err_expected_type);
280 return DeclGroupPtrTy();
283 } while (Tok.isNot(tok::annot_pragma_openmp_end));
285 if (ReductionTypes.empty()) {
286 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
287 return DeclGroupPtrTy();
290 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
291 return DeclGroupPtrTy();
294 if (ExpectAndConsume(tok::colon))
297 if (Tok.is(tok::annot_pragma_openmp_end)) {
298 Diag(Tok.getLocation(), diag::err_expected_expression);
299 return DeclGroupPtrTy();
302 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
303 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
305 // Parse <combiner> expression and then parse initializer if any for each
307 unsigned I = 0, E = ReductionTypes.size();
308 for (Decl *D : DRD.get()) {
309 TentativeParsingAction TPA(*this);
310 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
311 Scope::CompoundStmtScope |
312 Scope::OpenMPDirectiveScope);
313 // Parse <combiner> expression.
314 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
315 ExprResult CombinerResult =
316 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
317 D->getLocation(), /*DiscardedValue=*/true);
318 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
320 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
321 Tok.isNot(tok::annot_pragma_openmp_end)) {
326 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
327 ExprResult InitializerResult;
328 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
329 // Parse <initializer> expression.
330 if (Tok.is(tok::identifier) &&
331 Tok.getIdentifierInfo()->isStr("initializer")) {
334 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
340 BalancedDelimiterTracker T(*this, tok::l_paren,
341 tok::annot_pragma_openmp_end);
343 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
345 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
346 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
347 Scope::CompoundStmtScope |
348 Scope::OpenMPDirectiveScope);
350 VarDecl *OmpPrivParm =
351 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
353 // Check if initializer is omp_priv <init_expr> or something else.
354 if (Tok.is(tok::identifier) &&
355 Tok.getIdentifierInfo()->isStr("omp_priv")) {
356 if (Actions.getLangOpts().CPlusPlus) {
357 InitializerResult = Actions.ActOnFinishFullExpr(
358 ParseAssignmentExpression().get(), D->getLocation(),
359 /*DiscardedValue=*/true);
362 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
365 InitializerResult = Actions.ActOnFinishFullExpr(
366 ParseAssignmentExpression().get(), D->getLocation(),
367 /*DiscardedValue=*/true);
369 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
370 D, InitializerResult.get(), OmpPrivParm);
371 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
372 Tok.isNot(tok::annot_pragma_openmp_end)) {
378 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
383 // Revert parsing if not the last type, otherwise accept it, we're done with
390 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
394 void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
395 // Parse declarator '=' initializer.
396 // If a '==' or '+=' is found, suggest a fixit to '='.
397 if (isTokenEqualOrEqualTypo()) {
400 if (Tok.is(tok::code_completion)) {
401 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
402 Actions.FinalizeDeclaration(OmpPrivParm);
407 ExprResult Init(ParseInitializer());
409 if (Init.isInvalid()) {
410 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
411 Actions.ActOnInitializerError(OmpPrivParm);
413 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
414 /*DirectInit=*/false);
416 } else if (Tok.is(tok::l_paren)) {
417 // Parse C++ direct initializer: '(' expression-list ')'
418 BalancedDelimiterTracker T(*this, tok::l_paren);
422 CommaLocsTy CommaLocs;
424 SourceLocation LParLoc = T.getOpenLocation();
425 if (ParseExpressionList(
426 Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
427 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
429 OmpPrivParm->getType()->getCanonicalTypeInternal(),
430 OmpPrivParm->getLocation(), Exprs, LParLoc);
431 CalledSignatureHelp = true;
432 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
434 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) {
435 Actions.ProduceConstructorSignatureHelp(
436 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
437 OmpPrivParm->getLocation(), Exprs, LParLoc);
438 CalledSignatureHelp = true;
440 Actions.ActOnInitializerError(OmpPrivParm);
441 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
444 SourceLocation RLoc = Tok.getLocation();
445 if (!T.consumeClose())
446 RLoc = T.getCloseLocation();
448 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
449 "Unexpected number of commas!");
451 ExprResult Initializer =
452 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
453 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
454 /*DirectInit=*/true);
456 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
457 // Parse C++0x braced-init-list.
458 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
460 ExprResult Init(ParseBraceInitializer());
462 if (Init.isInvalid()) {
463 Actions.ActOnInitializerError(OmpPrivParm);
465 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
466 /*DirectInit=*/true);
469 Actions.ActOnUninitializedDecl(OmpPrivParm);
474 /// RAII that recreates function context for correct parsing of clauses of
475 /// 'declare simd' construct.
476 /// OpenMP, 2.8.2 declare simd Construct
477 /// The expressions appearing in the clauses of this directive are evaluated in
478 /// the scope of the arguments of the function declaration or definition.
479 class FNContextRAII final {
481 Sema::CXXThisScopeRAII *ThisScope;
482 Parser::ParseScope *TempScope;
483 Parser::ParseScope *FnScope;
484 bool HasTemplateScope = false;
485 bool HasFunScope = false;
486 FNContextRAII() = delete;
487 FNContextRAII(const FNContextRAII &) = delete;
488 FNContextRAII &operator=(const FNContextRAII &) = delete;
491 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
492 Decl *D = *Ptr.get().begin();
493 NamedDecl *ND = dyn_cast<NamedDecl>(D);
494 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
495 Sema &Actions = P.getActions();
497 // Allow 'this' within late-parsed attributes.
498 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
499 ND && ND->isCXXInstanceMember());
501 // If the Decl is templatized, add template parameters to scope.
502 HasTemplateScope = D->isTemplateDecl();
504 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
505 if (HasTemplateScope)
506 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
508 // If the Decl is on a function, add function parameters to the scope.
509 HasFunScope = D->isFunctionOrFunctionTemplate();
510 FnScope = new Parser::ParseScope(
511 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
514 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
518 P.getActions().ActOnExitFunctionContext();
519 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
521 if (HasTemplateScope)
530 /// Parses clauses for 'declare simd' directive.
532 /// 'inbranch' | 'notinbranch'
533 /// 'simdlen' '(' <expr> ')'
534 /// { 'uniform' '(' <argument_list> ')' }
535 /// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
536 /// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
537 static bool parseDeclareSimdClauses(
538 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
539 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
540 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
541 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
543 const Token &Tok = P.getCurToken();
544 bool IsError = false;
545 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
546 if (Tok.isNot(tok::identifier))
548 OMPDeclareSimdDeclAttr::BranchStateTy Out;
549 IdentifierInfo *II = Tok.getIdentifierInfo();
550 StringRef ClauseName = II->getName();
551 // Parse 'inranch|notinbranch' clauses.
552 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
553 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
554 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
556 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
560 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
562 } else if (ClauseName.equals("simdlen")) {
563 if (SimdLen.isUsable()) {
564 P.Diag(Tok, diag::err_omp_more_one_clause)
565 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
570 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
571 if (SimdLen.isInvalid())
574 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
575 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
576 CKind == OMPC_linear) {
577 Parser::OpenMPVarListDataTy Data;
578 SmallVectorImpl<Expr *> *Vars = &Uniforms;
579 if (CKind == OMPC_aligned)
581 else if (CKind == OMPC_linear)
585 if (P.ParseOpenMPVarList(OMPD_declare_simd,
586 getOpenMPClauseKind(ClauseName), *Vars, Data))
588 if (CKind == OMPC_aligned) {
589 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
590 } else if (CKind == OMPC_linear) {
591 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
593 Data.LinKind = OMPC_LINEAR_val;
594 LinModifiers.append(Linears.size() - LinModifiers.size(),
596 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
599 // TODO: add parsing of other clauses.
603 if (Tok.is(tok::comma))
609 /// Parse clauses for '#pragma omp declare simd'.
610 Parser::DeclGroupPtrTy
611 Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
612 CachedTokens &Toks, SourceLocation Loc) {
614 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
615 // Consume the previously pushed token.
616 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
618 FNContextRAII FnContext(*this, Ptr);
619 OMPDeclareSimdDeclAttr::BranchStateTy BS =
620 OMPDeclareSimdDeclAttr::BS_Undefined;
622 SmallVector<Expr *, 4> Uniforms;
623 SmallVector<Expr *, 4> Aligneds;
624 SmallVector<Expr *, 4> Alignments;
625 SmallVector<Expr *, 4> Linears;
626 SmallVector<unsigned, 4> LinModifiers;
627 SmallVector<Expr *, 4> Steps;
629 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
630 Alignments, Linears, LinModifiers, Steps);
631 // Need to check for extra tokens.
632 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
633 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
634 << getOpenMPDirectiveName(OMPD_declare_simd);
635 while (Tok.isNot(tok::annot_pragma_openmp_end))
638 // Skip the last annot_pragma_openmp_end.
639 SourceLocation EndLoc = ConsumeAnnotationToken();
642 return Actions.ActOnOpenMPDeclareSimdDirective(
643 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
644 LinModifiers, Steps, SourceRange(Loc, EndLoc));
647 Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
648 // OpenMP 4.5 syntax with list of entities.
649 Sema::NamedDeclSetType SameDirectiveDecls;
650 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
651 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
652 if (Tok.is(tok::identifier)) {
653 IdentifierInfo *II = Tok.getIdentifierInfo();
654 StringRef ClauseName = II->getName();
655 // Parse 'to|link' clauses.
656 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT)) {
657 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) << ClauseName;
662 auto &&Callback = [this, MT, &SameDirectiveDecls](
663 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
664 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
667 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
668 /*AllowScopeSpecifier=*/true))
671 // Consume optional ','.
672 if (Tok.is(tok::comma))
675 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
677 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
678 SameDirectiveDecls.end());
680 return DeclGroupPtrTy();
681 return Actions.BuildDeclaratorGroup(Decls);
684 void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
685 SourceLocation DTLoc) {
686 if (DKind != OMPD_end_declare_target) {
687 Diag(Tok, diag::err_expected_end_declare_target);
688 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
692 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
693 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
694 << getOpenMPDirectiveName(OMPD_end_declare_target);
695 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
697 // Skip the last annot_pragma_openmp_end.
701 /// Parsing of declarative OpenMP directives.
703 /// threadprivate-directive:
704 /// annot_pragma_openmp 'threadprivate' simple-variable-list
705 /// annot_pragma_openmp_end
707 /// declare-reduction-directive:
708 /// annot_pragma_openmp 'declare' 'reduction' [...]
709 /// annot_pragma_openmp_end
711 /// declare-simd-directive:
712 /// annot_pragma_openmp 'declare simd' {<clause> [,]}
713 /// annot_pragma_openmp_end
714 /// <function declaration/definition>
716 /// requires directive:
717 /// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
718 /// annot_pragma_openmp_end
720 Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
721 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
722 DeclSpec::TST TagType, Decl *Tag) {
723 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
724 ParenBraceBracketBalancer BalancerRAIIObj(*this);
726 SourceLocation Loc = ConsumeAnnotationToken();
727 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
730 case OMPD_threadprivate: {
732 ThreadprivateListParserHelper Helper(this);
733 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
734 // The last seen token is annot_pragma_openmp_end - need to check for
736 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
737 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
738 << getOpenMPDirectiveName(OMPD_threadprivate);
739 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
741 // Skip the last annot_pragma_openmp_end.
742 ConsumeAnnotationToken();
743 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
744 Helper.getIdentifiers());
748 case OMPD_requires: {
749 SourceLocation StartLoc = ConsumeToken();
750 SmallVector<OMPClause *, 5> Clauses;
751 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
752 FirstClauses(OMPC_unknown + 1);
753 if (Tok.is(tok::annot_pragma_openmp_end)) {
754 Diag(Tok, diag::err_omp_expected_clause)
755 << getOpenMPDirectiveName(OMPD_requires);
758 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
759 OpenMPClauseKind CKind = Tok.isAnnotation()
761 : getOpenMPClauseKind(PP.getSpelling(Tok));
762 Actions.StartOpenMPClause(CKind);
764 ParseOpenMPClause(OMPD_requires, CKind, !FirstClauses[CKind].getInt());
765 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, StopBeforeMatch);
766 FirstClauses[CKind].setInt(true);
767 if (Clause != nullptr)
768 Clauses.push_back(Clause);
769 if (Tok.is(tok::annot_pragma_openmp_end)) {
770 Actions.EndOpenMPClause();
774 if (Tok.is(tok::comma))
776 Actions.EndOpenMPClause();
778 // Consume final annot_pragma_openmp_end
779 if (Clauses.size() == 0) {
780 Diag(Tok, diag::err_omp_expected_clause)
781 << getOpenMPDirectiveName(OMPD_requires);
782 ConsumeAnnotationToken();
785 ConsumeAnnotationToken();
786 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
788 case OMPD_declare_reduction:
790 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
791 // The last seen token is annot_pragma_openmp_end - need to check for
793 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
794 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
795 << getOpenMPDirectiveName(OMPD_declare_reduction);
796 while (Tok.isNot(tok::annot_pragma_openmp_end))
799 // Skip the last annot_pragma_openmp_end.
800 ConsumeAnnotationToken();
804 case OMPD_declare_simd: {
806 // { #pragma omp declare simd }
807 // <function-declaration-or-definition>
811 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
819 if (Tok.is(tok::annot_pragma_openmp)) {
820 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
821 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
822 // Here we expect to see some function declaration.
824 assert(TagType == DeclSpec::TST_unspecified);
825 MaybeParseCXX11Attributes(Attrs);
826 ParsingDeclSpec PDS(*this);
827 Ptr = ParseExternalDeclaration(Attrs, &PDS);
830 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
834 Diag(Loc, diag::err_omp_decl_in_declare_simd);
835 return DeclGroupPtrTy();
837 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
839 case OMPD_declare_target: {
840 SourceLocation DTLoc = ConsumeAnyToken();
841 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
842 return ParseOMPDeclareTargetClauses();
845 // Skip the last annot_pragma_openmp_end.
848 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
849 return DeclGroupPtrTy();
851 llvm::SmallVector<Decl *, 4> Decls;
852 DKind = parseOpenMPDirectiveKind(*this);
853 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
854 Tok.isNot(tok::r_brace)) {
856 // Here we expect to see some function declaration.
858 assert(TagType == DeclSpec::TST_unspecified);
859 MaybeParseCXX11Attributes(Attrs);
860 ParsingDeclSpec PDS(*this);
861 Ptr = ParseExternalDeclaration(Attrs, &PDS);
864 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
867 DeclGroupRef Ref = Ptr.get();
868 Decls.append(Ref.begin(), Ref.end());
870 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
871 TentativeParsingAction TPA(*this);
872 ConsumeAnnotationToken();
873 DKind = parseOpenMPDirectiveKind(*this);
874 if (DKind != OMPD_end_declare_target)
881 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
882 Actions.ActOnFinishOpenMPDeclareTargetDirective();
883 return Actions.BuildDeclaratorGroup(Decls);
886 Diag(Tok, diag::err_omp_unknown_directive);
904 case OMPD_parallel_for:
905 case OMPD_parallel_for_simd:
906 case OMPD_parallel_sections:
910 case OMPD_cancellation_point:
912 case OMPD_target_data:
913 case OMPD_target_enter_data:
914 case OMPD_target_exit_data:
915 case OMPD_target_parallel:
916 case OMPD_target_parallel_for:
918 case OMPD_taskloop_simd:
919 case OMPD_distribute:
920 case OMPD_end_declare_target:
921 case OMPD_target_update:
922 case OMPD_distribute_parallel_for:
923 case OMPD_distribute_parallel_for_simd:
924 case OMPD_distribute_simd:
925 case OMPD_target_parallel_for_simd:
926 case OMPD_target_simd:
927 case OMPD_teams_distribute:
928 case OMPD_teams_distribute_simd:
929 case OMPD_teams_distribute_parallel_for_simd:
930 case OMPD_teams_distribute_parallel_for:
931 case OMPD_target_teams:
932 case OMPD_target_teams_distribute:
933 case OMPD_target_teams_distribute_parallel_for:
934 case OMPD_target_teams_distribute_parallel_for_simd:
935 case OMPD_target_teams_distribute_simd:
936 Diag(Tok, diag::err_omp_unexpected_directive)
937 << 1 << getOpenMPDirectiveName(DKind);
940 while (Tok.isNot(tok::annot_pragma_openmp_end))
946 /// Parsing of declarative or executable OpenMP directives.
948 /// threadprivate-directive:
949 /// annot_pragma_openmp 'threadprivate' simple-variable-list
950 /// annot_pragma_openmp_end
952 /// declare-reduction-directive:
953 /// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
954 /// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
955 /// ('omp_priv' '=' <expression>|<function_call>) ')']
956 /// annot_pragma_openmp_end
958 /// executable-directive:
959 /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
960 /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
961 /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
962 /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
963 /// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
964 /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
965 /// 'distribute' | 'target enter data' | 'target exit data' |
966 /// 'target parallel' | 'target parallel for' |
967 /// 'target update' | 'distribute parallel for' |
968 /// 'distribute paralle for simd' | 'distribute simd' |
969 /// 'target parallel for simd' | 'target simd' |
970 /// 'teams distribute' | 'teams distribute simd' |
971 /// 'teams distribute parallel for simd' |
972 /// 'teams distribute parallel for' | 'target teams' |
973 /// 'target teams distribute' |
974 /// 'target teams distribute parallel for' |
975 /// 'target teams distribute parallel for simd' |
976 /// 'target teams distribute simd' {clause}
977 /// annot_pragma_openmp_end
979 StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
980 AllowedConstructsKind Allowed) {
981 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
982 ParenBraceBracketBalancer BalancerRAIIObj(*this);
983 SmallVector<OMPClause *, 5> Clauses;
984 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
985 FirstClauses(OMPC_unknown + 1);
986 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
987 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
988 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
989 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
990 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
991 // Name of critical directive.
992 DeclarationNameInfo DirName;
993 StmtResult Directive = StmtError();
994 bool HasAssociatedStatement = true;
995 bool FlushHasClause = false;
998 case OMPD_threadprivate: {
999 if (Allowed != ACK_Any) {
1000 Diag(Tok, diag::err_omp_immediate_directive)
1001 << getOpenMPDirectiveName(DKind) << 0;
1004 ThreadprivateListParserHelper Helper(this);
1005 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
1006 // The last seen token is annot_pragma_openmp_end - need to check for
1008 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1009 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1010 << getOpenMPDirectiveName(OMPD_threadprivate);
1011 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1013 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1014 Loc, Helper.getIdentifiers());
1015 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1017 SkipUntil(tok::annot_pragma_openmp_end);
1020 case OMPD_declare_reduction:
1022 if (DeclGroupPtrTy Res =
1023 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
1024 // The last seen token is annot_pragma_openmp_end - need to check for
1026 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1027 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1028 << getOpenMPDirectiveName(OMPD_declare_reduction);
1029 while (Tok.isNot(tok::annot_pragma_openmp_end))
1033 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1035 SkipUntil(tok::annot_pragma_openmp_end);
1039 if (PP.LookAhead(0).is(tok::l_paren)) {
1040 FlushHasClause = true;
1041 // Push copy of the current token back to stream to properly parse
1042 // pseudo-clause OMPFlushClause.
1046 case OMPD_taskyield:
1049 case OMPD_cancellation_point:
1051 case OMPD_target_enter_data:
1052 case OMPD_target_exit_data:
1053 case OMPD_target_update:
1054 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
1055 Diag(Tok, diag::err_omp_immediate_directive)
1056 << getOpenMPDirectiveName(DKind) << 0;
1058 HasAssociatedStatement = false;
1059 // Fall through for further analysis.
1070 case OMPD_parallel_for:
1071 case OMPD_parallel_for_simd:
1072 case OMPD_parallel_sections:
1078 case OMPD_taskgroup:
1079 case OMPD_target_data:
1080 case OMPD_target_parallel:
1081 case OMPD_target_parallel_for:
1083 case OMPD_taskloop_simd:
1084 case OMPD_distribute:
1085 case OMPD_distribute_parallel_for:
1086 case OMPD_distribute_parallel_for_simd:
1087 case OMPD_distribute_simd:
1088 case OMPD_target_parallel_for_simd:
1089 case OMPD_target_simd:
1090 case OMPD_teams_distribute:
1091 case OMPD_teams_distribute_simd:
1092 case OMPD_teams_distribute_parallel_for_simd:
1093 case OMPD_teams_distribute_parallel_for:
1094 case OMPD_target_teams:
1095 case OMPD_target_teams_distribute:
1096 case OMPD_target_teams_distribute_parallel_for:
1097 case OMPD_target_teams_distribute_parallel_for_simd:
1098 case OMPD_target_teams_distribute_simd: {
1100 // Parse directive name of the 'critical' directive if any.
1101 if (DKind == OMPD_critical) {
1102 BalancedDelimiterTracker T(*this, tok::l_paren,
1103 tok::annot_pragma_openmp_end);
1104 if (!T.consumeOpen()) {
1105 if (Tok.isAnyIdentifier()) {
1107 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1110 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1114 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
1115 CancelRegion = parseOpenMPDirectiveKind(*this);
1116 if (Tok.isNot(tok::annot_pragma_openmp_end))
1120 if (isOpenMPLoopDirective(DKind))
1121 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1122 if (isOpenMPSimdDirective(DKind))
1123 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1124 ParseScope OMPDirectiveScope(this, ScopeFlags);
1125 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
1127 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1128 OpenMPClauseKind CKind =
1131 : FlushHasClause ? OMPC_flush
1132 : getOpenMPClauseKind(PP.getSpelling(Tok));
1133 Actions.StartOpenMPClause(CKind);
1134 FlushHasClause = false;
1136 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
1137 FirstClauses[CKind].setInt(true);
1139 FirstClauses[CKind].setPointer(Clause);
1140 Clauses.push_back(Clause);
1144 if (Tok.is(tok::comma))
1146 Actions.EndOpenMPClause();
1148 // End location of the directive.
1149 EndLoc = Tok.getLocation();
1150 // Consume final annot_pragma_openmp_end.
1151 ConsumeAnnotationToken();
1153 // OpenMP [2.13.8, ordered Construct, Syntax]
1154 // If the depend clause is specified, the ordered construct is a stand-alone
1156 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
1157 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
1158 Diag(Loc, diag::err_omp_immediate_directive)
1159 << getOpenMPDirectiveName(DKind) << 1
1160 << getOpenMPClauseName(OMPC_depend);
1162 HasAssociatedStatement = false;
1165 StmtResult AssociatedStmt;
1166 if (HasAssociatedStatement) {
1167 // The body is a block scope like in Lambdas and Blocks.
1168 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
1169 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1170 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1171 // should have at least one compound statement scope within it.
1172 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
1173 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
1174 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1175 DKind == OMPD_target_exit_data) {
1176 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
1177 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1178 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1179 /*isStmtExpr=*/false));
1180 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
1182 Directive = Actions.ActOnOpenMPExecutableDirective(
1183 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1187 Actions.EndOpenMPDSABlock(Directive.get());
1188 OMPDirectiveScope.Exit();
1191 case OMPD_declare_simd:
1192 case OMPD_declare_target:
1193 case OMPD_end_declare_target:
1195 Diag(Tok, diag::err_omp_unexpected_directive)
1196 << 1 << getOpenMPDirectiveName(DKind);
1197 SkipUntil(tok::annot_pragma_openmp_end);
1200 Diag(Tok, diag::err_omp_unknown_directive);
1201 SkipUntil(tok::annot_pragma_openmp_end);
1207 // Parses simple list:
1208 // simple-variable-list:
1209 // '(' id-expression {, id-expression} ')'
1211 bool Parser::ParseOpenMPSimpleVarList(
1212 OpenMPDirectiveKind Kind,
1213 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1215 bool AllowScopeSpecifier) {
1217 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1218 if (T.expectAndConsume(diag::err_expected_lparen_after,
1219 getOpenMPDirectiveName(Kind)))
1221 bool IsCorrect = true;
1222 bool NoIdentIsFound = true;
1224 // Read tokens while ')' or annot_pragma_openmp_end is not found.
1225 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
1229 Token PrevTok = Tok;
1230 NoIdentIsFound = false;
1232 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
1233 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
1235 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1237 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
1240 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1242 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1243 Tok.isNot(tok::annot_pragma_openmp_end)) {
1245 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1247 Diag(PrevTok.getLocation(), diag::err_expected)
1249 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
1251 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
1254 if (Tok.is(tok::comma)) {
1259 if (NoIdentIsFound) {
1260 Diag(Tok, diag::err_expected) << tok::identifier;
1265 IsCorrect = !T.consumeClose() && IsCorrect;
1270 /// Parsing of OpenMP clauses.
1273 /// if-clause | final-clause | num_threads-clause | safelen-clause |
1274 /// default-clause | private-clause | firstprivate-clause | shared-clause
1275 /// | linear-clause | aligned-clause | collapse-clause |
1276 /// lastprivate-clause | reduction-clause | proc_bind-clause |
1277 /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
1278 /// mergeable-clause | flush-clause | read-clause | write-clause |
1279 /// update-clause | capture-clause | seq_cst-clause | device-clause |
1280 /// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
1281 /// thread_limit-clause | priority-clause | grainsize-clause |
1282 /// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
1283 /// from-clause | is_device_ptr-clause | task_reduction-clause |
1284 /// in_reduction-clause
1286 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1287 OpenMPClauseKind CKind, bool FirstClause) {
1288 OMPClause *Clause = nullptr;
1289 bool ErrorFound = false;
1290 bool WrongDirective = false;
1291 // Check if clause is allowed for the given directive.
1292 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
1293 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1294 << getOpenMPDirectiveName(DKind);
1296 WrongDirective = true;
1301 case OMPC_num_threads:
1307 case OMPC_num_teams:
1308 case OMPC_thread_limit:
1310 case OMPC_grainsize:
1311 case OMPC_num_tasks:
1313 // OpenMP [2.5, Restrictions]
1314 // At most one num_threads clause can appear on the directive.
1315 // OpenMP [2.8.1, simd construct, Restrictions]
1316 // Only one safelen clause can appear on a simd directive.
1317 // Only one simdlen clause can appear on a simd directive.
1318 // Only one collapse clause can appear on a simd directive.
1319 // OpenMP [2.9.1, target data construct, Restrictions]
1320 // At most one device clause can appear on the directive.
1321 // OpenMP [2.11.1, task Construct, Restrictions]
1322 // At most one if clause can appear on the directive.
1323 // At most one final clause can appear on the directive.
1324 // OpenMP [teams Construct, Restrictions]
1325 // At most one num_teams clause can appear on the directive.
1326 // At most one thread_limit clause can appear on the directive.
1327 // OpenMP [2.9.1, task Construct, Restrictions]
1328 // At most one priority clause can appear on the directive.
1329 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1330 // At most one grainsize clause can appear on the directive.
1331 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1332 // At most one num_tasks clause can appear on the directive.
1334 Diag(Tok, diag::err_omp_more_one_clause)
1335 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
1339 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1340 Clause = ParseOpenMPClause(CKind, WrongDirective);
1342 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
1345 case OMPC_proc_bind:
1346 case OMPC_atomic_default_mem_order:
1347 // OpenMP [2.14.3.1, Restrictions]
1348 // Only a single default clause may be specified on a parallel, task or
1350 // OpenMP [2.5, parallel Construct, Restrictions]
1351 // At most one proc_bind clause can appear on the directive.
1352 // OpenMP [5.0, Requires directive, Restrictions]
1353 // At most one atomic_default_mem_order clause can appear
1356 Diag(Tok, diag::err_omp_more_one_clause)
1357 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
1361 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
1364 case OMPC_dist_schedule:
1365 case OMPC_defaultmap:
1366 // OpenMP [2.7.1, Restrictions, p. 3]
1367 // Only one schedule clause can appear on a loop directive.
1368 // OpenMP [2.10.4, Restrictions, p. 106]
1369 // At most one defaultmap clause can appear on the directive.
1371 Diag(Tok, diag::err_omp_more_one_clause)
1372 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
1378 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
1382 case OMPC_mergeable:
1391 case OMPC_unified_address:
1392 case OMPC_unified_shared_memory:
1393 case OMPC_reverse_offload:
1394 case OMPC_dynamic_allocators:
1395 // OpenMP [2.7.1, Restrictions, p. 9]
1396 // Only one ordered clause can appear on a loop directive.
1397 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1398 // Only one nowait clause can appear on a for directive.
1399 // OpenMP [5.0, Requires directive, Restrictions]
1400 // Each of the requires clauses can appear at most once on the directive.
1402 Diag(Tok, diag::err_omp_more_one_clause)
1403 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
1407 Clause = ParseOpenMPClause(CKind, WrongDirective);
1410 case OMPC_firstprivate:
1411 case OMPC_lastprivate:
1413 case OMPC_reduction:
1414 case OMPC_task_reduction:
1415 case OMPC_in_reduction:
1419 case OMPC_copyprivate:
1425 case OMPC_use_device_ptr:
1426 case OMPC_is_device_ptr:
1427 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
1430 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1431 << getOpenMPDirectiveName(DKind);
1432 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1434 case OMPC_threadprivate:
1436 if (!WrongDirective)
1437 Diag(Tok, diag::err_omp_unexpected_clause)
1438 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
1439 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
1442 return ErrorFound ? nullptr : Clause;
1445 /// Parses simple expression in parens for single-expression clauses of OpenMP
1447 /// \param RLoc Returned location of right paren.
1448 ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1449 SourceLocation &RLoc) {
1450 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1451 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1454 SourceLocation ELoc = Tok.getLocation();
1455 ExprResult LHS(ParseCastExpression(
1456 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1457 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1458 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1461 RLoc = Tok.getLocation();
1462 if (!T.consumeClose())
1463 RLoc = T.getCloseLocation();
1468 /// Parsing of OpenMP clauses with single expressions like 'final',
1469 /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
1470 /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
1473 /// 'final' '(' expression ')'
1475 /// num_threads-clause:
1476 /// 'num_threads' '(' expression ')'
1479 /// 'safelen' '(' expression ')'
1482 /// 'simdlen' '(' expression ')'
1484 /// collapse-clause:
1485 /// 'collapse' '(' expression ')'
1487 /// priority-clause:
1488 /// 'priority' '(' expression ')'
1490 /// grainsize-clause:
1491 /// 'grainsize' '(' expression ')'
1493 /// num_tasks-clause:
1494 /// 'num_tasks' '(' expression ')'
1497 /// 'hint' '(' expression ')'
1499 OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1501 SourceLocation Loc = ConsumeToken();
1502 SourceLocation LLoc = Tok.getLocation();
1503 SourceLocation RLoc;
1505 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
1507 if (Val.isInvalid())
1512 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
1515 /// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1518 /// 'default' '(' 'none' | 'shared' ')
1520 /// proc_bind-clause:
1521 /// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1523 OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1525 SourceLocation Loc = Tok.getLocation();
1526 SourceLocation LOpen = ConsumeToken();
1528 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1529 if (T.expectAndConsume(diag::err_expected_lparen_after,
1530 getOpenMPClauseName(Kind)))
1533 unsigned Type = getOpenMPSimpleClauseType(
1534 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1535 SourceLocation TypeLoc = Tok.getLocation();
1536 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1537 Tok.isNot(tok::annot_pragma_openmp_end))
1541 SourceLocation RLoc = Tok.getLocation();
1542 if (!T.consumeClose())
1543 RLoc = T.getCloseLocation();
1547 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
1550 /// Parsing of OpenMP clauses like 'ordered'.
1561 /// mergeable-clause:
1576 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
1577 SourceLocation Loc = Tok.getLocation();
1582 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1586 /// Parsing of OpenMP clauses with single expressions and some additional
1587 /// argument like 'schedule' or 'dist_schedule'.
1589 /// schedule-clause:
1590 /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1594 /// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1597 /// 'defaultmap' '(' modifier ':' kind ')'
1599 OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1601 SourceLocation Loc = ConsumeToken();
1602 SourceLocation DelimLoc;
1604 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1605 if (T.expectAndConsume(diag::err_expected_lparen_after,
1606 getOpenMPClauseName(Kind)))
1610 SmallVector<unsigned, 4> Arg;
1611 SmallVector<SourceLocation, 4> KLoc;
1612 if (Kind == OMPC_schedule) {
1613 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1614 Arg.resize(NumberOfElements);
1615 KLoc.resize(NumberOfElements);
1616 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1617 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1618 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1619 unsigned KindModifier = getOpenMPSimpleClauseType(
1620 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1621 if (KindModifier > OMPC_SCHEDULE_unknown) {
1623 Arg[Modifier1] = KindModifier;
1624 KLoc[Modifier1] = Tok.getLocation();
1625 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1626 Tok.isNot(tok::annot_pragma_openmp_end))
1628 if (Tok.is(tok::comma)) {
1629 // Parse ',' 'modifier'
1631 KindModifier = getOpenMPSimpleClauseType(
1632 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1633 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1635 : (unsigned)OMPC_SCHEDULE_unknown;
1636 KLoc[Modifier2] = Tok.getLocation();
1637 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1638 Tok.isNot(tok::annot_pragma_openmp_end))
1642 if (Tok.is(tok::colon))
1645 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1646 KindModifier = getOpenMPSimpleClauseType(
1647 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1649 Arg[ScheduleKind] = KindModifier;
1650 KLoc[ScheduleKind] = Tok.getLocation();
1651 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1652 Tok.isNot(tok::annot_pragma_openmp_end))
1654 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1655 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1656 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
1658 DelimLoc = ConsumeAnyToken();
1659 } else if (Kind == OMPC_dist_schedule) {
1660 Arg.push_back(getOpenMPSimpleClauseType(
1661 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1662 KLoc.push_back(Tok.getLocation());
1663 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1664 Tok.isNot(tok::annot_pragma_openmp_end))
1666 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1667 DelimLoc = ConsumeAnyToken();
1668 } else if (Kind == OMPC_defaultmap) {
1669 // Get a defaultmap modifier
1670 Arg.push_back(getOpenMPSimpleClauseType(
1671 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1672 KLoc.push_back(Tok.getLocation());
1673 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1674 Tok.isNot(tok::annot_pragma_openmp_end))
1677 if (Tok.is(tok::colon))
1679 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1680 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1681 // Get a defaultmap kind
1682 Arg.push_back(getOpenMPSimpleClauseType(
1683 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1684 KLoc.push_back(Tok.getLocation());
1685 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1686 Tok.isNot(tok::annot_pragma_openmp_end))
1689 assert(Kind == OMPC_if);
1690 KLoc.push_back(Tok.getLocation());
1691 TentativeParsingAction TPA(*this);
1692 Arg.push_back(parseOpenMPDirectiveKind(*this));
1693 if (Arg.back() != OMPD_unknown) {
1695 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1697 DelimLoc = ConsumeToken();
1700 Arg.back() = OMPD_unknown;
1707 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1708 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1710 if (NeedAnExpression) {
1711 SourceLocation ELoc = Tok.getLocation();
1712 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1713 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
1714 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1718 SourceLocation RLoc = Tok.getLocation();
1719 if (!T.consumeClose())
1720 RLoc = T.getCloseLocation();
1722 if (NeedAnExpression && Val.isInvalid())
1727 return Actions.ActOnOpenMPSingleExprWithArgClause(
1728 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
1731 static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1732 UnqualifiedId &ReductionId) {
1733 if (ReductionIdScopeSpec.isEmpty()) {
1735 switch (P.getCurToken().getKind()) {
1763 if (OOK != OO_None) {
1764 SourceLocation OpLoc = P.ConsumeToken();
1765 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
1766 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1770 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1771 /*AllowDestructorName*/ false,
1772 /*AllowConstructorName*/ false,
1773 /*AllowDeductionGuide*/ false,
1774 nullptr, nullptr, ReductionId);
1777 /// Checks if the token is a valid map-type-modifier.
1778 static OpenMPMapModifierKind isMapModifier(Parser &P) {
1779 Token Tok = P.getCurToken();
1780 if (!Tok.is(tok::identifier))
1781 return OMPC_MAP_MODIFIER_unknown;
1783 Preprocessor &PP = P.getPreprocessor();
1784 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
1785 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1786 return TypeModifier;
1789 /// Parse map-type-modifiers in map clause.
1790 /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1791 /// where, map-type-modifier ::= always | close
1792 static void parseMapTypeModifiers(Parser &P,
1793 Parser::OpenMPVarListDataTy &Data) {
1794 Preprocessor &PP = P.getPreprocessor();
1795 while (P.getCurToken().isNot(tok::colon)) {
1796 Token Tok = P.getCurToken();
1797 OpenMPMapModifierKind TypeModifier = isMapModifier(P);
1798 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
1799 TypeModifier == OMPC_MAP_MODIFIER_close) {
1800 Data.MapTypeModifiers.push_back(TypeModifier);
1801 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
1804 // For the case of unknown map-type-modifier or a map-type.
1805 // Map-type is followed by a colon; the function returns when it
1806 // encounters a token followed by a colon.
1807 if (Tok.is(tok::comma)) {
1808 P.Diag(Tok, diag::err_omp_map_type_modifier_missing);
1812 // Potential map-type token as it is followed by a colon.
1813 if (PP.LookAhead(0).is(tok::colon))
1815 P.Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1818 if (P.getCurToken().is(tok::comma))
1823 /// Checks if the token is a valid map-type.
1824 static OpenMPMapClauseKind isMapType(Parser &P) {
1825 Token Tok = P.getCurToken();
1826 // The map-type token can be either an identifier or the C++ delete keyword.
1827 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
1828 return OMPC_MAP_unknown;
1829 Preprocessor &PP = P.getPreprocessor();
1830 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
1831 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1835 /// Parse map-type in map clause.
1836 /// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1837 /// where, map-type ::= to | from | tofrom | alloc | release | delete
1838 static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
1839 Token Tok = P.getCurToken();
1840 if (Tok.is(tok::colon)) {
1841 P.Diag(Tok, diag::err_omp_map_type_missing);
1844 Data.MapType = isMapType(P);
1845 if (Data.MapType == OMPC_MAP_unknown)
1846 P.Diag(Tok, diag::err_omp_unknown_map_type);
1850 /// Parses clauses with list.
1851 bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1852 OpenMPClauseKind Kind,
1853 SmallVectorImpl<Expr *> &Vars,
1854 OpenMPVarListDataTy &Data) {
1855 UnqualifiedId UnqualifiedReductionId;
1856 bool InvalidReductionId = false;
1859 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1860 if (T.expectAndConsume(diag::err_expected_lparen_after,
1861 getOpenMPClauseName(Kind)))
1864 bool NeedRParenForLinear = false;
1865 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1866 tok::annot_pragma_openmp_end);
1867 // Handle reduction-identifier for reduction clause.
1868 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1869 Kind == OMPC_in_reduction) {
1870 ColonProtectionRAIIObject ColonRAII(*this);
1871 if (getLangOpts().CPlusPlus)
1872 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1873 /*ObjectType=*/nullptr,
1874 /*EnteringContext=*/false);
1875 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1876 UnqualifiedReductionId);
1877 if (InvalidReductionId) {
1878 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1881 if (Tok.is(tok::colon))
1882 Data.ColonLoc = ConsumeToken();
1884 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1885 if (!InvalidReductionId)
1887 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1888 } else if (Kind == OMPC_depend) {
1889 // Handle dependency type for depend clause.
1890 ColonProtectionRAIIObject ColonRAII(*this);
1892 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1893 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1894 Data.DepLinMapLoc = Tok.getLocation();
1896 if (Data.DepKind == OMPC_DEPEND_unknown) {
1897 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1901 // Special processing for depend(source) clause.
1902 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1908 if (Tok.is(tok::colon)) {
1909 Data.ColonLoc = ConsumeToken();
1911 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1912 : diag::warn_pragma_expected_colon)
1913 << "dependency type";
1915 } else if (Kind == OMPC_linear) {
1916 // Try to parse modifier if any.
1917 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1918 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1919 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1920 Data.DepLinMapLoc = ConsumeToken();
1921 LinearT.consumeOpen();
1922 NeedRParenForLinear = true;
1924 } else if (Kind == OMPC_map) {
1925 // Handle map type for map clause.
1926 ColonProtectionRAIIObject ColonRAII(*this);
1928 // The first identifier may be a list item, a map-type or a
1929 // map-type-modifier. The map-type can also be delete which has the same
1930 // spelling of the C++ delete keyword.
1931 Data.DepLinMapLoc = Tok.getLocation();
1933 // Check for presence of a colon in the map clause.
1934 TentativeParsingAction TPA(*this);
1935 bool ColonPresent = false;
1936 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1938 if (Tok.is(tok::colon))
1939 ColonPresent = true;
1942 // Only parse map-type-modifier[s] and map-type if a colon is present in
1945 parseMapTypeModifiers(*this, Data);
1946 parseMapType(*this, Data);
1948 if (Data.MapType == OMPC_MAP_unknown) {
1949 Data.MapType = OMPC_MAP_tofrom;
1950 Data.IsMapTypeImplicit = true;
1953 if (Tok.is(tok::colon))
1954 Data.ColonLoc = ConsumeToken();
1958 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1959 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1960 (Kind == OMPC_reduction && !InvalidReductionId) ||
1961 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
1962 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1963 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1964 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1965 Tok.isNot(tok::annot_pragma_openmp_end))) {
1966 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1968 ExprResult VarExpr =
1969 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1970 if (VarExpr.isUsable()) {
1971 Vars.push_back(VarExpr.get());
1973 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1977 IsComma = Tok.is(tok::comma);
1980 else if (Tok.isNot(tok::r_paren) &&
1981 Tok.isNot(tok::annot_pragma_openmp_end) &&
1982 (!MayHaveTail || Tok.isNot(tok::colon)))
1983 Diag(Tok, diag::err_omp_expected_punc)
1984 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1985 : getOpenMPClauseName(Kind))
1986 << (Kind == OMPC_flush);
1989 // Parse ')' for linear clause with modifier.
1990 if (NeedRParenForLinear)
1991 LinearT.consumeClose();
1993 // Parse ':' linear-step (or ':' alignment).
1994 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1996 Data.ColonLoc = Tok.getLocation();
1997 SourceLocation ELoc = ConsumeToken();
1998 ExprResult Tail = ParseAssignmentExpression();
1999 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
2000 if (Tail.isUsable())
2001 Data.TailExpr = Tail.get();
2003 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2008 Data.RLoc = Tok.getLocation();
2009 if (!T.consumeClose())
2010 Data.RLoc = T.getCloseLocation();
2011 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2013 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
2014 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
2017 /// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
2018 /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2022 /// 'private' '(' list ')'
2023 /// firstprivate-clause:
2024 /// 'firstprivate' '(' list ')'
2025 /// lastprivate-clause:
2026 /// 'lastprivate' '(' list ')'
2028 /// 'shared' '(' list ')'
2030 /// 'linear' '(' linear-list [ ':' linear-step ] ')'
2032 /// 'aligned' '(' list [ ':' alignment ] ')'
2033 /// reduction-clause:
2034 /// 'reduction' '(' reduction-identifier ':' list ')'
2035 /// task_reduction-clause:
2036 /// 'task_reduction' '(' reduction-identifier ':' list ')'
2037 /// in_reduction-clause:
2038 /// 'in_reduction' '(' reduction-identifier ':' list ')'
2039 /// copyprivate-clause:
2040 /// 'copyprivate' '(' list ')'
2042 /// 'flush' '(' list ')'
2044 /// 'depend' '(' in | out | inout : list | source ')'
2046 /// 'map' '(' [ [ always [,] ] [ close [,] ]
2047 /// to | from | tofrom | alloc | release | delete ':' ] list ')';
2049 /// 'to' '(' list ')'
2051 /// 'from' '(' list ')'
2052 /// use_device_ptr-clause:
2053 /// 'use_device_ptr' '(' list ')'
2054 /// is_device_ptr-clause:
2055 /// 'is_device_ptr' '(' list ')'
2057 /// For 'linear' clause linear-list may have the following forms:
2060 /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
2061 OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
2062 OpenMPClauseKind Kind,
2064 SourceLocation Loc = Tok.getLocation();
2065 SourceLocation LOpen = ConsumeToken();
2066 SmallVector<Expr *, 4> Vars;
2067 OpenMPVarListDataTy Data;
2069 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
2074 return Actions.ActOnOpenMPVarListClause(
2075 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
2076 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
2077 Data.MapTypeModifiers, Data.MapTypeModifiersLoc, Data.MapType,
2078 Data.IsMapTypeImplicit, Data.DepLinMapLoc);